The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
simple_item_selector.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2010 - 2016 by Ignacio Riquelme Morelle <[email protected]>
3  Part of the Battle for Wesnoth Project http://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 #ifdef GUI2_EXPERIMENTAL_LISTBOX
23 #include "gui/widgets/list.hpp"
24 #else
25 #include "gui/widgets/listbox.hpp"
26 #endif
27 #include "gui/widgets/settings.hpp"
28 #include "gui/widgets/window.hpp"
29 
30 namespace gui2
31 {
32 
33 /*WIKI
34  * @page = GUIWindowDefinitionWML
35  * @order = 2_simple_item_selector
36  *
37  * == Simple item selector ==
38  *
39  * A simple one-column listbox with OK and Cancel buttons.
40  *
41  * @begin{table}{dialog_widgets}
42  *
43  * title & & label & m &
44  * Dialog title label. $
45  *
46  * message & & control & m &
47  * Text label displaying a description or instructions. $
48  *
49  * listbox & & listbox & m &
50  * Listbox displaying user choices. $
51  *
52  * -item & & control & m &
53  * Widget which shows a listbox item label. $
54  *
55  * ok & & button & m &
56  * OK button. $
57  *
58  * cancel & & button & m &
59  * Cancel button. $
60  *
61  * @end{table}
62  */
63 
64 REGISTER_DIALOG(simple_item_selector)
65 
67  const std::string& message,
68  list_type const& items,
69  bool title_uses_markup,
70  bool message_uses_markup)
71  : index_(-1)
72  , single_button_(false)
73  , items_(items)
74  , ok_label_()
75  , cancel_label_()
76 {
77  register_label("title", true, title, title_uses_markup);
78  register_label("message", true, message, message_uses_markup);
79 }
80 
82 {
83  tlistbox& list = find_widget<tlistbox>(&window, "listbox", false);
84  window.keyboard_capture(&list);
85 
86  for(const auto & it : items_)
87  {
88  std::map<std::string, string_map> data;
90 
91  column["label"] = it;
92  data.insert(std::make_pair("item", column));
93 
94  list.add_row(data);
95  }
96 
97  if(index_ != -1 && static_cast<unsigned>(index_) < list.get_item_count()) {
98  list.select_row(index_);
99  }
100 
101  index_ = -1;
102 
103  tbutton& button_ok = find_widget<tbutton>(&window, "ok", false);
104  tbutton& button_cancel = find_widget<tbutton>(&window, "cancel", false);
105 
106  if(!ok_label_.empty()) {
107  button_ok.set_label(ok_label_);
108  }
109 
110  if(!cancel_label_.empty()) {
111  button_cancel.set_label(cancel_label_);
112  }
113 
114  if(single_button_) {
115  button_cancel.set_visible(gui2::twidget::tvisible::invisible);
116  }
117 }
118 
120 {
121  if(get_retval() != twindow::OK) {
122  return;
123  }
124 
125  tlistbox& list = find_widget<tlistbox>(&window, "listbox", false);
126  index_ = list.get_selected_row();
127 }
128 }
int get_retval() const
Definition: dialog.hpp:161
This file contains the window object, this object is a top level container which has the event manage...
bool select_row(const unsigned row, const bool select=true)
Selectes a row.
Definition: listbox.cpp:228
REGISTER_DIALOG(label_settings)
virtual void set_label(const t_string &label)
Definition: control.cpp:330
STL namespace.
const std::vector< std::string > items
GLint GLenum GLsizei GLint GLsizei const GLvoid * data
Definition: glew.h:1347
base class of top level items, the only item which needs to store the final canvases to draw on ...
Definition: window.hpp:62
A class inherited from ttext_box that displays its input as stars.
Definition: field-fwd.hpp:23
Simple push button.
Definition: button.hpp:32
The user set the widget invisible, that means:
Definition: widget.hpp:103
Dialog is closed with ok button.
Definition: window.hpp:125
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:138
void 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:74
std::map< std::string, t_string > string_map
Definition: generator.hpp:23
GLenum GLenum GLvoid GLvoid * column
Definition: glew.h:3805
void post_show(twindow &window)
Inherited from tdialog.
The listbox class.
Definition: listbox.hpp:39
std::vector< expression_ptr > items_
Definition: formula.cpp:119
void pre_show(twindow &window)
Inherited from tdialog.
GLsizei GLenum GLuint GLuint GLsizei char * message
Definition: glew.h:2499
std::vector< std::string > list_type
int get_selected_row() const
Returns the first selected row.
Definition: listbox.cpp:237