The Battle for Wesnoth  1.17.4+dev
panel.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2022
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(int x_offset, int y_offset)
69 {
70  DBG_GUI_D << LOG_HEADER << " size " << get_rectangle() << ".\n";
71 
72  get_canvas(0).blit(calculate_blitting_rectangle(x_offset, y_offset));
73 }
74 
75 void panel::impl_draw_foreground(int x_offset, int y_offset)
76 {
77  DBG_GUI_D << LOG_HEADER << " size " << get_rectangle() << ".\n";
78 
79  get_canvas(1).blit(calculate_blitting_rectangle(x_offset, y_offset));
80 }
81 
83 {
84  const auto conf = cast_config_to<panel_definition>();
85  assert(conf);
86 
87  return point(conf->left_border + conf->right_border, conf->top_border + conf->bottom_border);
88 }
89 
90 void panel::set_self_active(const bool /*active*/)
91 {
92  /* DO NOTHING */
93 }
94 
95 // }---------- DEFINITION ---------{
96 
99 {
100  DBG_GUI_P << "Parsing panel " << id << '\n';
101 
102  load_resolutions<resolution>(cfg);
103 }
104 
106  : resolution_definition(cfg)
107  , top_border(cfg["top_border"])
108  , bottom_border(cfg["bottom_border"])
109  , left_border(cfg["left_border"])
110  , right_border(cfg["right_border"])
111 {
112  // The panel needs to know the order.
113  state.emplace_back(cfg.child("background"));
114  state.emplace_back(cfg.child("foreground"));
115 }
116 
117 // }---------- BUILDER -----------{
118 
119 namespace implementation
120 {
121 
122 builder_panel::builder_panel(const config& cfg)
123  : builder_styled_widget(cfg), grid(nullptr)
124 {
125  const config& c = cfg.child("grid");
126 
127  VALIDATE(c, _("No grid defined."));
128 
129  grid = std::make_shared<builder_grid>(c);
130 }
131 
132 std::unique_ptr<widget> builder_panel::build() const
133 {
134  auto widget = std::make_unique<panel>(*this);
135 
136  DBG_GUI_G << "Window builder: placed panel '" << id << "' with definition '"
137  << definition << "'.\n";
138 
139  widget->init_grid(*grid);
140  return widget;
141 }
142 
143 } // namespace implementation
144 
145 // }------------ END --------------
146 
147 } // namespace gui2
Define the common log macros for the gui toolkit.
virtual void impl_draw_background()
See draw_background.
Definition: widget.hpp:595
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
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:353
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:311
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
STL namespace.
static std::string _(const char *str)
Definition: gettext.hpp:93
Generic file dialog.
Definition: field-fwd.hpp:23
virtual point border_space() const override
See container_base::border_space.
Definition: panel.cpp:82
#define DBG_GUI_D
Definition: log.hpp:29
Base container class.
Definition: grid.hpp:31
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
virtual std::unique_ptr< widget > build() const override
Definition: panel.cpp:132
resolution(const config &cfg)
Definition: panel.cpp:105
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:90
panel_definition(const config &cfg)
Definition: panel.cpp:97
Holds a 2D point.
Definition: point.hpp:24
A generic container base class.
void blit(SDL_Rect rect)
Draw the canvas&#39; shapes onto the screen.
Definition: canvas.cpp:833
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:60
mock_char c
canvas & get_canvas(const unsigned index)
#define DBG_GUI_G
Definition: log.hpp:41
virtual void impl_draw_foreground(int x_offset, int y_offset) override
See widget::impl_draw_foreground.
Definition: panel.cpp:75
Contains the implementation details for lexical_cast and shouldn&#39;t be used directly.