The Battle for Wesnoth  1.19.0-dev
size_lock.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2016 - 2024
3  by Jyrki Vesterinen <sandgtx@gmail.com>
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 
19 
20 #include "gettext.hpp"
23 #include "gui/widgets/helper.hpp"
24 #include "wml_exception.hpp"
25 
26 namespace gui2
27 {
28 REGISTER_WIDGET(size_lock)
29 
30 size_lock::size_lock(const implementation::builder_size_lock& builder)
31  : container_base(builder, type())
32  , width_(builder.width_)
33  , height_(builder.height_)
34  , widget_(nullptr)
35 {
36 }
37 
38 void size_lock::place(const point& origin, const point& size)
39 {
40  point content_size = widget_->get_best_size();
41 
42  if(content_size.x > size.x) {
43  reduce_width(size.x);
44  content_size = widget_->get_best_size();
45  }
46 
47  if(content_size.y > size.y) {
48  try {
50  } catch(const layout_exception_width_modified&) {
51  }
52 
53  content_size = widget_->get_best_size();
54  }
55 
56  if(content_size.x > size.x) {
57  reduce_width(size.x);
58  content_size = widget_->get_best_size();
59  }
60 
61  container_base::place(origin, size);
62 }
63 
65 {
66  assert(widget_ != nullptr);
67 
69 }
70 
71 void size_lock::finalize(const builder_widget& widget_builder)
72 {
73  set_rows_cols(1u, 1u);
74 
75  auto widget = widget_builder.build();
76  widget_ = widget.get();
77 
79 }
80 
82 {
84 
85  unsigned width = width_(size);
86  unsigned height = height_(size);
87 
88  VALIDATE(width > 0 || height > 0, _("Invalid size."));
89 
90  return point(width, height);
91 }
92 
95 {
96  DBG_GUI_P << "Parsing fixed size widget " << id;
97 
98  load_resolutions<resolution>(cfg);
99 }
100 
102  : resolution_definition(cfg)
103  , grid(nullptr)
104 {
105  // Add a dummy state since every widget needs a state.
106  static config dummy("draw");
107  state.emplace_back(dummy);
108 
109  auto child = cfg.optional_child("grid");
110  VALIDATE(child, _("No grid defined."));
111 
112  grid = std::make_shared<builder_grid>(*child);
113 }
114 
115 namespace implementation
116 {
117 builder_size_lock::builder_size_lock(const config& cfg)
118  : builder_styled_widget(cfg)
119  , width_(cfg["width"])
120  , height_(cfg["height"])
121  , content_(nullptr)
122 {
123  VALIDATE(cfg.has_child("widget"), _("No widget defined."));
125 }
126 
127 std::unique_ptr<widget> builder_size_lock::build() const
128 {
129  auto widget = std::make_unique<size_lock>(*this);
130 
131  DBG_GUI_G << "Window builder: placed fixed size widget '" << id << "' with definition '" << definition << "'.";
132 
133  const auto conf = widget->cast_config_to<size_lock_definition>();
134  assert(conf != nullptr);
135 
136  widget->init_grid(*conf->grid);
137  widget->finalize(*content_);
138 
139  return widget;
140 }
141 }
142 }
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:159
config & mandatory_child(config_key_type key, int n=0)
Returns the nth child with the given key, or throws an error if there is none.
Definition: config.cpp:367
bool has_child(config_key_type key) const
Determine whether a config has a child or not.
Definition: config.cpp:317
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:385
A generic container base class.
void reduce_height(const unsigned maximum_height)
Tries to reduce the height of a container.
void set_rows_cols(const unsigned rows, const unsigned cols)
void reduce_width(const unsigned maximum_width)
Tries to reduce the width of a container.
virtual void place(const point &origin, const point &size) override
See widget::place.
void set_child(std::unique_ptr< widget > widget, const unsigned row, const unsigned col, const unsigned flags, const unsigned border_size)
Base container class.
Definition: grid.hpp:32
static const unsigned HORIZONTAL_GROW_SEND_TO_CLIENT
Definition: grid.hpp:56
static const unsigned VERTICAL_GROW_SEND_TO_CLIENT
Definition: grid.hpp:49
A fixed-size widget that wraps an arbitrary widget and forces it to the given size.
Definition: size_lock.hpp:49
typed_formula< unsigned > width_
Definition: size_lock.hpp:75
void place(const point &origin, const point &size) override
See widget::place.
Definition: size_lock.cpp:38
typed_formula< unsigned > height_
Definition: size_lock.hpp:76
void finalize(const builder_widget &widget_builder)
Finishes the building initialization of the widget.
Definition: size_lock.cpp:71
void layout_children() override
See widget::layout_children.
Definition: size_lock.cpp:64
widget * widget_
Points to the actual widget.
Definition: size_lock.hpp:83
point calculate_best_size() const override
See widget::calculate_best_size.
Definition: size_lock.cpp:81
Base class for all widgets.
Definition: widget.hpp:53
point get_best_size() const
Gets the best size for the widget.
Definition: widget.cpp:193
virtual void layout_children()
Allows a widget to update its children.
Definition: widget.cpp:297
static std::string _(const char *str)
Definition: gettext.hpp:93
#define DBG_GUI_G
Definition: log.hpp:41
#define DBG_GUI_P
Definition: log.hpp:66
Defines the exception classes for the layout algorithm.
void point(int x, int y)
Draw a single point.
Definition: draw.cpp:202
Generic file dialog.
void get_screen_size_variables(wfl::map_formula_callable &variable)
Gets a formula object with the screen size.
Definition: helper.cpp:94
builder_widget_ptr create_widget_builder(const config &cfg)
Create a widget builder.
Contains the implementation details for lexical_cast and shouldn't be used directly.
std::size_t size(const std::string &str)
Length in characters of a UTF-8 string.
Definition: unicode.cpp:85
#define REGISTER_WIDGET(id)
Wrapper for REGISTER_WIDGET3.
Contains the info needed to instantiate a widget.
virtual std::unique_ptr< widget > build() const =0
virtual std::unique_ptr< widget > build() const override
Definition: size_lock.cpp:127
builder_widget_const_ptr content_
Definition: size_lock.hpp:135
std::string definition
Parameters for the styled_widget.
Exception thrown when the width has been modified during resizing.
Base class of a resolution, contains the common keys for a resolution.
std::vector< state_definition > state
size_lock_definition(const config &cfg)
Definition: size_lock.cpp:93
Holds a 2D point.
Definition: point.hpp:25
Add a special kind of assert to validate whether the input from WML doesn't contain any problems that...
#define VALIDATE(cond, message)
The macro to use for the validation of WML.