1272343Sngie/*
2272343Sngie * Copyright 2006, Axel D��rfler, axeld@pinc-software.de. All rights reserved.
3272343Sngie * Distributed under the terms of the MIT License.
4 */
5#ifndef ICON_VIEW_H
6#define ICON_VIEW_H
7
8
9#include <Control.h>
10#include <Entry.h>
11#include <Messenger.h>
12#include <Mime.h>
13#include <String.h>
14
15
16enum icon_source {
17	kNoIcon = 0,
18	kOwnIcon,
19	kApplicationIcon,
20	kSupertypeIcon
21};
22
23
24class Icon {
25public:
26								Icon();
27								Icon(const Icon& source);
28								~Icon();
29
30			void				SetTo(const BAppFileInfo& info,
31									const char* type = NULL);
32			void				SetTo(const entry_ref& ref,
33									const char* type = NULL);
34			void				SetTo(const BMimeType& type,
35									icon_source* _source = NULL);
36			status_t			CopyTo(BAppFileInfo& info,
37									const char* type = NULL,
38									bool force = false) const;
39			status_t			CopyTo(const entry_ref& ref,
40									const char* type = NULL,
41									bool force = false) const;
42			status_t			CopyTo(BMimeType& type,
43									bool force = false) const;
44			status_t			CopyTo(BMessage& message) const;
45
46			void				SetData(const uint8* data, size_t size);
47			void				SetLarge(const BBitmap* large);
48			void				SetMini(const BBitmap* large);
49			void				Unset();
50
51			bool				HasData() const;
52			status_t			GetData(icon_size which,
53									BBitmap** _bitmap) const;
54			status_t			GetData(uint8** _data, size_t* _size) const;
55
56			status_t			GetIcon(BBitmap* bitmap) const;
57
58			Icon&				operator=(const Icon& source);
59
60			void				AdoptLarge(BBitmap* large);
61			void				AdoptMini(BBitmap* mini);
62			void				AdoptData(uint8* data, size_t size);
63
64	static	BBitmap*			AllocateBitmap(int32 size, int32 space = -1);
65
66private:
67			BBitmap*			fLarge;
68			BBitmap*			fMini;
69			uint8*				fData;
70			size_t				fSize;
71};
72
73
74class BSize;
75
76
77class IconView : public BControl {
78public:
79								IconView(const char* name,
80									uint32 flags = B_NAVIGABLE);
81	virtual						~IconView();
82
83	virtual	void				AttachedToWindow();
84	virtual	void				DetachedFromWindow();
85	virtual	void				MessageReceived(BMessage* message);
86	virtual	void				Draw(BRect updateRect);
87	virtual	void				GetPreferredSize(float* _width, float* _height);
88
89	virtual	BSize				MaxSize();
90	virtual	BSize				MinSize();
91	virtual	BSize				PreferredSize();
92
93	virtual	void				MouseDown(BPoint where);
94	virtual	void				MouseUp(BPoint where);
95	virtual	void				MouseMoved(BPoint where, uint32 transit,
96									const BMessage* dragMessage);
97	virtual	void				KeyDown(const char* bytes, int32 numBytes);
98
99	virtual	void				MakeFocus(bool focus = true);
100
101			void				SetTo(const entry_ref& file,
102									const char* fileType = NULL);
103			void				SetTo(const BMimeType& type);
104			void				SetTo(::Icon* icon);
105			void				Unset();
106			void				Update();
107
108			void				SetIconSize(int32 size);
109			void				ShowIconHeap(bool show);
110			void				ShowEmptyFrame(bool show);
111			status_t			SetTarget(const BMessenger& target);
112			void				SetModificationMessage(BMessage* message);
113			status_t			Invoke(BMessage* message = NULL);
114
115			::Icon*				Icon();
116			int32				IconSize() const { return fIconSize; }
117			icon_source			IconSource() const { return fSource; }
118			status_t			GetRef(entry_ref& ref) const;
119			status_t			GetMimeType(BMimeType& type) const;
120
121#if __GNUC__ == 2
122	virtual	status_t			SetTarget(BMessenger target);
123	virtual	status_t			SetTarget(const BHandler* handler,
124									const BLooper* looper = NULL);
125#else
126			using BControl::SetTarget;
127#endif
128
129
130protected:
131	virtual	bool				AcceptsDrag(const BMessage* message);
132	virtual	BRect				BitmapRect() const;
133
134private:
135			void				_AddOrEditIcon();
136			void				_SetIcon(BBitmap* large, BBitmap* mini,
137									const uint8* data, size_t size,
138									bool force = false);
139			void				_SetIcon(entry_ref* ref);
140			void				_RemoveIcon();
141			void				_DeleteIcons();
142			void				_StartWatching();
143			void				_StopWatching();
144
145			BMessenger			fTarget;
146			BMessage*			fModificationMessage;
147			int32				fIconSize;
148			BBitmap*			fIcon;
149			BBitmap*			fHeapIcon;
150
151			bool				fHasRef;
152			bool				fHasType;
153			entry_ref			fRef;
154			BMimeType			fType;
155			icon_source			fSource;
156			::Icon*				fIconData;
157
158			BPoint				fDragPoint;
159			bool				fTracking;
160			bool				fDragging;
161			bool				fDropTarget;
162			bool				fShowEmptyFrame;
163};
164
165
166static const uint32 kMsgIconInvoked	= 'iciv';
167static const uint32 kMsgRemoveIcon	= 'icrm';
168static const uint32 kMsgAddIcon		= 'icad';
169static const uint32 kMsgEditIcon	= 'iced';
170
171
172extern status_t icon_for_type(const BMimeType& type, uint8** _data,
173	size_t* _size, icon_source* _source = NULL);
174extern status_t icon_for_type(const BMimeType& type, BBitmap& bitmap,
175	icon_size size, icon_source* _source = NULL);
176
177
178#endif	// ICON_VIEW_H
179