xref: /haiku/src/preferences/filetypes/IconView.h (revision dd2a1e350b303b855a50fd64e6cb55618be1ae6a)
1 /*
2  * Copyright 2006-2024, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
3  * 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 
16 enum icon_source {
17 	kNoIcon = 0,
18 	kOwnIcon,
19 	kApplicationIcon,
20 	kSupertypeIcon
21 };
22 
23 
24 class Icon {
25 public:
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(icon_size size, int32 space = -1);
65 
66 private:
67 			BBitmap*			fLarge;
68 			BBitmap*			fMini;
69 			uint8*				fData;
70 			size_t				fSize;
71 };
72 
73 
74 class BSize;
75 
76 
77 class IconView : public BControl {
78 public:
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(icon_size 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 			icon_size			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 
130 protected:
131 	virtual	bool				AcceptsDrag(const BMessage* message);
132 	virtual	BRect				BitmapRect() const;
133 
134 private:
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 			icon_size			fIconSize;
148 			BRect				fIconRect;
149 			BBitmap*			fIconBitmap;
150 			BBitmap*			fHeapIconBitmap;
151 
152 			bool				fHasRef;
153 			bool				fHasType;
154 			entry_ref			fRef;
155 			BMimeType			fType;
156 			icon_source			fSource;
157 			::Icon*				fIcon;
158 
159 			BPoint				fDragPoint;
160 			bool				fTracking;
161 			bool				fDragging;
162 			bool				fDropTarget;
163 			bool				fShowEmptyFrame;
164 };
165 
166 
167 static const uint32 kMsgIconInvoked	= 'iciv';
168 static const uint32 kMsgRemoveIcon	= 'icrm';
169 static const uint32 kMsgAddIcon		= 'icad';
170 static const uint32 kMsgEditIcon	= 'iced';
171 
172 
173 extern status_t icon_for_type(const BMimeType& type, uint8** _data,
174 	size_t* _size, icon_source* _source = NULL);
175 extern status_t icon_for_type(const BMimeType& type, BBitmap& bitmap,
176 	icon_size size, icon_source* _source = NULL);
177 
178 
179 #endif	// ICON_VIEW_H
180