UI:
[blender-staging.git] / source / blender / editors / interface / interface_regions.c
1 /**
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version. 
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17  *
18  * The Original Code is Copyright (C) 2008 Blender Foundation.
19  * All rights reserved.
20  * 
21  * Contributor(s): Blender Foundation
22  *
23  * ***** END GPL LICENSE BLOCK *****
24  */
25
26
27 #include <stdarg.h>
28 #include <stdlib.h>
29 #include <string.h>
30
31 #include "MEM_guardedalloc.h"
32
33 #include "DNA_screen_types.h"
34 #include "DNA_view2d_types.h"
35 #include "DNA_userdef_types.h"
36 #include "DNA_windowmanager_types.h"
37
38 #include "BLI_arithb.h"
39 #include "BLI_blenlib.h"
40 #include "BLI_dynstr.h"
41
42 #include "BKE_context.h"
43 #include "BKE_report.h"
44 #include "BKE_screen.h"
45 #include "BKE_utildefines.h"
46
47 #include "WM_api.h"
48 #include "WM_types.h"
49 #include "wm_draw.h"
50 #include "wm_subwindow.h"
51 #include "wm_window.h"
52
53 #include "RNA_access.h"
54
55 #include "BIF_gl.h"
56
57 #include "UI_interface.h"
58 #include "UI_view2d.h"
59
60 #include "BLF_api.h"
61
62 #include "ED_screen.h"
63
64 #include "interface_intern.h"
65
66 #define MENU_BUTTON_HEIGHT      20
67 #define MENU_SEPR_HEIGHT        6
68 #define B_NOP                   -1
69 #define MENU_SHADOW_SIDE        8
70 #define MENU_SHADOW_BOTTOM      10
71 #define MENU_TOP                        8
72
73 /*********************** Menu Data Parsing ********************* */
74
75 typedef struct {
76         char *str;
77         int retval;
78         int icon;
79 } MenuEntry;
80
81 typedef struct {
82         char *instr;
83         char *title;
84         int titleicon;
85         
86         MenuEntry *items;
87         int nitems, itemssize;
88 } MenuData;
89
90 static MenuData *menudata_new(char *instr)
91 {
92         MenuData *md= MEM_mallocN(sizeof(*md), "MenuData");
93
94         md->instr= instr;
95         md->title= NULL;
96         md->titleicon= 0;
97         md->items= NULL;
98         md->nitems= md->itemssize= 0;
99         
100         return md;
101 }
102
103 static void menudata_set_title(MenuData *md, char *title, int titleicon)
104 {
105         if (!md->title)
106                 md->title= title;
107         if (!md->titleicon)
108                 md->titleicon= titleicon;
109 }
110
111 static void menudata_add_item(MenuData *md, char *str, int retval, int icon)
112 {
113         if (md->nitems==md->itemssize) {
114                 int nsize= md->itemssize?(md->itemssize<<1):1;
115                 MenuEntry *oitems= md->items;
116                 
117                 md->items= MEM_mallocN(nsize*sizeof(*md->items), "md->items");
118                 if (oitems) {
119                         memcpy(md->items, oitems, md->nitems*sizeof(*md->items));
120                         MEM_freeN(oitems);
121                 }
122                 
123                 md->itemssize= nsize;
124         }
125         
126         md->items[md->nitems].str= str;
127         md->items[md->nitems].retval= retval;
128         md->items[md->nitems].icon= icon;
129         md->nitems++;
130 }
131
132 void menudata_free(MenuData *md)
133 {
134         MEM_freeN(md->instr);
135         if (md->items)
136                 MEM_freeN(md->items);
137         MEM_freeN(md);
138 }
139
140         /**
141          * Parse menu description strings, string is of the
142          * form "[sss%t|]{(sss[%xNN]|), (%l|)}", ssss%t indicates the
143          * menu title, sss or sss%xNN indicates an option, 
144          * if %xNN is given then NN is the return value if
145          * that option is selected otherwise the return value
146          * is the index of the option (starting with 1). %l
147          * indicates a seperator.
148          * 
149          * @param str String to be parsed.
150          * @retval new menudata structure, free with menudata_free()
151          */
152 MenuData *decompose_menu_string(char *str) 
153 {
154         char *instr= BLI_strdup(str);
155         MenuData *md= menudata_new(instr);
156         char *nitem= NULL, *s= instr;
157         int nicon=0, nretval= 1, nitem_is_title= 0;
158         
159         while (1) {
160                 char c= *s;
161
162                 if (c=='%') {
163                         if (s[1]=='x') {
164                                 nretval= atoi(s+2);
165
166                                 *s= '\0';
167                                 s++;
168                         } else if (s[1]=='t') {
169                                 nitem_is_title= 1;
170
171                                 *s= '\0';
172                                 s++;
173                         } else if (s[1]=='l') {
174                                 nitem= "%l";
175                                 s++;
176                         } else if (s[1]=='i') {
177                                 nicon= atoi(s+2);
178                                 
179                                 *s= '\0';
180                                 s++;
181                         }
182                 } else if (c=='|' || c=='\0') {
183                         if (nitem) {
184                                 *s= '\0';
185
186                                 if (nitem_is_title) {
187                                         menudata_set_title(md, nitem, nicon);
188                                         nitem_is_title= 0;
189                                 } else {
190                                         /* prevent separator to get a value */
191                                         if(nitem[0]=='%' && nitem[1]=='l')
192                                                 menudata_add_item(md, nitem, -1, nicon);
193                                         else
194                                                 menudata_add_item(md, nitem, nretval, nicon);
195                                         nretval= md->nitems+1;
196                                 } 
197                                 
198                                 nitem= NULL;
199                                 nicon= 0;
200                         }
201                         
202                         if (c=='\0')
203                                 break;
204                 } else if (!nitem)
205                         nitem= s;
206                 
207                 s++;
208         }
209         
210         return md;
211 }
212
213 void ui_set_name_menu(uiBut *but, int value)
214 {
215         MenuData *md;
216         int i;
217         
218         md= decompose_menu_string(but->str);
219         for (i=0; i<md->nitems; i++)
220                 if (md->items[i].retval==value)
221                         strcpy(but->drawstr, md->items[i].str);
222         
223         menudata_free(md);
224 }
225
226 /******************** Creating Temporary regions ******************/
227
228 ARegion *ui_add_temporary_region(bScreen *sc)
229 {
230         ARegion *ar;
231
232         ar= MEM_callocN(sizeof(ARegion), "area region");
233         BLI_addtail(&sc->regionbase, ar);
234
235         ar->regiontype= RGN_TYPE_TEMPORARY;
236         ar->alignment= RGN_ALIGN_FLOAT;
237
238         return ar;
239 }
240
241 void ui_remove_temporary_region(bContext *C, bScreen *sc, ARegion *ar)
242 {
243         if(CTX_wm_window(C))
244                 wm_draw_region_clear(CTX_wm_window(C), ar);
245
246         ED_region_exit(C, ar);
247         BKE_area_region_free(NULL, ar);         /* NULL: no spacetype */
248         BLI_freelinkN(&sc->regionbase, ar);
249 }
250
251 /************************* Creating Tooltips **********************/
252
253 typedef struct uiTooltipData {
254         rcti bbox;
255         uiFontStyle fstyle;
256         char *tip;
257 } uiTooltipData;
258
259 static void ui_tooltip_region_draw(const bContext *C, ARegion *ar)
260 {
261         uiTooltipData *data= ar->regiondata;
262         
263         ui_draw_menu_back(U.uistyles.first, NULL, &data->bbox);
264         
265         /* draw text */
266         glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
267         uiStyleFontSet(&data->fstyle);
268         uiStyleFontDraw(&data->fstyle, &data->bbox, data->tip);
269 }
270
271 static void ui_tooltip_region_free(ARegion *ar)
272 {
273         uiTooltipData *data;
274
275         data= ar->regiondata;
276         MEM_freeN(data->tip);
277         MEM_freeN(data);
278         ar->regiondata= NULL;
279 }
280
281 ARegion *ui_tooltip_create(bContext *C, ARegion *butregion, uiBut *but)
282 {
283         uiStyle *style= U.uistyles.first;       // XXX pass on as arg
284         static ARegionType type;
285         ARegion *ar;
286         uiTooltipData *data;
287         float fonth, fontw, aspect= but->block->aspect;
288         float x1f, x2f, y1f, y2f;
289         int x1, x2, y1, y2, winx, winy, ofsx, ofsy;
290
291         if(!but->tip || strlen(but->tip)==0)
292                 return NULL;
293
294         /* create area region */
295         ar= ui_add_temporary_region(CTX_wm_screen(C));
296
297         memset(&type, 0, sizeof(ARegionType));
298         type.draw= ui_tooltip_region_draw;
299         type.free= ui_tooltip_region_free;
300         ar->type= &type;
301
302         /* create tooltip data */
303         data= MEM_callocN(sizeof(uiTooltipData), "uiTooltipData");
304         data->tip= BLI_strdup(but->tip);
305         
306         /* set font, get bb */
307         data->fstyle= style->widget; /* copy struct */
308         data->fstyle.align= UI_STYLE_TEXT_CENTER;
309         ui_fontscale(&data->fstyle.points, aspect);
310         uiStyleFontSet(&data->fstyle);
311         fontw= aspect * BLF_width(data->tip);
312         fonth= aspect * BLF_height(data->tip);
313
314         ar->regiondata= data;
315
316         /* compute position */
317         ofsx= (but->block->panel)? but->block->panel->ofsx: 0;
318         ofsy= (but->block->panel)? but->block->panel->ofsy: 0;
319
320         x1f= (but->x1+but->x2)/2.0f + ofsx - 16.0f*aspect;
321         x2f= x1f + fontw + 16.0f*aspect;
322         y2f= but->y1 + ofsy - 15.0f*aspect;
323         y1f= y2f - fonth - 10.0f*aspect;
324         
325         /* copy to int, gets projected if possible too */
326         x1= x1f; y1= y1f; x2= x2f; y2= y2f; 
327         
328         if(butregion) {
329                 /* XXX temp, region v2ds can be empty still */
330                 if(butregion->v2d.cur.xmin != butregion->v2d.cur.xmax) {
331                         UI_view2d_to_region_no_clip(&butregion->v2d, x1f, y1f, &x1, &y1);
332                         UI_view2d_to_region_no_clip(&butregion->v2d, x2f, y2f, &x2, &y2);
333                 }
334
335                 x1 += butregion->winrct.xmin;
336                 x2 += butregion->winrct.xmin;
337                 y1 += butregion->winrct.ymin;
338                 y2 += butregion->winrct.ymin;
339         }
340
341         wm_window_get_size(CTX_wm_window(C), &winx, &winy);
342
343         if(x2 > winx) {
344                 /* super size */
345                 if(x2 > winx + x1) {
346                         x2= winx;
347                         x1= 0;
348                 }
349                 else {
350                         x1 -= x2-winx;
351                         x2= winx;
352                 }
353         }
354         if(y1 < 0) {
355                 y1 += 36;
356                 y2 += 36;
357         }
358
359         /* widget rect, in region coords */
360         data->bbox.xmin= MENU_SHADOW_SIDE;
361         data->bbox.xmax= x2-x1 + MENU_SHADOW_SIDE;
362         data->bbox.ymin= MENU_SHADOW_BOTTOM;
363         data->bbox.ymax= y2-y1 + MENU_SHADOW_BOTTOM;
364         
365         /* region bigger for shadow */
366         ar->winrct.xmin= x1 - MENU_SHADOW_SIDE;
367         ar->winrct.xmax= x2 + MENU_SHADOW_SIDE;
368         ar->winrct.ymin= y1 - MENU_SHADOW_BOTTOM;
369         ar->winrct.ymax= y2 + MENU_TOP;
370
371         /* adds subwindow */
372         ED_region_init(C, ar);
373         
374         /* notify change and redraw */
375         ED_region_tag_redraw(ar);
376
377         return ar;
378 }
379
380 void ui_tooltip_free(bContext *C, ARegion *ar)
381 {
382         ui_remove_temporary_region(C, CTX_wm_screen(C), ar);
383 }
384
385 /************************* Creating Menu Blocks **********************/
386
387 /* position block relative to but, result is in window space */
388 static void ui_block_position(wmWindow *window, ARegion *butregion, uiBut *but, uiBlock *block)
389 {
390         uiBut *bt;
391         uiSafetyRct *saferct;
392         rctf butrct;
393         float aspect;
394         int xsize, ysize, xof=0, yof=0, center;
395         short dir1= 0, dir2=0;
396         
397         /* transform to window coordinates, using the source button region/block */
398         butrct.xmin= but->x1; butrct.xmax= but->x2;
399         butrct.ymin= but->y1; butrct.ymax= but->y2;
400
401         ui_block_to_window_fl(butregion, but->block, &butrct.xmin, &butrct.ymin);
402         ui_block_to_window_fl(butregion, but->block, &butrct.xmax, &butrct.ymax);
403
404         /* calc block rect */
405         if(block->minx == 0.0f && block->maxx == 0.0f) {
406                 if(block->buttons.first) {
407                         block->minx= block->miny= 10000;
408                         block->maxx= block->maxy= -10000;
409                         
410                         bt= block->buttons.first;
411                         while(bt) {
412                                 if(bt->x1 < block->minx) block->minx= bt->x1;
413                                 if(bt->y1 < block->miny) block->miny= bt->y1;
414
415                                 if(bt->x2 > block->maxx) block->maxx= bt->x2;
416                                 if(bt->y2 > block->maxy) block->maxy= bt->y2;
417                                 
418                                 bt= bt->next;
419                         }
420                 }
421                 else {
422                         /* we're nice and allow empty blocks too */
423                         block->minx= block->miny= 0;
424                         block->maxx= block->maxy= 20;
425                 }
426         }
427         
428         aspect= (float)(block->maxx - block->minx + 4);
429         ui_block_to_window_fl(butregion, but->block, &block->minx, &block->miny);
430         ui_block_to_window_fl(butregion, but->block, &block->maxx, &block->maxy);
431
432         //block->minx-= 2.0; block->miny-= 2.0;
433         //block->maxx+= 2.0; block->maxy+= 2.0;
434         
435         xsize= block->maxx - block->minx+4; // 4 for shadow
436         ysize= block->maxy - block->miny+4;
437         aspect/= (float)xsize;
438
439         if(but) {
440                 int left=0, right=0, top=0, down=0;
441                 int winx, winy;
442
443                 wm_window_get_size(window, &winx, &winy);
444
445                 if(block->direction & UI_CENTER) center= ysize/2;
446                 else center= 0;
447
448                 if( butrct.xmin-xsize > 0.0) left= 1;
449                 if( butrct.xmax+xsize < winx) right= 1;
450                 if( butrct.ymin-ysize+center > 0.0) down= 1;
451                 if( butrct.ymax+ysize-center < winy) top= 1;
452                 
453                 dir1= block->direction & UI_DIRECTION;
454
455                 /* secundary directions */
456                 if(dir1 & (UI_TOP|UI_DOWN)) {
457                         if(dir1 & UI_LEFT) dir2= UI_LEFT;
458                         else if(dir1 & UI_RIGHT) dir2= UI_RIGHT;
459                         dir1 &= (UI_TOP|UI_DOWN);
460                 }
461
462                 if(dir2==0) if(dir1==UI_LEFT || dir1==UI_RIGHT) dir2= UI_DOWN;
463                 if(dir2==0) if(dir1==UI_TOP || dir1==UI_DOWN) dir2= UI_LEFT;
464                 
465                 /* no space at all? dont change */
466                 if(left || right) {
467                         if(dir1==UI_LEFT && left==0) dir1= UI_RIGHT;
468                         if(dir1==UI_RIGHT && right==0) dir1= UI_LEFT;
469                         /* this is aligning, not append! */
470                         if(dir2==UI_LEFT && right==0) dir2= UI_RIGHT;
471                         if(dir2==UI_RIGHT && left==0) dir2= UI_LEFT;
472                 }
473                 if(down || top) {
474                         if(dir1==UI_TOP && top==0) dir1= UI_DOWN;
475                         if(dir1==UI_DOWN && down==0) dir1= UI_TOP;
476                         if(dir2==UI_TOP && top==0) dir2= UI_DOWN;
477                         if(dir2==UI_DOWN && down==0) dir2= UI_TOP;
478                 }
479                 
480                 if(dir1==UI_LEFT) {
481                         xof= butrct.xmin - block->maxx;
482                         if(dir2==UI_TOP) yof= butrct.ymin - block->miny-center;
483                         else yof= butrct.ymax - block->maxy+center;
484                 }
485                 else if(dir1==UI_RIGHT) {
486                         xof= butrct.xmax - block->minx;
487                         if(dir2==UI_TOP) yof= butrct.ymin - block->miny-center;
488                         else yof= butrct.ymax - block->maxy+center;
489                 }
490                 else if(dir1==UI_TOP) {
491                         yof= butrct.ymax - block->miny;
492                         if(dir2==UI_RIGHT) xof= butrct.xmax - block->maxx;
493                         else xof= butrct.xmin - block->minx;
494                         // changed direction? 
495                         if((dir1 & block->direction)==0) {
496                                 if(block->direction & UI_SHIFT_FLIPPED)
497                                         xof+= dir2==UI_LEFT?25:-25;
498                                 uiBlockFlipOrder(block);
499                         }
500                 }
501                 else if(dir1==UI_DOWN) {
502                         yof= butrct.ymin - block->maxy;
503                         if(dir2==UI_RIGHT) xof= butrct.xmax - block->maxx;
504                         else xof= butrct.xmin - block->minx;
505                         // changed direction?
506                         if((dir1 & block->direction)==0) {
507                                 if(block->direction & UI_SHIFT_FLIPPED)
508                                         xof+= dir2==UI_LEFT?25:-25;
509                                 uiBlockFlipOrder(block);
510                         }
511                 }
512
513                 /* and now we handle the exception; no space below or to top */
514                 if(top==0 && down==0) {
515                         if(dir1==UI_LEFT || dir1==UI_RIGHT) {
516                                 // align with bottom of screen 
517                                 yof= ysize;
518                         }
519                 }
520                 
521                 /* or no space left or right */
522                 if(left==0 && right==0) {
523                         if(dir1==UI_TOP || dir1==UI_DOWN) {
524                                 // align with left size of screen 
525                                 xof= -block->minx+5;
526                         }
527                 }
528                 
529                 // apply requested offset in the block
530                 xof += block->xofs/block->aspect;
531                 yof += block->yofs/block->aspect;
532         }
533         
534         /* apply */
535         
536         for(bt= block->buttons.first; bt; bt= bt->next) {
537                 ui_block_to_window_fl(butregion, but->block, &bt->x1, &bt->y1);
538                 ui_block_to_window_fl(butregion, but->block, &bt->x2, &bt->y2);
539
540                 bt->x1 += xof;
541                 bt->x2 += xof;
542                 bt->y1 += yof;
543                 bt->y2 += yof;
544
545                 bt->aspect= 1.0;
546                 // ui_check_but recalculates drawstring size in pixels
547                 ui_check_but(bt);
548         }
549         
550         block->minx += xof;
551         block->miny += yof;
552         block->maxx += xof;
553         block->maxy += yof;
554
555         /* safety calculus */
556         if(but) {
557                 float midx= (butrct.xmin+butrct.xmax)/2.0;
558                 float midy= (butrct.ymin+butrct.ymax)/2.0;
559                 
560                 /* when you are outside parent button, safety there should be smaller */
561                 
562                 // parent button to left
563                 if( midx < block->minx ) block->safety.xmin= block->minx-3; 
564                 else block->safety.xmin= block->minx-40;
565                 // parent button to right
566                 if( midx > block->maxx ) block->safety.xmax= block->maxx+3; 
567                 else block->safety.xmax= block->maxx+40;
568                 
569                 // parent button on bottom
570                 if( midy < block->miny ) block->safety.ymin= block->miny-3; 
571                 else block->safety.ymin= block->miny-40;
572                 // parent button on top
573                 if( midy > block->maxy ) block->safety.ymax= block->maxy+3; 
574                 else block->safety.ymax= block->maxy+40;
575                 
576                 // exception for switched pulldowns...
577                 if(dir1 && (dir1 & block->direction)==0) {
578                         if(dir2==UI_RIGHT) block->safety.xmax= block->maxx+3; 
579                         if(dir2==UI_LEFT) block->safety.xmin= block->minx-3; 
580                 }
581                 block->direction= dir1;
582         }
583         else {
584                 block->safety.xmin= block->minx-40;
585                 block->safety.ymin= block->miny-40;
586                 block->safety.xmax= block->maxx+40;
587                 block->safety.ymax= block->maxy+40;
588         }
589
590         /* keep a list of these, needed for pulldown menus */
591         saferct= MEM_callocN(sizeof(uiSafetyRct), "uiSafetyRct");
592         saferct->parent= butrct;
593         saferct->safety= block->safety;
594         BLI_freelistN(&block->saferct);
595         if(but)
596                 BLI_duplicatelist(&block->saferct, &but->block->saferct);
597         BLI_addhead(&block->saferct, saferct);
598 }
599
600 static void ui_block_region_draw(const bContext *C, ARegion *ar)
601 {
602         uiBlock *block;
603
604         for(block=ar->uiblocks.first; block; block=block->next)
605                 uiDrawBlock(C, block);
606 }
607
608 uiPopupBlockHandle *ui_popup_block_create(bContext *C, ARegion *butregion, uiBut *but, uiBlockCreateFunc create_func, uiBlockHandleCreateFunc handle_create_func, void *arg)
609 {
610         wmWindow *window= CTX_wm_window(C);
611         static ARegionType type;
612         ARegion *ar;
613         uiBlock *block;
614         uiBut *bt;
615         uiPopupBlockHandle *handle;
616         uiSafetyRct *saferct;
617
618         /* create handle */
619         handle= MEM_callocN(sizeof(uiPopupBlockHandle), "uiPopupBlockHandle");
620
621         /* store context for operator */
622         handle->ctx_area= CTX_wm_area(C);
623         handle->ctx_region= CTX_wm_region(C);
624         
625         /* create area region */
626         ar= ui_add_temporary_region(CTX_wm_screen(C));
627
628         memset(&type, 0, sizeof(ARegionType));
629         type.draw= ui_block_region_draw;
630         ar->type= &type;
631
632         UI_add_region_handlers(&ar->handlers);
633
634         handle->region= ar;
635         ar->regiondata= handle;
636
637         /* create ui block */
638         if(create_func)
639                 block= create_func(C, handle->region, arg);
640         else
641                 block= handle_create_func(C, handle, arg);
642         block->handle= handle;
643
644         if(!block->endblock)
645                 uiEndBlock(C, block);
646
647         /* if this is being created from a button */
648         if(but) {
649                 if(ELEM3(but->type, BLOCK, PULLDOWN, HMENU))
650                         block->xofs = -2;       /* for proper alignment */
651
652                 /* only used for automatic toolbox, so can set the shift flag */
653                 if(but->flag & UI_MAKE_TOP) {
654                         block->direction= UI_TOP|UI_SHIFT_FLIPPED;
655                         uiBlockFlipOrder(block);
656                 }
657                 if(but->flag & UI_MAKE_DOWN) block->direction= UI_DOWN|UI_SHIFT_FLIPPED;
658                 if(but->flag & UI_MAKE_LEFT) block->direction |= UI_LEFT;
659                 if(but->flag & UI_MAKE_RIGHT) block->direction |= UI_RIGHT;
660
661                 ui_block_position(window, butregion, but, block);
662         }
663         else {
664                 /* keep a list of these, needed for pulldown menus */
665                 saferct= MEM_callocN(sizeof(uiSafetyRct), "uiSafetyRct");
666                 saferct->safety= block->safety;
667                 BLI_addhead(&block->saferct, saferct);
668                 block->flag |= UI_BLOCK_POPUP;
669         }
670
671         /* the block and buttons were positioned in window space as in 2.4x, now
672          * these menu blocks are regions so we bring it back to region space.
673          * additionally we add some padding for the menu shadow or rounded menus */
674         ar->winrct.xmin= block->minx - MENU_SHADOW_SIDE;
675         ar->winrct.xmax= block->maxx + MENU_SHADOW_SIDE;
676         ar->winrct.ymin= block->miny - MENU_SHADOW_BOTTOM;
677         ar->winrct.ymax= block->maxy + MENU_TOP;
678
679         block->minx -= ar->winrct.xmin;
680         block->maxx -= ar->winrct.xmin;
681         block->miny -= ar->winrct.ymin;
682         block->maxy -= ar->winrct.ymin;
683
684         for(bt= block->buttons.first; bt; bt= bt->next) {
685                 bt->x1 -= ar->winrct.xmin;
686                 bt->x2 -= ar->winrct.xmin;
687                 bt->y1 -= ar->winrct.ymin;
688                 bt->y2 -= ar->winrct.ymin;
689         }
690
691         block->flag |= UI_BLOCK_LOOP|UI_BLOCK_MOVEMOUSE_QUIT;
692
693         /* adds subwindow */
694         ED_region_init(C, ar);
695
696         /* get winmat now that we actually have the subwindow */
697         wmSubWindowSet(window, ar->swinid);
698                         // XXX ton, AA pixel space...
699         wmOrtho2(0.0, (float)ar->winrct.xmax-ar->winrct.xmin+1, 0.0, (float)ar->winrct.ymax-ar->winrct.ymin+1);
700         
701         wm_subwindow_getmatrix(window, ar->swinid, block->winmat);
702         
703         /* notify change and redraw */
704         ED_region_tag_redraw(ar);
705
706         return handle;
707 }
708
709 void ui_popup_block_free(bContext *C, uiPopupBlockHandle *handle)
710 {
711         ui_remove_temporary_region(C, CTX_wm_screen(C), handle->region);
712         MEM_freeN(handle);
713 }
714
715 /***************************** Menu Button ***************************/
716
717 uiBlock *ui_block_func_MENU(bContext *C, uiPopupBlockHandle *handle, void *arg_but)
718 {
719         uiBut *but= arg_but;
720         uiBlock *block;
721         uiBut *bt;
722         MenuData *md;
723         ListBase lb;
724         float aspect;
725         int width, height, boxh, columns, rows, startx, starty, x1, y1, xmax, a;
726
727         /* create the block */
728         block= uiBeginBlock(C, handle->region, "menu", UI_EMBOSSP);
729         block->flag= UI_BLOCK_LOOP|UI_BLOCK_REDRAW|UI_BLOCK_NUMSELECT;
730
731         /* compute menu data */
732         md= decompose_menu_string(but->str);
733
734         /* columns and row calculation */
735         columns= (md->nitems+20)/20;
736         if(columns<1)
737                 columns= 1;
738         if(columns>8)
739                 columns= (md->nitems+25)/25;
740         
741         rows= md->nitems/columns;
742         if(rows<1)
743                 rows= 1;
744         while(rows*columns<md->nitems)
745                 rows++;
746                 
747         /* prevent scaling up of pupmenu */
748         aspect= but->aspect;
749         if(aspect < 1.0f)
750                 aspect = 1.0f;
751
752         /* size and location */
753         if(md->title)
754                 width= 1.5*aspect*strlen(md->title)+UI_GetStringWidth(md->title);
755         else
756                 width= 0;
757
758         for(a=0; a<md->nitems; a++) {
759                 xmax= aspect*UI_GetStringWidth(md->items[a].str);
760                 if(md->items[a].icon)
761                         xmax += 20*aspect;
762                 if(xmax>width)
763                         width= xmax;
764         }
765
766         width+= 10;
767         if(width < (but->x2 - but->x1))
768                 width = (but->x2 - but->x1);
769         if(width<50)
770                 width=50;
771
772         boxh= MENU_BUTTON_HEIGHT;
773         
774         height= rows*boxh;
775         if(md->title)
776                 height+= boxh;
777
778         /* here we go! */
779         startx= but->x1;
780         starty= but->y1;
781         
782         if(md->title) {
783                 uiBut *bt;
784
785                 if (md->titleicon) {
786                         bt= uiDefIconTextBut(block, LABEL, 0, md->titleicon, md->title, startx, (short)(starty+rows*boxh), (short)width, (short)boxh, NULL, 0.0, 0.0, 0, 0, "");
787                 } else {
788                         bt= uiDefBut(block, LABEL, 0, md->title, startx, (short)(starty+rows*boxh), (short)width, (short)boxh, NULL, 0.0, 0.0, 0, 0, "");
789                         bt->flag= UI_TEXT_LEFT;
790                 }
791         }
792
793         for(a=0; a<md->nitems; a++) {
794                 
795                 x1= startx + width*((int)(md->nitems-a-1)/rows);
796                 y1= starty - boxh*(rows - ((md->nitems - a - 1)%rows)) + (rows*boxh);
797
798                 if (strcmp(md->items[md->nitems-a-1].str, "%l")==0) {
799                         bt= uiDefBut(block, SEPR, B_NOP, "", x1, y1,(short)(width-(rows>1)), (short)(boxh-1), NULL, 0.0, 0.0, 0, 0, "");
800                 }
801                 else if(md->items[md->nitems-a-1].icon) {
802                         bt= uiDefIconTextButF(block, BUTM|FLO, B_NOP, md->items[md->nitems-a-1].icon ,md->items[md->nitems-a-1].str, x1, y1,(short)(width-(rows>1)), (short)(boxh-1), &handle->retvalue, (float) md->items[md->nitems-a-1].retval, 0.0, 0, 0, "");
803                 }
804                 else {
805                         bt= uiDefButF(block, BUTM|FLO, B_NOP, md->items[md->nitems-a-1].str, x1, y1,(short)(width-(rows>1)), (short)(boxh-1), &handle->retvalue, (float) md->items[md->nitems-a-1].retval, 0.0, 0, 0, "");
806                 }
807         }
808         
809         menudata_free(md);
810
811         /* the code up here has flipped locations, because of change of preferred order */
812         /* thats why we have to switch list order too, to make arrowkeys work */
813         
814         lb.first= lb.last= NULL;
815         bt= block->buttons.first;
816         while(bt) {
817                 uiBut *next= bt->next;
818                 BLI_remlink(&block->buttons, bt);
819                 BLI_addhead(&lb, bt);
820                 bt= next;
821         }
822         block->buttons= lb;
823
824         block->direction= UI_TOP;
825         uiEndBlock(C, block);
826
827         return block;
828 }
829
830 uiBlock *ui_block_func_ICONROW(bContext *C, uiPopupBlockHandle *handle, void *arg_but)
831 {
832         uiBut *but= arg_but;
833         uiBlock *block;
834         int a;
835         
836         block= uiBeginBlock(C, handle->region, "menu", UI_EMBOSSP);
837         block->flag= UI_BLOCK_LOOP|UI_BLOCK_REDRAW|UI_BLOCK_NUMSELECT;
838         
839         for(a=(int)but->hardmin; a<=(int)but->hardmax; a++) {
840                 uiDefIconButF(block, BUTM|FLO, B_NOP, but->icon+(a-but->hardmin), 0, (short)(18*a), (short)(but->x2-but->x1-4), 18, &handle->retvalue, (float)a, 0.0, 0, 0, "");
841         }
842
843         block->direction= UI_TOP;       
844
845         uiEndBlock(C, block);
846
847         return block;
848 }
849
850 uiBlock *ui_block_func_ICONTEXTROW(bContext *C, uiPopupBlockHandle *handle, void *arg_but)
851 {
852         uiBut *but= arg_but;
853         uiBlock *block;
854         MenuData *md;
855         int width, xmax, ypos, a;
856
857         block= uiBeginBlock(C, handle->region, "menu", UI_EMBOSSP);
858         block->flag= UI_BLOCK_LOOP|UI_BLOCK_REDRAW|UI_BLOCK_NUMSELECT;
859
860         md= decompose_menu_string(but->str);
861
862         /* size and location */
863         /* expand menu width to fit labels */
864         if(md->title)
865                 width= 2*strlen(md->title)+UI_GetStringWidth(md->title);
866         else
867                 width= 0;
868
869         for(a=0; a<md->nitems; a++) {
870                 xmax= UI_GetStringWidth(md->items[a].str);
871                 if(xmax>width) width= xmax;
872         }
873
874         width+= 30;
875         if (width<50) width=50;
876
877         ypos = 1;
878
879         /* loop through the menu options and draw them out with icons & text labels */
880         for(a=0; a<md->nitems; a++) {
881
882                 /* add a space if there's a separator (%l) */
883                 if (strcmp(md->items[a].str, "%l")==0) {
884                         ypos +=3;
885                 }
886                 else {
887                         uiDefIconTextButF(block, BUTM|FLO, B_NOP, (short)((but->icon)+(md->items[a].retval-but->hardmin)), md->items[a].str, 0, ypos,(short)width, 19, &handle->retvalue, (float) md->items[a].retval, 0.0, 0, 0, "");
888                         ypos += 20;
889                 }
890         }
891         
892         if(md->title) {
893                 uiBut *bt;
894
895                 bt= uiDefBut(block, LABEL, 0, md->title, 0, ypos, (short)width, 19, NULL, 0.0, 0.0, 0, 0, "");
896                 bt->flag= UI_TEXT_LEFT;
897         }
898         
899         menudata_free(md);
900
901         block->direction= UI_TOP;
902
903         uiBoundsBlock(block, 3);
904         uiEndBlock(C, block);
905
906         return block;
907 }
908
909 static void ui_warp_pointer(short x, short y)
910 {
911         /* XXX 2.50 which function to use for this? */
912 #if 0
913         /* OSX has very poor mousewarp support, it sends events;
914            this causes a menu being pressed immediately ... */
915         #ifndef __APPLE__
916         warp_pointer(x, y);
917         #endif
918 #endif
919 }
920
921 /********************* Color Button ****************/
922
923 /* picker sizes S hsize, F full size, D spacer, B button/pallette height  */
924 #define SPICK   110.0
925 #define FPICK   180.0
926 #define DPICK   6.0
927 #define BPICK   24.0
928
929 #define UI_PALETTE_TOT 16
930 /* note; in tot+1 the old color is stored */
931 static float palette[UI_PALETTE_TOT+1][3]= {
932 {0.93, 0.83, 0.81}, {0.88, 0.89, 0.73}, {0.69, 0.81, 0.57}, {0.51, 0.76, 0.64}, 
933 {0.37, 0.56, 0.61}, {0.33, 0.29, 0.55}, {0.46, 0.21, 0.51}, {0.40, 0.12, 0.18}, 
934 {1.0, 1.0, 1.0}, {0.85, 0.85, 0.85}, {0.7, 0.7, 0.7}, {0.56, 0.56, 0.56}, 
935 {0.42, 0.42, 0.42}, {0.28, 0.28, 0.28}, {0.14, 0.14, 0.14}, {0.0, 0.0, 0.0}
936 };  
937
938 /* for picker, while editing hsv */
939 void ui_set_but_hsv(uiBut *but)
940 {
941         float col[3];
942         
943         hsv_to_rgb(but->hsv[0], but->hsv[1], but->hsv[2], col, col+1, col+2);
944         ui_set_but_vectorf(but, col);
945 }
946
947 static void update_picker_hex(uiBlock *block, float *rgb)
948 {
949         uiBut *bt;
950         char col[16];
951         
952         sprintf(col, "%02X%02X%02X", (unsigned int)(rgb[0]*255.0), (unsigned int)(rgb[1]*255.0), (unsigned int)(rgb[2]*255.0));
953         
954         // this updates button strings, is hackish... but button pointers are on stack of caller function
955
956         for(bt= block->buttons.first; bt; bt= bt->next) {
957                 if(strcmp(bt->str, "Hex: ")==0) {
958                         strcpy(bt->poin, col);
959                         ui_check_but(bt);
960                         break;
961                 }
962         }
963 }
964
965 void ui_update_block_buts_hsv(uiBlock *block, float *hsv)
966 {
967         uiBut *bt;
968         float r, g, b;
969         float rgb[3];
970         
971         // this updates button strings, is hackish... but button pointers are on stack of caller function
972         hsv_to_rgb(hsv[0], hsv[1], hsv[2], &r, &g, &b);
973         
974         rgb[0] = r; rgb[1] = g; rgb[2] = b;
975         update_picker_hex(block, rgb);
976
977         for(bt= block->buttons.first; bt; bt= bt->next) {
978                 if(bt->type==HSVCUBE) {
979                         VECCOPY(bt->hsv, hsv);
980                         ui_set_but_hsv(bt);
981                 }
982                 else if(bt->str[1]==' ') {
983                         if(bt->str[0]=='R') {
984                                 ui_set_but_val(bt, r);
985                         }
986                         else if(bt->str[0]=='G') {
987                                 ui_set_but_val(bt, g);
988                         }
989                         else if(bt->str[0]=='B') {
990                                 ui_set_but_val(bt, b);
991                         }
992                         else if(bt->str[0]=='H') {
993                                 ui_set_but_val(bt, hsv[0]);
994                         }
995                         else if(bt->str[0]=='S') {
996                                 ui_set_but_val(bt, hsv[1]);
997                         }
998                         else if(bt->str[0]=='V') {
999                                 ui_set_but_val(bt, hsv[2]);
1000                         }
1001                 }               
1002         }
1003 }
1004
1005 static void ui_update_block_buts_hex(uiBlock *block, char *hexcol)
1006 {
1007         uiBut *bt;
1008         float r=0, g=0, b=0;
1009         float h, s, v;
1010         
1011         
1012         // this updates button strings, is hackish... but button pointers are on stack of caller function
1013         hex_to_rgb(hexcol, &r, &g, &b);
1014         rgb_to_hsv(r, g, b, &h, &s, &v);
1015
1016         for(bt= block->buttons.first; bt; bt= bt->next) {
1017                 if(bt->type==HSVCUBE) {
1018                         bt->hsv[0] = h;
1019                         bt->hsv[1] = s;                 
1020                         bt->hsv[2] = v;
1021                         ui_set_but_hsv(bt);
1022                 }
1023                 else if(bt->str[1]==' ') {
1024                         if(bt->str[0]=='R') {
1025                                 ui_set_but_val(bt, r);
1026                         }
1027                         else if(bt->str[0]=='G') {
1028                                 ui_set_but_val(bt, g);
1029                         }
1030                         else if(bt->str[0]=='B') {
1031                                 ui_set_but_val(bt, b);
1032                         }
1033                         else if(bt->str[0]=='H') {
1034                                 ui_set_but_val(bt, h);
1035                         }
1036                         else if(bt->str[0]=='S') {
1037                                 ui_set_but_val(bt, s);
1038                         }
1039                         else if(bt->str[0]=='V') {
1040                                 ui_set_but_val(bt, v);
1041                         }
1042                 }
1043         }
1044 }
1045
1046 /* bt1 is palette but, col1 is original color */
1047 /* callback to copy from/to palette */
1048 static void do_palette_cb(bContext *C, void *bt1, void *col1)
1049 {
1050         uiBut *but1= (uiBut *)bt1;
1051         float *col= (float *)col1;
1052         float *fp, hsv[3];
1053         
1054         fp= (float *)but1->poin;
1055         
1056         /* XXX 2.50 bad access, how to solve?
1057          *
1058         if( (get_qual() & LR_CTRLKEY) ) {
1059                 VECCOPY(fp, col);
1060         }
1061         else*/ {
1062                 VECCOPY(col, fp);
1063         }
1064         
1065         rgb_to_hsv(col[0], col[1], col[2], hsv, hsv+1, hsv+2);
1066         ui_update_block_buts_hsv(but1->block, hsv);
1067         update_picker_hex(but1->block, col);
1068 }
1069
1070 /* bt1 is num but, hsv1 is pointer to original color in hsv space*/
1071 /* callback to handle changes in num-buts in picker */
1072 static void do_palette1_cb(bContext *C, void *bt1, void *hsv1)
1073 {
1074         uiBut *but1= (uiBut *)bt1;
1075         float *hsv= (float *)hsv1;
1076         float *fp= NULL;
1077         
1078         if(but1->str[1]==' ') {
1079                 if(but1->str[0]=='R') fp= (float *)but1->poin;
1080                 else if(but1->str[0]=='G') fp= ((float *)but1->poin)-1;
1081                 else if(but1->str[0]=='B') fp= ((float *)but1->poin)-2;
1082         }
1083         if(fp) {
1084                 rgb_to_hsv(fp[0], fp[1], fp[2], hsv, hsv+1, hsv+2);
1085         } 
1086         ui_update_block_buts_hsv(but1->block, hsv);
1087 }
1088
1089 /* bt1 is num but, col1 is pointer to original color */
1090 /* callback to handle changes in num-buts in picker */
1091 static void do_palette2_cb(bContext *C, void *bt1, void *col1)
1092 {
1093         uiBut *but1= (uiBut *)bt1;
1094         float *rgb= (float *)col1;
1095         float *fp= NULL;
1096         
1097         if(but1->str[1]==' ') {
1098                 if(but1->str[0]=='H') fp= (float *)but1->poin;
1099                 else if(but1->str[0]=='S') fp= ((float *)but1->poin)-1;
1100                 else if(but1->str[0]=='V') fp= ((float *)but1->poin)-2;
1101         }
1102         if(fp) {
1103                 hsv_to_rgb(fp[0], fp[1], fp[2], rgb, rgb+1, rgb+2);
1104         } 
1105         ui_update_block_buts_hsv(but1->block, fp);
1106 }
1107
1108 static void do_palette_hex_cb(bContext *C, void *bt1, void *hexcl)
1109 {
1110         uiBut *but1= (uiBut *)bt1;
1111         char *hexcol= (char *)hexcl;
1112         
1113         ui_update_block_buts_hex(but1->block, hexcol);  
1114 }
1115
1116 /* used for both 3d view and image window */
1117 static void do_palette_sample_cb(bContext *C, void *bt1, void *col1)    /* frontbuf */
1118 {
1119         /* XXX 2.50 this should become an operator? */
1120 #if 0
1121         uiBut *but1= (uiBut *)bt1;
1122         uiBut *but;
1123         float tempcol[4];
1124         int x=0, y=0;
1125         short mval[2];
1126         float hsv[3];
1127         short capturing;
1128         int oldcursor;
1129         Window *win;
1130         unsigned short dev;
1131         
1132         oldcursor=get_cursor();
1133         win=winlay_get_active_window();
1134         
1135         while (get_mbut() & L_MOUSE) UI_wait_for_statechange();
1136         
1137         SetBlenderCursor(BC_EYEDROPPER_CURSOR);
1138         
1139         /* loop and wait for a mouse click */
1140         capturing = TRUE;
1141         while(capturing) {
1142                 char ascii;
1143                 short val;
1144                 
1145                 dev = extern_qread_ext(&val, &ascii);
1146                 
1147                 if(dev==INPUTCHANGE) break;
1148                 if(get_mbut() & R_MOUSE) break;
1149                 else if(get_mbut() & L_MOUSE) {
1150                         uiGetMouse(mywinget(), mval);
1151                         x= mval[0]; y= mval[1];
1152                         
1153                         capturing = FALSE;
1154                         break;
1155                 }
1156                 else if(dev==ESCKEY) break;
1157         }
1158         window_set_cursor(win, oldcursor);
1159         
1160         if(capturing) return;
1161         
1162         if(x<0 || y<0) return;
1163         
1164         /* if we've got a glick, use OpenGL to sample the color under the mouse pointer */
1165         glReadBuffer(GL_FRONT);
1166         glReadPixels(x, y, 1, 1, GL_RGBA, GL_FLOAT, tempcol);
1167         glReadBuffer(GL_BACK);
1168         
1169         /* and send that color back to the picker */
1170         rgb_to_hsv(tempcol[0], tempcol[1], tempcol[2], hsv, hsv+1, hsv+2);
1171         ui_update_block_buts_hsv(but1->block, hsv);
1172         update_picker_hex(but1->block, tempcol);
1173         
1174         for (but= but1->block->buttons.first; but; but= but->next) {
1175                 ui_check_but(but);
1176                 ui_draw_but(but);
1177         }
1178         
1179         but= but1->block->buttons.first;
1180         ui_block_flush_back(but->block);
1181 #endif
1182 }
1183
1184 /* color picker, Gimp version. mode: 'f' = floating panel, 'p' =  popup */
1185 /* col = read/write to, hsv/old/hexcol = memory for temporal use */
1186 void uiBlockPickerButtons(uiBlock *block, float *col, float *hsv, float *old, char *hexcol, char mode, short retval)
1187 {
1188         uiBut *bt;
1189         float h, offs;
1190         int a;
1191
1192         VECCOPY(old, col);      // old color stored there, for palette_cb to work
1193         
1194         // the cube intersection
1195         bt= uiDefButF(block, HSVCUBE, retval, "",       0,DPICK+BPICK,FPICK,FPICK, col, 0.0, 0.0, 2, 0, "");
1196
1197         bt= uiDefButF(block, HSVCUBE, retval, "",       0,0,FPICK,BPICK, col, 0.0, 0.0, 3, 0, "");
1198
1199         // palette
1200         
1201         uiBlockSetEmboss(block, UI_EMBOSSP);
1202         
1203         bt=uiDefButF(block, COL, retval, "",            FPICK+DPICK, 0, BPICK,BPICK, old, 0.0, 0.0, -1, 0, "Old color, click to restore");
1204         uiButSetFunc(bt, do_palette_cb, bt, col);
1205         uiDefButF(block, COL, retval, "",               FPICK+DPICK, BPICK+DPICK, BPICK,60-BPICK-DPICK, col, 0.0, 0.0, -1, 0, "Active color");
1206
1207         h= (DPICK+BPICK+FPICK-64)/(UI_PALETTE_TOT/2.0);
1208         uiBlockBeginAlign(block);
1209         for(a= -1+UI_PALETTE_TOT/2; a>=0; a--) {
1210                 bt= uiDefButF(block, COL, retval, "",   FPICK+DPICK, 65.0+(float)a*h, BPICK/2, h, palette[a+UI_PALETTE_TOT/2], 0.0, 0.0, -1, 0, "Click to choose, hold CTRL to store in palette");
1211                 uiButSetFunc(bt, do_palette_cb, bt, col);
1212                 bt= uiDefButF(block, COL, retval, "",   FPICK+DPICK+BPICK/2, 65.0+(float)a*h, BPICK/2, h, palette[a], 0.0, 0.0, -1, 0, "Click to choose, hold CTRL to store in palette");               
1213                 uiButSetFunc(bt, do_palette_cb, bt, col);
1214         }
1215         uiBlockEndAlign(block);
1216         
1217         uiBlockSetEmboss(block, UI_EMBOSS);
1218
1219         // buttons
1220         rgb_to_hsv(col[0], col[1], col[2], hsv, hsv+1, hsv+2);
1221         sprintf(hexcol, "%02X%02X%02X", (unsigned int)(col[0]*255.0), (unsigned int)(col[1]*255.0), (unsigned int)(col[2]*255.0));      
1222
1223         offs= FPICK+2*DPICK+BPICK;
1224
1225         /* note; made this a TOG now, with NULL pointer. Is because BUT now gets handled with a afterfunc */
1226         bt= uiDefIconTextBut(block, TOG, UI_RETURN_OK, ICON_EYEDROPPER, "Sample", offs+55, 170, 85, 20, NULL, 0, 0, 0, 0, "Sample the color underneath the following mouse click (ESC or RMB to cancel)");
1227         uiButSetFunc(bt, do_palette_sample_cb, bt, col);
1228         uiButSetFlag(bt, UI_TEXT_LEFT);
1229         
1230         bt= uiDefBut(block, TEX, retval, "Hex: ", offs, 140, 140, 20, hexcol, 0, 8, 0, 0, "Hex triplet for color (#RRGGBB)");
1231         uiButSetFunc(bt, do_palette_hex_cb, bt, hexcol);
1232
1233         uiBlockBeginAlign(block);
1234         bt= uiDefButF(block, NUMSLI, retval, "R ",      offs, 110, 140,20, col, 0.0, 1.0, 10, 3, "");
1235         uiButSetFunc(bt, do_palette1_cb, bt, hsv);
1236         bt= uiDefButF(block, NUMSLI, retval, "G ",      offs, 90, 140,20, col+1, 0.0, 1.0, 10, 3, "");
1237         uiButSetFunc(bt, do_palette1_cb, bt, hsv);
1238         bt= uiDefButF(block, NUMSLI, retval, "B ",      offs, 70, 140,20, col+2, 0.0, 1.0, 10, 3, "");
1239         uiButSetFunc(bt, do_palette1_cb, bt, hsv);
1240         
1241         uiBlockBeginAlign(block);
1242         bt= uiDefButF(block, NUMSLI, retval, "H ",      offs, 40, 140,20, hsv, 0.0, 1.0, 10, 3, "");
1243         uiButSetFunc(bt, do_palette2_cb, bt, col);
1244         bt= uiDefButF(block, NUMSLI, retval, "S ",      offs, 20, 140,20, hsv+1, 0.0, 1.0, 10, 3, "");
1245         uiButSetFunc(bt, do_palette2_cb, bt, col);
1246         bt= uiDefButF(block, NUMSLI, retval, "V ",      offs, 0, 140,20, hsv+2, 0.0, 1.0, 10, 3, "");
1247         uiButSetFunc(bt, do_palette2_cb, bt, col);
1248         uiBlockEndAlign(block);
1249 }
1250
1251 uiBlock *ui_block_func_COL(bContext *C, uiPopupBlockHandle *handle, void *arg_but)
1252 {
1253         uiBut *but= arg_but;
1254         uiBlock *block;
1255         static float hsvcol[3], oldcol[3];
1256         static char hexcol[128];
1257         
1258         block= uiBeginBlock(C, handle->region, "colorpicker", UI_EMBOSS);
1259         block->flag= UI_BLOCK_LOOP|UI_BLOCK_REDRAW|UI_BLOCK_KEEP_OPEN;
1260         
1261         VECCOPY(handle->retvec, but->editvec);
1262         uiBlockPickerButtons(block, handle->retvec, hsvcol, oldcol, hexcol, 'p', 0);
1263
1264         /* and lets go */
1265         block->direction= UI_TOP;
1266         uiBoundsBlock(block, 3);
1267         
1268         return block;
1269 }
1270
1271 /* ******************** PUPmenu ****************** */
1272
1273 static int pupmenu_set= 0;
1274
1275 void uiPupMenuSetActive(int val)
1276 {
1277         pupmenu_set= val;
1278 }
1279
1280 /* value== -1 read, otherwise set */
1281 static int pupmenu_memory(char *str, int value)
1282 {
1283         static char mem[256], first=1;
1284         int val=0, nr=0;
1285         
1286         if(first) {
1287                 memset(mem, 0, 256);
1288                 first= 0;
1289         }
1290         while(str[nr]) {
1291                 val+= str[nr];
1292                 nr++;
1293         }
1294
1295         if(value >= 0) mem[ val & 255 ]= value;
1296         else return mem[ val & 255 ];
1297         
1298         return 0;
1299 }
1300
1301 #define PUP_LABELH      6
1302
1303 typedef struct uiPupMenuInfo {
1304         char *instr;
1305         int mx, my;
1306         int startx, starty;
1307         int maxrow;
1308 } uiPupMenuInfo;
1309
1310 uiBlock *ui_block_func_PUPMENU(bContext *C, uiPopupBlockHandle *handle, void *arg_info)
1311 {
1312         uiBlock *block;
1313         uiPupMenuInfo *info;
1314         int columns, rows, mousemove[2]= {0, 0}, mousewarp= 0;
1315         int width, height, xmax, ymax, maxrow;
1316         int a, startx, starty, endx, endy, x1, y1;
1317         int lastselected;
1318         MenuData *md;
1319
1320         info= arg_info;
1321         maxrow= info->maxrow;
1322         height= 0;
1323
1324         /* block stuff first, need to know the font */
1325         block= uiBeginBlock(C, handle->region, "menu", UI_EMBOSSP);
1326         uiBlockSetFlag(block, UI_BLOCK_LOOP|UI_BLOCK_REDRAW|UI_BLOCK_RET_1|UI_BLOCK_NUMSELECT);
1327         block->direction= UI_DOWN;
1328         
1329         md= decompose_menu_string(info->instr);
1330
1331         rows= md->nitems;
1332         columns= 1;
1333
1334         /* size and location, title slightly bigger for bold */
1335         if(md->title) {
1336                 width= 2*strlen(md->title)+UI_GetStringWidth(md->title);
1337                 width /= columns;
1338         }
1339         else width= 0;
1340
1341         for(a=0; a<md->nitems; a++) {
1342                 xmax= UI_GetStringWidth(md->items[a].str);
1343                 if(xmax>width) width= xmax;
1344
1345                 if(strcmp(md->items[a].str, "%l")==0) height+= PUP_LABELH;
1346                 else height+= MENU_BUTTON_HEIGHT;
1347         }
1348
1349         width+= 10;
1350         if (width<50) width=50;
1351         
1352         wm_window_get_size(CTX_wm_window(C), &xmax, &ymax);
1353
1354         /* set first item */
1355         lastselected= 0;
1356         if(pupmenu_set) {
1357                 lastselected= pupmenu_set-1;
1358                 pupmenu_set= 0;
1359         }
1360         else if(md->nitems>1) {
1361                 lastselected= pupmenu_memory(info->instr, -1);
1362         }
1363
1364         startx= info->mx-(0.8*(width));
1365         starty= info->my-height+MENU_BUTTON_HEIGHT/2;
1366         if(lastselected>=0 && lastselected<md->nitems) {
1367                 for(a=0; a<md->nitems; a++) {
1368                         if(a==lastselected) break;
1369                         if( strcmp(md->items[a].str, "%l")==0) starty+= PUP_LABELH;
1370                         else starty+=MENU_BUTTON_HEIGHT;
1371                 }
1372                 
1373                 //starty= info->my-height+MENU_BUTTON_HEIGHT/2+lastselected*MENU_BUTTON_HEIGHT;
1374         }
1375         
1376         if(startx<10) {
1377                 startx= 10;
1378         }
1379         if(starty<10) {
1380                 mousemove[1]= 10-starty;
1381                 starty= 10;
1382         }
1383         
1384         endx= startx+width*columns;
1385         endy= starty+height;
1386
1387         if(endx>xmax) {
1388                 endx= xmax-10;
1389                 startx= endx-width*columns;
1390         }
1391         if(endy>ymax-20) {
1392                 mousemove[1]= ymax-endy-20;
1393                 endy= ymax-20;
1394                 starty= endy-height;
1395         }
1396
1397         if(mousemove[0] || mousemove[1]) {
1398                 ui_warp_pointer(info->mx+mousemove[0], info->my+mousemove[1]);
1399                 mousemove[0]= info->mx;
1400                 mousemove[1]= info->my;
1401                 mousewarp= 1;
1402         }
1403
1404         /* here we go! */
1405         if(md->title) {
1406                 uiBut *bt;
1407                 char titlestr[256];
1408
1409                 if(md->titleicon) {
1410                         width+= 20;
1411                         sprintf(titlestr, " %s", md->title);
1412                         uiDefIconTextBut(block, LABEL, 0, md->titleicon, titlestr, startx, (short)(starty+height), width, MENU_BUTTON_HEIGHT, NULL, 0.0, 0.0, 0, 0, "");
1413                 }
1414                 else {
1415                         bt= uiDefBut(block, LABEL, 0, md->title, startx, (short)(starty+height), columns*width, MENU_BUTTON_HEIGHT, NULL, 0.0, 0.0, 0, 0, "");
1416                         bt->flag= UI_TEXT_LEFT;
1417                 }
1418                 
1419                 //uiDefBut(block, SEPR, 0, "", startx, (short)(starty+height)-MENU_SEPR_HEIGHT, width, MENU_SEPR_HEIGHT, NULL, 0.0, 0.0, 0, 0, "");
1420         }
1421
1422         x1= startx + width*((int)a/rows);
1423         y1= starty + height - MENU_BUTTON_HEIGHT; // - MENU_SEPR_HEIGHT;
1424                 
1425         for(a=0; a<md->nitems; a++) {
1426                 char *name= md->items[a].str;
1427                 int icon = md->items[a].icon;
1428
1429                 if(strcmp(name, "%l")==0) {
1430                         uiDefBut(block, SEPR, B_NOP, "", x1, y1, width, PUP_LABELH, NULL, 0, 0.0, 0, 0, "");
1431                         y1 -= PUP_LABELH;
1432                 }
1433                 else if (icon) {
1434                         uiDefIconButF(block, BUTM, B_NOP, icon, x1, y1, width+16, MENU_BUTTON_HEIGHT-1, &handle->retvalue, (float) md->items[a].retval, 0.0, 0, 0, "");
1435                         y1 -= MENU_BUTTON_HEIGHT;
1436                 }
1437                 else {
1438                         uiDefButF(block, BUTM, B_NOP, name, x1, y1, width, MENU_BUTTON_HEIGHT-1, &handle->retvalue, (float) md->items[a].retval, 0.0, 0, 0, "");
1439                         y1 -= MENU_BUTTON_HEIGHT;
1440                 }
1441         }
1442         
1443         uiBoundsBlock(block, 1);
1444         uiEndBlock(C, block);
1445
1446         menudata_free(md);
1447
1448         /* XXX 2.5 need to store last selected */
1449 #if 0
1450         /* calculate last selected */
1451         if(event & ui_return_ok) {
1452                 lastselected= 0;
1453                 for(a=0; a<md->nitems; a++) {
1454                         if(val==md->items[a].retval) lastselected= a;
1455                 }
1456                 
1457                 pupmenu_memory(info->instr, lastselected);
1458         }
1459 #endif
1460         
1461         /* XXX 2.5 need to warp back */
1462 #if 0
1463         if(mousemove[1] && (event & ui_return_out)==0)
1464                 ui_warp_pointer(mousemove[0], mousemove[1]);
1465         return val;
1466 #endif
1467
1468         return block;
1469 }
1470
1471 uiBlock *ui_block_func_PUPMENUCOL(bContext *C, uiPopupBlockHandle *handle, void *arg_info)
1472 {
1473         uiBlock *block;
1474         uiPupMenuInfo *info;
1475         int columns, rows, mousemove[2]= {0, 0}, mousewarp;
1476         int width, height, xmax, ymax, maxrow;
1477         int a, startx, starty, endx, endy, x1, y1;
1478         float fvalue;
1479         MenuData *md;
1480
1481         info= arg_info;
1482         maxrow= info->maxrow;
1483         height= 0;
1484
1485         /* block stuff first, need to know the font */
1486         block= uiBeginBlock(C, handle->region, "menu", UI_EMBOSSP);
1487         uiBlockSetFlag(block, UI_BLOCK_LOOP|UI_BLOCK_REDRAW|UI_BLOCK_RET_1|UI_BLOCK_NUMSELECT);
1488         block->direction= UI_DOWN;
1489         
1490         md= decompose_menu_string(info->instr);
1491
1492         /* columns and row calculation */
1493         columns= (md->nitems+maxrow)/maxrow;
1494         if (columns<1) columns= 1;
1495         
1496         if(columns > 8) {
1497                 maxrow += 5;
1498                 columns= (md->nitems+maxrow)/maxrow;
1499         }
1500         
1501         rows= (int) md->nitems/columns;
1502         if (rows<1) rows= 1;
1503         
1504         while (rows*columns<(md->nitems+columns) ) rows++;
1505
1506         /* size and location, title slightly bigger for bold */
1507         if(md->title) {
1508                 width= 2*strlen(md->title)+UI_GetStringWidth(md->title);
1509                 width /= columns;
1510         }
1511         else width= 0;
1512
1513         for(a=0; a<md->nitems; a++) {
1514                 xmax= UI_GetStringWidth(md->items[a].str);
1515                 if(xmax>width) width= xmax;
1516         }
1517
1518         width+= 10;
1519         if (width<50) width=50;
1520         
1521         height= rows*MENU_BUTTON_HEIGHT;
1522         if (md->title) height+= MENU_BUTTON_HEIGHT;
1523         
1524         wm_window_get_size(CTX_wm_window(C), &xmax, &ymax);
1525
1526         /* find active item */
1527         fvalue= handle->retvalue;
1528         for(a=0; a<md->nitems; a++) {
1529                 if( md->items[a].retval== (int)fvalue ) break;
1530         }
1531
1532         /* no active item? */
1533         if(a==md->nitems) {
1534                 if(md->title) a= -1;
1535                 else a= 0;
1536         }
1537
1538         if(a>0)
1539                 startx = info->mx-width/2 - ((int)(a)/rows)*width;
1540         else
1541                 startx= info->mx-width/2;
1542         starty = info->my-height + MENU_BUTTON_HEIGHT/2 + ((a)%rows)*MENU_BUTTON_HEIGHT;
1543
1544         if (md->title) starty+= MENU_BUTTON_HEIGHT;
1545         
1546         if(startx<10) {
1547                 mousemove[0]= 10-startx;
1548                 startx= 10;
1549         }
1550         if(starty<10) {
1551                 mousemove[1]= 10-starty;
1552                 starty= 10;
1553         }
1554         
1555         endx= startx+width*columns;
1556         endy= starty+height;
1557
1558         if(endx>xmax) {
1559                 mousemove[0]= xmax-endx-10;
1560                 endx= xmax-10;
1561                 startx= endx-width*columns;
1562         }
1563         if(endy>ymax) {
1564                 mousemove[1]= ymax-endy-10;
1565                 endy= ymax-10;
1566                 starty= endy-height;
1567         }
1568
1569         if(mousemove[0] || mousemove[1]) {
1570                 ui_warp_pointer(info->mx+mousemove[0], info->my+mousemove[1]);
1571                 mousemove[0]= info->mx;
1572                 mousemove[1]= info->my;
1573                 mousewarp= 1;
1574         }
1575
1576         /* here we go! */
1577         if(md->title) {
1578                 uiBut *bt;
1579
1580                 if(md->titleicon) {
1581                 }
1582                 else {
1583                         bt= uiDefBut(block, LABEL, 0, md->title, startx, (short)(starty+rows*MENU_BUTTON_HEIGHT), columns*width, MENU_BUTTON_HEIGHT, NULL, 0.0, 0.0, 0, 0, "");
1584                         bt->flag= UI_TEXT_LEFT;
1585                 }
1586         }
1587
1588         for(a=0; a<md->nitems; a++) {
1589                 char *name= md->items[a].str;
1590                 int icon = md->items[a].icon;
1591
1592                 x1= startx + width*((int)a/rows);
1593                 y1= starty - MENU_BUTTON_HEIGHT*(a%rows) + (rows-1)*MENU_BUTTON_HEIGHT; 
1594                 
1595                 if(strcmp(name, "%l")==0) {
1596                         uiDefBut(block, SEPR, B_NOP, "", x1, y1, width, PUP_LABELH, NULL, 0, 0.0, 0, 0, "");
1597                         y1 -= PUP_LABELH;
1598                 }
1599                 else if (icon) {
1600                         uiDefIconButF(block, BUTM, B_NOP, icon, x1, y1, width+16, MENU_BUTTON_HEIGHT-1, &handle->retvalue, (float) md->items[a].retval, 0.0, 0, 0, "");
1601                         y1 -= MENU_BUTTON_HEIGHT;
1602                 }
1603                 else {
1604                         uiDefButF(block, BUTM, B_NOP, name, x1, y1, width, MENU_BUTTON_HEIGHT-1, &handle->retvalue, (float) md->items[a].retval, 0.0, 0, 0, "");
1605                         y1 -= MENU_BUTTON_HEIGHT;
1606                 }
1607         }
1608         
1609         uiBoundsBlock(block, 1);
1610         uiEndBlock(C, block);
1611         
1612         menudata_free(md);
1613         
1614         /* XXX 2.5 need to warp back */
1615 #if 0
1616         if((event & UI_RETURN_OUT)==0)
1617                 ui_warp_pointer(mousemove[0], mousemove[1]);
1618 #endif
1619
1620         return block;
1621 }
1622
1623 /************************** Menu Definitions ***************************/
1624
1625 /* prototype */
1626 static uiBlock *ui_block_func_MENU_ITEM(bContext *C, uiPopupBlockHandle *handle, void *arg_info);
1627
1628 #define MAX_MENU_STR    64
1629
1630 /* type, internal */
1631 #define MENU_ITEM_TITLE                         0
1632 #define MENU_ITEM_ITEM                          1
1633 #define MENU_ITEM_SEPARATOR                     2
1634 #define MENU_ITEM_OPNAME                        10
1635 #define MENU_ITEM_OPNAME_BOOL           11
1636 #define MENU_ITEM_OPNAME_ENUM           12
1637 #define MENU_ITEM_OPNAME_INT            13
1638 #define MENU_ITEM_OPNAME_FLOAT          14
1639 #define MENU_ITEM_OPNAME_STRING         15
1640 #define MENU_ITEM_RNA_BOOL                      20
1641 #define MENU_ITEM_RNA_ENUM                      21
1642 #define MENU_ITEM_LEVEL                         30
1643 #define MENU_ITEM_LEVEL_OPNAME_ENUM     31
1644 #define MENU_ITEM_LEVEL_RNA_ENUM        32
1645
1646 struct uiPopupMenu {
1647         uiLayout *layout;
1648         int icon;
1649         char name[MAX_MENU_STR];
1650 };
1651
1652 typedef struct uiMenuInfo {
1653         uiPopupMenu *pup;
1654         int mx, my, popup, slideout;
1655         int startx, starty;
1656 } uiMenuInfo;
1657
1658 /************************ Menu Definitions to uiBlocks ***********************/
1659
1660 const char *ui_menu_enumpropname(char *opname, const char *propname, int retval)
1661 {
1662         wmOperatorType *ot= WM_operatortype_find(opname);
1663         PointerRNA ptr;
1664         PropertyRNA *prop;
1665
1666         if(!ot || !ot->srna)
1667                 return "";
1668         
1669         RNA_pointer_create(NULL, ot->srna, NULL, &ptr);
1670         prop= RNA_struct_find_property(&ptr, propname);
1671         
1672         if(prop) {
1673                 const EnumPropertyItem *item;
1674                 int totitem, i;
1675                 
1676                 RNA_property_enum_items(&ptr, prop, &item, &totitem);
1677                 
1678                 for (i=0; i<totitem; i++) {
1679                         if(item[i].value==retval)
1680                                 return item[i].name;
1681                 }
1682         }
1683
1684         return "";
1685 }
1686
1687 typedef struct MenuItemLevel {
1688         int opcontext;
1689         char *opname;
1690         char *propname;
1691         PointerRNA rnapoin;
1692 } MenuItemLevel;
1693
1694 static uiBlock *ui_block_func_MENU_ITEM(bContext *C, uiPopupBlockHandle *handle, void *arg_info)
1695 {
1696         uiBlock *block;
1697         uiBut *but;
1698         uiMenuInfo *info= arg_info;
1699         uiPopupMenu *pup;
1700         ScrArea *sa;
1701         ARegion *ar;
1702         static int counter= 0;
1703         char str[16];
1704         
1705         pup= info->pup;
1706         
1707         /* block stuff first, need to know the font */
1708         sprintf(str, "tb %d", counter++);
1709         block= uiBeginBlock(C, handle->region, str, UI_EMBOSSP);
1710         block->direction= UI_DOWN;
1711
1712         /* here we go! */
1713         if(pup->name[0]) {
1714                 char titlestr[256];
1715                 
1716                 if(pup->icon) {
1717                         sprintf(titlestr, " %s", pup->name);
1718                         uiDefIconTextBut(block, LABEL, 0, pup->icon, titlestr, 0, 0, 200, MENU_BUTTON_HEIGHT, NULL, 0.0, 0.0, 0, 0, "");
1719                 }
1720                 else {
1721                         but= uiDefBut(block, LABEL, 0, pup->name, 0, 0, 200, MENU_BUTTON_HEIGHT, NULL, 0.0, 0.0, 0, 0, "");
1722                         but->flag= UI_TEXT_LEFT;
1723                 }
1724                 
1725                 //uiDefBut(block, SEPR, 0, "", startx, (short)(starty+height)-MENU_SEPR_HEIGHT, width, MENU_SEPR_HEIGHT, NULL, 0.0, 0.0, 0, 0, "");
1726         }
1727         
1728         block->handle= handle;
1729         uiLayoutEnd(C, block, pup->layout, NULL, NULL);
1730
1731         if(info->popup) {
1732                 uiBlockSetFlag(block, UI_BLOCK_LOOP|UI_BLOCK_REDRAW|UI_BLOCK_NUMSELECT|UI_BLOCK_RET_1);
1733                 uiBlockSetDirection(block, UI_DOWN);
1734
1735                 /* here we set an offset for the mouse position */
1736                 uiMenuPopupBoundsBlock(block, 1, 0, MENU_BUTTON_HEIGHT/2);
1737         }
1738         else {
1739                 /* for a header menu we set the direction automatic */
1740                 if(!info->slideout) {
1741                         sa= CTX_wm_area(C);
1742                         ar= CTX_wm_region(C);
1743
1744                         if(sa && sa->headertype==HEADERDOWN) {
1745                                 if(ar && ar->regiontype == RGN_TYPE_HEADER) {
1746                                         uiBlockSetDirection(block, UI_TOP);
1747                                         uiBlockFlipOrder(block);
1748                                 }
1749                         }
1750                 }
1751
1752                 uiTextBoundsBlock(block, 50);
1753         }
1754
1755         /* if menu slides out of other menu, override direction */
1756         if(info->slideout)
1757                 uiBlockSetDirection(block, UI_RIGHT);
1758
1759         uiEndBlock(C, block);
1760         
1761         return block;
1762 }
1763
1764 uiPopupBlockHandle *ui_popup_menu_create(bContext *C, ARegion *butregion, uiBut *but, uiMenuCreateFunc menu_func, void *arg)
1765 {
1766         uiStyle *style= U.uistyles.first;
1767         uiPopupBlockHandle *handle;
1768         uiPopupMenu *pup;
1769         uiMenuInfo info;
1770         
1771         pup= MEM_callocN(sizeof(uiPopupMenu), "menu dummy");
1772         pup->layout= uiLayoutBegin(UI_LAYOUT_VERTICAL, UI_LAYOUT_MENU, 0, 0, 200, 0, style);
1773         uiLayoutContext(pup->layout, WM_OP_INVOKE_REGION_WIN);
1774         uiLayoutColumn(pup->layout);
1775
1776         menu_func(C, pup->layout, arg);
1777         
1778         memset(&info, 0, sizeof(info));
1779         info.pup= pup;
1780         info.slideout= (but && (but->block->flag & UI_BLOCK_LOOP));
1781         
1782         handle= ui_popup_block_create(C, butregion, but, NULL, ui_block_func_MENU_ITEM, &info);
1783         
1784         MEM_freeN(pup);
1785
1786         return handle;
1787 }
1788
1789 /*************************** Menu Creating API **************************/
1790
1791
1792 /*************************** Popup Menu API **************************/
1793
1794 /* only return handler, and set optional title */
1795 uiPopupMenu *uiPupMenuBegin(const char *title, int icon)
1796 {
1797         uiStyle *style= U.uistyles.first;
1798         uiPopupMenu *pup= MEM_callocN(sizeof(uiPopupMenu), "menu start");
1799         
1800         pup->icon= icon;
1801         pup->layout= uiLayoutBegin(UI_LAYOUT_VERTICAL, UI_LAYOUT_MENU, 0, 0, 200, 0, style);
1802         uiLayoutContext(pup->layout, WM_OP_EXEC_REGION_WIN);
1803         uiLayoutColumn(pup->layout);
1804         
1805         /* NULL is no title */
1806         if(title)
1807                 BLI_strncpy(pup->name, title, MAX_MENU_STR);
1808         
1809         return pup;
1810 }
1811
1812 /* set the whole structure to work */
1813 void uiPupMenuEnd(bContext *C, uiPopupMenu *pup)
1814 {
1815         wmWindow *window= CTX_wm_window(C);
1816         uiMenuInfo info;
1817         uiPopupBlockHandle *menu;
1818         
1819         memset(&info, 0, sizeof(info));
1820         info.popup= 1;
1821         info.mx= window->eventstate->x;
1822         info.my= window->eventstate->y;
1823         info.pup= pup;
1824         
1825         menu= ui_popup_block_create(C, NULL, NULL, NULL, ui_block_func_MENU_ITEM, &info);
1826         menu->popup= 1;
1827         
1828         UI_add_popup_handlers(C, &window->handlers, menu);
1829         WM_event_add_mousemove(C);
1830         
1831         MEM_freeN(pup);
1832 }
1833
1834 uiLayout *uiPupMenuLayout(uiPopupMenu *pup)
1835 {
1836         return pup->layout;
1837 }
1838
1839 /* ************** standard pupmenus *************** */
1840
1841 /* this one can called with operatortype name and operators */
1842 static uiPopupBlockHandle *ui_pup_menu(bContext *C, int maxrow, uiMenuHandleFunc func, void *arg, char *str, ...)
1843 {
1844         wmWindow *window= CTX_wm_window(C);
1845         uiPupMenuInfo info;
1846         uiPopupBlockHandle *menu;
1847
1848         memset(&info, 0, sizeof(info));
1849         info.mx= window->eventstate->x;
1850         info.my= window->eventstate->y;
1851         info.maxrow= maxrow;
1852         info.instr= str;
1853
1854         menu= ui_popup_block_create(C, NULL, NULL, NULL, ui_block_func_PUPMENU, &info);
1855         menu->popup= 1;
1856
1857         UI_add_popup_handlers(C, &window->handlers, menu);
1858         WM_event_add_mousemove(C);
1859
1860         menu->popup_func= func;
1861         menu->popup_arg= arg;
1862         
1863         return menu;
1864 }
1865
1866
1867 static void operator_name_cb(bContext *C, void *arg, int retval)
1868 {
1869         const char *opname= arg;
1870
1871         if(opname && retval > 0)
1872                 WM_operator_name_call(C, opname, WM_OP_EXEC_DEFAULT, NULL);
1873 }
1874
1875 static void vconfirm_opname(bContext *C, char *opname, char *title, char *itemfmt, va_list ap)
1876 {
1877         char *s, buf[512];
1878
1879         s= buf;
1880         if (title) s+= sprintf(s, "%s%%t|", title);
1881         vsprintf(s, itemfmt, ap);
1882
1883         ui_pup_menu(C, 0, operator_name_cb, opname, buf);
1884 }
1885
1886 static void operator_cb(bContext *C, void *arg, int retval)
1887 {
1888         wmOperator *op= arg;
1889         
1890         if(op && retval > 0)
1891                 WM_operator_call(C, op);
1892         else
1893                 WM_operator_free(op);
1894 }
1895
1896 static void confirm_cancel_operator(void *opv)
1897 {
1898         WM_operator_free(opv);
1899 }
1900
1901 static void confirm_operator(bContext *C, wmOperator *op, char *title, char *item)
1902 {
1903         uiPopupBlockHandle *handle;
1904         char *s, buf[512];
1905         
1906         s= buf;
1907         if (title) s+= sprintf(s, "%s%%t|%s", title, item);
1908         
1909         handle= ui_pup_menu(C, 0, operator_cb, op, buf);
1910         handle->cancel_func= confirm_cancel_operator;
1911 }
1912
1913
1914 void uiPupMenuOkee(bContext *C, char *opname, char *str, ...)
1915 {
1916         va_list ap;
1917         char titlestr[256];
1918
1919         sprintf(titlestr, "OK? %%i%d", ICON_HELP);
1920
1921         va_start(ap, str);
1922         vconfirm_opname(C, opname, titlestr, str, ap);
1923         va_end(ap);
1924 }
1925
1926
1927 void uiPupMenuSaveOver(bContext *C, wmOperator *op, char *filename)
1928 {
1929         size_t len= strlen(filename);
1930
1931         if(len==0)
1932                 return;
1933
1934         if(filename[len-1]=='/' || filename[len-1]=='\\') {
1935                 uiPupMenuError(C, "Cannot overwrite a directory");
1936                 WM_operator_free(op);
1937                 return;
1938         }
1939         if(BLI_exists(filename)==0)
1940                 operator_cb(C, op, 1);
1941         else
1942                 confirm_operator(C, op, "Save over", filename);
1943 }
1944
1945 void uiPupMenuNotice(bContext *C, char *str, ...)
1946 {
1947         va_list ap;
1948
1949         va_start(ap, str);
1950         vconfirm_opname(C, NULL, NULL, str, ap);
1951         va_end(ap);
1952 }
1953
1954 void uiPupMenuError(bContext *C, char *str, ...)
1955 {
1956         va_list ap;
1957         char nfmt[256];
1958         char titlestr[256];
1959
1960         sprintf(titlestr, "Error %%i%d", ICON_ERROR);
1961
1962         sprintf(nfmt, "%s", str);
1963
1964         va_start(ap, str);
1965         vconfirm_opname(C, NULL, titlestr, nfmt, ap);
1966         va_end(ap);
1967 }
1968
1969 void uiPupMenuReports(bContext *C, ReportList *reports)
1970 {
1971         Report *report;
1972         DynStr *ds;
1973         char *str;
1974
1975         if(!reports || !reports->list.first)
1976                 return;
1977         if(!CTX_wm_window(C))
1978                 return;
1979
1980         ds= BLI_dynstr_new();
1981
1982         for(report=reports->list.first; report; report=report->next) {
1983                 if(report->type >= RPT_ERROR)
1984                         BLI_dynstr_appendf(ds, "Error %%i%d%%t|%s", ICON_ERROR, report->message);
1985                 else if(report->type >= RPT_WARNING)
1986                         BLI_dynstr_appendf(ds, "Warning %%i%d%%t|%s", ICON_ERROR, report->message);
1987         }
1988
1989         str= BLI_dynstr_get_cstring(ds);
1990         ui_pup_menu(C, 0, NULL, NULL, str);
1991         MEM_freeN(str);
1992
1993         BLI_dynstr_free(ds);
1994 }
1995
1996 /*************************** Popup Block API **************************/
1997
1998 void uiPupBlockO(bContext *C, uiBlockCreateFunc func, void *arg, char *opname, int opcontext)
1999 {
2000         wmWindow *window= CTX_wm_window(C);
2001         uiPopupBlockHandle *handle;
2002         
2003         handle= ui_popup_block_create(C, NULL, NULL, func, NULL, arg);
2004         handle->popup= 1;
2005         handle->opname= opname;
2006         handle->opcontext= opcontext;
2007         
2008         UI_add_popup_handlers(C, &window->handlers, handle);
2009         WM_event_add_mousemove(C);
2010 }
2011
2012 void uiPupBlock(bContext *C, uiBlockCreateFunc func, void *arg)
2013 {
2014         uiPupBlockO(C, func, arg, NULL, 0);
2015 }
2016