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