The Battle for Wesnoth  1.19.7+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 
26 {
27 public:
28  typedef std::vector<std::string> list_type;
29 
30  simple_item_selector(const std::string& title,
31  const std::string& message,
32  const list_type& items,
33  bool title_uses_markup = false,
34  bool message_uses_markup = false);
35 
36  /**
37  * Returns the selected item index after displaying.
38  * @return -1 if the dialog was canceled.
39  */
40  int selected_index() const
41  {
42  return index_;
43  }
44 
45  /** Sets the initially selected item index (-1 by default). */
47  {
48  index_ = index;
49  }
50 
51  /** Sets the caption for the OK button. */
52  void set_ok_label(const std::string& s)
53  {
54  ok_label_ = s;
55  }
56  const std::string& ok_label() const
57  {
58  return ok_label_;
59  }
60 
61  /** Sets the caption for the Cancel button. */
62  void set_cancel_label(const std::string& s)
63  {
64  cancel_label_ = s;
65  }
66  const std::string& cancel_label() const
67  {
68  return cancel_label_;
69  }
70 
71  /** Sets whether the Cancel button should be hidden or not. */
72  void set_single_button(bool value)
73  {
74  single_button_ = value;
75  }
76  bool single_button() const
77  {
78  return single_button_;
79  }
80 
81 private:
82  int index_;
83 
86 
87  std::string ok_label_, cancel_label_;
88 
89  virtual const std::string& window_id() const override;
90 
91  virtual void pre_show() override;
92 
93  virtual void post_show() override;
94 };
95 } // namespace dialogs
Main class to show messages to the user.
Definition: message.hpp:36
Abstract base class for all modal dialogs.
void set_single_button(bool value)
Sets whether the Cancel button should be hidden or not.
virtual void pre_show() override
Actions to be taken before showing the window.
virtual void post_show() override
Actions to be taken after the window has been shown.
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.
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)
std::size_t index(std::string_view 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