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