The Battle for Wesnoth  1.15.1+dev
theme_list.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014 - 2018 by Iris Morelle <shadowm2006@gmail.com>
3  Part of the Battle for Wesnoth Project https://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 
16 
18 #include "gui/widgets/listbox.hpp"
19 #include "gui/widgets/settings.hpp"
20 #include "gui/widgets/window.hpp"
21 #include "theme.hpp"
22 
23 namespace gui2
24 {
25 namespace dialogs
26 {
27 
28 /*WIKI
29  * @page = GUIWindowDefinitionWML
30  * @order = 2_theme_list
31  *
32  * == Theme list ==
33  *
34  * Dialog for selecting a GUI theme.
35  *
36  * @begin{table}{dialog_widgets}
37  *
38  * themes & & listbox & m &
39  * Listbox displaying user choices. $
40  *
41  * -name & & styled_widget & m &
42  * Widget which shows a theme item name. $
43  *
44  * -description & & styled_widget & m &
45  * Widget which shows a theme item description. $
46  *
47  * @end{table}
48  */
49 
50 REGISTER_DIALOG(theme_list)
51 
52 theme_list::theme_list(const std::vector<theme_info>& themes, int selection)
53  : index_(selection), themes_(themes)
54 {
55 }
56 
58 {
59  listbox& list = find_widget<listbox>(&window, "themes", false);
60  window.keyboard_capture(&list);
61 
62  for(const auto & t : themes_)
63  {
64  std::map<std::string, string_map> data;
65  string_map column;
66 
67  std::string theme_name = t.name;
68  if(theme_name.empty()) {
69  theme_name = t.id;
70  }
71 
72  column["label"] = theme_name;
73  data.emplace("name", column);
74  column["label"] = t.description;
75  data.emplace("description", column);
76 
77  list.add_row(data);
78  }
79 
80  if(index_ != -1 && static_cast<unsigned>(index_) < list.get_item_count()) {
81  list.select_row(index_);
82  }
83 
84  index_ = -1;
85 }
86 
88 {
89  if(get_retval() != retval::OK) {
90  return;
91  }
92 
93  listbox& list = find_widget<listbox>(&window, "themes", false);
94  index_ = list.get_selected_row();
95 }
96 } // namespace dialogs
97 } // namespace gui2
This file contains the window object, this object is a top level container which has the event manage...
virtual void post_show(window &window) override
Inherited from modal_dialog.
Definition: theme_list.cpp:87
STL namespace.
int get_selected_row() const
Returns the first selected row.
Definition: listbox.cpp:272
std::vector< theme_info > themes_
Definition: theme_list.hpp:50
bool select_row(const unsigned row, const bool select=true)
Selects a row.
Definition: listbox.cpp:250
Generic file dialog.
Definition: field-fwd.hpp:22
The listbox class.
Definition: listbox.hpp:40
This file contains the settings handling of the widget library.
virtual void pre_show(window &window) override
Inherited from modal_dialog.
Definition: theme_list.cpp:57
unsigned get_item_count() const
Returns the number of items in the listbox.
Definition: listbox.cpp:131
Various uncategorised dialogs.
Definitions related to theme-support.
std::map< std::string, t_string > string_map
Definition: widget.hpp:24
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:66
double t
Definition: astarsearch.cpp:64
Dialog was closed with the OK button.
Definition: retval.hpp:34
base class of top level items, the only item which needs to store the final canvases to draw on ...
Definition: window.hpp:63