102be5353SAxel Dörfler /*
202be5353SAxel Dörfler Open Tracker License
302be5353SAxel Dörfler
402be5353SAxel Dörfler Terms and Conditions
502be5353SAxel Dörfler
602be5353SAxel Dörfler Copyright (c) 1991-2000, Be Incorporated. All rights reserved.
702be5353SAxel Dörfler
802be5353SAxel Dörfler Permission is hereby granted, free of charge, to any person obtaining a copy of
902be5353SAxel Dörfler this software and associated documentation files (the "Software"), to deal in
1002be5353SAxel Dörfler the Software without restriction, including without limitation the rights to
1102be5353SAxel Dörfler use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
1202be5353SAxel Dörfler of the Software, and to permit persons to whom the Software is furnished to do
1302be5353SAxel Dörfler so, subject to the following conditions:
1402be5353SAxel Dörfler
1502be5353SAxel Dörfler The above copyright notice and this permission notice applies to all licensees
1602be5353SAxel Dörfler and shall be included in all copies or substantial portions of the Software.
1702be5353SAxel Dörfler
1802be5353SAxel Dörfler THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1902be5353SAxel Dörfler IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF TITLE, MERCHANTABILITY,
2002be5353SAxel Dörfler FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
2102be5353SAxel Dörfler BE INCORPORATED BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
2202be5353SAxel Dörfler AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF, OR IN CONNECTION
2302be5353SAxel Dörfler WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2402be5353SAxel Dörfler
2502be5353SAxel Dörfler Except as contained in this notice, the name of Be Incorporated shall not be
2602be5353SAxel Dörfler used in advertising or otherwise to promote the sale, use or other dealings in
2702be5353SAxel Dörfler this Software without prior written authorization from Be Incorporated.
2802be5353SAxel Dörfler
2902be5353SAxel Dörfler Tracker(TM), Be(R), BeOS(R), and BeIA(TM) are trademarks or registered trademarks
3002be5353SAxel Dörfler of Be Incorporated in the United States and other countries. Other brand product
3102be5353SAxel Dörfler names are registered trademarks or trademarks of their respective holders.
3202be5353SAxel Dörfler All rights reserved.
3302be5353SAxel Dörfler */
34f2ed4761SJohn Scipione #ifndef _NU_ICON_CACHE_H
35f2ed4761SJohn Scipione #define _NU_ICON_CACHE_H
36b05aa8b5SJohn Scipione
3702be5353SAxel Dörfler
3802be5353SAxel Dörfler // Icon cache is used for drawing node icons; it caches icons
3902be5353SAxel Dörfler // and reuses them for successive draws
4002be5353SAxel Dörfler
4102be5353SAxel Dörfler
4202be5353SAxel Dörfler #include <Bitmap.h>
43f2ed4761SJohn Scipione #include <ObjectList.h>
4402be5353SAxel Dörfler #include <Mime.h>
4502be5353SAxel Dörfler #include <String.h>
4602be5353SAxel Dörfler
4702be5353SAxel Dörfler #include "AutoLock.h"
488eaf4427SAugustin Cavalier #include "HashSet.h"
4902be5353SAxel Dörfler #include "Utilities.h"
5002be5353SAxel Dörfler
51b05aa8b5SJohn Scipione
52a51764c5SJohn Scipione // Icon cache splits icons into two caches - the shared cache, likely to
53a51764c5SJohn Scipione // get the most hits and the node cache. Every icon that is found in a
54a51764c5SJohn Scipione // mime-based structure goes into the shared cache, only files that have
55a51764c5SJohn Scipione // their own private icon use the node cache;
56a51764c5SJohn Scipione // Entries are only deleted from the shared cache if an icon for a mime type
57a51764c5SJohn Scipione // changes, this makes async icon drawing easier. Node cache deletes it's
58a51764c5SJohn Scipione // entries whenever a file gets deleted.
5902be5353SAxel Dörfler
6002be5353SAxel Dörfler // if a view ever uses the cache to draw in async mode, it needs to call
6102be5353SAxel Dörfler // it when it is being destroyed
6202be5353SAxel Dörfler
6302be5353SAxel Dörfler namespace BPrivate {
6402be5353SAxel Dörfler
6502be5353SAxel Dörfler class Model;
6602be5353SAxel Dörfler class ModelNodeLazyOpener;
6702be5353SAxel Dörfler class LazyBitmapAllocator;
6802be5353SAxel Dörfler class SharedIconCache;
6902be5353SAxel Dörfler class SharedCacheEntry;
70a64cdd1dSAugustin Cavalier class GenerateThumbnailJob;
7102be5353SAxel Dörfler
7202be5353SAxel Dörfler enum IconDrawMode {
739ecf9d1cSIngo Weinhold // Different states of icon drawing
7402be5353SAxel Dörfler kSelected = 0x01,
7502be5353SAxel Dörfler kNotFocused = 0x02, // Tracker window
7602be5353SAxel Dörfler kOpen = 0x04, // open folder, trash
7702be5353SAxel Dörfler kNotEmpty = 0x08, // full trash
7802be5353SAxel Dörfler kDisabled = 0x10, // inactive nav menu entry
7902be5353SAxel Dörfler kActive = 0x20, // active home dir, boot volume
8002be5353SAxel Dörfler kLink = 0x40, // symbolic link
8102be5353SAxel Dörfler kTrackerSpecialized = 0x80,
8202be5353SAxel Dörfler
8302be5353SAxel Dörfler // some common combinations
8402be5353SAxel Dörfler kNormalIcon = 0,
8502be5353SAxel Dörfler kSelectedIcon = kSelected,
8602be5353SAxel Dörfler kSelectedInBackgroundIcon = kSelected | kNotFocused,
8702be5353SAxel Dörfler kOpenIcon = kOpen,
8802be5353SAxel Dörfler kOpenSelectedIcon = kSelected | kOpen,
8902be5353SAxel Dörfler kOpenSelectedInBackgroundIcon = kSelected | kNotFocused | kOpen,
9002be5353SAxel Dörfler kFullIcon = kNotEmpty,
9102be5353SAxel Dörfler kFullSelectedIcon = kNotEmpty | kOpen,
9202be5353SAxel Dörfler kDimmedIcon
9302be5353SAxel Dörfler };
9402be5353SAxel Dörfler
95b05aa8b5SJohn Scipione
9602be5353SAxel Dörfler #define NORMAL_ICON_ONLY kNormalIcon
9702be5353SAxel Dörfler // replace use of these defines with mode once the respective getters
9802be5353SAxel Dörfler // can get non-plain icons
9902be5353SAxel Dörfler
10002be5353SAxel Dörfler
10102be5353SAxel Dörfler // Where did an icon come from
10202be5353SAxel Dörfler enum IconSource {
10302be5353SAxel Dörfler kUnknownSource,
104a51764c5SJohn Scipione kUnknownNotFromNode, // icon origin not known but determined not
105a51764c5SJohn Scipione // to be from the node itself
106a51764c5SJohn Scipione kTrackerDefault, // file has no type, Tracker provides generic,
107a51764c5SJohn Scipione // folder, symlink or app
10802be5353SAxel Dörfler kTrackerSupplied, // home directory, boot volume, trash, etc.
10902be5353SAxel Dörfler kMetaMime, // from BMimeType
110a51764c5SJohn Scipione kPreferredAppForType, // have a preferred application for a type,
111a51764c5SJohn Scipione // has an icon
11202be5353SAxel Dörfler kPreferredAppForNode, // have a preferred application for this node,
11302be5353SAxel Dörfler // has an icon
11402be5353SAxel Dörfler kVolume,
11502be5353SAxel Dörfler kNode
11602be5353SAxel Dörfler };
11702be5353SAxel Dörfler
118b05aa8b5SJohn Scipione
1198eaf4427SAugustin Cavalier template<typename Class>
1208eaf4427SAugustin Cavalier struct SelfHashing {
1218eaf4427SAugustin Cavalier typedef typename Class::HashKeyType KeyType;
1228eaf4427SAugustin Cavalier typedef Class ValueType;
1238eaf4427SAugustin Cavalier
HashKeySelfHashing1248eaf4427SAugustin Cavalier size_t HashKey(KeyType key) const
1258eaf4427SAugustin Cavalier {
1268eaf4427SAugustin Cavalier return Class::Hash(key);
1278eaf4427SAugustin Cavalier }
1288eaf4427SAugustin Cavalier
HashSelfHashing1298eaf4427SAugustin Cavalier size_t Hash(ValueType* value) const
1308eaf4427SAugustin Cavalier {
1318eaf4427SAugustin Cavalier return value->Hash();
1328eaf4427SAugustin Cavalier }
1338eaf4427SAugustin Cavalier
CompareSelfHashing1348eaf4427SAugustin Cavalier bool Compare(KeyType key, ValueType* value) const
1358eaf4427SAugustin Cavalier {
1368eaf4427SAugustin Cavalier return *value == key;
1378eaf4427SAugustin Cavalier }
1388eaf4427SAugustin Cavalier
GetLinkSelfHashing1398eaf4427SAugustin Cavalier ValueType*& GetLink(ValueType* value) const
1408eaf4427SAugustin Cavalier {
1418eaf4427SAugustin Cavalier return value->HashNext();
1428eaf4427SAugustin Cavalier }
1438eaf4427SAugustin Cavalier };
1448eaf4427SAugustin Cavalier
1458eaf4427SAugustin Cavalier
14602be5353SAxel Dörfler class IconCacheEntry {
14702be5353SAxel Dörfler // aliased entries don't own their icons, just point
14802be5353SAxel Dörfler // to some other entry that does
14902be5353SAxel Dörfler
15002be5353SAxel Dörfler // This is used for icons that are defined by a preferred app for
1519ecf9d1cSIngo Weinhold // a metamime, types that do not have an icon get to point to
15202be5353SAxel Dörfler // generic, etc.
15302be5353SAxel Dörfler
15402be5353SAxel Dörfler public:
15502be5353SAxel Dörfler IconCacheEntry();
15602be5353SAxel Dörfler ~IconCacheEntry();
15702be5353SAxel Dörfler
158ae7d51b2SJohn Scipione void SetAliasFor(const SharedIconCache* sharedCache,
159ae7d51b2SJohn Scipione const SharedCacheEntry* entry);
160ae7d51b2SJohn Scipione static IconCacheEntry* ResolveIfAlias(const SharedIconCache* sharedCache,
161ae7d51b2SJohn Scipione IconCacheEntry* entry);
162ae7d51b2SJohn Scipione IconCacheEntry* ResolveIfAlias(const SharedIconCache* sharedCache);
16302be5353SAxel Dörfler
164*0c973c94SAugustin Cavalier void SetIcon(BBitmap* bitmap, IconDrawMode mode, BSize size);
16502be5353SAxel Dörfler
166*0c973c94SAugustin Cavalier bool HaveIconBitmap(IconDrawMode mode, BSize size) const;
167*0c973c94SAugustin Cavalier bool CanConstructBitmap(IconDrawMode mode, BSize size) const;
16802be5353SAxel Dörfler static bool AlternateModeForIconConstructing(IconDrawMode requestedMode,
169*0c973c94SAugustin Cavalier IconDrawMode &alternate, BSize size);
170a51764c5SJohn Scipione BBitmap* ConstructBitmap(BBitmap* constructFrom,
171a51764c5SJohn Scipione IconDrawMode requestedMode, IconDrawMode constructFromMode,
172*0c973c94SAugustin Cavalier BSize size, LazyBitmapAllocator*);
173*0c973c94SAugustin Cavalier BBitmap* ConstructBitmap(IconDrawMode requestedMode, BSize size,
17402be5353SAxel Dörfler LazyBitmapAllocator*);
17502be5353SAxel Dörfler // same as above, always uses normal icon as source
17602be5353SAxel Dörfler
177*0c973c94SAugustin Cavalier bool IconHitTest(BPoint, IconDrawMode, BSize) const;
17802be5353SAxel Dörfler // given a point, returns true if a non-transparent pixel was hit
17902be5353SAxel Dörfler
18002be5353SAxel Dörfler void RetireIcons(BObjectList<BBitmap>* retiredBitmapList);
18102be5353SAxel Dörfler // can't just delete icons, they may be still drawing
18202be5353SAxel Dörfler // async; instead, put them on the retired list and
18302be5353SAxel Dörfler // only delete the list if it grows too much, way after
18402be5353SAxel Dörfler // the icon finishes drawing
18502be5353SAxel Dörfler //
18602be5353SAxel Dörfler // This could fail if we retire a lot of icons (10 * 1024)
18702be5353SAxel Dörfler // while we are drawing them, shouldn't be a practical problem
18802be5353SAxel Dörfler
18902be5353SAxel Dörfler protected:
190*0c973c94SAugustin Cavalier BBitmap* IconForMode(IconDrawMode mode, BSize size) const;
191*0c973c94SAugustin Cavalier void SetIconForMode(BBitmap* bitmap, IconDrawMode mode, BSize size);
19202be5353SAxel Dörfler
19302be5353SAxel Dörfler // list of most common icons
19402be5353SAxel Dörfler BBitmap* fLargeIcon;
195c6433b0bSJohn Scipione BBitmap* fHighlightedLargeIcon;
19602be5353SAxel Dörfler BBitmap* fMiniIcon;
197c6433b0bSJohn Scipione BBitmap* fHighlightedMiniIcon;
1988eaf4427SAugustin Cavalier
1998eaf4427SAugustin Cavalier const IconCacheEntry* fAliasTo;
20002be5353SAxel Dörfler
20102be5353SAxel Dörfler // list of other icon kinds would be added here
20202be5353SAxel Dörfler
20302be5353SAxel Dörfler friend class SharedIconCache;
20402be5353SAxel Dörfler friend class NodeIconCache;
20502be5353SAxel Dörfler };
20602be5353SAxel Dörfler
207b05aa8b5SJohn Scipione
20802be5353SAxel Dörfler class SimpleIconCache {
20902be5353SAxel Dörfler public:
21002be5353SAxel Dörfler SimpleIconCache(const char*);
~SimpleIconCache()21102be5353SAxel Dörfler virtual ~SimpleIconCache() {}
21202be5353SAxel Dörfler
21302be5353SAxel Dörfler virtual void Draw(IconCacheEntry*, BView*, BPoint, IconDrawMode mode,
214*0c973c94SAugustin Cavalier BSize size, bool async = false) = 0;
21502be5353SAxel Dörfler virtual void Draw(IconCacheEntry*, BView*, BPoint, IconDrawMode,
216*0c973c94SAugustin Cavalier BSize, void (*)(BView*, BPoint, BBitmap*, void*),
217b05aa8b5SJohn Scipione void* = NULL) = 0;
21802be5353SAxel Dörfler
21902be5353SAxel Dörfler bool Lock();
22002be5353SAxel Dörfler void Unlock();
22102be5353SAxel Dörfler bool IsLocked() const;
22202be5353SAxel Dörfler
22302be5353SAxel Dörfler private:
22402be5353SAxel Dörfler Benaphore fLock;
22502be5353SAxel Dörfler };
22602be5353SAxel Dörfler
227b05aa8b5SJohn Scipione
22802be5353SAxel Dörfler class SharedCacheEntry : public IconCacheEntry {
22902be5353SAxel Dörfler public:
23002be5353SAxel Dörfler SharedCacheEntry();
23102be5353SAxel Dörfler SharedCacheEntry(const char* fileType, const char* appSignature = 0);
23202be5353SAxel Dörfler
233*0c973c94SAugustin Cavalier void Draw(BView*, BPoint, IconDrawMode mode, BSize size,
23402be5353SAxel Dörfler bool async = false);
23502be5353SAxel Dörfler
236*0c973c94SAugustin Cavalier void Draw(BView*, BPoint, IconDrawMode, BSize,
23702be5353SAxel Dörfler void (*)(BView*, BPoint, BBitmap*, void*), void* = NULL);
23802be5353SAxel Dörfler
23902be5353SAxel Dörfler const char* FileType() const;
24002be5353SAxel Dörfler const char* AppSignature() const;
24102be5353SAxel Dörfler
2428eaf4427SAugustin Cavalier public:
24302be5353SAxel Dörfler // hash table support
2448eaf4427SAugustin Cavalier struct TypeAndSignature {
2458eaf4427SAugustin Cavalier const char* type, *signature;
TypeAndSignatureTypeAndSignature2468eaf4427SAugustin Cavalier TypeAndSignature(const char* t, const char* s)
2478eaf4427SAugustin Cavalier : type(t), signature(s) {}
2488eaf4427SAugustin Cavalier };
2498eaf4427SAugustin Cavalier typedef TypeAndSignature HashKeyType;
2508eaf4427SAugustin Cavalier static size_t Hash(const TypeAndSignature& typeAndSignature);
25102be5353SAxel Dörfler
2528eaf4427SAugustin Cavalier size_t Hash() const;
HashNext()2538eaf4427SAugustin Cavalier SharedCacheEntry*& HashNext() { return fNext; }
2548eaf4427SAugustin Cavalier bool operator==(const TypeAndSignature& typeAndSignature) const;
2558eaf4427SAugustin Cavalier
25602be5353SAxel Dörfler private:
2578eaf4427SAugustin Cavalier SharedCacheEntry* fNext;
2588eaf4427SAugustin Cavalier
25902be5353SAxel Dörfler BString fFileType;
26002be5353SAxel Dörfler BString fAppSignature;
26102be5353SAxel Dörfler
26202be5353SAxel Dörfler friend class SharedIconCache;
26302be5353SAxel Dörfler };
26402be5353SAxel Dörfler
265b05aa8b5SJohn Scipione
26602be5353SAxel Dörfler class SharedIconCache : public SimpleIconCache {
26702be5353SAxel Dörfler // SharedIconCache is used for icons that come from the mime database
26802be5353SAxel Dörfler public:
26902be5353SAxel Dörfler SharedIconCache();
27002be5353SAxel Dörfler
27102be5353SAxel Dörfler virtual void Draw(IconCacheEntry*, BView*, BPoint, IconDrawMode mode,
272*0c973c94SAugustin Cavalier BSize size, bool async = false);
27302be5353SAxel Dörfler virtual void Draw(IconCacheEntry*, BView*, BPoint, IconDrawMode,
274*0c973c94SAugustin Cavalier BSize, void (*)(BView*, BPoint, BBitmap*, void*), void* = NULL);
27502be5353SAxel Dörfler
276a51764c5SJohn Scipione SharedCacheEntry* FindItem(const char* fileType,
277a51764c5SJohn Scipione const char* appSignature = 0) const;
278a51764c5SJohn Scipione SharedCacheEntry* AddItem(const char* fileType,
27902be5353SAxel Dörfler const char* appSignature = 0);
28002be5353SAxel Dörfler void IconChanged(SharedCacheEntry*);
28102be5353SAxel Dörfler
282ae7d51b2SJohn Scipione void SetAliasFor(IconCacheEntry* entry,
283a51764c5SJohn Scipione const SharedCacheEntry* original) const;
28402be5353SAxel Dörfler IconCacheEntry* ResolveIfAlias(IconCacheEntry* entry) const;
28502be5353SAxel Dörfler
2868eaf4427SAugustin Cavalier void RemoveAliasesTo(SharedCacheEntry* alias);
28702be5353SAxel Dörfler
28802be5353SAxel Dörfler private:
2898eaf4427SAugustin Cavalier typedef BOpenHashTable<SelfHashing<SharedCacheEntry> > EntryHashTable;
2908eaf4427SAugustin Cavalier EntryHashTable fHashTable;
2918eaf4427SAugustin Cavalier
29202be5353SAxel Dörfler BObjectList<BBitmap> fRetiredBitmaps;
293a51764c5SJohn Scipione // icons are drawn asynchronously, can't just delete them right away,
294a51764c5SJohn Scipione // instead have to place them onto the retired bitmap list and wait
295a51764c5SJohn Scipione // for the next sync to delete them
29602be5353SAxel Dörfler };
29702be5353SAxel Dörfler
298b05aa8b5SJohn Scipione
29902be5353SAxel Dörfler class NodeCacheEntry : public IconCacheEntry {
30002be5353SAxel Dörfler public:
30102be5353SAxel Dörfler NodeCacheEntry(bool permanent = false);
30202be5353SAxel Dörfler NodeCacheEntry(const node_ref*, bool permanent = false);
303*0c973c94SAugustin Cavalier void Draw(BView*, BPoint, IconDrawMode mode, BSize size,
30402be5353SAxel Dörfler bool async = false);
30502be5353SAxel Dörfler
306*0c973c94SAugustin Cavalier void Draw(BView*, BPoint, IconDrawMode, BSize,
30702be5353SAxel Dörfler void (*)(BView*, BPoint, BBitmap*, void*), void* = NULL);
30802be5353SAxel Dörfler
30902be5353SAxel Dörfler const node_ref* Node() const;
31002be5353SAxel Dörfler
31102be5353SAxel Dörfler bool Permanent() const;
31202be5353SAxel Dörfler
3138eaf4427SAugustin Cavalier public:
3148eaf4427SAugustin Cavalier // hash table support
3158eaf4427SAugustin Cavalier typedef const node_ref* HashKeyType;
3168eaf4427SAugustin Cavalier static size_t Hash(const node_ref* node);
3178eaf4427SAugustin Cavalier
3188eaf4427SAugustin Cavalier size_t Hash() const;
HashNext()3198eaf4427SAugustin Cavalier NodeCacheEntry*& HashNext() { return fNext; }
3208eaf4427SAugustin Cavalier bool operator==(const node_ref* ref) const;
3218eaf4427SAugustin Cavalier
32202be5353SAxel Dörfler private:
3238eaf4427SAugustin Cavalier NodeCacheEntry* fNext;
3248eaf4427SAugustin Cavalier
32502be5353SAxel Dörfler node_ref fRef;
32602be5353SAxel Dörfler bool fPermanent;
32702be5353SAxel Dörfler // special cache entry that has to be deleted explicitly
32802be5353SAxel Dörfler
32902be5353SAxel Dörfler friend class NodeIconCache;
33002be5353SAxel Dörfler };
33102be5353SAxel Dörfler
332b05aa8b5SJohn Scipione
33302be5353SAxel Dörfler class NodeIconCache : public SimpleIconCache {
3349ecf9d1cSIngo Weinhold // NodeIconCache is used for nodes that define their own icons
33502be5353SAxel Dörfler public:
33602be5353SAxel Dörfler NodeIconCache();
33702be5353SAxel Dörfler
33802be5353SAxel Dörfler virtual void Draw(IconCacheEntry*, BView*, BPoint, IconDrawMode,
339*0c973c94SAugustin Cavalier BSize, bool async = false);
34002be5353SAxel Dörfler
34102be5353SAxel Dörfler virtual void Draw(IconCacheEntry*, BView*, BPoint, IconDrawMode,
342*0c973c94SAugustin Cavalier BSize, void (*)(BView*, BPoint, BBitmap*, void*), void* = 0);
34302be5353SAxel Dörfler
34402be5353SAxel Dörfler NodeCacheEntry* FindItem(const node_ref*) const;
34502be5353SAxel Dörfler NodeCacheEntry* AddItem(const node_ref*, bool permanent = false);
34602be5353SAxel Dörfler void Deleting(const node_ref*);
347a51764c5SJohn Scipione // model for this node is getting deleted
348a51764c5SJohn Scipione // (not necessarily the node itself)
34902be5353SAxel Dörfler void Removing(const node_ref*);
3509ecf9d1cSIngo Weinhold // used by permanent NodeIconCache entries, when an entry gets deleted
35102be5353SAxel Dörfler void Deleting(const BView*);
35202be5353SAxel Dörfler void IconChanged(const Model*);
35302be5353SAxel Dörfler
3548eaf4427SAugustin Cavalier void RemoveAliasesTo(SharedCacheEntry* alias);
35502be5353SAxel Dörfler
35602be5353SAxel Dörfler private:
3578eaf4427SAugustin Cavalier typedef BOpenHashTable<SelfHashing<NodeCacheEntry> > EntryHashTable;
3588eaf4427SAugustin Cavalier EntryHashTable fHashTable;
35902be5353SAxel Dörfler };
36002be5353SAxel Dörfler
361b05aa8b5SJohn Scipione
36202be5353SAxel Dörfler const int32 kColorTransformTableSize = 256;
36302be5353SAxel Dörfler
364b05aa8b5SJohn Scipione
36502be5353SAxel Dörfler class IconCache {
36602be5353SAxel Dörfler public:
36702be5353SAxel Dörfler IconCache();
36802be5353SAxel Dörfler
36902be5353SAxel Dörfler void Draw(Model*, BView*, BPoint where, IconDrawMode mode,
370*0c973c94SAugustin Cavalier BSize size, bool async = false);
37102be5353SAxel Dörfler // draw an icon for a model, load the icon from the appropriate
37202be5353SAxel Dörfler // location if not cached already
37302be5353SAxel Dörfler
37402be5353SAxel Dörfler void SyncDraw(Model*, BView*, BPoint, IconDrawMode,
375*0c973c94SAugustin Cavalier BSize, void (*)(BView*, BPoint, BBitmap*, void*),
37602be5353SAxel Dörfler void* passThruState = 0);
37702be5353SAxel Dörfler // draw an icon for a model, load the icon from the appropriate
37802be5353SAxel Dörfler // location if not cached already; only works for sync draws,
37902be5353SAxel Dörfler // once the call returns, the bitmap may be deleted
38002be5353SAxel Dörfler
38102be5353SAxel Dörfler // preload calls used to ensure successive cache hit for the respective
38202be5353SAxel Dörfler // icon, used for common tracker types, etc; Not calling these should only
38302be5353SAxel Dörfler // cause a slowdown
384*0c973c94SAugustin Cavalier void Preload(Model*, IconDrawMode mode, BSize size,
385b05aa8b5SJohn Scipione bool permanent = false);
386*0c973c94SAugustin Cavalier status_t Preload(const char* mimeType, IconDrawMode mode, BSize size);
38702be5353SAxel Dörfler
38802be5353SAxel Dörfler void Deleting(const Model*);
38902be5353SAxel Dörfler // hook to manage unloading icons for nodes that are going away
39002be5353SAxel Dörfler void Removing(const Model* model);
39102be5353SAxel Dörfler // used by permanent NodeIconCache entries, when an entry gets
39202be5353SAxel Dörfler // deleted
39302be5353SAxel Dörfler void Deleting(const BView*);
39402be5353SAxel Dörfler // hook to manage deleting draw view caches for views that are
39502be5353SAxel Dörfler // going away
39602be5353SAxel Dörfler
39702be5353SAxel Dörfler // icon changed calls, used when a node or a file type has an icon changed
39802be5353SAxel Dörfler // the icons for the node/file type will be flushed and re-cached during
39902be5353SAxel Dörfler // the next draw
40002be5353SAxel Dörfler void IconChanged(Model*);
40102be5353SAxel Dörfler void IconChanged(const char* mimeType, const char* appSignature);
40202be5353SAxel Dörfler
40302be5353SAxel Dörfler bool IsIconFrom(const Model*, const char* mimeType,
40402be5353SAxel Dörfler const char* appSignature) const;
40502be5353SAxel Dörfler // called when metamime database changed to figure out which models
40602be5353SAxel Dörfler // to redraw
40702be5353SAxel Dörfler
408*0c973c94SAugustin Cavalier bool IconHitTest(BPoint, const Model*, IconDrawMode, BSize);
40902be5353SAxel Dörfler
41002be5353SAxel Dörfler // utility calls for building specialized icons
411*0c973c94SAugustin Cavalier BBitmap* MakeSelectedIcon(const BBitmap* normal, BSize,
41202be5353SAxel Dörfler LazyBitmapAllocator*);
41302be5353SAxel Dörfler
41402be5353SAxel Dörfler static bool NeedsDeletionNotification(IconSource);
41502be5353SAxel Dörfler
41602be5353SAxel Dörfler static IconCache* sIconCache;
417*0c973c94SAugustin Cavalier static BSize sMiniIconSize;
41802be5353SAxel Dörfler
41902be5353SAxel Dörfler private:
42002be5353SAxel Dörfler // shared calls
42102be5353SAxel Dörfler IconCacheEntry* Preload(AutoLock<SimpleIconCache>* nodeCache,
42202be5353SAxel Dörfler AutoLock<SimpleIconCache>* sharedCache,
42302be5353SAxel Dörfler AutoLock<SimpleIconCache>** resultingLockedCache,
424*0c973c94SAugustin Cavalier Model*, IconDrawMode mode, BSize size, bool permanent);
42502be5353SAxel Dörfler // preload uses lazy locking, returning the cache we decided
42602be5353SAxel Dörfler // to use to get the icon
42702be5353SAxel Dörfler // <resultingLockedCache> may be null if we don't care
42802be5353SAxel Dörfler
42902be5353SAxel Dörfler // shared mime-based icon retrieval calls
43002be5353SAxel Dörfler IconCacheEntry* GetIconForPreferredApp(const char* mimeTypeSignature,
431*0c973c94SAugustin Cavalier const char* preferredApp, IconDrawMode mode, BSize size,
43202be5353SAxel Dörfler LazyBitmapAllocator*, IconCacheEntry*);
433a51764c5SJohn Scipione IconCacheEntry* GetIconFromFileTypes(ModelNodeLazyOpener*,
434*0c973c94SAugustin Cavalier IconSource &source, IconDrawMode mode, BSize size,
435a51764c5SJohn Scipione LazyBitmapAllocator*, IconCacheEntry*);
436a51764c5SJohn Scipione IconCacheEntry* GetIconFromMetaMime(const char* fileType,
437*0c973c94SAugustin Cavalier IconDrawMode mode, BSize size, LazyBitmapAllocator*,
43802be5353SAxel Dörfler IconCacheEntry*);
43902be5353SAxel Dörfler IconCacheEntry* GetVolumeIcon(AutoLock<SimpleIconCache>* nodeCache,
44002be5353SAxel Dörfler AutoLock<SimpleIconCache>* sharedCache,
44102be5353SAxel Dörfler AutoLock<SimpleIconCache>** resultingLockedCache,
44202be5353SAxel Dörfler Model*, IconSource&, IconDrawMode mode,
443*0c973c94SAugustin Cavalier BSize size, LazyBitmapAllocator*);
44402be5353SAxel Dörfler IconCacheEntry* GetRootIcon(AutoLock<SimpleIconCache>* nodeCache,
44502be5353SAxel Dörfler AutoLock<SimpleIconCache>* sharedCache,
44602be5353SAxel Dörfler AutoLock<SimpleIconCache>** resultingLockedCache,
44702be5353SAxel Dörfler Model*, IconSource&, IconDrawMode mode,
448*0c973c94SAugustin Cavalier BSize size, LazyBitmapAllocator*);
44902be5353SAxel Dörfler IconCacheEntry* GetWellKnownIcon(AutoLock<SimpleIconCache> *nodeCache,
45002be5353SAxel Dörfler AutoLock<SimpleIconCache>* sharedCache,
45102be5353SAxel Dörfler AutoLock<SimpleIconCache>** resultingLockedCache,
45202be5353SAxel Dörfler Model*, IconSource&, IconDrawMode mode,
453*0c973c94SAugustin Cavalier BSize size, LazyBitmapAllocator*);
45402be5353SAxel Dörfler IconCacheEntry* GetNodeIcon(ModelNodeLazyOpener *,
45502be5353SAxel Dörfler AutoLock<SimpleIconCache>* nodeCache,
45602be5353SAxel Dörfler AutoLock<SimpleIconCache>** resultingLockedCache,
45702be5353SAxel Dörfler Model*, IconSource&, IconDrawMode mode,
458*0c973c94SAugustin Cavalier BSize size, LazyBitmapAllocator*, IconCacheEntry*,
459a51764c5SJohn Scipione bool permanent);
46002be5353SAxel Dörfler IconCacheEntry* GetGenericIcon(AutoLock<SimpleIconCache>* sharedCache,
46102be5353SAxel Dörfler AutoLock<SimpleIconCache>** resultingLockedCache,
46202be5353SAxel Dörfler Model*, IconSource&, IconDrawMode mode,
463*0c973c94SAugustin Cavalier BSize size, LazyBitmapAllocator*, IconCacheEntry*);
464a51764c5SJohn Scipione IconCacheEntry* GetFallbackIcon(
465a51764c5SJohn Scipione AutoLock<SimpleIconCache>* sharedCacheLocker,
46602be5353SAxel Dörfler AutoLock<SimpleIconCache>** resultingOpenCache,
467*0c973c94SAugustin Cavalier Model* model, IconDrawMode mode, BSize size,
46802be5353SAxel Dörfler LazyBitmapAllocator* lazyBitmap, IconCacheEntry* entry);
46902be5353SAxel Dörfler
470*0c973c94SAugustin Cavalier BBitmap* MakeTransformedIcon(const BBitmap*, BSize,
47102be5353SAxel Dörfler int32 colorTransformTable [], LazyBitmapAllocator*);
47202be5353SAxel Dörfler
473*0c973c94SAugustin Cavalier private:
47402be5353SAxel Dörfler NodeIconCache fNodeCache;
47502be5353SAxel Dörfler SharedIconCache fSharedCache;
47602be5353SAxel Dörfler
477ae7d51b2SJohn Scipione void InitHighlightTable();
47802be5353SAxel Dörfler
479ae7d51b2SJohn Scipione int32 fHighlightTable[kColorTransformTableSize];
480ae7d51b2SJohn Scipione bool fInitHighlightTable;
481ae7d51b2SJohn Scipione // whether or not we need to initialize the highlight table
482a64cdd1dSAugustin Cavalier
483a64cdd1dSAugustin Cavalier friend class BPrivate::GenerateThumbnailJob;
48402be5353SAxel Dörfler };
48502be5353SAxel Dörfler
48602be5353SAxel Dörfler
48702be5353SAxel Dörfler class LazyBitmapAllocator {
48802be5353SAxel Dörfler // Utility class used when we aren't sure that we will keep a bitmap,
48902be5353SAxel Dörfler // need a bitmap or be able to construct it properly
49002be5353SAxel Dörfler public:
491*0c973c94SAugustin Cavalier LazyBitmapAllocator(BSize size,
4929ecf9d1cSIngo Weinhold color_space colorSpace = kDefaultIconDepth,
49302be5353SAxel Dörfler bool preallocate = false);
49402be5353SAxel Dörfler ~LazyBitmapAllocator();
49502be5353SAxel Dörfler
49602be5353SAxel Dörfler BBitmap* Get();
49702be5353SAxel Dörfler BBitmap* Adopt();
49802be5353SAxel Dörfler
49902be5353SAxel Dörfler private:
50002be5353SAxel Dörfler BBitmap* fBitmap;
501*0c973c94SAugustin Cavalier BSize fSize;
50202be5353SAxel Dörfler color_space fColorSpace;
50302be5353SAxel Dörfler };
50402be5353SAxel Dörfler
505b05aa8b5SJohn Scipione
506b05aa8b5SJohn Scipione // inlines follow
50702be5353SAxel Dörfler
50802be5353SAxel Dörfler inline const char*
FileType()50902be5353SAxel Dörfler SharedCacheEntry::FileType() const
51002be5353SAxel Dörfler {
51102be5353SAxel Dörfler return fFileType.String();
51202be5353SAxel Dörfler }
51302be5353SAxel Dörfler
514b05aa8b5SJohn Scipione
51502be5353SAxel Dörfler inline const char*
AppSignature()51602be5353SAxel Dörfler SharedCacheEntry::AppSignature() const
51702be5353SAxel Dörfler {
51802be5353SAxel Dörfler return fAppSignature.String();
51902be5353SAxel Dörfler }
52002be5353SAxel Dörfler
521b05aa8b5SJohn Scipione
52202be5353SAxel Dörfler inline bool
NeedsDeletionNotification(IconSource from)52302be5353SAxel Dörfler IconCache::NeedsDeletionNotification(IconSource from)
52402be5353SAxel Dörfler {
52502be5353SAxel Dörfler return from == kNode;
52602be5353SAxel Dörfler }
52702be5353SAxel Dörfler
528b05aa8b5SJohn Scipione
52902be5353SAxel Dörfler inline IconCacheEntry*
ResolveIfAlias(IconCacheEntry * entry)53002be5353SAxel Dörfler SharedIconCache::ResolveIfAlias(IconCacheEntry* entry) const
53102be5353SAxel Dörfler {
5328eaf4427SAugustin Cavalier if (entry->fAliasTo == NULL)
53302be5353SAxel Dörfler return entry;
53402be5353SAxel Dörfler
5358eaf4427SAugustin Cavalier return const_cast<IconCacheEntry*>(entry->fAliasTo);
53602be5353SAxel Dörfler }
53702be5353SAxel Dörfler
538b05aa8b5SJohn Scipione
53902be5353SAxel Dörfler } // namespace BPrivate
54002be5353SAxel Dörfler
54102be5353SAxel Dörfler using namespace BPrivate;
54202be5353SAxel Dörfler
543f2ed4761SJohn Scipione
544f2ed4761SJohn Scipione #endif // _NU_ICON_CACHE_H
545