The Battle for Wesnoth  1.15.2+dev
panel.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 
17 #include "gui/widgets/panel.hpp"
18 
19 #include "gui/core/log.hpp"
21 #include "gui/widgets/settings.hpp"
22 #include "gettext.hpp"
23 #include "sdl/rect.hpp"
24 #include "wml_exception.hpp"
25 
26 #include "utils/functional.hpp"
27 
28 #define LOG_SCOPE_HEADER get_control_type() + " [" + id() + "] " + __func__
29 #define LOG_HEADER LOG_SCOPE_HEADER + ':'
30 
31 namespace gui2
32 {
33 
34 // ------------ WIDGET -----------{
35 
36 REGISTER_WIDGET(panel)
37 
38 panel::panel(const implementation::builder_styled_widget& builder, const std::string& control_type)
39  : container_base(builder, control_type.empty() ? type() : control_type)
40 {
41 }
42 
43 SDL_Rect panel::get_client_rect() const
44 {
45  const auto conf = cast_config_to<panel_definition>();
46  assert(conf);
47 
48  SDL_Rect result = get_rectangle();
49  result.x += conf->left_border;
50  result.y += conf->top_border;
51  result.w -= conf->left_border + conf->right_border;
52  result.h -= conf->top_border + conf->bottom_border;
53 
54  return result;
55 }
56 
57 bool panel::get_active() const
58 {
59  return true;
60 }
61 
62 unsigned panel::get_state() const
63 {
64  return 0;
65 }
66 
67 void panel::impl_draw_background(surface& frame_buffer, int x_offset, int y_offset)
68 {
69  DBG_GUI_D << LOG_HEADER << " size " << get_rectangle() << ".\n";
70 
71  get_canvas(0).blit(frame_buffer,
72  calculate_blitting_rectangle(x_offset, y_offset));
73 }
74 
75 void panel::impl_draw_foreground(surface& frame_buffer, int x_offset, int y_offset)
76 {
77  DBG_GUI_D << LOG_HEADER << " size " << get_rectangle() << ".\n";
78 
79  get_canvas(1).blit(frame_buffer,
80  calculate_blitting_rectangle(x_offset, y_offset));
81 }
82 
84 {
85  const auto conf = cast_config_to<panel_definition>();
86  assert(conf);
87 
88  return point(conf->left_border + conf->right_border, conf->top_border + conf->bottom_border);
89 }
90 
91 void panel::set_self_active(const bool /*active*/)
92 {
93  /* DO NOTHING */
94 }
95 
96 // }---------- DEFINITION ---------{
97 
100 {
101  DBG_GUI_P << "Parsing panel " << id << '\n';
102 
103  load_resolutions<resolution>(cfg);
104 }
105 
106 /*WIKI
107  * @page = GUIWidgetDefinitionWML
108  * @order = 1_panel
109  *
110  * == Panel ==
111  *
112  * @macro = panel_description
113  *
114  * @begin{parent}{name="gui/"}
115  * @begin{tag}{name="panel_definition"}{min=0}{max=-1}{super="generic/widget_definition"}
116  * A panel is always enabled and can't be disabled. Instead it uses the
117  * states as layers to draw on.
118  * @begin{tag}{name="resolution"}{min=0}{max=-1}{super="generic/widget_definition/resolution"}
119  * The resolution for a panel also contains the following keys:
120  * @begin{table}{config}
121  * top_border & unsigned & 0 & The size which isn't used for the client
122  * area. $
123  * bottom_border & unsigned & 0 & The size which isn't used for the client
124  * area. $
125  * left_border & unsigned & 0 & The size which isn't used for the client
126  * area. $
127  * right_border & unsigned & 0 & The size which isn't used for the client
128  * area. $
129  * @end{table}
130  *
131  * The following layers exist:
132  * * background, the background of the panel.
133  * * foreground, the foreground of the panel.
134  * @begin{tag}{name="foreground"}{min=0}{max=1}
135  * @allow{link}{name="generic/state/draw"}
136  * @end{tag}{name="foreground"}
137  * @begin{tag}{name="background"}{min=0}{max=1}
138  * @allow{link}{name="generic/state/draw"}
139  * @end{tag}{name="background"}
140  * @end{tag}{name="resolution"}
141  * @end{tag}{name="panel_definition"}
142  * @end{parent}{name="gui/"}
143  */
145  : resolution_definition(cfg)
146  , top_border(cfg["top_border"])
147  , bottom_border(cfg["bottom_border"])
148  , left_border(cfg["left_border"])
149  , right_border(cfg["right_border"])
150 {
151  // The panel needs to know the order.
152  state.emplace_back(cfg.child("background"));
153  state.emplace_back(cfg.child("foreground"));
154 }
155 
156 // }---------- BUILDER -----------{
157 
158 /*WIKI_MACRO
159  * @begin{macro}{panel_description}
160  *
161  * A panel is an item which can hold other items. The difference
162  * between a grid and a panel is that it's possible to define how a
163  * panel looks. A grid in an invisible container to just hold the
164  * items.
165  * @end{macro}
166  */
167 
168 /*WIKI
169  * @page = GUIWidgetInstanceWML
170  * @order = 2_panel
171  * @begin{parent}{name="gui/window/resolution/grid/row/column/"}
172  * @begin{tag}{name="panel"}{min="0"}{max="-1"}{super="generic/widget_instance"}
173  * == Panel ==
174  *
175  * @macro = panel_description
176  *
177  * @begin{table}{config}
178  * grid & grid & & Defines the grid with the widgets to
179  * place on the panel. $
180  * @end{table}
181  * @allow{link}{name="gui/window/resolution/grid"}
182  * @end{tag}{name="panel"}
183  * @end{parent}{name="gui/window/resolution/grid/row/column/"}
184  */
185 
186 namespace implementation
187 {
188 
189 builder_panel::builder_panel(const config& cfg)
190  : builder_styled_widget(cfg), grid(nullptr)
191 {
192  const config& c = cfg.child("grid");
193 
194  VALIDATE(c, _("No grid defined."));
195 
196  grid = std::make_shared<builder_grid>(c);
197 }
198 
200 {
201  panel* widget = new panel(*this);
202 
203  DBG_GUI_G << "Window builder: placed panel '" << id << "' with definition '"
204  << definition << "'.\n";
205 
206  widget->init_grid(grid);
207  return widget;
208 }
209 
210 } // namespace implementation
211 
212 // }------------ END --------------
213 
214 } // namespace gui2
Define the common log macros for the gui toolkit.
Base class of a resolution, contains the common keys for a resolution.
#define DBG_GUI_P
Definition: log.hpp:68
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:420
std::vector< state_definition > state
SDL_Rect calculate_blitting_rectangle(const int x_offset, const int y_offset)
Calculates the blitting rectangle of the widget.
Definition: widget.cpp:349
Visible container to hold multiple widgets.
Definition: panel.hpp:37
Add a special kind of assert to validate whether the input from WML doesn&#39;t contain any problems that...
SDL_Rect get_rectangle() const
Gets the bounding rectangle of the widget on the screen.
Definition: widget.cpp:307
Base class for all widgets.
Definition: widget.hpp:47
virtual SDL_Rect get_client_rect() const override
See container_base::get_client_rect.
Definition: panel.cpp:43
virtual void impl_draw_foreground(surface &frame_buffer, int x_offset, int y_offset) override
See widget::impl_draw_foreground.
Definition: panel.cpp:75
STL namespace.
Generic file dialog.
Definition: field-fwd.hpp:22
virtual void impl_draw_background(surface &frame_buffer, int x_offset, int y_offset) override
See widget::impl_draw_background.
Definition: panel.cpp:67
virtual point border_space() const override
See container_base::border_space.
Definition: panel.cpp:83
#define DBG_GUI_D
Definition: log.hpp:28
Base container class.
Definition: grid.hpp:30
void blit(surface &surf, SDL_Rect rect)
Blits the canvas unto another surface.
Definition: canvas.cpp:1427
static UNUSEDNOWARN std::string _(const char *str)
Definition: gettext.hpp:91
std::string definition
Parameters for the styled_widget.
#define VALIDATE(cond, message)
The macro to use for the validation of WML.
This file contains the settings handling of the widget library.
#define LOG_HEADER
Definition: panel.cpp:29
void init_grid(const std::shared_ptr< builder_grid > &grid_builder)
Initializes and builds the grid.
resolution(const config &cfg)
Definition: panel.cpp:144
virtual unsigned get_state() const override
See styled_widget::get_state.
Definition: panel.cpp:62
#define REGISTER_WIDGET(id)
Wrapper for REGISTER_WIDGET3.
virtual void set_self_active(const bool active) override
See container_base::set_self_active.
Definition: panel.cpp:91
panel_definition(const config &cfg)
Definition: panel.cpp:98
Holds a 2D point.
Definition: point.hpp:23
A generic container base class.
Contains the SDL_Rect helper code.
virtual bool get_active() const override
See styled_widget::get_active.
Definition: panel.cpp:57
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:68
mock_char c
canvas & get_canvas(const unsigned index)
#define DBG_GUI_G
Definition: log.hpp:40
Contains the implementation details for lexical_cast and shouldn&#39;t be used directly.