The Battle for Wesnoth  1.19.0-dev
horizontal_scrollbar.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2024
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 #include "gettext.hpp"
25 
26 namespace gui2
27 {
28 
29 // ------------ WIDGET -----------{
30 
31 REGISTER_WIDGET(horizontal_scrollbar)
32 
33 horizontal_scrollbar::horizontal_scrollbar(const implementation::builder_horizontal_scrollbar& builder)
34  : scrollbar_base(builder, type())
35 {
36 }
37 
39 {
40  const auto conf = cast_config_to<horizontal_scrollbar_definition>();
41  assert(conf);
42 
43  return conf->minimum_positioner_length;
44 }
45 
47 {
48  const auto conf = cast_config_to<horizontal_scrollbar_definition>();
49  assert(conf);
50 
51  return conf->maximum_positioner_length;
52 }
53 
55 {
56  const auto conf = cast_config_to<horizontal_scrollbar_definition>();
57  assert(conf);
58 
59  return conf->left_offset;
60 }
61 
63 {
64  const auto conf = cast_config_to<horizontal_scrollbar_definition>();
65  assert(conf);
66 
67  return conf->right_offset;
68 }
69 
71 {
72  rect positioner_rect(
74  );
75 
76  // Note we assume the positioner is over the entire height of the widget.
77  return positioner_rect.contains(coordinate);
78 }
79 
81 {
82  // Not on the widget, leave.
83  if(static_cast<std::size_t>(coordinate.x) > get_width()
84  || static_cast<std::size_t>(coordinate.y) > get_height()) {
85  return 0;
86  }
87 
88  // we also assume the bar is over the entire width of the widget.
89  if(static_cast<std::size_t>(coordinate.x) < get_positioner_offset()) {
90  return -1;
91  } else if(static_cast<std::size_t>(coordinate.x) > get_positioner_offset()
93 
94  return 1;
95  } else {
96  return 0;
97  }
98 }
99 
101 {
102  return static_cast<std::size_t>(coordinate.x) < get_width();
103 }
104 
105 // }---------- DEFINITION ---------{
106 
108  const config& cfg)
110 {
111  DBG_GUI_P << "Parsing horizontal scrollbar " << id;
112 
113  load_resolutions<resolution>(cfg);
114 }
115 
117  : resolution_definition(cfg)
118  , minimum_positioner_length(cfg["minimum_positioner_length"])
119  , maximum_positioner_length(cfg["maximum_positioner_length"])
120  , left_offset(cfg["left_offset"])
121  , right_offset(cfg["right_offset"])
122 {
124  missing_mandatory_wml_key("resolution",
125  "minimum_positioner_length"));
126 
127  // Note the order should be the same as the enum state_t is scrollbar.hpp.
128  state.emplace_back(VALIDATE_WML_CHILD(cfg, "state_enabled", _("Missing required state for horizontal scrollbar")));
129  state.emplace_back(VALIDATE_WML_CHILD(cfg, "state_disabled", _("Missing required state for horizontal scrollbar")));
130  state.emplace_back(VALIDATE_WML_CHILD(cfg, "state_pressed", _("Missing required state for horizontal scrollbar")));
131  state.emplace_back(VALIDATE_WML_CHILD(cfg, "state_focused", _("Missing required state for horizontal scrollbar")));
132 }
133 
134 // }---------- BUILDER -----------{
135 
136 namespace implementation
137 {
138 
139 builder_horizontal_scrollbar::builder_horizontal_scrollbar(const config& cfg)
140  : builder_styled_widget(cfg)
141 {
142 }
143 
144 std::unique_ptr<widget> builder_horizontal_scrollbar::build() const
145 {
146  auto widget = std::make_unique<horizontal_scrollbar>(*this);
147 
148  widget->finalize_setup();
149 
150  DBG_GUI_G << "Window builder:"
151  << " placed horizontal scrollbar '" << id << "' with definition '"
152  << definition << "'.";
153 
154  return widget;
155 }
156 
157 } // namespace implementation
158 
159 // }------------ END --------------
160 
161 } // namespace gui2
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:159
A horizontal scrollbar is a widget that shows a horizontal scrollbar.
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:42
unsigned get_positioner_offset() const
Definition: scrollbar.hpp:182
unsigned get_positioner_length() const
Definition: scrollbar.hpp:190
Base class for all widgets.
Definition: widget.hpp:54
unsigned get_width() const
Definition: widget.cpp:328
unsigned get_height() const
Definition: widget.cpp:333
static std::string _(const char *str)
Definition: gettext.hpp:93
#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.
map_location coordinate
Contains an x and y coordinate used for starting positions in maps.
#define REGISTER_WIDGET(id)
Wrapper for REGISTER_WIDGET3.
virtual std::unique_ptr< widget > build() const override
std::string definition
Parameters for the styled_widget.
Base class of a resolution, contains the common keys for a resolution.
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:47
bool contains(int x, int y) const
Whether the given point lies within the rectangle.
Definition: rect.cpp:54
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.