The Battle for Wesnoth  1.15.12+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 <functional>
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 
107  : resolution_definition(cfg)
108  , top_border(cfg["top_border"])
109  , bottom_border(cfg["bottom_border"])
110  , left_border(cfg["left_border"])
111  , right_border(cfg["right_border"])
112 {
113  // The panel needs to know the order.
114  state.emplace_back(cfg.child("background"));
115  state.emplace_back(cfg.child("foreground"));
116 }
117 
118 // }---------- BUILDER -----------{
119 
120 namespace implementation
121 {
122 
123 builder_panel::builder_panel(const config& cfg)
124  : builder_styled_widget(cfg), grid(nullptr)
125 {
126  const config& c = cfg.child("grid");
127 
128  VALIDATE(c, _("No grid defined."));
129 
130  grid = std::make_shared<builder_grid>(c);
131 }
132 
134 {
135  panel* widget = new panel(*this);
136 
137  DBG_GUI_G << "Window builder: placed panel '" << id << "' with definition '"
138  << definition << "'.\n";
139 
140  widget->init_grid(*grid);
141  return widget;
142 }
143 
144 } // namespace implementation
145 
146 // }------------ END --------------
147 
148 } // 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: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
virtual widget * build() const override
Definition: panel.cpp:133
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:351
A panel is a visible container to hold multiple widgets.
Definition: panel.hpp:57
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:309
Base class for all widgets.
Definition: widget.hpp:49
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.
static std::string _(const char *str)
Definition: gettext.hpp:92
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)
Draw the canvas&#39; shapes onto another surface.
Definition: canvas.cpp:825
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 builder_grid &grid_builder)
Initializes and builds the grid.
resolution(const config &cfg)
Definition: panel.cpp:106
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:59
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.