Blender  V2.93
interface_region_popup.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2008 Blender Foundation.
17  * All rights reserved.
18  */
19 
26 #include <stdarg.h>
27 #include <stdlib.h>
28 #include <string.h>
29 
30 #include "MEM_guardedalloc.h"
31 
32 #include "DNA_userdef_types.h"
33 
34 #include "BLI_listbase.h"
35 #include "BLI_math.h"
36 #include "BLI_rect.h"
37 #include "BLI_utildefines.h"
38 
39 #include "BKE_context.h"
40 #include "BKE_screen.h"
41 
42 #include "WM_api.h"
43 #include "WM_types.h"
44 
45 #include "UI_interface.h"
46 
47 #include "ED_screen.h"
48 
49 #include "interface_intern.h"
51 
52 /* -------------------------------------------------------------------- */
59 void ui_popup_translate(ARegion *region, const int mdiff[2])
60 {
61  BLI_rcti_translate(&region->winrct, UNPACK2(mdiff));
62 
63  ED_region_update_rect(region);
64 
65  ED_region_tag_redraw(region);
66 
67  /* update blocks */
68  LISTBASE_FOREACH (uiBlock *, block, &region->uiblocks) {
69  uiPopupBlockHandle *handle = block->handle;
70  /* Make empty, will be initialized on next use, see T60608. */
71  BLI_rctf_init(&handle->prev_block_rect, 0, 0, 0, 0);
72 
73  LISTBASE_FOREACH (uiSafetyRct *, saferct, &block->saferct) {
74  BLI_rctf_translate(&saferct->parent, UNPACK2(mdiff));
75  BLI_rctf_translate(&saferct->safety, UNPACK2(mdiff));
76  }
77  }
78 }
79 
80 /* position block relative to but, result is in window space */
81 static void ui_popup_block_position(wmWindow *window,
82  ARegion *butregion,
83  uiBut *but,
84  uiBlock *block)
85 {
86  uiPopupBlockHandle *handle = block->handle;
87 
88  /* Compute button position in window coordinates using the source
89  * button region/block, to position the popup attached to it. */
90  rctf butrct;
91 
92  if (!handle->refresh) {
93  ui_block_to_window_rctf(butregion, but->block, &butrct, &but->rect);
94 
95  /* widget_roundbox_set has this correction too, keep in sync */
96  if (but->type != UI_BTYPE_PULLDOWN) {
97  if (but->drawflag & UI_BUT_ALIGN_TOP) {
98  butrct.ymax += U.pixelsize;
99  }
100  if (but->drawflag & UI_BUT_ALIGN_LEFT) {
101  butrct.xmin -= U.pixelsize;
102  }
103  }
104 
105  handle->prev_butrct = butrct;
106  }
107  else {
108  /* For refreshes, keep same button position so popup doesn't move. */
109  butrct = handle->prev_butrct;
110  }
111 
112  /* Compute block size in window space, based on buttons contained in it. */
113  if (block->rect.xmin == 0.0f && block->rect.xmax == 0.0f) {
114  if (block->buttons.first) {
115  BLI_rctf_init_minmax(&block->rect);
116 
117  LISTBASE_FOREACH (uiBut *, bt, &block->buttons) {
119  bt->rect.xmax += UI_MENU_SUBMENU_PADDING;
120  }
121  BLI_rctf_union(&block->rect, &bt->rect);
122  }
123  }
124  else {
125  /* we're nice and allow empty blocks too */
126  block->rect.xmin = block->rect.ymin = 0;
127  block->rect.xmax = block->rect.ymax = 20;
128  }
129  }
130 
131  ui_block_to_window_rctf(butregion, but->block, &block->rect, &block->rect);
132 
133  /* Compute direction relative to button, based on available space. */
134  const int size_x = BLI_rctf_size_x(&block->rect) + 0.2f * UI_UNIT_X; /* 4 for shadow */
135  const int size_y = BLI_rctf_size_y(&block->rect) + 0.2f * UI_UNIT_Y;
136  const int center_x = (block->direction & UI_DIR_CENTER_X) ? size_x / 2 : 0;
137  const int center_y = (block->direction & UI_DIR_CENTER_Y) ? size_y / 2 : 0;
138 
139  short dir1 = 0, dir2 = 0;
140 
141  if (!handle->refresh) {
142  bool left = 0, right = 0, top = 0, down = 0;
143 
144  const int win_x = WM_window_pixels_x(window);
145  const int win_y = WM_window_pixels_y(window);
146 
147  /* Take into account maximum size so we don't have to flip on refresh. */
148  const float max_size_x = max_ff(size_x, handle->max_size_x);
149  const float max_size_y = max_ff(size_y, handle->max_size_y);
150 
151  /* check if there's space at all */
152  if (butrct.xmin - max_size_x + center_x > 0.0f) {
153  left = 1;
154  }
155  if (butrct.xmax + max_size_x - center_x < win_x) {
156  right = 1;
157  }
158  if (butrct.ymin - max_size_y + center_y > 0.0f) {
159  down = 1;
160  }
161  if (butrct.ymax + max_size_y - center_y < win_y) {
162  top = 1;
163  }
164 
165  if (top == 0 && down == 0) {
166  if (butrct.ymin - max_size_y < win_y - butrct.ymax - max_size_y) {
167  top = 1;
168  }
169  else {
170  down = 1;
171  }
172  }
173 
174  dir1 = (block->direction & UI_DIR_ALL);
175 
176  /* Secondary directions. */
177  if (dir1 & (UI_DIR_UP | UI_DIR_DOWN)) {
178  if (dir1 & UI_DIR_LEFT) {
179  dir2 = UI_DIR_LEFT;
180  }
181  else if (dir1 & UI_DIR_RIGHT) {
182  dir2 = UI_DIR_RIGHT;
183  }
184  dir1 &= (UI_DIR_UP | UI_DIR_DOWN);
185  }
186 
187  if ((dir2 == 0) && (dir1 == UI_DIR_LEFT || dir1 == UI_DIR_RIGHT)) {
188  dir2 = UI_DIR_DOWN;
189  }
190  if ((dir2 == 0) && (dir1 == UI_DIR_UP || dir1 == UI_DIR_DOWN)) {
191  dir2 = UI_DIR_LEFT;
192  }
193 
194  /* no space at all? don't change */
195  if (left || right) {
196  if (dir1 == UI_DIR_LEFT && left == 0) {
197  dir1 = UI_DIR_RIGHT;
198  }
199  if (dir1 == UI_DIR_RIGHT && right == 0) {
200  dir1 = UI_DIR_LEFT;
201  }
202  /* this is aligning, not append! */
203  if (dir2 == UI_DIR_LEFT && right == 0) {
204  dir2 = UI_DIR_RIGHT;
205  }
206  if (dir2 == UI_DIR_RIGHT && left == 0) {
207  dir2 = UI_DIR_LEFT;
208  }
209  }
210  if (down || top) {
211  if (dir1 == UI_DIR_UP && top == 0) {
212  dir1 = UI_DIR_DOWN;
213  }
214  if (dir1 == UI_DIR_DOWN && down == 0) {
215  dir1 = UI_DIR_UP;
216  }
217  BLI_assert(dir2 != UI_DIR_UP);
218  // if (dir2 == UI_DIR_UP && top == 0) { dir2 = UI_DIR_DOWN; }
219  if (dir2 == UI_DIR_DOWN && down == 0) {
220  dir2 = UI_DIR_UP;
221  }
222  }
223 
224  handle->prev_dir1 = dir1;
225  handle->prev_dir2 = dir2;
226  }
227  else {
228  /* For refreshes, keep same popup direct so popup doesn't move
229  * to a totally different position while editing in it. */
230  dir1 = handle->prev_dir1;
231  dir2 = handle->prev_dir2;
232  }
233 
234  /* Compute offset based on direction. */
235  float offset_x = 0, offset_y = 0;
236 
237  /* Ensure buttons don't come between the parent button and the popup, see: T63566. */
238  const float offset_overlap = max_ff(U.pixelsize, 1.0f);
239 
240  if (dir1 == UI_DIR_LEFT) {
241  offset_x = (butrct.xmin - block->rect.xmax) + offset_overlap;
242  if (dir2 == UI_DIR_UP) {
243  offset_y = butrct.ymin - block->rect.ymin - center_y - UI_MENU_PADDING;
244  }
245  else {
246  offset_y = butrct.ymax - block->rect.ymax + center_y + UI_MENU_PADDING;
247  }
248  }
249  else if (dir1 == UI_DIR_RIGHT) {
250  offset_x = (butrct.xmax - block->rect.xmin) - offset_overlap;
251  if (dir2 == UI_DIR_UP) {
252  offset_y = butrct.ymin - block->rect.ymin - center_y - UI_MENU_PADDING;
253  }
254  else {
255  offset_y = butrct.ymax - block->rect.ymax + center_y + UI_MENU_PADDING;
256  }
257  }
258  else if (dir1 == UI_DIR_UP) {
259  offset_y = (butrct.ymax - block->rect.ymin) - offset_overlap;
260  if (dir2 == UI_DIR_RIGHT) {
261  offset_x = butrct.xmax - block->rect.xmax + center_x;
262  }
263  else {
264  offset_x = butrct.xmin - block->rect.xmin - center_x;
265  }
266  /* changed direction? */
267  if ((dir1 & block->direction) == 0) {
268  /* TODO: still do */
269  UI_block_order_flip(block);
270  }
271  }
272  else if (dir1 == UI_DIR_DOWN) {
273  offset_y = (butrct.ymin - block->rect.ymax) + offset_overlap;
274  if (dir2 == UI_DIR_RIGHT) {
275  offset_x = butrct.xmax - block->rect.xmax + center_x;
276  }
277  else {
278  offset_x = butrct.xmin - block->rect.xmin - center_x;
279  }
280  /* changed direction? */
281  if ((dir1 & block->direction) == 0) {
282  /* TODO: still do */
283  UI_block_order_flip(block);
284  }
285  }
286 
287  /* Center over popovers for eg. */
288  if (block->direction & UI_DIR_CENTER_X) {
289  offset_x += BLI_rctf_size_x(&butrct) / ((dir2 == UI_DIR_LEFT) ? 2 : -2);
290  }
291 
292  /* Apply offset, buttons in window coords. */
293  LISTBASE_FOREACH (uiBut *, bt, &block->buttons) {
294  ui_block_to_window_rctf(butregion, but->block, &bt->rect, &bt->rect);
295 
296  BLI_rctf_translate(&bt->rect, offset_x, offset_y);
297 
298  /* ui_but_update recalculates drawstring size in pixels */
299  ui_but_update(bt);
300  }
301 
302  BLI_rctf_translate(&block->rect, offset_x, offset_y);
303 
304  /* Safety calculus. */
305  {
306  const float midx = BLI_rctf_cent_x(&butrct);
307  const float midy = BLI_rctf_cent_y(&butrct);
308 
309  /* when you are outside parent button, safety there should be smaller */
310 
311  const int s1 = 40 * U.dpi_fac;
312  const int s2 = 3 * U.dpi_fac;
313 
314  /* parent button to left */
315  if (midx < block->rect.xmin) {
316  block->safety.xmin = block->rect.xmin - s2;
317  }
318  else {
319  block->safety.xmin = block->rect.xmin - s1;
320  }
321  /* parent button to right */
322  if (midx > block->rect.xmax) {
323  block->safety.xmax = block->rect.xmax + s2;
324  }
325  else {
326  block->safety.xmax = block->rect.xmax + s1;
327  }
328 
329  /* parent button on bottom */
330  if (midy < block->rect.ymin) {
331  block->safety.ymin = block->rect.ymin - s2;
332  }
333  else {
334  block->safety.ymin = block->rect.ymin - s1;
335  }
336  /* parent button on top */
337  if (midy > block->rect.ymax) {
338  block->safety.ymax = block->rect.ymax + s2;
339  }
340  else {
341  block->safety.ymax = block->rect.ymax + s1;
342  }
343 
344  /* exception for switched pulldowns... */
345  if (dir1 && (dir1 & block->direction) == 0) {
346  if (dir2 == UI_DIR_RIGHT) {
347  block->safety.xmax = block->rect.xmax + s2;
348  }
349  if (dir2 == UI_DIR_LEFT) {
350  block->safety.xmin = block->rect.xmin - s2;
351  }
352  }
353  block->direction = dir1;
354  }
355 
356  /* Keep a list of these, needed for pull-down menus. */
357  uiSafetyRct *saferct = MEM_callocN(sizeof(uiSafetyRct), "uiSafetyRct");
358  saferct->parent = butrct;
359  saferct->safety = block->safety;
360  BLI_freelistN(&block->saferct);
361  BLI_duplicatelist(&block->saferct, &but->block->saferct);
362  BLI_addhead(&block->saferct, saferct);
363 }
364 
367 /* -------------------------------------------------------------------- */
371 static void ui_block_region_refresh(const bContext *C, ARegion *region)
372 {
373  ScrArea *ctx_area = CTX_wm_area(C);
374  ARegion *ctx_region = CTX_wm_region(C);
375 
376  if (region->do_draw & RGN_REFRESH_UI) {
377  ScrArea *handle_ctx_area;
378  ARegion *handle_ctx_region;
379 
380  region->do_draw &= ~RGN_REFRESH_UI;
381  LISTBASE_FOREACH_MUTABLE (uiBlock *, block, &region->uiblocks) {
382  uiPopupBlockHandle *handle = block->handle;
383 
384  if (handle->can_refresh) {
385  handle_ctx_area = handle->ctx_area;
386  handle_ctx_region = handle->ctx_region;
387 
388  if (handle_ctx_area) {
389  CTX_wm_area_set((bContext *)C, handle_ctx_area);
390  }
391  if (handle_ctx_region) {
392  CTX_wm_region_set((bContext *)C, handle_ctx_region);
393  }
394 
395  uiBut *but = handle->popup_create_vars.but;
396  ARegion *butregion = handle->popup_create_vars.butregion;
397  ui_popup_block_refresh((bContext *)C, handle, butregion, but);
398  }
399  }
400  }
401 
402  CTX_wm_area_set((bContext *)C, ctx_area);
403  CTX_wm_region_set((bContext *)C, ctx_region);
404 }
405 
406 static void ui_block_region_draw(const bContext *C, ARegion *region)
407 {
408  LISTBASE_FOREACH (uiBlock *, block, &region->uiblocks) {
409  UI_block_draw(C, block);
410  }
411 }
412 
417 {
418  ARegion *region = params->region;
419  wmNotifier *wmn = params->notifier;
420 
421  switch (wmn->category) {
422  case NC_WINDOW: {
423  switch (wmn->action) {
424  case NA_EDITED: {
425  /* window resize */
426  ED_region_tag_refresh_ui(region);
427  break;
428  }
429  }
430  break;
431  }
432  }
433 }
434 
435 static void ui_popup_block_clip(wmWindow *window, uiBlock *block)
436 {
437  const float xmin_orig = block->rect.xmin;
438  const int margin = UI_SCREEN_MARGIN;
439  int winx, winy;
440 
441  if (block->flag & UI_BLOCK_NO_WIN_CLIP) {
442  return;
443  }
444 
445  winx = WM_window_pixels_x(window);
446  winy = WM_window_pixels_y(window);
447 
448  /* shift to left if outside of view */
449  if (block->rect.xmax > winx - margin) {
450  const float xofs = winx - margin - block->rect.xmax;
451  block->rect.xmin += xofs;
452  block->rect.xmax += xofs;
453  }
454  /* shift menus to right if outside of view */
455  if (block->rect.xmin < margin) {
456  const float xofs = (margin - block->rect.xmin);
457  block->rect.xmin += xofs;
458  block->rect.xmax += xofs;
459  }
460 
461  if (block->rect.ymin < margin) {
462  block->rect.ymin = margin;
463  }
464  if (block->rect.ymax > winy - UI_POPUP_MENU_TOP) {
465  block->rect.ymax = winy - UI_POPUP_MENU_TOP;
466  }
467 
468  /* ensure menu items draw inside left/right boundary */
469  const float xofs = block->rect.xmin - xmin_orig;
470  LISTBASE_FOREACH (uiBut *, bt, &block->buttons) {
471  bt->rect.xmin += xofs;
472  bt->rect.xmax += xofs;
473  }
474 }
475 
477 {
479 
480  LISTBASE_FOREACH (uiBut *, bt, &block->buttons) {
481  bt->flag &= ~UI_SCROLLED;
482  }
483 
484  if (block->buttons.first == block->buttons.last) {
485  return;
486  }
487 
488  /* mark buttons that are outside boundary */
489  LISTBASE_FOREACH (uiBut *, bt, &block->buttons) {
490  if (bt->rect.ymin < block->rect.ymin) {
491  bt->flag |= UI_SCROLLED;
492  block->flag |= UI_BLOCK_CLIPBOTTOM;
493  }
494  if (bt->rect.ymax > block->rect.ymax) {
495  bt->flag |= UI_SCROLLED;
496  block->flag |= UI_BLOCK_CLIPTOP;
497  }
498  }
499 
500  /* mark buttons overlapping arrows, if we have them */
501  LISTBASE_FOREACH (uiBut *, bt, &block->buttons) {
502  if (block->flag & UI_BLOCK_CLIPBOTTOM) {
503  if (bt->rect.ymin < block->rect.ymin + UI_MENU_SCROLL_ARROW) {
504  bt->flag |= UI_SCROLLED;
505  }
506  }
507  if (block->flag & UI_BLOCK_CLIPTOP) {
508  if (bt->rect.ymax > block->rect.ymax - UI_MENU_SCROLL_ARROW) {
509  bt->flag |= UI_SCROLLED;
510  }
511  }
512  }
513 }
514 
516 {
517  wmWindow *ctx_win = CTX_wm_window(C);
518  ScrArea *ctx_area = CTX_wm_area(C);
519  ARegion *ctx_region = CTX_wm_region(C);
520 
522  wmWindow *win = ctx_win;
523  bScreen *screen = CTX_wm_screen(C);
524 
525  /* There may actually be a different window active than the one showing the popup, so lookup real
526  * one. */
527  if (BLI_findindex(&screen->regionbase, handle->region) == -1) {
528  LISTBASE_FOREACH (wmWindow *, win_iter, &wm->windows) {
529  screen = WM_window_get_active_screen(win_iter);
530  if (BLI_findindex(&screen->regionbase, handle->region) != -1) {
531  win = win_iter;
532  break;
533  }
534  }
535  }
536 
537  BLI_assert(win && screen);
538 
539  CTX_wm_window_set(C, win);
540  ui_region_temp_remove(C, screen, handle->region);
541 
542  /* Reset context (area and region were NULL'ed when changing context window). */
543  CTX_wm_window_set(C, ctx_win);
544  CTX_wm_area_set(C, ctx_area);
545  CTX_wm_region_set(C, ctx_region);
546 
547  /* reset to region cursor (only if there's not another menu open) */
548  if (BLI_listbase_is_empty(&screen->regionbase)) {
549  win->tag_cursor_refresh = true;
550  }
551 
552  if (handle->scrolltimer) {
553  WM_event_remove_timer(wm, win, handle->scrolltimer);
554  }
555 }
556 
561  uiPopupBlockHandle *handle,
562  ARegion *butregion,
563  uiBut *but)
564 {
565  const int margin = UI_POPUP_MARGIN;
566  wmWindow *window = CTX_wm_window(C);
567  ARegion *region = handle->region;
568 
570  const uiBlockHandleCreateFunc handle_create_func = handle->popup_create_vars.handle_create_func;
571  void *arg = handle->popup_create_vars.arg;
572 
573  uiBlock *block_old = region->uiblocks.first;
574  uiBlock *block;
575 
576  handle->refresh = (block_old != NULL);
577 
578  BLI_assert(!handle->refresh || handle->can_refresh);
579 
580 #ifdef DEBUG
581  wmEvent *event_back = window->eventstate;
582 #endif
583 
584  /* create ui block */
585  if (create_func) {
586  block = create_func(C, region, arg);
587  }
588  else {
589  block = handle_create_func(C, handle, arg);
590  }
591 
592  /* callbacks _must_ leave this for us, otherwise we can't call UI_block_update_from_old */
593  BLI_assert(!block->endblock);
594 
595  /* ensure we don't use mouse coords here! */
596 #ifdef DEBUG
597  window->eventstate = NULL;
598 #endif
599 
600  if (block->handle) {
601  memcpy(block->handle, handle, sizeof(uiPopupBlockHandle));
602  MEM_freeN(handle);
603  handle = block->handle;
604  }
605  else {
606  block->handle = handle;
607  }
608 
609  region->regiondata = handle;
610 
611  /* set UI_BLOCK_NUMSELECT before UI_block_end() so we get alphanumeric keys assigned */
612  if (but == NULL) {
613  block->flag |= UI_BLOCK_POPUP;
614  }
615 
616  block->flag |= UI_BLOCK_LOOP;
618 
619  /* defer this until blocks are translated (below) */
620  block->oldblock = NULL;
621 
622  if (!block->endblock) {
624  C, block, handle->popup_create_vars.event_xy, handle->popup_create_vars.event_xy);
625  }
626 
627  /* if this is being created from a button */
628  if (but) {
629  block->aspect = but->block->aspect;
630  ui_popup_block_position(window, butregion, but, block);
631  handle->direction = block->direction;
632  }
633  else {
634  uiSafetyRct *saferct;
635  /* Keep a list of these, needed for pull-down menus. */
636  saferct = MEM_callocN(sizeof(uiSafetyRct), "uiSafetyRct");
637  saferct->safety = block->safety;
638  BLI_addhead(&block->saferct, saferct);
639  }
640 
641  if (block->flag & UI_BLOCK_RADIAL) {
642  const int win_width = UI_SCREEN_MARGIN;
643  int winx, winy;
644 
645  int x_offset = 0, y_offset = 0;
646 
647  winx = WM_window_pixels_x(window);
648  winy = WM_window_pixels_y(window);
649 
651 
652  /* only try translation if area is large enough */
653  if (BLI_rctf_size_x(&block->rect) < winx - (2.0f * win_width)) {
654  if (block->rect.xmin < win_width) {
655  x_offset += win_width - block->rect.xmin;
656  }
657  if (block->rect.xmax > winx - win_width) {
658  x_offset += winx - win_width - block->rect.xmax;
659  }
660  }
661 
662  if (BLI_rctf_size_y(&block->rect) < winy - (2.0f * win_width)) {
663  if (block->rect.ymin < win_width) {
664  y_offset += win_width - block->rect.ymin;
665  }
666  if (block->rect.ymax > winy - win_width) {
667  y_offset += winy - win_width - block->rect.ymax;
668  }
669  }
670  /* if we are offsetting set up initial data for timeout functionality */
671 
672  if ((x_offset != 0) || (y_offset != 0)) {
673  block->pie_data.pie_center_spawned[0] += x_offset;
674  block->pie_data.pie_center_spawned[1] += y_offset;
675 
676  UI_block_translate(block, x_offset, y_offset);
677 
678  if (U.pie_initial_timeout > 0) {
680  }
681  }
682 
683  region->winrct.xmin = 0;
684  region->winrct.xmax = winx;
685  region->winrct.ymin = 0;
686  region->winrct.ymax = winy;
687 
689 
690  /* lastly set the buttons at the center of the pie menu, ready for animation */
691  if (U.pie_animation_timeout > 0) {
692  LISTBASE_FOREACH (uiBut *, but_iter, &block->buttons) {
693  if (but_iter->pie_dir != UI_RADIAL_NONE) {
694  BLI_rctf_recenter(&but_iter->rect, UNPACK2(block->pie_data.pie_center_spawned));
695  }
696  }
697  }
698  }
699  else {
700  /* Add an offset to draw the popover arrow. */
701  if ((block->flag & UI_BLOCK_POPOVER) && ELEM(block->direction, UI_DIR_UP, UI_DIR_DOWN)) {
702  /* Keep sync with 'ui_draw_popover_back_impl'. */
703  const float unit_size = U.widget_unit / block->aspect;
704  const float unit_half = unit_size * (block->direction == UI_DIR_DOWN ? 0.5 : -0.5);
705 
706  UI_block_translate(block, 0, -unit_half);
707  }
708 
709  /* clip block with window boundary */
710  ui_popup_block_clip(window, block);
711 
712  /* Avoid menu moving down and losing cursor focus by keeping it at
713  * the same height. */
714  if (handle->refresh && handle->prev_block_rect.ymax > block->rect.ymax) {
716  const float offset = handle->prev_block_rect.ymax - block->rect.ymax;
717  UI_block_translate(block, 0, offset);
718  block->rect.ymin = handle->prev_block_rect.ymin;
719  }
720  }
721 
722  handle->prev_block_rect = block->rect;
723 
724  /* the block and buttons were positioned in window space as in 2.4x, now
725  * these menu blocks are regions so we bring it back to region space.
726  * additionally we add some padding for the menu shadow or rounded menus */
727  region->winrct.xmin = block->rect.xmin - margin;
728  region->winrct.xmax = block->rect.xmax + margin;
729  region->winrct.ymin = block->rect.ymin - margin;
730  region->winrct.ymax = block->rect.ymax + UI_POPUP_MENU_TOP;
731 
732  UI_block_translate(block, -region->winrct.xmin, -region->winrct.ymin);
733 
734  /* apply scroll offset */
735  if (handle->scrolloffset != 0.0f) {
736  LISTBASE_FOREACH (uiBut *, bt, &block->buttons) {
737  bt->rect.ymin += handle->scrolloffset;
738  bt->rect.ymax += handle->scrolloffset;
739  }
740  }
741  }
742 
743  if (block_old) {
744  block->oldblock = block_old;
745  UI_block_update_from_old(C, block);
747  }
748 
749  /* checks which buttons are visible, sets flags to prevent draw (do after region init) */
751 
752  /* adds subwindow */
753  ED_region_floating_init(region);
754 
755  /* get winmat now that we actually have the subwindow */
756  wmGetProjectionMatrix(block->winmat, &region->winrct);
757 
758  /* notify change and redraw */
759  ED_region_tag_redraw(region);
760 
761  ED_region_update_rect(region);
762 
763 #ifdef DEBUG
764  window->eventstate = event_back;
765 #endif
766 
767  return block;
768 }
769 
771  ARegion *butregion,
772  uiBut *but,
774  uiBlockHandleCreateFunc handle_create_func,
775  void *arg,
776  void (*arg_free)(void *arg))
777 {
778  wmWindow *window = CTX_wm_window(C);
779  uiBut *activebut = UI_context_active_but_get(C);
780  static ARegionType type;
781  ARegion *region;
782  uiBlock *block;
783  uiPopupBlockHandle *handle;
784 
785  /* disable tooltips from buttons below */
786  if (activebut) {
787  UI_but_tooltip_timer_remove(C, activebut);
788  }
789  /* standard cursor by default */
791 
792  /* create handle */
793  handle = MEM_callocN(sizeof(uiPopupBlockHandle), "uiPopupBlockHandle");
794 
795  /* store context for operator */
796  handle->ctx_area = CTX_wm_area(C);
797  handle->ctx_region = CTX_wm_region(C);
798 
799  /* store vars to refresh popup (RGN_REFRESH_UI) */
801  handle->popup_create_vars.handle_create_func = handle_create_func;
802  handle->popup_create_vars.arg = arg;
803  handle->popup_create_vars.arg_free = arg_free;
804  handle->popup_create_vars.but = but;
805  handle->popup_create_vars.butregion = but ? butregion : NULL;
807 
808  /* don't allow by default, only if popup type explicitly supports it */
809  handle->can_refresh = false;
810 
811  /* create area region */
813  handle->region = region;
814 
815  memset(&type, 0, sizeof(ARegionType));
816  type.draw = ui_block_region_draw;
817  type.layout = ui_block_region_refresh;
818  type.regionid = RGN_TYPE_TEMPORARY;
819  region->type = &type;
820 
822 
823  block = ui_popup_block_refresh(C, handle, butregion, but);
824  handle = block->handle;
825 
826  /* keep centered on window resizing */
829  }
830 
831  return handle;
832 }
833 
835 {
836  /* If this popup is created from a popover which does NOT have keep-open flag set,
837  * then close the popover too. We could extend this to other popup types too. */
838  ARegion *region = handle->popup_create_vars.butregion;
839  if (region != NULL) {
840  LISTBASE_FOREACH (uiBlock *, block, &region->uiblocks) {
841  if (block->handle && (block->flag & UI_BLOCK_POPOVER) &&
842  (block->flag & UI_BLOCK_KEEP_OPEN) == 0) {
843  uiPopupBlockHandle *menu = block->handle;
844  menu->menuretval = UI_RETURN_OK;
845  }
846  }
847  }
848 
849  if (handle->popup_create_vars.arg_free) {
851  }
852 
853  ui_popup_block_remove(C, handle);
854 
855  MEM_freeN(handle);
856 }
857 
struct ScrArea * CTX_wm_area(const bContext *C)
Definition: context.c:714
void CTX_wm_region_set(bContext *C, struct ARegion *region)
Definition: context.c:985
struct wmWindowManager * CTX_wm_manager(const bContext *C)
Definition: context.c:689
struct bScreen * CTX_wm_screen(const bContext *C)
Definition: context.c:709
void CTX_wm_window_set(bContext *C, struct wmWindow *win)
Definition: context.c:942
struct ARegion * CTX_wm_region(const bContext *C)
Definition: context.c:725
void CTX_wm_area_set(bContext *C, struct ScrArea *area)
Definition: context.c:973
struct wmWindow * CTX_wm_window(const bContext *C)
Definition: context.c:699
#define BLI_assert(a)
Definition: BLI_assert.h:58
BLI_INLINE bool BLI_listbase_is_empty(const struct ListBase *lb)
Definition: BLI_listbase.h:124
void BLI_addhead(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition: listbase.c:87
#define LISTBASE_FOREACH(type, var, list)
Definition: BLI_listbase.h:172
void void void void void BLI_duplicatelist(struct ListBase *dst, const struct ListBase *src) ATTR_NONNULL(1
#define LISTBASE_FOREACH_MUTABLE(type, var, list)
Definition: BLI_listbase.h:188
void void BLI_freelistN(struct ListBase *listbase) ATTR_NONNULL(1)
Definition: listbase.c:547
int BLI_findindex(const struct ListBase *listbase, const void *vlink) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
MINLINE float max_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.c:604
BLI_INLINE float BLI_rctf_cent_y(const struct rctf *rct)
Definition: BLI_rect.h:148
BLI_INLINE float BLI_rctf_cent_x(const struct rctf *rct)
Definition: BLI_rect.h:144
void BLI_rcti_translate(struct rcti *rect, int x, int y)
Definition: rct.c:597
void BLI_rctf_init(struct rctf *rect, float xmin, float xmax, float ymin, float ymax)
Definition: rct.c:436
void BLI_rctf_recenter(struct rctf *rect, float x, float y)
Definition: rct.c:618
BLI_INLINE float BLI_rctf_size_x(const struct rctf *rct)
Definition: BLI_rect.h:161
BLI_INLINE float BLI_rctf_size_y(const struct rctf *rct)
Definition: BLI_rect.h:165
void BLI_rctf_union(struct rctf *rct1, const struct rctf *rct2)
void BLI_rctf_init_minmax(struct rctf *rect)
Definition: rct.c:522
#define UNPACK2(a)
#define ELEM(...)
@ RGN_REFRESH_UI
@ RGN_TYPE_TEMPORARY
void ED_region_tag_refresh_ui(struct ARegion *region)
Definition: area.c:695
void ED_region_floating_init(struct ARegion *region)
Definition: area.c:2002
void ED_region_tag_redraw(struct ARegion *region)
Definition: area.c:667
void ED_region_update_rect(struct ARegion *region)
Definition: area.c:1996
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum type
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei GLsizei GLenum type _GL_VOID_RET _GL_VOID GLsizei GLenum GLenum const void *pixels _GL_VOID_RET _GL_VOID const void *pointer _GL_VOID_RET _GL_VOID GLdouble v _GL_VOID_RET _GL_VOID GLfloat v _GL_VOID_RET _GL_VOID GLint GLint i2 _GL_VOID_RET _GL_VOID GLint j _GL_VOID_RET _GL_VOID GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLdouble right
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei GLsizei GLenum type _GL_VOID_RET _GL_VOID GLsizei GLenum GLenum const void *pixels _GL_VOID_RET _GL_VOID const void *pointer _GL_VOID_RET _GL_VOID GLdouble v _GL_VOID_RET _GL_VOID GLfloat v _GL_VOID_RET _GL_VOID GLint GLint i2 _GL_VOID_RET _GL_VOID GLint j _GL_VOID_RET _GL_VOID GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble top
Read Guarded memory(de)allocation.
#define C
Definition: RandGen.cpp:39
#define UI_UNIT_Y
@ UI_BUT_ALIGN_TOP
Definition: UI_interface.h:269
@ UI_BUT_ALIGN_LEFT
Definition: UI_interface.h:270
void UI_block_theme_style_set(uiBlock *block, char theme_style)
Definition: interface.c:3547
void UI_blocklist_free_inactive(const struct bContext *C, struct ListBase *lb)
uiBut * UI_context_active_but_get(const struct bContext *C)
void UI_but_tooltip_timer_remove(struct bContext *C, uiBut *but)
@ UI_BLOCK_BOUNDS_POPUP_CENTER
Definition: UI_interface.h:700
void UI_block_draw(const struct bContext *C, struct uiBlock *block)
@ UI_BLOCK_CLIPBOTTOM
Definition: UI_interface.h:146
@ UI_BLOCK_RADIAL
Definition: UI_interface.h:161
@ UI_BLOCK_LOOP
Definition: UI_interface.h:140
@ UI_BLOCK_KEEP_OPEN
Definition: UI_interface.h:149
@ UI_BLOCK_POPUP
Definition: UI_interface.h:150
@ UI_BLOCK_CLIPTOP
Definition: UI_interface.h:147
@ UI_BLOCK_POPOVER
Definition: UI_interface.h:162
@ UI_BLOCK_NO_WIN_CLIP
Definition: UI_interface.h:145
@ UI_RETURN_OK
Definition: UI_interface.h:178
void UI_block_order_flip(uiBlock *block)
Definition: interface.c:6034
void UI_region_handlers_add(struct ListBase *handlers)
#define UI_SCREEN_MARGIN
Definition: UI_interface.h:103
@ UI_DIR_CENTER_X
Definition: UI_interface.h:126
@ UI_DIR_CENTER_Y
Definition: UI_interface.h:127
@ UI_DIR_ALL
Definition: UI_interface.h:129
@ UI_DIR_DOWN
Definition: UI_interface.h:123
@ UI_DIR_RIGHT
Definition: UI_interface.h:125
@ UI_DIR_LEFT
Definition: UI_interface.h:124
@ UI_DIR_UP
Definition: UI_interface.h:122
#define UI_UNIT_X
void UI_block_end_ex(const struct bContext *C, uiBlock *block, const int xy[2], int r_xy[2])
@ UI_BTYPE_PULLDOWN
Definition: UI_interface.h:362
void UI_block_translate(uiBlock *block, int x, int y)
Definition: interface.c:353
@ UI_BLOCK_THEME_STYLE_POPUP
Definition: UI_interface.h:671
void UI_block_update_from_old(const struct bContext *C, struct uiBlock *block)
uiBlock *(* uiBlockCreateFunc)(struct bContext *C, struct ARegion *region, void *arg1)
Definition: UI_interface.h:619
#define NC_WINDOW
Definition: WM_types.h:277
#define NA_EDITED
Definition: WM_types.h:462
static PyObject * create_func(PyObject *, PyObject *args)
unsigned int U
Definition: btGjkEpa3.h:78
void ui_but_update(uiBut *but)
Definition: interface.c:3811
void ui_block_to_window_rctf(const ARegion *region, uiBlock *block, rctf *rct_dst, const rctf *rct_src)
Definition: interface.c:168
float ui_block_calc_pie_segment(uiBlock *block, const float event_xy[2])
#define UI_POPUP_MENU_TOP
@ UI_RADIAL_NONE
#define UI_MENU_SUBMENU_PADDING
uiBlock *(* uiBlockHandleCreateFunc)(struct bContext *C, struct uiPopupBlockHandle *handle, void *arg1)
@ UI_PIE_INITIAL_DIRECTION
#define UI_POPUP_MARGIN
#define UI_MENU_PADDING
#define UI_MENU_SCROLL_ARROW
@ UI_BLOCK_CONTAINS_SUBMENU_BUT
@ UI_SCROLLED
uiPopupBlockHandle * ui_popup_block_create(bContext *C, ARegion *butregion, uiBut *but, uiBlockCreateFunc create_func, uiBlockHandleCreateFunc handle_create_func, void *arg, void(*arg_free)(void *arg))
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)
uiBlock * ui_popup_block_refresh(bContext *C, uiPopupBlockHandle *handle, ARegion *butregion, uiBut *but)
static void ui_block_region_popup_window_listener(const wmRegionListenerParams *params)
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])
static void ui_block_region_draw(const bContext *C, ARegion *region)
static void ui_popup_block_remove(bContext *C, uiPopupBlockHandle *handle)
void ui_region_temp_remove(bContext *C, bScreen *screen, ARegion *region)
ARegion * ui_region_temp_add(bScreen *screen)
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:41
void *(* MEM_callocN)(size_t len, const char *str)
Definition: mallocn.c:45
static int left
void * regiondata
ListBase handlers
struct ARegionType * type
ListBase uiblocks
void * last
Definition: DNA_listBase.h:47
void * first
Definition: DNA_listBase.h:47
float pie_center_spawned[2]
float pie_center_init[2]
ListBase regionbase
float xmax
Definition: DNA_vec_types.h:85
float xmin
Definition: DNA_vec_types.h:85
float ymax
Definition: DNA_vec_types.h:86
float ymin
Definition: DNA_vec_types.h:86
int ymin
Definition: DNA_vec_types.h:80
int ymax
Definition: DNA_vec_types.h:80
int xmin
Definition: DNA_vec_types.h:79
int xmax
Definition: DNA_vec_types.h:79
float winmat[4][4]
ListBase saferct
uiBlock * oldblock
uiPopupBlockHandle * handle
struct PieMenuData pie_data
ListBase buttons
eBlockBoundsCalc bounds_type
short content_hints
eButType type
uiBlock * block
uiBlockHandleCreateFunc handle_create_func
struct ARegion * butregion
uiBlockCreateFunc create_func
void(* arg_free)(void *arg)
struct ARegion * region
struct wmTimer * scrolltimer
struct ARegion * ctx_region
struct ScrArea * ctx_area
struct uiPopupBlockCreate popup_create_vars
int x
Definition: WM_types.h:581
unsigned int action
Definition: WM_types.h:260
unsigned int category
Definition: WM_types.h:260
struct wmEvent * eventstate
void WM_cursor_set(wmWindow *win, int curs)
Definition: wm_cursors.c:142
@ WM_CURSOR_DEFAULT
Definition: wm_cursors.h:34
void wmGetProjectionMatrix(float mat[4][4], const rcti *winrct)
Definition: wm_subwindow.c:130
void WM_event_remove_timer(wmWindowManager *wm, wmWindow *UNUSED(win), wmTimer *timer)
Definition: wm_window.c:1669
int WM_window_pixels_y(const wmWindow *win)
Definition: wm_window.c:2136
bScreen * WM_window_get_active_screen(const wmWindow *win)
Definition: wm_window.c:2372
int WM_window_pixels_x(const wmWindow *win)
Definition: wm_window.c:2130