More scalable ToolButton
Replaces libinterface's ToolButton, a custom button that can display a pop-up menu on click (along with a custom image). It didn't scale well with different themes and font-sizes compared to BButtons― which ToolButton is supposed to look like to begin with. A BButton-derived class is used instead, so these buttons look more in-place among other buttons.
This commit is contained in:
parent
0ffe3e5ce8
commit
33ad4d8acf
|
@ -15,7 +15,7 @@
|
|||
#include <ScrollView.h>
|
||||
|
||||
#include <libinterface/BitmapMenuItem.h>
|
||||
#include <libinterface/ToolButton.h>
|
||||
#include <libinterface/MenuButton.h>
|
||||
|
||||
#include "AccountDialog.h"
|
||||
#include "AccountListItem.h"
|
||||
|
@ -79,7 +79,7 @@ PreferencesAccounts::PreferencesAccounts()
|
|||
fProtosMenu->AddItem(item);
|
||||
}
|
||||
|
||||
ToolButton* proto = new ToolButton(B_TRANSLATE("Add"), NULL);
|
||||
MenuButton* proto = new MenuButton("addButton", B_TRANSLATE("Add"), NULL);
|
||||
proto->SetMenu(fProtosMenu);
|
||||
fDelButton = new BButton(B_TRANSLATE_COMMENT("Del", "Short for 'delete'"),
|
||||
new BMessage(kDelAccount));
|
||||
|
|
|
@ -11,7 +11,6 @@ class BButton;
|
|||
class BListView;
|
||||
class BPopUpMenu;
|
||||
|
||||
class ToolButton;
|
||||
class ProtocolSettings;
|
||||
|
||||
|
||||
|
@ -33,6 +32,4 @@ private:
|
|||
bool _AccountEnabled(const char* account);
|
||||
};
|
||||
|
||||
|
||||
#endif // _PREFERENCES_ACCOUNTS_H
|
||||
|
||||
|
|
|
@ -36,9 +36,9 @@ SRCS = \
|
|||
libs/libinterface/BitmapUtils.cpp \
|
||||
libs/libinterface/BitmapView.cpp \
|
||||
libs/libinterface/Divider.cpp \
|
||||
libs/libinterface/MenuButton.cpp \
|
||||
libs/libinterface/NotifyingTextView.cpp \
|
||||
libs/libinterface/PictureView.cpp \
|
||||
libs/libinterface/ToolButton.cpp
|
||||
libs/libinterface/PictureView.cpp
|
||||
|
||||
# Specify the resource definition files to use. Full or relative paths can be
|
||||
# used.
|
||||
|
|
|
@ -0,0 +1,54 @@
|
|||
/*
|
||||
* Copyright 2021, Jaidyn Levesque <jadedctrl@teknik.io>
|
||||
* All rights reserved. Distributed under the terms of the MIT license.
|
||||
*/
|
||||
|
||||
#include "MenuButton.h"
|
||||
|
||||
#include <PopUpMenu.h>
|
||||
|
||||
|
||||
MenuButton::MenuButton(const char* name, const char* label, BMessage* message)
|
||||
:
|
||||
BButton(name, BString(label).Append(" ▾"), message),
|
||||
fMenu(NULL)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
MenuButton::MouseDown(BPoint where)
|
||||
{
|
||||
BButton::MouseDown(where);
|
||||
if (fMenu != NULL)
|
||||
fMenu->Go(ConvertToScreen(where), true, true, true);
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
MenuButton::MouseUp(BPoint where)
|
||||
{
|
||||
BButton::MouseUp(where);
|
||||
SetValue(B_CONTROL_OFF);
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
MenuButton::MouseMoved(BPoint where, uint32 code, const BMessage* dragMsg)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
|
||||
BPopUpMenu*
|
||||
MenuButton::Menu()
|
||||
{
|
||||
return fMenu;
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
MenuButton::SetMenu(BPopUpMenu* menu)
|
||||
{
|
||||
fMenu = menu;
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
/*
|
||||
* Copyright 2021, Jaidyn Levesque <jadedctrl@teknik.io>
|
||||
* All rights reserved. Distributed under the terms of the MIT license.
|
||||
*/
|
||||
#ifndef _MENU_BUTTON_H
|
||||
#define _MENU_BUTTON_H
|
||||
|
||||
#include <Button.h>
|
||||
|
||||
class BPopUpMenu;
|
||||
|
||||
|
||||
class MenuButton : public BButton {
|
||||
public:
|
||||
MenuButton(const char* name, const char* label,
|
||||
BMessage* message);
|
||||
|
||||
virtual void MouseDown(BPoint where);
|
||||
virtual void MouseUp(BPoint where);
|
||||
virtual void MouseMoved(BPoint where, uint32 code, const BMessage* dragMsg);
|
||||
|
||||
BPopUpMenu* Menu();
|
||||
void SetMenu(BPopUpMenu* menu);
|
||||
|
||||
private:
|
||||
BPopUpMenu* fMenu;
|
||||
|
||||
};
|
||||
|
||||
#endif // _MENU_BUTTON_H
|
|
@ -1,434 +0,0 @@
|
|||
/*
|
||||
* Copyright 2010, Pier Luigi Fiorini. All rights reserved.
|
||||
* Distributed under the terms of the MIT License.
|
||||
*
|
||||
* Authors:
|
||||
* Pier Luigi Fiorini, pierluigi.fiorini@gmail.com
|
||||
*/
|
||||
|
||||
#include <new>
|
||||
|
||||
#include <ControlLook.h>
|
||||
#include <LayoutUtils.h>
|
||||
#include <Looper.h>
|
||||
#include <MenuItem.h>
|
||||
#include <PopUpMenu.h>
|
||||
#include <Window.h>
|
||||
|
||||
#include <binary_compatibility/Interface.h>
|
||||
|
||||
#include "BitmapUtils.h"
|
||||
#include "ToolButton.h"
|
||||
|
||||
|
||||
const float kPopUpMarkerSize = 5.0f;
|
||||
const float kPopUpMarkerTop = 0.5f;
|
||||
const float kPopUpMarkerRect = (kPopUpMarkerSize * 2) + 2;
|
||||
const uint32 kToolbarIconSize = 16;
|
||||
// this should go on BControlLook
|
||||
|
||||
|
||||
ToolButton::ToolButton(const char* name, const char* label,
|
||||
BMessage* message, uint32 flags)
|
||||
:
|
||||
BControl(name, label, message,
|
||||
flags | B_WILL_DRAW | B_FULL_UPDATE_ON_RESIZE),
|
||||
fBitmap(NULL),
|
||||
fMenu(NULL),
|
||||
fPreferredSize(-1, -1)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
ToolButton::ToolButton(const char* label, BMessage* message)
|
||||
:
|
||||
BControl(NULL, label, message,
|
||||
B_WILL_DRAW | B_NAVIGABLE | B_FULL_UPDATE_ON_RESIZE),
|
||||
fBitmap(NULL),
|
||||
fMenu(NULL),
|
||||
fPreferredSize(-1, -1)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
ToolButton::ToolButton(BMessage* archive)
|
||||
:
|
||||
BControl(archive),
|
||||
fBitmap(NULL),
|
||||
fMenu(NULL),
|
||||
fPreferredSize(-1, -1)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
ToolButton::~ToolButton()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
BArchivable*
|
||||
ToolButton::Instantiate(BMessage* archive)
|
||||
{
|
||||
if (validate_instantiation(archive, "ToolButton"))
|
||||
return new(std::nothrow) ToolButton(archive);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
status_t
|
||||
ToolButton::Archive(BMessage* archive, bool deep) const
|
||||
{
|
||||
status_t err = BControl::Archive(archive, deep);
|
||||
if (err != B_OK)
|
||||
return err;
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
|
||||
BSize
|
||||
ToolButton::MinSize()
|
||||
{
|
||||
return BLayoutUtils::ComposeSize(ExplicitMinSize(),
|
||||
_ValidatePreferredSize());
|
||||
}
|
||||
|
||||
|
||||
BSize
|
||||
ToolButton::MaxSize()
|
||||
{
|
||||
return BLayoutUtils::ComposeSize(ExplicitMaxSize(),
|
||||
_ValidatePreferredSize());
|
||||
}
|
||||
|
||||
|
||||
BSize
|
||||
ToolButton::PreferredSize()
|
||||
{
|
||||
return BLayoutUtils::ComposeSize(ExplicitPreferredSize(),
|
||||
_ValidatePreferredSize());
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
ToolButton::Draw(BRect updateRect)
|
||||
{
|
||||
BRect bounds(Bounds());
|
||||
rgb_color base = ui_color(B_PANEL_BACKGROUND_COLOR);
|
||||
uint32 flags = be_control_look->Flags(this);
|
||||
|
||||
// Draw background and borders
|
||||
rgb_color button = IsEnabled() ? tint_color(base, 1.07f) : base;
|
||||
be_control_look->DrawButtonBackground(this, bounds, updateRect,
|
||||
button, flags, 0);
|
||||
be_control_look->DrawBorder(this, bounds, updateRect,
|
||||
base, B_PLAIN_BORDER, flags);
|
||||
|
||||
// Calculate popup marker space
|
||||
if (fMenu)
|
||||
bounds.right -= kPopUpMarkerRect;
|
||||
|
||||
// Draw bitmap
|
||||
if (Bitmap()) {
|
||||
SetDrawingMode(B_OP_ALPHA);
|
||||
SetBlendingMode(B_PIXEL_ALPHA, B_ALPHA_OVERLAY);
|
||||
|
||||
BRect frame(bounds);
|
||||
BPoint center = _Center(frame);
|
||||
frame.bottom = frame.top + kToolbarIconSize + 4;
|
||||
frame.top += 2;
|
||||
frame.left = center.x - (kToolbarIconSize / 2);
|
||||
frame.right = center.x + (kToolbarIconSize / 2);
|
||||
|
||||
DrawBitmap(fBitmap, fBitmap->Bounds(), frame, B_FILTER_BITMAP_BILINEAR);
|
||||
bounds.top = frame.bottom;
|
||||
}
|
||||
|
||||
// Draw label
|
||||
if (Label())
|
||||
be_control_look->DrawLabel(this, Label(), bounds, updateRect,
|
||||
base, flags, BAlignment(B_ALIGN_CENTER, B_ALIGN_MIDDLE));
|
||||
|
||||
if (fMenu) {
|
||||
// Draw popup marker
|
||||
bounds = Bounds();
|
||||
bounds.left = bounds.right - kPopUpMarkerRect;
|
||||
bounds.InsetBy(1, 1);
|
||||
|
||||
be_control_look->DrawButtonBackground(this, bounds, updateRect,
|
||||
tint_color(button, B_DARKEN_2_TINT), flags, 0);
|
||||
|
||||
float tint = IsEnabled() ? B_DARKEN_4_TINT : B_DARKEN_1_TINT;
|
||||
BPoint center(_Center(bounds));
|
||||
BPoint triangle[3];
|
||||
triangle[0] = center + BPoint(-(kPopUpMarkerSize / 2.0),
|
||||
-kPopUpMarkerTop);
|
||||
triangle[1] = center + BPoint(kPopUpMarkerSize, -kPopUpMarkerTop);
|
||||
triangle[2] = center + BPoint(0.0, (kPopUpMarkerSize / 2.0f)
|
||||
- kPopUpMarkerTop);
|
||||
|
||||
uint32 origFlags = Flags();
|
||||
SetFlags(origFlags | B_SUBPIXEL_PRECISE);
|
||||
SetHighColor(tint_color(base, tint));
|
||||
FillTriangle(triangle[0], triangle[1], triangle[2]);
|
||||
SetFlags(origFlags);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
ToolButton::AttachedToWindow()
|
||||
{
|
||||
BControl::AttachedToWindow();
|
||||
SetViewColor(B_TRANSPARENT_COLOR);
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
ToolButton::MouseDown(BPoint where)
|
||||
{
|
||||
if (!IsEnabled())
|
||||
return;
|
||||
|
||||
BRect rect(Bounds());
|
||||
rect.left = rect.right - kPopUpMarkerRect;
|
||||
|
||||
// NOTE the following code was disabled to allow the widget
|
||||
// show the menu when the click is received in any part of the
|
||||
// widget instead of the flap only.
|
||||
if (fMenu /*&& rect.Contains(where)*/) {
|
||||
BPoint coords(rect.left + 2, rect.bottom + 1);
|
||||
ConvertToScreen(&coords);
|
||||
|
||||
BMenuItem* selected = fMenu->Go(coords, false, true);
|
||||
if (selected) {
|
||||
BLooper* looper;
|
||||
BHandler* target = selected->Target(&looper);
|
||||
(void)looper->PostMessage(selected->Message(), target);
|
||||
}
|
||||
} else {
|
||||
SetValue(B_CONTROL_ON);
|
||||
|
||||
if (Window()->Flags() & B_ASYNCHRONOUS_CONTROLS) {
|
||||
SetTracking(true);
|
||||
SetMouseEventMask(B_POINTER_EVENTS, B_LOCK_WINDOW_FOCUS);
|
||||
} else {
|
||||
BRect bounds = Bounds();
|
||||
uint32 buttons;
|
||||
|
||||
do {
|
||||
Window()->UpdateIfNeeded();
|
||||
snooze(40000);
|
||||
|
||||
GetMouse(&where, &buttons, true);
|
||||
|
||||
bool inside = bounds.Contains(where);
|
||||
|
||||
if ((Value() == B_CONTROL_ON) != inside)
|
||||
SetValue(inside ? B_CONTROL_ON : B_CONTROL_OFF);
|
||||
} while (buttons != 0);
|
||||
|
||||
if (Value() == B_CONTROL_ON)
|
||||
Invoke();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
ToolButton::MouseMoved(BPoint where, uint32 transit, const BMessage* message)
|
||||
{
|
||||
if (!IsTracking())
|
||||
return;
|
||||
|
||||
bool inside = Bounds().Contains(where);
|
||||
|
||||
if ((Value() == B_CONTROL_ON) != inside)
|
||||
SetValue(inside ? B_CONTROL_ON : B_CONTROL_OFF);
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
ToolButton::MouseUp(BPoint where)
|
||||
{
|
||||
if (!IsTracking())
|
||||
return;
|
||||
|
||||
if (Bounds().Contains(where))
|
||||
Invoke();
|
||||
|
||||
SetTracking(false);
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
ToolButton::SetLabel(const char* string)
|
||||
{
|
||||
BControl::SetLabel(string);
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
ToolButton::MessageReceived(BMessage* message)
|
||||
{
|
||||
BControl::MessageReceived(message);
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
ToolButton::WindowActivated(bool active)
|
||||
{
|
||||
BControl::WindowActivated(active);
|
||||
}
|
||||
|
||||
|
||||
status_t
|
||||
ToolButton::Invoke(BMessage* message)
|
||||
{
|
||||
Sync();
|
||||
snooze(50000);
|
||||
|
||||
status_t err = BControl::Invoke(message);
|
||||
|
||||
SetValue(B_CONTROL_OFF);
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
|
||||
status_t
|
||||
ToolButton::Perform(perform_code code, void* data)
|
||||
{
|
||||
switch (code) {
|
||||
case PERFORM_CODE_MIN_SIZE:
|
||||
((perform_data_min_size*)data)->return_value
|
||||
= ToolButton::MinSize();
|
||||
return B_OK;
|
||||
case PERFORM_CODE_MAX_SIZE:
|
||||
((perform_data_max_size*)data)->return_value
|
||||
= ToolButton::MaxSize();
|
||||
return B_OK;
|
||||
case PERFORM_CODE_PREFERRED_SIZE:
|
||||
((perform_data_preferred_size*)data)->return_value
|
||||
= ToolButton::PreferredSize();
|
||||
return B_OK;
|
||||
case PERFORM_CODE_LAYOUT_ALIGNMENT:
|
||||
((perform_data_layout_alignment*)data)->return_value
|
||||
= ToolButton::LayoutAlignment();
|
||||
return B_OK;
|
||||
case PERFORM_CODE_HAS_HEIGHT_FOR_WIDTH:
|
||||
((perform_data_has_height_for_width*)data)->return_value
|
||||
= ToolButton::HasHeightForWidth();
|
||||
return B_OK;
|
||||
case PERFORM_CODE_GET_HEIGHT_FOR_WIDTH: {
|
||||
perform_data_get_height_for_width* _data
|
||||
= (perform_data_get_height_for_width*)data;
|
||||
ToolButton::GetHeightForWidth(_data->width, &_data->min, &_data->max,
|
||||
&_data->preferred);
|
||||
return B_OK;
|
||||
}
|
||||
case PERFORM_CODE_INVALIDATE_LAYOUT: {
|
||||
perform_data_invalidate_layout* _data
|
||||
= (perform_data_invalidate_layout*)_data;
|
||||
ToolButton::InvalidateLayout(_data->descendants);
|
||||
return B_OK;
|
||||
}
|
||||
case PERFORM_CODE_DO_LAYOUT:
|
||||
ToolButton::DoLayout();
|
||||
return B_OK;
|
||||
}
|
||||
|
||||
return BControl::Perform(code, data);
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
ToolButton::InvalidateLayout(bool descendants)
|
||||
{
|
||||
fPreferredSize.Set(-1, -1);
|
||||
BControl::InvalidateLayout(descendants);
|
||||
}
|
||||
|
||||
|
||||
BBitmap*
|
||||
ToolButton::Bitmap() const
|
||||
{
|
||||
return fBitmap;
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
ToolButton::SetBitmap(BBitmap* bitmap)
|
||||
{
|
||||
fBitmap = bitmap;
|
||||
}
|
||||
|
||||
|
||||
BPopUpMenu*
|
||||
ToolButton::Menu() const
|
||||
{
|
||||
return fMenu;
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
ToolButton::SetMenu(BPopUpMenu* menu)
|
||||
{
|
||||
fMenu = menu;
|
||||
}
|
||||
|
||||
|
||||
BPoint
|
||||
ToolButton::_Center(BRect rect)
|
||||
{
|
||||
BPoint center(roundf((rect.left + rect.right) / 2.0f),
|
||||
roundf((rect.top + rect.bottom) / 2.0f));
|
||||
return center;
|
||||
}
|
||||
|
||||
|
||||
BSize
|
||||
ToolButton::_ValidatePreferredSize()
|
||||
{
|
||||
if (fPreferredSize.width < 0) {
|
||||
float startWidth = 10;
|
||||
float minSize = 25;
|
||||
float minWidth = minSize;
|
||||
|
||||
if (fMenu) {
|
||||
startWidth += kPopUpMarkerRect;
|
||||
minWidth += kPopUpMarkerRect;
|
||||
}
|
||||
|
||||
// Width
|
||||
float width = startWidth + (float)ceil(StringWidth(Label()));
|
||||
if (width < minWidth)
|
||||
width = minWidth;
|
||||
|
||||
fPreferredSize.width = width;
|
||||
|
||||
// Height
|
||||
fPreferredSize.height = 0;
|
||||
|
||||
if (Label()) {
|
||||
font_height fontHeight;
|
||||
GetFontHeight(&fontHeight);
|
||||
|
||||
fPreferredSize.height
|
||||
+= ceilf((fontHeight.ascent + fontHeight.descent) * 1.5f);
|
||||
}
|
||||
|
||||
if (Bitmap())
|
||||
fPreferredSize.height += kToolbarIconSize + 6;
|
||||
|
||||
if (Bitmap() && Label())
|
||||
fPreferredSize.height += 4;
|
||||
|
||||
if (fPreferredSize.height < minSize)
|
||||
fPreferredSize.height = minSize;
|
||||
|
||||
ResetLayoutInvalidation();
|
||||
}
|
||||
|
||||
return fPreferredSize;
|
||||
}
|
|
@ -1,66 +0,0 @@
|
|||
/*
|
||||
* Copyright 2010, Pier Luigi Fiorini. All rights reserved.
|
||||
* Distributed under the terms of the MIT License.
|
||||
*/
|
||||
#ifndef _TOOL_BUTTON_H
|
||||
#define _TOOL_BUTTON_H
|
||||
|
||||
#include <Control.h>
|
||||
|
||||
class BBitmap;
|
||||
class BPopUpMenu;
|
||||
|
||||
class ToolButton : public BControl {
|
||||
public:
|
||||
ToolButton(const char* name, const char* label,
|
||||
BMessage* message,
|
||||
uint32 flags = B_WILL_DRAW | B_NAVIGABLE
|
||||
| B_FULL_UPDATE_ON_RESIZE);
|
||||
ToolButton(const char* label,
|
||||
BMessage* message = NULL);
|
||||
ToolButton(BMessage* archive);
|
||||
|
||||
virtual ~ToolButton();
|
||||
|
||||
static BArchivable* Instantiate(BMessage* archive);
|
||||
virtual status_t Archive(BMessage* archive, bool deep) const;
|
||||
|
||||
virtual BSize MinSize();
|
||||
virtual BSize MaxSize();
|
||||
virtual BSize PreferredSize();
|
||||
|
||||
virtual void Draw(BRect updateRect);
|
||||
virtual void AttachedToWindow();
|
||||
|
||||
virtual void MouseDown(BPoint where);
|
||||
virtual void MouseMoved(BPoint where, uint32 transit,
|
||||
const BMessage* message);
|
||||
virtual void MouseUp(BPoint where);
|
||||
|
||||
virtual void SetLabel(const char* string);
|
||||
|
||||
virtual void MessageReceived(BMessage* message);
|
||||
virtual void WindowActivated(bool active);
|
||||
|
||||
virtual status_t Invoke(BMessage* message = NULL);
|
||||
|
||||
virtual status_t Perform(perform_code code, void* data);
|
||||
|
||||
virtual void InvalidateLayout(bool descendants);
|
||||
|
||||
BBitmap* Bitmap() const;
|
||||
void SetBitmap(BBitmap* bitmap);
|
||||
|
||||
BPopUpMenu* Menu() const;
|
||||
void SetMenu(BPopUpMenu* menu);
|
||||
|
||||
private:
|
||||
BBitmap* fBitmap;
|
||||
BPopUpMenu* fMenu;
|
||||
BSize fPreferredSize;
|
||||
|
||||
BPoint _Center(BRect rect);
|
||||
BSize _ValidatePreferredSize();
|
||||
};
|
||||
|
||||
#endif // _TOOL_BUTTON_H
|
Ŝarĝante…
Reference in New Issue