The Battle for Wesnoth  1.15.0-dev
listbox.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2018 by Mark de Wever <koraq@xs4all.nl>
3  Part of the Battle for Wesnoth Project http://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #ifndef GUI2_EXPERIMENTAL_LISTBOX
16 
17 #define GETTEXT_DOMAIN "wesnoth-lib"
18 
19 #include "gui/widgets/listbox.hpp"
20 
21 #include "gettext.hpp"
24 #include "gui/core/log.hpp"
29 #include "gui/widgets/pane.hpp"
31 #include "gui/widgets/settings.hpp"
33 #include "gui/widgets/viewport.hpp"
35 #include "gui/widgets/window.hpp"
36 #include "sdl/rect.hpp"
37 #include "utils/functional.hpp"
38 
39 #include <boost/optional.hpp>
40 
41 #define LOG_SCOPE_HEADER get_control_type() + " [" + id() + "] " + __func__
42 #define LOG_HEADER LOG_SCOPE_HEADER + ':'
43 
44 namespace gui2
45 {
46 // ------------ WIDGET -----------{
47 
48 REGISTER_WIDGET(listbox)
49 REGISTER_WIDGET3(listbox_definition, horizontal_listbox, nullptr)
50 REGISTER_WIDGET3(listbox_definition, grid_listbox, nullptr)
51 
52 listbox::listbox(const implementation::builder_styled_widget& builder,
53  const generator_base::placement placement,
54  builder_grid_ptr list_builder,
55  const bool has_minimum,
56  const bool has_maximum,
57  const bool select)
58  : scrollbar_container(builder, get_control_type())
59  , generator_(generator_base::build(has_minimum, has_maximum, placement, select))
60  , is_horizontal_(placement == generator_base::horizontal_list)
61  , list_builder_(list_builder)
62  , need_layout_(false)
63  , orders_()
64  , callback_order_change_()
65 {
66 }
67 
68 grid& listbox::add_row(const string_map& item, const int index)
69 {
70  assert(generator_);
71  grid& row = generator_->create_item(index, list_builder_, item, std::bind(&listbox::list_item_clicked, this, _1));
72 
73  resize_content(row);
74 
75  return row;
76 }
77 
78 grid& listbox::add_row(const std::map<std::string /* widget id */, string_map>& data, const int index)
79 {
80  assert(generator_);
81  grid& row = generator_->create_item(index, list_builder_, data, std::bind(&listbox::list_item_clicked, this, _1));
82 
83  resize_content(row);
84 
85  return row;
86 }
87 
88 void listbox::remove_row(const unsigned row, unsigned count)
89 {
90  assert(generator_);
91 
92  if(row >= get_item_count()) {
93  return;
94  }
95 
96  if(!count || count + row > get_item_count()) {
97  count = get_item_count() - row;
98  }
99 
100  int height_reduced = 0;
101  int width_reduced = 0;
102 
103  // TODO: Fix this for horizontal listboxes
104  // Note the we have to use content_grid_ and cannot use "_list_grid" which is what generator_ uses.
105  int row_pos_y = is_horizontal_ ? -1 : generator_->item(row).get_y() - content_grid_->get_y();
106  int row_pos_x = is_horizontal_ ? -1 : 0;
107 
108  for(; count; --count) {
110  if(is_horizontal_) {
111  width_reduced += generator_->item(row).get_width();
112  } else {
113  height_reduced += generator_->item(row).get_height();
114  }
115  }
116 
117  generator_->delete_item(row);
118  }
119 
120  if((height_reduced != 0 || width_reduced != 0) && get_item_count() != 0) {
121  resize_content(-width_reduced, -height_reduced, row_pos_x, row_pos_y);
122  } else {
124  }
125 }
126 
128 {
129  generator_->clear();
131 }
132 
133 unsigned listbox::get_item_count() const
134 {
135  assert(generator_);
136  return generator_->get_item_count();
137 }
138 
139 void listbox::set_row_active(const unsigned row, const bool active)
140 {
141  assert(generator_);
142  generator_->item(row).set_active(active);
143 }
144 
145 void listbox::set_row_shown(const unsigned row, const bool shown)
146 {
147  assert(generator_);
148 
149  window* window = get_window();
150  assert(window);
151 
152  const int selected_row = get_selected_row();
153 
154  bool resize_needed = false;
155 
156  // Local scope for invalidate_layout_blocker
157  {
158  window::invalidate_layout_blocker invalidate_layout_blocker(*window);
159 
160  generator_->set_item_shown(row, shown);
161 
162  point best_size = generator_->calculate_best_size();
163  generator_->place(generator_->get_origin(), {std::max(best_size.x, content_visible_area().w), best_size.y});
164 
165  resize_needed = !content_resize_request();
166  }
167 
168  if(resize_needed) {
169  window->invalidate_layout();
170  } else {
171  content_grid_->set_visible_rectangle(content_visible_area());
172  }
173 
174  if(selected_row != get_selected_row()) {
175  fire(event::NOTIFY_MODIFIED, *this, nullptr);
176  }
177 }
178 
179 void listbox::set_row_shown(const boost::dynamic_bitset<>& shown)
180 {
181  assert(generator_);
182  assert(shown.size() == get_item_count());
183 
184  if(generator_->get_items_shown() == shown) {
185  LOG_GUI_G << LOG_HEADER << " returning early" << std::endl;
186  return;
187  }
188 
189  window* window = get_window();
190  assert(window);
191 
192  const int selected_row = get_selected_row();
193 
194  bool resize_needed = false;
195 
196  // Local scope for invalidate_layout_blocker
197  {
198  window::invalidate_layout_blocker invalidate_layout_blocker(*window);
199 
200  for(std::size_t i = 0; i < shown.size(); ++i) {
201  generator_->set_item_shown(i, shown[i]);
202  }
203 
204  point best_size = generator_->calculate_best_size();
205  generator_->place(generator_->get_origin(), {std::max(best_size.x, content_visible_area().w), best_size.y});
206 
207  resize_needed = !content_resize_request();
208  }
209 
210  if(resize_needed) {
211  window->invalidate_layout();
212  } else {
213  content_grid_->set_visible_rectangle(content_visible_area());
214  }
215 
216  if(selected_row != get_selected_row()) {
217  fire(event::NOTIFY_MODIFIED, *this, nullptr);
218  }
219 }
220 
221 boost::dynamic_bitset<> listbox::get_rows_shown() const
222 {
223  return generator_->get_items_shown();
224 }
225 
227 {
228  for(std::size_t i = 0; i < get_item_count(); i++) {
229  if(generator_->get_item_shown(i)) {
230  return true;
231  }
232  }
233 
234  return false;
235 }
236 
237 const grid* listbox::get_row_grid(const unsigned row) const
238 {
239  assert(generator_);
240  // rename this function and can we return a reference??
241  return &generator_->item(row);
242 }
243 
244 grid* listbox::get_row_grid(const unsigned row)
245 {
246  assert(generator_);
247  return &generator_->item(row);
248 }
249 
250 bool listbox::select_row(const unsigned row, const bool select)
251 {
252  assert(generator_);
253 
254  unsigned int before = generator_->get_selected_item_count();
255  generator_->select_item(row, select);
256 
257  return before != generator_->get_selected_item_count();
258 }
259 
260 bool listbox::select_row_at(const unsigned row, const bool select)
261 {
262  assert(generator_);
263  return select_row(generator_->get_item_at_ordered(row), select);
264 }
265 
266 bool listbox::row_selected(const unsigned row)
267 {
268  assert(generator_);
269  return generator_->is_selected(row);
270 }
271 
273 {
274  assert(generator_);
275  return generator_->get_selected_item();
276 }
277 
279 {
280  assert(generator_);
281 
282  /** @todo Hack to capture the keyboard focus. */
283  get_window()->keyboard_capture(this);
284 
285  for(std::size_t i = 0; i < generator_->get_item_count(); ++i) {
286  if(generator_->item(i).has_widget(caller)) {
287  toggle_button* checkbox = dynamic_cast<toggle_button*>(&caller);
288 
289  if(checkbox != nullptr) {
290  generator_->select_item(i, checkbox->get_value_bool());
291  } else {
293  }
294 
295  // TODO: enable this code once toggle_panel::set_value dispatches
296  // NOTIFY_MODIFED events. See comment in said function for more details.
297 #if 0
298  selectable_item& selectable = dynamic_cast<selectable_item&>(caller);
299 
300  generator_->select_item(i, selectable.get_value_bool());
301 #endif
302 
303  fire(event::NOTIFY_MODIFIED, *this, nullptr);
304  break;
305  }
306  }
307 
308  const int selected_item = generator_->get_selected_item();
309  if(selected_item == -1) {
310  return;
311  }
312 
313  const SDL_Rect& visible = content_visible_area();
314  SDL_Rect rect = generator_->item(selected_item).get_rectangle();
315 
316  if(sdl::rects_overlap(visible, rect)) {
317  rect.x = visible.x;
318  rect.w = visible.w;
319 
320  show_content_rect(rect);
321  }
322 }
323 
324 void listbox::set_self_active(const bool /*active*/)
325 {
326  /* DO NOTHING */
327 }
328 
330 {
332  return true;
333  }
334 
335  if(get_size() == point()) {
336  return false;
337  }
338 
339  if(content_resize_request(true)) {
340  content_grid_->set_visible_rectangle(content_visible_area());
341  return true;
342  }
343 
344  return false;
345 }
346 
347 void listbox::place(const point& origin, const point& size)
348 {
349  boost::optional<unsigned> vertical_scrollbar_position, horizontal_scrollbar_position;
350 
351  // Check if this is the first time placing the list box
352  if(get_origin() != point {-1, -1}) {
353  vertical_scrollbar_position = get_vertical_scrollbar_item_position();
354  horizontal_scrollbar_position = get_horizontal_scrollbar_item_position();
355  }
356 
357  // Inherited.
358  scrollbar_container::place(origin, size);
359 
360  const int selected_item = generator_->get_selected_item();
361  if(vertical_scrollbar_position && horizontal_scrollbar_position) {
362  LOG_GUI_L << LOG_HEADER << " restoring scroll position" << std::endl;
363 
364  set_vertical_scrollbar_item_position(*vertical_scrollbar_position);
365  set_horizontal_scrollbar_item_position(*horizontal_scrollbar_position);
366  } else if(selected_item != -1) {
367  LOG_GUI_L << LOG_HEADER << " making the initially selected item visible" << std::endl;
368 
369  const SDL_Rect& visible = content_visible_area();
370  SDL_Rect rect = generator_->item(selected_item).get_rectangle();
371 
372  rect.x = visible.x;
373  rect.w = visible.w;
374 
375  show_content_rect(rect);
376  }
377 }
378 
379 void listbox::resize_content(const int width_modification,
380  const int height_modification,
381  const int width_modification_pos,
382  const int height_modification_pos)
383 {
384  DBG_GUI_L << LOG_HEADER << " current size " << content_grid()->get_size() << " width_modification "
385  << width_modification << " height_modification " << height_modification << ".\n";
386 
388  width_modification, height_modification, width_modification_pos, height_modification_pos))
389  {
390  // Calculate new size.
392  size.x += width_modification;
393  size.y += height_modification;
394 
395  // Set new size.
396  content_grid()->set_size(size);
397 
398  // Set status.
399  need_layout_ = true;
400 
401 
402  DBG_GUI_L << LOG_HEADER << " succeeded.\n";
403  } else {
404  DBG_GUI_L << LOG_HEADER << " failed.\n";
405  }
406 }
407 
409 {
410  if(row.get_visible() == visibility::invisible) {
411  return;
412  }
413 
414  DBG_GUI_L << LOG_HEADER << " current size " << content_grid()->get_size() << " row size " << row.get_best_size()
415  << ".\n";
416 
417  const point content = content_grid()->get_size();
418  point size = row.get_best_size();
419 
420  if(size.x < content.x) {
421  size.x = 0;
422  } else {
423  size.x -= content.x;
424  }
425 
426  resize_content(size.x, size.y);
427 }
428 
430 {
431  layout_children(false);
432 }
433 
435 {
436  // Get the size from the base class, then add any extra space for the header and footer.
438 
439  if(const grid* header = find_widget<const grid>(&get_grid(), "_header_grid", false, false)) {
440  result.y += header->get_best_size().y;
441  }
442 
443  if(const grid* footer = find_widget<const grid>(&get_grid(), "_footer_grid", false, false)) {
444  result.y += footer->get_best_size().y;
445  }
446 
447  return result;
448 }
449 
451 {
452  const SDL_Rect& visible = content_visible_area();
454 
455  // When scrolling make sure the new items are visible...
456  if(direction == KEY_VERTICAL) {
457  // ...but leave the horizontal scrollbar position.
458  rect.x = visible.x;
459  rect.w = visible.w;
460  } else {
461  // ...but leave the vertical scrollbar position.
462  rect.y = visible.y;
463  rect.h = visible.h;
464  }
465 
466  show_content_rect(rect);
467 
468  fire(event::NOTIFY_MODIFIED, *this, nullptr);
469 }
470 
471 void listbox::handle_key_up_arrow(SDL_Keymod modifier, bool& handled)
472 {
473  assert(generator_);
474 
475  generator_->handle_key_up_arrow(modifier, handled);
476 
477  if(handled) {
479  } else {
480  // Inherited.
481  scrollbar_container::handle_key_up_arrow(modifier, handled);
482  }
483 }
484 
485 void listbox::handle_key_down_arrow(SDL_Keymod modifier, bool& handled)
486 {
487  assert(generator_);
488 
489  generator_->handle_key_down_arrow(modifier, handled);
490 
491  if(handled) {
493  } else {
494  // Inherited.
495  scrollbar_container::handle_key_up_arrow(modifier, handled);
496  }
497 }
498 
499 void listbox::handle_key_left_arrow(SDL_Keymod modifier, bool& handled)
500 {
501  assert(generator_);
502 
503  generator_->handle_key_left_arrow(modifier, handled);
504 
505  // Inherited.
506  if(handled) {
508  } else {
510  }
511 }
512 
513 void listbox::handle_key_right_arrow(SDL_Keymod modifier, bool& handled)
514 {
515  assert(generator_);
516 
517  generator_->handle_key_right_arrow(modifier, handled);
518 
519  // Inherited.
520  if(handled) {
522  } else {
524  }
525 }
526 
528  builder_grid_const_ptr footer,
529  const std::vector<std::map<std::string, string_map>>& list_data)
530 {
531  // "Inherited."
533 
534  assert(generator_);
535 
536  if(header) {
537  swap_grid(&get_grid(), content_grid(), header->build(), "_header_grid");
538  }
539 
540  grid& p = find_widget<grid>(this, "_header_grid", false);
541 
542  for(unsigned i = 0, max = std::max(p.get_cols(), p.get_rows()); i < max; ++i) {
543  //
544  // TODO: I had to change this to case to a toggle_button in order to use a signal handler.
545  // Should probably look into a way to make it more general like it was before (used to be
546  // cast to selectable_item).
547  //
548  // - vultraz, 2017-08-23
549  //
550  if(toggle_button* selectable = find_widget<toggle_button>(&p, "sort_" + std::to_string(i), false, false)) {
551  // Register callback to sort the list.
552  connect_signal_notify_modified(*selectable, std::bind(&listbox::order_by_column, this, i, _1));
553 
554  if(orders_.size() < max) {
555  orders_.resize(max);
556  }
557 
558  orders_[i].first = selectable;
559  }
560  }
561 
562  if(footer) {
563  swap_grid(&get_grid(), content_grid(), footer->build(), "_footer_grid");
564  }
565 
566  generator_->create_items(-1, list_builder_, list_data, std::bind(&listbox::list_item_clicked, this, _1));
567  swap_grid(nullptr, content_grid(), generator_, "_list_grid");
568 }
569 
570 void listbox::order_by_column(unsigned column, widget& widget)
571 {
572  selectable_item& selectable = dynamic_cast<selectable_item&>(widget);
573  if(column >= orders_.size()) {
574  return;
575  }
576 
577  for(auto& pair : orders_) {
578  if(pair.first != nullptr && pair.first != &selectable) {
579  pair.first->set_value(SORT_NONE);
580  }
581  }
582 
583  SORT_ORDER order = static_cast<SORT_ORDER>(selectable.get_value());
584 
585  if(static_cast<unsigned int>(order) > orders_[column].second.size()) {
586  return;
587  }
588 
589  if(order == SORT_NONE) {
590  order_by(std::less<unsigned>());
591  } else {
592  order_by(orders_[column].second[order - 1]);
593  }
594 
595  if(callback_order_change_ != nullptr) {
596  callback_order_change_(column, order);
597  }
598 }
599 
601 {
602  generator_->set_order(func);
603 
604  need_layout_ = true;
605 }
606 
607 void listbox::set_column_order(unsigned col, const generator_sort_array& func)
608 {
609  if(col >= orders_.size()) {
610  orders_.resize(col + 1);
611  }
612 
613  orders_[col].second = func;
614 }
615 
617 {
618  set_column_order(col, {{
619  [f](int lhs, int rhs) { return translation::icompare(f(lhs), f(rhs)) < 0; },
620  [f](int lhs, int rhs) { return translation::icompare(f(lhs), f(rhs)) > 0; }
621  }});
622 }
623 
624 void listbox::set_active_sorting_option(const order_pair& sort_by, const bool select_first)
625 {
626  // TODO: should this be moved to a public header_grid() getter function?
627  grid& header_grid = find_widget<grid>(this, "_header_grid", false);
628 
629  selectable_item& w = find_widget<selectable_item>(&header_grid, "sort_" + std::to_string(sort_by.first), false);
630 
631  // Set the sorting toggle widgets' value (in this case, its state) to the given sorting
632  // order. This is necessary since the widget's value is used to determine the order in
633  // @ref order_by_column in lieu of a direction being passed directly.
634  w.set_value(static_cast<int>(sort_by.second));
635 
636  order_by_column(sort_by.first, dynamic_cast<widget&>(w));
637 
638  if(select_first && generator_->get_item_count() > 0) {
639  select_row_at(0);
640  }
641 }
642 
644 {
645  for(unsigned int column = 0; column < orders_.size(); ++column) {
646  selectable_item* w = orders_[column].first;
647 
648  if(w && w->get_value() != SORT_NONE) {
649  return std::make_pair(column, static_cast<SORT_ORDER>(w->get_value()));
650  }
651  }
652 
653  return std::make_pair(-1, SORT_NONE);
654 }
655 
657 {
658  for(auto& pair : orders_) {
659  if(pair.first != nullptr) {
660  pair.first->set_value(SORT_NONE);
661  }
662  }
663 }
664 
665 void listbox::set_content_size(const point& origin, const point& size)
666 {
667  /** @todo This function needs more testing. */
668  assert(content_grid());
669 
670  const int best_height = content_grid()->get_best_size().y;
671  const point s(size.x, size.y < best_height ? size.y : best_height);
672 
673  content_grid()->place(origin, s);
674 }
675 
676 void listbox::layout_children(const bool force)
677 {
678  assert(content_grid());
679 
680  if(need_layout_ || force) {
682 
683  const SDL_Rect& visible = content_visible_area_;
684 
686 
687  need_layout_ = false;
688  }
689 }
690 
691 // }---------- DEFINITION ---------{
692 
695 {
696  DBG_GUI_P << "Parsing listbox " << id << '\n';
697 
698  load_resolutions<resolution>(cfg);
699 }
700 
701 /*WIKI
702  * @page = GUIWidgetDefinitionWML
703  * @order = 1_listbox
704  * @begin{parent}{name="gui/"}
705  * @begin{tag}{name="listbox_definition"}{min=0}{max=-1}{super="generic/widget_definition"}
706  * == Listbox ==
707  *
708  * @macro = listbox_description
709  *
710  * The definition of a listbox contains the definition of its scrollbar.
711  *
712  * The resolution for a listbox also contains the following keys:
713  * @begin{tag}{name="resolution"}{min=0}{max=-1}{super=generic/widget_definition/resolution}
714  * @begin{table}{config}
715  * scrollbar & section & & A grid containing the widgets for the
716  * scrollbar. The scrollbar has some special
717  * widgets so it can make default behavior
718  * for certain widgets. $
719  * @end{table}
720  * @begin{table}{dialog_widgets}
721  * _begin & & clickable & o & Moves the position to the beginning
722  * of the list. $
723  * _line_up & & clickable & o & Move the position one item up. (NOTE
724  * if too many items to move per item it
725  * might be more items.) $
726  * _half_page_up & & clickable & o &
727  * Move the position half the number of the
728  * visible items up. (See note at
729  * _line_up.) $
730  * _page_up & & clickable & o & Move the position the number of
731  * visible items up. (See note at
732  * _line_up.) $
733  *
734  * _end & & clickable & o & Moves the position to the end of the
735  * list. $
736  * _line_down & & clickable & o & Move the position one item down.(See
737  * note at _line_up.) $
738  * _half_page_down & & clickable & o &
739  * Move the position half the number of the
740  * visible items down. (See note at
741  * _line_up.) $
742  * _page_down & & clickable & o & Move the position the number of
743  * visible items down. (See note at
744  * _line_up.) $
745  *
746  * _scrollbar & & vertical_scrollbar & m &
747  * This is the scrollbar so the user can
748  * scroll through the list. $
749  * @end{table}
750  * A clickable is one of:
751  * * button
752  * * repeating_button
753  * @{allow}{link}{name="gui/window/resolution/grid/row/column/button"}
754  * @{allow}{link}{name="gui/window/resolution/grid/row/column/repeating_button"}
755  * The following states exist:
756  * * state_enabled, the listbox is enabled.
757  * * state_disabled, the listbox is disabled.
758  * @begin{tag}{name="state_enabled"}{min=0}{max=1}{super="generic/state"}
759  * @end{tag}{name="state_enabled"}
760  * @begin{tag}{name="state_disabled"}{min=0}{max=1}{super="generic/state"}
761  * @end{tag}{name="state_disabled"}
762  * @allow{link}{name="gui/window/resolution/grid"}
763  * @end{tag}{name="resolution"}
764  * @end{tag}{name="listbox_definition"}
765  * @end{parent}{name="gui/"}
766  */
767 
768 /*WIKI
769  * @page = GUIWidgetDefinitionWML
770  * @order = 1_horizonal_listbox
771  *
772  * == Horizontal listbox ==
773  * @begin{parent}{name="gui/"}
774  * @begin{tag}{name="horizontal_listbox_definition"}{min=0}{max=-1}{super="gui/listbox_definition"}
775  * @end{tag}{name="horizontal_listbox_definition"}
776  * @end{parent}{name="gui/"}
777  * @macro = horizontal_listbox_description
778  * The definition of a horizontal listbox is the same as for a normal listbox.
779  */
781  : resolution_definition(cfg)
782  , grid(nullptr)
783 {
784  // Note the order should be the same as the enum state_t in listbox.hpp.
785  state.emplace_back(cfg.child("state_enabled"));
786  state.emplace_back(cfg.child("state_disabled"));
787 
788  const config& child = cfg.child("grid");
789  VALIDATE(child, _("No grid defined."));
790 
791  grid = std::make_shared<builder_grid>(child);
792 }
793 
794 // }---------- BUILDER -----------{
795 
796 /*WIKI_MACRO
797  * @begin{macro}{listbox_description}
798  *
799  * A listbox is a styled_widget that holds several items of the same type.
800  * Normally the items in a listbox are ordered in rows, this version
801  * might allow more options for ordering the items in the future.
802  * @end{macro}
803  */
804 
805 /*WIKI
806  * @page = GUIWidgetInstanceWML
807  * @order = 2_listbox
808  *
809  * == Listbox ==
810  * @begin{parent}{name="gui/window/resolution/grid/row/column/"}
811  * @begin{tag}{name="listbox"}{min=0}{max=-1}{super="generic/widget_instance"}
812  * @macro = listbox_description
813  *
814  * List with the listbox specific variables:
815  * @begin{table}{config}
816  * vertical_scrollbar_mode & scrollbar_mode & initial_auto &
817  * Determines whether or not to show the
818  * scrollbar. $
819  * horizontal_scrollbar_mode & scrollbar_mode & initial_auto &
820  * Determines whether or not to show the
821  * scrollbar. $
822  *
823  * header & grid & [] & Defines the grid for the optional
824  * header. (This grid will automatically
825  * get the id _header_grid.) $
826  * footer & grid & [] & Defines the grid for the optional
827  * footer. (This grid will automatically
828  * get the id _footer_grid.) $
829  *
830  * list_definition & section & & This defines how a listbox item
831  * looks. It must contain the grid
832  * definition for 1 row of the list. $
833  *
834  * list_data & section & [] & A grid alike section which stores the
835  * initial data for the listbox. Every row
836  * must have the same number of columns as
837  * the 'list_definition'. $
838  *
839  * has_minimum & bool & true & If false, less than one row can be selected. $
840  *
841  * has_maximum & bool & true & If false, more than one row can be selected. $
842  *
843  * @end{table}
844  * @begin{tag}{name="header"}{min=0}{max=1}{super="gui/window/resolution/grid"}
845  * @end{tag}{name="header"}
846  * @begin{tag}{name="footer"}{min=0}{max=1}{super="gui/window/resolution/grid"}
847  * @end{tag}{name="footer"}
848  * @begin{tag}{name="list_definition"}{min=0}{max=1}
849  * @begin{tag}{name="row"}{min=1}{max=1}{super="generic/listbox_grid/row"}
850  * @end{tag}{name="row"}
851  * @end{tag}{name="list_definition"}x
852  * @begin{tag}{name="list_data"}{min=0}{max=1}{super="generic/listbox_grid"}
853  * @end{tag}{name="list_data"}
854  *
855  * In order to force widgets to be the same size inside a listbox, the widgets
856  * need to be inside a linked_group.
857  *
858  * Inside the list section there are only the following widgets allowed
859  * * grid (to nest)
860  * * selectable widgets which are
861  * ** toggle_button
862  * ** toggle_panel
863  * @end{tag}{name="listbox"}
864  *
865  * @end{parent}{name="gui/window/resolution/grid/row/column/"}
866  */
867 
868 /*WIKI
869  * @begin{parent}{name="generic/"}
870  * @begin{tag}{name="listbox_grid"}{min="0"}{max="-1"}
871  * @begin{tag}{name="row"}{min="0"}{max="-1"}
872  * @begin{table}{config}
873  * grow_factor & unsigned & 0 & The grow factor for a row. $
874  * @end{table}
875  * @begin{tag}{name="column"}{min="0"}{max="-1"}{super="gui/window/resolution/grid/row/column"}
876  * @begin{table}{config}
877  * label & t_string & "" & $
878  * tooltip & t_string & "" & $
879  * icon & t_string & "" & $
880  * @end{table}
881  * @allow{link}{name="gui/window/resolution/grid/row/column/toggle_button"}
882  * @allow{link}{name="gui/window/resolution/grid/row/column/toggle_panel"}
883  * @end{tag}{name="column"}
884  * @end{tag}{name="row"}
885  * @end{tag}{name="listbox_grid"}
886  * @end{parent}{name="generic/"}
887  */
888 
889 namespace implementation
890 {
891 static std::vector<std::map<std::string, string_map>> parse_list_data(const config& data, const unsigned int req_cols)
892 {
893  std::vector<std::map<std::string, string_map>> list_data;
894 
895  for(const auto& row : data.child_range("row")) {
896  auto cols = row.child_range("column");
897 
898  VALIDATE(static_cast<unsigned>(cols.size()) == req_cols,
899  _("'list_data' must have the same number of columns as the 'list_definition'.")
900  );
901 
902  for(const auto& c : cols) {
903  list_data.emplace_back();
904 
905  for(const auto& i : c.attribute_range()) {
906  list_data.back()[""][i.first] = i.second;
907  }
908 
909  for(const auto& w : c.child_range("widget")) {
910  VALIDATE(w.has_attribute("id"), missing_mandatory_wml_key("[list_data][row][column][widget]", "id"));
911 
912  for(const auto& i : w.attribute_range()) {
913  list_data.back()[w["id"]][i.first] = i.second;
914  }
915  }
916  }
917  }
918 
919  return list_data;
920 }
921 
922 builder_listbox::builder_listbox(const config& cfg)
923  : builder_styled_widget(cfg)
924  , vertical_scrollbar_mode(get_scrollbar_mode(cfg["vertical_scrollbar_mode"]))
925  , horizontal_scrollbar_mode(get_scrollbar_mode(cfg["horizontal_scrollbar_mode"]))
926  , header(nullptr)
927  , footer(nullptr)
928  , list_builder(nullptr)
929  , list_data()
930  , has_minimum_(cfg["has_minimum"].to_bool(true))
931  , has_maximum_(cfg["has_maximum"].to_bool(true))
932 {
933  if(const config& h = cfg.child("header")) {
934  header = std::make_shared<builder_grid>(h);
935  }
936 
937  if(const config& f = cfg.child("footer")) {
938  footer = std::make_shared<builder_grid>(f);
939  }
940 
941  const config& l = cfg.child("list_definition");
942 
943  VALIDATE(l, _("No list defined."));
944 
945  list_builder = std::make_shared<builder_grid>(l);
946  assert(list_builder);
947 
948  VALIDATE(list_builder->rows == 1, _("A 'list_definition' should contain one row."));
949 
950  if(cfg.has_child("list_data")) {
951  list_data = parse_list_data(cfg.child("list_data"), list_builder->cols);
952  }
953 }
954 
956 {
957 #ifdef GUI2_EXPERIMENTAL_LISTBOX
958  list_view* widget = new list_view(true, true, generator_base::vertical_list, true, list_builder);
959 
960  // init_control(widget);
961  if(!list_data.empty()) {
962  widget->append_rows(list_data);
963  }
964 
965  return widget;
966 #else
967 
969 
972 
973  DBG_GUI_G << "Window builder: placed listbox '" << id << "' with definition '" << definition << "'.\n";
974 
975  const auto conf = widget->cast_config_to<listbox_definition>();
976  assert(conf);
977 
978  widget->init_grid(conf->grid);
979 
980  widget->finalize(header, footer, list_data);
981 
982  return widget;
983 #endif
984 }
985 
986 /*WIKI_MACRO
987  * @begin{macro}{horizontal_listbox_description}
988  *
989  * A horizontal listbox is a styled_widget that holds several items of the
990  * same type. Normally the items in a listbox are ordered in rows,
991  * this version orders them in columns instead.
992  * @end{macro}
993  */
994 
995 /*WIKI
996  * @page = GUIWidgetInstanceWML
997  * @order = 2_horizontal_listbox
998  * @begin{parent}{name="gui/window/resolution/grid/row/column/"}
999  * @begin{tag}{name="horizontal_listbox"}{min="0"}{max="-1"}{super="generic/widget_instance"}
1000  * == Horizontal listbox ==
1001  *
1002  * @macro = horizontal_listbox_description
1003  *
1004  * List with the horizontal listbox specific variables:
1005  * @begin{table}{config}
1006  * vertical_scrollbar_mode & scrollbar_mode & initial_auto &
1007  * Determines whether or not to show the
1008  * scrollbar. $
1009  * horizontal_scrollbar_mode & scrollbar_mode & initial_auto &
1010  * Determines whether or not to show the
1011  * scrollbar. $
1012  *
1013  * list_definition & section & & This defines how a listbox item
1014  * looks. It must contain the grid
1015  * definition for 1 column of the list. $
1016  *
1017  * list_data & section & [] & A grid alike section which stores the
1018  * initial data for the listbox. Every row
1019  * must have the same number of columns as
1020  * the 'list_definition'. $
1021  *
1022  * has_minimum & bool & true & If false, less than one row can be selected. $
1023  *
1024  * has_maximum & bool & true & If false, more than one row can be selected. $
1025  *
1026  * @end{table}
1027  * @begin{tag}{name="header"}{min=0}{max=1}{super="gui/window/resolution/grid"}
1028  * @end{tag}{name="header"}
1029  * @begin{tag}{name="footer"}{min=0}{max=1}{super="gui/window/resolution/grid"}
1030  * @end{tag}{name="footer"}
1031  * @begin{tag}{name="list_definition"}{min=0}{max=1}
1032  * @begin{tag}{name="row"}{min=1}{max=1}{super="generic/listbox_grid/row"}
1033  * @end{tag}{name="row"}
1034  * @end{tag}{name="list_definition"}
1035  * @begin{tag}{name="list_data"}{min=0}{max=1}{super="generic/listbox_grid"}
1036  * @end{tag}{name="list_data"}
1037  * In order to force widgets to be the same size inside a horizontal listbox,
1038  * the widgets need to be inside a linked_group.
1039  *
1040  * Inside the list section there are only the following widgets allowed
1041  * * grid (to nest)
1042  * * selectable widgets which are
1043  * ** toggle_button
1044  * ** toggle_panel
1045  * @end{tag}{name="horizontal_listbox"}
1046  * @end{parent}{name="gui/window/resolution/grid/row/column/"}
1047  */
1048 
1050  : builder_styled_widget(cfg)
1051  , vertical_scrollbar_mode(get_scrollbar_mode(cfg["vertical_scrollbar_mode"]))
1052  , horizontal_scrollbar_mode(get_scrollbar_mode(cfg["horizontal_scrollbar_mode"]))
1053  , list_builder(nullptr)
1054  , list_data()
1055  , has_minimum_(cfg["has_minimum"].to_bool(true))
1056  , has_maximum_(cfg["has_maximum"].to_bool(true))
1057 {
1058  const config& l = cfg.child("list_definition");
1059 
1060  VALIDATE(l, _("No list defined."));
1061 
1062  list_builder = std::make_shared<builder_grid>(l);
1063  assert(list_builder);
1064 
1065  VALIDATE(list_builder->rows == 1, _("A 'list_definition' should contain one row."));
1066 
1067  if(cfg.has_child("list_data")) {
1068  list_data = parse_list_data(cfg.child("list_data"), list_builder->cols);
1069  }
1070 }
1071 
1073 {
1074 #ifdef GUI2_EXPERIMENTAL_LISTBOX
1075  list_view* widget = new list_view(true, true, generator_base::horizontal_list, true, list_builder);
1076 
1077  // init_control(widget);
1078  if(!list_data.empty()) {
1079  widget->append_rows(list_data);
1080  }
1081 
1082  return widget;
1083 #else
1084 
1086 
1089 
1090  DBG_GUI_G << "Window builder: placed listbox '" << id << "' with definition '" << definition << "'.\n";
1091 
1092  const auto conf = widget->cast_config_to<listbox_definition>();
1093  assert(conf);
1094 
1095  widget->init_grid(conf->grid);
1096 
1097  widget->finalize(nullptr, nullptr, list_data);
1098 
1099  return widget;
1100 #endif
1101 }
1102 
1103 /*WIKI_MACRO
1104  * @begin{macro}{grid_listbox_description}
1105  *
1106  * A grid listbox is a styled_widget that holds several items of the
1107  * same type. Normally the items in a listbox are ordered in rows,
1108  * this version orders them in a grid instead.
1109  * @end{macro}
1110  */
1111 
1112 /*WIKI
1113  * @page = GUIWidgetInstanceWML
1114  * @order = 2_grid_listbox
1115  * @begin{parent}{name="gui/window/resolution/grid/row/column/"}
1116  * @begin{tag}{name="grid_listbox"}{min="0"}{max="-1"}{super="generic/widget_instance"}
1117  * == Horizontal listbox ==
1118  *
1119  * @macro = grid_listbox_description
1120  *
1121  * List with the grid listbox specific variables:
1122  * @begin{table}{config}
1123  * vertical_scrollbar_mode & scrollbar_mode & initial_auto &
1124  * Determines whether or not to show the
1125  * scrollbar. $
1126  * horizontal_scrollbar_mode & scrollbar_mode & initial_auto &
1127  * Determines whether or not to show the
1128  * scrollbar. $
1129  *
1130  * list_definition & section & & This defines how a listbox item
1131  * looks. It must contain the grid
1132  * definition for 1 column of the list. $
1133  *
1134  * list_data & section & [] & A grid alike section which stores the
1135  * initial data for the listbox. Every row
1136  * must have the same number of columns as
1137  * the 'list_definition'. $
1138  *
1139  * has_minimum & bool & true & If false, less than one cell can be selected. $
1140  *
1141  * has_maximum & bool & true & If false, more than one cell can be selected. $
1142  *
1143  * @end{table}
1144  * @begin{tag}{name="header"}{min=0}{max=1}{super="gui/window/resolution/grid"}
1145  * @end{tag}{name="header"}
1146  * @begin{tag}{name="footer"}{min=0}{max=1}{super="gui/window/resolution/grid"}
1147  * @end{tag}{name="footer"}
1148  * @begin{tag}{name="list_definition"}{min=0}{max=1}
1149  * @begin{tag}{name="row"}{min=1}{max=1}{super="generic/listbox_grid/row"}
1150  * @end{tag}{name="row"}
1151  * @end{tag}{name="list_definition"}
1152  * @begin{tag}{name="list_data"}{min=0}{max=1}{super="generic/listbox_grid"}
1153  * @end{tag}{name="list_data"}
1154  * In order to force widgets to be the same size inside a grid listbox,
1155  * the widgets need to be inside a linked_group.
1156  *
1157  * Inside the list section there are only the following widgets allowed
1158  * * grid (to nest)
1159  * * selectable widgets which are
1160  * ** toggle_button
1161  * ** toggle_panel
1162  * @end{tag}{name="grid_listbox"}
1163  * @end{parent}{name="gui/window/resolution/grid/row/column/"}
1164  */
1165 
1167  : builder_styled_widget(cfg)
1168  , vertical_scrollbar_mode(get_scrollbar_mode(cfg["vertical_scrollbar_mode"]))
1169  , horizontal_scrollbar_mode(get_scrollbar_mode(cfg["horizontal_scrollbar_mode"]))
1170  , list_builder(nullptr)
1171  , list_data()
1172  , has_minimum_(cfg["has_minimum"].to_bool(true))
1173  , has_maximum_(cfg["has_maximum"].to_bool(true))
1174 {
1175  const config& l = cfg.child("list_definition");
1176 
1177  VALIDATE(l, _("No list defined."));
1178 
1179  list_builder = std::make_shared<builder_grid>(l);
1180  assert(list_builder);
1181 
1182  VALIDATE(list_builder->rows == 1, _("A 'list_definition' should contain one row."));
1183 
1184  if(cfg.has_child("list_data")) {
1185  list_data = parse_list_data(cfg.child("list_data"), list_builder->cols);
1186  }
1187 }
1188 
1190 {
1191 #ifdef GUI2_EXPERIMENTAL_LISTBOX
1192  list_view* widget = new list_view(true, true, generator_base::grid, true, list_builder);
1193 
1194  // init_control(widget);
1195  if(!list_data.empty()) {
1196  widget->append_rows(list_data);
1197  }
1198 
1199  return widget;
1200 #else
1201 
1203 
1206 
1207  DBG_GUI_G << "Window builder: placed listbox '" << id << "' with definition '" << definition << "'.\n";
1208 
1209  const auto conf = widget->cast_config_to<listbox_definition>();
1210  assert(conf);
1211 
1212  widget->init_grid(conf->grid);
1213 
1214  widget->finalize(nullptr, nullptr, list_data);
1215 
1216  return widget;
1217 #endif
1218 }
1219 
1220 } // namespace implementation
1221 
1222 // }------------ END --------------
1223 
1224 } // namespace gui2
1225 
1226 #endif
const order_pair get_active_sorting_option()
Definition: listbox.cpp:643
Define the common log macros for the gui toolkit.
virtual void handle_key_left_arrow(SDL_Keymod modifier, bool &handled)=0
Left arrow key pressed.
void show_content_rect(const SDL_Rect &rect)
Shows a certain part of the content.
Base class of a resolution, contains the common keys for a resolution.
void keyboard_capture(widget *widget)
Definition: window.cpp:1075
virtual void handle_key_up_arrow(SDL_Keymod modifier, bool &handled)=0
Up arrow key pressed.
virtual unsigned get_item_at_ordered(unsigned index_ordered) const =0
#define DBG_GUI_P
Definition: log.hpp:68
void set_active_sorting_option(const order_pair &sort_by, const bool select_first=false)
Sorts the listbox by a pre-set sorting option.
Definition: listbox.cpp:624
Defines the exception classes for the layout algorithm.
Small abstract helper class.
virtual void set_visible_rectangle(const SDL_Rect &rectangle) override
See widget::set_visible_rectangle.
Definition: grid.cpp:603
config & child(config_key_type key, int n=0)
Returns the nth child with the given key, or a reference to an invalid config if there is none...
Definition: config.cpp:423
#define LOG_GUI_L
Definition: log.hpp:58
Helper class to block invalidate_layout.
Definition: window.hpp:180
std::vector< state_definition > state
void set_horizontal_scrollbar_mode(const scrollbar_mode scrollbar_mode)
std::function< void(unsigned, SORT_ORDER)> callback_order_change_
Definition: listbox.hpp:380
virtual point calculate_best_size() const override
See widget::calculate_best_size.
Definition: listbox.cpp:434
#define DBG_GUI_L
Definition: log.hpp:57
virtual unsigned get_item_count() const =0
Returns the number of items.
virtual void handle_key_right_arrow(SDL_Keymod modifier, bool &handled)=0
Right arrow key pressed.
virtual grid & create_item(const int index, builder_grid_const_ptr list_builder, const string_map &item_data, const std::function< void(widget &)> &callback)=0
Creates a new item.
void finalize_setup()
The builder needs to call us so we do our setup.
void update_visible_area_on_key_event(const KEY_SCROLL_DIRECTION direction)
Helper to update visible area after a key event.
Definition: listbox.cpp:450
virtual void set_item_shown(const unsigned index, const bool show)=0
Shows or hides an item.
void mark_as_unsorted()
Deactivates all sorting toggle buttons at the top, making the list look like it&#39;s not sorted...
Definition: listbox.cpp:656
std::shared_ptr< const builder_grid > builder_grid_const_ptr
scrollbar_container::scrollbar_mode horizontal_scrollbar_mode
Definition: listbox.hpp:450
bool rects_overlap(const SDL_Rect &rect1, const SDL_Rect &rect2)
Tests whether two rectangles overlap.
Definition: rect.cpp:33
const grid & get_grid() const
virtual void place(const point &origin, const point &size) override=0
See widget::place.
scrollbar_container::scrollbar_mode horizontal_scrollbar_mode
Definition: listbox.hpp:477
visibility get_visible() const
Definition: widget.cpp:470
virtual void place(const point &origin, const point &size) override
See widget::place.
#define LOG_HEADER
Definition: listbox.cpp:42
bool has_child(config_key_type key) const
Determine whether a config has a child or not.
Definition: config.cpp:416
virtual void set_order(const order_func &order)=0
virtual bool is_selected(const unsigned index) const =0
Returns whether the item is selected.
void resize_content(const int width_modification, const int height_modification, const int width__modification_pos=-1, const int height_modification_pos=-1)
Resizes the content.
Definition: listbox.cpp:379
virtual bool has_widget(const widget &widget) const override
See widget::has_widget.
Definition: grid.cpp:651
This file contains the window object, this object is a top level container which has the event manage...
child_itors child_range(config_key_type key)
Definition: config.cpp:366
void set_vertical_scrollbar_mode(const scrollbar_mode scrollbar_mode)
SDL_Rect get_rectangle() const
Gets the bounding rectangle of the widget on the screen.
Definition: widget.cpp:302
Base class for all widgets.
Definition: widget.hpp:47
std::function< bool(unsigned, unsigned)> order_func
Definition: generator.hpp:248
unsigned get_height() const
Definition: widget.cpp:322
void register_translatable_sorting_option(const int col, translatable_sorter_func_t f)
Registers a special sorting function specifically for translatable values.
Definition: listbox.cpp:616
virtual grid & item(const unsigned index)=0
Gets the grid of an item.
#define h
void swap_grid(grid *g, grid *content_grid, widget *widget, const std::string &id)
Swaps an item in a grid for another one.
std::unique_ptr< grid > content_grid_
The grid that holds the content.
bool row_selected(const unsigned row)
Check if a row is selected.
Definition: listbox.cpp:266
bool need_layout_
Definition: listbox.hpp:375
virtual unsigned get_selected_item_count() const =0
Returns the number of selected items.
int get_selected_row() const
Returns the first selected row.
Definition: listbox.cpp:272
void handle_key_down_arrow(SDL_Keymod modifier, bool &handled) override
Inherited from scrollbar_container.
Definition: listbox.cpp:485
void set_row_active(const unsigned row, const bool active)
Makes a row active or inactive.
Definition: listbox.cpp:139
std::pair< int, SORT_ORDER > order_pair
Definition: listbox.hpp:287
unsigned get_width() const
Definition: widget.cpp:317
bool select_row(const unsigned row, const bool select=true)
Selects a row.
Definition: listbox.cpp:250
torder_list orders_
Definition: listbox.hpp:378
std::string missing_mandatory_wml_key(const std::string &section, const std::string &key, const std::string &primary_key, const std::string &primary_value)
Returns a standard message for a missing wml key.
virtual point calculate_best_size() const override
See widget::calculate_best_size.
int x
x coordinate.
Definition: point.hpp:44
bool select_row_at(const unsigned row, const bool select=true)
Selects a row at the given position, regardless of sorting order.
Definition: listbox.cpp:260
Generic file dialog.
Definition: field-fwd.hpp:22
Sent by a widget to notify others its contents or state are modified.
Definition: handler.hpp:88
virtual point calculate_best_size() const override=0
See widget::calculate_best_size.
The listbox class.
Definition: listbox.hpp:42
void finalize(builder_grid_const_ptr header, builder_grid_const_ptr footer, const std::vector< std::map< std::string, string_map >> &list_data)
Finishes the building initialization of the widget.
Definition: listbox.cpp:527
Base container class.
Definition: grid.hpp:30
scrollbar_container::scrollbar_mode vertical_scrollbar_mode
Definition: listbox.hpp:500
virtual void handle_key_left_arrow(SDL_Keymod modifier, bool &handled)
Left arrow key pressed.
static UNUSEDNOWARN std::string _(const char *str)
Definition: gettext.hpp:89
std::string definition
Parameters for the styled_widget.
std::size_t size(const std::string &str)
Length in characters of a UTF-8 string.
Definition: unicode.cpp:86
scrollbar_container::scrollbar_mode horizontal_scrollbar_mode
Definition: listbox.hpp:501
void connect_signal_notify_modified(dispatcher &dispatcher, const signal_notification_function &signal)
Connects a signal handler for getting a notification upon modification.
Definition: dispatcher.cpp:179
point get_best_size() const
Gets the best size for the widget.
Definition: widget.cpp:189
Abstract base class for the generator.
Definition: generator.hpp:39
#define VALIDATE(cond, message)
The macro to use for the validation of WML.
listbox_definition(const config &cfg)
Definition: listbox.cpp:693
std::vector< std::map< std::string, string_map > > list_data
Listbox data.
Definition: listbox.hpp:463
This file contains the settings handling of the widget library.
virtual void set_content_size(const point &origin, const point &size) override
Inherited from scrollbar_container.
Definition: listbox.cpp:665
void clear()
Removes all the rows in the listbox, clearing it.
Definition: listbox.cpp:127
builder_grid_const_ptr list_builder_
Contains the builder for the new items.
Definition: listbox.hpp:373
void toggle_item(const unsigned index)
Toggles the selection state of an item.
Definition: generator.hpp:97
unsigned get_item_count() const
Returns the number of items in the listbox.
Definition: listbox.cpp:133
bool content_resize_request(const bool force_sizing=false)
Notification if the content of a child needs a resize.
bool any_rows_shown() const
Definition: listbox.cpp:226
int get_y() const
Definition: widget.cpp:312
void order_by_column(unsigned column, widget &widget)
Definition: listbox.cpp:570
bool update_content_size()
Request to update the size of the content after changing the content.
Definition: listbox.cpp:329
virtual void create_items(const int index, builder_grid_const_ptr list_builder, const std::vector< string_map > &data, const std::function< void(widget &)> &callback)=0
Creates one or more new item(s).
void init_grid(const std::shared_ptr< builder_grid > &grid_builder)
Initializes and builds the grid.
virtual bool get_item_shown(const unsigned index) const =0
Returns whether the item is shown.
void list_item_clicked(widget &caller)
Function to call after the user clicked on a row.
Definition: listbox.cpp:278
#define REGISTER_WIDGET(id)
Wrapper for REGISTER_WIDGET3.
SDL_Rect rect
The coordinates of this image on the spritesheet.
virtual void place(const point &origin, const point &size) override
See widget::place.
Definition: grid.cpp:479
void order_by(const generator_base::order_func &func)
Definition: listbox.cpp:600
virtual void set_self_active(const bool active) override
See container_base::set_self_active.
Definition: listbox.cpp:324
std::vector< std::map< std::string, string_map > > list_data
Listbox data.
Definition: listbox.hpp:487
Base class for creating containers with one or two scrollbar(s).
virtual int get_selected_item() const =0
Returns the selected item.
std::size_t i
Definition: function.cpp:933
void handle_key_right_arrow(SDL_Keymod modifier, bool &handled) override
Inherited from scrollbar_container.
Definition: listbox.cpp:513
#define LOG_GUI_G
Definition: log.hpp:41
virtual void clear()=0
Deletes all items.
std::shared_ptr< const typename T::resolution > cast_config_to() const
Casts the current resolution definition config to the respective type of a derived widget...
void set_vertical_scrollbar_item_position(const unsigned position)
Move the vertical scrollbar to a position.
mock_party p
The user set the widget invisible, that means:
window * get_window()
Get the parent window.
Definition: widget.cpp:113
static map_location::DIRECTION s
const SDL_Rect & content_visible_area() const
std::map< std::string, t_string > string_map
Definition: widget.hpp:24
std::array< generator_base::order_func, 2 > generator_sort_array
Definition: generator.hpp:371
Holds a 2D point.
Definition: point.hpp:23
grid & add_row(const string_map &item, const int index=-1)
When an item in the list is selected by the user we need to update the state.
Definition: listbox.cpp:68
void set_active(const bool active)
Activates all children.
Definition: grid.cpp:162
int w
std::size_t index(const std::string &str, const std::size_t index)
Codepoint index corresponding to the nth character in a UTF-8 string.
Definition: unicode.cpp:71
virtual void set_value(unsigned value, bool fire_event=false)=0
Select the styled_widget.
scrollbar_mode get_scrollbar_mode(const std::string &scrollbar_mode)
Returns the scrollbar mode flags.
Definition: helper.cpp:120
boost::dynamic_bitset get_rows_shown() const
Returns a list of visible rows.
Definition: listbox.cpp:221
virtual void layout_children() override
See widget::layout_children.
Definition: listbox.cpp:429
bool grid()
Definition: general.cpp:507
point get_size() const
Returns the size of the widget.
Definition: widget.cpp:297
const grid * get_row_grid(const unsigned row) const
Returns the grid of the wanted row.
Definition: listbox.cpp:237
std::vector< std::map< std::string, string_map > > list_data
Listbox data.
Definition: listbox.hpp:511
Contains the SDL_Rect helper code.
virtual void delete_item(const unsigned index)=0
Deletes an item.
#define f
static std::vector< std::map< std::string, string_map > > parse_list_data(const config &data, const unsigned int req_cols)
Definition: listbox.cpp:891
int icompare(const std::string &s1, const std::string &s2)
Case-insensitive lexicographical comparison.
Definition: gettext.cpp:476
SDL_Rect content_visible_area_
Cache for the visible area for the content.
virtual void handle_key_down_arrow(SDL_Keymod modifier, bool &handled)=0
Down arrow key pressed.
virtual void select_item(const unsigned index, const bool select)=0
(De)selects an item.
void remove_row(const unsigned row, unsigned count=1)
Removes a row in the listbox.
Definition: listbox.cpp:88
const bool is_horizontal_
Definition: listbox.hpp:370
#define REGISTER_WIDGET3(type, id, key)
Registers a widget.
unsigned get_vertical_scrollbar_item_position() const
Returns current position of the vertical scrollbar.
bool fire(const ui_event event, widget &target)
Fires an event which has no extra parameters.
Definition: dispatcher.cpp:66
void set_column_order(unsigned col, const generator_sort_array &func)
Definition: listbox.cpp:607
virtual void set_size(const point &size)
Sets the size of the widget.
Definition: widget.cpp:223
void invalidate_layout()
Updates the size of the window.
Definition: window.cpp:613
void handle_key_up_arrow(SDL_Keymod modifier, bool &handled) override
Inherited from scrollbar_container.
Definition: listbox.cpp:471
virtual void handle_key_up_arrow(SDL_Keymod modifier, bool &handled)
Up arrow key pressed.
generator_base * generator_
Contains a pointer to the generator.
Definition: listbox.hpp:368
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:68
mock_char c
std::function< std::string(const int)> translatable_sorter_func_t
Definition: listbox.hpp:276
unsigned get_horizontal_scrollbar_item_position() const
Returns current position of the horizontal scrollbar.
int y
y coordinate.
Definition: point.hpp:47
scrollbar_container::scrollbar_mode vertical_scrollbar_mode
Definition: listbox.hpp:476
base class of top level items, the only item which needs to store the final canvases to draw on ...
Definition: window.hpp:63
#define DBG_GUI_G
Definition: log.hpp:40
void set_row_shown(const unsigned row, const bool shown)
Makes a row visible or invisible.
Definition: listbox.cpp:145
std::shared_ptr< builder_grid > builder_grid_ptr
void handle_key_left_arrow(SDL_Keymod modifier, bool &handled) override
Inherited from scrollbar_container.
Definition: listbox.cpp:499
scrollbar_container::scrollbar_mode vertical_scrollbar_mode
Definition: listbox.hpp:449
virtual void place(const point &origin, const point &size) override
See widget::place.
Definition: listbox.cpp:347
point get_origin() const
Returns the screen origin of the widget.
Definition: widget.cpp:292
Class for a toggle button.
Contains the implementation details for lexical_cast and shouldn&#39;t be used directly.
boost::dynamic_bitset get_items_shown() const
Returns the visibility of all the items as a bit set.
Definition: generator.hpp:119
virtual unsigned get_value() const =0
Is the styled_widget selected?
void set_horizontal_scrollbar_item_position(const unsigned position)
Move the horizontal scrollbar to a position.