Fix empty struct errors in paint_vertex_mode.c
[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 #include <stdarg.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <assert.h>
34
35 #include "MEM_guardedalloc.h"
36
37 #include "DNA_userdef_types.h"
38
39 #include "BLI_math.h"
40 #include "BLI_blenlib.h"
41 #include "BLI_utildefines.h"
42 #include "BLI_ghash.h"
43
44 #include "PIL_time.h"
45
46 #include "BKE_context.h"
47 #include "BKE_screen.h"
48 #include "BKE_report.h"
49 #include "BKE_global.h"
50
51 #include "WM_api.h"
52 #include "WM_types.h"
53 #include "wm_draw.h"
54 #include "wm_subwindow.h"
55
56 #include "RNA_access.h"
57
58 #include "BIF_gl.h"
59
60 #include "UI_interface.h"
61 #include "UI_interface_icons.h"
62 #include "UI_view2d.h"
63
64 #include "BLF_api.h"
65 #include "BLT_translation.h"
66
67 #include "ED_screen.h"
68
69 #include "IMB_colormanagement.h"
70
71 #include "interface_intern.h"
72
73 #define MENU_PADDING            (int)(0.2f * UI_UNIT_Y)
74 #define MENU_BORDER                     (int)(0.3f * U.widget_unit)
75
76
77 bool ui_but_menu_step_poll(const uiBut *but)
78 {
79         BLI_assert(but->type == UI_BTYPE_MENU);
80
81         /* currently only RNA buttons */
82         return ((but->menu_step_func != NULL) ||
83                 (but->rnaprop && RNA_property_type(but->rnaprop) == PROP_ENUM));
84 }
85
86 int ui_but_menu_step(uiBut *but, int direction)
87 {
88         if (ui_but_menu_step_poll(but)) {
89                 if (but->menu_step_func) {
90                         return but->menu_step_func(but->block->evil_C, direction, but->poin);
91                 }
92                 else {
93                         const int curval = RNA_property_enum_get(&but->rnapoin, but->rnaprop);
94                         return RNA_property_enum_step(but->block->evil_C, &but->rnapoin, but->rnaprop, curval, direction);
95                 }
96         }
97
98         printf("%s: cannot cycle button '%s'\n", __func__, but->str);
99         return 0;
100 }
101
102 /******************** Creating Temporary regions ******************/
103
104 static ARegion *ui_region_temp_add(bScreen *sc)
105 {
106         ARegion *ar;
107
108         ar = MEM_callocN(sizeof(ARegion), "area region");
109         BLI_addtail(&sc->regionbase, ar);
110
111         ar->regiontype = RGN_TYPE_TEMPORARY;
112         ar->alignment = RGN_ALIGN_FLOAT;
113
114         return ar;
115 }
116
117 static void ui_region_temp_remove(bContext *C, bScreen *sc, ARegion *ar)
118 {
119         wmWindow *win = CTX_wm_window(C);
120         if (win)
121                 wm_draw_region_clear(win, ar);
122
123         ED_region_exit(C, ar);
124         BKE_area_region_free(NULL, ar);     /* NULL: no spacetype */
125         BLI_freelinkN(&sc->regionbase, ar);
126 }
127
128 /************************* Creating Tooltips **********************/
129
130 #define UI_TIP_PAD_FAC      1.3f
131 #define UI_TIP_PADDING      (int)(UI_TIP_PAD_FAC * UI_UNIT_Y)
132 #define UI_TIP_MAXWIDTH     600
133
134 #define MAX_TOOLTIP_LINES 8
135 typedef struct uiTooltipData {
136         rcti bbox;
137         uiFontStyle fstyle;
138         char lines[MAX_TOOLTIP_LINES][2048];
139         char header[2048], active_info[2048];
140         struct {
141                 enum {
142                         UI_TIP_STYLE_NORMAL = 0,
143                         UI_TIP_STYLE_HEADER,
144                         UI_TIP_STYLE_MONO,
145                 } style : 3;
146                 enum {
147                         UI_TIP_LC_MAIN = 0,     /* primary text */
148                         UI_TIP_LC_VALUE,        /* the value of buttons (also shortcuts) */
149                         UI_TIP_LC_ACTIVE,       /* titles of active enum values */
150                         UI_TIP_LC_NORMAL,       /* regular text */
151                         UI_TIP_LC_PYTHON,       /* Python snippet */
152                         UI_TIP_LC_ALERT,        /* description of why operator can't run */
153                 } color_id : 4;
154                 int is_pad : 1;
155         } format[MAX_TOOLTIP_LINES];
156
157         struct {
158                 unsigned int x_pos;     /* x cursor position at the end of the last line */
159                 unsigned int lines;     /* number of lines, 1 or more with word-wrap */
160         } line_geom[MAX_TOOLTIP_LINES];
161
162         int wrap_width;
163
164         int totline;
165         int toth, lineh;
166 } uiTooltipData;
167
168 #define UI_TIP_LC_MAX 6
169
170 BLI_STATIC_ASSERT(UI_TIP_LC_MAX == UI_TIP_LC_ALERT + 1, "invalid lc-max");
171 BLI_STATIC_ASSERT(sizeof(((uiTooltipData *)NULL)->format[0]) <= sizeof(int), "oversize");
172
173 static void rgb_tint(
174         float col[3],
175         float h, float h_strength,
176         float v, float v_strength)
177 {
178         float col_hsv_from[3];
179         float col_hsv_to[3];
180
181         rgb_to_hsv_v(col, col_hsv_from);
182
183         col_hsv_to[0] = h;
184         col_hsv_to[1] = h_strength;
185         col_hsv_to[2] = (col_hsv_from[2] * (1.0f - v_strength)) + (v * v_strength);
186
187         hsv_to_rgb_v(col_hsv_to, col);
188 }
189
190 static void ui_tooltip_region_draw_cb(const bContext *UNUSED(C), ARegion *ar)
191 {
192         const float pad_px = UI_TIP_PADDING;
193         uiTooltipData *data = ar->regiondata;
194         uiWidgetColors *theme = ui_tooltip_get_theme();
195         rcti bbox = data->bbox;
196         float tip_colors[UI_TIP_LC_MAX][3];
197         unsigned char drawcol[4] = {0, 0, 0, 255}; /* to store color in while drawing (alpha is always 255) */
198
199         float *main_color    = tip_colors[UI_TIP_LC_MAIN]; /* the color from the theme */
200         float *value_color   = tip_colors[UI_TIP_LC_VALUE];
201         float *active_color  = tip_colors[UI_TIP_LC_ACTIVE];
202         float *normal_color  = tip_colors[UI_TIP_LC_NORMAL];
203         float *python_color  = tip_colors[UI_TIP_LC_PYTHON];
204         float *alert_color   = tip_colors[UI_TIP_LC_ALERT];
205
206         float background_color[3];
207         float tone_bg;
208         int i, multisample_enabled;
209
210         /* disable AA, makes widgets too blurry */
211         multisample_enabled = glIsEnabled(GL_MULTISAMPLE);
212         if (multisample_enabled)
213                 glDisable(GL_MULTISAMPLE);
214
215         wmOrtho2_region_pixelspace(ar);
216
217         /* draw background */
218         ui_draw_tooltip_background(UI_style_get(), NULL, &bbox);
219
220         /* set background_color */
221         rgb_uchar_to_float(background_color, (const unsigned char *)theme->inner);
222
223         /* calculate normal_color */
224         rgb_uchar_to_float(main_color, (const unsigned char *)theme->text);
225         copy_v3_v3(active_color, main_color);
226         copy_v3_v3(normal_color, main_color);
227         copy_v3_v3(python_color, main_color);
228         copy_v3_v3(alert_color, main_color);
229         copy_v3_v3(value_color, main_color);
230
231         /* find the brightness difference between background and text colors */
232         
233         tone_bg = rgb_to_grayscale(background_color);
234         /* tone_fg = rgb_to_grayscale(main_color); */
235
236         /* mix the colors */
237         rgb_tint(value_color,  0.0f, 0.0f, tone_bg, 0.2f);  /* light gray */
238         rgb_tint(active_color, 0.6f, 0.2f, tone_bg, 0.2f);  /* light blue */
239         rgb_tint(normal_color, 0.0f, 0.0f, tone_bg, 0.4f);  /* gray       */
240         rgb_tint(python_color, 0.0f, 0.0f, tone_bg, 0.5f);  /* dark gray  */
241         rgb_tint(alert_color,  0.0f, 0.8f, tone_bg, 0.1f);  /* red        */
242
243         /* draw text */
244         BLF_wordwrap(data->fstyle.uifont_id, data->wrap_width);
245         BLF_wordwrap(blf_mono_font, data->wrap_width);
246
247         bbox.xmin += 0.5f * pad_px;  /* add padding to the text */
248         bbox.ymax -= 0.25f * pad_px;
249
250         for (i = 0; i < data->totline; i++) {
251                 bbox.ymin = bbox.ymax - (data->lineh * data->line_geom[i].lines);
252                 if (data->format[i].style == UI_TIP_STYLE_HEADER) {
253                         /* draw header and active data (is done here to be able to change color) */
254                         uiFontStyle fstyle_header = data->fstyle;
255                         float xofs, yofs;
256
257                         /* override text-style */
258                         fstyle_header.shadow = 1;
259                         fstyle_header.shadowcolor = rgb_to_grayscale(tip_colors[UI_TIP_LC_MAIN]);
260                         fstyle_header.shadx = fstyle_header.shady = 0;
261                         fstyle_header.shadowalpha = 1.0f;
262                         fstyle_header.word_wrap = true;
263
264                         rgb_float_to_uchar(drawcol, tip_colors[UI_TIP_LC_MAIN]);
265                         UI_fontstyle_set(&fstyle_header);
266                         UI_fontstyle_draw(&fstyle_header, &bbox, data->header, drawcol);
267
268                         /* offset to the end of the last line */
269                         xofs = data->line_geom[i].x_pos;
270                         yofs = data->lineh * (data->line_geom[i].lines - 1);
271                         bbox.xmin += xofs;
272                         bbox.ymax -= yofs;
273
274                         rgb_float_to_uchar(drawcol, tip_colors[UI_TIP_LC_ACTIVE]);
275                         fstyle_header.shadow = 0;
276                         UI_fontstyle_draw(&fstyle_header, &bbox, data->active_info, drawcol);
277
278                         /* undo offset */
279                         bbox.xmin -= xofs;
280                         bbox.ymax += yofs;
281                 }
282                 else if (data->format[i].style == UI_TIP_STYLE_MONO) {
283                         uiFontStyle fstyle_mono = data->fstyle;
284
285                         fstyle_mono.uifont_id = blf_mono_font;
286                         fstyle_mono.word_wrap = true;
287
288                         UI_fontstyle_set(&fstyle_mono);
289                         /* XXX, needed because we dont have mono in 'U.uifonts' */
290                         BLF_size(fstyle_mono.uifont_id, fstyle_mono.points * U.pixelsize, U.dpi);
291                         rgb_float_to_uchar(drawcol, tip_colors[data->format[i].color_id]);
292                         UI_fontstyle_draw(&fstyle_mono, &bbox, data->lines[i], drawcol);
293                 }
294                 else {
295                         uiFontStyle fstyle_normal = data->fstyle;
296                         BLI_assert(data->format[i].style == UI_TIP_STYLE_NORMAL);
297                         fstyle_normal.word_wrap = true;
298
299                         /* draw remaining data */
300                         UI_fontstyle_set(&fstyle_normal);
301                         rgb_float_to_uchar(drawcol, tip_colors[data->format[i].color_id]);
302                         UI_fontstyle_draw(&fstyle_normal, &bbox, data->lines[i], drawcol);
303                 }
304
305                 bbox.ymax -= data->lineh * data->line_geom[i].lines;
306
307                 if ((i + 1 != data->totline) && data->format[i + 1].is_pad) {
308                         bbox.ymax -= data->lineh * (UI_TIP_PAD_FAC - 1);
309                 }
310         }
311
312         BLF_disable(data->fstyle.uifont_id, BLF_WORD_WRAP);
313         BLF_disable(blf_mono_font, BLF_WORD_WRAP);
314
315         if (multisample_enabled)
316                 glEnable(GL_MULTISAMPLE);
317 }
318
319 static void ui_tooltip_region_free_cb(ARegion *ar)
320 {
321         uiTooltipData *data;
322
323         data = ar->regiondata;
324         MEM_freeN(data);
325         ar->regiondata = NULL;
326 }
327
328 static uiTooltipData *ui_tooltip_data_from_button(bContext *C, uiBut *but)
329 {
330         uiStringInfo but_tip = {BUT_GET_TIP, NULL};
331         uiStringInfo enum_label = {BUT_GET_RNAENUM_LABEL, NULL};
332         uiStringInfo enum_tip = {BUT_GET_RNAENUM_TIP, NULL};
333         uiStringInfo op_keymap = {BUT_GET_OP_KEYMAP, NULL};
334         uiStringInfo prop_keymap = {BUT_GET_PROP_KEYMAP, NULL};
335         uiStringInfo rna_struct = {BUT_GET_RNASTRUCT_IDENTIFIER, NULL};
336         uiStringInfo rna_prop = {BUT_GET_RNAPROP_IDENTIFIER, NULL};
337
338         char buf[512];
339
340         /* create tooltip data */
341         uiTooltipData *data = MEM_callocN(sizeof(uiTooltipData), "uiTooltipData");
342
343         UI_but_string_info_get(C, but, &but_tip, &enum_label, &enum_tip, &op_keymap, &prop_keymap, &rna_struct, &rna_prop, NULL);
344
345         /* Tip */
346         if (but_tip.strinfo) {
347                 BLI_strncpy(data->header, but_tip.strinfo, sizeof(data->lines[0]));
348                 if (enum_label.strinfo) {
349                         BLI_snprintf(data->header, sizeof(data->header), "%s:  ", but_tip.strinfo);
350                         BLI_strncpy(data->active_info, enum_label.strinfo, sizeof(data->lines[0]));
351                 }
352                 data->format[data->totline].style = UI_TIP_STYLE_HEADER;
353                 data->totline++;
354
355                 /* special case enum rna buttons */
356                 if ((but->type & UI_BTYPE_ROW) && but->rnaprop && RNA_property_flag(but->rnaprop) & PROP_ENUM_FLAG) {
357                         BLI_strncpy(data->lines[data->totline], IFACE_("(Shift-Click/Drag to select multiple)"),
358                                     sizeof(data->lines[0]));
359
360                         data->format[data->totline].color_id = UI_TIP_LC_NORMAL;
361                         data->totline++;
362                 }
363
364         }
365         /* Enum item label & tip */
366         if (enum_tip.strinfo) {
367                 BLI_strncpy(data->lines[data->totline], enum_tip.strinfo, sizeof(data->lines[0]));
368                 data->format[data->totline].is_pad = true;
369                 data->format[data->totline].color_id = UI_TIP_LC_VALUE;
370                 data->totline++;
371         }
372
373         /* Op shortcut */
374         if (op_keymap.strinfo) {
375                 BLI_snprintf(data->lines[data->totline], sizeof(data->lines[0]), TIP_("Shortcut: %s"), op_keymap.strinfo);
376                 data->format[data->totline].is_pad = true;
377                 data->format[data->totline].color_id = UI_TIP_LC_VALUE;
378                 data->totline++;
379         }
380         
381         /* Property context-toggle shortcut */
382         if (prop_keymap.strinfo) {
383                 BLI_snprintf(data->lines[data->totline], sizeof(data->lines[0]), TIP_("Shortcut: %s"), prop_keymap.strinfo);
384                 data->format[data->totline].is_pad = true;
385                 data->format[data->totline].color_id = UI_TIP_LC_VALUE;
386                 data->totline++;
387         }
388
389         if (ELEM(but->type, UI_BTYPE_TEXT, UI_BTYPE_SEARCH_MENU)) {
390                 /* better not show the value of a password */
391                 if ((but->rnaprop && (RNA_property_subtype(but->rnaprop) == PROP_PASSWORD)) == 0) {
392                         /* full string */
393                         ui_but_string_get(but, buf, sizeof(buf));
394                         if (buf[0]) {
395                                 BLI_snprintf(data->lines[data->totline], sizeof(data->lines[0]), TIP_("Value: %s"), buf);
396                                 data->format[data->totline].is_pad = true;
397                                 data->format[data->totline].color_id = UI_TIP_LC_VALUE;
398                                 data->totline++;
399                         }
400                 }
401         }
402
403         if (but->rnaprop) {
404                 int unit_type = UI_but_unit_type_get(but);
405                 
406                 if (unit_type == PROP_UNIT_ROTATION) {
407                         if (RNA_property_type(but->rnaprop) == PROP_FLOAT) {
408                                 float value = RNA_property_array_check(but->rnaprop) ?
409                                                   RNA_property_float_get_index(&but->rnapoin, but->rnaprop, but->rnaindex) :
410                                                   RNA_property_float_get(&but->rnapoin, but->rnaprop);
411                                 BLI_snprintf(data->lines[data->totline], sizeof(data->lines[0]), TIP_("Radians: %f"), value);
412                                 data->format[data->totline].color_id = UI_TIP_LC_NORMAL;
413                                 data->totline++;
414                         }
415                 }
416                 
417                 if (but->flag & UI_BUT_DRIVEN) {
418                         if (ui_but_anim_expression_get(but, buf, sizeof(buf))) {
419                                 /* expression */
420                                 BLI_snprintf(data->lines[data->totline], sizeof(data->lines[0]), TIP_("Expression: %s"), buf);
421                                 data->format[data->totline].color_id = UI_TIP_LC_NORMAL;
422                                 data->totline++;
423                         }
424                 }
425
426                 if (but->rnapoin.id.data) {
427                         ID *id = but->rnapoin.id.data;
428                         if (ID_IS_LINKED_DATABLOCK(id)) {
429                                 BLI_snprintf(data->lines[data->totline], sizeof(data->lines[0]), TIP_("Library: %s"), id->lib->name);
430                                 data->format[data->totline].color_id = UI_TIP_LC_NORMAL;
431                                 data->totline++;
432                         }
433                 }
434         }
435         else if (but->optype) {
436                 PointerRNA *opptr;
437                 char *str;
438                 opptr = UI_but_operator_ptr_get(but); /* allocated when needed, the button owns it */
439
440                 /* so the context is passed to itemf functions (some py itemf functions use it) */
441                 WM_operator_properties_sanitize(opptr, false);
442
443                 str = WM_operator_pystring_ex(C, NULL, false, false, but->optype, opptr);
444
445                 /* avoid overly verbose tips (eg, arrays of 20 layers), exact limit is arbitrary */
446                 WM_operator_pystring_abbreviate(str, 32);
447
448                 /* operator info */
449                 if ((U.flag & USER_TOOLTIPS_PYTHON) == 0) {
450                         BLI_snprintf(data->lines[data->totline], sizeof(data->lines[0]), TIP_("Python: %s"), str);
451                         data->format[data->totline].style = UI_TIP_STYLE_MONO;
452                         data->format[data->totline].is_pad = true;
453                         data->format[data->totline].color_id = UI_TIP_LC_PYTHON;
454                         data->totline++;
455                 }
456
457                 MEM_freeN(str);
458         }
459
460         /* button is disabled, we may be able to tell user why */
461         if (but->flag & UI_BUT_DISABLED) {
462                 const char *disabled_msg = NULL;
463
464                 /* if operator poll check failed, it can give pretty precise info why */
465                 if (but->optype) {
466                         CTX_wm_operator_poll_msg_set(C, NULL);
467                         WM_operator_poll_context(C, but->optype, but->opcontext);
468                         disabled_msg = CTX_wm_operator_poll_msg_get(C);
469                 }
470                 /* alternatively, buttons can store some reasoning too */
471                 else if (but->disabled_info) {
472                         disabled_msg = TIP_(but->disabled_info);
473                 }
474
475                 if (disabled_msg && disabled_msg[0]) {
476                         BLI_snprintf(data->lines[data->totline], sizeof(data->lines[0]), TIP_("Disabled: %s"), disabled_msg);
477                         data->format[data->totline].color_id = UI_TIP_LC_ALERT;
478                         data->totline++;
479                 }
480         }
481
482         if ((U.flag & USER_TOOLTIPS_PYTHON) == 0 && !but->optype && rna_struct.strinfo) {
483                 if (rna_prop.strinfo) {
484                         /* Struct and prop */
485                         BLI_snprintf(data->lines[data->totline], sizeof(data->lines[0]),
486                                      TIP_("Python: %s.%s"),
487                                      rna_struct.strinfo, rna_prop.strinfo);
488                 }
489                 else {
490                         /* Only struct (e.g. menus) */
491                         BLI_snprintf(data->lines[data->totline], sizeof(data->lines[0]),
492                                      TIP_("Python: %s"), rna_struct.strinfo);
493                 }
494                 data->format[data->totline].style = UI_TIP_STYLE_MONO;
495                 data->format[data->totline].is_pad = true;
496                 data->format[data->totline].color_id = UI_TIP_LC_PYTHON;
497                 data->totline++;
498
499                 if (but->rnapoin.id.data) {
500                         /* this could get its own 'BUT_GET_...' type */
501
502                         /* never fails */
503                         char *id_path;
504
505                         if (but->rnaprop) {
506                                 id_path = RNA_path_full_property_py_ex(&but->rnapoin, but->rnaprop, but->rnaindex, true);
507                         }
508                         else {
509                                 id_path = RNA_path_full_struct_py(&but->rnapoin);
510                         }
511
512                         BLI_strncat_utf8(data->lines[data->totline], id_path, sizeof(data->lines[0]));
513                         MEM_freeN(id_path);
514
515                         data->format[data->totline].style = UI_TIP_STYLE_MONO;
516                         data->format[data->totline].color_id = UI_TIP_LC_PYTHON;
517                         data->totline++;
518                 }
519         }
520
521         /* Free strinfo's... */
522         if (but_tip.strinfo)
523                 MEM_freeN(but_tip.strinfo);
524         if (enum_label.strinfo)
525                 MEM_freeN(enum_label.strinfo);
526         if (enum_tip.strinfo)
527                 MEM_freeN(enum_tip.strinfo);
528         if (op_keymap.strinfo)
529                 MEM_freeN(op_keymap.strinfo);
530         if (prop_keymap.strinfo)
531                 MEM_freeN(prop_keymap.strinfo);
532         if (rna_struct.strinfo)
533                 MEM_freeN(rna_struct.strinfo);
534         if (rna_prop.strinfo)
535                 MEM_freeN(rna_prop.strinfo);
536
537         BLI_assert(data->totline < MAX_TOOLTIP_LINES);
538         
539         if (data->totline == 0) {
540                 MEM_freeN(data);
541                 return NULL;
542         }
543         else {
544                 return data;
545         }
546 }
547
548 ARegion *ui_tooltip_create(bContext *C, ARegion *butregion, uiBut *but)
549 {
550         const float pad_px = UI_TIP_PADDING;
551         wmWindow *win = CTX_wm_window(C);
552         const int winx = WM_window_pixels_x(win);
553         uiStyle *style = UI_style_get();
554         static ARegionType type;
555         ARegion *ar;
556 /*      IDProperty *prop;*/
557         /* aspect values that shrink text are likely unreadable */
558         const float aspect = min_ff(1.0f, but->block->aspect);
559         int fonth, fontw;
560         int ofsx, ofsy, h, i;
561         rctf rect_fl;
562         rcti rect_i;
563         int font_flag = 0;
564
565         if (but->drawflag & UI_BUT_NO_TOOLTIP) {
566                 return NULL;
567         }
568
569         uiTooltipData *data = ui_tooltip_data_from_button(C, but);
570         if (data == NULL) {
571                 return NULL;
572         }
573
574         /* create area region */
575         ar = ui_region_temp_add(CTX_wm_screen(C));
576
577         memset(&type, 0, sizeof(ARegionType));
578         type.draw = ui_tooltip_region_draw_cb;
579         type.free = ui_tooltip_region_free_cb;
580         type.regionid = RGN_TYPE_TEMPORARY;
581         ar->type = &type;
582         
583         /* set font, get bb */
584         data->fstyle = style->widget; /* copy struct */
585         ui_fontscale(&data->fstyle.points, aspect);
586
587         UI_fontstyle_set(&data->fstyle);
588
589         data->wrap_width = min_ii(UI_TIP_MAXWIDTH * U.pixelsize / aspect, winx - (UI_TIP_PADDING * 2));
590
591         font_flag |= BLF_WORD_WRAP;
592         if (data->fstyle.kerning == 1) {
593                 font_flag |= BLF_KERNING_DEFAULT;
594         }
595         BLF_enable(data->fstyle.uifont_id, font_flag);
596         BLF_enable(blf_mono_font, font_flag);
597         BLF_wordwrap(data->fstyle.uifont_id, data->wrap_width);
598         BLF_wordwrap(blf_mono_font, data->wrap_width);
599
600         /* these defines tweaked depending on font */
601 #define TIP_BORDER_X (16.0f / aspect)
602 #define TIP_BORDER_Y (6.0f / aspect)
603
604         h = BLF_height_max(data->fstyle.uifont_id);
605
606         for (i = 0, fontw = 0, fonth = 0; i < data->totline; i++) {
607                 struct ResultBLF info;
608                 int w, x_pos = 0;
609
610                 if (data->format[i].style == UI_TIP_STYLE_HEADER) {
611                         w = BLF_width_ex(data->fstyle.uifont_id, data->header, sizeof(data->header), &info);
612                         /* check for enum label */
613                         if (data->active_info[0]) {
614                                 x_pos = info.width;
615                                 w = max_ii(w, x_pos + BLF_width(data->fstyle.uifont_id, data->active_info, sizeof(data->active_info)));
616                         }
617                 }
618                 else if (data->format[i].style == UI_TIP_STYLE_MONO) {
619                         BLF_size(blf_mono_font, data->fstyle.points * U.pixelsize, U.dpi);
620
621                         w = BLF_width_ex(blf_mono_font, data->lines[i], sizeof(data->lines[i]), &info);
622                 }
623                 else {
624                         BLI_assert(data->format[i].style == UI_TIP_STYLE_NORMAL);
625
626                         w = BLF_width_ex(data->fstyle.uifont_id, data->lines[i], sizeof(data->lines[i]), &info);
627                 }
628
629                 fontw = max_ii(fontw, w);
630
631                 fonth += h * info.lines;
632                 if ((i + 1 != data->totline) && data->format[i + 1].is_pad) {
633                         fonth += h * (UI_TIP_PAD_FAC - 1);
634                 }
635
636                 data->line_geom[i].lines = info.lines;
637                 data->line_geom[i].x_pos = x_pos;
638         }
639
640         //fontw *= aspect;
641
642         BLF_disable(data->fstyle.uifont_id, font_flag);
643         BLF_disable(blf_mono_font, font_flag);
644
645         ar->regiondata = data;
646
647         data->toth = fonth;
648         data->lineh = h;
649
650         /* compute position */
651         ofsx = 0; //(but->block->panel) ? but->block->panel->ofsx : 0;
652         ofsy = 0; //(but->block->panel) ? but->block->panel->ofsy : 0;
653
654         rect_fl.xmin = BLI_rctf_cent_x(&but->rect) + ofsx - TIP_BORDER_X;
655         rect_fl.xmax = rect_fl.xmin + fontw + pad_px;
656         rect_fl.ymax = but->rect.ymin + ofsy - TIP_BORDER_Y;
657         rect_fl.ymin = rect_fl.ymax - fonth  - TIP_BORDER_Y;
658         
659         /* since the text has beens caled already, the size of tooltips is defined now */
660         /* here we try to figure out the right location */
661         if (butregion) {
662                 float mx, my;
663                 float ofsx_fl = rect_fl.xmin, ofsy_fl = rect_fl.ymax;
664                 ui_block_to_window_fl(butregion, but->block, &ofsx_fl, &ofsy_fl);
665
666 #if 1
667                 /* use X mouse location */
668                 mx = (win->eventstate->x + (TIP_BORDER_X * 2)) - BLI_rctf_cent_x(&but->rect);
669 #else
670                 mx = ofsx_fl - rect_fl.xmin;
671 #endif
672                 my = ofsy_fl - rect_fl.ymax;
673
674                 BLI_rctf_translate(&rect_fl, mx, my);
675         }
676         BLI_rcti_rctf_copy(&rect_i, &rect_fl);
677
678 #undef TIP_BORDER_X
679 #undef TIP_BORDER_Y
680
681         /* clip with window boundaries */
682         if (rect_i.xmax > winx) {
683                 /* super size */
684                 if (rect_i.xmax > winx + rect_i.xmin) {
685                         rect_i.xmax = winx;
686                         rect_i.xmin = 0;
687                 }
688                 else {
689                         rect_i.xmin -= rect_i.xmax - winx;
690                         rect_i.xmax = winx;
691                 }
692         }
693         /* ensure at least 5 px above screen bounds
694          * 25 is just a guess to be above the menu item */
695         if (rect_i.ymin < 5) {
696                 rect_i.ymax += (-rect_i.ymin) + 30;
697                 rect_i.ymin = 30;
698         }
699
700         /* add padding */
701         BLI_rcti_resize(&rect_i,
702                         BLI_rcti_size_x(&rect_i) + pad_px,
703                         BLI_rcti_size_y(&rect_i) + pad_px);
704
705         /* widget rect, in region coords */
706         {
707                 const int margin = UI_POPUP_MARGIN;
708                 
709                 data->bbox.xmin = margin;
710                 data->bbox.xmax = BLI_rcti_size_x(&rect_i) - margin;
711                 data->bbox.ymin = margin;
712                 data->bbox.ymax = BLI_rcti_size_y(&rect_i);
713                 
714                 /* region bigger for shadow */
715                 ar->winrct.xmin = rect_i.xmin - margin;
716                 ar->winrct.xmax = rect_i.xmax + margin;
717                 ar->winrct.ymin = rect_i.ymin - margin;
718                 ar->winrct.ymax = rect_i.ymax + margin;
719         }
720
721         /* adds subwindow */
722         ED_region_init(C, ar);
723         
724         /* notify change and redraw */
725         ED_region_tag_redraw(ar);
726
727         return ar;
728 }
729
730 void ui_tooltip_free(bContext *C, ARegion *ar)
731 {
732         ui_region_temp_remove(C, CTX_wm_screen(C), ar);
733 }
734
735
736 /************************* Creating Search Box **********************/
737
738 struct uiSearchItems {
739         int maxitem, totitem, maxstrlen;
740         
741         int offset, offset_i; /* offset for inserting in array */
742         int more;  /* flag indicating there are more items */
743         
744         char **names;
745         void **pointers;
746         int *icons;
747
748         AutoComplete *autocpl;
749         void *active;
750 };
751
752 typedef struct uiSearchboxData {
753         rcti bbox;
754         uiFontStyle fstyle;
755         uiSearchItems items;
756         int active;     /* index in items array */
757         bool noback;    /* when menu opened with enough space for this */
758         bool preview;   /* draw thumbnail previews, rather than list */
759         bool use_sep;   /* use the UI_SEP_CHAR char for splitting shortcuts (good for operators, bad for data) */
760         int prv_rows, prv_cols;
761 } uiSearchboxData;
762
763 #define SEARCH_ITEMS    10
764
765 /* exported for use by search callbacks */
766 /* returns zero if nothing to add */
767 bool UI_search_item_add(uiSearchItems *items, const char *name, void *poin, int iconid)
768 {
769         /* hijack for autocomplete */
770         if (items->autocpl) {
771                 UI_autocomplete_update_name(items->autocpl, name);
772                 return true;
773         }
774         
775         /* hijack for finding active item */
776         if (items->active) {
777                 if (poin == items->active)
778                         items->offset_i = items->totitem;
779                 items->totitem++;
780                 return true;
781         }
782         
783         if (items->totitem >= items->maxitem) {
784                 items->more = 1;
785                 return false;
786         }
787         
788         /* skip first items in list */
789         if (items->offset_i > 0) {
790                 items->offset_i--;
791                 return true;
792         }
793         
794         if (items->names)
795                 BLI_strncpy(items->names[items->totitem], name, items->maxstrlen);
796         if (items->pointers)
797                 items->pointers[items->totitem] = poin;
798         if (items->icons)
799                 items->icons[items->totitem] = iconid;
800         
801         items->totitem++;
802         
803         return true;
804 }
805
806 int UI_searchbox_size_y(void)
807 {
808         return SEARCH_ITEMS * UI_UNIT_Y + 2 * UI_POPUP_MENU_TOP;
809 }
810
811 int UI_searchbox_size_x(void)
812 {
813         return 12 * UI_UNIT_X;
814 }
815
816 int UI_search_items_find_index(uiSearchItems *items, const char *name, const size_t offset)
817 {
818         int i;
819         for (i = 0; i < items->totitem; i++) {
820                 if (STREQ(name, items->names[i] + offset)) {
821                         return i;
822                 }
823         }
824         return -1;
825 }
826
827 /* ar is the search box itself */
828 static void ui_searchbox_select(bContext *C, ARegion *ar, uiBut *but, int step)
829 {
830         uiSearchboxData *data = ar->regiondata;
831         
832         /* apply step */
833         data->active += step;
834         
835         if (data->items.totitem == 0) {
836                 data->active = -1;
837         }
838         else if (data->active >= data->items.totitem) {
839                 if (data->items.more) {
840                         data->items.offset++;
841                         data->active = data->items.totitem - 1;
842                         ui_searchbox_update(C, ar, but, false);
843                 }
844                 else {
845                         data->active = data->items.totitem - 1;
846                 }
847         }
848         else if (data->active < 0) {
849                 if (data->items.offset) {
850                         data->items.offset--;
851                         data->active = 0;
852                         ui_searchbox_update(C, ar, but, false);
853                 }
854                 else {
855                         /* only let users step into an 'unset' state for unlink buttons */
856                         data->active = (but->flag & UI_BUT_VALUE_CLEAR) ? -1 : 0;
857                 }
858         }
859         
860         ED_region_tag_redraw(ar);
861 }
862
863 static void ui_searchbox_butrect(rcti *r_rect, uiSearchboxData *data, int itemnr)
864 {
865         /* thumbnail preview */
866         if (data->preview) {
867                 int butw = (BLI_rcti_size_x(&data->bbox) - 2 * MENU_BORDER) / data->prv_cols;
868                 int buth = (BLI_rcti_size_y(&data->bbox) - 2 * MENU_BORDER) / data->prv_rows;
869                 int row, col;
870                 
871                 *r_rect = data->bbox;
872                 
873                 col = itemnr % data->prv_cols;
874                 row = itemnr / data->prv_cols;
875                 
876                 r_rect->xmin += MENU_BORDER + (col * butw);
877                 r_rect->xmax = r_rect->xmin + butw;
878                 
879                 r_rect->ymax -= MENU_BORDER + (row * buth);
880                 r_rect->ymin = r_rect->ymax - buth;
881         }
882         /* list view */
883         else {
884                 int buth = (BLI_rcti_size_y(&data->bbox) - 2 * UI_POPUP_MENU_TOP) / SEARCH_ITEMS;
885                 
886                 *r_rect = data->bbox;
887                 r_rect->xmin = data->bbox.xmin + 3.0f;
888                 r_rect->xmax = data->bbox.xmax - 3.0f;
889                 
890                 r_rect->ymax = data->bbox.ymax - UI_POPUP_MENU_TOP - itemnr * buth;
891                 r_rect->ymin = r_rect->ymax - buth;
892         }
893         
894 }
895
896 int ui_searchbox_find_index(ARegion *ar, const char *name)
897 {
898         uiSearchboxData *data = ar->regiondata;
899         return UI_search_items_find_index(&data->items, name, 0);
900 }
901
902 /* x and y in screencoords */
903 bool ui_searchbox_inside(ARegion *ar, int x, int y)
904 {
905         uiSearchboxData *data = ar->regiondata;
906         
907         return BLI_rcti_isect_pt(&data->bbox, x - ar->winrct.xmin, y - ar->winrct.ymin);
908 }
909
910 /* string validated to be of correct length (but->hardmax) */
911 bool ui_searchbox_apply(uiBut *but, ARegion *ar)
912 {
913         uiSearchboxData *data = ar->regiondata;
914
915         but->func_arg2 = NULL;
916         
917         if (data->active != -1) {
918                 const char *name = data->items.names[data->active];
919                 const char *name_sep = data->use_sep ? strrchr(name, UI_SEP_CHAR) : NULL;
920
921                 BLI_strncpy(but->editstr, name, name_sep ? (name_sep - name) : data->items.maxstrlen);
922                 
923                 but->func_arg2 = data->items.pointers[data->active];
924
925                 return true;
926         }
927         else if (but->flag & UI_BUT_VALUE_CLEAR) {
928                 /* It is valid for _VALUE_CLEAR flavor to have no active element (it's a valid way to unlink). */
929                 but->editstr[0] = '\0';
930
931                 return true;
932         }
933         else {
934                 return false;
935         }
936 }
937
938 void ui_searchbox_event(bContext *C, ARegion *ar, uiBut *but, const wmEvent *event)
939 {
940         uiSearchboxData *data = ar->regiondata;
941         int type = event->type, val = event->val;
942         
943         if (type == MOUSEPAN)
944                 ui_pan_to_scroll(event, &type, &val);
945         
946         switch (type) {
947                 case WHEELUPMOUSE:
948                 case UPARROWKEY:
949                         ui_searchbox_select(C, ar, but, -1);
950                         break;
951                 case WHEELDOWNMOUSE:
952                 case DOWNARROWKEY:
953                         ui_searchbox_select(C, ar, but, 1);
954                         break;
955                 case MOUSEMOVE:
956                         if (BLI_rcti_isect_pt(&ar->winrct, event->x, event->y)) {
957                                 rcti rect;
958                                 int a;
959                                 
960                                 for (a = 0; a < data->items.totitem; a++) {
961                                         ui_searchbox_butrect(&rect, data, a);
962                                         if (BLI_rcti_isect_pt(&rect, event->x - ar->winrct.xmin, event->y - ar->winrct.ymin)) {
963                                                 if (data->active != a) {
964                                                         data->active = a;
965                                                         ui_searchbox_select(C, ar, but, 0);
966                                                         break;
967                                                 }
968                                         }
969                                 }
970                         }
971                         break;
972         }
973 }
974
975 /* ar is the search box itself */
976 void ui_searchbox_update(bContext *C, ARegion *ar, uiBut *but, const bool reset)
977 {
978         uiSearchboxData *data = ar->regiondata;
979         
980         /* reset vars */
981         data->items.totitem = 0;
982         data->items.more = 0;
983         if (reset == false) {
984                 data->items.offset_i = data->items.offset;
985         }
986         else {
987                 data->items.offset_i = data->items.offset = 0;
988                 data->active = -1;
989                 
990                 /* handle active */
991                 if (but->search_func && but->func_arg2) {
992                         data->items.active = but->func_arg2;
993                         but->search_func(C, but->search_arg, but->editstr, &data->items);
994                         data->items.active = NULL;
995                         
996                         /* found active item, calculate real offset by centering it */
997                         if (data->items.totitem) {
998                                 /* first case, begin of list */
999                                 if (data->items.offset_i < data->items.maxitem) {
1000                                         data->active = data->items.offset_i;
1001                                         data->items.offset_i = 0;
1002                                 }
1003                                 else {
1004                                         /* second case, end of list */
1005                                         if (data->items.totitem - data->items.offset_i <= data->items.maxitem) {
1006                                                 data->active = data->items.offset_i - data->items.totitem + data->items.maxitem;
1007                                                 data->items.offset_i = data->items.totitem - data->items.maxitem;
1008                                         }
1009                                         else {
1010                                                 /* center active item */
1011                                                 data->items.offset_i -= data->items.maxitem / 2;
1012                                                 data->active = data->items.maxitem / 2;
1013                                         }
1014                                 }
1015                         }
1016                         data->items.offset = data->items.offset_i;
1017                         data->items.totitem = 0;
1018                 }
1019         }
1020         
1021         /* callback */
1022         if (but->search_func)
1023                 but->search_func(C, but->search_arg, but->editstr, &data->items);
1024         
1025         /* handle case where editstr is equal to one of items */
1026         if (reset && data->active == -1) {
1027                 int a;
1028                 
1029                 for (a = 0; a < data->items.totitem; a++) {
1030                         const char *name = data->items.names[a];
1031                         const char *name_sep = data->use_sep ? strrchr(name, UI_SEP_CHAR) : NULL;
1032                         if (STREQLEN(but->editstr, name, name_sep ? (name_sep - name) : data->items.maxstrlen)) {
1033                                 data->active = a;
1034                                 break;
1035                         }
1036                 }
1037                 if (data->items.totitem == 1 && but->editstr[0])
1038                         data->active = 0;
1039         }
1040
1041         /* validate selected item */
1042         ui_searchbox_select(C, ar, but, 0);
1043         
1044         ED_region_tag_redraw(ar);
1045 }
1046
1047 int ui_searchbox_autocomplete(bContext *C, ARegion *ar, uiBut *but, char *str)
1048 {
1049         uiSearchboxData *data = ar->regiondata;
1050         int match = AUTOCOMPLETE_NO_MATCH;
1051
1052         if (str[0]) {
1053                 data->items.autocpl = UI_autocomplete_begin(str, ui_but_string_get_max_length(but));
1054
1055                 but->search_func(C, but->search_arg, but->editstr, &data->items);
1056
1057                 match = UI_autocomplete_end(data->items.autocpl, str);
1058                 data->items.autocpl = NULL;
1059         }
1060
1061         return match;
1062 }
1063
1064 static void ui_searchbox_region_draw_cb(const bContext *C, ARegion *ar)
1065 {
1066         uiSearchboxData *data = ar->regiondata;
1067         
1068         /* pixel space */
1069         wmOrtho2_region_pixelspace(ar);
1070
1071         if (data->noback == false)
1072                 ui_draw_search_back(NULL, NULL, &data->bbox);  /* style not used yet */
1073         
1074         /* draw text */
1075         if (data->items.totitem) {
1076                 rcti rect;
1077                 int a;
1078                 
1079                 if (data->preview) {
1080                         /* draw items */
1081                         for (a = 0; a < data->items.totitem; a++) {
1082                                 /* ensure icon is up-to-date */
1083                                 ui_icon_ensure_deferred(C, data->items.icons[a], data->preview);
1084
1085                                 ui_searchbox_butrect(&rect, data, a);
1086                                 
1087                                 /* widget itself */
1088                                 ui_draw_preview_item(&data->fstyle, &rect, data->items.names[a], data->items.icons[a],
1089                                                      (a == data->active) ? UI_ACTIVE : 0);
1090                         }
1091                         
1092                         /* indicate more */
1093                         if (data->items.more) {
1094                                 ui_searchbox_butrect(&rect, data, data->items.maxitem - 1);
1095                                 glEnable(GL_BLEND);
1096                                 UI_icon_draw(rect.xmax - 18, rect.ymin - 7, ICON_TRIA_DOWN);
1097                                 glDisable(GL_BLEND);
1098                         }
1099                         if (data->items.offset) {
1100                                 ui_searchbox_butrect(&rect, data, 0);
1101                                 glEnable(GL_BLEND);
1102                                 UI_icon_draw(rect.xmin, rect.ymax - 9, ICON_TRIA_UP);
1103                                 glDisable(GL_BLEND);
1104                         }
1105                         
1106                 }
1107                 else {
1108                         /* draw items */
1109                         for (a = 0; a < data->items.totitem; a++) {
1110                                 ui_searchbox_butrect(&rect, data, a);
1111                                 
1112                                 /* widget itself */
1113                                 ui_draw_menu_item(&data->fstyle, &rect, data->items.names[a], data->items.icons[a],
1114                                                   (a == data->active) ? UI_ACTIVE : 0, data->use_sep);
1115                                 
1116                         }
1117                         /* indicate more */
1118                         if (data->items.more) {
1119                                 ui_searchbox_butrect(&rect, data, data->items.maxitem - 1);
1120                                 glEnable(GL_BLEND);
1121                                 UI_icon_draw((BLI_rcti_size_x(&rect)) / 2, rect.ymin - 9, ICON_TRIA_DOWN);
1122                                 glDisable(GL_BLEND);
1123                         }
1124                         if (data->items.offset) {
1125                                 ui_searchbox_butrect(&rect, data, 0);
1126                                 glEnable(GL_BLEND);
1127                                 UI_icon_draw((BLI_rcti_size_x(&rect)) / 2, rect.ymax - 7, ICON_TRIA_UP);
1128                                 glDisable(GL_BLEND);
1129                         }
1130                 }
1131         }
1132 }
1133
1134 static void ui_searchbox_region_free_cb(ARegion *ar)
1135 {
1136         uiSearchboxData *data = ar->regiondata;
1137         int a;
1138
1139         /* free search data */
1140         for (a = 0; a < data->items.maxitem; a++) {
1141                 MEM_freeN(data->items.names[a]);
1142         }
1143         MEM_freeN(data->items.names);
1144         MEM_freeN(data->items.pointers);
1145         MEM_freeN(data->items.icons);
1146         
1147         MEM_freeN(data);
1148         ar->regiondata = NULL;
1149 }
1150
1151 ARegion *ui_searchbox_create_generic(bContext *C, ARegion *butregion, uiBut *but)
1152 {
1153         wmWindow *win = CTX_wm_window(C);
1154         uiStyle *style = UI_style_get();
1155         static ARegionType type;
1156         ARegion *ar;
1157         uiSearchboxData *data;
1158         float aspect = but->block->aspect;
1159         rctf rect_fl;
1160         rcti rect_i;
1161         const int margin = UI_POPUP_MARGIN;
1162         int winx /*, winy */, ofsx, ofsy;
1163         int i;
1164         
1165         /* create area region */
1166         ar = ui_region_temp_add(CTX_wm_screen(C));
1167         
1168         memset(&type, 0, sizeof(ARegionType));
1169         type.draw = ui_searchbox_region_draw_cb;
1170         type.free = ui_searchbox_region_free_cb;
1171         type.regionid = RGN_TYPE_TEMPORARY;
1172         ar->type = &type;
1173         
1174         /* create searchbox data */
1175         data = MEM_callocN(sizeof(uiSearchboxData), "uiSearchboxData");
1176
1177         /* set font, get bb */
1178         data->fstyle = style->widget; /* copy struct */
1179         data->fstyle.align = UI_STYLE_TEXT_CENTER;
1180         ui_fontscale(&data->fstyle.points, aspect);
1181         UI_fontstyle_set(&data->fstyle);
1182         
1183         ar->regiondata = data;
1184         
1185         /* special case, hardcoded feature, not draw backdrop when called from menus,
1186          * assume for design that popup already added it */
1187         if (but->block->flag & UI_BLOCK_SEARCH_MENU)
1188                 data->noback = true;
1189         
1190         if (but->a1 > 0 && but->a2 > 0) {
1191                 data->preview = true;
1192                 data->prv_rows = but->a1;
1193                 data->prv_cols = but->a2;
1194         }
1195
1196         /* only show key shortcuts when needed (not rna buttons) [#36699] */
1197         if (but->rnaprop == NULL) {
1198                 data->use_sep = true;
1199         }
1200         
1201         /* compute position */
1202         if (but->block->flag & UI_BLOCK_SEARCH_MENU) {
1203                 const int search_but_h = BLI_rctf_size_y(&but->rect) + 10;
1204                 /* this case is search menu inside other menu */
1205                 /* we copy region size */
1206
1207                 ar->winrct = butregion->winrct;
1208                 
1209                 /* widget rect, in region coords */
1210                 data->bbox.xmin = margin;
1211                 data->bbox.xmax = BLI_rcti_size_x(&ar->winrct) - margin;
1212                 data->bbox.ymin = margin;
1213                 data->bbox.ymax = BLI_rcti_size_y(&ar->winrct) - margin;
1214                 
1215                 /* check if button is lower half */
1216                 if (but->rect.ymax < BLI_rctf_cent_y(&but->block->rect)) {
1217                         data->bbox.ymin += search_but_h;
1218                 }
1219                 else {
1220                         data->bbox.ymax -= search_but_h;
1221                 }
1222         }
1223         else {
1224                 const int searchbox_width = UI_searchbox_size_x();
1225
1226                 rect_fl.xmin = but->rect.xmin - 5;   /* align text with button */
1227                 rect_fl.xmax = but->rect.xmax + 5;   /* symmetrical */
1228                 rect_fl.ymax = but->rect.ymin;
1229                 rect_fl.ymin = rect_fl.ymax - UI_searchbox_size_y();
1230
1231                 ofsx = (but->block->panel) ? but->block->panel->ofsx : 0;
1232                 ofsy = (but->block->panel) ? but->block->panel->ofsy : 0;
1233
1234                 BLI_rctf_translate(&rect_fl, ofsx, ofsy);
1235         
1236                 /* minimal width */
1237                 if (BLI_rctf_size_x(&rect_fl) < searchbox_width) {
1238                         rect_fl.xmax = rect_fl.xmin + searchbox_width;
1239                 }
1240                 
1241                 /* copy to int, gets projected if possible too */
1242                 BLI_rcti_rctf_copy(&rect_i, &rect_fl);
1243                 
1244                 if (butregion->v2d.cur.xmin != butregion->v2d.cur.xmax) {
1245                         UI_view2d_view_to_region_rcti(&butregion->v2d, &rect_fl, &rect_i);
1246                 }
1247
1248                 BLI_rcti_translate(&rect_i, butregion->winrct.xmin, butregion->winrct.ymin);
1249
1250                 winx = WM_window_pixels_x(win);
1251                 // winy = WM_window_pixels_y(win);  /* UNUSED */
1252                 //wm_window_get_size(win, &winx, &winy);
1253                 
1254                 if (rect_i.xmax > winx) {
1255                         /* super size */
1256                         if (rect_i.xmax > winx + rect_i.xmin) {
1257                                 rect_i.xmax = winx;
1258                                 rect_i.xmin = 0;
1259                         }
1260                         else {
1261                                 rect_i.xmin -= rect_i.xmax - winx;
1262                                 rect_i.xmax = winx;
1263                         }
1264                 }
1265
1266                 if (rect_i.ymin < 0) {
1267                         int newy1 = but->rect.ymax + ofsy;
1268
1269                         if (butregion->v2d.cur.xmin != butregion->v2d.cur.xmax)
1270                                 newy1 = UI_view2d_view_to_region_y(&butregion->v2d, newy1);
1271
1272                         newy1 += butregion->winrct.ymin;
1273
1274                         rect_i.ymax = BLI_rcti_size_y(&rect_i) + newy1;
1275                         rect_i.ymin = newy1;
1276                 }
1277
1278                 /* widget rect, in region coords */
1279                 data->bbox.xmin = margin;
1280                 data->bbox.xmax = BLI_rcti_size_x(&rect_i) + margin;
1281                 data->bbox.ymin = margin;
1282                 data->bbox.ymax = BLI_rcti_size_y(&rect_i) + margin;
1283                 
1284                 /* region bigger for shadow */
1285                 ar->winrct.xmin = rect_i.xmin - margin;
1286                 ar->winrct.xmax = rect_i.xmax + margin;
1287                 ar->winrct.ymin = rect_i.ymin - margin;
1288                 ar->winrct.ymax = rect_i.ymax;
1289         }
1290         
1291         /* adds subwindow */
1292         ED_region_init(C, ar);
1293         
1294         /* notify change and redraw */
1295         ED_region_tag_redraw(ar);
1296         
1297         /* prepare search data */
1298         if (data->preview) {
1299                 data->items.maxitem = data->prv_rows * data->prv_cols;
1300         }
1301         else {
1302                 data->items.maxitem = SEARCH_ITEMS;
1303         }
1304         data->items.maxstrlen = but->hardmax;
1305         data->items.totitem = 0;
1306         data->items.names = MEM_callocN(data->items.maxitem * sizeof(void *), "search names");
1307         data->items.pointers = MEM_callocN(data->items.maxitem * sizeof(void *), "search pointers");
1308         data->items.icons = MEM_callocN(data->items.maxitem * sizeof(int), "search icons");
1309         for (i = 0; i < data->items.maxitem; i++)
1310                 data->items.names[i] = MEM_callocN(but->hardmax + 1, "search pointers");
1311         
1312         return ar;
1313 }
1314
1315 /**
1316  * Similar to Python's `str.title` except...
1317  *
1318  * - we know words are upper case and ascii only.
1319  * - '_' are replaces by spaces.
1320  */
1321 static void str_tolower_titlecaps_ascii(char *str, const size_t len)
1322 {
1323         size_t i;
1324         bool prev_delim = true;
1325
1326         for (i = 0; (i < len) && str[i]; i++) {
1327                 if (str[i] >= 'A' && str[i] <= 'Z') {
1328                         if (prev_delim == false) {
1329                                 str[i] += 'a' - 'A';
1330                         }
1331                 }
1332                 else if (str[i] == '_') {
1333                         str[i] = ' ';
1334                 }
1335
1336                 prev_delim = ELEM(str[i], ' ') || (str[i] >= '0' && str[i] <= '9');
1337         }
1338
1339 }
1340
1341 static void ui_searchbox_region_draw_cb__operator(const bContext *UNUSED(C), ARegion *ar)
1342 {
1343         uiSearchboxData *data = ar->regiondata;
1344
1345         /* pixel space */
1346         wmOrtho2_region_pixelspace(ar);
1347
1348         if (data->noback == false)
1349                 ui_draw_search_back(NULL, NULL, &data->bbox);  /* style not used yet */
1350
1351         /* draw text */
1352         if (data->items.totitem) {
1353                 rcti rect;
1354                 int a;
1355
1356                 /* draw items */
1357                 for (a = 0; a < data->items.totitem; a++) {
1358                         rcti rect_pre, rect_post;
1359                         ui_searchbox_butrect(&rect, data, a);
1360
1361                         rect_pre  = rect;
1362                         rect_post = rect;
1363
1364                         rect_pre.xmax = rect_post.xmin = rect.xmin + ((rect.xmax - rect.xmin) / 4);
1365
1366                         /* widget itself */
1367                         /* NOTE: i18n messages extracting tool does the same, please keep it in sync. */
1368                         {
1369                                 wmOperatorType *ot = data->items.pointers[a];
1370
1371                                 int state = (a == data->active) ? UI_ACTIVE : 0;
1372                                 char  text_pre[128];
1373                                 char *text_pre_p = strstr(ot->idname, "_OT_");
1374                                 if (text_pre_p == NULL) {
1375                                         text_pre[0] = '\0';
1376                                 }
1377                                 else {
1378                                         int text_pre_len;
1379                                         text_pre_p += 1;
1380                                         text_pre_len = BLI_strncpy_rlen(
1381                                                 text_pre, ot->idname, min_ii(sizeof(text_pre), text_pre_p - ot->idname));
1382                                         text_pre[text_pre_len] = ':';
1383                                         text_pre[text_pre_len + 1] = '\0';
1384                                         str_tolower_titlecaps_ascii(text_pre, sizeof(text_pre));
1385                                 }
1386
1387                                 rect_pre.xmax += 4;  /* sneaky, avoid showing ugly margin */
1388                                 ui_draw_menu_item(&data->fstyle, &rect_pre, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, text_pre),
1389                                                   data->items.icons[a], state, false);
1390                                 ui_draw_menu_item(&data->fstyle, &rect_post, data->items.names[a], 0, state, data->use_sep);
1391                         }
1392
1393                 }
1394                 /* indicate more */
1395                 if (data->items.more) {
1396                         ui_searchbox_butrect(&rect, data, data->items.maxitem - 1);
1397                         glEnable(GL_BLEND);
1398                         UI_icon_draw((BLI_rcti_size_x(&rect)) / 2, rect.ymin - 9, ICON_TRIA_DOWN);
1399                         glDisable(GL_BLEND);
1400                 }
1401                 if (data->items.offset) {
1402                         ui_searchbox_butrect(&rect, data, 0);
1403                         glEnable(GL_BLEND);
1404                         UI_icon_draw((BLI_rcti_size_x(&rect)) / 2, rect.ymax - 7, ICON_TRIA_UP);
1405                         glDisable(GL_BLEND);
1406                 }
1407         }
1408 }
1409
1410 ARegion *ui_searchbox_create_operator(bContext *C, ARegion *butregion, uiBut *but)
1411 {
1412         ARegion *ar;
1413
1414         ar = ui_searchbox_create_generic(C, butregion, but);
1415
1416         ar->type->draw = ui_searchbox_region_draw_cb__operator;
1417
1418         return ar;
1419 }
1420
1421 void ui_searchbox_free(bContext *C, ARegion *ar)
1422 {
1423         ui_region_temp_remove(C, CTX_wm_screen(C), ar);
1424 }
1425
1426 /* sets red alert if button holds a string it can't find */
1427 /* XXX weak: search_func adds all partial matches... */
1428 void ui_but_search_refresh(uiBut *but, const bool is_template_ID)
1429 {
1430         uiSearchItems *items;
1431         int x1;
1432
1433         /* possibly very large lists (such as ID datablocks),
1434          * only validate string and pointer RNA buts */
1435         if (but->rnaprop && !ELEM(RNA_property_type(but->rnaprop), PROP_STRING, PROP_POINTER)) {
1436                 return;
1437         }
1438
1439         items = MEM_callocN(sizeof(uiSearchItems), "search items");
1440
1441         /* setup search struct */
1442         items->maxitem = 10;
1443         items->maxstrlen = 256;
1444         items->names = MEM_callocN(items->maxitem * sizeof(void *), "search names");
1445         for (x1 = 0; x1 < items->maxitem; x1++)
1446                 items->names[x1] = MEM_callocN(but->hardmax + 1, "search names");
1447         
1448         but->search_func(but->block->evil_C, but->search_arg, but->drawstr, items);
1449         
1450         /* only redalert when we are sure of it, this can miss cases when >10 matches */
1451         if (items->totitem == 0) {
1452                 UI_but_flag_enable(but, UI_BUT_REDALERT);
1453         }
1454         else if (items->more == 0) {
1455                 const size_t offset = is_template_ID ? 3 : 0;
1456                 if (UI_search_items_find_index(items, but->drawstr, offset) == -1) {
1457                         UI_but_flag_enable(but, UI_BUT_REDALERT);
1458                 }
1459         }
1460         
1461         for (x1 = 0; x1 < items->maxitem; x1++) {
1462                 MEM_freeN(items->names[x1]);
1463         }
1464         MEM_freeN(items->names);
1465         MEM_freeN(items);
1466 }
1467
1468
1469 /************************* Creating Menu Blocks **********************/
1470
1471 /* position block relative to but, result is in window space */
1472 static void ui_block_position(wmWindow *window, ARegion *butregion, uiBut *but, uiBlock *block)
1473 {
1474         uiBut *bt;
1475         uiSafetyRct *saferct;
1476         rctf butrct;
1477         /*float aspect;*/ /*UNUSED*/
1478         int xsize, ysize, xof = 0, yof = 0, center;
1479         short dir1 = 0, dir2 = 0;
1480         
1481         /* transform to window coordinates, using the source button region/block */
1482         ui_block_to_window_rctf(butregion, but->block, &butrct, &but->rect);
1483
1484         /* widget_roundbox_set has this correction too, keep in sync */
1485         if (but->type != UI_BTYPE_PULLDOWN) {
1486                 if (but->drawflag & UI_BUT_ALIGN_TOP)
1487                         butrct.ymax += U.pixelsize;
1488                 if (but->drawflag & UI_BUT_ALIGN_LEFT)
1489                         butrct.xmin -= U.pixelsize;
1490         }
1491         
1492         /* calc block rect */
1493         if (block->rect.xmin == 0.0f && block->rect.xmax == 0.0f) {
1494                 if (block->buttons.first) {
1495                         BLI_rctf_init_minmax(&block->rect);
1496
1497                         for (bt = block->buttons.first; bt; bt = bt->next) {
1498                                 BLI_rctf_union(&block->rect, &bt->rect);
1499                         }
1500                 }
1501                 else {
1502                         /* we're nice and allow empty blocks too */
1503                         block->rect.xmin = block->rect.ymin = 0;
1504                         block->rect.xmax = block->rect.ymax = 20;
1505                 }
1506         }
1507                 
1508         /* aspect = (float)(BLI_rcti_size_x(&block->rect) + 4);*/ /*UNUSED*/
1509         ui_block_to_window_rctf(butregion, but->block, &block->rect, &block->rect);
1510
1511         //block->rect.xmin -= 2.0; block->rect.ymin -= 2.0;
1512         //block->rect.xmax += 2.0; block->rect.ymax += 2.0;
1513         
1514         xsize = BLI_rctf_size_x(&block->rect) + 0.2f * UI_UNIT_X;  /* 4 for shadow */
1515         ysize = BLI_rctf_size_y(&block->rect) + 0.2f * UI_UNIT_Y;
1516         /* aspect /= (float)xsize;*/ /*UNUSED*/
1517
1518         {
1519                 bool left = 0, right = 0, top = 0, down = 0;
1520                 int winx, winy;
1521                 // int offscreen;
1522
1523                 winx = WM_window_pixels_x(window);
1524                 winy = WM_window_pixels_y(window);
1525                 // wm_window_get_size(window, &winx, &winy);
1526
1527                 if (block->direction & UI_DIR_CENTER_Y) {
1528                         center = ysize / 2;
1529                 }
1530                 else {
1531                         center = 0;
1532                 }
1533                 
1534                 /* check if there's space at all */
1535                 if (butrct.xmin - xsize > 0.0f) left = 1;
1536                 if (butrct.xmax + xsize < winx) right = 1;
1537                 if (butrct.ymin - ysize + center > 0.0f) down = 1;
1538                 if (butrct.ymax + ysize - center < winy) top = 1;
1539
1540                 if (top == 0 && down == 0) {
1541                         if (butrct.ymin - ysize < winy - butrct.ymax - ysize)
1542                                 top = 1;
1543                         else
1544                                 down = 1;
1545                 }
1546                 
1547                 dir1 = (block->direction & UI_DIR_ALL);
1548
1549                 /* secundary directions */
1550                 if (dir1 & (UI_DIR_UP | UI_DIR_DOWN)) {
1551                         if      (dir1 & UI_DIR_LEFT)  dir2 = UI_DIR_LEFT;
1552                         else if (dir1 & UI_DIR_RIGHT) dir2 = UI_DIR_RIGHT;
1553                         dir1 &= (UI_DIR_UP | UI_DIR_DOWN);
1554                 }
1555
1556                 if ((dir2 == 0) && (dir1 == UI_DIR_LEFT || dir1 == UI_DIR_RIGHT)) dir2 = UI_DIR_DOWN;
1557                 if ((dir2 == 0) && (dir1 == UI_DIR_UP   || dir1 == UI_DIR_DOWN))  dir2 = UI_DIR_LEFT;
1558                 
1559                 /* no space at all? don't change */
1560                 if (left || right) {
1561                         if (dir1 == UI_DIR_LEFT  && left == 0)  dir1 = UI_DIR_RIGHT;
1562                         if (dir1 == UI_DIR_RIGHT && right == 0) dir1 = UI_DIR_LEFT;
1563                         /* this is aligning, not append! */
1564                         if (dir2 == UI_DIR_LEFT  && right == 0) dir2 = UI_DIR_RIGHT;
1565                         if (dir2 == UI_DIR_RIGHT && left == 0)  dir2 = UI_DIR_LEFT;
1566                 }
1567                 if (down || top) {
1568                         if (dir1 == UI_DIR_UP   && top == 0)  dir1 = UI_DIR_DOWN;
1569                         if (dir1 == UI_DIR_DOWN && down == 0) dir1 = UI_DIR_UP;
1570                         BLI_assert(dir2 != UI_DIR_UP);
1571 //                      if (dir2 == UI_DIR_UP   && top == 0)  dir2 = UI_DIR_DOWN;
1572                         if (dir2 == UI_DIR_DOWN && down == 0) dir2 = UI_DIR_UP;
1573                 }
1574
1575                 if (dir1 == UI_DIR_LEFT) {
1576                         xof = butrct.xmin - block->rect.xmax;
1577                         if (dir2 == UI_DIR_UP) yof = butrct.ymin - block->rect.ymin - center - MENU_PADDING;
1578                         else                   yof = butrct.ymax - block->rect.ymax + center + MENU_PADDING;
1579                 }
1580                 else if (dir1 == UI_DIR_RIGHT) {
1581                         xof = butrct.xmax - block->rect.xmin;
1582                         if (dir2 == UI_DIR_UP) yof = butrct.ymin - block->rect.ymin - center - MENU_PADDING;
1583                         else                   yof = butrct.ymax - block->rect.ymax + center + MENU_PADDING;
1584                 }
1585                 else if (dir1 == UI_DIR_UP) {
1586                         yof = butrct.ymax - block->rect.ymin;
1587                         if (dir2 == UI_DIR_RIGHT) xof = butrct.xmax - block->rect.xmax;
1588                         else                      xof = butrct.xmin - block->rect.xmin;
1589                         /* changed direction? */
1590                         if ((dir1 & block->direction) == 0) {
1591                                 UI_block_order_flip(block);
1592                         }
1593                 }
1594                 else if (dir1 == UI_DIR_DOWN) {
1595                         yof = butrct.ymin - block->rect.ymax;
1596                         if (dir2 == UI_DIR_RIGHT) xof = butrct.xmax - block->rect.xmax;
1597                         else                      xof = butrct.xmin - block->rect.xmin;
1598                         /* changed direction? */
1599                         if ((dir1 & block->direction) == 0) {
1600                                 UI_block_order_flip(block);
1601                         }
1602                 }
1603
1604                 /* and now we handle the exception; no space below or to top */
1605                 if (top == 0 && down == 0) {
1606                         if (dir1 == UI_DIR_LEFT || dir1 == UI_DIR_RIGHT) {
1607                                 /* align with bottom of screen */
1608                                 // yof = ysize; (not with menu scrolls)
1609                         }
1610                 }
1611
1612 #if 0 /* seems redundant and causes issues with blocks inside big regions */
1613                 /* or no space left or right */
1614                 if (left == 0 && right == 0) {
1615                         if (dir1 == UI_DIR_UP || dir1 == UI_DIR_DOWN) {
1616                                 /* align with left size of screen */
1617                                 xof = -block->rect.xmin + 5;
1618                         }
1619                 }
1620 #endif
1621
1622 #if 0
1623                 /* clamp to window bounds, could be made into an option if its ever annoying */
1624                 if (     (offscreen = (block->rect.ymin + yof)) < 0) yof -= offscreen;   /* bottom */
1625                 else if ((offscreen = (block->rect.ymax + yof) - winy) > 0) yof -= offscreen;  /* top */
1626                 if (     (offscreen = (block->rect.xmin + xof)) < 0) xof -= offscreen;   /* left */
1627                 else if ((offscreen = (block->rect.xmax + xof) - winx) > 0) xof -= offscreen;  /* right */
1628 #endif
1629         }
1630         
1631         /* apply offset, buttons in window coords */
1632         
1633         for (bt = block->buttons.first; bt; bt = bt->next) {
1634                 ui_block_to_window_rctf(butregion, but->block, &bt->rect, &bt->rect);
1635
1636                 BLI_rctf_translate(&bt->rect, xof, yof);
1637
1638                 /* ui_but_update recalculates drawstring size in pixels */
1639                 ui_but_update(bt);
1640         }
1641         
1642         BLI_rctf_translate(&block->rect, xof, yof);
1643
1644         /* safety calculus */
1645         {
1646                 const float midx = BLI_rctf_cent_x(&butrct);
1647                 const float midy = BLI_rctf_cent_y(&butrct);
1648                 
1649                 /* when you are outside parent button, safety there should be smaller */
1650                 
1651                 /* parent button to left */
1652                 if (midx < block->rect.xmin) block->safety.xmin = block->rect.xmin - 3;
1653                 else block->safety.xmin = block->rect.xmin - 40;
1654                 /* parent button to right */
1655                 if (midx > block->rect.xmax) block->safety.xmax = block->rect.xmax + 3;
1656                 else block->safety.xmax = block->rect.xmax + 40;
1657
1658                 /* parent button on bottom */
1659                 if (midy < block->rect.ymin) block->safety.ymin = block->rect.ymin - 3;
1660                 else block->safety.ymin = block->rect.ymin - 40;
1661                 /* parent button on top */
1662                 if (midy > block->rect.ymax) block->safety.ymax = block->rect.ymax + 3;
1663                 else block->safety.ymax = block->rect.ymax + 40;
1664
1665                 /* exception for switched pulldowns... */
1666                 if (dir1 && (dir1 & block->direction) == 0) {
1667                         if (dir2 == UI_DIR_RIGHT) block->safety.xmax = block->rect.xmax + 3;
1668                         if (dir2 == UI_DIR_LEFT)  block->safety.xmin = block->rect.xmin - 3;
1669                 }
1670                 block->direction = dir1;
1671         }
1672
1673         /* keep a list of these, needed for pulldown menus */
1674         saferct = MEM_callocN(sizeof(uiSafetyRct), "uiSafetyRct");
1675         saferct->parent = butrct;
1676         saferct->safety = block->safety;
1677         BLI_freelistN(&block->saferct);
1678         BLI_duplicatelist(&block->saferct, &but->block->saferct);
1679         BLI_addhead(&block->saferct, saferct);
1680 }
1681
1682 static void ui_block_region_draw(const bContext *C, ARegion *ar)
1683 {
1684         uiBlock *block;
1685
1686         if (ar->do_draw & RGN_DRAW_REFRESH_UI) {
1687                 uiBlock *block_next;
1688                 ar->do_draw &= ~RGN_DRAW_REFRESH_UI;
1689                 for (block = ar->uiblocks.first; block; block = block_next) {
1690                         block_next = block->next;
1691                         if (block->handle->can_refresh) {
1692                                 ui_popup_block_refresh((bContext *)C, block->handle, NULL, NULL);
1693                         }
1694                 }
1695         }
1696
1697         for (block = ar->uiblocks.first; block; block = block->next)
1698                 UI_block_draw(C, block);
1699 }
1700
1701 /**
1702  * Use to refresh centered popups on screen resizing (for splash).
1703  */
1704 static void ui_block_region_popup_window_listener(
1705         bScreen *UNUSED(sc), ScrArea *UNUSED(sa), ARegion *ar, wmNotifier *wmn, const Scene *UNUSED(scene))
1706 {
1707         switch (wmn->category) {
1708                 case NC_WINDOW:
1709                 {
1710                         switch (wmn->action) {
1711                                 case NA_EDITED:
1712                                 {
1713                                         /* window resize */
1714                                         ED_region_tag_refresh_ui(ar);
1715                                         break;
1716                                 }
1717                         }
1718                         break;
1719                 }
1720         }
1721 }
1722
1723 static void ui_popup_block_clip(wmWindow *window, uiBlock *block)
1724 {
1725         uiBut *bt;
1726         float xofs = 0.0f;
1727         int width = UI_SCREEN_MARGIN;
1728         int winx, winy;
1729
1730         if (block->flag & UI_BLOCK_NO_WIN_CLIP) {
1731                 return;
1732         }
1733
1734         winx = WM_window_pixels_x(window);
1735         winy = WM_window_pixels_y(window);
1736         
1737         /* shift menus to right if outside of view */
1738         if (block->rect.xmin < width) {
1739                 xofs = (width - block->rect.xmin);
1740                 block->rect.xmin += xofs;
1741                 block->rect.xmax += xofs;
1742         }
1743         /* or shift to left if outside of view */
1744         if (block->rect.xmax > winx - width) {
1745                 xofs = winx - width - block->rect.xmax;
1746                 block->rect.xmin += xofs;
1747                 block->rect.xmax += xofs;
1748         }
1749         
1750         if (block->rect.ymin < width)
1751                 block->rect.ymin = width;
1752         if (block->rect.ymax > winy - UI_POPUP_MENU_TOP)
1753                 block->rect.ymax = winy - UI_POPUP_MENU_TOP;
1754         
1755         /* ensure menu items draw inside left/right boundary */
1756         for (bt = block->buttons.first; bt; bt = bt->next) {
1757                 bt->rect.xmin += xofs;
1758                 bt->rect.xmax += xofs;
1759         }
1760
1761 }
1762
1763 void ui_popup_block_scrolltest(uiBlock *block)
1764 {
1765         uiBut *bt;
1766         
1767         block->flag &= ~(UI_BLOCK_CLIPBOTTOM | UI_BLOCK_CLIPTOP);
1768         
1769         for (bt = block->buttons.first; bt; bt = bt->next)
1770                 bt->flag &= ~UI_SCROLLED;
1771         
1772         if (block->buttons.first == block->buttons.last)
1773                 return;
1774         
1775         /* mark buttons that are outside boundary */
1776         for (bt = block->buttons.first; bt; bt = bt->next) {
1777                 if (bt->rect.ymin < block->rect.ymin) {
1778                         bt->flag |= UI_SCROLLED;
1779                         block->flag |= UI_BLOCK_CLIPBOTTOM;
1780                 }
1781                 if (bt->rect.ymax > block->rect.ymax) {
1782                         bt->flag |= UI_SCROLLED;
1783                         block->flag |= UI_BLOCK_CLIPTOP;
1784                 }
1785         }
1786
1787         /* mark buttons overlapping arrows, if we have them */
1788         for (bt = block->buttons.first; bt; bt = bt->next) {
1789                 if (block->flag & UI_BLOCK_CLIPBOTTOM) {
1790                         if (bt->rect.ymin < block->rect.ymin + UI_MENU_SCROLL_ARROW)
1791                                 bt->flag |= UI_SCROLLED;
1792                 }
1793                 if (block->flag & UI_BLOCK_CLIPTOP) {
1794                         if (bt->rect.ymax > block->rect.ymax - UI_MENU_SCROLL_ARROW)
1795                                 bt->flag |= UI_SCROLLED;
1796                 }
1797         }
1798 }
1799
1800 static void ui_popup_block_remove(bContext *C, uiPopupBlockHandle *handle)
1801 {
1802         wmWindow *win = CTX_wm_window(C);
1803         bScreen *sc = CTX_wm_screen(C);
1804
1805         ui_region_temp_remove(C, sc, handle->region);
1806
1807         /* reset to region cursor (only if there's not another menu open) */
1808         if (BLI_listbase_is_empty(&sc->regionbase)) {
1809                 ED_region_cursor_set(win, CTX_wm_area(C), CTX_wm_region(C));
1810                 /* in case cursor needs to be changed again */
1811                 WM_event_add_mousemove(C);
1812         }
1813
1814         if (handle->scrolltimer)
1815                 WM_event_remove_timer(CTX_wm_manager(C), win, handle->scrolltimer);
1816 }
1817
1818 /**
1819  * Called for creating new popups and refreshing existing ones.
1820  */
1821 uiBlock *ui_popup_block_refresh(
1822         bContext *C, uiPopupBlockHandle *handle,
1823         ARegion *butregion, uiBut *but)
1824 {
1825         BLI_assert(handle->can_refresh == true);
1826
1827         const int margin = UI_POPUP_MARGIN;
1828         wmWindow *window = CTX_wm_window(C);
1829         ARegion *ar = handle->region;
1830
1831         uiBlockCreateFunc create_func = handle->popup_create_vars.create_func;
1832         uiBlockHandleCreateFunc handle_create_func = handle->popup_create_vars.handle_create_func;
1833         void *arg = handle->popup_create_vars.arg;
1834
1835         uiBlock *block_old = ar->uiblocks.first;
1836         uiBlock *block;
1837
1838 #ifdef DEBUG
1839         wmEvent *event_back = window->eventstate;
1840 #endif
1841
1842         /* create ui block */
1843         if (create_func)
1844                 block = create_func(C, ar, arg);
1845         else
1846                 block = handle_create_func(C, handle, arg);
1847         
1848         /* callbacks _must_ leave this for us, otherwise we can't call UI_block_update_from_old */
1849         BLI_assert(!block->endblock);
1850
1851         /* ensure we don't use mouse coords here! */
1852 #ifdef DEBUG
1853         window->eventstate = NULL;
1854 #endif
1855
1856         if (block->handle) {
1857                 memcpy(block->handle, handle, sizeof(uiPopupBlockHandle));
1858                 MEM_freeN(handle);
1859                 handle = block->handle;
1860         }
1861         else
1862                 block->handle = handle;
1863
1864         ar->regiondata = handle;
1865
1866         /* set UI_BLOCK_NUMSELECT before UI_block_end() so we get alphanumeric keys assigned */
1867         if (but == NULL) {
1868                 block->flag |= UI_BLOCK_POPUP;
1869         }
1870
1871         block->flag |= UI_BLOCK_LOOP;
1872
1873         /* defer this until blocks are translated (below) */
1874         block->oldblock = NULL;
1875
1876         if (!block->endblock)
1877                 UI_block_end_ex(C, block, handle->popup_create_vars.event_xy);
1878
1879         /* if this is being created from a button */
1880         if (but) {
1881                 block->aspect = but->block->aspect;
1882                 ui_block_position(window, butregion, but, block);
1883                 handle->direction = block->direction;
1884         }
1885         else {
1886                 uiSafetyRct *saferct;
1887                 /* keep a list of these, needed for pulldown menus */
1888                 saferct = MEM_callocN(sizeof(uiSafetyRct), "uiSafetyRct");
1889                 saferct->safety = block->safety;
1890                 BLI_addhead(&block->saferct, saferct);
1891         }
1892
1893         if (block->flag & UI_BLOCK_RADIAL) {
1894                 int win_width = UI_SCREEN_MARGIN;
1895                 int winx, winy;
1896
1897                 int x_offset = 0, y_offset = 0;
1898
1899                 winx = WM_window_pixels_x(window);
1900                 winy = WM_window_pixels_y(window);
1901
1902                 copy_v2_v2(block->pie_data.pie_center_init, block->pie_data.pie_center_spawned);
1903
1904                 /* only try translation if area is large enough */
1905                 if (BLI_rctf_size_x(&block->rect) < winx - (2.0f * win_width)) {
1906                         if (block->rect.xmin < win_width )   x_offset += win_width - block->rect.xmin;
1907                         if (block->rect.xmax > winx - win_width) x_offset += winx - win_width - block->rect.xmax;
1908                 }
1909
1910                 if (BLI_rctf_size_y(&block->rect) < winy - (2.0f * win_width)) {
1911                         if (block->rect.ymin < win_width )   y_offset += win_width - block->rect.ymin;
1912                         if (block->rect.ymax > winy - win_width) y_offset += winy - win_width - block->rect.ymax;
1913                 }
1914                 /* if we are offsetting set up initial data for timeout functionality */
1915
1916                 if ((x_offset != 0) || (y_offset != 0)) {
1917                         block->pie_data.pie_center_spawned[0] += x_offset;
1918                         block->pie_data.pie_center_spawned[1] += y_offset;
1919
1920                         ui_block_translate(block, x_offset, y_offset);
1921
1922                         if (U.pie_initial_timeout > 0)
1923                                 block->pie_data.flags |= UI_PIE_INITIAL_DIRECTION;
1924                 }
1925
1926                 ar->winrct.xmin = 0;
1927                 ar->winrct.xmax = winx;
1928                 ar->winrct.ymin = 0;
1929                 ar->winrct.ymax = winy;
1930
1931                 ui_block_calc_pie_segment(block, block->pie_data.pie_center_init);
1932
1933                 /* lastly set the buttons at the center of the pie menu, ready for animation */
1934                 if (U.pie_animation_timeout > 0) {
1935                         for (uiBut *but_iter = block->buttons.first; but_iter; but_iter = but_iter->next) {
1936                                 if (but_iter->pie_dir != UI_RADIAL_NONE) {
1937                                         BLI_rctf_recenter(&but_iter->rect, UNPACK2(block->pie_data.pie_center_spawned));
1938                                 }
1939                         }
1940                 }
1941         }
1942         else {
1943                 /* clip block with window boundary */
1944                 ui_popup_block_clip(window, block);
1945                 /* the block and buttons were positioned in window space as in 2.4x, now
1946                  * these menu blocks are regions so we bring it back to region space.
1947                  * additionally we add some padding for the menu shadow or rounded menus */
1948                 ar->winrct.xmin = block->rect.xmin - margin;
1949                 ar->winrct.xmax = block->rect.xmax + margin;
1950                 ar->winrct.ymin = block->rect.ymin - margin;
1951                 ar->winrct.ymax = block->rect.ymax + UI_POPUP_MENU_TOP;
1952
1953                 ui_block_translate(block, -ar->winrct.xmin, -ar->winrct.ymin);
1954         }
1955
1956         if (block_old) {
1957                 block->oldblock = block_old;
1958                 UI_block_update_from_old(C, block);
1959                 UI_blocklist_free_inactive(C, &ar->uiblocks);
1960         }
1961
1962         /* checks which buttons are visible, sets flags to prevent draw (do after region init) */
1963         ui_popup_block_scrolltest(block);
1964         
1965         /* adds subwindow */
1966         ED_region_init(C, ar);
1967
1968         /* get winmat now that we actually have the subwindow */
1969         wmSubWindowSet(window, ar->swinid);
1970         
1971         wm_subwindow_matrix_get(window, ar->swinid, block->winmat);
1972         
1973         /* notify change and redraw */
1974         ED_region_tag_redraw(ar);
1975
1976         ED_region_update_rect(C, ar);
1977
1978 #ifdef DEBUG
1979         window->eventstate = event_back;
1980 #endif
1981
1982         return block;
1983 }
1984
1985 uiPopupBlockHandle *ui_popup_block_create(
1986         bContext *C, ARegion *butregion, uiBut *but,
1987         uiBlockCreateFunc create_func, uiBlockHandleCreateFunc handle_create_func,
1988         void *arg)
1989 {
1990         wmWindow *window = CTX_wm_window(C);
1991         uiBut *activebut = UI_context_active_but_get(C);
1992         static ARegionType type;
1993         ARegion *ar;
1994         uiBlock *block;
1995         uiPopupBlockHandle *handle;
1996
1997         /* disable tooltips from buttons below */
1998         if (activebut) {
1999                 UI_but_tooltip_timer_remove(C, activebut);
2000         }
2001         /* standard cursor by default */
2002         WM_cursor_set(window, CURSOR_STD);
2003
2004         /* create handle */
2005         handle = MEM_callocN(sizeof(uiPopupBlockHandle), "uiPopupBlockHandle");
2006
2007         /* store context for operator */
2008         handle->ctx_area = CTX_wm_area(C);
2009         handle->ctx_region = CTX_wm_region(C);
2010
2011         /* store vars to refresh popup (RGN_DRAW_REFRESH_UI) */
2012         handle->popup_create_vars.create_func = create_func;
2013         handle->popup_create_vars.handle_create_func = handle_create_func;
2014         handle->popup_create_vars.arg = arg;
2015         handle->popup_create_vars.butregion = but ? butregion : NULL;
2016         copy_v2_v2_int(handle->popup_create_vars.event_xy, &window->eventstate->x);
2017         /* caller may free vars used to create this popup, in that case this variable should be disabled. */
2018         handle->can_refresh = true;
2019
2020         /* create area region */
2021         ar = ui_region_temp_add(CTX_wm_screen(C));
2022         handle->region = ar;
2023
2024         memset(&type, 0, sizeof(ARegionType));
2025         type.draw = ui_block_region_draw;
2026         type.regionid = RGN_TYPE_TEMPORARY;
2027         ar->type = &type;
2028
2029         UI_region_handlers_add(&ar->handlers);
2030
2031         block = ui_popup_block_refresh(C, handle, butregion, but);
2032         handle = block->handle;
2033
2034         /* keep centered on window resizing */
2035         if ((block->bounds_type == UI_BLOCK_BOUNDS_POPUP_CENTER) && handle->can_refresh) {
2036                 type.listener = ui_block_region_popup_window_listener;
2037         }
2038
2039         return handle;
2040 }
2041
2042 void ui_popup_block_free(bContext *C, uiPopupBlockHandle *handle)
2043 {
2044         ui_popup_block_remove(C, handle);
2045         
2046         MEM_freeN(handle);
2047 }
2048
2049 /***************************** Menu Button ***************************/
2050
2051 #if 0
2052 static void ui_warp_pointer(int x, int y)
2053 {
2054         /* XXX 2.50 which function to use for this? */
2055         /* OSX has very poor mouse-warp support, it sends events;
2056          * this causes a menu being pressed immediately ... */
2057 #  ifndef __APPLE__
2058         warp_pointer(x, y);
2059 #  endif
2060 }
2061 #endif
2062
2063 /********************* Color Button ****************/
2064
2065 /* for picker, while editing hsv */
2066 void ui_but_hsv_set(uiBut *but)
2067 {
2068         float col[3];
2069         ColorPicker *cpicker = but->custom_data;
2070         float *hsv = cpicker->color_data;
2071
2072         ui_color_picker_to_rgb_v(hsv, col);
2073
2074         ui_but_v3_set(but, col);
2075 }
2076
2077 /* Updates all buttons who share the same color picker as the one passed
2078  * also used by small picker, be careful with name checks below... */
2079 static void ui_update_color_picker_buts_rgb(uiBlock *block, ColorPicker *cpicker, const float rgb[3], bool is_display_space)
2080 {
2081         uiBut *bt;
2082         float *hsv = cpicker->color_data;
2083         struct ColorManagedDisplay *display = NULL;
2084         /* this is to keep the H and S value when V is equal to zero
2085          * and we are working in HSV mode, of course!
2086          */
2087         if (is_display_space) {
2088                 ui_rgb_to_color_picker_compat_v(rgb, hsv);
2089         }
2090         else {
2091                 /* we need to convert to display space to use hsv, because hsv is stored in display space */
2092                 float rgb_display[3];
2093
2094                 copy_v3_v3(rgb_display, rgb);
2095                 ui_block_cm_to_display_space_v3(block, rgb_display);
2096                 ui_rgb_to_color_picker_compat_v(rgb_display, hsv);
2097         }
2098
2099         if (block->color_profile)
2100                 display = ui_block_cm_display_get(block);
2101         
2102         /* this updates button strings, is hackish... but button pointers are on stack of caller function */
2103         for (bt = block->buttons.first; bt; bt = bt->next) {
2104                 if (bt->custom_data != cpicker)
2105                         continue;
2106
2107                 if (bt->rnaprop) {
2108                         
2109                         ui_but_v3_set(bt, rgb);
2110                         
2111                 }
2112                 else if (STREQ(bt->str, "Hex: ")) {
2113                         float rgb_gamma[3];
2114                         unsigned char rgb_gamma_uchar[3];
2115                         double intpart;
2116                         char col[16];
2117                         
2118                         /* Hex code is assumed to be in sRGB space (coming from other applications, web, etc) */
2119                         
2120                         copy_v3_v3(rgb_gamma, rgb);
2121
2122                         if (display) {
2123                                 /* make a display version, for Hex code */
2124                                 IMB_colormanagement_scene_linear_to_display_v3(rgb_gamma, display);
2125                         }
2126                         
2127                         if (rgb_gamma[0] > 1.0f) rgb_gamma[0] = modf(rgb_gamma[0], &intpart);
2128                         if (rgb_gamma[1] > 1.0f) rgb_gamma[1] = modf(rgb_gamma[1], &intpart);
2129                         if (rgb_gamma[2] > 1.0f) rgb_gamma[2] = modf(rgb_gamma[2], &intpart);
2130
2131                         rgb_float_to_uchar(rgb_gamma_uchar, rgb_gamma);
2132                         BLI_snprintf(col, sizeof(col), "%02X%02X%02X", UNPACK3_EX((unsigned int), rgb_gamma_uchar, ));
2133                         
2134                         strcpy(bt->poin, col);
2135                 }
2136                 else if (bt->str[1] == ' ') {
2137                         if (bt->str[0] == 'R') {
2138                                 ui_but_value_set(bt, rgb[0]);
2139                         }
2140                         else if (bt->str[0] == 'G') {
2141                                 ui_but_value_set(bt, rgb[1]);
2142                         }
2143                         else if (bt->str[0] == 'B') {
2144                                 ui_but_value_set(bt, rgb[2]);
2145                         }
2146                         else if (bt->str[0] == 'H') {
2147                                 ui_but_value_set(bt, hsv[0]);
2148                         }
2149                         else if (bt->str[0] == 'S') {
2150                                 ui_but_value_set(bt, hsv[1]);
2151                         }
2152                         else if (bt->str[0] == 'V') {
2153                                 ui_but_value_set(bt, hsv[2]);
2154                         }
2155                         else if (bt->str[0] == 'L') {
2156                                 ui_but_value_set(bt, hsv[2]);
2157                         }
2158                 }
2159
2160                 ui_but_update(bt);
2161         }
2162 }
2163
2164 static void ui_colorpicker_rna_cb(bContext *UNUSED(C), void *bt1, void *UNUSED(arg))
2165 {
2166         uiBut *but = (uiBut *)bt1;
2167         uiPopupBlockHandle *popup = but->block->handle;
2168         PropertyRNA *prop = but->rnaprop;
2169         PointerRNA ptr = but->rnapoin;
2170         float rgb[4];
2171         
2172         if (prop) {
2173                 RNA_property_float_get_array(&ptr, prop, rgb);
2174                 ui_update_color_picker_buts_rgb(but->block, but->custom_data, rgb, (RNA_property_subtype(prop) == PROP_COLOR_GAMMA));
2175         }
2176         
2177         if (popup)
2178                 popup->menuretval = UI_RETURN_UPDATE;
2179 }
2180
2181 static void ui_color_wheel_rna_cb(bContext *UNUSED(C), void *bt1, void *UNUSED(arg))
2182 {
2183         uiBut *but = (uiBut *)bt1;
2184         uiPopupBlockHandle *popup = but->block->handle;
2185         float rgb[3];
2186         ColorPicker *cpicker = but->custom_data;
2187         float *hsv = cpicker->color_data;
2188         bool use_display_colorspace = ui_but_is_colorpicker_display_space(but);
2189
2190         ui_color_picker_to_rgb_v(hsv, rgb);
2191
2192         /* hsv is saved in display space so convert back */
2193         if (use_display_colorspace) {
2194                 ui_block_cm_to_scene_linear_v3(but->block, rgb);
2195         }
2196
2197         ui_update_color_picker_buts_rgb(but->block, cpicker, rgb, !use_display_colorspace);
2198         
2199         if (popup)
2200                 popup->menuretval = UI_RETURN_UPDATE;
2201 }
2202
2203 static void ui_colorpicker_hex_rna_cb(bContext *UNUSED(C), void *bt1, void *hexcl)
2204 {
2205         uiBut *but = (uiBut *)bt1;
2206         uiPopupBlockHandle *popup = but->block->handle;
2207         ColorPicker *cpicker = but->custom_data;
2208         char *hexcol = (char *)hexcl;
2209         float rgb[3];
2210         
2211         hex_to_rgb(hexcol, rgb, rgb + 1, rgb + 2);
2212         
2213         /* Hex code is assumed to be in sRGB space (coming from other applications, web, etc) */
2214         if (but->block->color_profile) {
2215                 /* so we need to linearise it for Blender */
2216                 ui_block_cm_to_scene_linear_v3(but->block, rgb);
2217         }
2218         
2219         ui_update_color_picker_buts_rgb(but->block, cpicker, rgb, false);
2220         
2221         if (popup)
2222                 popup->menuretval = UI_RETURN_UPDATE;
2223 }
2224
2225 static void ui_popup_close_cb(bContext *UNUSED(C), void *bt1, void *UNUSED(arg))
2226 {
2227         uiBut *but = (uiBut *)bt1;
2228         uiPopupBlockHandle *popup = but->block->handle;
2229         
2230         if (popup)
2231                 popup->menuretval = UI_RETURN_OK;
2232 }
2233
2234 static void ui_colorpicker_hide_reveal(uiBlock *block, short colormode)
2235 {
2236         uiBut *bt;
2237         
2238         /* tag buttons */
2239         for (bt = block->buttons.first; bt; bt = bt->next) {
2240                 if ((bt->func == ui_colorpicker_rna_cb) && bt->type == UI_BTYPE_NUM_SLIDER && bt->rnaindex != 3) {
2241                         /* RGB sliders (color circle and alpha are always shown) */
2242                         if (colormode == 0) bt->flag &= ~UI_HIDDEN;
2243                         else bt->flag |= UI_HIDDEN;
2244                 }
2245                 else if (bt->func == ui_color_wheel_rna_cb) {
2246                         /* HSV sliders */
2247                         if (colormode == 1) bt->flag &= ~UI_HIDDEN;
2248                         else bt->flag |= UI_HIDDEN;
2249                 }
2250                 else if (bt->func == ui_colorpicker_hex_rna_cb || bt->type == UI_BTYPE_LABEL) {
2251                         /* hex input or gamma correction status label */
2252                         if (colormode == 2) bt->flag &= ~UI_HIDDEN;
2253                         else bt->flag |= UI_HIDDEN;
2254                 }
2255         }
2256 }
2257
2258 static void ui_colorpicker_create_mode_cb(bContext *UNUSED(C), void *bt1, void *UNUSED(arg))
2259 {
2260         uiBut *bt = bt1;
2261         short colormode = ui_but_value_get(bt);
2262         ui_colorpicker_hide_reveal(bt->block, colormode);
2263 }
2264
2265 #define PICKER_H    (7.5f * U.widget_unit)
2266 #define PICKER_W    (7.5f * U.widget_unit)
2267 #define PICKER_SPACE    (0.3f * U.widget_unit)
2268 #define PICKER_BAR      (0.7f * U.widget_unit)
2269
2270 #define PICKER_TOTAL_W  (PICKER_W + PICKER_SPACE + PICKER_BAR)
2271
2272 static void ui_colorpicker_circle(uiBlock *block, PointerRNA *ptr, PropertyRNA *prop, ColorPicker *cpicker)
2273 {
2274         uiBut *bt;
2275         
2276         /* HS circle */
2277         bt = uiDefButR_prop(block, UI_BTYPE_HSVCIRCLE, 0, "", 0, 0, PICKER_H, PICKER_W, ptr, prop, -1, 0.0, 0.0, 0.0, 0, TIP_("Color"));
2278         UI_but_func_set(bt, ui_colorpicker_rna_cb, bt, NULL);
2279         bt->custom_data = cpicker;
2280
2281         /* value */
2282         if (U.color_picker_type == USER_CP_CIRCLE_HSL) {
2283                 bt = uiDefButR_prop(block, UI_BTYPE_HSVCUBE, 0, "", PICKER_W + PICKER_SPACE, 0, PICKER_BAR, PICKER_H, ptr, prop, -1, 0.0, 0.0, UI_GRAD_L_ALT, 0, "Lightness");
2284                 UI_but_func_set(bt, ui_colorpicker_rna_cb, bt, NULL);
2285         }
2286         else {
2287                 bt = uiDefButR_prop(block, UI_BTYPE_HSVCUBE, 0, "", PICKER_W + PICKER_SPACE, 0, PICKER_BAR, PICKER_H, ptr, prop, -1, 0.0, 0.0, UI_GRAD_V_ALT, 0, TIP_("Value"));
2288                 UI_but_func_set(bt, ui_colorpicker_rna_cb, bt, NULL);
2289         }
2290         bt->custom_data = cpicker;
2291 }
2292
2293
2294 static void ui_colorpicker_square(uiBlock *block, PointerRNA *ptr, PropertyRNA *prop, int type, ColorPicker *cpicker)
2295 {
2296         uiBut *bt;
2297         int bartype = type + 3;
2298         
2299         /* HS square */
2300         bt = uiDefButR_prop(block, UI_BTYPE_HSVCUBE, 0, "",   0, PICKER_BAR + PICKER_SPACE, PICKER_TOTAL_W, PICKER_H, ptr, prop, -1, 0.0, 0.0, type, 0, TIP_("Color"));
2301         UI_but_func_set(bt, ui_colorpicker_rna_cb, bt, NULL);
2302         bt->custom_data = cpicker;
2303         
2304         /* value */
2305         bt = uiDefButR_prop(block, UI_BTYPE_HSVCUBE, 0, "",       0, 0, PICKER_TOTAL_W, PICKER_BAR, ptr, prop, -1, 0.0, 0.0, bartype, 0, TIP_("Value"));
2306         UI_but_func_set(bt, ui_colorpicker_rna_cb, bt, NULL);
2307         bt->custom_data = cpicker;
2308 }
2309
2310
2311 /* a HS circle, V slider, rgb/hsv/hex sliders */
2312 static void ui_block_colorpicker(uiBlock *block, float rgba[4], PointerRNA *ptr, PropertyRNA *prop, bool show_picker)
2313 {
2314         static short colormode = 0;  /* temp? 0=rgb, 1=hsv, 2=hex */
2315         uiBut *bt;
2316         int width, butwidth;
2317         static char tip[50];
2318         static char hexcol[128];
2319         float rgb_gamma[3];
2320         unsigned char rgb_gamma_uchar[3];
2321         float softmin, softmax, hardmin, hardmax, step, precision;
2322         int yco;
2323         ColorPicker *cpicker = ui_block_colorpicker_create(block);
2324         float *hsv = cpicker->color_data;
2325                 
2326         width = PICKER_TOTAL_W;
2327         butwidth = width - 1.5f * UI_UNIT_X;
2328         
2329         /* existence of profile means storage is in linear color space, with display correction */
2330         /* XXX That tip message is not use anywhere! */
2331         if (!block->color_profile) {
2332                 BLI_strncpy(tip, N_("Value in Display Color Space"), sizeof(tip));
2333                 copy_v3_v3(rgb_gamma, rgba);
2334         }
2335         else {
2336                 BLI_strncpy(tip, N_("Value in Linear RGB Color Space"), sizeof(tip));
2337
2338                 /* make a display version, for Hex code */
2339                 copy_v3_v3(rgb_gamma, rgba);
2340                 ui_block_cm_to_display_space_v3(block, rgb_gamma);
2341         }
2342         
2343         /* sneaky way to check for alpha */
2344         rgba[3] = FLT_MAX;
2345
2346         RNA_property_float_ui_range(ptr, prop, &softmin, &softmax, &step, &precision);
2347         RNA_property_float_range(ptr, prop, &hardmin, &hardmax);
2348         RNA_property_float_get_array(ptr, prop, rgba);
2349
2350         /* when the softmax isn't defined in the RNA,
2351          * using very large numbers causes sRGB/linear round trip to fail. */
2352         if (softmax == FLT_MAX) {
2353                 softmax = 1.0f;
2354         }
2355
2356         switch (U.color_picker_type) {
2357                 case USER_CP_SQUARE_SV:
2358                         ui_colorpicker_square(block, ptr, prop, UI_GRAD_SV, cpicker);
2359                         break;
2360                 case USER_CP_SQUARE_HS:
2361                         ui_colorpicker_square(block, ptr, prop, UI_GRAD_HS, cpicker);
2362                         break;
2363                 case USER_CP_SQUARE_HV:
2364                         ui_colorpicker_square(block, ptr, prop, UI_GRAD_HV, cpicker);
2365                         break;
2366
2367                 /* user default */
2368                 case USER_CP_CIRCLE_HSV:
2369                 case USER_CP_CIRCLE_HSL:
2370                 default:
2371                         ui_colorpicker_circle(block, ptr, prop, cpicker);
2372                         break;
2373         }
2374         
2375         /* mode */
2376         yco = -1.5f * UI_UNIT_Y;
2377         UI_block_align_begin(block);
2378         bt = uiDefButS(block, UI_BTYPE_ROW, 0, IFACE_("RGB"), 0, yco, width / 3, UI_UNIT_Y, &colormode, 0.0, 0.0, 0, 0, "");
2379         UI_but_func_set(bt, ui_colorpicker_create_mode_cb, bt, NULL);
2380         bt->custom_data = cpicker;
2381         if (U.color_picker_type == USER_CP_CIRCLE_HSL)
2382                 bt = uiDefButS(block, UI_BTYPE_ROW, 0, IFACE_("HSL"), width / 3, yco, width / 3, UI_UNIT_Y, &colormode, 0.0, 1.0, 0, 0, "");
2383         else
2384                 bt = uiDefButS(block, UI_BTYPE_ROW, 0, IFACE_("HSV"), width / 3, yco, width / 3, UI_UNIT_Y, &colormode, 0.0, 1.0, 0, 0, "");
2385         UI_but_func_set(bt, ui_colorpicker_create_mode_cb, bt, NULL);
2386         bt->custom_data = cpicker;
2387         bt = uiDefButS(block, UI_BTYPE_ROW, 0, IFACE_("Hex"), 2 * width / 3, yco, width / 3, UI_UNIT_Y, &colormode, 0.0, 2.0, 0, 0, "");
2388         UI_but_func_set(bt, ui_colorpicker_create_mode_cb, bt, NULL);
2389         bt->custom_data = cpicker;
2390         UI_block_align_end(block);
2391
2392         yco = -3.0f * UI_UNIT_Y;
2393         if (show_picker) {
2394                 bt = uiDefIconButO(block, UI_BTYPE_BUT, "UI_OT_eyedropper_color", WM_OP_INVOKE_DEFAULT, ICON_EYEDROPPER, butwidth + 10, yco, UI_UNIT_X, UI_UNIT_Y, NULL);
2395                 UI_but_func_set(bt, ui_popup_close_cb, bt, NULL);
2396                 bt->custom_data = cpicker;
2397         }
2398         
2399         /* RGB values */
2400         UI_block_align_begin(block);
2401         bt = uiDefButR_prop(block, UI_BTYPE_NUM_SLIDER, 0, IFACE_("R:"),  0, yco, butwidth, UI_UNIT_Y, ptr, prop, 0, 0.0, 0.0, 0, 3, TIP_("Red"));
2402         UI_but_func_set(bt, ui_colorpicker_rna_cb, bt, NULL);
2403         bt->custom_data = cpicker;
2404         bt = uiDefButR_prop(block, UI_BTYPE_NUM_SLIDER, 0, IFACE_("G:"),  0, yco -= UI_UNIT_Y, butwidth, UI_UNIT_Y, ptr, prop, 1, 0.0, 0.0, 0, 3, TIP_("Green"));
2405         UI_but_func_set(bt, ui_colorpicker_rna_cb, bt, NULL);
2406         bt->custom_data = cpicker;
2407         bt = uiDefButR_prop(block, UI_BTYPE_NUM_SLIDER, 0, IFACE_("B:"),  0, yco -= UI_UNIT_Y, butwidth, UI_UNIT_Y, ptr, prop, 2, 0.0, 0.0, 0, 3, TIP_("Blue"));
2408         UI_but_func_set(bt, ui_colorpicker_rna_cb, bt, NULL);
2409         bt->custom_data = cpicker;
2410
2411         /* could use uiItemFullR(col, ptr, prop, -1, 0, UI_ITEM_R_EXPAND|UI_ITEM_R_SLIDER, "", ICON_NONE);
2412          * but need to use UI_but_func_set for updating other fake buttons */
2413         
2414         /* HSV values */
2415         yco = -3.0f * UI_UNIT_Y;
2416         UI_block_align_begin(block);
2417         bt = uiDefButF(block, UI_BTYPE_NUM_SLIDER, 0, IFACE_("H:"),   0, yco, butwidth, UI_UNIT_Y, hsv, 0.0, 1.0, 10, 3, TIP_("Hue"));
2418         UI_but_func_set(bt, ui_color_wheel_rna_cb, bt, hsv);
2419         bt->custom_data = cpicker;
2420         bt = uiDefButF(block, UI_BTYPE_NUM_SLIDER, 0, IFACE_("S:"),   0, yco -= UI_UNIT_Y, butwidth, UI_UNIT_Y, hsv + 1, 0.0, 1.0, 10, 3, TIP_("Saturation"));
2421         UI_but_func_set(bt, ui_color_wheel_rna_cb, bt, hsv);
2422         bt->custom_data = cpicker;
2423         if (U.color_picker_type == USER_CP_CIRCLE_HSL)
2424                 bt = uiDefButF(block, UI_BTYPE_NUM_SLIDER, 0, IFACE_("L:"),   0, yco -= UI_UNIT_Y, butwidth, UI_UNIT_Y, hsv + 2, 0.0, 1.0, 10, 3, TIP_("Lightness"));
2425         else
2426                 bt = uiDefButF(block, UI_BTYPE_NUM_SLIDER, 0, IFACE_("V:"),   0, yco -= UI_UNIT_Y, butwidth, UI_UNIT_Y, hsv + 2, 0.0, softmax, 10, 3, TIP_("Value"));
2427
2428         bt->hardmax = hardmax;  /* not common but rgb  may be over 1.0 */
2429         UI_but_func_set(bt, ui_color_wheel_rna_cb, bt, hsv);
2430         bt->custom_data = cpicker;
2431
2432         UI_block_align_end(block);
2433
2434         if (rgba[3] != FLT_MAX) {
2435                 bt = uiDefButR_prop(block, UI_BTYPE_NUM_SLIDER, 0, IFACE_("A: "),  0, yco -= UI_UNIT_Y, butwidth, UI_UNIT_Y, ptr, prop, 3, 0.0, 0.0, 0, 3, TIP_("Alpha"));
2436                 UI_but_func_set(bt, ui_colorpicker_rna_cb, bt, NULL);
2437                 bt->custom_data = cpicker;
2438         }
2439         else {
2440                 rgba[3] = 1.0f;
2441         }
2442
2443         rgb_float_to_uchar(rgb_gamma_uchar, rgb_gamma);
2444         BLI_snprintf(hexcol, sizeof(hexcol), "%02X%02X%02X", UNPACK3_EX((unsigned int), rgb_gamma_uchar, ));
2445
2446         yco = -3.0f * UI_UNIT_Y;
2447         bt = uiDefBut(block, UI_BTYPE_TEXT, 0, IFACE_("Hex: "), 0, yco, butwidth, UI_UNIT_Y, hexcol, 0, 7, 0, 0, TIP_("Hex triplet for color (#RRGGBB)"));
2448         UI_but_func_set(bt, ui_colorpicker_hex_rna_cb, bt, hexcol);
2449         bt->custom_data = cpicker;
2450         uiDefBut(block, UI_BTYPE_LABEL, 0, IFACE_("(Gamma Corrected)"), 0, yco - UI_UNIT_Y, butwidth, UI_UNIT_Y, NULL, 0.0, 0.0, 0, 0, "");
2451
2452         ui_rgb_to_color_picker_v(rgb_gamma, hsv);
2453
2454         ui_colorpicker_hide_reveal(block, colormode);
2455 }
2456
2457
2458 static int ui_colorpicker_small_wheel_cb(const bContext *UNUSED(C), uiBlock *block, const wmEvent *event)
2459 {
2460         float add = 0.0f;
2461         
2462         if (event->type == WHEELUPMOUSE)
2463                 add = 0.05f;
2464         else if (event->type == WHEELDOWNMOUSE)
2465                 add = -0.05f;
2466         
2467         if (add != 0.0f) {
2468                 uiBut *but;
2469                 
2470                 for (but = block->buttons.first; but; but = but->next) {
2471                         if (but->type == UI_BTYPE_HSVCUBE && but->active == NULL) {
2472                                 uiPopupBlockHandle *popup = block->handle;
2473                                 float rgb[3];
2474                                 ColorPicker *cpicker = but->custom_data;
2475                                 float *hsv = cpicker->color_data;
2476                                 bool use_display_colorspace = ui_but_is_colorpicker_display_space(but);
2477                                 
2478                                 ui_but_v3_get(but, rgb);
2479
2480                                 if (use_display_colorspace)
2481                                         ui_block_cm_to_display_space_v3(block, rgb);
2482
2483                                 ui_rgb_to_color_picker_compat_v(rgb, hsv);
2484
2485                                 hsv[2] = CLAMPIS(hsv[2] + add, 0.0f, 1.0f);
2486                                 ui_color_picker_to_rgb_v(hsv, rgb);
2487
2488                                 if (use_display_colorspace)
2489                                         ui_block_cm_to_scene_linear_v3(block, rgb);
2490
2491                                 ui_but_v3_set(but, rgb);
2492                                 
2493                                 ui_update_color_picker_buts_rgb(block, cpicker, rgb, !use_display_colorspace);
2494                                 if (popup)
2495                                         popup->menuretval = UI_RETURN_UPDATE;
2496                                 
2497                                 return 1;
2498                         }
2499                 }
2500         }
2501         return 0;
2502 }
2503
2504 uiBlock *ui_block_func_COLOR(bContext *C, uiPopupBlockHandle *handle, void *arg_but)
2505 {
2506         uiBut *but = arg_but;
2507         uiBlock *block;
2508         bool show_picker = true;
2509         
2510         block = UI_block_begin(C, handle->region, __func__, UI_EMBOSS);
2511         
2512         if (RNA_property_subtype(but->rnaprop) == PROP_COLOR_GAMMA) {
2513                 block->color_profile = false;
2514         }
2515
2516         if (but->block) {
2517                 /* if color block is invoked from a popup we wouldn't be able to set color properly
2518                  * this is because color picker will close popups first and then will try to figure
2519                  * out active button RNA, and of course it'll fail
2520                  */
2521                 show_picker = (but->block->flag & UI_BLOCK_POPUP) == 0;
2522         }
2523         
2524         copy_v3_v3(handle->retvec, but->editvec);
2525         
2526         ui_block_colorpicker(block, handle->retvec, &but->rnapoin, but->rnaprop, show_picker);
2527         
2528         block->flag = UI_BLOCK_LOOP | UI_BLOCK_KEEP_OPEN | UI_BLOCK_OUT_1 | UI_BLOCK_MOVEMOUSE_QUIT;
2529         UI_block_bounds_set_normal(block, 0.5 * UI_UNIT_X);
2530         
2531         block->block_event_func = ui_colorpicker_small_wheel_cb;
2532         
2533         /* and lets go */
2534         block->direction = UI_DIR_UP;
2535         
2536         return block;
2537 }
2538
2539 /************************ Popup Menu Memory ****************************/
2540
2541 static unsigned int ui_popup_string_hash(const char *str)
2542 {
2543         /* sometimes button contains hotkey, sometimes not, strip for proper compare */
2544         int hash;
2545         const char *delimit = strrchr(str, UI_SEP_CHAR);
2546
2547         if (delimit) {
2548                 hash = BLI_ghashutil_strhash_n(str, delimit - str);
2549         }
2550         else {
2551                 hash = BLI_ghashutil_strhash(str);
2552         }
2553
2554         return hash;
2555 }
2556
2557 static unsigned int ui_popup_menu_hash(const char *str)
2558 {
2559         return BLI_ghashutil_strhash(str);
2560 }
2561
2562 /* but == NULL read, otherwise set */
2563 static uiBut *ui_popup_menu_memory__internal(uiBlock *block, uiBut *but)
2564 {
2565         static unsigned int mem[256];
2566         static bool first = true;
2567
2568         const unsigned int hash = block->puphash;
2569         const unsigned int hash_mod = hash & 255;
2570         
2571         if (first) {
2572                 /* init */
2573                 memset(mem, -1, sizeof(mem));
2574                 first = 0;
2575         }
2576
2577         if (but) {
2578                 /* set */
2579                 mem[hash_mod] = ui_popup_string_hash(but->str);
2580                 return NULL;
2581         }
2582         else {
2583                 /* get */
2584                 for (but = block->buttons.first; but; but = but->next)
2585                         if (ui_popup_string_hash(but->str) == mem[hash_mod])
2586                                 return but;
2587
2588                 return NULL;
2589         }
2590 }
2591
2592 uiBut *ui_popup_menu_memory_get(uiBlock *block)
2593 {
2594         return ui_popup_menu_memory__internal(block, NULL);
2595 }
2596
2597 void ui_popup_menu_memory_set(uiBlock *block, uiBut *but)
2598 {
2599         ui_popup_menu_memory__internal(block, but);
2600 }
2601
2602 /**
2603  * Translate any popup regions (so we can drag them).
2604  */
2605 void ui_popup_translate(bContext *C, ARegion *ar, const int mdiff[2])
2606 {
2607         uiBlock *block;
2608
2609         BLI_rcti_translate(&ar->winrct, UNPACK2(mdiff));
2610
2611         ED_region_update_rect(C, ar);
2612
2613         ED_region_tag_redraw(ar);
2614
2615         /* update blocks */
2616         for (block = ar->uiblocks.first; block; block = block->next) {
2617                 uiSafetyRct *saferct;
2618                 for (saferct = block->saferct.first; saferct; saferct = saferct->next) {
2619                         BLI_rctf_translate(&saferct->parent, UNPACK2(mdiff));
2620                         BLI_rctf_translate(&saferct->safety, UNPACK2(mdiff));
2621                 }
2622         }
2623 }
2624
2625 /******************** Popup Menu with callback or string **********************/
2626
2627 struct uiPopupMenu {
2628         uiBlock *block;
2629         uiLayout *layout;
2630         uiBut *but;
2631
2632         int mx, my;
2633         bool popup, slideout;
2634
2635         uiMenuCreateFunc menu_func;
2636         void *menu_arg;
2637 };
2638
2639 struct uiPieMenu {
2640         uiBlock *block_radial; /* radial block of the pie menu (more could be added later) */
2641         uiLayout *layout;
2642         int mx, my;
2643 };
2644
2645 static uiBlock *ui_block_func_POPUP(bContext *C, uiPopupBlockHandle *handle, void *arg_pup)
2646 {
2647         uiBlock *block;
2648         uiBut *bt;
2649         uiPopupMenu *pup = arg_pup;
2650         int offset[2], minwidth, width, height;
2651         char direction;
2652         bool flip;
2653
2654         if (pup->menu_func) {
2655                 pup->block->handle = handle;
2656                 pup->menu_func(C, pup->layout, pup->menu_arg);
2657                 pup->block->handle = NULL;
2658         }
2659
2660         if (pup->but) {
2661                 /* minimum width to enforece */
2662                 minwidth = BLI_rctf_size_x(&pup->but->rect);
2663
2664                 /* settings (typically rna-enum-popups) show above the button,
2665                  * menu's like file-menu, show below */
2666                 if (pup->block->direction != 0) {
2667                         /* allow overriding the direction from menu_func */
2668                         direction = pup->block->direction;
2669                 }
2670                 else if ((pup->but->type == UI_BTYPE_PULLDOWN) ||
2671                          (UI_but_menutype_get(pup->but) != NULL))
2672                 {
2673                         direction = UI_DIR_DOWN;
2674                 }
2675                 else {
2676                         direction = UI_DIR_UP;
2677                 }
2678         }
2679         else {
2680                 minwidth = 50;
2681                 direction = UI_DIR_DOWN;
2682         }
2683
2684         flip = (direction == UI_DIR_DOWN);
2685
2686         block = pup->block;
2687         
2688         /* in some cases we create the block before the region,
2689          * so we set it delayed here if necessary */
2690         if (BLI_findindex(&handle->region->uiblocks, block) == -1)
2691                 UI_block_region_set(block, handle->region);
2692
2693         block->direction = direction;
2694
2695         UI_block_layout_resolve(block, &width, &height);
2696
2697         UI_block_flag_enable(block, UI_BLOCK_MOVEMOUSE_QUIT);
2698         
2699         if (pup->popup) {
2700                 uiBut *but_activate = NULL;
2701                 UI_block_flag_enable(block, UI_BLOCK_LOOP | UI_BLOCK_NUMSELECT);
2702                 UI_block_direction_set(block, direction);
2703
2704                 /* offset the mouse position, possibly based on earlier selection */
2705                 if ((block->flag & UI_BLOCK_POPUP_MEMORY) &&
2706                     (bt = ui_popup_menu_memory_get(block)))
2707                 {
2708                         /* position mouse on last clicked item, at 0.8*width of the
2709                          * button, so it doesn't overlap the text too much, also note
2710                          * the offset is negative because we are inverse moving the
2711                          * block to be under the mouse */
2712                         offset[0] = -(bt->rect.xmin + 0.8f * BLI_rctf_size_x(&bt->rect));
2713                         offset[1] = -(bt->rect.ymin + 0.5f * UI_UNIT_Y);
2714
2715                         if (ui_but_is_editable(bt)) {
2716                                 but_activate = bt;
2717                         }
2718                 }
2719                 else {
2720                         /* position mouse at 0.8*width of the button and below the tile
2721                          * on the first item */
2722                         offset[0] = 0;
2723                         for (bt = block->buttons.first; bt; bt = bt->next)
2724                                 offset[0] = min_ii(offset[0], -(bt->rect.xmin + 0.8f * BLI_rctf_size_x(&bt->rect)));
2725
2726                         offset[1] = 2.1 * UI_UNIT_Y;
2727
2728                         for (bt = block->buttons.first; bt; bt = bt->next) {
2729                                 if (ui_but_is_editable(bt)) {
2730                                         but_activate = bt;
2731                                         break;
2732                                 }
2733                         }
2734                 }
2735
2736                 /* in rare cases this is needed since moving the popup
2737                  * to be within the window bounds may move it away from the mouse,
2738                  * This ensures we set an item to be active. */
2739                 if (but_activate) {
2740                         ui_but_activate_over(C, handle->region, but_activate);
2741                 }
2742
2743                 block->minbounds = minwidth;
2744                 UI_block_bounds_set_menu(block, 1, offset[0], offset[1]);
2745         }
2746         else {
2747                 /* for a header menu we set the direction automatic */
2748                 if (!pup->slideout && flip) {
2749                         ScrArea *sa = CTX_wm_area(C);
2750                         if (sa && sa->headertype == HEADERDOWN) {
2751                                 ARegion *ar = CTX_wm_region(C);
2752                                 if (ar && ar->regiontype == RGN_TYPE_HEADER) {
2753                                         UI_block_direction_set(block, UI_DIR_UP);
2754                                         UI_block_order_flip(block);
2755                                 }
2756                         }
2757                 }
2758
2759                 block->minbounds = minwidth;
2760                 UI_block_bounds_set_text(block, 3.0f * UI_UNIT_X);
2761         }
2762
2763         /* if menu slides out of other menu, override direction */
2764         if (pup->slideout)
2765                 UI_block_direction_set(block, UI_DIR_RIGHT);
2766
2767         return pup->block;
2768 }
2769
2770 uiPopupBlockHandle *ui_popup_menu_create(
2771         bContext *C, ARegion *butregion, uiBut *but,
2772         uiMenuCreateFunc menu_func, void *arg)
2773 {
2774         wmWindow *window = CTX_wm_window(C);
2775         uiStyle *style = UI_style_get_dpi();
2776         uiPopupBlockHandle *handle;
2777         uiPopupMenu *pup;
2778
2779         pup = MEM_callocN(sizeof(uiPopupMenu), __func__);
2780         pup->block = UI_block_begin(C, NULL, __func__, UI_EMBOSS_PULLDOWN);
2781         pup->block->flag |= UI_BLOCK_NUMSELECT;  /* default menus to numselect */
2782         pup->layout = UI_block_layout(pup->block, UI_LAYOUT_VERTICAL, UI_LAYOUT_MENU, 0, 0, 200, 0, MENU_PADDING, style);
2783         pup->slideout = but ? ui_block_is_menu(but->block) : false;
2784         pup->but = but;
2785         uiLayoutSetOperatorContext(pup->layout, WM_OP_INVOKE_REGION_WIN);
2786
2787         if (!but) {
2788                 /* no button to start from, means we are a popup */
2789                 pup->mx = window->eventstate->x;
2790                 pup->my = window->eventstate->y;
2791                 pup->popup = true;
2792                 pup->block->flag |= UI_BLOCK_NO_FLIP;
2793         }
2794         /* some enums reversing is strange, currently we have no good way to
2795          * reverse some enum's but not others, so reverse all so the first menu
2796          * items are always close to the mouse cursor */
2797         else {
2798 #if 0
2799                 /* if this is an rna button then we can assume its an enum
2800                  * flipping enums is generally not good since the order can be
2801                  * important [#28786] */
2802                 if (but->rnaprop && RNA_property_type(but->rnaprop) == PROP_ENUM) {
2803                         pup->block->flag |= UI_BLOCK_NO_FLIP;
2804                 }
2805 #endif
2806                 if (but->context)
2807                         uiLayoutContextCopy(pup->layout, but->context);
2808         }
2809
2810         /* menu is created from a callback */
2811         pup->menu_func = menu_func;
2812         pup->menu_arg = arg;
2813         
2814         handle = ui_popup_block_create(C, butregion, but, NULL, ui_block_func_POPUP, pup);
2815
2816         if (!but) {
2817                 handle->popup = true;
2818
2819                 UI_popup_handlers_add(C, &window->modalhandlers, handle, 0);
2820                 WM_event_add_mousemove(C);
2821         }
2822         
2823         handle->can_refresh = false;
2824         MEM_freeN(pup);
2825
2826         return handle;
2827 }
2828
2829 /******************** Popup Menu API with begin and end ***********************/
2830
2831 /**
2832  * Only return handler, and set optional title.
2833  * \param block_name: Assigned to uiBlock.name (useful info for debugging).
2834  */
2835 uiPopupMenu *UI_popup_menu_begin_ex(bContext *C, const char *title, const char *block_name, int icon)
2836 {
2837         uiStyle *style = UI_style_get_dpi();
2838         uiPopupMenu *pup = MEM_callocN(sizeof(uiPopupMenu), "popup menu");
2839         uiBut *but;
2840
2841         pup->block = UI_block_begin(C, NULL, block_name, UI_EMBOSS_PULLDOWN);
2842         pup->block->flag |= UI_BLOCK_POPUP_MEMORY | UI_BLOCK_IS_FLIP;
2843         pup->block->puphash = ui_popup_menu_hash(title);
2844         pup->layout = UI_block_layout(pup->block, UI_LAYOUT_VERTICAL, UI_LAYOUT_MENU, 0, 0, 200, 0, MENU_PADDING, style);
2845
2846         /* note, this intentionally differs from the menu & submenu default because many operators
2847          * use popups like this to select one of their options - where having invoke doesn't make sense */
2848         uiLayoutSetOperatorContext(pup->layout, WM_OP_EXEC_REGION_WIN);
2849
2850         /* create in advance so we can let buttons point to retval already */
2851         pup->block->handle = MEM_callocN(sizeof(uiPopupBlockHandle), "uiPopupBlockHandle");
2852         
2853         /* create title button */
2854         if (title[0]) {
2855                 char titlestr[256];
2856                 
2857                 if (icon) {
2858                         BLI_snprintf(titlestr, sizeof(titlestr), " %s", title);
2859                         uiDefIconTextBut(pup->block, UI_BTYPE_LABEL, 0, icon, titlestr, 0, 0, 200, UI_UNIT_Y, NULL, 0.0, 0.0, 0, 0, "");
2860                 }
2861                 else {
2862                         but = uiDefBut(pup->block, UI_BTYPE_LABEL, 0, title, 0, 0, 200, UI_UNIT_Y, NULL, 0.0, 0.0, 0, 0, "");
2863                         but->drawflag = UI_BUT_TEXT_LEFT;
2864                 }
2865
2866                 uiItemS(pup->layout);
2867         }
2868
2869         return pup;
2870 }
2871
2872 uiPopupMenu *UI_popup_menu_begin(bContext *C, const char *title, int icon)
2873 {
2874         return UI_popup_menu_begin_ex(C, title, __func__, icon);
2875 }
2876
2877 /* set the whole structure to work */
2878 void UI_popup_menu_end(bContext *C, uiPopupMenu *pup)
2879 {
2880         wmWindow *window = CTX_wm_window(C);
2881         uiPopupBlockHandle *menu;
2882         
2883         pup->popup = true;
2884         pup->mx = window->eventstate->x;
2885         pup->my = window->eventstate->y;
2886         
2887         menu = ui_popup_block_create(C, NULL, NULL, NULL, ui_block_func_POPUP, pup);
2888         menu->popup = true;
2889         
2890         UI_popup_handlers_add(C, &window->modalhandlers, menu, 0);
2891         WM_event_add_mousemove(C);
2892
2893         menu->can_refresh = false;
2894         MEM_freeN(pup);
2895 }
2896
2897 uiLayout *UI_popup_menu_layout(uiPopupMenu *pup)
2898 {
2899         return pup->layout;
2900 }
2901
2902 /*************************** Pie Menus ***************************************/
2903
2904 static uiBlock *ui_block_func_PIE(bContext *UNUSED(C), uiPopupBlockHandle *handle, void *arg_pie)
2905 {
2906         uiBlock *block;
2907         uiPieMenu *pie = arg_pie;
2908         int minwidth, width, height;
2909
2910         minwidth = 50;
2911         block = pie->block_radial;
2912
2913         /* in some cases we create the block before the region,
2914          * so we set it delayed here if necessary */
2915         if (BLI_findindex(&handle->region->uiblocks, block) == -1)
2916                 UI_block_region_set(block, handle->region);
2917
2918         UI_block_layout_resolve(block, &width, &height);
2919
2920         UI_block_flag_enable(block, UI_BLOCK_LOOP | UI_BLOCK_NUMSELECT);
2921
2922         block->minbounds = minwidth;
2923         block->bounds = 1;
2924         block->mx = 0;
2925         block->my = 0;
2926         block->bounds_type = UI_BLOCK_BOUNDS_PIE_CENTER;
2927
2928         block->pie_data.pie_center_spawned[0] = pie->mx;
2929         block->pie_data.pie_center_spawned[1] = pie->my;
2930
2931         return pie->block_radial;
2932 }
2933
2934 static float ui_pie_menu_title_width(const char *name, int icon)
2935 {
2936         const uiFontStyle *fstyle = UI_FSTYLE_WIDGET;
2937         return (UI_fontstyle_string_width(fstyle, name) +
2938                  (UI_UNIT_X * (1.50f + (icon ? 0.25f : 0.0f))));
2939 }
2940
2941 uiPieMenu *UI_pie_menu_begin(struct bContext *C, const char *title, int icon, const wmEvent *event)
2942 {
2943         uiStyle *style;
2944         uiPieMenu *pie;
2945         short event_type;
2946
2947         wmWindow *win = CTX_wm_window(C);
2948
2949         style = UI_style_get_dpi();
2950         pie = MEM_callocN(sizeof(uiPopupMenu), "pie menu");
2951
2952         pie->block_radial = UI_block_begin(C, NULL, __func__, UI_EMBOSS);
2953         /* may be useful later to allow spawning pies
2954          * from old positions */
2955         /* pie->block_radial->flag |= UI_BLOCK_POPUP_MEMORY; */
2956         pie->block_radial->puphash = ui_popup_menu_hash(title);
2957         pie->block_radial->flag |= UI_BLOCK_RADIAL;
2958
2959         /* if pie is spawned by a left click, it is always assumed to be click style */
2960         if (event->type == LEFTMOUSE) {
2961                 pie->block_radial->pie_data.flags |= UI_PIE_CLICK_STYLE;
2962                 pie->block_radial->pie_data.event = EVENT_NONE;
2963                 win->lock_pie_event = EVENT_NONE;
2964         }
2965         else {
2966                 if (win->last_pie_event != EVENT_NONE) {
2967                         /* original pie key has been released, so don't propagate the event */
2968                         if (win->lock_pie_event == EVENT_NONE) {
2969                                 event_type = EVENT_NONE;
2970                                 pie->block_radial->pie_data.flags |= UI_PIE_CLICK_STYLE;
2971                         }
2972                         else
2973                                 event_type = win->last_pie_event;
2974                 }
2975                 else {
2976                         event_type = event->type;
2977                 }
2978
2979                 pie->block_radial->pie_data.event = event_type;
2980                 win->lock_pie_event = event_type;
2981         }
2982
2983         pie->layout = UI_block_layout(pie->block_radial, UI_LAYOUT_VERTICAL, UI_LAYOUT_PIEMENU, 0, 0, 200, 0, 0, style);
2984         pie->mx = event->x;
2985         pie->my = event->y;
2986
2987         /* create title button */
2988         if (title[0]) {
2989                 uiBut *but;
2990                 char titlestr[256];
2991                 int w;
2992                 if (icon) {
2993                         BLI_snprintf(titlestr, sizeof(titlestr), " %s", title);
2994                         w = ui_pie_menu_title_width(titlestr, icon);
2995                         but = uiDefIconTextBut(pie->block_radial, UI_BTYPE_LABEL, 0, icon, titlestr, 0, 0, w, UI_UNIT_Y, NULL, 0.0, 0.0, 0, 0, "");
2996                 }
2997                 else {
2998                         w = ui_pie_menu_title_width(title, 0);
2999                         but = uiDefBut(pie->block_radial, UI_BTYPE_LABEL, 0, title, 0, 0, w, UI_UNIT_Y, NULL, 0.0, 0.0, 0, 0, "");
3000                 }
3001                 /* do not align left */
3002                 but->drawflag &= ~UI_BUT_TEXT_LEFT;
3003                 pie->block_radial->pie_data.title = but->str;
3004                 pie->block_radial->pie_data.icon = icon;
3005         }
3006
3007         return pie;
3008 }
3009
3010 void UI_pie_menu_end(bContext *C, uiPieMenu *pie)
3011 {
3012         wmWindow *window = CTX_wm_window(C);
3013         uiPopupBlockHandle *menu;
3014
3015         menu = ui_popup_block_create(C, NULL, NULL, NULL, ui_block_func_PIE, pie);
3016         menu->popup = true;
3017         menu->towardstime = PIL_check_seconds_timer();
3018
3019         UI_popup_handlers_add(
3020                 C, &window->modalhandlers,
3021                 menu, WM_HANDLER_ACCEPT_DBL_CLICK);
3022         WM_event_add_mousemove(C);
3023
3024         menu->can_refresh = false;
3025         MEM_freeN(pie);
3026 }
3027
3028 uiLayout *UI_pie_menu_layout(uiPieMenu *pie)
3029 {
3030         return pie->layout;
3031 }
3032
3033 int UI_pie_menu_invoke(struct bContext *C, const char *idname, const wmEvent *event)
3034 {
3035         uiPieMenu *pie;
3036         uiLayout *layout;
3037         Menu menu;
3038         MenuType *mt = WM_menutype_find(idname, true);
3039
3040         if (mt == NULL) {
3041                 printf("%s: named menu \"%s\" not found\n", __func__, idname);
3042                 return OPERATOR_CANCELLED;
3043         }
3044
3045         if (mt->poll && mt->poll(C, mt) == 0)
3046                 /* cancel but allow event to pass through, just like operators do */
3047                 return (OPERATOR_CANCELLED | OPERATOR_PASS_THROUGH);
3048
3049         pie = UI_pie_menu_begin(C, IFACE_(mt->label), ICON_NONE, event);
3050         layout = UI_pie_menu_layout(pie);
3051
3052         menu.layout = layout;
3053         menu.type = mt;
3054
3055         if (G.debug & G_DEBUG_WM) {
3056                 printf("%s: opening menu \"%s\"\n", __func__, idname);
3057         }
3058
3059         mt->draw(C, &menu);
3060
3061         UI_pie_menu_end(C, pie);
3062
3063         return OPERATOR_INTERFACE;
3064 }
3065
3066 int UI_pie_menu_invoke_from_operator_enum(
3067         struct bContext *C, const char *title, const char *opname,
3068         const char *propname, const wmEvent *event)
3069 {
3070         uiPieMenu *pie;
3071         uiLayout *layout;
3072
3073         pie = UI_pie_menu_begin(C, IFACE_(title), ICON_NONE, event);
3074         layout = UI_pie_menu_layout(pie);
3075
3076         layout = uiLayoutRadial(layout);
3077         uiItemsEnumO(layout, opname, propname);
3078
3079         UI_pie_menu_end(C, pie);
3080
3081         return OPERATOR_INTERFACE;
3082 }
3083
3084 int UI_pie_menu_invoke_from_rna_enum(
3085         struct bContext *C, const char *title, const char *path,
3086         const wmEvent *event)
3087 {
3088         PointerRNA ctx_ptr;
3089         PointerRNA r_ptr;
3090         PropertyRNA *r_prop;
3091         uiPieMenu *pie;
3092         uiLayout *layout;
3093
3094         RNA_pointer_create(NULL, &RNA_Context, C, &ctx_ptr);
3095
3096