The Battle for Wesnoth  1.17.12+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 
69 {
70  DBG_GUI_D << LOG_HEADER << " size " << get_rectangle() << ".";
71  get_canvas(0).draw();
72 }
73 
75 {
76  DBG_GUI_D << LOG_HEADER << " size " << get_rectangle() << ".";
77  get_canvas(1).draw();
78 }
79 
81 {
82  const auto conf = cast_config_to<panel_definition>();
83  assert(conf);
84 
85  return point(conf->left_border + conf->right_border, conf->top_border + conf->bottom_border);
86 }
87 
88 void panel::set_self_active(const bool /*active*/)
89 {
90  /* DO NOTHING */
91 }
92 
93 // }---------- DEFINITION ---------{
94 
97 {
98  DBG_GUI_P << "Parsing panel " << id;
99 
100  load_resolutions<resolution>(cfg);
101 }
102 
104  : resolution_definition(cfg)
105  , top_border(cfg["top_border"])
106  , bottom_border(cfg["bottom_border"])
107  , left_border(cfg["left_border"])
108  , right_border(cfg["right_border"])
109 {
110  // The panel needs to know the order.
111  state.emplace_back(cfg.child("background"));
112  state.emplace_back(cfg.child("foreground"));
113 }
114 
115 // }---------- BUILDER -----------{
116 
117 namespace implementation
118 {
119 
120 builder_panel::builder_panel(const config& cfg)
121  : builder_styled_widget(cfg), grid(nullptr)
122 {
123  const config& c = cfg.child("grid");
124 
125  VALIDATE(c, _("No grid defined."));
126 
127  grid = std::make_shared<builder_grid>(c);
128 }
129 
130 std::unique_ptr<widget> builder_panel::build() const
131 {
132  auto widget = std::make_unique<panel>(*this);
133 
134  DBG_GUI_G << "Window builder: placed panel '" << id << "' with definition '"
135  << definition << "'.";
136 
137  widget->init_grid(*grid);
138  return widget;
139 }
140 
141 } // namespace implementation
142 
143 // }------------ END --------------
144 
145 } // 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
std::vector< state_definition > state
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...
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:53
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.
virtual point border_space() const override
See container_base::border_space.
Definition: panel.cpp:80
#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:130
resolution(const config &cfg)
Definition: panel.cpp:103
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 impl_draw_foreground() override
See widget::impl_draw_foreground.
Definition: panel.cpp:74
virtual void set_self_active(const bool active) override
See container_base::set_self_active.
Definition: panel.cpp:88
panel_definition(const config &cfg)
Definition: panel.cpp:95
Holds a 2D point.
Definition: point.hpp:24
A generic container base class.
virtual void impl_draw_background() override
See widget::impl_draw_background.
Definition: panel.cpp:68
Contains the SDL_Rect helper code.
void draw()
Draw the canvas&#39; shapes onto the screen.
Definition: canvas.cpp:502
void point(int x, int y)
Draw a single point.
Definition: draw.cpp:193
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
Contains the implementation details for lexical_cast and shouldn&#39;t be used directly.