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