-
Notifications
You must be signed in to change notification settings - Fork 726
Expand file tree
/
Copy pathTabButton.cpp
More file actions
235 lines (203 loc) · 5.88 KB
/
TabButton.cpp
File metadata and controls
235 lines (203 loc) · 5.88 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
#include "TabButton.hpp"
#include "Widgets/Label.hpp"
#include <wx/dcclient.h>
#include <wx/dcgraph.h>
BEGIN_EVENT_TABLE(TabButton, StaticBox)
EVT_LEFT_DOWN(TabButton::mouseDown)
EVT_LEFT_UP(TabButton::mouseReleased)
// catch paint events
EVT_PAINT(TabButton::paintEvent)
END_EVENT_TABLE()
static wxColour BORDER_HOVER_COL = wxColour(0, 174, 66);
const static wxColour TAB_BUTTON_BG = wxColour("#FEFFFF");
const static wxColour TAB_BUTTON_SEL = wxColour(219, 253, 213, 255);
TabButton::TabButton()
: paddingSize(43, 16)
, text_color(*wxBLACK)
{
background_color = StateColor(
std::make_pair(TAB_BUTTON_SEL, (int) StateColor::Checked),
std::make_pair(wxColour("#FEFFFF"), (int) StateColor::Hovered),
std::make_pair(wxColour("#FEFFFF"), (int) StateColor::Normal));
border_color = StateColor(
std::make_pair(wxColour("#FEFFFF"), (int) StateColor::Checked),
std::make_pair(BORDER_HOVER_COL, (int) StateColor::Hovered),
std::make_pair(wxColour("#FEFFFF"), (int)StateColor::Normal));
}
TabButton::TabButton(wxWindow *parent, wxString text, ScalableBitmap &bmp, long style, int iconSize)
: TabButton()
{
Create(parent, text, bmp, style, iconSize);
}
bool TabButton::Create(wxWindow *parent, wxString text, ScalableBitmap &bmp, long style, int iconSize)
{
StaticBox::Create(parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, style);
newtag_img = ScalableBitmap(this, "monitor_hms_new",7);
state_handler.attach({&text_color, &border_color});
state_handler.update_binds();
//BBS set default font
SetFont(Label::Body_14);
wxWindow::SetLabel(text);
this->icon = bmp;
messureSize();
return true;
}
void TabButton::SetLabel(const wxString &label)
{
wxWindow::SetLabel(label);
messureSize();
Refresh();
}
void TabButton::SetMinSize(const wxSize &size)
{
minSize = size;
messureSize();
}
void TabButton::SetPaddingSize(const wxSize &size)
{
paddingSize = size;
messureSize();
}
const wxSize& TabButton::GetPaddingSize()
{
return paddingSize;
}
void TabButton::SetTextColor(StateColor const &color)
{
text_color = color;
state_handler.update_binds();
Refresh();
}
void TabButton::SetBorderColor(StateColor const &color)
{
border_color = color;
state_handler.update_binds();
Refresh();
}
void TabButton::SetBGColor(StateColor const &color)
{
background_color = color;
state_handler.update_binds();
Refresh();
}
void TabButton::SetBitmap(ScalableBitmap &bitmap)
{
this->icon = bitmap;
}
bool TabButton::Enable(bool enable)
{
bool result = wxWindow::Enable(enable);
if (result) {
wxCommandEvent e(EVT_ENABLE_CHANGED);
e.SetEventObject(this);
GetEventHandler()->ProcessEvent(e);
}
return result;
}
void TabButton::Rescale()
{
messureSize();
}
void TabButton::paintEvent(wxPaintEvent &evt)
{
// depending on your system you may need to look at double-buffered dcs
wxPaintDC dc(this);
render(dc);
}
/*
* 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 TabButton::render(wxDC &dc)
{
StaticBox::render(dc);
int states = state_handler.states();
wxSize size = GetSize();
dc.SetPen(wxPen(border_color.colorForStates(states)));
dc.SetBrush(*wxTRANSPARENT_BRUSH);
dc.DrawRectangle(0, 0, size.x, size.y);
// 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};
wxSize offset = (size - szContent) / 2;
rcContent.Deflate(offset.x, offset.y);
// start draw
wxPoint pt = rcContent.GetLeftTop();
auto text = GetLabel();
if (!text.IsEmpty()) {
pt.x = paddingSize.x;
pt.y = rcContent.y + (rcContent.height - textSize.y) / 2;
dc.SetFont(GetFont());
dc.SetTextForeground(text_color.colorForStates(states));
dc.DrawText(text, pt);
}
wxBitmap showimg = icon.bmp();
int offset_left = 0;
if (show_new_tag) {
showimg = newtag_img.bmp();
offset_left = FromDIP(4);
}
if (showimg.IsOk()) {
pt.x = size.x - showimg.GetWidth() - paddingSize.y - offset_left;
pt.y = (size.y - showimg.GetHeight()) / 2;
dc.DrawBitmap(showimg, pt);
}
}
void TabButton::messureSize()
{
wxClientDC dc(this);
textSize = dc.GetTextExtent(GetLabel());
if (minSize.GetWidth() > 0) {
wxWindow::SetMinSize(minSize);
return;
}
wxSize szContent = textSize;
if (this->icon.bmp().IsOk()) {
if (szContent.y > 0) {
// BBS norrow size between text and icon
szContent.x += 5;
}
wxSize szIcon = this->icon.GetBmpSize();
szContent.x += szIcon.x;
if (szIcon.y > szContent.y) szContent.y = szIcon.y;
}
wxWindow::SetMinSize(szContent + paddingSize);
}
void TabButton::mouseDown(wxMouseEvent &event)
{
event.Skip();
pressedDown = true;
SetFocus();
CaptureMouse();
}
void TabButton::mouseReleased(wxMouseEvent &event)
{
event.Skip();
if (pressedDown) {
pressedDown = false;
if (HasCapture())
ReleaseMouse();
// Touch input drift slop — see Button::mouseReleased.
constexpr int kReleaseSlop = 15;
if (wxRect({0, 0}, GetSize()).Inflate(kReleaseSlop).Contains(event.GetPosition()))
sendButtonEvent();
}
}
void TabButton::sendButtonEvent()
{
wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, GetId());
event.SetEventObject(this);
GetEventHandler()->ProcessEvent(event);
}