d32cad9256903df787f265fc60aaad87f9d6031d
[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 B_NOP                   -1
63 #define MENU_SHADOW_LEFT        -1
64 #define MENU_SHADOW_BOTTOM      -10
65 #define MENU_SHADOW_RIGHT       10
66 #define MENU_SHADOW_TOP         1
67
68 /*********************** Menu Data Parsing ********************* */
69
70 typedef struct {
71         char *str;
72         int retval;
73         int icon;
74 } MenuEntry;
75
76 typedef struct {
77         char *instr;
78         char *title;
79         int titleicon;
80         
81         MenuEntry *items;
82         int nitems, itemssize;
83 } MenuData;
84
85 static MenuData *menudata_new(char *instr)
86 {
87         MenuData *md= MEM_mallocN(sizeof(*md), "MenuData");
88
89         md->instr= instr;
90         md->title= NULL;
91         md->titleicon= 0;
92         md->items= NULL;
93         md->nitems= md->itemssize= 0;
94         
95         return md;
96 }
97
98 static void menudata_set_title(MenuData *md, char *title, int titleicon)
99 {
100         if (!md->title)
101                 md->title= title;
102         if (!md->titleicon)
103                 md->titleicon= titleicon;
104 }
105
106 static void menudata_add_item(MenuData *md, char *str, int retval, int icon)
107 {
108         if (md->nitems==md->itemssize) {
109                 int nsize= md->itemssize?(md->itemssize<<1):1;
110                 MenuEntry *oitems= md->items;
111                 
112                 md->items= MEM_mallocN(nsize*sizeof(*md->items), "md->items");
113                 if (oitems) {
114                         memcpy(md->items, oitems, md->nitems*sizeof(*md->items));
115                         MEM_freeN(oitems);
116                 }
117                 
118                 md->itemssize= nsize;
119         }
120         
121         md->items[md->nitems].str= str;
122         md->items[md->nitems].retval= retval;
123         md->items[md->nitems].icon= icon;
124         md->nitems++;
125 }
126
127 void menudata_free(MenuData *md)
128 {
129         MEM_freeN(md->instr);
130         if (md->items)
131                 MEM_freeN(md->items);
132         MEM_freeN(md);
133 }
134
135         /**
136          * Parse menu description strings, string is of the
137          * form "[sss%t|]{(sss[%xNN]|), (%l|)}", ssss%t indicates the
138          * menu title, sss or sss%xNN indicates an option, 
139          * if %xNN is given then NN is the return value if
140          * that option is selected otherwise the return value
141          * is the index of the option (starting with 1). %l
142          * indicates a seperator.
143          * 
144          * @param str String to be parsed.
145          * @retval new menudata structure, free with menudata_free()
146          */
147 MenuData *decompose_menu_string(char *str) 
148 {
149         char *instr= BLI_strdup(str);
150         MenuData *md= menudata_new(instr);
151         char *nitem= NULL, *s= instr;
152         int nicon=0, nretval= 1, nitem_is_title= 0;
153         
154         while (1) {
155                 char c= *s;
156
157                 if (c=='%') {
158                         if (s[1]=='x') {
159                                 nretval= atoi(s+2);
160
161                                 *s= '\0';
162                                 s++;
163                         } else if (s[1]=='t') {
164                                 nitem_is_title= 1;
165
166                                 *s= '\0';
167                                 s++;
168                         } else if (s[1]=='l') {
169                                 nitem= "%l";
170                                 s++;
171                         } else if (s[1]=='i') {
172                                 nicon= atoi(s+2);
173                                 
174                                 *s= '\0';
175                                 s++;
176                         }
177                 } else if (c=='|' || c=='\0') {
178                         if (nitem) {
179                                 *s= '\0';
180
181                                 if (nitem_is_title) {
182                                         menudata_set_title(md, nitem, nicon);
183                                         nitem_is_title= 0;
184                                 } else {
185                                         /* prevent separator to get a value */
186                                         if(nitem[0]=='%' && nitem[1]=='l')
187                                                 menudata_add_item(md, nitem, -1, nicon);
188                                         else
189                                                 menudata_add_item(md, nitem, nretval, nicon);
190                                         nretval= md->nitems+1;
191                                 } 
192                                 
193                                 nitem= NULL;
194                                 nicon= 0;
195                         }
196                         
197                         if (c=='\0')
198                                 break;
199                 } else if (!nitem)
200                         nitem= s;
201                 
202                 s++;
203         }
204         
205         return md;
206 }
207
208 void ui_set_name_menu(uiBut *but, int value)
209 {
210         MenuData *md;
211         int i;
212         
213         md= decompose_menu_string(but->str);
214         for (i=0; i<md->nitems; i++)
215                 if (md->items[i].retval==value)
216                         strcpy(but->drawstr, md->items[i].str);
217         
218         menudata_free(md);
219 }
220
221 /******************** Creating Temporary regions ******************/
222
223 ARegion *ui_add_temporary_region(bScreen *sc)
224 {
225         ARegion *ar;
226
227         ar= MEM_callocN(sizeof(ARegion), "area region");
228         BLI_addtail(&sc->regionbase, ar);
229
230         ar->regiontype= RGN_TYPE_TEMPORARY;
231         ar->alignment= RGN_ALIGN_FLOAT;
232
233         return ar;
234 }
235
236 void ui_remove_temporary_region(bContext *C, bScreen *sc, ARegion *ar)
237 {
238         ED_region_exit(C, ar);
239         BKE_area_region_free(ar);
240         BLI_freelinkN(&sc->regionbase, ar);
241 }
242
243 /************************* Creating Tooltips **********************/
244
245 typedef struct uiTooltipData {
246         rctf bbox;
247         struct BMF_Font *font;
248         char *tip;
249         float aspect;
250 } uiTooltipData;
251
252 static void ui_tooltip_region_draw(const bContext *C, ARegion *ar)
253 {
254         uiTooltipData *data;
255         int x1, y1, x2, y2;
256
257         data= ar->regiondata;
258
259         x1= ar->winrct.xmin;
260         y1= ar->winrct.ymin;
261         x2= ar->winrct.xmax;
262         y2= ar->winrct.ymax;
263
264         /* draw drop shadow */
265         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
266         glEnable(GL_BLEND);
267
268         glColor4ub(0, 0, 0, 20);
269         
270         gl_round_box(GL_POLYGON, 3, 3, x2-x1-3, y2-y1-2, 2.0);
271         gl_round_box(GL_POLYGON, 3, 2, x2-x1-2, y2-y1-2, 3.0);
272         
273         glColor4ub(0, 0, 0, 8);
274         
275         gl_round_box(GL_POLYGON, 3, 1, x2-x1-1, y2-y1-3, 4.0);
276         gl_round_box(GL_POLYGON, 3, 0, x2-x1-0, y2-y1-3, 5.0);
277
278         glDisable(GL_BLEND);
279         
280         /* draw background */
281         glColor3f(1.0f, 1.0f, 0.8666f);
282         glRectf(0, 4, x2-x1+4, y2-y1);
283         
284         /* draw text */
285         glColor3ub(0,0,0);
286
287         /* set the position for drawing text +4 in from the left edge, and leaving
288          * an equal gap between the top of the background box and the top of the
289          * string's bbox, and the bottom of the background box, and the bottom of
290          * the string's bbox */
291         ui_rasterpos_safe(4, ((y2-data->bbox.ymax)+(y1+data->bbox.ymin))/2 - data->bbox.ymin - y1, data->aspect);
292         UI_SetScale(1.0);
293
294         UI_DrawString(data->font, data->tip, ui_translate_tooltips());
295 }
296
297 static void ui_tooltip_region_free(ARegion *ar)
298 {
299         uiTooltipData *data;
300
301         data= ar->regiondata;
302         MEM_freeN(data->tip);
303         MEM_freeN(data);
304 }
305
306 ARegion *ui_tooltip_create(bContext *C, ARegion *butregion, uiBut *but)
307 {
308         static ARegionType type;
309         ARegion *ar;
310         uiTooltipData *data;
311         int x1, x2, y1, y2, winx, winy, ofsx, ofsy;
312
313         if(!but->tip || strlen(but->tip)==0)
314                 return NULL;
315
316         /* create area region */
317         ar= ui_add_temporary_region(CTX_wm_screen(C));
318
319         memset(&type, 0, sizeof(ARegionType));
320         type.draw= ui_tooltip_region_draw;
321         type.free= ui_tooltip_region_free;
322         ar->type= &type;
323
324         /* create tooltip data */
325         data= MEM_callocN(sizeof(uiTooltipData), "uiTooltipData");
326         data->tip= BLI_strdup(but->tip);
327         data->font= but->font;
328         data->aspect= but->aspect;
329         UI_GetBoundingBox(data->font, data->tip, ui_translate_tooltips(), &data->bbox);
330
331         ar->regiondata= data;
332
333         /* compute position */
334         ofsx= (but->block->panel)? but->block->panel->ofsx: 0;
335         ofsy= (but->block->panel)? but->block->panel->ofsy: 0;
336
337         x1= (but->x1+but->x2)/2 + ofsx;
338         x2= x1+but->aspect*((data->bbox.xmax-data->bbox.xmin) + 8);
339         y2= but->y1-10 + ofsy;
340         y1= y2-but->aspect*((data->bbox.ymax+(data->bbox.ymax-data->bbox.ymin)));
341
342         y2 += 8;
343         x2 += 8;
344
345         if(butregion) {
346                 /* XXX temp, region v2ds can be empty still */
347                 if(butregion->v2d.cur.xmin != butregion->v2d.cur.xmax) {
348                         short tx, ty;
349                         UI_view2d_to_region_no_clip(&butregion->v2d, x1, y1, &tx, &ty);
350                         x1= (int)tx; y1= (int)ty;
351                         UI_view2d_to_region_no_clip(&butregion->v2d, x2, y2, &tx, &ty);
352                         x2= (int)tx; y2= (int)ty;
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 */
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;
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         
1341         md= decompose_menu_string(info->instr);
1342
1343         rows= md->nitems;
1344         columns= 1;
1345
1346         /* size and location, title slightly bigger for bold */
1347         if(md->title) {
1348                 width= 2*strlen(md->title)+UI_GetStringWidth(uiBlockGetCurFont(block), md->title, ui_translate_buttons());
1349                 width /= columns;
1350         }
1351         else width= 0;
1352
1353         for(a=0; a<md->nitems; a++) {
1354                 xmax= UI_GetStringWidth(uiBlockGetCurFont(block), md->items[a].str, ui_translate_buttons());
1355                 if(xmax>width) width= xmax;
1356
1357                 if(strcmp(md->items[a].str, "%l")==0) height+= PUP_LABELH;
1358                 else height+= MENU_BUTTON_HEIGHT;
1359         }
1360
1361         width+= 10;
1362         if (width<50) width=50;
1363         
1364         wm_window_get_size(CTX_wm_window(C), &xmax, &ymax);
1365
1366         /* set first item */
1367         lastselected= 0;
1368         if(pupmenu_set) {
1369                 lastselected= pupmenu_set-1;
1370                 pupmenu_set= 0;
1371         }
1372         else if(md->nitems>1) {
1373                 lastselected= pupmenu_memory(info->instr, -1);
1374         }
1375
1376         startx= info->mx-(0.8*(width));
1377         starty= info->my-height+MENU_BUTTON_HEIGHT/2;
1378         if(lastselected>=0 && lastselected<md->nitems) {
1379                 for(a=0; a<md->nitems; a++) {
1380                         if(a==lastselected) break;
1381                         if( strcmp(md->items[a].str, "%l")==0) starty+= PUP_LABELH;
1382                         else starty+=MENU_BUTTON_HEIGHT;
1383                 }
1384                 
1385                 //starty= info->my-height+MENU_BUTTON_HEIGHT/2+lastselected*MENU_BUTTON_HEIGHT;
1386         }
1387         
1388         if(startx<10) {
1389                 startx= 10;
1390         }
1391         if(starty<10) {
1392                 mousemove[1]= 10-starty;
1393                 starty= 10;
1394         }
1395         
1396         endx= startx+width*columns;
1397         endy= starty+height;
1398
1399         if(endx>xmax) {
1400                 endx= xmax-10;
1401                 startx= endx-width*columns;
1402         }
1403         if(endy>ymax-20) {
1404                 mousemove[1]= ymax-endy-20;
1405                 endy= ymax-20;
1406                 starty= endy-height;
1407         }
1408
1409         if(mousemove[0] || mousemove[1]) {
1410                 ui_warp_pointer(info->mx+mousemove[0], info->my+mousemove[1]);
1411                 mousemove[0]= info->mx;
1412                 mousemove[1]= info->my;
1413                 mousewarp= 1;
1414         }
1415
1416         /* here we go! */
1417         if(md->title) {
1418                 uiBut *bt;
1419                 char titlestr[256];
1420                 uiSetCurFont(block, UI_HELVB);
1421
1422                 if(md->titleicon) {
1423                         width+= 20;
1424                         sprintf(titlestr, " %s", md->title);
1425                         uiDefIconTextBut(block, LABEL, 0, md->titleicon, titlestr, startx, (short)(starty+height), width, MENU_BUTTON_HEIGHT, NULL, 0.0, 0.0, 0, 0, "");
1426                 }
1427                 else {
1428                         bt= uiDefBut(block, LABEL, 0, md->title, startx, (short)(starty+height), columns*width, MENU_BUTTON_HEIGHT, NULL, 0.0, 0.0, 0, 0, "");
1429                         bt->flag= UI_TEXT_LEFT;
1430                 }
1431                 uiSetCurFont(block, UI_HELV);
1432         }
1433
1434         x1= startx + width*((int)a/rows);
1435         y1= starty + height - MENU_BUTTON_HEIGHT;
1436                 
1437         for(a=0; a<md->nitems; a++) {
1438                 char *name= md->items[a].str;
1439                 int icon = md->items[a].icon;
1440
1441                 if(strcmp(name, "%l")==0) {
1442                         uiDefBut(block, SEPR, B_NOP, "", x1, y1, width, PUP_LABELH, NULL, 0, 0.0, 0, 0, "");
1443                         y1 -= PUP_LABELH;
1444                 }
1445                 else if (icon) {
1446                         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, "");
1447                         y1 -= MENU_BUTTON_HEIGHT;
1448                 }
1449                 else {
1450                         uiDefButF(block, BUTM, B_NOP, name, x1, y1, width, MENU_BUTTON_HEIGHT-1, &handle->retvalue, (float) md->items[a].retval, 0.0, 0, 0, "");
1451                         y1 -= MENU_BUTTON_HEIGHT;
1452                 }
1453         }
1454         
1455         uiBoundsBlock(block, 1);
1456         uiEndBlock(C, block);
1457
1458         menudata_free(md);
1459
1460         /* XXX 2.5 need to store last selected */
1461 #if 0
1462         /* calculate last selected */
1463         if(event & ui_return_ok) {
1464                 lastselected= 0;
1465                 for(a=0; a<md->nitems; a++) {
1466                         if(val==md->items[a].retval) lastselected= a;
1467                 }
1468                 
1469                 pupmenu_memory(info->instr, lastselected);
1470         }
1471 #endif
1472         
1473         /* XXX 2.5 need to warp back */
1474 #if 0
1475         if(mousemove[1] && (event & ui_return_out)==0)
1476                 ui_warp_pointer(mousemove[0], mousemove[1]);
1477         return val;
1478 #endif
1479
1480         return block;
1481 }
1482
1483 uiBlock *ui_block_func_PUPMENUCOL(bContext *C, uiMenuBlockHandle *handle, void *arg_info)
1484 {
1485         uiBlock *block;
1486         uiPupMenuInfo *info;
1487         int columns, rows, mousemove[2]= {0, 0}, mousewarp;
1488         int width, height, xmax, ymax, maxrow;
1489         int a, startx, starty, endx, endy, x1, y1;
1490         float fvalue;
1491         MenuData *md;
1492
1493         info= arg_info;
1494         maxrow= info->maxrow;
1495         height= 0;
1496
1497         /* block stuff first, need to know the font */
1498         block= uiBeginBlock(C, handle->region, "menu", UI_EMBOSSP, UI_HELV);
1499         uiBlockSetFlag(block, UI_BLOCK_LOOP|UI_BLOCK_REDRAW|UI_BLOCK_RET_1|UI_BLOCK_NUMSELECT);
1500         block->themecol= TH_MENU_ITEM;
1501         
1502         md= decompose_menu_string(info->instr);
1503
1504         /* columns and row calculation */
1505         columns= (md->nitems+maxrow)/maxrow;
1506         if (columns<1) columns= 1;
1507         
1508         if(columns > 8) {
1509                 maxrow += 5;
1510                 columns= (md->nitems+maxrow)/maxrow;
1511         }
1512         
1513         rows= (int) md->nitems/columns;
1514         if (rows<1) rows= 1;
1515         
1516         while (rows*columns<(md->nitems+columns) ) rows++;
1517
1518         /* size and location, title slightly bigger for bold */
1519         if(md->title) {
1520                 width= 2*strlen(md->title)+UI_GetStringWidth(uiBlockGetCurFont(block), md->title, ui_translate_buttons());
1521                 width /= columns;
1522         }
1523         else width= 0;
1524
1525         for(a=0; a<md->nitems; a++) {
1526                 xmax= UI_GetStringWidth(uiBlockGetCurFont(block), md->items[a].str, ui_translate_buttons());
1527                 if(xmax>width) width= xmax;
1528         }
1529
1530         width+= 10;
1531         if (width<50) width=50;
1532         
1533         height= rows*MENU_BUTTON_HEIGHT;
1534         if (md->title) height+= MENU_BUTTON_HEIGHT;
1535         
1536         wm_window_get_size(CTX_wm_window(C), &xmax, &ymax);
1537
1538         /* find active item */
1539         fvalue= handle->retvalue;
1540         for(a=0; a<md->nitems; a++) {
1541                 if( md->items[a].retval== (int)fvalue ) break;
1542         }
1543
1544         /* no active item? */
1545         if(a==md->nitems) {
1546                 if(md->title) a= -1;
1547                 else a= 0;
1548         }
1549
1550         if(a>0)
1551                 startx = info->mx-width/2 - ((int)(a)/rows)*width;
1552         else
1553                 startx= info->mx-width/2;
1554         starty = info->my-height + MENU_BUTTON_HEIGHT/2 + ((a)%rows)*MENU_BUTTON_HEIGHT;
1555
1556         if (md->title) starty+= MENU_BUTTON_HEIGHT;
1557         
1558         if(startx<10) {
1559                 mousemove[0]= 10-startx;
1560                 startx= 10;
1561         }
1562         if(starty<10) {
1563                 mousemove[1]= 10-starty;
1564                 starty= 10;
1565         }
1566         
1567         endx= startx+width*columns;
1568         endy= starty+height;
1569
1570         if(endx>xmax) {
1571                 mousemove[0]= xmax-endx-10;
1572                 endx= xmax-10;
1573                 startx= endx-width*columns;
1574         }
1575         if(endy>ymax) {
1576                 mousemove[1]= ymax-endy-10;
1577                 endy= ymax-10;
1578                 starty= endy-height;
1579         }
1580
1581         if(mousemove[0] || mousemove[1]) {
1582                 ui_warp_pointer(info->mx+mousemove[0], info->my+mousemove[1]);
1583                 mousemove[0]= info->mx;
1584                 mousemove[1]= info->my;
1585                 mousewarp= 1;
1586         }
1587
1588         /* here we go! */
1589         if(md->title) {
1590                 uiBut *bt;
1591                 uiSetCurFont(block, UI_HELVB);
1592
1593                 if(md->titleicon) {
1594                 }
1595                 else {
1596                         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, "");
1597                         bt->flag= UI_TEXT_LEFT;
1598                 }
1599                 uiSetCurFont(block, UI_HELV);
1600         }
1601
1602         for(a=0; a<md->nitems; a++) {
1603                 char *name= md->items[a].str;
1604                 int icon = md->items[a].icon;
1605
1606                 x1= startx + width*((int)a/rows);
1607                 y1= starty - MENU_BUTTON_HEIGHT*(a%rows) + (rows-1)*MENU_BUTTON_HEIGHT; 
1608                 
1609                 if(strcmp(name, "%l")==0) {
1610                         uiDefBut(block, SEPR, B_NOP, "", x1, y1, width, PUP_LABELH, NULL, 0, 0.0, 0, 0, "");
1611                         y1 -= PUP_LABELH;
1612                 }
1613                 else if (icon) {
1614                         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, "");
1615                         y1 -= MENU_BUTTON_HEIGHT;
1616                 }
1617                 else {
1618                         uiDefButF(block, BUTM, B_NOP, name, x1, y1, width, MENU_BUTTON_HEIGHT-1, &handle->retvalue, (float) md->items[a].retval, 0.0, 0, 0, "");
1619                         y1 -= MENU_BUTTON_HEIGHT;
1620                 }
1621         }
1622         
1623         uiBoundsBlock(block, 1);
1624         uiEndBlock(C, block);
1625         
1626         menudata_free(md);
1627         
1628         /* XXX 2.5 need to warp back */
1629 #if 0
1630         if((event & UI_RETURN_OUT)==0)
1631                 ui_warp_pointer(mousemove[0], mousemove[1]);
1632 #endif
1633
1634         return block;
1635 }
1636
1637 /* This one will set enum propname, call operator and register it, and free the operator itself, 
1638    call it in op->invoke with returning OPERATOR_RUNNING_MODAL */
1639 /* Note: propname has to be static */
1640 void uiPupmenuOperator(bContext *C, int maxrow, wmOperator *op, const char *propname, char *str)
1641 {
1642         wmWindow *window= CTX_wm_window(C);
1643         uiPupMenuInfo info;
1644         uiMenuBlockHandle *menu;
1645         
1646         memset(&info, 0, sizeof(info));
1647         info.mx= window->eventstate->x;
1648         info.my= window->eventstate->y;
1649         info.maxrow= maxrow;
1650         info.instr= str;
1651         
1652         menu= ui_menu_block_create(C, NULL, NULL, ui_block_func_PUPMENU, &info);
1653         menu->popup= 1;
1654         
1655         UI_add_popup_handlers(&window->handlers, menu);
1656         WM_event_add_mousemove(C);
1657         
1658         menu->op_arg= op;
1659         menu->propname= propname;
1660 }
1661
1662
1663 /* this one only to be called with operatortype name option */
1664 void uiPupmenu(bContext *C, int maxrow, uiPupmenuFunc func, void *arg, char *str, ...)
1665 {
1666         wmWindow *window= CTX_wm_window(C);
1667         uiPupMenuInfo info;
1668         uiMenuBlockHandle *menu;
1669
1670         memset(&info, 0, sizeof(info));
1671         info.mx= window->eventstate->x;
1672         info.my= window->eventstate->y;
1673         info.maxrow= maxrow;
1674         info.instr= str;
1675
1676         menu= ui_menu_block_create(C, NULL, NULL, ui_block_func_PUPMENU, &info);
1677         menu->popup= 1;
1678
1679         UI_add_popup_handlers(&window->handlers, menu);
1680         WM_event_add_mousemove(C);
1681
1682         menu->popup_func= func;
1683         menu->popup_arg= arg;
1684 }
1685
1686 /* standard pupmenus */
1687
1688 static void operator_cb(bContext *C, void *arg, int retval)
1689 {
1690         const char *opname= arg;
1691
1692         if(opname && retval > 0)
1693                 WM_operator_name_call(C, opname, WM_OP_EXEC_DEFAULT, NULL, NULL);
1694 }
1695
1696 static void vconfirm(bContext *C, char *opname, char *title, char *itemfmt, va_list ap)
1697 {
1698         char *s, buf[512];
1699
1700         s= buf;
1701         if (title) s+= sprintf(s, "%s%%t|", title);
1702         vsprintf(s, itemfmt, ap);
1703
1704         uiPupmenu(C, 0, operator_cb, opname, buf);
1705 }
1706
1707 static void confirm(bContext *C, char *opname, char *title, char *itemfmt, ...)
1708 {
1709         va_list ap;
1710
1711         va_start(ap, itemfmt);
1712         vconfirm(C, opname, title, itemfmt, ap);
1713         va_end(ap);
1714 }
1715
1716 void uiPupmenuOkee(bContext *C, char *opname, char *str, ...)
1717 {
1718         va_list ap;
1719         char titlestr[256];
1720
1721         sprintf(titlestr, "OK? %%i%d", ICON_HELP);
1722
1723         va_start(ap, str);
1724         vconfirm(C, opname, titlestr, str, ap);
1725         va_end(ap);
1726 }
1727
1728 void uiPupmenuSaveOver(bContext *C, char *opname, char *filename, ...)
1729 {
1730         size_t len= strlen(filename);
1731
1732         if(len==0)
1733                 return;
1734
1735         if(BLI_exists(filename)==0)
1736                 operator_cb(C, opname, 1);
1737
1738         if(filename[len-1]=='/' || filename[len-1]=='\\') {
1739                 uiPupmenuError(C, "Cannot overwrite a directory");
1740                 return;
1741         }
1742
1743         confirm(C, opname, "Save over", filename);
1744 }
1745
1746 void uiPupmenuNotice(bContext *C, char *str, ...)
1747 {
1748         va_list ap;
1749
1750         va_start(ap, str);
1751         vconfirm(C, NULL, NULL, str, ap);
1752         va_end(ap);
1753 }
1754
1755 void uiPupmenuError(bContext *C, char *str, ...)
1756 {
1757         va_list ap;
1758         char nfmt[256];
1759         char titlestr[256];
1760
1761         sprintf(titlestr, "Error %%i%d", ICON_ERROR);
1762
1763         sprintf(nfmt, "%s", str);
1764
1765         va_start(ap, str);
1766         vconfirm(C, NULL, titlestr, nfmt, ap);
1767         va_end(ap);
1768 }
1769
1770 void uiPupmenuReports(bContext *C, ReportList *reports)
1771 {
1772         Report *report;
1773         DynStr *ds;
1774         char *str;
1775
1776         if(!reports || !reports->list.first)
1777                 return;
1778         if(!CTX_wm_window(C))
1779                 return;
1780
1781         ds= BLI_dynstr_new();
1782
1783         for(report=reports->list.first; report; report=report->next) {
1784                 if(report->type >= RPT_ERROR)
1785                         BLI_dynstr_appendf(ds, "Error %%i%d%%t|%s", ICON_ERROR, report->message);
1786                 else if(report->type >= RPT_WARNING)
1787                         BLI_dynstr_appendf(ds, "Warning %%i%d%%t|%s", ICON_ERROR, report->message);
1788         }
1789
1790         str= BLI_dynstr_get_cstring(ds);
1791         uiPupmenu(C, 0, NULL, NULL, str);
1792         MEM_freeN(str);
1793
1794         BLI_dynstr_free(ds);
1795 }
1796