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