The Battle for Wesnoth  1.17.12+dev
theme_list.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014 - 2022
3  by Iris Morelle <shadowm2006@gmail.com>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
17 
19 #include "gui/widgets/listbox.hpp"
20 #include "gui/widgets/settings.hpp"
21 #include "gui/widgets/window.hpp"
22 #include "theme.hpp"
23 
24 namespace gui2::dialogs
25 {
26 
27 REGISTER_DIALOG(theme_list)
28 
29 theme_list::theme_list(const std::vector<theme_info>& themes, int selection)
30  : modal_dialog(window_id())
31  , index_(selection)
32  , themes_(themes)
33 {
34 }
35 
37 {
38  listbox& list = find_widget<listbox>(&window, "themes", false);
39  window.keyboard_capture(&list);
40 
41  for(const auto & t : themes_)
42  {
44  widget_item column;
45 
46  std::string theme_name = t.name;
47  if(theme_name.empty()) {
48  theme_name = t.id;
49  }
50 
51  column["label"] = theme_name;
52  data.emplace("name", column);
53  column["label"] = t.description;
54  data.emplace("description", column);
55 
56  list.add_row(data);
57  }
58 
59  if(index_ != -1 && static_cast<unsigned>(index_) < list.get_item_count()) {
60  list.select_row(index_);
61  }
62 
63  index_ = -1;
64 }
65 
67 {
68  if(get_retval() != retval::OK) {
69  return;
70  }
71 
72  listbox& list = find_widget<listbox>(&window, "themes", false);
73  index_ = list.get_selected_row();
74 }
75 } // namespace dialogs
window(const builder_window::window_resolution &definition)
< Needs to be initialized in show.
Definition: window.cpp:263
Dialog for selecting a GUI theme.
Definition: theme_list.hpp:35
This file contains the window object, this object is a top level container which has the event manage...
std::string_view data
Definition: picture.cpp:206
virtual void post_show(window &window) override
Actions to be taken after the window has been shown.
Definition: theme_list.cpp:66
grid & add_row(const widget_item &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:61
STL namespace.
int get_selected_row() const
Returns the first selected row.
Definition: listbox.cpp:270
std::vector< theme_info > themes_
Definition: theme_list.hpp:59
bool select_row(const unsigned row, const bool select=true)
Selects a row.
Definition: listbox.cpp:245
The listbox class.
Definition: listbox.hpp:45
std::map< std::string, t_string > widget_item
Definition: widget.hpp:32
This file contains the settings handling of the widget library.
virtual void pre_show(window &window) override
Actions to be taken before showing the window.
Definition: theme_list.cpp:36
unsigned get_item_count() const
Returns the number of items in the listbox.
Definition: listbox.cpp:126
Definitions related to theme-support.
int get_retval() const
Returns the cached window exit code.
double t
Definition: astarsearch.cpp:65
Abstract base class for all modal dialogs.
std::map< std::string, widget_item > widget_data
Definition: widget.hpp:35
Dialog was closed with the OK button.
Definition: retval.hpp:35
base class of top level items, the only item which needs to store the final canvases to draw on...
Definition: window.hpp:66