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 
20 /** \file
21  * \ingroup edinterface
22  *
23  * PopUp Region (Generic)
24  */
25 
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"
50 #include "interface_regions_intern.h"
51 
52 /* -------------------------------------------------------------------- */
53 /** \name Utility Functions
54  * \{ */
55 
56 /**
57  * Translate any popup regions (so we can drag them).
58  */
ui_popup_translate(ARegion * region,const int mdiff[2])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 */
ui_popup_block_position(wmWindow * window,ARegion * butregion,uiBut * but,uiBlock * block)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) {
118         if (block->content_hints & UI_BLOCK_CONTAINS_SUBMENU_BUT) {
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     /* parent button to left */
312     if (midx < block->rect.xmin) {
313       block->safety.xmin = block->rect.xmin - 3;
314     }
315     else {
316       block->safety.xmin = block->rect.xmin - 40;
317     }
318     /* parent button to right */
319     if (midx > block->rect.xmax) {
320       block->safety.xmax = block->rect.xmax + 3;
321     }
322     else {
323       block->safety.xmax = block->rect.xmax + 40;
324     }
325 
326     /* parent button on bottom */
327     if (midy < block->rect.ymin) {
328       block->safety.ymin = block->rect.ymin - 3;
329     }
330     else {
331       block->safety.ymin = block->rect.ymin - 40;
332     }
333     /* parent button on top */
334     if (midy > block->rect.ymax) {
335       block->safety.ymax = block->rect.ymax + 3;
336     }
337     else {
338       block->safety.ymax = block->rect.ymax + 40;
339     }
340 
341     /* exception for switched pulldowns... */
342     if (dir1 && (dir1 & block->direction) == 0) {
343       if (dir2 == UI_DIR_RIGHT) {
344         block->safety.xmax = block->rect.xmax + 3;
345       }
346       if (dir2 == UI_DIR_LEFT) {
347         block->safety.xmin = block->rect.xmin - 3;
348       }
349     }
350     block->direction = dir1;
351   }
352 
353   /* keep a list of these, needed for pulldown menus */
354   uiSafetyRct *saferct = MEM_callocN(sizeof(uiSafetyRct), "uiSafetyRct");
355   saferct->parent = butrct;
356   saferct->safety = block->safety;
357   BLI_freelistN(&block->saferct);
358   BLI_duplicatelist(&block->saferct, &but->block->saferct);
359   BLI_addhead(&block->saferct, saferct);
360 }
361 
362 /** \} */
363 
364 /* -------------------------------------------------------------------- */
365 /** \name Menu Block Creation
366  * \{ */
367 
ui_block_region_refresh(const bContext * C,ARegion * region)368 static void ui_block_region_refresh(const bContext *C, ARegion *region)
369 {
370   ScrArea *ctx_area = CTX_wm_area(C);
371   ARegion *ctx_region = CTX_wm_region(C);
372 
373   if (region->do_draw & RGN_REFRESH_UI) {
374     ScrArea *handle_ctx_area;
375     ARegion *handle_ctx_region;
376 
377     region->do_draw &= ~RGN_REFRESH_UI;
378     LISTBASE_FOREACH_MUTABLE (uiBlock *, block, &region->uiblocks) {
379       uiPopupBlockHandle *handle = block->handle;
380 
381       if (handle->can_refresh) {
382         handle_ctx_area = handle->ctx_area;
383         handle_ctx_region = handle->ctx_region;
384 
385         if (handle_ctx_area) {
386           CTX_wm_area_set((bContext *)C, handle_ctx_area);
387         }
388         if (handle_ctx_region) {
389           CTX_wm_region_set((bContext *)C, handle_ctx_region);
390         }
391 
392         uiBut *but = handle->popup_create_vars.but;
393         ARegion *butregion = handle->popup_create_vars.butregion;
394         ui_popup_block_refresh((bContext *)C, handle, butregion, but);
395       }
396     }
397   }
398 
399   CTX_wm_area_set((bContext *)C, ctx_area);
400   CTX_wm_region_set((bContext *)C, ctx_region);
401 }
402 
ui_block_region_draw(const bContext * C,ARegion * region)403 static void ui_block_region_draw(const bContext *C, ARegion *region)
404 {
405   LISTBASE_FOREACH (uiBlock *, block, &region->uiblocks) {
406     UI_block_draw(C, block);
407   }
408 }
409 
410 /**
411  * Use to refresh centered popups on screen resizing (for splash).
412  */
ui_block_region_popup_window_listener(wmWindow * UNUSED (win),ScrArea * UNUSED (area),ARegion * region,wmNotifier * wmn,const Scene * UNUSED (scene))413 static void ui_block_region_popup_window_listener(wmWindow *UNUSED(win),
414                                                   ScrArea *UNUSED(area),
415                                                   ARegion *region,
416                                                   wmNotifier *wmn,
417                                                   const Scene *UNUSED(scene))
418 {
419   switch (wmn->category) {
420     case NC_WINDOW: {
421       switch (wmn->action) {
422         case NA_EDITED: {
423           /* window resize */
424           ED_region_tag_refresh_ui(region);
425           break;
426         }
427       }
428       break;
429     }
430   }
431 }
432 
ui_popup_block_clip(wmWindow * window,uiBlock * block)433 static void ui_popup_block_clip(wmWindow *window, uiBlock *block)
434 {
435   const float xmin_orig = block->rect.xmin;
436   const int margin = UI_SCREEN_MARGIN;
437   int winx, winy;
438 
439   if (block->flag & UI_BLOCK_NO_WIN_CLIP) {
440     return;
441   }
442 
443   winx = WM_window_pixels_x(window);
444   winy = WM_window_pixels_y(window);
445 
446   /* shift to left if outside of view */
447   if (block->rect.xmax > winx - margin) {
448     const float xofs = winx - margin - block->rect.xmax;
449     block->rect.xmin += xofs;
450     block->rect.xmax += xofs;
451   }
452   /* shift menus to right if outside of view */
453   if (block->rect.xmin < margin) {
454     const float xofs = (margin - block->rect.xmin);
455     block->rect.xmin += xofs;
456     block->rect.xmax += xofs;
457   }
458 
459   if (block->rect.ymin < margin) {
460     block->rect.ymin = margin;
461   }
462   if (block->rect.ymax > winy - UI_POPUP_MENU_TOP) {
463     block->rect.ymax = winy - UI_POPUP_MENU_TOP;
464   }
465 
466   /* ensure menu items draw inside left/right boundary */
467   const float xofs = block->rect.xmin - xmin_orig;
468   LISTBASE_FOREACH (uiBut *, bt, &block->buttons) {
469     bt->rect.xmin += xofs;
470     bt->rect.xmax += xofs;
471   }
472 }
473 
ui_popup_block_scrolltest(uiBlock * block)474 void ui_popup_block_scrolltest(uiBlock *block)
475 {
476   block->flag &= ~(UI_BLOCK_CLIPBOTTOM | UI_BLOCK_CLIPTOP);
477 
478   LISTBASE_FOREACH (uiBut *, bt, &block->buttons) {
479     bt->flag &= ~UI_SCROLLED;
480   }
481 
482   if (block->buttons.first == block->buttons.last) {
483     return;
484   }
485 
486   /* mark buttons that are outside boundary */
487   LISTBASE_FOREACH (uiBut *, bt, &block->buttons) {
488     if (bt->rect.ymin < block->rect.ymin) {
489       bt->flag |= UI_SCROLLED;
490       block->flag |= UI_BLOCK_CLIPBOTTOM;
491     }
492     if (bt->rect.ymax > block->rect.ymax) {
493       bt->flag |= UI_SCROLLED;
494       block->flag |= UI_BLOCK_CLIPTOP;
495     }
496   }
497 
498   /* mark buttons overlapping arrows, if we have them */
499   LISTBASE_FOREACH (uiBut *, bt, &block->buttons) {
500     if (block->flag & UI_BLOCK_CLIPBOTTOM) {
501       if (bt->rect.ymin < block->rect.ymin + UI_MENU_SCROLL_ARROW) {
502         bt->flag |= UI_SCROLLED;
503       }
504     }
505     if (block->flag & UI_BLOCK_CLIPTOP) {
506       if (bt->rect.ymax > block->rect.ymax - UI_MENU_SCROLL_ARROW) {
507         bt->flag |= UI_SCROLLED;
508       }
509     }
510   }
511 }
512 
ui_popup_block_remove(bContext * C,uiPopupBlockHandle * handle)513 static void ui_popup_block_remove(bContext *C, uiPopupBlockHandle *handle)
514 {
515   wmWindow *ctx_win = CTX_wm_window(C);
516   ScrArea *ctx_area = CTX_wm_area(C);
517   ARegion *ctx_region = CTX_wm_region(C);
518 
519   wmWindowManager *wm = CTX_wm_manager(C);
520   wmWindow *win = ctx_win;
521   bScreen *screen = CTX_wm_screen(C);
522 
523   /* There may actually be a different window active than the one showing the popup, so lookup real
524    * one. */
525   if (BLI_findindex(&screen->regionbase, handle->region) == -1) {
526     LISTBASE_FOREACH (wmWindow *, win_iter, &wm->windows) {
527       screen = WM_window_get_active_screen(win_iter);
528       if (BLI_findindex(&screen->regionbase, handle->region) != -1) {
529         win = win_iter;
530         break;
531       }
532     }
533   }
534 
535   BLI_assert(win && screen);
536 
537   CTX_wm_window_set(C, win);
538   ui_region_temp_remove(C, screen, handle->region);
539 
540   /* Reset context (area and region were NULL'ed when chaning context window). */
541   CTX_wm_window_set(C, ctx_win);
542   CTX_wm_area_set(C, ctx_area);
543   CTX_wm_region_set(C, ctx_region);
544 
545   /* reset to region cursor (only if there's not another menu open) */
546   if (BLI_listbase_is_empty(&screen->regionbase)) {
547     win->tag_cursor_refresh = true;
548   }
549 
550   if (handle->scrolltimer) {
551     WM_event_remove_timer(wm, win, handle->scrolltimer);
552   }
553 }
554 
555 /**
556  * Called for creating new popups and refreshing existing ones.
557  */
ui_popup_block_refresh(bContext * C,uiPopupBlockHandle * handle,ARegion * butregion,uiBut * but)558 uiBlock *ui_popup_block_refresh(bContext *C,
559                                 uiPopupBlockHandle *handle,
560                                 ARegion *butregion,
561                                 uiBut *but)
562 {
563   const int margin = UI_POPUP_MARGIN;
564   wmWindow *window = CTX_wm_window(C);
565   ARegion *region = handle->region;
566 
567   const uiBlockCreateFunc create_func = handle->popup_create_vars.create_func;
568   const uiBlockHandleCreateFunc handle_create_func = handle->popup_create_vars.handle_create_func;
569   void *arg = handle->popup_create_vars.arg;
570 
571   uiBlock *block_old = region->uiblocks.first;
572   uiBlock *block;
573 
574   handle->refresh = (block_old != NULL);
575 
576   BLI_assert(!handle->refresh || handle->can_refresh);
577 
578 #ifdef DEBUG
579   wmEvent *event_back = window->eventstate;
580 #endif
581 
582   /* create ui block */
583   if (create_func) {
584     block = create_func(C, region, arg);
585   }
586   else {
587     block = handle_create_func(C, handle, arg);
588   }
589 
590   /* callbacks _must_ leave this for us, otherwise we can't call UI_block_update_from_old */
591   BLI_assert(!block->endblock);
592 
593   /* ensure we don't use mouse coords here! */
594 #ifdef DEBUG
595   window->eventstate = NULL;
596 #endif
597 
598   if (block->handle) {
599     memcpy(block->handle, handle, sizeof(uiPopupBlockHandle));
600     MEM_freeN(handle);
601     handle = block->handle;
602   }
603   else {
604     block->handle = handle;
605   }
606 
607   region->regiondata = handle;
608 
609   /* set UI_BLOCK_NUMSELECT before UI_block_end() so we get alphanumeric keys assigned */
610   if (but == NULL) {
611     block->flag |= UI_BLOCK_POPUP;
612   }
613 
614   block->flag |= UI_BLOCK_LOOP;
615   UI_block_theme_style_set(block, UI_BLOCK_THEME_STYLE_POPUP);
616 
617   /* defer this until blocks are translated (below) */
618   block->oldblock = NULL;
619 
620   if (!block->endblock) {
621     UI_block_end_ex(
622         C, block, handle->popup_create_vars.event_xy, handle->popup_create_vars.event_xy);
623   }
624 
625   /* if this is being created from a button */
626   if (but) {
627     block->aspect = but->block->aspect;
628     ui_popup_block_position(window, butregion, but, block);
629     handle->direction = block->direction;
630   }
631   else {
632     uiSafetyRct *saferct;
633     /* keep a list of these, needed for pulldown menus */
634     saferct = MEM_callocN(sizeof(uiSafetyRct), "uiSafetyRct");
635     saferct->safety = block->safety;
636     BLI_addhead(&block->saferct, saferct);
637   }
638 
639   if (block->flag & UI_BLOCK_RADIAL) {
640     const int win_width = UI_SCREEN_MARGIN;
641     int winx, winy;
642 
643     int x_offset = 0, y_offset = 0;
644 
645     winx = WM_window_pixels_x(window);
646     winy = WM_window_pixels_y(window);
647 
648     copy_v2_v2(block->pie_data.pie_center_init, block->pie_data.pie_center_spawned);
649 
650     /* only try translation if area is large enough */
651     if (BLI_rctf_size_x(&block->rect) < winx - (2.0f * win_width)) {
652       if (block->rect.xmin < win_width) {
653         x_offset += win_width - block->rect.xmin;
654       }
655       if (block->rect.xmax > winx - win_width) {
656         x_offset += winx - win_width - block->rect.xmax;
657       }
658     }
659 
660     if (BLI_rctf_size_y(&block->rect) < winy - (2.0f * win_width)) {
661       if (block->rect.ymin < win_width) {
662         y_offset += win_width - block->rect.ymin;
663       }
664       if (block->rect.ymax > winy - win_width) {
665         y_offset += winy - win_width - block->rect.ymax;
666       }
667     }
668     /* if we are offsetting set up initial data for timeout functionality */
669 
670     if ((x_offset != 0) || (y_offset != 0)) {
671       block->pie_data.pie_center_spawned[0] += x_offset;
672       block->pie_data.pie_center_spawned[1] += y_offset;
673 
674       UI_block_translate(block, x_offset, y_offset);
675 
676       if (U.pie_initial_timeout > 0) {
677         block->pie_data.flags |= UI_PIE_INITIAL_DIRECTION;
678       }
679     }
680 
681     region->winrct.xmin = 0;
682     region->winrct.xmax = winx;
683     region->winrct.ymin = 0;
684     region->winrct.ymax = winy;
685 
686     ui_block_calc_pie_segment(block, block->pie_data.pie_center_init);
687 
688     /* lastly set the buttons at the center of the pie menu, ready for animation */
689     if (U.pie_animation_timeout > 0) {
690       LISTBASE_FOREACH (uiBut *, but_iter, &block->buttons) {
691         if (but_iter->pie_dir != UI_RADIAL_NONE) {
692           BLI_rctf_recenter(&but_iter->rect, UNPACK2(block->pie_data.pie_center_spawned));
693         }
694       }
695     }
696   }
697   else {
698     /* Add an offset to draw the popover arrow. */
699     if ((block->flag & UI_BLOCK_POPOVER) && ELEM(block->direction, UI_DIR_UP, UI_DIR_DOWN)) {
700       /* Keep sync with 'ui_draw_popover_back_impl'. */
701       const float unit_size = U.widget_unit / block->aspect;
702       const float unit_half = unit_size * (block->direction == UI_DIR_DOWN ? 0.5 : -0.5);
703 
704       UI_block_translate(block, 0, -unit_half);
705     }
706 
707     /* clip block with window boundary */
708     ui_popup_block_clip(window, block);
709 
710     /* Avoid menu moving down and losing cursor focus by keeping it at
711      * the same height. */
712     if (handle->refresh && handle->prev_block_rect.ymax > block->rect.ymax) {
713       if (block->bounds_type != UI_BLOCK_BOUNDS_POPUP_CENTER) {
714         const float offset = handle->prev_block_rect.ymax - block->rect.ymax;
715         UI_block_translate(block, 0, offset);
716         block->rect.ymin = handle->prev_block_rect.ymin;
717       }
718     }
719 
720     handle->prev_block_rect = block->rect;
721 
722     /* the block and buttons were positioned in window space as in 2.4x, now
723      * these menu blocks are regions so we bring it back to region space.
724      * additionally we add some padding for the menu shadow or rounded menus */
725     region->winrct.xmin = block->rect.xmin - margin;
726     region->winrct.xmax = block->rect.xmax + margin;
727     region->winrct.ymin = block->rect.ymin - margin;
728     region->winrct.ymax = block->rect.ymax + UI_POPUP_MENU_TOP;
729 
730     UI_block_translate(block, -region->winrct.xmin, -region->winrct.ymin);
731 
732     /* apply scroll offset */
733     if (handle->scrolloffset != 0.0f) {
734       LISTBASE_FOREACH (uiBut *, bt, &block->buttons) {
735         bt->rect.ymin += handle->scrolloffset;
736         bt->rect.ymax += handle->scrolloffset;
737       }
738     }
739   }
740 
741   if (block_old) {
742     block->oldblock = block_old;
743     UI_block_update_from_old(C, block);
744     UI_blocklist_free_inactive(C, &region->uiblocks);
745   }
746 
747   /* checks which buttons are visible, sets flags to prevent draw (do after region init) */
748   ui_popup_block_scrolltest(block);
749 
750   /* adds subwindow */
751   ED_region_floating_init(region);
752 
753   /* get winmat now that we actually have the subwindow */
754   wmGetProjectionMatrix(block->winmat, &region->winrct);
755 
756   /* notify change and redraw */
757   ED_region_tag_redraw(region);
758 
759   ED_region_update_rect(region);
760 
761 #ifdef DEBUG
762   window->eventstate = event_back;
763 #endif
764 
765   return block;
766 }
767 
ui_popup_block_create(bContext * C,ARegion * butregion,uiBut * but,uiBlockCreateFunc create_func,uiBlockHandleCreateFunc handle_create_func,void * arg,void (* arg_free)(void * arg))768 uiPopupBlockHandle *ui_popup_block_create(bContext *C,
769                                           ARegion *butregion,
770                                           uiBut *but,
771                                           uiBlockCreateFunc create_func,
772                                           uiBlockHandleCreateFunc handle_create_func,
773                                           void *arg,
774                                           void (*arg_free)(void *arg))
775 {
776   wmWindow *window = CTX_wm_window(C);
777   uiBut *activebut = UI_context_active_but_get(C);
778   static ARegionType type;
779   ARegion *region;
780   uiBlock *block;
781   uiPopupBlockHandle *handle;
782 
783   /* disable tooltips from buttons below */
784   if (activebut) {
785     UI_but_tooltip_timer_remove(C, activebut);
786   }
787   /* standard cursor by default */
788   WM_cursor_set(window, WM_CURSOR_DEFAULT);
789 
790   /* create handle */
791   handle = MEM_callocN(sizeof(uiPopupBlockHandle), "uiPopupBlockHandle");
792 
793   /* store context for operator */
794   handle->ctx_area = CTX_wm_area(C);
795   handle->ctx_region = CTX_wm_region(C);
796 
797   /* store vars to refresh popup (RGN_REFRESH_UI) */
798   handle->popup_create_vars.create_func = create_func;
799   handle->popup_create_vars.handle_create_func = handle_create_func;
800   handle->popup_create_vars.arg = arg;
801   handle->popup_create_vars.arg_free = arg_free;
802   handle->popup_create_vars.but = but;
803   handle->popup_create_vars.butregion = but ? butregion : NULL;
804   copy_v2_v2_int(handle->popup_create_vars.event_xy, &window->eventstate->x);
805 
806   /* don't allow by default, only if popup type explicitly supports it */
807   handle->can_refresh = false;
808 
809   /* create area region */
810   region = ui_region_temp_add(CTX_wm_screen(C));
811   handle->region = region;
812 
813   memset(&type, 0, sizeof(ARegionType));
814   type.draw = ui_block_region_draw;
815   type.layout = ui_block_region_refresh;
816   type.regionid = RGN_TYPE_TEMPORARY;
817   region->type = &type;
818 
819   UI_region_handlers_add(&region->handlers);
820 
821   block = ui_popup_block_refresh(C, handle, butregion, but);
822   handle = block->handle;
823 
824   /* keep centered on window resizing */
825   if (block->bounds_type == UI_BLOCK_BOUNDS_POPUP_CENTER) {
826     type.listener = ui_block_region_popup_window_listener;
827   }
828 
829   return handle;
830 }
831 
ui_popup_block_free(bContext * C,uiPopupBlockHandle * handle)832 void ui_popup_block_free(bContext *C, uiPopupBlockHandle *handle)
833 {
834   /* If this popup is created from a popover which does NOT have keep-open flag set,
835    * then close the popover too. We could extend this to other popup types too. */
836   ARegion *region = handle->popup_create_vars.butregion;
837   if (region != NULL) {
838     LISTBASE_FOREACH (uiBlock *, block, &region->uiblocks) {
839       if (block->handle && (block->flag & UI_BLOCK_POPOVER) &&
840           (block->flag & UI_BLOCK_KEEP_OPEN) == 0) {
841         uiPopupBlockHandle *menu = block->handle;
842         menu->menuretval = UI_RETURN_OK;
843       }
844     }
845   }
846 
847   if (handle->popup_create_vars.arg_free) {
848     handle->popup_create_vars.arg_free(handle->popup_create_vars.arg);
849   }
850 
851   ui_popup_block_remove(C, handle);
852 
853   MEM_freeN(handle);
854 }
855 
856 /** \} */
857