Sculpt:
[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;
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         uiBlockSetFlag(block, UI_BLOCK_MOVEMOUSE_QUIT);
1342         
1343         /* compute menu data */
1344         md= decompose_menu_string(instr);
1345
1346         /* columns and row estimation */
1347         columns= (md->nitems+20)/20;
1348         if(columns<1)
1349                 columns= 1;
1350         if(columns>8)
1351                 columns= (md->nitems+25)/25;
1352         
1353         rows= md->nitems/columns;
1354         if(rows<1)
1355                 rows= 1;
1356         while(rows*columns<md->nitems)
1357                 rows++;
1358
1359         /* create title */
1360         if(md->title) {
1361                 if(md->titleicon) {
1362                         uiItemL(layout, md->title, md->titleicon);
1363                 }
1364                 else {
1365                         uiItemL(layout, md->title, 0);
1366                         bt= block->buttons.last;
1367                         bt->flag= UI_TEXT_LEFT;
1368                 }
1369         }
1370
1371         /* inconsistent, but menus with labels do not look good flipped */
1372         for(a=0, b=0; a<md->nitems; a++, b++) {
1373                 entry= &md->items[a];
1374
1375                 if(entry->sepr && entry->str[0])
1376                         block->flag |= UI_BLOCK_NO_FLIP;
1377         }
1378
1379         /* create items */
1380         split= uiLayoutSplit(layout, 0);
1381
1382         for(a=0, b=0; a<md->nitems; a++, b++) {
1383                 if(block->flag & UI_BLOCK_NO_FLIP)
1384                         entry= &md->items[a];
1385                 else
1386                         entry= &md->items[md->nitems-a-1];
1387                 
1388                 /* new column on N rows or on separation label */
1389                 if((b % rows == 0) || (entry->sepr && entry->str[0])) {
1390                         column= uiLayoutColumn(split, 0);
1391                         b= 0;
1392                 }
1393
1394                 if(entry->sepr) {
1395                         uiItemL(column, entry->str, entry->icon);
1396                         bt= block->buttons.last;
1397                         bt->flag= UI_TEXT_LEFT;
1398                 }
1399                 else if(entry->icon) {
1400                         uiDefIconTextButF(block, BUTM|FLO, B_NOP, entry->icon, entry->str, 0, 0,
1401                                 UI_UNIT_X*5, UI_UNIT_Y, &handle->retvalue, (float) entry->retval, 0.0, 0, 0, "");
1402                 }
1403                 else {
1404                         uiDefButF(block, BUTM|FLO, B_NOP, entry->str, 0, 0,
1405                                 UI_UNIT_X*5, UI_UNIT_X, &handle->retvalue, (float) entry->retval, 0.0, 0, 0, "");
1406                 }
1407         }
1408         
1409         menudata_free(md);
1410 }
1411
1412 void ui_block_func_ICONROW(bContext *C, uiLayout *layout, void *arg_but)
1413 {
1414         uiBlock *block= uiLayoutGetBlock(layout);
1415         uiPopupBlockHandle *handle= block->handle;
1416         uiBut *but= arg_but;
1417         int a;
1418         
1419         uiBlockSetFlag(block, UI_BLOCK_MOVEMOUSE_QUIT);
1420         
1421         for(a=(int)but->hardmin; a<=(int)but->hardmax; a++)
1422                 uiDefIconButF(block, BUTM|FLO, B_NOP, but->icon+(a-but->hardmin), 0, 0, UI_UNIT_X*5, UI_UNIT_Y,
1423                         &handle->retvalue, (float)a, 0.0, 0, 0, "");
1424 }
1425
1426 void ui_block_func_ICONTEXTROW(bContext *C, uiLayout *layout, void *arg_but)
1427 {
1428         uiBlock *block= uiLayoutGetBlock(layout);
1429         uiPopupBlockHandle *handle= block->handle;
1430         uiBut *but= arg_but, *bt;
1431         MenuData *md;
1432         MenuEntry *entry;
1433         int a;
1434         
1435         uiBlockSetFlag(block, UI_BLOCK_MOVEMOUSE_QUIT);
1436
1437         md= decompose_menu_string(but->str);
1438
1439         /* title */
1440         if(md->title) {
1441                 bt= uiDefBut(block, LABEL, 0, md->title, 0, 0, UI_UNIT_X*5, UI_UNIT_Y, NULL, 0.0, 0.0, 0, 0, "");
1442                 bt->flag= UI_TEXT_LEFT;
1443         }
1444
1445         /* loop through the menu options and draw them out with icons & text labels */
1446         for(a=0; a<md->nitems; a++) {
1447                 entry= &md->items[md->nitems-a-1];
1448
1449                 if(entry->sepr)
1450                         uiItemS(layout);
1451                 else
1452                         uiDefIconTextButF(block, BUTM|FLO, B_NOP, (short)((but->icon)+(entry->retval-but->hardmin)), entry->str,
1453                                 0, 0, UI_UNIT_X*5, UI_UNIT_Y, &handle->retvalue, (float) entry->retval, 0.0, 0, 0, "");
1454         }
1455
1456         menudata_free(md);
1457 }
1458
1459 #if 0
1460 static void ui_warp_pointer(short x, short y)
1461 {
1462         /* XXX 2.50 which function to use for this? */
1463         /* OSX has very poor mousewarp support, it sends events;
1464            this causes a menu being pressed immediately ... */
1465         #ifndef __APPLE__
1466         warp_pointer(x, y);
1467         #endif
1468 }
1469 #endif
1470
1471 /********************* Color Button ****************/
1472
1473 /* picker sizes S hsize, F full size, D spacer, B button/pallette height  */
1474 #define SPICK   110.0
1475 #define FPICK   180.0
1476 #define DPICK   6.0
1477 #define BPICK   24.0
1478
1479 #define UI_PALETTE_TOT 16
1480 /* note; in tot+1 the old color is stored */
1481 static float palette[UI_PALETTE_TOT+1][3]= {
1482 {0.93, 0.83, 0.81}, {0.88, 0.89, 0.73}, {0.69, 0.81, 0.57}, {0.51, 0.76, 0.64}, 
1483 {0.37, 0.56, 0.61}, {0.33, 0.29, 0.55}, {0.46, 0.21, 0.51}, {0.40, 0.12, 0.18}, 
1484 {1.0, 1.0, 1.0}, {0.85, 0.85, 0.85}, {0.7, 0.7, 0.7}, {0.56, 0.56, 0.56}, 
1485 {0.42, 0.42, 0.42}, {0.28, 0.28, 0.28}, {0.14, 0.14, 0.14}, {0.0, 0.0, 0.0}
1486 };  
1487
1488 /* for picker, while editing hsv */
1489 void ui_set_but_hsv(uiBut *but)
1490 {
1491         float col[3];
1492         
1493         hsv_to_rgb(but->hsv[0], but->hsv[1], but->hsv[2], col, col+1, col+2);
1494         ui_set_but_vectorf(but, col);
1495 }
1496
1497 static void update_picker_hex(uiBlock *block, float *rgb)
1498 {
1499         uiBut *bt;
1500         char col[16];
1501         
1502         sprintf(col, "%02X%02X%02X", (unsigned int)(rgb[0]*255.0), (unsigned int)(rgb[1]*255.0), (unsigned int)(rgb[2]*255.0));
1503         
1504         // this updates button strings, is hackish... but button pointers are on stack of caller function
1505
1506         for(bt= block->buttons.first; bt; bt= bt->next) {
1507                 if(strcmp(bt->str, "Hex: ")==0)
1508                         strcpy(bt->poin, col);
1509
1510                 ui_check_but(bt);
1511         }
1512 }
1513
1514 /* also used by small picker, be careful with name checks below... */
1515 void ui_update_block_buts_hsv(uiBlock *block, float *hsv)
1516 {
1517         uiBut *bt;
1518         float r, g, b;
1519         float rgb[3];
1520         
1521         // this updates button strings, is hackish... but button pointers are on stack of caller function
1522         hsv_to_rgb(hsv[0], hsv[1], hsv[2], &r, &g, &b);
1523         
1524         rgb[0] = r; rgb[1] = g; rgb[2] = b;
1525         update_picker_hex(block, rgb);
1526
1527         for(bt= block->buttons.first; bt; bt= bt->next) {
1528                 if(ELEM(bt->type, HSVCUBE, HSVCIRCLE)) {
1529                         VECCOPY(bt->hsv, hsv);
1530                         ui_set_but_hsv(bt);
1531                 }
1532                 else if(bt->str[1]==' ') {
1533                         if(bt->str[0]=='R') {
1534                                 ui_set_but_val(bt, r);
1535                         }
1536                         else if(bt->str[0]=='G') {
1537                                 ui_set_but_val(bt, g);
1538                         }
1539                         else if(bt->str[0]=='B') {
1540                                 ui_set_but_val(bt, b);
1541                         }
1542                         else if(bt->str[0]=='H') {
1543                                 ui_set_but_val(bt, hsv[0]);
1544                         }
1545                         else if(bt->str[0]=='S') {
1546                                 ui_set_but_val(bt, hsv[1]);
1547                         }
1548                         else if(bt->str[0]=='V') {
1549                                 ui_set_but_val(bt, hsv[2]);
1550                         }
1551                 }               
1552
1553                 ui_check_but(bt);
1554         }
1555 }
1556
1557 static void ui_update_block_buts_hex(uiBlock *block, char *hexcol)
1558 {
1559         uiBut *bt;
1560         float r=0, g=0, b=0;
1561         float h, s, v;
1562         
1563         
1564         // this updates button strings, is hackish... but button pointers are on stack of caller function
1565         hex_to_rgb(hexcol, &r, &g, &b);
1566         rgb_to_hsv(r, g, b, &h, &s, &v);
1567
1568         for(bt= block->buttons.first; bt; bt= bt->next) {
1569                 if(bt->type==HSVCUBE) {
1570                         bt->hsv[0] = h;
1571                         bt->hsv[1] = s;                 
1572                         bt->hsv[2] = v;
1573                         ui_set_but_hsv(bt);
1574                 }
1575                 else if(bt->str[1]==' ') {
1576                         if(bt->str[0]=='R') {
1577                                 ui_set_but_val(bt, r);
1578                         }
1579                         else if(bt->str[0]=='G') {
1580                                 ui_set_but_val(bt, g);
1581                         }
1582                         else if(bt->str[0]=='B') {
1583                                 ui_set_but_val(bt, b);
1584                         }
1585                         else if(bt->str[0]=='H') {
1586                                 ui_set_but_val(bt, h);
1587                         }
1588                         else if(bt->str[0]=='S') {
1589                                 ui_set_but_val(bt, s);
1590                         }
1591                         else if(bt->str[0]=='V') {
1592                                 ui_set_but_val(bt, v);
1593                         }
1594                 }
1595
1596                 ui_check_but(bt);
1597         }
1598 }
1599
1600 /* bt1 is palette but, col1 is original color */
1601 /* callback to copy from/to palette */
1602 static void do_palette_cb(bContext *C, void *bt1, void *col1)
1603 {
1604         wmWindow *win= CTX_wm_window(C);
1605         uiBut *but1= (uiBut *)bt1;
1606         uiPopupBlockHandle *popup= but1->block->handle;
1607         float *col= (float *)col1;
1608         float *fp, hsv[3];
1609         
1610         fp= (float *)but1->poin;
1611         
1612         if(win->eventstate->ctrl) {
1613                 VECCOPY(fp, col);
1614         }
1615         else {
1616                 VECCOPY(col, fp);
1617         }
1618         
1619         rgb_to_hsv(col[0], col[1], col[2], hsv, hsv+1, hsv+2);
1620         ui_update_block_buts_hsv(but1->block, hsv);
1621         update_picker_hex(but1->block, col);
1622
1623         if(popup)
1624                 popup->menuretval= UI_RETURN_UPDATE;
1625 }
1626
1627 static void do_hsv_cb(bContext *C, void *bt1, void *unused)
1628 {
1629         uiBut *but1= (uiBut *)bt1;
1630         uiPopupBlockHandle *popup= but1->block->handle;
1631
1632         if(popup)
1633                 popup->menuretval= UI_RETURN_UPDATE;
1634 }
1635
1636 /* bt1 is num but, hsv1 is pointer to original color in hsv space*/
1637 /* callback to handle changes in num-buts in picker */
1638 static void do_palette1_cb(bContext *C, void *bt1, void *hsv1)
1639 {
1640         uiBut *but1= (uiBut *)bt1;
1641         uiPopupBlockHandle *popup= but1->block->handle;
1642         float *hsv= (float *)hsv1;
1643         float *fp= NULL;
1644         
1645         if(but1->str[1]==' ') {
1646                 if(but1->str[0]=='R') fp= (float *)but1->poin;
1647                 else if(but1->str[0]=='G') fp= ((float *)but1->poin)-1;
1648                 else if(but1->str[0]=='B') fp= ((float *)but1->poin)-2;
1649         }
1650         if(fp) {
1651                 rgb_to_hsv(fp[0], fp[1], fp[2], hsv, hsv+1, hsv+2);
1652         } 
1653         ui_update_block_buts_hsv(but1->block, hsv);
1654
1655         if(popup)
1656                 popup->menuretval= UI_RETURN_UPDATE;
1657 }
1658
1659 /* bt1 is num but, col1 is pointer to original color */
1660 /* callback to handle changes in num-buts in picker */
1661 static void do_palette2_cb(bContext *C, void *bt1, void *col1)
1662 {
1663         uiBut *but1= (uiBut *)bt1;
1664         uiPopupBlockHandle *popup= but1->block->handle;
1665         float *rgb= (float *)col1;
1666         float *fp= NULL;
1667         
1668         if(but1->str[1]==' ') {
1669                 if(but1->str[0]=='H') fp= (float *)but1->poin;
1670                 else if(but1->str[0]=='S') fp= ((float *)but1->poin)-1;
1671                 else if(but1->str[0]=='V') fp= ((float *)but1->poin)-2;
1672         }
1673         if(fp) {
1674                 hsv_to_rgb(fp[0], fp[1], fp[2], rgb, rgb+1, rgb+2);
1675         } 
1676         ui_update_block_buts_hsv(but1->block, fp);
1677
1678         if(popup)
1679                 popup->menuretval= UI_RETURN_UPDATE;
1680 }
1681
1682 static void do_palette_hex_cb(bContext *C, void *bt1, void *hexcl)
1683 {
1684         uiBut *but1= (uiBut *)bt1;
1685         uiPopupBlockHandle *popup= but1->block->handle;
1686         char *hexcol= (char *)hexcl;
1687         
1688         ui_update_block_buts_hex(but1->block, hexcol);  
1689
1690         if(popup)
1691                 popup->menuretval= UI_RETURN_UPDATE;
1692 }
1693
1694 /* used for both 3d view and image window */
1695 static void do_palette_sample_cb(bContext *C, void *bt1, void *col1)    /* frontbuf */
1696 {
1697         /* XXX 2.50 this should become an operator? */
1698 #if 0
1699         uiBut *but1= (uiBut *)bt1;
1700         uiBut *but;
1701         float tempcol[4];
1702         int x=0, y=0;
1703         short mval[2];
1704         float hsv[3];
1705         short capturing;
1706         int oldcursor;
1707         Window *win;
1708         unsigned short dev;
1709         
1710         oldcursor=get_cursor();
1711         win=winlay_get_active_window();
1712         
1713         while (get_mbut() & L_MOUSE) UI_wait_for_statechange();
1714         
1715         SetBlenderCursor(BC_EYEDROPPER_CURSOR);
1716         
1717         /* loop and wait for a mouse click */
1718         capturing = TRUE;
1719         while(capturing) {
1720                 char ascii;
1721                 short val;
1722                 
1723                 dev = extern_qread_ext(&val, &ascii);
1724                 
1725                 if(dev==INPUTCHANGE) break;
1726                 if(get_mbut() & R_MOUSE) break;
1727                 else if(get_mbut() & L_MOUSE) {
1728                         uiGetMouse(mywinget(), mval);
1729                         x= mval[0]; y= mval[1];
1730                         
1731                         capturing = FALSE;
1732                         break;
1733                 }
1734                 else if(dev==ESCKEY) break;
1735         }
1736         window_set_cursor(win, oldcursor);
1737         
1738         if(capturing) return;
1739         
1740         if(x<0 || y<0) return;
1741         
1742         /* if we've got a glick, use OpenGL to sample the color under the mouse pointer */
1743         glReadBuffer(GL_FRONT);
1744         glReadPixels(x, y, 1, 1, GL_RGBA, GL_FLOAT, tempcol);
1745         glReadBuffer(GL_BACK);
1746         
1747         /* and send that color back to the picker */
1748         rgb_to_hsv(tempcol[0], tempcol[1], tempcol[2], hsv, hsv+1, hsv+2);
1749         ui_update_block_buts_hsv(but1->block, hsv);
1750         update_picker_hex(but1->block, tempcol);
1751         
1752         for (but= but1->block->buttons.first; but; but= but->next) {
1753                 ui_check_but(but);
1754                 ui_draw_but(but);
1755         }
1756         
1757         but= but1->block->buttons.first;
1758         ui_block_flush_back(but->block);
1759 #endif
1760 }
1761
1762 /* color picker, Gimp version. mode: 'f' = floating panel, 'p' =  popup */
1763 /* col = read/write to, hsv/old/hexcol = memory for temporal use */
1764 void uiBlockPickerButtons(uiBlock *block, float *col, float *hsv, float *old, char *hexcol, char mode, short retval)
1765 {
1766         uiBut *bt;
1767         float h, offs;
1768         int a;
1769
1770         VECCOPY(old, col);      // old color stored there, for palette_cb to work
1771         
1772         // the cube intersection
1773         bt= uiDefButF(block, HSVCUBE, retval, "",       0,DPICK+BPICK,FPICK,FPICK, col, 0.0, 0.0, 2, 0, "");
1774         uiButSetFunc(bt, do_hsv_cb, bt, NULL);
1775
1776         bt= uiDefButF(block, HSVCUBE, retval, "",       0,0,FPICK,BPICK, col, 0.0, 0.0, 3, 0, "");
1777         uiButSetFunc(bt, do_hsv_cb, bt, NULL);
1778
1779         // palette
1780         
1781         bt=uiDefButF(block, COL, retval, "",            FPICK+DPICK, 0, BPICK,BPICK, old, 0.0, 0.0, -1, 0, "Old color, click to restore");
1782         uiButSetFunc(bt, do_palette_cb, bt, col);
1783         uiDefButF(block, COL, retval, "",               FPICK+DPICK, BPICK+DPICK, BPICK,60-BPICK-DPICK, col, 0.0, 0.0, -1, 0, "Active color");
1784
1785         h= (DPICK+BPICK+FPICK-64)/(UI_PALETTE_TOT/2.0);
1786         uiBlockBeginAlign(block);
1787         for(a= -1+UI_PALETTE_TOT/2; a>=0; a--) {
1788                 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");
1789                 uiButSetFunc(bt, do_palette_cb, bt, col);
1790                 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");               
1791                 uiButSetFunc(bt, do_palette_cb, bt, col);
1792         }
1793         uiBlockEndAlign(block);
1794         
1795         // buttons
1796         rgb_to_hsv(col[0], col[1], col[2], hsv, hsv+1, hsv+2);
1797         sprintf(hexcol, "%02X%02X%02X", (unsigned int)(col[0]*255.0), (unsigned int)(col[1]*255.0), (unsigned int)(col[2]*255.0));      
1798
1799         offs= FPICK+2*DPICK+BPICK;
1800
1801         /* note; made this a TOG now, with NULL pointer. Is because BUT now gets handled with a afterfunc */
1802         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)");
1803         uiButSetFunc(bt, do_palette_sample_cb, bt, col);
1804         uiButSetFlag(bt, UI_TEXT_LEFT);
1805         
1806         bt= uiDefBut(block, TEX, retval, "Hex: ", offs, 140, 140, 20, hexcol, 0, 8, 0, 0, "Hex triplet for color (#RRGGBB)");
1807         uiButSetFunc(bt, do_palette_hex_cb, bt, hexcol);
1808
1809         uiBlockBeginAlign(block);
1810         bt= uiDefButF(block, NUMSLI, retval, "R ",      offs, 110, 140,20, col, 0.0, 1.0, 10, 3, "");
1811         uiButSetFunc(bt, do_palette1_cb, bt, hsv);
1812         bt= uiDefButF(block, NUMSLI, retval, "G ",      offs, 90, 140,20, col+1, 0.0, 1.0, 10, 3, "");
1813         uiButSetFunc(bt, do_palette1_cb, bt, hsv);
1814         bt= uiDefButF(block, NUMSLI, retval, "B ",      offs, 70, 140,20, col+2, 0.0, 1.0, 10, 3, "");
1815         uiButSetFunc(bt, do_palette1_cb, bt, hsv);
1816         
1817         uiBlockBeginAlign(block);
1818         bt= uiDefButF(block, NUMSLI, retval, "H ",      offs, 40, 140,20, hsv, 0.0, 1.0, 10, 3, "");
1819         uiButSetFunc(bt, do_palette2_cb, bt, col);
1820         bt= uiDefButF(block, NUMSLI, retval, "S ",      offs, 20, 140,20, hsv+1, 0.0, 1.0, 10, 3, "");
1821         uiButSetFunc(bt, do_palette2_cb, bt, col);
1822         bt= uiDefButF(block, NUMSLI, retval, "V ",      offs, 0, 140,20, hsv+2, 0.0, 1.0, 10, 3, "");
1823         uiButSetFunc(bt, do_palette2_cb, bt, col);
1824         uiBlockEndAlign(block);
1825 }
1826
1827 /* bt1 is num but, hsv1 is pointer to original color in hsv space*/
1828 /* callback to handle changes */
1829 static void do_picker_small_cb(bContext *C, void *bt1, void *hsv1)
1830 {
1831         uiBut *but1= (uiBut *)bt1;
1832         uiPopupBlockHandle *popup= but1->block->handle;
1833         float *hsv= (float *)hsv1;
1834         float *fp= NULL;
1835         
1836         fp= (float *)but1->poin;
1837         rgb_to_hsv(fp[0], fp[1], fp[2], hsv, hsv+1, hsv+2);
1838
1839         ui_update_block_buts_hsv(but1->block, hsv);
1840         
1841         if(popup)
1842                 popup->menuretval= UI_RETURN_UPDATE;
1843 }
1844
1845 /* picker sizes S hsize, F full size, D spacer, B button/pallette height  */
1846 #define SPICK1  150.0
1847 #define DPICK1  6.0
1848
1849 /* only the color, a HS circle and V slider */
1850 static void uiBlockPickerSmall(uiBlock *block, float *col, float *hsv, float *old, char *hexcol, char mode, short retval)
1851 {
1852         uiBut *bt;
1853         
1854         VECCOPY(old, col);      // old color stored there, for palette_cb to work
1855         
1856         /* HS circle */
1857         bt= uiDefButF(block, HSVCIRCLE, retval, "",     0, 0,SPICK1,SPICK1, col, 0.0, 0.0, 0, 0, "");
1858         uiButSetFunc(bt, do_picker_small_cb, bt, hsv);
1859
1860         /* value */
1861         bt= uiDefButF(block, HSVCUBE, retval, "",       SPICK1+DPICK1,0,14,SPICK1, col, 0.0, 0.0, 4, 0, "");
1862         uiButSetFunc(bt, do_picker_small_cb, bt, hsv);
1863 }
1864
1865
1866 static void picker_new_hide_reveal(uiBlock *block, short colormode)
1867 {
1868         uiBut *bt;
1869         
1870         /* tag buttons */
1871         for(bt= block->buttons.first; bt; bt= bt->next) {
1872                 
1873                 if(bt->type==NUMSLI || bt->type==TEX) {
1874                         if( bt->str[1]=='e') {
1875                                 if(colormode==2) bt->flag &= ~UI_HIDDEN;
1876                                 else bt->flag |= UI_HIDDEN;
1877                         }
1878                         else if( ELEM3(bt->str[0], 'R', 'G', 'B')) {
1879                                 if(colormode==0) bt->flag &= ~UI_HIDDEN;
1880                                 else bt->flag |= UI_HIDDEN;
1881                         }
1882                         else if( ELEM3(bt->str[0], 'H', 'S', 'V')) {
1883                                 if(colormode==1) bt->flag &= ~UI_HIDDEN;
1884                                 else bt->flag |= UI_HIDDEN;
1885                         }
1886                 }
1887         }
1888 }
1889
1890 static void do_picker_new_mode_cb(bContext *C, void *bt1, void *colv)
1891 {
1892         uiBut *bt= bt1;
1893         short colormode= ui_get_but_val(bt);
1894
1895         picker_new_hide_reveal(bt->block, colormode);
1896 }
1897
1898
1899 /* a HS circle, V slider, rgb/hsv/hex sliders */
1900 static void uiBlockPickerNew(uiBlock *block, float *col, float *hsv, float *old, char *hexcol, char mode, short retval)
1901 {
1902         static short colormode= 0;      /* temp? 0=rgb, 1=hsv, 2=hex */
1903         uiBut *bt;
1904         int width;
1905         
1906         VECCOPY(old, col);      // old color stored there, for palette_cb to work
1907         
1908         /* HS circle */
1909         bt= uiDefButF(block, HSVCIRCLE, retval, "",     0, 0,SPICK1,SPICK1, col, 0.0, 0.0, 0, 0, "");
1910         uiButSetFunc(bt, do_picker_small_cb, bt, hsv);
1911         
1912         /* value */
1913         bt= uiDefButF(block, HSVCUBE, retval, "",       SPICK1+DPICK1,0,14,SPICK1, col, 0.0, 0.0, 4, 0, "");
1914         uiButSetFunc(bt, do_picker_small_cb, bt, hsv);
1915         
1916         /* mode */
1917         width= (SPICK1+DPICK1+14)/3;
1918         uiBlockBeginAlign(block);
1919         bt= uiDefButS(block, ROW, retval, "RGB",        0, -30, width, 19, &colormode, 0.0, 0.0, 0, 0, "");
1920         uiButSetFunc(bt, do_picker_new_mode_cb, bt, col);
1921         bt= uiDefButS(block, ROW, retval, "HSV",        width, -30, width, 19, &colormode, 0.0, 1.0, 0, 0, "");
1922         uiButSetFunc(bt, do_picker_new_mode_cb, bt, hsv);
1923         bt= uiDefButS(block, ROW, retval, "Hex",        2*width, -30, width, 19, &colormode, 0.0, 2.0, 0, 0, "");
1924         uiButSetFunc(bt, do_picker_new_mode_cb, bt, hexcol);
1925         uiBlockEndAlign(block);
1926         
1927         /* sliders or hex */
1928         width= (SPICK1+DPICK1+14);
1929         rgb_to_hsv(col[0], col[1], col[2], hsv, hsv+1, hsv+2);
1930         sprintf(hexcol, "%02X%02X%02X", (unsigned int)(col[0]*255.0), (unsigned int)(col[1]*255.0), (unsigned int)(col[2]*255.0));      
1931
1932         uiBlockBeginAlign(block);
1933         bt= uiDefButF(block, NUMSLI, 0, "R ",   0, -60, width, 19, col, 0.0, 1.0, 10, 3, "");
1934         uiButSetFunc(bt, do_palette1_cb, bt, hsv);
1935         bt= uiDefButF(block, NUMSLI, 0, "G ",   0, -80, width, 19, col+1, 0.0, 1.0, 10, 3, "");
1936         uiButSetFunc(bt, do_palette1_cb, bt, hsv);
1937         bt= uiDefButF(block, NUMSLI, 0, "B ",   0, -100, width, 19, col+2, 0.0, 1.0, 10, 3, "");
1938         uiButSetFunc(bt, do_palette1_cb, bt, hsv);
1939         uiBlockEndAlign(block);
1940
1941         uiBlockBeginAlign(block);
1942         bt= uiDefButF(block, NUMSLI, 0, "H ",   0, -60, width, 19, hsv, 0.0, 1.0, 10, 3, "");
1943         uiButSetFunc(bt, do_palette2_cb, bt, col);
1944         bt= uiDefButF(block, NUMSLI, 0, "S ",   0, -80, width, 19, hsv+1, 0.0, 1.0, 10, 3, "");
1945         uiButSetFunc(bt, do_palette2_cb, bt, col);
1946         bt= uiDefButF(block, NUMSLI, 0, "V ",   0, -100, width, 19, hsv+2, 0.0, 1.0, 10, 3, "");
1947         uiButSetFunc(bt, do_palette2_cb, bt, col);
1948         uiBlockEndAlign(block);
1949
1950         bt= uiDefBut(block, TEX, 0, "Hex: ", 0, -80, width, 19, hexcol, 0, 8, 0, 0, "Hex triplet for color (#RRGGBB)");
1951         uiButSetFunc(bt, do_palette_hex_cb, bt, hexcol);
1952
1953         picker_new_hide_reveal(block, colormode);
1954 }
1955
1956
1957 static int ui_picker_small_wheel(const bContext *C, uiBlock *block, wmEvent *event)
1958 {
1959         float add= 0.0f;
1960         
1961         if(event->type==WHEELUPMOUSE)
1962                 add= 0.05f;
1963         else if(event->type==WHEELDOWNMOUSE)
1964                 add= -0.05f;
1965         
1966         if(add!=0.0f) {
1967                 uiBut *but;
1968                 
1969                 for(but= block->buttons.first; but; but= but->next) {
1970                         if(but->type==HSVCUBE && but->active==NULL) {
1971                                 uiPopupBlockHandle *popup= block->handle;
1972                                 float col[3];
1973                                 
1974                                 ui_get_but_vectorf(but, col);
1975                                 
1976                                 rgb_to_hsv(col[0], col[1], col[2], but->hsv, but->hsv+1, but->hsv+2);
1977                                 but->hsv[2]= CLAMPIS(but->hsv[2]+add, 0.0f, 1.0f);
1978                                 hsv_to_rgb(but->hsv[0], but->hsv[1], but->hsv[2], col, col+1, col+2);
1979
1980                                 ui_set_but_vectorf(but, col);
1981                                 
1982                                 ui_update_block_buts_hsv(block, but->hsv);
1983                                 if(popup)
1984                                         popup->menuretval= UI_RETURN_UPDATE;
1985                                 
1986                                 return 1;
1987                         }
1988                 }
1989         }
1990         return 0;
1991 }
1992
1993 uiBlock *ui_block_func_COL(bContext *C, uiPopupBlockHandle *handle, void *arg_but)
1994 {
1995         wmWindow *win= CTX_wm_window(C); // XXX temp, needs to become keymap to detect type?
1996         uiBut *but= arg_but;
1997         uiBlock *block;
1998         static float hsvcol[3], oldcol[3];
1999         static char hexcol[128];
2000         
2001         block= uiBeginBlock(C, handle->region, "colorpicker", UI_EMBOSS);
2002         
2003         uiBlockSetFlag(block, UI_BLOCK_MOVEMOUSE_QUIT);
2004         
2005         VECCOPY(handle->retvec, but->editvec);
2006         if(win->eventstate->shift) {
2007                 uiBlockPickerButtons(block, handle->retvec, hsvcol, oldcol, hexcol, 'p', 0);
2008                 block->flag= UI_BLOCK_LOOP|UI_BLOCK_REDRAW|UI_BLOCK_KEEP_OPEN;
2009                 uiBoundsBlock(block, 3);
2010         }
2011         else if(win->eventstate->alt) {
2012                 uiBlockPickerSmall(block, handle->retvec, hsvcol, oldcol, hexcol, 'p', 0);
2013                 block->flag= UI_BLOCK_LOOP|UI_BLOCK_REDRAW|UI_BLOCK_RET_1|UI_BLOCK_OUT_1;
2014                 uiBoundsBlock(block, 10);
2015                 
2016                 block->block_event_func= ui_picker_small_wheel;
2017         }
2018         else {
2019                 uiBlockPickerNew(block, handle->retvec, hsvcol, oldcol, hexcol, 'p', 0);
2020                 block->flag= UI_BLOCK_LOOP|UI_BLOCK_REDRAW|UI_BLOCK_KEEP_OPEN;
2021                 uiBoundsBlock(block, 10);
2022                 
2023                 block->block_event_func= ui_picker_small_wheel;
2024         }
2025         
2026         
2027         /* and lets go */
2028         block->direction= UI_TOP;
2029         
2030         return block;
2031 }
2032
2033 /* ******************** Color band *************** */
2034
2035 static int vergcband(const void *a1, const void *a2)
2036 {
2037         const CBData *x1=a1, *x2=a2;
2038         
2039         if( x1->pos > x2->pos ) return 1;
2040         else if( x1->pos < x2->pos) return -1;
2041         return 0;
2042 }
2043
2044 static void colorband_pos_cb(bContext *C, void *coba_v, void *unused_v)
2045 {
2046         ColorBand *coba= coba_v;
2047         int a;
2048         
2049         if(coba->tot<2) return;
2050         
2051         for(a=0; a<coba->tot; a++) coba->data[a].cur= a;
2052         qsort(coba->data, coba->tot, sizeof(CBData), vergcband);
2053         for(a=0; a<coba->tot; a++) {
2054                 if(coba->data[a].cur==coba->cur) {
2055                         /* if(coba->cur!=a) addqueue(curarea->win, REDRAW, 0); */       /* button cur */
2056                         coba->cur= a;
2057                         break;
2058                 }
2059         }
2060 }
2061
2062 static void colorband_add_cb(bContext *C, void *coba_v, void *unused_v)
2063 {
2064         ColorBand *coba= coba_v;
2065         
2066         if(coba->tot < MAXCOLORBAND-1) coba->tot++;
2067         coba->cur= coba->tot-1;
2068         
2069         colorband_pos_cb(C, coba, NULL);
2070 //      BIF_undo_push("Add colorband");
2071         
2072 }
2073
2074 static void colorband_del_cb(bContext *C, void *coba_v, void *unused_v)
2075 {
2076         ColorBand *coba= coba_v;
2077         int a;
2078         
2079         if(coba->tot<2) return;
2080         
2081         for(a=coba->cur; a<coba->tot; a++) {
2082                 coba->data[a]= coba->data[a+1];
2083         }
2084         if(coba->cur) coba->cur--;
2085         coba->tot--;
2086         
2087 //      BIF_undo_push("Delete colorband");
2088 //      BIF_preview_changed(ID_TE);
2089 }
2090
2091 void uiBlockColorbandButtons(uiBlock *block, ColorBand *coba, rctf *butr, int event)
2092 {
2093         CBData *cbd;
2094         uiBut *bt;
2095         float unit= (butr->xmax-butr->xmin)/14.0f;
2096         float xs= butr->xmin;
2097         
2098         cbd= coba->data + coba->cur;
2099         
2100         uiBlockBeginAlign(block);
2101         uiDefButF(block, COL, event,            "",                     xs,butr->ymin+20.0f,2.0f*unit,20,                               &(cbd->r), 0, 0, 0, 0, "");
2102         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, "");
2103         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");
2104         uiButSetFunc(bt, colorband_add_cb, coba, NULL);
2105         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");
2106         uiButSetFunc(bt, colorband_del_cb, coba, NULL);
2107         
2108         uiDefButS(block, MENU, event,           "Interpolation %t|Ease %x1|Cardinal %x3|Linear %x0|B-Spline %x2|Constant %x4",
2109                           xs + 10.0f*unit, butr->ymin+20.0f, unit*4.0f, 20,             &coba->ipotype, 0.0, 0.0, 0, 0, "Sets interpolation type");
2110         
2111         uiDefBut(block, BUT_COLORBAND, event, "",               xs, butr->ymin, butr->xmax-butr->xmin, 20.0f, coba, 0, 0, 0, 0, "");
2112         uiBlockEndAlign(block);
2113         
2114 }
2115
2116
2117 /************************ Popup Menu Memory ****************************/
2118
2119 static int ui_popup_menu_hash(char *str)
2120 {
2121         return BLI_ghashutil_strhash(str);
2122 }
2123
2124 /* but == NULL read, otherwise set */
2125 uiBut *ui_popup_menu_memory(uiBlock *block, uiBut *but)
2126 {
2127         static char mem[256], first=1;
2128         int hash= block->puphash;
2129         
2130         if(first) {
2131                 /* init */
2132                 memset(mem, -1, sizeof(mem));
2133                 first= 0;
2134         }
2135
2136         if(but) {
2137                 /* set */
2138                 mem[hash & 255 ]= BLI_findindex(&block->buttons, but);
2139                 return NULL;
2140         }
2141         else {
2142                 /* get */
2143                 return BLI_findlink(&block->buttons, mem[hash & 255]);
2144         }
2145 }
2146
2147 /******************** Popup Menu with callback or string **********************/
2148
2149 struct uiPopupMenu {
2150         uiBlock *block;
2151         uiLayout *layout;
2152         uiBut *but;
2153
2154         int mx, my, popup, slideout;
2155         int startx, starty, maxrow;
2156
2157         uiMenuCreateFunc menu_func;
2158         void *menu_arg;
2159 };
2160
2161 static uiBlock *ui_block_func_POPUP(bContext *C, uiPopupBlockHandle *handle, void *arg_pup)
2162 {
2163         uiBlock *block;
2164         uiBut *bt;
2165         ScrArea *sa;
2166         ARegion *ar;
2167         uiPopupMenu *pup= arg_pup;
2168         int offset, direction, minwidth, flip;
2169
2170         if(pup->menu_func) {
2171                 pup->block->handle= handle;
2172                 pup->menu_func(C, pup->layout, pup->menu_arg);
2173                 pup->block->handle= NULL;
2174         }
2175
2176         if(pup->but) {
2177                 /* minimum width to enforece */
2178                 minwidth= pup->but->x2 - pup->but->x1;
2179
2180                 if(pup->but->type == PULLDOWN || pup->but->menu_create_func) {
2181                         direction= UI_DOWN;
2182                         flip= 1;
2183                 }
2184                 else {
2185                         direction= UI_TOP;
2186                         flip= 0;
2187                 }
2188         }
2189         else {
2190                 minwidth= 50;
2191                 direction= UI_DOWN;
2192                 flip= 1;
2193         }
2194
2195         block= pup->block;
2196         
2197         /* in some cases we create the block before the region,
2198            so we set it delayed here if necessary */
2199         if(BLI_findindex(&handle->region->uiblocks, block) == -1)
2200                 uiBlockSetRegion(block, handle->region);
2201
2202         block->direction= direction;
2203
2204         uiBlockLayoutResolve(block, NULL, NULL);
2205
2206         uiBlockSetFlag(block, UI_BLOCK_MOVEMOUSE_QUIT);
2207         
2208         if(pup->popup) {
2209                 uiBlockSetFlag(block, UI_BLOCK_LOOP|UI_BLOCK_REDRAW|UI_BLOCK_NUMSELECT|UI_BLOCK_RET_1);
2210                 uiBlockSetDirection(block, direction);
2211
2212                 /* offset the mouse position, possibly based on earlier selection */
2213                 offset= 1.5*MENU_BUTTON_HEIGHT;
2214
2215                 if(block->flag & UI_BLOCK_POPUP_MEMORY) {
2216                         bt= ui_popup_menu_memory(block, NULL);
2217
2218                         if(bt)
2219                                 offset= -bt->y1 - 0.5f*MENU_BUTTON_HEIGHT;
2220                 }
2221
2222                 block->minbounds= minwidth;
2223                 uiMenuPopupBoundsBlock(block, 1, 20, offset);
2224         }
2225         else {
2226                 /* for a header menu we set the direction automatic */
2227                 if(!pup->slideout && flip) {
2228                         sa= CTX_wm_area(C);
2229                         ar= CTX_wm_region(C);
2230
2231                         if(sa && sa->headertype==HEADERDOWN) {
2232                                 if(ar && ar->regiontype == RGN_TYPE_HEADER) {
2233                                         uiBlockSetDirection(block, UI_TOP);
2234                                         uiBlockFlipOrder(block);
2235                                 }
2236                         }
2237                 }
2238
2239                 block->minbounds= minwidth;
2240                 uiTextBoundsBlock(block, 50);
2241         }
2242
2243         /* if menu slides out of other menu, override direction */
2244         if(pup->slideout)
2245                 uiBlockSetDirection(block, UI_RIGHT);
2246
2247         uiEndBlock(C, block);
2248
2249         return pup->block;
2250 }
2251
2252 uiPopupBlockHandle *ui_popup_menu_create(bContext *C, ARegion *butregion, uiBut *but, uiMenuCreateFunc menu_func, void *arg, char *str)
2253 {
2254         wmWindow *window= CTX_wm_window(C);
2255         uiStyle *style= U.uistyles.first;
2256         uiPopupBlockHandle *handle;
2257         uiPopupMenu *pup;
2258         
2259         pup= MEM_callocN(sizeof(uiPopupMenu), "menu dummy");
2260         pup->block= uiBeginBlock(C, NULL, "ui_button_menu_create", UI_EMBOSSP);
2261         pup->layout= uiBlockLayout(pup->block, UI_LAYOUT_VERTICAL, UI_LAYOUT_MENU, 0, 0, 200, 0, style);
2262         pup->slideout= (but && (but->block->flag & UI_BLOCK_LOOP));
2263         pup->but= but;
2264         uiLayoutSetOperatorContext(pup->layout, WM_OP_INVOKE_REGION_WIN);
2265
2266         if(!but) {
2267                 /* no button to start from, means we are a popup */
2268                 pup->mx= window->eventstate->x;
2269                 pup->my= window->eventstate->y;
2270                 pup->popup= 1;
2271         }
2272
2273         if(str) {
2274                 /* menu is created from a string */
2275                 pup->menu_func= ui_block_func_MENUSTR;
2276                 pup->menu_arg= str;
2277         }
2278         else {
2279                 /* menu is created from a callback */
2280                 pup->menu_func= menu_func;
2281                 pup->menu_arg= arg;
2282         }
2283         
2284         handle= ui_popup_block_create(C, butregion, but, NULL, ui_block_func_POPUP, pup);
2285
2286         if(!but) {
2287                 handle->popup= 1;
2288
2289                 UI_add_popup_handlers(C, &window->modalhandlers, handle);
2290                 WM_event_add_mousemove(C);
2291         }
2292         
2293         MEM_freeN(pup);
2294
2295         return handle;
2296 }
2297
2298 /******************** Popup Menu API with begin and end ***********************/
2299
2300 /* only return handler, and set optional title */
2301 uiPopupMenu *uiPupMenuBegin(bContext *C, const char *title, int icon)
2302 {
2303         uiStyle *style= U.uistyles.first;
2304         uiPopupMenu *pup= MEM_callocN(sizeof(uiPopupMenu), "popup menu");
2305         uiBut *but;
2306         
2307         pup->block= uiBeginBlock(C, NULL, "uiPupMenuBegin", UI_EMBOSSP);
2308         pup->block->flag |= UI_BLOCK_POPUP_MEMORY;
2309         pup->block->puphash= ui_popup_menu_hash((char*)title);
2310         pup->layout= uiBlockLayout(pup->block, UI_LAYOUT_VERTICAL, UI_LAYOUT_MENU, 0, 0, 200, 0, style);
2311         uiLayoutSetOperatorContext(pup->layout, WM_OP_EXEC_REGION_WIN);
2312
2313         /* create in advance so we can let buttons point to retval already */
2314         pup->block->handle= MEM_callocN(sizeof(uiPopupBlockHandle), "uiPopupBlockHandle");
2315         
2316         /* create title button */
2317         if(title && title[0]) {
2318                 char titlestr[256];
2319                 
2320                 if(icon) {
2321                         sprintf(titlestr, " %s", title);
2322                         uiDefIconTextBut(pup->block, LABEL, 0, icon, titlestr, 0, 0, 200, MENU_BUTTON_HEIGHT, NULL, 0.0, 0.0, 0, 0, "");
2323                 }
2324                 else {
2325                         but= uiDefBut(pup->block, LABEL, 0, (char*)title, 0, 0, 200, MENU_BUTTON_HEIGHT, NULL, 0.0, 0.0, 0, 0, "");
2326                         but->flag= UI_TEXT_LEFT;
2327                 }
2328         }
2329
2330         return pup;
2331 }
2332
2333 /* set the whole structure to work */
2334 void uiPupMenuEnd(bContext *C, uiPopupMenu *pup)
2335 {
2336         wmWindow *window= CTX_wm_window(C);
2337         uiPopupBlockHandle *menu;
2338         
2339         pup->popup= 1;
2340         pup->mx= window->eventstate->x;
2341         pup->my= window->eventstate->y;
2342         
2343         menu= ui_popup_block_create(C, NULL, NULL, NULL, ui_block_func_POPUP, pup);
2344         menu->popup= 1;
2345         
2346         UI_add_popup_handlers(C, &window->modalhandlers, menu);
2347         WM_event_add_mousemove(C);
2348         
2349         MEM_freeN(pup);
2350 }
2351
2352 uiLayout *uiPupMenuLayout(uiPopupMenu *pup)
2353 {
2354         return pup->layout;
2355 }
2356
2357 /*************************** Standard Popup Menus ****************************/
2358
2359 static void operator_name_cb(bContext *C, void *arg, int retval)
2360 {
2361         const char *opname= arg;
2362
2363         if(opname && retval > 0)
2364                 WM_operator_name_call(C, opname, WM_OP_EXEC_DEFAULT, NULL);
2365 }
2366
2367 static void operator_cb(bContext *C, void *arg, int retval)
2368 {
2369         wmOperator *op= arg;
2370         
2371         if(op && retval > 0)
2372                 WM_operator_call(C, op);
2373         else
2374                 WM_operator_free(op);
2375 }
2376
2377 static void confirm_cancel_operator(void *opv)
2378 {
2379         WM_operator_free(opv);
2380 }
2381
2382 static void vconfirm_opname(bContext *C, char *opname, char *title, char *itemfmt, va_list ap)
2383 {
2384         uiPopupBlockHandle *handle;
2385         char *s, buf[512];
2386
2387         s= buf;
2388         if (title) s+= sprintf(s, "%s%%t|", title);
2389         vsprintf(s, itemfmt, ap);
2390
2391         handle= ui_popup_menu_create(C, NULL, NULL, NULL, NULL, buf);
2392
2393         handle->popup_func= operator_name_cb;
2394         handle->popup_arg= opname;
2395 }
2396
2397 static void confirm_operator(bContext *C, wmOperator *op, char *title, char *item)
2398 {
2399         uiPopupBlockHandle *handle;
2400         char *s, buf[512];
2401         
2402         s= buf;
2403         if (title) s+= sprintf(s, "%s%%t|%s", title, item);
2404         
2405         handle= ui_popup_menu_create(C, NULL, NULL, NULL, NULL, buf);
2406
2407         handle->popup_func= operator_cb;
2408         handle->popup_arg= op;
2409         handle->cancel_func= confirm_cancel_operator;
2410 }
2411
2412 void uiPupMenuOkee(bContext *C, char *opname, char *str, ...)
2413 {
2414         va_list ap;
2415         char titlestr[256];
2416
2417         sprintf(titlestr, "OK? %%i%d", ICON_QUESTION);
2418
2419         va_start(ap, str);
2420         vconfirm_opname(C, opname, titlestr, str, ap);
2421         va_end(ap);
2422 }
2423
2424 void uiPupMenuSaveOver(bContext *C, wmOperator *op, char *filename)
2425 {
2426         size_t len= strlen(filename);
2427
2428         if(len==0)
2429                 return;
2430
2431         if(filename[len-1]=='/' || filename[len-1]=='\\') {
2432                 uiPupMenuError(C, "Cannot overwrite a directory");
2433                 WM_operator_free(op);
2434                 return;
2435         }
2436         if(BLI_exists(filename)==0)
2437                 operator_cb(C, op, 1);
2438         else
2439                 confirm_operator(C, op, "Save Over", filename);
2440 }
2441
2442 void uiPupMenuNotice(bContext *C, char *str, ...)
2443 {
2444         va_list ap;
2445
2446         va_start(ap, str);
2447         vconfirm_opname(C, NULL, NULL, str, ap);
2448         va_end(ap);
2449 }
2450
2451 void uiPupMenuError(bContext *C, char *str, ...)
2452 {
2453         va_list ap;
2454         char nfmt[256];
2455         char titlestr[256];
2456
2457         sprintf(titlestr, "Error %%i%d", ICON_ERROR);
2458
2459         sprintf(nfmt, "%s", str);
2460
2461         va_start(ap, str);
2462         vconfirm_opname(C, NULL, titlestr, nfmt, ap);
2463         va_end(ap);
2464 }
2465
2466 void uiPupMenuReports(bContext *C, ReportList *reports)
2467 {
2468         Report *report;
2469         DynStr *ds;
2470         char *str;
2471
2472         if(!reports || !reports->list.first)
2473                 return;
2474         if(!CTX_wm_window(C))
2475                 return;
2476
2477         ds= BLI_dynstr_new();
2478
2479         for(report=reports->list.first; report; report=report->next) {
2480                 if(report->type >= RPT_ERROR)
2481                         BLI_dynstr_appendf(ds, "Error %%i%d%%t|%s", ICON_ERROR, report->message);
2482                 else if(report->type >= RPT_WARNING)
2483                         BLI_dynstr_appendf(ds, "Warning %%i%d%%t|%s", ICON_ERROR, report->message);
2484                 else if(report->type >= RPT_INFO)
2485                         BLI_dynstr_appendf(ds, "Info %%t|%s", report->message);
2486         }
2487
2488         str= BLI_dynstr_get_cstring(ds);
2489         ui_popup_menu_create(C, NULL, NULL, NULL, NULL, str);
2490         MEM_freeN(str);
2491
2492         BLI_dynstr_free(ds);
2493 }
2494
2495 void uiPupMenuInvoke(bContext *C, const char *idname)
2496 {
2497         uiPopupMenu *pup;
2498         uiLayout *layout;
2499         Menu menu;
2500         MenuType *mt= WM_menutype_find(idname, TRUE);
2501
2502         if(mt==NULL) {
2503                 printf("uiPupMenuInvoke: named menu \"%s\" not found\n", idname);
2504                 return;
2505         }
2506
2507         if(mt->poll && mt->poll(C, mt)==0)
2508                 return;
2509
2510         pup= uiPupMenuBegin(C, mt->label, 0);
2511         layout= uiPupMenuLayout(pup);
2512
2513         menu.layout= layout;
2514         menu.type= mt;
2515
2516         mt->draw(C, &menu);
2517
2518         uiPupMenuEnd(C, pup);
2519 }
2520
2521
2522 /*************************** Popup Block API **************************/
2523
2524 void uiPupBlockO(bContext *C, uiBlockCreateFunc func, void *arg, char *opname, int opcontext)
2525 {
2526         wmWindow *window= CTX_wm_window(C);
2527         uiPopupBlockHandle *handle;
2528         
2529         handle= ui_popup_block_create(C, NULL, NULL, func, NULL, arg);
2530         handle->popup= 1;
2531         handle->optype= (opname)? WM_operatortype_find(opname, 0): NULL;
2532         handle->opcontext= opcontext;
2533         
2534         UI_add_popup_handlers(C, &window->modalhandlers, handle);
2535         WM_event_add_mousemove(C);
2536 }
2537
2538 void uiPupBlock(bContext *C, uiBlockCreateFunc func, void *arg)
2539 {
2540         uiPupBlockO(C, func, arg, NULL, 0);
2541 }
2542
2543 void uiPupBlockOperator(bContext *C, uiBlockCreateFunc func, wmOperator *op, int opcontext)
2544 {
2545         wmWindow *window= CTX_wm_window(C);
2546         uiPopupBlockHandle *handle;
2547         
2548         handle= ui_popup_block_create(C, NULL, NULL, func, NULL, op);
2549         handle->popup= 1;
2550         handle->retvalue= 1;
2551
2552         handle->popup_arg= op;
2553         handle->popup_func= operator_cb;
2554         handle->cancel_func= confirm_cancel_operator;
2555         handle->opcontext= opcontext;
2556         
2557         UI_add_popup_handlers(C, &window->modalhandlers, handle);
2558         WM_event_add_mousemove(C);
2559 }
2560
2561 void uiPupBlockClose(bContext *C, uiBlock *block)
2562 {
2563         if(block->handle) {
2564                 UI_remove_popup_handlers(&CTX_wm_window(C)->modalhandlers, block->handle);
2565                 ui_popup_block_free(C, block->handle);
2566         }
2567 }
2568