The Battle for Wesnoth  1.17.0-dev
simple_item_selector.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2010 - 2021
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 
16 #define GETTEXT_DOMAIN "wesnoth-lib"
17 
19 
21 #include "gui/widgets/button.hpp"
22 #include "gui/widgets/label.hpp"
23 #include "gui/widgets/listbox.hpp"
24 #include "gui/widgets/settings.hpp"
25 #include "gui/widgets/window.hpp"
26 
27 namespace gui2::dialogs
28 {
29 
30 REGISTER_DIALOG(simple_item_selector)
31 
33  const std::string& message,
34  const list_type& items,
35  bool title_uses_markup,
36  bool message_uses_markup)
37  : index_(-1)
38  , single_button_(false)
39  , items_(items)
40  , ok_label_()
41  , cancel_label_()
42 {
43  register_label("title", true, title, title_uses_markup);
44  register_label("message", true, message, message_uses_markup);
45 }
46 
48 {
49  listbox& list = find_widget<listbox>(&window, "listbox", false);
50  window.keyboard_capture(&list);
51 
52  for(const auto & it : items_)
53  {
54  std::map<std::string, string_map> data;
55  string_map column;
56 
57  column["label"] = it;
58  data.emplace("item", column);
59 
60  list.add_row(data);
61  }
62 
63  if(index_ != -1 && static_cast<unsigned>(index_) < list.get_item_count()) {
64  list.select_row(index_);
65  }
66 
67  index_ = -1;
68 
69  button& button_ok = find_widget<button>(&window, "ok", false);
70  button& button_cancel = find_widget<button>(&window, "cancel", false);
71 
72  if(!ok_label_.empty()) {
73  button_ok.set_label(ok_label_);
74  }
75 
76  if(!cancel_label_.empty()) {
77  button_cancel.set_label(cancel_label_);
78  }
79 
80  if(single_button_) {
81  button_cancel.set_visible(gui2::widget::visibility::invisible);
82  }
83 }
84 
86 {
88  index_ = find_widget<listbox>(&window, "listbox", false).get_selected_row();
89  }
90 }
91 
92 } // namespace dialogs
Main class to show messages to the user.
Definition: message.hpp:35
This file contains the window object, this object is a top level container which has the event manage...
STL namespace.
const std::vector< std::string > items
bool select_row(const unsigned row, const bool select=true)
Selects a row.
Definition: listbox.cpp:252
virtual void set_label(const t_string &label)
The listbox class.
Definition: listbox.hpp:43
A simple one-column listbox with OK and Cancel buttons.
This file contains the settings handling of the widget library.
unsigned get_item_count() const
Returns the number of items in the listbox.
Definition: listbox.cpp:133
The user set the widget invisible, that means:
std::map< std::string, t_string > string_map
Definition: widget.hpp:26
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
virtual void post_show(window &window) override
Actions to be taken after the window has been shown.
int get_retval() const
Returns the cached window exit code.
Simple push button.
Definition: button.hpp:36
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:65
virtual void pre_show(window &window) override
Actions to be taken before showing the window.