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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
|
/*
ui/widget.cc
This file is part of the Osirion project and is distributed under
the terms of the GNU General Public License version 2
*/
#include "auxiliary/functions.h"
#include "sys/sys.h"
#include "ui/paint.h"
#include "ui/ui.h"
#include "ui/widget.h"
namespace ui
{
Widget::Widget(Widget *parent)
{
widget_focus = false;
widget_visible = true;
widget_border = true;
widget_background = false;
widget_enabled = true;
widget_palette = 0;
widget_font = 0;
widget_label.assign("widget");
if (!parent) {
widget_parent = root();
} else {
widget_parent = parent;
}
if (widget_parent)
widget_parent->add_child(this);
}
Widget::~Widget()
{
remove_children();
}
void Widget::remove_children()
{
for (Children::iterator it = widget_children.begin(); it != widget_children.end(); it++) {
delete(*it);
(*it) = 0;
}
widget_children.clear();
}
bool Widget::is_child(const Widget *widget) const
{
for (Children::const_iterator it = widget_children.begin(); it != widget_children.end(); it++) {
if (widget == (*it))
return true;
}
return false;
}
size_t Widget::list(const size_t indent, const bool visible_only) const
{
if (visible_only && !visible())
return 0;
print(indent);
size_t n = 1;
for (Children::const_iterator it = widget_children.begin(); it != widget_children.end(); it++) {
n += (*it)->list(indent + 1, visible_only);
}
return n;
}
void Widget::print(const size_t indent) const
{
if (indent) {
std::string marker;
if (widget_focus)
marker.assign("^B* ^N");
else
marker.assign(" ");
con_print << aux::pad_left(marker, indent*2) << label() << std::endl;
}
}
const Palette *Widget::palette() const
{
if (widget_palette) {
return widget_palette;
} else {
return parent()->palette();
}
}
const Font *Widget::font() const
{
if (widget_font) {
return widget_font;
} else {
return parent()->font();
}
}
void Widget::lower()
{
if (!parent())
return;
Children::iterator it = parent()->find_child(this);
if (it != parent()->children().end()) {
parent()->children().erase(it);
parent()->children().push_front(this);
}
}
void Widget::raise()
{
if (!parent())
return;
Children::iterator it = parent()->find_child(this);
if (it != parent()->children().end()) {
parent()->children().erase(it);
parent()->children().push_back(this);
}
}
void Widget::set_focus()
{
if (!parent()) {
widget_focus = true;
return;
}
for (Children::iterator it = parent()->children().begin(); it != parent()->children().end(); it++) {
(*it)->widget_focus = false;
}
widget_focus = true;
}
void Widget::show()
{
widget_visible = true;
}
void Widget::hide()
{
widget_visible = false;
if (parent() && focus()) {
Widget::Children::reverse_iterator it = parent()->children().rbegin();
while (it != parent()->children().rend()) {
Widget *w = (*it);
if (w != this && w->visible()) {
widget_focus = false;
w->widget_focus = true;
it = parent()->children().rend();
} else {
it++;
}
}
}
}
void Widget::set_visible(const bool visible)
{
if (visible)
show();
else
hide();
}
void Widget::enable()
{
widget_enabled = true;
}
void Widget::disable()
{
widget_enabled = false;
}
void Widget::set_enabled(const bool enabled)
{
if (enabled)
enable();
else
disable();
}
void Widget::set_border(const bool border)
{
widget_border = border;
}
void Widget::set_background(const bool background)
{
widget_background = background;
}
void Widget::set_label(const std::string & label)
{
widget_label.assign(label);
aux::to_label(widget_label);
}
void Widget::set_label(const char *label)
{
if (label) {
widget_label.assign(label);
aux::to_label(widget_label);
} else {
widget_label.clear();
}
}
void Widget::set_palette(const Palette *palette)
{
widget_palette = palette;
}
void Widget::set_font(const Font *font)
{
widget_font = font;
}
void Widget::set_geometry(const float x, const float y, const float w, const float h)
{
widget_location.assign(x, y);
widget_size.assign(w, h);
}
void Widget::set_geometry(const math::Vector2f &location, const math::Vector2f &size)
{
widget_location.assign(location);
widget_size.assign(size);
}
void Widget::set_location(const float x, const float y)
{
widget_location.assign(x, y);
}
void Widget::set_location(const math::Vector2f &location)
{
widget_location.assign(location);
}
void Widget::set_size(const float w, const float h)
{
widget_size.assign(w, h);
}
void Widget::set_size(const math::Vector2f &size)
{
widget_size.assign(size);
}
void Widget::set_width(const float w)
{
widget_size[0] = w;
}
void Widget::set_height(const float h)
{
widget_size[1] = h;
}
Widget::Children::iterator Widget::find_child(Widget *child)
{
Children::iterator it;
for (it = widget_children.begin(); it != widget_children.end(); it++) {
if ((*it) == child)
return it;
}
return it;
}
void Widget::add_child(Widget *child)
{
Children::iterator it = find_child(child);
if (it == widget_children.end()) {
widget_children.push_back(child);
}
}
void Widget::remove_child(Widget *child)
{
Children::iterator it = find_child(child);
if (it != widget_children.end()) {
Widget *w = (*it);
w->widget_parent = 0;
delete(w);
widget_children.erase(it);
}
}
Widget *Widget::find_input_focus()
{
if (!visible() || !widget_focus)
return 0;
for (Children::reverse_iterator rit = widget_children.rbegin(); rit != widget_children.rend(); ++rit) {
Widget *w = (*rit);
if (w->visible() && w->widget_focus) {
Widget *f = w->find_input_focus();
if (f)
return f;
}
}
// no child with input focus
return this;
}
Widget *Widget::find_mouse_focus(const math::Vector2f & pos)
{
// this widget is not visible
if (!visible() || !size().contains(pos))
return 0;
// reverse-iterate children
for (Children::reverse_iterator rit = widget_children.rbegin(); rit != widget_children.rend(); ++rit) {
Widget *w = (*rit);
if (w->visible()) {
Widget *f = w->find_mouse_focus(pos - w->location());
if (f)
return f;
}
}
// no child with mouse focus
return this;
}
bool Widget::has_mouse_focus() const
{
return (root()->mouse_focus() == this);
}
bool Widget::has_input_focus() const
{
return (root()->input_focus() == this);
}
/* -- event distributors ------------------------------------------- */
bool Widget::event_emit(Widget *sender, const Event event, void *data)
// Unhandled events are sent to the parent widget
{
if (on_emit(sender, event, data)) {
return true;
} else if (parent()) {
return (parent()->event_emit(sender, event, data));
} else {
return false;
}
}
bool Widget::event_key(const bool pressed, const int key, const unsigned int modifier)
// Unhandled key events are sent to the parent widget
{
bool handled = false;
if (pressed) {
handled = on_keypress(key, modifier);
} else {
handled = on_keyrelease(key, modifier);
}
if (!handled && parent())
handled = parent()->event_key(pressed, key, modifier);
return handled;
}
bool Widget::event_mouse(const math::Vector2f &cursor)
{
math::Vector2f local_cursor = to_local_coords(cursor);
bool handled = false;
if (root()->mouse_focus() != this) {
on_mouseover(local_cursor);
}
on_mousemove(local_cursor);
return handled;
}
void Widget::event_draw()
{
if (!visible())
return;
if (widget_background)
draw_background();
if (widget_border)
draw_border();
if (debug())
draw_debug();
draw();
for (Children::iterator it = widget_children.begin(); it != widget_children.end(); it++) {
if ((*it)->visible())
(*it)->event_draw();
}
}
void Widget::event_resize()
{
resize();
for (Children::iterator it = widget_children.begin(); it != widget_children.end(); it++) {
(*it)->event_resize();
}
}
/* -- event handlers ----------------------------------------------- */
void Widget::on_mouseover(const math::Vector2f &cursor)
{
return;
}
void Widget::on_mousemove(const math::Vector2f &cursor)
{
return;
}
bool Widget::on_keypress(const int key, const unsigned int modifier)
{
return false;
}
bool Widget::on_keyrelease(const int key, const unsigned int modifier)
{
return false;
}
bool Widget::on_emit(Widget *sender, const Event event, void *data)
{
return false;
}
/* -- draw functions ----------------------------------------------- */
void Widget::draw_debug()
{
Paint::set_color(1.0f, 0.0f, 1.0f, 0.5f);
Paint::draw_border(global_location(), size());
}
void Widget::draw_background()
{
Paint::set_color(palette()->background());
Paint::draw_rectangle(global_location(), size());
}
void Widget::draw_border()
{
Paint::set_color(palette()->border());
Paint::draw_border(global_location(), size());
}
void Widget::draw()
{
}
void Widget::resize()
{
}
}
|