Blender V4.5
interface_region_popup.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2008 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
10
11#include <algorithm>
12#include <cstdarg>
13#include <cstdlib>
14#include <cstring>
15
16#include "MEM_guardedalloc.h"
17
18#include "DNA_userdef_types.h"
19
20#include "BLI_listbase.h"
21#include "BLI_math_vector.h"
22#include "BLI_rect.h"
23#include "BLI_utildefines.h"
24
25#include "BKE_context.hh"
26#include "BKE_screen.hh"
27
28#include "WM_api.hh"
29#include "WM_types.hh"
30
31#include "UI_interface.hh"
32
33#include "ED_screen.hh"
34
35#include "interface_intern.hh"
37
38/* -------------------------------------------------------------------- */
41
42void ui_popup_translate(ARegion *region, const int mdiff[2])
43{
44 BLI_rcti_translate(&region->winrct, UNPACK2(mdiff));
45
47
49
50 /* update blocks */
51 LISTBASE_FOREACH (uiBlock *, block, &region->runtime->uiblocks) {
52 uiPopupBlockHandle *handle = block->handle;
53 /* Make empty, will be initialized on next use, see #60608. */
54 BLI_rctf_init(&handle->prev_block_rect, 0, 0, 0, 0);
55
56 LISTBASE_FOREACH (uiSafetyRct *, saferct, &block->saferct) {
57 BLI_rctf_translate(&saferct->parent, UNPACK2(mdiff));
58 BLI_rctf_translate(&saferct->safety, UNPACK2(mdiff));
59 }
60 }
61}
62
63/* position block relative to but, result is in window space */
65 ARegion *butregion,
66 uiBut *but,
67 uiBlock *block)
68{
69 uiPopupBlockHandle *handle = block->handle;
70
71 /* Compute button position in window coordinates using the source
72 * button region/block, to position the popup attached to it. */
73 rctf butrct;
74 if (!handle->refresh) {
75 ui_block_to_window_rctf(butregion, but->block, &butrct, &but->rect);
76
77 /* widget_roundbox_set has this correction too, keep in sync */
78 if (but->type != UI_BTYPE_PULLDOWN) {
79 if (but->drawflag & UI_BUT_ALIGN_TOP) {
80 butrct.ymax += U.pixelsize;
81 }
82 if (but->drawflag & UI_BUT_ALIGN_LEFT) {
83 butrct.xmin -= U.pixelsize;
84 }
85 }
86
87 handle->prev_butrct = butrct;
88 }
89 else {
90 /* For refreshes, keep same button position so popup doesn't move. */
91 butrct = handle->prev_butrct;
92 }
93
94 /* Compute block size in window space, based on buttons contained in it. */
95 if (block->rect.xmin == 0.0f && block->rect.xmax == 0.0f) {
96 if (!block->buttons.is_empty()) {
98
99 for (const std::unique_ptr<uiBut> &bt : block->buttons) {
101 bt->rect.xmax += UI_MENU_SUBMENU_PADDING;
102 }
103 BLI_rctf_union(&block->rect, &bt->rect);
104 }
105 }
106 else {
107 /* we're nice and allow empty blocks too */
108 block->rect.xmin = block->rect.ymin = 0;
109 block->rect.xmax = block->rect.ymax = 20;
110 }
111 }
112
113 /* Trim the popup and its contents to the width of the button if the size difference
114 * is small. This avoids cases where the rounded corner clips underneath the button. */
115 const int delta = BLI_rctf_size_x(&block->rect) - BLI_rctf_size_x(&butrct);
116 const float max_radius = (0.5f * U.widget_unit);
117
118 if (delta >= 0 && delta < max_radius) {
119 for (const std::unique_ptr<uiBut> &bt : block->buttons) {
120 /* Only trim the right most buttons in multi-column popovers. */
121 if (bt->rect.xmax == block->rect.xmax) {
122 bt->rect.xmax -= delta;
123 }
124 }
125 block->rect.xmax -= delta;
126 }
127
128 ui_block_to_window_rctf(butregion, but->block, &block->rect, &block->rect);
129
130 /* `block->rect` is already scaled with `butregion->winrct`,
131 * apply this scale to layout panels too. */
132 if (Panel *panel = block->panel) {
133 for (LayoutPanelBody &body : panel->runtime->layout_panels.bodies) {
134 body.start_y /= block->aspect;
135 body.end_y /= block->aspect;
136 }
137 for (LayoutPanelHeader &header : panel->runtime->layout_panels.headers) {
138 header.start_y /= block->aspect;
139 header.end_y /= block->aspect;
140 }
141 }
142
143 /* Compute direction relative to button, based on available space. */
144 const int size_x = BLI_rctf_size_x(&block->rect) + 0.2f * UI_UNIT_X; /* 4 for shadow */
145 const int size_y = BLI_rctf_size_y(&block->rect) + 0.2f * UI_UNIT_Y;
146 const int center_x = (block->direction & UI_DIR_CENTER_X) ? size_x / 2 : 0;
147 const int center_y = (block->direction & UI_DIR_CENTER_Y) ? size_y / 2 : 0;
148
149 const blender::int2 win_size = WM_window_native_pixel_size(window);
150
151 /* Take into account maximum size so we don't have to flip on refresh. */
152 const blender::float2 max_size = {
153 max_ff(size_x, handle->max_size_x),
154 max_ff(size_y, handle->max_size_y),
155 };
156
157 short dir1 = 0, dir2 = 0;
158
159 if (!handle->refresh) {
160 bool left = false, right = false, top = false, down = false;
161
162 /* check if there's space at all */
163 if (butrct.xmin - max_size[0] + center_x > 0.0f) {
164 left = true;
165 }
166 if (butrct.xmax + max_size[0] - center_x < win_size[0]) {
167 right = true;
168 }
169 if (butrct.ymin - max_size[1] + center_y > 0.0f) {
170 down = true;
171 }
172 if (butrct.ymax + max_size[1] - center_y < win_size[1]) {
173 top = true;
174 }
175
176 if (top == 0 && down == 0) {
177 if (butrct.ymin - max_size[1] < win_size[1] - butrct.ymax - max_size[1]) {
178 top = true;
179 }
180 else {
181 down = true;
182 }
183 }
184
185 dir1 = (block->direction & UI_DIR_ALL);
186
187 /* Secondary directions. */
188 if (dir1 & (UI_DIR_UP | UI_DIR_DOWN)) {
189 if (dir1 & UI_DIR_LEFT) {
190 dir2 = UI_DIR_LEFT;
191 }
192 else if (dir1 & UI_DIR_RIGHT) {
193 dir2 = UI_DIR_RIGHT;
194 }
195 dir1 &= (UI_DIR_UP | UI_DIR_DOWN);
196 }
197
198 if ((dir2 == 0) && ELEM(dir1, UI_DIR_LEFT, UI_DIR_RIGHT)) {
199 dir2 = UI_DIR_DOWN;
200 }
201 if ((dir2 == 0) && ELEM(dir1, UI_DIR_UP, UI_DIR_DOWN)) {
202 dir2 = UI_DIR_LEFT;
203 }
204
205 /* no space at all? don't change */
206 if (left || right) {
207 if (dir1 == UI_DIR_LEFT && left == 0) {
208 dir1 = UI_DIR_RIGHT;
209 }
210 if (dir1 == UI_DIR_RIGHT && right == 0) {
211 dir1 = UI_DIR_LEFT;
212 }
213 /* this is aligning, not append! */
214 if (dir2 == UI_DIR_LEFT && right == 0) {
215 dir2 = UI_DIR_RIGHT;
216 }
217 if (dir2 == UI_DIR_RIGHT && left == 0) {
218 dir2 = UI_DIR_LEFT;
219 }
220 }
221 if (down || top) {
222 if (dir1 == UI_DIR_UP && top == 0) {
223 dir1 = UI_DIR_DOWN;
224 }
225 if (dir1 == UI_DIR_DOWN && down == 0) {
226 dir1 = UI_DIR_UP;
227 }
228 BLI_assert(dir2 != UI_DIR_UP);
229 // if (dir2 == UI_DIR_UP && top == 0) { dir2 = UI_DIR_DOWN; }
230 if (dir2 == UI_DIR_DOWN && down == 0) {
231 dir2 = UI_DIR_UP;
232 }
233 }
234
235 handle->prev_dir1 = dir1;
236 handle->prev_dir2 = dir2;
237 }
238 else {
239 /* For refreshes, keep same popup direct so popup doesn't move
240 * to a totally different position while editing in it. */
241 dir1 = handle->prev_dir1;
242 dir2 = handle->prev_dir2;
243 }
244
245 /* Compute offset based on direction. */
246 float offset_x = 0, offset_y = 0;
247
248 /* Ensure buttons don't come between the parent button and the popup, see: #63566. */
249 const float offset_overlap = max_ff(U.pixelsize, 1.0f);
250
251 if (dir1 == UI_DIR_LEFT) {
252 offset_x = (butrct.xmin - block->rect.xmax) + offset_overlap;
253 if (dir2 == UI_DIR_UP) {
254 offset_y = butrct.ymin - block->rect.ymin - center_y - UI_MENU_PADDING;
255 }
256 else {
257 offset_y = butrct.ymax - block->rect.ymax + center_y + UI_MENU_PADDING;
258 }
259 }
260 else if (dir1 == UI_DIR_RIGHT) {
261 offset_x = (butrct.xmax - block->rect.xmin) - offset_overlap;
262 if (dir2 == UI_DIR_UP) {
263 offset_y = butrct.ymin - block->rect.ymin - center_y - UI_MENU_PADDING;
264 }
265 else {
266 offset_y = butrct.ymax - block->rect.ymax + center_y + UI_MENU_PADDING;
267 }
268 }
269 else if (dir1 == UI_DIR_UP) {
270 offset_y = (butrct.ymax - block->rect.ymin) - offset_overlap;
271
272 if (but->type == UI_BTYPE_COLOR &&
273 block->rect.ymax + offset_y > win_size[1] - UI_POPUP_MENU_TOP)
274 {
275 /* Shift this down, aligning the top edge close to the window top. */
276 offset_y = win_size[1] - block->rect.ymax - UI_POPUP_MENU_TOP;
277 /* All four corners should be rounded since this no longer button-aligned. */
278 block->direction = UI_DIR_CENTER_Y;
279 dir1 = UI_DIR_CENTER_Y;
280 }
281
282 if (dir2 == UI_DIR_RIGHT) {
283 offset_x = butrct.xmax - block->rect.xmax + center_x;
284 }
285 else {
286 offset_x = butrct.xmin - block->rect.xmin - center_x;
287 }
288 }
289 else if (dir1 == UI_DIR_DOWN) {
290 offset_y = (butrct.ymin - block->rect.ymax) + offset_overlap;
291
292 if (but->type == UI_BTYPE_COLOR && block->rect.ymin + offset_y < UI_SCREEN_MARGIN) {
293 /* Shift this up, aligning the bottom edge close to the window bottom. */
294 offset_y = -block->rect.ymin + UI_SCREEN_MARGIN;
295 /* All four corners should be rounded since this no longer button-aligned. */
296 block->direction = UI_DIR_CENTER_Y;
297 dir1 = UI_DIR_CENTER_Y;
298 }
299
300 if (dir2 == UI_DIR_RIGHT) {
301 offset_x = butrct.xmax - block->rect.xmax + center_x;
302 }
303 else {
304 offset_x = butrct.xmin - block->rect.xmin - center_x;
305 }
306 }
307
308 /* Center over popovers for eg. */
309 if (block->direction & UI_DIR_CENTER_X) {
310 offset_x += BLI_rctf_size_x(&butrct) / ((dir2 == UI_DIR_LEFT) ? 2 : -2);
311 }
312
313 /* Apply offset, buttons in window coords. */
314 for (const std::unique_ptr<uiBut> &bt : block->buttons) {
315 ui_block_to_window_rctf(butregion, but->block, &bt->rect, &bt->rect);
316
317 BLI_rctf_translate(&bt->rect, offset_x, offset_y);
318
319 /* ui_but_update recalculates drawstring size in pixels */
320 ui_but_update(bt.get());
321 }
322
323 BLI_rctf_translate(&block->rect, offset_x, offset_y);
324
325 /* Safety calculus. */
326 {
327 const float midx = BLI_rctf_cent_x(&butrct);
328 const float midy = BLI_rctf_cent_y(&butrct);
329
330 /* when you are outside parent button, safety there should be smaller */
331
332 const int s1 = 40 * UI_SCALE_FAC;
333 const int s2 = 3 * UI_SCALE_FAC;
334
335 /* parent button to left */
336 if (midx < block->rect.xmin) {
337 block->safety.xmin = block->rect.xmin - s2;
338 }
339 else {
340 block->safety.xmin = block->rect.xmin - s1;
341 }
342 /* parent button to right */
343 if (midx > block->rect.xmax) {
344 block->safety.xmax = block->rect.xmax + s2;
345 }
346 else {
347 block->safety.xmax = block->rect.xmax + s1;
348 }
349
350 /* parent button on bottom */
351 if (midy < block->rect.ymin) {
352 block->safety.ymin = block->rect.ymin - s2;
353 }
354 else {
355 block->safety.ymin = block->rect.ymin - s1;
356 }
357 /* parent button on top */
358 if (midy > block->rect.ymax) {
359 block->safety.ymax = block->rect.ymax + s2;
360 }
361 else {
362 block->safety.ymax = block->rect.ymax + s1;
363 }
364
365 /* Exception for switched pull-downs. */
366 if (dir1 && (dir1 & block->direction) == 0) {
367 if (dir2 == UI_DIR_RIGHT) {
368 block->safety.xmax = block->rect.xmax + s2;
369 }
370 if (dir2 == UI_DIR_LEFT) {
371 block->safety.xmin = block->rect.xmin - s2;
372 }
373 }
374
375 const bool fully_aligned_with_button = BLI_rctf_size_x(&block->rect) <=
376 BLI_rctf_size_x(&butrct) + 1;
377 const bool off_screen_left = (block->rect.xmin < 0);
378 const bool off_screen_right = (block->rect.xmax > win_size[0]);
379
380 if (fully_aligned_with_button) {
381 /* Popup is neither left or right from the button. */
382 dir2 &= ~(UI_DIR_LEFT | UI_DIR_RIGHT);
383 }
384 else if (off_screen_left || off_screen_right) {
385 /* Popup is both left and right from the button. */
386 dir2 |= (UI_DIR_LEFT | UI_DIR_RIGHT);
387 }
388
389 /* Popovers don't need secondary direction. Pull-downs to
390 * the left or right are currently not supported. */
391 const bool no_2nd_dir = (but->type == UI_BTYPE_POPOVER || ui_but_menu_draw_as_popover(but) ||
392 dir1 & (UI_DIR_RIGHT | UI_DIR_LEFT));
393 block->direction = no_2nd_dir ? dir1 : (dir1 | dir2);
394 }
395
396 /* Keep a list of these, needed for pull-down menus. */
397 uiSafetyRct *saferct = MEM_callocN<uiSafetyRct>(__func__);
398 saferct->parent = butrct;
399 saferct->safety = block->safety;
400 BLI_freelistN(&block->saferct);
401 BLI_duplicatelist(&block->saferct, &but->block->saferct);
402 BLI_addhead(&block->saferct, saferct);
403}
404
406
407/* -------------------------------------------------------------------- */
410
411static void ui_block_region_refresh(const bContext *C, ARegion *region)
412{
414
415 ScrArea *ctx_area = CTX_wm_area(C);
416 ARegion *ctx_region = CTX_wm_region(C);
417
418 if (region->runtime->do_draw & RGN_REFRESH_UI) {
419 ScrArea *handle_ctx_area;
420 ARegion *handle_ctx_region;
421
422 region->runtime->do_draw &= ~RGN_REFRESH_UI;
423 LISTBASE_FOREACH_MUTABLE (uiBlock *, block, &region->runtime->uiblocks) {
424 uiPopupBlockHandle *handle = block->handle;
425
426 if (handle->can_refresh) {
427 handle_ctx_area = handle->ctx_area;
428 handle_ctx_region = handle->ctx_region;
429
430 if (handle_ctx_area) {
431 CTX_wm_area_set((bContext *)C, handle_ctx_area);
432 }
433 if (handle_ctx_region) {
434 CTX_wm_region_set((bContext *)C, handle_ctx_region);
435 }
436
437 uiBut *but = handle->popup_create_vars.but;
438 ARegion *butregion = handle->popup_create_vars.butregion;
439 ui_popup_block_refresh((bContext *)C, handle, butregion, but);
440 }
441 }
442 }
443
444 CTX_wm_area_set((bContext *)C, ctx_area);
445 CTX_wm_region_set((bContext *)C, ctx_region);
446}
447
448static void ui_block_region_draw(const bContext *C, ARegion *region)
449{
450 LISTBASE_FOREACH (uiBlock *, block, &region->runtime->uiblocks) {
451 UI_block_draw(C, block);
452 }
453}
454
459{
460 ARegion *region = params->region;
461 const wmNotifier *wmn = params->notifier;
462
463 switch (wmn->category) {
464 case NC_WINDOW: {
465 switch (wmn->action) {
466 case NA_EDITED: {
467 /* window resize */
469 break;
470 }
471 }
472 break;
473 }
474 }
475}
476
477static void ui_popup_block_clip(wmWindow *window, uiBlock *block)
478{
479 const float xmin_orig = block->rect.xmin;
480 const int margin = UI_SCREEN_MARGIN;
481
482 if (block->flag & UI_BLOCK_NO_WIN_CLIP) {
483 return;
484 }
485
486 const blender::int2 win_size = WM_window_native_pixel_size(window);
487
488 /* shift to left if outside of view */
489 if (block->rect.xmax > win_size[0] - margin) {
490 const float xofs = win_size[0] - margin - block->rect.xmax;
491 block->rect.xmin += xofs;
492 block->rect.xmax += xofs;
493 }
494 /* shift menus to right if outside of view */
495 if (block->rect.xmin < margin) {
496 const float xofs = (margin - block->rect.xmin);
497 block->rect.xmin += xofs;
498 block->rect.xmax += xofs;
499 }
500
501 block->rect.ymin = std::max<float>(block->rect.ymin, margin);
502 block->rect.ymax = std::min<float>(block->rect.ymax, win_size[1] - UI_POPUP_MENU_TOP);
503
504 /* ensure menu items draw inside left/right boundary */
505 const float xofs = block->rect.xmin - xmin_orig;
506 for (const std::unique_ptr<uiBut> &bt : block->buttons) {
507 bt->rect.xmin += xofs;
508 bt->rect.xmax += xofs;
509 }
510}
511
513{
515
516 for (const std::unique_ptr<uiBut> &bt : block->buttons) {
517 bt->flag &= ~UI_SCROLLED;
518 }
519
520 if (block->buttons.size() < 2) {
521 return;
522 }
523
524 /* mark buttons that are outside boundary */
525 for (const std::unique_ptr<uiBut> &bt : block->buttons) {
526 if (bt->rect.ymin < block->rect.ymin) {
527 bt->flag |= UI_SCROLLED;
528 block->flag |= UI_BLOCK_CLIPBOTTOM;
529 }
530 if (bt->rect.ymax > block->rect.ymax) {
531 bt->flag |= UI_SCROLLED;
532 block->flag |= UI_BLOCK_CLIPTOP;
533 }
534 }
535
536 /* mark buttons overlapping arrows, if we have them */
537 for (const std::unique_ptr<uiBut> &bt : block->buttons) {
538 if (block->flag & UI_BLOCK_CLIPBOTTOM) {
539 if (bt->rect.ymin < block->rect.ymin + UI_MENU_SCROLL_ARROW) {
540 bt->flag |= UI_SCROLLED;
541 }
542 }
543 if (block->flag & UI_BLOCK_CLIPTOP) {
544 if (bt->rect.ymax > block->rect.ymax - UI_MENU_SCROLL_ARROW) {
545 bt->flag |= UI_SCROLLED;
546 }
547 }
548 }
549}
550
552{
553 wmWindow *ctx_win = CTX_wm_window(C);
554 ScrArea *ctx_area = CTX_wm_area(C);
555 ARegion *ctx_region = CTX_wm_region(C);
556
558 wmWindow *win = ctx_win;
559 bScreen *screen = CTX_wm_screen(C);
560
561 /* There may actually be a different window active than the one showing the popup, so lookup real
562 * one. */
563 if (BLI_findindex(&screen->regionbase, handle->region) == -1) {
564 LISTBASE_FOREACH (wmWindow *, win_iter, &wm->windows) {
565 screen = WM_window_get_active_screen(win_iter);
566 if (BLI_findindex(&screen->regionbase, handle->region) != -1) {
567 win = win_iter;
568 break;
569 }
570 }
571 }
572
573 BLI_assert(win && screen);
574
575 CTX_wm_window_set(C, win);
576 ui_region_temp_remove(C, screen, handle->region);
577
578 /* Reset context (area and region were null'ed when changing context window). */
579 CTX_wm_window_set(C, ctx_win);
580 CTX_wm_area_set(C, ctx_area);
581 CTX_wm_region_set(C, ctx_region);
582
583 /* reset to region cursor (only if there's not another menu open) */
584 if (BLI_listbase_is_empty(&screen->regionbase)) {
585 win->tag_cursor_refresh = true;
586 }
587
588 if (handle->scrolltimer) {
589 WM_event_timer_remove(wm, win, handle->scrolltimer);
590 }
591}
592
593void ui_layout_panel_popup_scroll_apply(Panel *panel, const float dy)
594{
595 if (!panel || dy == 0.0f) {
596 return;
597 }
598 for (LayoutPanelBody &body : panel->runtime->layout_panels.bodies) {
599 body.start_y += dy;
600 body.end_y += dy;
601 }
602 for (LayoutPanelHeader &headcer : panel->runtime->layout_panels.headers) {
603 headcer.start_y += dy;
604 headcer.end_y += dy;
605 }
606}
607
609{
610 Panel *&panel = region->runtime->popup_block_panel;
611 if (!panel) {
612 /* Dummy popup panel type. */
613 static PanelType panel_type = []() {
614 PanelType type{};
616 return type;
617 }();
618 panel = BKE_panel_new(&panel_type);
619 }
620 panel->runtime->layout_panels.clear();
621 block->panel = panel;
622 panel->runtime->block = block;
623}
624
626 uiPopupBlockHandle *handle,
627 ARegion *butregion,
628 uiBut *but)
629{
630 const int margin = UI_POPUP_MARGIN;
631 wmWindow *window = CTX_wm_window(C);
632 ARegion *region = handle->region;
633
635 const uiBlockHandleCreateFunc handle_create_func = handle->popup_create_vars.handle_create_func;
636 void *arg = handle->popup_create_vars.arg;
637
638 uiBlock *block_old = static_cast<uiBlock *>(region->runtime->uiblocks.first);
639
640 handle->refresh = (block_old != nullptr);
641
642 BLI_assert(!handle->refresh || handle->can_refresh);
643
644#ifndef NDEBUG
645 wmEvent *event_back = window->eventstate;
646 wmEvent *event_last_back = window->event_last_handled;
647#endif
648
649 /* create ui block */
650 uiBlock *block;
651 if (create_func) {
652 block = create_func(C, region, arg);
653 }
654 else {
655 block = handle_create_func(C, handle, arg);
656 }
657
658 /* Don't create accelerator keys if the parent menu does not have them. */
659 if (but && but->block->flag & UI_BLOCK_NO_ACCELERATOR_KEYS) {
661 }
662
663 /* callbacks _must_ leave this for us, otherwise we can't call UI_block_update_from_old */
664 BLI_assert(!block->endblock);
665
666 /* Ensure we don't use mouse coords here.
667 *
668 * NOTE(@ideasman42): Important because failing to do will cause glitches refreshing the popup.
669 *
670 * - Many popups use #wmEvent::xy to position them.
671 * - Refreshing a pop-up must only ever change it's contents. Consider that refreshing
672 * might be used to show a menu item as grayed out, or change a text label,
673 * we *never* want the popup to move based on the cursor location while refreshing.
674 * - The location of the cursor at the time of creation is stored in:
675 * `handle->popup_create_vars.event_xy` which must be used instead.
676 *
677 * Since it's difficult to control logic which is called indirectly here,
678 * clear the `eventstate` entirely to ensure it's never used when refreshing a popup. */
679#ifndef NDEBUG
680 window->eventstate = nullptr;
681#endif
682
683 if (block->handle) {
684 memcpy(block->handle, handle, sizeof(uiPopupBlockHandle));
685 MEM_delete(handle);
686 handle = block->handle;
687 }
688 else {
689 block->handle = handle;
690 }
691
692 region->regiondata = handle;
693
694 /* set UI_BLOCK_NUMSELECT before UI_block_end() so we get alphanumeric keys assigned */
695 if (but == nullptr) {
696 block->flag |= UI_BLOCK_POPUP;
697 }
698
699 block->flag |= UI_BLOCK_LOOP;
701
702 /* defer this until blocks are translated (below) */
703 block->oldblock = nullptr;
704
705 if (!block->endblock) {
708 window,
710 region,
712 block,
715 }
716
717 /* if this is being created from a button */
718 if (but) {
719 block->aspect = but->block->aspect;
720 ui_popup_block_position(window, butregion, but, block);
721 handle->direction = block->direction;
722 }
723 else {
724 /* Keep a list of these, needed for pull-down menus. */
725 uiSafetyRct *saferct = MEM_callocN<uiSafetyRct>(__func__);
726 saferct->safety = block->safety;
727 BLI_addhead(&block->saferct, saferct);
728 }
729
730 if (block->flag & UI_BLOCK_PIE_MENU) {
731 const int win_width = UI_SCREEN_MARGIN;
732
733 const blender::int2 win_size = WM_window_native_pixel_size(window);
734
736
737 /* only try translation if area is large enough */
738 int x_offset = 0;
739 if (BLI_rctf_size_x(&block->rect) < win_size[0] - (2.0f * win_width)) {
740 if (block->rect.xmin < win_width) {
741 x_offset += win_width - block->rect.xmin;
742 }
743 if (block->rect.xmax > win_size[0] - win_width) {
744 x_offset += win_size[0] - win_width - block->rect.xmax;
745 }
746 }
747
748 int y_offset = 0;
749 if (BLI_rctf_size_y(&block->rect) < win_size[1] - (2.0f * win_width)) {
750 if (block->rect.ymin < win_width) {
751 y_offset += win_width - block->rect.ymin;
752 }
753 if (block->rect.ymax > win_size[1] - win_width) {
754 y_offset += win_size[1] - win_width - block->rect.ymax;
755 }
756 }
757 /* if we are offsetting set up initial data for timeout functionality */
758
759 if ((x_offset != 0) || (y_offset != 0)) {
760 block->pie_data.pie_center_spawned[0] += x_offset;
761 block->pie_data.pie_center_spawned[1] += y_offset;
762
763 UI_block_translate(block, x_offset, y_offset);
764
765 if (U.pie_initial_timeout > 0) {
767 }
768 }
769
770 region->winrct.xmin = 0;
771 region->winrct.xmax = win_size[0];
772 region->winrct.ymin = 0;
773 region->winrct.ymax = win_size[1];
774
776
777 /* lastly set the buttons at the center of the pie menu, ready for animation */
778 if (U.pie_animation_timeout > 0) {
779 for (const std::unique_ptr<uiBut> &but_iter : block->buttons) {
780 if (but_iter->pie_dir != UI_RADIAL_NONE) {
781 BLI_rctf_recenter(&but_iter->rect, UNPACK2(block->pie_data.pie_center_spawned));
782 }
783 }
784 }
785 }
786 else {
787 /* Add an offset to draw the popover arrow. */
788 if ((block->flag & UI_BLOCK_POPOVER) && ELEM(block->direction, UI_DIR_UP, UI_DIR_DOWN)) {
789 /* Keep sync with 'ui_draw_popover_back_impl'. */
790 const float unit_size = U.widget_unit / block->aspect;
791 const float unit_half = unit_size * (block->direction == UI_DIR_DOWN ? 0.5 : -0.5);
792
793 UI_block_translate(block, 0, -unit_half);
794 }
795
796 /* clip block with window boundary */
797 ui_popup_block_clip(window, block);
798
799 /* Avoid menu moving down and losing cursor focus by keeping it at
800 * the same height. */
801 if (handle->refresh && handle->prev_block_rect.ymax > block->rect.ymax) {
803 const float offset = handle->prev_block_rect.ymax - block->rect.ymax;
804 UI_block_translate(block, 0, offset);
805 block->rect.ymin = handle->prev_block_rect.ymin;
806 }
807 }
808
809 handle->prev_block_rect = block->rect;
810
811 /* the block and buttons were positioned in window space as in 2.4x, now
812 * these menu blocks are regions so we bring it back to region space.
813 * additionally we add some padding for the menu shadow or rounded menus */
814 region->winrct.xmin = block->rect.xmin - margin;
815 region->winrct.xmax = block->rect.xmax + margin;
816 region->winrct.ymin = block->rect.ymin - margin;
817 region->winrct.ymax = block->rect.ymax + UI_POPUP_MENU_TOP;
818
819 UI_block_translate(block, -region->winrct.xmin, -region->winrct.ymin);
820 /* Popups can change size, fix scroll offset if a panel was closed. */
821 float ymin = FLT_MAX;
822 float ymax = -FLT_MAX;
823 for (const std::unique_ptr<uiBut> &bt : block->buttons) {
824 ymin = min_ff(ymin, bt->rect.ymin);
825 ymax = max_ff(ymax, bt->rect.ymax);
826 }
827 const int scroll_pad = ui_block_is_menu(block) ? UI_MENU_SCROLL_PAD : UI_UNIT_Y * 0.5f;
828 const float scroll_min = std::min(block->rect.ymax - ymax - scroll_pad, 0.0f);
829 const float scroll_max = std::max(block->rect.ymin - ymin + scroll_pad, 0.0f);
830 handle->scrolloffset = std::clamp(handle->scrolloffset, scroll_min, scroll_max);
831 /* apply scroll offset */
832 if (handle->scrolloffset != 0.0f) {
833 for (const std::unique_ptr<uiBut> &bt : block->buttons) {
834 bt->rect.ymin += handle->scrolloffset;
835 bt->rect.ymax += handle->scrolloffset;
836 }
837 }
838 /* Layout panels are relative to `block->rect.ymax`. Rather than a
839 * scroll, this is a offset applied due to the overflow at the top. */
840 ui_layout_panel_popup_scroll_apply(block->panel, -scroll_min);
841 }
842 /* Apply popup scroll offset to layout panels. */
844
845 if (block_old) {
846 block->oldblock = block_old;
849 }
850
851 /* checks which buttons are visible, sets flags to prevent draw (do after region init) */
853
854 /* Adds sub-window. */
856
857 /* Get `winmat` now that we actually have the sub-window. */
858 wmGetProjectionMatrix(block->winmat, &region->winrct);
859
860 /* notify change and redraw */
861 ED_region_tag_redraw(region);
862
863 ED_region_update_rect(region);
864
865#ifndef NDEBUG
866 window->eventstate = event_back;
867 window->event_last_handled = event_last_back;
868#endif
869
870 return block;
871}
872
874 ARegion *butregion,
875 uiBut *but,
877 uiBlockHandleCreateFunc handle_create_func,
878 void *arg,
879 uiFreeArgFunc arg_free,
880 const bool can_refresh)
881{
882 wmWindow *window = CTX_wm_window(C);
883 uiBut *activebut = UI_context_active_but_get(C);
884
885 /* disable tooltips from buttons below */
886 if (activebut) {
887 UI_but_tooltip_timer_remove(C, activebut);
888 }
889 /* standard cursor by default */
891
892 /* create handle */
893 uiPopupBlockHandle *handle = MEM_new<uiPopupBlockHandle>(__func__);
894
895 /* store context for operator */
896 handle->ctx_area = CTX_wm_area(C);
897 handle->ctx_region = CTX_wm_region(C);
898 handle->can_refresh = can_refresh;
899
900 /* store vars to refresh popup (RGN_REFRESH_UI) */
902 handle->popup_create_vars.handle_create_func = handle_create_func;
903 handle->popup_create_vars.arg = arg;
904 handle->popup_create_vars.arg_free = arg_free;
905 handle->popup_create_vars.but = but;
906 handle->popup_create_vars.butregion = but ? butregion : nullptr;
908
909 /* create area region */
911 handle->region = region;
912
913 static ARegionType type;
914 memset(&type, 0, sizeof(ARegionType));
918 region->runtime->type = &type;
919
920 UI_region_handlers_add(&region->runtime->handlers);
921
922 /* Note that this will be set in the code-path that typically calls refreshing
923 * (that loops over #Screen::regionbase and refreshes regions tagged with #RGN_REFRESH_UI).
924 * Whereas this only runs on initial creation.
925 * Set the region here so drawing logic can rely on it being set.
926 * Note that restoring the previous value may not be needed, it just avoids potential
927 * problems caused by popups manipulating the context which created them.
928 *
929 * The check for `can_refresh` exists because the context when refreshing sets the "region_popup"
930 * so failing to do so here would cause callbacks draw function to have a different context
931 * the first time it's called. Setting this in every context causes button context menus to
932 * fail because setting the "region_popup" causes poll functions to reference the popup region
933 * instead of the region where the button was created, see #121728.
934 *
935 * NOTE(@ideasman42): the logic for which popups run with their region set to
936 * #bContext::wm::region_popup could be adjusted, making this context member depend on
937 * the ability to refresh seems somewhat arbitrary although it does make *some* sense
938 * because accessing the region later (to tag for refreshing for example)
939 * only makes sense if that region supports refreshing. */
940 ARegion *region_popup_prev = nullptr;
941 if (can_refresh) {
942 region_popup_prev = CTX_wm_region_popup(C);
943 CTX_wm_region_popup_set(C, region);
944 }
945
946 uiBlock *block = ui_popup_block_refresh(C, handle, butregion, but);
947 handle = block->handle;
948
949 /* Wait with tooltips until the mouse is moved, button handling will re-enable them on the first
950 * actual mouse move. */
951 block->tooltipdisabled = true;
952
953 if (can_refresh) {
954 CTX_wm_region_popup_set(C, region_popup_prev);
955 }
956
957 /* keep centered on window resizing */
960 }
961
962 return handle;
963}
964
966{
967 bool is_submenu = false;
968
969 /* If this popup is created from a popover which does NOT have keep-open flag set,
970 * then close the popover too. We could extend this to other popup types too. */
971 ARegion *region = handle->popup_create_vars.butregion;
972 if (region != nullptr) {
973 LISTBASE_FOREACH (uiBlock *, block, &region->runtime->uiblocks) {
974 if (block->handle && (block->flag & UI_BLOCK_POPOVER) &&
975 (block->flag & UI_BLOCK_KEEP_OPEN) == 0)
976 {
977 uiPopupBlockHandle *menu = block->handle;
978 menu->menuretval = UI_RETURN_OK;
979 }
980
981 if (ui_block_is_menu(block)) {
982 is_submenu = true;
983 }
984 }
985 }
986
987 /* Clear the status bar text that is set when opening a menu. */
988 if (!is_submenu) {
989 ED_workspace_status_text(C, nullptr);
990 }
991
992 if (handle->popup_create_vars.arg_free) {
994 }
995
996 if (handle->region->runtime->popup_block_panel) {
997 BKE_panel_free(handle->region->runtime->popup_block_panel);
998 }
999
1000 ui_popup_block_remove(C, handle);
1001
1002 MEM_delete(handle);
1003}
1004
bScreen * CTX_wm_screen(const bContext *C)
ARegion * CTX_wm_region_popup(const bContext *C)
ScrArea * CTX_wm_area(const bContext *C)
wmWindow * CTX_wm_window(const bContext *C)
Depsgraph * CTX_data_depsgraph_pointer(const bContext *C)
Scene * CTX_data_scene(const bContext *C)
void CTX_wm_window_set(bContext *C, wmWindow *win)
Main * CTX_data_main(const bContext *C)
void CTX_wm_area_set(bContext *C, ScrArea *area)
void CTX_wm_region_set(bContext *C, ARegion *region)
ARegion * CTX_wm_region(const bContext *C)
wmWindowManager * CTX_wm_manager(const bContext *C)
void CTX_wm_region_popup_set(bContext *C, ARegion *region_popup)
@ PANEL_TYPE_NO_HEADER
void BKE_panel_free(Panel *panel)
Definition screen.cc:557
Panel * BKE_panel_new(PanelType *panel_type)
Definition screen.cc:540
#define BLI_assert(a)
Definition BLI_assert.h:46
int BLI_findindex(const ListBase *listbase, const void *vlink) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:586
#define LISTBASE_FOREACH(type, var, list)
BLI_INLINE bool BLI_listbase_is_empty(const ListBase *lb)
#define LISTBASE_FOREACH_MUTABLE(type, var, list)
void void BLI_freelistN(ListBase *listbase) ATTR_NONNULL(1)
Definition listbase.cc:497
void BLI_addhead(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:91
void void void void void void BLI_duplicatelist(ListBase *dst, const ListBase *src) ATTR_NONNULL(1
MINLINE float max_ff(float a, float b)
MINLINE float min_ff(float a, float b)
MINLINE void copy_v2_v2(float r[2], const float a[2])
MINLINE void copy_v2_v2_int(int r[2], const int a[2])
void BLI_rctf_translate(struct rctf *rect, float x, float y)
Definition rct.cc:573
void BLI_rctf_union(struct rctf *rct_a, const struct rctf *rct_b)
BLI_INLINE float BLI_rctf_cent_y(const struct rctf *rct)
Definition BLI_rect.h:189
BLI_INLINE float BLI_rctf_cent_x(const struct rctf *rct)
Definition BLI_rect.h:185
void BLI_rcti_translate(struct rcti *rect, int x, int y)
Definition rct.cc:566
void BLI_rctf_init(struct rctf *rect, float xmin, float xmax, float ymin, float ymax)
Definition rct.cc:404
void BLI_rctf_recenter(struct rctf *rect, float x, float y)
Definition rct.cc:602
BLI_INLINE float BLI_rctf_size_x(const struct rctf *rct)
Definition BLI_rect.h:202
BLI_INLINE float BLI_rctf_size_y(const struct rctf *rct)
Definition BLI_rect.h:206
void BLI_rctf_init_minmax(struct rctf *rect)
Definition rct.cc:480
#define UNPACK2(a)
#define ELEM(...)
@ RGN_TYPE_TEMPORARY
@ RGN_REFRESH_UI
#define UI_SCALE_FAC
void ED_region_tag_refresh_ui(ARegion *region)
Definition area.cc:668
void ED_region_update_rect(ARegion *region)
Definition area.cc:2300
void ED_region_floating_init(ARegion *region)
Definition area.cc:2305
void ED_workspace_status_text(bContext *C, const char *str)
Definition area.cc:1040
void ED_region_tag_redraw(ARegion *region)
Definition area.cc:639
Read Guarded memory(de)allocation.
#define C
Definition RandGen.cpp:29
void UI_block_update_from_old(const bContext *C, uiBlock *block)
@ UI_BLOCK_THEME_STYLE_POPUP
#define UI_UNIT_Y
void UI_block_theme_style_set(uiBlock *block, char theme_style)
@ UI_BUT_ALIGN_TOP
@ UI_BUT_ALIGN_LEFT
void UI_block_translate(uiBlock *block, float x, float y)
Definition interface.cc:390
void UI_but_tooltip_timer_remove(bContext *C, uiBut *but)
uiBut * UI_context_active_but_get(const bContext *C)
@ UI_DIR_CENTER_X
@ UI_DIR_CENTER_Y
@ UI_DIR_ALL
@ UI_DIR_DOWN
@ UI_DIR_RIGHT
@ UI_DIR_LEFT
@ UI_DIR_UP
@ UI_BLOCK_BOUNDS_POPUP_CENTER
@ UI_RETURN_OK
void UI_block_draw(const bContext *C, uiBlock *block)
void UI_blocklist_free_inactive(const bContext *C, ARegion *region)
@ UI_BLOCK_CLIPBOTTOM
@ UI_BLOCK_LOOP
@ UI_BLOCK_NO_ACCELERATOR_KEYS
@ UI_BLOCK_PIE_MENU
@ UI_BLOCK_KEEP_OPEN
@ UI_BLOCK_POPUP
@ UI_BLOCK_CLIPTOP
@ UI_BLOCK_POPOVER
@ UI_BLOCK_NO_WIN_CLIP
#define UI_SCREEN_MARGIN
#define UI_UNIT_X
uiBlock *(*)(bContext *C, ARegion *region, void *arg1) uiBlockCreateFunc
@ UI_BTYPE_POPOVER
@ UI_BTYPE_PULLDOWN
@ UI_BTYPE_COLOR
void UI_block_end_ex(const bContext *C, Main *bmain, wmWindow *window, Scene *scene, ARegion *region, Depsgraph *depsgraph, uiBlock *block, const int xy[2]=nullptr, int r_xy[2]=nullptr)
void UI_region_handlers_add(ListBase *handlers)
void(*)(void *arg) uiFreeArgFunc
#define NC_WINDOW
Definition WM_types.hh:372
#define NA_EDITED
Definition WM_types.hh:581
#define U
int64_t size() const
bool is_empty() const
uint top
void ui_but_update(uiBut *but)
bool ui_but_menu_draw_as_popover(const uiBut *but)
void ui_block_to_window_rctf(const ARegion *region, const uiBlock *block, rctf *rct_dst, const rctf *rct_src)
Definition interface.cc:165
float ui_block_calc_pie_segment(uiBlock *block, const float event_xy[2])
#define UI_POPUP_MENU_TOP
@ UI_SCROLLED
uiBlock *(*)(bContext *C, uiPopupBlockHandle *handle, void *arg1) uiBlockHandleCreateFunc
@ UI_RADIAL_NONE
#define UI_MENU_SUBMENU_PADDING
#define UI_POPUP_MARGIN
bool ui_block_is_menu(const uiBlock *block) ATTR_WARN_UNUSED_RESULT
#define UI_MENU_SCROLL_PAD
#define UI_MENU_PADDING
#define UI_MENU_SCROLL_ARROW
@ UI_BLOCK_CONTAINS_SUBMENU_BUT
@ UI_PIE_INITIAL_DIRECTION
static void ui_popup_block_position(wmWindow *window, ARegion *butregion, uiBut *but, uiBlock *block)
static void ui_block_region_refresh(const bContext *C, ARegion *region)
static void ui_block_region_popup_window_listener(const wmRegionListenerParams *params)
uiPopupBlockHandle * ui_popup_block_create(bContext *C, ARegion *butregion, uiBut *but, uiBlockCreateFunc create_func, uiBlockHandleCreateFunc handle_create_func, void *arg, uiFreeArgFunc arg_free, const bool can_refresh)
void UI_popup_dummy_panel_set(ARegion *region, uiBlock *block)
static void ui_popup_block_clip(wmWindow *window, uiBlock *block)
void ui_popup_block_scrolltest(uiBlock *block)
void ui_popup_block_free(bContext *C, uiPopupBlockHandle *handle)
void ui_popup_translate(ARegion *region, const int mdiff[2])
uiBlock * ui_popup_block_refresh(bContext *C, uiPopupBlockHandle *handle, ARegion *butregion, uiBut *but)
static void ui_block_region_draw(const bContext *C, ARegion *region)
static void ui_popup_block_remove(bContext *C, uiPopupBlockHandle *handle)
void ui_layout_panel_popup_scroll_apply(Panel *panel, const float dy)
ARegion * ui_region_temp_add(bScreen *screen)
void ui_region_temp_remove(bContext *C, bScreen *screen, ARegion *region)
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
void * MEM_callocN(size_t len, const char *str)
Definition mallocn.cc:118
static int left
VecBase< int32_t, 2 > int2
VecBase< float, 2 > float2
static PyObject * create_func(PyObject *, PyObject *args)
Definition python.cpp:157
#define FLT_MAX
Definition stdcycles.h:14
void(* listener)(const wmRegionListenerParams *params)
void(* draw)(const bContext *C, ARegion *region)
void(* layout)(const bContext *C, ARegion *region)
void * regiondata
ARegionRuntimeHandle * runtime
blender::Vector< LayoutPanelBody > bodies
blender::Vector< LayoutPanelHeader > headers
uiBlock * block
LayoutPanels layout_panels
struct Panel_Runtime * runtime
float pie_center_spawned[2]
float pie_center_init[2]
ListBase regionbase
float xmax
float xmin
float ymax
float ymin
int ymin
int ymax
int xmin
int xmax
float winmat[4][4]
blender::Vector< std::unique_ptr< uiBut > > buttons
PieMenuData pie_data
ListBase saferct
uiBlock * oldblock
uiPopupBlockHandle * handle
eBlockBoundsCalc bounds_type
short content_hints
eButType type
uiBlock * block
uiBlockHandleCreateFunc handle_create_func
uiBlockCreateFunc create_func
uiPopupBlockCreate popup_create_vars
int xy[2]
Definition WM_types.hh:758
unsigned int action
Definition WM_types.hh:355
unsigned int category
Definition WM_types.hh:355
struct wmEvent * eventstate
struct wmEvent * event_last_handled
void WM_cursor_set(wmWindow *win, int curs)
@ WM_CURSOR_DEFAULT
Definition wm_cursors.hh:15
void wmGetProjectionMatrix(float mat[4][4], const rcti *winrct)
blender::int2 WM_window_native_pixel_size(const wmWindow *win)
void WM_event_timer_remove(wmWindowManager *wm, wmWindow *, wmTimer *timer)
bScreen * WM_window_get_active_screen(const wmWindow *win)