The Battle for Wesnoth  1.17.10+dev
vertical_scrollbar.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2022
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 
23 #include <functional>
24 
25 namespace gui2
26 {
27 
28 // ------------ WIDGET -----------{
29 
30 REGISTER_WIDGET(vertical_scrollbar)
31 
32 vertical_scrollbar::vertical_scrollbar(const implementation::builder_vertical_scrollbar& builder)
33  : scrollbar_base(builder, type())
34 {
35 }
36 
38 {
39  const auto conf = cast_config_to<vertical_scrollbar_definition>();
40  assert(conf);
41  return conf->minimum_positioner_length;
42 }
43 
45 {
46  const auto conf = cast_config_to<vertical_scrollbar_definition>();
47  assert(conf);
48  return conf->maximum_positioner_length;
49 }
50 
52 {
53  const auto conf = cast_config_to<vertical_scrollbar_definition>();
54  assert(conf);
55  return conf->top_offset;
56 }
57 
59 {
60  const auto conf = cast_config_to<vertical_scrollbar_definition>();
61  assert(conf);
62  return conf->bottom_offset;
63 }
64 
66 {
67  rect positioner_rect(
69  );
70 
71  // Note we assume the positioner is over the entire height of the widget.
72  return positioner_rect.contains(coordinate);
73 }
74 
76 {
77  // Not on the widget, leave.
78  if(static_cast<std::size_t>(coordinate.x) > get_width()
79  || static_cast<std::size_t>(coordinate.y) > get_height()) {
80  return 0;
81  }
82 
83  // we also assume the bar is over the entire width of the widget.
84  if(static_cast<std::size_t>(coordinate.y) < get_positioner_offset()) {
85  return -1;
86  } else if(static_cast<std::size_t>(coordinate.y) > get_positioner_offset()
88  return 1;
89  } else {
90  return 0;
91  }
92 }
93 
95 {
96  return static_cast<std::size_t>(coordinate.y) < get_height();
97 }
98 
99 // }---------- DEFINITION ---------{
100 
102  const config& cfg)
104 {
105  DBG_GUI_P << "Parsing vertical scrollbar " << id;
106 
107  load_resolutions<resolution>(cfg);
108 }
109 
111  : resolution_definition(cfg)
112  , minimum_positioner_length(cfg["minimum_positioner_length"])
113  , maximum_positioner_length(cfg["maximum_positioner_length"])
114  , top_offset(cfg["top_offset"])
115  , bottom_offset(cfg["bottom_offset"])
116 {
118  missing_mandatory_wml_key("resolution",
119  "minimum_positioner_length"));
120 
121  // Note the order should be the same as the enum state_t in scrollbar.hpp.
122  state.emplace_back(cfg.child("state_enabled"));
123  state.emplace_back(cfg.child("state_disabled"));
124  state.emplace_back(cfg.child("state_pressed"));
125  state.emplace_back(cfg.child("state_focused"));
126 }
127 
128 // }---------- BUILDER -----------{
129 
130 namespace implementation
131 {
132 
133 builder_vertical_scrollbar::builder_vertical_scrollbar(const config& cfg)
134  : builder_styled_widget(cfg)
135 {
136 }
137 
138 std::unique_ptr<widget> builder_vertical_scrollbar::build() const
139 {
140  auto widget = std::make_unique<vertical_scrollbar>(*this);
141 
142  widget->finalize_setup();
143 
144  DBG_GUI_G << "Window builder:"
145  << " placed vertical scrollbar '" << id << "' with definition '"
146  << definition << "'.";
147 
148  return widget;
149 }
150 
151 } // namespace implementation
152 
153 // }------------ END --------------
154 
155 } // namespace gui2
virtual std::unique_ptr< widget > build() const override
Base class for a scroll bar.
Definition: scrollbar.hpp:41
Base class of a resolution, contains the common keys for a resolution.
#define DBG_GUI_P
Definition: log.hpp:66
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:402
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:53
unsigned get_height() const
Definition: widget.cpp:331
virtual unsigned offset_after() const override
Inherited from scrollbar_base.
bool contains(int x, int y) const
Whether the given point lies within the rectangle.
Definition: rect.cpp:54
unsigned get_width() const
Definition: widget.cpp:326
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.
Generic file dialog.
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:185
virtual unsigned offset_before() const override
Inherited from scrollbar_base.
#define REGISTER_WIDGET(id)
Wrapper for REGISTER_WIDGET3.
virtual int on_bar(const point &coordinate) const override
Inherited from scrollbar_base.
An abstract description of a rectangle with integer coordinates.
Definition: rect.hpp:46
Holds a 2D point.
Definition: point.hpp:24
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:60
unsigned get_positioner_length() const
Definition: scrollbar.hpp:190
#define DBG_GUI_G
Definition: log.hpp:41
Contains the implementation details for lexical_cast and shouldn&#39;t be used directly.