The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
horizontal_scrollbar.cpp
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 #define GETTEXT_DOMAIN "wesnoth-lib"
16 
18 
22 
23 #include "gui/widgets/settings.hpp"
24 
25 #include "wml_exception.hpp"
26 
27 #include "utils/functional.hpp"
28 
29 namespace gui2
30 {
31 
32 // ------------ WIDGET -----------{
33 
34 REGISTER_WIDGET(horizontal_scrollbar)
35 
36 unsigned thorizontal_scrollbar::minimum_positioner_length() const
37 {
39  conf = boost::dynamic_pointer_cast<const thorizontal_scrollbar_definition::
41 
42  assert(conf);
43  return conf->minimum_positioner_length;
44 }
45 
47 {
49  conf = boost::dynamic_pointer_cast<const thorizontal_scrollbar_definition::
51 
52  assert(conf);
53  return conf->maximum_positioner_length;
54 }
55 
57 {
59  conf = boost::dynamic_pointer_cast<const thorizontal_scrollbar_definition::
61 
62  assert(conf);
63  return conf->left_offset;
64 }
65 
67 {
69  conf = boost::dynamic_pointer_cast<const thorizontal_scrollbar_definition::
71  assert(conf);
72 
73  return conf->right_offset;
74 }
75 
76 bool thorizontal_scrollbar::on_positioner(const tpoint& coordinate) const
77 {
78  // Note we assume the positioner is over the entire height of the widget.
79  return coordinate.x >= static_cast<int>(get_positioner_offset())
80  && coordinate.x < static_cast<int>(get_positioner_offset()
82  && coordinate.y > 0 && coordinate.y < static_cast<int>(get_height());
83 }
84 
85 int thorizontal_scrollbar::on_bar(const tpoint& coordinate) const
86 {
87  // Not on the widget, leave.
88  if(static_cast<size_t>(coordinate.x) > get_width()
89  || static_cast<size_t>(coordinate.y) > get_height()) {
90  return 0;
91  }
92 
93  // we also assume the bar is over the entire width of the widget.
94  if(static_cast<size_t>(coordinate.x) < get_positioner_offset()) {
95  return -1;
96  } else if(static_cast<size_t>(coordinate.x) > get_positioner_offset()
98 
99  return 1;
100  } else {
101  return 0;
102  }
103 }
104 
106 {
107  return static_cast<size_t>(coordinate.x) < get_width();
108 }
109 
111 {
112  static const std::string type = "horizontal_scrollbar";
113  return type;
114 }
115 
116 // }---------- DEFINITION ---------{
117 
119  const config& cfg)
120  : tcontrol_definition(cfg)
121 {
122  DBG_GUI_P << "Parsing horizontal scrollbar " << id << '\n';
123 
124  load_resolutions<tresolution>(cfg);
125 }
126 
127 /*WIKI
128  * @page = GUIWidgetDefinitionWML
129  * @order = 1_vertical_scrollbar
130  *
131  * == Horizontal scrollbar ==
132  *
133  * @macro = horizontal_scrollbar_description
134  * @begin{parent}{name="gui/"}
135  * @begin{tag}{name="horizontal_scrollbar_definition"}{min=0}{max=-1}{super="generic/widget_definition"}
136  * The resolution for a horizontal scrollbar also contains the following keys:
137  * @begin{tag}{name="resolution"}{min=0}{max=-1}{super="generic/widget_definition/resolution"}
138  * @begin{table}{config}
139  * minimum_positioner_length & unsigned & &
140  * The minimum size the positioner is
141  * allowed to be. The engine needs to know
142  * this in order to calculate the best size
143  * for the positioner. $
144  * maximum_positioner_length & unsigned & 0 &
145  * The maximum size the positioner is
146  * allowed to be. If minimum and maximum are
147  * the same value the positioner is fixed
148  * size. If the maximum is 0 (and the
149  * minimum not) there's no maximum. $
150  * left_offset & unsigned & 0 & The number of pixels at the left which
151  * can't be used by the positioner. $
152  * right_offset & unsigned & 0 & The number of pixels at the right which
153  * can't be used by the positioner. $
154  * @end{table}
155  *
156  * The following states exist:
157  * * state_enabled, the horizontal scrollbar is enabled.
158  * * state_disabled, the horizontal scrollbar is disabled.
159  * * state_pressed, the left mouse button is down on the positioner of the
160  * horizontal scrollbar.
161  * * state_focused, the mouse is over the positioner of the horizontal
162  * scrollbar.
163  * @begin{tag}{name="state_enabled"}{min=0}{max=1}{super="generic/state"}
164  * @end{tag}{name="state_enabled"}
165  * @begin{tag}{name="state_disabled"}{min=0}{max=1}{super="generic/state"}
166  * @end{tag}{name="state_disabled"}
167  * @begin{tag}{name="state_pressed"}{min=0}{max=1}{super="generic/state"}
168  * @end{tag}{name="state_pressed"}
169  * @begin{tag}{name="state_focused"}{min=0}{max=1}{super="generic/state"}
170  * @end{tag}{name="state_focused"}
171  * @end{tag}{name="resolution"}
172  * @end{tag}{name="horizontal_scrollbar_definition"}
173  * @end{parent}{name="gui/"}
174  */
177  , minimum_positioner_length(cfg["minimum_positioner_length"])
178  , maximum_positioner_length(cfg["maximum_positioner_length"])
179  , left_offset(cfg["left_offset"])
180  , right_offset(cfg["right_offset"])
181 {
183  missing_mandatory_wml_key("resolution",
184  "minimum_positioner_length"));
185 
186  // Note the order should be the same as the enum tstate is scrollbar.hpp.
187  state.push_back(tstate_definition(cfg.child("state_enabled")));
188  state.push_back(tstate_definition(cfg.child("state_disabled")));
189  state.push_back(tstate_definition(cfg.child("state_pressed")));
190  state.push_back(tstate_definition(cfg.child("state_focused")));
191 }
192 
193 // }---------- BUILDER -----------{
194 
195 /*WIKI_MACRO
196  * @begin{macro}{horizontal_scrollbar_description}
197  *
198  * A horizontal scrollbar is a widget that shows a horizontal scrollbar.
199  * This widget is most of the time used in a container to control the
200  * scrolling of its contents.
201  * @end{macro}
202  */
203 
204 /*WIKI
205  * @page = GUIToolkitWML
206  * @order = 2_horizontal_scrollbar
207  *
208  * == Horizontal scrollbar ==
209  *
210  * @macro = horizontal_scrollbar_description
211  * @begin{parent}{name="gui/window/resolution/grid/row/column/"}
212  * @begin{tag}{name="horizontal_scrollbar"}{min=0}{max=-1}{super="generic/widget_instance"}
213  * @end{tag}{name="horizontal_scrollbar"}
214  * @end{parent}{name="gui/window/resolution/grid/row/column/"}
215  * A horizontal scrollbar has no special fields.
216  */
217 
218 namespace implementation
219 {
220 
221 tbuilder_horizontal_scrollbar::tbuilder_horizontal_scrollbar(const config& cfg)
222  : tbuilder_control(cfg)
223 {
224 }
225 
227 {
229 
230  init_control(widget);
231 
232  DBG_GUI_G << "Window builder:"
233  << " placed horizontal scrollbar '" << id << "' with definition '"
234  << definition << "'.\n";
235 
236  return widget;
237 }
238 
239 } // namespace implementation
240 
241 // }------------ END --------------
242 
243 } // namespace gui2
unsigned get_width() const
Definition: widget.cpp:294
#define DBG_GUI_P
Definition: log.hpp:69
unsigned offset_before() const
Inherited from tscrollbar.
Add a special kind of assert to validate whether the input from WML doesn't contain any problems that...
GLuint GLuint GLsizei GLenum type
Definition: glew.h:1221
tresolution_definition_ptr config()
Definition: control.hpp:299
Base class of a resolution, contains the common keys for a resolution.
std::string missing_mandatory_wml_key(const std::string &section, const std::string &key, const std::string &primary_key, const std::string &primary_value)
Returns a standard message for a missing wml key.
int on_bar(const tpoint &coordinate) const
Inherited from tscrollbar.
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
#define VALIDATE(cond, message)
The macro to use for the validation of WML.
This file contains the settings handling of the widget library.
int y
y coordinate.
Definition: point.hpp:34
unsigned offset_after() const
Inherited from tscrollbar.
Contains the state info for a resolution.
#define REGISTER_WIDGET(id)
Wrapper for REGISTER_WIDGET3.
int x
x coordinate.
Definition: point.hpp:31
unsigned get_positioner_offset() const
Definition: scrollbar.hpp:157
unsigned get_height() const
Definition: widget.cpp:299
Holds a 2D point.
Definition: point.hpp:24
unsigned maximum_positioner_length() const
Inherited from tscrollbar.
A horizontal scrollbar.
bool on_positioner(const tpoint &coordinate) const
Inherited from tscrollbar.
std::vector< tstate_definition > state
config & child(const std::string &key, int n=0)
Returns the nth child with the given key, or a reference to an invalid config if there is none...
Definition: config.cpp:658
Base class for all widgets.
Definition: widget.hpp:49
bool in_orthogonal_range(const tpoint &coordinate) const
Inherited from tscrollbar.
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
#define DBG_GUI_G
Definition: log.hpp:41
GLsizei const GLcharARB ** string
Definition: glew.h:4503
Contains the implementation details for lexical_cast and shouldn't be used directly.
virtual const std::string & get_control_type() const override
See tcontrol::get_control_type.
unsigned get_positioner_length() const
Definition: scrollbar.hpp:162