-
-
Notifications
You must be signed in to change notification settings - Fork 988
/
list.cpp
474 lines (388 loc) · 11.2 KB
/
list.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
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
/*
Copyright (C) 2010 - 2017 by Mark de Wever <koraq@xs4all.nl>
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#ifdef GUI2_EXPERIMENTAL_LISTBOX
#define GETTEXT_DOMAIN "wesnoth-lib"
#include "gui/widgets/list.hpp"
#include "gui/widgets/listbox.hpp"
#include "gui/auxiliary/find_widget.hpp"
#include "gui/core/log.hpp"
#include "gui/widgets/detail/register.hpp"
#include "gui/widgets/selectable.hpp"
#include "gui/widgets/settings.hpp"
#include "gui/widgets/window.hpp"
#include "utils/functional.hpp"
#define LOG_SCOPE_HEADER get_control_type() + " [" + id() + "] " + __func__
#define LOG_HEADER LOG_SCOPE_HEADER + ':'
namespace gui2
{
#ifdef GUI2_EXPERIMENTAL_LISTBOX
REGISTER_WIDGET(listbox)
#endif
list_view::list_view(const bool has_minimum,
const bool has_maximum,
const generator_base::tplacement placement,
const bool select,
const builder_grid_const_ptr list_builder)
: container_base()
, state_(ENABLED)
, generator_(nullptr)
, list_builder_(list_builder)
, need_layout_(false)
{
assert(list_builder);
generator_
= generator_base::build(has_minimum, has_maximum, placement, select);
assert(generator_);
connect_signal<event::LEFT_BUTTON_DOWN>(
std::bind(&list_view::signal_handler_left_button_down, this, _2),
event::dispatcher::back_pre_child);
connect_signal<event::SDL_KEY_DOWN>(std::bind(
&list_view::signal_handler_sdl_key_down, this, _2, _3, _5, _6));
connect_signal<event::SDL_KEY_DOWN>(
std::bind(
&list_view::signal_handler_sdl_key_down, this, _2, _3, _5, _6),
event::dispatcher::back_pre_child);
}
void list_view::add_row(const string_map& item, const int index)
{
std::map<std::string, string_map> data;
data.emplace("", item);
add_row(data, index);
}
void
list_view::add_row(const std::map<std::string /* widget id */, string_map>& data,
const int index)
{
assert(generator_);
grid& grid = generator_->create_item(index, list_builder_, data, nullptr);
selectable_item* selectable
= find_widget<selectable_item>(&grid, "_toggle", false, false);
if(selectable) {
dynamic_cast<widget&>(*selectable)
.connect_signal<event::LEFT_BUTTON_CLICK>(
std::bind(
&list_view::signal_handler_pre_child_left_button_click,
this,
&grid,
_2,
_3,
_4),
event::dispatcher::back_pre_child);
// Post widget for panel.
dynamic_cast<widget&>(*selectable)
.connect_signal<event::LEFT_BUTTON_CLICK>(
std::bind(&list_view::signal_handler_left_button_click,
this,
&grid,
_2),
event::dispatcher::back_post_child);
// Post widget for button and widgets on the panel.
dynamic_cast<widget&>(*selectable)
.connect_signal<event::LEFT_BUTTON_CLICK>(
std::bind(&list_view::signal_handler_left_button_click,
this,
&grid,
_2),
event::dispatcher::back_child);
}
}
void list_view::append_rows(const std::vector<string_map>& items)
{
for(const string_map & item : items)
{
add_row(item);
}
}
void list_view::remove_row(const unsigned row, unsigned count)
{
assert(generator_);
if(row >= get_item_count()) {
return;
}
if(!count || count > get_item_count()) {
count = get_item_count();
}
unsigned height_reduced = 0;
for(; count; --count) {
if(generator_->item(row).get_visible() != visibility::invisible) {
height_reduced += generator_->item(row).get_height();
}
generator_->delete_item(row);
}
if(height_reduced != 0) {
// resize_content(0, -height_reduced);
}
}
void list_view::clear()
{
// Due to the removing from the linked group, don't use
// generator_->clear() directly.
remove_row(0, 0);
}
unsigned list_view::get_item_count() const
{
assert(generator_);
return generator_->get_item_count();
}
void list_view::set_row_active(const unsigned row, const bool active)
{
assert(generator_);
generator_->item(row).set_active(active);
}
void list_view::set_row_shown(const unsigned row, const bool shown)
{
assert(generator_);
window* window = get_window();
assert(window);
const int selected_row = get_selected_row();
bool resize_needed = false;
{
window::invalidate_layout_blocker invalidate_layout_blocker(*window);
generator_->set_item_shown(row, shown);
generator_->place(generator_->get_origin(),
generator_->calculate_best_size());
// resize_needed = !content_resize_request();
}
if(resize_needed) {
window->invalidate_layout();
} else {
// get_grid().set_visible_rectangle(content_visible_rectangle());
}
if(selected_row != get_selected_row()) {
fire(event::NOTIFY_MODIFIED, *this, nullptr);
}
}
void list_view::set_row_shown(const boost::dynamic_bitset<>& shown)
{
assert(generator_);
assert(shown.size() == get_item_count());
window* window = get_window();
assert(window);
const int selected_row = get_selected_row();
bool resize_needed = false;
{
window::invalidate_layout_blocker invalidate_layout_blocker(*window);
for(size_t i = 0; i < shown.size(); ++i) {
generator_->set_item_shown(i, shown[i]);
}
generator_->place(generator_->get_origin(),
generator_->calculate_best_size());
// resize_needed = !content_resize_request();
}
if(resize_needed) {
window->invalidate_layout();
} else {
// content_grid_->set_visible_rectangle(content_visible_rectangle());
}
if(selected_row != get_selected_row()) {
fire(event::NOTIFY_MODIFIED, *this, nullptr);
}
}
const grid* list_view::get_row_grid(const unsigned row) const
{
assert(generator_);
// rename this function and can we return a reference??
return &generator_->item(row);
}
grid* list_view::get_row_grid(const unsigned row)
{
assert(generator_);
return &generator_->item(row);
}
bool list_view::select_row(const unsigned row, const bool select)
{
assert(generator_);
generator_->select_item(row, select);
return true; // FIXME test what result should have been!!!
}
int list_view::get_selected_row() const
{
assert(generator_);
return generator_->get_selected_item();
}
void list_view::place(const point& origin, const point& size)
{
// Inherited.
container_base::place(origin, size);
/**
* @todo Work-around to set the selected item visible again.
*
* At the moment the lists and dialogs in general are resized a lot as
* work-around for sizing. So this function makes the selected item in view
* again. It doesn't work great in all cases but the proper fix is to avoid
* resizing dialogs a lot. Need more work later on.
*/
const int selected_item = generator_->get_selected_item();
if(selected_item != -1) {
/*
const SDL_Rect& visible = content_visible_area();
SDL_Rect rect = generator_->item(selected_item).get_rectangle();
rect.x = visible.x;
rect.w = visible.w;
show_content_rect(rect);
*/
}
}
#if 0
void list_view::resize_content(
const int width_modification
, const int height_modification)
{
DBG_GUI_L << LOG_HEADER << " current size " << content_grid()->get_size()
<< " width_modification " << width_modification
<< " height_modification " << height_modification
<< ".\n";
if(content_resize_request(width_modification, height_modification)) {
// Calculate new size.
point size = content_grid()->get_size();
size.x += width_modification;
size.y += height_modification;
// Set new size.
content_grid()->set_size(size);
// Set status.
need_layout_ = true;
DBG_GUI_L << LOG_HEADER << " succeeded.\n";
} else {
DBG_GUI_L << LOG_HEADER << " failed.\n";
}
}
#endif
void list_view::init()
{
init_grid(cast<listbox_definition::resolution>(config()).grid);
set_single_child(find_widget<grid>(&get_grid(), "_list_grid", false),
generator_);
/*
* These items should be managed by the new listbox class.
* So make them invisible for now.
*/
grid* g = find_widget<grid>(&get_grid(), "_header_grid", false, false);
if(g)
g->set_visible(widget::visibility::invisible);
g = find_widget<grid>(&get_grid(), "_footer_grid", false, false);
if(g)
g->set_visible(widget::visibility::invisible);
g = find_widget<grid>(&get_grid(), "_vertical_scrollbar_grid", false, false);
if(g)
g->set_visible(widget::visibility::invisible);
g = find_widget<grid>(&get_grid(), "_horizontal_scrollbar_grid", false, false);
if(g)
g->set_visible(widget::visibility::invisible);
}
bool list_view::get_active() const
{
return state_ != DISABLED;
}
unsigned list_view::get_state() const
{
return state_;
}
void list_view::layout_children(const bool force)
{
if(need_layout_ || force) {
get_grid().place(get_grid().get_origin(), get_grid().get_size());
/*
get_grid().set_visible_rectangle(content_visible_area_);
*/
need_layout_ = false;
}
}
void list_view::set_self_active(const bool active)
{
/* DO NOTHING */
}
const std::string& list_view::get_control_type() const
{
static const std::string type = "list";
return type;
}
void list_view::signal_handler_left_button_down(const event::ui_event event)
{
DBG_GUI_E << LOG_HEADER << ' ' << event << ".\n";
assert(get_window());
get_window()->keyboard_capture(this);
}
void list_view::signal_handler_pre_child_left_button_click(
grid* grid, const event::ui_event event, bool& handled, bool& halt)
{
DBG_GUI_E << LOG_HEADER << ' ' << event << ".\n";
assert(grid);
assert(generator_);
for(size_t i = 0; i < generator_->get_item_count(); ++i) {
if(&generator_->item(i) == grid) {
/**
* @todo Here we should check whether the panel can be toggled.
*
* NO set halt + handled
* YES do nothing
*
* Then a post to the widget, which if done sets the proper state
* in the list.
*
* For now we simply assume an item can only be selected and not
* deselected (which is true at the moment).
*/
if(generator_->is_selected(i)) {
halt = true;
handled = true;
}
return;
}
}
assert(false);
}
void list_view::signal_handler_left_button_click(grid* grid,
const event::ui_event event)
{
DBG_GUI_E << LOG_HEADER << ' ' << event << ".\n";
assert(grid);
assert(generator_);
/** @todo Test the proper state to set. */
for(size_t i = 0; i < generator_->get_item_count(); ++i) {
if(&generator_->item(i) == grid) {
generator_->select_item(i);
fire(event::NOTIFY_MODIFIED, *this, nullptr);
}
}
}
void list_view::signal_handler_sdl_key_down(const event::ui_event event,
bool& handled,
const SDL_Keycode key,
SDL_Keymod modifier)
{
DBG_GUI_E << LOG_HEADER << ' ' << event << ".\n";
if(handled) {
return;
}
switch(key) {
case SDLK_UP:
generator_->handle_key_up_arrow(modifier, handled);
break;
case SDLK_DOWN:
generator_->handle_key_down_arrow(modifier, handled);
break;
case SDLK_LEFT:
generator_->handle_key_left_arrow(modifier, handled);
break;
case SDLK_RIGHT:
generator_->handle_key_right_arrow(modifier, handled);
break;
default:
;
/* Do nothing. */
}
if(handled) {
fire(event::NOTIFY_MODIFIED, *this, nullptr);
}
}
} // namespace gui2
#endif