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