* BIconButton now inherits from BControl, solving a TODO comment.

* I tried to reuse as much state from BControl as possible, so I removed a few
  states.
* Also made the flags private, and added protected SetInside()/IsInside()
  methods.
* Removed useless BIconButton::ID().
* Adjusted users.
* Minor cleanup, automatic white space cleanup.
-alpha


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@41611 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Axel Dörfler
2011-05-20 21:10:15 +00:00
parent 4b6dc1bc09
commit a1e3c32da9
7 changed files with 143 additions and 157 deletions
+18 -36
View File
@@ -11,12 +11,9 @@
//! GUI class that loads an image from disk and shows it as clickable button.
// TODO: inherit from BControl
#include <Invoker.h>
#include <Control.h>
#include <String.h>
#include <View.h>
class BBitmap;
@@ -26,13 +23,12 @@ class BMimeType;
namespace BPrivate {
class BIconButton : public BView, public BInvoker {
class BIconButton : public BControl {
public:
BIconButton(const char* name,
uint32 id,
const char* label = NULL,
BMessage* message = NULL,
BHandler* target = NULL);
const char* label = NULL,
BMessage* message = NULL,
BHandler* target = NULL);
virtual ~BIconButton();
// BView interface
@@ -63,49 +59,34 @@ public:
// BInvoker interface
virtual status_t Invoke(BMessage* message = NULL);
// BControl interface
virtual void SetValue(int32 value);
virtual void SetEnabled(bool enable);
// BIconButton
bool IsValid() const;
virtual int32 Value() const;
virtual void SetValue(int32 value);
bool IsEnabled() const;
void SetEnabled(bool enable);
void SetPressed(bool pressed);
bool IsPressed() const;
uint32 ID() const
{ return fID; }
status_t SetIcon(int32 resourceID);
status_t SetIcon(const char* pathToBitmap);
status_t SetIcon(const BBitmap* bitmap);
status_t SetIcon(const BMimeType* fileType,
bool small = true);
bool small = true);
status_t SetIcon(const unsigned char* bitsFromQuickRes,
uint32 width, uint32 height,
color_space format,
bool convertToBW = false);
uint32 width, uint32 height,
color_space format,
bool convertToBW = false);
void ClearIcon();
void TrimIcon(bool keepAspect = true);
BBitmap* Bitmap() const;
// caller has to delete the returned bitmap
const BString& Label() const;
protected:
enum {
STATE_NONE = 0x0000,
STATE_TRACKING = 0x0001,
STATE_PRESSED = 0x0002,
STATE_ENABLED = 0x0004,
STATE_INSIDE = 0x0008,
STATE_FORCE_PRESSED = 0x0010,
};
void _AddFlags(uint32 flags);
void _ClearFlags(uint32 flags);
bool _HasFlags(uint32 flags) const;
bool IsInside() const;
void SetInside(bool inside);
private:
BBitmap* _ConvertToRGB32(const BBitmap* bitmap) const;
@@ -113,15 +94,16 @@ private:
void _DeleteBitmaps();
void _SendMessage() const;
void _Update();
void _SetTracking(bool state);
void _SetFlags(uint32 flags, bool set);
bool _HasFlags(uint32 flags) const;
private:
uint32 fButtonState;
int32 fID;
BBitmap* fNormalBitmap;
BBitmap* fDisabledBitmap;
BBitmap* fClickedBitmap;
BBitmap* fDisabledClickedBitmap;
BString fLabel;
BHandler* fTargetCache;
};
+20 -17
View File
@@ -3,15 +3,16 @@
* All rights reserved. Distributed under the terms of the MIT License.
*/
#include "LaunchButton.h"
#include <malloc.h> // string.h is not enough on Haiku?!?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <Application.h>
#include <AppDefs.h>
#include <AppFileInfo.h>
#include <Application.h>
#include <Bitmap.h>
#include <Catalog.h>
#include <File.h>
@@ -24,25 +25,24 @@
#include "PadView.h"
#include "MainWindow.h"
#undef B_TRANSLATE_CONTEXT
#define B_TRANSLATE_CONTEXT "LaunchBox"
static const float kDragStartDist = 10.0;
static const float kDragBitmapAlphaScale = 0.6;
static const char* kEmptyHelpString = B_TRANSLATE("You can drag an icon here.");
bigtime_t
LaunchButton::sClickSpeed = 0;
bool
LaunchButton::sIgnoreDoubleClick = true;
bigtime_t LaunchButton::sClickSpeed = 0;
bool LaunchButton::sIgnoreDoubleClick = true;
LaunchButton::LaunchButton(const char* name, uint32 id, const char* label,
LaunchButton::LaunchButton(const char* name, const char* label,
BMessage* message, BHandler* target)
:
BIconButton(name, id, label, message, target),
BIconButton(name, label, message, target),
fRef(NULL),
fAppSig(NULL),
fDescription(""),
@@ -107,7 +107,7 @@ LaunchButton::MessageReceived(BMessage* message)
switch (message->what) {
case B_SIMPLE_DATA:
case B_REFS_RECEIVED: {
entry_ref ref;
entry_ref ref;
if (message->FindRef("refs", &ref) == B_OK) {
if (fRef) {
if (ref != *fRef) {
@@ -166,10 +166,10 @@ LaunchButton::MouseDown(BPoint where)
if (BMessage* message = Window()->CurrentMessage()) {
uint32 buttons;
message->FindInt32("buttons", (int32*)&buttons);
if (buttons & B_SECONDARY_MOUSE_BUTTON) {
if ((buttons & B_SECONDARY_MOUSE_BUTTON) != 0 && IsInside()) {
if (PadView* parent = dynamic_cast<PadView*>(Parent())) {
parent->DisplayMenu(ConvertToParent(where), this);
_ClearFlags(STATE_INSIDE);
SetInside(false);
callInherited = false;
}
} else {
@@ -211,12 +211,15 @@ LaunchButton::MouseMoved(BPoint where, uint32 transit,
}
}
// see if we should create a drag message
if (_HasFlags(STATE_TRACKING) && fRef) {
if (IsTracking() && fRef != NULL) {
BPoint diff = where - fDragStart;
float dist = sqrtf(diff.x * diff.x + diff.y * diff.y);
if (dist >= kDragStartDist) {
// stop tracking
_ClearFlags(STATE_PRESSED | STATE_TRACKING | STATE_INSIDE);
SetTracking(false);
SetPressed(false);
SetInside(false);
// create drag bitmap and message
if (BBitmap* bitmap = Bitmap()) {
if (bitmap->ColorSpace() == B_RGB32) {
@@ -263,11 +266,11 @@ LaunchButton::PreferredSize()
float hPadding = max_c(6.0, ceilf(minHeight / 3.0));
float vPadding = max_c(6.0, ceilf(minWidth / 3.0));
if (Label().CountChars() > 0) {
if (Label() != NULL && Label()[0] != '\0') {
font_height fh;
GetFontHeight(&fh);
minHeight += ceilf(fh.ascent + fh.descent) + vPadding;
minWidth += StringWidth(Label().String()) + vPadding;
minWidth += StringWidth(Label()) + vPadding;
}
return BSize(minWidth + hPadding, minHeight + vPadding);
@@ -389,7 +392,7 @@ LaunchButton::_UpdateToolTip()
BString helper(fRef->name);
if (fDescription.CountChars() > 0) {
if (fDescription != helper)
helper << "\n\n" << fDescription.String();
helper << "\n\n" << fDescription.String();
} else {
BFile file(fRef, B_READ_ONLY);
BAppFileInfo appFileInfo;
+4 -4
View File
@@ -21,7 +21,7 @@ enum {
class LaunchButton : public BIconButton {
public:
LaunchButton(const char* name, uint32 id,
LaunchButton(const char* name,
const char* label = NULL,
BMessage* message = NULL,
BHandler* target = NULL);
@@ -73,12 +73,12 @@ private:
private:
entry_ref* fRef;
char* fAppSig;
BString fDescription;
BString fDescription;
bool fAnticipatingDrop;
bigtime_t fLastClickTime;
BPoint fDragStart;
uint32 fIconSize;
static bigtime_t sClickSpeed;
+11 -20
View File
@@ -36,7 +36,6 @@ MainWindow::MainWindow(const char* name, BRect frame, bool addDefaultButtons)
B_ALL_WORKSPACES),
fSettings(new BMessage('sett')),
fPadView(new PadView("pad view")),
fLastID(0),
fNamePanelFrame(-1000.0, -1000.0, -800.0, -900.0),
fAutoRaise(false),
fShowOnAllWorkspaces(true)
@@ -66,7 +65,6 @@ MainWindow::MainWindow(const char* name, BRect frame, BMessage* settings)
B_ALL_WORKSPACES),
fSettings(settings),
fPadView(new PadView("pad view")),
fLastID(0),
fNamePanelFrame(-1000.0, -1000.0, -900.0, -900.0),
fAutoRaise(false),
fShowOnAllWorkspaces(true)
@@ -182,7 +180,7 @@ MainWindow::MessageReceived(BMessage* message)
case MSG_ADD_SLOT: {
LaunchButton* button;
if (message->FindPointer("be:source", (void**)&button) >= B_OK) {
fPadView->AddButton(new LaunchButton("launch button", fLastID++,
fPadView->AddButton(new LaunchButton("launch button",
NULL, new BMessage(MSG_LAUNCH)), button);
}
break;
@@ -378,7 +376,7 @@ MainWindow::LoadSettings(const BMessage* message)
const char* path;
bool buttonAdded = false;
for (int32 i = 0; message->FindString("path", i, &path) >= B_OK; i++) {
LaunchButton* button = new LaunchButton("launch button", fLastID++,
LaunchButton* button = new LaunchButton("launch button",
NULL, new BMessage(MSG_LAUNCH));
fPadView->AddButton(button);
BString signature;
@@ -555,38 +553,33 @@ void
MainWindow::_AddDefaultButtons()
{
// Mail
LaunchButton* button = new LaunchButton("launch button", fLastID++, NULL,
LaunchButton* button = new LaunchButton("launch button", NULL,
new BMessage(MSG_LAUNCH));
fPadView->AddButton(button);
button->SetTo("application/x-vnd.Be-MAIL", true);
// StyledEdit
button = new LaunchButton("launch button", fLastID++, NULL,
new BMessage(MSG_LAUNCH));
button = new LaunchButton("launch button", NULL, new BMessage(MSG_LAUNCH));
fPadView->AddButton(button);
button->SetTo("application/x-vnd.Haiku-StyledEdit", true);
// ShowImage
button = new LaunchButton("launch button", fLastID++, NULL,
new BMessage(MSG_LAUNCH));
button = new LaunchButton("launch button", NULL, new BMessage(MSG_LAUNCH));
fPadView->AddButton(button);
button->SetTo("application/x-vnd.Haiku-ShowImage", true);
// MediaPlayer
button = new LaunchButton("launch button", fLastID++, NULL,
new BMessage(MSG_LAUNCH));
button = new LaunchButton("launch button", NULL, new BMessage(MSG_LAUNCH));
fPadView->AddButton(button);
button->SetTo("application/x-vnd.Haiku-MediaPlayer", true);
// DeskCalc
button = new LaunchButton("launch button", fLastID++, NULL,
new BMessage(MSG_LAUNCH));
button = new LaunchButton("launch button", NULL, new BMessage(MSG_LAUNCH));
fPadView->AddButton(button);
button->SetTo("application/x-vnd.Haiku-DeskCalc", true);
// Terminal
button = new LaunchButton("launch button", fLastID++, NULL,
new BMessage(MSG_LAUNCH));
button = new LaunchButton("launch button", NULL, new BMessage(MSG_LAUNCH));
fPadView->AddButton(button);
button->SetTo("application/x-vnd.Haiku-Terminal", true);
}
@@ -595,16 +588,14 @@ MainWindow::_AddDefaultButtons()
void
MainWindow::_AddEmptyButtons()
{
LaunchButton* button = new LaunchButton("launch button", fLastID++, NULL,
LaunchButton* button = new LaunchButton("launch button", NULL,
new BMessage(MSG_LAUNCH));
fPadView->AddButton(button);
button = new LaunchButton("launch button", fLastID++, NULL,
new BMessage(MSG_LAUNCH));
button = new LaunchButton("launch button", NULL, new BMessage(MSG_LAUNCH));
fPadView->AddButton(button);
button = new LaunchButton("launch button", fLastID++, NULL,
new BMessage(MSG_LAUNCH));
button = new LaunchButton("launch button", NULL, new BMessage(MSG_LAUNCH));
fPadView->AddButton(button);
}
+3 -3
View File
@@ -27,9 +27,9 @@ enum {
class MainWindow : public BWindow {
public:
MainWindow(const char* name, BRect frame,
bool addDefaultButtons = false);
bool addDefaultButtons = false);
MainWindow(const char* name, BRect frame,
BMessage* settings);
BMessage* settings);
virtual ~MainWindow();
// BWindow interface
@@ -65,9 +65,9 @@ private:
void _NotifySettingsChanged();
private:
BMessage* fSettings;
PadView* fPadView;
int32 fLastID;
float fBorderDist;
BPoint fScreenPosition;
+1 -1
View File
@@ -54,7 +54,7 @@ void
ToolBarView::AddAction(BMessage* message, BHandler* target, const BBitmap* icon,
const char* toolTipText)
{
BIconButton* button = new BIconButton(NULL, 0, NULL, message, target);
BIconButton* button = new BIconButton(NULL, NULL, message, target);
button->SetIcon(icon);
if (toolTipText != NULL)
button->SetToolTip(toolTipText);
+86 -76
View File
@@ -4,6 +4,7 @@
*
* Authors:
* Stephan Aßmus <[email protected]>
* Axel Dörfler, [email protected].
*/
@@ -29,20 +30,30 @@
#include <Window.h>
BIconButton::BIconButton(const char* name, uint32 id, const char* label,
BMessage* message, BHandler* target)
namespace BPrivate {
enum {
STATE_NONE = 0x0000,
STATE_PRESSED = 0x0002,
STATE_INSIDE = 0x0008,
STATE_FORCE_PRESSED = 0x0010,
};
BIconButton::BIconButton(const char* name, const char* label,
BMessage* message, BHandler* target)
:
BView(name, B_WILL_DRAW),
BInvoker(message, target),
fButtonState(STATE_ENABLED),
fID(id),
BControl(name, label, message, B_WILL_DRAW),
fButtonState(0),
fNormalBitmap(NULL),
fDisabledBitmap(NULL),
fClickedBitmap(NULL),
fDisabledClickedBitmap(NULL),
fLabel(label),
fTargetCache(target)
{
SetTarget(target);
SetLowColor(ui_color(B_PANEL_BACKGROUND_COLOR));
SetViewColor(B_TRANSPARENT_32_BIT);
}
@@ -126,8 +137,8 @@ BIconButton::Draw(BRect updateRect)
bool
BIconButton::ShouldDrawBorder() const
{
return ((IsEnabled() && (_HasFlags(STATE_INSIDE)
|| _HasFlags(STATE_TRACKING))) || _HasFlags(STATE_FORCE_PRESSED));
return (IsEnabled() && (IsInside() || IsTracking()))
|| _HasFlags(STATE_FORCE_PRESSED);
}
@@ -155,12 +166,14 @@ BIconButton::MouseDown(BPoint where)
if (!IsValid())
return;
if (_HasFlags(STATE_ENABLED)) {
if (IsEnabled()) {
if (Bounds().Contains(where)) {
SetMouseEventMask(B_POINTER_EVENTS, B_LOCK_WINDOW_FOCUS);
_AddFlags(STATE_PRESSED | STATE_TRACKING);
_SetFlags(STATE_PRESSED, true);
_SetTracking(true);
} else {
_ClearFlags(STATE_PRESSED | STATE_TRACKING);
_SetFlags(STATE_PRESSED, false);
_SetTracking(false);
}
}
}
@@ -172,12 +185,14 @@ BIconButton::MouseUp(BPoint where)
if (!IsValid())
return;
if (_HasFlags(STATE_ENABLED) && _HasFlags(STATE_PRESSED)
if (IsEnabled() && _HasFlags(STATE_PRESSED)
&& Bounds().Contains(where)) {
Invoke();
} else if (Bounds().Contains(where))
_AddFlags(STATE_INSIDE);
_ClearFlags(STATE_PRESSED | STATE_TRACKING);
SetInside(true);
_SetFlags(STATE_PRESSED, false);
_SetTracking(false);
}
@@ -194,19 +209,13 @@ BIconButton::MouseMoved(BPoint where, uint32 transit, const BMessage* message)
MouseUp(where);
return;
}
if (buttons && !_HasFlags(STATE_TRACKING))
if (buttons != 0 && !IsTracking())
return;
if ((transit == B_INSIDE_VIEW || transit == B_ENTERED_VIEW)
&& _HasFlags(STATE_ENABLED))
_AddFlags(STATE_INSIDE);
else
_ClearFlags(STATE_INSIDE);
if (_HasFlags(STATE_TRACKING)) {
if (Bounds().Contains(where))
_AddFlags(STATE_PRESSED);
else
_ClearFlags(STATE_PRESSED);
}
SetInside((transit == B_INSIDE_VIEW || transit == B_ENTERED_VIEW)
&& IsEnabled());
if (IsTracking())
_SetFlags(STATE_PRESSED, Bounds().Contains(where));
}
@@ -229,11 +238,11 @@ BIconButton::GetPreferredSize(float* width, float* height)
float hPadding = max_c(6.0f, ceilf(minHeight / 4.0f));
float vPadding = max_c(6.0f, ceilf(minWidth / 4.0f));
if (fLabel.CountChars() > 0) {
if (Label() != NULL && Label()[0] != '\0') {
font_height fh;
GetFontHeight(&fh);
minHeight += ceilf(fh.ascent + fh.descent) + vPadding;
minWidth += StringWidth(fLabel.String()) + vPadding;
minWidth += StringWidth(Label()) + vPadding;
}
if (width)
@@ -269,7 +278,6 @@ BIconButton::Invoke(BMessage* message)
clone.AddInt64("be:when", system_time());
clone.AddPointer("be:source", (BView*)this);
clone.AddInt32("be:value", Value());
clone.AddInt32("id", ID());
return BInvoker::Invoke(&clone);
}
return BInvoker::Invoke(message);
@@ -279,10 +287,7 @@ BIconButton::Invoke(BMessage* message)
void
BIconButton::SetPressed(bool pressed)
{
if (pressed)
_AddFlags(STATE_FORCE_PRESSED);
else
_ClearFlags(STATE_FORCE_PRESSED);
_SetFlags(STATE_FORCE_PRESSED, pressed);
}
@@ -618,44 +623,39 @@ BIconButton::Bitmap() const
}
const BString&
BIconButton::Label() const
{
return fLabel;
}
int32
BIconButton::Value() const
{
return _HasFlags(STATE_PRESSED) ? B_CONTROL_ON : B_CONTROL_OFF;
}
void
BIconButton::SetValue(int32 value)
{
if (value)
_AddFlags(STATE_PRESSED);
else
_ClearFlags(STATE_PRESSED);
}
bool
BIconButton::IsEnabled() const
{
return _HasFlags(STATE_ENABLED) ? B_CONTROL_ON : B_CONTROL_OFF;
BControl::SetValue(value);
_SetFlags(STATE_PRESSED, value != 0);
}
void
BIconButton::SetEnabled(bool enabled)
{
if (enabled)
_AddFlags(STATE_ENABLED);
else
_ClearFlags(STATE_ENABLED | STATE_TRACKING | STATE_INSIDE);
BControl::SetEnabled(enabled);
if (!enabled) {
SetInside(false);
_SetTracking(false);
}
}
// #pragma mark - protected
bool
BIconButton::IsInside() const
{
return _HasFlags(STATE_INSIDE);
}
void
BIconButton::SetInside(bool inside)
{
_SetFlags(STATE_INSIDE, inside);
}
@@ -690,7 +690,7 @@ status_t
BIconButton::_MakeBitmaps(const BBitmap* bitmap)
{
status_t status = bitmap ? bitmap->InitCheck() : B_BAD_VALUE;
if (status >= B_OK) {
if (status == B_OK) {
// make our own versions of the bitmap
BRect b(bitmap->Bounds());
_DeleteBitmaps();
@@ -838,20 +838,16 @@ BIconButton::_Update()
void
BIconButton::_AddFlags(uint32 flags)
BIconButton::_SetFlags(uint32 flags, bool set)
{
if (!_HasFlags(flags)) {
fButtonState |= flags;
_Update();
}
}
if (_HasFlags(flags) != set) {
if (set)
fButtonState |= flags;
else
fButtonState &= ~flags;
void
BIconButton::_ClearFlags(uint32 flags)
{
if (_HasFlags(flags)) {
fButtonState &= ~flags;
if ((flags & STATE_PRESSED) != 0)
SetValueNoUpdate(set ? B_CONTROL_ON : B_CONTROL_OFF);
_Update();
}
}
@@ -863,3 +859,17 @@ BIconButton::_HasFlags(uint32 flags) const
return (fButtonState & flags) != 0;
}
//! This one calls _Update() if needed; BControl::SetTracking() isn't virtual.
void
BIconButton::_SetTracking(bool tracking)
{
if (IsTracking() == tracking)
return;
SetTracking(tracking);
_Update();
}
} // namespace BPrivate