1 /* 2 * Copyright 2006, Haiku. 3 * Distributed under the terms of the MIT License. 4 * 5 * Authors: 6 * Stephan Aßmus <superstippi@gmx.de> 7 */ 8 9 /** gui class that loads an image from disk and shows it 10 as clickable button */ 11 12 // TODO: inherit from BControl? 13 14 #ifndef ICON_BUTTON_H 15 #define ICON_BUTTON_H 16 17 #include <layout.h> 18 19 #include <Invoker.h> 20 #include <String.h> 21 #include <View.h> 22 23 class BBitmap; 24 class BMimeType; 25 26 class IconButton : public MView, public BView, public BInvoker { 27 public: 28 IconButton(const char* name, 29 uint32 id, 30 const char* label = NULL, 31 BMessage* message = NULL, 32 BHandler* target = NULL); 33 virtual ~IconButton(); 34 35 // BHandler 36 virtual void MessageReceived(BMessage* message); 37 38 // BView 39 virtual void AttachedToWindow(); 40 virtual void Draw(BRect updateRect); 41 virtual void MouseDown(BPoint where); 42 virtual void MouseUp(BPoint where); 43 virtual void MouseMoved(BPoint where, uint32 transit, 44 const BMessage* message); 45 virtual void GetPreferredSize(float* width, 46 float* height); 47 48 // BInvoker 49 virtual status_t Invoke(BMessage* message = NULL); 50 51 // MView 52 virtual minimax layoutprefs(); 53 virtual BRect layout(BRect rect); 54 55 // IconButton 56 bool IsValid() const; 57 58 virtual int32 Value() const; 59 virtual void SetValue(int32 value); 60 61 bool IsEnabled() const; 62 void SetEnabled(bool enable); 63 64 void SetPressed(bool pressed); 65 bool IsPressed() const; 66 uint32 ID() const 67 { return fID; } 68 69 status_t SetIcon(const char* pathToBitmap); 70 status_t SetIcon(const BBitmap* bitmap); 71 status_t SetIcon(const BMimeType* fileType, 72 bool small = true); 73 status_t SetIcon(const unsigned char* bitsFromQuickRes, 74 uint32 width, uint32 height, 75 color_space format, 76 bool convertToBW = false); 77 void ClearIcon(); 78 79 BBitmap* Bitmap() const; // caller has to delete the returned bitmap 80 81 virtual bool DrawBorder() const; 82 virtual void DrawNormalBorder(BRect r, rgb_color background, 83 rgb_color shadow, 84 rgb_color darkShadow, 85 rgb_color lightShadow, 86 rgb_color light); 87 virtual void DrawPressedBorder(BRect r, rgb_color background, 88 rgb_color shadow, 89 rgb_color darkShadow, 90 rgb_color lightShadow, 91 rgb_color light); 92 93 protected: 94 enum { 95 STATE_NONE = 0x0000, 96 STATE_TRACKING = 0x0001, 97 STATE_PRESSED = 0x0002, 98 STATE_ENABLED = 0x0004, 99 STATE_INSIDE = 0x0008, 100 STATE_FORCE_PRESSED = 0x0010, 101 }; 102 103 void _AddFlags(uint32 flags); 104 void _ClearFlags(uint32 flags); 105 bool _HasFlags(uint32 flags) const; 106 107 void _DrawFrame(BRect frame, 108 rgb_color col1, 109 rgb_color col2, 110 rgb_color col3, 111 rgb_color col4); 112 113 // private: 114 BBitmap* _ConvertToRGB32(const BBitmap* bitmap) const; 115 status_t _MakeBitmaps(const BBitmap* bitmap); 116 void _DeleteBitmaps(); 117 void _SendMessage() const; 118 void _Update(); 119 120 uint32 fButtonState; 121 int32 fID; 122 BBitmap* fNormalBitmap; 123 BBitmap* fDisabledBitmap; 124 BBitmap* fClickedBitmap; 125 BBitmap* fDisabledClickedBitmap; 126 BString fLabel; 127 128 BHandler* fTargetCache; 129 }; 130 131 #endif // ICON_BUTTON_H 132