HaikuDepot: BitmapButton fixes

* BitmapView can be configured to scale or not to
   scale the bitmap.
 * BitmapButton more or less completed.
This commit is contained in:
Stephan Aßmus
2013-08-18 17:52:57 +02:00
parent ecbb2c1943
commit bef5ba1ac3
4 changed files with 77 additions and 10 deletions
+34 -2
View File
@@ -6,16 +6,19 @@
#include "BitmapButton.h"
#include <ControlLook.h>
#include <LayoutUtils.h>
#include <Window.h>
BitmapButton::BitmapButton(const char* name)
BitmapButton::BitmapButton(const char* name, BMessage* message)
:
BitmapView(name),
BInvoker(message, NULL, NULL),
fMouseInside(false),
fMouseDown(false)
{
SetScaleBitmap(false);
}
@@ -48,7 +51,10 @@ BitmapButton::MouseMoved(BPoint where, uint32 transit,
void
BitmapButton::MouseDown(BPoint where)
{
_SetMouseDown(true);
if (fMouseInside) {
_SetMouseDown(true);
SetMouseEventMask(B_POINTER_EVENTS, B_LOCK_WINDOW_FOCUS);
}
}
@@ -56,6 +62,8 @@ void
BitmapButton::MouseUp(BPoint where)
{
_SetMouseDown(false);
if (fMouseInside)
Invoke();
}
@@ -63,6 +71,8 @@ BSize
BitmapButton::MinSize()
{
BSize size = BitmapView::MinSize();
size.width += 6;
size.height += 6;
return BLayoutUtils::ComposeSize(ExplicitMinSize(), size);
}
@@ -86,6 +96,28 @@ BitmapButton::MaxSize()
// #pragma mark -
void
BitmapButton::DrawBackground(BRect& bounds, BRect updateRect)
{
if (Message() != NULL && (fMouseInside || fMouseDown)) {
rgb_color color = LowColor();
uint32 flags = 0;
if (fMouseDown)
flags |= BControlLook::B_ACTIVATED;
be_control_look->DrawButtonFrame(this, bounds, updateRect,
color, color, flags);
be_control_look->DrawButtonBackground(this, bounds, updateRect,
color, flags);
} else {
BitmapView::DrawBackground(bounds, updateRect);
}
}
// #pragma mark -
void
BitmapButton::_SetMouseInside(bool inside)
{
+6 -1
View File
@@ -13,7 +13,8 @@
class BitmapButton : public BitmapView, public BInvoker {
public:
BitmapButton(const char* name);
BitmapButton(const char* name,
BMessage* message = NULL);
virtual ~BitmapButton();
virtual void AttachedToWindow();
@@ -27,6 +28,10 @@ public:
virtual BSize PreferredSize();
virtual BSize MaxSize();
protected:
virtual void DrawBackground(BRect& bounds,
BRect updateRect);
private:
void _SetMouseInside(bool inside);
void _SetMouseDown(bool down);
+31 -7
View File
@@ -15,11 +15,11 @@
BitmapView::BitmapView(const char* name)
:
BView(name, B_WILL_DRAW | B_FULL_UPDATE_ON_RESIZE),
fBitmap(NULL)
fBitmap(NULL),
fScaleBitmap(true)
{
SetViewColor(B_TRANSPARENT_COLOR);
SetLowColor(ui_color(B_PANEL_BACKGROUND_COLOR));
SetDrawingMode(B_OP_OVER);
}
@@ -45,7 +45,7 @@ void
BitmapView::Draw(BRect updateRect)
{
BRect bounds(Bounds());
FillRect(updateRect, B_SOLID_LOW);
DrawBackground(bounds, updateRect);
if (fBitmap == NULL)
return;
@@ -54,10 +54,14 @@ BitmapView::Draw(BRect updateRect)
if (bitmapBounds.Width() <= 0.0f || bitmapBounds.Height() <= 0.0f)
return;
float hScale = bounds.Width() / bitmapBounds.Width();
float vScale = bounds.Height() / bitmapBounds.Height();
float scale = std::min(hScale, vScale);
float scale = 1.0f;
if (fScaleBitmap) {
float hScale = bounds.Width() / bitmapBounds.Width();
float vScale = bounds.Height() / bitmapBounds.Height();
scale = std::min(hScale, vScale);
}
float width = bitmapBounds.Width() * scale;
float height = bitmapBounds.Height() * scale;
@@ -90,6 +94,7 @@ BitmapView::Draw(BRect updateRect)
bounds.right = ceilf(bounds.left + width);
bounds.bottom = ceilf(bounds.top + height);
SetDrawingMode(B_OP_OVER);
DrawBitmap(fBitmap, bitmapBounds, bounds, B_FILTER_BITMAP_BILINEAR);
}
@@ -141,3 +146,22 @@ BitmapView::SetBitmap(const BBitmap* bitmap)
Invalidate();
}
void
BitmapView::SetScaleBitmap(bool scaleBitmap)
{
if (scaleBitmap == fScaleBitmap)
return;
fScaleBitmap = scaleBitmap;
Invalidate();
}
void
BitmapView::DrawBackground(BRect& bounds, BRect updateRect)
{
FillRect(updateRect, B_SOLID_LOW);
}
+6
View File
@@ -23,9 +23,15 @@ public:
virtual BSize MaxSize();
void SetBitmap(const BBitmap* bitmap);
void SetScaleBitmap(bool scaleBitmap);
protected:
virtual void DrawBackground(BRect& bounds,
BRect updateRect);
private:
const BBitmap* fBitmap;
bool fScaleBitmap;
};