1/**************************************************************************/
2/* margin_container.cpp */
3/**************************************************************************/
4/* This file is part of: */
5/* GODOT ENGINE */
6/* https://godotengine.org */
7/**************************************************************************/
8/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10/* */
11/* Permission is hereby granted, free of charge, to any person obtaining */
12/* a copy of this software and associated documentation files (the */
13/* "Software"), to deal in the Software without restriction, including */
14/* without limitation the rights to use, copy, modify, merge, publish, */
15/* distribute, sublicense, and/or sell copies of the Software, and to */
16/* permit persons to whom the Software is furnished to do so, subject to */
17/* the following conditions: */
18/* */
19/* The above copyright notice and this permission notice shall be */
20/* included in all copies or substantial portions of the Software. */
21/* */
22/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29/**************************************************************************/
30
31#include "margin_container.h"
32
33#include "scene/theme/theme_db.h"
34
35Size2 MarginContainer::get_minimum_size() const {
36 Size2 max;
37
38 for (int i = 0; i < get_child_count(); i++) {
39 Control *c = Object::cast_to<Control>(get_child(i));
40 if (!c) {
41 continue;
42 }
43 if (c->is_set_as_top_level()) {
44 continue;
45 }
46 if (!c->is_visible()) {
47 continue;
48 }
49
50 Size2 s = c->get_combined_minimum_size();
51 if (s.width > max.width) {
52 max.width = s.width;
53 }
54 if (s.height > max.height) {
55 max.height = s.height;
56 }
57 }
58
59 max.width += (theme_cache.margin_left + theme_cache.margin_right);
60 max.height += (theme_cache.margin_top + theme_cache.margin_bottom);
61
62 return max;
63}
64
65Vector<int> MarginContainer::get_allowed_size_flags_horizontal() const {
66 Vector<int> flags;
67 flags.append(SIZE_FILL);
68 flags.append(SIZE_SHRINK_BEGIN);
69 flags.append(SIZE_SHRINK_CENTER);
70 flags.append(SIZE_SHRINK_END);
71 return flags;
72}
73
74Vector<int> MarginContainer::get_allowed_size_flags_vertical() const {
75 Vector<int> flags;
76 flags.append(SIZE_FILL);
77 flags.append(SIZE_SHRINK_BEGIN);
78 flags.append(SIZE_SHRINK_CENTER);
79 flags.append(SIZE_SHRINK_END);
80 return flags;
81}
82
83int MarginContainer::get_margin_size(Side p_side) const {
84 ERR_FAIL_INDEX_V((int)p_side, 4, 0);
85
86 switch (p_side) {
87 case SIDE_LEFT:
88 return theme_cache.margin_left;
89 case SIDE_RIGHT:
90 return theme_cache.margin_right;
91 case SIDE_TOP:
92 return theme_cache.margin_top;
93 case SIDE_BOTTOM:
94 return theme_cache.margin_bottom;
95 }
96
97 return 0;
98}
99
100void MarginContainer::_notification(int p_what) {
101 switch (p_what) {
102 case NOTIFICATION_SORT_CHILDREN: {
103 Size2 s = get_size();
104
105 for (int i = 0; i < get_child_count(); i++) {
106 Control *c = Object::cast_to<Control>(get_child(i));
107 if (!c) {
108 continue;
109 }
110 if (c->is_set_as_top_level()) {
111 continue;
112 }
113
114 int w = s.width - theme_cache.margin_left - theme_cache.margin_right;
115 int h = s.height - theme_cache.margin_top - theme_cache.margin_bottom;
116 fit_child_in_rect(c, Rect2(theme_cache.margin_left, theme_cache.margin_top, w, h));
117 }
118 } break;
119
120 case NOTIFICATION_THEME_CHANGED: {
121 update_minimum_size();
122 } break;
123 }
124}
125
126void MarginContainer::_bind_methods() {
127 BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, MarginContainer, margin_left);
128 BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, MarginContainer, margin_top);
129 BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, MarginContainer, margin_right);
130 BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, MarginContainer, margin_bottom);
131}
132
133MarginContainer::MarginContainer() {
134}
135