UI: report if WM_OT_call_menu uses an unknown menu
[blender.git] / source / blender / editors / interface / interface_regions.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_regions.c
27  *  \ingroup edinterface
28  */
29
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_blenlib.h"
43 #include "BLI_utildefines.h"
44 #include "BLI_dynstr.h"
45 #include "BLI_ghash.h"
46
47 #include "BKE_context.h"
48 #include "BKE_screen.h"
49 #include "BKE_idcode.h"
50 #include "BKE_report.h"
51 #include "BKE_global.h"
52
53 #include "WM_api.h"
54 #include "WM_types.h"
55 #include "wm_draw.h"
56 #include "wm_subwindow.h"
57 #include "wm_window.h"
58
59 #include "RNA_access.h"
60
61 #include "BIF_gl.h"
62
63 #include "UI_interface.h"
64 #include "UI_interface_icons.h"
65 #include "UI_view2d.h"
66
67 #include "BLF_api.h"
68 #include "BLF_translation.h"
69
70 #include "ED_screen.h"
71
72 #include "IMB_colormanagement.h"
73
74 #include "interface_intern.h"
75
76 #define MENU_TOP            8
77 #define MENU_PADDING            (int)(0.2f * UI_UNIT_Y)
78
79 static int rna_property_enum_step(const bContext *C, PointerRNA *ptr, PropertyRNA *prop, int direction)
80 {
81         EnumPropertyItem *item_array;
82         int totitem;
83         bool free;
84         int value;
85         int i, i_init;
86         int step = (direction < 0) ? -1 : 1;
87         int step_tot = 0;
88
89         RNA_property_enum_items((bContext *)C, ptr, prop, &item_array, &totitem, &free);
90         value = RNA_property_enum_get(ptr, prop);
91         i = RNA_enum_from_value(item_array, value);
92         i_init = i;
93
94         do {
95                 i = mod_i(i + step, totitem);
96                 if (item_array[i].identifier[0]) {
97                         step_tot += step;
98                 }
99         } while ((i != i_init) && (step_tot != direction));
100
101         if (i != i_init) {
102                 value = item_array[i].value;
103         }
104
105         if (free) {
106                 MEM_freeN(item_array);
107         }
108
109         return value;
110 }
111
112 int ui_step_name_menu(uiBut *but, int direction)
113 {
114         /* currenly only RNA buttons */
115         if ((but->rnaprop == NULL) || (RNA_property_type(but->rnaprop) != PROP_ENUM)) {
116                 printf("%s: cannot cycle button '%s'", __func__, but->str);
117                 return 0;
118         }
119
120         return rna_property_enum_step(but->block->evil_C, &but->rnapoin, but->rnaprop, direction);
121 }
122
123 /******************** Creating Temporary regions ******************/
124
125 static ARegion *ui_add_temporary_region(bScreen *sc)
126 {
127         ARegion *ar;
128
129         ar = MEM_callocN(sizeof(ARegion), "area region");
130         BLI_addtail(&sc->regionbase, ar);
131
132         ar->regiontype = RGN_TYPE_TEMPORARY;
133         ar->alignment = RGN_ALIGN_FLOAT;
134
135         return ar;
136 }
137
138 static void ui_remove_temporary_region(bContext *C, bScreen *sc, ARegion *ar)
139 {
140         wmWindow *win = CTX_wm_window(C);
141         if (win)
142                 wm_draw_region_clear(win, ar);
143
144         ED_region_exit(C, ar);
145         BKE_area_region_free(NULL, ar);     /* NULL: no spacetype */
146         BLI_freelinkN(&sc->regionbase, ar);
147 }
148
149 /************************* Creating Tooltips **********************/
150
151 typedef enum {
152         UI_TIP_LC_MAIN,
153         UI_TIP_LC_NORMAL,
154         UI_TIP_LC_PYTHON,
155         UI_TIP_LC_ALERT,
156         UI_TIP_LC_SUBMENU
157 } uiTooltipLineColor;
158 #define UI_TIP_LC_MAX 5
159
160 #define MAX_TOOLTIP_LINES 8
161 typedef struct uiTooltipData {
162         rcti bbox;
163         uiFontStyle fstyle;
164         char lines[MAX_TOOLTIP_LINES][512];
165         uiTooltipLineColor color_id[MAX_TOOLTIP_LINES];
166         int totline;
167         int toth, spaceh, lineh;
168 } uiTooltipData;
169
170 static void rgb_tint(float col[3],
171                      float h, float h_strength,
172                      float v, float v_strength)
173 {
174         float col_hsv_from[3];
175         float col_hsv_to[3];
176
177         rgb_to_hsv_v(col, col_hsv_from);
178
179         col_hsv_to[0] = h;
180         col_hsv_to[1] = h_strength;
181         col_hsv_to[2] = (col_hsv_from[2] * (1.0f - v_strength)) + (v * v_strength);
182
183         hsv_to_rgb_v(col_hsv_to, col);
184 }
185
186 static void ui_tooltip_region_draw_cb(const bContext *UNUSED(C), ARegion *ar)
187 {
188         uiTooltipData *data = ar->regiondata;
189         uiWidgetColors *theme = ui_tooltip_get_theme();
190         rcti bbox = data->bbox;
191         float tip_colors[UI_TIP_LC_MAX][3];
192
193         float *main_color    = tip_colors[UI_TIP_LC_MAIN]; /* the color from the theme */
194         float *normal_color  = tip_colors[UI_TIP_LC_NORMAL];
195         float *python_color  = tip_colors[UI_TIP_LC_PYTHON];
196         float *alert_color   = tip_colors[UI_TIP_LC_ALERT];
197         float *submenu_color = tip_colors[UI_TIP_LC_SUBMENU];
198
199         float background_color[3];
200         float tone_bg;
201         int i, multisample_enabled;
202
203         /* disable AA, makes widgets too blurry */
204         multisample_enabled = glIsEnabled(GL_MULTISAMPLE_ARB);
205         if (multisample_enabled)
206                 glDisable(GL_MULTISAMPLE_ARB);
207
208         /* draw background */
209         ui_draw_tooltip_background(UI_GetStyle(), NULL, &bbox);
210
211         /* set background_color */
212         rgb_uchar_to_float(background_color, (const unsigned char *)theme->inner);
213
214         /* calculate normal_color */
215         rgb_uchar_to_float(main_color, (const unsigned char *)theme->text);
216         copy_v3_v3(normal_color, main_color);
217         copy_v3_v3(python_color, main_color);
218         copy_v3_v3(alert_color, main_color);
219         copy_v3_v3(submenu_color, main_color);
220
221         /* find the brightness difference between background and text colors */
222         
223         tone_bg = rgb_to_grayscale(background_color);
224         /* tone_fg = rgb_to_grayscale(main_color); */
225
226         rgb_tint(normal_color, 0.0f, 0.0f, tone_bg, 0.3f);   /* a shade darker (to bg) */
227         rgb_tint(python_color, 0.666f, 0.25f, tone_bg, 0.3f); /* blue */
228         rgb_tint(alert_color, 0.0f, 0.8f, tone_bg, 0.1f);    /* bright red */
229         rgb_tint(submenu_color, 0.0f, 0.0f, tone_bg, 0.3f);  /* a shade darker (to bg) */
230
231         /* draw text */
232         uiStyleFontSet(&data->fstyle);
233
234         bbox.ymax = bbox.ymax - 0.5f * (BLI_rcti_size_y(&bbox) - data->toth);
235         bbox.ymin = bbox.ymax - data->lineh;
236
237         for (i = 0; i < data->totline; i++) {
238                 glColor3fv(tip_colors[data->color_id[i]]);
239                 uiStyleFontDraw(&data->fstyle, &bbox, data->lines[i]);
240                 bbox.ymin -= data->lineh + data->spaceh;
241                 bbox.ymax -= data->lineh + data->spaceh;
242         }
243
244         if (multisample_enabled)
245                 glEnable(GL_MULTISAMPLE_ARB);
246 }
247
248 static void ui_tooltip_region_free_cb(ARegion *ar)
249 {
250         uiTooltipData *data;
251
252         data = ar->regiondata;
253         MEM_freeN(data);
254         ar->regiondata = NULL;
255 }
256
257 ARegion *ui_tooltip_create(bContext *C, ARegion *butregion, uiBut *but)
258 {
259         wmWindow *win = CTX_wm_window(C);
260         uiStyle *style = UI_GetStyle();
261         static ARegionType type;
262         ARegion *ar;
263         uiTooltipData *data;
264 /*      IDProperty *prop;*/
265         char buf[512];
266         /* aspect values that shrink text are likely unreadable */
267         const float aspect = min_ff(1.0f, but->block->aspect);
268         float fonth, fontw;
269         int winx /*, winy */, ofsx, ofsy, w, h, a;
270         rctf rect_fl;
271         rcti rect_i;
272
273         uiStringInfo but_tip = {BUT_GET_TIP, NULL};
274         uiStringInfo enum_label = {BUT_GET_RNAENUM_LABEL, NULL};
275         uiStringInfo enum_tip = {BUT_GET_RNAENUM_TIP, NULL};
276         uiStringInfo op_keymap = {BUT_GET_OP_KEYMAP, NULL};
277         uiStringInfo prop_keymap = {BUT_GET_PROP_KEYMAP, NULL};
278         uiStringInfo rna_struct = {BUT_GET_RNASTRUCT_IDENTIFIER, NULL};
279         uiStringInfo rna_prop = {BUT_GET_RNAPROP_IDENTIFIER, NULL};
280
281         if (but->drawflag & UI_BUT_NO_TOOLTIP)
282                 return NULL;
283
284         /* create tooltip data */
285         data = MEM_callocN(sizeof(uiTooltipData), "uiTooltipData");
286
287         uiButGetStrInfo(C, but, &but_tip, &enum_label, &enum_tip, &op_keymap, &prop_keymap, &rna_struct, &rna_prop, NULL);
288
289         /* special case, enum rna buttons only have enum item description,
290          * use general enum description too before the specific one */
291
292         /* Tip */
293         if (but_tip.strinfo) {
294                 /* Expanded Bit-flag enums have a specific way to select multiple... */
295                 if ((but->type & ROW) && but->rnaprop && RNA_property_flag(but->rnaprop) & PROP_ENUM_FLAG) {
296                         BLI_snprintf(data->lines[data->totline], sizeof(data->lines[0]),
297                                      "%s %s", but_tip.strinfo, IFACE_("(Shift-click to select multiple)"));
298                 }
299                 else {
300                         BLI_strncpy(data->lines[data->totline], but_tip.strinfo, sizeof(data->lines[0]));
301                 }
302                 data->color_id[data->totline] = UI_TIP_LC_MAIN;
303                 data->totline++;
304         }
305         /* Enum item label & tip */
306         if (enum_label.strinfo && enum_tip.strinfo) {
307                 BLI_snprintf(data->lines[data->totline], sizeof(data->lines[0]),
308                              "%s: %s", enum_label.strinfo, enum_tip.strinfo);
309                 data->color_id[data->totline] = UI_TIP_LC_SUBMENU;
310                 data->totline++;
311         }
312
313         /* Op shortcut */
314         if (op_keymap.strinfo) {
315                 BLI_snprintf(data->lines[data->totline], sizeof(data->lines[0]), TIP_("Shortcut: %s"), op_keymap.strinfo);
316                 data->color_id[data->totline] = UI_TIP_LC_NORMAL;
317                 data->totline++;
318         }
319         
320         /* Property context-toggle shortcut */
321         if (prop_keymap.strinfo) {
322                 BLI_snprintf(data->lines[data->totline], sizeof(data->lines[0]), TIP_("Shortcut: %s"), prop_keymap.strinfo);
323                 data->color_id[data->totline] = UI_TIP_LC_NORMAL;
324                 data->totline++;
325         }
326
327         if (ELEM3(but->type, TEX, SEARCH_MENU, SEARCH_MENU_UNLINK)) {
328                 /* full string */
329                 ui_get_but_string(but, buf, sizeof(buf));
330                 if (buf[0]) {
331                         BLI_snprintf(data->lines[data->totline], sizeof(data->lines[0]), TIP_("Value: %s"), buf);
332                         data->color_id[data->totline] = UI_TIP_LC_NORMAL;
333                         data->totline++;
334                 }
335         }
336
337         if (but->rnaprop) {
338                 int unit_type = uiButGetUnitType(but);
339                 
340                 if (unit_type == PROP_UNIT_ROTATION) {
341                         if (RNA_property_type(but->rnaprop) == PROP_FLOAT) {
342                                 float value = RNA_property_array_check(but->rnaprop) ?
343                                                   RNA_property_float_get_index(&but->rnapoin, but->rnaprop, but->rnaindex) :
344                                                   RNA_property_float_get(&but->rnapoin, but->rnaprop);
345                                 BLI_snprintf(data->lines[data->totline], sizeof(data->lines[0]), TIP_("Radians: %f"), value);
346                                 data->color_id[data->totline] = UI_TIP_LC_NORMAL;
347                                 data->totline++;
348                         }
349                 }
350                 
351                 if (but->flag & UI_BUT_DRIVEN) {
352                         if (ui_but_anim_expression_get(but, buf, sizeof(buf))) {
353                                 /* expression */
354                                 BLI_snprintf(data->lines[data->totline], sizeof(data->lines[0]), TIP_("Expression: %s"), buf);
355                                 data->color_id[data->totline] = UI_TIP_LC_NORMAL;
356                                 data->totline++;
357                         }
358                 }
359
360                 if (but->rnapoin.id.data) {
361                         ID *id = but->rnapoin.id.data;
362                         if (id->lib) {
363                                 BLI_snprintf(data->lines[data->totline], sizeof(data->lines[0]), TIP_("Library: %s"), id->lib->name);
364                                 data->color_id[data->totline] = UI_TIP_LC_NORMAL;
365                                 data->totline++;
366                         }
367                 }
368         }
369         else if (but->optype) {
370                 PointerRNA *opptr;
371                 char *str;
372                 opptr = uiButGetOperatorPtrRNA(but); /* allocated when needed, the button owns it */
373
374                 /* so the context is passed to itemf functions (some py itemf functions use it) */
375                 WM_operator_properties_sanitize(opptr, false);
376
377                 str = WM_operator_pystring_ex(C, NULL, false, false, but->optype, opptr);
378
379                 /* avoid overly verbose tips (eg, arrays of 20 layers), exact limit is arbitrary */
380                 WM_operator_pystring_abbreviate(str, 32);
381
382                 /* operator info */
383                 if ((U.flag & USER_TOOLTIPS_PYTHON) == 0) {
384                         BLI_snprintf(data->lines[data->totline], sizeof(data->lines[0]), TIP_("Python: %s"), str);
385                         data->color_id[data->totline] = UI_TIP_LC_PYTHON;
386                         data->totline++;
387                 }
388
389                 MEM_freeN(str);
390
391                 /* second check if we are disabled - why */
392                 if (but->flag & UI_BUT_DISABLED) {
393                         const char *poll_msg;
394                         CTX_wm_operator_poll_msg_set(C, NULL);
395                         WM_operator_poll_context(C, but->optype, but->opcontext);
396                         poll_msg = CTX_wm_operator_poll_msg_get(C);
397                         if (poll_msg) {
398                                 BLI_snprintf(data->lines[data->totline], sizeof(data->lines[0]), TIP_("Disabled: %s"), poll_msg);
399                                 data->color_id[data->totline] = UI_TIP_LC_ALERT; /* alert */
400                                 data->totline++;
401                         }
402                 }
403         }
404         if ((U.flag & USER_TOOLTIPS_PYTHON) == 0 && !but->optype && rna_struct.strinfo) {
405                 if (rna_prop.strinfo) {
406                         /* Struct and prop */
407                         BLI_snprintf(data->lines[data->totline], sizeof(data->lines[0]),
408                                      TIP_("Python: %s.%s"),
409                                      rna_struct.strinfo, rna_prop.strinfo);
410                 }
411                 else {
412                         /* Only struct (e.g. menus) */
413                         BLI_snprintf(data->lines[data->totline], sizeof(data->lines[0]),
414                                      TIP_("Python: %s"), rna_struct.strinfo);
415                 }
416                 data->color_id[data->totline] = UI_TIP_LC_PYTHON;
417                 data->totline++;
418
419                 if (but->rnapoin.id.data) {
420                         /* this could get its own 'BUT_GET_...' type */
421                         PointerRNA *ptr = &but->rnapoin;
422                         PropertyRNA *prop = but->rnaprop;
423                         ID *id = ptr->id.data;
424
425                         char *id_path;
426                         char *data_path = NULL;
427
428                         /* never fails */
429                         id_path = RNA_path_full_ID_py(id);
430
431                         if (ptr->data && prop) {
432                                 data_path = RNA_path_from_ID_to_property(ptr, prop);
433                         }
434
435                         if (data_path) {
436                                 BLI_snprintf(data->lines[data->totline], sizeof(data->lines[0]),
437                                              "%s.%s",  /* no need to translate */
438                                              id_path, data_path);
439                                 MEM_freeN(data_path);
440                         }
441                         else if (prop) {
442                                 /* can't find the path. be explicit in our ignorance "..." */
443                                 BLI_snprintf(data->lines[data->totline], sizeof(data->lines[0]),
444                                              "%s ... %s",  /* no need to translate */
445                                              id_path, rna_prop.strinfo ? rna_prop.strinfo : RNA_property_identifier(prop));
446                         }
447                         MEM_freeN(id_path);
448
449                         data->color_id[data->totline] = UI_TIP_LC_PYTHON;
450                         data->totline++;
451                 }
452         }
453
454         /* Free strinfo's... */
455         if (but_tip.strinfo)
456                 MEM_freeN(but_tip.strinfo);
457         if (enum_label.strinfo)
458                 MEM_freeN(enum_label.strinfo);
459         if (enum_tip.strinfo)
460                 MEM_freeN(enum_tip.strinfo);
461         if (op_keymap.strinfo)
462                 MEM_freeN(op_keymap.strinfo);
463         if (prop_keymap.strinfo)
464                 MEM_freeN(prop_keymap.strinfo);
465         if (rna_struct.strinfo)
466                 MEM_freeN(rna_struct.strinfo);
467         if (rna_prop.strinfo)
468                 MEM_freeN(rna_prop.strinfo);
469
470         BLI_assert(data->totline < MAX_TOOLTIP_LINES);
471         
472         if (data->totline == 0) {
473                 MEM_freeN(data);
474                 return NULL;
475         }
476
477         /* create area region */
478         ar = ui_add_temporary_region(CTX_wm_screen(C));
479
480         memset(&type, 0, sizeof(ARegionType));
481         type.draw = ui_tooltip_region_draw_cb;
482         type.free = ui_tooltip_region_free_cb;
483         type.regionid = RGN_TYPE_TEMPORARY;
484         ar->type = &type;
485         
486         /* set font, get bb */
487         data->fstyle = style->widget; /* copy struct */
488         data->fstyle.align = UI_STYLE_TEXT_CENTER;
489         ui_fontscale(&data->fstyle.points, aspect);
490
491         uiStyleFontSet(&data->fstyle);
492
493         /* these defines tweaked depending on font */
494 #define TIP_MARGIN_Y (2.0f / aspect)
495 #define TIP_BORDER_X (16.0f / aspect)
496 #define TIP_BORDER_Y (6.0f / aspect)
497
498         h = BLF_height_max(data->fstyle.uifont_id);
499
500         for (a = 0, fontw = 0, fonth = 0; a < data->totline; a++) {
501                 w = BLF_width(data->fstyle.uifont_id, data->lines[a], sizeof(data->lines[a]));
502                 fontw = max_ff(fontw, (float)w);
503                 fonth += (a == 0) ? h : h + TIP_MARGIN_Y;
504         }
505
506         //fontw *= aspect;
507
508         ar->regiondata = data;
509
510         data->toth = fonth;
511         data->lineh = h;
512         data->spaceh = TIP_MARGIN_Y;
513
514         /* compute position */
515         ofsx = 0; //(but->block->panel) ? but->block->panel->ofsx : 0;
516         ofsy = 0; //(but->block->panel) ? but->block->panel->ofsy : 0;
517
518         rect_fl.xmin = BLI_rctf_cent_x(&but->rect) + ofsx - TIP_BORDER_X;
519         rect_fl.xmax = rect_fl.xmin + fontw + (TIP_BORDER_X * 2);
520         rect_fl.ymax = but->rect.ymin + ofsy - TIP_BORDER_Y;
521         rect_fl.ymin = rect_fl.ymax - fonth  - TIP_BORDER_Y;
522
523 #undef TIP_MARGIN_Y
524 #undef TIP_BORDER_X
525 #undef TIP_BORDER_Y
526         
527         /* since the text has beens caled already, the size of tooltips is defined now */
528         /* here we try to figure out the right location */
529         if (butregion) {
530                 float ofsx_fl = rect_fl.xmin, ofsy_fl = rect_fl.ymax;
531                 ui_block_to_window_fl(butregion, but->block, &ofsx_fl, &ofsy_fl);
532                 BLI_rctf_translate(&rect_fl, ofsx_fl - rect_fl.xmin, ofsy_fl - rect_fl.ymax);
533         }
534         BLI_rcti_rctf_copy(&rect_i, &rect_fl);
535
536         /* clip with window boundaries */
537         winx = WM_window_pixels_x(win);
538
539         if (rect_i.xmax > winx) {
540                 /* super size */
541                 if (rect_i.xmax > winx + rect_i.xmin) {
542                         rect_i.xmax = winx;
543                         rect_i.xmin = 0;
544                 }
545                 else {
546                         rect_i.xmin -= rect_i.xmax - winx;
547                         rect_i.xmax = winx;
548                 }
549         }
550         /* ensure at least 5 px above screen bounds
551          * 25 is just a guess to be above the menu item */
552         if (rect_i.ymin < 5) {
553                 rect_i.ymax += (-rect_i.ymin) + 30;
554                 rect_i.ymin = 30;
555         }
556
557         /* widget rect, in region coords */
558         {
559                 int width = UI_ThemeMenuShadowWidth();
560                 
561                 data->bbox.xmin = width;
562                 data->bbox.xmax = BLI_rcti_size_x(&rect_i) + width;
563                 data->bbox.ymin = width;
564                 data->bbox.ymax = BLI_rcti_size_y(&rect_i) + width;
565                 
566                 /* region bigger for shadow */
567                 ar->winrct.xmin = rect_i.xmin - width;
568                 ar->winrct.xmax = rect_i.xmax + width;
569                 ar->winrct.ymin = rect_i.ymin - width;
570                 ar->winrct.ymax = rect_i.ymax + MENU_TOP;
571         }
572
573         /* adds subwindow */
574         ED_region_init(C, ar);
575         
576         /* notify change and redraw */
577         ED_region_tag_redraw(ar);
578
579         return ar;
580 }
581
582 void ui_tooltip_free(bContext *C, ARegion *ar)
583 {
584         ui_remove_temporary_region(C, CTX_wm_screen(C), ar);
585 }
586
587
588 /************************* Creating Search Box **********************/
589
590 struct uiSearchItems {
591         int maxitem, totitem, maxstrlen;
592         
593         int offset, offset_i; /* offset for inserting in array */
594         int more;  /* flag indicating there are more items */
595         
596         char **names;
597         void **pointers;
598         int *icons;
599
600         AutoComplete *autocpl;
601         void *active;
602 };
603
604 typedef struct uiSearchboxData {
605         rcti bbox;
606         uiFontStyle fstyle;
607         uiSearchItems items;
608         int active;     /* index in items array */
609         bool noback;    /* when menu opened with enough space for this */
610         bool preview;   /* draw thumbnail previews, rather than list */
611         bool use_sep;   /* use the UI_SEP_CHAR char for splitting shortcuts (good for operators, bad for data) */
612         int prv_rows, prv_cols;
613 } uiSearchboxData;
614
615 #define SEARCH_ITEMS    10
616
617 /* exported for use by search callbacks */
618 /* returns zero if nothing to add */
619 bool uiSearchItemAdd(uiSearchItems *items, const char *name, void *poin, int iconid)
620 {
621         /* hijack for autocomplete */
622         if (items->autocpl) {
623                 autocomplete_do_name(items->autocpl, name);
624                 return true;
625         }
626         
627         /* hijack for finding active item */
628         if (items->active) {
629                 if (poin == items->active)
630                         items->offset_i = items->totitem;
631                 items->totitem++;
632                 return true;
633         }
634         
635         if (items->totitem >= items->maxitem) {
636                 items->more = 1;
637                 return false;
638         }
639         
640         /* skip first items in list */
641         if (items->offset_i > 0) {
642                 items->offset_i--;
643                 return true;
644         }
645         
646         if (items->names)
647                 BLI_strncpy(items->names[items->totitem], name, items->maxstrlen);
648         if (items->pointers)
649                 items->pointers[items->totitem] = poin;
650         if (items->icons)
651                 items->icons[items->totitem] = iconid;
652         
653         items->totitem++;
654         
655         return true;
656 }
657
658 int uiSearchBoxHeight(void)
659 {
660         return SEARCH_ITEMS * UI_UNIT_Y + 2 * MENU_TOP;
661 }
662
663 int uiSearchBoxWidth(void)
664 {
665         /* was hardcoded at 150 */
666         return 9 * UI_UNIT_X;
667 }
668
669 int uiSearchItemFindIndex(uiSearchItems *items, const char *name)
670 {
671         int i;
672         for (i = 0; i < items->totitem; i++) {
673                 if (STREQ(name, items->names[i])) {
674                         return i;
675                 }
676         }
677         return -1;
678 }
679
680 /* ar is the search box itself */
681 static void ui_searchbox_select(bContext *C, ARegion *ar, uiBut *but, int step)
682 {
683         uiSearchboxData *data = ar->regiondata;
684         
685         /* apply step */
686         data->active += step;
687         
688         if (data->items.totitem == 0) {
689                 data->active = -1;
690         }
691         else if (data->active >= data->items.totitem) {
692                 if (data->items.more) {
693                         data->items.offset++;
694                         data->active = data->items.totitem - 1;
695                         ui_searchbox_update(C, ar, but, false);
696                 }
697                 else {
698                         data->active = data->items.totitem - 1;
699                 }
700         }
701         else if (data->active < 0) {
702                 if (data->items.offset) {
703                         data->items.offset--;
704                         data->active = 0;
705                         ui_searchbox_update(C, ar, but, false);
706                 }
707                 else if (data->active < -1)
708                         data->active = -1;
709         }
710         
711         ED_region_tag_redraw(ar);
712 }
713
714 static void ui_searchbox_butrect(rcti *r_rect, uiSearchboxData *data, int itemnr)
715 {
716         /* thumbnail preview */
717         if (data->preview) {
718                 int butw =  BLI_rcti_size_x(&data->bbox)                 / data->prv_cols;
719                 int buth = (BLI_rcti_size_y(&data->bbox) - 2 * MENU_TOP) / data->prv_rows;
720                 int row, col;
721                 
722                 *r_rect = data->bbox;
723                 
724                 col = itemnr % data->prv_cols;
725                 row = itemnr / data->prv_cols;
726                 
727                 r_rect->xmin += col * butw;
728                 r_rect->xmax = r_rect->xmin + butw;
729                 
730                 r_rect->ymax = data->bbox.ymax - MENU_TOP - (row * buth);
731                 r_rect->ymin = r_rect->ymax - buth;
732         }
733         /* list view */
734         else {
735                 int buth = (BLI_rcti_size_y(&data->bbox) - 2 * MENU_TOP) / SEARCH_ITEMS;
736                 
737                 *r_rect = data->bbox;
738                 r_rect->xmin = data->bbox.xmin + 3.0f;
739                 r_rect->xmax = data->bbox.xmax - 3.0f;
740                 
741                 r_rect->ymax = data->bbox.ymax - MENU_TOP - itemnr * buth;
742                 r_rect->ymin = r_rect->ymax - buth;
743         }
744         
745 }
746
747 int ui_searchbox_find_index(ARegion *ar, const char *name)
748 {
749         uiSearchboxData *data = ar->regiondata;
750         return uiSearchItemFindIndex(&data->items, name);
751 }
752
753 /* x and y in screencoords */
754 bool ui_searchbox_inside(ARegion *ar, int x, int y)
755 {
756         uiSearchboxData *data = ar->regiondata;
757         
758         return BLI_rcti_isect_pt(&data->bbox, x - ar->winrct.xmin, y - ar->winrct.ymin);
759 }
760
761 /* string validated to be of correct length (but->hardmax) */
762 bool ui_searchbox_apply(uiBut *but, ARegion *ar)
763 {
764         uiSearchboxData *data = ar->regiondata;
765
766         but->func_arg2 = NULL;
767         
768         if (data->active != -1) {
769                 const char *name = data->items.names[data->active];
770                 const char *name_sep = data->use_sep ? strchr(name, UI_SEP_CHAR) : NULL;
771
772                 BLI_strncpy(but->editstr, name, name_sep ? (name_sep - name) : data->items.maxstrlen);
773                 
774                 but->func_arg2 = data->items.pointers[data->active];
775
776                 return true;
777         }
778         else {
779                 return false;
780         }
781 }
782
783 void ui_searchbox_event(bContext *C, ARegion *ar, uiBut *but, const wmEvent *event)
784 {
785         uiSearchboxData *data = ar->regiondata;
786         int type = event->type, val = event->val;
787         
788         if (type == MOUSEPAN)
789                 ui_pan_to_scroll(event, &type, &val);
790         
791         switch (type) {
792                 case WHEELUPMOUSE:
793                 case UPARROWKEY:
794                         ui_searchbox_select(C, ar, but, -1);
795                         break;
796                 case WHEELDOWNMOUSE:
797                 case DOWNARROWKEY:
798                         ui_searchbox_select(C, ar, but, 1);
799                         break;
800                 case MOUSEMOVE:
801                         if (BLI_rcti_isect_pt(&ar->winrct, event->x, event->y)) {
802                                 rcti rect;
803                                 int a;
804                                 
805                                 for (a = 0; a < data->items.totitem; a++) {
806                                         ui_searchbox_butrect(&rect, data, a);
807                                         if (BLI_rcti_isect_pt(&rect, event->x - ar->winrct.xmin, event->y - ar->winrct.ymin)) {
808                                                 if (data->active != a) {
809                                                         data->active = a;
810                                                         ui_searchbox_select(C, ar, but, 0);
811                                                         break;
812                                                 }
813                                         }
814                                 }
815                         }
816                         break;
817         }
818 }
819
820 /* ar is the search box itself */
821 void ui_searchbox_update(bContext *C, ARegion *ar, uiBut *but, const bool reset)
822 {
823         uiSearchboxData *data = ar->regiondata;
824         
825         /* reset vars */
826         data->items.totitem = 0;
827         data->items.more = 0;
828         if (reset == false) {
829                 data->items.offset_i = data->items.offset;
830         }
831         else {
832                 data->items.offset_i = data->items.offset = 0;
833                 data->active = -1;
834                 
835                 /* handle active */
836                 if (but->search_func && but->func_arg2) {
837                         data->items.active = but->func_arg2;
838                         but->search_func(C, but->search_arg, but->editstr, &data->items);
839                         data->items.active = NULL;
840                         
841                         /* found active item, calculate real offset by centering it */
842                         if (data->items.totitem) {
843                                 /* first case, begin of list */
844                                 if (data->items.offset_i < data->items.maxitem) {
845                                         data->active = data->items.offset_i;
846                                         data->items.offset_i = 0;
847                                 }
848                                 else {
849                                         /* second case, end of list */
850                                         if (data->items.totitem - data->items.offset_i <= data->items.maxitem) {
851                                                 data->active = data->items.offset_i - data->items.totitem + data->items.maxitem;
852                                                 data->items.offset_i = data->items.totitem - data->items.maxitem;
853                                         }
854                                         else {
855                                                 /* center active item */
856                                                 data->items.offset_i -= data->items.maxitem / 2;
857                                                 data->active = data->items.maxitem / 2;
858                                         }
859                                 }
860                         }
861                         data->items.offset = data->items.offset_i;
862                         data->items.totitem = 0;
863                 }
864         }
865         
866         /* callback */
867         if (but->search_func)
868                 but->search_func(C, but->search_arg, but->editstr, &data->items);
869         
870         /* handle case where editstr is equal to one of items */
871         if (reset && data->active == -1) {
872                 int a;
873                 
874                 for (a = 0; a < data->items.totitem; a++) {
875                         const char *name = data->items.names[a];
876                         const char *name_sep = data->use_sep ? strchr(name, UI_SEP_CHAR) : NULL;
877                         if (STREQLEN(but->editstr, name, name_sep ? (name_sep - name) : data->items.maxstrlen)) {
878                                 data->active = a;
879                                 break;
880                         }
881                 }
882                 if (data->items.totitem == 1 && but->editstr[0])
883                         data->active = 0;
884         }
885
886         /* validate selected item */
887         ui_searchbox_select(C, ar, but, 0);
888         
889         ED_region_tag_redraw(ar);
890 }
891
892 int ui_searchbox_autocomplete(bContext *C, ARegion *ar, uiBut *but, char *str)
893 {
894         uiSearchboxData *data = ar->regiondata;
895         int match = AUTOCOMPLETE_NO_MATCH;
896
897         if (str[0]) {
898                 data->items.autocpl = autocomplete_begin(str, ui_get_but_string_max_length(but));
899
900                 but->search_func(C, but->search_arg, but->editstr, &data->items);
901
902                 match = autocomplete_end(data->items.autocpl, str);
903                 data->items.autocpl = NULL;
904         }
905
906         return match;
907 }
908
909 static void ui_searchbox_region_draw_cb(const bContext *UNUSED(C), ARegion *ar)
910 {
911         uiSearchboxData *data = ar->regiondata;
912         
913         /* pixel space */
914         wmOrtho2(-0.01f, ar->winx - 0.01f, -0.01f, ar->winy - 0.01f);
915
916         if (data->noback == false)
917                 ui_draw_search_back(NULL, NULL, &data->bbox);  /* style not used yet */
918         
919         /* draw text */
920         if (data->items.totitem) {
921                 rcti rect;
922                 int a;
923                 
924                 if (data->preview) {
925                         /* draw items */
926                         for (a = 0; a < data->items.totitem; a++) {
927                                 ui_searchbox_butrect(&rect, data, a);
928                                 
929                                 /* widget itself */
930                                 if (data->preview) {
931                                         ui_draw_preview_item(&data->fstyle, &rect, data->items.names[a], data->items.icons[a],
932                                                              (a == data->active) ? UI_ACTIVE : 0);
933                                 }
934                                 else {
935                                         ui_draw_menu_item(&data->fstyle, &rect, data->items.names[a], data->items.icons[a],
936                                                           (a == data->active) ? UI_ACTIVE : 0, data->use_sep);
937                                 }
938                         }
939                         
940                         /* indicate more */
941                         if (data->items.more) {
942                                 ui_searchbox_butrect(&rect, data, data->items.maxitem - 1);
943                                 glEnable(GL_BLEND);
944                                 UI_icon_draw(rect.xmax - 18, rect.ymin - 7, ICON_TRIA_DOWN);
945                                 glDisable(GL_BLEND);
946                         }
947                         if (data->items.offset) {
948                                 ui_searchbox_butrect(&rect, data, 0);
949                                 glEnable(GL_BLEND);
950                                 UI_icon_draw(rect.xmin, rect.ymax - 9, ICON_TRIA_UP);
951                                 glDisable(GL_BLEND);
952                         }
953                         
954                 }
955                 else {
956                         /* draw items */
957                         for (a = 0; a < data->items.totitem; a++) {
958                                 ui_searchbox_butrect(&rect, data, a);
959                                 
960                                 /* widget itself */
961                                 ui_draw_menu_item(&data->fstyle, &rect, data->items.names[a], data->items.icons[a],
962                                                   (a == data->active) ? UI_ACTIVE : 0, data->use_sep);
963                                 
964                         }
965                         /* indicate more */
966                         if (data->items.more) {
967                                 ui_searchbox_butrect(&rect, data, data->items.maxitem - 1);
968                                 glEnable(GL_BLEND);
969                                 UI_icon_draw((BLI_rcti_size_x(&rect)) / 2, rect.ymin - 9, ICON_TRIA_DOWN);
970                                 glDisable(GL_BLEND);
971                         }
972                         if (data->items.offset) {
973                                 ui_searchbox_butrect(&rect, data, 0);
974                                 glEnable(GL_BLEND);
975                                 UI_icon_draw((BLI_rcti_size_x(&rect)) / 2, rect.ymax - 7, ICON_TRIA_UP);
976                                 glDisable(GL_BLEND);
977                         }
978                 }
979         }
980 }
981
982 static void ui_searchbox_region_free_cb(ARegion *ar)
983 {
984         uiSearchboxData *data = ar->regiondata;
985         int a;
986
987         /* free search data */
988         for (a = 0; a < data->items.maxitem; a++) {
989                 MEM_freeN(data->items.names[a]);
990         }
991         MEM_freeN(data->items.names);
992         MEM_freeN(data->items.pointers);
993         MEM_freeN(data->items.icons);
994         
995         MEM_freeN(data);
996         ar->regiondata = NULL;
997 }
998
999 ARegion *ui_searchbox_create(bContext *C, ARegion *butregion, uiBut *but)
1000 {
1001         wmWindow *win = CTX_wm_window(C);
1002         uiStyle *style = UI_GetStyle();
1003         static ARegionType type;
1004         ARegion *ar;
1005         uiSearchboxData *data;
1006         float aspect = but->block->aspect;
1007         rctf rect_fl;
1008         rcti rect_i;
1009         int winx /*, winy */, ofsx, ofsy;
1010         int i;
1011         
1012         /* create area region */
1013         ar = ui_add_temporary_region(CTX_wm_screen(C));
1014         
1015         memset(&type, 0, sizeof(ARegionType));
1016         type.draw = ui_searchbox_region_draw_cb;
1017         type.free = ui_searchbox_region_free_cb;
1018         type.regionid = RGN_TYPE_TEMPORARY;
1019         ar->type = &type;
1020         
1021         /* create searchbox data */
1022         data = MEM_callocN(sizeof(uiSearchboxData), "uiSearchboxData");
1023
1024         /* set font, get bb */
1025         data->fstyle = style->widget; /* copy struct */
1026         data->fstyle.align = UI_STYLE_TEXT_CENTER;
1027         ui_fontscale(&data->fstyle.points, aspect);
1028         uiStyleFontSet(&data->fstyle);
1029         
1030         ar->regiondata = data;
1031         
1032         /* special case, hardcoded feature, not draw backdrop when called from menus,
1033          * assume for design that popup already added it */
1034         if (but->block->flag & UI_BLOCK_SEARCH_MENU)
1035                 data->noback = true;
1036         
1037         if (but->a1 > 0 && but->a2 > 0) {
1038                 data->preview = true;
1039                 data->prv_rows = but->a1;
1040                 data->prv_cols = but->a2;
1041         }
1042
1043         /* only show key shortcuts when needed (not rna buttons) [#36699] */
1044         if (but->rnaprop == NULL) {
1045                 data->use_sep = true;
1046         }
1047         
1048         /* compute position */
1049         if (but->block->flag & UI_BLOCK_SEARCH_MENU) {
1050                 int width = UI_ThemeMenuShadowWidth();
1051                 /* this case is search menu inside other menu */
1052                 /* we copy region size */
1053
1054                 ar->winrct = butregion->winrct;
1055                 
1056                 /* widget rect, in region coords */
1057                 data->bbox.xmin = width;
1058                 data->bbox.xmax = BLI_rcti_size_x(&ar->winrct) - width;
1059                 data->bbox.ymin = width;
1060                 data->bbox.ymax = BLI_rcti_size_y(&ar->winrct) - width;
1061                 
1062                 /* check if button is lower half */
1063                 if (but->rect.ymax < BLI_rctf_cent_y(&but->block->rect)) {
1064                         data->bbox.ymin += BLI_rctf_size_y(&but->rect);
1065                 }
1066                 else {
1067                         data->bbox.ymax -= BLI_rctf_size_y(&but->rect);
1068                 }
1069         }
1070         else {
1071                 const int searchbox_width = uiSearchBoxWidth();
1072                 const int shadow_width = UI_ThemeMenuShadowWidth();
1073
1074                 rect_fl.xmin = but->rect.xmin - 5;   /* align text with button */
1075                 rect_fl.xmax = but->rect.xmax + 5;   /* symmetrical */
1076                 rect_fl.ymax = but->rect.ymin;
1077                 rect_fl.ymin = rect_fl.ymax - uiSearchBoxHeight();
1078
1079                 ofsx = (but->block->panel) ? but->block->panel->ofsx : 0;
1080                 ofsy = (but->block->panel) ? but->block->panel->ofsy : 0;
1081
1082                 BLI_rctf_translate(&rect_fl, ofsx, ofsy);
1083         
1084                 /* minimal width */
1085                 if (BLI_rctf_size_x(&rect_fl) < searchbox_width) {
1086                         rect_fl.xmax = rect_fl.xmin + searchbox_width;
1087                 }
1088                 
1089                 /* copy to int, gets projected if possible too */
1090                 BLI_rcti_rctf_copy(&rect_i, &rect_fl);
1091                 
1092                 if (butregion->v2d.cur.xmin != butregion->v2d.cur.xmax) {
1093                         UI_view2d_to_region_no_clip(&butregion->v2d, rect_fl.xmin, rect_fl.ymin, &rect_i.xmin, &rect_i.ymin);
1094                         UI_view2d_to_region_no_clip(&butregion->v2d, rect_fl.xmax, rect_fl.ymax, &rect_i.xmax, &rect_i.ymax);
1095                 }
1096
1097                 BLI_rcti_translate(&rect_i, butregion->winrct.xmin, butregion->winrct.ymin);
1098
1099                 winx = WM_window_pixels_x(win);
1100                 // winy = WM_window_pixels_y(win);  /* UNUSED */
1101                 //wm_window_get_size(win, &winx, &winy);
1102                 
1103                 if (rect_i.xmax > winx) {
1104                         /* super size */
1105                         if (rect_i.xmax > winx + rect_i.xmin) {
1106                                 rect_i.xmax = winx;
1107                                 rect_i.xmin = 0;
1108                         }
1109                         else {
1110                                 rect_i.xmin -= rect_i.xmax - winx;
1111                                 rect_i.xmax = winx;
1112                         }
1113                 }
1114
1115                 if (rect_i.ymin < 0) {
1116                         int newy1 = but->rect.ymax + ofsy;
1117
1118                         if (butregion->v2d.cur.xmin != butregion->v2d.cur.xmax)
1119                                 UI_view2d_to_region_no_clip(&butregion->v2d, 0, newy1, NULL, &newy1);
1120
1121                         newy1 += butregion->winrct.ymin;
1122
1123                         rect_i.ymax = BLI_rcti_size_y(&rect_i) + newy1;
1124                         rect_i.ymin = newy1;
1125                 }
1126
1127                 /* widget rect, in region coords */
1128                 data->bbox.xmin = shadow_width;
1129                 data->bbox.xmax = BLI_rcti_size_x(&rect_i) + shadow_width;
1130                 data->bbox.ymin = shadow_width;
1131                 data->bbox.ymax = BLI_rcti_size_y(&rect_i) + shadow_width;
1132                 
1133                 /* region bigger for shadow */
1134                 ar->winrct.xmin = rect_i.xmin - shadow_width;
1135                 ar->winrct.xmax = rect_i.xmax + shadow_width;
1136                 ar->winrct.ymin = rect_i.ymin - shadow_width;
1137                 ar->winrct.ymax = rect_i.ymax;
1138         }
1139         
1140         /* adds subwindow */
1141         ED_region_init(C, ar);
1142         
1143         /* notify change and redraw */
1144         ED_region_tag_redraw(ar);
1145         
1146         /* prepare search data */
1147         if (data->preview) {
1148                 data->items.maxitem = data->prv_rows * data->prv_cols;
1149         }
1150         else {
1151                 data->items.maxitem = SEARCH_ITEMS;
1152         }
1153         data->items.maxstrlen = but->hardmax;
1154         data->items.totitem = 0;
1155         data->items.names = MEM_callocN(data->items.maxitem * sizeof(void *), "search names");
1156         data->items.pointers = MEM_callocN(data->items.maxitem * sizeof(void *), "search pointers");
1157         data->items.icons = MEM_callocN(data->items.maxitem * sizeof(int), "search icons");
1158         for (i = 0; i < data->items.maxitem; i++)
1159                 data->items.names[i] = MEM_callocN(but->hardmax + 1, "search pointers");
1160         
1161         return ar;
1162 }
1163
1164 void ui_searchbox_free(bContext *C, ARegion *ar)
1165 {
1166         ui_remove_temporary_region(C, CTX_wm_screen(C), ar);
1167 }
1168
1169 /* sets red alert if button holds a string it can't find */
1170 /* XXX weak: search_func adds all partial matches... */
1171 void ui_but_search_test(uiBut *but)
1172 {
1173         uiSearchItems *items;
1174         int x1;
1175
1176         /* possibly very large lists (such as ID datablocks) only
1177          * only validate string RNA buts (not pointers) */
1178         if (but->rnaprop && RNA_property_type(but->rnaprop) != PROP_STRING) {
1179                 return;
1180         }
1181
1182         items = MEM_callocN(sizeof(uiSearchItems), "search items");
1183
1184         /* setup search struct */
1185         items->maxitem = 10;
1186         items->maxstrlen = 256;
1187         items->names = MEM_callocN(items->maxitem * sizeof(void *), "search names");
1188         for (x1 = 0; x1 < items->maxitem; x1++)
1189                 items->names[x1] = MEM_callocN(but->hardmax + 1, "search names");
1190         
1191         but->search_func(but->block->evil_C, but->search_arg, but->drawstr, items);
1192         
1193         /* only redalert when we are sure of it, this can miss cases when >10 matches */
1194         if (items->totitem == 0) {
1195                 uiButSetFlag(but, UI_BUT_REDALERT);
1196         }
1197         else if (items->more == 0) {
1198                 if (uiSearchItemFindIndex(items, but->drawstr) == -1) {
1199                         uiButSetFlag(but, UI_BUT_REDALERT);
1200                 }
1201         }
1202         
1203         for (x1 = 0; x1 < items->maxitem; x1++) {
1204                 MEM_freeN(items->names[x1]);
1205         }
1206         MEM_freeN(items->names);
1207         MEM_freeN(items);
1208 }
1209
1210
1211 /************************* Creating Menu Blocks **********************/
1212
1213 /* position block relative to but, result is in window space */
1214 static void ui_block_position(wmWindow *window, ARegion *butregion, uiBut *but, uiBlock *block)
1215 {
1216         uiBut *bt;
1217         uiSafetyRct *saferct;
1218         rctf butrct;
1219         /*float aspect;*/ /*UNUSED*/
1220         int xsize, ysize, xof = 0, yof = 0, center;
1221         short dir1 = 0, dir2 = 0;
1222         
1223         /* transform to window coordinates, using the source button region/block */
1224         butrct = but->rect;
1225
1226         ui_block_to_window_fl(butregion, but->block, &butrct.xmin, &butrct.ymin);
1227         ui_block_to_window_fl(butregion, but->block, &butrct.xmax, &butrct.ymax);
1228
1229         /* widget_roundbox_set has this correction too, keep in sync */
1230         if (but->type != PULLDOWN) {
1231                 if (but->drawflag & UI_BUT_ALIGN_TOP)
1232                         butrct.ymax += U.pixelsize;
1233                 if (but->drawflag & UI_BUT_ALIGN_LEFT)
1234                         butrct.xmin -= U.pixelsize;
1235         }
1236         
1237         /* calc block rect */
1238         if (block->rect.xmin == 0.0f && block->rect.xmax == 0.0f) {
1239                 if (block->buttons.first) {
1240                         BLI_rctf_init_minmax(&block->rect);
1241
1242                         for (bt = block->buttons.first; bt; bt = bt->next) {
1243                                 BLI_rctf_union(&block->rect, &bt->rect);
1244                         }
1245                 }
1246                 else {
1247                         /* we're nice and allow empty blocks too */
1248                         block->rect.xmin = block->rect.ymin = 0;
1249                         block->rect.xmax = block->rect.ymax = 20;
1250                 }
1251         }
1252                 
1253         /* aspect = (float)(BLI_rcti_size_x(&block->rect) + 4);*/ /*UNUSED*/
1254         ui_block_to_window_fl(butregion, but->block, &block->rect.xmin, &block->rect.ymin);
1255         ui_block_to_window_fl(butregion, but->block, &block->rect.xmax, &block->rect.ymax);
1256
1257         //block->rect.xmin -= 2.0; block->rect.ymin -= 2.0;
1258         //block->rect.xmax += 2.0; block->rect.ymax += 2.0;
1259         
1260         xsize = BLI_rctf_size_x(&block->rect) + 0.2f * UI_UNIT_X;  /* 4 for shadow */
1261         ysize = BLI_rctf_size_y(&block->rect) + 0.2f * UI_UNIT_Y;
1262         /* aspect /= (float)xsize;*/ /*UNUSED*/
1263
1264         {
1265                 int left = 0, right = 0, top = 0, down = 0;
1266                 int winx, winy;
1267                 // int offscreen;
1268
1269                 winx = WM_window_pixels_x(window);
1270                 winy = WM_window_pixels_y(window);
1271                 // wm_window_get_size(window, &winx, &winy);
1272
1273                 if (block->direction & UI_CENTER) center = ysize / 2;
1274                 else center = 0;
1275                 
1276                 /* check if there's space at all */
1277                 if (butrct.xmin - xsize > 0.0f) left = 1;
1278                 if (butrct.xmax + xsize < winx) right = 1;
1279                 if (butrct.ymin - ysize + center > 0.0f) down = 1;
1280                 if (butrct.ymax + ysize - center < winy) top = 1;
1281
1282                 if (top == 0 && down == 0) {
1283                         if (butrct.ymin - ysize < winy - butrct.ymax - ysize)
1284                                 top = 1;
1285                         else
1286                                 down = 1;
1287                 }
1288                 
1289                 dir1 = block->direction & UI_DIRECTION;
1290
1291                 /* secundary directions */
1292                 if (dir1 & (UI_TOP | UI_DOWN)) {
1293                         if (dir1 & UI_LEFT) dir2 = UI_LEFT;
1294                         else if (dir1 & UI_RIGHT) dir2 = UI_RIGHT;
1295                         dir1 &= (UI_TOP | UI_DOWN);
1296                 }
1297
1298                 if ((dir2 == 0) && (dir1 == UI_LEFT || dir1 == UI_RIGHT)) dir2 = UI_DOWN;
1299                 if ((dir2 == 0) && (dir1 == UI_TOP  || dir1 == UI_DOWN))  dir2 = UI_LEFT;
1300                 
1301                 /* no space at all? don't change */
1302                 if (left || right) {
1303                         if (dir1 == UI_LEFT && left == 0) dir1 = UI_RIGHT;
1304                         if (dir1 == UI_RIGHT && right == 0) dir1 = UI_LEFT;
1305                         /* this is aligning, not append! */
1306                         if (dir2 == UI_LEFT && right == 0) dir2 = UI_RIGHT;
1307                         if (dir2 == UI_RIGHT && left == 0) dir2 = UI_LEFT;
1308                 }
1309                 if (down || top) {
1310                         if (dir1 == UI_TOP && top == 0) dir1 = UI_DOWN;
1311                         if (dir1 == UI_DOWN && down == 0) dir1 = UI_TOP;
1312                         if (dir2 == UI_TOP && top == 0) dir2 = UI_DOWN;
1313                         if (dir2 == UI_DOWN && down == 0) dir2 = UI_TOP;
1314                 }
1315
1316                 if (dir1 == UI_LEFT) {
1317                         xof = butrct.xmin - block->rect.xmax;
1318                         if (dir2 == UI_TOP) yof = butrct.ymin - block->rect.ymin - center - MENU_PADDING;
1319                         else yof = butrct.ymax - block->rect.ymax + center + MENU_PADDING;
1320                 }
1321                 else if (dir1 == UI_RIGHT) {
1322                         xof = butrct.xmax - block->rect.xmin;
1323                         if (dir2 == UI_TOP) yof = butrct.ymin - block->rect.ymin - center - MENU_PADDING;
1324                         else yof = butrct.ymax - block->rect.ymax + center + MENU_PADDING;
1325                 }
1326                 else if (dir1 == UI_TOP) {
1327                         yof = butrct.ymax - block->rect.ymin;
1328                         if (dir2 == UI_RIGHT) xof = butrct.xmax - block->rect.xmax;
1329                         else xof = butrct.xmin - block->rect.xmin;
1330                         /* changed direction? */
1331                         if ((dir1 & block->direction) == 0) {
1332                                 if (block->direction & UI_SHIFT_FLIPPED)
1333                                         xof += dir2 == UI_LEFT ? 25 : -25;
1334                                 uiBlockFlipOrder(block);
1335                         }
1336                 }
1337                 else if (dir1 == UI_DOWN) {
1338                         yof = butrct.ymin - block->rect.ymax;
1339                         if (dir2 == UI_RIGHT) xof = butrct.xmax - block->rect.xmax;
1340                         else xof = butrct.xmin - block->rect.xmin;
1341                         /* changed direction? */
1342                         if ((dir1 & block->direction) == 0) {
1343                                 if (block->direction & UI_SHIFT_FLIPPED)
1344                                         xof += dir2 == UI_LEFT ? 25 : -25;
1345                                 uiBlockFlipOrder(block);
1346                         }
1347                 }
1348
1349                 /* and now we handle the exception; no space below or to top */
1350                 if (top == 0 && down == 0) {
1351                         if (dir1 == UI_LEFT || dir1 == UI_RIGHT) {
1352                                 /* align with bottom of screen */
1353                                 // yof = ysize; (not with menu scrolls)
1354                         }
1355                 }
1356                 
1357                 /* or no space left or right */
1358                 if (left == 0 && right == 0) {
1359                         if (dir1 == UI_TOP || dir1 == UI_DOWN) {
1360                                 /* align with left size of screen */
1361                                 xof = -block->rect.xmin + 5;
1362                         }
1363                 }
1364                 
1365 #if 0
1366                 /* clamp to window bounds, could be made into an option if its ever annoying */
1367                 if (     (offscreen = (block->rect.ymin + yof)) < 0) yof -= offscreen;   /* bottom */
1368                 else if ((offscreen = (block->rect.ymax + yof) - winy) > 0) yof -= offscreen;  /* top */
1369                 if (     (offscreen = (block->rect.xmin + xof)) < 0) xof -= offscreen;   /* left */
1370                 else if ((offscreen = (block->rect.xmax + xof) - winx) > 0) xof -= offscreen;  /* right */
1371 #endif
1372         }
1373         
1374         /* apply offset, buttons in window coords */
1375         
1376         for (bt = block->buttons.first; bt; bt = bt->next) {
1377                 ui_block_to_window_fl(butregion, but->block, &bt->rect.xmin, &bt->rect.ymin);
1378                 ui_block_to_window_fl(butregion, but->block, &bt->rect.xmax, &bt->rect.ymax);
1379
1380                 BLI_rctf_translate(&bt->rect, xof, yof);
1381
1382                 bt->aspect = 1.0f;
1383                 /* ui_check_but recalculates drawstring size in pixels */
1384                 ui_check_but(bt);
1385         }
1386         
1387         BLI_rctf_translate(&block->rect, xof, yof);
1388
1389         /* safety calculus */
1390         {
1391                 const float midx = BLI_rctf_cent_x(&butrct);
1392                 const float midy = BLI_rctf_cent_y(&butrct);
1393                 
1394                 /* when you are outside parent button, safety there should be smaller */
1395                 
1396                 /* parent button to left */
1397                 if (midx < block->rect.xmin) block->safety.xmin = block->rect.xmin - 3;
1398                 else block->safety.xmin = block->rect.xmin - 40;
1399                 /* parent button to right */
1400                 if (midx > block->rect.xmax) block->safety.xmax = block->rect.xmax + 3;
1401                 else block->safety.xmax = block->rect.xmax + 40;
1402
1403                 /* parent button on bottom */
1404                 if (midy < block->rect.ymin) block->safety.ymin = block->rect.ymin - 3;
1405                 else block->safety.ymin = block->rect.ymin - 40;
1406                 /* parent button on top */
1407                 if (midy > block->rect.ymax) block->safety.ymax = block->rect.ymax + 3;
1408                 else block->safety.ymax = block->rect.ymax + 40;
1409
1410                 /* exception for switched pulldowns... */
1411                 if (dir1 && (dir1 & block->direction) == 0) {
1412                         if (dir2 == UI_RIGHT) block->safety.xmax = block->rect.xmax + 3;
1413                         if (dir2 == UI_LEFT) block->safety.xmin = block->rect.xmin - 3;
1414                 }
1415                 block->direction = dir1;
1416         }
1417
1418         /* keep a list of these, needed for pulldown menus */
1419         saferct = MEM_callocN(sizeof(uiSafetyRct), "uiSafetyRct");
1420         saferct->parent = butrct;
1421         saferct->safety = block->safety;
1422         BLI_freelistN(&block->saferct);
1423         BLI_duplicatelist(&block->saferct, &but->block->saferct);
1424         BLI_addhead(&block->saferct, saferct);
1425 }
1426
1427 static void ui_block_region_draw(const bContext *C, ARegion *ar)
1428 {
1429         uiBlock *block;
1430
1431         for (block = ar->uiblocks.first; block; block = block->next)
1432                 uiDrawBlock(C, block);
1433 }
1434
1435 static void ui_popup_block_clip(wmWindow *window, uiBlock *block)
1436 {
1437         uiBut *bt;
1438         float xofs = 0.0f;
1439         int width = UI_SCREEN_MARGIN;
1440         int winx, winy;
1441
1442         if (block->flag & UI_BLOCK_NO_WIN_CLIP) {
1443                 return;
1444         }
1445
1446         winx = WM_window_pixels_x(window);
1447         winy = WM_window_pixels_y(window);
1448         
1449         /* shift menus to right if outside of view */
1450         if (block->rect.xmin < width) {
1451                 xofs = (width - block->rect.xmin);
1452                 block->rect.xmin += xofs;
1453                 block->rect.xmax += xofs;
1454         }
1455         /* or shift to left if outside of view */
1456         if (block->rect.xmax > winx - width) {
1457                 xofs = winx - width - block->rect.xmax;
1458                 block->rect.xmin += xofs;
1459                 block->rect.xmax += xofs;
1460         }
1461         
1462         if (block->rect.ymin < width)
1463                 block->rect.ymin = width;
1464         if (block->rect.ymax > winy - MENU_TOP)
1465                 block->rect.ymax = winy - MENU_TOP;
1466         
1467         /* ensure menu items draw inside left/right boundary */
1468         for (bt = block->buttons.first; bt; bt = bt->next) {
1469                 bt->rect.xmin += xofs;
1470                 bt->rect.xmax += xofs;
1471         }
1472
1473 }
1474
1475 void ui_popup_block_scrolltest(uiBlock *block)
1476 {
1477         uiBut *bt;
1478         
1479         block->flag &= ~(UI_BLOCK_CLIPBOTTOM | UI_BLOCK_CLIPTOP);
1480         
1481         for (bt = block->buttons.first; bt; bt = bt->next)
1482                 bt->flag &= ~UI_SCROLLED;
1483         
1484         if (block->buttons.first == block->buttons.last)
1485                 return;
1486         
1487         /* mark buttons that are outside boundary */
1488         for (bt = block->buttons.first; bt; bt = bt->next) {
1489                 if (bt->rect.ymin < block->rect.ymin) {
1490                         bt->flag |= UI_SCROLLED;
1491                         block->flag |= UI_BLOCK_CLIPBOTTOM;
1492                 }
1493                 if (bt->rect.ymax > block->rect.ymax) {
1494                         bt->flag |= UI_SCROLLED;
1495                         block->flag |= UI_BLOCK_CLIPTOP;
1496                 }
1497         }
1498
1499         /* mark buttons overlapping arrows, if we have them */
1500         for (bt = block->buttons.first; bt; bt = bt->next) {
1501                 if (block->flag & UI_BLOCK_CLIPBOTTOM) {
1502                         if (bt->rect.ymin < block->rect.ymin + UI_MENU_SCROLL_ARROW)
1503                                 bt->flag |= UI_SCROLLED;
1504                 }
1505                 if (block->flag & UI_BLOCK_CLIPTOP) {
1506                         if (bt->rect.ymax > block->rect.ymax - UI_MENU_SCROLL_ARROW)
1507                                 bt->flag |= UI_SCROLLED;
1508                 }
1509         }
1510 }
1511
1512 uiPopupBlockHandle *ui_popup_block_create(bContext *C, ARegion *butregion, uiBut *but,
1513                                           uiBlockCreateFunc create_func, uiBlockHandleCreateFunc handle_create_func,
1514                                           void *arg)
1515 {
1516         wmWindow *window = CTX_wm_window(C);
1517         static ARegionType type;
1518         ARegion *ar;
1519         uiBlock *block;
1520         uiPopupBlockHandle *handle;
1521         uiSafetyRct *saferct;
1522         int width = UI_ThemeMenuShadowWidth();
1523
1524         /* create handle */
1525         handle = MEM_callocN(sizeof(uiPopupBlockHandle), "uiPopupBlockHandle");
1526
1527         /* store context for operator */
1528         handle->ctx_area = CTX_wm_area(C);
1529         handle->ctx_region = CTX_wm_region(C);
1530         
1531         /* create area region */
1532         ar = ui_add_temporary_region(CTX_wm_screen(C));
1533         handle->region = ar;
1534
1535         memset(&type, 0, sizeof(ARegionType));
1536         type.draw = ui_block_region_draw;
1537         type.regionid = RGN_TYPE_TEMPORARY;
1538         ar->type = &type;
1539
1540         UI_add_region_handlers(&ar->handlers);
1541
1542         /* create ui block */
1543         if (create_func)
1544                 block = create_func(C, handle->region, arg);
1545         else
1546                 block = handle_create_func(C, handle, arg);
1547         
1548         if (block->handle) {
1549                 memcpy(block->handle, handle, sizeof(uiPopupBlockHandle));
1550                 MEM_freeN(handle);
1551                 handle = block->handle;
1552         }
1553         else
1554                 block->handle = handle;
1555
1556         ar->regiondata = handle;
1557
1558         /* set UI_BLOCK_NUMSELECT before uiEndBlock() so we get alphanumeric keys assigned */
1559         if (but) {
1560                 if (but->type == PULLDOWN) {
1561                         block->flag |= UI_BLOCK_NUMSELECT;
1562                 }
1563         }
1564         else {
1565                 block->flag |= UI_BLOCK_POPUP | UI_BLOCK_NUMSELECT;
1566         }
1567
1568         block->flag |= UI_BLOCK_LOOP;
1569
1570         if (!block->endblock)
1571                 uiEndBlock(C, block);
1572
1573         /* if this is being created from a button */
1574         if (but) {
1575                 block->aspect = but->block->aspect;
1576                 ui_block_position(window, butregion, but, block);
1577                 handle->direction = block->direction;
1578         }
1579         else {
1580                 /* keep a list of these, needed for pulldown menus */
1581                 saferct = MEM_callocN(sizeof(uiSafetyRct), "uiSafetyRct");
1582                 saferct->safety = block->safety;
1583                 BLI_addhead(&block->saferct, saferct);
1584         }
1585
1586         /* clip block with window boundary */
1587         ui_popup_block_clip(window, block);
1588         
1589         /* the block and buttons were positioned in window space as in 2.4x, now
1590          * these menu blocks are regions so we bring it back to region space.
1591          * additionally we add some padding for the menu shadow or rounded menus */
1592         ar->winrct.xmin = block->rect.xmin - width;
1593         ar->winrct.xmax = block->rect.xmax + width;
1594         ar->winrct.ymin = block->rect.ymin - width;
1595         ar->winrct.ymax = block->rect.ymax + MENU_TOP;
1596         
1597         ui_block_translate(block, -ar->winrct.xmin, -ar->winrct.ymin);
1598
1599         /* adds subwindow */
1600         ED_region_init(C, ar);
1601
1602         /* checks which buttons are visible, sets flags to prevent draw (do after region init) */
1603         ui_popup_block_scrolltest(block);
1604         
1605         /* get winmat now that we actually have the subwindow */
1606         wmSubWindowSet(window, ar->swinid);
1607         
1608         wm_subwindow_getmatrix(window, ar->swinid, block->winmat);
1609         
1610         /* notify change and redraw */
1611         ED_region_tag_redraw(ar);
1612
1613         return handle;
1614 }
1615
1616 void ui_popup_block_free(bContext *C, uiPopupBlockHandle *handle)
1617 {
1618         ui_remove_temporary_region(C, CTX_wm_screen(C), handle->region);
1619         
1620         if (handle->scrolltimer)
1621                 WM_event_remove_timer(CTX_wm_manager(C), CTX_wm_window(C), handle->scrolltimer);
1622         
1623         MEM_freeN(handle);
1624 }
1625
1626 /***************************** Menu Button ***************************/
1627
1628 #if 0
1629 static void ui_warp_pointer(int x, int y)
1630 {
1631         /* XXX 2.50 which function to use for this? */
1632         /* OSX has very poor mouse-warp support, it sends events;
1633          * this causes a menu being pressed immediately ... */
1634 #  ifndef __APPLE__
1635         warp_pointer(x, y);
1636 #  endif
1637 }
1638 #endif
1639
1640 /********************* Color Button ****************/
1641
1642 /* for picker, while editing hsv */
1643 void ui_set_but_hsv(uiBut *but)
1644 {
1645         float col[3];
1646         float *hsv = ui_block_hsv_get(but->block);
1647         
1648         hsv_to_rgb_v(hsv, col);
1649         ui_set_but_vectorf(but, col);
1650 }
1651
1652 /* also used by small picker, be careful with name checks below... */
1653 static void ui_update_block_buts_rgb(uiBlock *block, const float rgb[3])
1654 {
1655         uiBut *bt;
1656         float *hsv = ui_block_hsv_get(block);
1657         struct ColorManagedDisplay *display = NULL;
1658         
1659         /* this is to keep the H and S value when V is equal to zero
1660          * and we are working in HSV mode, of course!
1661          */
1662         rgb_to_hsv_compat_v(rgb, hsv);
1663
1664         if (block->color_profile)
1665                 display = ui_block_display_get(block);
1666         
1667         /* this updates button strings, is hackish... but button pointers are on stack of caller function */
1668         for (bt = block->buttons.first; bt; bt = bt->next) {
1669                 if (bt->rnaprop) {
1670                         
1671                         ui_set_but_vectorf(bt, rgb);
1672                         
1673                 }
1674                 else if (strcmp(bt->str, "Hex: ") == 0) {
1675                         float rgb_gamma[3];
1676                         double intpart;
1677                         char col[16];
1678                         
1679                         /* Hex code is assumed to be in sRGB space (coming from other applications, web, etc) */
1680                         
1681                         copy_v3_v3(rgb_gamma, rgb);
1682
1683                         if (display) {
1684                                 /* make a display version, for Hex code */
1685                                 IMB_colormanagement_scene_linear_to_display_v3(rgb_gamma, display);
1686                         }
1687                         
1688                         if (rgb_gamma[0] > 1.0f) rgb_gamma[0] = modf(rgb_gamma[0], &intpart);
1689                         if (rgb_gamma[1] > 1.0f) rgb_gamma[1] = modf(rgb_gamma[1], &intpart);
1690                         if (rgb_gamma[2] > 1.0f) rgb_gamma[2] = modf(rgb_gamma[2], &intpart);
1691
1692                         BLI_snprintf(col, sizeof(col), "%02X%02X%02X",
1693                                      FTOCHAR(rgb_gamma[0]), FTOCHAR(rgb_gamma[1]), FTOCHAR(rgb_gamma[2]));
1694                         
1695                         strcpy(bt->poin, col);
1696                 }
1697                 else if (bt->str[1] == ' ') {
1698                         if (bt->str[0] == 'R') {
1699                                 ui_set_but_val(bt, rgb[0]);
1700                         }
1701                         else if (bt->str[0] == 'G') {
1702                                 ui_set_but_val(bt, rgb[1]);
1703                         }
1704                         else if (bt->str[0] == 'B') {
1705                                 ui_set_but_val(bt, rgb[2]);
1706                         }
1707                         else if (bt->str[0] == 'H') {
1708                                 ui_set_but_val(bt, hsv[0]);
1709                         }
1710                         else if (bt->str[0] == 'S') {
1711                                 ui_set_but_val(bt, hsv[1]);
1712                         }
1713                         else if (bt->str[0] == 'V') {
1714                                 ui_set_but_val(bt, hsv[2]);
1715                         }
1716                 }
1717
1718                 ui_check_but(bt);
1719         }
1720 }
1721
1722 static void do_picker_rna_cb(bContext *UNUSED(C), void *bt1, void *UNUSED(arg))
1723 {
1724         uiBut *but = (uiBut *)bt1;
1725         uiPopupBlockHandle *popup = but->block->handle;
1726         PropertyRNA *prop = but->rnaprop;
1727         PointerRNA ptr = but->rnapoin;
1728         float rgb[4];
1729         
1730         if (prop) {
1731                 RNA_property_float_get_array(&ptr, prop, rgb);
1732                 ui_update_block_buts_rgb(but->block, rgb);
1733         }
1734         
1735         if (popup)
1736                 popup->menuretval = UI_RETURN_UPDATE;
1737 }
1738
1739 static void do_hsv_rna_cb(bContext *UNUSED(C), void *bt1, void *UNUSED(arg))
1740 {
1741         uiBut *but = (uiBut *)bt1;
1742         uiPopupBlockHandle *popup = but->block->handle;
1743         float rgb[3];
1744         float *hsv = ui_block_hsv_get(but->block);
1745         
1746         hsv_to_rgb_v(hsv, rgb);
1747         
1748         ui_update_block_buts_rgb(but->block, rgb);
1749         
1750         if (popup)
1751                 popup->menuretval = UI_RETURN_UPDATE;
1752 }
1753
1754 static void do_hex_rna_cb(bContext *UNUSED(C), void *bt1, void *hexcl)
1755 {
1756         uiBut *but = (uiBut *)bt1;
1757         uiPopupBlockHandle *popup = but->block->handle;
1758         char *hexcol = (char *)hexcl;
1759         float rgb[3];
1760         
1761         hex_to_rgb(hexcol, rgb, rgb + 1, rgb + 2);
1762         
1763         /* Hex code is assumed to be in sRGB space (coming from other applications, web, etc) */
1764         if (but->block->color_profile) {
1765                 /* so we need to linearise it for Blender */
1766                 ui_block_to_scene_linear_v3(but->block, rgb);
1767         }
1768         
1769         ui_update_block_buts_rgb(but->block, rgb);
1770         
1771         if (popup)
1772                 popup->menuretval = UI_RETURN_UPDATE;
1773 }
1774
1775 static void close_popup_cb(bContext *UNUSED(C), void *bt1, void *UNUSED(arg))
1776 {
1777         uiBut *but = (uiBut *)bt1;
1778         uiPopupBlockHandle *popup = but->block->handle;
1779         
1780         if (popup)
1781                 popup->menuretval = UI_RETURN_OK;
1782 }
1783
1784 static void picker_new_hide_reveal(uiBlock *block, short colormode)
1785 {
1786         uiBut *bt;
1787         
1788         /* tag buttons */
1789         for (bt = block->buttons.first; bt; bt = bt->next) {
1790                 if (bt->func == do_picker_rna_cb && bt->type == NUMSLI && bt->rnaindex != 3) {
1791                         /* RGB sliders (color circle and alpha are always shown) */
1792                         if (colormode == 0) bt->flag &= ~UI_HIDDEN;
1793                         else bt->flag |= UI_HIDDEN;
1794                 }
1795                 else if (bt->func == do_hsv_rna_cb) {
1796                         /* HSV sliders */
1797                         if (colormode == 1) bt->flag &= ~UI_HIDDEN;
1798                         else bt->flag |= UI_HIDDEN;
1799                 }
1800                 else if (bt->func == do_hex_rna_cb || bt->type == LABEL) {
1801                         /* hex input or gamma correction status label */
1802                         if (colormode == 2) bt->flag &= ~UI_HIDDEN;
1803                         else bt->flag |= UI_HIDDEN;
1804                 }
1805         }
1806 }
1807
1808 static void do_picker_new_mode_cb(bContext *UNUSED(C), void *bt1, void *UNUSED(arg))
1809 {
1810         uiBut *bt = bt1;
1811         short colormode = ui_get_but_val(bt);
1812         picker_new_hide_reveal(bt->block, colormode);
1813 }
1814
1815 #define PICKER_H    (7.5f * U.widget_unit)
1816 #define PICKER_W    (7.5f * U.widget_unit)
1817 #define PICKER_SPACE    (0.3f * U.widget_unit)
1818 #define PICKER_BAR      (0.7f * U.widget_unit)
1819
1820 #define PICKER_TOTAL_W  (PICKER_W + PICKER_SPACE + PICKER_BAR)
1821
1822 static void circle_picker(uiBlock *block, PointerRNA *ptr, PropertyRNA *prop)
1823 {
1824         uiBut *bt;
1825         
1826         /* HS circle */
1827         bt = uiDefButR_prop(block, HSVCIRCLE, 0, "", 0, 0, PICKER_H, PICKER_W, ptr, prop, -1, 0.0, 0.0, 0, 0, "Color");
1828         uiButSetFunc(bt, do_picker_rna_cb, bt, NULL);
1829         
1830         /* value */
1831         bt = uiDefButR_prop(block, HSVCUBE, 0, "", PICKER_W + PICKER_SPACE, 0, PICKER_BAR, PICKER_H, ptr, prop, -1, 0.0, 0.0, UI_GRAD_V_ALT, 0, "Value");
1832         uiButSetFunc(bt, do_picker_rna_cb, bt, NULL);
1833 }
1834
1835
1836 static void square_picker(uiBlock *block, PointerRNA *ptr, PropertyRNA *prop, int type)
1837 {
1838         uiBut *bt;
1839         int bartype = type + 3;
1840         
1841         /* HS square */
1842         bt = uiDefButR_prop(block, HSVCUBE, 0, "",   0, PICKER_BAR + PICKER_SPACE, PICKER_TOTAL_W, PICKER_H, ptr, prop, -1, 0.0, 0.0, type, 0, "Color");
1843         uiButSetFunc(bt, do_picker_rna_cb, bt, NULL);
1844         
1845         /* value */
1846         bt = uiDefButR_prop(block, HSVCUBE, 0, "",       0, 0, PICKER_TOTAL_W, PICKER_BAR, ptr, prop, -1, 0.0, 0.0, bartype, 0, "Value");
1847         uiButSetFunc(bt, do_picker_rna_cb, bt, NULL);
1848 }
1849
1850
1851 /* a HS circle, V slider, rgb/hsv/hex sliders */
1852 static void uiBlockPicker(uiBlock *block, float rgba[4], PointerRNA *ptr, PropertyRNA *prop, bool show_picker)
1853 {
1854         static short colormode = 0;  /* temp? 0=rgb, 1=hsv, 2=hex */
1855         uiBut *bt;
1856         int width, butwidth;
1857         static char tip[50];
1858         static char hexcol[128];
1859         float rgb_gamma[3];
1860         float softmin, softmax, hardmin, hardmax, step, precision;
1861         float *hsv = ui_block_hsv_get(block);
1862         int yco;
1863         
1864         ui_block_hsv_get(block);
1865         
1866         width = PICKER_TOTAL_W;
1867         butwidth = width - 1.5f * UI_UNIT_X;
1868         
1869         /* existence of profile means storage is in linear color space, with display correction */
1870         /* XXX That tip message is not use anywhere! */
1871         if (!block->color_profile) {
1872                 BLI_strncpy(tip, N_("Value in Display Color Space"), sizeof(tip));
1873                 copy_v3_v3(rgb_gamma, rgba);
1874         }
1875         else {
1876                 BLI_strncpy(tip, N_("Value in Linear RGB Color Space"), sizeof(tip));
1877
1878                 /* make a display version, for Hex code */
1879                 copy_v3_v3(rgb_gamma, rgba);
1880                 ui_block_to_display_space_v3(block, rgb_gamma);
1881         }
1882         
1883         /* sneaky way to check for alpha */
1884         rgba[3] = FLT_MAX;
1885
1886         RNA_property_float_ui_range(ptr, prop, &softmin, &softmax, &step, &precision);
1887         RNA_property_float_range(ptr, prop, &hardmin, &hardmax);
1888         RNA_property_float_get_array(ptr, prop, rgba);
1889
1890         switch (U.color_picker_type) {
1891                 case USER_CP_CIRCLE:
1892                         circle_picker(block, ptr, prop);
1893                         break;
1894                 case USER_CP_SQUARE_SV:
1895                         square_picker(block, ptr, prop, UI_GRAD_SV);
1896                         break;
1897                 case USER_CP_SQUARE_HS:
1898                         square_picker(block, ptr, prop, UI_GRAD_HS);
1899                         break;
1900                 case USER_CP_SQUARE_HV:
1901                         square_picker(block, ptr, prop, UI_GRAD_HV);
1902                         break;
1903         }
1904         
1905         /* mode */
1906         yco = -1.5f * UI_UNIT_Y;
1907         uiBlockBeginAlign(block);
1908         bt = uiDefButS(block, ROW, 0, IFACE_("RGB"), 0, yco, width / 3, UI_UNIT_Y, &colormode, 0.0, 0.0, 0, 0, "");
1909         uiButSetFunc(bt, do_picker_new_mode_cb, bt, NULL);
1910         bt = uiDefButS(block, ROW, 0, IFACE_("HSV"), width / 3, yco, width / 3, UI_UNIT_Y, &colormode, 0.0, 1.0, 0, 0, "");
1911         uiButSetFunc(bt, do_picker_new_mode_cb, bt, NULL);
1912         bt = uiDefButS(block, ROW, 0, IFACE_("Hex"), 2 * width / 3, yco, width / 3, UI_UNIT_Y, &colormode, 0.0, 2.0, 0, 0, "");
1913         uiButSetFunc(bt, do_picker_new_mode_cb, bt, NULL);
1914         uiBlockEndAlign(block);
1915
1916         yco = -3.0f * UI_UNIT_Y;
1917         if (show_picker) {
1918                 bt = uiDefIconButO(block, BUT, "UI_OT_eyedropper_color", WM_OP_INVOKE_DEFAULT, ICON_EYEDROPPER, butwidth + 10, yco, UI_UNIT_X, UI_UNIT_Y, NULL);
1919                 uiButSetFunc(bt, close_popup_cb, bt, NULL);
1920         }
1921         
1922         /* RGB values */
1923         uiBlockBeginAlign(block);
1924         bt = uiDefButR_prop(block, NUMSLI, 0, IFACE_("R:"),  0, yco, butwidth, UI_UNIT_Y, ptr, prop, 0, 0.0, 0.0, 0, 3, TIP_("Red"));
1925         uiButSetFunc(bt, do_picker_rna_cb, bt, NULL);
1926         bt = uiDefButR_prop(block, NUMSLI, 0, IFACE_("G:"),  0, yco -= UI_UNIT_Y, butwidth, UI_UNIT_Y, ptr, prop, 1, 0.0, 0.0, 0, 3, TIP_("Green"));
1927         uiButSetFunc(bt, do_picker_rna_cb, bt, NULL);
1928         bt = uiDefButR_prop(block, NUMSLI, 0, IFACE_("B:"),  0, yco -= UI_UNIT_Y, butwidth, UI_UNIT_Y, ptr, prop, 2, 0.0, 0.0, 0, 3, TIP_("Blue"));
1929         uiButSetFunc(bt, do_picker_rna_cb, bt, NULL);
1930
1931         /* could use uiItemFullR(col, ptr, prop, -1, 0, UI_ITEM_R_EXPAND|UI_ITEM_R_SLIDER, "", ICON_NONE);
1932          * but need to use uiButSetFunc for updating other fake buttons */
1933         
1934         /* HSV values */
1935         yco = -3.0f * UI_UNIT_Y;
1936         uiBlockBeginAlign(block);
1937         bt = uiDefButF(block, NUMSLI, 0, IFACE_("H:"),   0, yco, butwidth, UI_UNIT_Y, hsv, 0.0, 1.0, 10, 3, TIP_("Hue"));
1938         uiButSetFunc(bt, do_hsv_rna_cb, bt, hsv);
1939         bt = uiDefButF(block, NUMSLI, 0, IFACE_("S:"),   0, yco -= UI_UNIT_Y, butwidth, UI_UNIT_Y, hsv + 1, 0.0, 1.0, 10, 3, TIP_("Saturation"));
1940         uiButSetFunc(bt, do_hsv_rna_cb, bt, hsv);
1941         bt = uiDefButF(block, NUMSLI, 0, IFACE_("V:"),   0, yco -= UI_UNIT_Y, butwidth, UI_UNIT_Y, hsv + 2, 0.0, softmax, 10, 3, TIP_("Value"));
1942         bt->hardmax = hardmax;  /* not common but rgb  may be over 1.0 */
1943         uiButSetFunc(bt, do_hsv_rna_cb, bt, hsv);
1944         uiBlockEndAlign(block);
1945
1946         if (rgba[3] != FLT_MAX) {
1947                 bt = uiDefButR_prop(block, NUMSLI, 0, IFACE_("A "),  0, yco -= UI_UNIT_Y, butwidth, UI_UNIT_Y, ptr, prop, 3, 0.0, 0.0, 0, 3, TIP_("Alpha"));
1948                 uiButSetFunc(bt, do_picker_rna_cb, bt, NULL);
1949         }
1950         else {
1951                 rgba[3] = 1.0f;
1952         }
1953
1954         BLI_snprintf(hexcol, sizeof(hexcol), "%02X%02X%02X", FTOCHAR(rgb_gamma[0]), FTOCHAR(rgb_gamma[1]), FTOCHAR(rgb_gamma[2]));
1955
1956         yco = -3.0f * UI_UNIT_Y;
1957         bt = uiDefBut(block, TEX, 0, IFACE_("Hex: "), 0, yco, butwidth, UI_UNIT_Y, hexcol, 0, 8, 0, 0, TIP_("Hex triplet for color (#RRGGBB)"));
1958         uiButSetFunc(bt, do_hex_rna_cb, bt, hexcol);
1959         uiDefBut(block, LABEL, 0, IFACE_("(Gamma Corrected)"), 0, yco - UI_UNIT_Y, butwidth, UI_UNIT_Y, NULL, 0.0, 0.0, 0, 0, "");
1960
1961         rgb_to_hsv_v(rgba, hsv);
1962
1963         picker_new_hide_reveal(block, colormode);
1964 }
1965
1966
1967 static int ui_picker_small_wheel_cb(const bContext *UNUSED(C), uiBlock *block, const wmEvent *event)
1968 {
1969         float add = 0.0f;
1970         
1971         if (event->type == WHEELUPMOUSE)
1972                 add = 0.05f;
1973         else if (event->type == WHEELDOWNMOUSE)
1974                 add = -0.05f;
1975         
1976         if (add != 0.0f) {
1977                 uiBut *but;
1978                 
1979                 for (but = block->buttons.first; but; but = but->next) {
1980                         if (but->type == HSVCUBE && but->active == NULL) {
1981                                 uiPopupBlockHandle *popup = block->handle;
1982                                 float rgb[3];
1983                                 float *hsv = ui_block_hsv_get(block);
1984                                 
1985                                 ui_get_but_vectorf(but, rgb);
1986                                 
1987                                 rgb_to_hsv_compat_v(rgb, hsv);
1988                                 hsv[2] = CLAMPIS(hsv[2] + add, 0.0f, 1.0f);
1989                                 hsv_to_rgb_v(hsv, rgb);
1990
1991                                 ui_set_but_vectorf(but, rgb);
1992                                 
1993                                 ui_update_block_buts_rgb(block, rgb);
1994                                 if (popup)
1995                                         popup->menuretval = UI_RETURN_UPDATE;
1996                                 
1997                                 return 1;
1998                         }
1999                 }
2000         }
2001         return 0;
2002 }
2003
2004 uiBlock *ui_block_func_COLOR(bContext *C, uiPopupBlockHandle *handle, void *arg_but)
2005 {
2006         uiBut *but = arg_but;
2007         uiBlock *block;
2008         bool show_picker = true;
2009         
2010         block = uiBeginBlock(C, handle->region, __func__, UI_EMBOSS);
2011         
2012         if (RNA_property_subtype(but->rnaprop) == PROP_COLOR_GAMMA) {
2013                 block->color_profile = false;
2014         }
2015
2016         if (but->block) {
2017                 /* if color block is invoked from a popup we wouldn't be able to set color properly
2018                  * this is because color picker will close popups first and then will try to figure
2019                  * out active button RNA, and of course it'll fail
2020                  */
2021                 show_picker = (but->block->flag & UI_BLOCK_POPUP) == 0;
2022         }
2023         
2024         copy_v3_v3(handle->retvec, but->editvec);
2025         
2026         uiBlockPicker(block, handle->retvec, &but->rnapoin, but->rnaprop, show_picker);
2027         
2028         block->flag = UI_BLOCK_LOOP | UI_BLOCK_REDRAW | UI_BLOCK_KEEP_OPEN | UI_BLOCK_OUT_1 | UI_BLOCK_MOVEMOUSE_QUIT;
2029         uiBoundsBlock(block, 0.5 * UI_UNIT_X);
2030         
2031         block->block_event_func = ui_picker_small_wheel_cb;
2032         
2033         /* and lets go */
2034         block->direction = UI_TOP;
2035         
2036         return block;
2037 }
2038
2039 /************************ Popup Menu Memory ****************************/
2040
2041 static int ui_popup_string_hash(const char *str)
2042 {
2043         /* sometimes button contains hotkey, sometimes not, strip for proper compare */
2044         int hash;
2045         char *delimit = strchr(str, UI_SEP_CHAR);
2046
2047         if (delimit) *delimit = '\0';
2048         hash = BLI_ghashutil_strhash(str);
2049         if (delimit) *delimit = UI_SEP_CHAR;
2050
2051         return hash;
2052 }
2053
2054 static int ui_popup_menu_hash(const char *str)
2055 {
2056         return BLI_ghashutil_strhash(str);
2057 }
2058
2059 /* but == NULL read, otherwise set */
2060 uiBut *ui_popup_menu_memory(uiBlock *block, uiBut *but)
2061 {
2062         static int mem[256], first = 1;
2063         int hash = block->puphash;
2064         
2065         if (first) {
2066                 /* init */
2067                 memset(mem, -1, sizeof(mem));
2068                 first = 0;
2069         }
2070
2071         if (but) {
2072                 /* set */
2073                 mem[hash & 255] = ui_popup_string_hash(but->str);
2074                 return NULL;
2075         }
2076         else {
2077                 /* get */
2078                 for (but = block->buttons.first; but; but = but->next)
2079                         if (ui_popup_string_hash(but->str) == mem[hash & 255])
2080                                 return but;
2081
2082                 return NULL;
2083         }
2084 }
2085
2086 /******************** Popup Menu with callback or string **********************/
2087
2088 struct uiPopupMenu {
2089         uiBlock *block;
2090         uiLayout *layout;
2091         uiBut *but;
2092
2093         int mx, my;
2094         bool popup, slideout;
2095
2096         uiMenuCreateFunc menu_func;
2097         void *menu_arg;
2098 };
2099
2100 static uiBlock *ui_block_func_POPUP(bContext *C, uiPopupBlockHandle *handle, void *arg_pup)
2101 {
2102         uiBlock *block;
2103         uiBut *bt;
2104         uiPopupMenu *pup = arg_pup;
2105         int offset[2], direction, minwidth, width, height, flip;
2106
2107         if (pup->menu_func) {
2108                 pup->block->handle = handle;
2109                 pup->menu_func(C, pup->layout, pup->menu_arg);
2110                 pup->block->handle = NULL;
2111         }
2112
2113         if (pup->but) {
2114                 /* minimum width to enforece */
2115                 minwidth = BLI_rctf_size_x(&pup->but->rect);
2116
2117                 /* settings (typically rna-enum-popups) show above the button,
2118                  * menu's like file-menu, show below */
2119                 if (pup->but->type == PULLDOWN || (uiButGetMenuType(pup->but) != NULL)) {
2120                         direction = UI_DOWN;
2121                         flip = 1;
2122                 }
2123                 else {
2124                         direction = UI_TOP;
2125                         flip = 0;
2126                 }
2127         }
2128         else {
2129                 minwidth = 50;
2130                 direction = UI_DOWN;
2131                 flip = 1;
2132         }
2133
2134         block = pup->block;
2135         
2136         /* in some cases we create the block before the region,
2137          * so we set it delayed here if necessary */
2138         if (BLI_findindex(&handle->region->uiblocks, block) == -1)
2139                 uiBlockSetRegion(block, handle->region);
2140
2141         block->direction = direction;
2142
2143         uiBlockLayoutResolve(block, &width, &height);
2144
2145         uiBlockSetFlag(block, UI_BLOCK_MOVEMOUSE_QUIT);
2146         
2147         if (pup->popup) {
2148                 uiBlockSetFlag(block, UI_BLOCK_LOOP | UI_BLOCK_REDRAW | UI_BLOCK_NUMSELECT);
2149                 uiBlockSetDirection(block, direction);
2150
2151                 /* offset the mouse position, possibly based on earlier selection */
2152                 if ((block->flag & UI_BLOCK_POPUP_MEMORY) &&
2153                     (bt = ui_popup_menu_memory(block, NULL)))
2154                 {
2155                         /* position mouse on last clicked item, at 0.8*width of the
2156                          * button, so it doesn't overlap the text too much, also note
2157                          * the offset is negative because we are inverse moving the
2158                          * block to be under the mouse */
2159                         offset[0] = -(bt->rect.xmin + 0.8f * BLI_rctf_size_x(&bt->rect));
2160                         offset[1] = -(bt->rect.ymin + 0.5f * UI_UNIT_Y);
2161                 }
2162                 else {
2163                         /* position mouse at 0.8*width of the button and below the tile
2164                          * on the first item */
2165                         offset[0] = 0;
2166                         for (bt = block->buttons.first; bt; bt = bt->next)
2167                                 offset[0] = min_ii(offset[0], -(bt->rect.xmin + 0.8f * BLI_rctf_size_x(&bt->rect)));
2168
2169                         offset[1] = 2.1 * UI_UNIT_Y;
2170                 }
2171
2172                 block->minbounds = minwidth;
2173                 uiMenuPopupBoundsBlock(block, 1, offset[0], offset[1]);
2174         }
2175         else {
2176                 /* for a header menu we set the direction automatic */
2177                 if (!pup->slideout && flip) {
2178                         ScrArea *sa = CTX_wm_area(C);
2179                         if (sa && sa->headertype == HEADERDOWN) {
2180                                 ARegion *ar = CTX_wm_region(C);
2181                                 if (ar && ar->regiontype == RGN_TYPE_HEADER) {
2182                                         uiBlockSetDirection(block, UI_TOP);
2183                                         uiBlockFlipOrder(block);
2184                                 }
2185                         }
2186                 }
2187
2188                 block->minbounds = minwidth;
2189                 uiTextBoundsBlock(block, 3.0f * UI_UNIT_X);
2190         }
2191
2192         /* if menu slides out of other menu, override direction */
2193         if (pup->slideout)
2194                 uiBlockSetDirection(block, UI_RIGHT);
2195
2196         uiEndBlock(C, block);
2197
2198         return pup->block;
2199 }
2200
2201 uiPopupBlockHandle *ui_popup_menu_create(bContext *C, ARegion *butregion, uiBut *but,
2202                                          uiMenuCreateFunc menu_func, void *arg)
2203 {
2204         wmWindow *window = CTX_wm_window(C);
2205         uiStyle *style = UI_GetStyleDraw();
2206         uiPopupBlockHandle *handle;
2207         uiPopupMenu *pup;
2208
2209         pup = MEM_callocN(sizeof(uiPopupMenu), __func__);
2210         pup->block = uiBeginBlock(C, NULL, __func__, UI_EMBOSSP);
2211         pup->block->flag |= UI_BLOCK_NUMSELECT;  /* default menus to numselect */
2212         pup->layout = uiBlockLayout(pup->block, UI_LAYOUT_VERTICAL, UI_LAYOUT_MENU, 0, 0, 200, 0, MENU_PADDING, style);
2213         pup->slideout = but ? ui_block_is_menu(but->block) : false;
2214         pup->but = but;
2215         uiLayoutSetOperatorContext(pup->layout, WM_OP_INVOKE_REGION_WIN);
2216
2217         if (!but) {
2218                 /* no button to start from, means we are a popup */
2219                 pup->mx = window->eventstate->x;
2220                 pup->my = window->eventstate->y;
2221                 pup->popup = true;
2222                 pup->block->flag |= UI_BLOCK_NO_FLIP;
2223         }
2224         /* some enums reversing is strange, currently we have no good way to
2225          * reverse some enum's but not others, so reverse all so the first menu
2226          * items are always close to the mouse cursor */
2227         else {
2228 #if 0
2229                 /* if this is an rna button then we can assume its an enum
2230                  * flipping enums is generally not good since the order can be
2231                  * important [#28786] */
2232                 if (but->rnaprop && RNA_property_type(but->rnaprop) == PROP_ENUM) {
2233                         pup->block->flag |= UI_BLOCK_NO_FLIP;
2234                 }
2235 #endif
2236                 if (but->context)
2237                         uiLayoutContextCopy(pup->layout, but->context);
2238         }
2239
2240         /* menu is created from a callback */
2241         pup->menu_func = menu_func;
2242         pup->menu_arg = arg;
2243         
2244         handle = ui_popup_block_create(C, butregion, but, NULL, ui_block_func_POPUP, pup);
2245
2246         if (!but) {
2247                 handle->popup = true;
2248
2249                 UI_add_popup_handlers(C, &window->modalhandlers, handle);
2250                 WM_event_add_mousemove(C);
2251         }
2252         
2253         MEM_freeN(pup);
2254
2255         return handle;
2256 }
2257
2258 /******************** Popup Menu API with begin and end ***********************/
2259
2260 /* only return handler, and set optional title */
2261 uiPopupMenu *uiPupMenuBegin(bContext *C, const char *title, int icon)
2262 {
2263         uiStyle *style = UI_GetStyleDraw();
2264         uiPopupMenu *pup = MEM_callocN(sizeof(uiPopupMenu), "popup menu");
2265         uiBut *but;
2266
2267         pup->block = uiBeginBlock(C, NULL, __func__, UI_EMBOSSP);
2268         pup->block->flag |= UI_BLOCK_POPUP_MEMORY;
2269         pup->block->puphash = ui_popup_menu_hash(title);
2270         pup->layout = uiBlockLayout(pup->block, UI_LAYOUT_VERTICAL, UI_LAYOUT_MENU, 0, 0, 200, 0, MENU_PADDING, style);
2271
2272         /* note, this intentionally differs from the menu & submenu default because many operators
2273          * use popups like this to select one of their options - where having invoke doesn't make sense */
2274         uiLayoutSetOperatorContext(pup->layout, WM_OP_EXEC_REGION_WIN);
2275
2276         /* create in advance so we can let buttons point to retval already */
2277         pup->block->handle = MEM_callocN(sizeof(uiPopupBlockHandle), "uiPopupBlockHandle");
2278         
2279         /* create title button */
2280         if (title[0]) {
2281                 char titlestr[256];
2282                 
2283                 if (icon) {
2284                         BLI_snprintf(titlestr, sizeof(titlestr), " %s", title);
2285                         uiDefIconTextBut(pup->block, LABEL, 0, icon, titlestr, 0, 0, 200, UI_UNIT_Y, NULL, 0.0, 0.0, 0, 0, "");
2286                 }
2287                 else {
2288                         but = uiDefBut(pup->block, LABEL, 0, title, 0, 0, 200, UI_UNIT_Y, NULL, 0.0, 0.0, 0, 0, "");
2289                         but->drawflag = UI_BUT_TEXT_LEFT;
2290                 }
2291
2292                 uiItemS(pup->layout);
2293         }
2294
2295         return pup;
2296 }
2297
2298 /* set the whole structure to work */
2299 void uiPupMenuEnd(bContext *C, uiPopupMenu *pup)
2300 {
2301         wmWindow *window = CTX_wm_window(C);
2302         uiPopupBlockHandle *menu;
2303         
2304         pup->popup = true;
2305         pup->mx = window->eventstate->x;
2306         pup->my = window->eventstate->y;
2307         
2308         menu = ui_popup_block_create(C, NULL, NULL, NULL, ui_block_func_POPUP, pup);
2309         menu->popup = true;
2310         
2311         UI_add_popup_handlers(C, &window->modalhandlers, menu);
2312         WM_event_add_mousemove(C);
2313         
2314         MEM_freeN(pup);
2315 }
2316
2317 uiLayout *uiPupMenuLayout(uiPopupMenu *pup)
2318 {
2319         return pup->layout;
2320 }
2321
2322 /*************************** Standard Popup Menus ****************************/
2323
2324 void uiPupMenuReports(bContext *C, ReportList *reports)
2325 {
2326         Report *report;
2327
2328         uiPopupMenu *pup = NULL;
2329         uiLayout *layout;
2330
2331         if (!CTX_wm_window(C))
2332                 return;
2333
2334         for (report = reports->list.first; report; report = report->next) {
2335                 int icon;
2336                 const char *msg, *msg_next;
2337
2338                 if (report->type < reports->printlevel) {
2339                         continue;
2340                 }
2341
2342                 if (pup == NULL) {
2343                         char title[UI_MAX_DRAW_STR];
2344                         BLI_snprintf(title, sizeof(title), "%s: %s", IFACE_("Report"), report->typestr);
2345                         pup = uiPupMenuBegin(C, title, ICON_NONE);
2346                         layout = uiPupMenuLayout(pup);
2347                 }
2348                 else {
2349                         uiItemS(layout);
2350                 }
2351
2352                 /* split each newline into a label */
2353                 msg = report->message;
2354                 icon = uiIconFromReportType(report->type);
2355                 do {
2356                         char buf[UI_MAX_DRAW_STR];
2357                         msg_next = strchr(msg, '\n');
2358                         if (msg_next) {
2359                                 msg_next++;
2360                                 BLI_strncpy(buf, msg, MIN2(sizeof(buf), msg_next - msg));
2361                                 msg = buf;
2362                         }
2363                         uiItemL(layout, msg, icon);
2364                         icon = ICON_NONE;
2365                 } while ((msg = msg_next) && *msg);
2366         }
2367
2368         if (pup) {
2369                 uiPupMenuEnd(C, pup);
2370         }
2371 }
2372
2373 bool uiPupMenuInvoke(bContext *C, const char *idname, ReportList *reports)
2374 {
2375         uiPopupMenu *pup;
2376         uiLayout *layout;
2377         Menu menu;
2378         MenuType *mt = WM_menutype_find(idname, true);
2379
2380         if (mt == NULL) {
2381                 BKE_reportf(reports, RPT_ERROR, "menu \"%s\" not found\n", idname);
2382                 return false;
2383         }
2384
2385         if (mt->poll && mt->poll(C, mt) == 0)
2386                 return false;
2387
2388         pup = uiPupMenuBegin(C, IFACE_(mt->label), ICON_NONE);
2389         layout = uiPupMenuLayout(pup);
2390
2391         menu.layout = layout;
2392         menu.type = mt;
2393
2394         if (G.debug & G_DEBUG_WM) {
2395                 printf("%s: opening menu \"%s\"\n", __func__, idname);
2396         }
2397
2398         mt->draw(C, &menu);
2399
2400         uiPupMenuEnd(C, pup);
2401
2402         return true;
2403 }
2404
2405
2406 /*************************** Popup Block API **************************/
2407
2408 void uiPupBlockO(bContext *C, uiBlockCreateFunc func, void *arg, const char *opname, int opcontext)
2409 {
2410         wmWindow *window = CTX_wm_window(C);
2411         uiPopupBlockHandle *handle;
2412         
2413         handle = ui_popup_block_create(C, NULL, NULL, func, NULL, arg);
2414         handle->popup = true;
2415         handle->optype = (opname) ? WM_operatortype_find(opname, 0) : NULL;
2416         handle->opcontext = opcontext;
2417         
2418         UI_add_popup_handlers(C, &window->modalhandlers, handle);
2419         WM_event_add_mousemove(C);
2420 }
2421
2422 void uiPupBlock(bContext *C, uiBlockCreateFunc func, void *arg)
2423 {
2424         uiPupBlockO(C, func, arg, NULL, 0);
2425 }
2426
2427 void uiPupBlockEx(bContext *C, uiBlockCreateFunc func, uiBlockHandleFunc popup_func, uiBlockCancelFunc cancel_func, void *arg)
2428 {
2429         wmWindow *window = CTX_wm_window(C);
2430         uiPopupBlockHandle *handle;
2431         
2432         handle = ui_popup_block_create(C, NULL, NULL, func, NULL, arg);
2433         handle->popup = true;
2434         handle->retvalue = 1;
2435
2436         handle->popup_arg = arg;
2437         handle->popup_func = popup_func;
2438         handle->cancel_func = cancel_func;
2439         // handle->opcontext = opcontext;
2440         
2441         UI_add_popup_handlers(C, &window->modalhandlers, handle);
2442         WM_event_add_mousemove(C);
2443 }
2444
2445 #if 0 /* UNUSED */
2446 void uiPupBlockOperator(bContext *C, uiBlockCreateFunc func, wmOperator *op, int opcontext)
2447 {
2448         wmWindow *window = CTX_wm_window(C);
2449         uiPopupBlockHandle *handle;
2450         
2451         handle = ui_popup_block_create(C, NULL, NULL, func, NULL, op);
2452         handle->popup = 1;
2453         handle->retvalue = 1;
2454
2455         handle->popup_arg = op;
2456         handle->popup_func = operator_cb;
2457         handle->cancel_func = confirm_cancel_operator;
2458         handle->opcontext = opcontext;
2459         
2460         UI_add_popup_handlers(C, &window->modalhandlers, handle);
2461         WM_event_add_mousemove(C);
2462 }
2463 #endif
2464
2465 void uiPupBlockClose(bContext *C, uiBlock *block)
2466 {
2467         if (block->handle) {
2468                 wmWindow *win = CTX_wm_window(C);
2469
2470                 /* if loading new .blend while popup is open, window will be NULL */
2471                 if (win) {
2472                         UI_remove_popup_handlers(&win->modalhandlers, block->handle);
2473                         ui_popup_block_free(C, block->handle);
2474                 }
2475         }
2476 }
2477
2478 float *ui_block_hsv_get(uiBlock *block)
2479 {
2480         return block->_hsv;
2481 }