The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
list.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2010 - 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_LIST_HPP_INCLUDED
16 #define GUI_WIDGETS_LIST_HPP_INCLUDED
17 
18 #ifdef GUI2_EXPERIMENTAL_LISTBOX
19 
22 
23 namespace gui2
24 {
25 
26 /**
27  * The list class.
28  *
29  * For now it's a generic for all kind of lists, horizontal, vertical etc.
30  * Might be that there will be different types per class, not sure yet.
31  */
32 class tlist : public tcontainer_
33 {
34  friend class tdebug_layout_graph;
35 
36 public:
37  /**
38  * Constructor.
39  *
40  * @param has_minimum Does the listbox need to have one item
41  * selected.
42  * @param has_maximum Can the listbox only have one item
43  * selected.
44  * @param placement How are the items placed.
45  * @param select Select an item when selected, if false it
46  * changes the visible state instead.
47  */
48  tlist(const bool has_minimum,
49  const bool has_maximum,
50  const tgenerator_::tplacement placement,
51  const bool select,
52  const tbuilder_grid_const_ptr list_builder);
53 
54  /***** ***** ***** ***** Row handling. ***** ***** ****** *****/
55  /**
56  * When an item in the list is selected by the user we need to
57  * update the state. We installed a callback handler which
58  * calls us.
59  *
60  * @param item The data send to the set_members of the
61  * widgets.
62  * @param index The item before which to add the new item,
63  * 0 == begin, -1 == end.
64  */
65  void add_row(const string_map& item, const int index = -1);
66 
67  /**
68  * Adds single row to the grid.
69  *
70  * This function expect a row to have multiple widgets (either multiple
71  * columns or one column with multiple widgets).
72  *
73  *
74  * @param data The data to send to the set_members of the
75  * widgets. If the member id is not an empty
76  * string it is only send to the widget that has
77  * the wanted id (if any). If the member id is an
78  * empty string, it is send to all members.
79  * Having both empty and non-empty id's gives
80  * undefined behavior.
81  * @param index The item before which to add the new item,
82  * 0 == begin, -1 == end.
83  */
84  void add_row(const std::map<std::string /* widget id */, string_map>& data,
85  const int index = -1);
86 
87  /**
88  * Appends several rows to the grid.
89  *
90  * @param items The data to send to the set_members of the
91  * widgets.
92  */
93  void append_rows(const std::vector<string_map>& items);
94 
95  /**
96  * Removes a row in the listbox.
97  *
98  * @param row The row to remove, when not in
99  * range the function is ignored.
100  * @param count The number of rows to remove, 0 means all
101  * rows (starting from row).
102  */
103  void remove_row(const unsigned row, unsigned count = 1);
104 
105  /** Removes all the rows in the listbox, clearing it. */
106  void clear();
107 
108  /** Returns the number of items in the listbox. */
109  unsigned get_item_count() const;
110 
111  /**
112  * Makes a row active or inactive.
113  *
114  * NOTE this doesn't change the select status of the row.
115  *
116  * @param row The row to (de)activate.
117  * @param active true activate, false deactivate.
118  */
119  void set_row_active(const unsigned row, const bool active);
120 
121  /**
122  * Makes a row visible or invisible.
123  *
124  * @param row The row to show or hide.
125  * @param shown true visible, false invisible.
126  */
127  void set_row_shown(const unsigned row, const bool shown);
128 
129  /**
130  * Makes a row visible or invisible.
131  *
132  * Use this version if you want to show hide multiple items since it's
133  * optimized for that purpose, for one it calls the selection changed
134  * callback only once instead of several times.
135  *
136  * @param shown A vector with the show hide status for every
137  * row. The number of items in the vector must
138  * be equal to the number of items in the
139  * listbox.
140  */
141  void set_row_shown(const std::vector<bool>& shown);
142 
143  /**
144  * Returns the grid of the wanted row.
145  *
146  * There's only a const version since allowing callers to modify the grid
147  * behind our backs might give problems. We return a pointer instead of a
148  * reference since dynamic casting of pointers is easier (no try catch
149  * needed).
150  *
151  * @param row The row to get the grid from, the caller has
152  * to make sure the row is a valid row.
153  * @returns The grid of the wanted row.
154  */
155  const tgrid* get_row_grid(const unsigned row) const;
156 
157  /**
158  * The possibly-giving-problems nonconst version of get_row_grid
159  *
160  * @param row The row to get the grid from, the caller has
161  * to make sure the row is a valid row.
162  * @returns The grid of the wanted row.
163  */
164  tgrid* get_row_grid(const unsigned row);
165 
166  /**
167  * Selectes a row.
168  *
169  * @param row The row to select.
170  * @param select Select or deselect the row.
171  */
172  bool select_row(const unsigned row, const bool select = true);
173 
174  /**
175  * Returns the first selected row
176  *
177  * @returns The first selected row.
178  * @retval -1 No row selected.
179  */
180  int get_selected_row() const;
181 #if 0
182  /**
183  * Request to update the size of the content after changing the content.
184  *
185  * When a resize is required the container first can try to handle it
186  * itself. If it can't honor the request the function will call @ref
187  * twindow::invalidate_layout().
188  *
189  * @note Calling this function on a widget with size == (0, 0) results
190  * false but doesn't call invalidate_layout, the engine expects to be in
191  * build up phase with the layout already invalidated.
192  *
193  * @returns True if the resizing succeeded, false
194  * otherwise.
195  */
196  bool update_content_size();
197 #endif
198  /***** ***** ***** ***** inherited ***** ***** ****** *****/
199 
200  /** Inherited from tcontrol_. */
201  void init();
202 
203  /** See @ref tcontrol::get_active. */
204  virtual bool get_active() const override;
205 
206  /** See @ref tcontrol::get_state. */
207  virtual unsigned get_state() const override;
208 
209  /** See @ref twidget::place. */
210  virtual void place(const tpoint& origin, const tpoint& size) override;
211 
212 private:
213  /**
214  * Possible states of the widget.
215  *
216  * Note the order of the states must be the same as defined in settings.hpp.
217  */
218  enum tstate {
219  ENABLED,
220  DISABLED,
221  COUNT
222  };
223 
224  /**
225  * Current state of the widget.
226  *
227  * The state of the widget determines what to render and how the widget
228  * reacts to certain 'events'.
229  */
230  tstate state_;
231 
232  /**
233  * Contains a pointer to the generator.
234  *
235  * The pointer is not owned by this class, it's stored in the content_grid_
236  * of the tscrollbar_container super class and freed when it's grid is
237  * freed.
238  */
239  tgenerator_* generator_;
240 
241  /** Contains the builder for the new items. */
242  tbuilder_grid_const_ptr list_builder_;
243 
244  bool need_layout_;
245 #if 0
246  /**
247  * Resizes the content.
248  *
249  * The resize either happens due to resizing the content or invalidate the
250  * layout of the window.
251  *
252  * @param width_modification The wanted modification to the width:
253  * * negative values reduce width.
254  * * zero leave width as is.
255  * * positive values increase width.
256  * @param height_modification The wanted modification to the height:
257  * * negative values reduce height.
258  * * zero leave height as is.
259  * * positive values increase height.
260  */
261  void resize_content(
262  const int width_modification
263  , const int height_modification);
264 #endif
265  /** Layouts the children if needed. */
266  void layout_children(const bool force);
267 #if 0
268  /** Inherited from tscrollbar_container. */
269  virtual void set_content_size(const tpoint& origin, const tpoint& size);
270 #endif
271  /** See @ref tcontainer_::set_self_active. */
272  virtual void set_self_active(const bool active) override;
273 
274  /** See @ref tcontrol::get_control_type. */
275  virtual const std::string& get_control_type() const override;
276 
277  /***** ***** ***** signal handlers ***** ****** *****/
278 
279  void signal_handler_left_button_down(const event::tevent event);
280 
281  void signal_handler_pre_child_left_button_click(tgrid* grid,
282  const event::tevent event,
283  bool& handled,
284  bool& halt);
285 
286  void signal_handler_left_button_click(tgrid* grid,
287  const event::tevent event);
288 
289  void signal_handler_sdl_key_down(const event::tevent event,
290  bool& handled,
291  const SDLKey key,
292  SDLMod modifier);
293 };
294 
295 typedef tlist tlistbox;
296 
297 } // namespace gui2
298 
299 #endif
300 #endif
#define SDLMod
Definition: compat.hpp:30
boost::intrusive_ptr< const tbuilder_grid > tbuilder_grid_const_ptr
void clear(const std::string &key)
tplacement
Determines how the items are placed.
Definition: generator.hpp:51
const std::vector< std::string > items
GLint GLenum GLsizei GLint GLsizei const GLvoid * data
Definition: glew.h:1347
#define SDLKey
Definition: compat.hpp:29
A class inherited from ttext_box that displays its input as stars.
Definition: field-fwd.hpp:23
tevent
The event send to the dispatcher.
Definition: handler.hpp:54
bool init()
Initializes the gui subsystems.
Definition: helper.cpp:37
GLuint GLuint GLsizei count
Definition: glew.h:1221
std::map< std::string, t_string > string_map
Definition: generator.hpp:23
GLuint index
Definition: glew.h:1782
GLsizeiptr size
Definition: glew.h:1649
GLenum GLenum GLvoid * row
Definition: glew.h:3805
cl_event event
Definition: glew.h:3070
GLsizei const GLcharARB ** string
Definition: glew.h:4503