2.5 internal bugfix: remove modal handlers before exiting the screen,
[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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, 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
27 #include <stdarg.h>
28 #include <stdlib.h>
29 #include <string.h>
30
31 #include "MEM_guardedalloc.h"
32
33 #include "DNA_screen_types.h"
34 #include "DNA_view2d_types.h"
35 #include "DNA_userdef_types.h"
36 #include "DNA_windowmanager_types.h"
37
38 #include "BLI_math.h"
39 #include "BLI_blenlib.h"
40 #include "BLI_dynstr.h"
41 #include "BLI_ghash.h"
42
43 #include "BKE_context.h"
44 #include "BKE_icons.h"
45 #include "BKE_report.h"
46 #include "BKE_screen.h"
47 #include "BKE_texture.h"
48 #include "BKE_utildefines.h"
49
50 #include "WM_api.h"
51 #include "WM_types.h"
52 #include "wm_draw.h"
53 #include "wm_subwindow.h"
54 #include "wm_window.h"
55
56 #include "RNA_access.h"
57
58 #include "BIF_gl.h"
59
60 #include "UI_interface.h"
61 #include "UI_interface_icons.h"
62 #include "UI_view2d.h"
63
64 #include "BLF_api.h"
65
66 #include "ED_screen.h"
67
68 #include "interface_intern.h"
69
70 #define MENU_BUTTON_HEIGHT      20
71 #define MENU_SEPR_HEIGHT        6
72 #define B_NOP                   -1
73 #define MENU_SHADOW_SIDE        8
74 #define MENU_SHADOW_BOTTOM      10
75 #define MENU_TOP                        8
76
77 /*********************** Menu Data Parsing ********************* */
78
79 typedef struct MenuEntry {
80         char *str;
81         int retval;
82         int icon;
83         int sepr;
84 } MenuEntry;
85
86 typedef struct MenuData {
87         char *instr;
88         char *title;
89         int titleicon;
90         
91         MenuEntry *items;
92         int nitems, itemssize;
93 } MenuData;
94
95 static MenuData *menudata_new(char *instr)
96 {
97         MenuData *md= MEM_mallocN(sizeof(*md), "MenuData");
98
99         md->instr= instr;
100         md->title= NULL;
101         md->titleicon= 0;
102         md->items= NULL;
103         md->nitems= md->itemssize= 0;
104         
105         return md;
106 }
107
108 static void menudata_set_title(MenuData *md, char *title, int titleicon)
109 {
110         if (!md->title)
111                 md->title= title;
112         if (!md->titleicon)
113                 md->titleicon= titleicon;
114 }
115
116 static void menudata_add_item(MenuData *md, char *str, int retval, int icon, int sepr)
117 {
118         if (md->nitems==md->itemssize) {
119                 int nsize= md->itemssize?(md->itemssize<<1):1;
120                 MenuEntry *oitems= md->items;
121                 
122                 md->items= MEM_mallocN(nsize*sizeof(*md->items), "md->items");
123                 if (oitems) {
124                         memcpy(md->items, oitems, md->nitems*sizeof(*md->items));
125                         MEM_freeN(oitems);
126                 }
127                 
128                 md->itemssize= nsize;
129         }
130         
131         md->items[md->nitems].str= str;
132         md->items[md->nitems].retval= retval;
133         md->items[md->nitems].icon= icon;
134         md->items[md->nitems].sepr= sepr;
135         md->nitems++;
136 }
137
138 void menudata_free(MenuData *md)
139 {
140         MEM_freeN(md->instr);
141         if (md->items)
142                 MEM_freeN(md->items);
143         MEM_freeN(md);
144 }
145
146         /**
147          * Parse menu description strings, string is of the
148          * form "[sss%t|]{(sss[%xNN]|), (%l|), (sss%l|)}", ssss%t indicates the
149          * menu title, sss or sss%xNN indicates an option, 
150          * if %xNN is given then NN is the return value if
151          * that option is selected otherwise the return value
152          * is the index of the option (starting with 1). %l
153          * indicates a seperator, sss%l indicates a label and
154          * new column.
155          * 
156          * @param str String to be parsed.
157          * @retval new menudata structure, free with menudata_free()
158          */
159 MenuData *decompose_menu_string(char *str) 
160 {
161         char *instr= BLI_strdup(str);
162         MenuData *md= menudata_new(instr);
163         char *nitem= NULL, *s= instr;
164         int nicon=0, nretval= 1, nitem_is_title= 0, nitem_is_sepr= 0;
165         
166         while (1) {
167                 char c= *s;
168
169                 if (c=='%') {
170                         if (s[1]=='x') {
171                                 nretval= atoi(s+2);
172
173                                 *s= '\0';
174                                 s++;
175                         } else if (s[1]=='t') {
176                                 nitem_is_title= 1;
177
178                                 *s= '\0';
179                                 s++;
180                         } else if (s[1]=='l') {
181                                 nitem_is_sepr= 1;
182                                 if(!nitem) nitem= "";
183
184                                 *s= '\0';
185                                 s++;
186                         } else if (s[1]=='i') {
187                                 nicon= atoi(s+2);
188                                 
189                                 *s= '\0';
190                                 s++;
191                         }
192                 } else if (c=='|' || c == '\n' || c=='\0') {
193                         if (nitem) {
194                                 *s= '\0';
195
196                                 if(nitem_is_title) {
197                                         menudata_set_title(md, nitem, nicon);
198                                         nitem_is_title= 0;
199                                 }
200                                 else if(nitem_is_sepr) {
201                                         /* prevent separator to get a value */
202                                         menudata_add_item(md, nitem, -1, nicon, 1);
203                                         nretval= md->nitems+1;
204                                         nitem_is_sepr= 0;
205                                 }
206                                 else {
207                                         menudata_add_item(md, nitem, nretval, nicon, 0);
208                                         nretval= md->nitems+1;
209                                 } 
210                                 
211                                 nitem= NULL;
212                                 nicon= 0;
213                         }
214                         
215                         if (c=='\0')
216                                 break;
217                 } else if (!nitem)
218                         nitem= s;
219                 
220                 s++;
221         }
222         
223         return md;
224 }
225
226 void ui_set_name_menu(uiBut *but, int value)
227 {
228         MenuData *md;
229         int i;
230         
231         md= decompose_menu_string(but->str);
232         for (i=0; i<md->nitems; i++)
233                 if (md->items[i].retval==value)
234                         strcpy(but->drawstr, md->items[i].str);
235         
236         menudata_free(md);
237 }
238
239 int ui_step_name_menu(uiBut *but, int step)
240 {
241         MenuData *md;
242         int value= ui_get_but_val(but);
243         int i;
244         
245         md= decompose_menu_string(but->str);
246         for (i=0; i<md->nitems; i++)
247                 if (md->items[i].retval==value)
248                         break;
249         
250         if(step==1) {
251                 /* skip separators */
252                 for(; i<md->nitems-1; i++) {
253                         if(md->items[i+1].retval != -1) {
254                                 value= md->items[i+1].retval;
255                                 break;
256                         }
257                 }
258         }
259         else {
260                 if(i>0) {
261                         /* skip separators */
262                         for(; i>0; i--) {
263                                 if(md->items[i-1].retval != -1) {
264                                         value= md->items[i-1].retval;
265                                         break;
266                                 }
267                         }
268                 }
269         }
270         
271         menudata_free(md);
272                 
273         return value;
274 }
275
276
277 /******************** Creating Temporary regions ******************/
278
279 ARegion *ui_add_temporary_region(bScreen *sc)
280 {
281         ARegion *ar;
282
283         ar= MEM_callocN(sizeof(ARegion), "area region");
284         BLI_addtail(&sc->regionbase, ar);
285
286         ar->regiontype= RGN_TYPE_TEMPORARY;
287         ar->alignment= RGN_ALIGN_FLOAT;
288
289         return ar;
290 }
291
292 void ui_remove_temporary_region(bContext *C, bScreen *sc, ARegion *ar)
293 {
294         if(CTX_wm_window(C))
295                 wm_draw_region_clear(CTX_wm_window(C), ar);
296
297         ED_region_exit(C, ar);
298         BKE_area_region_free(NULL, ar);         /* NULL: no spacetype */
299         BLI_freelinkN(&sc->regionbase, ar);
300 }
301
302 /************************* Creating Tooltips **********************/
303
304 #define MAX_TOOLTIP_LINES 8
305
306 typedef struct uiTooltipData {
307         rcti bbox;
308         uiFontStyle fstyle;
309         char lines[MAX_TOOLTIP_LINES][512];
310         int linedark[MAX_TOOLTIP_LINES];
311         int totline;
312         int toth, spaceh, lineh;
313 } uiTooltipData;
314
315 static void ui_tooltip_region_draw(const bContext *C, ARegion *ar)
316 {
317         uiTooltipData *data= ar->regiondata;
318         rcti bbox= data->bbox;
319         int a;
320         
321         ui_draw_menu_back(U.uistyles.first, NULL, &data->bbox);
322         
323         /* draw text */
324         uiStyleFontSet(&data->fstyle);
325
326         bbox.ymax= bbox.ymax - 0.5f*((bbox.ymax - bbox.ymin) - data->toth);
327         bbox.ymin= bbox.ymax - data->lineh;
328
329         for(a=0; a<data->totline; a++) {
330                 if(!data->linedark[a]) glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
331                 else glColor4f(0.5f, 0.5f, 0.5f, 1.0f);
332
333                 uiStyleFontDraw(&data->fstyle, &bbox, data->lines[a]);
334                 bbox.ymin -= data->lineh + data->spaceh;
335                 bbox.ymax -= data->lineh + data->spaceh;
336         }
337 }
338
339 static void ui_tooltip_region_free(ARegion *ar)
340 {
341         uiTooltipData *data;
342
343         data= ar->regiondata;
344         MEM_freeN(data);
345         ar->regiondata= NULL;
346 }
347
348 ARegion *ui_tooltip_create(bContext *C, ARegion *butregion, uiBut *but)
349 {
350         uiStyle *style= U.uistyles.first;       // XXX pass on as arg
351         static ARegionType type;
352         ARegion *ar;
353         uiTooltipData *data;
354         IDProperty *prop;
355         char buf[512];
356         float fonth, fontw, aspect= but->block->aspect;
357         float x1f, x2f, y1f, y2f;
358         int x1, x2, y1, y2, winx, winy, ofsx, ofsy, w, h, a;
359
360         if(but->flag & UI_BUT_NO_TOOLTIP)
361                 return NULL;
362
363         /* create tooltip data */
364         data= MEM_callocN(sizeof(uiTooltipData), "uiTooltipData");
365
366         if(but->tip && strlen(but->tip)) {
367                 BLI_strncpy(data->lines[data->totline], but->tip, sizeof(data->lines[0]));
368                 data->totline++;
369         }
370
371         if(but->optype && !(but->block->flag & UI_BLOCK_LOOP)) {
372                 /* operator keymap (not menus, they already have it) */
373                 prop= (but->opptr)? but->opptr->data: NULL;
374
375                 if(WM_key_event_operator_string(C, but->optype->idname, but->opcontext, prop, buf, sizeof(buf))) {
376                         BLI_snprintf(data->lines[data->totline], sizeof(data->lines[0]), "Shortcut: %s", buf);
377                         data->linedark[data->totline]= 1;
378                         data->totline++;
379                 }
380         }
381
382         if(ELEM3(but->type, TEX, IDPOIN, SEARCH_MENU)) {
383                 /* full string */
384                 ui_get_but_string(but, buf, sizeof(buf));
385                 if(buf[0]) {
386                         BLI_snprintf(data->lines[data->totline], sizeof(data->lines[0]), "Value: %s", buf);
387                         data->linedark[data->totline]= 1;
388                         data->totline++;
389                 }
390         }
391
392         if(but->rnaprop) {
393                 if(but->flag & UI_BUT_DRIVEN) {
394                         if(ui_but_anim_expression_get(but, buf, sizeof(buf))) {
395                                 /* expression */
396                                 BLI_snprintf(data->lines[data->totline], sizeof(data->lines[0]), "Expression: %s", buf);
397                                 data->linedark[data->totline]= 1;
398                                 data->totline++;
399                         }
400                 }
401
402                 /* rna info */
403                 BLI_snprintf(data->lines[data->totline], sizeof(data->lines[0]), "Python: %s.%s", RNA_struct_identifier(but->rnapoin.type), RNA_property_identifier(but->rnaprop));
404                 data->linedark[data->totline]= 1;
405                 data->totline++;
406         }
407         else if (but->optype) {
408                 PointerRNA *opptr;
409                 char *str;
410                 opptr= uiButGetOperatorPtrRNA(but); /* allocated when needed, the button owns it */
411
412                 str= WM_operator_pystring(C, but->optype, opptr, 0);
413
414                 /* operator info */
415                 BLI_snprintf(data->lines[data->totline], sizeof(data->lines[0]), "Python: %s", str);
416                 data->linedark[data->totline]= 1;
417                 data->totline++;
418
419                 MEM_freeN(str);
420         }
421
422         if(data->totline == 0) {
423                 MEM_freeN(data);
424                 return NULL;
425         }
426
427         /* create area region */
428         ar= ui_add_temporary_region(CTX_wm_screen(C));
429
430         memset(&type, 0, sizeof(ARegionType));
431         type.draw= ui_tooltip_region_draw;
432         type.free= ui_tooltip_region_free;
433         ar->type= &type;
434         
435         /* set font, get bb */
436         data->fstyle= style->widget; /* copy struct */
437         data->fstyle.align= UI_STYLE_TEXT_CENTER;
438         ui_fontscale(&data->fstyle.points, aspect);
439         uiStyleFontSet(&data->fstyle);
440
441         h= BLF_height(data->lines[0]);
442
443         for(a=0, fontw=0, fonth=0; a<data->totline; a++) {
444                 w= BLF_width(data->lines[a]);
445                 fontw= MAX2(fontw, w);
446                 fonth += (a == 0)? h: h+5;
447         }
448
449         fontw *= aspect;
450         fonth *= aspect;
451
452         ar->regiondata= data;
453
454         data->toth= fonth;
455         data->lineh= h*aspect;
456         data->spaceh= 5*aspect;
457
458         /* compute position */
459         ofsx= (but->block->panel)? but->block->panel->ofsx: 0;
460         ofsy= (but->block->panel)? but->block->panel->ofsy: 0;
461
462         x1f= (but->x1+but->x2)/2.0f + ofsx - 16.0f*aspect;
463         x2f= x1f + fontw + 16.0f*aspect;
464         y2f= but->y1 + ofsy - 15.0f*aspect;
465         y1f= y2f - fonth - 10.0f*aspect;
466         
467         /* copy to int, gets projected if possible too */
468         x1= x1f; y1= y1f; x2= x2f; y2= y2f; 
469         
470         if(butregion) {
471                 /* XXX temp, region v2ds can be empty still */
472                 if(butregion->v2d.cur.xmin != butregion->v2d.cur.xmax) {
473                         UI_view2d_to_region_no_clip(&butregion->v2d, x1f, y1f, &x1, &y1);
474                         UI_view2d_to_region_no_clip(&butregion->v2d, x2f, y2f, &x2, &y2);
475                 }
476
477                 x1 += butregion->winrct.xmin;
478                 x2 += butregion->winrct.xmin;
479                 y1 += butregion->winrct.ymin;
480                 y2 += butregion->winrct.ymin;
481         }
482
483         wm_window_get_size(CTX_wm_window(C), &winx, &winy);
484
485         if(x2 > winx) {
486                 /* super size */
487                 if(x2 > winx + x1) {
488                         x2= winx;
489                         x1= 0;
490                 }
491                 else {
492                         x1 -= x2-winx;
493                         x2= winx;
494                 }
495         }
496         if(y1 < 0) {
497                 y1 += 56*aspect;
498                 y2 += 56*aspect;
499         }
500
501         /* widget rect, in region coords */
502         data->bbox.xmin= MENU_SHADOW_SIDE;
503         data->bbox.xmax= x2-x1 + MENU_SHADOW_SIDE;
504         data->bbox.ymin= MENU_SHADOW_BOTTOM;
505         data->bbox.ymax= y2-y1 + MENU_SHADOW_BOTTOM;
506         
507         /* region bigger for shadow */
508         ar->winrct.xmin= x1 - MENU_SHADOW_SIDE;
509         ar->winrct.xmax= x2 + MENU_SHADOW_SIDE;
510         ar->winrct.ymin= y1 - MENU_SHADOW_BOTTOM;
511         ar->winrct.ymax= y2 + MENU_TOP;
512
513         /* adds subwindow */
514         ED_region_init(C, ar);
515         
516         /* notify change and redraw */
517         ED_region_tag_redraw(ar);
518
519         return ar;
520 }
521
522 void ui_tooltip_free(bContext *C, ARegion *ar)
523 {
524         ui_remove_temporary_region(C, CTX_wm_screen(C), ar);
525 }
526
527
528 /************************* Creating Search Box **********************/
529
530 struct uiSearchItems {
531         int maxitem, totitem, maxstrlen;
532         
533         int offset, offset_i; /* offset for inserting in array */
534         int more;  /* flag indicating there are more items */
535         
536         char **names;
537         void **pointers;
538         int *icons;
539
540         AutoComplete *autocpl;
541         void *active;
542 };
543
544 typedef struct uiSearchboxData {
545         rcti bbox;
546         uiFontStyle fstyle;
547         uiSearchItems items;
548         int active;             /* index in items array */
549         int noback;             /* when menu opened with enough space for this */
550 } uiSearchboxData;
551
552 #define SEARCH_ITEMS    10
553
554 /* exported for use by search callbacks */
555 /* returns zero if nothing to add */
556 int uiSearchItemAdd(uiSearchItems *items, const char *name, void *poin, int iconid)
557 {
558         /* hijack for autocomplete */
559         if(items->autocpl) {
560                 autocomplete_do_name(items->autocpl, name);
561                 return 1;
562         }
563         
564         /* hijack for finding active item */
565         if(items->active) {
566                 if(poin==items->active)
567                         items->offset_i= items->totitem;
568                 items->totitem++;
569                 return 1;
570         }
571         
572         if(items->totitem>=items->maxitem) {
573                 items->more= 1;
574                 return 0;
575         }
576         
577         /* skip first items in list */
578         if(items->offset_i > 0) {
579                 items->offset_i--;
580                 return 1;
581         }
582         
583         BLI_strncpy(items->names[items->totitem], name, items->maxstrlen);
584         items->pointers[items->totitem]= poin;
585         items->icons[items->totitem]= iconid;
586         
587         items->totitem++;
588         
589         return 1;
590 }
591
592 int uiSearchBoxhHeight(void)
593 {
594         return SEARCH_ITEMS*MENU_BUTTON_HEIGHT + 2*MENU_TOP;
595 }
596
597 /* ar is the search box itself */
598 static void ui_searchbox_select(bContext *C, ARegion *ar, uiBut *but, int step)
599 {
600         uiSearchboxData *data= ar->regiondata;
601         
602         /* apply step */
603         data->active+= step;
604         
605         if(data->items.totitem==0)
606                 data->active= 0;
607         else if(data->active > data->items.totitem) {
608                 if(data->items.more) {
609                         data->items.offset++;
610                         data->active= data->items.totitem;
611                         ui_searchbox_update(C, ar, but, 0);
612                 }
613                 else
614                         data->active= data->items.totitem;
615         }
616         else if(data->active < 1) {
617                 if(data->items.offset) {
618                         data->items.offset--;
619                         data->active= 1;
620                         ui_searchbox_update(C, ar, but, 0);
621                 }
622                 else if(data->active < 0)
623                         data->active= 0;
624         }
625         
626         ED_region_tag_redraw(ar);
627 }
628
629 static void ui_searchbox_butrect(rcti *rect, uiSearchboxData *data, int itemnr)
630 {
631         int buth= (data->bbox.ymax-data->bbox.ymin - 2*MENU_TOP)/SEARCH_ITEMS;
632         
633         *rect= data->bbox;
634         rect->xmin= data->bbox.xmin + 3.0f;
635         rect->xmax= data->bbox.xmax - 3.0f;
636         
637         rect->ymax= data->bbox.ymax - MENU_TOP - itemnr*buth;
638         rect->ymin= rect->ymax - buth;
639         
640 }
641
642 /* x and y in screencoords */
643 int ui_searchbox_inside(ARegion *ar, int x, int y)
644 {
645         uiSearchboxData *data= ar->regiondata;
646         
647         return(BLI_in_rcti(&data->bbox, x-ar->winrct.xmin, y-ar->winrct.ymin));
648 }
649
650 /* string validated to be of correct length (but->hardmax) */
651 void ui_searchbox_apply(uiBut *but, ARegion *ar)
652 {
653         uiSearchboxData *data= ar->regiondata;
654
655         but->func_arg2= NULL;
656         
657         if(data->active) {
658                 char *name= data->items.names[data->active-1];
659                 char *cpoin= strchr(name, '|');
660                 
661                 if(cpoin) cpoin[0]= 0;
662                 BLI_strncpy(but->editstr, name, data->items.maxstrlen);
663                 if(cpoin) cpoin[0]= '|';
664                 
665                 but->func_arg2= data->items.pointers[data->active-1];
666         }
667 }
668
669 void ui_searchbox_event(bContext *C, ARegion *ar, uiBut *but, wmEvent *event)
670 {
671         uiSearchboxData *data= ar->regiondata;
672         
673         switch(event->type) {
674                 case WHEELUPMOUSE:
675                 case UPARROWKEY:
676                         ui_searchbox_select(C, ar, but, -1);
677                         break;
678                 case WHEELDOWNMOUSE:
679                 case DOWNARROWKEY:
680                         ui_searchbox_select(C, ar, but, 1);
681                         break;
682                 case MOUSEMOVE:
683                         if(BLI_in_rcti(&ar->winrct, event->x, event->y)) {
684                                 rcti rect;
685                                 int a;
686                                 
687                                 for(a=0; a<data->items.totitem; a++) {
688                                         ui_searchbox_butrect(&rect, data, a);
689                                         if(BLI_in_rcti(&rect, event->x - ar->winrct.xmin, event->y - ar->winrct.ymin)) {
690                                                 if( data->active!= a+1) {
691                                                         data->active= a+1;
692                                                         ui_searchbox_select(C, ar, but, 0);
693                                                         break;
694                                                 }
695                                         }
696                                 }
697                         }
698                         break;
699         }
700 }
701
702 /* ar is the search box itself */
703 void ui_searchbox_update(bContext *C, ARegion *ar, uiBut *but, int reset)
704 {
705         uiSearchboxData *data= ar->regiondata;
706         
707         /* reset vars */
708         data->items.totitem= 0;
709         data->items.more= 0;
710         if(reset==0) {
711                 data->items.offset_i= data->items.offset;
712         }
713         else {
714                 data->items.offset_i= data->items.offset= 0;
715                 data->active= 0;
716                 
717                 /* handle active */
718                 if(but->search_func && but->func_arg2) {
719                         data->items.active= but->func_arg2;
720                         but->search_func(C, but->search_arg, but->editstr, &data->items);
721                         data->items.active= NULL;
722                         
723                         /* found active item, calculate real offset by centering it */
724                         if(data->items.totitem) {
725                                 /* first case, begin of list */
726                                 if(data->items.offset_i < data->items.maxitem) {
727                                         data->active= data->items.offset_i+1;
728                                         data->items.offset_i= 0;
729                                 }
730                                 else {
731                                         /* second case, end of list */
732                                         if(data->items.totitem - data->items.offset_i <= data->items.maxitem) {
733                                                 data->active= 1 + data->items.offset_i - data->items.totitem + data->items.maxitem;
734                                                 data->items.offset_i= data->items.totitem - data->items.maxitem;
735                                         }
736                                         else {
737                                                 /* center active item */
738                                                 data->items.offset_i -= data->items.maxitem/2;
739                                                 data->active= 1 + data->items.maxitem/2;
740                                         }
741                                 }
742                         }
743                         data->items.offset= data->items.offset_i;
744                         data->items.totitem= 0;
745                 }
746         }
747         
748         /* callback */
749         if(but->search_func)
750                 but->search_func(C, but->search_arg, but->editstr, &data->items);
751         
752         /* handle case where editstr is equal to one of items */
753         if(reset && data->active==0) {
754                 int a;
755                 
756                 for(a=0; a<data->items.totitem; a++) {
757                         char *cpoin= strchr(data->items.names[a], '|');
758                         
759                         if(cpoin) cpoin[0]= 0;
760                         if(0==strcmp(but->editstr, data->items.names[a]))
761                                 data->active= a+1;
762                         if(cpoin) cpoin[0]= '|';
763                 }
764                 if(data->items.totitem==1 && but->editstr[0])
765                         data->active= 1;
766         }
767
768         /* validate selected item */
769         ui_searchbox_select(C, ar, but, 0);
770         
771         ED_region_tag_redraw(ar);
772 }
773
774 void ui_searchbox_autocomplete(bContext *C, ARegion *ar, uiBut *but, char *str)
775 {
776         uiSearchboxData *data= ar->regiondata;
777
778         if(str[0]) {
779                 data->items.autocpl= autocomplete_begin(str, ui_get_but_string_max_length(but));
780
781                 but->search_func(C, but->search_arg, but->editstr, &data->items);
782
783                 autocomplete_end(data->items.autocpl, str);
784                 data->items.autocpl= NULL;
785         }
786 }
787
788 static void ui_searchbox_region_draw(const bContext *C, ARegion *ar)
789 {
790         uiSearchboxData *data= ar->regiondata;
791         
792         /* pixel space */
793         wmOrtho2(-0.01f, ar->winx-0.01f, -0.01f, ar->winy-0.01f);
794
795         if(!data->noback)
796                 ui_draw_search_back(U.uistyles.first, NULL, &data->bbox);
797         
798         /* draw text */
799         if(data->items.totitem) {
800                 rcti rect;
801                 int a;
802                                 
803                 /* draw items */
804                 for(a=0; a<data->items.totitem; a++) {
805                         ui_searchbox_butrect(&rect, data, a);
806                         
807                         /* widget itself */
808                         ui_draw_menu_item(&data->fstyle, &rect, data->items.names[a], data->items.icons[a], (a+1)==data->active?UI_ACTIVE:0);
809                         
810                 }
811                 /* indicate more */
812                 if(data->items.more) {
813                         ui_searchbox_butrect(&rect, data, data->items.maxitem-1);
814                         glEnable(GL_BLEND);
815                         UI_icon_draw((rect.xmax-rect.xmin)/2, rect.ymin-9, ICON_TRIA_DOWN);
816                         glDisable(GL_BLEND);
817                 }
818                 if(data->items.offset) {
819                         ui_searchbox_butrect(&rect, data, 0);
820                         glEnable(GL_BLEND);
821                         UI_icon_draw((rect.xmax-rect.xmin)/2, rect.ymax-7, ICON_TRIA_UP);
822                         glDisable(GL_BLEND);
823                 }
824         }
825 }
826
827 static void ui_searchbox_region_free(ARegion *ar)
828 {
829         uiSearchboxData *data= ar->regiondata;
830         int a;
831
832         /* free search data */
833         for(a=0; a<SEARCH_ITEMS; a++)
834                 MEM_freeN(data->items.names[a]);
835         MEM_freeN(data->items.names);
836         MEM_freeN(data->items.pointers);
837         MEM_freeN(data->items.icons);
838         
839         MEM_freeN(data);
840         ar->regiondata= NULL;
841 }
842
843 static void ui_block_position(wmWindow *window, ARegion *butregion, uiBut *but, uiBlock *block);
844
845 ARegion *ui_searchbox_create(bContext *C, ARegion *butregion, uiBut *but)
846 {
847         uiStyle *style= U.uistyles.first;       // XXX pass on as arg
848         static ARegionType type;
849         ARegion *ar;
850         uiSearchboxData *data;
851         float aspect= but->block->aspect;
852         float x1f, x2f, y1f, y2f;
853         int x1, x2, y1, y2, winx, winy, ofsx, ofsy;
854         
855         /* create area region */
856         ar= ui_add_temporary_region(CTX_wm_screen(C));
857         
858         memset(&type, 0, sizeof(ARegionType));
859         type.draw= ui_searchbox_region_draw;
860         type.free= ui_searchbox_region_free;
861         ar->type= &type;
862         
863         /* create searchbox data */
864         data= MEM_callocN(sizeof(uiSearchboxData), "uiSearchboxData");
865
866         /* set font, get bb */
867         data->fstyle= style->widget; /* copy struct */
868         data->fstyle.align= UI_STYLE_TEXT_CENTER;
869         ui_fontscale(&data->fstyle.points, aspect);
870         uiStyleFontSet(&data->fstyle);
871         
872         ar->regiondata= data;
873         
874         /* special case, hardcoded feature, not draw backdrop when called from menus,
875            assume for design that popup already added it */
876         if(but->block->flag & UI_BLOCK_LOOP)
877                 data->noback= 1;
878         
879         /* compute position */
880         
881         if(but->block->flag & UI_BLOCK_LOOP) {
882                 /* this case is search menu inside other menu */
883                 /* we copy region size */
884
885                 ar->winrct= butregion->winrct;
886                 
887                 /* widget rect, in region coords */
888                 data->bbox.xmin= MENU_SHADOW_SIDE;
889                 data->bbox.xmax= (ar->winrct.xmax-ar->winrct.xmin) - MENU_SHADOW_SIDE;
890                 data->bbox.ymin= MENU_SHADOW_BOTTOM;
891                 data->bbox.ymax= (ar->winrct.ymax-ar->winrct.ymin) - MENU_SHADOW_BOTTOM;
892                 
893                 /* check if button is lower half */
894                 if( but->y2 < (but->block->minx+but->block->maxx)/2 ) {
895                         data->bbox.ymin += (but->y2-but->y1);
896                 }
897                 else {
898                         data->bbox.ymax -= (but->y2-but->y1);
899                 }
900         }
901         else {
902                 x1f= but->x1 - 5;       /* align text with button */
903                 x2f= but->x2 + 5;       /* symmetrical */
904                 y2f= but->y1;
905                 y1f= y2f - uiSearchBoxhHeight();
906
907                 ofsx= (but->block->panel)? but->block->panel->ofsx: 0;
908                 ofsy= (but->block->panel)? but->block->panel->ofsy: 0;
909
910                 x1f += ofsx;
911                 x2f += ofsx;
912                 y1f += ofsy;
913                 y2f += ofsy;
914         
915                 /* minimal width */
916                 if(x2f - x1f < 150) x2f= x1f+150; // XXX arbitrary
917                 
918                 /* copy to int, gets projected if possible too */
919                 x1= x1f; y1= y1f; x2= x2f; y2= y2f; 
920                 
921                 if(butregion) {
922                         if(butregion->v2d.cur.xmin != butregion->v2d.cur.xmax) {
923                                 UI_view2d_to_region_no_clip(&butregion->v2d, x1f, y1f, &x1, &y1);
924                                 UI_view2d_to_region_no_clip(&butregion->v2d, x2f, y2f, &x2, &y2);
925                         }
926                         
927                         x1 += butregion->winrct.xmin;
928                         x2 += butregion->winrct.xmin;
929                         y1 += butregion->winrct.ymin;
930                         y2 += butregion->winrct.ymin;
931                 }
932                 
933                 wm_window_get_size(CTX_wm_window(C), &winx, &winy);
934                 
935                 if(x2 > winx) {
936                         /* super size */
937                         if(x2 > winx + x1) {
938                                 x2= winx;
939                                 x1= 0;
940                         }
941                         else {
942                                 x1 -= x2-winx;
943                                 x2= winx;
944                         }
945                 }
946                 if(y1 < 0) {
947                         int newy1;
948                         UI_view2d_to_region_no_clip(&butregion->v2d, 0, but->y2 + ofsy, 0, &newy1);
949                         newy1 += butregion->winrct.ymin;
950
951                         y2= y2-y1 + newy1;
952                         y1= newy1;
953                 }
954
955                 /* widget rect, in region coords */
956                 data->bbox.xmin= MENU_SHADOW_SIDE;
957                 data->bbox.xmax= x2-x1 + MENU_SHADOW_SIDE;
958                 data->bbox.ymin= MENU_SHADOW_BOTTOM;
959                 data->bbox.ymax= y2-y1 + MENU_SHADOW_BOTTOM;
960                 
961                 /* region bigger for shadow */
962                 ar->winrct.xmin= x1 - MENU_SHADOW_SIDE;
963                 ar->winrct.xmax= x2 + MENU_SHADOW_SIDE;
964                 ar->winrct.ymin= y1 - MENU_SHADOW_BOTTOM;
965                 ar->winrct.ymax= y2;
966         }
967         
968         /* adds subwindow */
969         ED_region_init(C, ar);
970         
971         /* notify change and redraw */
972         ED_region_tag_redraw(ar);
973         
974         /* prepare search data */
975         data->items.maxitem= SEARCH_ITEMS;
976         data->items.maxstrlen= but->hardmax;
977         data->items.totitem= 0;
978         data->items.names= MEM_callocN(SEARCH_ITEMS*sizeof(void *), "search names");
979         data->items.pointers= MEM_callocN(SEARCH_ITEMS*sizeof(void *), "search pointers");
980         data->items.icons= MEM_callocN(SEARCH_ITEMS*sizeof(int), "search icons");
981         for(x1=0; x1<SEARCH_ITEMS; x1++)
982                 data->items.names[x1]= MEM_callocN(but->hardmax+1, "search pointers");
983         
984         return ar;
985 }
986
987 void ui_searchbox_free(bContext *C, ARegion *ar)
988 {
989         ui_remove_temporary_region(C, CTX_wm_screen(C), ar);
990 }
991
992
993 /************************* Creating Menu Blocks **********************/
994
995 /* position block relative to but, result is in window space */
996 static void ui_block_position(wmWindow *window, ARegion *butregion, uiBut *but, uiBlock *block)
997 {
998         uiBut *bt;
999         uiSafetyRct *saferct;
1000         rctf butrct;
1001         float aspect;
1002         int xsize, ysize, xof=0, yof=0, center;
1003         short dir1= 0, dir2=0;
1004         
1005         /* transform to window coordinates, using the source button region/block */
1006         butrct.xmin= but->x1; butrct.xmax= but->x2;
1007         butrct.ymin= but->y1; butrct.ymax= but->y2;
1008
1009         ui_block_to_window_fl(butregion, but->block, &butrct.xmin, &butrct.ymin);
1010         ui_block_to_window_fl(butregion, but->block, &butrct.xmax, &butrct.ymax);
1011
1012         /* calc block rect */
1013         if(block->minx == 0.0f && block->maxx == 0.0f) {
1014                 if(block->buttons.first) {
1015                         block->minx= block->miny= 10000;
1016                         block->maxx= block->maxy= -10000;
1017                         
1018                         bt= block->buttons.first;
1019                         while(bt) {
1020                                 if(bt->x1 < block->minx) block->minx= bt->x1;
1021                                 if(bt->y1 < block->miny) block->miny= bt->y1;
1022
1023                                 if(bt->x2 > block->maxx) block->maxx= bt->x2;
1024                                 if(bt->y2 > block->maxy) block->maxy= bt->y2;
1025                                 
1026                                 bt= bt->next;
1027                         }
1028                 }
1029                 else {
1030                         /* we're nice and allow empty blocks too */
1031                         block->minx= block->miny= 0;
1032                         block->maxx= block->maxy= 20;
1033                 }
1034         }
1035         
1036         aspect= (float)(block->maxx - block->minx + 4);
1037         ui_block_to_window_fl(butregion, but->block, &block->minx, &block->miny);
1038         ui_block_to_window_fl(butregion, but->block, &block->maxx, &block->maxy);
1039
1040         //block->minx-= 2.0; block->miny-= 2.0;
1041         //block->maxx+= 2.0; block->maxy+= 2.0;
1042         
1043         xsize= block->maxx - block->minx+4; // 4 for shadow
1044         ysize= block->maxy - block->miny+4;
1045         aspect/= (float)xsize;
1046
1047         if(but) {
1048                 int left=0, right=0, top=0, down=0;
1049                 int winx, winy;
1050
1051                 wm_window_get_size(window, &winx, &winy);
1052
1053                 if(block->direction & UI_CENTER) center= ysize/2;
1054                 else center= 0;
1055
1056                 if( butrct.xmin-xsize > 0.0) left= 1;
1057                 if( butrct.xmax+xsize < winx) right= 1;
1058                 if( butrct.ymin-ysize+center > 0.0) down= 1;
1059                 if( butrct.ymax+ysize-center < winy) top= 1;
1060                 
1061                 dir1= block->direction & UI_DIRECTION;
1062
1063                 /* secundary directions */
1064                 if(dir1 & (UI_TOP|UI_DOWN)) {
1065                         if(dir1 & UI_LEFT) dir2= UI_LEFT;
1066                         else if(dir1 & UI_RIGHT) dir2= UI_RIGHT;
1067                         dir1 &= (UI_TOP|UI_DOWN);
1068                 }
1069
1070                 if(dir2==0) if(dir1==UI_LEFT || dir1==UI_RIGHT) dir2= UI_DOWN;
1071                 if(dir2==0) if(dir1==UI_TOP || dir1==UI_DOWN) dir2= UI_LEFT;
1072                 
1073                 /* no space at all? dont change */
1074                 if(left || right) {
1075                         if(dir1==UI_LEFT && left==0) dir1= UI_RIGHT;
1076                         if(dir1==UI_RIGHT && right==0) dir1= UI_LEFT;
1077                         /* this is aligning, not append! */
1078                         if(dir2==UI_LEFT && right==0) dir2= UI_RIGHT;
1079                         if(dir2==UI_RIGHT && left==0) dir2= UI_LEFT;
1080                 }
1081                 if(down || top) {
1082                         if(dir1==UI_TOP && top==0) dir1= UI_DOWN;
1083                         if(dir1==UI_DOWN && down==0) dir1= UI_TOP;
1084                         if(dir2==UI_TOP && top==0) dir2= UI_DOWN;
1085                         if(dir2==UI_DOWN && down==0) dir2= UI_TOP;
1086                 }
1087                 
1088                 if(dir1==UI_LEFT) {
1089                         xof= butrct.xmin - block->maxx;
1090                         if(dir2==UI_TOP) yof= butrct.ymin - block->miny-center;
1091                         else yof= butrct.ymax - block->maxy+center;
1092                 }
1093                 else if(dir1==UI_RIGHT) {
1094                         xof= butrct.xmax - block->minx;
1095                         if(dir2==UI_TOP) yof= butrct.ymin - block->miny-center;
1096                         else yof= butrct.ymax - block->maxy+center;
1097                 }
1098                 else if(dir1==UI_TOP) {
1099                         yof= butrct.ymax - block->miny;
1100                         if(dir2==UI_RIGHT) xof= butrct.xmax - block->maxx;
1101                         else xof= butrct.xmin - block->minx;
1102                         // changed direction? 
1103                         if((dir1 & block->direction)==0) {
1104                                 if(block->direction & UI_SHIFT_FLIPPED)
1105                                         xof+= dir2==UI_LEFT?25:-25;
1106                                 uiBlockFlipOrder(block);
1107                         }
1108                 }
1109                 else if(dir1==UI_DOWN) {
1110                         yof= butrct.ymin - block->maxy;
1111                         if(dir2==UI_RIGHT) xof= butrct.xmax - block->maxx;
1112                         else xof= butrct.xmin - block->minx;
1113                         // changed direction?
1114                         if((dir1 & block->direction)==0) {
1115                                 if(block->direction & UI_SHIFT_FLIPPED)
1116                                         xof+= dir2==UI_LEFT?25:-25;
1117                                 uiBlockFlipOrder(block);
1118                         }
1119                 }
1120
1121                 /* and now we handle the exception; no space below or to top */
1122                 if(top==0 && down==0) {
1123                         if(dir1==UI_LEFT || dir1==UI_RIGHT) {
1124                                 // align with bottom of screen 
1125                                 yof= ysize;
1126                         }
1127                 }
1128                 
1129                 /* or no space left or right */
1130                 if(left==0 && right==0) {
1131                         if(dir1==UI_TOP || dir1==UI_DOWN) {
1132                                 // align with left size of screen 
1133                                 xof= -block->minx+5;
1134                         }
1135                 }
1136                 
1137                 // apply requested offset in the block
1138                 xof += block->xofs/block->aspect;
1139                 yof += block->yofs/block->aspect;
1140         }
1141         
1142         /* apply */
1143         
1144         for(bt= block->buttons.first; bt; bt= bt->next) {
1145                 ui_block_to_window_fl(butregion, but->block, &bt->x1, &bt->y1);
1146                 ui_block_to_window_fl(butregion, but->block, &bt->x2, &bt->y2);
1147
1148                 bt->x1 += xof;
1149                 bt->x2 += xof;
1150                 bt->y1 += yof;
1151                 bt->y2 += yof;
1152
1153                 bt->aspect= 1.0;
1154                 // ui_check_but recalculates drawstring size in pixels
1155                 ui_check_but(bt);
1156         }
1157         
1158         block->minx += xof;
1159         block->miny += yof;
1160         block->maxx += xof;
1161         block->maxy += yof;
1162
1163         /* safety calculus */
1164         if(but) {
1165                 float midx= (butrct.xmin+butrct.xmax)/2.0;
1166                 float midy= (butrct.ymin+butrct.ymax)/2.0;
1167                 
1168                 /* when you are outside parent button, safety there should be smaller */
1169                 
1170                 // parent button to left
1171                 if( midx < block->minx ) block->safety.xmin= block->minx-3; 
1172                 else block->safety.xmin= block->minx-40;
1173                 // parent button to right
1174                 if( midx > block->maxx ) block->safety.xmax= block->maxx+3; 
1175                 else block->safety.xmax= block->maxx+40;
1176                 
1177                 // parent button on bottom
1178                 if( midy < block->miny ) block->safety.ymin= block->miny-3; 
1179                 else block->safety.ymin= block->miny-40;
1180                 // parent button on top
1181                 if( midy > block->maxy ) block->safety.ymax= block->maxy+3; 
1182                 else block->safety.ymax= block->maxy+40;
1183                 
1184                 // exception for switched pulldowns...
1185                 if(dir1 && (dir1 & block->direction)==0) {
1186                         if(dir2==UI_RIGHT) block->safety.xmax= block->maxx+3; 
1187                         if(dir2==UI_LEFT) block->safety.xmin= block->minx-3; 
1188                 }
1189                 block->direction= dir1;
1190         }
1191         else {
1192                 block->safety.xmin= block->minx-40;
1193                 block->safety.ymin= block->miny-40;
1194                 block->safety.xmax= block->maxx+40;
1195                 block->safety.ymax= block->maxy+40;
1196         }
1197
1198         /* keep a list of these, needed for pulldown menus */
1199         saferct= MEM_callocN(sizeof(uiSafetyRct), "uiSafetyRct");
1200         saferct->parent= butrct;
1201         saferct->safety= block->safety;
1202         BLI_freelistN(&block->saferct);
1203         if(but)
1204                 BLI_duplicatelist(&block->saferct, &but->block->saferct);
1205         BLI_addhead(&block->saferct, saferct);
1206 }
1207
1208 static void ui_block_region_draw(const bContext *C, ARegion *ar)
1209 {
1210         uiBlock *block;
1211
1212         for(block=ar->uiblocks.first; block; block=block->next)
1213                 uiDrawBlock(C, block);
1214 }
1215
1216 uiPopupBlockHandle *ui_popup_block_create(bContext *C, ARegion *butregion, uiBut *but, uiBlockCreateFunc create_func, uiBlockHandleCreateFunc handle_create_func, void *arg)
1217 {
1218         wmWindow *window= CTX_wm_window(C);
1219         static ARegionType type;
1220         ARegion *ar;
1221         uiBlock *block;
1222         uiBut *bt;
1223         uiPopupBlockHandle *handle;
1224         uiSafetyRct *saferct;
1225
1226         /* create handle */
1227         handle= MEM_callocN(sizeof(uiPopupBlockHandle), "uiPopupBlockHandle");
1228
1229         /* store context for operator */
1230         handle->ctx_area= CTX_wm_area(C);
1231         handle->ctx_region= CTX_wm_region(C);
1232         
1233         /* create area region */
1234         ar= ui_add_temporary_region(CTX_wm_screen(C));
1235         handle->region= ar;
1236
1237         memset(&type, 0, sizeof(ARegionType));
1238         type.draw= ui_block_region_draw;
1239         ar->type= &type;
1240
1241         UI_add_region_handlers(&ar->handlers);
1242
1243         /* create ui block */
1244         if(create_func)
1245                 block= create_func(C, handle->region, arg);
1246         else
1247                 block= handle_create_func(C, handle, arg);
1248         
1249         if(block->handle) {
1250                 memcpy(block->handle, handle, sizeof(uiPopupBlockHandle));
1251                 MEM_freeN(handle);
1252                 handle= block->handle;
1253         }
1254         else
1255                 block->handle= handle;
1256
1257         ar->regiondata= handle;
1258
1259         if(!block->endblock)
1260                 uiEndBlock(C, block);
1261
1262         /* if this is being created from a button */
1263         if(but) {
1264                 if(ELEM(but->type, BLOCK, PULLDOWN))
1265                         block->xofs = -2;       /* for proper alignment */
1266
1267                 /* only used for automatic toolbox, so can set the shift flag */
1268                 if(but->flag & UI_MAKE_TOP) {
1269                         block->direction= UI_TOP|UI_SHIFT_FLIPPED;
1270                         uiBlockFlipOrder(block);
1271                 }
1272                 if(but->flag & UI_MAKE_DOWN) block->direction= UI_DOWN|UI_SHIFT_FLIPPED;
1273                 if(but->flag & UI_MAKE_LEFT) block->direction |= UI_LEFT;
1274                 if(but->flag & UI_MAKE_RIGHT) block->direction |= UI_RIGHT;
1275
1276                 ui_block_position(window, butregion, but, block);
1277         }
1278         else {
1279                 /* keep a list of these, needed for pulldown menus */
1280                 saferct= MEM_callocN(sizeof(uiSafetyRct), "uiSafetyRct");
1281                 saferct->safety= block->safety;
1282                 BLI_addhead(&block->saferct, saferct);
1283                 block->flag |= UI_BLOCK_POPUP;
1284         }
1285
1286         /* the block and buttons were positioned in window space as in 2.4x, now
1287          * these menu blocks are regions so we bring it back to region space.
1288          * additionally we add some padding for the menu shadow or rounded menus */
1289         ar->winrct.xmin= block->minx - MENU_SHADOW_SIDE;
1290         ar->winrct.xmax= block->maxx + MENU_SHADOW_SIDE;
1291         ar->winrct.ymin= block->miny - MENU_SHADOW_BOTTOM;
1292         ar->winrct.ymax= block->maxy + MENU_TOP;
1293
1294         block->minx -= ar->winrct.xmin;
1295         block->maxx -= ar->winrct.xmin;
1296         block->miny -= ar->winrct.ymin;
1297         block->maxy -= ar->winrct.ymin;
1298
1299         for(bt= block->buttons.first; bt; bt= bt->next) {
1300                 bt->x1 -= ar->winrct.xmin;
1301                 bt->x2 -= ar->winrct.xmin;
1302                 bt->y1 -= ar->winrct.ymin;
1303                 bt->y2 -= ar->winrct.ymin;
1304         }
1305
1306         block->flag |= UI_BLOCK_LOOP|UI_BLOCK_MOVEMOUSE_QUIT;
1307
1308         /* adds subwindow */
1309         ED_region_init(C, ar);
1310
1311         /* get winmat now that we actually have the subwindow */
1312         wmSubWindowSet(window, ar->swinid);
1313         
1314         wm_subwindow_getmatrix(window, ar->swinid, block->winmat);
1315         
1316         /* notify change and redraw */
1317         ED_region_tag_redraw(ar);
1318
1319         return handle;
1320 }
1321
1322 void ui_popup_block_free(bContext *C, uiPopupBlockHandle *handle)
1323 {
1324         ui_remove_temporary_region(C, CTX_wm_screen(C), handle->region);
1325         MEM_freeN(handle);
1326 }
1327
1328 /***************************** Menu Button ***************************/
1329
1330 static void ui_block_func_MENUSTR(bContext *C, uiLayout *layout, void *arg_str)
1331 {
1332         uiBlock *block= uiLayoutGetBlock(layout);
1333         uiPopupBlockHandle *handle= block->handle;
1334         uiLayout *split, *column=NULL;
1335         uiBut *bt;
1336         MenuData *md;
1337         MenuEntry *entry;
1338         char *instr= arg_str;
1339         int columns, rows, a, b;
1340
1341         /* compute menu data */
1342         md= decompose_menu_string(instr);
1343
1344         /* columns and row estimation */
1345         columns= (md->nitems+20)/20;
1346         if(columns<1)
1347                 columns= 1;
1348         if(columns>8)
1349                 columns= (md->nitems+25)/25;
1350         
1351         rows= md->nitems/columns;
1352         if(rows<1)
1353                 rows= 1;
1354         while(rows*columns<md->nitems)
1355                 rows++;
1356
1357         /* create title */
1358         if(md->title) {
1359                 if(md->titleicon) {
1360                         uiItemL(layout, md->title, md->titleicon);
1361                 }
1362                 else {
1363                         uiItemL(layout, md->title, 0);
1364                         bt= block->buttons.last;
1365                         bt->flag= UI_TEXT_LEFT;
1366                 }
1367         }
1368
1369         /* inconsistent, but menus with labels do not look good flipped */
1370         for(a=0, b=0; a<md->nitems; a++, b++) {
1371                 entry= &md->items[a];
1372
1373                 if(entry->sepr && entry->str[0])
1374                         block->flag |= UI_BLOCK_NO_FLIP;
1375         }
1376
1377         /* create items */
1378         split= uiLayoutSplit(layout, 0);
1379
1380         for(a=0, b=0; a<md->nitems; a++, b++) {
1381                 if(block->flag & UI_BLOCK_NO_FLIP)
1382                         entry= &md->items[a];
1383                 else
1384                         entry= &md->items[md->nitems-a-1];
1385                 
1386                 /* new column on N rows or on separation label */
1387                 if((b % rows == 0) || (entry->sepr && entry->str[0])) {
1388                         column= uiLayoutColumn(split, 0);
1389                         b= 0;
1390                 }
1391
1392                 if(entry->sepr) {
1393                         uiItemL(column, entry->str, entry->icon);
1394                         bt= block->buttons.last;
1395                         bt->flag= UI_TEXT_LEFT;
1396                 }
1397                 else if(entry->icon) {
1398                         uiDefIconTextButF(block, BUTM|FLO, B_NOP, entry->icon, entry->str, 0, 0,
1399                                 UI_UNIT_X*5, UI_UNIT_Y, &handle->retvalue, (float) entry->retval, 0.0, 0, 0, "");
1400                 }
1401                 else {
1402                         uiDefButF(block, BUTM|FLO, B_NOP, entry->str, 0, 0,
1403                                 UI_UNIT_X*5, UI_UNIT_X, &handle->retvalue, (float) entry->retval, 0.0, 0, 0, "");
1404                 }
1405         }
1406         
1407         menudata_free(md);
1408 }
1409
1410 void ui_block_func_ICONROW(bContext *C, uiLayout *layout, void *arg_but)
1411 {
1412         uiBlock *block= uiLayoutGetBlock(layout);
1413         uiPopupBlockHandle *handle= block->handle;
1414         uiBut *but= arg_but;
1415         int a;
1416         
1417         for(a=(int)but->hardmin; a<=(int)but->hardmax; a++)
1418                 uiDefIconButF(block, BUTM|FLO, B_NOP, but->icon+(a-but->hardmin), 0, 0, UI_UNIT_X*5, UI_UNIT_Y,
1419                         &handle->retvalue, (float)a, 0.0, 0, 0, "");
1420 }
1421
1422 void ui_block_func_ICONTEXTROW(bContext *C, uiLayout *layout, void *arg_but)
1423 {
1424         uiBlock *block= uiLayoutGetBlock(layout);
1425         uiPopupBlockHandle *handle= block->handle;
1426         uiBut *but= arg_but, *bt;
1427         MenuData *md;
1428         MenuEntry *entry;
1429         int a;
1430
1431         md= decompose_menu_string(but->str);
1432
1433         /* title */
1434         if(md->title) {
1435                 bt= uiDefBut(block, LABEL, 0, md->title, 0, 0, UI_UNIT_X*5, UI_UNIT_Y, NULL, 0.0, 0.0, 0, 0, "");
1436                 bt->flag= UI_TEXT_LEFT;
1437         }
1438
1439         /* loop through the menu options and draw them out with icons & text labels */
1440         for(a=0; a<md->nitems; a++) {
1441                 entry= &md->items[md->nitems-a-1];
1442
1443                 if(entry->sepr)
1444                         uiItemS(layout);
1445                 else
1446                         uiDefIconTextButF(block, BUTM|FLO, B_NOP, (short)((but->icon)+(entry->retval-but->hardmin)), entry->str,
1447                                 0, 0, UI_UNIT_X*5, UI_UNIT_Y, &handle->retvalue, (float) entry->retval, 0.0, 0, 0, "");
1448         }
1449
1450         menudata_free(md);
1451 }
1452
1453 #if 0
1454 static void ui_warp_pointer(short x, short y)
1455 {
1456         /* XXX 2.50 which function to use for this? */
1457         /* OSX has very poor mousewarp support, it sends events;
1458            this causes a menu being pressed immediately ... */
1459         #ifndef __APPLE__
1460         warp_pointer(x, y);
1461         #endif
1462 }
1463 #endif
1464
1465 /********************* Color Button ****************/
1466
1467 /* picker sizes S hsize, F full size, D spacer, B button/pallette height  */
1468 #define SPICK   110.0
1469 #define FPICK   180.0
1470 #define DPICK   6.0
1471 #define BPICK   24.0
1472
1473 #define UI_PALETTE_TOT 16
1474 /* note; in tot+1 the old color is stored */
1475 static float palette[UI_PALETTE_TOT+1][3]= {
1476 {0.93, 0.83, 0.81}, {0.88, 0.89, 0.73}, {0.69, 0.81, 0.57}, {0.51, 0.76, 0.64}, 
1477 {0.37, 0.56, 0.61}, {0.33, 0.29, 0.55}, {0.46, 0.21, 0.51}, {0.40, 0.12, 0.18}, 
1478 {1.0, 1.0, 1.0}, {0.85, 0.85, 0.85}, {0.7, 0.7, 0.7}, {0.56, 0.56, 0.56}, 
1479 {0.42, 0.42, 0.42}, {0.28, 0.28, 0.28}, {0.14, 0.14, 0.14}, {0.0, 0.0, 0.0}
1480 };  
1481
1482 /* for picker, while editing hsv */
1483 void ui_set_but_hsv(uiBut *but)
1484 {
1485         float col[3];
1486         
1487         hsv_to_rgb(but->hsv[0], but->hsv[1], but->hsv[2], col, col+1, col+2);
1488         ui_set_but_vectorf(but, col);
1489 }
1490
1491 static void update_picker_hex(uiBlock *block, float *rgb)
1492 {
1493         uiBut *bt;
1494         char col[16];
1495         
1496         sprintf(col, "%02X%02X%02X", (unsigned int)(rgb[0]*255.0), (unsigned int)(rgb[1]*255.0), (unsigned int)(rgb[2]*255.0));
1497         
1498         // this updates button strings, is hackish... but button pointers are on stack of caller function
1499
1500         for(bt= block->buttons.first; bt; bt= bt->next) {
1501                 if(strcmp(bt->str, "Hex: ")==0)
1502                         strcpy(bt->poin, col);
1503
1504                 ui_check_but(bt);
1505         }
1506 }
1507
1508 /* also used by small picker, be careful with name checks below... */
1509 void ui_update_block_buts_hsv(uiBlock *block, float *hsv)
1510 {
1511         uiBut *bt;
1512         float r, g, b;
1513         float rgb[3];
1514         
1515         // this updates button strings, is hackish... but button pointers are on stack of caller function
1516         hsv_to_rgb(hsv[0], hsv[1], hsv[2], &r, &g, &b);
1517         
1518         rgb[0] = r; rgb[1] = g; rgb[2] = b;
1519         update_picker_hex(block, rgb);
1520
1521         for(bt= block->buttons.first; bt; bt= bt->next) {
1522                 if(ELEM(bt->type, HSVCUBE, HSVCIRCLE)) {
1523                         VECCOPY(bt->hsv, hsv);
1524                         ui_set_but_hsv(bt);
1525                 }
1526                 else if(bt->str[1]==' ') {
1527                         if(bt->str[0]=='R') {
1528                                 ui_set_but_val(bt, r);
1529                         }
1530                         else if(bt->str[0]=='G') {
1531                                 ui_set_but_val(bt, g);
1532                         }
1533                         else if(bt->str[0]=='B') {
1534                                 ui_set_but_val(bt, b);
1535                         }
1536                         else if(bt->str[0]=='H') {
1537                                 ui_set_but_val(bt, hsv[0]);
1538                         }
1539                         else if(bt->str[0]=='S') {
1540                                 ui_set_but_val(bt, hsv[1]);
1541                         }
1542                         else if(bt->str[0]=='V') {
1543                                 ui_set_but_val(bt, hsv[2]);
1544                         }
1545                 }               
1546
1547                 ui_check_but(bt);
1548         }
1549 }
1550
1551 static void ui_update_block_buts_hex(uiBlock *block, char *hexcol)
1552 {
1553         uiBut *bt;
1554         float r=0, g=0, b=0;
1555         float h, s, v;
1556         
1557         
1558         // this updates button strings, is hackish... but button pointers are on stack of caller function
1559         hex_to_rgb(hexcol, &r, &g, &b);
1560         rgb_to_hsv(r, g, b, &h, &s, &v);
1561
1562         for(bt= block->buttons.first; bt; bt= bt->next) {
1563                 if(bt->type==HSVCUBE) {
1564                         bt->hsv[0] = h;
1565                         bt->hsv[1] = s;                 
1566                         bt->hsv[2] = v;
1567                         ui_set_but_hsv(bt);
1568                 }
1569                 else if(bt->str[1]==' ') {
1570                         if(bt->str[0]=='R') {
1571                                 ui_set_but_val(bt, r);
1572                         }
1573                         else if(bt->str[0]=='G') {
1574                                 ui_set_but_val(bt, g);
1575                         }
1576                         else if(bt->str[0]=='B') {
1577                                 ui_set_but_val(bt, b);
1578                         }
1579                         else if(bt->str[0]=='H') {
1580                                 ui_set_but_val(bt, h);
1581                         }
1582                         else if(bt->str[0]=='S') {
1583                                 ui_set_but_val(bt, s);
1584                         }
1585                         else if(bt->str[0]=='V') {
1586                                 ui_set_but_val(bt, v);
1587                         }
1588                 }
1589
1590                 ui_check_but(bt);
1591         }
1592 }
1593
1594 /* bt1 is palette but, col1 is original color */
1595 /* callback to copy from/to palette */
1596 static void do_palette_cb(bContext *C, void *bt1, void *col1)
1597 {
1598         wmWindow *win= CTX_wm_window(C);
1599         uiBut *but1= (uiBut *)bt1;
1600         uiPopupBlockHandle *popup= but1->block->handle;
1601         float *col= (float *)col1;
1602         float *fp, hsv[3];
1603         
1604         fp= (float *)but1->poin;
1605         
1606         if(win->eventstate->ctrl) {
1607                 VECCOPY(fp, col);
1608         }
1609         else {
1610                 VECCOPY(col, fp);
1611         }
1612         
1613         rgb_to_hsv(col[0], col[1], col[2], hsv, hsv+1, hsv+2);
1614         ui_update_block_buts_hsv(but1->block, hsv);
1615         update_picker_hex(but1->block, col);
1616
1617         if(popup)
1618                 popup->menuretval= UI_RETURN_UPDATE;
1619 }
1620
1621 static void do_hsv_cb(bContext *C, void *bt1, void *unused)
1622 {
1623         uiBut *but1= (uiBut *)bt1;
1624         uiPopupBlockHandle *popup= but1->block->handle;
1625
1626         if(popup)
1627                 popup->menuretval= UI_RETURN_UPDATE;
1628 }
1629
1630 /* bt1 is num but, hsv1 is pointer to original color in hsv space*/
1631 /* callback to handle changes in num-buts in picker */
1632 static void do_palette1_cb(bContext *C, void *bt1, void *hsv1)
1633 {
1634         uiBut *but1= (uiBut *)bt1;
1635         uiPopupBlockHandle *popup= but1->block->handle;
1636         float *hsv= (float *)hsv1;
1637         float *fp= NULL;
1638         
1639         if(but1->str[1]==' ') {
1640                 if(but1->str[0]=='R') fp= (float *)but1->poin;
1641                 else if(but1->str[0]=='G') fp= ((float *)but1->poin)-1;
1642                 else if(but1->str[0]=='B') fp= ((float *)but1->poin)-2;
1643         }
1644         if(fp) {
1645                 rgb_to_hsv(fp[0], fp[1], fp[2], hsv, hsv+1, hsv+2);
1646         } 
1647         ui_update_block_buts_hsv(but1->block, hsv);
1648
1649         if(popup)
1650                 popup->menuretval= UI_RETURN_UPDATE;
1651 }
1652
1653 /* bt1 is num but, col1 is pointer to original color */
1654 /* callback to handle changes in num-buts in picker */
1655 static void do_palette2_cb(bContext *C, void *bt1, void *col1)
1656 {
1657         uiBut *but1= (uiBut *)bt1;
1658         uiPopupBlockHandle *popup= but1->block->handle;
1659         float *rgb= (float *)col1;
1660         float *fp= NULL;
1661         
1662         if(but1->str[1]==' ') {
1663                 if(but1->str[0]=='H') fp= (float *)but1->poin;
1664                 else if(but1->str[0]=='S') fp= ((float *)but1->poin)-1;
1665                 else if(but1->str[0]=='V') fp= ((float *)but1->poin)-2;
1666         }
1667         if(fp) {
1668                 hsv_to_rgb(fp[0], fp[1], fp[2], rgb, rgb+1, rgb+2);
1669         } 
1670         ui_update_block_buts_hsv(but1->block, fp);
1671
1672         if(popup)
1673                 popup->menuretval= UI_RETURN_UPDATE;
1674 }
1675
1676 static void do_palette_hex_cb(bContext *C, void *bt1, void *hexcl)
1677 {
1678         uiBut *but1= (uiBut *)bt1;
1679         uiPopupBlockHandle *popup= but1->block->handle;
1680         char *hexcol= (char *)hexcl;
1681         
1682         ui_update_block_buts_hex(but1->block, hexcol);  
1683
1684         if(popup)
1685                 popup->menuretval= UI_RETURN_UPDATE;
1686 }
1687
1688 /* used for both 3d view and image window */
1689 static void do_palette_sample_cb(bContext *C, void *bt1, void *col1)    /* frontbuf */
1690 {
1691         /* XXX 2.50 this should become an operator? */
1692 #if 0
1693         uiBut *but1= (uiBut *)bt1;
1694         uiBut *but;
1695         float tempcol[4];
1696         int x=0, y=0;
1697         short mval[2];
1698         float hsv[3];
1699         short capturing;
1700         int oldcursor;
1701         Window *win;
1702         unsigned short dev;
1703         
1704         oldcursor=get_cursor();
1705         win=winlay_get_active_window();
1706         
1707         while (get_mbut() & L_MOUSE) UI_wait_for_statechange();
1708         
1709         SetBlenderCursor(BC_EYEDROPPER_CURSOR);
1710         
1711         /* loop and wait for a mouse click */
1712         capturing = TRUE;
1713         while(capturing) {
1714                 char ascii;
1715                 short val;
1716                 
1717                 dev = extern_qread_ext(&val, &ascii);
1718                 
1719                 if(dev==INPUTCHANGE) break;
1720                 if(get_mbut() & R_MOUSE) break;
1721                 else if(get_mbut() & L_MOUSE) {
1722                         uiGetMouse(mywinget(), mval);
1723                         x= mval[0]; y= mval[1];
1724                         
1725                         capturing = FALSE;
1726                         break;
1727                 }
1728                 else if(dev==ESCKEY) break;
1729         }
1730         window_set_cursor(win, oldcursor);
1731         
1732         if(capturing) return;
1733         
1734         if(x<0 || y<0) return;
1735         
1736         /* if we've got a glick, use OpenGL to sample the color under the mouse pointer */
1737         glReadBuffer(GL_FRONT);
1738         glReadPixels(x, y, 1, 1, GL_RGBA, GL_FLOAT, tempcol);
1739         glReadBuffer(GL_BACK);
1740         
1741         /* and send that color back to the picker */
1742         rgb_to_hsv(tempcol[0], tempcol[1], tempcol[2], hsv, hsv+1, hsv+2);
1743         ui_update_block_buts_hsv(but1->block, hsv);
1744         update_picker_hex(but1->block, tempcol);
1745         
1746         for (but= but1->block->buttons.first; but; but= but->next) {
1747                 ui_check_but(but);
1748                 ui_draw_but(but);
1749         }
1750         
1751         but= but1->block->buttons.first;
1752         ui_block_flush_back(but->block);
1753 #endif
1754 }
1755
1756 /* color picker, Gimp version. mode: 'f' = floating panel, 'p' =  popup */
1757 /* col = read/write to, hsv/old/hexcol = memory for temporal use */
1758 void uiBlockPickerButtons(uiBlock *block, float *col, float *hsv, float *old, char *hexcol, char mode, short retval)
1759 {
1760         uiBut *bt;
1761         float h, offs;
1762         int a;
1763
1764         VECCOPY(old, col);      // old color stored there, for palette_cb to work
1765         
1766         // the cube intersection
1767         bt= uiDefButF(block, HSVCUBE, retval, "",       0,DPICK+BPICK,FPICK,FPICK, col, 0.0, 0.0, 2, 0, "");
1768         uiButSetFunc(bt, do_hsv_cb, bt, NULL);
1769
1770         bt= uiDefButF(block, HSVCUBE, retval, "",       0,0,FPICK,BPICK, col, 0.0, 0.0, 3, 0, "");
1771         uiButSetFunc(bt, do_hsv_cb, bt, NULL);
1772
1773         // palette
1774         
1775         bt=uiDefButF(block, COL, retval, "",            FPICK+DPICK, 0, BPICK,BPICK, old, 0.0, 0.0, -1, 0, "Old color, click to restore");
1776         uiButSetFunc(bt, do_palette_cb, bt, col);
1777         uiDefButF(block, COL, retval, "",               FPICK+DPICK, BPICK+DPICK, BPICK,60-BPICK-DPICK, col, 0.0, 0.0, -1, 0, "Active color");
1778
1779         h= (DPICK+BPICK+FPICK-64)/(UI_PALETTE_TOT/2.0);
1780         uiBlockBeginAlign(block);
1781         for(a= -1+UI_PALETTE_TOT/2; a>=0; a--) {
1782                 bt= uiDefButF(block, COL, retval, "",   FPICK+DPICK, 65.0+(float)a*h, BPICK/2, h, palette[a+UI_PALETTE_TOT/2], 0.0, 0.0, -1, 0, "Click to choose, hold CTRL to store in palette");
1783                 uiButSetFunc(bt, do_palette_cb, bt, col);
1784                 bt= uiDefButF(block, COL, retval, "",   FPICK+DPICK+BPICK/2, 65.0+(float)a*h, BPICK/2, h, palette[a], 0.0, 0.0, -1, 0, "Click to choose, hold CTRL to store in palette");               
1785                 uiButSetFunc(bt, do_palette_cb, bt, col);
1786         }
1787         uiBlockEndAlign(block);
1788         
1789         // buttons
1790         rgb_to_hsv(col[0], col[1], col[2], hsv, hsv+1, hsv+2);
1791         sprintf(hexcol, "%02X%02X%02X", (unsigned int)(col[0]*255.0), (unsigned int)(col[1]*255.0), (unsigned int)(col[2]*255.0));      
1792
1793         offs= FPICK+2*DPICK+BPICK;
1794
1795         /* note; made this a TOG now, with NULL pointer. Is because BUT now gets handled with a afterfunc */
1796         bt= uiDefIconTextBut(block, TOG, UI_RETURN_OK, ICON_EYEDROPPER, "Sample", offs+55, 170, 85, 20, NULL, 0, 0, 0, 0, "Sample the color underneath the following mouse click (ESC or RMB to cancel)");
1797         uiButSetFunc(bt, do_palette_sample_cb, bt, col);
1798         uiButSetFlag(bt, UI_TEXT_LEFT);
1799         
1800         bt= uiDefBut(block, TEX, retval, "Hex: ", offs, 140, 140, 20, hexcol, 0, 8, 0, 0, "Hex triplet for color (#RRGGBB)");
1801         uiButSetFunc(bt, do_palette_hex_cb, bt, hexcol);
1802
1803         uiBlockBeginAlign(block);
1804         bt= uiDefButF(block, NUMSLI, retval, "R ",      offs, 110, 140,20, col, 0.0, 1.0, 10, 3, "");
1805         uiButSetFunc(bt, do_palette1_cb, bt, hsv);
1806         bt= uiDefButF(block, NUMSLI, retval, "G ",      offs, 90, 140,20, col+1, 0.0, 1.0, 10, 3, "");
1807         uiButSetFunc(bt, do_palette1_cb, bt, hsv);
1808         bt= uiDefButF(block, NUMSLI, retval, "B ",      offs, 70, 140,20, col+2, 0.0, 1.0, 10, 3, "");
1809         uiButSetFunc(bt, do_palette1_cb, bt, hsv);
1810         
1811         uiBlockBeginAlign(block);
1812         bt= uiDefButF(block, NUMSLI, retval, "H ",      offs, 40, 140,20, hsv, 0.0, 1.0, 10, 3, "");
1813         uiButSetFunc(bt, do_palette2_cb, bt, col);
1814         bt= uiDefButF(block, NUMSLI, retval, "S ",      offs, 20, 140,20, hsv+1, 0.0, 1.0, 10, 3, "");
1815         uiButSetFunc(bt, do_palette2_cb, bt, col);
1816         bt= uiDefButF(block, NUMSLI, retval, "V ",      offs, 0, 140,20, hsv+2, 0.0, 1.0, 10, 3, "");
1817         uiButSetFunc(bt, do_palette2_cb, bt, col);
1818         uiBlockEndAlign(block);
1819 }
1820
1821 /* bt1 is num but, hsv1 is pointer to original color in hsv space*/
1822 /* callback to handle changes */
1823 static void do_picker_small_cb(bContext *C, void *bt1, void *hsv1)
1824 {
1825         uiBut *but1= (uiBut *)bt1;
1826         uiPopupBlockHandle *popup= but1->block->handle;
1827         float *hsv= (float *)hsv1;
1828         float *fp= NULL;
1829         
1830         fp= (float *)but1->poin;
1831         rgb_to_hsv(fp[0], fp[1], fp[2], hsv, hsv+1, hsv+2);
1832
1833         ui_update_block_buts_hsv(but1->block, hsv);
1834         
1835         if(popup)
1836                 popup->menuretval= UI_RETURN_UPDATE;
1837 }
1838
1839 /* picker sizes S hsize, F full size, D spacer, B button/pallette height  */
1840 #define SPICK1  150.0
1841 #define DPICK1  6.0
1842
1843 /* only the color, a HS circle and V slider */
1844 static void uiBlockPickerSmall(uiBlock *block, float *col, float *hsv, float *old, char *hexcol, char mode, short retval)
1845 {
1846         uiBut *bt;
1847         
1848         VECCOPY(old, col);      // old color stored there, for palette_cb to work
1849         
1850         /* HS circle */
1851         bt= uiDefButF(block, HSVCIRCLE, retval, "",     0, 0,SPICK1,SPICK1, col, 0.0, 0.0, 0, 0, "");
1852         uiButSetFunc(bt, do_picker_small_cb, bt, hsv);
1853
1854         /* value */
1855         bt= uiDefButF(block, HSVCUBE, retval, "",       SPICK1+DPICK1,0,14,SPICK1, col, 0.0, 0.0, 4, 0, "");
1856         uiButSetFunc(bt, do_picker_small_cb, bt, hsv);
1857 }
1858
1859
1860 static void picker_new_hide_reveal(uiBlock *block, short colormode)
1861 {
1862         uiBut *bt;
1863         
1864         /* tag buttons */
1865         for(bt= block->buttons.first; bt; bt= bt->next) {
1866                 
1867                 if(bt->type==NUMSLI || bt->type==TEX) {
1868                         if( bt->str[1]=='e') {
1869                                 if(colormode==2) bt->flag &= ~UI_HIDDEN;
1870                                 else bt->flag |= UI_HIDDEN;
1871                         }
1872                         else if( ELEM3(bt->str[0], 'R', 'G', 'B')) {
1873                                 if(colormode==0) bt->flag &= ~UI_HIDDEN;
1874                                 else bt->flag |= UI_HIDDEN;
1875                         }
1876                         else if( ELEM3(bt->str[0], 'H', 'S', 'V')) {
1877                                 if(colormode==1) bt->flag &= ~UI_HIDDEN;
1878                                 else bt->flag |= UI_HIDDEN;
1879                         }
1880                 }
1881         }
1882 }
1883
1884 static void do_picker_new_mode_cb(bContext *C, void *bt1, void *colv)
1885 {
1886         uiBut *bt= bt1;
1887         short colormode= ui_get_but_val(bt);
1888
1889         picker_new_hide_reveal(bt->block, colormode);
1890 }
1891
1892
1893 /* a HS circle, V slider, rgb/hsv/hex sliders */
1894 static void uiBlockPickerNew(uiBlock *block, float *col, float *hsv, float *old, char *hexcol, char mode, short retval)
1895 {
1896         static short colormode= 0;      /* temp? 0=rgb, 1=hsv, 2=hex */
1897         uiBut *bt;
1898         int width;
1899         
1900         VECCOPY(old, col);      // old color stored there, for palette_cb to work
1901         
1902         /* HS circle */
1903         bt= uiDefButF(block, HSVCIRCLE, retval, "",     0, 0,SPICK1,SPICK1, col, 0.0, 0.0, 0, 0, "");
1904         uiButSetFunc(bt, do_picker_small_cb, bt, hsv);
1905         
1906         /* value */
1907         bt= uiDefButF(block, HSVCUBE, retval, "",       SPICK1+DPICK1,0,14,SPICK1, col, 0.0, 0.0, 4, 0, "");
1908         uiButSetFunc(bt, do_picker_small_cb, bt, hsv);
1909         
1910         /* mode */
1911         width= (SPICK1+DPICK1+14)/3;
1912         uiBlockBeginAlign(block);
1913         bt= uiDefButS(block, ROW, retval, "RGB",        0, -30, width, 19, &colormode, 0.0, 0.0, 0, 0, "");
1914         uiButSetFunc(bt, do_picker_new_mode_cb, bt, col);
1915         bt= uiDefButS(block, ROW, retval, "HSV",        width, -30, width, 19, &colormode, 0.0, 1.0, 0, 0, "");
1916         uiButSetFunc(bt, do_picker_new_mode_cb, bt, hsv);
1917         bt= uiDefButS(block, ROW, retval, "Hex",        2*width, -30, width, 19, &colormode, 0.0, 2.0, 0, 0, "");
1918         uiButSetFunc(bt, do_picker_new_mode_cb, bt, hexcol);
1919         uiBlockEndAlign(block);
1920         
1921         /* sliders or hex */
1922         width= (SPICK1+DPICK1+14);
1923         rgb_to_hsv(col[0], col[1], col[2], hsv, hsv+1, hsv+2);
1924         sprintf(hexcol, "%02X%02X%02X", (unsigned int)(col[0]*255.0), (unsigned int)(col[1]*255.0), (unsigned int)(col[2]*255.0));      
1925
1926         uiBlockBeginAlign(block);
1927         bt= uiDefButF(block, NUMSLI, 0, "R ",   0, -60, width, 19, col, 0.0, 1.0, 10, 3, "");
1928         uiButSetFunc(bt, do_palette1_cb, bt, hsv);
1929         bt= uiDefButF(block, NUMSLI, 0, "G ",   0, -80, width, 19, col+1, 0.0, 1.0, 10, 3, "");
1930         uiButSetFunc(bt, do_palette1_cb, bt, hsv);
1931         bt= uiDefButF(block, NUMSLI, 0, "B ",   0, -100, width, 19, col+2, 0.0, 1.0, 10, 3, "");
1932         uiButSetFunc(bt, do_palette1_cb, bt, hsv);
1933         uiBlockEndAlign(block);
1934
1935         uiBlockBeginAlign(block);
1936         bt= uiDefButF(block, NUMSLI, 0, "H ",   0, -60, width, 19, hsv, 0.0, 1.0, 10, 3, "");
1937         uiButSetFunc(bt, do_palette2_cb, bt, col);
1938         bt= uiDefButF(block, NUMSLI, 0, "S ",   0, -80, width, 19, hsv+1, 0.0, 1.0, 10, 3, "");
1939         uiButSetFunc(bt, do_palette2_cb, bt, col);
1940         bt= uiDefButF(block, NUMSLI, 0, "V ",   0, -100, width, 19, hsv+2, 0.0, 1.0, 10, 3, "");
1941         uiButSetFunc(bt, do_palette2_cb, bt, col);
1942         uiBlockEndAlign(block);
1943
1944         bt= uiDefBut(block, TEX, 0, "Hex: ", 0, -80, width, 19, hexcol, 0, 8, 0, 0, "Hex triplet for color (#RRGGBB)");
1945         uiButSetFunc(bt, do_palette_hex_cb, bt, hexcol);
1946
1947         picker_new_hide_reveal(block, colormode);
1948 }
1949
1950
1951 static int ui_picker_small_wheel(const bContext *C, uiBlock *block, wmEvent *event)
1952 {
1953         float add= 0.0f;
1954         
1955         if(event->type==WHEELUPMOUSE)
1956                 add= 0.05f;
1957         else if(event->type==WHEELDOWNMOUSE)
1958                 add= -0.05f;
1959         
1960         if(add!=0.0f) {
1961                 uiBut *but;
1962                 
1963                 for(but= block->buttons.first; but; but= but->next) {
1964                         if(but->type==HSVCUBE && but->active==NULL) {
1965                                 uiPopupBlockHandle *popup= block->handle;
1966                                 float col[3];
1967                                 
1968                                 ui_get_but_vectorf(but, col);
1969                                 
1970                                 rgb_to_hsv(col[0], col[1], col[2], but->hsv, but->hsv+1, but->hsv+2);
1971                                 but->hsv[2]= CLAMPIS(but->hsv[2]+add, 0.0f, 1.0f);
1972                                 hsv_to_rgb(but->hsv[0], but->hsv[1], but->hsv[2], col, col+1, col+2);
1973
1974                                 ui_set_but_vectorf(but, col);
1975                                 
1976                                 ui_update_block_buts_hsv(block, but->hsv);
1977                                 if(popup)
1978                                         popup->menuretval= UI_RETURN_UPDATE;
1979                                 
1980                                 return 1;
1981                         }
1982                 }
1983         }
1984         return 0;
1985 }
1986
1987 uiBlock *ui_block_func_COL(bContext *C, uiPopupBlockHandle *handle, void *arg_but)
1988 {
1989         wmWindow *win= CTX_wm_window(C); // XXX temp, needs to become keymap to detect type?
1990         uiBut *but= arg_but;
1991         uiBlock *block;
1992         static float hsvcol[3], oldcol[3];
1993         static char hexcol[128];
1994         
1995         block= uiBeginBlock(C, handle->region, "colorpicker", UI_EMBOSS);
1996         
1997         VECCOPY(handle->retvec, but->editvec);
1998         if(win->eventstate->shift) {
1999                 uiBlockPickerButtons(block, handle->retvec, hsvcol, oldcol, hexcol, 'p', 0);
2000                 block->flag= UI_BLOCK_LOOP|UI_BLOCK_REDRAW|UI_BLOCK_KEEP_OPEN;
2001                 uiBoundsBlock(block, 3);
2002         }
2003         else if(win->eventstate->alt) {
2004                 uiBlockPickerSmall(block, handle->retvec, hsvcol, oldcol, hexcol, 'p', 0);
2005                 block->flag= UI_BLOCK_LOOP|UI_BLOCK_REDRAW|UI_BLOCK_RET_1|UI_BLOCK_OUT_1;
2006                 uiBoundsBlock(block, 10);
2007                 
2008                 block->block_event_func= ui_picker_small_wheel;
2009         }
2010         else {
2011                 uiBlockPickerNew(block, handle->retvec, hsvcol, oldcol, hexcol, 'p', 0);
2012                 block->flag= UI_BLOCK_LOOP|UI_BLOCK_REDRAW|UI_BLOCK_KEEP_OPEN;
2013                 uiBoundsBlock(block, 10);
2014                 
2015                 block->block_event_func= ui_picker_small_wheel;
2016         }
2017         
2018         
2019         /* and lets go */
2020         block->direction= UI_TOP;
2021         
2022         return block;
2023 }
2024
2025 /* ******************** Color band *************** */
2026
2027 static int vergcband(const void *a1, const void *a2)
2028 {
2029         const CBData *x1=a1, *x2=a2;
2030         
2031         if( x1->pos > x2->pos ) return 1;
2032         else if( x1->pos < x2->pos) return -1;
2033         return 0;
2034 }
2035
2036 static void colorband_pos_cb(bContext *C, void *coba_v, void *unused_v)
2037 {
2038         ColorBand *coba= coba_v;
2039         int a;
2040         
2041         if(coba->tot<2) return;
2042         
2043         for(a=0; a<coba->tot; a++) coba->data[a].cur= a;
2044         qsort(coba->data, coba->tot, sizeof(CBData), vergcband);
2045         for(a=0; a<coba->tot; a++) {
2046                 if(coba->data[a].cur==coba->cur) {
2047                         /* if(coba->cur!=a) addqueue(curarea->win, REDRAW, 0); */       /* button cur */
2048                         coba->cur= a;
2049                         break;
2050                 }
2051         }
2052 }
2053
2054 static void colorband_add_cb(bContext *C, void *coba_v, void *unused_v)
2055 {
2056         ColorBand *coba= coba_v;
2057         
2058         if(coba->tot < MAXCOLORBAND-1) coba->tot++;
2059         coba->cur= coba->tot-1;
2060         
2061         colorband_pos_cb(C, coba, NULL);
2062 //      BIF_undo_push("Add colorband");
2063         
2064 }
2065
2066 static void colorband_del_cb(bContext *C, void *coba_v, void *unused_v)
2067 {
2068         ColorBand *coba= coba_v;
2069         int a;
2070         
2071         if(coba->tot<2) return;
2072         
2073         for(a=coba->cur; a<coba->tot; a++) {
2074                 coba->data[a]= coba->data[a+1];
2075         }
2076         if(coba->cur) coba->cur--;
2077         coba->tot--;
2078         
2079 //      BIF_undo_push("Delete colorband");
2080 //      BIF_preview_changed(ID_TE);
2081 }
2082
2083 void uiBlockColorbandButtons(uiBlock *block, ColorBand *coba, rctf *butr, int event)
2084 {
2085         CBData *cbd;
2086         uiBut *bt;
2087         float unit= (butr->xmax-butr->xmin)/14.0f;
2088         float xs= butr->xmin;
2089         
2090         cbd= coba->data + coba->cur;
2091         
2092         uiBlockBeginAlign(block);
2093         uiDefButF(block, COL, event,            "",                     xs,butr->ymin+20.0f,2.0f*unit,20,                               &(cbd->r), 0, 0, 0, 0, "");
2094         uiDefButF(block, NUM, event,            "A:",           xs+2.0f*unit,butr->ymin+20.0f,4.0f*unit,20,     &(cbd->a), 0.0f, 1.0f, 10, 2, "");
2095         bt= uiDefBut(block, BUT, event, "Add",          xs+6.0f*unit,butr->ymin+20.0f,2.0f*unit,20,     NULL, 0, 0, 0, 0, "Adds a new color position to the colorband");
2096         uiButSetFunc(bt, colorband_add_cb, coba, NULL);
2097         bt= uiDefBut(block, BUT, event, "Del",          xs+8.0f*unit, butr->ymin+20.0f, 2.0f*unit, 20,  NULL, 0, 0, 0, 0, "Deletes the active position");
2098         uiButSetFunc(bt, colorband_del_cb, coba, NULL);
2099         
2100         uiDefButS(block, MENU, event,           "Interpolation %t|Ease %x1|Cardinal %x3|Linear %x0|B-Spline %x2|Constant %x4",
2101                           xs + 10.0f*unit, butr->ymin+20.0f, unit*4.0f, 20,             &coba->ipotype, 0.0, 0.0, 0, 0, "Sets interpolation type");
2102         
2103         uiDefBut(block, BUT_COLORBAND, event, "",               xs, butr->ymin, butr->xmax-butr->xmin, 20.0f, coba, 0, 0, 0, 0, "");
2104         uiBlockEndAlign(block);
2105         
2106 }
2107
2108
2109 /************************ Popup Menu Memory ****************************/
2110
2111 static int ui_popup_menu_hash(char *str)
2112 {
2113         return BLI_ghashutil_strhash(str);
2114 }
2115
2116 /* but == NULL read, otherwise set */
2117 uiBut *ui_popup_menu_memory(uiBlock *block, uiBut *but)
2118 {
2119         static char mem[256], first=1;
2120         int hash= block->puphash;
2121         
2122         if(first) {
2123                 /* init */
2124                 memset(mem, -1, sizeof(mem));
2125                 first= 0;
2126         }
2127
2128         if(but) {
2129                 /* set */
2130                 mem[hash & 255 ]= BLI_findindex(&block->buttons, but);
2131                 return NULL;
2132         }
2133         else {
2134                 /* get */
2135                 return BLI_findlink(&block->buttons, mem[hash & 255]);
2136         }
2137 }
2138
2139 /******************** Popup Menu with callback or string **********************/
2140
2141 struct uiPopupMenu {
2142         uiBlock *block;
2143         uiLayout *layout;
2144         uiBut *but;
2145
2146         int mx, my, popup, slideout;
2147         int startx, starty, maxrow;
2148
2149         uiMenuCreateFunc menu_func;
2150         void *menu_arg;
2151 };
2152
2153 static uiBlock *ui_block_func_POPUP(bContext *C, uiPopupBlockHandle *handle, void *arg_pup)
2154 {
2155         uiBlock *block;
2156         uiBut *bt;
2157         ScrArea *sa;
2158         ARegion *ar;
2159         uiPopupMenu *pup= arg_pup;
2160         int offset, direction, minwidth, flip;
2161
2162         if(pup->menu_func) {
2163                 pup->block->handle= handle;
2164                 pup->menu_func(C, pup->layout, pup->menu_arg);
2165                 pup->block->handle= NULL;
2166         }
2167
2168         if(pup->but) {
2169                 /* minimum width to enforece */
2170                 minwidth= pup->but->x2 - pup->but->x1;
2171
2172                 if(pup->but->type == PULLDOWN || pup->but->menu_create_func) {
2173                         direction= UI_DOWN;
2174                         flip= 1;
2175                 }
2176                 else {
2177                         direction= UI_TOP;
2178                         flip= 0;
2179                 }
2180         }
2181         else {
2182                 minwidth= 50;
2183                 direction= UI_DOWN;
2184                 flip= 1;
2185         }
2186
2187         block= pup->block;
2188         
2189         /* in some cases we create the block before the region,
2190            so we set it delayed here if necessary */
2191         if(BLI_findindex(&handle->region->uiblocks, block) == -1)
2192                 uiBlockSetRegion(block, handle->region);
2193
2194         block->direction= direction;
2195
2196         uiBlockLayoutResolve(block, NULL, NULL);
2197
2198         if(pup->popup) {
2199                 uiBlockSetFlag(block, UI_BLOCK_LOOP|UI_BLOCK_REDRAW|UI_BLOCK_NUMSELECT|UI_BLOCK_RET_1);
2200                 uiBlockSetDirection(block, direction);
2201
2202                 /* offset the mouse position, possibly based on earlier selection */
2203                 offset= 1.5*MENU_BUTTON_HEIGHT;
2204
2205                 if(block->flag & UI_BLOCK_POPUP_MEMORY) {
2206                         bt= ui_popup_menu_memory(block, NULL);
2207
2208                         if(bt)
2209                                 offset= -bt->y1 - 0.5f*MENU_BUTTON_HEIGHT;
2210                 }
2211
2212                 block->minbounds= minwidth;
2213                 uiMenuPopupBoundsBlock(block, 1, 20, offset);
2214         }
2215         else {
2216                 /* for a header menu we set the direction automatic */
2217                 if(!pup->slideout && flip) {
2218                         sa= CTX_wm_area(C);
2219                         ar= CTX_wm_region(C);
2220
2221                         if(sa && sa->headertype==HEADERDOWN) {
2222                                 if(ar && ar->regiontype == RGN_TYPE_HEADER) {
2223                                         uiBlockSetDirection(block, UI_TOP);
2224                                         uiBlockFlipOrder(block);
2225                                 }
2226                         }
2227                 }
2228
2229                 block->minbounds= minwidth;
2230                 uiTextBoundsBlock(block, 50);
2231         }
2232
2233         /* if menu slides out of other menu, override direction */
2234         if(pup->slideout)
2235                 uiBlockSetDirection(block, UI_RIGHT);
2236
2237         uiEndBlock(C, block);
2238
2239         return pup->block;
2240 }
2241
2242 uiPopupBlockHandle *ui_popup_menu_create(bContext *C, ARegion *butregion, uiBut *but, uiMenuCreateFunc menu_func, void *arg, char *str)
2243 {
2244         wmWindow *window= CTX_wm_window(C);
2245         uiStyle *style= U.uistyles.first;
2246         uiPopupBlockHandle *handle;
2247         uiPopupMenu *pup;
2248         
2249         pup= MEM_callocN(sizeof(uiPopupMenu), "menu dummy");
2250         pup->block= uiBeginBlock(C, NULL, "ui_button_menu_create", UI_EMBOSSP);
2251         pup->layout= uiBlockLayout(pup->block, UI_LAYOUT_VERTICAL, UI_LAYOUT_MENU, 0, 0, 200, 0, style);
2252         pup->slideout= (but && (but->block->flag & UI_BLOCK_LOOP));
2253         pup->but= but;
2254         uiLayoutSetOperatorContext(pup->layout, WM_OP_INVOKE_REGION_WIN);
2255
2256         if(!but) {
2257                 /* no button to start from, means we are a popup */
2258                 pup->mx= window->eventstate->x;
2259                 pup->my= window->eventstate->y;
2260                 pup->popup= 1;
2261         }
2262
2263         if(str) {
2264                 /* menu is created from a string */
2265                 pup->menu_func= ui_block_func_MENUSTR;
2266                 pup->menu_arg= str;
2267         }
2268         else {
2269                 /* menu is created from a callback */
2270                 pup->menu_func= menu_func;
2271                 pup->menu_arg= arg;
2272         }
2273         
2274         handle= ui_popup_block_create(C, butregion, but, NULL, ui_block_func_POPUP, pup);
2275
2276         if(!but) {
2277                 handle->popup= 1;
2278
2279                 UI_add_popup_handlers(C, &window->modalhandlers, handle);
2280                 WM_event_add_mousemove(C);
2281         }
2282         
2283         MEM_freeN(pup);
2284
2285         return handle;
2286 }
2287
2288 /******************** Popup Menu API with begin and end ***********************/
2289
2290 /* only return handler, and set optional title */
2291 uiPopupMenu *uiPupMenuBegin(bContext *C, const char *title, int icon)
2292 {
2293         uiStyle *style= U.uistyles.first;
2294         uiPopupMenu *pup= MEM_callocN(sizeof(uiPopupMenu), "popup menu");
2295         uiBut *but;
2296         
2297         pup->block= uiBeginBlock(C, NULL, "uiPupMenuBegin", UI_EMBOSSP);
2298         pup->block->flag |= UI_BLOCK_POPUP_MEMORY;
2299         pup->block->puphash= ui_popup_menu_hash((char*)title);
2300         pup->layout= uiBlockLayout(pup->block, UI_LAYOUT_VERTICAL, UI_LAYOUT_MENU, 0, 0, 200, 0, style);
2301         uiLayoutSetOperatorContext(pup->layout, WM_OP_EXEC_REGION_WIN);
2302
2303         /* create in advance so we can let buttons point to retval already */
2304         pup->block->handle= MEM_callocN(sizeof(uiPopupBlockHandle), "uiPopupBlockHandle");
2305         
2306         /* create title button */
2307         if(title && title[0]) {
2308                 char titlestr[256];
2309                 
2310                 if(icon) {
2311                         sprintf(titlestr, " %s", title);
2312                         uiDefIconTextBut(pup->block, LABEL, 0, icon, titlestr, 0, 0, 200, MENU_BUTTON_HEIGHT, NULL, 0.0, 0.0, 0, 0, "");
2313                 }
2314                 else {
2315                         but= uiDefBut(pup->block, LABEL, 0, (char*)title, 0, 0, 200, MENU_BUTTON_HEIGHT, NULL, 0.0, 0.0, 0, 0, "");
2316                         but->flag= UI_TEXT_LEFT;
2317                 }
2318         }
2319
2320         return pup;
2321 }
2322
2323 /* set the whole structure to work */
2324 void uiPupMenuEnd(bContext *C, uiPopupMenu *pup)
2325 {
2326         wmWindow *window= CTX_wm_window(C);
2327         uiPopupBlockHandle *menu;
2328         
2329         pup->popup= 1;
2330         pup->mx= window->eventstate->x;
2331         pup->my= window->eventstate->y;
2332         
2333         menu= ui_popup_block_create(C, NULL, NULL, NULL, ui_block_func_POPUP, pup);
2334         menu->popup= 1;
2335         
2336         UI_add_popup_handlers(C, &window->modalhandlers, menu);
2337         WM_event_add_mousemove(C);
2338         
2339         MEM_freeN(pup);
2340 }
2341
2342 uiLayout *uiPupMenuLayout(uiPopupMenu *pup)
2343 {
2344         return pup->layout;
2345 }
2346
2347 /*************************** Standard Popup Menus ****************************/
2348
2349 static void operator_name_cb(bContext *C, void *arg, int retval)
2350 {
2351         const char *opname= arg;
2352
2353         if(opname && retval > 0)
2354                 WM_operator_name_call(C, opname, WM_OP_EXEC_DEFAULT, NULL);
2355 }
2356
2357 static void operator_cb(bContext *C, void *arg, int retval)
2358 {
2359         wmOperator *op= arg;
2360         
2361         if(op && retval > 0)
2362                 WM_operator_call(C, op);
2363         else
2364                 WM_operator_free(op);
2365 }
2366
2367 static void confirm_cancel_operator(void *opv)
2368 {
2369         WM_operator_free(opv);
2370 }
2371
2372 static void vconfirm_opname(bContext *C, char *opname, char *title, char *itemfmt, va_list ap)
2373 {
2374         uiPopupBlockHandle *handle;
2375         char *s, buf[512];
2376
2377         s= buf;
2378         if (title) s+= sprintf(s, "%s%%t|", title);
2379         vsprintf(s, itemfmt, ap);
2380
2381         handle= ui_popup_menu_create(C, NULL, NULL, NULL, NULL, buf);
2382
2383         handle->popup_func= operator_name_cb;
2384         handle->popup_arg= opname;
2385 }
2386
2387 static void confirm_operator(bContext *C, wmOperator *op, char *title, char *item)
2388 {
2389         uiPopupBlockHandle *handle;
2390         char *s, buf[512];
2391         
2392         s= buf;
2393         if (title) s+= sprintf(s, "%s%%t|%s", title, item);
2394         
2395         handle= ui_popup_menu_create(C, NULL, NULL, NULL, NULL, buf);
2396
2397         handle->popup_func= operator_cb;
2398         handle->popup_arg= op;
2399         handle->cancel_func= confirm_cancel_operator;
2400 }
2401
2402 void uiPupMenuOkee(bContext *C, char *opname, char *str, ...)
2403 {
2404         va_list ap;
2405         char titlestr[256];
2406
2407         sprintf(titlestr, "OK? %%i%d", ICON_QUESTION);
2408
2409         va_start(ap, str);
2410         vconfirm_opname(C, opname, titlestr, str, ap);
2411         va_end(ap);
2412 }
2413
2414 void uiPupMenuSaveOver(bContext *C, wmOperator *op, char *filename)
2415 {
2416         size_t len= strlen(filename);
2417
2418         if(len==0)
2419                 return;
2420
2421         if(filename[len-1]=='/' || filename[len-1]=='\\') {
2422                 uiPupMenuError(C, "Cannot overwrite a directory");
2423                 WM_operator_free(op);
2424                 return;
2425         }
2426         if(BLI_exists(filename)==0)
2427                 operator_cb(C, op, 1);
2428         else
2429                 confirm_operator(C, op, "Save Over", filename);
2430 }
2431
2432 void uiPupMenuNotice(bContext *C, char *str, ...)
2433 {
2434         va_list ap;
2435
2436         va_start(ap, str);
2437         vconfirm_opname(C, NULL, NULL, str, ap);
2438         va_end(ap);
2439 }
2440
2441 void uiPupMenuError(bContext *C, char *str, ...)
2442 {
2443         va_list ap;
2444         char nfmt[256];
2445         char titlestr[256];
2446
2447         sprintf(titlestr, "Error %%i%d", ICON_ERROR);
2448
2449         sprintf(nfmt, "%s", str);
2450
2451         va_start(ap, str);
2452         vconfirm_opname(C, NULL, titlestr, nfmt, ap);
2453         va_end(ap);
2454 }
2455
2456 void uiPupMenuReports(bContext *C, ReportList *reports)
2457 {
2458         Report *report;
2459         DynStr *ds;
2460         char *str;
2461
2462         if(!reports || !reports->list.first)
2463                 return;
2464         if(!CTX_wm_window(C))
2465                 return;
2466
2467         ds= BLI_dynstr_new();
2468
2469         for(report=reports->list.first; report; report=report->next) {
2470                 if(report->type >= RPT_ERROR)
2471                         BLI_dynstr_appendf(ds, "Error %%i%d%%t|%s", ICON_ERROR, report->message);
2472                 else if(report->type >= RPT_WARNING)
2473                         BLI_dynstr_appendf(ds, "Warning %%i%d%%t|%s", ICON_ERROR, report->message);
2474                 else if(report->type >= RPT_INFO)
2475                         BLI_dynstr_appendf(ds, "Info %%t|%s", report->message);
2476         }
2477
2478         str= BLI_dynstr_get_cstring(ds);
2479         ui_popup_menu_create(C, NULL, NULL, NULL, NULL, str);
2480         MEM_freeN(str);
2481
2482         BLI_dynstr_free(ds);
2483 }
2484
2485 void uiPupMenuInvoke(bContext *C, const char *idname)
2486 {
2487         uiPopupMenu *pup;
2488         uiLayout *layout;
2489         Menu menu;
2490         MenuType *mt= WM_menutype_find(idname, TRUE);
2491
2492         if(mt==NULL) {
2493                 printf("uiPupMenuInvoke: named menu \"%s\" not found\n", idname);
2494                 return;
2495         }
2496
2497         if(mt->poll && mt->poll(C, mt)==0)
2498                 return;
2499
2500         pup= uiPupMenuBegin(C, mt->label, 0);
2501         layout= uiPupMenuLayout(pup);
2502
2503         menu.layout= layout;
2504         menu.type= mt;
2505
2506         mt->draw(C, &menu);
2507
2508         uiPupMenuEnd(C, pup);
2509 }
2510
2511
2512 /*************************** Popup Block API **************************/
2513
2514 void uiPupBlockO(bContext *C, uiBlockCreateFunc func, void *arg, char *opname, int opcontext)
2515 {
2516         wmWindow *window= CTX_wm_window(C);
2517         uiPopupBlockHandle *handle;
2518         
2519         handle= ui_popup_block_create(C, NULL, NULL, func, NULL, arg);
2520         handle->popup= 1;
2521         handle->optype= (opname)? WM_operatortype_find(opname, 0): NULL;
2522         handle->opcontext= opcontext;
2523         
2524         UI_add_popup_handlers(C, &window->modalhandlers, handle);
2525         WM_event_add_mousemove(C);
2526 }
2527
2528 void uiPupBlock(bContext *C, uiBlockCreateFunc func, void *arg)
2529 {
2530         uiPupBlockO(C, func, arg, NULL, 0);
2531 }
2532
2533 void uiPupBlockOperator(bContext *C, uiBlockCreateFunc func, wmOperator *op, int opcontext)
2534 {
2535         wmWindow *window= CTX_wm_window(C);
2536         uiPopupBlockHandle *handle;
2537         
2538         handle= ui_popup_block_create(C, NULL, NULL, func, NULL, op);
2539         handle->popup= 1;
2540         handle->retvalue= 1;
2541
2542         handle->popup_arg= op;
2543         handle->popup_func= operator_cb;
2544         handle->cancel_func= confirm_cancel_operator;
2545         handle->opcontext= opcontext;
2546         
2547         UI_add_popup_handlers(C, &window->modalhandlers, handle);
2548         WM_event_add_mousemove(C);
2549 }
2550