102be5353SAxel Dörfler/*
202be5353SAxel DörflerOpen Tracker License
302be5353SAxel Dörfler
402be5353SAxel DörflerTerms and Conditions
502be5353SAxel Dörfler
602be5353SAxel DörflerCopyright (c) 1991-2000, Be Incorporated. All rights reserved.
702be5353SAxel Dörfler
802be5353SAxel DörflerPermission is hereby granted, free of charge, to any person obtaining a copy of
902be5353SAxel Dörflerthis software and associated documentation files (the "Software"), to deal in
1002be5353SAxel Dörflerthe Software without restriction, including without limitation the rights to
1102be5353SAxel Dörfleruse, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
1202be5353SAxel Dörflerof the Software, and to permit persons to whom the Software is furnished to do
1302be5353SAxel Dörflerso, subject to the following conditions:
1402be5353SAxel Dörfler
1502be5353SAxel DörflerThe above copyright notice and this permission notice applies to all licensees
1602be5353SAxel Dörflerand shall be included in all copies or substantial portions of the Software.
1702be5353SAxel Dörfler
1802be5353SAxel DörflerTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1902be5353SAxel DörflerIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF TITLE, MERCHANTABILITY,
2002be5353SAxel DörflerFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
2102be5353SAxel DörflerBE INCORPORATED BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
2202be5353SAxel DörflerAN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF, OR IN CONNECTION
2302be5353SAxel DörflerWITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2402be5353SAxel Dörfler
2502be5353SAxel DörflerExcept as contained in this notice, the name of Be Incorporated shall not be
2602be5353SAxel Dörflerused in advertising or otherwise to promote the sale, use or other dealings in
2702be5353SAxel Dörflerthis Software without prior written authorization from Be Incorporated.
2802be5353SAxel Dörfler
2902be5353SAxel DörflerTracker(TM), Be(R), BeOS(R), and BeIA(TM) are trademarks or registered trademarks
3002be5353SAxel Dörflerof Be Incorporated in the United States and other countries. Other brand product
3102be5353SAxel Dörflernames are registered trademarks or trademarks of their respective holders.
3202be5353SAxel DörflerAll rights reserved.
3302be5353SAxel Dörfler*/
34f2ed4761SJohn Scipione#ifndef _NU_ICON_CACHE_H
35f2ed4761SJohn Scipione#define _NU_ICON_CACHE_H
3602be5353SAxel Dörfler
37b05aa8b5SJohn Scipione
38b05aa8b5SJohn Scipione// Icon cache is used for drawing node icons; it caches icons
39b05aa8b5SJohn Scipione// and reuses them for successive draws
40b05aa8b5SJohn Scipione
41b05aa8b5SJohn Scipione
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"
4802be5353SAxel Dörfler#include "OpenHashTable.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örflernamespace BPrivate {
6402be5353SAxel Dörfler
6502be5353SAxel Dörflerclass Model;
6602be5353SAxel Dörflerclass ModelNodeLazyOpener;
6702be5353SAxel Dörflerclass LazyBitmapAllocator;
6802be5353SAxel Dörflerclass SharedIconCache;
6902be5353SAxel Dörflerclass SharedCacheEntry;
7002be5353SAxel Dörfler
7102be5353SAxel Dörflerenum IconDrawMode {
729ecf9d1cSIngo Weinhold	// Different states of icon drawing
7302be5353SAxel Dörfler	kSelected 					= 0x01,
7402be5353SAxel Dörfler	kNotFocused					= 0x02,		// Tracker window
7502be5353SAxel Dörfler	kOpen						= 0x04,		// open folder, trash
7602be5353SAxel Dörfler	kNotEmpty					= 0x08,		// full trash
7702be5353SAxel Dörfler	kDisabled					= 0x10,		// inactive nav menu entry
7802be5353SAxel Dörfler	kActive						= 0x20,		// active home dir, boot volume
7902be5353SAxel Dörfler	kLink						= 0x40,		// symbolic link
8002be5353SAxel Dörfler	kTrackerSpecialized			= 0x80,
8102be5353SAxel Dörfler
8202be5353SAxel Dörfler	// some common combinations
8302be5353SAxel Dörfler	kNormalIcon						= 0,
8402be5353SAxel Dörfler	kSelectedIcon					= kSelected,
8502be5353SAxel Dörfler	kSelectedInBackgroundIcon		= kSelected | kNotFocused,
8602be5353SAxel Dörfler	kOpenIcon						= kOpen,
8702be5353SAxel Dörfler	kOpenSelectedIcon				= kSelected | kOpen,
8802be5353SAxel Dörfler	kOpenSelectedInBackgroundIcon	= kSelected | kNotFocused | kOpen,
8902be5353SAxel Dörfler	kFullIcon						= kNotEmpty,
9002be5353SAxel Dörfler	kFullSelectedIcon				= kNotEmpty | kOpen,
9102be5353SAxel Dörfler	kDimmedIcon
9202be5353SAxel Dörfler};
9302be5353SAxel Dörfler
94b05aa8b5SJohn Scipione
9502be5353SAxel Dörfler#define NORMAL_ICON_ONLY kNormalIcon
9602be5353SAxel Dörfler	// replace use of these defines with mode once the respective getters
9702be5353SAxel Dörfler	// can get non-plain icons
9802be5353SAxel Dörfler
9902be5353SAxel Dörfler
10002be5353SAxel Dörfler// Where did an icon come from
10102be5353SAxel Dörflerenum IconSource {
10202be5353SAxel Dörfler	kUnknownSource,
103a51764c5SJohn Scipione	kUnknownNotFromNode,	// icon origin not known but determined not
104a51764c5SJohn Scipione							// to be from the node itself
105a51764c5SJohn Scipione	kTrackerDefault,		// file has no type, Tracker provides generic,
106a51764c5SJohn Scipione							// folder, symlink or app
10702be5353SAxel Dörfler	kTrackerSupplied,		// home directory, boot volume, trash, etc.
10802be5353SAxel Dörfler	kMetaMime,				// from BMimeType
109a51764c5SJohn Scipione	kPreferredAppForType,	// have a preferred application for a type,
110a51764c5SJohn Scipione							// has an icon
11102be5353SAxel Dörfler	kPreferredAppForNode,	// have a preferred application for this node,
11202be5353SAxel Dörfler							// has an icon
11302be5353SAxel Dörfler	kVolume,
11402be5353SAxel Dörfler	kNode
11502be5353SAxel Dörfler};
11602be5353SAxel Dörfler
117b05aa8b5SJohn Scipione
11802be5353SAxel Dörflerclass IconCacheEntry {
11902be5353SAxel Dörfler	// aliased entries don't own their icons, just point
12002be5353SAxel Dörfler	// to some other entry that does
121a51764c5SJohn Scipione
12202be5353SAxel Dörfler	// This is used for icons that are defined by a preferred app for
1239ecf9d1cSIngo Weinhold	// a metamime, types that do not have an icon get to point to
12402be5353SAxel Dörfler	// generic, etc.
125a51764c5SJohn Scipione
12602be5353SAxel Dörflerpublic:
12702be5353SAxel Dörfler	IconCacheEntry();
12802be5353SAxel Dörfler	~IconCacheEntry();
129db416834SMatt Madia
130ae7d51b2SJohn Scipione	void SetAliasFor(const SharedIconCache* sharedCache,
131ae7d51b2SJohn Scipione		const SharedCacheEntry* entry);
132ae7d51b2SJohn Scipione	static IconCacheEntry* ResolveIfAlias(const SharedIconCache* sharedCache,
133ae7d51b2SJohn Scipione		IconCacheEntry* entry);
134ae7d51b2SJohn Scipione	IconCacheEntry* ResolveIfAlias(const SharedIconCache* sharedCache);
135db416834SMatt Madia
136b05aa8b5SJohn Scipione	void SetIcon(BBitmap* bitmap, IconDrawMode mode, icon_size size,
13702be5353SAxel Dörfler		bool create = false);
138db416834SMatt Madia
13902be5353SAxel Dörfler	bool HaveIconBitmap(IconDrawMode mode, icon_size size) const;
14002be5353SAxel Dörfler	bool CanConstructBitmap(IconDrawMode mode, icon_size size) const;
14102be5353SAxel Dörfler	static bool AlternateModeForIconConstructing(IconDrawMode requestedMode,
14202be5353SAxel Dörfler		IconDrawMode &alternate, icon_size size);
143a51764c5SJohn Scipione	BBitmap* ConstructBitmap(BBitmap* constructFrom,
144a51764c5SJohn Scipione		IconDrawMode requestedMode, IconDrawMode constructFromMode,
145a51764c5SJohn Scipione		icon_size size, LazyBitmapAllocator*);
146b05aa8b5SJohn Scipione	BBitmap* ConstructBitmap(IconDrawMode requestedMode, icon_size size,
147b05aa8b5SJohn Scipione		LazyBitmapAllocator*);
148b05aa8b5SJohn Scipione		// same as above, always uses normal icon as source
14902be5353SAxel Dörfler
1509ecf9d1cSIngo Weinhold	bool IconHitTest(BPoint, IconDrawMode, icon_size) const;
15102be5353SAxel Dörfler		// given a point, returns true if a non-transparent pixel was hit
15202be5353SAxel Dörfler
153b05aa8b5SJohn Scipione	void RetireIcons(BObjectList<BBitmap>* retiredBitmapList);
15402be5353SAxel Dörfler		// can't just delete icons, they may be still drawing
15502be5353SAxel Dörfler		// async; instead, put them on the retired list and
15602be5353SAxel Dörfler		// only delete the list if it grows too much, way after
15702be5353SAxel Dörfler		// the icon finishes drawing
158b05aa8b5SJohn Scipione		//
15902be5353SAxel Dörfler		// This could fail if we retire a lot of icons (10 * 1024)
16002be5353SAxel Dörfler		// while we are drawing them, shouldn't be a practical problem
16102be5353SAxel Dörfler
16202be5353SAxel Dörflerprotected:
163b05aa8b5SJohn Scipione	BBitmap* IconForMode(IconDrawMode mode, icon_size size) const;
164b05aa8b5SJohn Scipione	void SetIconForMode(BBitmap* bitmap, IconDrawMode mode, icon_size size);
16502be5353SAxel Dörfler
16602be5353SAxel Dörfler	// list of most common icons
167b05aa8b5SJohn Scipione	BBitmap* fLargeIcon;
168c6433b0bSJohn Scipione	BBitmap* fHighlightedLargeIcon;
169b05aa8b5SJohn Scipione	BBitmap* fMiniIcon;
170c6433b0bSJohn Scipione	BBitmap* fHighlightedMiniIcon;
17102be5353SAxel Dörfler	int32 fAliasForIndex;
172db416834SMatt Madia
17302be5353SAxel Dörfler	// list of other icon kinds would be added here
174db416834SMatt Madia
17502be5353SAxel Dörfler	friend class SharedIconCache;
17602be5353SAxel Dörfler	friend class NodeIconCache;
17702be5353SAxel Dörfler};
17802be5353SAxel Dörfler
179b05aa8b5SJohn Scipione
18002be5353SAxel Dörflerclass SimpleIconCache {
18102be5353SAxel Dörflerpublic:
182b05aa8b5SJohn Scipione	SimpleIconCache(const char*);
18302be5353SAxel Dörfler	virtual ~SimpleIconCache() {}
18402be5353SAxel Dörfler
185b05aa8b5SJohn Scipione	virtual void Draw(IconCacheEntry*, BView*, BPoint, IconDrawMode mode,
18602be5353SAxel Dörfler		icon_size size, bool async = false) = 0;
187b05aa8b5SJohn Scipione	virtual void Draw(IconCacheEntry*, BView*, BPoint, IconDrawMode,
188b05aa8b5SJohn Scipione		icon_size, void (*)(BView*, BPoint, BBitmap*, void*),
189b05aa8b5SJohn Scipione		void* = NULL) = 0;
19002be5353SAxel Dörfler
19102be5353SAxel Dörfler	bool Lock();
19202be5353SAxel Dörfler	void Unlock();
19302be5353SAxel Dörfler	bool IsLocked() const;
194db416834SMatt Madia
19502be5353SAxel Dörflerprivate:
19602be5353SAxel Dörfler	Benaphore fLock;
19702be5353SAxel Dörfler};
19802be5353SAxel Dörfler
199b05aa8b5SJohn Scipione
20002be5353SAxel Dörflerclass SharedCacheEntry : public IconCacheEntry {
20102be5353SAxel Dörflerpublic:
20202be5353SAxel Dörfler	SharedCacheEntry();
203b05aa8b5SJohn Scipione	SharedCacheEntry(const char* fileType, const char* appSignature = 0);
20402be5353SAxel Dörfler
205b05aa8b5SJohn Scipione	void Draw(BView*, BPoint, IconDrawMode mode, icon_size size,
20602be5353SAxel Dörfler		bool async = false);
20702be5353SAxel Dörfler
208b05aa8b5SJohn Scipione	void Draw(BView*, BPoint, IconDrawMode, icon_size,
209b05aa8b5SJohn Scipione		void (*)(BView*, BPoint, BBitmap*, void*), void* = NULL);
21002be5353SAxel Dörfler
211b05aa8b5SJohn Scipione	const char* FileType() const;
212b05aa8b5SJohn Scipione	const char* AppSignature() const;
213db416834SMatt Madia
21402be5353SAxel Dörfler	// hash table support
21502be5353SAxel Dörfler	uint32 Hash() const;
216b05aa8b5SJohn Scipione	static uint32 Hash(const char* fileType, const char* appSignature = 0);
21702be5353SAxel Dörfler	bool operator==(const SharedCacheEntry &) const;
218b05aa8b5SJohn Scipione	void SetTo(const char* fileType, const char* appSignature = 0);
21902be5353SAxel Dörfler
22002be5353SAxel Dörfler	int32 fNext;
22102be5353SAxel Dörflerprivate:
22202be5353SAxel Dörfler	BString fFileType;
22302be5353SAxel Dörfler	BString fAppSignature;
22402be5353SAxel Dörfler
22502be5353SAxel Dörfler	friend class SharedIconCache;
22602be5353SAxel Dörfler};
22702be5353SAxel Dörfler
228b05aa8b5SJohn Scipione
22902be5353SAxel Dörflerclass SharedCacheEntryArray : public OpenHashElementArray<SharedCacheEntry> {
23002be5353SAxel Dörfler	// SharedIconCache stores all it's elements in this array
23102be5353SAxel Dörflerpublic:
23202be5353SAxel Dörfler	SharedCacheEntryArray(int32 initialSize);
233b05aa8b5SJohn Scipione	SharedCacheEntry* Add();
23402be5353SAxel Dörfler};
23502be5353SAxel Dörfler
236b05aa8b5SJohn Scipione
23702be5353SAxel Dörflerclass SharedIconCache : public SimpleIconCache {
23802be5353SAxel Dörfler	// SharedIconCache is used for icons that come from the mime database
23902be5353SAxel Dörflerpublic:
24002be5353SAxel Dörfler	SharedIconCache();
24102be5353SAxel Dörfler
242b05aa8b5SJohn Scipione	virtual void Draw(IconCacheEntry*, BView*, BPoint, IconDrawMode mode,
24302be5353SAxel Dörfler		icon_size size, bool async = false);
244b05aa8b5SJohn Scipione	virtual void Draw(IconCacheEntry*, BView*, BPoint, IconDrawMode,
245b05aa8b5SJohn Scipione		icon_size, void (*)(BView*, BPoint, BBitmap*, void*), void* = NULL);
24602be5353SAxel Dörfler
247a51764c5SJohn Scipione	SharedCacheEntry* FindItem(const char* fileType,
248a51764c5SJohn Scipione		const char* appSignature = 0) const;
249a51764c5SJohn Scipione	SharedCacheEntry* AddItem(const char* fileType,
250b05aa8b5SJohn Scipione		const char* appSignature = 0);
251a51764c5SJohn Scipione	SharedCacheEntry* AddItem(SharedCacheEntry** outstandingEntry,
252a51764c5SJohn Scipione		const char* fileType, const char* appSignature = 0);
253a51764c5SJohn Scipione		// same as previous AddItem, updates the pointer to outstandingEntry,
254a51764c5SJohn Scipione		// because adding to the hash table makes any pending pointer invalid
255b05aa8b5SJohn Scipione	void IconChanged(SharedCacheEntry*);
25602be5353SAxel Dörfler
257ae7d51b2SJohn Scipione	void SetAliasFor(IconCacheEntry* entry,
258a51764c5SJohn Scipione		const SharedCacheEntry* original) const;
259b05aa8b5SJohn Scipione	IconCacheEntry* ResolveIfAlias(IconCacheEntry* entry) const;
260b05aa8b5SJohn Scipione	int32 EntryIndex(const SharedCacheEntry* entry) const;
26102be5353SAxel Dörfler
26202be5353SAxel Dörfler	void RemoveAliasesTo(int32 index);
26302be5353SAxel Dörfler
26402be5353SAxel Dörflerprivate:
26502be5353SAxel Dörfler	SharedCacheEntryArray fElementArray;
266d0a9f680SMichael Lotz	OpenHashTable<SharedCacheEntry, SharedCacheEntryArray> fHashTable;
26702be5353SAxel Dörfler	BObjectList<BBitmap> fRetiredBitmaps;
268a51764c5SJohn Scipione		// icons are drawn asynchronously, can't just delete them right away,
269a51764c5SJohn Scipione		// instead have to place them onto the retired bitmap list and wait
270a51764c5SJohn Scipione		// for the next sync to delete them
27102be5353SAxel Dörfler};
27202be5353SAxel Dörfler
273b05aa8b5SJohn Scipione
27402be5353SAxel Dörflerclass NodeCacheEntry : public IconCacheEntry {
27502be5353SAxel Dörflerpublic:
27602be5353SAxel Dörfler	NodeCacheEntry(bool permanent = false);
277b05aa8b5SJohn Scipione	NodeCacheEntry(const node_ref*, bool permanent = false);
278b05aa8b5SJohn Scipione	void Draw(BView*, BPoint, IconDrawMode mode, icon_size size,
27902be5353SAxel Dörfler		bool async = false);
280db416834SMatt Madia
281b05aa8b5SJohn Scipione	void Draw(BView*, BPoint, IconDrawMode, icon_size,
282b05aa8b5SJohn Scipione		void (*)(BView*, BPoint, BBitmap*, void*), void* = NULL);
28302be5353SAxel Dörfler
284b05aa8b5SJohn Scipione	const node_ref* Node() const;
285db416834SMatt Madia
28602be5353SAxel Dörfler	uint32 Hash() const;
287b05aa8b5SJohn Scipione	static uint32 Hash(const node_ref*);
288b05aa8b5SJohn Scipione	bool operator==(const NodeCacheEntry&) const;
289b05aa8b5SJohn Scipione	void SetTo(const node_ref*);
29002be5353SAxel Dörfler	void MakePermanent();
29102be5353SAxel Dörfler	bool Permanent() const;
292db416834SMatt Madia
29302be5353SAxel Dörfler	int32 fNext;
29402be5353SAxel Dörflerprivate:
29502be5353SAxel Dörfler	node_ref fRef;
29602be5353SAxel Dörfler	bool fPermanent;
29702be5353SAxel Dörfler		// special cache entry that has to be deleted explicitly
29802be5353SAxel Dörfler
29902be5353SAxel Dörfler	friend class NodeIconCache;
30002be5353SAxel Dörfler};
30102be5353SAxel Dörfler
302b05aa8b5SJohn Scipione
30302be5353SAxel Dörflerclass NodeCacheEntryArray : public OpenHashElementArray<NodeCacheEntry> {
30402be5353SAxel Dörfler	// NodeIconCache stores all it's elements in this array
30502be5353SAxel Dörflerpublic:
30602be5353SAxel Dörfler	NodeCacheEntryArray(int32 initialSize);
307b05aa8b5SJohn Scipione	NodeCacheEntry* Add();
30802be5353SAxel Dörfler};
30902be5353SAxel Dörfler
310b05aa8b5SJohn Scipione
31102be5353SAxel Dörflerclass NodeIconCache : public SimpleIconCache {
3129ecf9d1cSIngo Weinhold	// NodeIconCache is used for nodes that define their own icons
31302be5353SAxel Dörflerpublic:
31402be5353SAxel Dörfler	NodeIconCache();
31502be5353SAxel Dörfler
316b05aa8b5SJohn Scipione	virtual void Draw(IconCacheEntry*, BView*, BPoint, IconDrawMode,
317b05aa8b5SJohn Scipione		icon_size, bool async = false);
31802be5353SAxel Dörfler
319b05aa8b5SJohn Scipione	virtual void Draw(IconCacheEntry*, BView*, BPoint, IconDrawMode,
320b05aa8b5SJohn Scipione		icon_size, void (*)(BView*, BPoint, BBitmap*, void*), void* = 0);
32102be5353SAxel Dörfler
322b05aa8b5SJohn Scipione	NodeCacheEntry* FindItem(const node_ref*) const;
323b05aa8b5SJohn Scipione	NodeCacheEntry* AddItem(const node_ref*, bool permanent = false);
324a51764c5SJohn Scipione	NodeCacheEntry* AddItem(NodeCacheEntry** outstandingEntry,
325a51764c5SJohn Scipione		const node_ref*);
326a51764c5SJohn Scipione		// same as previous AddItem, updates the pointer to outstandingEntry,
327a51764c5SJohn Scipione		// because adding to the hash table makes any pending pointer invalid
328b05aa8b5SJohn Scipione	void Deleting(const node_ref*);
329a51764c5SJohn Scipione		// model for this node is getting deleted
330a51764c5SJohn Scipione		// (not necessarily the node itself)
331b05aa8b5SJohn Scipione	void Removing(const node_ref*);
3329ecf9d1cSIngo Weinhold		// used by permanent NodeIconCache entries, when an entry gets deleted
333b05aa8b5SJohn Scipione	void Deleting(const BView*);
334b05aa8b5SJohn Scipione	void IconChanged(const Model*);
33502be5353SAxel Dörfler
33602be5353SAxel Dörfler	void RemoveAliasesTo(int32 index);
33702be5353SAxel Dörfler
33802be5353SAxel Dörflerprivate:
33902be5353SAxel Dörfler	NodeCacheEntryArray fElementArray;
340d0a9f680SMichael Lotz	OpenHashTable<NodeCacheEntry, NodeCacheEntryArray> fHashTable;
34102be5353SAxel Dörfler};
34202be5353SAxel Dörfler
343b05aa8b5SJohn Scipione
34402be5353SAxel Dörflerconst int32 kColorTransformTableSize = 256;
34502be5353SAxel Dörfler
346b05aa8b5SJohn Scipione
34702be5353SAxel Dörflerclass IconCache {
34802be5353SAxel Dörflerpublic:
34902be5353SAxel Dörfler	IconCache();
350db416834SMatt Madia
351b05aa8b5SJohn Scipione	void Draw(Model*, BView*, BPoint where, IconDrawMode mode,
35202be5353SAxel Dörfler		icon_size size, bool async = false);
35302be5353SAxel Dörfler		// draw an icon for a model, load the icon from the appropriate
35402be5353SAxel Dörfler		// location if not cached already
35502be5353SAxel Dörfler
356b05aa8b5SJohn Scipione	void SyncDraw(Model*, BView*, BPoint, IconDrawMode,
357b05aa8b5SJohn Scipione		icon_size, void (*)(BView*, BPoint, BBitmap*, void*),
358b05aa8b5SJohn Scipione		void* passThruState = 0);
35902be5353SAxel Dörfler		// draw an icon for a model, load the icon from the appropriate
36002be5353SAxel Dörfler		// location if not cached already; only works for sync draws,
36102be5353SAxel Dörfler		// once the call returns, the bitmap may be deleted
36202be5353SAxel Dörfler
36302be5353SAxel Dörfler	// preload calls used to ensure successive cache hit for the respective
36402be5353SAxel Dörfler	// icon, used for common tracker types, etc; Not calling these should only
36502be5353SAxel Dörfler	// cause a slowdown
366b05aa8b5SJohn Scipione	void Preload(Model*, IconDrawMode mode, icon_size size,
367b05aa8b5SJohn Scipione		bool permanent = false);
368b05aa8b5SJohn Scipione	status_t Preload(const char* mimeType, IconDrawMode mode, icon_size size);
36902be5353SAxel Dörfler
370b05aa8b5SJohn Scipione	void Deleting(const Model*);
371b05aa8b5SJohn Scipione		// hook to manage unloading icons for nodes that are going away
372b05aa8b5SJohn Scipione	void Removing(const Model* model);
37302be5353SAxel Dörfler		// used by permanent NodeIconCache entries, when an entry gets
37402be5353SAxel Dörfler		// deleted
375b05aa8b5SJohn Scipione	void Deleting(const BView*);
37602be5353SAxel Dörfler		// hook to manage deleting draw view caches for views that are
37702be5353SAxel Dörfler		// going away
378db416834SMatt Madia
37902be5353SAxel Dörfler	// icon changed calls, used when a node or a file type has an icon changed
38002be5353SAxel Dörfler	// the icons for the node/file type will be flushed and re-cached during
38102be5353SAxel Dörfler	// the next draw
382b05aa8b5SJohn Scipione	void IconChanged(Model*);
383b05aa8b5SJohn Scipione	void IconChanged(const char* mimeType, const char* appSignature);
384db416834SMatt Madia
385b05aa8b5SJohn Scipione	bool IsIconFrom(const Model*, const char* mimeType,
386b05aa8b5SJohn Scipione		const char* appSignature) const;
38702be5353SAxel Dörfler		// called when metamime database changed to figure out which models
38802be5353SAxel Dörfler		// to redraw
38902be5353SAxel Dörfler
390b05aa8b5SJohn Scipione	bool IconHitTest(BPoint, const Model*, IconDrawMode, icon_size);
39102be5353SAxel Dörfler
39202be5353SAxel Dörfler	// utility calls for building specialized icons
393b05aa8b5SJohn Scipione	BBitmap* MakeSelectedIcon(const BBitmap* normal, icon_size,
394b05aa8b5SJohn Scipione		LazyBitmapAllocator*);
395b05aa8b5SJohn Scipione
39602be5353SAxel Dörfler	static bool NeedsDeletionNotification(IconSource);
39702be5353SAxel Dörfler
398b05aa8b5SJohn Scipione	static IconCache* sIconCache;
39902be5353SAxel Dörfler
40002be5353SAxel Dörflerprivate:
40102be5353SAxel Dörfler	// shared calls
402b05aa8b5SJohn Scipione	IconCacheEntry* Preload(AutoLock<SimpleIconCache>* nodeCache,
403b05aa8b5SJohn Scipione		AutoLock<SimpleIconCache>* sharedCache,
404b05aa8b5SJohn Scipione		AutoLock<SimpleIconCache>** resultingLockedCache,
405b05aa8b5SJohn Scipione		Model*, IconDrawMode mode, icon_size size, bool permanent);
40602be5353SAxel Dörfler		// preload uses lazy locking, returning the cache we decided
40702be5353SAxel Dörfler		// to use to get the icon
40802be5353SAxel Dörfler		// <resultingLockedCache> may be null if we don't care
40902be5353SAxel Dörfler
41002be5353SAxel Dörfler	// shared mime-based icon retrieval calls
411b05aa8b5SJohn Scipione	IconCacheEntry* GetIconForPreferredApp(const char* mimeTypeSignature,
412b05aa8b5SJohn Scipione		const char* preferredApp, IconDrawMode mode, icon_size size,
413b05aa8b5SJohn Scipione		 LazyBitmapAllocator*, IconCacheEntry*);
414a51764c5SJohn Scipione	IconCacheEntry* GetIconFromFileTypes(ModelNodeLazyOpener*,
415a51764c5SJohn Scipione		IconSource &source, IconDrawMode mode, icon_size size,
416a51764c5SJohn Scipione		LazyBitmapAllocator*, IconCacheEntry*);
417a51764c5SJohn Scipione	IconCacheEntry* GetIconFromMetaMime(const char* fileType,
418b05aa8b5SJohn Scipione		IconDrawMode mode, icon_size size, LazyBitmapAllocator*,
419b05aa8b5SJohn Scipione		IconCacheEntry*);
420b05aa8b5SJohn Scipione	IconCacheEntry* GetVolumeIcon(AutoLock<SimpleIconCache>* nodeCache,
421b05aa8b5SJohn Scipione		AutoLock<SimpleIconCache>* sharedCache,
422b05aa8b5SJohn Scipione		AutoLock<SimpleIconCache>** resultingLockedCache,
423b05aa8b5SJohn Scipione		Model*, IconSource&, IconDrawMode mode,
424b05aa8b5SJohn Scipione		icon_size size, LazyBitmapAllocator*);
425b05aa8b5SJohn Scipione	IconCacheEntry* GetRootIcon(AutoLock<SimpleIconCache>* nodeCache,
426b05aa8b5SJohn Scipione		AutoLock<SimpleIconCache>* sharedCache,
427b05aa8b5SJohn Scipione		AutoLock<SimpleIconCache>** resultingLockedCache,
428b05aa8b5SJohn Scipione		Model*, IconSource&, IconDrawMode mode,
429b05aa8b5SJohn Scipione		icon_size size, LazyBitmapAllocator*);
430b05aa8b5SJohn Scipione	IconCacheEntry* GetWellKnownIcon(AutoLock<SimpleIconCache> *nodeCache,
431b05aa8b5SJohn Scipione		AutoLock<SimpleIconCache>* sharedCache,
432b05aa8b5SJohn Scipione		AutoLock<SimpleIconCache>** resultingLockedCache,
433b05aa8b5SJohn Scipione		Model*, IconSource&, IconDrawMode mode,
434b05aa8b5SJohn Scipione		icon_size size, LazyBitmapAllocator*);
435b05aa8b5SJohn Scipione	IconCacheEntry* GetNodeIcon(ModelNodeLazyOpener *,
436b05aa8b5SJohn Scipione		AutoLock<SimpleIconCache>* nodeCache,
437b05aa8b5SJohn Scipione		AutoLock<SimpleIconCache>** resultingLockedCache,
438b05aa8b5SJohn Scipione		Model*, IconSource&, IconDrawMode mode,
439a51764c5SJohn Scipione		icon_size size, LazyBitmapAllocator*, IconCacheEntry*,
440a51764c5SJohn Scipione		bool permanent);
441b05aa8b5SJohn Scipione	IconCacheEntry* GetGenericIcon(AutoLock<SimpleIconCache>* sharedCache,
442b05aa8b5SJohn Scipione		AutoLock<SimpleIconCache>** resultingLockedCache,
443b05aa8b5SJohn Scipione		Model*, IconSource&, IconDrawMode mode,
444b05aa8b5SJohn Scipione		icon_size size, LazyBitmapAllocator*, IconCacheEntry*);
445a51764c5SJohn Scipione	IconCacheEntry* GetFallbackIcon(
446a51764c5SJohn Scipione		AutoLock<SimpleIconCache>* sharedCacheLocker,
447b05aa8b5SJohn Scipione		AutoLock<SimpleIconCache>** resultingOpenCache,
448b05aa8b5SJohn Scipione		Model* model, IconDrawMode mode, icon_size size,
449b05aa8b5SJohn Scipione		LazyBitmapAllocator* lazyBitmap, IconCacheEntry* entry);
450b05aa8b5SJohn Scipione
451b05aa8b5SJohn Scipione	BBitmap* MakeTransformedIcon(const BBitmap*, icon_size,
452b05aa8b5SJohn Scipione		int32 colorTransformTable [], LazyBitmapAllocator*);
45302be5353SAxel Dörfler
45402be5353SAxel Dörfler	NodeIconCache fNodeCache;
45502be5353SAxel Dörfler	SharedIconCache fSharedCache;
45602be5353SAxel Dörfler
457ae7d51b2SJohn Scipione	void InitHighlightTable();
45802be5353SAxel Dörfler
459ae7d51b2SJohn Scipione	int32 fHighlightTable[kColorTransformTableSize];
460ae7d51b2SJohn Scipione	bool fInitHighlightTable;
461ae7d51b2SJohn Scipione		// whether or not we need to initialize the highlight table
46202be5353SAxel Dörfler};
46302be5353SAxel Dörfler
46402be5353SAxel Dörfler
46502be5353SAxel Dörflerclass LazyBitmapAllocator {
466ae7d51b2SJohn Scipione	// Utility class used when we aren't sure that we will keep a bitmap,
467ae7d51b2SJohn Scipione	// need a bitmap or be able to construct it properly
46802be5353SAxel Dörflerpublic:
4699ecf9d1cSIngo Weinhold	LazyBitmapAllocator(icon_size size,
470ae7d51b2SJohn Scipione		color_space colorSpace = kDefaultIconDepth,
47102be5353SAxel Dörfler		bool preallocate = false);
47202be5353SAxel Dörfler	~LazyBitmapAllocator();
47302be5353SAxel Dörfler
474b05aa8b5SJohn Scipione	BBitmap* Get();
475b05aa8b5SJohn Scipione	BBitmap* Adopt();
47602be5353SAxel Dörfler
47702be5353SAxel Dörflerprivate:
478b05aa8b5SJohn Scipione	BBitmap* fBitmap;
47902be5353SAxel Dörfler	icon_size fSize;
48002be5353SAxel Dörfler	color_space fColorSpace;
48102be5353SAxel Dörfler};
48202be5353SAxel Dörfler
48302be5353SAxel Dörfler
484b05aa8b5SJohn Scipione// inlines follow
485b05aa8b5SJohn Scipione
486b05aa8b5SJohn Scipioneinline const char*
48702be5353SAxel DörflerSharedCacheEntry::FileType() const
48802be5353SAxel Dörfler{
48902be5353SAxel Dörfler	return fFileType.String();
49002be5353SAxel Dörfler}
49102be5353SAxel Dörfler
492b05aa8b5SJohn Scipione
493b05aa8b5SJohn Scipioneinline const char*
49402be5353SAxel DörflerSharedCacheEntry::AppSignature() const
49502be5353SAxel Dörfler{
49602be5353SAxel Dörfler	return fAppSignature.String();
49702be5353SAxel Dörfler}
49802be5353SAxel Dörfler
499b05aa8b5SJohn Scipione
500b05aa8b5SJohn Scipioneinline bool
50102be5353SAxel DörflerIconCache::NeedsDeletionNotification(IconSource from)
50202be5353SAxel Dörfler{
50302be5353SAxel Dörfler	return from == kNode;
50402be5353SAxel Dörfler}
50502be5353SAxel Dörfler
506b05aa8b5SJohn Scipione
507b05aa8b5SJohn Scipioneinline IconCacheEntry*
508b05aa8b5SJohn ScipioneSharedIconCache::ResolveIfAlias(IconCacheEntry* entry) const
50902be5353SAxel Dörfler{
51002be5353SAxel Dörfler	if (entry->fAliasForIndex < 0)
51102be5353SAxel Dörfler		return entry;
512db416834SMatt Madia
51302be5353SAxel Dörfler	return fHashTable.ElementAt(entry->fAliasForIndex);
51402be5353SAxel Dörfler}
51502be5353SAxel Dörfler
516b05aa8b5SJohn Scipione
517b05aa8b5SJohn Scipioneinline int32
518b05aa8b5SJohn ScipioneSharedIconCache::EntryIndex(const SharedCacheEntry* entry) const
51902be5353SAxel Dörfler{
52002be5353SAxel Dörfler	return fHashTable.ElementIndex(entry);
52102be5353SAxel Dörfler}
52202be5353SAxel Dörfler
52302be5353SAxel Dörfler} // namespace BPrivate
52402be5353SAxel Dörfler
52502be5353SAxel Dörflerusing namespace BPrivate;
52602be5353SAxel Dörfler
527f2ed4761SJohn Scipione
528f2ed4761SJohn Scipione#endif	// _NU_ICON_CACHE_H
529