The Battle for Wesnoth 1.21.0-dev
 
Loading...
Searching...
No Matches
horizontal_scrollbar.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2008 - 2025
3 by Mark de Wever <koraq@xs4all.nl>
4 Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY.
12
13 See the COPYING file for more details.
14*/
15
16#define GETTEXT_DOMAIN "wesnoth-lib"
17
19
22
23#include "wml_exception.hpp"
24
25namespace gui2
26{
27
28// ------------ WIDGET -----------{
29
30REGISTER_WIDGET(horizontal_scrollbar)
31
32horizontal_scrollbar::horizontal_scrollbar(const implementation::builder_horizontal_scrollbar& builder)
33 : scrollbar_base(builder, type())
34{
35}
36
38{
39 const auto conf = cast_config_to<horizontal_scrollbar_definition>();
40 assert(conf);
41
42 return conf->minimum_positioner_length;
43}
44
46{
47 const auto conf = cast_config_to<horizontal_scrollbar_definition>();
48 assert(conf);
49
50 return conf->maximum_positioner_length;
51}
52
54{
55 const auto conf = cast_config_to<horizontal_scrollbar_definition>();
56 assert(conf);
57
58 return conf->left_offset;
59}
60
62{
63 const auto conf = cast_config_to<horizontal_scrollbar_definition>();
64 assert(conf);
65
66 return conf->right_offset;
67}
68
69bool horizontal_scrollbar::on_positioner(const point& coordinate) const
70{
71 rect positioner_rect(
73 );
74
75 // Note we assume the positioner is over the entire height of the widget.
76 return positioner_rect.contains(coordinate);
77}
78
79int horizontal_scrollbar::on_bar(const point& coordinate) const
80{
81 // Not on the widget, leave.
82 if(static_cast<std::size_t>(coordinate.x) > get_width()
83 || static_cast<std::size_t>(coordinate.y) > get_height()) {
84 return 0;
85 }
86
87 // we also assume the bar is over the entire width of the widget.
88 if(static_cast<std::size_t>(coordinate.x) < get_positioner_offset()) {
89 return -1;
90 } else if(static_cast<std::size_t>(coordinate.x) > get_positioner_offset()
92
93 return 1;
94 } else {
95 return 0;
96 }
97}
98
100{
101 return static_cast<std::size_t>(coordinate.x) < get_width();
102}
103
104// }---------- DEFINITION ---------{
105
107 const config& cfg)
109{
110 DBG_GUI_P << "Parsing horizontal scrollbar " << id;
111
112 load_resolutions<resolution>(cfg);
113}
114
117 , minimum_positioner_length(cfg["minimum_positioner_length"].to_unsigned())
118 , maximum_positioner_length(cfg["maximum_positioner_length"].to_unsigned())
119 , left_offset(cfg["left_offset"].to_unsigned())
120 , right_offset(cfg["right_offset"].to_unsigned())
121{
123 missing_mandatory_wml_key("resolution",
124 "minimum_positioner_length"));
125
126 // Note the order should be the same as the enum state_t is scrollbar.hpp.
127 state.emplace_back(VALIDATE_WML_CHILD(cfg, "state_enabled", missing_mandatory_wml_tag("horizontal_scrollbar_definition][resolution", "state_enabled")));
128 state.emplace_back(VALIDATE_WML_CHILD(cfg, "state_disabled", missing_mandatory_wml_tag("horizontal_scrollbar_definition][resolution", "state_disabled")));
129 state.emplace_back(VALIDATE_WML_CHILD(cfg, "state_pressed", missing_mandatory_wml_tag("horizontal_scrollbar_definition][resolution", "state_pressed")));
130 state.emplace_back(VALIDATE_WML_CHILD(cfg, "state_focused", missing_mandatory_wml_tag("horizontal_scrollbar_definition][resolution", "state_focused")));
131}
132
133// }---------- BUILDER -----------{
134
135namespace implementation
136{
137
138builder_horizontal_scrollbar::builder_horizontal_scrollbar(const config& cfg)
140{
141}
142
143std::unique_ptr<widget> builder_horizontal_scrollbar::build() const
144{
145 auto widget = std::make_unique<horizontal_scrollbar>(*this);
146
147 widget->finalize_setup();
148
149 DBG_GUI_G << "Window builder:"
150 << " placed horizontal scrollbar '" << id << "' with definition '"
151 << definition << "'.";
152
153 return widget;
154}
155
156} // namespace implementation
157
158// }------------ END --------------
159
160} // namespace gui2
A config object defines a single node in a WML file, with access to child nodes.
Definition config.hpp:157
virtual bool in_orthogonal_range(const point &coordinate) const override
Inherited from scrollbar_base.
virtual unsigned minimum_positioner_length() const override
Inherited from scrollbar_base.
virtual int on_bar(const point &coordinate) const override
Inherited from scrollbar_base.
virtual unsigned offset_before() const override
Inherited from scrollbar_base.
virtual bool on_positioner(const point &coordinate) const override
Inherited from scrollbar_base.
virtual unsigned maximum_positioner_length() const override
Inherited from scrollbar_base.
virtual unsigned offset_after() const override
Inherited from scrollbar_base.
Base class for a scroll bar.
Definition scrollbar.hpp:39
unsigned get_positioner_offset() const
unsigned get_positioner_length() const
Base class for all widgets.
Definition widget.hpp:55
unsigned get_width() const
Definition widget.cpp:336
unsigned get_height() const
Definition widget.cpp:341
const config * cfg
#define DBG_GUI_G
Definition log.hpp:41
#define DBG_GUI_P
Definition log.hpp:66
Generic file dialog.
Contains the implementation details for lexical_cast and shouldn't be used directly.
#define REGISTER_WIDGET(id)
Wrapper for REGISTER_WIDGET3.
virtual std::unique_ptr< widget > build() const override
std::string definition
Parameters for the styled_widget.
std::vector< state_definition > state
Holds a 2D point.
Definition point.hpp:25
An abstract description of a rectangle with integer coordinates.
Definition rect.hpp:49
bool contains(int x, int y) const
Whether the given point lies within the rectangle.
Definition rect.cpp:54
std::string missing_mandatory_wml_tag(const std::string &section, const std::string &tag)
Returns a standard message for a missing wml child (tag).
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 (attribute).
Add a special kind of assert to validate whether the input from WML doesn't contain any problems that...
#define VALIDATE_WML_CHILD(cfg, key, message)
#define VALIDATE(cond, message)
The macro to use for the validation of WML.