1 /* 2 * Copyright 2010 Stephan Aßmus <superstippi@gmx.de>. All rights reserved. 3 * Distributed under the terms of the MIT License. 4 */ 5 6 #include "BitmapButton.h" 7 8 #include <string.h> 9 10 #include <Bitmap.h> 11 #include <ControlLook.h> 12 #include <TranslationUtils.h> 13 14 15 static const float kFrameInset = 2; 16 17 18 BBitmapButton::BBitmapButton(const char* resourceName, BMessage* message) 19 : 20 BButton("", message), 21 fBitmap(BTranslationUtils::GetBitmap(resourceName)), 22 fBackgroundMode(BUTTON_BACKGROUND) 23 { 24 } 25 26 27 BBitmapButton::BBitmapButton(const uint8* bits, uint32 width, uint32 height, 28 color_space format, BMessage* message) 29 : 30 BButton("", message), 31 fBitmap(new BBitmap(BRect(0, 0, width - 1, height - 1), 0, format)), 32 fBackgroundMode(BUTTON_BACKGROUND) 33 { 34 memcpy(fBitmap->Bits(), bits, fBitmap->BitsLength()); 35 } 36 37 38 BBitmapButton::~BBitmapButton() 39 { 40 delete fBitmap; 41 } 42 43 44 BSize 45 BBitmapButton::MinSize() 46 { 47 BSize min(0, 0); 48 if (fBitmap) { 49 min.width = fBitmap->Bounds().Width(); 50 min.height = fBitmap->Bounds().Height(); 51 } 52 min.width += kFrameInset * 2; 53 min.height += kFrameInset * 2; 54 return min; 55 } 56 57 58 BSize 59 BBitmapButton::MaxSize() 60 { 61 return BSize(B_SIZE_UNLIMITED, B_SIZE_UNLIMITED); 62 } 63 64 65 BSize 66 BBitmapButton::PreferredSize() 67 { 68 return MinSize(); 69 } 70 71 72 void 73 BBitmapButton::Draw(BRect updateRect) 74 { 75 BRect bounds(Bounds()); 76 rgb_color base = ui_color(B_PANEL_BACKGROUND_COLOR); 77 uint32 flags = be_control_look->Flags(this); 78 79 if (fBackgroundMode == BUTTON_BACKGROUND || Value() == B_CONTROL_ON) { 80 be_control_look->DrawButtonBackground(this, bounds, updateRect, base, 81 flags); 82 } else { 83 SetHighColor(tint_color(base, B_DARKEN_2_TINT)); 84 StrokeLine(bounds.LeftBottom(), bounds.RightBottom()); 85 bounds.bottom--; 86 be_control_look->DrawMenuBarBackground(this, bounds, updateRect, base, 87 flags); 88 } 89 90 if (fBitmap == NULL) 91 return; 92 93 SetDrawingMode(B_OP_ALPHA); 94 95 if (!IsEnabled()) { 96 SetBlendingMode(B_CONSTANT_ALPHA, B_ALPHA_OVERLAY); 97 SetHighColor(0, 0, 0, 120); 98 } 99 100 BRect bitmapBounds(fBitmap->Bounds()); 101 BPoint bitmapLocation( 102 floorf((bounds.left + bounds.right 103 - (bitmapBounds.left + bitmapBounds.right)) / 2 + 0.5f), 104 floorf((bounds.top + bounds.bottom 105 - (bitmapBounds.top + bitmapBounds.bottom)) / 2 + 0.5f)); 106 107 DrawBitmap(fBitmap, bitmapLocation); 108 } 109 110 111 void 112 BBitmapButton::SetBackgroundMode(uint32 mode) 113 { 114 if (fBackgroundMode != mode) { 115 fBackgroundMode = mode; 116 Invalidate(); 117 } 118 } 119 120