The Battle for Wesnoth  1.17.0-dev
vertical_scrollbar.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2021
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 <functional>
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 
109  : resolution_definition(cfg)
110  , minimum_positioner_length(cfg["minimum_positioner_length"])
111  , maximum_positioner_length(cfg["maximum_positioner_length"])
112  , top_offset(cfg["top_offset"])
113  , bottom_offset(cfg["bottom_offset"])
114 {
116  missing_mandatory_wml_key("resolution",
117  "minimum_positioner_length"));
118 
119  // Note the order should be the same as the enum state_t in scrollbar.hpp.
120  state.emplace_back(cfg.child("state_enabled"));
121  state.emplace_back(cfg.child("state_disabled"));
122  state.emplace_back(cfg.child("state_pressed"));
123  state.emplace_back(cfg.child("state_focused"));
124 }
125 
126 // }---------- BUILDER -----------{
127 
128 namespace implementation
129 {
130 
131 builder_vertical_scrollbar::builder_vertical_scrollbar(const config& cfg)
132  : builder_styled_widget(cfg)
133 {
134 }
135 
137 {
139 
140  widget->finalize_setup();
141 
142  DBG_GUI_G << "Window builder:"
143  << " placed vertical scrollbar '" << id << "' with definition '"
144  << definition << "'.\n";
145 
146  return widget;
147 }
148 
149 } // namespace implementation
150 
151 // }------------ END --------------
152 
153 } // 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:65
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:417
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:48
unsigned get_height() const
Definition: widget.cpp:329
virtual unsigned offset_after() const override
Inherited from scrollbar_base.
unsigned get_width() const
Definition: widget.cpp:324
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.
The definition of 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:59
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.