The Battle for Wesnoth  1.17.0-dev
vertical_scrollbar.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2021
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  SDL_Rect positioner_rect =
69 
70  // Note we assume the positioner is over the entire height of the widget.
71  return sdl::point_in_rect(coordinate, positioner_rect);
72 }
73 
75 {
76  // Not on the widget, leave.
77  if(static_cast<std::size_t>(coordinate.x) > get_width()
78  || static_cast<std::size_t>(coordinate.y) > get_height()) {
79  return 0;
80  }
81 
82  // we also assume the bar is over the entire width of the widget.
83  if(static_cast<std::size_t>(coordinate.y) < get_positioner_offset()) {
84  return -1;
85  } else if(static_cast<std::size_t>(coordinate.y) > get_positioner_offset()
87  return 1;
88  } else {
89  return 0;
90  }
91 }
92 
94 {
95  return static_cast<std::size_t>(coordinate.y) < get_height();
96 }
97 
98 // }---------- DEFINITION ---------{
99 
101  const config& cfg)
103 {
104  DBG_GUI_P << "Parsing vertical scrollbar " << id << '\n';
105 
106  load_resolutions<resolution>(cfg);
107 }
108 
110  : resolution_definition(cfg)
111  , minimum_positioner_length(cfg["minimum_positioner_length"])
112  , maximum_positioner_length(cfg["maximum_positioner_length"])
113  , top_offset(cfg["top_offset"])
114  , bottom_offset(cfg["bottom_offset"])
115 {
117  missing_mandatory_wml_key("resolution",
118  "minimum_positioner_length"));
119 
120  // Note the order should be the same as the enum state_t in scrollbar.hpp.
121  state.emplace_back(cfg.child("state_enabled"));
122  state.emplace_back(cfg.child("state_disabled"));
123  state.emplace_back(cfg.child("state_pressed"));
124  state.emplace_back(cfg.child("state_focused"));
125 }
126 
127 // }---------- BUILDER -----------{
128 
129 namespace implementation
130 {
131 
132 builder_vertical_scrollbar::builder_vertical_scrollbar(const config& cfg)
133  : builder_styled_widget(cfg)
134 {
135 }
136 
138 {
140 
141  widget->finalize_setup();
142 
143  DBG_GUI_G << "Window builder:"
144  << " placed vertical scrollbar '" << id << "' with definition '"
145  << definition << "'.\n";
146 
147  return widget;
148 }
149 
150 } // namespace implementation
151 
152 // }------------ END --------------
153 
154 } // namespace gui2
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:49
unsigned get_height() const
Definition: widget.cpp:330
virtual unsigned offset_after() const override
Inherited from scrollbar_base.
unsigned get_width() const
Definition: widget.cpp:325
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.
int x
x coordinate.
Definition: point.hpp:45
Generic file dialog.
Definition: field-fwd.hpp:23
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.
bool point_in_rect(int x, int y, const SDL_Rect &rect)
Tests whether a point is inside a rectangle.
Definition: rect.cpp:23
virtual int on_bar(const point &coordinate) const override
Inherited from scrollbar_base.
Holds a 2D point.
Definition: point.hpp:24
SDL_Rect create_rect(const int x, const int y, const int w, const int h)
Creates an SDL_Rect with the given dimensions.
Definition: rect.hpp:40
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:61
int y
y coordinate.
Definition: point.hpp:48
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.