The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
slider.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_SLIDER_HPP_INCLUDED
16 #define GUI_WIDGETS_SLIDER_HPP_INCLUDED
17 
20 
23 
24 namespace gui2
25 {
26 
27 // ------------ WIDGET -----------{
28 
29 /** A slider. */
30 class tslider : public tscrollbar_, public tinteger_selector_
31 {
32 public:
33  tslider();
34 
35  /***** ***** ***** ***** layout functions ***** ***** ***** *****/
36 
37 private:
38  /** See @ref twidget::calculate_best_size. */
39  virtual tpoint calculate_best_size() const override;
40 
41 public:
42  /***** ***** ***** ***** Inherited ***** ***** ***** *****/
43 
44  /** Inherited from tinteger_selector_. */
45  void set_value(const int value);
46 
47  /** Inherited from tinteger_selector_. */
48  int get_value() const
49  {
51  }
52 
53  /** Inherited from tinteger_selector_. */
54  void set_minimum_value(const int minimum_value);
55 
56  /** Inherited from tinteger_selector_. */
57  int get_minimum_value() const
58  {
59  return minimum_value_;
60  }
61 
62  /** Inherited from tinteger_selector_. */
63  void set_maximum_value(const int maximum_value);
64 
65  /** Inherited from tinteger_selector_. */
66  int get_maximum_value() const
67  // The number of items needs to include the begin and end so count - 1.
68  {
69  return minimum_value_ + get_item_count() - 1;
70  }
71  typedef std::function<t_string(int /*current position*/, int /*num positions*/)> tlabel_creator;
72  /***** ***** ***** setters / getters for members ***** ****** *****/
73 
74  void set_best_slider_length(const unsigned length)
75  {
77  set_is_dirty(true);
78  }
79 
80  void set_minimum_value_label(const t_string& minimum_value_label)
81  {
82  minimum_value_label_ = minimum_value_label;
83  }
84 
85  void set_maximum_value_label(const t_string& maximum_value_label)
86  {
87  maximum_value_label_ = maximum_value_label;
88  }
89 
90  void set_value_labels(const std::vector<t_string>& value_labels);
91 
92  void set_value_labels(const tlabel_creator& value_labels)
93  {
94  value_labels_ = value_labels;
95  }
96 
97  /**
98  * Returns the label shown for the current value.
99  *
100  * @returns The label for the current value, if no label
101  * for the current label is defined, it returns
102  * the result of get_value().
103  */
104  t_string get_value_label() const;
105 
106 protected:
107  /** Inherited from tscrollbar. */
109 
110 private:
111  /** The best size for the slider part itself, if 0 ignored. */
113 
114  /**
115  * The minimum value the slider holds.
116  *
117  * The maximum value is minimum + item_count_.
118  * The current value is minimum + item_position_.
119  */
121 
122  /** Inherited from tscrollbar. */
123  unsigned get_length() const
124  {
125  return get_width();
126  }
127 
128  /** Inherited from tscrollbar. */
129  unsigned minimum_positioner_length() const;
130 
131  /** Inherited from tscrollbar. */
132  unsigned maximum_positioner_length() const;
133 
134  /** Inherited from tscrollbar. */
135  unsigned offset_before() const;
136 
137  /** Inherited from tscrollbar. */
138  unsigned offset_after() const;
139 
140  /** Inherited from tscrollbar. */
141  bool on_positioner(const tpoint& coordinate) const;
142 
143  /** Inherited from tscrollbar. */
144  int on_bar(const tpoint& coordinate) const;
145 
146  /** Inherited from tscrollbar. */
147  bool in_orthogonal_range(const tpoint& coordinate) const;
148 
149  /** Inherited from tscrollbar. */
150  int get_length_difference(const tpoint& original, const tpoint& current)
151  const
152  {
153  return current.x - original.x;
154  }
155 
156  /** See @ref tcontrol::update_canvas. */
157  virtual void update_canvas() override;
158 
159  /**
160  * When the slider shows the minimum value can show a special text.
161  * If this text is not empty this text is shown else the minimum value.
162  */
164 
165  /**
166  * When the slider shows the maximum value can show a special text.
167  * If this text is not empty this text is shown else the maximum value.
168  */
170 
171  /**
172  * This allows the slider to show custom texts instead of the values.
173  * This vector should have the same amount of items as options for the
174  * sliders. When set these texts are shown instead of the values. It also
175  * overrides minimum_value_label_ and maximum_value_label_.
176  */
177  tlabel_creator value_labels_;
178 
179  /** See @ref tcontrol::get_control_type. */
180  virtual const std::string& get_control_type() const override;
181 
182  /**
183  * Handlers for keyboard input
184  */
185  void handle_key_decrease(bool& handled);
186  void handle_key_increase(bool& handled);
187 
188  /**
189  * Signal handlers:
190  */
192  bool& handled,
193  const SDLKey key);
194 
195  // In this subclass, only used to grab keyboard focus -
196  // see tscrollbar class for more handling of this event.
198  bool& handled);
199 };
200 
201 // }---------- DEFINITION ---------{
202 
204 {
205  explicit tslider_definition(const config& cfg);
206 
208  {
209  explicit tresolution(const config& cfg);
210 
213 
214  unsigned left_offset;
215  unsigned right_offset;
216  };
217 };
218 
219 // }---------- BUILDER -----------{
220 
221 namespace implementation
222 {
223 
225 {
226  explicit tbuilder_slider(const config& cfg);
227 
229 
230  twidget* build() const;
231 
232 private:
236  unsigned step_size_;
237  int value_;
238 
241 
242  std::vector<t_string> value_labels_;
243 };
244 
245 } // namespace implementation
246 
247 // }------------ END --------------
248 
249 } // namespace gui2
250 
251 #endif
int on_bar(const tpoint &coordinate) const
Inherited from tscrollbar.
Definition: slider.cpp:203
unsigned get_width() const
Definition: widget.cpp:294
void handle_key_increase(bool &handled)
Definition: slider.cpp:255
t_string maximum_value_label_
When the slider shows the maximum value can show a special text.
Definition: slider.hpp:169
int get_length_difference(const tpoint &original, const tpoint &current) const
Inherited from tscrollbar.
Definition: slider.hpp:150
tbuilder_slider(const config &cfg)
Definition: slider.cpp:434
void set_best_slider_length(const unsigned length)
Definition: slider.hpp:74
int minimum_value_
The minimum value the slider holds.
Definition: slider.hpp:120
Small abstract helper class.
int get_maximum_value() const
Inherited from tinteger_selector_.
Definition: slider.hpp:66
void child_callback_positioner_moved()
Inherited from tscrollbar.
Definition: slider.cpp:153
void set_minimum_value(const int minimum_value)
Inherited from tinteger_selector_.
Definition: slider.cpp:92
virtual void update_canvas() override
See tcontrol::update_canvas.
Definition: slider.cpp:228
void signal_handler_left_button_up(const event::tevent event, bool &handled)
Definition: slider.cpp:280
Base class for a scroll bar.
Definition: scrollbar.hpp:41
void set_maximum_value(const int maximum_value)
Inherited from tinteger_selector_.
Definition: slider.cpp:116
bool on_positioner(const tpoint &coordinate) const
Inherited from tscrollbar.
Definition: slider.cpp:194
void set_value(const int value)
Inherited from tinteger_selector_.
Definition: slider.cpp:77
t_string get_value_label() const
Returns the label shown for the current value.
Definition: slider.cpp:138
void set_is_dirty(const bool is_dirty)
Definition: widget.cpp:435
#define SDLKey
Definition: compat.hpp:29
Base class of a resolution, contains the common keys for a resolution.
tlabel_creator value_labels_
This allows the slider to show custom texts instead of the values.
Definition: slider.hpp:177
unsigned get_item_count() const
Definition: scrollbar.hpp:121
GLuint GLsizei GLsizei * length
Definition: glew.h:1793
void set_value_labels(const std::vector< t_string > &value_labels)
Definition: slider.cpp:297
A class inherited from ttext_box that displays its input as stars.
Definition: field-fwd.hpp:23
void handle_key_decrease(bool &handled)
Handlers for keyboard input.
Definition: slider.cpp:246
unsigned minimum_positioner_length() const
Inherited from tscrollbar.
Definition: slider.cpp:158
unsigned offset_after() const
Inherited from tscrollbar.
Definition: slider.cpp:185
virtual twidget * build() const =0
int get_value() const
Inherited from tinteger_selector_.
Definition: slider.hpp:48
unsigned get_item_position() const
Definition: scrollbar.hpp:131
GLsizei const GLfloat * value
Definition: glew.h:1817
unsigned offset_before() const
Inherited from tscrollbar.
Definition: slider.cpp:176
std::function< t_string(int, int)> tlabel_creator
Definition: slider.hpp:71
tevent
The event send to the dispatcher.
Definition: handler.hpp:54
unsigned get_length() const
Inherited from tscrollbar.
Definition: slider.hpp:123
std::vector< t_string > value_labels_
Definition: slider.hpp:242
unsigned best_slider_length_
The best size for the slider part itself, if 0 ignored.
Definition: slider.hpp:112
tslider_definition(const config &cfg)
Definition: slider.cpp:305
int x
x coordinate.
Definition: point.hpp:31
virtual const std::string & get_control_type() const override
See tcontrol::get_control_type.
Definition: slider.cpp:240
unsigned maximum_positioner_length() const
Inherited from tscrollbar.
Definition: slider.cpp:167
t_string minimum_value_label_
When the slider shows the minimum value can show a special text.
Definition: slider.hpp:163
void signal_handler_sdl_key_down(const event::tevent event, bool &handled, const SDLKey key)
Signal handlers:
Definition: slider.cpp:264
Holds a 2D point.
Definition: point.hpp:24
int get_minimum_value() const
Inherited from tinteger_selector_.
Definition: slider.hpp:57
virtual tpoint calculate_best_size() const override
See twidget::calculate_best_size.
Definition: slider.cpp:54
cl_event event
Definition: glew.h:3070
Base class for all widgets.
Definition: widget.hpp:49
void set_value_labels(const tlabel_creator &value_labels)
Definition: slider.hpp:92
A slider.
Definition: slider.hpp:30
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
unsigned get_step_size() const
Definition: scrollbar.hpp:146
bool in_orthogonal_range(const tpoint &coordinate) const
Inherited from tscrollbar.
Definition: slider.cpp:223
GLsizei const GLcharARB ** string
Definition: glew.h:4503
void set_maximum_value_label(const t_string &maximum_value_label)
Definition: slider.hpp:85
Contains the implementation details for lexical_cast and shouldn't be used directly.
void set_minimum_value_label(const t_string &minimum_value_label)
Definition: slider.hpp:80