The Battle for Wesnoth  1.15.1+dev
vertical_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 
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 vertical_scrollbar::vertical_scrollbar(const implementation::builder_vertical_scrollbar& builder)
32  : scrollbar_base(builder, type())
33 {
34 }
35 
37 {
38  const auto conf = cast_config_to<vertical_scrollbar_definition>();
39  assert(conf);
40  return conf->minimum_positioner_length;
41 }
42 
44 {
45  const auto conf = cast_config_to<vertical_scrollbar_definition>();
46  assert(conf);
47  return conf->maximum_positioner_length;
48 }
49 
51 {
52  const auto conf = cast_config_to<vertical_scrollbar_definition>();
53  assert(conf);
54  return conf->top_offset;
55 }
56 
58 {
59  const auto conf = cast_config_to<vertical_scrollbar_definition>();
60  assert(conf);
61  return conf->bottom_offset;
62 }
63 
65 {
66  SDL_Rect positioner_rect =
68 
69  // Note we assume the positioner is over the entire height of the widget.
70  return sdl::point_in_rect(coordinate, positioner_rect);
71 }
72 
74 {
75  // Not on the widget, leave.
76  if(static_cast<std::size_t>(coordinate.x) > get_width()
77  || static_cast<std::size_t>(coordinate.y) > get_height()) {
78  return 0;
79  }
80 
81  // we also assume the bar is over the entire width of the widget.
82  if(static_cast<std::size_t>(coordinate.y) < get_positioner_offset()) {
83  return -1;
84  } else if(static_cast<std::size_t>(coordinate.y) > get_positioner_offset()
86  return 1;
87  } else {
88  return 0;
89  }
90 }
91 
93 {
94  return static_cast<std::size_t>(coordinate.y) < get_height();
95 }
96 
97 // }---------- DEFINITION ---------{
98 
100  const config& cfg)
102 {
103  DBG_GUI_P << "Parsing vertical scrollbar " << id << '\n';
104 
105  load_resolutions<resolution>(cfg);
106 }
107 
108 /*WIKI
109  * @page = GUIWidgetDefinitionWML
110  * @order = 1_vertical_scrollbar
111  *
112  * == Vertical scrollbar ==
113  *
114  * The definition of a vertical scrollbar. This class is most of the time not
115  * used directly. Instead it's used to build other items with scrollbars.
116  *
117  * @begin{parent}{name="gui/"}
118  * @begin{tag}{name="vertical_scrollbar_definition"}{min=0}{max=-1}{super="generic/widget_definition"}
119  * The resolution for a vertical scrollbar also contains the following keys:
120  * @begin{tag}{name="resolution"}{min=0}{max=-1}{super=generic/widget_definition/resolution}
121  * @begin{table}{config}
122  * minimum_positioner_length & unsigned & &
123  * The minimum size the positioner is
124  * allowed to be. The engine needs to know
125  * this in order to calculate the best size
126  * for the positioner. $
127  * maximum_positioner_length & unsigned & 0 &
128  * The maximum size the positioner is
129  * allowed to be. If minimum and maximum are
130  * the same value the positioner is fixed
131  * size. If the maximum is 0 (and the
132  * minimum not) there's no maximum. $
133  * top_offset & unsigned & 0 & The number of pixels at the top which
134  * can't be used by the positioner. $
135  * bottom_offset & unsigned & 0 & The number of pixels at the bottom which
136  * can't be used by the positioner. $
137  * @end{table}
138  * The following states exist:
139  * * state_enabled, the vertical scrollbar is enabled.
140  * * state_disabled, the vertical scrollbar is disabled.
141  * * state_pressed, the left mouse button is down on the positioner of the
142  * vertical scrollbar.
143  * * state_focused, the mouse is over the positioner of the vertical scrollbar.
144  * @begin{tag}{name="state_enabled"}{min=0}{max=1}{super="generic/state"}
145  * @end{tag}{name="state_enabled"}
146  * @begin{tag}{name="state_disabled"}{min=0}{max=1}{super="generic/state"}
147  * @end{tag}{name="state_disabled"}
148  * @begin{tag}{name="state_pressed"}{min=0}{max=1}{super="generic/state"}
149  * @end{tag}{name="state_pressed"}
150  * @begin{tag}{name="state_focused"}{min=0}{max=1}{super="generic/state"}
151  * @end{tag}{name="state_focused"}
152  * @end{tag}{name="resolution"}
153  * @end{tag}{name="vertical_scrollbar_definition"}
154  * @end{parent}{name="gui/"}
155  */
157  : resolution_definition(cfg)
158  , minimum_positioner_length(cfg["minimum_positioner_length"])
159  , maximum_positioner_length(cfg["maximum_positioner_length"])
160  , top_offset(cfg["top_offset"])
161  , bottom_offset(cfg["bottom_offset"])
162 {
164  missing_mandatory_wml_key("resolution",
165  "minimum_positioner_length"));
166 
167  // Note the order should be the same as the enum state_t in scrollbar.hpp.
168  state.emplace_back(cfg.child("state_enabled"));
169  state.emplace_back(cfg.child("state_disabled"));
170  state.emplace_back(cfg.child("state_pressed"));
171  state.emplace_back(cfg.child("state_focused"));
172 }
173 
174 // }---------- BUILDER -----------{
175 
176 /*WIKI
177  * @page = GUIWidgetInstanceWML
178  * @order = 2_vertical_scrollbar
179  *
180  * == Vertical scrollbar ==
181  *
182  *
183  * @begin{parent}{name="gui/window/resolution/grid/row/column/"}
184  * @begin{tag}{name="vertical_scrollbar"}{min=0}{max=1}{super="generic/widget_instance"}
185  * @end{tag}{name="vertical_scrollbar"}
186  * @end{parent}{name="gui/window/resolution/grid/row/column/"}
187  */
188 
189 namespace implementation
190 {
191 
192 builder_vertical_scrollbar::builder_vertical_scrollbar(const config& cfg)
193  : builder_styled_widget(cfg)
194 {
195 }
196 
198 {
200 
201  widget->finalize_setup();
202 
203  DBG_GUI_G << "Window builder:"
204  << " placed vertical scrollbar '" << id << "' with definition '"
205  << definition << "'.\n";
206 
207  return widget;
208 }
209 
210 } // namespace implementation
211 
212 // }------------ END --------------
213 
214 } // 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
virtual unsigned minimum_positioner_length() const override
Inherited from scrollbar_base.
virtual bool in_orthogonal_range(const point &coordinate) 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
virtual unsigned offset_after() const override
Inherited from scrollbar_base.
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 bool on_positioner(const point &coordinate) const override
Inherited from scrollbar_base.
This file contains the settings handling of the widget library.
unsigned get_positioner_offset() const
Definition: scrollbar.hpp:184
virtual unsigned offset_before() const override
Inherited from scrollbar_base.
#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 int on_bar(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 unsigned maximum_positioner_length() const override
Inherited from scrollbar_base.
A vertical scrollbar.
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.