The Battle for Wesnoth  1.17.17+dev
panel.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2023
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(VALIDATE_WML_CHILD(cfg, "background", _("Missing required background for panel definition")));
112  state.emplace_back(VALIDATE_WML_CHILD(cfg, "foreground", _("Missing required foreground for panel definition")));
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  auto c = cfg.optional_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
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:161
optional_config_impl< config > optional_child(config_key_type key, int n=0)
Euivalent to mandatory_child, but returns an empty optional if the nth child was not found.
Definition: config.cpp:389
void draw()
Draw the canvas' shapes onto the screen.
Definition: canvas.cpp:502
A generic container base class.
Base container class.
Definition: grid.hpp:32
A panel is a visible container to hold multiple widgets.
Definition: panel.hpp:59
virtual void impl_draw_background() override
See widget::impl_draw_background.
Definition: panel.cpp:68
virtual void impl_draw_foreground() override
See widget::impl_draw_foreground.
Definition: panel.cpp:74
virtual bool get_active() const override
See styled_widget::get_active.
Definition: panel.cpp:58
virtual unsigned get_state() const override
See styled_widget::get_state.
Definition: panel.cpp:63
virtual SDL_Rect get_client_rect() const override
See container_base::get_client_rect.
Definition: panel.cpp:44
virtual point border_space() const override
See container_base::border_space.
Definition: panel.cpp:80
virtual void set_self_active(const bool active) override
See container_base::set_self_active.
Definition: panel.cpp:88
canvas & get_canvas(const unsigned index)
Base class for all widgets.
Definition: widget.hpp:54
rect get_rectangle() const
Gets the bounding rectangle of the widget on the screen.
Definition: widget.cpp:311
static std::string _(const char *str)
Definition: gettext.hpp:93
Define the common log macros for the gui toolkit.
#define DBG_GUI_G
Definition: log.hpp:41
#define DBG_GUI_P
Definition: log.hpp:66
#define DBG_GUI_D
Definition: log.hpp:29
void point(int x, int y)
Draw a single point.
Definition: draw.cpp:193
Generic file dialog.
Contains the implementation details for lexical_cast and shouldn't be used directly.
#define LOG_HEADER
Definition: panel.cpp:30
Contains the SDL_Rect helper code.
#define REGISTER_WIDGET(id)
Wrapper for REGISTER_WIDGET3.
This file contains the settings handling of the widget library.
virtual std::unique_ptr< widget > build() const override
Definition: panel.cpp:130
std::string definition
Parameters for the styled_widget.
resolution(const config &cfg)
Definition: panel.cpp:103
panel_definition(const config &cfg)
Definition: panel.cpp:95
Base class of a resolution, contains the common keys for a resolution.
std::vector< state_definition > state
Holds a 2D point.
Definition: point.hpp:25
mock_char c
Add a special kind of assert to validate whether the input from WML doesn't contain any problems that...
#define VALIDATE_WML_CHILD(cfg, key, message)
#define VALIDATE(cond, message)
The macro to use for the validation of WML.