The Battle for Wesnoth  1.15.1+dev
horizontal_scrollbar.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2018 by Mark de Wever <koraq@xs4all.nl>
3  Part of the Battle for Wesnoth Project https://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 horizontal_scrollbar::horizontal_scrollbar(const implementation::builder_horizontal_scrollbar& builder)
37  : scrollbar_base(builder, type())
38 {
39 }
40 
42 {
43  const auto conf = cast_config_to<horizontal_scrollbar_definition>();
44  assert(conf);
45 
46  return conf->minimum_positioner_length;
47 }
48 
50 {
51  const auto conf = cast_config_to<horizontal_scrollbar_definition>();
52  assert(conf);
53 
54  return conf->maximum_positioner_length;
55 }
56 
58 {
59  const auto conf = cast_config_to<horizontal_scrollbar_definition>();
60  assert(conf);
61 
62  return conf->left_offset;
63 }
64 
66 {
67  const auto conf = cast_config_to<horizontal_scrollbar_definition>();
68  assert(conf);
69 
70  return conf->right_offset;
71 }
72 
74 {
75  SDL_Rect positioner_rect =
77 
78  // Note we assume the positioner is over the entire height of the widget.
79  return sdl::point_in_rect(coordinate, positioner_rect);
80 }
81 
83 {
84  // Not on the widget, leave.
85  if(static_cast<std::size_t>(coordinate.x) > get_width()
86  || static_cast<std::size_t>(coordinate.y) > get_height()) {
87  return 0;
88  }
89 
90  // we also assume the bar is over the entire width of the widget.
91  if(static_cast<std::size_t>(coordinate.x) < get_positioner_offset()) {
92  return -1;
93  } else if(static_cast<std::size_t>(coordinate.x) > get_positioner_offset()
95 
96  return 1;
97  } else {
98  return 0;
99  }
100 }
101 
103 {
104  return static_cast<std::size_t>(coordinate.x) < get_width();
105 }
106 
107 // }---------- DEFINITION ---------{
108 
110  const config& cfg)
112 {
113  DBG_GUI_P << "Parsing horizontal scrollbar " << id << '\n';
114 
115  load_resolutions<resolution>(cfg);
116 }
117 
118 /*WIKI
119  * @page = GUIWidgetDefinitionWML
120  * @order = 1_vertical_scrollbar
121  *
122  * == Horizontal scrollbar ==
123  *
124  * @macro = horizontal_scrollbar_description
125  * @begin{parent}{name="gui/"}
126  * @begin{tag}{name="horizontal_scrollbar_definition"}{min=0}{max=-1}{super="generic/widget_definition"}
127  * The resolution for a horizontal scrollbar also contains the following keys:
128  * @begin{tag}{name="resolution"}{min=0}{max=-1}{super="generic/widget_definition/resolution"}
129  * @begin{table}{config}
130  * minimum_positioner_length & unsigned & &
131  * The minimum size the positioner is
132  * allowed to be. The engine needs to know
133  * this in order to calculate the best size
134  * for the positioner. $
135  * maximum_positioner_length & unsigned & 0 &
136  * The maximum size the positioner is
137  * allowed to be. If minimum and maximum are
138  * the same value the positioner is fixed
139  * size. If the maximum is 0 (and the
140  * minimum not) there's no maximum. $
141  * left_offset & unsigned & 0 & The number of pixels at the left which
142  * can't be used by the positioner. $
143  * right_offset & unsigned & 0 & The number of pixels at the right which
144  * can't be used by the positioner. $
145  * @end{table}
146  *
147  * The following states exist:
148  * * state_enabled, the horizontal scrollbar is enabled.
149  * * state_disabled, the horizontal scrollbar is disabled.
150  * * state_pressed, the left mouse button is down on the positioner of the
151  * horizontal scrollbar.
152  * * state_focused, the mouse is over the positioner of the horizontal
153  * scrollbar.
154  * @begin{tag}{name="state_enabled"}{min=0}{max=1}{super="generic/state"}
155  * @end{tag}{name="state_enabled"}
156  * @begin{tag}{name="state_disabled"}{min=0}{max=1}{super="generic/state"}
157  * @end{tag}{name="state_disabled"}
158  * @begin{tag}{name="state_pressed"}{min=0}{max=1}{super="generic/state"}
159  * @end{tag}{name="state_pressed"}
160  * @begin{tag}{name="state_focused"}{min=0}{max=1}{super="generic/state"}
161  * @end{tag}{name="state_focused"}
162  * @end{tag}{name="resolution"}
163  * @end{tag}{name="horizontal_scrollbar_definition"}
164  * @end{parent}{name="gui/"}
165  */
167  : resolution_definition(cfg)
168  , minimum_positioner_length(cfg["minimum_positioner_length"])
169  , maximum_positioner_length(cfg["maximum_positioner_length"])
170  , left_offset(cfg["left_offset"])
171  , right_offset(cfg["right_offset"])
172 {
174  missing_mandatory_wml_key("resolution",
175  "minimum_positioner_length"));
176 
177  // Note the order should be the same as the enum state_t is scrollbar.hpp.
178  state.emplace_back(cfg.child("state_enabled"));
179  state.emplace_back(cfg.child("state_disabled"));
180  state.emplace_back(cfg.child("state_pressed"));
181  state.emplace_back(cfg.child("state_focused"));
182 }
183 
184 // }---------- BUILDER -----------{
185 
186 /*WIKI_MACRO
187  * @begin{macro}{horizontal_scrollbar_description}
188  *
189  * A horizontal scrollbar is a widget that shows a horizontal scrollbar.
190  * This widget is most of the time used in a container to control the
191  * scrolling of its contents.
192  * @end{macro}
193  */
194 
195 /*WIKI
196  * @page = GUIToolkitWML
197  * @order = 2_horizontal_scrollbar
198  *
199  * == Horizontal scrollbar ==
200  *
201  * @macro = horizontal_scrollbar_description
202  * @begin{parent}{name="gui/window/resolution/grid/row/column/"}
203  * @begin{tag}{name="horizontal_scrollbar"}{min=0}{max=-1}{super="generic/widget_instance"}
204  * @end{tag}{name="horizontal_scrollbar"}
205  * @end{parent}{name="gui/window/resolution/grid/row/column/"}
206  * A horizontal scrollbar has no special fields.
207  */
208 
209 namespace implementation
210 {
211 
212 builder_horizontal_scrollbar::builder_horizontal_scrollbar(const config& cfg)
213  : builder_styled_widget(cfg)
214 {
215 }
216 
218 {
220 
221  widget->finalize_setup();
222 
223  DBG_GUI_G << "Window builder:"
224  << " placed horizontal scrollbar '" << id << "' with definition '"
225  << definition << "'.\n";
226 
227  return widget;
228 }
229 
230 } // namespace implementation
231 
232 // }------------ END --------------
233 
234 } // namespace gui2
Base class for a scroll bar.
Definition: scrollbar.hpp:40
Base class of a resolution, contains the common keys for a resolution.
#define DBG_GUI_P
Definition: log.hpp:68
config & child(config_key_type 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:420
std::vector< state_definition > state
A horizontal scrollbar.
virtual unsigned minimum_positioner_length() const override
Inherited from scrollbar_base.
virtual unsigned offset_before() const override
Inherited from scrollbar_base.
Add a special kind of assert to validate whether the input from WML doesn&#39;t contain any problems that...
Base class for all widgets.
Definition: widget.hpp:47
unsigned get_height() const
Definition: widget.cpp:327
unsigned get_width() const
Definition: widget.cpp:322
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 x
x coordinate.
Definition: point.hpp:44
Generic file dialog.
Definition: field-fwd.hpp:22
std::string definition
Parameters for the styled_widget.
#define VALIDATE(cond, message)
The macro to use for the validation of WML.
virtual unsigned offset_after() const override
Inherited from scrollbar_base.
This file contains the settings handling of the widget library.
virtual int on_bar(const point &coordinate) const override
Inherited from scrollbar_base.
unsigned get_positioner_offset() const
Definition: scrollbar.hpp:184
#define REGISTER_WIDGET(id)
Wrapper for REGISTER_WIDGET3.
bool point_in_rect(int x, int y, const SDL_Rect &rect)
Tests whether a point is inside a rectangle.
Definition: rect.cpp:22
virtual bool in_orthogonal_range(const point &coordinate) const override
Inherited from scrollbar_base.
Holds a 2D point.
Definition: point.hpp:23
SDL_Rect create_rect(const int x, const int y, const int w, const int h)
Creates an SDL_Rect with the given dimensions.
Definition: rect.hpp:39
virtual bool on_positioner(const point &coordinate) const override
Inherited from scrollbar_base.
virtual unsigned maximum_positioner_length() const override
Inherited from scrollbar_base.
map_location coordinate
Contains an x and y coordinate used for starting positions in maps.
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:92
int y
y coordinate.
Definition: point.hpp:47
unsigned get_positioner_length() const
Definition: scrollbar.hpp:189
#define DBG_GUI_G
Definition: log.hpp:40
Contains the implementation details for lexical_cast and shouldn&#39;t be used directly.