From 6fbedc2a890f926e9d1597a4c092078eb85cdb47 Mon Sep 17 00:00:00 2001
From: looncraz <looncraz@looncraz.net>
Date: Mon, 11 Jul 2016 04:53:33 +0000
Subject: [PATCH] Add BButton SetButtonColor and SetLabelColor
Also corrects ticket# 12604
Usage of the new methods requires an active attachment to a window or
view, otherwise the colors will become overridden by the default
AttachedToWindow() logic. A debugger call is made to make this failure
obvious.
In the future, adding two rgb_color members for the colors will be preferred,
for now there is no ABI room.
---
headers/os/interface/Button.h | 38 ++++++++++++++++++++++++++++++
src/kits/interface/Button.cpp | 54 +++++++++++++++++++++++++++++++++++++++----
2 files changed, 88 insertions(+), 4 deletions(-)
diff --git a/headers/os/interface/Button.h b/headers/os/interface/Button.h
index 71dfac5..aba1341 100644
a
|
b
|
public:
|
83 | 83 | |
84 | 84 | virtual status_t SetIcon(const BBitmap* icon, uint32 flags = 0); |
85 | 85 | |
| 86 | virtual void SetHighColor(rgb_color color); |
| 87 | virtual void SetLowColor(rgb_color color); |
| 88 | |
| 89 | void SetButtonColor(rgb_color color); |
| 90 | void SetButtonColor(uchar red, uchar green, |
| 91 | uchar blue, uchar alpha = 255); |
| 92 | |
| 93 | void SetLabelColor(rgb_color color); |
| 94 | void SetLabelColor(uchar red, uchar green, |
| 95 | uchar blue, uchar alpha = 255); |
| 96 | |
86 | 97 | protected: |
87 | 98 | virtual void LayoutInvalidated(bool descendants = false); |
88 | 99 | |
… |
… |
private:
|
108 | 119 | }; |
109 | 120 | |
110 | 121 | |
| 122 | // #pragma mark - inline definitions |
| 123 | |
| 124 | |
| 125 | inline void |
| 126 | BButton::SetButtonColor(uchar red, uchar green, uchar blue, uchar alpha) |
| 127 | { |
| 128 | rgb_color color; |
| 129 | color.red = red; |
| 130 | color.green = green; |
| 131 | color.blue = blue; |
| 132 | color.alpha = alpha; |
| 133 | SetButtonColor(color); |
| 134 | } |
| 135 | |
| 136 | |
| 137 | inline void |
| 138 | BButton::SetLabelColor(uchar red, uchar green, uchar blue, uchar alpha) |
| 139 | { |
| 140 | rgb_color color; |
| 141 | color.red = red; |
| 142 | color.green = green; |
| 143 | color.blue = blue; |
| 144 | color.alpha = alpha; |
| 145 | SetLabelColor(color); |
| 146 | } |
| 147 | |
| 148 | |
111 | 149 | #endif // _BUTTON_H |
diff --git a/src/kits/interface/Button.cpp b/src/kits/interface/Button.cpp
index 6d3fd4b..babe619 100644
a
|
b
|
enum {
|
32 | 32 | FLAG_FLAT = 0x02, |
33 | 33 | FLAG_INSIDE = 0x04, |
34 | 34 | FLAG_WAS_PRESSED = 0x08, |
| 35 | FLAG_CUSTOM_COLORS = 0x10, |
| 36 | FLAG_ATTACHED = 0x20, |
35 | 37 | }; |
36 | 38 | |
37 | 39 | |
… |
… |
BButton::Draw(BRect updateRect)
|
137 | 139 | rgb_color base = LowColor(); |
138 | 140 | rgb_color textColor = HighColor(); |
139 | 141 | |
| 142 | if (!_Flag(FLAG_CUSTOM_COLORS)) { |
| 143 | textColor = ui_color(B_CONTROL_TEXT_COLOR); |
| 144 | base = tint_color(ui_color(B_CONTROL_BACKGROUND_COLOR), 1.115); |
| 145 | } |
| 146 | |
140 | 147 | uint32 flags = be_control_look->Flags(this); |
141 | 148 | if (_Flag(FLAG_DEFAULT)) |
142 | 149 | flags |= BControlLook::B_DEFAULT_BUTTON; |
… |
… |
BButton::MouseDown(BPoint where)
|
226 | 233 | } |
227 | 234 | } |
228 | 235 | |
| 236 | |
229 | 237 | void |
230 | 238 | BButton::AttachedToWindow() |
231 | 239 | { |
232 | 240 | BControl::AttachedToWindow(); |
233 | 241 | |
234 | | // Tint default control background color to match default panel background. |
235 | | SetLowUIColor(B_CONTROL_BACKGROUND_COLOR, 1.115); |
236 | | SetHighUIColor(B_CONTROL_TEXT_COLOR); |
237 | | |
238 | 242 | if (IsDefault()) |
239 | 243 | Window()->SetDefaultButton(this); |
| 244 | |
| 245 | _SetFlag(FLAG_ATTACHED, true); |
240 | 246 | } |
241 | 247 | |
242 | 248 | |
… |
… |
void
|
418 | 424 | BButton::DetachedFromWindow() |
419 | 425 | { |
420 | 426 | BControl::DetachedFromWindow(); |
| 427 | |
| 428 | _SetFlag(FLAG_ATTACHED, false); |
421 | 429 | } |
422 | 430 | |
423 | 431 | |
… |
… |
BButton::SetIcon(const BBitmap* icon, uint32 flags)
|
618 | 626 | |
619 | 627 | |
620 | 628 | void |
| 629 | BButton::SetHighColor(rgb_color color) |
| 630 | { |
| 631 | // Disabled. |
| 632 | } |
| 633 | |
| 634 | |
| 635 | void |
| 636 | BButton::SetLowColor(rgb_color color) |
| 637 | { |
| 638 | // Disabled. |
| 639 | } |
| 640 | |
| 641 | |
| 642 | void |
| 643 | BButton::SetButtonColor(rgb_color color) |
| 644 | { |
| 645 | if (!_Flag(FLAG_ATTACHED)) |
| 646 | debugger("BButton must be attached to parent!"); |
| 647 | |
| 648 | _SetFlag(FLAG_CUSTOM_COLORS, true); |
| 649 | BView::SetLowColor(color); |
| 650 | Invalidate(); |
| 651 | } |
| 652 | |
| 653 | |
| 654 | void |
| 655 | BButton::SetLabelColor(rgb_color color) |
| 656 | { |
| 657 | if (!_Flag(FLAG_ATTACHED)) |
| 658 | debugger("BButton must be attached to parent!"); |
| 659 | |
| 660 | _SetFlag(FLAG_CUSTOM_COLORS, true); |
| 661 | BView::SetHighColor(color); |
| 662 | Invalidate(); |
| 663 | } |
| 664 | |
| 665 | |
| 666 | void |
621 | 667 | BButton::LayoutInvalidated(bool descendants) |
622 | 668 | { |
623 | 669 | // invalidate cached preferred size |