The Battle for Wesnoth  1.15.12+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::dialogs
27 {
28 
29 REGISTER_DIALOG(simple_item_selector)
30 
32  const std::string& message,
33  const list_type& items,
34  bool title_uses_markup,
35  bool message_uses_markup)
36  : index_(-1)
37  , single_button_(false)
38  , items_(items)
39  , ok_label_()
40  , cancel_label_()
41 {
42  register_label("title", true, title, title_uses_markup);
43  register_label("message", true, message, message_uses_markup);
44 }
45 
47 {
48  listbox& list = find_widget<listbox>(&window, "listbox", false);
49  window.keyboard_capture(&list);
50 
51  for(const auto & it : items_)
52  {
53  std::map<std::string, string_map> data;
54  string_map column;
55 
56  column["label"] = it;
57  data.emplace("item", column);
58 
59  list.add_row(data);
60  }
61 
62  if(index_ != -1 && static_cast<unsigned>(index_) < list.get_item_count()) {
63  list.select_row(index_);
64  }
65 
66  index_ = -1;
67 
68  button& button_ok = find_widget<button>(&window, "ok", false);
69  button& button_cancel = find_widget<button>(&window, "cancel", false);
70 
71  if(!ok_label_.empty()) {
72  button_ok.set_label(ok_label_);
73  }
74 
75  if(!cancel_label_.empty()) {
76  button_cancel.set_label(cancel_label_);
77  }
78 
79  if(single_button_) {
80  button_cancel.set_visible(gui2::widget::visibility::invisible);
81  }
82 }
83 
85 {
87  index_ = find_widget<listbox>(&window, "listbox", false).get_selected_row();
88  }
89 }
90 
91 } // namespace dialogs
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:251
virtual void set_label(const t_string &label)
The listbox class.
Definition: listbox.hpp:42
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:132
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:67
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: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:64
virtual void pre_show(window &window) override
Actions to be taken before showing the window.