1/**************************************************************************/
2/* popup.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 "popup.h"
32
33#include "core/config/engine.h"
34#include "core/os/keyboard.h"
35#include "scene/gui/panel.h"
36#include "scene/theme/theme_db.h"
37
38void Popup::_input_from_window(const Ref<InputEvent> &p_event) {
39 if (get_flag(FLAG_POPUP) && p_event->is_action_pressed(SNAME("ui_cancel"), false, true)) {
40 _close_pressed();
41 }
42}
43
44void Popup::_initialize_visible_parents() {
45 if (is_embedded()) {
46 visible_parents.clear();
47
48 Window *parent_window = this;
49 while (parent_window) {
50 parent_window = parent_window->get_parent_visible_window();
51 if (parent_window) {
52 visible_parents.push_back(parent_window);
53 parent_window->connect("focus_entered", callable_mp(this, &Popup::_parent_focused));
54 parent_window->connect("tree_exited", callable_mp(this, &Popup::_deinitialize_visible_parents));
55 }
56 }
57 }
58}
59
60void Popup::_deinitialize_visible_parents() {
61 if (is_embedded()) {
62 for (Window *parent_window : visible_parents) {
63 parent_window->disconnect("focus_entered", callable_mp(this, &Popup::_parent_focused));
64 parent_window->disconnect("tree_exited", callable_mp(this, &Popup::_deinitialize_visible_parents));
65 }
66
67 visible_parents.clear();
68 }
69}
70
71void Popup::_notification(int p_what) {
72 switch (p_what) {
73 case NOTIFICATION_VISIBILITY_CHANGED: {
74 if (!is_in_edited_scene_root()) {
75 if (is_visible()) {
76 _initialize_visible_parents();
77 } else {
78 _deinitialize_visible_parents();
79 emit_signal(SNAME("popup_hide"));
80 popped_up = false;
81 }
82 }
83 } break;
84
85 case NOTIFICATION_WM_WINDOW_FOCUS_IN: {
86 if (!is_in_edited_scene_root()) {
87 if (has_focus()) {
88 popped_up = true;
89 }
90 }
91 } break;
92
93 case NOTIFICATION_UNPARENTED:
94 case NOTIFICATION_EXIT_TREE: {
95 if (!is_in_edited_scene_root()) {
96 _deinitialize_visible_parents();
97 }
98 } break;
99
100 case NOTIFICATION_WM_CLOSE_REQUEST: {
101 if (!is_in_edited_scene_root()) {
102 _close_pressed();
103 }
104 } break;
105
106 case NOTIFICATION_APPLICATION_FOCUS_OUT: {
107 if (!is_in_edited_scene_root() && get_flag(FLAG_POPUP)) {
108 _close_pressed();
109 }
110 } break;
111 }
112}
113
114void Popup::_parent_focused() {
115 if (popped_up && get_flag(FLAG_POPUP)) {
116 _close_pressed();
117 }
118}
119
120void Popup::_close_pressed() {
121 popped_up = false;
122
123 _deinitialize_visible_parents();
124
125 call_deferred(SNAME("hide"));
126}
127
128void Popup::_post_popup() {
129 Window::_post_popup();
130 popped_up = true;
131}
132
133void Popup::_validate_property(PropertyInfo &p_property) const {
134 if (
135 p_property.name == "transient" ||
136 p_property.name == "exclusive" ||
137 p_property.name == "popup_window" ||
138 p_property.name == "unfocusable") {
139 p_property.usage = PROPERTY_USAGE_NO_EDITOR;
140 }
141}
142
143Rect2i Popup::_popup_adjust_rect() const {
144 ERR_FAIL_COND_V(!is_inside_tree(), Rect2());
145 Rect2i parent_rect = get_usable_parent_rect();
146
147 if (parent_rect == Rect2i()) {
148 return Rect2i();
149 }
150
151 Rect2i current(get_position(), get_size());
152
153 if (current.position.x + current.size.x > parent_rect.position.x + parent_rect.size.x) {
154 current.position.x = parent_rect.position.x + parent_rect.size.x - current.size.x;
155 }
156
157 if (current.position.x < parent_rect.position.x) {
158 current.position.x = parent_rect.position.x;
159 }
160
161 if (current.position.y + current.size.y > parent_rect.position.y + parent_rect.size.y) {
162 current.position.y = parent_rect.position.y + parent_rect.size.y - current.size.y;
163 }
164
165 if (current.position.y < parent_rect.position.y) {
166 current.position.y = parent_rect.position.y;
167 }
168
169 if (current.size.y > parent_rect.size.y) {
170 current.size.y = parent_rect.size.y;
171 }
172
173 if (current.size.x > parent_rect.size.x) {
174 current.size.x = parent_rect.size.x;
175 }
176
177 // Early out if max size not set.
178 Size2i popup_max_size = get_max_size();
179 if (popup_max_size <= Size2()) {
180 return current;
181 }
182
183 if (current.size.x > popup_max_size.x) {
184 current.size.x = popup_max_size.x;
185 }
186
187 if (current.size.y > popup_max_size.y) {
188 current.size.y = popup_max_size.y;
189 }
190
191 return current;
192}
193
194void Popup::_bind_methods() {
195 ADD_SIGNAL(MethodInfo("popup_hide"));
196
197 BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, Popup, panel_style, "panel");
198}
199
200Popup::Popup() {
201 set_wrap_controls(true);
202 set_visible(false);
203 set_transient(true);
204 set_flag(FLAG_BORDERLESS, true);
205 set_flag(FLAG_RESIZE_DISABLED, true);
206 set_flag(FLAG_POPUP, true);
207
208 connect("window_input", callable_mp(this, &Popup::_input_from_window));
209}
210
211Popup::~Popup() {
212}
213
214Size2 PopupPanel::_get_contents_minimum_size() const {
215 Size2 ms;
216
217 for (int i = 0; i < get_child_count(); i++) {
218 Control *c = Object::cast_to<Control>(get_child(i));
219 if (!c || c == panel) {
220 continue;
221 }
222
223 if (c->is_set_as_top_level()) {
224 continue;
225 }
226
227 Size2 cms = c->get_combined_minimum_size();
228 ms.x = MAX(cms.x, ms.x);
229 ms.y = MAX(cms.y, ms.y);
230 }
231
232 return ms + theme_cache.panel_style->get_minimum_size();
233}
234
235void PopupPanel::_update_child_rects() {
236 Vector2 cpos(theme_cache.panel_style->get_offset());
237 Vector2 csize(get_size() - theme_cache.panel_style->get_minimum_size());
238
239 for (int i = 0; i < get_child_count(); i++) {
240 Control *c = Object::cast_to<Control>(get_child(i));
241 if (!c) {
242 continue;
243 }
244
245 if (c->is_set_as_top_level()) {
246 continue;
247 }
248
249 if (c == panel) {
250 c->set_position(Vector2());
251 c->set_size(get_size());
252 } else {
253 c->set_position(cpos);
254 c->set_size(csize);
255 }
256 }
257}
258
259void PopupPanel::_notification(int p_what) {
260 switch (p_what) {
261 case NOTIFICATION_READY:
262 case NOTIFICATION_THEME_CHANGED: {
263 panel->add_theme_style_override("panel", theme_cache.panel_style);
264 _update_child_rects();
265 } break;
266
267 case NOTIFICATION_WM_SIZE_CHANGED: {
268 _update_child_rects();
269 } break;
270 }
271}
272
273void PopupPanel::_bind_methods() {
274 BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, PopupPanel, panel_style, "panel");
275}
276
277PopupPanel::PopupPanel() {
278 panel = memnew(Panel);
279 add_child(panel, false, INTERNAL_MODE_FRONT);
280}
281