The Battle for Wesnoth  1.17.23+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  if(!get_canvas(0).update_blur(get_rectangle())) {
72  return false;
73  }
74  get_canvas(0).draw();
75  return true;
76 }
77 
79 {
80  DBG_GUI_D << LOG_HEADER << " size " << get_rectangle() << ".";
81  if(!get_canvas(1).update_blur(get_rectangle())) {
82  return false;
83  }
84  get_canvas(1).draw();
85  return true;
86 }
87 
89 {
90  const auto conf = cast_config_to<panel_definition>();
91  assert(conf);
92 
93  return point(conf->left_border + conf->right_border, conf->top_border + conf->bottom_border);
94 }
95 
96 void panel::set_self_active(const bool /*active*/)
97 {
98  /* DO NOTHING */
99 }
100 
101 // }---------- DEFINITION ---------{
102 
105 {
106  DBG_GUI_P << "Parsing panel " << id;
107 
108  load_resolutions<resolution>(cfg);
109 }
110 
112  : resolution_definition(cfg)
113  , top_border(cfg["top_border"])
114  , bottom_border(cfg["bottom_border"])
115  , left_border(cfg["left_border"])
116  , right_border(cfg["right_border"])
117 {
118  // The panel needs to know the order.
119  state.emplace_back(VALIDATE_WML_CHILD(cfg, "background", _("Missing required background for panel definition")));
120  state.emplace_back(VALIDATE_WML_CHILD(cfg, "foreground", _("Missing required foreground for panel definition")));
121 }
122 
123 // }---------- BUILDER -----------{
124 
125 namespace implementation
126 {
127 
128 builder_panel::builder_panel(const config& cfg)
129  : builder_styled_widget(cfg), grid(nullptr)
130 {
131  auto c = cfg.optional_child("grid");
132 
133  VALIDATE(c, _("No grid defined."));
134 
135  grid = std::make_shared<builder_grid>(*c);
136 }
137 
138 std::unique_ptr<widget> builder_panel::build() const
139 {
140  auto widget = std::make_unique<panel>(*this);
141 
142  DBG_GUI_G << "Window builder: placed panel '" << id << "' with definition '"
143  << definition << "'.";
144 
145  widget->init_grid(*grid);
146  return widget;
147 }
148 
149 } // namespace implementation
150 
151 // }------------ END --------------
152 
153 } // 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:569
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 bool impl_draw_foreground() override
See widget::impl_draw_foreground.
Definition: panel.cpp:78
virtual bool impl_draw_background() override
See widget::impl_draw_background.
Definition: panel.cpp:68
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:88
virtual void set_self_active(const bool active) override
See container_base::set_self_active.
Definition: panel.cpp:96
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:313
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:203
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:138
std::string definition
Parameters for the styled_widget.
resolution(const config &cfg)
Definition: panel.cpp:111
panel_definition(const config &cfg)
Definition: panel.cpp:103
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.