The Battle for Wesnoth  1.17.17+dev
size_lock.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2016 - 2023
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 "gui/widgets/settings.hpp"
25 #include "wml_exception.hpp"
26 
27 namespace gui2
28 {
29 REGISTER_WIDGET(size_lock)
30 
31 size_lock::size_lock(const implementation::builder_size_lock& builder)
32  : container_base(builder, type())
33  , width_(builder.width_)
34  , height_(builder.height_)
35  , widget_(nullptr)
36 {
37 }
38 
39 void size_lock::place(const point& origin, const point& size)
40 {
41  point content_size = widget_->get_best_size();
42 
43  if(content_size.x > size.x) {
44  reduce_width(size.x);
45  content_size = widget_->get_best_size();
46  }
47 
48  if(content_size.y > size.y) {
49  try {
51  } catch(const layout_exception_width_modified&) {
52  }
53 
54  content_size = widget_->get_best_size();
55  }
56 
57  if(content_size.x > size.x) {
58  reduce_width(size.x);
59  content_size = widget_->get_best_size();
60  }
61 
62  container_base::place(origin, size);
63 }
64 
66 {
67  assert(widget_ != nullptr);
68 
70 }
71 
72 void size_lock::finalize(const builder_widget& widget_builder)
73 {
74  set_rows_cols(1u, 1u);
75 
76  auto widget = widget_builder.build();
77  widget_ = widget.get();
78 
80 }
81 
83 {
85 
86  unsigned width = width_(size);
87  unsigned height = height_(size);
88 
89  VALIDATE(width > 0 || height > 0, _("Invalid size."));
90 
91  return point(width, height);
92 }
93 
96 {
97  DBG_GUI_P << "Parsing fixed size widget " << id;
98 
99  load_resolutions<resolution>(cfg);
100 }
101 
103  : resolution_definition(cfg)
104  , grid(nullptr)
105 {
106  // Add a dummy state since every widget needs a state.
107  static config dummy("draw");
108  state.emplace_back(dummy);
109 
110  auto child = cfg.optional_child("grid");
111  VALIDATE(child, _("No grid defined."));
112 
113  grid = std::make_shared<builder_grid>(*child);
114 }
115 
116 namespace implementation
117 {
118 builder_size_lock::builder_size_lock(const config& cfg)
119  : builder_styled_widget(cfg)
120  , width_(cfg["width"])
121  , height_(cfg["height"])
122  , content_(nullptr)
123 {
124  VALIDATE(cfg.has_child("widget"), _("No widget defined."));
126 }
127 
128 std::unique_ptr<widget> builder_size_lock::build() const
129 {
130  auto widget = std::make_unique<size_lock>(*this);
131 
132  DBG_GUI_G << "Window builder: placed fixed size widget '" << id << "' with definition '" << definition << "'.";
133 
134  const auto conf = widget->cast_config_to<size_lock_definition>();
135  assert(conf != nullptr);
136 
137  widget->init_grid(*conf->grid);
138  widget->finalize(*content_);
139 
140  return widget;
141 }
142 }
143 }
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:161
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:371
bool has_child(config_key_type key) const
Determine whether a config has a child or not.
Definition: config.cpp:321
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
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:50
typed_formula< unsigned > width_
Definition: size_lock.hpp:76
void place(const point &origin, const point &size) override
See widget::place.
Definition: size_lock.cpp:39
typed_formula< unsigned > height_
Definition: size_lock.hpp:77
void finalize(const builder_widget &widget_builder)
Finishes the building initialization of the widget.
Definition: size_lock.cpp:72
void layout_children() override
See widget::layout_children.
Definition: size_lock.cpp:65
widget * widget_
Points to the actual widget.
Definition: size_lock.hpp:84
point calculate_best_size() const override
See widget::calculate_best_size.
Definition: size_lock.cpp:82
Base class for all widgets.
Definition: widget.hpp:54
point get_best_size() const
Gets the best size for the widget.
Definition: widget.cpp:194
virtual void layout_children()
Allows a widget to update its children.
Definition: widget.cpp:296
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:193
Generic file dialog.
void get_screen_size_variables(wfl::map_formula_callable &variable)
Gets a formula object with the screen size.
Definition: helper.cpp:96
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:87
#define REGISTER_WIDGET(id)
Wrapper for REGISTER_WIDGET3.
This file contains the settings handling of the widget library.
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:128
builder_widget_const_ptr content_
Definition: size_lock.hpp:136
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:94
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.