The Battle for Wesnoth  1.17.0-dev
horizontal_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 
19 
23 
24 #include "gui/widgets/settings.hpp"
25 
26 #include "wml_exception.hpp"
27 
28 #include <functional>
29 
30 namespace gui2
31 {
32 
33 // ------------ WIDGET -----------{
34 
35 REGISTER_WIDGET(horizontal_scrollbar)
36 
37 horizontal_scrollbar::horizontal_scrollbar(const implementation::builder_horizontal_scrollbar& builder)
38  : scrollbar_base(builder, type())
39 {
40 }
41 
43 {
44  const auto conf = cast_config_to<horizontal_scrollbar_definition>();
45  assert(conf);
46 
47  return conf->minimum_positioner_length;
48 }
49 
51 {
52  const auto conf = cast_config_to<horizontal_scrollbar_definition>();
53  assert(conf);
54 
55  return conf->maximum_positioner_length;
56 }
57 
59 {
60  const auto conf = cast_config_to<horizontal_scrollbar_definition>();
61  assert(conf);
62 
63  return conf->left_offset;
64 }
65 
67 {
68  const auto conf = cast_config_to<horizontal_scrollbar_definition>();
69  assert(conf);
70 
71  return conf->right_offset;
72 }
73 
75 {
76  SDL_Rect positioner_rect =
78 
79  // Note we assume the positioner is over the entire height of the widget.
80  return sdl::point_in_rect(coordinate, positioner_rect);
81 }
82 
84 {
85  // Not on the widget, leave.
86  if(static_cast<std::size_t>(coordinate.x) > get_width()
87  || static_cast<std::size_t>(coordinate.y) > get_height()) {
88  return 0;
89  }
90 
91  // we also assume the bar is over the entire width of the widget.
92  if(static_cast<std::size_t>(coordinate.x) < get_positioner_offset()) {
93  return -1;
94  } else if(static_cast<std::size_t>(coordinate.x) > get_positioner_offset()
96 
97  return 1;
98  } else {
99  return 0;
100  }
101 }
102 
104 {
105  return static_cast<std::size_t>(coordinate.x) < get_width();
106 }
107 
108 // }---------- DEFINITION ---------{
109 
111  const config& cfg)
113 {
114  DBG_GUI_P << "Parsing horizontal scrollbar " << id << '\n';
115 
116  load_resolutions<resolution>(cfg);
117 }
118 
120  : resolution_definition(cfg)
121  , minimum_positioner_length(cfg["minimum_positioner_length"])
122  , maximum_positioner_length(cfg["maximum_positioner_length"])
123  , left_offset(cfg["left_offset"])
124  , right_offset(cfg["right_offset"])
125 {
127  missing_mandatory_wml_key("resolution",
128  "minimum_positioner_length"));
129 
130  // Note the order should be the same as the enum state_t is scrollbar.hpp.
131  state.emplace_back(cfg.child("state_enabled"));
132  state.emplace_back(cfg.child("state_disabled"));
133  state.emplace_back(cfg.child("state_pressed"));
134  state.emplace_back(cfg.child("state_focused"));
135 }
136 
137 // }---------- BUILDER -----------{
138 
139 namespace implementation
140 {
141 
142 builder_horizontal_scrollbar::builder_horizontal_scrollbar(const config& cfg)
143  : builder_styled_widget(cfg)
144 {
145 }
146 
148 {
150 
151  widget->finalize_setup();
152 
153  DBG_GUI_G << "Window builder:"
154  << " placed horizontal scrollbar '" << id << "' with definition '"
155  << definition << "'.\n";
156 
157  return widget;
158 }
159 
160 } // namespace implementation
161 
162 // }------------ END --------------
163 
164 } // 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
A horizontal scrollbar is a widget that shows a horizontal scrollbar.
virtual unsigned minimum_positioner_length() const override
Inherited from scrollbar_base.
virtual unsigned offset_before() 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
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 unsigned offset_after() const override
Inherited from scrollbar_base.
This file contains the settings handling of the widget library.
virtual int on_bar(const point &coordinate) const override
Inherited from scrollbar_base.
unsigned get_positioner_offset() const
Definition: scrollbar.hpp:185
#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 bool in_orthogonal_range(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 bool on_positioner(const point &coordinate) const override
Inherited from scrollbar_base.
virtual unsigned maximum_positioner_length() const override
Inherited from scrollbar_base.
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.