View2D: Presets for Initialising Views
[blender-staging.git] / source / blender / editors / interface / interface_regions.c
1
2 #include <stdarg.h>
3 #include <stdlib.h>
4 #include <string.h>
5
6 #include "MEM_guardedalloc.h"
7
8 #include "DNA_screen_types.h"
9 #include "DNA_view2d_types.h"
10 #include "DNA_windowmanager_types.h"
11
12 #include "BLI_arithb.h"
13 #include "BLI_blenlib.h"
14
15 #include "BKE_global.h"
16 #include "BKE_screen.h"
17 #include "BKE_utildefines.h"
18
19 #include "WM_api.h"
20 #include "WM_types.h"
21 #include "wm_subwindow.h"
22 #include "wm_window.h"
23
24 #include "BIF_gl.h"
25
26 #include "UI_interface.h"
27 #include "UI_text.h"
28 #include "UI_view2d.h"
29
30 #include "ED_screen.h"
31
32 #include "interface.h"
33
34 #define MENU_BUTTON_HEIGHT      20
35 #define B_NOP                   -1
36 #define MENU_SHADOW_LEFT        -1
37 #define MENU_SHADOW_BOTTOM      -10
38 #define MENU_SHADOW_RIGHT       10
39 #define MENU_SHADOW_TOP         1
40
41 /*********************** Menu Data Parsing ********************* */
42
43 typedef struct {
44         char *str;
45         int retval;
46         int icon;
47 } MenuEntry;
48
49 typedef struct {
50         char *instr;
51         char *title;
52         int titleicon;
53         
54         MenuEntry *items;
55         int nitems, itemssize;
56 } MenuData;
57
58 static MenuData *menudata_new(char *instr)
59 {
60         MenuData *md= MEM_mallocN(sizeof(*md), "MenuData");
61
62         md->instr= instr;
63         md->title= NULL;
64         md->titleicon= 0;
65         md->items= NULL;
66         md->nitems= md->itemssize= 0;
67         
68         return md;
69 }
70
71 static void menudata_set_title(MenuData *md, char *title, int titleicon)
72 {
73         if (!md->title)
74                 md->title= title;
75         if (!md->titleicon)
76                 md->titleicon= titleicon;
77 }
78
79 static void menudata_add_item(MenuData *md, char *str, int retval, int icon)
80 {
81         if (md->nitems==md->itemssize) {
82                 int nsize= md->itemssize?(md->itemssize<<1):1;
83                 MenuEntry *oitems= md->items;
84                 
85                 md->items= MEM_mallocN(nsize*sizeof(*md->items), "md->items");
86                 if (oitems) {
87                         memcpy(md->items, oitems, md->nitems*sizeof(*md->items));
88                         MEM_freeN(oitems);
89                 }
90                 
91                 md->itemssize= nsize;
92         }
93         
94         md->items[md->nitems].str= str;
95         md->items[md->nitems].retval= retval;
96         md->items[md->nitems].icon= icon;
97         md->nitems++;
98 }
99
100 void menudata_free(MenuData *md)
101 {
102         MEM_freeN(md->instr);
103         if (md->items)
104                 MEM_freeN(md->items);
105         MEM_freeN(md);
106 }
107
108         /**
109          * Parse menu description strings, string is of the
110          * form "[sss%t|]{(sss[%xNN]|), (%l|)}", ssss%t indicates the
111          * menu title, sss or sss%xNN indicates an option, 
112          * if %xNN is given then NN is the return value if
113          * that option is selected otherwise the return value
114          * is the index of the option (starting with 1). %l
115          * indicates a seperator.
116          * 
117          * @param str String to be parsed.
118          * @retval new menudata structure, free with menudata_free()
119          */
120 MenuData *decompose_menu_string(char *str) 
121 {
122         char *instr= BLI_strdup(str);
123         MenuData *md= menudata_new(instr);
124         char *nitem= NULL, *s= instr;
125         int nicon=0, nretval= 1, nitem_is_title= 0;
126         
127         while (1) {
128                 char c= *s;
129
130                 if (c=='%') {
131                         if (s[1]=='x') {
132                                 nretval= atoi(s+2);
133
134                                 *s= '\0';
135                                 s++;
136                         } else if (s[1]=='t') {
137                                 nitem_is_title= 1;
138
139                                 *s= '\0';
140                                 s++;
141                         } else if (s[1]=='l') {
142                                 nitem= "%l";
143                                 s++;
144                         } else if (s[1]=='i') {
145                                 nicon= atoi(s+2);
146                                 
147                                 *s= '\0';
148                                 s++;
149                         }
150                 } else if (c=='|' || c=='\0') {
151                         if (nitem) {
152                                 *s= '\0';
153
154                                 if (nitem_is_title) {
155                                         menudata_set_title(md, nitem, nicon);
156                                         nitem_is_title= 0;
157                                 } else {
158                                         /* prevent separator to get a value */
159                                         if(nitem[0]=='%' && nitem[1]=='l')
160                                                 menudata_add_item(md, nitem, -1, nicon);
161                                         else
162                                                 menudata_add_item(md, nitem, nretval, nicon);
163                                         nretval= md->nitems+1;
164                                 } 
165                                 
166                                 nitem= NULL;
167                                 nicon= 0;
168                         }
169                         
170                         if (c=='\0')
171                                 break;
172                 } else if (!nitem)
173                         nitem= s;
174                 
175                 s++;
176         }
177         
178         return md;
179 }
180
181 void ui_set_name_menu(uiBut *but, int value)
182 {
183         MenuData *md;
184         int i;
185         
186         md= decompose_menu_string(but->str);
187         for (i=0; i<md->nitems; i++)
188                 if (md->items[i].retval==value)
189                         strcpy(but->drawstr, md->items[i].str);
190         
191         menudata_free(md);
192 }
193
194 /******************** Creating Temporary regions ******************/
195
196 ARegion *ui_add_temporary_region(bScreen *sc)
197 {
198         ARegion *ar;
199
200         ar= MEM_callocN(sizeof(ARegion), "area region");
201         BLI_addtail(&sc->regionbase, ar);
202
203         ar->regiontype= RGN_TYPE_TEMPORARY;
204         ar->alignment= RGN_ALIGN_FLOAT;
205
206         return ar;
207 }
208
209 void ui_remove_temporary_region(bContext *C, bScreen *sc, ARegion *ar)
210 {
211         ED_region_exit(C, ar);
212         BKE_area_region_free(ar);
213         BLI_freelinkN(&sc->regionbase, ar);
214 }
215
216 /************************* Creating Tooltips **********************/
217
218 typedef struct uiTooltipData {
219         rctf bbox;
220         struct BMF_Font *font;
221         char *tip;
222         float aspect;
223 } uiTooltipData;
224
225 static void ui_tooltip_region_draw(const bContext *C, ARegion *ar)
226 {
227         uiTooltipData *data;
228         int x1, y1, x2, y2;
229
230         data= ar->regiondata;
231
232         x1= ar->winrct.xmin;
233         y1= ar->winrct.ymin;
234         x2= ar->winrct.xmax;
235         y2= ar->winrct.ymax;
236
237         /* draw drop shadow */
238         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
239         glEnable(GL_BLEND);
240
241         glColor4ub(0, 0, 0, 20);
242         
243         gl_round_box(GL_POLYGON, 3, 3, x2-x1-3, y2-y1-2, 2.0);
244         gl_round_box(GL_POLYGON, 3, 2, x2-x1-2, y2-y1-2, 3.0);
245         
246         glColor4ub(0, 0, 0, 8);
247         
248         gl_round_box(GL_POLYGON, 3, 1, x2-x1-1, y2-y1-3, 4.0);
249         gl_round_box(GL_POLYGON, 3, 0, x2-x1-0, y2-y1-3, 5.0);
250
251         glDisable(GL_BLEND);
252         
253         /* draw background */
254         glColor3f(1.0f, 1.0f, 0.8666f);
255         glRectf(0, 4, x2-x1+4, y2-y1);
256         
257         /* draw text */
258         glColor3ub(0,0,0);
259
260         /* set the position for drawing text +4 in from the left edge, and leaving
261          * an equal gap between the top of the background box and the top of the
262          * string's bbox, and the bottom of the background box, and the bottom of
263          * the string's bbox */
264         ui_rasterpos_safe(4, ((y2-data->bbox.ymax)+(y1+data->bbox.ymin))/2 - data->bbox.ymin - y1, data->aspect);
265         UI_SetScale(1.0);
266
267         UI_DrawString(data->font, data->tip, ui_translate_tooltips());
268 }
269
270 static void ui_tooltip_region_free(ARegion *ar)
271 {
272         uiTooltipData *data;
273
274         data= ar->regiondata;
275         MEM_freeN(data->tip);
276         MEM_freeN(data);
277 }
278
279 ARegion *ui_tooltip_create(bContext *C, ARegion *butregion, uiBut *but)
280 {
281         static ARegionType type;
282         ARegion *ar;
283         uiTooltipData *data;
284         int x1, x2, y1, y2, winx, winy;
285
286         if(!but->tip || strlen(but->tip)==0)
287                 return NULL;
288
289         /* create area region */
290         ar= ui_add_temporary_region(C->window->screen);
291
292         memset(&type, 0, sizeof(ARegionType));
293         type.draw= ui_tooltip_region_draw;
294         type.free= ui_tooltip_region_free;
295         ar->type= &type;
296
297         /* create tooltip data */
298         data= MEM_callocN(sizeof(uiTooltipData), "uiTooltipData");
299         data->tip= BLI_strdup(but->tip);
300         data->font= but->font;
301         data->aspect= but->aspect;
302         UI_GetBoundingBox(data->font, data->tip, ui_translate_tooltips(), &data->bbox);
303
304         ar->regiondata= data;
305
306         /* compute position */
307         x1= (but->x1+but->x2)/2;
308         x2= x1+but->aspect*((data->bbox.xmax-data->bbox.xmin) + 8);
309         y2= but->y1-10;
310         y1= y2-but->aspect*((data->bbox.ymax+(data->bbox.ymax-data->bbox.ymin)));
311
312         y2 += 8;
313         x2 += 8;
314
315         if(butregion) {
316                 /* XXX temp, region v2ds can be empty still */
317                 if(butregion->v2d.cur.xmin != butregion->v2d.cur.xmax) {
318                         short tx, ty;
319                         UI_view2d_to_region_no_clip(&butregion->v2d, x1, y1, &tx, &ty);
320                         x1= (int)tx; y1= (int)ty;
321                         UI_view2d_to_region_no_clip(&butregion->v2d, x2, y2, &tx, &ty);
322                         x2= (int)tx; y2= (int)ty;
323                 }
324
325                 x1 += butregion->winrct.xmin;
326                 x2 += butregion->winrct.xmin;
327                 y1 += butregion->winrct.ymin;
328                 y2 += butregion->winrct.ymin;
329         }
330
331         wm_window_get_size(C->window, &winx, &winy);
332
333         if(x2 > winx) {
334                 /* super size */
335                 if(x2 > winx + x1) {
336                         x2= winx;
337                         x1= 0;
338                 }
339                 else {
340                         x1 -= x2-winx;
341                         x2= winx;
342                 }
343         }
344         if(y1 < 0) {
345                 y1 += 36;
346                 y2 += 36;
347         }
348         
349         ar->winrct.xmin= x1;
350         ar->winrct.ymin= y1;
351         ar->winrct.xmax= x2;
352         ar->winrct.ymax= y2;
353
354         /* adds subwindow */
355         ED_region_init(C, ar);
356         
357         /* notify change and redraw */
358         WM_event_add_notifier(C, WM_NOTE_WINDOW_REDRAW, 0, NULL);
359
360         return ar;
361 }
362
363 void ui_tooltip_free(bContext *C, ARegion *ar)
364 {
365         ui_remove_temporary_region(C, C->window->screen, ar);
366
367         WM_event_add_notifier(C, WM_NOTE_WINDOW_REDRAW, 0, NULL);
368 }
369
370 /************************* Creating Menu Blocks **********************/
371
372 /* position block relative to but, result is in window space */
373 static void ui_block_position(wmWindow *window, ARegion *butregion, uiBut *but, uiBlock *block)
374 {
375         uiBut *bt;
376         uiSafetyRct *saferct;
377         rctf butrct;
378         float aspect;
379         int xsize, ysize, xof=0, yof=0, center;
380         short dir1= 0, dir2=0;
381         
382         /* transform to window coordinates, using the source button region/block */
383         butrct.xmin= but->x1; butrct.xmax= but->x2;
384         butrct.ymin= but->y1; butrct.ymax= but->y2;
385
386         ui_block_to_window_fl(butregion, but->block, &butrct.xmin, &butrct.ymin);
387         ui_block_to_window_fl(butregion, but->block, &butrct.xmax, &butrct.ymax);
388
389         /* calc block rect */
390         if(block->minx == 0.0f && block->maxx == 0.0f) {
391                 if(block->buttons.first) {
392                         block->minx= block->miny= 10000;
393                         block->maxx= block->maxy= -10000;
394                         
395                         bt= block->buttons.first;
396                         while(bt) {
397                                 if(bt->x1 < block->minx) block->minx= bt->x1;
398                                 if(bt->y1 < block->miny) block->miny= bt->y1;
399
400                                 if(bt->x2 > block->maxx) block->maxx= bt->x2;
401                                 if(bt->y2 > block->maxy) block->maxy= bt->y2;
402                                 
403                                 bt= bt->next;
404                         }
405                 }
406                 else {
407                         /* we're nice and allow empty blocks too */
408                         block->minx= block->miny= 0;
409                         block->maxx= block->maxy= 20;
410                 }
411         }
412         
413         aspect= (float)(block->maxx - block->minx + 4);
414         ui_block_to_window_fl(butregion, but->block, &block->minx, &block->miny);
415         ui_block_to_window_fl(butregion, but->block, &block->maxx, &block->maxy);
416
417         //block->minx-= 2.0; block->miny-= 2.0;
418         //block->maxx+= 2.0; block->maxy+= 2.0;
419         
420         xsize= block->maxx - block->minx+4; // 4 for shadow
421         ysize= block->maxy - block->miny+4;
422         aspect/= (float)xsize;
423
424         if(but) {
425                 int left=0, right=0, top=0, down=0;
426                 int winx, winy;
427
428                 wm_window_get_size(window, &winx, &winy);
429
430                 if(block->direction & UI_CENTER) center= ysize/2;
431                 else center= 0;
432
433                 if( butrct.xmin-xsize > 0.0) left= 1;
434                 if( butrct.xmax+xsize < winx) right= 1;
435                 if( butrct.ymin-ysize+center > 0.0) down= 1;
436                 if( butrct.ymax+ysize-center < winy) top= 1;
437                 
438                 dir1= block->direction & UI_DIRECTION;
439
440                 /* secundary directions */
441                 if(dir1 & (UI_TOP|UI_DOWN)) {
442                         if(dir1 & UI_LEFT) dir2= UI_LEFT;
443                         else if(dir1 & UI_RIGHT) dir2= UI_RIGHT;
444                         dir1 &= (UI_TOP|UI_DOWN);
445                 }
446
447                 if(dir2==0) if(dir1==UI_LEFT || dir1==UI_RIGHT) dir2= UI_DOWN;
448                 if(dir2==0) if(dir1==UI_TOP || dir1==UI_DOWN) dir2= UI_LEFT;
449                 
450                 /* no space at all? dont change */
451                 if(left || right) {
452                         if(dir1==UI_LEFT && left==0) dir1= UI_RIGHT;
453                         if(dir1==UI_RIGHT && right==0) dir1= UI_LEFT;
454                         /* this is aligning, not append! */
455                         if(dir2==UI_LEFT && right==0) dir2= UI_RIGHT;
456                         if(dir2==UI_RIGHT && left==0) dir2= UI_LEFT;
457                 }
458                 if(down || top) {
459                         if(dir1==UI_TOP && top==0) dir1= UI_DOWN;
460                         if(dir1==UI_DOWN && down==0) dir1= UI_TOP;
461                         if(dir2==UI_TOP && top==0) dir2= UI_DOWN;
462                         if(dir2==UI_DOWN && down==0) dir2= UI_TOP;
463                 }
464                 
465                 if(dir1==UI_LEFT) {
466                         xof= butrct.xmin - block->maxx;
467                         if(dir2==UI_TOP) yof= butrct.ymin - block->miny-center;
468                         else yof= butrct.ymax - block->maxy+center;
469                 }
470                 else if(dir1==UI_RIGHT) {
471                         xof= butrct.xmax - block->minx;
472                         if(dir2==UI_TOP) yof= butrct.ymin - block->miny-center;
473                         else yof= butrct.ymax - block->maxy+center;
474                 }
475                 else if(dir1==UI_TOP) {
476                         yof= butrct.ymax - block->miny;
477                         if(dir2==UI_RIGHT) xof= butrct.xmax - block->maxx;
478                         else xof= butrct.xmin - block->minx;
479                         // changed direction? 
480                         if((dir1 & block->direction)==0) {
481                                 if(block->direction & UI_SHIFT_FLIPPED)
482                                         xof+= dir2==UI_LEFT?25:-25;
483                                 uiBlockFlipOrder(block);
484                         }
485                 }
486                 else if(dir1==UI_DOWN) {
487                         yof= butrct.ymin - block->maxy;
488                         if(dir2==UI_RIGHT) xof= butrct.xmax - block->maxx;
489                         else xof= butrct.xmin - block->minx;
490                         // changed direction?
491                         if((dir1 & block->direction)==0) {
492                                 if(block->direction & UI_SHIFT_FLIPPED)
493                                         xof+= dir2==UI_LEFT?25:-25;
494                                 uiBlockFlipOrder(block);
495                         }
496                 }
497
498                 /* and now we handle the exception; no space below or to top */
499                 if(top==0 && down==0) {
500                         if(dir1==UI_LEFT || dir1==UI_RIGHT) {
501                                 // align with bottom of screen 
502                                 yof= ysize;
503                         }
504                 }
505                 
506                 /* or no space left or right */
507                 if(left==0 && right==0) {
508                         if(dir1==UI_TOP || dir1==UI_DOWN) {
509                                 // align with left size of screen 
510                                 xof= -block->minx+5;
511                         }
512                 }
513                 
514                 // apply requested offset in the block
515                 xof += block->xofs/block->aspect;
516                 yof += block->yofs/block->aspect;
517         }
518         
519         /* apply */
520         
521         for(bt= block->buttons.first; bt; bt= bt->next) {
522                 ui_block_to_window_fl(butregion, but->block, &bt->x1, &bt->y1);
523                 ui_block_to_window_fl(butregion, but->block, &bt->x2, &bt->y2);
524
525                 bt->x1 += xof;
526                 bt->x2 += xof;
527                 bt->y1 += yof;
528                 bt->y2 += yof;
529
530                 bt->aspect= 1.0;
531                 // ui_check_but recalculates drawstring size in pixels
532                 ui_check_but(bt);
533         }
534         
535         block->minx += xof;
536         block->miny += yof;
537         block->maxx += xof;
538         block->maxy += yof;
539
540         /* safety calculus */
541         if(but) {
542                 float midx= (butrct.xmin+butrct.xmax)/2.0;
543                 float midy= (butrct.ymin+butrct.ymax)/2.0;
544                 
545                 /* when you are outside parent button, safety there should be smaller */
546                 
547                 // parent button to left
548                 if( midx < block->minx ) block->safety.xmin= block->minx-3; 
549                 else block->safety.xmin= block->minx-40;
550                 // parent button to right
551                 if( midx > block->maxx ) block->safety.xmax= block->maxx+3; 
552                 else block->safety.xmax= block->maxx+40;
553                 
554                 // parent button on bottom
555                 if( midy < block->miny ) block->safety.ymin= block->miny-3; 
556                 else block->safety.ymin= block->miny-40;
557                 // parent button on top
558                 if( midy > block->maxy ) block->safety.ymax= block->maxy+3; 
559                 else block->safety.ymax= block->maxy+40;
560                 
561                 // exception for switched pulldowns...
562                 if(dir1 && (dir1 & block->direction)==0) {
563                         if(dir2==UI_RIGHT) block->safety.xmax= block->maxx+3; 
564                         if(dir2==UI_LEFT) block->safety.xmin= block->minx-3; 
565                 }
566                 block->direction= dir1;
567         }
568         else {
569                 block->safety.xmin= block->minx-40;
570                 block->safety.ymin= block->miny-40;
571                 block->safety.xmax= block->maxx+40;
572                 block->safety.ymax= block->maxy+40;
573         }
574
575         /* keep a list of these, needed for pulldown menus */
576         saferct= MEM_callocN(sizeof(uiSafetyRct), "uiSafetyRct");
577         saferct->parent= butrct;
578         saferct->safety= block->safety;
579         BLI_freelistN(&block->saferct);
580         if(but)
581                 BLI_duplicatelist(&block->saferct, &but->block->saferct);
582         BLI_addhead(&block->saferct, saferct);
583 }
584
585 static void ui_block_region_draw(const bContext *C, ARegion *ar)
586 {
587         uiBlock *block;
588
589         for(block=ar->uiblocks.first; block; block=block->next)
590                 uiDrawBlock(block);
591 }
592
593 uiMenuBlockHandle *ui_menu_block_create(bContext *C, ARegion *butregion, uiBut *but, uiBlockFuncFP block_func, void *arg)
594 {
595         static ARegionType type;
596         ARegion *ar;
597         uiBlock *block;
598         uiBut *bt;
599         uiMenuBlockHandle *handle;
600         uiSafetyRct *saferct;
601
602         /* create handle */
603         handle= MEM_callocN(sizeof(uiMenuBlockHandle), "uiMenuBlockHandle");
604
605         /* create area region */
606         ar= ui_add_temporary_region(C->window->screen);
607
608         memset(&type, 0, sizeof(ARegionType));
609         type.draw= ui_block_region_draw;
610         ar->type= &type;
611
612         UI_add_region_handlers(&ar->handlers);
613
614         handle->region= ar;
615         ar->regiondata= handle;
616
617         /* create ui block */
618         block= block_func(C, handle, arg);
619         block->handle= handle;
620
621         /* if this is being created from a button */
622         if(but) {
623                 if(ELEM(but->type, BLOCK, PULLDOWN))
624                         block->xofs = -2;       /* for proper alignment */
625
626                 /* only used for automatic toolbox, so can set the shift flag */
627                 if(but->flag & UI_MAKE_TOP) {
628                         block->direction= UI_TOP|UI_SHIFT_FLIPPED;
629                         uiBlockFlipOrder(block);
630                 }
631                 if(but->flag & UI_MAKE_DOWN) block->direction= UI_DOWN|UI_SHIFT_FLIPPED;
632                 if(but->flag & UI_MAKE_LEFT) block->direction |= UI_LEFT;
633                 if(but->flag & UI_MAKE_RIGHT) block->direction |= UI_RIGHT;
634
635                 ui_block_position(C->window, butregion, but, block);
636         }
637         else {
638                 /* keep a list of these, needed for pulldown menus */
639                 saferct= MEM_callocN(sizeof(uiSafetyRct), "uiSafetyRct");
640                 saferct->safety= block->safety;
641                 BLI_addhead(&block->saferct, saferct);
642         }
643
644         /* the block and buttons were positioned in window space as in 2.4x, now
645          * these menu blocks are regions so we bring it back to region space.
646          * additionally we add some padding for the menu shadow */
647         ar->winrct.xmin= block->minx + MENU_SHADOW_LEFT;
648         ar->winrct.xmax= block->maxx + MENU_SHADOW_RIGHT;
649         ar->winrct.ymin= block->miny + MENU_SHADOW_BOTTOM;
650         ar->winrct.ymax= block->maxy + MENU_SHADOW_TOP;
651
652         block->minx -= ar->winrct.xmin;
653         block->maxx -= ar->winrct.xmin;
654         block->miny -= ar->winrct.ymin;
655         block->maxy -= ar->winrct.ymin;
656
657         for(bt= block->buttons.first; bt; bt= bt->next) {
658                 bt->x1 -= ar->winrct.xmin;
659                 bt->x2 -= ar->winrct.xmin;
660                 bt->y1 -= ar->winrct.ymin;
661                 bt->y2 -= ar->winrct.ymin;
662         }
663
664         block->flag |= UI_BLOCK_LOOP|UI_BLOCK_MOVEMOUSE_QUIT;
665
666         /* adds subwindow */
667         ED_region_init(C, ar);
668
669         /* get winmat now that we actually have the subwindow */
670         wm_subwindow_set(C->window, ar->swinid);
671         wm_subwindow_getmatrix(C->window, ar->swinid, block->winmat);
672         
673         /* notify change and redraw */
674         WM_event_add_notifier(C, WM_NOTE_WINDOW_REDRAW, 0, NULL);
675
676         return handle;
677 }
678
679 void ui_menu_block_free(bContext *C, uiMenuBlockHandle *handle)
680 {
681         ui_remove_temporary_region(C, C->window->screen, handle->region);
682         MEM_freeN(handle);
683
684         WM_event_add_notifier(C, WM_NOTE_WINDOW_REDRAW, 0, NULL);
685 }
686
687 /***************************** Menu Button ***************************/
688
689 uiBlock *ui_block_func_MENU(bContext *C, uiMenuBlockHandle *handle, void *arg_but)
690 {
691         uiBut *but= arg_but;
692         uiBlock *block;
693         uiBut *bt;
694         MenuData *md;
695         ListBase lb;
696         float aspect;
697         int width, height, boxh, columns, rows, startx, starty, x1, y1, xmax, a;
698
699         /* create the block */
700         block= uiBeginBlock(C, handle->region, "menu", UI_EMBOSSP, UI_HELV);
701         block->dt= UI_EMBOSSP;
702         block->flag= UI_BLOCK_LOOP|UI_BLOCK_REDRAW|UI_BLOCK_NUMSELECT;
703         block->themecol= TH_MENU_ITEM;
704
705         /* compute menu data */
706         md= decompose_menu_string(but->str);
707
708         /* columns and row calculation */
709         columns= (md->nitems+20)/20;
710         if(columns<1)
711                 columns= 1;
712         if(columns>8)
713                 columns= (md->nitems+25)/25;
714         
715         rows= md->nitems/columns;
716         if(rows<1)
717                 rows= 1;
718         while(rows*columns<md->nitems)
719                 rows++;
720                 
721         /* prevent scaling up of pupmenu */
722         aspect= but->aspect;
723         if(aspect < 1.0f)
724                 aspect = 1.0f;
725
726         /* size and location */
727         if(md->title)
728                 width= 1.5*aspect*strlen(md->title)+UI_GetStringWidth(block->curfont, md->title, ui_translate_menus());
729         else
730                 width= 0;
731
732         for(a=0; a<md->nitems; a++) {
733                 xmax= aspect*UI_GetStringWidth(block->curfont, md->items[a].str, ui_translate_menus());
734                 if(md->items[a].icon)
735                         xmax += 20*aspect;
736                 if(xmax>width)
737                         width= xmax;
738         }
739
740         width+= 10;
741         if(width < (but->x2 - but->x1))
742                 width = (but->x2 - but->x1);
743         if(width<50)
744                 width=50;
745
746         boxh= MENU_BUTTON_HEIGHT;
747         
748         height= rows*boxh;
749         if(md->title)
750                 height+= boxh;
751
752         /* here we go! */
753         startx= but->x1;
754         starty= but->y1;
755         
756         if(md->title) {
757                 uiBut *bt;
758                 uiSetCurFont(block, block->font+1);
759                 if (md->titleicon) {
760                         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, "");
761                 } else {
762                         bt= uiDefBut(block, LABEL, 0, md->title, startx, (short)(starty+rows*boxh), (short)width, (short)boxh, NULL, 0.0, 0.0, 0, 0, "");
763                         bt->flag= UI_TEXT_LEFT;
764                 }
765                 uiSetCurFont(block, block->font);
766         }
767
768         for(a=0; a<md->nitems; a++) {
769                 
770                 x1= startx + width*((int)(md->nitems-a-1)/rows);
771                 y1= starty - boxh*(rows - ((md->nitems - a - 1)%rows)) + (rows*boxh);
772
773                 if (strcmp(md->items[md->nitems-a-1].str, "%l")==0) {
774                         bt= uiDefBut(block, SEPR, B_NOP, "", x1, y1,(short)(width-(rows>1)), (short)(boxh-1), NULL, 0.0, 0.0, 0, 0, "");
775                 }
776                 else if(md->items[md->nitems-a-1].icon) {
777                         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, "");
778                 }
779                 else {
780                         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, "");
781                 }
782         }
783         
784         menudata_free(md);
785
786         /* the code up here has flipped locations, because of change of preferred order */
787         /* thats why we have to switch list order too, to make arrowkeys work */
788         
789         lb.first= lb.last= NULL;
790         bt= block->buttons.first;
791         while(bt) {
792                 uiBut *next= bt->next;
793                 BLI_remlink(&block->buttons, bt);
794                 BLI_addhead(&lb, bt);
795                 bt= next;
796         }
797         block->buttons= lb;
798
799         block->direction= UI_TOP;
800         uiEndBlock(C, block);
801
802         return block;
803 }
804
805 uiBlock *ui_block_func_ICONROW(bContext *C, uiMenuBlockHandle *handle, void *arg_but)
806 {
807         uiBut *but= arg_but;
808         uiBlock *block;
809         int a;
810         
811         block= uiBeginBlock(C, handle->region, "menu", UI_EMBOSSP, UI_HELV);
812         block->flag= UI_BLOCK_LOOP|UI_BLOCK_REDRAW|UI_BLOCK_NUMSELECT;
813         block->themecol= TH_MENU_ITEM;
814         
815         for(a=(int)but->min; a<=(int)but->max; a++) {
816                 uiDefIconButF(block, BUTM|FLO, B_NOP, but->icon+(a-but->min), 0, (short)(18*a), (short)(but->x2-but->x1-4), 18, &handle->retvalue, (float)a, 0.0, 0, 0, "");
817         }
818
819         block->direction= UI_TOP;       
820
821         uiEndBlock(C, block);
822
823         return block;
824 }
825
826 uiBlock *ui_block_func_ICONTEXTROW(bContext *C, uiMenuBlockHandle *handle, void *arg_but)
827 {
828         uiBut *but= arg_but;
829         uiBlock *block;
830         MenuData *md;
831         int width, xmax, ypos, a;
832
833         block= uiBeginBlock(C, handle->region, "menu", UI_EMBOSSP, UI_HELV);
834         block->flag= UI_BLOCK_LOOP|UI_BLOCK_REDRAW|UI_BLOCK_NUMSELECT;
835         block->themecol= TH_MENU_ITEM;
836
837         md= decompose_menu_string(but->str);
838
839         /* size and location */
840         /* expand menu width to fit labels */
841         if(md->title)
842                 width= 2*strlen(md->title)+UI_GetStringWidth(block->curfont, md->title, ui_translate_menus());
843         else
844                 width= 0;
845
846         for(a=0; a<md->nitems; a++) {
847                 xmax= UI_GetStringWidth(block->curfont, md->items[a].str, ui_translate_menus());
848                 if(xmax>width) width= xmax;
849         }
850
851         width+= 30;
852         if (width<50) width=50;
853
854         ypos = 1;
855
856         /* loop through the menu options and draw them out with icons & text labels */
857         for(a=0; a<md->nitems; a++) {
858
859                 /* add a space if there's a separator (%l) */
860                 if (strcmp(md->items[a].str, "%l")==0) {
861                         ypos +=3;
862                 }
863                 else {
864                         uiDefIconTextButF(block, BUTM|FLO, B_NOP, (short)((but->icon)+(md->items[a].retval-but->min)), md->items[a].str, 0, ypos,(short)width, 19, &handle->retvalue, (float) md->items[a].retval, 0.0, 0, 0, "");
865                         ypos += 20;
866                 }
867         }
868         
869         if(md->title) {
870                 uiBut *bt;
871                 uiSetCurFont(block, block->font+1);
872                 bt= uiDefBut(block, LABEL, 0, md->title, 0, ypos, (short)width, 19, NULL, 0.0, 0.0, 0, 0, "");
873                 uiSetCurFont(block, block->font);
874                 bt->flag= UI_TEXT_LEFT;
875         }
876         
877         menudata_free(md);
878
879         block->direction= UI_TOP;
880
881         uiBoundsBlock(block, 3);
882         uiEndBlock(C, block);
883
884         return block;
885 }
886
887 static void ui_warp_pointer(short x, short y)
888 {
889         /* XXX 2.50 which function to use for this? */
890 #if 0
891         /* OSX has very poor mousewarp support, it sends events;
892            this causes a menu being pressed immediately ... */
893         #ifndef __APPLE__
894         warp_pointer(x, y);
895         #endif
896 #endif
897 }
898
899 /********************* Color Button ****************/
900
901 /* picker sizes S hsize, F full size, D spacer, B button/pallette height  */
902 #define SPICK   110.0
903 #define FPICK   180.0
904 #define DPICK   6.0
905 #define BPICK   24.0
906
907 #define UI_PALETTE_TOT 16
908 /* note; in tot+1 the old color is stored */
909 static float palette[UI_PALETTE_TOT+1][3]= {
910 {0.93, 0.83, 0.81}, {0.88, 0.89, 0.73}, {0.69, 0.81, 0.57}, {0.51, 0.76, 0.64}, 
911 {0.37, 0.56, 0.61}, {0.33, 0.29, 0.55}, {0.46, 0.21, 0.51}, {0.40, 0.12, 0.18}, 
912 {1.0, 1.0, 1.0}, {0.85, 0.85, 0.85}, {0.7, 0.7, 0.7}, {0.56, 0.56, 0.56}, 
913 {0.42, 0.42, 0.42}, {0.28, 0.28, 0.28}, {0.14, 0.14, 0.14}, {0.0, 0.0, 0.0}
914 };  
915
916 /* for picker, while editing hsv */
917 void ui_set_but_hsv(uiBut *but)
918 {
919         float col[3];
920         
921         hsv_to_rgb(but->hsv[0], but->hsv[1], but->hsv[2], col, col+1, col+2);
922         ui_set_but_vectorf(but, col);
923 }
924
925 static void update_picker_hex(uiBlock *block, float *rgb)
926 {
927         uiBut *bt;
928         char col[16];
929         
930         sprintf(col, "%02X%02X%02X", (unsigned int)(rgb[0]*255.0), (unsigned int)(rgb[1]*255.0), (unsigned int)(rgb[2]*255.0));
931         
932         // this updates button strings, is hackish... but button pointers are on stack of caller function
933
934         for(bt= block->buttons.first; bt; bt= bt->next) {
935                 if(strcmp(bt->str, "Hex: ")==0) {
936                         strcpy(bt->poin, col);
937                         ui_check_but(bt);
938                         break;
939                 }
940         }
941 }
942
943 void ui_update_block_buts_hsv(uiBlock *block, float *hsv)
944 {
945         uiBut *bt;
946         float r, g, b;
947         float rgb[3];
948         
949         // this updates button strings, is hackish... but button pointers are on stack of caller function
950         hsv_to_rgb(hsv[0], hsv[1], hsv[2], &r, &g, &b);
951         
952         rgb[0] = r; rgb[1] = g; rgb[2] = b;
953         update_picker_hex(block, rgb);
954
955         for(bt= block->buttons.first; bt; bt= bt->next) {
956                 if(bt->type==HSVCUBE) {
957                         VECCOPY(bt->hsv, hsv);
958                         ui_set_but_hsv(bt);
959                 }
960                 else if(bt->str[1]==' ') {
961                         if(bt->str[0]=='R') {
962                                 ui_set_but_val(bt, r);
963                         }
964                         else if(bt->str[0]=='G') {
965                                 ui_set_but_val(bt, g);
966                         }
967                         else if(bt->str[0]=='B') {
968                                 ui_set_but_val(bt, b);
969                         }
970                         else if(bt->str[0]=='H') {
971                                 ui_set_but_val(bt, hsv[0]);
972                         }
973                         else if(bt->str[0]=='S') {
974                                 ui_set_but_val(bt, hsv[1]);
975                         }
976                         else if(bt->str[0]=='V') {
977                                 ui_set_but_val(bt, hsv[2]);
978                         }
979                 }               
980         }
981 }
982
983 static void ui_update_block_buts_hex(uiBlock *block, char *hexcol)
984 {
985         uiBut *bt;
986         float r=0, g=0, b=0;
987         float h, s, v;
988         
989         
990         // this updates button strings, is hackish... but button pointers are on stack of caller function
991         hex_to_rgb(hexcol, &r, &g, &b);
992         rgb_to_hsv(r, g, b, &h, &s, &v);
993
994         for(bt= block->buttons.first; bt; bt= bt->next) {
995                 if(bt->type==HSVCUBE) {
996                         bt->hsv[0] = h;
997                         bt->hsv[1] = s;                 
998                         bt->hsv[2] = v;
999                         ui_set_but_hsv(bt);
1000                 }
1001                 else if(bt->str[1]==' ') {
1002                         if(bt->str[0]=='R') {
1003                                 ui_set_but_val(bt, r);
1004                         }
1005                         else if(bt->str[0]=='G') {
1006                                 ui_set_but_val(bt, g);
1007                         }
1008                         else if(bt->str[0]=='B') {
1009                                 ui_set_but_val(bt, b);
1010                         }
1011                         else if(bt->str[0]=='H') {
1012                                 ui_set_but_val(bt, h);
1013                         }
1014                         else if(bt->str[0]=='S') {
1015                                 ui_set_but_val(bt, s);
1016                         }
1017                         else if(bt->str[0]=='V') {
1018                                 ui_set_but_val(bt, v);
1019                         }
1020                 }
1021         }
1022 }
1023
1024 /* bt1 is palette but, col1 is original color */
1025 /* callback to copy from/to palette */
1026 static void do_palette_cb(bContext *C, void *bt1, void *col1)
1027 {
1028         uiBut *but1= (uiBut *)bt1;
1029         float *col= (float *)col1;
1030         float *fp, hsv[3];
1031         
1032         fp= (float *)but1->poin;
1033         
1034         /* XXX 2.50 bad access, how to solve?
1035          *
1036         if( (get_qual() & LR_CTRLKEY) ) {
1037                 VECCOPY(fp, col);
1038         }
1039         else*/ {
1040                 VECCOPY(col, fp);
1041         }
1042         
1043         rgb_to_hsv(col[0], col[1], col[2], hsv, hsv+1, hsv+2);
1044         ui_update_block_buts_hsv(but1->block, hsv);
1045         update_picker_hex(but1->block, col);
1046 }
1047
1048 /* bt1 is num but, hsv1 is pointer to original color in hsv space*/
1049 /* callback to handle changes in num-buts in picker */
1050 static void do_palette1_cb(bContext *C, void *bt1, void *hsv1)
1051 {
1052         uiBut *but1= (uiBut *)bt1;
1053         float *hsv= (float *)hsv1;
1054         float *fp= NULL;
1055         
1056         if(but1->str[1]==' ') {
1057                 if(but1->str[0]=='R') fp= (float *)but1->poin;
1058                 else if(but1->str[0]=='G') fp= ((float *)but1->poin)-1;
1059                 else if(but1->str[0]=='B') fp= ((float *)but1->poin)-2;
1060         }
1061         if(fp) {
1062                 rgb_to_hsv(fp[0], fp[1], fp[2], hsv, hsv+1, hsv+2);
1063         } 
1064         ui_update_block_buts_hsv(but1->block, hsv);
1065 }
1066
1067 /* bt1 is num but, col1 is pointer to original color */
1068 /* callback to handle changes in num-buts in picker */
1069 static void do_palette2_cb(bContext *C, void *bt1, void *col1)
1070 {
1071         uiBut *but1= (uiBut *)bt1;
1072         float *rgb= (float *)col1;
1073         float *fp= NULL;
1074         
1075         if(but1->str[1]==' ') {
1076                 if(but1->str[0]=='H') fp= (float *)but1->poin;
1077                 else if(but1->str[0]=='S') fp= ((float *)but1->poin)-1;
1078                 else if(but1->str[0]=='V') fp= ((float *)but1->poin)-2;
1079         }
1080         if(fp) {
1081                 hsv_to_rgb(fp[0], fp[1], fp[2], rgb, rgb+1, rgb+2);
1082         } 
1083         ui_update_block_buts_hsv(but1->block, fp);
1084 }
1085
1086 static void do_palette_hex_cb(bContext *C, void *bt1, void *hexcl)
1087 {
1088         uiBut *but1= (uiBut *)bt1;
1089         char *hexcol= (char *)hexcl;
1090         
1091         ui_update_block_buts_hex(but1->block, hexcol);  
1092 }
1093
1094 /* used for both 3d view and image window */
1095 static void do_palette_sample_cb(bContext *C, void *bt1, void *col1)    /* frontbuf */
1096 {
1097         /* XXX 2.50 this should become an operator? */
1098 #if 0
1099         uiBut *but1= (uiBut *)bt1;
1100         uiBut *but;
1101         float tempcol[4];
1102         int x=0, y=0;
1103         short mval[2];
1104         float hsv[3];
1105         short capturing;
1106         int oldcursor;
1107         Window *win;
1108         unsigned short dev;
1109         
1110         oldcursor=get_cursor();
1111         win=winlay_get_active_window();
1112         
1113         while (get_mbut() & L_MOUSE) UI_wait_for_statechange();
1114         
1115         SetBlenderCursor(BC_EYEDROPPER_CURSOR);
1116         
1117         /* loop and wait for a mouse click */
1118         capturing = TRUE;
1119         while(capturing) {
1120                 char ascii;
1121                 short val;
1122                 
1123                 dev = extern_qread_ext(&val, &ascii);
1124                 
1125                 if(dev==INPUTCHANGE) break;
1126                 if(get_mbut() & R_MOUSE) break;
1127                 else if(get_mbut() & L_MOUSE) {
1128                         uiGetMouse(mywinget(), mval);
1129                         x= mval[0]; y= mval[1];
1130                         
1131                         capturing = FALSE;
1132                         break;
1133                 }
1134                 else if(dev==ESCKEY) break;
1135         }
1136         window_set_cursor(win, oldcursor);
1137         
1138         if(capturing) return;
1139         
1140         if(x<0 || y<0) return;
1141         
1142         /* if we've got a glick, use OpenGL to sample the color under the mouse pointer */
1143         glReadBuffer(GL_FRONT);
1144         glReadPixels(x, y, 1, 1, GL_RGBA, GL_FLOAT, tempcol);
1145         glReadBuffer(GL_BACK);
1146         
1147         /* and send that color back to the picker */
1148         rgb_to_hsv(tempcol[0], tempcol[1], tempcol[2], hsv, hsv+1, hsv+2);
1149         ui_update_block_buts_hsv(but1->block, hsv);
1150         update_picker_hex(but1->block, tempcol);
1151         
1152         for (but= but1->block->buttons.first; but; but= but->next) {
1153                 ui_check_but(but);
1154                 ui_draw_but(but);
1155         }
1156         
1157         but= but1->block->buttons.first;
1158         ui_block_flush_back(but->block);
1159 #endif
1160 }
1161
1162 /* color picker, Gimp version. mode: 'f' = floating panel, 'p' =  popup */
1163 /* col = read/write to, hsv/old/hexcol = memory for temporal use */
1164 void uiBlockPickerButtons(uiBlock *block, float *col, float *hsv, float *old, char *hexcol, char mode, short retval)
1165 {
1166         uiBut *bt;
1167         float h, offs;
1168         int a;
1169
1170         VECCOPY(old, col);      // old color stored there, for palette_cb to work
1171         
1172         // the cube intersection
1173         bt= uiDefButF(block, HSVCUBE, retval, "",       0,DPICK+BPICK,FPICK,FPICK, col, 0.0, 0.0, 2, 0, "");
1174         uiButSetFlag(bt, UI_NO_HILITE);
1175
1176         bt= uiDefButF(block, HSVCUBE, retval, "",       0,0,FPICK,BPICK, col, 0.0, 0.0, 3, 0, "");
1177         uiButSetFlag(bt, UI_NO_HILITE);
1178
1179         // palette
1180         
1181         uiBlockSetEmboss(block, UI_EMBOSSP);
1182         
1183         bt=uiDefButF(block, COL, retval, "",            FPICK+DPICK, 0, BPICK,BPICK, old, 0.0, 0.0, -1, 0, "Old color, click to restore");
1184         uiButSetFunc(bt, do_palette_cb, bt, col);
1185         uiDefButF(block, COL, retval, "",               FPICK+DPICK, BPICK+DPICK, BPICK,60-BPICK-DPICK, col, 0.0, 0.0, -1, 0, "Active color");
1186
1187         h= (DPICK+BPICK+FPICK-64)/(UI_PALETTE_TOT/2.0);
1188         uiBlockBeginAlign(block);
1189         for(a= -1+UI_PALETTE_TOT/2; a>=0; a--) {
1190                 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");
1191                 uiButSetFunc(bt, do_palette_cb, bt, col);
1192                 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");               
1193                 uiButSetFunc(bt, do_palette_cb, bt, col);
1194         }
1195         uiBlockEndAlign(block);
1196         
1197         uiBlockSetEmboss(block, UI_EMBOSS);
1198
1199         // buttons
1200         rgb_to_hsv(col[0], col[1], col[2], hsv, hsv+1, hsv+2);
1201         sprintf(hexcol, "%02X%02X%02X", (unsigned int)(col[0]*255.0), (unsigned int)(col[1]*255.0), (unsigned int)(col[2]*255.0));      
1202
1203         offs= FPICK+2*DPICK+BPICK;
1204
1205         /* note; made this a TOG now, with NULL pointer. Is because BUT now gets handled with a afterfunc */
1206         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)");
1207         uiButSetFunc(bt, do_palette_sample_cb, bt, col);
1208         uiButSetFlag(bt, UI_TEXT_LEFT);
1209         
1210         bt= uiDefBut(block, TEX, retval, "Hex: ", offs, 140, 140, 20, hexcol, 0, 8, 0, 0, "Hex triplet for color (#RRGGBB)");
1211         uiButSetFunc(bt, do_palette_hex_cb, bt, hexcol);
1212
1213         uiBlockBeginAlign(block);
1214         bt= uiDefButF(block, NUMSLI, retval, "R ",      offs, 110, 140,20, col, 0.0, 1.0, 10, 3, "");
1215         uiButSetFunc(bt, do_palette1_cb, bt, hsv);
1216         bt= uiDefButF(block, NUMSLI, retval, "G ",      offs, 90, 140,20, col+1, 0.0, 1.0, 10, 3, "");
1217         uiButSetFunc(bt, do_palette1_cb, bt, hsv);
1218         bt= uiDefButF(block, NUMSLI, retval, "B ",      offs, 70, 140,20, col+2, 0.0, 1.0, 10, 3, "");
1219         uiButSetFunc(bt, do_palette1_cb, bt, hsv);
1220         
1221         uiBlockBeginAlign(block);
1222         bt= uiDefButF(block, NUMSLI, retval, "H ",      offs, 40, 140,20, hsv, 0.0, 1.0, 10, 3, "");
1223         uiButSetFunc(bt, do_palette2_cb, bt, col);
1224         bt= uiDefButF(block, NUMSLI, retval, "S ",      offs, 20, 140,20, hsv+1, 0.0, 1.0, 10, 3, "");
1225         uiButSetFunc(bt, do_palette2_cb, bt, col);
1226         bt= uiDefButF(block, NUMSLI, retval, "V ",      offs, 0, 140,20, hsv+2, 0.0, 1.0, 10, 3, "");
1227         uiButSetFunc(bt, do_palette2_cb, bt, col);
1228         uiBlockEndAlign(block);
1229 }
1230
1231 uiBlock *ui_block_func_COL(bContext *C, uiMenuBlockHandle *handle, void *arg_but)
1232 {
1233         uiBut *but= arg_but;
1234         uiBlock *block;
1235         static float hsvcol[3], oldcol[3];
1236         static char hexcol[128];
1237         
1238         block= uiBeginBlock(C, handle->region, "colorpicker", UI_EMBOSS, UI_HELV);
1239         block->flag= UI_BLOCK_LOOP|UI_BLOCK_REDRAW|UI_BLOCK_KEEP_OPEN;
1240         block->themecol= TH_BUT_NUM;
1241         
1242         VECCOPY(handle->retvec, but->editvec);
1243         uiBlockPickerButtons(block, handle->retvec, hsvcol, oldcol, hexcol, 'p', 0);
1244
1245         /* and lets go */
1246         block->direction= UI_TOP;
1247         uiBoundsBlock(block, 3);
1248         
1249         return block;
1250 }
1251
1252 /* ******************** PUPmenu ****************** */
1253
1254 static int pupmenu_set= 0;
1255
1256 void pupmenu_set_active(int val)
1257 {
1258         pupmenu_set= val;
1259 }
1260
1261 /* value== -1 read, otherwise set */
1262 static int pupmenu_memory(char *str, int value)
1263 {
1264         static char mem[256], first=1;
1265         int val=0, nr=0;
1266         
1267         if(first) {
1268                 memset(mem, 0, 256);
1269                 first= 0;
1270         }
1271         while(str[nr]) {
1272                 val+= str[nr];
1273                 nr++;
1274         }
1275
1276         if(value >= 0) mem[ val & 255 ]= value;
1277         else return mem[ val & 255 ];
1278         
1279         return 0;
1280 }
1281
1282 #define PUP_LABELH      6
1283
1284 typedef struct uiPupMenuInfo {
1285         char *instr;
1286         int mx, my;
1287         int startx, starty;
1288         int maxrow;
1289 } uiPupMenuInfo;
1290
1291 uiBlock *ui_block_func_PUPMENU(bContext *C, uiMenuBlockHandle *handle, void *arg_info)
1292 {
1293         uiBlock *block;
1294         uiPupMenuInfo *info;
1295         int columns, rows, mousemove[2]= {0, 0}, mousewarp= 0;
1296         int width, height, xmax, ymax, maxrow;
1297         int a, startx, starty, endx, endy, x1, y1;
1298         int lastselected;
1299         MenuData *md;
1300
1301         info= arg_info;
1302         maxrow= info->maxrow;
1303         height= 0;
1304
1305         /* block stuff first, need to know the font */
1306         block= uiBeginBlock(C, handle->region, "menu", UI_EMBOSSP, UI_HELV);
1307         uiBlockSetFlag(block, UI_BLOCK_LOOP|UI_BLOCK_REDRAW|UI_BLOCK_RET_1|UI_BLOCK_NUMSELECT);
1308         block->themecol= TH_MENU_ITEM;
1309         
1310         md= decompose_menu_string(info->instr);
1311
1312         rows= md->nitems;
1313         columns= 1;
1314
1315         /* size and location, title slightly bigger for bold */
1316         if(md->title) {
1317                 width= 2*strlen(md->title)+UI_GetStringWidth(uiBlockGetCurFont(block), md->title, ui_translate_buttons());
1318                 width /= columns;
1319         }
1320         else width= 0;
1321
1322         for(a=0; a<md->nitems; a++) {
1323                 xmax= UI_GetStringWidth(uiBlockGetCurFont(block), md->items[a].str, ui_translate_buttons());
1324                 if(xmax>width) width= xmax;
1325
1326                 if(strcmp(md->items[a].str, "%l")==0) height+= PUP_LABELH;
1327                 else height+= MENU_BUTTON_HEIGHT;
1328         }
1329
1330         width+= 10;
1331         if (width<50) width=50;
1332         
1333         wm_window_get_size(C->window, &xmax, &ymax);
1334
1335         /* set first item */
1336         lastselected= 0;
1337         if(pupmenu_set) {
1338                 lastselected= pupmenu_set-1;
1339                 pupmenu_set= 0;
1340         }
1341         else if(md->nitems>1) {
1342                 lastselected= pupmenu_memory(info->instr, -1);
1343         }
1344
1345         startx= info->mx-(0.8*(width));
1346         starty= info->my-height+MENU_BUTTON_HEIGHT/2;
1347         if(lastselected>=0 && lastselected<md->nitems) {
1348                 for(a=0; a<md->nitems; a++) {
1349                         if(a==lastselected) break;
1350                         if( strcmp(md->items[a].str, "%l")==0) starty+= PUP_LABELH;
1351                         else starty+=MENU_BUTTON_HEIGHT;
1352                 }
1353                 
1354                 //starty= info->my-height+MENU_BUTTON_HEIGHT/2+lastselected*MENU_BUTTON_HEIGHT;
1355         }
1356         
1357         if(startx<10) {
1358                 startx= 10;
1359         }
1360         if(starty<10) {
1361                 mousemove[1]= 10-starty;
1362                 starty= 10;
1363         }
1364         
1365         endx= startx+width*columns;
1366         endy= starty+height;
1367
1368         if(endx>xmax) {
1369                 endx= xmax-10;
1370                 startx= endx-width*columns;
1371         }
1372         if(endy>ymax-20) {
1373                 mousemove[1]= ymax-endy-20;
1374                 endy= ymax-20;
1375                 starty= endy-height;
1376         }
1377
1378         if(mousemove[0] || mousemove[1]) {
1379                 ui_warp_pointer(info->mx+mousemove[0], info->my+mousemove[1]);
1380                 mousemove[0]= info->mx;
1381                 mousemove[1]= info->my;
1382                 mousewarp= 1;
1383         }
1384
1385         /* here we go! */
1386         if(md->title) {
1387                 uiBut *bt;
1388                 char titlestr[256];
1389                 uiSetCurFont(block, UI_HELVB);
1390
1391                 if(md->titleicon) {
1392                         width+= 20;
1393                         sprintf(titlestr, " %s", md->title);
1394                         uiDefIconTextBut(block, LABEL, 0, md->titleicon, titlestr, startx, (short)(starty+height), width, MENU_BUTTON_HEIGHT, NULL, 0.0, 0.0, 0, 0, "");
1395                 }
1396                 else {
1397                         bt= uiDefBut(block, LABEL, 0, md->title, startx, (short)(starty+height), columns*width, MENU_BUTTON_HEIGHT, NULL, 0.0, 0.0, 0, 0, "");
1398                         bt->flag= UI_TEXT_LEFT;
1399                 }
1400                 uiSetCurFont(block, UI_HELV);
1401         }
1402
1403         x1= startx + width*((int)a/rows);
1404         y1= starty + height - MENU_BUTTON_HEIGHT;
1405                 
1406         for(a=0; a<md->nitems; a++) {
1407                 char *name= md->items[a].str;
1408                 int icon = md->items[a].icon;
1409
1410                 if(strcmp(name, "%l")==0) {
1411                         uiDefBut(block, SEPR, B_NOP, "", x1, y1, width, PUP_LABELH, NULL, 0, 0.0, 0, 0, "");
1412                         y1 -= PUP_LABELH;
1413                 }
1414                 else if (icon) {
1415                         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, "");
1416                         y1 -= MENU_BUTTON_HEIGHT;
1417                 }
1418                 else {
1419                         uiDefButF(block, BUTM, B_NOP, name, x1, y1, width, MENU_BUTTON_HEIGHT-1, &handle->retvalue, (float) md->items[a].retval, 0.0, 0, 0, "");
1420                         y1 -= MENU_BUTTON_HEIGHT;
1421                 }
1422         }
1423         
1424         uiBoundsBlock(block, 1);
1425         uiEndBlock(C, block);
1426
1427         menudata_free(md);
1428
1429         /* XXX 2.5 need to store last selected */
1430 #if 0
1431         /* calculate last selected */
1432         if(event & ui_return_ok) {
1433                 lastselected= 0;
1434                 for(a=0; a<md->nitems; a++) {
1435                         if(val==md->items[a].retval) lastselected= a;
1436                 }
1437                 
1438                 pupmenu_memory(info->instr, lastselected);
1439         }
1440 #endif
1441         
1442         /* XXX 2.5 need to warp back */
1443 #if 0
1444         if(mousemove[1] && (event & ui_return_out)==0)
1445                 ui_warp_pointer(mousemove[0], mousemove[1]);
1446         return val;
1447 #endif
1448
1449         return block;
1450 }
1451
1452 uiBlock *ui_block_func_PUPMENUCOL(bContext *C, uiMenuBlockHandle *handle, void *arg_info)
1453 {
1454         uiBlock *block;
1455         uiPupMenuInfo *info;
1456         int columns, rows, mousemove[2]= {0, 0}, mousewarp;
1457         int width, height, xmax, ymax, maxrow;
1458         int a, startx, starty, endx, endy, x1, y1;
1459         float fvalue;
1460         MenuData *md;
1461
1462         info= arg_info;
1463         maxrow= info->maxrow;
1464         height= 0;
1465
1466         /* block stuff first, need to know the font */
1467         block= uiBeginBlock(C, handle->region, "menu", UI_EMBOSSP, UI_HELV);
1468         uiBlockSetFlag(block, UI_BLOCK_LOOP|UI_BLOCK_REDRAW|UI_BLOCK_RET_1|UI_BLOCK_NUMSELECT);
1469         block->themecol= TH_MENU_ITEM;
1470         
1471         md= decompose_menu_string(info->instr);
1472
1473         /* columns and row calculation */
1474         columns= (md->nitems+maxrow)/maxrow;
1475         if (columns<1) columns= 1;
1476         
1477         if(columns > 8) {
1478                 maxrow += 5;
1479                 columns= (md->nitems+maxrow)/maxrow;
1480         }
1481         
1482         rows= (int) md->nitems/columns;
1483         if (rows<1) rows= 1;
1484         
1485         while (rows*columns<(md->nitems+columns) ) rows++;
1486
1487         /* size and location, title slightly bigger for bold */
1488         if(md->title) {
1489                 width= 2*strlen(md->title)+UI_GetStringWidth(uiBlockGetCurFont(block), md->title, ui_translate_buttons());
1490                 width /= columns;
1491         }
1492         else width= 0;
1493
1494         for(a=0; a<md->nitems; a++) {
1495                 xmax= UI_GetStringWidth(uiBlockGetCurFont(block), md->items[a].str, ui_translate_buttons());
1496                 if(xmax>width) width= xmax;
1497         }
1498
1499         width+= 10;
1500         if (width<50) width=50;
1501         
1502         height= rows*MENU_BUTTON_HEIGHT;
1503         if (md->title) height+= MENU_BUTTON_HEIGHT;
1504         
1505         wm_window_get_size(C->window, &xmax, &ymax);
1506
1507         /* find active item */
1508         fvalue= handle->retvalue;
1509         for(a=0; a<md->nitems; a++) {
1510                 if( md->items[a].retval== (int)fvalue ) break;
1511         }
1512
1513         /* no active item? */
1514         if(a==md->nitems) {
1515                 if(md->title) a= -1;
1516                 else a= 0;
1517         }
1518
1519         if(a>0)
1520                 startx = info->mx-width/2 - ((int)(a)/rows)*width;
1521         else
1522                 startx= info->mx-width/2;
1523         starty = info->my-height + MENU_BUTTON_HEIGHT/2 + ((a)%rows)*MENU_BUTTON_HEIGHT;
1524
1525         if (md->title) starty+= MENU_BUTTON_HEIGHT;
1526         
1527         if(startx<10) {
1528                 mousemove[0]= 10-startx;
1529                 startx= 10;
1530         }
1531         if(starty<10) {
1532                 mousemove[1]= 10-starty;
1533                 starty= 10;
1534         }
1535         
1536         endx= startx+width*columns;
1537         endy= starty+height;
1538
1539         if(endx>xmax) {
1540                 mousemove[0]= xmax-endx-10;
1541                 endx= xmax-10;
1542                 startx= endx-width*columns;
1543         }
1544         if(endy>ymax) {
1545                 mousemove[1]= ymax-endy-10;
1546                 endy= ymax-10;
1547                 starty= endy-height;
1548         }
1549
1550         if(mousemove[0] || mousemove[1]) {
1551                 ui_warp_pointer(info->mx+mousemove[0], info->my+mousemove[1]);
1552                 mousemove[0]= info->mx;
1553                 mousemove[1]= info->my;
1554                 mousewarp= 1;
1555         }
1556
1557         /* here we go! */
1558         if(md->title) {
1559                 uiBut *bt;
1560                 uiSetCurFont(block, UI_HELVB);
1561
1562                 if(md->titleicon) {
1563                 }
1564                 else {
1565                         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, "");
1566                         bt->flag= UI_TEXT_LEFT;
1567                 }
1568                 uiSetCurFont(block, UI_HELV);
1569         }
1570
1571         for(a=0; a<md->nitems; a++) {
1572                 char *name= md->items[a].str;
1573                 int icon = md->items[a].icon;
1574
1575                 x1= startx + width*((int)a/rows);
1576                 y1= starty - MENU_BUTTON_HEIGHT*(a%rows) + (rows-1)*MENU_BUTTON_HEIGHT; 
1577                 
1578                 if(strcmp(name, "%l")==0) {
1579                         uiDefBut(block, SEPR, B_NOP, "", x1, y1, width, PUP_LABELH, NULL, 0, 0.0, 0, 0, "");
1580                         y1 -= PUP_LABELH;
1581                 }
1582                 else if (icon) {
1583                         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, "");
1584                         y1 -= MENU_BUTTON_HEIGHT;
1585                 }
1586                 else {
1587                         uiDefButF(block, BUTM, B_NOP, name, x1, y1, width, MENU_BUTTON_HEIGHT-1, &handle->retvalue, (float) md->items[a].retval, 0.0, 0, 0, "");
1588                         y1 -= MENU_BUTTON_HEIGHT;
1589                 }
1590         }
1591         
1592         uiBoundsBlock(block, 1);
1593         uiEndBlock(C, block);
1594
1595 #if 0
1596         event= uiDoBlocks(&listb, 0, 1);
1597 #endif
1598         
1599         menudata_free(md);
1600         
1601         /* XXX 2.5 need to warp back */
1602 #if 0
1603         if((event & UI_RETURN_OUT)==0)
1604                 ui_warp_pointer(mousemove[0], mousemove[1]);
1605 #endif
1606
1607         return block;
1608 }
1609
1610 void pupmenu_col(bContext *C, char *instr, int mx, int my, int maxrow, uiPupmenuFunc func, void *arg)
1611 {
1612         uiPupMenuInfo info;
1613         uiMenuBlockHandle *menu;
1614
1615         memset(&info, 0, sizeof(info));
1616         info.instr= instr;
1617         info.mx= mx;
1618         info.my= my;
1619         info.maxrow= maxrow;
1620
1621         menu= ui_menu_block_create(C, NULL, NULL, ui_block_func_PUPMENUCOL, &info);
1622         menu->popup= 1;
1623
1624         UI_add_popup_handlers(&C->window->handlers, menu);
1625         WM_event_add_mousemove(C);
1626
1627         menu->popup_func= func;
1628         menu->popup_arg= arg;
1629 }
1630
1631 void pupmenu(bContext *C, char *instr, int mx, int my, uiPupmenuFunc func, void *arg)
1632 {
1633         uiPupMenuInfo info;
1634         uiMenuBlockHandle *menu;
1635
1636         memset(&info, 0, sizeof(info));
1637         info.instr= instr;
1638         info.mx= mx;
1639         info.my= my;
1640
1641         menu= ui_menu_block_create(C, NULL, NULL, ui_block_func_PUPMENU, &info);
1642         menu->popup= 1;
1643
1644         UI_add_popup_handlers(&C->window->handlers, menu);
1645         WM_event_add_mousemove(C);
1646
1647         menu->popup_func= func;
1648         menu->popup_arg= arg;
1649 }
1650
1651 /* XXX test */
1652 static void operator_callback(bContext *C, void *arg, int retval)
1653 {
1654         const char *opname= arg;
1655
1656         if(retval > 0)
1657                 WM_operator_call(C, opname, WM_OP_DEFAULT);
1658 }
1659
1660 void okee_operator(bContext *C, char *opname, char *str, ...)
1661 {
1662         va_list ap;
1663         char *s, buf[512];
1664         int mx, my;
1665
1666         mx= C->window->eventstate->x;
1667         my= C->window->eventstate->y;
1668
1669         va_start(ap, str);
1670
1671         s= buf;
1672         s += sprintf(s, "OK? %%i%d%%t|", ICON_HELP);
1673         vsprintf(s, str, ap);
1674         va_end(ap);
1675
1676         pupmenu(C, buf, mx, my, operator_callback, opname);
1677 }
1678