-
-
Notifications
You must be signed in to change notification settings - Fork 988
/
size_lock.cpp
186 lines (148 loc) · 4.81 KB
/
size_lock.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
Copyright (C) 2016 - 2017 Jyrki Vesterinen <sandgtx@gmail.com>
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#define GETTEXT_DOMAIN "wesnoth-lib"
#include "size_lock.hpp"
#include <gettext.hpp>
#include <gui/core/layout_exception.hpp>
#include <gui/core/register_widget.hpp>
#include <gui/widgets/helper.hpp>
#include <gui/widgets/settings.hpp>
#include <wml_exception.hpp>
namespace gui2
{
REGISTER_WIDGET(size_lock)
size_lock::size_lock(const implementation::builder_size_lock& builder)
: container_base(builder, get_control_type())
, widget_(nullptr)
{
}
void size_lock::place(const point& origin, const point& size)
{
point content_size = widget_->get_best_size();
if (content_size.x > size.x)
{
reduce_width(size.x);
content_size = widget_->get_best_size();
}
if (content_size.y > size.y)
{
try
{
reduce_height(size.y);
}
catch(layout_exception_width_modified&)
{
}
content_size = widget_->get_best_size();
}
if (content_size.x > size.x)
{
reduce_width(size.x);
content_size = widget_->get_best_size();
}
container_base::place(origin, size);
}
void size_lock::layout_children()
{
assert(widget_ != nullptr);
widget_->layout_children();
}
void size_lock::finalize(builder_widget_const_ptr widget_builder)
{
set_rows_cols(1u, 1u);
widget_ = widget_builder->build();
set_child(widget_, 0u, 0u,
grid::VERTICAL_GROW_SEND_TO_CLIENT | grid::HORIZONTAL_GROW_SEND_TO_CLIENT,
0u);
}
size_lock_definition::size_lock_definition(const config& cfg) :
styled_widget_definition(cfg)
{
DBG_GUI_P << "Parsing fixed size widget " << id << '\n';
load_resolutions<resolution>(cfg);
}
/*WIKI
* @page = GUIWidgetDefinitionWML
* @order = 1_size_lock
*
* == Size lock ==
*
* A size lock contains one child widget and forces it to have the specified size.
* This can be used, for example, when there are two list boxes in different rows of
* the same grid and it's desired that only one list box changes size when its
* contents change.
*
* A size lock has no states.
* @begin{parent}{name="gui/"}
* @begin{tag}{name="size_lock_definition"}{min=0}{max=-1}{super="generic/widget_definition"}
* @end{tag}{name="size_lock_definition"}
* @end{tag}{name="gui/"}
*/
size_lock_definition::resolution::resolution(const config& cfg) :
resolution_definition(cfg), grid(nullptr)
{
// Add a dummy state since every widget needs a state.
static config dummy("draw");
state.emplace_back(dummy);
const config& child = cfg.child("grid");
VALIDATE(child, _("No grid defined."));
grid = std::make_shared<builder_grid>(child);
}
/*WIKI
* @page = GUIWidgetInstanceWML
* @order = 2_size_lock
* @begin{parent}{name="gui/window/resolution/grid/row/column/"}
* @begin{tag}{name="size_lock"}{min=0}{max=-1}{super="generic/widget_instance"}
* == Size lock ==
*
* A size lock contains one child widget and forces it to have the specified size.
* This can be used, for example, when there are two list boxes in different rows of
* the same grid and it's desired that only one list box changes size when its
* contents change.
*
* @begin{table}{config}
* widget & section & mandatory & The widget. $
* width & f_unsigned & mandatory & The width of the widget. $
* height & f_unsigned & mandatory & The height of the widget. $
* @end{table}
*
* The variables available are the same as for window resolution, see
* [[GuiToolkitWML#Resolution_2]] for the list of items.
* @end{tag}{name="size_lock"}
* @end{parent}{name="gui/window/resolution/grid/row/column/"}
*/
namespace implementation
{
builder_size_lock::builder_size_lock(const config& cfg) :
builder_styled_widget(cfg), content_(nullptr), width_(cfg["width"]), height_(cfg["height"])
{
VALIDATE(cfg.has_child("widget"), _("No widget defined."));
content_ = create_builder_widget(cfg.child("widget"));
}
widget* builder_size_lock::build() const
{
size_lock* widget = new size_lock(*this);
DBG_GUI_G << "Window builder: placed fixed size widget '" << id <<
"' with definition '" << definition << "'.\n";
const auto conf = widget->cast_config_to<size_lock_definition>();
assert(conf != nullptr);
widget->init_grid(conf->grid);
wfl::map_formula_callable size = get_screen_size_variables();
const unsigned width = width_(size);
const unsigned height = height_(size);
VALIDATE(width > 0 || height > 0, _("Invalid size."));
widget->set_target_size(point(width, height));
widget->finalize(content_);
return widget;
}
}
}