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