The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
vertical_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 
19 #include "gui/widgets/settings.hpp"
20 #include "wml_exception.hpp"
21 
22 #include "utils/functional.hpp"
23 
24 namespace gui2
25 {
26 
27 // ------------ WIDGET -----------{
28 
29 REGISTER_WIDGET(vertical_scrollbar)
30 
31 unsigned tvertical_scrollbar::minimum_positioner_length() const
32 {
34  = boost::dynamic_pointer_cast<const tvertical_scrollbar_definition::
36  assert(conf);
37  return conf->minimum_positioner_length;
38 }
39 
41 {
43  = boost::dynamic_pointer_cast<const tvertical_scrollbar_definition::
45  assert(conf);
46  return conf->maximum_positioner_length;
47 }
48 
50 {
52  = boost::dynamic_pointer_cast<const tvertical_scrollbar_definition::
54  assert(conf);
55  return conf->top_offset;
56 }
57 
59 {
61  = boost::dynamic_pointer_cast<const tvertical_scrollbar_definition::
63  assert(conf);
64  return conf->bottom_offset;
65 }
66 
67 bool tvertical_scrollbar::on_positioner(const tpoint& coordinate) const
68 {
69  // Note we assume the positioner is over the entire width of the widget.
70  return coordinate.y >= static_cast<int>(get_positioner_offset())
71  && coordinate.y < static_cast<int>(get_positioner_offset()
73  && coordinate.x > 0 && coordinate.x < static_cast<int>(get_width());
74 }
75 
76 int tvertical_scrollbar::on_bar(const tpoint& coordinate) const
77 {
78  // Not on the widget, leave.
79  if(static_cast<size_t>(coordinate.x) > get_width()
80  || static_cast<size_t>(coordinate.y) > get_height()) {
81  return 0;
82  }
83 
84  // we also assume the bar is over the entire width of the widget.
85  if(static_cast<size_t>(coordinate.y) < get_positioner_offset()) {
86  return -1;
87  } else if(static_cast<size_t>(coordinate.y) > get_positioner_offset()
89  return 1;
90  } else {
91  return 0;
92  }
93 }
94 
95 bool tvertical_scrollbar::in_orthogonal_range(const tpoint& coordinate) const
96 {
97  return static_cast<size_t>(coordinate.y) < get_height();
98 }
99 
101 {
102  static const std::string type = "vertical_scrollbar";
103  return type;
104 }
105 
106 // }---------- DEFINITION ---------{
107 
109  const config& cfg)
110  : tcontrol_definition(cfg)
111 {
112  DBG_GUI_P << "Parsing vertical scrollbar " << id << '\n';
113 
114  load_resolutions<tresolution>(cfg);
115 }
116 
117 /*WIKI
118  * @page = GUIWidgetDefinitionWML
119  * @order = 1_vertical_scrollbar
120  *
121  * == Vertical scrollbar ==
122  *
123  * The definition of a vertical scrollbar. This class is most of the time not
124  * used directly. Instead it's used to build other items with scrollbars.
125  *
126  * @begin{parent}{name="gui/"}
127  * @begin{tag}{name="vertical_scrollbar_definition"}{min=0}{max=-1}{super="generic/widget_definition"}
128  * The resolution for a vertical scrollbar also contains the following keys:
129  * @begin{tag}{name="resolution"}{min=0}{max=-1}{super=generic/widget_definition/resolution}
130  * @begin{table}{config}
131  * minimum_positioner_length & unsigned & &
132  * The minimum size the positioner is
133  * allowed to be. The engine needs to know
134  * this in order to calculate the best size
135  * for the positioner. $
136  * maximum_positioner_length & unsigned & 0 &
137  * The maximum size the positioner is
138  * allowed to be. If minimum and maximum are
139  * the same value the positioner is fixed
140  * size. If the maximum is 0 (and the
141  * minimum not) there's no maximum. $
142  * top_offset & unsigned & 0 & The number of pixels at the top which
143  * can't be used by the positioner. $
144  * bottom_offset & unsigned & 0 & The number of pixels at the bottom which
145  * can't be used by the positioner. $
146  * @end{table}
147  * The following states exist:
148  * * state_enabled, the vertical scrollbar is enabled.
149  * * state_disabled, the vertical scrollbar is disabled.
150  * * state_pressed, the left mouse button is down on the positioner of the
151  * vertical scrollbar.
152  * * state_focused, the mouse is over the positioner of the vertical scrollbar.
153  * @begin{tag}{name="state_enabled"}{min=0}{max=1}{super="generic/state"}
154  * @end{tag}{name="state_enabled"}
155  * @begin{tag}{name="state_disabled"}{min=0}{max=1}{super="generic/state"}
156  * @end{tag}{name="state_disabled"}
157  * @begin{tag}{name="state_pressed"}{min=0}{max=1}{super="generic/state"}
158  * @end{tag}{name="state_pressed"}
159  * @begin{tag}{name="state_focused"}{min=0}{max=1}{super="generic/state"}
160  * @end{tag}{name="state_focused"}
161  * @end{tag}{name="resolution"}
162  * @end{tag}{name="vertical_scrollbar_definition"}
163  * @end{parent}{name="gui/"}
164  */
167  , minimum_positioner_length(cfg["minimum_positioner_length"])
168  , maximum_positioner_length(cfg["maximum_positioner_length"])
169  , top_offset(cfg["top_offset"])
170  , bottom_offset(cfg["bottom_offset"])
171 {
173  missing_mandatory_wml_key("resolution",
174  "minimum_positioner_length"));
175 
176  // Note the order should be the same as the enum tstate in scrollbar.hpp.
177  state.push_back(tstate_definition(cfg.child("state_enabled")));
178  state.push_back(tstate_definition(cfg.child("state_disabled")));
179  state.push_back(tstate_definition(cfg.child("state_pressed")));
180  state.push_back(tstate_definition(cfg.child("state_focused")));
181 }
182 
183 // }---------- BUILDER -----------{
184 
185 /*WIKI
186  * @page = GUIWidgetInstanceWML
187  * @order = 2_vertical_scrollbar
188  *
189  * == Vertical scrollbar ==
190  *
191  *
192  * @begin{parent}{name="gui/window/resolution/grid/row/column/"}
193  * @begin{tag}{name="vertical_scrollbar"}{min=0}{max=1}{super="generic/widget_instance"}
194  * @end{tag}{name="vertical_scrollbar"}
195  * @end{parent}{name="gui/window/resolution/grid/row/column/"}
196  */
197 
198 namespace implementation
199 {
200 
201 tbuilder_vertical_scrollbar::tbuilder_vertical_scrollbar(const config& cfg)
202  : tbuilder_control(cfg)
203 {
204 }
205 
207 {
209 
210  init_control(widget);
211 
212  DBG_GUI_G << "Window builder:"
213  << " placed vertical scrollbar '" << id << "' with definition '"
214  << definition << "'.\n";
215 
216  return widget;
217 }
218 
219 } // namespace implementation
220 
221 // }------------ END --------------
222 
223 } // namespace gui2
unsigned get_width() const
Definition: widget.cpp:294
#define DBG_GUI_P
Definition: log.hpp:69
Add a special kind of assert to validate whether the input from WML doesn't contain any problems that...
int on_bar(const tpoint &coordinate) const
Inherited from tscrollbar.
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.
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.
virtual const std::string & get_control_type() const override
See tcontrol::get_control_type.
bool in_orthogonal_range(const tpoint &coordinate) const
Inherited from tscrollbar.
int y
y coordinate.
Definition: point.hpp:34
unsigned offset_before() const
Inherited from tscrollbar.
unsigned maximum_positioner_length() 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 offset_after() 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
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
bool on_positioner(const tpoint &coordinate) const
Inherited from tscrollbar.
#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.
unsigned get_positioner_length() const
Definition: scrollbar.hpp:162
A vertical scrollbar.