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:
@@ -6,16 +6,19 @@
|
|||||||
|
|
||||||
#include "BitmapButton.h"
|
#include "BitmapButton.h"
|
||||||
|
|
||||||
|
#include <ControlLook.h>
|
||||||
#include <LayoutUtils.h>
|
#include <LayoutUtils.h>
|
||||||
#include <Window.h>
|
#include <Window.h>
|
||||||
|
|
||||||
|
|
||||||
BitmapButton::BitmapButton(const char* name)
|
BitmapButton::BitmapButton(const char* name, BMessage* message)
|
||||||
:
|
:
|
||||||
BitmapView(name),
|
BitmapView(name),
|
||||||
|
BInvoker(message, NULL, NULL),
|
||||||
fMouseInside(false),
|
fMouseInside(false),
|
||||||
fMouseDown(false)
|
fMouseDown(false)
|
||||||
{
|
{
|
||||||
|
SetScaleBitmap(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -48,7 +51,10 @@ BitmapButton::MouseMoved(BPoint where, uint32 transit,
|
|||||||
void
|
void
|
||||||
BitmapButton::MouseDown(BPoint where)
|
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)
|
BitmapButton::MouseUp(BPoint where)
|
||||||
{
|
{
|
||||||
_SetMouseDown(false);
|
_SetMouseDown(false);
|
||||||
|
if (fMouseInside)
|
||||||
|
Invoke();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -63,6 +71,8 @@ BSize
|
|||||||
BitmapButton::MinSize()
|
BitmapButton::MinSize()
|
||||||
{
|
{
|
||||||
BSize size = BitmapView::MinSize();
|
BSize size = BitmapView::MinSize();
|
||||||
|
size.width += 6;
|
||||||
|
size.height += 6;
|
||||||
return BLayoutUtils::ComposeSize(ExplicitMinSize(), size);
|
return BLayoutUtils::ComposeSize(ExplicitMinSize(), size);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -86,6 +96,28 @@ BitmapButton::MaxSize()
|
|||||||
// #pragma mark -
|
// #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
|
void
|
||||||
BitmapButton::_SetMouseInside(bool inside)
|
BitmapButton::_SetMouseInside(bool inside)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -13,7 +13,8 @@
|
|||||||
|
|
||||||
class BitmapButton : public BitmapView, public BInvoker {
|
class BitmapButton : public BitmapView, public BInvoker {
|
||||||
public:
|
public:
|
||||||
BitmapButton(const char* name);
|
BitmapButton(const char* name,
|
||||||
|
BMessage* message = NULL);
|
||||||
virtual ~BitmapButton();
|
virtual ~BitmapButton();
|
||||||
|
|
||||||
virtual void AttachedToWindow();
|
virtual void AttachedToWindow();
|
||||||
@@ -27,6 +28,10 @@ public:
|
|||||||
virtual BSize PreferredSize();
|
virtual BSize PreferredSize();
|
||||||
virtual BSize MaxSize();
|
virtual BSize MaxSize();
|
||||||
|
|
||||||
|
protected:
|
||||||
|
virtual void DrawBackground(BRect& bounds,
|
||||||
|
BRect updateRect);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void _SetMouseInside(bool inside);
|
void _SetMouseInside(bool inside);
|
||||||
void _SetMouseDown(bool down);
|
void _SetMouseDown(bool down);
|
||||||
|
|||||||
@@ -15,11 +15,11 @@
|
|||||||
BitmapView::BitmapView(const char* name)
|
BitmapView::BitmapView(const char* name)
|
||||||
:
|
:
|
||||||
BView(name, B_WILL_DRAW | B_FULL_UPDATE_ON_RESIZE),
|
BView(name, B_WILL_DRAW | B_FULL_UPDATE_ON_RESIZE),
|
||||||
fBitmap(NULL)
|
fBitmap(NULL),
|
||||||
|
fScaleBitmap(true)
|
||||||
{
|
{
|
||||||
SetViewColor(B_TRANSPARENT_COLOR);
|
SetViewColor(B_TRANSPARENT_COLOR);
|
||||||
SetLowColor(ui_color(B_PANEL_BACKGROUND_COLOR));
|
SetLowColor(ui_color(B_PANEL_BACKGROUND_COLOR));
|
||||||
SetDrawingMode(B_OP_OVER);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -45,7 +45,7 @@ void
|
|||||||
BitmapView::Draw(BRect updateRect)
|
BitmapView::Draw(BRect updateRect)
|
||||||
{
|
{
|
||||||
BRect bounds(Bounds());
|
BRect bounds(Bounds());
|
||||||
FillRect(updateRect, B_SOLID_LOW);
|
DrawBackground(bounds, updateRect);
|
||||||
|
|
||||||
if (fBitmap == NULL)
|
if (fBitmap == NULL)
|
||||||
return;
|
return;
|
||||||
@@ -54,10 +54,14 @@ BitmapView::Draw(BRect updateRect)
|
|||||||
if (bitmapBounds.Width() <= 0.0f || bitmapBounds.Height() <= 0.0f)
|
if (bitmapBounds.Width() <= 0.0f || bitmapBounds.Height() <= 0.0f)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
float hScale = bounds.Width() / bitmapBounds.Width();
|
float scale = 1.0f;
|
||||||
float vScale = bounds.Height() / bitmapBounds.Height();
|
|
||||||
|
if (fScaleBitmap) {
|
||||||
float scale = std::min(hScale, vScale);
|
float hScale = bounds.Width() / bitmapBounds.Width();
|
||||||
|
float vScale = bounds.Height() / bitmapBounds.Height();
|
||||||
|
|
||||||
|
scale = std::min(hScale, vScale);
|
||||||
|
}
|
||||||
|
|
||||||
float width = bitmapBounds.Width() * scale;
|
float width = bitmapBounds.Width() * scale;
|
||||||
float height = bitmapBounds.Height() * scale;
|
float height = bitmapBounds.Height() * scale;
|
||||||
@@ -90,6 +94,7 @@ BitmapView::Draw(BRect updateRect)
|
|||||||
bounds.right = ceilf(bounds.left + width);
|
bounds.right = ceilf(bounds.left + width);
|
||||||
bounds.bottom = ceilf(bounds.top + height);
|
bounds.bottom = ceilf(bounds.top + height);
|
||||||
|
|
||||||
|
SetDrawingMode(B_OP_OVER);
|
||||||
DrawBitmap(fBitmap, bitmapBounds, bounds, B_FILTER_BITMAP_BILINEAR);
|
DrawBitmap(fBitmap, bitmapBounds, bounds, B_FILTER_BITMAP_BILINEAR);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -141,3 +146,22 @@ BitmapView::SetBitmap(const BBitmap* bitmap)
|
|||||||
|
|
||||||
Invalidate();
|
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);
|
||||||
|
}
|
||||||
|
|||||||
@@ -23,9 +23,15 @@ public:
|
|||||||
virtual BSize MaxSize();
|
virtual BSize MaxSize();
|
||||||
|
|
||||||
void SetBitmap(const BBitmap* bitmap);
|
void SetBitmap(const BBitmap* bitmap);
|
||||||
|
void SetScaleBitmap(bool scaleBitmap);
|
||||||
|
|
||||||
|
protected:
|
||||||
|
virtual void DrawBackground(BRect& bounds,
|
||||||
|
BRect updateRect);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
const BBitmap* fBitmap;
|
const BBitmap* fBitmap;
|
||||||
|
bool fScaleBitmap;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user