The Battle for Wesnoth  1.19.0-dev
theme_list.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014 - 2024
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/window.hpp"
21 #include "theme.hpp"
22 
23 namespace gui2::dialogs
24 {
25 
26 REGISTER_DIALOG(theme_list)
27 
28 theme_list::theme_list(const std::vector<theme_info>& themes, int selection)
29  : modal_dialog(window_id())
30  , index_(selection)
31  , themes_(themes)
32 {
33 }
34 
36 {
37  listbox& list = find_widget<listbox>(&window, "themes", false);
38  window.keyboard_capture(&list);
39 
40  for(const auto & t : themes_)
41  {
43  widget_item column;
44 
45  std::string theme_name = t.name;
46  if(theme_name.empty()) {
47  theme_name = t.id;
48  }
49 
50  column["label"] = theme_name;
51  data.emplace("name", column);
52  column["label"] = t.description;
53  data.emplace("description", column);
54 
55  list.add_row(data);
56  }
57 
58  if(index_ != -1 && static_cast<unsigned>(index_) < list.get_item_count()) {
59  list.select_row(index_);
60  }
61 
62  index_ = -1;
63 }
64 
66 {
67  if(get_retval() != retval::OK) {
68  return;
69  }
70 
71  listbox& list = find_widget<listbox>(&window, "themes", false);
72  index_ = list.get_selected_row();
73 }
74 } // namespace dialogs
double t
Definition: astarsearch.cpp:63
Abstract base class for all modal dialogs.
int get_retval() const
Returns the cached window exit code.
Dialog for selecting a GUI theme.
Definition: theme_list.hpp:36
virtual void post_show(window &window) override
Actions to be taken after the window has been shown.
Definition: theme_list.cpp:65
std::vector< theme_info > themes_
Definition: theme_list.hpp:59
virtual void pre_show(window &window) override
Actions to be taken before showing the window.
Definition: theme_list.cpp:35
The listbox class.
Definition: listbox.hpp:43
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:59
bool select_row(const unsigned row, const bool select=true)
Selects a row.
Definition: listbox.cpp:243
int get_selected_row() const
Returns the first selected row.
Definition: listbox.cpp:268
unsigned get_item_count() const
Returns the number of items in the listbox.
Definition: listbox.cpp:124
base class of top level items, the only item which needs to store the final canvases to draw on.
Definition: window.hpp:63
void keyboard_capture(widget *widget)
Definition: window.cpp:1221
This file contains the window object, this object is a top level container which has the event manage...
REGISTER_DIALOG(editor_edit_unit)
std::map< std::string, widget_item > widget_data
Definition: widget.hpp:34
std::map< std::string, t_string > widget_item
Definition: widget.hpp:31
@ OK
Dialog was closed with the OK button.
Definition: retval.hpp:35
std::string_view data
Definition: picture.cpp:194
Definitions related to theme-support.