forked from phoudoin/sanity
-
Notifications
You must be signed in to change notification settings - Fork 4
/
CollapsableBox.cpp
338 lines (267 loc) · 8.46 KB
/
CollapsableBox.cpp
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
#include "CollapsableBox.h"
#ifdef CODEWARRIOR
#pragma mark [Constructor & destructor]
#endif
// Constructor & destructor
// ------------------------
// --------------------------------------------------------------
CollapsableBox::CollapsableBox
(
BRect frame,
const char *name,
const char *label,
BMessage *msg,
uint32 resize_mask,
uint32 flags
)
: BControl(frame, name, label, msg, resize_mask,
flags | B_WILL_DRAW | B_FRAME_EVENTS | B_NAVIGABLE | B_NAVIGABLE_JUMP)
{
m_pressing = false;
font_height fh;
GetFontHeight(&fh);
float min_height = (float) ceil(6.0f + fh.ascent + fh.descent) + 2.0f;
if (Bounds().Height() < min_height)
ResizeTo(Bounds().Width(), min_height);
m_collapsed_rect = BRect(0, 0, Bounds().Width(), min_height);
m_expanded_rect = Bounds();
SetValue(B_CONTROL_ON);
}
#ifdef CODEWARRIOR
#pragma mark [Public methods]
#endif
// Public methods
// --------------
void CollapsableBox::AttachedToWindow(void)
{
SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
SetLowColor(ui_color(B_PANEL_BACKGROUND_COLOR));
// if (Parent())
// SetViewColor(Parent()->ViewColor());
}
void CollapsableBox::SetValue(int32 new_value)
{
if (new_value != Value()) {
BRect r = Bounds();
if (!new_value)
ResizeTo(r.Width(), m_collapsed_rect.Height());
else
ResizeTo(r.Width(), m_expanded_rect.Height());
BView *child;
child = ChildAt(0);
while (child) {
if (new_value)
child->Show();
else
child->Hide();
child = child->NextSibling();
};
Invalidate();
BControl::SetValue(new_value);
Invoke();
};
}
void CollapsableBox::GetPreferredSize(float *width, float *height)
{
font_height fh;
GetFontHeight(&fh);
*height = (float) ceil(6.0f + fh.ascent + fh.descent) + 2.0f;
*width = 12.0f + fh.ascent;
if (Label())
*width += StringWidth(Label());
*width = (float) ceil(*width);
}
void CollapsableBox::FrameResized(float width, float height)
{
if (height > m_collapsed_rect.Height())
m_expanded_rect = BRect(0, 0, width, height);
BControl::FrameResized(width, height);
Invoke();
}
void CollapsableBox::Draw(BRect updateRect)
{
font_height fh;
GetFontHeight(&fh);
BRect r = Bounds();
bool show_focus = IsEnabled() && IsFocus() && Window()->IsActive();
// If the focus is changing, just redraw the focus indicator
if (IsFocusChanging()) {
float x = (float) ceil(10.0f + fh.ascent);
float y = 5.0f + (float)ceil(fh.ascent);
SetHighColor(show_focus ? ui_color(B_KEYBOARD_NAVIGATION_COLOR) : ViewColor());
StrokeLine(BPoint(x, y), BPoint(x + StringWidth(Label()), y));
if (show_focus)
SetHighColor(255, 255, 255, 255);
else
SetHighColor(ViewColor());
StrokeLine(BPoint(x, y+1), BPoint(x + StringWidth(Label()), y+1));
return;
};
SetHighColor(tint_color(ViewColor(), B_LIGHTEN_MAX_TINT));
StrokeLine(BPoint(r.left, r.bottom), BPoint(r.left, r.top));
StrokeLine(BPoint(r.left+1.0f, r.top), BPoint(r.right+1.0f, r.top));
SetHighColor(tint_color(ViewColor(), B_DARKEN_2_TINT));
StrokeLine(BPoint(r.left+1.0f, r.bottom), BPoint(r.right+1.0f, r.bottom));
StrokeLine(BPoint(r.right+1.0f, r.bottom), BPoint(r.right+1.0f, r.top+1.0f));
rgb_color no_tint = ui_color(B_PANEL_BACKGROUND_COLOR),
lighten1 = tint_color(no_tint, B_LIGHTEN_1_TINT),
lightenmax = tint_color(no_tint, B_LIGHTEN_MAX_TINT),
darken1 = tint_color(no_tint, B_DARKEN_1_TINT),
darken2 = tint_color(no_tint, B_DARKEN_2_TINT),
darken3 = tint_color(no_tint, B_DARKEN_3_TINT),
darken4 = tint_color(no_tint, B_DARKEN_4_TINT),
darkenmax = tint_color(no_tint, B_DARKEN_MAX_TINT);
if (m_pressing)
draw_switch(PRESSED);
else if (Value())
draw_switch(EXPANDED);
else
draw_switch(COLLAPSED);
// Label
if (!IsEnabled())
SetHighColor(tint_color(no_tint, B_DISABLED_LABEL_TINT));
else
SetHighColor(darkenmax);
DrawString(Label(), BPoint((float)ceil(10.0f + fh.ascent),
3.0f + (float)ceil(fh.ascent)));
// Focus
if (show_focus || !Window()->IsActive()) {
float x = (float)ceil(10.0f + fh.ascent);
float y = 5.0f + (float)ceil(fh.ascent);
SetHighColor(show_focus ? ui_color(B_KEYBOARD_NAVIGATION_COLOR) : ViewColor());
StrokeLine(BPoint(x, y), BPoint(x + StringWidth(Label()), y));
if (show_focus)
SetHighColor(255, 255, 255, 255);
else
SetHighColor(ViewColor());
StrokeLine(BPoint(x, y+1), BPoint(x + StringWidth(Label()), y+1));
};
}
void CollapsableBox::KeyDown(const char *bytes, int32 numBytes)
{
if (bytes[0] == B_ENTER || bytes[0] == B_SPACE)
SetValue(Value() == B_CONTROL_OFF ? B_CONTROL_ON : B_CONTROL_OFF);
else
BControl::KeyDown(bytes, numBytes);
}
void CollapsableBox::MouseDown(BPoint where)
{
if (!IsEnabled())
return;
Window()->Activate();
uint32 buttons;
GetMouse(&where, &buttons);
if ((buttons & B_PRIMARY_MOUSE_BUTTON) == 0)
return;
if (! m_collapsed_rect.Contains(where))
return;
m_click = where;
SetMouseEventMask(B_POINTER_EVENTS,
B_SUSPEND_VIEW_FOCUS | B_LOCK_WINDOW_FOCUS | B_NO_POINTER_HISTORY);
SetTracking(true);
m_pressing = true;
Draw(Bounds());
Flush();
}
void CollapsableBox::MouseMoved(BPoint where, uint32 transit, const BMessage *message)
{
if (!IsTracking())
return;
if (where == m_click)
return;
m_click = where;
bool inside = m_collapsed_rect.Contains(where);
if (m_pressing != inside) {
m_pressing = inside;
Draw(Bounds());
Flush();
};
}
void CollapsableBox::MouseUp(BPoint where)
{
if (!IsTracking())
return;
bool inside = m_collapsed_rect.Contains(where);
if (inside)
SetValue(Value() == B_CONTROL_OFF ? B_CONTROL_ON : B_CONTROL_OFF);
else {
Draw(Bounds());
Flush();
};
m_pressing = false;
SetTracking(false);
SetMouseEventMask(0, 0);
}
void CollapsableBox::MessageReceived(BMessage *message)
{
BControl::MessageReceived(message);
}
#ifdef CODEWARRIOR
#pragma mark [Private stuffs]
#endif
// Privates methods
// ----------------
void CollapsableBox::draw_switch(int state)
{
BRect rect(4, 4, 14, 14);
SetHighColor(ViewColor());
FillRect(rect);
rgb_color switch_color = {150,150,255, 255};
rgb_color light_color = tint_color(switch_color, B_LIGHTEN_2_TINT);
rgb_color dark_color = tint_color(switch_color, B_DARKEN_2_TINT);
rgb_color outlineColor = dark_color; // {0, 0, 0, 255};
rgb_color middleColor = state == PRESSED ? light_color : switch_color;
SetDrawingMode(B_OP_COPY);
switch (state) {
case COLLAPSED:
BeginLineArray(6);
AddLine(BPoint(rect.left + 3, rect.top + 1),
BPoint(rect.left + 3, rect.bottom - 1), outlineColor);
AddLine(BPoint(rect.left + 3, rect.top + 1),
BPoint(rect.left + 7, rect.top + 5), outlineColor);
AddLine(BPoint(rect.left + 7, rect.top + 5),
BPoint(rect.left + 3, rect.bottom - 1), outlineColor);
AddLine(BPoint(rect.left + 4, rect.top + 3),
BPoint(rect.left + 4, rect.bottom - 3), middleColor);
AddLine(BPoint(rect.left + 5, rect.top + 4),
BPoint(rect.left + 5, rect.bottom - 4), middleColor);
AddLine(BPoint(rect.left + 5, rect.top + 5),
BPoint(rect.left + 6, rect.top + 5), middleColor);
EndLineArray();
break;
case PRESSED:
BeginLineArray(7);
AddLine(BPoint(rect.left + 1, rect.top + 7),
BPoint(rect.left + 7, rect.top + 7), outlineColor);
AddLine(BPoint(rect.left + 7, rect.top + 1),
BPoint(rect.left + 7, rect.top + 7), outlineColor);
AddLine(BPoint(rect.left + 1, rect.top + 7),
BPoint(rect.left + 7, rect.top + 1), outlineColor);
AddLine(BPoint(rect.left + 3, rect.top + 6),
BPoint(rect.left + 6, rect.top + 6), middleColor);
AddLine(BPoint(rect.left + 4, rect.top + 5),
BPoint(rect.left + 6, rect.top + 5), middleColor);
AddLine(BPoint(rect.left + 5, rect.top + 4),
BPoint(rect.left + 6, rect.top + 4), middleColor);
AddLine(BPoint(rect.left + 6, rect.top + 3),
BPoint(rect.left + 6, rect.top + 4), middleColor);
EndLineArray();
break;
case EXPANDED:
BeginLineArray(6);
AddLine(BPoint(rect.left + 1, rect.top + 3),
BPoint(rect.right - 1, rect.top + 3), outlineColor);
AddLine(BPoint(rect.left + 1, rect.top + 3),
BPoint(rect.left + 5, rect.top + 7), outlineColor);
AddLine(BPoint(rect.left + 5, rect.top + 7),
BPoint(rect.right - 1, rect.top + 3), outlineColor);
AddLine(BPoint(rect.left + 3, rect.top + 4),
BPoint(rect.right - 3, rect.top + 4), middleColor);
AddLine(BPoint(rect.left + 4, rect.top + 5),
BPoint(rect.right - 4, rect.top + 5), middleColor);
AddLine(BPoint(rect.left + 5, rect.top + 5),
BPoint(rect.left + 5, rect.top + 6), middleColor);
EndLineArray();
break;
}
}