The Battle for Wesnoth  1.19.0-dev
vertical_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 
20 #include "wml_exception.hpp"
21 
22 
23 namespace gui2
24 {
25 
26 // ------------ WIDGET -----------{
27 
28 REGISTER_WIDGET(vertical_scrollbar)
29 
30 vertical_scrollbar::vertical_scrollbar(const implementation::builder_vertical_scrollbar& builder)
31  : scrollbar_base(builder, type())
32 {
33 }
34 
36 {
37  const auto conf = cast_config_to<vertical_scrollbar_definition>();
38  assert(conf);
39  return conf->minimum_positioner_length;
40 }
41 
43 {
44  const auto conf = cast_config_to<vertical_scrollbar_definition>();
45  assert(conf);
46  return conf->maximum_positioner_length;
47 }
48 
50 {
51  const auto conf = cast_config_to<vertical_scrollbar_definition>();
52  assert(conf);
53  return conf->top_offset;
54 }
55 
57 {
58  const auto conf = cast_config_to<vertical_scrollbar_definition>();
59  assert(conf);
60  return conf->bottom_offset;
61 }
62 
64 {
65  rect positioner_rect(
67  );
68 
69  // Note we assume the positioner is over the entire height of the widget.
70  return positioner_rect.contains(coordinate);
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;
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(VALIDATE_WML_CHILD(cfg, "state_enabled", missing_mandatory_wml_tag("scrollbar_definition][resolution", "state_enabled")));
121  state.emplace_back(VALIDATE_WML_CHILD(cfg, "state_disabled", missing_mandatory_wml_tag("scrollbar_definition][resolution", "state_disabled")));
122  state.emplace_back(VALIDATE_WML_CHILD(cfg, "state_pressed", missing_mandatory_wml_tag("scrollbar_definition][resolution", "state_pressed")));
123  state.emplace_back(VALIDATE_WML_CHILD(cfg, "state_focused", missing_mandatory_wml_tag("scrollbar_definition][resolution", "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 
136 std::unique_ptr<widget> builder_vertical_scrollbar::build() const
137 {
138  auto widget = std::make_unique<vertical_scrollbar>(*this);
139 
140  widget->finalize_setup();
141 
142  DBG_GUI_G << "Window builder:"
143  << " placed vertical scrollbar '" << id << "' with definition '"
144  << definition << "'.";
145 
146  return widget;
147 }
148 
149 } // namespace implementation
150 
151 // }------------ END --------------
152 
153 } // namespace gui2
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:159
Base class for a scroll bar.
Definition: scrollbar.hpp:39
unsigned get_positioner_offset() const
Definition: scrollbar.hpp:179
unsigned get_positioner_length() const
Definition: scrollbar.hpp:187
The definition of a vertical scrollbar.
virtual int on_bar(const point &coordinate) const override
Inherited from scrollbar_base.
virtual unsigned maximum_positioner_length() const override
Inherited from scrollbar_base.
virtual bool on_positioner(const point &coordinate) const override
Inherited from scrollbar_base.
virtual bool in_orthogonal_range(const point &coordinate) const override
Inherited from scrollbar_base.
virtual unsigned offset_after() const override
Inherited from scrollbar_base.
virtual unsigned offset_before() const override
Inherited from scrollbar_base.
virtual unsigned minimum_positioner_length() const override
Inherited from scrollbar_base.
Base class for all widgets.
Definition: widget.hpp:53
unsigned get_width() const
Definition: widget.cpp:327
unsigned get_height() const
Definition: widget.cpp:332
#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.
std::string definition
Parameters for the styled_widget.
virtual std::unique_ptr< widget > build() const override
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:52
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.