The Battle for Wesnoth  1.15.2+dev
simple_item_selector.hpp
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 #pragma once
16 
18 
19 #include <vector>
20 
21 namespace gui2
22 {
23 namespace dialogs
24 {
25 
27 {
28 public:
29  typedef std::vector<std::string> list_type;
30 
31  simple_item_selector(const std::string& title,
32  const std::string& message,
33  const list_type& items,
34  bool title_uses_markup = false,
35  bool message_uses_markup = false);
36 
37  /**
38  * Returns the selected item index after displaying.
39  * @return -1 if the dialog was canceled.
40  */
41  int selected_index() const
42  {
43  return index_;
44  }
45 
46  /** Sets the initially selected item index (-1 by default). */
48  {
49  index_ = index;
50  }
51 
52  /** Sets the caption for the OK button. */
53  void set_ok_label(const std::string& s)
54  {
55  ok_label_ = s;
56  }
57  const std::string& ok_label() const
58  {
59  return ok_label_;
60  }
61 
62  /** Sets the caption for the Cancel button. */
63  void set_cancel_label(const std::string& s)
64  {
65  cancel_label_ = s;
66  }
67  const std::string& cancel_label() const
68  {
69  return cancel_label_;
70  }
71 
72  /** Sets whether the Cancel button should be hidden or not. */
73  void set_single_button(bool value)
74  {
75  single_button_ = value;
76  }
77  bool single_button() const
78  {
79  return single_button_;
80  }
81 
82 private:
83  int index_;
84 
86  list_type items_;
87 
88  std::string ok_label_, cancel_label_;
89 
90  /** Inherited from modal_dialog, implemented by REGISTER_DIALOG. */
91  virtual const std::string& window_id() const override;
92 
93  /** Inherited from modal_dialog. */
94  virtual void pre_show(window& window) override;
95 
96  /** Inherited from modal_dialog. */
97  virtual void post_show(window& window) override;
98 };
99 } // namespace dialogs
100 } // namespace gui2
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
Inherited from modal_dialog, implemented by REGISTER_DIALOG.
const std::vector< std::string > items
Generic file dialog.
Definition: field-fwd.hpp:22
Various uncategorised dialogs.
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
Inherited from modal_dialog.
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:71
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:63
virtual void pre_show(window &window) override
Inherited from modal_dialog.
void set_single_button(bool value)
Sets whether the Cancel button should be hidden or not.