The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
panel.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2016 by Mark de Wever <[email protected]>
3  Part of the Battle for Wesnoth Project http://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 "wml_exception.hpp"
24 
25 #include "utils/functional.hpp"
26 
27 #define LOG_SCOPE_HEADER get_control_type() + " [" + id() + "] " + __func__
28 #define LOG_HEADER LOG_SCOPE_HEADER + ':'
29 
30 namespace gui2
31 {
32 
33 // ------------ WIDGET -----------{
34 
35 REGISTER_WIDGET(panel)
36 
37 SDL_Rect tpanel::get_client_rect() const
38 {
40  = boost::dynamic_pointer_cast<const tpanel_definition::tresolution>(
41  config());
42  assert(conf);
43 
44  SDL_Rect result = get_rectangle();
45  result.x += conf->left_border;
46  result.y += conf->top_border;
47  result.w -= conf->left_border + conf->right_border;
48  result.h -= conf->top_border + conf->bottom_border;
49 
50  return result;
51 }
52 
53 bool tpanel::get_active() const
54 {
55  return true;
56 }
57 
58 unsigned tpanel::get_state() const
59 {
60  return 0;
61 }
62 
63 void tpanel::impl_draw_background(surface& frame_buffer, int x_offset, int y_offset)
64 {
65  DBG_GUI_D << LOG_HEADER << " size " << get_rectangle() << ".\n";
66 
67  canvas(0).blit(frame_buffer,
68  calculate_blitting_rectangle(x_offset, y_offset));
69 }
70 
71 void tpanel::impl_draw_foreground(surface& frame_buffer, int x_offset, int y_offset)
72 {
73  canvas(1).blit(frame_buffer,
74  calculate_blitting_rectangle(x_offset, y_offset));
75 }
76 
78 {
80  = boost::dynamic_pointer_cast<const tpanel_definition::tresolution>(
81  config());
82  assert(conf);
83 
84  return tpoint(conf->left_border + conf->right_border,
85  conf->top_border + conf->bottom_border);
86 }
87 
89 {
90  static const std::string type = "panel";
91  return type;
92 }
93 
94 void tpanel::set_self_active(const bool /*active*/)
95 {
96  /* DO NOTHING */
97 }
98 
99 // }---------- DEFINITION ---------{
100 
102  : tcontrol_definition(cfg)
103 {
104  DBG_GUI_P << "Parsing panel " << id << '\n';
105 
106  load_resolutions<tresolution>(cfg);
107 }
108 
109 /*WIKI
110  * @page = GUIWidgetDefinitionWML
111  * @order = 1_panel
112  *
113  * == Panel ==
114  *
115  * @macro = panel_description
116  *
117  * @begin{parent}{name="gui/"}
118  * @begin{tag}{name="panel_definition"}{min=0}{max=-1}{super="generic/widget_definition"}
119  * A panel is always enabled and can't be disabled. Instead it uses the
120  * states as layers to draw on.
121  * @begin{tag}{name="resolution"}{min=0}{max=-1}{super="generic/widget_definition/resolution"}
122  * The resolution for a panel also contains the following keys:
123  * @begin{table}{config}
124  * top_border & unsigned & 0 & The size which isn't used for the client
125  * area. $
126  * bottom_border & unsigned & 0 & The size which isn't used for the client
127  * area. $
128  * left_border & unsigned & 0 & The size which isn't used for the client
129  * area. $
130  * right_border & unsigned & 0 & The size which isn't used for the client
131  * area. $
132  * @end{table}
133  *
134  * The following layers exist:
135  * * background, the background of the panel.
136  * * foreground, the foreground of the panel.
137  * @begin{tag}{name="foreground"}{min=0}{max=1}
138  * @allow{link}{name="generic/state/draw"}
139  * @end{tag}{name="foreground"}
140  * @begin{tag}{name="background"}{min=0}{max=1}
141  * @allow{link}{name="generic/state/draw"}
142  * @end{tag}{name="background"}
143  * @end{tag}{name="resolution"}
144  * @end{tag}{name="panel_definition"}
145  * @end{parent}{name="gui/"}
146  */
149  , top_border(cfg["top_border"])
150  , bottom_border(cfg["bottom_border"])
151  , left_border(cfg["left_border"])
152  , right_border(cfg["right_border"])
153 {
154  // The panel needs to know the order.
155  state.push_back(tstate_definition(cfg.child("background")));
156  state.push_back(tstate_definition(cfg.child("foreground")));
157 }
158 
159 // }---------- BUILDER -----------{
160 
161 /*WIKI_MACRO
162  * @begin{macro}{panel_description}
163  *
164  * A panel is an item which can hold other items. The difference
165  * between a grid and a panel is that it's possible to define how a
166  * panel looks. A grid in an invisible container to just hold the
167  * items.
168  * @end{macro}
169  */
170 
171 /*WIKI
172  * @page = GUIWidgetInstanceWML
173  * @order = 2_panel
174  * @begin{parent}{name="gui/window/resolution/grid/row/column/"}
175  * @begin{tag}{name="panel"}{min="0"}{max="-1"}{super="generic/widget_instance"}
176  * == Panel ==
177  *
178  * @macro = panel_description
179  *
180  * @begin{table}{config}
181  * grid & grid & & Defines the grid with the widgets to
182  * place on the panel. $
183  * @end{table}
184  * @allow{link}{name="gui/window/resolution/grid"}
185  * @end{tag}{name="panel"}
186  * @end{parent}{name="gui/window/resolution/grid/row/column/"}
187  */
188 
189 namespace implementation
190 {
191 
192 tbuilder_panel::tbuilder_panel(const config& cfg)
193  : tbuilder_control(cfg), grid(nullptr)
194 {
195  const config& c = cfg.child("grid");
196 
197  VALIDATE(c, _("No grid defined."));
198 
199  grid = new tbuilder_grid(c);
200 }
201 
203 {
204  tpanel* widget = new tpanel();
205 
206  init_control(widget);
207 
208  DBG_GUI_G << "Window builder: placed panel '" << id << "' with definition '"
209  << definition << "'.\n";
210 
211  widget->init_grid(grid);
212  return widget;
213 }
214 
215 } // namespace implementation
216 
217 // }------------ END --------------
218 
219 } // namespace gui2
Define the common log macros for the gui toolkit.
virtual bool get_active() const override
See tcontrol::get_active.
Definition: panel.cpp:53
#define DBG_GUI_P
Definition: log.hpp:69
tpanel_definition(const config &cfg)
Definition: panel.cpp:101
virtual const std::string & get_control_type() const override
See tcontrol::get_control_type.
Definition: panel.cpp:88
const GLfloat * c
Definition: glew.h:12741
Add a special kind of assert to validate whether the input from WML doesn't contain any problems that...
GLuint GLuint GLsizei GLenum type
Definition: glew.h:1221
tresolution_definition_ptr config()
Definition: control.hpp:299
virtual void set_self_active(const bool active) override
See tcontainer_::set_self_active.
Definition: panel.cpp:94
tresolution(const config &cfg)
Definition: panel.cpp:147
Base class of a resolution, contains the common keys for a resolution.
A class inherited from ttext_box that displays its input as stars.
Definition: field-fwd.hpp:23
void init_control(tcontrol *control) const
Definition: control.cpp:675
std::string definition
Parameters for the control.
Definition: control.hpp:530
#define DBG_GUI_D
Definition: log.hpp:29
static UNUSEDNOWARN std::string _(const char *str)
Definition: gettext.hpp:82
GLuint64EXT * result
Definition: glew.h:10727
#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:28
void init_grid(const boost::intrusive_ptr< tbuilder_grid > &grid_builder)
Initializes and builds the grid.
Definition: container.cpp:209
Visible container to hold multiple widgets.
Definition: panel.hpp:34
Contains the state info for a resolution.
virtual unsigned get_state() const override
See tcontrol::get_state.
Definition: panel.cpp:58
#define REGISTER_WIDGET(id)
Wrapper for REGISTER_WIDGET3.
Holds a 2D point.
Definition: point.hpp:24
SDL_Rect calculate_blitting_rectangle(const int x_offset, const int y_offset)
Calculates the blitting rectangle of the widget.
Definition: widget.cpp:321
std::vector< tcanvas > & canvas()
Definition: control.hpp:282
virtual void impl_draw_foreground(surface &frame_buffer, int x_offset, int y_offset) override
See twidget::impl_draw_foreground.
Definition: panel.cpp:71
virtual void impl_draw_background(surface &frame_buffer, int x_offset, int y_offset) override
See twidget::impl_draw_background.
Definition: panel.cpp:63
std::vector< tstate_definition > state
config & child(const std::string &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:658
Base class for all widgets.
Definition: widget.hpp:49
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
SDL_Rect get_rectangle() const
Gets the bounding rectangle of the widget on the screen.
Definition: widget.cpp:279
virtual tpoint border_space() const override
See tcontainer_::border_space.
Definition: panel.cpp:77
#define DBG_GUI_G
Definition: log.hpp:41
GLsizei const GLcharARB ** string
Definition: glew.h:4503
Contains the implementation details for lexical_cast and shouldn't be used directly.