The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
control.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_CONTROL_HPP_INCLUDED
16 #define GUI_WIDGETS_CONTROL_HPP_INCLUDED
17 
20 #include "gui/widgets/widget.hpp"
21 #include "../../text.hpp" // We want the file in src/
22 
23 namespace gui2
24 {
25 
26 // ------------ WIDGET -----------{
27 
28 namespace implementation
29 {
30 struct tbuilder_control;
31 } // namespace implementation
32 
33 /** Base class for all visible items. */
34 class tcontrol : public twidget
35 {
36  friend class tdebug_layout_graph;
37 
38 public:
39  /** @deprecated Used the second overload. */
40  explicit tcontrol(const unsigned canvas_count);
41 
42  /**
43  * Constructor.
44  *
45  * @param builder The builder object with the settings for the
46  * object.
47  *
48  * @param canvas_count The number of canvasses in the control.
49  */
51  const unsigned canvas_count,
52  const std::string& control_type);
53 
54  /**
55  * Sets the members of the control.
56  *
57  * The map contains named members it can set, controls inheriting from us
58  * can add additional members to set by this function. The following
59  * members can by the following key:
60  * * label_ label
61  * * tooltip_ tooltip
62  * * help_message_ help
63  *
64  *
65  * @param data Map with the key value pairs to set the
66  * members.
67  */
68  virtual void set_members(const string_map& data);
69 
70  /***** ***** ***** ***** State handling ***** ***** ***** *****/
71 
72  /**
73  * Sets the control's state.
74  *
75  * Sets the control in the active state, when inactive a control can't be
76  * used and doesn't react to events. (Note read-only for a ttext_ is a
77  * different state.)
78  */
79  virtual void set_active(const bool active) = 0;
80 
81  /** Gets the active state of the control. */
82  virtual bool get_active() const = 0;
83 
84 protected:
85  /** Returns the id of the state.
86  *
87  * The current state is also the index canvas_.
88  */
89  virtual unsigned get_state() const = 0;
90 
91 public:
92  /***** ***** ***** ***** Easy close handling ***** ***** ***** *****/
93 
94  /**
95  * See @ref twidget::disable_click_dismiss.
96  *
97  * The default behavior is that a widget blocks easy close, if not it
98  * should override this function.
99  */
100  bool disable_click_dismiss() const override;
101 
102  /** See @ref twidget::create_walker. */
103  virtual iterator::twalker_* create_walker() override;
104 
105  /***** ***** ***** ***** layout functions ***** ***** ***** *****/
106 
107  /**
108  * Gets the minimum size as defined in the config.
109  *
110  * @pre config_ != nullptr
111  *
112  * @returns The size.
113  */
115 
116  /**
117  * Gets the default size as defined in the config.
118  *
119  * @pre config_ != nullptr
120  *
121  * @returns The size.
122  */
124 
125  /**
126  * Gets the best size as defined in the config.
127  *
128  * @pre config_ != nullptr
129  *
130  * @returns The size.
131  */
133 
134  /**
135  * Returns the number of characters per line.
136  *
137  * This value is used to call @ref ttext::set_characters_per_line
138  * (indirectly).
139  *
140  * @returns The characters per line. This implementation
141  * always returns 0.
142  */
143  virtual unsigned get_characters_per_line() const;
144 
145  /**
146  * Returns whether the label should be link_aware, in
147  * in rendering and in searching for links with get_link.
148  *
149  * This value is used to call @ref ttext::set_link_aware
150  * (indirectly).
151  *
152  * @returns The link aware status. This impl always
153  * always returns false.
154  */
155  virtual bool get_link_aware() const;
156 
157  /**
158  * Returns the color string to be used with links.
159  *
160  * This value is used to call @ref ttext::set_link_color
161  * (indirectly).
162  *
163  * @returns The link color string. This impl returns "#ffff00".
164  *
165  */
166  virtual std::string get_link_color() const;
167 
168  /**
169  * See @ref twidget::layout_initialise.
170  *
171  * @todo Also handle the tooltip state.
172  * Handle if shrunken_ && use_tooltip_on_label_overflow_.
173  */
174  virtual void layout_initialise(const bool full_initialisation) override;
175 
176  /** See @ref twidget::request_reduce_width. */
177  virtual void request_reduce_width(const unsigned maximum_width) override;
178 
179 protected:
180  /** See @ref twidget::calculate_best_size. */
181  virtual tpoint calculate_best_size() const override;
182 
183 public:
184  /** See @ref twidget::place. */
185  virtual void place(const tpoint& origin, const tpoint& size) override;
186 
187  /***** ***** ***** ***** Inherited ***** ***** ***** *****/
188 
189 private:
190  /**
191  * Loads the configuration of the widget.
192  *
193  * Controls have their definition stored in a definition object. In order to
194  * determine sizes and drawing the widget this definition needs to be
195  * loaded. The member definition_ contains the name of the definition and
196  * function load the proper configuration.
197  *
198  * @deprecated @ref definition_load_configuration() is the replacement.
199  */
200  void load_config();
201 
202  /**
203  * Uses the load function.
204  *
205  * @note This doesn't look really clean, but the final goal is refactor
206  * more code and call load_config in the ctor, removing the use case for
207  * the window. That however is a longer termine refactoring.
208  */
209  friend class twindow;
210 
211 public:
212  /** See @ref twidget::find_at. */
213  virtual twidget* find_at(const tpoint& coordinate,
214  const bool must_be_active) override;
215 
216  /** See @ref twidget::find_at. */
217  virtual const twidget* find_at(const tpoint& coordinate,
218  const bool must_be_active) const override;
219 
220  /** See @ref twidget::find. */
221  twidget* find(const std::string& id, const bool must_be_active) override;
222 
223  /** See @ref twidget::find. */
224  const twidget* find(const std::string& id,
225  const bool must_be_active) const override;
226 
227  /**
228  * Sets the definition.
229  *
230  * This function sets the definition of a control and should be called soon
231  * after creating the object since a lot of internal functions depend on the
232  * definition.
233  *
234  * This function should be called one time only!!!
235  */
236  void set_definition(const std::string& definition);
237 
238  /***** ***** ***** setters / getters for members ***** ****** *****/
240  {
242  }
243  void set_use_tooltip_on_label_overflow(const bool use_tooltip = true)
244  {
245  use_tooltip_on_label_overflow_ = use_tooltip;
246  }
247 
248  const t_string& label() const
249  {
250  return label_;
251  }
252  virtual void set_label(const t_string& label);
253 
254  virtual void set_use_markup(bool use_markup);
255  bool get_use_markup() const
256  {
257  return use_markup_;
258  }
259 
260  const t_string& tooltip() const
261  {
262  return tooltip_;
263  }
264  // Note setting the tooltip_ doesn't dirty an object.
266  {
267  tooltip_ = tooltip;
269  }
270 
271  const t_string& help_message() const
272  {
273  return help_message_;
274  }
275  // Note setting the help_message_ doesn't dirty an object.
277  {
279  }
280 
281  // const versions will be added when needed
282  std::vector<tcanvas>& canvas()
283  {
284  return canvas_;
285  }
286  tcanvas& canvas(const unsigned index)
287  {
288  assert(index < canvas_.size());
289  return canvas_[index];
290  }
291 
292  void set_text_alignment(const PangoAlignment text_alignment);
293  PangoAlignment get_text_alignment() const
294  {
295  return text_alignment_;
296  }
297 
298 protected:
300  {
301  return config_;
302  }
304  {
305  return config_;
306  }
307 
309  {
310  config_ = config;
311  }
312 
313  /***** ***** ***** ***** miscellaneous ***** ***** ***** *****/
314 
315  /**
316  * Updates the canvas(ses).
317  *
318  * This function should be called if either the size of the widget changes
319  * or the text on the widget changes.
320  */
321  virtual void update_canvas();
322 
323  /**
324  * Returns the maximum width available for the text.
325  *
326  * This value makes sense after the widget has been given a size, since the
327  * maximum width is based on the width of the widget.
328  */
329  int get_text_maximum_width() const;
330 
331  /**
332  * Returns the maximum height available for the text.
333  *
334  * This value makes sense after the widget has been given a size, since the
335  * maximum height is based on the height of the widget.
336  */
337  int get_text_maximum_height() const;
338 
339 private:
340  /**
341  * The definition is the id of that widget class.
342  *
343  * Eg for a button it [button_definition]id. A button can have multiple
344  * definitions which all look different but for the engine still is a
345  * button.
346  */
348 
349  /** Contain the non-editable text associated with control. */
351 
352  /** Use markup for the label? */
354 
355  /**
356  * If the text doesn't fit on the label should the text be used as tooltip?
357  *
358  * This only happens if the tooltip is empty.
359  */
361 
362  /**
363  * Tooltip text.
364  *
365  * The hovering event can cause a small tooltip to be shown, this is the
366  * text to be shown. At the moment the tooltip is a single line of text.
367  */
369 
370  /**
371  * Tooltip text.
372  *
373  * The help event can cause a tooltip to be shown, this is the text to be
374  * shown. At the moment the tooltip is a single line of text.
375  */
377 
378  /**
379  * Holds all canvas objects for a control.
380  *
381  * A control can have multiple states, which are defined in the classes
382  * inheriting from us. For every state there is a separate canvas, which is
383  * stored here. When drawing the state is determined and that canvas is
384  * drawn.
385  */
386  std::vector<tcanvas> canvas_;
387 
388  /**
389  * Contains the pointer to the configuration.
390  *
391  * Every control has a definition of how it should look, this contains a
392  * pointer to the definition. The definition is resolution dependent, where
393  * the resolution is the size of the Wesnoth application window. Depending
394  * on the resolution widgets can look different, use different fonts.
395  * Windows can use extra scrollbars use abbreviations as text etc.
396  */
398 
399  /**
400  * Load class dependent config settings.
401  *
402  * load_config will call this method after loading the config, by default it
403  * does nothing but classes can override it to implement custom behavior.
404  */
405  virtual void load_config_extra()
406  {
407  }
408 
409  /**
410  * Loads the configuration of the widget.
411  *
412  * Controls have their definition stored in a definition object. In order to
413  * determine sizes and drawing the widget this definition needs to be
414  * loaded. The member definition_ contains the name of the definition and
415  * function load the proper configuration.
416  */
417  void definition_load_configuration(const std::string& control_type);
418 
419 public:
420  /**
421  * Returns the control_type of the control.
422  *
423  * The control_type parameter for tgui_definition::get_control() To keep the
424  * code more generic this type is required so the controls need to return
425  * the proper string here. Might be used at other parts as well the get the
426  * type of
427  * control involved.
428  */
429  virtual const std::string& get_control_type() const = 0;
430 
431 protected:
432  /** See @ref twidget::impl_draw_background. */
433  virtual void impl_draw_background(surface& frame_buffer,
434  int x_offset,
435  int y_offset) override;
436 
437  /** See @ref twidget::impl_draw_foreground. */
438  virtual void impl_draw_foreground(surface& frame_buffer,
439  int x_offset,
440  int y_offset) override;
441 
442  /** Exposes font::ttext::get_token, for the text label of this control */
443  std::string get_label_token(const gui2::tpoint & position, const char * delimiters = " \n\r\t") const;
444 
445  std::string get_label_link(const gui2::tpoint & position) const;
446 
447 private:
448 #ifdef GUI2_EXPERIMENTAL_LISTBOX
449  /**
450  * Initializes the control.
451  *
452  * Not everything can be code in the constructor since virtual functions
453  * can't be used. So after construction this function needs to be called and
454  * only once, this happens when set_definition is called.
455  */
456  virtual void init()
457  {
458  }
459 #endif
460 
461  /**
462  * Gets the best size for a text.
463  *
464  * @param minimum_size The minimum size of the text.
465  * @param maximum_size The wanted maximum size of the text, if not
466  * possible it's ignored. A value of 0 means
467  * that it's ignored as well.
468  *
469  * @returns The best size.
470  */
471  tpoint get_best_text_size(const tpoint& minimum_size,
472  const tpoint& maximum_size = tpoint(0, 0)) const;
473 
474  /**
475  * Contains a helper cache for the rendering.
476  *
477  * Creating a ttext object is quite expensive and is done on various
478  * occasions so it's cached here.
479  *
480  * @todo Maybe if still too slow we might also copy this cache to the
481  * canvas so it can reuse our results, but for now it seems fast enough.
482  * Unfortunately that would make the dependency between the classes bigger
483  * as wanted.
484  */
486 
487  /** The maximum width for the text in a control. */
489 
490  /** The alignment of the text in a control. */
491  PangoAlignment text_alignment_;
492 
493  /** Is the widget smaller as it's best size? */
494  bool shrunken_;
495 
496  /***** ***** ***** signal handlers ***** ****** *****/
497 
499  bool& handled,
500  const tpoint& location);
501 
503  bool& handled,
504  const tpoint& location);
505 
507  bool& handled);
508 };
509 
510 // }---------- BUILDER -----------{
511 
512 class tcontrol;
513 
514 namespace implementation
515 {
516 
518 {
519 public:
520  tbuilder_control(const config& cfg);
521 
523 
524  virtual twidget* build(const treplacements& replacements) const override;
525 
526  /** @deprecated The control can initialize itself. */
527  void init_control(tcontrol* control) const;
528 
529  /** Parameters for the control. */
535 };
536 
537 } // namespace implementation
538 
539 // }------------ END --------------
540 
541 } // namespace gui2
542 
543 #endif
tpoint get_config_default_size() const
Gets the default size as defined in the config.
Definition: control.cpp:159
virtual void impl_draw_background(surface &frame_buffer, int x_offset, int y_offset) override
See twidget::impl_draw_background.
Definition: control.cpp:403
virtual void impl_draw_foreground(surface &frame_buffer, int x_offset, int y_offset) override
See twidget::impl_draw_foreground.
Definition: control.cpp:414
void signal_handler_show_tooltip(const event::tevent event, bool &handled, const tpoint &location)
Definition: control.cpp:512
void set_config(tresolution_definition_ptr config)
Definition: control.hpp:308
virtual unsigned get_characters_per_line() const
Returns the number of characters per line.
Definition: control.cpp:179
Contains the info needed to instantiate a widget.
twidget * find(const std::string &id, const bool must_be_active) override
See twidget::find.
Definition: control.cpp:301
virtual void load_config_extra()
Load class dependent config settings.
Definition: control.hpp:405
virtual const std::string & get_control_type() const =0
Returns the control_type of the control.
tresolution_definition_ptr config()
Definition: control.hpp:299
virtual void request_reduce_width(const unsigned maximum_width) override
See twidget::request_reduce_width.
Definition: control.cpp:204
virtual void set_label(const t_string &label)
Definition: control.cpp:330
int text_maximum_width_
The maximum width for the text in a control.
Definition: control.hpp:488
void set_help_message(const t_string &help_message)
Definition: control.hpp:276
GLint GLenum GLsizei GLint GLsizei const GLvoid * data
Definition: glew.h:1347
tresolution_definition_const_ptr config() const
Definition: control.hpp:303
virtual unsigned get_state() const =0
Returns the id of the state.
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
void init_control(tcontrol *control) const
Definition: control.cpp:675
std::string definition
Parameters for the control.
Definition: control.hpp:530
virtual void set_use_markup(bool use_markup)
Definition: control.cpp:342
virtual twidget * build() const =0
virtual void set_active(const bool active)=0
Sets the control's state.
tpoint get_config_maximum_size() const
Gets the best size as defined in the config.
Definition: control.cpp:169
tpoint get_best_text_size(const tpoint &minimum_size, const tpoint &maximum_size=tpoint(0, 0)) const
Gets the best size for a text.
Definition: control.cpp:442
t_string tooltip_
Tooltip text.
Definition: control.hpp:368
std::string get_label_token(const gui2::tpoint &position, const char *delimiters=" \n\r\t") const
Exposes font::ttext::get_token, for the text label of this control.
Definition: control.cpp:562
bool get_use_tooltip_on_label_overflow() const
Definition: control.hpp:239
int get_text_maximum_width() const
Returns the maximum width available for the text.
Definition: control.cpp:388
const t_string & tooltip() const
Definition: control.hpp:260
virtual void place(const tpoint &origin, const tpoint &size) override
See twidget::place.
Definition: control.cpp:250
PangoAlignment get_text_alignment() const
Definition: control.hpp:293
void load_config()
Loads the configuration of the widget.
Definition: control.cpp:274
const t_string & label() const
Definition: control.hpp:248
tevent
The event send to the dispatcher.
Definition: handler.hpp:54
A simple canvas which can be drawn upon.
Definition: canvas.hpp:44
void set_use_tooltip_on_label_overflow(const bool use_tooltip=true)
Definition: control.hpp:243
bool shrunken_
Is the widget smaller as it's best size?
Definition: control.hpp:494
void set_tooltip(const t_string &tooltip)
Definition: control.hpp:265
bool init()
Initializes the gui subsystems.
Definition: helper.cpp:37
bool get_use_markup() const
Definition: control.hpp:255
tresolution_definition_ptr config_
Contains the pointer to the configuration.
Definition: control.hpp:397
font::ttext renderer_
Contains a helper cache for the rendering.
Definition: control.hpp:485
std::map< std::string, t_string > string_map
Definition: generator.hpp:23
PangoAlignment text_alignment_
The alignment of the text in a control.
Definition: control.hpp:491
Encapsulates the map of the game.
Definition: location.hpp:38
std::string definition_
The definition is the id of that widget class.
Definition: control.hpp:347
void set_wants_mouse_hover(const bool hover=true)
virtual void update_canvas()
Updates the canvas(ses).
Definition: control.cpp:364
tpoint get_config_minimum_size() const
Gets the minimum size as defined in the config.
Definition: control.cpp:149
std::vector< tcanvas > canvas_
Holds all canvas objects for a control.
Definition: control.hpp:386
GLuint index
Definition: glew.h:1782
virtual iterator::twalker_ * create_walker() override
See twidget::create_walker.
Definition: control.cpp:144
bool use_markup_
Use markup for the label?
Definition: control.hpp:353
void signal_handler_show_helptip(const event::tevent event, bool &handled, const tpoint &location)
Definition: control.cpp:535
Holds a 2D point.
Definition: point.hpp:24
void definition_load_configuration(const std::string &control_type)
Loads the configuration of the widget.
Definition: control.cpp:423
std::vector< tcanvas > & canvas()
Definition: control.hpp:282
void signal_handler_notify_remove_tooltip(const event::tevent event, bool &handled)
Definition: control.cpp:547
const t_string & help_message() const
Definition: control.hpp:271
friend class tdebug_layout_graph
Definition: control.hpp:36
virtual bool get_link_aware() const
Returns whether the label should be link_aware, in in rendering and in searching for links with get_l...
Definition: control.cpp:184
tcanvas & canvas(const unsigned index)
Definition: control.hpp:286
Base class for all visible items.
Definition: control.hpp:34
bool disable_click_dismiss() const override
See twidget::disable_click_dismiss.
Definition: control.cpp:139
GLsizeiptr size
Definition: glew.h:1649
bool use_tooltip_on_label_overflow_
If the text doesn't fit on the label should the text be used as tooltip?
Definition: control.hpp:360
t_string label_
Contain the non-editable text associated with control.
Definition: control.hpp:350
void set_text_alignment(const PangoAlignment text_alignment)
Definition: control.cpp:353
cl_event event
Definition: glew.h:3070
int get_text_maximum_height() const
Returns the maximum height available for the text.
Definition: control.cpp:396
virtual void set_members(const string_map &data)
Sets the members of the control.
Definition: control.cpp:99
virtual twidget * find_at(const tpoint &coordinate, const bool must_be_active) override
See twidget::find_at.
Definition: control.cpp:284
Base class for all widgets.
Definition: widget.hpp:49
std::string get_label_link(const gui2::tpoint &position) const
Definition: control.cpp:567
tcontrol(const unsigned canvas_count)
Definition: control.cpp:46
virtual std::string get_link_color() const
Returns the color string to be used with links.
Definition: control.cpp:189
void set_definition(const std::string &definition)
Sets the definition.
Definition: control.cpp:318
The walker abstract base class.
Definition: walker.hpp:27
std::map< std::string, boost::intrusive_ptr< tbuilder_widget > > treplacements
The replacements type is used to define replacement types.
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
virtual bool get_active() const =0
Gets the active state of the control.
virtual void layout_initialise(const bool full_initialisation) override
See twidget::layout_initialise.
Definition: control.cpp:194
GLsizei const GLcharARB ** string
Definition: glew.h:4503
Text class.
Definition: text.hpp:66
t_string help_message_
Tooltip text.
Definition: control.hpp:376
bool empty() const
Definition: tstring.hpp:166
Contains the implementation details for lexical_cast and shouldn't be used directly.
virtual tpoint calculate_best_size() const override
See twidget::calculate_best_size.
Definition: control.cpp:229