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