6 Copyright (c) 1991-2000, Be Incorporated. All rights reserved.
8 Permission is hereby granted, free of charge, to any person obtaining a copy of
9 this software and associated documentation files (the "Software"), to deal in
10 the Software without restriction, including without limitation the rights to
11 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
12 of the Software, and to permit persons to whom the Software is furnished to do
13 so, subject to the following conditions:
15 The above copyright notice and this permission notice applies to all licensees
16 and shall be included in all copies or substantial portions of the Software.
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF TITLE, MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 BE INCORPORATED BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF, OR IN CONNECTION
23 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 Except as contained in this notice, the name of Be Incorporated shall not be
26 used in advertising or otherwise to promote the sale, use or other dealings in
27 this Software without prior written authorization from Be Incorporated.
29 Tracker(TM), Be(R), BeOS(R), and BeIA(TM) are trademarks or registered trademarks
30 of Be Incorporated in the United States and other countries. Other brand product
31 names are registered trademarks or trademarks of their respective holders.
43 #include <ByteOrder.h>
45 #include <Directory.h>
47 #include <GraphicsDefs.h>
50 #include <MessageFilter.h>
52 #include <NaturalCompare.h>
53 #include <ObjectList.h>
57 #include <StringView.h>
72 static const rgb_color kBlack
= {0, 0, 0, 255};
73 static const rgb_color kWhite
= {255, 255, 255 ,255};
75 const int64 kHalfKBSize
= 512;
76 const int64 kKBSize
= 1024;
77 const int64 kMBSize
= 1048576;
78 const int64 kGBSize
= 1073741824;
79 const int64 kTBSize
= kGBSize
* kKBSize
;
81 const int32 kMiniIconSeparator
= 3;
82 const int32 kLargeIconSeparator
= 6;
84 const color_space kDefaultIconDepth
= B_RGBA32
;
87 extern bool gLocalizedNamePreferred
;
90 // misc typedefs, constants and structs
92 class PeriodicUpdatePoses
{
93 // Periodically updated poses (ones with a volume space bar) register
94 // themselfs in this global list. This way they can be iterated over instead
95 // of sending around update messages.
97 PeriodicUpdatePoses();
98 ~PeriodicUpdatePoses();
100 typedef bool (*PeriodicUpdateCallback
)(BPose
* pose
, void* cookie
);
102 void AddPose(BPose
* pose
, BPoseView
* poseView
,
103 PeriodicUpdateCallback callback
, void* cookie
);
104 bool RemovePose(BPose
* pose
, void** cookie
);
106 void DoPeriodicUpdate(bool forceRedraw
);
109 struct periodic_pose
{
111 BPoseView
* pose_view
;
112 PeriodicUpdateCallback callback
;
117 BObjectList
<periodic_pose
> fPoseList
;
120 extern PeriodicUpdatePoses gPeriodicUpdatePoses
;
124 // PoseInfo is the structure that gets saved as attributes for every node
125 // on disk, defining the node's position and visibility
127 static void EndianSwap(void* castToThis
);
128 void PrintToStream();
131 ino_t fInitedDirectory
;
132 // For a location to be valid, fInitedDirectory has to contain
133 // the inode of the items parent directory. This makes it
134 // impossible to for instance zip up files and extract them in
135 // the same location. This should probably be reworked.
136 // Tracker could strip the file location attributes when dropping
137 // files into a closed folder.
142 class ExtendedPoseInfo
{
143 // extends PoseInfo adding workspace support; used for desktop
147 static size_t Size(int32
);
148 size_t SizeWithHeadroom() const;
149 static size_t SizeWithHeadroom(size_t);
150 bool HasLocationForFrame(BRect
) const;
151 BPoint
LocationForFrame(BRect
) const;
152 bool SetLocationForFrame(BPoint
, BRect
);
154 static void EndianSwap(void* castToThis
);
155 void PrintToStream();
159 bool fShowFromBootOnly
;
169 struct FrameLocation
{
175 FrameLocation fLocations
[0];
179 void DisallowMetaKeys(BTextView
*);
180 void DisallowFilenameKeys(BTextView
*);
183 bool ValidateStream(BMallocIO
*, uint32
, int32 version
);
186 bool SecondaryMouseButtonDown(int32 modifiers
, int32 buttons
);
187 uint32
HashString(const char* string
, uint32 seed
);
188 uint32
AttrHashString(const char* string
, uint32 type
);
191 class OffscreenBitmap
{
192 // a utility class for setting up offscreen bitmaps
194 OffscreenBitmap(BRect bounds
);
198 BView
* BeginUsing(BRect bounds
);
200 BBitmap
* Bitmap() const;
201 // blit this to your view when you are done rendering
203 // use this to render your image
206 void NewBitmap(BRect frame
);
212 extern void FadeRGBA32Horizontal(uint32
* bits
, int32 width
, int32 height
,
213 int32 from
, int32 to
);
214 extern void FadeRGBA32Vertical(uint32
* bits
, int32 width
, int32 height
,
215 int32 from
, int32 to
);
218 class FlickerFreeStringView
: public BStringView
{
219 // adds support for offscreen bitmap drawing for string views that update
220 // often this would be better implemented as an option of BStringView
222 FlickerFreeStringView(BRect bounds
, const char* name
,
223 const char* text
, uint32 resizingMode
= B_FOLLOW_LEFT
| B_FOLLOW_TOP
,
224 uint32 flags
= B_WILL_DRAW
);
225 FlickerFreeStringView(BRect bounds
, const char* name
,
226 const char* text
, BBitmap
* existingOffscreen
,
227 uint32 resizingMode
= B_FOLLOW_LEFT
| B_FOLLOW_TOP
,
228 uint32 flags
= B_WILL_DRAW
);
229 virtual ~FlickerFreeStringView();
230 virtual void Draw(BRect
);
231 virtual void AttachedToWindow();
232 virtual void SetViewColor(rgb_color
);
233 virtual void SetLowColor(rgb_color
);
236 OffscreenBitmap
* fBitmap
;
237 rgb_color fViewColor
;
239 BBitmap
* fOriginalBitmap
;
241 typedef BStringView _inherited
;
245 class DraggableIcon
: public BView
{
246 // used to determine a save location for a file
248 DraggableIcon(BRect rect
, const char* name
, const char* mimeType
,
249 icon_size which
, const BMessage
* message
, BMessenger target
,
250 uint32 resizingMode
= B_FOLLOW_LEFT
| B_FOLLOW_TOP
,
251 uint32 flags
= B_WILL_DRAW
);
252 virtual ~DraggableIcon();
254 static BRect
PreferredRect(BPoint offset
, icon_size which
);
255 void SetTarget(BMessenger
);
258 virtual void AttachedToWindow();
259 virtual void MouseDown(BPoint
);
260 virtual void Draw(BRect
);
262 virtual bool DragStarted(BMessage
* dragMessage
);
271 class PositionPassingMenuItem
: public BMenuItem
{
273 PositionPassingMenuItem(const char* title
, BMessage
*,
274 char shortcut
= 0, uint32 modifiers
= 0);
275 PositionPassingMenuItem(BMenu
*, BMessage
*);
276 PositionPassingMenuItem(BMessage
* data
);
278 static BArchivable
* Instantiate(BMessage
* data
);
281 virtual status_t
Invoke(BMessage
* = 0);
282 // appends the invoke location for NewFolder, etc. to use
285 typedef BMenuItem _inherited
;
292 Benaphore(const char* name
= "Light Lock")
293 : fSemaphore(create_sem(0, name
)),
300 delete_sem(fSemaphore
);
305 if (atomic_add(&fCount
, -1) <= 0)
306 return acquire_sem(fSemaphore
) == B_OK
;
313 if (atomic_add(&fCount
, 1) < 0)
314 release_sem(fSemaphore
);
317 bool IsLocked() const
328 class SeparatorLine
: public BView
{
330 SeparatorLine(BPoint
, float, bool vertical
, const char* name
= "");
331 virtual void Draw(BRect bounds
);
335 class TitledSeparatorItem
: public BMenuItem
{
337 TitledSeparatorItem(const char*);
338 virtual ~TitledSeparatorItem();
340 virtual void SetEnabled(bool state
);
343 virtual void GetContentSize(float* width
, float* height
);
347 typedef BMenuItem _inherited
;
351 class LooperAutoLocker
{
353 LooperAutoLocker(BHandler
* handler
)
355 fHasLock(handler
->LockLooper())
362 fHandler
->UnlockLooper();
365 bool operator!() const
370 bool IsLocked() const
381 class ShortcutFilter
: public BMessageFilter
{
383 ShortcutFilter(uint32 shortcutKey
, uint32 shortcutModifier
,
384 uint32 shortcutWhat
, BHandler
* target
);
387 filter_result
Filter(BMessage
*, BHandler
**);
391 uint32 fShortcutModifier
;
392 uint32 fShortcutWhat
;
397 // iterates over all the refs in a message
398 entry_ref
* EachEntryRef(BMessage
*, entry_ref
* (*)(entry_ref
*, void*),
400 const entry_ref
* EachEntryRef(const BMessage
*,
401 const entry_ref
* (*)(const entry_ref
*, void*), void* passThru
= 0);
403 entry_ref
* EachEntryRef(BMessage
*, entry_ref
* (*)(entry_ref
*, void*),
404 void* passThru
, int32 maxCount
);
405 const entry_ref
* EachEntryRef(const BMessage
*,
406 const entry_ref
* (*)(const entry_ref
*, void*), void* passThru
,
410 bool ContainsEntryRef(const BMessage
*, const entry_ref
*);
411 int32
CountRefs(const BMessage
*);
413 BMenuItem
* EachMenuItem(BMenu
* menu
, bool recursive
,
414 BMenuItem
* (*func
)(BMenuItem
*));
415 const BMenuItem
* EachMenuItem(const BMenu
* menu
, bool recursive
,
416 BMenuItem
* (*func
)(const BMenuItem
*));
418 int64
StringToScalar(const char* text
);
419 // string to num, understands kB, MB, etc.
422 void EmbedUniqueVolumeInfo(BMessage
* message
, const BVolume
* volume
);
423 status_t
MatchArchivedVolume(BVolume
* volume
, const BMessage
* message
,
425 void TruncateLeaf(BString
* string
);
427 void StringFromStream(BString
*, BMallocIO
*, bool endianSwap
= false);
428 void StringToStream(const BString
*, BMallocIO
*);
429 int32
ArchiveSize(const BString
*);
431 extern void EnableNamedMenuItem(BMenu
* menu
, const char* itemName
, bool on
);
432 extern void MarkNamedMenuItem(BMenu
* menu
, const char* itemName
, bool on
);
433 extern void EnableNamedMenuItem(BMenu
* menu
, uint32 commandName
, bool on
);
434 extern void MarkNamedMenuItem(BMenu
* menu
, uint32 commandName
, bool on
);
435 extern void DeleteSubmenu(BMenuItem
* submenuItem
);
437 extern bool BootedInSafeMode();
441 Color(int32 r
, int32 g
, int32 b
, int32 alpha
= 255)
444 result
.red
= (uchar
)r
;
445 result
.green
= (uchar
)g
;
446 result
.blue
= (uchar
)b
;
447 result
.alpha
= (uchar
)alpha
;
452 void PrintToStream(rgb_color color
);
454 template <class InitCheckable
>
456 ThrowOnInitCheckError(InitCheckable
* item
)
459 throw (status_t
)B_ERROR
;
461 status_t result
= item
->InitCheck();
463 throw (status_t
)result
;
467 # define ThrowOnError(x) _ThrowOnError(x, __FILE__, __LINE__)
468 # define ThrowIfNotSize(x) _ThrowIfNotSize(x, __FILE__, __LINE__)
469 # define ThrowOnAssert(x) _ThrowOnAssert(x, __FILE__, __LINE__)
471 # define ThrowOnError(x) _ThrowOnError(x, NULL, 0)
472 # define ThrowIfNotSize(x) _ThrowIfNotSize(x, NULL, 0)
473 # define ThrowOnAssert(x) _ThrowOnAssert(x, NULL, 0)
476 void _ThrowOnError(status_t
, const char*, int32
);
477 void _ThrowIfNotSize(ssize_t
, const char*, int32
);
478 void _ThrowOnAssert(bool, const char*, int32
);
480 // stub calls that work around BAppFile info inefficiency
481 status_t
GetAppSignatureFromAttr(BFile
*, char*);
482 status_t
GetAppIconFromAttr(BFile
* file
, BBitmap
* icon
, icon_size which
);
483 status_t
GetFileIconFromAttr(BNode
* node
, BBitmap
* icon
, icon_size which
);
486 void HexDump(const void* buffer
, int32 length
);
491 PrintRefToStream(const entry_ref
* ref
, const char* trailer
= "\n")
494 PRINT(("NULL entry_ref%s", trailer
));
500 entry
.GetPath(&path
);
501 PRINT(("%s%s", path
.Path(), trailer
));
506 PrintEntryToStream(const BEntry
* entry
, const char* trailer
= "\n")
509 PRINT(("NULL entry%s", trailer
));
514 entry
->GetPath(&path
);
515 PRINT(("%s%s", path
.Path(), trailer
));
520 PrintDirToStream(const BDirectory
* dir
, const char* trailer
= "\n")
523 PRINT(("NULL entry_ref%s", trailer
));
529 dir
->GetEntry(&entry
);
530 entry
.GetPath(&path
);
531 PRINT(("%s%s", path
.Path(), trailer
));
536 inline void PrintRefToStream(const entry_ref
*, const char* = 0) {}
537 inline void PrintEntryToStream(const BEntry
*, const char* = 0) {}
538 inline void PrintDirToStream(const BDirectory
*, const char* = 0) {}
544 extern FILE* logFile
;
546 inline void PrintToLogFile(const char* format
, ...)
550 vfprintf(logFile
, fmt
, ap
);
554 #define WRITELOG(_ARGS_) \
556 logFile = fopen("/var/log/tracker.log", "a+"); \
558 if (logFile != 0) { \
560 get_thread_info(find_thread(NULL), &info); \
561 PrintToLogFile("[t %Ld] \"%s\" (%s:%i) ", system_time(), \
562 info.name, __FILE__, __LINE__); \
563 PrintToLogFile _ARGS_; \
564 PrintToLogFile("\n"); \
570 #define WRITELOG(_ARGS_)
574 // fancy casting macros
576 template <typename NewType
, typename OldType
>
577 inline NewType
assert_cast(OldType castedPointer
) {
578 ASSERT(dynamic_cast<NewType
>(castedPointer
) != NULL
);
579 return static_cast<NewType
>(castedPointer
);
582 // B_SWAP_INT32 have broken signedness, simple cover calls to fix that
583 // should fix up in ByteOrder.h
585 inline int32
SwapInt32(int32 value
)
586 { return (int32
)B_SWAP_INT32((uint32
)value
); }
587 inline uint32
SwapUInt32(uint32 value
) { return B_SWAP_INT32(value
); }
588 inline int64
SwapInt64(int64 value
)
589 { return (int64
)B_SWAP_INT64((uint64
)value
); }
590 inline uint64
SwapUInt64(uint64 value
) { return B_SWAP_INT64(value
); }
593 extern const float kExactMatchScore
;
594 float ComputeTypeAheadScore(const char* text
, const char* match
,
595 bool wordMode
= false);
597 } // namespace BPrivate
600 #endif // _UTILITIES_H