-
Notifications
You must be signed in to change notification settings - Fork 726
Expand file tree
/
Copy pathSideButton.cpp
More file actions
353 lines (307 loc) · 9.2 KB
/
SideButton.cpp
File metadata and controls
353 lines (307 loc) · 9.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
#include "SideButton.hpp"
#include "Label.hpp"
#include <wx/dcclient.h>
#include <wx/dcgraph.h>
BEGIN_EVENT_TABLE(SideButton, wxPanel)
EVT_LEFT_DOWN(SideButton::mouseDown)
EVT_LEFT_UP(SideButton::mouseReleased)
EVT_PAINT(SideButton::paintEvent)
END_EVENT_TABLE()
SideButton::SideButton(wxWindow* parent, wxString text, wxString icon, long stlye, int iconSize)
: wxWindow(parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, stlye)
, state_handler(this)
{
radius = 12;
#ifdef __APPLE__
extra_size = wxSize(38 + FromDIP(20), 10);
text_margin = 15 + FromDIP(20);
#else
extra_size = wxSize(38, 10);
text_margin = 15;
#endif
icon_offset = 0;
text_orientation = HO_Left;
border_color.append(0x6B6B6B, StateColor::Disabled);
border_color.append(wxColour(23, 129, 63), StateColor::Pressed);
border_color.append(wxColour(48,221,112), StateColor::Hovered);
border_color.append(0x00AE42, StateColor::Normal);
border_color.setTakeFocusedAsHovered(false);
text_color.append(0xACACAC, StateColor::Disabled);
text_color.append(0xFEFEFE, StateColor::Pressed);
text_color.append(0xFEFEFE, StateColor::Hovered);
text_color.append(0xFEFEFE, StateColor::Normal);
background_color.append(0x6B6B6B, StateColor::Disabled);
background_color.append(wxColour(23, 129, 63), StateColor::Pressed);
background_color.append(wxColour(48, 221, 112), StateColor::Hovered);
background_color.append(0x00AE42, StateColor::Normal);
background_color.setTakeFocusedAsHovered(false);
SetBottomColour(wxColour("#3B4446"));
state_handler.attach({ &border_color, &text_color, &background_color });
state_handler.update_binds();
// icon only
if (!icon.IsEmpty()) {
this->icon = ScalableBitmap(this, icon.ToStdString(), iconSize > 0 ? iconSize : 14);
}
SetFont(Label::Body_14);
wxWindow::SetLabel(text);
messureSize();
}
void SideButton::SetCornerRadius(double radius)
{
this->radius = radius;
Refresh();
}
void SideButton::SetCornerEnable(const std::vector<bool>& enable)
{
radius_enable.clear();
for (auto en : enable) {
radius_enable.push_back(en);
}
}
void SideButton::SetTextLayout(EHorizontalOrientation orient, int margin)
{
text_orientation = orient;
text_margin = margin;
messureSize();
Refresh();
}
void SideButton::SetLayoutStyle(int style)
{
layout_style = style;
messureSize();
Refresh();
}
void SideButton::SetLabel(const wxString& label)
{
wxWindow::SetLabel(label);
messureSize();
Refresh();
}
bool SideButton::SetForegroundColour(wxColour const &color)
{
text_color = StateColor(color);
state_handler.update_binds();
return true;
}
bool SideButton::SetBackgroundColour(wxColour const& color)
{
background_color = StateColor(color);
state_handler.update_binds();
return true;
}
bool SideButton::SetBottomColour(wxColour const& color)
{
bottom_color = color;
return true;
}
void SideButton::SetMinSize(const wxSize& size)
{
minSize = size;
messureSize();
}
void SideButton::SetBorderColor(StateColor const &color)
{
border_color = color;
state_handler.update_binds();
Refresh();
}
void SideButton::SetForegroundColor(StateColor const &color)
{
text_color = color;
state_handler.update_binds();
Refresh();
}
void SideButton::SetBackgroundColor(StateColor const &color)
{
background_color = color;
state_handler.update_binds();
Refresh();
}
bool SideButton::Enable(bool enable)
{
bool result = wxWindow::Enable(enable);
if (result) {
wxCommandEvent e(EVT_ENABLE_CHANGED);
e.SetEventObject(this);
GetEventHandler()->ProcessEvent(e);
}
return result;
}
void SideButton::Rescale()
{
if (this->icon.bmp().IsOk())
this->icon.msw_rescale();
messureSize();
}
void SideButton::SetExtraSize(const wxSize& size)
{
extra_size = size;
messureSize();
}
void SideButton::SetIconOffset(const int offset)
{
icon_offset = offset;
messureSize();
}
void SideButton::paintEvent(wxPaintEvent& evt)
{
// depending on your system you may need to look at double-buffered dcs
wxPaintDC dc(this);
#ifdef __WXMSW__
wxGCDC dc2(dc);
#else
wxDC & dc2(dc);
#endif
wxDC & dctext(dc);
dorender(dc2, dctext);
}
/*
* Here we do the actual rendering. I put it in a separate
* method so that it can work no matter what type of DC
* (e.g. wxPaintDC or wxClientDC) is used.
*/
void SideButton::dorender(wxDC& dc, wxDC& text_dc)
{
wxSize size = GetSize();
// draw background
dc.SetPen(wxNullPen);
dc.SetBrush(StateColor::darkModeColorFor(bottom_color));
dc.DrawRectangle(0, 0, size.x, size.y);
int states = state_handler.states();
dc.SetBrush(wxBrush(background_color.colorForStates(states)));
dc.SetPen(wxPen(border_color.colorForStates(states)));
int pen_width = dc.GetPen().GetWidth();
// draw icon style
if (icon.bmp().IsOk()) {
if (radius > 1e-5) {
dc.DrawRoundedRectangle(0, 0, size.x, size.y, radius);
dc.DrawRectangle(radius, 0, size.x - radius, size.y);
dc.SetPen(wxNullPen);
dc.DrawRectangle(radius - pen_width, pen_width, radius, size.y - 2 * pen_width);
}
else {
dc.DrawRectangle(0, 0, size.x, size.y);
}
}
// draw text style
else {
if (radius > 1e-5) {
if (layout_style == 1) {
dc.DrawRoundedRectangle(0, 0, size.x, size.y, radius);
dc.SetPen(wxNullPen);
} else {
dc.DrawRoundedRectangle(0, 0, size.x, size.y, radius);
dc.DrawRectangle(0, 0, radius, size.y);
dc.SetPen(wxNullPen);
dc.DrawRectangle(pen_width, pen_width, size.x - radius, size.y - 2 * pen_width);
}
} else {
dc.DrawRectangle(0, 0, size.x, size.y);
}
}
dc.SetBrush(*wxTRANSPARENT_BRUSH);
// calc content size
wxSize szIcon;
wxSize szContent = textSize;
if (icon.bmp().IsOk()) {
if (szContent.y > 0) {
//BBS norrow size between text and icon
szContent.x += 5;
}
szIcon = icon.GetBmpSize();
szContent.x += szIcon.x;
if (szIcon.y > szContent.y)
szContent.y = szIcon.y;
}
// move to center
wxRect rcContent = { {0, 0}, size };
if (text_orientation == EHorizontalOrientation::HO_Center) {
wxSize offset = (size - szContent) / 2;
rcContent.Deflate(offset.x, offset.y);
} else if (text_orientation == EHorizontalOrientation::HO_Left) {
wxSize offset = (size - szContent) / 2;
rcContent.Deflate(text_margin, offset.y);
} else if (text_orientation == EHorizontalOrientation::HO_Right) {
wxSize offset = (size - szContent) / 2;
rcContent.Deflate(size.x - text_margin, offset.y);
}
// start draw
wxPoint pt = rcContent.GetLeftTop();
if (icon.bmp().IsOk()) {
//BBS extra pixels for icon
pt.x += icon_offset;
pt.y += (rcContent.height - szIcon.y) / 2;
dc.DrawBitmap(icon.bmp(), pt);
//BBS norrow size between text and icon
pt.x += szIcon.x + 5;
pt.y = rcContent.y;
}
auto text = GetLabel();
if (!text.IsEmpty()) {
pt.y += (rcContent.height - textSize.y) / 2;
#ifdef __APPLE__
pt.y -= FromDIP(2);
#endif
text_dc.SetFont(GetFont());
text_dc.SetTextForeground(text_color.colorForStates(states));
text_dc.DrawText(text, pt);
}
}
void SideButton::messureSize()
{
textSize = GetTextExtent(GetLabel());
if (minSize.GetWidth() > 0) {
wxWindow::SetMinSize(minSize);
return;
}
wxSize szContent = textSize;
if (this->icon.bmp().IsOk()) {
if (szContent.y > 0) {
szContent.x += 5;
}
wxSize szIcon = this->icon.GetBmpSize();
szContent.x += szIcon.x;
if (szIcon.y > szContent.y)
szContent.y = szIcon.y;
//BBS icon only
wxWindow::SetMinSize(szContent + wxSize(szContent.GetX() + extra_size.GetX(), minSize.GetHeight()));
}
else {
if (minSize.GetHeight() > 0) {
//BBS with text size
wxWindow::SetMinSize(wxSize(szContent.GetX() + extra_size.GetX(), minSize.GetHeight()));
} else {
//BBS with text size
wxWindow::SetMinSize(szContent + extra_size);
}
}
}
void SideButton::mouseDown(wxMouseEvent& event)
{
event.Skip();
pressedDown = true;
SetFocus();
CaptureMouse();
}
void SideButton::mouseReleased(wxMouseEvent& event)
{
event.Skip();
if (HasCapture())
{
// Release mouse capture regardless of pressedDown state, to avoid cases where capture may be stuck permanently
ReleaseMouse();
}
if (pressedDown) {
pressedDown = false;
// Touch input drift slop — see Button::mouseReleased.
constexpr int kReleaseSlop = 15;
if (wxRect({0, 0}, GetSize()).Inflate(kReleaseSlop).Contains(event.GetPosition()))
sendButtonEvent();
}
}
void SideButton::sendButtonEvent()
{
wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, GetId());
event.SetEventObject(this);
GetEventHandler()->ProcessEvent(event);
}