The Battle for Wesnoth  1.17.0-dev
simple_item_selector.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2010 - 2021
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 #pragma once
16 
18 
19 #include <vector>
20 
21 namespace gui2::dialogs
22 {
23 
24 /**
25  * @ingroup GUIWindowDefinitionWML
26  *
27  * A simple one-column listbox with OK and Cancel buttons.
28  * Key |Type |Mandatory|Description
29  * ------------------|--------------|---------|-----------
30  * title | @ref label |yes |Dialog title label.
31  * message | control |yes |Text label displaying a description or instructions.
32  * listbox | @ref listbox |yes |Listbox displaying user choices.
33  * item | control |yes |Widget which shows a listbox item label.
34  * ok | @ref button |yes |OK button.
35  * cancel | @ref button |yes |Cancel button.
36  */
38 {
39 public:
40  typedef std::vector<std::string> list_type;
41 
42  simple_item_selector(const std::string& title,
43  const std::string& message,
44  const list_type& items,
45  bool title_uses_markup = false,
46  bool message_uses_markup = false);
47 
48  /**
49  * Returns the selected item index after displaying.
50  * @return -1 if the dialog was canceled.
51  */
52  int selected_index() const
53  {
54  return index_;
55  }
56 
57  /** Sets the initially selected item index (-1 by default). */
59  {
60  index_ = index;
61  }
62 
63  /** Sets the caption for the OK button. */
64  void set_ok_label(const std::string& s)
65  {
66  ok_label_ = s;
67  }
68  const std::string& ok_label() const
69  {
70  return ok_label_;
71  }
72 
73  /** Sets the caption for the Cancel button. */
74  void set_cancel_label(const std::string& s)
75  {
76  cancel_label_ = s;
77  }
78  const std::string& cancel_label() const
79  {
80  return cancel_label_;
81  }
82 
83  /** Sets whether the Cancel button should be hidden or not. */
84  void set_single_button(bool value)
85  {
86  single_button_ = value;
87  }
88  bool single_button() const
89  {
90  return single_button_;
91  }
92 
93 private:
94  int index_;
95 
97  list_type items_;
98 
99  std::string ok_label_, cancel_label_;
100 
101  virtual const std::string& window_id() const override;
102 
103  virtual void pre_show(window& window) override;
104 
105  virtual void post_show(window& window) override;
106 };
107 } // namespace dialogs
void set_cancel_label(const std::string &s)
Sets the caption for the Cancel button.
simple_item_selector(const std::string &title, const std::string &message, const list_type &items, bool title_uses_markup=false, bool message_uses_markup=false)
Main class to show messages to the user.
Definition: message.hpp:34
const std::string & ok_label() const
const std::string & cancel_label() const
virtual const std::string & window_id() const override
The id of the window to build.
const std::vector< std::string > items
A simple one-column listbox with OK and Cancel buttons.
void set_ok_label(const std::string &s)
Sets the caption for the OK button.
int selected_index() const
Returns the selected item index after displaying.
static map_location::DIRECTION s
virtual void post_show(window &window) override
Actions to be taken after the window has been shown.
std::size_t index(const std::string &str, const std::size_t index)
Codepoint index corresponding to the nth character in a UTF-8 string.
Definition: unicode.cpp:69
void set_selected_index(int index)
Sets the initially selected item index (-1 by default).
Abstract base class for all modal dialogs.
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.
void set_single_button(bool value)
Sets whether the Cancel button should be hidden or not.