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