The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
generator.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2016 by Mark de Wever <[email protected]>
3  Part of the Battle for Wesnoth Project http://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 #ifndef GUI_WIDGETS_GENERATOR_HPP_INCLUDED
16 #define GUI_WIDGETS_GENERATOR_HPP_INCLUDED
17 
18 #include "widget.hpp"
19 #include "tstring.hpp"
20 
21 #include <boost/intrusive_ptr.hpp>
22 
23 typedef std::map<std::string, t_string> string_map;
24 
25 namespace gui2
26 {
27 
28 struct tbuilder_grid;
30 
31 class tgrid;
32 
33 /**
34  * Abstract base class for the generator.
35  *
36  * A generator is a class which holds multiple grids and controls their
37  * placement on the screen. The final class is policy based, more info about
38  * the possible policies is documented in the build() function. This function
39  * is the factory to generate the classes as well.
40  */
41 class tgenerator_ : public twidget
42 {
43  friend class tdebug_layout_graph;
44 
45 public:
46  virtual ~tgenerator_()
47  {
48  }
49 
50  /** Determines how the items are placed. */
51  enum tplacement {
56  };
57 
58  /**
59  * Create a new generator.
60  *
61  * @param has_minimum Does one item need to be selected.
62  * @param has_maximum Is one the maximum number of items that can
63  * be selected?
64  * @param placement The placement of the grids, see tplacement
65  * for more info.
66  * @param select If a grid is selected, what should happen?
67  * If true the grid is selected, if false the
68  * grid is shown.
69  *
70  * @returns A pointer to a new object. The caller gets
71  * ownership of the new object.
72  */
73  static tgenerator_* build(const bool has_minimum,
74  const bool has_maximum,
75  const tplacement placement,
76  const bool select);
77 
78  /**
79  * Deletes an item.
80  */
81  virtual void delete_item(const unsigned index) = 0;
82 
83  /** Deletes all items. */
84  virtual void clear() = 0;
85 
86  /**
87  * (De)selects an item.
88  *
89  * @param index The item to (de)select.
90  * @param select If true selects, if false deselects.
91  */
92  virtual void select_item(const unsigned index, const bool select) = 0;
93 
94  /**
95  * Toggles the selection state of an item.
96  *
97  * @param index The item to toggle.
98  */
99  void toggle_item(const unsigned index)
100  {
101  select_item(index, !is_selected(index));
102  }
103 
104  /** Returns whether the item is selected. */
105  virtual bool is_selected(const unsigned index) const = 0;
106 
107  /**
108  * Shows or hides an item.
109  *
110  * The caller is responsible for reformatting the grid.
111  *
112  * @param index The item to show or hide.
113  * @param show If true shows the item, else hides it.
114  */
115  virtual void set_item_shown(const unsigned index, const bool show) = 0;
116 
117  /** Returns whether the item is shown. */
118  virtual bool get_item_shown(const unsigned index) const = 0;
119 
120  /** Returns the number of items. */
121  virtual unsigned get_item_count() const = 0;
122 
123  /** Returns the number of selected items. */
124  virtual unsigned get_selected_item_count() const = 0;
125 
126  /**
127  * Returns the selected item.
128  *
129  * If a list has multiple selected items it looks whether it knows the last
130  * item actually selected, if that item is selected that one is chosen.
131  * Else is goes through all selected items and returns the first one
132  * selected.
133  *
134  * @note tstacked_widget depends on that behavior it always has all items
135  * selected and thus shown and by default the last selected item (the top
136  * one) is active.
137  *
138  * @returns The selected item, -1 if none selected.
139  */
140  virtual int get_selected_item() const = 0;
141 
142  /** Gets the grid of an item. */
143  virtual tgrid& item(const unsigned index) = 0;
144 
145  /** Gets the grid of an item. */
146  virtual const tgrid& item(const unsigned index) const = 0;
147 
148  /***** ***** ***** ***** Create items ***** ***** ***** *****/
149 
150  /**
151  * Creates a new item.
152  *
153  * The item_data is used for the first widget found, this normally should
154  * be used when there's one widget in an item.
155  *
156  * @param index The item before which to add the new item,
157  * 0 == begin, -1 == end.
158  * @param list_builder A grid builder that's will build the
159  * contents of the new item.
160  * @param item_data The data to initialize the parameters of
161  * the new item.
162  * @param callback The callback function to call when an item
163  * in the grid is (de)selected.
164  *
165  * @returns A reference to the newly created grid.
166  */
167  virtual tgrid& create_item(const int index,
168  tbuilder_grid_const_ptr list_builder,
169  const string_map& item_data,
170  const std::function<void(twidget&)>& callback)
171  = 0;
172 
173  /**
174  * Creates a new item.
175  *
176  * The item_data is used by id, and is meant to set multiple widgets in
177  * an item.
178  *
179  * @param index The item before which to add the new item,
180  * 0 == begin, -1 == end.
181  * @param list_builder A grid builder that's will build the
182  * contents of the new item.
183  * @param data The data to initialize the parameters of
184  * the new item.
185  * @param callback The callback function to call when an item
186  * in the grid is (de)selected.
187  *
188  * @returns A reference to the newly created grid.
189  */
190  virtual tgrid&
191  create_item(const int index,
192  tbuilder_grid_const_ptr list_builder,
193  const std::map<std::string /* widget id */, string_map>& data,
194  const std::function<void(twidget&)>& callback) = 0;
195 
196  /**
197  * Creates one or more new item(s).
198  *
199  * For every item in item_data a new item is generated. This version
200  * expects one widget per item.
201  *
202  * @param index The item before which to add the new item,
203  * 0 == begin, -1 == end.
204  * @param list_builder A grid builder that's will build the
205  * contents of the new item.
206  * @param data The data to initialize the parameters of
207  * the new item.
208  * @param callback The callback function to call when an item
209  * in the grid is (de)selected.
210  */
211  virtual void create_items(const int index,
212  tbuilder_grid_const_ptr list_builder,
213  const std::vector<string_map>& data,
214  const std::function<void(twidget&)>& callback)
215  = 0;
216 
217  /**
218  * Creates one or more new item(s).
219  *
220  * For every item in item_data a new item is generated. This version
221  * expects multiple widgets per item.
222  *
223  * @param index The item before which to add the new item,
224  * 0 == begin, -1 == end.
225  * @param list_builder A grid builder that's will build the
226  * contents of the new item.
227  * @param data The data to initialize the parameters of
228  * the new item.
229  * @param callback The callback function to call when an item
230  * in the grid is (de)selected.
231  */
232  virtual void create_items(
233  const int index,
234  tbuilder_grid_const_ptr list_builder,
235  const std::vector<std::map<std::string /*widget id*/, string_map> >&
236  data,
237  const std::function<void(twidget&)>& callback) = 0;
238 
239  typedef std::function<bool (unsigned, unsigned)> torder_func;
240  virtual void set_order(const torder_func& order) = 0;
241 
242  /***** ***** ***** ***** Inherited ***** ***** ***** *****/
243 
244  /*
245  * These functions must be defined in our child classes so make sure they
246  * become pure virtuals.
247  */
248 
249  /** See @ref twidget::layout_initialise. */
250  virtual void layout_initialise(const bool full_initialisation) override = 0;
251 
252  /** See @ref twidget::request_reduce_width. */
253  virtual void request_reduce_width(const unsigned maximum_width) override
254  = 0;
255 
256  /** See @ref twidget::request_reduce_height. */
257  virtual void request_reduce_height(const unsigned maximum_height) override
258  = 0;
259 
260  /** See @ref twidget::calculate_best_size. */
261  virtual tpoint calculate_best_size() const override = 0;
262 
263  /** See @ref twidget::place. */
264  virtual void place(const tpoint& origin, const tpoint& size) override = 0;
265 
266  /** See @ref twidget::set_origin. */
267  virtual void set_origin(const tpoint& origin) override = 0;
268 
269  /** See @ref twidget::set_visible_rectangle. */
270  virtual void set_visible_rectangle(const SDL_Rect& rectangle) override = 0;
271 
272  /** See @ref twidget::impl_draw_children. */
273  virtual void impl_draw_children(surface& frame_buffer,
274  int x_offset,
275  int y_offset) override = 0;
276 
277 protected:
278  /** See @ref twidget::child_populate_dirty_list. */
279  virtual void
281  const std::vector<twidget*>& call_stack) override
282  = 0;
283 
284 public:
285  /** See @ref twidget::find_at. */
286  virtual twidget* find_at(const tpoint& coordinate,
287  const bool must_be_active) override = 0;
288 
289  /** See @ref twidget::find_at. */
290  virtual const twidget* find_at(const tpoint& coordinate,
291  const bool must_be_active) const override
292  = 0;
293 
294  /***** ***** ***** ***** keyboard functions ***** ***** ***** *****/
295 
296  /**
297  * Up arrow key pressed.
298  *
299  * @param modifier The SDL keyboard modifier when the key was
300  * pressed.
301  * @param handled If the function handles the key it should
302  * set handled to true else do not modify it.
303  * This is used in the keyboard event
304  * changing.
305  */
306  virtual void handle_key_up_arrow(SDLMod modifier, bool& handled) = 0;
307 
308  /**
309  * Down arrow key pressed.
310  *
311  * @param modifier The SDL keyboard modifier when the key was
312  * pressed.
313  * @param handled If the function handles the key it should
314  * set handled to true else do not modify it.
315  * This is used in the keyboard event
316  * changing.
317  */
318  virtual void handle_key_down_arrow(SDLMod modifier, bool& handled) = 0;
319 
320  /**
321  * Left arrow key pressed.
322  *
323  * @param modifier The SDL keyboard modifier when the key was
324  * pressed.
325  * @param handled If the function handles the key it should
326  * set handled to true else do not modify it.
327  * This is used in the keyboard event
328  * changing.
329  */
330  virtual void handle_key_left_arrow(SDLMod modifier, bool& handled) = 0;
331 
332  /**
333  * Right arrow key pressed.
334  *
335  * @param modifier The SDL keyboard modifier when the key was
336  * pressed.
337  * @param handled If the function handles the key it should
338  * set handled to true else do not modify it.
339  * This is used in the keyboard event
340  * changing.
341  */
342  virtual void handle_key_right_arrow(SDLMod modifier, bool& handled) = 0;
343 
344 protected:
345  /**
346  * Selects a not selected item.
347  *
348  * @param index The index of a not selected item.
349  */
350  virtual void do_select_item(const unsigned index) = 0;
351 
352  /**
353  * Deselects a selected item.
354  *
355  * @param index The index of a selected item.
356  */
357  virtual void do_deselect_item(const unsigned index) = 0;
358 
359  /** Gets the grid of an item. */
360  virtual tgrid& item_ordered(const unsigned index) = 0;
361 
362  /** Gets the grid of an item. */
363  virtual const tgrid& item_ordered(const unsigned index) const = 0;
364 
365  virtual unsigned get_ordered_index(unsigned index) const = 0;
366  virtual unsigned get_item_at_ordered(unsigned index_ordered) const = 0;
367 };
368 
369 } // namespace gui2
370 
371 #endif
Abstract base class for the generator.
Definition: generator.hpp:41
virtual void select_item(const unsigned index, const bool select)=0
(De)selects an item.
#define SDLMod
Definition: compat.hpp:30
GLuint GLdouble GLdouble GLint GLint order
Definition: glew.h:2972
boost::intrusive_ptr< const tbuilder_grid > tbuilder_grid_const_ptr
virtual void place(const tpoint &origin, const tpoint &size) override=0
See twidget::place.
virtual unsigned get_selected_item_count() const =0
Returns the number of selected items.
virtual unsigned get_item_at_ordered(unsigned index_ordered) const =0
virtual void clear()=0
Deletes all items.
Base container class.
Definition: grid.hpp:29
virtual int get_selected_item() const =0
Returns the selected item.
virtual void handle_key_right_arrow(SDLMod modifier, bool &handled)=0
Right arrow key pressed.
virtual tgrid & item(const unsigned index)=0
Gets the grid of an item.
tplacement
Determines how the items are placed.
Definition: generator.hpp:51
GLint GLenum GLsizei GLint GLsizei const GLvoid * data
Definition: glew.h:1347
virtual void impl_draw_children(surface &frame_buffer, int x_offset, int y_offset) override=0
See twidget::impl_draw_children.
std::function< bool(unsigned, unsigned)> torder_func
Definition: generator.hpp:239
base class of top level items, the only item which needs to store the final canvases to draw on ...
Definition: window.hpp:62
A class inherited from ttext_box that displays its input as stars.
Definition: field-fwd.hpp:23
virtual void handle_key_down_arrow(SDLMod modifier, bool &handled)=0
Down arrow key pressed.
virtual unsigned get_item_count() const =0
Returns the number of items.
virtual bool is_selected(const unsigned index) const =0
Returns whether the item is selected.
virtual void layout_initialise(const bool full_initialisation) override=0
See twidget::layout_initialise.
virtual bool get_item_shown(const unsigned index) const =0
Returns whether the item is shown.
virtual tgrid & create_item(const int index, tbuilder_grid_const_ptr list_builder, const string_map &item_data, const std::function< void(twidget &)> &callback)=0
Creates a new item.
virtual ~tgenerator_()
Definition: generator.hpp:46
virtual void handle_key_up_arrow(SDLMod modifier, bool &handled)=0
Up arrow key pressed.
virtual void set_item_shown(const unsigned index, const bool show)=0
Shows or hides an item.
virtual void set_origin(const tpoint &origin) override=0
See twidget::set_origin.
std::map< std::string, t_string > string_map
Definition: generator.hpp:23
void show(CVideo &video, const std::string &window_id, const t_string &message, const tpoint &mouse)
Shows a tip.
Definition: tip.cpp:133
virtual void set_order(const torder_func &order)=0
virtual void request_reduce_width(const unsigned maximum_width) override=0
See twidget::request_reduce_width.
virtual void request_reduce_height(const unsigned maximum_height) override=0
See twidget::request_reduce_height.
GLuint index
Definition: glew.h:1782
virtual unsigned get_ordered_index(unsigned index) const =0
virtual tpoint calculate_best_size() const override=0
See twidget::calculate_best_size.
Holds a 2D point.
Definition: point.hpp:24
static tgenerator_ * build(const bool has_minimum, const bool has_maximum, const tplacement placement, const bool select)
Create a new generator.
Definition: generator.cpp:802
void toggle_item(const unsigned index)
Toggles the selection state of an item.
Definition: generator.hpp:99
virtual void do_select_item(const unsigned index)=0
Selects a not selected item.
virtual void create_items(const int index, tbuilder_grid_const_ptr list_builder, const std::vector< string_map > &data, const std::function< void(twidget &)> &callback)=0
Creates one or more new item(s).
GLsizeiptr size
Definition: glew.h:1649
virtual void handle_key_left_arrow(SDLMod modifier, bool &handled)=0
Left arrow key pressed.
friend class tdebug_layout_graph
Definition: generator.hpp:43
virtual void child_populate_dirty_list(twindow &caller, const std::vector< twidget * > &call_stack) override=0
See twidget::child_populate_dirty_list.
Base class for all widgets.
Definition: widget.hpp:49
virtual void set_visible_rectangle(const SDL_Rect &rectangle) override=0
See twidget::set_visible_rectangle.
virtual void do_deselect_item(const unsigned index)=0
Deselects a selected item.
virtual tgrid & item_ordered(const unsigned index)=0
Gets the grid of an item.
virtual twidget * find_at(const tpoint &coordinate, const bool must_be_active) override=0
See twidget::find_at.
virtual void delete_item(const unsigned index)=0
Deletes an item.