Blender V5.3
interface_region_menu_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#include <functional>
16
17#include "MEM_guardedalloc.h"
18
19#include "DNA_userdef_types.h"
20
21#include "BLI_hash.hh"
22#include "BLI_listbase.hh"
23#include "BLI_math_vector_c.hh"
24#include "BLI_rect.hh"
25#include "BLI_string_utf8.hh"
26#include "BLI_utildefines.hh"
27
28#include "BKE_context.hh"
29#include "BKE_report.hh"
30#include "BKE_screen.hh"
31
32#include "WM_api.hh"
33#include "WM_types.hh"
34
35#include "RNA_access.hh"
36
38
39#include "BLT_translation.hh"
40
41#include "ED_screen.hh"
42
43#include "interface_intern.hh"
45
46namespace blender::ui {
47
48/* -------------------------------------------------------------------- */
51
53{
55
56 /* currently only RNA buttons */
57 return ((but->menu_step_func != nullptr) ||
58 (but->rnaprop && RNA_property_type(but->rnaprop) == PROP_ENUM));
59}
60
61int button_menu_step(Button *but, int direction)
62{
63 if (button_menu_step_poll(but)) {
64 if (but->menu_step_func) {
65 return but->menu_step_func(
66 static_cast<bContext *>(but->block->evil_C), direction, but->poin);
67 }
68
69 const int curval = RNA_property_enum_get(&but->rnapoin, but->rnaprop);
70 return RNA_property_enum_step(static_cast<bContext *>(but->block->evil_C),
71 &but->rnapoin,
72 but->rnaprop,
73 curval,
74 direction);
75 }
76
77 printf("%s: cannot cycle button '%s'\n", __func__, but->str.c_str());
78 return 0;
79}
80
82
83/* -------------------------------------------------------------------- */
91
92static uint popup_string_hash(const StringRef str, const bool use_sep)
93{
94 /* sometimes button contains hotkey, sometimes not, strip for proper compare */
95 const size_t sep_index = use_sep ? str.find_first_of(UI_SEP_CHAR) : StringRef::not_found;
96 const StringRef before_hotkey = sep_index == StringRef::not_found ? str :
97 str.substr(0, sep_index);
98
99 return get_default_hash(before_hotkey);
100}
101
103{
104 return get_default_hash(str);
105}
106
107/* but == nullptr read, otherwise set */
109{
110 static uint mem[256];
111 static bool first = true;
112
113 const uint hash = block->puphash;
114 const uint hash_mod = hash & 255;
115
116 if (first) {
117 /* init */
118 memset(mem, -1, sizeof(mem));
119 first = false;
120 }
121
122 if (but) {
123 /* set */
124 mem[hash_mod] = popup_string_hash(but->str, but->flag & BUT_HAS_SEP_CHAR);
125 return nullptr;
126 }
127
128 /* get */
129 for (Button &but_iter : block->buttons()) {
130 /* Prevent labels (typically headings), from being returned in the case the text
131 * happens to matches one of the menu items.
132 * Skip separators too as checking them is redundant. */
134 continue;
135 }
136 if (mem[hash_mod] == popup_string_hash(but_iter.str, but_iter.flag & BUT_HAS_SEP_CHAR)) {
137 return &but_iter;
138 }
139 }
140
141 return nullptr;
142}
143
145{
146 return popup_menu_memory__internal(block, nullptr);
147}
148
150{
151 popup_menu_memory__internal(block, but);
152}
153
155
156/* -------------------------------------------------------------------- */
159
160struct PopupMenu {
165
166 /* Menu hash is created from this, to keep a memory of recently opened menus. */
168
169 int mx, my;
171
172 std::function<void(bContext *C, Layout *layout)> menu_func;
173};
174
180 PopupMenu *pup,
181 const StringRef title,
182 const StringRef block_name)
183{
184 const uiStyle *style = style_get_dpi();
185
186 pup->block = block_begin(C, nullptr, block_name, EmbossType::Pulldown);
187
188 /* A title is only provided when a Menu has a label, this is not always the case, see e.g.
189 * `VIEW3D_MT_edit_mesh_context_menu` -- this specifies its own label inside the draw function
190 * depending on vertex/edge/face mode. We still want to flag the Block (but only insert into
191 * the `puphash` if we have a title provided). Choosing an entry in a menu will still handle
192 * `puphash` later (see `button_activate_exit`) though multiple menus without a label might fight
193 * for the same storage of the menu memory. Using `idname` instead (or in combination with the
194 * label) for the hash could be investigated to solve this. */
196 if (!title.is_empty()) {
197 pup->block->puphash = popup_menu_hash(title);
198 }
199 pup->layout = &block_layout(pup->block,
202 0,
203 0,
204 200,
205 0,
207 style);
208
209 /* NOTE: this intentionally differs from the menu & sub-menu default because many operators
210 * use popups like this to select one of their options -
211 * where having invoke doesn't make sense.
212 * When the menu was opened from a button, use invoke still for compatibility. This used to be
213 * the default and changing now could cause issues. */
216
217 pup->layout->operator_context_set(opcontext);
218
219 if (pup->but) {
220 if (pup->but->context) {
221 pup->layout->context_copy(pup->but->context);
222 }
223 }
224}
225
226static Block *block_func_POPUP(bContext *C, PopupBlockHandle *handle, void *arg_pup)
227{
228 PopupMenu *pup = static_cast<PopupMenu *>(arg_pup);
229
230 int minwidth = 0;
231
232 if (!pup->layout) {
233 popup_menu_create_block(C, pup, pup->title, __func__);
234
235 if (pup->menu_func) {
236 pup->block->handle = handle;
237 pup->menu_func(C, pup->layout);
238 pup->block->handle = nullptr;
239 }
240
241 if (pup->layout->ui_units_x() != 0.0f) {
242 /* Use the minimum width from the layout if it's set. */
243 minwidth = pup->layout->ui_units_x() * UI_UNIT_X;
244 }
245
246 pup->layout = nullptr;
247 }
248
249 /* Find block minimum width. */
250 if (minwidth) {
251 /* Skip. */
252 }
253 else if (pup->but) {
254 /* Minimum width to enforce. */
255 if (!pup->but->drawstr.empty()) {
256 minwidth = BLI_rctf_size_x(&pup->but->rect);
257 }
258 else {
259 /* For buttons with no text, use the minimum (typically icon only). */
260 minwidth = UI_MENU_WIDTH_MIN;
261 }
262 }
263 else {
264 minwidth = UI_MENU_WIDTH_MIN;
265 }
266
267 /* Find block direction. */
268 char direction;
269 if (pup->but) {
270 if (pup->block->direction != 0) {
271 /* allow overriding the direction from menu_func */
272 direction = pup->block->direction;
273 }
274 else {
275 direction = UI_DIR_DOWN;
276 }
277 }
278 else {
279 direction = UI_DIR_DOWN;
280 }
281
282 bool flip = (direction == UI_DIR_DOWN);
283
284 Block *block = pup->block;
285
286 /* in some cases we create the block before the region,
287 * so we set it delayed here if necessary */
288 if (BLI_findindex(&handle->region->runtime->uiblocks, block) == -1) {
289 block_region_set(block, handle->region);
290 }
291
292 block->direction = direction;
293
295
297
298 if (pup->popup) {
299 int offset[2] = {0, 0};
300
301 Button *but_activate = nullptr;
304 block_direction_set(block, direction);
305
306 /* offset the mouse position, possibly based on earlier selection */
307 if (!handle->refresh) {
308 Button *bt;
309 if ((block->flag & BLOCK_POPUP_MEMORY) && (bt = popup_menu_memory_get(block))) {
310 /* position mouse on last clicked item, at 0.8*width of the
311 * button, so it doesn't overlap the text too much, also note
312 * the offset is negative because we are inverse moving the
313 * block to be under the mouse */
314 offset[0] = -(bt->rect.xmin + 0.8f * BLI_rctf_size_x(&bt->rect));
315 offset[1] = -(bt->rect.ymin + 0.5f * UI_UNIT_Y);
316
317 if (button_is_editable(bt)) {
318 but_activate = bt;
319 }
320 }
321 else {
322 /* position mouse at 0.8*width of the button and below the tile
323 * on the first item */
324 offset[0] = 0;
325 for (const Button &but_iter : block->buttons()) {
326 offset[0] = min_ii(offset[0],
327 -(but_iter.rect.xmin + 0.8f * BLI_rctf_size_x(&but_iter.rect)));
328 }
329
330 offset[1] = 2.1 * UI_UNIT_Y;
331
332 for (Button &but_iter : block->buttons()) {
333 if (button_is_editable(&but_iter)) {
334 but_activate = &but_iter;
335 break;
336 }
337 }
338 }
339 copy_v2_v2_int(handle->prev_bounds_offset, offset);
340 }
341 else {
342 copy_v2_v2_int(offset, handle->prev_bounds_offset);
343 }
344
345 /* in rare cases this is needed since moving the popup
346 * to be within the window bounds may move it away from the mouse,
347 * This ensures we set an item to be active. */
348 if (but_activate) {
349 ARegion *region = CTX_wm_region(C);
350 if (region && region->regiontype == RGN_TYPE_TOOLS && but_activate->block &&
351 (but_activate->block->flag & BLOCK_POPUP_HOLD))
352 {
353 /* In Toolbars, highlight the button with select color. */
354 but_activate->flag |= UI_SELECT_DRAW;
355 }
356 button_activate_over(C, handle->region, but_activate);
357 }
358
359 block->minbounds = minwidth;
360 block_bounds_set_menu(block, 1, offset);
361 }
362 else {
363 /* for a header menu we set the direction automatic */
364 if (!pup->slideout && flip) {
365 ARegion *region = CTX_wm_region(C);
366 if (region) {
367 if (RGN_TYPE_IS_HEADER_ANY(region->regiontype)) {
370 }
371 }
372 }
373 }
374
375 block->minbounds = minwidth;
376 block_bounds_set_text(block, 3.0f * UI_UNIT_X);
377 }
378
379 /* if menu slides out of other menu, override direction */
380 if (pup->slideout) {
382 }
383
384 return pup->block;
385}
386
387static void block_free_func_POPUP(void *arg_pup)
388{
389 PopupMenu *pup = static_cast<PopupMenu *>(arg_pup);
390 MEM_delete(pup);
391}
392
394 bContext *C,
395 ARegion *butregion,
396 Button *but,
397 const char *title,
398 std::function<void(bContext *, Layout *)> menu_func,
399 const bool can_refresh)
400{
401 wmWindow *window = CTX_wm_window(C);
402
403 PopupMenu *pup = MEM_new<PopupMenu>(__func__);
404 pup->title = title;
405 /* menu is created from a callback */
406 pup->menu_func = menu_func;
407 if (but) {
408 pup->slideout = block_is_menu(but->block);
409 pup->but = but;
410
411 if (but->type == ButtonType::Pulldown) {
412 WorkspaceStatus status(C);
413 status.item(IFACE_("Search"), ICON_EVENT_SPACEKEY);
414 }
415 }
416
417 if (!but) {
418 /* no button to start from, means we are a popup */
419 pup->mx = window->runtime->eventstate->xy[0];
420 pup->my = window->runtime->eventstate->xy[1];
421 pup->popup = true;
422 }
424 C, butregion, but, nullptr, block_func_POPUP, pup, block_free_func_POPUP, can_refresh);
425
426 if (!but) {
427 handle->popup = true;
428
429 popup_handlers_add(C, &window->runtime->modalhandlers, handle, 0);
431 }
432
433 return handle;
434}
435
437 ARegion *butregion,
438 Button *but,
439 MenuCreateFunc menu_func,
440 void *arg,
441 const bool can_refresh)
442{
444 C,
445 butregion,
446 but,
447 nullptr,
448 [menu_func, arg](bContext *C, Layout *layout) { menu_func(C, layout, arg); },
449 can_refresh);
450}
451
453
454/* -------------------------------------------------------------------- */
457
458static void create_title_button(Layout &layout, const char *title, int icon)
459{
460 Block *block = layout.block();
461 char titlestr[256];
462
463 if (icon) {
464 SNPRINTF_UTF8(titlestr, " %s", title);
465 uiDefIconTextBut(block, ButtonType::Label, icon, titlestr, 0, 0, 200, UI_UNIT_Y, nullptr, "");
466 }
467 else {
468 Button *but = uiDefBut(
469 block, ButtonType::Label, title, 0, 0, 200, UI_UNIT_Y, nullptr, 0.0, 0.0, "");
470 but->drawflag = BUT_TEXT_LEFT;
471 }
472
473 layout.separator();
474}
475
476PopupMenu *popup_menu_begin_ex(bContext *C, const char *title, const char *block_name, int icon)
477{
478 PopupMenu *pup = MEM_new<PopupMenu>(__func__);
479
480 pup->title = title;
481
482 popup_menu_create_block(C, pup, title, block_name);
483
484 /* create in advance so we can let buttons point to retval already */
485 pup->block->handle = MEM_new<PopupBlockHandle>(__func__);
486
487 if (title[0]) {
488 create_title_button(*pup->layout, title, icon);
489 }
490
491 return pup;
492}
493
494PopupMenu *popup_menu_begin(bContext *C, const char *title, int icon)
495{
496 return popup_menu_begin_ex(C, title, __func__, icon);
497}
498
499void popup_menu_but_set(PopupMenu *pup, ARegion *butregion, Button *but)
500{
501 pup->but = but;
502 pup->butregion = butregion;
503}
504
506{
507 wmWindow *window = CTX_wm_window(C);
508
509 pup->popup = true;
510 pup->mx = window->runtime->eventstate->xy[0];
511 pup->my = window->runtime->eventstate->xy[1];
512
513 Button *but = nullptr;
514 ARegion *butregion = nullptr;
515 if (pup->but) {
516 but = pup->but;
517 butregion = pup->butregion;
518 }
519
521 C, butregion, but, nullptr, block_func_POPUP, pup, nullptr, false);
522 menu->popup = true;
523
524 popup_handlers_add(C, &window->runtime->modalhandlers, menu, 0);
526
527 MEM_delete(pup);
528}
529
531{
532 if (!block_is_empty_ex(pup->block, true)) {
533 popup_menu_end(C, pup);
534 return true;
535 }
537 MEM_delete(pup->block->handle);
538 block_free(C, pup->block);
539 MEM_delete(pup);
540 return false;
541}
542
544{
545 return pup->layout;
546}
547
549
550/* -------------------------------------------------------------------- */
553
555{
556 PopupMenu *pup = nullptr;
557 Layout *layout;
558
559 if (!CTX_wm_window(C)) {
560 return;
561 }
562
563 BKE_reports_lock(reports);
564
565 for (Report &report : reports->list) {
566 int icon;
567 const char *msg, *msg_next;
568
569 if (report.type < reports->printlevel) {
570 continue;
571 }
572
573 if (pup == nullptr) {
574 char title[UI_MAX_DRAW_STR];
575 SNPRINTF_UTF8(title, "%s: %s", RPT_("Report"), report.typestr);
576 /* popup_menu stuff does just what we need (but pass meaningful block name) */
577 pup = popup_menu_begin_ex(C, title, __func__, ICON_NONE);
578 layout = popup_menu_layout(pup);
579 }
580 else {
581 layout->separator();
582 }
583
584 /* split each newline into a label */
585 msg = report.message;
586 icon = icon_from_report_type(report.type);
587 do {
588 char buf[UI_MAX_DRAW_STR];
589 msg_next = strchr(msg, '\n');
590 if (msg_next) {
591 msg_next++;
592 BLI_strncpy_utf8(buf, msg, std::min(sizeof(buf), size_t(msg_next - msg)));
593 msg = buf;
594 }
595 layout->label(msg, icon);
596 icon = ICON_NONE;
597 } while ((msg = msg_next) && *msg);
598 }
599
600 BKE_reports_unlock(reports);
601
602 if (pup) {
603 popup_menu_end(C, pup);
604 }
605}
606
608 MenuType *mt,
609 const char *title,
610 const int icon)
611{
613 C,
614 nullptr,
615 nullptr,
616 title,
617 [mt, title, icon](bContext *C, Layout *layout) -> void {
618 if (title && title[0]) {
619 create_title_button(*layout, title, icon);
620 }
621 item_menutype_func(C, layout, mt);
622 },
623 true);
624 handle->srna_owner = mt->rna_ext.srna;
625 STRNCPY_UTF8(handle->menu_idname, mt->idname);
626
627 WorkspaceStatus status(C);
628 if (flag_is_set(mt->flag, MenuTypeFlag::SearchOnKeyPress)) {
629 status.range(IFACE_("Search"), ICON_EVENT_A, ICON_EVENT_Z);
630 }
631 else if (mt->idname[0]) {
632 status.item(IFACE_("Search"), ICON_EVENT_SPACEKEY);
633 }
634}
635
637{
638 MenuType *mt = WM_menutype_find(idname, true);
639
640 if (mt == nullptr) {
641 BKE_reportf(reports, RPT_ERROR, "Menu \"%s\" not found", idname);
642 return OPERATOR_CANCELLED;
643 }
644
645 if (WM_menutype_poll(C, mt) == false) {
646 /* cancel but allow event to pass through, just like operators do */
648 }
649 /* For now always recreate menus on redraw that were invoked with this function. Maybe we want to
650 * make that optional somehow. */
651 const bool allow_refresh = true;
652
653 const char *title = CTX_IFACE_(mt->translation_context, mt->label);
654 if (allow_refresh) {
655 popup_menu_create_from_menutype(C, mt, title, ICON_NONE);
656 }
657 else {
658 /* If no refresh is needed, create the block directly. */
659 PopupMenu *pup = popup_menu_begin(C, title, ICON_NONE);
660 Layout *layout = popup_menu_layout(pup);
661 menutype_draw(C, mt, layout);
662 popup_menu_end(C, pup);
663 }
664
665 return OPERATOR_INTERFACE;
666}
667
669
670/* -------------------------------------------------------------------- */
673
675 BlockCreateFunc func,
676 void *arg,
677 FreeArgFunc arg_free,
678 const bool can_refresh,
679 StructRNA *srna_owner)
680{
681 wmWindow *window = CTX_wm_window(C);
682
684 C, nullptr, nullptr, func, nullptr, arg, arg_free, can_refresh);
685 handle->popup = true;
686 handle->srna_owner = srna_owner;
687
688 /* Clear the status bar. */
689 WorkspaceStatus status(C);
690 status.item(" ", ICON_NONE);
691
692 popup_handlers_add(C, &window->runtime->modalhandlers, handle, 0);
695}
696
698 bContext *C, BlockCreateFunc func, void *arg, FreeArgFunc arg_free, StructRNA *srna_owner)
699{
700 popup_block_invoke_ex(C, func, arg, arg_free, true, srna_owner);
701}
702
704 BlockCreateFunc func,
705 BlockHandleFunc popup_func,
706 BlockCancelFunc cancel_func,
707 void *arg,
708 wmOperator *op)
709{
710 wmWindow *window = CTX_wm_window(C);
711
713 C, nullptr, nullptr, func, nullptr, arg, nullptr, true);
714 handle->popup = true;
715 handle->retvalue = 1;
716
717 handle->popup_op = op;
718 if (op) {
719 handle->srna_owner = op->type->srna;
720 }
721 handle->popup_arg = arg;
722 handle->popup_func = popup_func;
723 handle->cancel_func = cancel_func;
724 // handle->opcontext = opcontext;
725
726 /* Clear the status bar. */
727 WorkspaceStatus status(C);
728 status.item(" ", ICON_NONE);
729
730 popup_handlers_add(C, &window->runtime->modalhandlers, handle, 0);
733}
734
735static void popup_block_template_close_cb(bContext *C, void *arg1, void * /*arg2*/)
736{
737 Block *block = static_cast<Block *>(arg1);
738
739 PopupBlockHandle *handle = block->handle;
740 if (handle == nullptr) {
741 printf("Error: used outside of a popup!\n");
742 return;
743 }
744
745 wmWindow *win = CTX_wm_window(C);
747
748 if (handle->cancel_func) {
749 handle->cancel_func(C, handle->popup_arg);
750 }
751
752 popup_block_close(C, win, block);
753}
754
756{
757 if (block->flag & (BLOCK_KEEP_OPEN | BLOCK_POPOVER)) {
758 return true;
759 }
760 return false;
761}
762
764 const bool cancel_default,
765 FunctionRef<Button *()> confirm_fn,
766 FunctionRef<Button *()> cancel_fn)
767{
768#ifdef _WIN32
769 const bool windows_layout = true;
770#else
771 const bool windows_layout = false;
772#endif
773 FunctionRef<Button *()> *button_functions[2];
774 if (windows_layout) {
775 ARRAY_SET_ITEMS(button_functions, &confirm_fn, &cancel_fn);
776 }
777 else {
778 ARRAY_SET_ITEMS(button_functions, &cancel_fn, &confirm_fn);
779 }
780
781 for (int i = 0; i < ARRAY_SIZE(button_functions); i++) {
782 FunctionRef<Button *()> *but_fn = button_functions[i];
783 if (Button *but = (*but_fn)()) {
784 const bool is_cancel = (but_fn == &cancel_fn);
785 if ((block->flag & BLOCK_LOOP) == 0) {
787 }
788 if (is_cancel == cancel_default) {
789 /* An active button shouldn't exist, if it does, never set another. */
792 }
793 }
794 }
795 }
796}
797
799 wmOperatorType *ot,
800 const std::optional<StringRef> confirm_text_opt,
801 const std::optional<StringRef> cancel_text_opt,
802 const int icon,
803 bool cancel_default,
804 PointerRNA *r_ptr)
805{
806 Block *block = layout->block();
807
808 const StringRef confirm_text = confirm_text_opt.value_or(IFACE_("OK"));
809 const StringRef cancel_text = cancel_text_opt.value_or(IFACE_("Cancel"));
810
811 /* Use a split so both buttons are the same size. */
812 const bool show_confirm = !confirm_text.is_empty();
813 const bool show_cancel = !cancel_text.is_empty();
814 Layout &row = (show_confirm && show_cancel) ? layout->split(0.5f, false) : *layout;
815
816 /* When only one button is shown, make it default. */
817 if (!show_confirm) {
818 cancel_default = true;
819 }
820
821 auto confirm_fn = [&row, &ot, &confirm_text, &icon, &r_ptr, &show_confirm]() -> Button * {
822 if (!show_confirm) {
823 return nullptr;
824 }
825 Block *block = row.block();
826 const Button *but_ref = block->last_but();
827 *r_ptr = row.op(ot, confirm_text, icon, row.operator_context(), UI_ITEM_NONE);
828
829 if (block->buttons_ptrs.is_empty() || but_ref == block->buttons_ptrs.last().get()) {
830 return nullptr;
831 }
832 return block->buttons_ptrs.last().get();
833 };
834
835 auto cancel_fn = [&row, &cancel_text, &show_cancel]() -> Button * {
836 if (!show_cancel) {
837 return nullptr;
838 }
839 Block *block = row.block();
840 Button *but = uiDefIconTextBut(block,
842 ICON_NONE,
843 cancel_text,
844 0,
845 0,
846 UI_UNIT_X, /* Ignored, as a split is used. */
847 UI_UNIT_Y,
848 nullptr,
849 "");
850 return but;
851 };
852
853 popup_block_template_confirm(block, cancel_default, confirm_fn, cancel_fn);
854}
855
856#if 0 /* UNUSED */
857void uiPupBlockOperator(bContext *C,
858 uiBlockCreateFunc func,
859 wmOperator *op,
860 wm::OpCallContext opcontext)
861{
862 wmWindow *window = CTX_wm_window(C);
863
864 PopupBlockHandle *handle = popup_block_create(C, nullptr, nullptr, func, nullptr, op, nullptr, true);
865 handle->popup = 1;
866 handle->retvalue = 1;
867
868 handle->popup_arg = op;
869 handle->popup_func = operator_cb;
870 handle->cancel_func = confirm_cancel_operator;
871 handle->opcontext = opcontext;
872
873 popup_handlers_add(C, &window->runtime->modalhandlers, handle, 0);
875}
876#endif
877
879{
880 /* if loading new .blend while popup is open, window will be nullptr */
881 if (block->handle) {
882 if (win) {
883 const bScreen *screen = WM_window_get_active_screen(win);
884
886 popup_block_free(C, block->handle);
887
888 /* In the case we have nested popups,
889 * closing one may need to redraw another, see: #48874 */
890 for (ARegion &region : screen->regionbase) {
892 }
893 }
894 }
895
896 ED_workspace_status_text(C, nullptr);
897}
898
899bool popup_block_name_exists(const bScreen *screen, const StringRef name)
900{
901 for (const ARegion &region : screen->regionbase) {
902 for (const Block &block : region.runtime->uiblocks) {
903 if (block.name == name) {
904 return true;
905 }
906 }
907 }
908 return false;
909}
910
911bool popup_region_from_operator(const bScreen *screen, const ARegion *region)
912{
913 /* Walk up parent popups, e.g. a popover opened from an operator dialog. Parents may
914 * already be closed, as with a context menu opened from a popover. */
915 while (region && BLI_findindex(&screen->regionbase, region) != -1) {
916 const Block *block = region->runtime->uiblocks.first();
917 if (!(block && block->handle)) {
918 return false;
919 }
920 if (block->handle->popup_op) {
921 return true;
922 }
923 region = block->handle->ctx_region_popup;
924 }
925 return false;
926}
927
928void popup_menu_close(const Block *block, const bool is_cancel)
929{
930 popup_menu_retval_set(block, is_cancel ? RETURN_CANCEL : RETURN_OK, true);
931}
932
933void popup_menu_close_from_but(const Button *but, const bool is_cancel)
934{
935 popup_menu_close(but->block, is_cancel);
936}
937
939
940} // namespace blender::ui
#define SNPRINTF_UTF8(dst, format,...)
#define STRNCPY_UTF8(dst, src)
#define RPT_(msgid)
#define CTX_IFACE_(context, msgid)
#define IFACE_(msgid)
#define RGN_ALIGN_ENUM_FROM_MASK(align)
#define RGN_TYPE_IS_HEADER_ANY(regiontype)
Read Blender memory allocation and freeing.
#define C
Definition RandGen.cpp:29
#define UI_UNIT_Y
#define UI_SEP_CHAR
#define UI_UNIT_X
#define UI_ITEM_NONE
#define UI_MAX_DRAW_STR
#define ARRAY_SET_ITEMS(...)
#define ELEM(...)
void item(std::string text, int icon1, int icon2=0)
Definition area.cc:1076
void range(std::string text, int icon1, int icon2)
Definition area.cc:1088
static constexpr int64_t not_found
constexpr bool is_empty() const
#define str(s)
#define UI_MENU_PADDING
#define UI_MENU_WIDTH_MIN
Button * uiDefIconTextBut(Block *block, ButtonTypeWithPointerType but_and_ptr_type, int icon, StringRef str, int x, int y, short width, short height, void *poin, std::optional< StringRef > tip)
void item_menutype_func(bContext *C, Layout *layout, void *arg_mt)
int icon_from_report_type(int type)
void block_direction_set(Block *block, char direction)
void(*)(bContext *C, Layout *layout, void *arg1) MenuCreateFunc
void popup_block_close(bContext *C, wmWindow *win, Block *block)
static Block * block_func_POPUP(bContext *C, PopupBlockHandle *handle, void *arg_pup)
void button_func_set(Button *but, std::function< void(bContext &)> func)
bool button_menu_step_poll(const Button *but)
void popup_block_template_confirm_op(Layout *layout, wmOperatorType *ot, std::optional< StringRef > confirm_text, std::optional< StringRef > cancel_text, const int icon, bool cancel_default, PointerRNA *r_ptr)
void popup_block_ex(bContext *C, BlockCreateFunc func, BlockHandleFunc popup_func, BlockCancelFunc cancel_func, void *arg, wmOperator *op)
void popup_block_free(bContext *C, PopupBlockHandle *handle)
static PopupBlockHandle * popup_menu_create_impl(bContext *C, ARegion *butregion, Button *but, const char *title, std::function< void(bContext *, Layout *)> menu_func, const bool can_refresh)
void popup_menu_but_set(PopupMenu *pup, ARegion *butregion, Button *but)
wmOperatorStatus popup_menu_invoke(bContext *C, const char *idname, ReportList *reports) ATTR_NONNULL(1
PopupMenu * popup_menu_begin_ex(bContext *C, const char *title, const char *block_name, int icon) ATTR_NONNULL()
void(*)(void *arg) FreeArgFunc
void block_free(const bContext *C, Block *block)
PopupMenu * popup_menu_begin(bContext *C, const char *title, int icon) ATTR_NONNULL()
static void popup_block_template_close_cb(bContext *C, void *arg1, void *)
bool popup_menu_end_or_cancel(bContext *C, PopupMenu *pup)
void block_flag_enable(Block *block, int flag)
Button * popup_menu_memory_get(Block *block)
bool block_is_menu(const Block *block) ATTR_WARN_UNUSED_RESULT
const uiStyle * style_get_dpi()
void popup_block_invoke(bContext *C, BlockCreateFunc func, void *arg, FreeArgFunc arg_free, StructRNA *srna_owner=nullptr)
PopupBlockHandle * popup_menu_create(bContext *C, ARegion *butregion, Button *but, MenuCreateFunc menu_func, void *arg, bool can_refresh)
void block_bounds_set_text(Block *block, int addval)
Definition interface.cc:661
bool block_active_only_flagged_buttons(const bContext *C, ARegion *region, Block *block)
wmOperatorStatus void popup_menu_close(const Block *block, bool is_cancel=false)
void button_flag_enable(Button *but, int64_t flag)
bool block_has_active_default_button(const Block *block)
void popup_handlers_add(bContext *C, ListBaseT< wmEventHandler > *handlers, PopupBlockHandle *popup, char flag)
Block * block_begin(const bContext *C, ARegion *region, std::string name, EmbossType emboss)
Layout * popup_menu_layout(PopupMenu *pup)
void(*)(bContext *C, void *arg, int event) BlockHandleFunc
Button * uiDefBut(Block *block, ButtonTypeWithPointerType but_and_ptr_type, StringRef str, int x, int y, short width, short height, void *poin, float min, float max, std::optional< StringRef > tip)
static void block_free_func_POPUP(void *arg_pup)
Block *(*)(bContext *C, ARegion *region, void *arg1) BlockCreateFunc
void popup_handlers_remove(ListBaseT< wmEventHandler > *handlers, PopupBlockHandle *popup)
void block_theme_style_set(Block *block, char theme_style)
static void popup_menu_create_block(bContext *C, PopupMenu *pup, const StringRef title, const StringRef block_name)
bool popup_region_from_operator(const bScreen *screen, const ARegion *region)
void popup_menu_retval_set(const Block *block, int retval, bool enable)
int button_menu_step(Button *but, int direction)
PopupBlockHandle * popup_block_create(bContext *C, ARegion *butregion, Button *but, BlockCreateFunc create_func, BlockHandleCreateFunc handle_create_func, void *arg, FreeArgFunc arg_free, bool can_refresh)
bool popup_block_template_confirm_is_supported(const Block *block)
static uint popup_string_hash(const StringRef str, const bool use_sep)
bool button_is_editable(const Button *but) ATTR_WARN_UNUSED_RESULT
void popup_menu_reports(bContext *C, ReportList *reports) ATTR_NONNULL()
int2 block_layout_resolve(Block *block)
void menutype_draw(bContext *C, MenuType *mt, Layout *layout)
bool block_is_empty_ex(const Block *block, bool skip_title)
void(*)(bContext *C, void *arg1) BlockCancelFunc
static Button * popup_menu_memory__internal(Block *block, Button *but)
void popup_block_invoke_ex(bContext *C, BlockCreateFunc func, void *arg, FreeArgFunc arg_free, bool can_refresh, StructRNA *srna_owner=nullptr)
uint popup_menu_hash(const StringRef str)
void button_activate_over(bContext *C, ARegion *region, Button *but)
void block_region_set(Block *block, ARegion *region)
void block_bounds_set_menu(Block *block, int addval, const int bounds_offset[2])
Definition interface.cc:681
void popup_menu_memory_set(Block *block, Button *but)
static void create_title_button(Layout &layout, const char *title, int icon)
Layout & block_layout(Block *block, LayoutDirection direction, LayoutType type, int x, int y, int size, int em, int padding, const uiStyle *style)
void popup_menu_end(bContext *C, PopupMenu *pup)
void popup_block_template_confirm(Block *block, bool cancel_default, FunctionRef< Button *()> confirm_fn, FunctionRef< Button *()> cancel_fn)
static void popup_menu_create_from_menutype(bContext *C, MenuType *mt, const char *title, const int icon)
bool popup_block_name_exists(const bScreen *screen, StringRef name)
void popup_menu_close_from_but(const Button *but, bool is_cancel=false)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
MINLINE void copy_v2_v2_int(int r[2], const int a[2])
unsigned int uint
Definition BKE_text.h:55
int RNA_property_enum_get(PointerRNA *ptr, PropertyRNA *prop)
MenuType * WM_menutype_find(const StringRef idname, bool quiet)
char * BLI_strncpy_utf8(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_NONNULL(1
wmWindow * CTX_wm_window(const bContext *C)
PropertyType RNA_property_type(PropertyRNA *prop)
int BLI_findindex(const ListBase *listbase, const void *vlink) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:593
void ED_region_tag_refresh_ui(ARegion *region)
Definition area.cc:738
void BKE_reports_unlock(ReportList *reports)
Definition report.cc:145
constexpr size_t ARRAY_SIZE(const T(&arg)[N]) noexcept
bool WM_menutype_poll(bContext *C, MenuType *mt)
int RNA_property_enum_step(const bContext *C, PointerRNA *ptr, PropertyRNA *prop, int from_value, int step)
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
Definition report.cc:191
BLI_INLINE float BLI_rctf_size_x(const struct rctf *rct)
Definition BLI_rect.hh:211
void BKE_reports_lock(ReportList *reports)
Definition report.cc:140
void WM_event_add_mousemove(wmWindow *win)
bScreen * WM_window_get_active_screen(const wmWindow *win)
MINLINE int min_ii(int a, int b)
constexpr uint64_t get_default_hash(const T &v, const Args &...args)
Definition BLI_hash.hh:252
ARegion * CTX_wm_region(const bContext *C)
void ED_workspace_status_text(bContext *C, const char *str)
Definition area.cc:1153
#define hash
Definition noise_c.cc:156
bke::ARegionRuntime * runtime
eRegion_Alignment alignment
eRegion_Type regiontype
char idname[BKE_ST_MAXNAME]
char translation_context[BKE_ST_MAXNAME]
char label[BKE_ST_MAXNAME]
ExtensionRNA rna_ext
MenuTypeFlag flag
ListBaseT< Report > list
Definition BKE_report.hh:79
const char * typestr
Definition BKE_report.hh:72
const char * message
Definition BKE_report.hh:73
ListBaseT< ARegion > regionbase
ListBaseT< ui::Block > uiblocks
ListBaseT< wmEventHandler > modalhandlers
Button * last_but() const
Definition interface.cc:311
Vector< std::unique_ptr< Button > > buttons_ptrs
std::ranges::transform_view< std::ranges::ref_view< const Vector< std::unique_ptr< Button > > >, Button &(*)(const std::unique_ptr< Button > &)> buttons() const
PopupBlockHandle * handle
const bContextStore * context
void operator_context_set(wm::OpCallContext opcontext)
wm::OpCallContext operator_context() const
void label(StringRef name, int icon)
PointerRNA op(wmOperatorType *ot, std::optional< StringRef > name, int icon, wm::OpCallContext context, eUI_Item_Flag flag)
Layout & split(float percentage, bool align)
void separator(float factor=1.0f, LayoutSeparatorType type=LayoutSeparatorType::Auto)
void context_copy(const bContextStore *context)
void(*) cancel_func(bContext *C, void *arg)
void(*) popup_func(bContext *C, void *arg, int event)
std::function< void(bContext *C, Layout *layout)> menu_func
struct wmOperatorType * type
bke::WindowRuntime * runtime