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