xref: /haiku/src/preferences/filetypes/IconView.h (revision 16d5c24e533eb14b7b8a99ee9f3ec9ba66335b1e)
1 /*
2  * Copyright 2006, 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, const char* type = NULL);
31 		void SetTo(const entry_ref& ref, const char* type = NULL);
32 		void SetTo(const BMimeType& type, icon_source* _source = NULL);
33 		status_t CopyTo(BAppFileInfo& info, const char* type = NULL,
34 			bool force = false) const;
35 		status_t CopyTo(const entry_ref& ref, const char* type = NULL,
36 			bool force = false) const;
37 		status_t CopyTo(BMimeType& type, bool force = false) const;
38 		status_t CopyTo(BMessage& message) const;
39 
40 		void SetData(const uint8* data, size_t size);
41 		void SetLarge(const BBitmap* large);
42 		void SetMini(const BBitmap* large);
43 		void Unset();
44 
45 		bool HasData() const;
46 		status_t GetData(icon_size which, BBitmap** _bitmap) const;
47 		status_t GetData(uint8** _data, size_t* _size) const;
48 
49 		status_t GetIcon(BBitmap* bitmap) const;
50 
51 		Icon& operator=(const Icon& source);
52 
53 		void AdoptLarge(BBitmap *large);
54 		void AdoptMini(BBitmap *mini);
55 		void AdoptData(uint8* data, size_t size);
56 
57 		static BBitmap* AllocateBitmap(int32 size, int32 space = -1);
58 
59 	private:
60 		BBitmap*	fLarge;
61 		BBitmap*	fMini;
62 		uint8*		fData;
63 		size_t		fSize;
64 };
65 
66 class IconView : public BControl {
67 	public:
68 		IconView(BRect rect, const char* name,
69 			uint32 resizeMode = B_FOLLOW_LEFT | B_FOLLOW_TOP,
70 			uint32 flags = B_NAVIGABLE);
71 		virtual ~IconView();
72 
73 		virtual void AttachedToWindow();
74 		virtual void DetachedFromWindow();
75 		virtual void MessageReceived(BMessage* message);
76 		virtual void Draw(BRect updateRect);
77 		virtual void GetPreferredSize(float* _width, float* _height);
78 
79 		virtual void MouseDown(BPoint where);
80 		virtual void MouseUp(BPoint where);
81 		virtual void MouseMoved(BPoint where, uint32 transit, const BMessage* dragMessage);
82 		virtual void KeyDown(const char* bytes, int32 numBytes);
83 
84 		virtual void MakeFocus(bool focus = true);
85 
86 		void SetTo(const entry_ref& file, const char* fileType = NULL);
87 		void SetTo(const BMimeType& type);
88 		void SetTo(::Icon* icon);
89 		void Unset();
90 		void Update();
91 
92 		void SetIconSize(int32 size);
93 		void ShowIconHeap(bool show);
94 		void ShowEmptyFrame(bool show);
95 		void SetTarget(const BMessenger& target);
96 		void SetModificationMessage(BMessage* message);
97 		void Invoke(const BMessage* message = NULL);
98 
99 		::Icon* Icon();
100 		int32 IconSize() const { return fIconSize; }
101 		icon_source IconSource() const { return fSource; }
102 		status_t GetRef(entry_ref& ref) const;
103 		status_t GetMimeType(BMimeType& type) const;
104 
105 	protected:
106 		virtual bool AcceptsDrag(const BMessage* message);
107 		virtual BRect BitmapRect() const;
108 
109 	private:
110 		void _AddOrEditIcon();
111 		void _SetIcon(BBitmap* large, BBitmap* mini, const uint8* data, size_t size,
112 			bool force = false);
113 		void _SetIcon(entry_ref* ref);
114 		void _RemoveIcon();
115 		void _DeleteIcons();
116 		void _StartWatching();
117 		void _StopWatching();
118 
119 		BMessenger	fTarget;
120 		BMessage*	fModificationMessage;
121 		int32		fIconSize;
122 		BBitmap*	fIcon;
123 		BBitmap*	fHeapIcon;
124 
125 		bool		fHasRef;
126 		bool		fHasType;
127 		entry_ref	fRef;
128 		BMimeType	fType;
129 		icon_source	fSource;
130 		::Icon*		fIconData;
131 
132 		BPoint		fDragPoint;
133 		bool		fTracking;
134 		bool		fDragging;
135 		bool		fDropTarget;
136 		bool		fShowEmptyFrame;
137 };
138 
139 static const uint32 kMsgIconInvoked = 'iciv';
140 static const uint32 kMsgRemoveIcon = 'icrm';
141 static const uint32 kMsgAddIcon = 'icad';
142 static const uint32 kMsgEditIcon = 'iced';
143 
144 extern status_t icon_for_type(const BMimeType& type, uint8** _data, size_t* _size,
145 	icon_source* _source = NULL);
146 extern status_t icon_for_type(const BMimeType& type, BBitmap& bitmap,
147 	icon_size size, icon_source* _source = NULL);
148 
149 #endif	// ICON_VIEW_H
150