b62857983df9a0efe812ad04256f484007f95977
[blender.git] / source / blender / editors / interface / interface_region_popup.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2008 Blender Foundation.
19  * All rights reserved.
20  *
21  * Contributor(s): Blender Foundation
22  *
23  * ***** END GPL LICENSE BLOCK *****
24  */
25
26 /** \file blender/editors/interface/interface_region_popup.c
27  *  \ingroup edinterface
28  *
29  * PopUp Region (Generic)
30  */
31
32 #include <stdarg.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <assert.h>
36
37 #include "MEM_guardedalloc.h"
38
39 #include "DNA_userdef_types.h"
40
41 #include "BLI_math.h"
42 #include "BLI_listbase.h"
43 #include "BLI_rect.h"
44 #include "BLI_utildefines.h"
45
46 #include "BKE_context.h"
47 #include "BKE_screen.h"
48
49 #include "WM_api.h"
50 #include "WM_types.h"
51
52 #include "UI_interface.h"
53
54 #include "ED_screen.h"
55
56 #include "interface_intern.h"
57 #include "interface_regions_intern.h"
58
59 /* -------------------------------------------------------------------- */
60 /** \name Utility Functions
61  * \{ */
62
63 /**
64  * Translate any popup regions (so we can drag them).
65  */
66 void ui_popup_translate(ARegion *ar, const int mdiff[2])
67 {
68         uiBlock *block;
69
70         BLI_rcti_translate(&ar->winrct, UNPACK2(mdiff));
71
72         ED_region_update_rect(ar);
73
74         ED_region_tag_redraw(ar);
75
76         /* update blocks */
77         for (block = ar->uiblocks.first; block; block = block->next) {
78                 uiSafetyRct *saferct;
79                 for (saferct = block->saferct.first; saferct; saferct = saferct->next) {
80                         BLI_rctf_translate(&saferct->parent, UNPACK2(mdiff));
81                         BLI_rctf_translate(&saferct->safety, UNPACK2(mdiff));
82                 }
83         }
84 }
85
86 /* position block relative to but, result is in window space */
87 static void ui_popup_block_position(wmWindow *window, ARegion *butregion, uiBut *but, uiBlock *block)
88 {
89         uiPopupBlockHandle *handle = block->handle;
90
91         /* Compute button position in window coordinates using the source
92          * button region/block, to position the popup attached to it. */
93         rctf butrct;
94
95         if (!handle->refresh) {
96                 ui_block_to_window_rctf(butregion, but->block, &butrct, &but->rect);
97
98                 /* widget_roundbox_set has this correction too, keep in sync */
99                 if (but->type != UI_BTYPE_PULLDOWN) {
100                         if (but->drawflag & UI_BUT_ALIGN_TOP)
101                                 butrct.ymax += U.pixelsize;
102                         if (but->drawflag & UI_BUT_ALIGN_LEFT)
103                                 butrct.xmin -= U.pixelsize;
104                 }
105
106                 handle->prev_butrct = butrct;
107         }
108         else {
109                 /* For refreshes, keep same button position so popup doesn't move. */
110                 butrct = handle->prev_butrct;
111         }
112
113         /* Compute block size in window space, based on buttons contained in it. */
114         if (block->rect.xmin == 0.0f && block->rect.xmax == 0.0f) {
115                 if (block->buttons.first) {
116                         BLI_rctf_init_minmax(&block->rect);
117
118                         for (uiBut *bt = block->buttons.first; bt; bt = bt->next) {
119                                 if (block->content_hints & UI_BLOCK_CONTAINS_SUBMENU_BUT) {
120                                         bt->rect.xmax += UI_MENU_SUBMENU_PADDING;
121                                 }
122                                 BLI_rctf_union(&block->rect, &bt->rect);
123                         }
124                 }
125                 else {
126                         /* we're nice and allow empty blocks too */
127                         block->rect.xmin = block->rect.ymin = 0;
128                         block->rect.xmax = block->rect.ymax = 20;
129                 }
130         }
131
132         ui_block_to_window_rctf(butregion, but->block, &block->rect, &block->rect);
133
134         /* Compute direction relative to button, based on available space. */
135         const int size_x = BLI_rctf_size_x(&block->rect) + 0.2f * UI_UNIT_X;  /* 4 for shadow */
136         const int size_y = BLI_rctf_size_y(&block->rect) + 0.2f * UI_UNIT_Y;
137         const int center_x = (block->direction & UI_DIR_CENTER_X) ? size_x / 2 : 0;
138         const int center_y = (block->direction & UI_DIR_CENTER_Y) ? size_y / 2 : 0;
139
140         short dir1 = 0, dir2 = 0;
141
142         if (!handle->refresh) {
143                 bool left = 0, right = 0, top = 0, down = 0;
144
145                 const int win_x = WM_window_pixels_x(window);
146                 const int win_y = WM_window_pixels_y(window);
147
148                 /* Take into account maximum size so we don't have to flip on refresh. */
149                 const float max_size_x = max_ff(size_x, handle->max_size_x);
150                 const float max_size_y = max_ff(size_y, handle->max_size_y);
151
152                 /* check if there's space at all */
153                 if (butrct.xmin - max_size_x + center_x > 0.0f) left = 1;
154                 if (butrct.xmax + max_size_x - center_x < win_x) right = 1;
155                 if (butrct.ymin - max_size_y + center_y > 0.0f) down = 1;
156                 if (butrct.ymax + max_size_y - center_y < win_y) top = 1;
157
158                 if (top == 0 && down == 0) {
159                         if (butrct.ymin - max_size_y < win_y - butrct.ymax - max_size_y)
160                                 top = 1;
161                         else
162                                 down = 1;
163                 }
164
165                 dir1 = (block->direction & UI_DIR_ALL);
166
167                 /* Secondary directions. */
168                 if (dir1 & (UI_DIR_UP | UI_DIR_DOWN)) {
169                         if      (dir1 & UI_DIR_LEFT)  dir2 = UI_DIR_LEFT;
170                         else if (dir1 & UI_DIR_RIGHT) dir2 = UI_DIR_RIGHT;
171                         dir1 &= (UI_DIR_UP | UI_DIR_DOWN);
172                 }
173
174                 if ((dir2 == 0) && (dir1 == UI_DIR_LEFT || dir1 == UI_DIR_RIGHT)) dir2 = UI_DIR_DOWN;
175                 if ((dir2 == 0) && (dir1 == UI_DIR_UP   || dir1 == UI_DIR_DOWN))  dir2 = UI_DIR_LEFT;
176
177                 /* no space at all? don't change */
178                 if (left || right) {
179                         if (dir1 == UI_DIR_LEFT  && left == 0)  dir1 = UI_DIR_RIGHT;
180                         if (dir1 == UI_DIR_RIGHT && right == 0) dir1 = UI_DIR_LEFT;
181                         /* this is aligning, not append! */
182                         if (dir2 == UI_DIR_LEFT  && right == 0) dir2 = UI_DIR_RIGHT;
183                         if (dir2 == UI_DIR_RIGHT && left == 0)  dir2 = UI_DIR_LEFT;
184                 }
185                 if (down || top) {
186                         if (dir1 == UI_DIR_UP   && top == 0)  dir1 = UI_DIR_DOWN;
187                         if (dir1 == UI_DIR_DOWN && down == 0) dir1 = UI_DIR_UP;
188                         BLI_assert(dir2 != UI_DIR_UP);
189 //                      if (dir2 == UI_DIR_UP   && top == 0)  dir2 = UI_DIR_DOWN;
190                         if (dir2 == UI_DIR_DOWN && down == 0) dir2 = UI_DIR_UP;
191                 }
192
193                 handle->prev_dir1 = dir1;
194                 handle->prev_dir2 = dir2;
195         }
196         else {
197                 /* For refreshes, keep same popup direct so popup doesn't move
198                  * to a totally different position while editing in it. */
199                 dir1 = handle->prev_dir1;
200                 dir2 = handle->prev_dir2;
201         }
202
203         /* Compute offset based on direction. */
204         int offset_x = 0, offset_y = 0;
205
206         if (dir1 == UI_DIR_LEFT) {
207                 offset_x = butrct.xmin - block->rect.xmax;
208                 if (dir2 == UI_DIR_UP) offset_y = butrct.ymin - block->rect.ymin - center_y - MENU_PADDING;
209                 else                   offset_y = butrct.ymax - block->rect.ymax + center_y + MENU_PADDING;
210         }
211         else if (dir1 == UI_DIR_RIGHT) {
212                 offset_x = butrct.xmax - block->rect.xmin;
213                 if (dir2 == UI_DIR_UP) offset_y = butrct.ymin - block->rect.ymin - center_y - MENU_PADDING;
214                 else                   offset_y = butrct.ymax - block->rect.ymax + center_y + MENU_PADDING;
215         }
216         else if (dir1 == UI_DIR_UP) {
217                 offset_y = butrct.ymax - block->rect.ymin;
218                 if (dir2 == UI_DIR_RIGHT) offset_x = butrct.xmax - block->rect.xmax + center_x;
219                 else                      offset_x = butrct.xmin - block->rect.xmin - center_x;
220                 /* changed direction? */
221                 if ((dir1 & block->direction) == 0) {
222                         /* TODO: still do */
223                         UI_block_order_flip(block);
224                 }
225         }
226         else if (dir1 == UI_DIR_DOWN) {
227                 offset_y = butrct.ymin - block->rect.ymax;
228                 if (dir2 == UI_DIR_RIGHT) offset_x = butrct.xmax - block->rect.xmax + center_x;
229                 else                      offset_x = butrct.xmin - block->rect.xmin - center_x;
230                 /* changed direction? */
231                 if ((dir1 & block->direction) == 0) {
232                         /* TODO: still do */
233                         UI_block_order_flip(block);
234                 }
235         }
236
237         /* Center over popovers for eg. */
238         if (block->direction & UI_DIR_CENTER_X) {
239                 offset_x += BLI_rctf_size_x(&butrct) / ((dir2 == UI_DIR_LEFT) ? 2 : - 2);
240         }
241
242         /* Apply offset, buttons in window coords. */
243         for (uiBut *bt = block->buttons.first; bt; bt = bt->next) {
244                 ui_block_to_window_rctf(butregion, but->block, &bt->rect, &bt->rect);
245
246                 BLI_rctf_translate(&bt->rect, offset_x, offset_y);
247
248                 /* ui_but_update recalculates drawstring size in pixels */
249                 ui_but_update(bt);
250         }
251
252         BLI_rctf_translate(&block->rect, offset_x, offset_y);
253
254         /* Safety calculus. */
255         {
256                 const float midx = BLI_rctf_cent_x(&butrct);
257                 const float midy = BLI_rctf_cent_y(&butrct);
258
259                 /* when you are outside parent button, safety there should be smaller */
260
261                 /* parent button to left */
262                 if (midx < block->rect.xmin) block->safety.xmin = block->rect.xmin - 3;
263                 else block->safety.xmin = block->rect.xmin - 40;
264                 /* parent button to right */
265                 if (midx > block->rect.xmax) block->safety.xmax = block->rect.xmax + 3;
266                 else block->safety.xmax = block->rect.xmax + 40;
267
268                 /* parent button on bottom */
269                 if (midy < block->rect.ymin) block->safety.ymin = block->rect.ymin - 3;
270                 else block->safety.ymin = block->rect.ymin - 40;
271                 /* parent button on top */
272                 if (midy > block->rect.ymax) block->safety.ymax = block->rect.ymax + 3;
273                 else block->safety.ymax = block->rect.ymax + 40;
274
275                 /* exception for switched pulldowns... */
276                 if (dir1 && (dir1 & block->direction) == 0) {
277                         if (dir2 == UI_DIR_RIGHT) block->safety.xmax = block->rect.xmax + 3;
278                         if (dir2 == UI_DIR_LEFT)  block->safety.xmin = block->rect.xmin - 3;
279                 }
280                 block->direction = dir1;
281         }
282
283         /* keep a list of these, needed for pulldown menus */
284         uiSafetyRct *saferct = MEM_callocN(sizeof(uiSafetyRct), "uiSafetyRct");
285         saferct->parent = butrct;
286         saferct->safety = block->safety;
287         BLI_freelistN(&block->saferct);
288         BLI_duplicatelist(&block->saferct, &but->block->saferct);
289         BLI_addhead(&block->saferct, saferct);
290 }
291
292 /** \} */
293
294 /* -------------------------------------------------------------------- */
295 /** \name Menu Block Creation
296  * \{ */
297
298 static void ui_block_region_refresh(const bContext *C, ARegion *ar)
299 {
300         ScrArea *ctx_area = CTX_wm_area(C);
301         ARegion *ctx_region = CTX_wm_region(C);
302         uiBlock *block;
303
304         if (ar->do_draw & RGN_DRAW_REFRESH_UI) {
305                 ScrArea *handle_ctx_area;
306                 ARegion *handle_ctx_region;
307                 uiBlock *block_next;
308
309                 ar->do_draw &= ~RGN_DRAW_REFRESH_UI;
310                 for (block = ar->uiblocks.first; block; block = block_next) {
311                         block_next = block->next;
312                         uiPopupBlockHandle *handle = block->handle;
313
314                         if (handle->can_refresh) {
315                                 handle_ctx_area = handle->ctx_area;
316                                 handle_ctx_region = handle->ctx_region;
317
318                                 if (handle_ctx_area) {
319                                         CTX_wm_area_set((bContext *)C, handle_ctx_area);
320                                 }
321                                 if (handle_ctx_region) {
322                                         CTX_wm_region_set((bContext *)C, handle_ctx_region);
323                                 }
324
325                                 uiBut *but = handle->popup_create_vars.but;
326                                 ARegion *butregion = handle->popup_create_vars.butregion;
327                                 ui_popup_block_refresh((bContext *)C, handle, butregion, but);
328                         }
329                 }
330         }
331
332         CTX_wm_area_set((bContext *)C, ctx_area);
333         CTX_wm_region_set((bContext *)C, ctx_region);
334 }
335
336 static void ui_block_region_draw(const bContext *C, ARegion *ar)
337 {
338         uiBlock *block;
339
340         for (block = ar->uiblocks.first; block; block = block->next)
341                 UI_block_draw(C, block);
342 }
343
344 /**
345  * Use to refresh centered popups on screen resizing (for splash).
346  */
347 static void ui_block_region_popup_window_listener(
348         wmWindow *UNUSED(win), ScrArea *UNUSED(sa), ARegion *ar, wmNotifier *wmn, const Scene *UNUSED(scene))
349 {
350         switch (wmn->category) {
351                 case NC_WINDOW:
352                 {
353                         switch (wmn->action) {
354                                 case NA_EDITED:
355                                 {
356                                         /* window resize */
357                                         ED_region_tag_refresh_ui(ar);
358                                         break;
359                                 }
360                         }
361                         break;
362                 }
363         }
364 }
365
366 static void ui_popup_block_clip(wmWindow *window, uiBlock *block)
367 {
368         uiBut *bt;
369         float xofs = 0.0f;
370         int width = UI_SCREEN_MARGIN;
371         int winx, winy;
372
373         if (block->flag & UI_BLOCK_NO_WIN_CLIP) {
374                 return;
375         }
376
377         winx = WM_window_pixels_x(window);
378         winy = WM_window_pixels_y(window);
379
380         /* shift menus to right if outside of view */
381         if (block->rect.xmin < width) {
382                 xofs = (width - block->rect.xmin);
383                 block->rect.xmin += xofs;
384                 block->rect.xmax += xofs;
385         }
386         /* or shift to left if outside of view */
387         if (block->rect.xmax > winx - width) {
388                 xofs = winx - width - block->rect.xmax;
389                 block->rect.xmin += xofs;
390                 block->rect.xmax += xofs;
391         }
392
393         if (block->rect.ymin < width)
394                 block->rect.ymin = width;
395         if (block->rect.ymax > winy - UI_POPUP_MENU_TOP)
396                 block->rect.ymax = winy - UI_POPUP_MENU_TOP;
397
398         /* ensure menu items draw inside left/right boundary */
399         for (bt = block->buttons.first; bt; bt = bt->next) {
400                 bt->rect.xmin += xofs;
401                 bt->rect.xmax += xofs;
402         }
403
404 }
405
406 void ui_popup_block_scrolltest(uiBlock *block)
407 {
408         uiBut *bt;
409
410         block->flag &= ~(UI_BLOCK_CLIPBOTTOM | UI_BLOCK_CLIPTOP);
411
412         for (bt = block->buttons.first; bt; bt = bt->next)
413                 bt->flag &= ~UI_SCROLLED;
414
415         if (block->buttons.first == block->buttons.last)
416                 return;
417
418         /* mark buttons that are outside boundary */
419         for (bt = block->buttons.first; bt; bt = bt->next) {
420                 if (bt->rect.ymin < block->rect.ymin) {
421                         bt->flag |= UI_SCROLLED;
422                         block->flag |= UI_BLOCK_CLIPBOTTOM;
423                 }
424                 if (bt->rect.ymax > block->rect.ymax) {
425                         bt->flag |= UI_SCROLLED;
426                         block->flag |= UI_BLOCK_CLIPTOP;
427                 }
428         }
429
430         /* mark buttons overlapping arrows, if we have them */
431         for (bt = block->buttons.first; bt; bt = bt->next) {
432                 if (block->flag & UI_BLOCK_CLIPBOTTOM) {
433                         if (bt->rect.ymin < block->rect.ymin + UI_MENU_SCROLL_ARROW)
434                                 bt->flag |= UI_SCROLLED;
435                 }
436                 if (block->flag & UI_BLOCK_CLIPTOP) {
437                         if (bt->rect.ymax > block->rect.ymax - UI_MENU_SCROLL_ARROW)
438                                 bt->flag |= UI_SCROLLED;
439                 }
440         }
441 }
442
443 static void ui_popup_block_remove(bContext *C, uiPopupBlockHandle *handle)
444 {
445         wmWindow *win = CTX_wm_window(C);
446         bScreen *sc = CTX_wm_screen(C);
447
448         ui_region_temp_remove(C, sc, handle->region);
449
450         /* reset to region cursor (only if there's not another menu open) */
451         if (BLI_listbase_is_empty(&sc->regionbase)) {
452                 ED_region_cursor_set(win, CTX_wm_area(C), CTX_wm_region(C));
453                 /* in case cursor needs to be changed again */
454                 WM_event_add_mousemove(C);
455         }
456
457         if (handle->scrolltimer)
458                 WM_event_remove_timer(CTX_wm_manager(C), win, handle->scrolltimer);
459 }
460
461 /**
462  * Called for creating new popups and refreshing existing ones.
463  */
464 uiBlock *ui_popup_block_refresh(
465         bContext *C, uiPopupBlockHandle *handle,
466         ARegion *butregion, uiBut *but)
467 {
468         const int margin = UI_POPUP_MARGIN;
469         wmWindow *window = CTX_wm_window(C);
470         ARegion *ar = handle->region;
471
472         uiBlockCreateFunc create_func = handle->popup_create_vars.create_func;
473         uiBlockHandleCreateFunc handle_create_func = handle->popup_create_vars.handle_create_func;
474         void *arg = handle->popup_create_vars.arg;
475
476         uiBlock *block_old = ar->uiblocks.first;
477         uiBlock *block;
478
479         handle->refresh = (block_old != NULL);
480
481         BLI_assert(!handle->refresh || handle->can_refresh);
482
483 #ifdef DEBUG
484         wmEvent *event_back = window->eventstate;
485 #endif
486
487         /* create ui block */
488         if (create_func)
489                 block = create_func(C, ar, arg);
490         else
491                 block = handle_create_func(C, handle, arg);
492
493         /* callbacks _must_ leave this for us, otherwise we can't call UI_block_update_from_old */
494         BLI_assert(!block->endblock);
495
496         /* ensure we don't use mouse coords here! */
497 #ifdef DEBUG
498         window->eventstate = NULL;
499 #endif
500
501         if (block->handle) {
502                 memcpy(block->handle, handle, sizeof(uiPopupBlockHandle));
503                 MEM_freeN(handle);
504                 handle = block->handle;
505         }
506         else
507                 block->handle = handle;
508
509         ar->regiondata = handle;
510
511         /* set UI_BLOCK_NUMSELECT before UI_block_end() so we get alphanumeric keys assigned */
512         if (but == NULL) {
513                 block->flag |= UI_BLOCK_POPUP;
514         }
515
516         block->flag |= UI_BLOCK_LOOP;
517         UI_block_theme_style_set(block, UI_BLOCK_THEME_STYLE_POPUP);
518
519         /* defer this until blocks are translated (below) */
520         block->oldblock = NULL;
521
522         if (!block->endblock) {
523                 UI_block_end_ex(C, block, handle->popup_create_vars.event_xy, handle->popup_create_vars.event_xy);
524         }
525
526         /* if this is being created from a button */
527         if (but) {
528                 block->aspect = but->block->aspect;
529                 ui_popup_block_position(window, butregion, but, block);
530                 handle->direction = block->direction;
531         }
532         else {
533                 uiSafetyRct *saferct;
534                 /* keep a list of these, needed for pulldown menus */
535                 saferct = MEM_callocN(sizeof(uiSafetyRct), "uiSafetyRct");
536                 saferct->safety = block->safety;
537                 BLI_addhead(&block->saferct, saferct);
538         }
539
540         if (block->flag & UI_BLOCK_RADIAL) {
541                 int win_width = UI_SCREEN_MARGIN;
542                 int winx, winy;
543
544                 int x_offset = 0, y_offset = 0;
545
546                 winx = WM_window_pixels_x(window);
547                 winy = WM_window_pixels_y(window);
548
549                 copy_v2_v2(block->pie_data.pie_center_init, block->pie_data.pie_center_spawned);
550
551                 /* only try translation if area is large enough */
552                 if (BLI_rctf_size_x(&block->rect) < winx - (2.0f * win_width)) {
553                         if (block->rect.xmin < win_width )   x_offset += win_width - block->rect.xmin;
554                         if (block->rect.xmax > winx - win_width) x_offset += winx - win_width - block->rect.xmax;
555                 }
556
557                 if (BLI_rctf_size_y(&block->rect) < winy - (2.0f * win_width)) {
558                         if (block->rect.ymin < win_width )   y_offset += win_width - block->rect.ymin;
559                         if (block->rect.ymax > winy - win_width) y_offset += winy - win_width - block->rect.ymax;
560                 }
561                 /* if we are offsetting set up initial data for timeout functionality */
562
563                 if ((x_offset != 0) || (y_offset != 0)) {
564                         block->pie_data.pie_center_spawned[0] += x_offset;
565                         block->pie_data.pie_center_spawned[1] += y_offset;
566
567                         UI_block_translate(block, x_offset, y_offset);
568
569                         if (U.pie_initial_timeout > 0)
570                                 block->pie_data.flags |= UI_PIE_INITIAL_DIRECTION;
571                 }
572
573                 ar->winrct.xmin = 0;
574                 ar->winrct.xmax = winx;
575                 ar->winrct.ymin = 0;
576                 ar->winrct.ymax = winy;
577
578                 ui_block_calc_pie_segment(block, block->pie_data.pie_center_init);
579
580                 /* lastly set the buttons at the center of the pie menu, ready for animation */
581                 if (U.pie_animation_timeout > 0) {
582                         for (uiBut *but_iter = block->buttons.first; but_iter; but_iter = but_iter->next) {
583                                 if (but_iter->pie_dir != UI_RADIAL_NONE) {
584                                         BLI_rctf_recenter(&but_iter->rect, UNPACK2(block->pie_data.pie_center_spawned));
585                                 }
586                         }
587                 }
588         }
589         else {
590                 /* clip block with window boundary */
591                 ui_popup_block_clip(window, block);
592
593                 /* Avoid menu moving down and losing cursor focus by keeping it at
594                  * the same height. */
595                 if (handle->refresh && handle->prev_block_rect.ymax > block->rect.ymax) {
596                         float offset = handle->prev_block_rect.ymax - block->rect.ymax;
597                         UI_block_translate(block, 0, offset);
598                         block->rect.ymin = handle->prev_block_rect.ymin;
599                 }
600
601                 handle->prev_block_rect = block->rect;
602
603                 /* the block and buttons were positioned in window space as in 2.4x, now
604                  * these menu blocks are regions so we bring it back to region space.
605                  * additionally we add some padding for the menu shadow or rounded menus */
606                 ar->winrct.xmin = block->rect.xmin - margin;
607                 ar->winrct.xmax = block->rect.xmax + margin;
608                 ar->winrct.ymin = block->rect.ymin - margin;
609                 ar->winrct.ymax = block->rect.ymax + UI_POPUP_MENU_TOP;
610
611                 UI_block_translate(block, -ar->winrct.xmin, -ar->winrct.ymin);
612
613                 /* apply scroll offset */
614                 if (handle->scrolloffset != 0.0f) {
615                         for (uiBut *bt = block->buttons.first; bt; bt = bt->next) {
616                                 bt->rect.ymin += handle->scrolloffset;
617                                 bt->rect.ymax += handle->scrolloffset;
618                         }
619                 }
620         }
621
622         if (block_old) {
623                 block->oldblock = block_old;
624                 UI_block_update_from_old(C, block);
625                 UI_blocklist_free_inactive(C, &ar->uiblocks);
626         }
627
628         /* checks which buttons are visible, sets flags to prevent draw (do after region init) */
629         ui_popup_block_scrolltest(block);
630
631         /* adds subwindow */
632         ED_region_init(ar);
633
634         /* get winmat now that we actually have the subwindow */
635         wmGetProjectionMatrix(block->winmat, &ar->winrct);
636
637         /* notify change and redraw */
638         ED_region_tag_redraw(ar);
639
640         ED_region_update_rect(ar);
641
642 #ifdef DEBUG
643         window->eventstate = event_back;
644 #endif
645
646         return block;
647 }
648
649 uiPopupBlockHandle *ui_popup_block_create(
650         bContext *C, ARegion *butregion, uiBut *but,
651         uiBlockCreateFunc create_func, uiBlockHandleCreateFunc handle_create_func,
652         void *arg)
653 {
654         wmWindow *window = CTX_wm_window(C);
655         uiBut *activebut = UI_context_active_but_get(C);
656         static ARegionType type;
657         ARegion *ar;
658         uiBlock *block;
659         uiPopupBlockHandle *handle;
660
661         /* disable tooltips from buttons below */
662         if (activebut) {
663                 UI_but_tooltip_timer_remove(C, activebut);
664         }
665         /* standard cursor by default */
666         WM_cursor_set(window, CURSOR_STD);
667
668         /* create handle */
669         handle = MEM_callocN(sizeof(uiPopupBlockHandle), "uiPopupBlockHandle");
670
671         /* store context for operator */
672         handle->ctx_area = CTX_wm_area(C);
673         handle->ctx_region = CTX_wm_region(C);
674
675         /* store vars to refresh popup (RGN_DRAW_REFRESH_UI) */
676         handle->popup_create_vars.create_func = create_func;
677         handle->popup_create_vars.handle_create_func = handle_create_func;
678         handle->popup_create_vars.arg = arg;
679         handle->popup_create_vars.but = but;
680         handle->popup_create_vars.butregion = but ? butregion : NULL;
681         copy_v2_v2_int(handle->popup_create_vars.event_xy, &window->eventstate->x);
682
683         /* don't allow by default, only if popup type explicitly supports it */
684         handle->can_refresh = false;
685
686         /* create area region */
687         ar = ui_region_temp_add(CTX_wm_screen(C));
688         handle->region = ar;
689
690         memset(&type, 0, sizeof(ARegionType));
691         type.draw = ui_block_region_draw;
692         type.layout = ui_block_region_refresh;
693         type.regionid = RGN_TYPE_TEMPORARY;
694         ar->type = &type;
695
696         UI_region_handlers_add(&ar->handlers);
697
698         block = ui_popup_block_refresh(C, handle, butregion, but);
699         handle = block->handle;
700
701         /* keep centered on window resizing */
702         if (block->bounds_type == UI_BLOCK_BOUNDS_POPUP_CENTER) {
703                 type.listener = ui_block_region_popup_window_listener;
704         }
705
706         return handle;
707 }
708
709 void ui_popup_block_free(bContext *C, uiPopupBlockHandle *handle)
710 {
711         /* If this popup is created from a popover which does NOT have keep-open flag set,
712          * then close the popover too. We could extend this to other popup types too. */
713         ARegion *ar = handle->popup_create_vars.butregion;
714         if (ar != NULL) {
715                 for (uiBlock *block = ar->uiblocks.first; block; block = block->next) {
716                         if (block->handle &&
717                             (block->flag & UI_BLOCK_POPOVER) &&
718                             (block->flag & UI_BLOCK_KEEP_OPEN) == 0)
719                         {
720                                 uiPopupBlockHandle *menu = block->handle;
721                                 menu->menuretval = UI_RETURN_OK;
722                         }
723                 }
724         }
725
726         if (handle->popup_create_vars.free_func) {
727                 handle->popup_create_vars.free_func(handle, handle->popup_create_vars.arg);
728         }
729
730         ui_popup_block_remove(C, handle);
731
732         MEM_freeN(handle);
733 }
734
735 /** \} */