The Battle for Wesnoth  1.17.0-dev
size_lock.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2016 - 2021
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 {
50  reduce_height(size.y);
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  widget_ = widget_builder.build();
78 }
79 
81 {
83 
84  unsigned width = width_(size);
85  unsigned height = height_(size);
86 
87  VALIDATE(width > 0 || height > 0, _("Invalid size."));
88 
89  return point(width, height);
90 }
91 
94 {
95  DBG_GUI_P << "Parsing fixed size widget " << id << '\n';
96 
97  load_resolutions<resolution>(cfg);
98 }
99 
101  : resolution_definition(cfg)
102  , grid(nullptr)
103 {
104  // Add a dummy state since every widget needs a state.
105  static config dummy("draw");
106  state.emplace_back(dummy);
107 
108  const config& child = cfg.child("grid");
109  VALIDATE(child, _("No grid defined."));
110 
111  grid = std::make_shared<builder_grid>(child);
112 }
113 
114 namespace implementation
115 {
116 builder_size_lock::builder_size_lock(const config& cfg)
117  : builder_styled_widget(cfg)
118  , width_(cfg["width"])
119  , height_(cfg["height"])
120  , content_(nullptr)
121 {
122  VALIDATE(cfg.has_child("widget"), _("No widget defined."));
123  content_ = create_widget_builder(cfg.child("widget"));
124 }
125 
127 {
128  size_lock* widget = new size_lock(*this);
129 
130  DBG_GUI_G << "Window builder: placed fixed size widget '" << id << "' with definition '" << definition << "'.\n";
131 
132  const auto conf = widget->cast_config_to<size_lock_definition>();
133  assert(conf != nullptr);
134 
135  widget->init_grid(*conf->grid);
136  widget->finalize(*content_);
137 
138  return widget;
139 }
140 }
141 }
Contains the info needed to instantiate a widget.
Base class of a resolution, contains the common keys for a resolution.
#define DBG_GUI_P
Definition: log.hpp:66
Defines the exception classes for the layout algorithm.
virtual widget * build() const =0
config & child(config_key_type key, int n=0)
Returns the nth child with the given key, or a reference to an invalid config if there is none...
Definition: config.cpp:402
void reduce_width(const unsigned maximum_width)
Tries to reduce the width of a container.
std::vector< state_definition > state
size_lock_definition(const config &cfg)
Definition: size_lock.cpp:92
int dummy
Definition: lstrlib.cpp:1347
virtual void place(const point &origin, const point &size) override
See widget::place.
Add a special kind of assert to validate whether the input from WML doesn&#39;t contain any problems that...
bool has_child(config_key_type key) const
Determine whether a config has a child or not.
Definition: config.cpp:394
Base class for all widgets.
Definition: widget.hpp:49
virtual widget * build() const override
Definition: size_lock.cpp:126
typed_formula< unsigned > height_
Definition: size_lock.hpp:77
static std::string _(const char *str)
Definition: gettext.hpp:93
void get_screen_size_variables(wfl::map_formula_callable &variable)
Gets a formula object with the screen size.
Definition: helper.cpp:100
int x
x coordinate.
Definition: point.hpp:45
Generic file dialog.
Definition: field-fwd.hpp:23
void set_child(widget *widget, const unsigned row, const unsigned col, const unsigned flags, const unsigned border_size)
Base container class.
Definition: grid.hpp:31
std::string definition
Parameters for the styled_widget.
void reduce_height(const unsigned maximum_height)
Tries to reduce the height of a container.
std::size_t size(const std::string &str)
Length in characters of a UTF-8 string.
Definition: unicode.cpp:87
Exception thrown when the width has been modified during resizing.
point get_best_size() const
Gets the best size for the widget.
Definition: widget.cpp:193
#define VALIDATE(cond, message)
The macro to use for the validation of WML.
This file contains the settings handling of the widget library.
void init_grid(const builder_grid &grid_builder)
Initializes and builds the grid.
typed_formula< unsigned > width_
Definition: size_lock.hpp:76
#define REGISTER_WIDGET(id)
Wrapper for REGISTER_WIDGET3.
A fixed-size widget that wraps an arbitrary widget and forces it to the given size.
Definition: size_lock.hpp:49
std::shared_ptr< const typename T::resolution > cast_config_to() const
Casts the current resolution definition config to the respective type of a derived widget...
void layout_children() override
See widget::layout_children.
Definition: size_lock.cpp:65
Holds a 2D point.
Definition: point.hpp:24
virtual void layout_children()
Allows a widget to update its children.
Definition: widget.cpp:295
static const unsigned HORIZONTAL_GROW_SEND_TO_CLIENT
Definition: grid.hpp:56
static const unsigned VERTICAL_GROW_SEND_TO_CLIENT
Definition: grid.hpp:49
point calculate_best_size() const override
See widget::calculate_best_size.
Definition: size_lock.cpp:80
A generic container base class.
builder_widget_const_ptr content_
Definition: size_lock.hpp:136
void set_rows_cols(const unsigned rows, const unsigned cols)
void place(const point &origin, const point &size) override
See widget::place.
Definition: size_lock.cpp:39
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:61
int y
y coordinate.
Definition: point.hpp:48
#define DBG_GUI_G
Definition: log.hpp:41
void finalize(const builder_widget &widget_builder)
Finishes the building initialization of the widget.
Definition: size_lock.cpp:72
Contains the implementation details for lexical_cast and shouldn&#39;t be used directly.
widget * widget_
Points to the actual widget.
Definition: size_lock.hpp:84
builder_widget_ptr create_widget_builder(const config &cfg)
Create a widget builder.