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 "gui/widgets/settings.hpp"
21 #include "wml_exception.hpp"
22 #include "gettext.hpp"
23 
24 #include <functional>
25 
26 namespace gui2
27 {
28 
29 // ------------ WIDGET -----------{
30 
31 REGISTER_WIDGET(vertical_scrollbar)
32 
33 vertical_scrollbar::vertical_scrollbar(const implementation::builder_vertical_scrollbar& builder)
34  : scrollbar_base(builder, type())
35 {
36 }
37 
39 {
40  const auto conf = cast_config_to<vertical_scrollbar_definition>();
41  assert(conf);
42  return conf->minimum_positioner_length;
43 }
44 
46 {
47  const auto conf = cast_config_to<vertical_scrollbar_definition>();
48  assert(conf);
49  return conf->maximum_positioner_length;
50 }
51 
53 {
54  const auto conf = cast_config_to<vertical_scrollbar_definition>();
55  assert(conf);
56  return conf->top_offset;
57 }
58 
60 {
61  const auto conf = cast_config_to<vertical_scrollbar_definition>();
62  assert(conf);
63  return conf->bottom_offset;
64 }
65 
67 {
68  rect positioner_rect(
70  );
71 
72  // Note we assume the positioner is over the entire height of the widget.
73  return positioner_rect.contains(coordinate);
74 }
75 
77 {
78  // Not on the widget, leave.
79  if(static_cast<std::size_t>(coordinate.x) > get_width()
80  || static_cast<std::size_t>(coordinate.y) > get_height()) {
81  return 0;
82  }
83 
84  // we also assume the bar is over the entire width of the widget.
85  if(static_cast<std::size_t>(coordinate.y) < get_positioner_offset()) {
86  return -1;
87  } else if(static_cast<std::size_t>(coordinate.y) > get_positioner_offset()
89  return 1;
90  } else {
91  return 0;
92  }
93 }
94 
96 {
97  return static_cast<std::size_t>(coordinate.y) < get_height();
98 }
99 
100 // }---------- DEFINITION ---------{
101 
103  const config& cfg)
105 {
106  DBG_GUI_P << "Parsing vertical scrollbar " << id;
107 
108  load_resolutions<resolution>(cfg);
109 }
110 
112  : resolution_definition(cfg)
113  , minimum_positioner_length(cfg["minimum_positioner_length"])
114  , maximum_positioner_length(cfg["maximum_positioner_length"])
115  , top_offset(cfg["top_offset"])
116  , bottom_offset(cfg["bottom_offset"])
117 {
119  missing_mandatory_wml_key("resolution",
120  "minimum_positioner_length"));
121 
122  // Note the order should be the same as the enum state_t in scrollbar.hpp.
123  state.emplace_back(VALIDATE_WML_CHILD(cfg, "state_enabled", _("Missing required state for vertical scrollbar")));
124  state.emplace_back(VALIDATE_WML_CHILD(cfg, "state_disabled", _("Missing required state for vertical scrollbar")));
125  state.emplace_back(VALIDATE_WML_CHILD(cfg, "state_pressed", _("Missing required state for vertical scrollbar")));
126  state.emplace_back(VALIDATE_WML_CHILD(cfg, "state_focused", _("Missing required state for vertical scrollbar")));
127 }
128 
129 // }---------- BUILDER -----------{
130 
131 namespace implementation
132 {
133 
134 builder_vertical_scrollbar::builder_vertical_scrollbar(const config& cfg)
135  : builder_styled_widget(cfg)
136 {
137 }
138 
139 std::unique_ptr<widget> builder_vertical_scrollbar::build() const
140 {
141  auto widget = std::make_unique<vertical_scrollbar>(*this);
142 
143  widget->finalize_setup();
144 
145  DBG_GUI_G << "Window builder:"
146  << " placed vertical scrollbar '" << id << "' with definition '"
147  << definition << "'.";
148 
149  return widget;
150 }
151 
152 } // namespace implementation
153 
154 // }------------ END --------------
155 
156 } // 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:42
unsigned get_positioner_offset() const
Definition: scrollbar.hpp:182
unsigned get_positioner_length() const
Definition: scrollbar.hpp:190
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: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.
This file contains the settings handling of the widget library.
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: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.