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 // TODO: inherit from BControl 15 16 17 #include <Invoker.h> 18 #include <String.h> 19 #include <View.h> 20 21 22 class BBitmap; 23 class BMimeType; 24 25 26 namespace BPrivate { 27 28 29 class BIconButton : public BView, public BInvoker { 30 public: 31 BIconButton(const char* name, 32 uint32 id, 33 const char* label = NULL, 34 BMessage* message = NULL, 35 BHandler* target = NULL); 36 virtual ~BIconButton(); 37 38 // BView interface 39 virtual void MessageReceived(BMessage* message); 40 virtual void AttachedToWindow(); 41 42 virtual void Draw(BRect updateRect); 43 virtual bool ShouldDrawBorder() const; 44 virtual void DrawBorder(BRect& frame, 45 const BRect& updateRect, 46 const rgb_color& backgroundColor, 47 uint32 controlLookFlags); 48 virtual void DrawBackground(BRect& frame, 49 const BRect& updateRect, 50 const rgb_color& backgroundColor, 51 uint32 controlLookFlags); 52 53 virtual void MouseDown(BPoint where); 54 virtual void MouseUp(BPoint where); 55 virtual void MouseMoved(BPoint where, uint32 transit, 56 const BMessage* message); 57 virtual void GetPreferredSize(float* width, 58 float* height); 59 virtual BSize MinSize(); 60 virtual BSize MaxSize(); 61 62 63 // BInvoker interface 64 virtual status_t Invoke(BMessage* message = NULL); 65 66 // BIconButton 67 bool IsValid() const; 68 69 virtual int32 Value() const; 70 virtual void SetValue(int32 value); 71 72 bool IsEnabled() const; 73 void SetEnabled(bool enable); 74 75 void SetPressed(bool pressed); 76 bool IsPressed() const; 77 uint32 ID() const 78 { return fID; } 79 80 status_t SetIcon(int32 resourceID); 81 status_t SetIcon(const char* pathToBitmap); 82 status_t SetIcon(const BBitmap* bitmap); 83 status_t SetIcon(const BMimeType* fileType, 84 bool small = true); 85 status_t SetIcon(const unsigned char* bitsFromQuickRes, 86 uint32 width, uint32 height, 87 color_space format, 88 bool convertToBW = false); 89 void ClearIcon(); 90 void TrimIcon(bool keepAspect = true); 91 92 BBitmap* Bitmap() const; 93 // caller has to delete the returned bitmap 94 const BString& Label() const; 95 96 protected: 97 enum { 98 STATE_NONE = 0x0000, 99 STATE_TRACKING = 0x0001, 100 STATE_PRESSED = 0x0002, 101 STATE_ENABLED = 0x0004, 102 STATE_INSIDE = 0x0008, 103 STATE_FORCE_PRESSED = 0x0010, 104 }; 105 106 void _AddFlags(uint32 flags); 107 void _ClearFlags(uint32 flags); 108 bool _HasFlags(uint32 flags) const; 109 110 private: 111 BBitmap* _ConvertToRGB32(const BBitmap* bitmap) const; 112 status_t _MakeBitmaps(const BBitmap* bitmap); 113 void _DeleteBitmaps(); 114 void _SendMessage() const; 115 void _Update(); 116 117 private: 118 uint32 fButtonState; 119 int32 fID; 120 BBitmap* fNormalBitmap; 121 BBitmap* fDisabledBitmap; 122 BBitmap* fClickedBitmap; 123 BBitmap* fDisabledClickedBitmap; 124 BString fLabel; 125 126 BHandler* fTargetCache; 127 }; 128 129 130 } // namespac BPrivate 131 132 133 using BPrivate::BIconButton; 134 135 136 #endif // ICON_BUTTON_H 137