The Battle for Wesnoth  1.19.5+dev
combobox.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2024
3  by Subhraman Sarkar (babaissarkar) <suvrax@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 
20 
21 namespace gui2
22 {
23 namespace implementation
24 {
25 struct builder_combobox;
26 }
27 
28 // ------------ WIDGET -----------{
29 
30 /**
31  * Class for a combobox.
32  * A widget that allows the user to input text or to select predefined options from a list
33  * accessed by clicking the dropdown arrow.
34  */
35 
36 class combobox : public text_box_base
37 {
39 
40 public:
41  explicit combobox(const implementation::builder_styled_widget& builder);
42 
43  void set_max_input_length(const std::size_t length)
44  {
45  max_input_length_ = length;
46  }
47 
48  std::size_t get_max_input_length() const
49  {
50  return max_input_length_;
51  }
52 
53  void set_hint_text(const std::string& text)
54  {
55  hint_text_ = text;
56  update_canvas();
57  }
58 
59  std::string get_hint_text() const
60  {
61  return hint_text_;
62  }
63 
64  void set_hint_image(const std::string& image)
65  {
67  update_canvas();
68  }
69 
70  std::string get_hint_image() const
71  {
72  return hint_image_;
73  }
74 
75 
76  void set_hint_data(const std::string& text, const std::string& image)
77  {
78  hint_text_ = text;
80 
81  update_canvas();
82  }
83 
84  void clear()
85  {
86  set_value("");
87  }
88 
89  int get_item_count() const
90  {
91  return values_.size();
92  }
93 
94  void set_values(const std::vector<::config>& values, unsigned selected = 0);
95  void set_selected(unsigned selected, bool fire_event = true);
96  unsigned get_selected() const { return selected_; }
97 
98 protected:
99  /* **** ***** ***** ***** layout functions ***** ***** ***** **** */
100 
101  virtual void place(const point& origin, const point& size) override;
102 
103  /* **** ***** ***** ***** Inherited ***** ***** ***** **** */
104 
105  virtual void update_canvas() override;
106 
107  void goto_end_of_line(const bool select = false) override
108  {
109  goto_end_of_data(select);
110  }
111 
112  void goto_start_of_line(const bool select = false) override
113  {
114  goto_start_of_data(select);
115  }
116 
117  void delete_char(const bool before_cursor) override;
118 
119  void delete_selection() override;
120 
121  void handle_mouse_selection(point mouse, const bool start_selection);
122 
123 private:
124 
125  /** The maximum length of the text input. */
126  std::size_t max_input_length_;
127 
128  /** Size of the dropdown icon
129  * TODO : Should be dynamically loaded from image
130  */
131  unsigned const ICON_SIZE = 25;
132 
133  /**
134  * The x offset in the widget where the text starts.
135  *
136  * This value is needed to translate a location in the widget to a location
137  * in the text.
138  */
139  unsigned text_x_offset_;
140 
141  /**
142  * The y offset in the widget where the text starts.
143  *
144  * Needed to determine whether a click is on the text.
145  */
146  unsigned text_y_offset_;
147 
148  /**
149  * The height of the text itself.
150  *
151  * Needed to determine whether a click is on the text.
152  */
153  unsigned text_height_;
154 
155  /** Updates text_x_offset_ and text_y_offset_. */
156  void update_offsets();
157 
158  /** Is the mouse in dragging mode, this affects selection in mouse move */
159  bool dragging_;
160 
161  /** Helper text to display (such as "Search") if the combo box is empty. */
162  std::string hint_text_;
163 
164  /** Image (such as a magnifying glass) that accompanies the help text. */
165  std::string hint_image_;
166 
167  std::vector<::config> values_;
168 
169  unsigned selected_;
170 
171  /**
172  * Inherited from text_box_base.
173  *
174  * Unmodified Handled.
175  * Control Ignored.
176  * Shift Ignored.
177  * Alt Ignored.
178  */
179  void handle_key_up_arrow(SDL_Keymod /*modifier*/, bool& handled) override;
180 
181  /**
182  * Inherited from text_box_base.
183  *
184  * Unmodified Handled.
185  * Control Ignored.
186  * Shift Ignored.
187  * Alt Ignored.
188  */
189  void handle_key_down_arrow(SDL_Keymod /*modifier*/, bool& handled) override;
190 
191 
192  /** Inherited from text_box_base. */
193  void handle_key_clear_line(SDL_Keymod modifier, bool& handled) override;
194 
195  /** Update the mouse cursor based on whether it is over button area or text area */
196  void update_mouse_cursor();
197 
198 public:
199  /** Static type getter that does not rely on the widget being constructed. */
200  static const std::string& type();
201 
202 private:
203  /** Inherited from styled_widget, implemented by REGISTER_WIDGET. */
204  virtual const std::string& get_control_type() const override;
205 
206  /* **** ***** ***** signal handlers ***** ****** **** */
207 
208  void signal_handler_mouse_enter(const event::ui_event /*event*/, bool& /*handled*/);
210  bool& handled,
211  const point& coordinate);
212 
214  bool& handled);
215 
217  bool& handled);
218 
220  bool& handled);
221 };
222 
223 // }---------- DEFINITION ---------{
224 
226 {
227  explicit combobox_definition(const config& cfg);
228 
230  {
231  explicit resolution(const config& cfg);
232 
235  };
236 };
237 
238 // }---------- BUILDER -----------{
239 
240 namespace implementation
241 {
242 
244 {
245 public:
246  explicit builder_combobox(const config& cfg);
247 
249 
250  virtual std::unique_ptr<widget> build() const override;
251 
252  std::size_t max_input_length;
253 
255  std::string hint_image;
256 
257 private:
258  std::vector<::config> options_;
259 };
260 
261 } // namespace implementation
262 
263 // }------------ END --------------
264 
265 } // namespace gui2
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:172
Class for a combobox.
Definition: combobox.hpp:37
void set_hint_data(const std::string &text, const std::string &image)
Definition: combobox.hpp:76
void signal_handler_mouse_enter(const event::ui_event, bool &)
Definition: combobox.cpp:316
void signal_handler_left_button_up(const event::ui_event event, bool &handled)
Definition: combobox.cpp:375
std::size_t max_input_length_
The maximum length of the text input.
Definition: combobox.hpp:126
void update_mouse_cursor()
Update the mouse cursor based on whether it is over button area or text area.
Definition: combobox.cpp:304
unsigned selected_
Definition: combobox.hpp:169
void delete_selection() override
Deletes the current selection.
Definition: combobox.cpp:179
void goto_start_of_line(const bool select=false) override
Moves the cursor to the beginning of the line.
Definition: combobox.hpp:112
void update_offsets()
Updates text_x_offset_ and text_y_offset_.
Definition: combobox.cpp:223
void goto_end_of_line(const bool select=false) override
Moves the cursor to the end of the line.
Definition: combobox.hpp:107
std::string get_hint_text() const
Definition: combobox.hpp:59
bool dragging_
Is the mouse in dragging mode, this affects selection in mouse move.
Definition: combobox.hpp:159
void set_max_input_length(const std::size_t length)
Definition: combobox.hpp:43
static const std::string & type()
Static type getter that does not rely on the widget being constructed.
void signal_handler_mouse_motion(const event::ui_event event, bool &handled, const point &coordinate)
Definition: combobox.cpp:322
void handle_mouse_selection(point mouse, const bool start_selection)
Definition: combobox.cpp:199
std::string get_hint_image() const
Definition: combobox.hpp:70
void clear()
Definition: combobox.hpp:84
virtual void update_canvas() override
Updates the canvas(ses).
Definition: combobox.cpp:89
unsigned const ICON_SIZE
Size of the dropdown icon TODO : Should be dynamically loaded from image.
Definition: combobox.hpp:131
void handle_key_clear_line(SDL_Keymod modifier, bool &handled) override
Inherited from text_box_base.
Definition: combobox.cpp:248
std::size_t get_max_input_length() const
Definition: combobox.hpp:48
std::string hint_image_
Image (such as a magnifying glass) that accompanies the help text.
Definition: combobox.hpp:165
void handle_key_down_arrow(SDL_Keymod, bool &handled) override
Inherited from text_box_base.
Definition: combobox.cpp:263
void set_hint_image(const std::string &image)
Definition: combobox.hpp:64
void set_values(const std::vector<::config > &values, unsigned selected=0)
Definition: combobox.cpp:272
unsigned text_y_offset_
The y offset in the widget where the text starts.
Definition: combobox.hpp:146
void set_hint_text(const std::string &text)
Definition: combobox.hpp:53
void signal_handler_left_button_down(const event::ui_event event, bool &handled)
Definition: combobox.cpp:337
void delete_char(const bool before_cursor) override
Deletes the character.
Definition: combobox.cpp:168
std::vector<::config > values_
Definition: combobox.hpp:167
int get_item_count() const
Definition: combobox.hpp:89
void signal_handler_left_button_double_click(const event::ui_event event, bool &handled)
Definition: combobox.cpp:385
void set_selected(unsigned selected, bool fire_event=true)
Definition: combobox.cpp:287
unsigned text_x_offset_
The x offset in the widget where the text starts.
Definition: combobox.hpp:139
void handle_key_up_arrow(SDL_Keymod, bool &handled) override
Inherited from text_box_base.
Definition: combobox.cpp:254
std::string hint_text_
Helper text to display (such as "Search") if the combo box is empty.
Definition: combobox.hpp:162
unsigned text_height_
The height of the text itself.
Definition: combobox.hpp:153
unsigned get_selected() const
Definition: combobox.hpp:96
virtual const std::string & get_control_type() const override
Inherited from styled_widget, implemented by REGISTER_WIDGET.
virtual void place(const point &origin, const point &size) override
See widget::place.
Definition: combobox.cpp:76
combobox(const implementation::builder_styled_widget &builder)
Definition: combobox.cpp:42
Abstract base class for text items.
const std::string & text() const
virtual void set_value(const std::string &text)
The set_value is virtual for the password_box class.
virtual void goto_end_of_data(const bool select=false)
Moves the cursor to the end of all text.
virtual void goto_start_of_data(const bool select=false)
Moves the cursor to the beginning of the data.
std::string selected
bool fire_event(const ui_event event, const std::vector< std::pair< widget *, ui_event >> &event_chain, widget *dispatcher, widget *w, F &&... params)
Helper function for fire_event.
ui_event
The event sent to the dispatcher.
Definition: handler.hpp:115
Generic file dialog.
Functions to load and save images from/to disk.
Contains the implementation details for lexical_cast and shouldn't be used directly.
map_location coordinate
Contains an x and y coordinate used for starting positions in maps.
std::size_t size(const std::string &str)
Length in characters of a UTF-8 string.
Definition: unicode.cpp:85
typed_formula< unsigned > text_y_offset
Definition: combobox.hpp:234
typed_formula< unsigned > text_x_offset
Definition: combobox.hpp:233
combobox_definition(const config &cfg)
Definition: combobox.cpp:396
std::vector<::config > options_
Definition: combobox.hpp:258
virtual std::unique_ptr< widget > build() const override
Definition: combobox.cpp:433
virtual std::unique_ptr< widget > build() const=0
Holds a 2D point.
Definition: point.hpp:25