btrfs: Attempt to fix GCC2 build.
[haiku.git] / src / apps / webpositive / support / BitmapButton.cpp
blob009079969cf8b5c807bf7f0391c74f1e8dc4492b
1 /*
2 * Copyright 2010 Stephan Aßmus <superstippi@gmx.de>. All rights reserved.
3 * Distributed under the terms of the MIT License.
4 */
6 #include "BitmapButton.h"
8 #include <string.h>
10 #include <Bitmap.h>
11 #include <ControlLook.h>
12 #include <TranslationUtils.h>
15 static const float kFrameInset = 2;
18 BitmapButton::BitmapButton(const char* resourceName, BMessage* message)
20 BButton("", message),
21 fBitmap(BTranslationUtils::GetBitmap(resourceName)),
22 fBackgroundMode(BUTTON_BACKGROUND)
27 BitmapButton::BitmapButton(const uint8* bits, uint32 width, uint32 height,
28 color_space format, BMessage* message)
30 BButton("", message),
31 fBitmap(new BBitmap(BRect(0, 0, width - 1, height - 1), 0, format)),
32 fBackgroundMode(BUTTON_BACKGROUND)
34 memcpy(fBitmap->Bits(), bits, fBitmap->BitsLength());
38 BitmapButton::~BitmapButton()
40 delete fBitmap;
44 BSize
45 BitmapButton::MinSize()
47 BSize min(0, 0);
48 if (fBitmap) {
49 min.width = fBitmap->Bounds().Width();
50 min.height = fBitmap->Bounds().Height();
52 min.width += kFrameInset * 2;
53 min.height += kFrameInset * 2;
54 return min;
58 BSize
59 BitmapButton::MaxSize()
61 return BSize(B_SIZE_UNLIMITED, B_SIZE_UNLIMITED);
65 BSize
66 BitmapButton::PreferredSize()
68 return MinSize();
72 void
73 BitmapButton::Draw(BRect updateRect)
75 BRect bounds(Bounds());
76 rgb_color base = ui_color(B_PANEL_BACKGROUND_COLOR);
77 uint32 flags = be_control_look->Flags(this);
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);
90 if (fBitmap == NULL)
91 return;
93 SetDrawingMode(B_OP_ALPHA);
95 if (!IsEnabled()) {
96 SetBlendingMode(B_CONSTANT_ALPHA, B_ALPHA_OVERLAY);
97 SetHighColor(0, 0, 0, 120);
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));
107 DrawBitmap(fBitmap, bitmapLocation);
111 void
112 BitmapButton::SetBackgroundMode(uint32 mode)
114 if (fBackgroundMode != mode) {
115 fBackgroundMode = mode;
116 Invalidate();