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