The Battle for Wesnoth  1.19.5+dev
panel.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2024
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 "gettext.hpp"
23 #include "sdl/rect.hpp"
24 #include "wml_exception.hpp"
25 
26 
27 #define LOG_SCOPE_HEADER get_control_type() + " [" + id() + "] " + __func__
28 #define LOG_HEADER LOG_SCOPE_HEADER + ':'
29 
30 namespace gui2
31 {
32 
33 // ------------ WIDGET -----------{
34 
35 REGISTER_WIDGET(panel)
36 
37 panel::panel(const implementation::builder_styled_widget& builder, const std::string& control_type)
38  : container_base(builder, control_type.empty() ? type() : control_type)
39 {
40 }
41 
42 SDL_Rect panel::get_client_rect() const
43 {
44  const auto conf = cast_config_to<panel_definition>();
45  assert(conf);
46 
47  SDL_Rect result = get_rectangle();
48  result.x += conf->left_border;
49  result.y += conf->top_border;
50  result.w -= conf->left_border + conf->right_border;
51  result.h -= conf->top_border + conf->bottom_border;
52 
53  return result;
54 }
55 
56 bool panel::get_active() const
57 {
58  return true;
59 }
60 
61 unsigned panel::get_state() const
62 {
63  return 0;
64 }
65 
67 {
68  DBG_GUI_D << LOG_HEADER << " size " << get_rectangle() << ".";
69  if(!get_canvas(0).update_blur(get_rectangle())) {
70  return false;
71  }
72  get_canvas(0).draw();
73  return true;
74 }
75 
77 {
78  DBG_GUI_D << LOG_HEADER << " size " << get_rectangle() << ".";
79  if(!get_canvas(1).update_blur(get_rectangle())) {
80  return false;
81  }
82  get_canvas(1).draw();
83  return true;
84 }
85 
87 {
88  const auto conf = cast_config_to<panel_definition>();
89  assert(conf);
90 
91  return point(conf->left_border + conf->right_border, conf->top_border + conf->bottom_border);
92 }
93 
94 void panel::set_self_active(const bool /*active*/)
95 {
96  /* DO NOTHING */
97 }
98 
99 // }---------- DEFINITION ---------{
100 
103 {
104  DBG_GUI_P << "Parsing panel " << id;
105 
106  load_resolutions<resolution>(cfg);
107 }
108 
110  : resolution_definition(cfg)
111  , top_border(cfg["top_border"].to_unsigned())
112  , bottom_border(cfg["bottom_border"].to_unsigned())
113  , left_border(cfg["left_border"].to_unsigned())
114  , right_border(cfg["right_border"].to_unsigned())
115 {
116  // The panel needs to know the order.
117  state.emplace_back(VALIDATE_WML_CHILD(cfg, "background", missing_mandatory_wml_tag("panel_definition][resolution", "background")));
118  state.emplace_back(VALIDATE_WML_CHILD(cfg, "foreground", missing_mandatory_wml_tag("panel_definition][resolution", "foreground")));
119 }
120 
121 // }---------- BUILDER -----------{
122 
123 namespace implementation
124 {
125 
126 builder_panel::builder_panel(const config& cfg)
127  : builder_styled_widget(cfg), grid(nullptr)
128 {
129  auto c = cfg.optional_child("grid");
130 
131  VALIDATE(c, _("No grid defined."));
132 
133  grid = std::make_shared<builder_grid>(*c);
134 }
135 
136 std::unique_ptr<widget> builder_panel::build() const
137 {
138  auto widget = std::make_unique<panel>(*this);
139 
140  DBG_GUI_G << "Window builder: placed panel '" << id << "' with definition '"
141  << definition << "'.";
142 
143  widget->init_grid(*grid);
144  return widget;
145 }
146 
147 } // namespace implementation
148 
149 // }------------ END --------------
150 
151 } // namespace gui2
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:172
optional_config_impl< config > optional_child(config_key_type key, int n=0)
Equivalent to mandatory_child, but returns an empty optional if the nth child was not found.
Definition: config.cpp:384
void draw()
Draw the canvas' shapes onto the screen.
Definition: canvas.cpp:634
A generic container base class.
Base container class.
Definition: grid.hpp:32
virtual bool impl_draw_foreground() override
See widget::impl_draw_foreground.
Definition: panel.cpp:76
virtual bool impl_draw_background() override
See widget::impl_draw_background.
Definition: panel.cpp:66
virtual bool get_active() const override
See styled_widget::get_active.
Definition: panel.cpp:56
virtual unsigned get_state() const override
See styled_widget::get_state.
Definition: panel.cpp:61
virtual SDL_Rect get_client_rect() const override
See container_base::get_client_rect.
Definition: panel.cpp:42
virtual point border_space() const override
See container_base::border_space.
Definition: panel.cpp:86
virtual void set_self_active(const bool active) override
See container_base::set_self_active.
Definition: panel.cpp:94
canvas & get_canvas(const unsigned index)
Base class for all widgets.
Definition: widget.hpp:55
rect get_rectangle() const
Gets the bounding rectangle of the widget on the screen.
Definition: widget.cpp:321
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:202
Generic file dialog.
Contains the implementation details for lexical_cast and shouldn't be used directly.
#define LOG_HEADER
Definition: panel.cpp:28
Contains the SDL_Rect helper code.
#define REGISTER_WIDGET(id)
Wrapper for REGISTER_WIDGET3.
virtual std::unique_ptr< widget > build() const override
Definition: panel.cpp:136
std::string definition
Parameters for the styled_widget.
resolution(const config &cfg)
Definition: panel.cpp:109
panel_definition(const config &cfg)
Definition: panel.cpp:101
std::vector< state_definition > state
Holds a 2D point.
Definition: point.hpp:25
mock_char c
std::string missing_mandatory_wml_tag(const std::string &section, const std::string &tag)
Returns a standard message for a missing wml child (tag).
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.