The Battle for Wesnoth  1.15.12+dev
horizontal_scrollbar.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2018 by Mark de Wever <koraq@xs4all.nl>
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #define GETTEXT_DOMAIN "wesnoth-lib"
16 
18 
22 
23 #include "gui/widgets/settings.hpp"
24 
25 #include "wml_exception.hpp"
26 
27 #include <functional>
28 
29 namespace gui2
30 {
31 
32 // ------------ WIDGET -----------{
33 
34 REGISTER_WIDGET(horizontal_scrollbar)
35 
36 horizontal_scrollbar::horizontal_scrollbar(const implementation::builder_horizontal_scrollbar& builder)
37  : scrollbar_base(builder, type())
38 {
39 }
40 
42 {
43  const auto conf = cast_config_to<horizontal_scrollbar_definition>();
44  assert(conf);
45 
46  return conf->minimum_positioner_length;
47 }
48 
50 {
51  const auto conf = cast_config_to<horizontal_scrollbar_definition>();
52  assert(conf);
53 
54  return conf->maximum_positioner_length;
55 }
56 
58 {
59  const auto conf = cast_config_to<horizontal_scrollbar_definition>();
60  assert(conf);
61 
62  return conf->left_offset;
63 }
64 
66 {
67  const auto conf = cast_config_to<horizontal_scrollbar_definition>();
68  assert(conf);
69 
70  return conf->right_offset;
71 }
72 
74 {
75  SDL_Rect positioner_rect =
77 
78  // Note we assume the positioner is over the entire height of the widget.
79  return sdl::point_in_rect(coordinate, positioner_rect);
80 }
81 
83 {
84  // Not on the widget, leave.
85  if(static_cast<std::size_t>(coordinate.x) > get_width()
86  || static_cast<std::size_t>(coordinate.y) > get_height()) {
87  return 0;
88  }
89 
90  // we also assume the bar is over the entire width of the widget.
91  if(static_cast<std::size_t>(coordinate.x) < get_positioner_offset()) {
92  return -1;
93  } else if(static_cast<std::size_t>(coordinate.x) > get_positioner_offset()
95 
96  return 1;
97  } else {
98  return 0;
99  }
100 }
101 
103 {
104  return static_cast<std::size_t>(coordinate.x) < get_width();
105 }
106 
107 // }---------- DEFINITION ---------{
108 
110  const config& cfg)
112 {
113  DBG_GUI_P << "Parsing horizontal scrollbar " << id << '\n';
114 
115  load_resolutions<resolution>(cfg);
116 }
117 
119  : resolution_definition(cfg)
120  , minimum_positioner_length(cfg["minimum_positioner_length"])
121  , maximum_positioner_length(cfg["maximum_positioner_length"])
122  , left_offset(cfg["left_offset"])
123  , right_offset(cfg["right_offset"])
124 {
126  missing_mandatory_wml_key("resolution",
127  "minimum_positioner_length"));
128 
129  // Note the order should be the same as the enum state_t is scrollbar.hpp.
130  state.emplace_back(cfg.child("state_enabled"));
131  state.emplace_back(cfg.child("state_disabled"));
132  state.emplace_back(cfg.child("state_pressed"));
133  state.emplace_back(cfg.child("state_focused"));
134 }
135 
136 // }---------- BUILDER -----------{
137 
138 namespace implementation
139 {
140 
141 builder_horizontal_scrollbar::builder_horizontal_scrollbar(const config& cfg)
142  : builder_styled_widget(cfg)
143 {
144 }
145 
147 {
149 
150  widget->finalize_setup();
151 
152  DBG_GUI_G << "Window builder:"
153  << " placed horizontal scrollbar '" << id << "' with definition '"
154  << definition << "'.\n";
155 
156  return widget;
157 }
158 
159 } // namespace implementation
160 
161 // }------------ END --------------
162 
163 } // namespace gui2
Base class for a scroll bar.
Definition: scrollbar.hpp:40
Base class of a resolution, contains the common keys for a resolution.
#define DBG_GUI_P
Definition: log.hpp:65
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:414
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:329
unsigned get_width() const
Definition: widget.cpp:324
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:44
Generic file dialog.
Definition: field-fwd.hpp:22
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:184
#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:22
virtual bool in_orthogonal_range(const point &coordinate) const override
Inherited from scrollbar_base.
Holds a 2D point.
Definition: point.hpp:23
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:39
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:59
int y
y coordinate.
Definition: point.hpp:47
unsigned get_positioner_length() const
Definition: scrollbar.hpp:189
#define DBG_GUI_G
Definition: log.hpp:40
Contains the implementation details for lexical_cast and shouldn&#39;t be used directly.