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