xref: /haiku/headers/private/shared/IconButton.h (revision 1deede7388b04dbeec5af85cae7164735ea9e70d)
1 /*
2  * Copyright 2006-2011, Haiku.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Stephan Aßmus <superstippi@gmx.de>
7  */
8 #ifndef ICON_BUTTON_H
9 #define ICON_BUTTON_H
10 
11 
12 //! GUI class that loads an image from disk and shows it as clickable button.
13 
14 
15 #include <Control.h>
16 #include <String.h>
17 
18 
19 class BBitmap;
20 class BMimeType;
21 
22 
23 namespace BPrivate {
24 
25 
26 class BIconButton : public BControl {
27 public:
28 								BIconButton(const char* name,
29 									const char* label = NULL,
30 									BMessage* message = NULL,
31 									BHandler* target = NULL);
32 	virtual						~BIconButton();
33 
34 	// BView interface
35 	virtual	void				MessageReceived(BMessage* message);
36 	virtual	void				AttachedToWindow();
37 
38 	virtual	void				Draw(BRect updateRect);
39 	virtual	bool				ShouldDrawBorder() const;
40 	virtual	void				DrawBorder(BRect& frame,
41 									const BRect& updateRect,
42 									const rgb_color& backgroundColor,
43 									uint32 controlLookFlags);
44 	virtual	void				DrawBackground(BRect& frame,
45 									const BRect& updateRect,
46 									const rgb_color& backgroundColor,
47 									uint32 controlLookFlags);
48 
49 	virtual	void				MouseDown(BPoint where);
50 	virtual	void				MouseUp(BPoint where);
51 	virtual	void				MouseMoved(BPoint where, uint32 transit,
52 										   const BMessage* message);
53 	virtual	void				GetPreferredSize(float* width,
54 												 float* height);
55 	virtual	BSize				MinSize();
56 	virtual	BSize				MaxSize();
57 
58 
59 	// BInvoker interface
60 	virtual	status_t			Invoke(BMessage* message = NULL);
61 
62 	// BControl interface
63 	virtual	void				SetValue(int32 value);
64 	virtual	void				SetEnabled(bool enable);
65 
66 	// BIconButton
67 			bool				IsValid() const;
68 
69 			void				SetPressed(bool pressed);
70 			bool				IsPressed() const;
71 
72 			status_t			SetIcon(int32 resourceID);
73 			status_t			SetIcon(const char* pathToBitmap);
74 	virtual	status_t			SetIcon(const BBitmap* bitmap,
75 									uint32 flags = 0);
76 			status_t			SetIcon(const BMimeType* fileType,
77 									bool small = true);
78 			status_t			SetIcon(const unsigned char* bitsFromQuickRes,
79 									uint32 width, uint32 height,
80 									color_space format,
81 									bool convertToBW = false);
82 			void				ClearIcon();
83 			void				TrimIcon(bool keepAspect = true);
84 
85 			BBitmap*			Bitmap() const;
86 									// caller has to delete the returned bitmap
87 
88 protected:
89 			bool				IsInside() const;
90 			void				SetInside(bool inside);
91 
92 private:
93 			BBitmap*			_ConvertToRGB32(const BBitmap* bitmap) const;
94 			status_t			_MakeBitmaps(const BBitmap* bitmap);
95 			void				_DeleteBitmaps();
96 			void				_SendMessage() const;
97 			void				_Update();
98 			void				_SetTracking(bool state);
99 			void				_SetFlags(uint32 flags, bool set);
100 			bool				_HasFlags(uint32 flags) const;
101 
102 private:
103 			uint32				fButtonState;
104 			BBitmap*			fNormalBitmap;
105 			BBitmap*			fDisabledBitmap;
106 			BBitmap*			fClickedBitmap;
107 			BBitmap*			fDisabledClickedBitmap;
108 
109 			BHandler*			fTargetCache;
110 };
111 
112 
113 } // namespac BPrivate
114 
115 
116 using BPrivate::BIconButton;
117 
118 
119 #endif // ICON_BUTTON_H
120