The Battle for Wesnoth  1.15.2+dev
simple_item_selector.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2010 - 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 
15 #define GETTEXT_DOMAIN "wesnoth-lib"
16 
18 
20 #include "gui/widgets/button.hpp"
21 #include "gui/widgets/label.hpp"
22 #include "gui/widgets/listbox.hpp"
23 #include "gui/widgets/settings.hpp"
24 #include "gui/widgets/window.hpp"
25 
26 namespace gui2
27 {
28 namespace dialogs
29 {
30 
31 /*WIKI
32  * @page = GUIWindowDefinitionWML
33  * @order = 2_simple_item_selector
34  *
35  * == Simple item selector ==
36  *
37  * A simple one-column listbox with OK and Cancel buttons.
38  *
39  * @begin{table}{dialog_widgets}
40  *
41  * title & & label & m &
42  * Dialog title label. $
43  *
44  * message & & styled_widget & m &
45  * Text label displaying a description or instructions. $
46  *
47  * listbox & & listbox & m &
48  * Listbox displaying user choices. $
49  *
50  * -item & & styled_widget & m &
51  * Widget which shows a listbox item label. $
52  *
53  * ok & & button & m &
54  * OK button. $
55  *
56  * cancel & & button & m &
57  * Cancel button. $
58  *
59  * @end{table}
60  */
61 
62 REGISTER_DIALOG(simple_item_selector)
63 
65  const std::string& message,
66  const list_type& items,
67  bool title_uses_markup,
68  bool message_uses_markup)
69  : index_(-1)
70  , single_button_(false)
71  , items_(items)
72  , ok_label_()
73  , cancel_label_()
74 {
75  register_label("title", true, title, title_uses_markup);
76  register_label("message", true, message, message_uses_markup);
77 }
78 
80 {
81  listbox& list = find_widget<listbox>(&window, "listbox", false);
82  window.keyboard_capture(&list);
83 
84  for(const auto & it : items_)
85  {
86  std::map<std::string, string_map> data;
87  string_map column;
88 
89  column["label"] = it;
90  data.emplace("item", column);
91 
92  list.add_row(data);
93  }
94 
95  if(index_ != -1 && static_cast<unsigned>(index_) < list.get_item_count()) {
96  list.select_row(index_);
97  }
98 
99  index_ = -1;
100 
101  button& button_ok = find_widget<button>(&window, "ok", false);
102  button& button_cancel = find_widget<button>(&window, "cancel", false);
103 
104  if(!ok_label_.empty()) {
105  button_ok.set_label(ok_label_);
106  }
107 
108  if(!cancel_label_.empty()) {
109  button_cancel.set_label(cancel_label_);
110  }
111 
112  if(single_button_) {
113  button_cancel.set_visible(gui2::widget::visibility::invisible);
114  }
115 }
116 
118 {
119  if(get_retval() == retval::OK || single_button_) {
120  index_ = find_widget<listbox>(&window, "listbox", false).get_selected_row();
121  }
122 }
123 
124 } // namespace dialogs
125 } // namespace gui2
Main class to show messages to the user.
Definition: message.hpp:34
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:250
Generic file dialog.
Definition: field-fwd.hpp:22
virtual void set_label(const t_string &label)
The listbox class.
Definition: listbox.hpp:40
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:131
Various uncategorised dialogs.
The user set the widget invisible, that means:
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
virtual void post_show(window &window) override
Inherited from modal_dialog.
Simple push button.
Definition: button.hpp:35
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:62
virtual void pre_show(window &window) override
Inherited from modal_dialog.