The Battle for Wesnoth  1.17.0-dev
panel.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 
18 #include "gui/widgets/panel.hpp"
19 
20 #include "gui/core/log.hpp"
22 #include "gui/widgets/settings.hpp"
23 #include "gettext.hpp"
24 #include "sdl/rect.hpp"
25 #include "wml_exception.hpp"
26 
27 #include <functional>
28 
29 #define LOG_SCOPE_HEADER get_control_type() + " [" + id() + "] " + __func__
30 #define LOG_HEADER LOG_SCOPE_HEADER + ':'
31 
32 namespace gui2
33 {
34 
35 // ------------ WIDGET -----------{
36 
37 REGISTER_WIDGET(panel)
38 
39 panel::panel(const implementation::builder_styled_widget& builder, const std::string& control_type)
40  : container_base(builder, control_type.empty() ? type() : control_type)
41 {
42 }
43 
44 SDL_Rect panel::get_client_rect() const
45 {
46  const auto conf = cast_config_to<panel_definition>();
47  assert(conf);
48 
49  SDL_Rect result = get_rectangle();
50  result.x += conf->left_border;
51  result.y += conf->top_border;
52  result.w -= conf->left_border + conf->right_border;
53  result.h -= conf->top_border + conf->bottom_border;
54 
55  return result;
56 }
57 
58 bool panel::get_active() const
59 {
60  return true;
61 }
62 
63 unsigned panel::get_state() const
64 {
65  return 0;
66 }
67 
68 void panel::impl_draw_background(surface& frame_buffer, int x_offset, int y_offset)
69 {
70  DBG_GUI_D << LOG_HEADER << " size " << get_rectangle() << ".\n";
71 
72  get_canvas(0).blit(frame_buffer,
73  calculate_blitting_rectangle(x_offset, y_offset));
74 }
75 
76 void panel::impl_draw_foreground(surface& frame_buffer, int x_offset, int y_offset)
77 {
78  DBG_GUI_D << LOG_HEADER << " size " << get_rectangle() << ".\n";
79 
80  get_canvas(1).blit(frame_buffer,
81  calculate_blitting_rectangle(x_offset, y_offset));
82 }
83 
85 {
86  const auto conf = cast_config_to<panel_definition>();
87  assert(conf);
88 
89  return point(conf->left_border + conf->right_border, conf->top_border + conf->bottom_border);
90 }
91 
92 void panel::set_self_active(const bool /*active*/)
93 {
94  /* DO NOTHING */
95 }
96 
97 // }---------- DEFINITION ---------{
98 
101 {
102  DBG_GUI_P << "Parsing panel " << id << '\n';
103 
104  load_resolutions<resolution>(cfg);
105 }
106 
108  : resolution_definition(cfg)
109  , top_border(cfg["top_border"])
110  , bottom_border(cfg["bottom_border"])
111  , left_border(cfg["left_border"])
112  , right_border(cfg["right_border"])
113 {
114  // The panel needs to know the order.
115  state.emplace_back(cfg.child("background"));
116  state.emplace_back(cfg.child("foreground"));
117 }
118 
119 // }---------- BUILDER -----------{
120 
121 namespace implementation
122 {
123 
124 builder_panel::builder_panel(const config& cfg)
125  : builder_styled_widget(cfg), grid(nullptr)
126 {
127  const config& c = cfg.child("grid");
128 
129  VALIDATE(c, _("No grid defined."));
130 
131  grid = std::make_shared<builder_grid>(c);
132 }
133 
135 {
136  panel* widget = new panel(*this);
137 
138  DBG_GUI_G << "Window builder: placed panel '" << id << "' with definition '"
139  << definition << "'.\n";
140 
141  widget->init_grid(*grid);
142  return widget;
143 }
144 
145 } // namespace implementation
146 
147 // }------------ END --------------
148 
149 } // 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: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
virtual widget * build() const override
Definition: panel.cpp:134
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:352
A panel is a visible container to hold multiple widgets.
Definition: panel.hpp:58
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:310
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:44
virtual void impl_draw_foreground(surface &frame_buffer, int x_offset, int y_offset) override
See widget::impl_draw_foreground.
Definition: panel.cpp:76
STL namespace.
static std::string _(const char *str)
Definition: gettext.hpp:93
Generic file dialog.
Definition: field-fwd.hpp:23
virtual void impl_draw_background(surface &frame_buffer, int x_offset, int y_offset) override
See widget::impl_draw_background.
Definition: panel.cpp:68
virtual point border_space() const override
See container_base::border_space.
Definition: panel.cpp:84
#define DBG_GUI_D
Definition: log.hpp:29
Base container class.
Definition: grid.hpp:31
void blit(surface &surf, SDL_Rect rect)
Draw the canvas&#39; shapes onto another surface.
Definition: canvas.cpp:832
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:30
void init_grid(const builder_grid &grid_builder)
Initializes and builds the grid.
resolution(const config &cfg)
Definition: panel.cpp:107
virtual unsigned get_state() const override
See styled_widget::get_state.
Definition: panel.cpp:63
#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:92
panel_definition(const config &cfg)
Definition: panel.cpp:99
Holds a 2D point.
Definition: point.hpp:24
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:58
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:61
mock_char c
canvas & get_canvas(const unsigned index)
#define DBG_GUI_G
Definition: log.hpp:41
Contains the implementation details for lexical_cast and shouldn&#39;t be used directly.