BugFix:
[blender-staging.git] / source / blender / editors / interface / interface.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * Contributor(s): Blender Foundation 2002-2008, full recode.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 #include <float.h>
29 #include <limits.h>
30 #include <math.h>
31 #include <string.h>
32  
33 #include "MEM_guardedalloc.h"
34
35 #include "DNA_scene_types.h"
36 #include "DNA_screen_types.h"
37 #include "DNA_userdef_types.h"
38
39 #include "BLI_math.h"
40 #include "BLI_blenlib.h"
41 #include "BLI_dynstr.h"
42
43 #include "BKE_context.h"
44 #include "BKE_idprop.h"
45 #include "BKE_library.h"
46 #include "BKE_screen.h"
47 #include "BKE_texture.h"
48 #include "BKE_utildefines.h"
49 #include "BKE_unit.h"
50
51 #include "BIF_gl.h"
52 #include "BIF_glutil.h"
53
54 #include "BLF_api.h"
55
56 #include "UI_interface.h"
57
58
59 #include "WM_api.h"
60 #include "WM_types.h"
61 #include "wm_subwindow.h"
62 #include "wm_window.h"
63
64 #include "RNA_access.h"
65
66 #include "BPY_extern.h"
67
68 #include "interface_intern.h"
69
70 #define MENU_WIDTH                      120
71 #define MENU_ITEM_HEIGHT        20
72 #define MENU_SEP_HEIGHT         6
73
74 /* 
75  * a full doc with API notes can be found in bf-blender/blender/doc/interface_API.txt
76  * 
77  * uiBlahBlah()         external function
78  * ui_blah_blah()       internal function
79  */
80
81 static void ui_free_but(const bContext *C, uiBut *but);
82
83 /* ************* translation ************** */
84
85 int ui_translate_buttons()
86 {
87         return (U.transopts & USER_TR_BUTTONS);
88 }
89
90 int ui_translate_menus()
91 {
92         return (U.transopts & USER_TR_MENUS);
93 }
94
95 int ui_translate_tooltips()
96 {
97         return (U.transopts & USER_TR_TOOLTIPS);
98 }
99
100 /* ************* window matrix ************** */
101
102 void ui_block_to_window_fl(const ARegion *ar, uiBlock *block, float *x, float *y)
103 {
104         float gx, gy;
105         int sx, sy, getsizex, getsizey;
106
107         getsizex= ar->winrct.xmax-ar->winrct.xmin+1;
108         getsizey= ar->winrct.ymax-ar->winrct.ymin+1;
109         sx= ar->winrct.xmin;
110         sy= ar->winrct.ymin;
111
112         gx= *x;
113         gy= *y;
114
115         if(block->panel) {
116                 gx += block->panel->ofsx;
117                 gy += block->panel->ofsy;
118         }
119
120         *x= ((float)sx) + ((float)getsizex)*(0.5+ 0.5*(gx*block->winmat[0][0]+ gy*block->winmat[1][0]+ block->winmat[3][0]));
121         *y= ((float)sy) + ((float)getsizey)*(0.5+ 0.5*(gx*block->winmat[0][1]+ gy*block->winmat[1][1]+ block->winmat[3][1]));
122 }
123
124 void ui_block_to_window(const ARegion *ar, uiBlock *block, int *x, int *y)
125 {
126         float fx, fy;
127
128         fx= *x;
129         fy= *y;
130
131         ui_block_to_window_fl(ar, block, &fx, &fy);
132
133         *x= (int)(fx+0.5f);
134         *y= (int)(fy+0.5f);
135 }
136
137 void ui_block_to_window_rct(const ARegion *ar, uiBlock *block, rctf *graph, rcti *winr)
138 {
139         rctf tmpr;
140
141         tmpr= *graph;
142         ui_block_to_window_fl(ar, block, &tmpr.xmin, &tmpr.ymin);
143         ui_block_to_window_fl(ar, block, &tmpr.xmax, &tmpr.ymax);
144
145         winr->xmin= tmpr.xmin;
146         winr->ymin= tmpr.ymin;
147         winr->xmax= tmpr.xmax;
148         winr->ymax= tmpr.ymax;
149 }
150
151 void ui_window_to_block_fl(const ARegion *ar, uiBlock *block, float *x, float *y)       /* for mouse cursor */
152 {
153         float a, b, c, d, e, f, px, py;
154         int sx, sy, getsizex, getsizey;
155
156         getsizex= ar->winrct.xmax-ar->winrct.xmin+1;
157         getsizey= ar->winrct.ymax-ar->winrct.ymin+1;
158         sx= ar->winrct.xmin;
159         sy= ar->winrct.ymin;
160
161         a= .5*((float)getsizex)*block->winmat[0][0];
162         b= .5*((float)getsizex)*block->winmat[1][0];
163         c= .5*((float)getsizex)*(1.0+block->winmat[3][0]);
164
165         d= .5*((float)getsizey)*block->winmat[0][1];
166         e= .5*((float)getsizey)*block->winmat[1][1];
167         f= .5*((float)getsizey)*(1.0+block->winmat[3][1]);
168
169         px= *x - sx;
170         py= *y - sy;
171
172         *y=  (a*(py-f) + d*(c-px))/(a*e-d*b);
173         *x= (px- b*(*y)- c)/a;
174
175         if(block->panel) {
176                 *x -= block->panel->ofsx;
177                 *y -= block->panel->ofsy;
178         }
179 }
180
181 void ui_window_to_block(const ARegion *ar, uiBlock *block, int *x, int *y)
182 {
183         float fx, fy;
184
185         fx= *x;
186         fy= *y;
187
188         ui_window_to_block_fl(ar, block, &fx, &fy);
189
190         *x= (int)(fx+0.5f);
191         *y= (int)(fy+0.5f);
192 }
193
194 void ui_window_to_region(const ARegion *ar, int *x, int *y)
195 {
196         *x-= ar->winrct.xmin;
197         *y-= ar->winrct.ymin;
198 }
199
200 /* ******************* block calc ************************* */
201
202 void ui_block_translate(uiBlock *block, int x, int y)
203 {
204         uiBut *bt;
205
206         for(bt= block->buttons.first; bt; bt=bt->next) {
207                 bt->x1 += x;
208                 bt->y1 += y;
209                 bt->x2 += x;
210                 bt->y2 += y;
211         }
212
213         block->minx += x;
214         block->miny += y;
215         block->maxx += x;
216         block->maxy += y;
217 }
218
219 static void ui_text_bounds_block(uiBlock *block, float offset)
220 {
221         uiStyle *style= U.uistyles.first;       // XXX pass on as arg
222         uiBut *bt;
223         int i = 0, j, x1addval= offset, nextcol;
224         int lastcol= 0, col= 0;
225         
226         uiStyleFontSet(&style->widget);
227         
228         for(bt= block->buttons.first; bt; bt= bt->next) {
229                 if(bt->type!=SEPR) {
230                         //int transopts= ui_translate_buttons();
231                         //if(bt->type==TEX || bt->type==IDPOIN) transopts= 0;
232                         
233                         j= BLF_width(style->widget.uifont_id, bt->drawstr);
234
235                         if(j > i) i = j;
236                 }
237
238                 if(bt->next && bt->x1 < bt->next->x1)
239                         lastcol++;
240         }
241
242         /* cope with multi collumns */
243         bt= block->buttons.first;
244         while(bt) {
245                 if(bt->next && bt->x1 < bt->next->x1) {
246                         nextcol= 1;
247                         col++;
248                 }
249                 else nextcol= 0;
250                 
251                 bt->x1 = x1addval;
252                 bt->x2 = bt->x1 + i + block->bounds;
253                 
254                 if(col == lastcol)
255                         bt->x2= MAX2(bt->x2, offset + block->minbounds);
256
257                 ui_check_but(bt);       // clips text again
258                 
259                 if(nextcol)
260                         x1addval+= i + block->bounds;
261                 
262                 bt= bt->next;
263         }
264 }
265
266 void ui_bounds_block(uiBlock *block)
267 {
268         uiBut *bt;
269         int xof;
270         
271         if(block->buttons.first==NULL) {
272                 if(block->panel) {
273                         block->minx= 0.0; block->maxx= block->panel->sizex;
274                         block->miny= 0.0; block->maxy= block->panel->sizey;
275                 }
276         }
277         else {
278         
279                 block->minx= block->miny= 10000;
280                 block->maxx= block->maxy= -10000;
281                 
282                 bt= block->buttons.first;
283                 while(bt) {
284                         if(bt->x1 < block->minx) block->minx= bt->x1;
285                         if(bt->y1 < block->miny) block->miny= bt->y1;
286         
287                           if(bt->x2 > block->maxx) block->maxx= bt->x2;
288                         if(bt->y2 > block->maxy) block->maxy= bt->y2;
289
290                         bt= bt->next;
291                 }
292                 
293                 block->minx -= block->bounds;
294                 block->miny -= block->bounds;
295                 block->maxx += block->bounds;
296                 block->maxy += block->bounds;
297         }
298
299         block->maxx= block->minx + MAX2(block->maxx - block->minx, block->minbounds);
300
301         /* hardcoded exception... but that one is annoying with larger safety */ 
302         bt= block->buttons.first;
303         if(bt && strncmp(bt->str, "ERROR", 5)==0) xof= 10;
304         else xof= 40;
305         
306         block->safety.xmin= block->minx-xof;
307         block->safety.ymin= block->miny-xof;
308         block->safety.xmax= block->maxx+xof;
309         block->safety.ymax= block->maxy+xof;
310 }
311
312 static void ui_centered_bounds_block(const bContext *C, uiBlock *block)
313 {
314         wmWindow *window= CTX_wm_window(C);
315         int xmax, ymax;
316         int startx, starty;
317         int width, height;
318         
319         /* note: this is used for the splash where window bounds event has not been
320          * updated by ghost, get the window bounds from ghost directly */
321
322         // wm_window_get_size(window, &xmax, &ymax);
323         wm_window_get_size_ghost(window, &xmax, &ymax);
324         
325         ui_bounds_block(block);
326         
327         width= block->maxx - block->minx;
328         height= block->maxy - block->miny;
329         
330         startx = (xmax * 0.5f) - (width * 0.5f);
331         starty = (ymax * 0.5f) - (height * 0.5f);
332         
333         ui_block_translate(block, startx - block->minx, starty - block->miny);
334         
335         /* now recompute bounds and safety */
336         ui_bounds_block(block);
337         
338 }
339 static void ui_popup_bounds_block(const bContext *C, uiBlock *block, int bounds_calc)
340 {
341         wmWindow *window= CTX_wm_window(C);
342         int startx, starty, endx, endy, width, height, oldwidth, oldheight;
343         int oldbounds, xmax, ymax;
344
345         oldbounds= block->bounds;
346
347         /* compute mouse position with user defined offset */
348         ui_bounds_block(block);
349         
350         wm_window_get_size(window, &xmax, &ymax);
351
352         oldwidth= block->maxx - block->minx;
353         oldheight= block->maxy - block->miny;
354
355         /* first we ensure wide enough text bounds */
356         if(bounds_calc==UI_BLOCK_BOUNDS_POPUP_MENU) {
357                 if(block->flag & UI_BLOCK_LOOP) {
358                         block->bounds= 50;
359                         ui_text_bounds_block(block, block->minx);
360                 }
361         }
362
363         /* next we recompute bounds */
364         block->bounds= oldbounds;
365         ui_bounds_block(block);
366
367         /* and we adjust the position to fit within window */
368         width= block->maxx - block->minx;
369         height= block->maxy - block->miny;
370     
371         /* avoid divide by zero below, caused by calling with no UI, but better not crash */
372         oldwidth= oldwidth > 0 ? oldwidth : MAX2(1, width);
373         oldheight= oldheight > 0 ? oldheight : MAX2(1, height);
374
375         /* offset block based on mouse position, user offset is scaled
376            along in case we resized the block in ui_text_bounds_block */
377         startx= window->eventstate->x + block->minx + (block->mx*width)/oldwidth;
378         starty= window->eventstate->y + block->miny + (block->my*height)/oldheight;
379
380         if(startx<10)
381                 startx= 10;
382         if(starty<10)
383                 starty= 10;
384         
385         endx= startx+width;
386         endy= starty+height;
387         
388         if(endx>xmax) {
389                 endx= xmax-10;
390                 startx= endx-width;
391         }
392         if(endy>ymax-20) {
393                 endy= ymax-20;
394                 starty= endy-height;
395         }
396
397         ui_block_translate(block, startx - block->minx, starty - block->miny);
398
399         /* now recompute bounds and safety */
400         ui_bounds_block(block);
401 }
402
403 /* used for various cases */
404 void uiBoundsBlock(uiBlock *block, int addval)
405 {
406         if(block==NULL)
407                 return;
408         
409         block->bounds= addval;
410         block->dobounds= UI_BLOCK_BOUNDS;
411 }
412
413 /* used for pulldowns */
414 void uiTextBoundsBlock(uiBlock *block, int addval)
415 {
416         block->bounds= addval;
417         block->dobounds= UI_BLOCK_BOUNDS_TEXT;
418 }
419
420 /* used for block popups */
421 void uiPopupBoundsBlock(uiBlock *block, int addval, int mx, int my)
422 {
423         block->bounds= addval;
424         block->dobounds= UI_BLOCK_BOUNDS_POPUP_MOUSE;
425         block->mx= mx;
426         block->my= my;
427 }
428
429 /* used for menu popups */
430 void uiMenuPopupBoundsBlock(uiBlock *block, int addval, int mx, int my)
431 {
432         block->bounds= addval;
433         block->dobounds= UI_BLOCK_BOUNDS_POPUP_MENU;
434         block->mx= mx;
435         block->my= my;
436 }
437
438 /* used for centered popups, i.e. splash */
439 void uiCenteredBoundsBlock(uiBlock *block, int addval)
440 {
441         block->bounds= addval;
442         block->dobounds= UI_BLOCK_BOUNDS_POPUP_CENTER;
443 }
444
445 /* ************** LINK LINE DRAWING  ************* */
446
447 /* link line drawing is not part of buttons or theme.. so we stick with it here */
448
449 static void ui_draw_linkline(uiBut *but, uiLinkLine *line)
450 {
451         rcti rect;
452
453         if(line->from==NULL || line->to==NULL) return;
454         
455         rect.xmin= (line->from->x1+line->from->x2)/2.0;
456         rect.ymin= (line->from->y1+line->from->y2)/2.0;
457         rect.xmax= (line->to->x1+line->to->x2)/2.0;
458         rect.ymax= (line->to->y1+line->to->y2)/2.0;
459         
460         if(line->flag & UI_SELECT) 
461                 glColor3ub(100,100,100);
462         else 
463                 glColor3ub(0,0,0);
464
465         ui_draw_link_bezier(&rect);
466 }
467
468 static void ui_draw_links(uiBlock *block)
469 {
470         uiBut *but;
471         uiLinkLine *line;
472         
473         but= block->buttons.first;
474         while(but) {
475                 if(but->type==LINK && but->link) {
476                         line= but->link->lines.first;
477                         while(line) {
478                                 ui_draw_linkline(but, line);
479                                 line= line->next;
480                         }
481                 }
482                 but= but->next;
483         }       
484 }
485
486 /* ************** BLOCK ENDING FUNCTION ************* */
487
488 /* NOTE: if but->poin is allocated memory for every defbut, things fail... */
489 static int ui_but_equals_old(uiBut *but, uiBut *oldbut)
490 {
491         /* various properties are being compared here, hopfully sufficient
492          * to catch all cases, but it is simple to add more checks later */
493         if(but->retval != oldbut->retval) return 0;
494         if(but->rnapoin.data != oldbut->rnapoin.data) return 0;
495         if(but->rnaprop != oldbut->rnaprop)
496                 if(but->rnaindex != oldbut->rnaindex) return 0;
497         if(but->func != oldbut->func) return 0;
498         if(but->funcN != oldbut->funcN) return 0;
499         if(oldbut->func_arg1 != oldbut && but->func_arg1 != oldbut->func_arg1) return 0;
500         if(oldbut->func_arg2 != oldbut && but->func_arg2 != oldbut->func_arg2) return 0;
501         if(!but->funcN && ((but->poin != oldbut->poin && (uiBut*)oldbut->poin != oldbut) || but->pointype != oldbut->pointype)) return 0;
502
503         return 1;
504 }
505
506 static int ui_but_update_from_old_block(const bContext *C, uiBlock *block, uiBut *but)
507 {
508         uiBlock *oldblock;
509         uiBut *oldbut;
510         int found= 0;
511
512         oldblock= block->oldblock;
513         if(!oldblock)
514                 return found;
515
516         for(oldbut=oldblock->buttons.first; oldbut; oldbut=oldbut->next) {
517                 if(ui_but_equals_old(oldbut, but)) {
518                         if(oldbut->active) {
519                                 but->flag= oldbut->flag;
520                                 but->active= oldbut->active;
521                                 but->pos= oldbut->pos;
522                                 but->editstr= oldbut->editstr;
523                                 but->editval= oldbut->editval;
524                                 but->editvec= oldbut->editvec;
525                                 but->editcoba= oldbut->editcoba;
526                                 but->editcumap= oldbut->editcumap;
527                                 but->selsta= oldbut->selsta;
528                                 but->selend= oldbut->selend;
529                                 but->softmin= oldbut->softmin;
530                                 but->softmax= oldbut->softmax;
531                                 but->linkto[0]= oldbut->linkto[0];
532                                 but->linkto[1]= oldbut->linkto[1];
533                                 found= 1;
534
535                                 oldbut->active= NULL;
536                         }
537
538                         /* ensures one button can get activated, and in case the buttons
539                          * draw are the same this gives O(1) lookup for each button */
540                         BLI_remlink(&oldblock->buttons, oldbut);
541                         ui_free_but(C, oldbut);
542                         
543                         break;
544                 }
545         }
546         
547         return found;
548 }
549
550 /* needed for temporarily rename buttons, such as in outliner or fileselect,
551    they should keep calling uiDefButs to keep them alive */
552 /* returns 0 when button removed */
553 int uiButActiveOnly(const bContext *C, uiBlock *block, uiBut *but)
554 {
555         uiBlock *oldblock;
556         uiBut *oldbut;
557         int activate= 0, found= 0, isactive= 0;
558         
559         oldblock= block->oldblock;
560         if(!oldblock)
561                 activate= 1;
562         else {
563                 for(oldbut=oldblock->buttons.first; oldbut; oldbut=oldbut->next) {
564                         if(ui_but_equals_old(oldbut, but)) {
565                                 found= 1;
566                                 
567                                 if(oldbut->active)
568                                         isactive= 1;
569                                 
570                                 break;
571                         }
572                 }
573         }
574         if(activate || found==0) {
575                 ui_button_activate_do( (bContext *)C, CTX_wm_region(C), but);
576         }
577         else if(found && isactive==0) {
578                 
579                 BLI_remlink(&block->buttons, but);
580                 ui_free_but(C, but);
581                 return 0;
582         }
583         
584         return 1;
585 }
586
587 void ui_menu_block_set_keymaps(const bContext *C, uiBlock *block)
588 {
589         uiBut *but;
590         IDProperty *prop;
591         char buf[512], *butstr;
592
593         /* only do it before bounding */
594         if(block->minx != block->maxx)
595                 return;
596
597         for(but=block->buttons.first; but; but=but->next) {
598                 if(but->optype) {
599                         prop= (but->opptr)? but->opptr->data: NULL;
600
601                         if(WM_key_event_operator_string(C, but->optype->idname, but->opcontext, prop, buf, sizeof(buf))) {
602                                 butstr= MEM_mallocN(strlen(but->str)+strlen(buf)+2, "menu_block_set_keymaps");
603                                 strcpy(butstr, but->str);
604                                 strcat(butstr, "|");
605                                 strcat(butstr, buf);
606
607                                 but->str= but->strdata;
608                                 BLI_strncpy(but->str, butstr, sizeof(but->strdata));
609                                 MEM_freeN(butstr);
610
611                                 ui_check_but(but);
612                         }
613                 }
614         }
615 }
616
617 void uiEndBlock(const bContext *C, uiBlock *block)
618 {
619         uiBut *but;
620         Scene *scene= CTX_data_scene(C);
621
622         /* inherit flags from 'old' buttons that was drawn here previous, based
623          * on matching buttons, we need this to make button event handling non
624          * blocking, while still alowing buttons to be remade each redraw as it
625          * is expected by blender code */
626         for(but=block->buttons.first; but; but=but->next) {
627                 if(ui_but_update_from_old_block(C, block, but))
628                         ui_check_but(but);
629                 
630                 /* temp? Proper check for greying out */
631                 if(but->optype) {
632                         wmOperatorType *ot= but->optype;
633
634                         if(but->context)
635                                 CTX_store_set((bContext*)C, but->context);
636
637                         if(ot == NULL || WM_operator_poll((bContext*)C, ot)==0) {
638                                 but->flag |= UI_BUT_DISABLED;
639                                 but->lock = 1;
640                         }
641
642                         if(but->context)
643                                 CTX_store_set((bContext*)C, NULL);
644                 }
645
646                 ui_but_anim_flag(but, (scene)? scene->r.cfra: 0.0f);
647         }
648
649         if(block->oldblock) {
650                 block->auto_open= block->oldblock->auto_open;
651                 block->auto_open_last= block->oldblock->auto_open_last;
652                 block->tooltipdisabled= block->oldblock->tooltipdisabled;
653
654                 block->oldblock= NULL;
655         }
656
657         /* handle pending stuff */
658         if(block->layouts.first) uiBlockLayoutResolve(block, NULL, NULL);
659         ui_block_do_align(block);
660         if(block->flag & UI_BLOCK_LOOP) ui_menu_block_set_keymaps(C, block);
661         
662         /* after keymaps! */
663         if(block->dobounds == UI_BLOCK_BOUNDS) ui_bounds_block(block);
664         else if(block->dobounds == UI_BLOCK_BOUNDS_TEXT) ui_text_bounds_block(block, 0.0f);
665         else if(block->dobounds == UI_BLOCK_BOUNDS_POPUP_CENTER) ui_centered_bounds_block(C, block);
666         else if(block->dobounds) ui_popup_bounds_block(C, block, block->dobounds);
667
668         if(block->minx==0.0 && block->maxx==0.0) uiBoundsBlock(block, 0);
669         if(block->flag & UI_BUT_ALIGN) uiBlockEndAlign(block);
670
671         block->endblock= 1;
672 }
673
674 /* ************** BLOCK DRAWING FUNCTION ************* */
675
676 void ui_fontscale(short *points, float aspect)
677 {
678         if(aspect < 0.9f || aspect > 1.1f) {
679                 float pointsf= *points;
680                 
681                 /* for some reason scaling fonts goes too fast compared to widget size */
682                 aspect= sqrt(aspect);
683                 pointsf /= aspect;
684                 
685                 if(aspect > 1.0)
686                         *points= ceil(pointsf);
687                 else
688                         *points= floor(pointsf);
689         }
690 }
691
692 /* project button or block (but==NULL) to pixels in regionspace */
693 static void ui_but_to_pixelrect(rcti *rect, const ARegion *ar, uiBlock *block, uiBut *but)
694 {
695         float gx, gy;
696         float getsizex, getsizey;
697         
698         getsizex= ar->winx;
699         getsizey= ar->winy;
700
701         gx= (but?but->x1:block->minx) + (block->panel?block->panel->ofsx:0.0f);
702         gy= (but?but->y1:block->miny) + (block->panel?block->panel->ofsy:0.0f);
703         
704         rect->xmin= floor(getsizex*(0.5+ 0.5*(gx*block->winmat[0][0]+ gy*block->winmat[1][0]+ block->winmat[3][0])));
705         rect->ymin= floor(getsizey*(0.5+ 0.5*(gx*block->winmat[0][1]+ gy*block->winmat[1][1]+ block->winmat[3][1])));
706         
707         gx= (but?but->x2:block->maxx) + (block->panel?block->panel->ofsx:0.0f);
708         gy= (but?but->y2:block->maxy) + (block->panel?block->panel->ofsy:0.0f);
709         
710         rect->xmax= floor(getsizex*(0.5+ 0.5*(gx*block->winmat[0][0]+ gy*block->winmat[1][0]+ block->winmat[3][0])));
711         rect->ymax= floor(getsizey*(0.5+ 0.5*(gx*block->winmat[0][1]+ gy*block->winmat[1][1]+ block->winmat[3][1])));
712
713 }
714
715 /* uses local copy of style, to scale things down, and allow widgets to change stuff */
716 void uiDrawBlock(const bContext *C, uiBlock *block)
717 {
718         uiStyle style= *((uiStyle *)U.uistyles.first);  // XXX pass on as arg
719         ARegion *ar;
720         uiBut *but;
721         rcti rect;
722         
723         /* get menu region or area region */
724         ar= CTX_wm_menu(C);
725         if(!ar)
726                 ar= CTX_wm_region(C);
727
728         if(!block->endblock)
729                 uiEndBlock(C, block);
730
731         /* we set this only once */
732         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
733         
734         /* scale fonts */
735         ui_fontscale(&style.paneltitle.points, block->aspect);
736         ui_fontscale(&style.grouplabel.points, block->aspect);
737         ui_fontscale(&style.widgetlabel.points, block->aspect);
738         ui_fontscale(&style.widget.points, block->aspect);
739         
740         /* scale block min/max to rect */
741         ui_but_to_pixelrect(&rect, ar, block, NULL);
742         
743         /* pixel space for AA widgets */
744         glMatrixMode(GL_PROJECTION);
745         glPushMatrix();
746         glMatrixMode(GL_MODELVIEW);
747         glPushMatrix();
748         glLoadIdentity();
749         
750         wmOrtho2(-0.01f, ar->winx-0.01f, -0.01f, ar->winy-0.01f);
751         
752         /* back */
753         if(block->flag & UI_BLOCK_LOOP)
754                 ui_draw_menu_back(&style, block, &rect);
755         else if(block->panel)
756                 ui_draw_aligned_panel(ar, &style, block, &rect);
757
758         /* widgets */
759         for(but= block->buttons.first; but; but= but->next) {
760                 ui_but_to_pixelrect(&rect, ar, block, but);
761                 if(!(but->flag & UI_HIDDEN))
762                         ui_draw_but(C, ar, &style, but, &rect);
763         }
764         
765         /* restore matrix */
766         glMatrixMode(GL_PROJECTION);
767         glPopMatrix();
768         glMatrixMode(GL_MODELVIEW);
769         glPopMatrix();
770         
771         ui_draw_links(block);
772 }
773
774 /* ************* EVENTS ************* */
775
776 static void ui_is_but_sel(uiBut *but)
777 {
778         double value;
779         int lvalue;
780         short push=0, true=1;
781
782         value= ui_get_but_val(but);
783
784         if(ELEM3(but->type, TOGN, ICONTOGN, OPTIONN)) true= 0;
785
786         if( but->bit ) {
787                 lvalue= (int)value;
788                 if( BTST(lvalue, (but->bitnr)) ) push= true;
789                 else push= !true;
790         }
791         else {
792                 switch(but->type) {
793                 case BUT:
794                         push= 2;
795                         break;
796                 case HOTKEYEVT:
797                 case KEYEVT:
798                         push= 2;
799                         break;
800                 case TOGBUT:
801                 case TOG:
802                 case TOGR:
803                 case TOG3:
804                 case BUT_TOGDUAL:
805                 case ICONTOG:
806                 case OPTION:
807                         if(value!=but->hardmin) push= 1;
808                         break;
809                 case ICONTOGN:
810                 case TOGN:
811                 case OPTIONN:
812                         if(value==0.0) push= 1;
813                         break;
814                 case ROW:
815                 case LISTROW:
816                         if(value == but->hardmax) push= 1;
817                         break;
818                 case COL:
819                         push= 2;
820                         break;
821                 default:
822                         push= 2;
823                         break;
824                 }
825         }
826         
827         if(push==2);
828         else if(push==1) but->flag |= UI_SELECT;
829         else but->flag &= ~UI_SELECT;
830 }
831
832 /* XXX 2.50 no links supported yet */
833
834 static int uibut_contains_pt(uiBut *but, short *mval)
835 {
836         return 0;
837
838 }
839
840 uiBut *ui_get_valid_link_button(uiBlock *block, uiBut *but, short *mval)
841 {
842         uiBut *bt;
843         
844                 /* find button to link to */
845         for (bt= block->buttons.first; bt; bt= bt->next)
846                 if(bt!=but && uibut_contains_pt(bt, mval))
847                         break;
848
849         if (bt) {
850                 if (but->type==LINK && bt->type==INLINK) {
851                         if( but->link->tocode == (int)bt->hardmin ) {
852                                 return bt;
853                         }
854                 }
855                 else if(but->type==INLINK && bt->type==LINK) {
856                         if( bt->link->tocode == (int)but->hardmin ) {
857                                 return bt;
858                         }
859                 }
860         }
861
862         return NULL;
863 }
864
865
866 static uiBut *ui_find_inlink(uiBlock *block, void *poin)
867 {
868         uiBut *but;
869         
870         but= block->buttons.first;
871         while(but) {
872                 if(but->type==INLINK) {
873                         if(but->poin == poin) return but;
874                 }
875                 but= but->next;
876         }
877         return NULL;
878 }
879
880 static void ui_add_link_line(ListBase *listb, uiBut *but, uiBut *bt)
881 {
882         uiLinkLine *line;
883         
884         line= MEM_callocN(sizeof(uiLinkLine), "linkline");
885         BLI_addtail(listb, line);
886         line->from= but;
887         line->to= bt;
888 }
889
890 uiBut *uiFindInlink(uiBlock *block, void *poin)
891 {
892         return ui_find_inlink(block, poin);
893 }
894
895 void uiComposeLinks(uiBlock *block)
896 {
897         uiBut *but, *bt;
898         uiLink *link;
899         void ***ppoin;
900         int a;
901         
902         but= block->buttons.first;
903         while(but) {
904                 if(but->type==LINK) {
905                         link= but->link;
906                         
907                         /* for all pointers in the array */
908                         if(link) {
909                                 if(link->ppoin) {
910                                         ppoin= link->ppoin;
911                                         for(a=0; a < *(link->totlink); a++) {
912                                                 bt= ui_find_inlink(block, (*ppoin)[a] );
913                                                 if(bt) {
914                                                         ui_add_link_line(&link->lines, but, bt);
915                                                 }
916                                         }
917                                 }
918                                 else if(link->poin) {
919                                         bt= ui_find_inlink(block, *(link->poin) );
920                                         if(bt) {
921                                                 ui_add_link_line(&link->lines, but, bt);
922                                         }
923                                 }
924                         }
925                 }
926                 but= but->next;
927         }
928 }
929
930
931 /* ************************************************ */
932
933 void uiBlockSetButLock(uiBlock *block, int val, char *lockstr)
934 {
935         if(val) {
936                 block->lock |= val;
937                 block->lockstr= lockstr;
938         }
939 }
940
941 void uiBlockClearButLock(uiBlock *block)
942 {
943         block->lock= 0;
944         block->lockstr= NULL;
945 }
946
947 /* *************************************************************** */
948
949 void ui_delete_linkline(uiLinkLine *line, uiBut *but)
950 {
951         uiLink *link;
952         int a, b;
953         
954         BLI_remlink(&but->link->lines, line);
955
956         link= line->from->link;
957
958         /* are there more pointers allowed? */
959         if(link->ppoin) {
960                 
961                 if(*(link->totlink)==1) {
962                         *(link->totlink)= 0;
963                         MEM_freeN(*(link->ppoin));
964                         *(link->ppoin)= NULL;
965                 }
966                 else {
967                         b= 0;
968                         for(a=0; a< (*(link->totlink)); a++) {
969                                 
970                                 if( (*(link->ppoin))[a] != line->to->poin ) {
971                                         (*(link->ppoin))[b]= (*(link->ppoin))[a];
972                                         b++;
973                                 }
974                         }       
975                         (*(link->totlink))--;
976                 }
977         }
978         else {
979                 *(link->poin)= NULL;
980         }
981
982         MEM_freeN(line);
983         //REDRAW
984 }
985 /* XXX 2.50 no links supported yet */
986 #if 0
987 static void ui_delete_active_linkline(uiBlock *block)
988 {
989         uiBut *but;
990         uiLink *link;
991         uiLinkLine *line, *nline;
992         int a, b;
993         
994         but= block->buttons.first;
995         while(but) {
996                 if(but->type==LINK && but->link) {
997                         line= but->link->lines.first;
998                         while(line) {
999                                 
1000                                 nline= line->next;
1001                                 
1002                                 if(line->flag & UI_SELECT) {
1003                                         BLI_remlink(&but->link->lines, line);
1004
1005                                         link= line->from->link;
1006
1007                                         /* are there more pointers allowed? */
1008                                         if(link->ppoin) {
1009                                                 
1010                                                 if(*(link->totlink)==1) {
1011                                                         *(link->totlink)= 0;
1012                                                         MEM_freeN(*(link->ppoin));
1013                                                         *(link->ppoin)= NULL;
1014                                                 }
1015                                                 else {
1016                                                         b= 0;
1017                                                         for(a=0; a< (*(link->totlink)); a++) {
1018                                                                 
1019                                                                 if( (*(link->ppoin))[a] != line->to->poin ) {
1020                                                                         (*(link->ppoin))[b]= (*(link->ppoin))[a];
1021                                                                         b++;
1022                                                                 }
1023                                                         }       
1024                                                         (*(link->totlink))--;
1025                                                 }
1026                                         }
1027                                         else {
1028                                                 *(link->poin)= NULL;
1029                                         }
1030
1031                                         MEM_freeN(line);
1032                                 }
1033                                 line= nline;
1034                         }
1035                 }
1036                 but= but->next;
1037         }
1038         
1039         /* temporal! these buttons can be everywhere... */
1040         allqueue(REDRAWBUTSLOGIC, 0);
1041 }
1042
1043 static void ui_do_active_linklines(uiBlock *block, short *mval)
1044 {
1045         uiBut *but;
1046         uiLinkLine *line, *act= NULL;
1047         float mindist= 12.0, fac, v1[2], v2[2], v3[3];
1048         int foundone= 0; 
1049         
1050         if(mval) {
1051                 v1[0]= mval[0];
1052                 v1[1]= mval[1];
1053                 
1054                 /* find a line close to the mouse */
1055                 but= block->buttons.first;
1056                 while(but) {
1057                         if(but->type==LINK && but->link) {
1058                                 foundone= 1;
1059                                 line= but->link->lines.first;
1060                                 while(line) {
1061                                         v2[0]= line->from->x2;
1062                                         v2[1]= (line->from->y1+line->from->y2)/2.0;
1063                                         v3[0]= line->to->x1;
1064                                         v3[1]= (line->to->y1+line->to->y2)/2.0;
1065                                         
1066                                         fac= dist_to_line_segment_v2(v1, v2, v3);
1067                                         if(fac < mindist) {
1068                                                 mindist= fac;
1069                                                 act= line;
1070                                         }
1071                                         line= line->next;
1072                                 }
1073                         }
1074                         but= but->next;
1075                 }
1076         }
1077
1078         /* check for a 'found one' to prevent going to 'frontbuffer' mode.
1079                 this slows done gfx quite some, and at OSX the 'finish' forces a swapbuffer */
1080         if(foundone) {
1081                 glDrawBuffer(GL_FRONT);
1082                 
1083                 /* draw */
1084                 but= block->buttons.first;
1085                 while(but) {
1086                         if(but->type==LINK && but->link) {
1087                                 line= but->link->lines.first;
1088                                 while(line) {
1089                                         if(line==act) {
1090                                                 if((line->flag & UI_SELECT)==0) {
1091                                                         line->flag |= UI_SELECT;
1092                                                         ui_draw_linkline(but, line);
1093                                                 }
1094                                         }
1095                                         else if(line->flag & UI_SELECT) {
1096                                                 line->flag &= ~UI_SELECT;
1097                                                 ui_draw_linkline(but, line);
1098                                         }
1099                                         line= line->next;
1100                                 }
1101                         }
1102                         but= but->next;
1103                 }
1104                 bglFlush();
1105                 glDrawBuffer(GL_BACK);
1106         }
1107 }
1108 #endif
1109
1110 /* ******************************************************* */
1111
1112 /* XXX 2.50 no screendump supported yet */
1113
1114 #if 0
1115 /* nasty but safe way to store screendump rect */
1116 static int scr_x=0, scr_y=0, scr_sizex=0, scr_sizey=0;
1117
1118 static void ui_set_screendump_bbox(uiBlock *block)
1119 {
1120         if(block) {
1121                 scr_x= block->minx;
1122                 scr_y= block->miny;
1123                 scr_sizex= block->maxx - block->minx;
1124                 scr_sizey= block->maxy - block->miny;
1125         }
1126         else {
1127                 scr_sizex= scr_sizey= 0;
1128         }
1129 }
1130
1131 /* used for making screenshots for menus, called in screendump.c */
1132 int uiIsMenu(int *x, int *y, int *sizex, int *sizey)
1133 {
1134         if(scr_sizex!=0 && scr_sizey!=0) {
1135                 *x= scr_x;
1136                 *y= scr_y;
1137                 *sizex= scr_sizex;
1138                 *sizey= scr_sizey;
1139                 return 1;
1140         }
1141         
1142         return 0;
1143 }
1144 #endif
1145
1146 /* *********************** data get/set ***********************
1147  * this either works with the pointed to data, or can work with
1148  * an edit override pointer while dragging for example */
1149
1150 /* for buttons pointing to color for example */
1151 void ui_get_but_vectorf(uiBut *but, float *vec)
1152 {
1153         PropertyRNA *prop;
1154         int a, tot;
1155
1156         if(but->editvec) {
1157                 VECCOPY(vec, but->editvec);
1158         }
1159
1160         if(but->rnaprop) {
1161                 prop= but->rnaprop;
1162
1163                 vec[0]= vec[1]= vec[2]= 0.0f;
1164
1165                 if(RNA_property_type(prop) == PROP_FLOAT) {
1166                         tot= RNA_property_array_length(&but->rnapoin, prop);
1167                         tot= MIN2(tot, 3);
1168
1169                         for(a=0; a<tot; a++)
1170                                 vec[a]= RNA_property_float_get_index(&but->rnapoin, prop, a);
1171                 }
1172         }
1173         else if(but->pointype == CHA) {
1174                 char *cp= (char *)but->poin;
1175                 vec[0]= ((float)cp[0])/255.0;
1176                 vec[1]= ((float)cp[1])/255.0;
1177                 vec[2]= ((float)cp[2])/255.0;
1178         }
1179         else if(but->pointype == FLO) {
1180                 float *fp= (float *)but->poin;
1181                 VECCOPY(vec, fp);
1182         }
1183         else {
1184                 if (but->editvec==NULL) {
1185                         fprintf(stderr, "ui_get_but_vectorf: can't get color, should never happen\n");
1186                         vec[0]= vec[1]= vec[2]= 0.0f;
1187                 }
1188         }
1189 }
1190
1191 /* for buttons pointing to color for example */
1192 void ui_set_but_vectorf(uiBut *but, float *vec)
1193 {
1194         PropertyRNA *prop;
1195         int a, tot;
1196
1197         if(but->editvec) {
1198                 VECCOPY(but->editvec, vec);
1199         }
1200
1201         if(but->rnaprop) {
1202                 prop= but->rnaprop;
1203
1204                 if(RNA_property_type(prop) == PROP_FLOAT) {
1205                         tot= RNA_property_array_length(&but->rnapoin, prop);
1206                         tot= MIN2(tot, 3);
1207
1208                         for(a=0; a<tot; a++)
1209                                 RNA_property_float_set_index(&but->rnapoin, prop, a, vec[a]);
1210                 }
1211         }
1212         else if(but->pointype == CHA) {
1213                 char *cp= (char *)but->poin;
1214                 cp[0]= (char)(0.5 +vec[0]*255.0);
1215                 cp[1]= (char)(0.5 +vec[1]*255.0);
1216                 cp[2]= (char)(0.5 +vec[2]*255.0);
1217         }
1218         else if(but->pointype == FLO) {
1219                 float *fp= (float *)but->poin;
1220                 VECCOPY(fp, vec);
1221         }
1222 }
1223
1224 int ui_is_but_float(uiBut *but)
1225 {
1226         if(but->pointype==FLO && but->poin)
1227                 return 1;
1228         
1229         if(but->rnaprop && RNA_property_type(but->rnaprop) == PROP_FLOAT)
1230                 return 1;
1231         
1232         return 0;
1233 }
1234
1235 int ui_is_but_unit(uiBut *but)
1236 {
1237         Scene *scene= CTX_data_scene((bContext *)but->block->evil_C);
1238         int unit_type;
1239         
1240         if(but->rnaprop==NULL)
1241                 return 0;
1242         
1243         unit_type = RNA_SUBTYPE_UNIT(RNA_property_subtype(but->rnaprop));
1244         
1245         if (scene->unit.flag & USER_UNIT_ROT_RADIANS && unit_type == PROP_UNIT_ROTATION)
1246                 return 0;
1247         
1248         /* for now disable time unit conversion */      
1249         if (unit_type == PROP_UNIT_TIME)
1250                 return 0;
1251
1252         if (scene->unit.system == USER_UNIT_NONE) {
1253            if (unit_type != PROP_UNIT_ROTATION)
1254                         return 0;
1255         }
1256
1257         if(unit_type == PROP_UNIT_NONE)
1258                 return 0;
1259
1260         return 1;
1261 }
1262
1263 double ui_get_but_val(uiBut *but)
1264 {
1265         PropertyRNA *prop;
1266         double value = 0.0;
1267
1268         if(but->editval) { return *(but->editval); }
1269         if(but->poin==NULL && but->rnapoin.data==NULL) return 0.0;
1270
1271         if(but->rnaprop) {
1272                 prop= but->rnaprop;
1273
1274                 switch(RNA_property_type(prop)) {
1275                         case PROP_BOOLEAN:
1276                                 if(RNA_property_array_length(&but->rnapoin, prop))
1277                                         value= RNA_property_boolean_get_index(&but->rnapoin, prop, but->rnaindex);
1278                                 else
1279                                         value= RNA_property_boolean_get(&but->rnapoin, prop);
1280                                 break;
1281                         case PROP_INT:
1282                                 if(RNA_property_array_length(&but->rnapoin, prop))
1283                                         value= RNA_property_int_get_index(&but->rnapoin, prop, but->rnaindex);
1284                                 else
1285                                         value= RNA_property_int_get(&but->rnapoin, prop);
1286                                 break;
1287                         case PROP_FLOAT:
1288                                 if(RNA_property_array_length(&but->rnapoin, prop))
1289                                         value= RNA_property_float_get_index(&but->rnapoin, prop, but->rnaindex);
1290                                 else
1291                                         value= RNA_property_float_get(&but->rnapoin, prop);
1292                                 break;
1293                         case PROP_ENUM:
1294                                 value= RNA_property_enum_get(&but->rnapoin, prop);
1295                                 break;
1296                         default:
1297                                 value= 0.0;
1298                                 break;
1299                 }
1300         }
1301         else if(but->type== HSVSLI) {
1302                 float h, s, v, *fp;
1303                 
1304                 fp= (but->editvec)? but->editvec: (float *)but->poin;
1305                 rgb_to_hsv(fp[0], fp[1], fp[2], &h, &s, &v);
1306
1307                 switch(but->str[0]) {
1308                         case 'H': value= h; break;
1309                         case 'S': value= s; break;
1310                         case 'V': value= v; break;
1311                 }
1312         } 
1313         else if( but->pointype == CHA ) {
1314                 value= *(char *)but->poin;
1315         }
1316         else if( but->pointype == SHO ) {
1317                 value= *(short *)but->poin;
1318         } 
1319         else if( but->pointype == INT ) {
1320                 value= *(int *)but->poin;
1321         } 
1322         else if( but->pointype == FLO ) {
1323                 value= *(float *)but->poin;
1324         }
1325     
1326         return value;
1327 }
1328
1329 void ui_set_but_val(uiBut *but, double value)
1330 {
1331         PropertyRNA *prop;
1332
1333         /* value is a hsv value: convert to rgb */
1334         if(but->rnaprop) {
1335                 prop= but->rnaprop;
1336
1337                 if(RNA_property_editable(&but->rnapoin, prop)) {
1338                         switch(RNA_property_type(prop)) {
1339                                 case PROP_BOOLEAN:
1340                                         if(RNA_property_array_length(&but->rnapoin, prop))
1341                                                 RNA_property_boolean_set_index(&but->rnapoin, prop, but->rnaindex, value);
1342                                         else
1343                                                 RNA_property_boolean_set(&but->rnapoin, prop, value);
1344                                         break;
1345                                 case PROP_INT:
1346                                         if(RNA_property_array_length(&but->rnapoin, prop))
1347                                                 RNA_property_int_set_index(&but->rnapoin, prop, but->rnaindex, value);
1348                                         else
1349                                                 RNA_property_int_set(&but->rnapoin, prop, value);
1350                                         break;
1351                                 case PROP_FLOAT:
1352                                         if(RNA_property_array_length(&but->rnapoin, prop))
1353                                                 RNA_property_float_set_index(&but->rnapoin, prop, but->rnaindex, value);
1354                                         else
1355                                                 RNA_property_float_set(&but->rnapoin, prop, value);
1356                                         break;
1357                                 case PROP_ENUM:
1358                                         RNA_property_enum_set(&but->rnapoin, prop, value);
1359                                         break;
1360                                 default:
1361                                         break;
1362                         }
1363                 }
1364         }
1365         else if(but->pointype==0);
1366         else if(but->type==HSVSLI ) {
1367                 float h, s, v, *fp;
1368                 
1369                 fp= (but->editvec)? but->editvec: (float *)but->poin;
1370                 rgb_to_hsv(fp[0], fp[1], fp[2], &h, &s, &v);
1371                 
1372                 switch(but->str[0]) {
1373                 case 'H': h= value; break;
1374                 case 'S': s= value; break;
1375                 case 'V': v= value; break;
1376                 }
1377                 
1378                 hsv_to_rgb(h, s, v, fp, fp+1, fp+2);
1379                 
1380         }
1381         else {
1382                 /* first do rounding */
1383                 if(but->pointype==CHA)
1384                         value= (char)floor(value+0.5);
1385                 else if(but->pointype==SHO ) {
1386                         /* gcc 3.2.1 seems to have problems 
1387                          * casting a double like 32772.0 to
1388                          * a short so we cast to an int, then 
1389                          to a short */
1390                         int gcckludge;
1391                         gcckludge = (int) floor(value+0.5);
1392                         value= (short)gcckludge;
1393                 }
1394                 else if(but->pointype==INT )
1395                         value= (int)floor(value+0.5);
1396                 else if(but->pointype==FLO ) {
1397                         float fval= (float)value;
1398                         if(fval>= -0.00001f && fval<= 0.00001f) fval= 0.0f;     /* prevent negative zero */
1399                         value= fval;
1400                 }
1401                 
1402                 /* then set value with possible edit override */
1403                 if(but->editval)
1404                         *but->editval= value;
1405                 else if(but->pointype==CHA)
1406                         *((char *)but->poin)= (char)value;
1407                 else if(but->pointype==SHO)
1408                         *((short *)but->poin)= (short)value;
1409                 else if(but->pointype==INT)
1410                         *((int *)but->poin)= (int)value;
1411                 else if(but->pointype==FLO)
1412                         *((float *)but->poin)= (float)value;
1413         }
1414
1415         /* update select flag */
1416         ui_is_but_sel(but);
1417 }
1418
1419 int ui_get_but_string_max_length(uiBut *but)
1420 {
1421         if(ELEM(but->type, TEX, SEARCH_MENU))
1422                 return but->hardmax;
1423         else if(but->type == IDPOIN)
1424                 return sizeof(((ID*)NULL)->name)-2;
1425         else
1426                 return UI_MAX_DRAW_STR;
1427 }
1428
1429 static double ui_get_but_scale_unit(uiBut *but, double value)
1430 {
1431         Scene *scene= CTX_data_scene((bContext *)but->block->evil_C);
1432         int subtype= RNA_SUBTYPE_UNIT(RNA_property_subtype(but->rnaprop));
1433
1434         if(subtype == PROP_UNIT_LENGTH) {
1435                 return value * scene->unit.scale_length;
1436         }
1437         else if(subtype == PROP_UNIT_AREA) {
1438                 return value * pow(scene->unit.scale_length, 2);
1439         }
1440         else if(subtype == PROP_UNIT_VOLUME) {
1441                 return value * pow(scene->unit.scale_length, 3);
1442         }
1443         else if(subtype == PROP_UNIT_TIME) { /* WARNING - using evil_C :| */
1444                 return FRA2TIME(value);
1445         }
1446         else {
1447                 return value;
1448         }
1449 }
1450
1451 static void ui_get_but_string_unit(uiBut *but, char *str, int len_max, double value, int pad)
1452 {
1453         Scene *scene= CTX_data_scene((bContext *)but->block->evil_C);
1454         int do_split= scene->unit.flag & USER_UNIT_OPT_SPLIT;
1455         int unit_type=  RNA_SUBTYPE_UNIT_VALUE(RNA_property_subtype(but->rnaprop));
1456         int precision= but->a2;
1457
1458         if(scene->unit.scale_length<0.0001) scene->unit.scale_length= 1.0; // XXX do_versions
1459
1460         /* Sanity checks */
1461         if(precision>4)         precision= 4;
1462         else if(precision==0)   precision= 2;
1463
1464         bUnit_AsString(str, len_max, ui_get_but_scale_unit(but, value), precision, scene->unit.system, unit_type, do_split, pad);
1465 }
1466
1467 static float ui_get_but_step_unit(uiBut *but, double value, float step_default)
1468 {
1469         Scene *scene= CTX_data_scene((bContext *)but->block->evil_C);
1470         int unit_type=  RNA_SUBTYPE_UNIT_VALUE(RNA_property_subtype(but->rnaprop));
1471         float step;
1472
1473         step = bUnit_ClosestScalar(ui_get_but_scale_unit(but, value), scene->unit.system, unit_type);
1474
1475         if(step > 0.0) { /* -1 is an error value */
1476                 return (step/ui_get_but_scale_unit(but, 1.0))*100;
1477         }
1478         else {
1479                 return step_default;
1480         }
1481 }
1482
1483
1484 void ui_get_but_string(uiBut *but, char *str, int maxlen)
1485 {
1486         if(but->rnaprop && ELEM3(but->type, TEX, IDPOIN, SEARCH_MENU)) {
1487                 PropertyType type;
1488                 char *buf= NULL;
1489
1490                 type= RNA_property_type(but->rnaprop);
1491
1492                 if(type == PROP_STRING) {
1493                         /* RNA string */
1494                         buf= RNA_property_string_get_alloc(&but->rnapoin, but->rnaprop, str, maxlen);
1495                 }
1496                 else if(type == PROP_POINTER) {
1497                         /* RNA pointer */
1498                         PointerRNA ptr= RNA_property_pointer_get(&but->rnapoin, but->rnaprop);
1499                         buf= RNA_struct_name_get_alloc(&ptr, str, maxlen);
1500                 }
1501
1502                 if(!buf) {
1503                         str[0] = '\0';
1504                 }
1505                 else if(buf && buf != str) {
1506                         /* string was too long, we have to truncate */
1507                         BLI_strncpy(str, buf, maxlen);
1508                         MEM_freeN(buf);
1509                 }
1510         }
1511         else if(but->type == IDPOIN) {
1512                 /* ID pointer */
1513                 if(but->idpoin_idpp) { /* Can be NULL for ID properties by python */
1514                         ID *id= *(but->idpoin_idpp);
1515                         if(id) {
1516                                 BLI_strncpy(str, id->name+2, maxlen);
1517                                 return;
1518                         }
1519                 }
1520                 str[0] = '\0';
1521                 return;
1522         }
1523         else if(but->type == TEX) {
1524                 /* string */
1525                 BLI_strncpy(str, but->poin, maxlen);
1526                 return;
1527         }
1528         else if(but->type == SEARCH_MENU) {
1529                 /* string */
1530                 BLI_strncpy(str, but->poin, maxlen);
1531                 return;
1532         }
1533         else if(ui_but_anim_expression_get(but, str, maxlen))
1534                 ; /* driver expression */
1535         else {
1536                 /* number editing */
1537                 double value;
1538
1539                 value= ui_get_but_val(but);
1540
1541                 if(ui_is_but_float(but)) {
1542                         if(ui_is_but_unit(but)) {
1543                                 ui_get_but_string_unit(but, str, maxlen, value, 0);
1544                         }
1545                         else if(but->a2) { /* amount of digits defined */
1546                                 if(but->a2==1) BLI_snprintf(str, maxlen, "%.1f", value);
1547                                 else if(but->a2==2) BLI_snprintf(str, maxlen, "%.2f", value);
1548                                 else if(but->a2==3) BLI_snprintf(str, maxlen, "%.3f", value);
1549                                 else BLI_snprintf(str, maxlen, "%.4f", value);
1550                         }
1551                         else
1552                                 BLI_snprintf(str, maxlen, "%.3f", value);
1553                 }
1554                 else
1555                         BLI_snprintf(str, maxlen, "%d", (int)value);
1556         }
1557 }
1558
1559 int ui_set_but_string(bContext *C, uiBut *but, const char *str)
1560 {
1561         if(but->rnaprop && ELEM3(but->type, TEX, IDPOIN, SEARCH_MENU)) {
1562                 if(RNA_property_editable(&but->rnapoin, but->rnaprop)) {
1563                         PropertyType type;
1564
1565                         type= RNA_property_type(but->rnaprop);
1566
1567                         if(type == PROP_STRING) {
1568                                 /* RNA string */
1569                                 RNA_property_string_set(&but->rnapoin, but->rnaprop, str);
1570                                 return 1;
1571                         }
1572                         else if(type == PROP_POINTER) {
1573                                 /* RNA pointer */
1574                                 PointerRNA ptr, rptr;
1575                                 PropertyRNA *prop;
1576
1577                                 if(str == NULL || str[0] == '\0') {
1578                                         RNA_property_pointer_set(&but->rnapoin, but->rnaprop, PointerRNA_NULL);
1579                                         return 1;
1580                                 }
1581                                 else {
1582                                         ptr= but->rnasearchpoin;
1583                                         prop= but->rnasearchprop;
1584                                         
1585                                         if(prop && RNA_property_collection_lookup_string(&ptr, prop, str, &rptr))
1586                                                 RNA_property_pointer_set(&but->rnapoin, but->rnaprop, rptr);
1587
1588                                         return 1;
1589                                 }
1590
1591                                 return 0;
1592                         }
1593                 }
1594         }
1595         else if(but->type == IDPOIN) {
1596                 /* ID pointer */
1597                 but->idpoin_func(C, (char*)str, but->idpoin_idpp);
1598                 return 1;
1599         }
1600         else if(but->type == TEX) {
1601                 /* string */
1602                 BLI_strncpy(but->poin, str, but->hardmax);
1603                 return 1;
1604         }
1605         else if(but->type == SEARCH_MENU) {
1606                 /* string */
1607                 BLI_strncpy(but->poin, str, but->hardmax);
1608                 return 1;
1609         }
1610         else if(ui_but_anim_expression_set(but, str)) {
1611                 /* driver expression */
1612                 return 1;
1613         }
1614         else {
1615                 /* number editing */
1616                 double value;
1617
1618 #ifndef DISABLE_PYTHON
1619                 {
1620                         char str_unit_convert[256];
1621                         int unit_type;
1622                         Scene *scene= CTX_data_scene((bContext *)but->block->evil_C);
1623
1624                         if(but->rnaprop)
1625                                 unit_type= RNA_SUBTYPE_UNIT_VALUE(RNA_property_subtype(but->rnaprop));
1626                         else
1627                                 unit_type= 0;
1628
1629                         BLI_strncpy(str_unit_convert, str, sizeof(str_unit_convert));
1630
1631                         if(ui_is_but_unit(but)) {
1632                                 /* ugly, use the draw string to get the value, this could cause problems if it includes some text which resolves to a unit */
1633                                 bUnit_ReplaceString(str_unit_convert, sizeof(str_unit_convert), but->drawstr, ui_get_but_scale_unit(but, 1.0), scene->unit.system, unit_type);
1634                         }
1635
1636                         if(BPY_button_eval(C, str_unit_convert, &value)) {
1637                                 value = ui_get_but_val(but); /* use its original value */
1638
1639                                 if(str[0])
1640                                         return 0;
1641                         }
1642                 }
1643 #else
1644                 value= atof(str);
1645 #endif
1646
1647                 if(!ui_is_but_float(but)) value= (int)value;
1648                 if(but->type==NUMABS) value= fabs(value);
1649
1650                 /* not that we use hard limits here */
1651                 if(value<but->hardmin) value= but->hardmin;
1652                 if(value>but->hardmax) value= but->hardmax;
1653
1654                 ui_set_but_val(but, value);
1655                 return 1;
1656         }
1657
1658         return 0;
1659 }
1660
1661 void ui_set_but_default(bContext *C, uiBut *but)
1662 {
1663         /* if there is a valid property that is editable... */
1664         if (but->rnapoin.data && but->rnaprop && RNA_property_editable(&but->rnapoin, but->rnaprop)) {
1665                 if(RNA_property_reset(&but->rnapoin, but->rnaprop, -1)) {
1666                         /* perform updates required for this property */
1667                         RNA_property_update(C, &but->rnapoin, but->rnaprop);
1668                 }
1669         }
1670 }
1671
1672 static double soft_range_round_up(double value, double max)
1673 {
1674         /* round up to .., 0.1, 0.2, 0.5, 1, 2, 5, 10, 20, 50, .. */
1675         double newmax= pow(10.0, ceil(log(value)/log(10.0)));
1676
1677         if(newmax*0.2 >= max && newmax*0.2 >= value)
1678                 return newmax*0.2;
1679         else if(newmax*0.5 >= max && newmax*0.5 >= value)
1680                 return newmax*0.5;
1681         else
1682                 return newmax;
1683 }
1684
1685 static double soft_range_round_down(double value, double max)
1686 {
1687         /* round down to .., 0.1, 0.2, 0.5, 1, 2, 5, 10, 20, 50, .. */
1688         double newmax= pow(10.0, floor(log(value)/log(10.0)));
1689
1690         if(newmax*5.0 <= max && newmax*5.0 <= value)
1691                 return newmax*5.0;
1692         else if(newmax*2.0 <= max && newmax*2.0 <= value)
1693                 return newmax*2.0;
1694         else
1695                 return newmax;
1696 }
1697
1698 void ui_set_but_soft_range(uiBut *but, double value)
1699 {
1700         PropertyType type;
1701         double softmin, softmax, step, precision;
1702         
1703         if(but->rnaprop) {
1704                 type= RNA_property_type(but->rnaprop);
1705
1706                 /* clamp button range to something reasonable in case
1707                  * we get -inf/inf from RNA properties */
1708                 if(type == PROP_INT) {
1709                         int imin, imax, istep;
1710
1711                         RNA_property_int_ui_range(&but->rnapoin, but->rnaprop, &imin, &imax, &istep);
1712                         softmin= (imin == INT_MIN)? -1e4: imin;
1713                         softmax= (imin == INT_MAX)? 1e4: imax;
1714                         step= istep;
1715                         precision= 1;
1716                 }
1717                 else if(type == PROP_FLOAT) {
1718                         float fmin, fmax, fstep, fprecision;
1719
1720                         RNA_property_float_ui_range(&but->rnapoin, but->rnaprop, &fmin, &fmax, &fstep, &fprecision);
1721                         softmin= (fmin == -FLT_MAX)? -1e4: fmin;
1722                         softmax= (fmax == FLT_MAX)? 1e4: fmax;
1723                         step= fstep;
1724                         precision= fprecision;
1725                 }
1726                 else
1727                         return;
1728
1729                 /* if the value goes out of the soft/max range, adapt the range */
1730                 if(value+1e-10 < softmin) {
1731                         if(value < 0.0)
1732                                 softmin= -soft_range_round_up(-value, -softmin);
1733                         else
1734                                 softmin= soft_range_round_down(value, softmin);
1735
1736                         if(softmin < but->hardmin)
1737                                 softmin= but->hardmin;
1738                 }
1739                 else if(value-1e-10 > softmax) {
1740                         if(value < 0.0)
1741                                 softmax= -soft_range_round_down(-value, -softmax);
1742                         else
1743                                 softmax= soft_range_round_up(value, softmax);
1744
1745                         if(softmax > but->hardmax)
1746                                 softmax= but->hardmax;
1747                 }
1748
1749                 but->softmin= softmin;
1750                 but->softmax= softmax;
1751         }
1752 }
1753
1754 /* ******************* Free ********************/
1755
1756 static void ui_free_link(uiLink *link)
1757 {
1758         if(link) {      
1759                 BLI_freelistN(&link->lines);
1760                 MEM_freeN(link);
1761         }
1762 }
1763
1764 /* can be called with C==NULL */
1765 static void ui_free_but(const bContext *C, uiBut *but)
1766 {
1767         if(but->opptr) {
1768                 WM_operator_properties_free(but->opptr);
1769                 MEM_freeN(but->opptr);
1770         }
1771         if(but->func_argN) MEM_freeN(but->func_argN);
1772         if(but->active) {
1773                 /* XXX solve later, buttons should be free-able without context? */
1774                 if(C) 
1775                         ui_button_active_cancel(C, but);
1776                 else
1777                         if(but->active) 
1778                                 MEM_freeN(but->active);
1779         }
1780         if(but->str && but->str != but->strdata) MEM_freeN(but->str);
1781         ui_free_link(but->link);
1782
1783         MEM_freeN(but);
1784 }
1785
1786 /* can be called with C==NULL */
1787 void uiFreeBlock(const bContext *C, uiBlock *block)
1788 {
1789         uiBut *but;
1790
1791         while( (but= block->buttons.first) ) {
1792                 BLI_remlink(&block->buttons, but);      
1793                 ui_free_but(C, but);
1794         }
1795
1796         if(block->func_argN)
1797                 MEM_freeN(block->func_argN);
1798
1799         CTX_store_free_list(&block->contexts);
1800
1801         BLI_freelistN(&block->saferct);
1802         
1803         MEM_freeN(block);
1804 }
1805
1806 /* can be called with C==NULL */
1807 void uiFreeBlocks(const bContext *C, ListBase *lb)
1808 {
1809         uiBlock *block;
1810         
1811         while( (block= lb->first) ) {
1812                 BLI_remlink(lb, block);
1813                 uiFreeBlock(C, block);
1814         }
1815 }
1816
1817 void uiFreeInactiveBlocks(const bContext *C, ListBase *lb)
1818 {
1819         uiBlock *block, *nextblock;
1820
1821         for(block=lb->first; block; block=nextblock) {
1822                 nextblock= block->next;
1823         
1824                 if(!block->handle) {
1825                         if(!block->active) {
1826                                 BLI_remlink(lb, block);
1827                                 uiFreeBlock(C, block);
1828                         }
1829                         else
1830                                 block->active= 0;
1831                 }
1832         }
1833 }
1834
1835 void uiBlockSetRegion(uiBlock *block, ARegion *region)
1836 {
1837         ListBase *lb;
1838         uiBlock *oldblock= NULL;
1839
1840         lb= &region->uiblocks;
1841         
1842         /* each listbase only has one block with this name, free block
1843          * if is already there so it can be rebuilt from scratch */
1844         if(lb) {
1845                 for (oldblock= lb->first; oldblock; oldblock= oldblock->next)
1846                         if (BLI_streq(oldblock->name, block->name))
1847                                 break;
1848
1849                 if (oldblock) {
1850                         oldblock->active= 0;
1851                         oldblock->panel= NULL;
1852                 }
1853         }
1854
1855         block->oldblock= oldblock;
1856
1857         /* at the beginning of the list! for dynamical menus/blocks */
1858         if(lb)
1859                 BLI_addhead(lb, block);
1860 }
1861
1862 uiBlock *uiBeginBlock(const bContext *C, ARegion *region, const char *name, short dt)
1863 {
1864         uiBlock *block;
1865         wmWindow *window;
1866         Scene *scn;
1867         int getsizex, getsizey;
1868
1869         window= CTX_wm_window(C);
1870         scn = CTX_data_scene(C);
1871
1872         block= MEM_callocN(sizeof(uiBlock), "uiBlock");
1873         block->active= 1;
1874         block->dt= dt;
1875         block->evil_C= (void*)C; // XXX
1876         if (scn) block->color_profile= (scn->r.color_mgt_flag & R_COLOR_MANAGEMENT);
1877         BLI_strncpy(block->name, name, sizeof(block->name));
1878
1879         if(region)
1880                 uiBlockSetRegion(block, region);
1881
1882         /* window matrix and aspect */
1883         if(region && region->swinid) {
1884                 wm_subwindow_getmatrix(window, region->swinid, block->winmat);
1885                 wm_subwindow_getsize(window, region->swinid, &getsizex, &getsizey);
1886
1887                 /* TODO - investigate why block->winmat[0][0] is negative
1888                  * in the image view when viewRedrawForce is called */
1889                 block->aspect= 2.0/fabs( (getsizex)*block->winmat[0][0]);
1890         }
1891         else {
1892                 /* no subwindow created yet, for menus for example, so we
1893                  * use the main window instead, since buttons are created
1894                  * there anyway */
1895                 wm_subwindow_getmatrix(window, window->screen->mainwin, block->winmat);
1896                 wm_subwindow_getsize(window, window->screen->mainwin, &getsizex, &getsizey);
1897
1898                 block->aspect= 2.0/fabs(getsizex*block->winmat[0][0]);
1899                 block->auto_open= 2;
1900                 block->flag |= UI_BLOCK_LOOP; /* tag as menu */
1901         }
1902
1903         return block;
1904 }
1905
1906 uiBlock *uiGetBlock(char *name, ARegion *ar)
1907 {
1908         uiBlock *block= ar->uiblocks.first;
1909         
1910         while(block) {
1911                 if( strcmp(name, block->name)==0 ) return block;
1912                 block= block->next;
1913         }
1914         
1915         return NULL;
1916 }
1917
1918 void uiBlockSetEmboss(uiBlock *block, short dt)
1919 {
1920         block->dt= dt;
1921 }
1922
1923 void ui_check_but(uiBut *but)
1924 {
1925         /* if something changed in the button */
1926         double value;
1927         float okwidth;
1928 //      int transopts= ui_translate_buttons();
1929         
1930         ui_is_but_sel(but);
1931         
1932 //      if(but->type==TEX || but->type==IDPOIN) transopts= 0;
1933
1934         /* only update soft range while not editing */
1935         if(but->rnaprop && !(but->editval || but->editstr || but->editvec))
1936                 ui_set_but_soft_range(but, ui_get_but_val(but));
1937
1938         /* test for min and max, icon sliders, etc */
1939         switch( but->type ) {
1940                 case NUM:
1941                 case SLI:
1942                 case SCROLL:
1943                 case NUMSLI:
1944                 case HSVSLI:
1945                         value= ui_get_but_val(but);
1946                         if(value < but->hardmin) ui_set_but_val(but, but->hardmin);
1947                         else if(value > but->hardmax) ui_set_but_val(but, but->hardmax);
1948                         break;
1949                         
1950                 case NUMABS:
1951                         value= fabs( ui_get_but_val(but) );
1952                         if(value < but->hardmin) ui_set_but_val(but, but->hardmin);
1953                         else if(value > but->hardmax) ui_set_but_val(but, but->hardmax);
1954                         break;
1955                         
1956                 case ICONTOG: 
1957                 case ICONTOGN:
1958                         if(!but->rnaprop || (RNA_property_flag(but->rnaprop) & PROP_ICONS_CONSECUTIVE)) {
1959                                 if(but->flag & UI_SELECT) but->iconadd= 1;
1960                                 else but->iconadd= 0;
1961                         }
1962                         break;
1963                         
1964                 case ICONROW:
1965                         if(!but->rnaprop || (RNA_property_flag(but->rnaprop) & PROP_ICONS_CONSECUTIVE)) {
1966                                 value= ui_get_but_val(but);
1967                                 but->iconadd= (int)value- (int)(but->hardmin);
1968                         }
1969                         break;
1970                         
1971                 case ICONTEXTROW:
1972                         if(!but->rnaprop || (RNA_property_flag(but->rnaprop) & PROP_ICONS_CONSECUTIVE)) {
1973                                 value= ui_get_but_val(but);
1974                                 but->iconadd= (int)value- (int)(but->hardmin);
1975                         }
1976                         break;
1977         }
1978         
1979         
1980         /* safety is 4 to enable small number buttons (like 'users') */
1981         okwidth= -4 + (but->x2 - but->x1); 
1982         
1983         /* name: */
1984         switch( but->type ) {
1985         
1986         case MENU:
1987         case ICONTEXTROW:
1988                 
1989                 if(but->x2 - but->x1 > 24) {
1990                         value= ui_get_but_val(but);
1991                         ui_set_name_menu(but, (int)value);
1992                 }
1993                 break;
1994         
1995         case NUM:
1996         case NUMSLI:
1997         case HSVSLI:
1998         case NUMABS:
1999
2000                 value= ui_get_but_val(but);
2001
2002                 if(ui_is_but_float(but)) {
2003                         if(value == FLT_MAX) sprintf(but->drawstr, "%sinf", but->str);
2004                         else if(value == -FLT_MAX) sprintf(but->drawstr, "%s-inf", but->str);
2005                         /* support length type buttons */
2006                         else if(ui_is_but_unit(but)) {
2007                                 char new_str[sizeof(but->drawstr)];
2008                                 ui_get_but_string_unit(but, new_str, sizeof(new_str), value, TRUE);
2009                                 BLI_snprintf(but->drawstr, sizeof(but->drawstr), "%s%s", but->str, new_str);
2010                         }
2011                         else if(but->a2) { /* amount of digits defined */
2012                                 if(but->a2==1) sprintf(but->drawstr, "%s%.1f", but->str, value);
2013                                 else if(but->a2==2) sprintf(but->drawstr, "%s%.2f", but->str, value);
2014                                 else if(but->a2==3) sprintf(but->drawstr, "%s%.3f", but->str, value);
2015                                 else sprintf(but->drawstr, "%s%.4f", but->str, value);
2016                         }
2017                         else {
2018                                 if(but->hardmax<10.001) sprintf(but->drawstr, "%s%.3f", but->str, value);
2019                                 else sprintf(but->drawstr, "%s%.2f", but->str, value);
2020                         }
2021                 }
2022                 else {
2023                         sprintf(but->drawstr, "%s%d", but->str, (int)value);
2024                 }
2025                         
2026                 if(but->rnaprop) {
2027                         PropertySubType pstype = RNA_property_subtype(but->rnaprop);
2028                         
2029                         if (pstype == PROP_PERCENTAGE)
2030                                 strcat(but->drawstr, "%");
2031                 }
2032                 break;
2033
2034         case LABEL:
2035                 if(ui_is_but_float(but)) {
2036                         value= ui_get_but_val(but);
2037                         if(but->a2) { /* amount of digits defined */
2038                                 if(but->a2==1) sprintf(but->drawstr, "%s%.1f", but->str, value);
2039                                 else if(but->a2==2) sprintf(but->drawstr, "%s%.2f", but->str, value);
2040                                 else if(but->a2==3) sprintf(but->drawstr, "%s%.3f", but->str, value);
2041                                 else sprintf(but->drawstr, "%s%.4f", but->str, value);
2042                         }
2043                         else {
2044                                 sprintf(but->drawstr, "%s%.2f", but->str, value);
2045                         }
2046                 }
2047                 else strncpy(but->drawstr, but->str, UI_MAX_DRAW_STR);
2048                 
2049                 break;
2050
2051         case IDPOIN:
2052         case TEX:
2053         case SEARCH_MENU:
2054                 if(!but->editstr) {
2055                         char str[UI_MAX_DRAW_STR];
2056
2057                         ui_get_but_string(but, str, UI_MAX_DRAW_STR-strlen(but->str));
2058
2059                         strcpy(but->drawstr, but->str);
2060                         strcat(but->drawstr, str);
2061                 }
2062                 break;
2063         
2064         case KEYEVT:
2065                 strncpy(but->drawstr, but->str, UI_MAX_DRAW_STR);
2066                 if (but->flag & UI_SELECT) {
2067                         strcat(but->drawstr, "Press a key");
2068                 } else {
2069                         strcat(but->drawstr, WM_key_event_string((short) ui_get_but_val(but)));
2070                 }
2071                 break;
2072                 
2073         case HOTKEYEVT:
2074                 if (but->flag & UI_SELECT) {
2075                         but->drawstr[0]= '\0';
2076                         
2077                         if(but->modifier_key) {
2078                                 char *str= but->drawstr;
2079                                 
2080                                 if(but->modifier_key & KM_SHIFT)
2081                                         str= strcat(str, "Shift ");
2082                                 if(but->modifier_key & KM_CTRL)
2083                                         str= strcat(str, "Ctrl ");
2084                                 if(but->modifier_key & KM_ALT)
2085                                         str= strcat(str, "Alt ");
2086                                 if(but->modifier_key & KM_OSKEY)
2087                                         str= strcat(str, "Cmd ");
2088                         }
2089                         else
2090                                 strcat(but->drawstr, "Press a key  ");
2091                 }
2092                 else
2093                         strncpy(but->drawstr, but->str, UI_MAX_DRAW_STR);
2094
2095                 break;
2096                 
2097         case BUT_TOGDUAL:
2098                 /* trying to get the dual-icon to left of text... not very nice */
2099                 if(but->str[0]) {
2100                         strncpy(but->drawstr, "  ", UI_MAX_DRAW_STR);
2101                         strncpy(but->drawstr+2, but->str, UI_MAX_DRAW_STR-2);
2102                 }
2103                 break;
2104
2105         case HSVCUBE:
2106         case HSVCIRCLE:
2107                 {
2108                         float rgb[3];
2109                         ui_get_but_vectorf(but, rgb);
2110                         rgb_to_hsv(rgb[0], rgb[1], rgb[2], but->hsv, but->hsv+1, but->hsv+2);
2111                 }
2112                 break;
2113         default:
2114                 strncpy(but->drawstr, but->str, UI_MAX_DRAW_STR);
2115                 
2116         }
2117
2118         /* if we are doing text editing, this will override the drawstr */
2119         if(but->editstr)
2120                 strncpy(but->drawstr, but->editstr, UI_MAX_DRAW_STR);
2121         
2122         /* text clipping moved to widget drawing code itself */
2123 }
2124
2125
2126 void uiBlockBeginAlign(uiBlock *block)
2127 {
2128         /* if other align was active, end it */
2129         if(block->flag & UI_BUT_ALIGN) uiBlockEndAlign(block);
2130
2131         block->flag |= UI_BUT_ALIGN_DOWN;       
2132         block->alignnr++;
2133
2134         /* buttons declared after this call will get this align nr */ // XXX flag?
2135 }
2136
2137 static int buts_are_horiz(uiBut *but1, uiBut *but2)
2138 {
2139         float dx, dy;
2140         
2141         dx= fabs( but1->x2 - but2->x1);
2142         dy= fabs( but1->y1 - but2->y2);
2143         
2144         if(dx > dy) return 0;
2145         return 1;
2146 }
2147
2148 void uiBlockEndAlign(uiBlock *block)
2149 {
2150         block->flag &= ~UI_BUT_ALIGN;   // all 4 flags
2151 }
2152
2153 int ui_but_can_align(uiBut *but)
2154 {
2155         return !ELEM3(but->type, LABEL, OPTION, OPTIONN);
2156 }
2157
2158 static void ui_block_do_align_but(uiBlock *block, uiBut *first, int nr)
2159 {
2160         uiBut *prev, *but=NULL, *next;
2161         int flag= 0, cols=0, rows=0;
2162         
2163         /* auto align */
2164
2165         for(but=first; but && but->alignnr == nr; but=but->next) {
2166                 if(but->next && but->next->alignnr == nr) {
2167                         if(buts_are_horiz(but, but->next)) cols++;
2168                         else rows++;
2169                 }
2170         }
2171
2172         /* rows==0: 1 row, cols==0: 1 collumn */
2173         
2174         /* note;  how it uses 'flag' in loop below (either set it, or OR it) is confusing */
2175         for(but=first, prev=NULL; but && but->alignnr == nr; prev=but, but=but->next) {
2176                 next= but->next;
2177                 if(next && next->alignnr != nr)
2178                         next= NULL;
2179
2180                 /* clear old flag */
2181                 but->flag &= ~UI_BUT_ALIGN;
2182                 
2183                 if(flag==0) {   /* first case */
2184                         if(next) {
2185                                 if(buts_are_horiz(but, next)) {
2186                                         if(rows==0)
2187                                                 flag= UI_BUT_ALIGN_RIGHT;
2188                                         else 
2189                                                 flag= UI_BUT_ALIGN_DOWN|UI_BUT_ALIGN_RIGHT;
2190                                 }
2191                                 else {
2192                                         flag= UI_BUT_ALIGN_DOWN;
2193                                 }
2194                         }
2195                 }
2196                 else if(next==NULL) {   /* last case */
2197                         if(prev) {
2198                                 if(buts_are_horiz(prev, but)) {
2199                                         if(rows==0) 
2200                                                 flag= UI_BUT_ALIGN_LEFT;
2201                                         else
2202                                                 flag= UI_BUT_ALIGN_TOP|UI_BUT_ALIGN_LEFT;
2203                                 }
2204                                 else flag= UI_BUT_ALIGN_TOP;
2205                         }
2206                 }
2207                 else if(buts_are_horiz(but, next)) {
2208                         /* check if this is already second row */
2209                         if( prev && buts_are_horiz(prev, but)==0) {
2210                                 flag &= ~UI_BUT_ALIGN_LEFT;
2211                                 flag |= UI_BUT_ALIGN_TOP;
2212                                 /* exception case: bottom row */
2213                                 if(rows>0) {
2214                                         uiBut *bt= but;
2215                                         while(bt && bt->alignnr == nr) {
2216                                                 if(bt->next && bt->next->alignnr == nr && buts_are_horiz(bt, bt->next)==0 ) break; 
2217                                                 bt= bt->next;
2218                                         }
2219                                         if(bt==0 || bt->alignnr != nr) flag= UI_BUT_ALIGN_TOP|UI_BUT_ALIGN_RIGHT;
2220                                 }
2221                         }
2222                         else flag |= UI_BUT_ALIGN_LEFT;
2223                 }
2224                 else {
2225                         if(cols==0) {
2226                                 flag |= UI_BUT_ALIGN_TOP;
2227                         }
2228                         else {  /* next button switches to new row */
2229                                 
2230                                 if(prev && buts_are_horiz(prev, but))
2231                                    flag |= UI_BUT_ALIGN_LEFT;
2232                                 
2233                                 if( (flag & UI_BUT_ALIGN_TOP)==0) {     /* stil top row */
2234                                         if(prev)
2235                                                 flag= UI_BUT_ALIGN_DOWN|UI_BUT_ALIGN_LEFT;
2236                                         else 
2237                                                 flag |= UI_BUT_ALIGN_DOWN;
2238                                 }
2239                                 else 
2240                                         flag |= UI_BUT_ALIGN_TOP;
2241                         }
2242                 }
2243                 
2244                 but->flag |= flag;
2245                 
2246                 /* merge coordinates */
2247                 if(prev) {
2248                         // simple cases 
2249                         if(rows==0) {
2250                                 but->x1= (prev->x2+but->x1)/2.0;
2251                                 prev->x2= but->x1;
2252                         }
2253                         else if(cols==0) {
2254                                 but->y2= (prev->y1+but->y2)/2.0;
2255                                 prev->y1= but->y2;
2256                         }
2257                         else {
2258                                 if(buts_are_horiz(prev, but)) {
2259                                         but->x1= (prev->x2+but->x1)/2.0;
2260                                         prev->x2= but->x1;
2261                                         /* copy height too */
2262                                         but->y2= prev->y2;
2263                                 }
2264                                 else if(prev->prev && buts_are_horiz(prev->prev, prev)==0) {
2265                                         /* the previous button is a single one in its row */
2266                                         but->y2= (prev->y1+but->y2)/2.0;
2267                                         prev->y1= but->y2;
2268                                 }
2269                                 else {
2270                                         /* the previous button is not a single one in its row */
2271                                         but->y2= prev->y1;
2272                                 }
2273                         }
2274                 }
2275         }
2276 }
2277
2278 void ui_block_do_align(uiBlock *block)
2279 {
2280         uiBut *but;
2281         int nr;
2282
2283         /* align buttons with same align nr */
2284         for(but=block->buttons.first; but;) {
2285                 if(but->alignnr) {
2286                         nr= but->alignnr;
2287                         ui_block_do_align_but(block, but, nr);
2288
2289                         /* skip with same number */
2290                         for(; but && but->alignnr == nr; but=but->next);
2291
2292                         if(!but)
2293                                 break;
2294                 }
2295                 else
2296                         but= but->next;
2297         }
2298 }
2299
2300 /*
2301 ui_def_but is the function that draws many button types
2302
2303 for float buttons:
2304         "a1" Click Step (how much to change the value each click)
2305         "a2" Number of decimal point values to display. 0 defaults to 3 (0.000) 1,2,3, and a maximum of 4,
2306            all greater values will be clamped to 4.
2307
2308 */
2309 static uiBut *ui_def_but(uiBlock *block, int type, int retval, char *str, short x1, short y1, short x2, short y2, void *poin, float min, float max, float a1, float a2,  char *tip)
2310 {
2311         uiBut *but;
2312         short slen;
2313         
2314         if(type & BUTPOIN) {            /* a pointer is required */
2315                 if(poin==NULL)
2316                         return NULL;
2317         }
2318
2319         but= MEM_callocN(sizeof(uiBut), "uiBut");
2320
2321         but->type= type & BUTTYPE;
2322         but->pointype= type & BUTPOIN;
2323         but->bit= type & BIT;
2324         but->bitnr= type & 31;
2325         but->icon = 0;
2326         but->iconadd=0;
2327
2328         but->retval= retval;
2329         if( strlen(str)>=UI_MAX_NAME_STR-1 ) {
2330                 but->str= MEM_callocN( strlen(str)+2, "uiDefBut");
2331                 strcpy(but->str, str);
2332         }
2333         else {
2334                 but->str= but->strdata;
2335                 strcpy(but->str, str);
2336         }
2337         but->x1= x1; 
2338         but->y1= y1;
2339         but->x2= (x1+x2); 
2340         but->y2= (y1+y2);
2341         
2342         but->poin= poin;
2343         but->hardmin= but->softmin= min; 
2344         but->hardmax= but->softmax= max;
2345         but->a1= a1; 
2346         but->a2= a2;
2347         but->tip= tip;
2348         
2349         but->lock= block->lock;
2350         but->lockstr= block->lockstr;
2351         but->dt= block->dt;
2352
2353         but->aspect= 1.0f; //XXX block->aspect;
2354         but->block= block;              // pointer back, used for frontbuffer status, and picker
2355
2356         if((block->flag & UI_BUT_ALIGN) && ui_but_can_align(but))
2357                 but->alignnr= block->alignnr;
2358
2359         but->func= block->func;
2360         but->func_arg1= block->func_arg1;
2361         but->func_arg2= block->func_arg2;
2362
2363         but->funcN= block->funcN;
2364         if(block->func_argN)
2365                 but->func_argN= MEM_dupallocN(block->func_argN);
2366         
2367         but->pos= -1;   /* cursor invisible */
2368
2369         if(ELEM4(but->type, NUM, NUMABS, NUMSLI, HSVSLI)) {     /* add a space to name */
2370                 slen= strlen(but->str);
2371                 if(slen>0 && slen<UI_MAX_NAME_STR-2) {
2372                         if(but->str[slen-1]!=' ') {
2373                                 but->str[slen]= ' ';
2374                                 but->str[slen+1]= 0;
2375                         }
2376                 }
2377         }
2378
2379         if((block->flag & UI_BLOCK_LOOP) || ELEM7(but->type, MENU, TEX, LABEL, IDPOIN, BLOCK, BUTM, SEARCH_MENU))
2380                 but->flag |= (UI_TEXT_LEFT|UI_ICON_LEFT);
2381         else if(but->type==BUT_TOGDUAL)
2382                 but->flag |= UI_ICON_LEFT;
2383
2384         but->flag |= (block->flag & UI_BUT_ALIGN);
2385
2386         if (but->lock) {
2387                 if (but->lockstr) {
2388                         but->flag |= UI_BUT_DISABLED;
2389                 }
2390         }
2391
2392         if(ELEM8(but->type, BLOCK, BUT, LABEL, PULLDOWN, ROUNDBOX, LISTBOX, SEARCH_MENU, BUTM));
2393         else if(ELEM5(but->type, SCROLL, SEPR, LINK, INLINK, FTPREVIEW));
2394         else but->flag |= UI_BUT_UNDO;
2395
2396         BLI_addtail(&block->buttons, but);
2397         
2398         if(block->curlayout)
2399                 ui_layout_add_but(block->curlayout, but);
2400
2401         return but;
2402 }
2403
2404 uiBut *ui_def_but_rna(uiBlock *block, int type, int retval, char *str, short x1, short y1, short x2, short y2, PointerRNA *ptr, const char *propname, int index, float min, float max, float a1, float a2,  char *tip)
2405 {
2406         uiBut *but;
2407         PropertyRNA *prop;
2408         PropertyType proptype;
2409         int freestr= 0, icon= 0;
2410
2411         prop= RNA_struct_find_property(ptr, propname);
2412
2413         if(prop) {
2414                 proptype= RNA_property_type(prop);
2415
2416                 /* use rna values if parameters are not specified */
2417                 if(!str) {
2418                         if(type == MENU && proptype == PROP_ENUM) {
2419                                 EnumPropertyItem *item;
2420                                 DynStr *dynstr;
2421                                 int i, totitem, value, free;
2422
2423                                 RNA_property_enum_items(block->evil_C, ptr, prop, &item, &totitem, &free);
2424                                 value= RNA_property_enum_get(ptr, prop);
2425
2426                                 dynstr= BLI_dynstr_new();
2427                                 BLI_dynstr_appendf(dynstr, "%s%%t", RNA_property_ui_name(prop));
2428                                 for(i=0; i<totitem; i++) {
2429                                         if(!item[i].identifier[0]) {
2430                                                 if(item[i].name)
2431                                                         BLI_dynstr_appendf(dynstr, "|%s%%l", item[i].name);
2432                                                 else
2433                                                         BLI_dynstr_append(dynstr, "|%l");
2434                                         }
2435                                         else if(item[i].icon)
2436                                                 BLI_dynstr_appendf(dynstr, "|%s %%i%d %%x%d", item[i].name, item[i].icon, item[i].value);
2437                                         else
2438                                                 BLI_dynstr_appendf(dynstr, "|%s %%x%d", item[i].name, item[i].value);
2439
2440                                         if(value == item[i].value)
2441                                                 icon= item[i].icon;
2442                                 }
2443                                 str= BLI_dynstr_get_cstring(dynstr);
2444                                 BLI_dynstr_free(dynstr);
2445
2446                                 if(free)
2447                                         MEM_freeN(item);
2448
2449                                 freestr= 1;
2450                         }
2451                         else if(ELEM(type, ROW, LISTROW) && proptype == PROP_ENUM) {
2452                                 EnumPropertyItem *item;
2453                                 int i, totitem, free;
2454
2455                                 RNA_property_enum_items(block->evil_C, ptr, prop, &item, &totitem, &free);
2456                                 for(i=0; i<totitem; i++) {
2457                                         if(item[i].identifier[0] && item[i].value == (int)max) {
2458                                                 str= (char*)item[i].name;
2459                                                 icon= item[i].icon;
2460                                         }
2461                                 }
2462
2463                                 if(!str)
2464                                         str= (char*)RNA_property_ui_name(prop);
2465                                 if(free)
2466                                         MEM_freeN(item);
2467                         }
2468                         else {
2469                                 str= (char*)RNA_property_ui_name(prop);
2470                                 icon= RNA_property_ui_icon(prop);
2471                         }
2472                 }
2473
2474                 if(!tip) {
2475                         if(type == ROW && proptype == PROP_ENUM) {
2476                                 EnumPropertyItem *item;
2477                                 int i, totitem, free;
2478
2479                                 RNA_property_enum_items(block->evil_C, ptr, prop, &item, &totitem, &free);
2480
2481                                 for(i=0; i<totitem; i++) {
2482                                         if(item[i].identifier[0] && item[i].value == (int)max) {
2483                                                 if(item[i].description[0])
2484                                                         tip= (char*)item[i].description;
2485                                                 break;
2486                                         }
2487                                 }
2488
2489                                 if(free)
2490                                         MEM_freeN(item);
2491                         }
2492                 }
2493                 
2494                 if(!tip)
2495                         tip= (char*)RNA_property_ui_description(prop);
2496
2497                 if(min == max || a1 == -1 || a2 == -1) {
2498                         if(proptype == PROP_INT) {
2499                                 int hardmin, hardmax, softmin, softmax, step;
2500
2501                                 RNA_property_int_range(ptr, prop, &hardmin, &hardmax);
2502                                 RNA_property_int_ui_range(ptr, prop, &softmin, &softmax, &step);
2503
2504                                 if(!ELEM(type, ROW, LISTROW) && min == max) {
2505                                         min= hardmin;
2506                                         max= hardmax;
2507                                 }
2508                                 if(a1 == -1)
2509                                         a1= step;
2510                                 if(a2 == -1)
2511                                         a2= 0;
2512                         }
2513                         else if(proptype == PROP_FLOAT) {
2514                                 float hardmin, hardmax, softmin, softmax, step, precision;
2515
2516                                 RNA_property_float_range(ptr, prop, &hardmin, &hardmax);
2517                                 RNA_property_float_ui_range(ptr, prop, &softmin, &softmax, &step, &precision);
2518
2519                                 if(!ELEM(type, ROW, LISTROW) && min == max) {
2520                                         min= hardmin;
2521                                         max= hardmax;
2522                                 }
2523                                 if(a1 == -1)
2524                                         a1= step;
2525                                 if(a2 == -1)
2526                                         a2= precision;
2527                         }
2528                         else if(proptype == PROP_STRING) {
2529                                 min= 0;
2530                                 max= RNA_property_string_maxlength(prop);
2531                                 if(max == 0) /* interface code should ideally support unlimited length */
2532                                         max= UI_MAX_DRAW_STR; 
2533                         }
2534                 }
2535         }
2536         else
2537                 str= (char*)propname;
2538
2539         /* now create button */
2540         but= ui_def_but(block, type, retval, str, x1, y1, x2, y2, NULL, min, max, a1, a2, tip);
2541
2542         if(prop) {
2543                 but->rnapoin= *ptr;
2544                 but->rnaprop= prop;
2545
2546                 if(RNA_property_array_length(&but->rnapoin, but->rnaprop))
2547                         but->rnaindex= index;
2548                 else
2549                         but->rnaindex= 0;
2550         }
2551
2552         if(icon) {
2553                 but->icon= (BIFIconID)icon;
2554                 but->flag |= UI_HAS_ICON;
2555                 but->flag|= UI_ICON_LEFT;
2556         }
2557         
2558         if (!prop || !RNA_property_editable(&but->rnapoin, prop)) {
2559                 but->flag |= UI_BUT_DISABLED;
2560                 but->lock = 1;
2561                 but->lockstr = "";
2562         }
2563
2564         /* If this button uses units, calculate the step from this */
2565         if(ui_is_but_unit(but))
2566                 but->a1= ui_get_but_step_unit(but, ui_get_but_val(but), but->a1);
2567
2568         if(freestr)
2569                 MEM_freeN(str);
2570         
2571         return but;
2572 }
2573
2574 uiBut *ui_def_but_operator(uiBlock *block, int type, char *opname, int opcontext, char *str, short x1, short y1, short x2, short y2, char *tip)
2575 {
2576         uiBut *but;
2577         wmOperatorType *ot;
2578         
2579         ot= WM_operatortype_find(opname, 0);
2580
2581         if(!str) {
2582                 if(ot) str= ot->name;
2583                 else str= opname;
2584         }
2585         
2586         if ((!tip || tip[0]=='\0') && ot && ot->description) {
2587                 tip= ot->description;
2588         }
2589
2590         but= ui_def_but(block, type, -1, str, x1, y1, x2, y2, NULL, 0, 0, 0, 0, tip);
2591         but->optype= ot;
2592         but->opcontext= opcontext;
2593
2594         if(!ot) {
2595                 but->flag |= UI_BUT_DISABLED;
2596                 but->lock = 1;
2597                 but->lockstr = "";
2598         }
2599
2600         return but;
2601 }
2602
2603 uiBut *uiDefBut(uiBlock *block, int type, int retval, char *str, short x1, short y1, short x2, short y2, void *poin, float min, float max, float a1, float a2,  char *tip)
2604 {
2605         uiBut *but= ui_def_but(block, type, retval, str, x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2606
2607         ui_check_but(but);
2608         
2609         return but;
2610 }
2611
2612         /* if _x_ is a power of two (only one bit) return the power,
2613          * otherwise return -1. 
2614          * (1<<findBitIndex(x))==x for powers of two.
2615          */
2616 static int findBitIndex(unsigned int x) {
2617         if (!x || (x&(x-1))!=0) {       /* x&(x-1) strips lowest bit */
2618                 return -1;
2619         } else {
2620                 int idx= 0;
2621
2622                 if (x&0xFFFF0000)       idx+=16, x>>=16;
2623                 if (x&0xFF00)           idx+=8, x>>=8;
2624                 if (x&0xF0)                     idx+=4, x>>=4;
2625                 if (x&0xC)                      idx+=2, x>>=2;
2626                 if (x&0x2)                      idx+=1;
2627
2628                 return idx;
2629         }
2630 }
2631
2632 /* autocomplete helper functions */
2633 struct AutoComplete {
2634         int maxlen;
2635         char *truncate;
2636         char *startname;
2637 };
2638
2639 AutoComplete *autocomplete_begin(char *startname, int maxlen)
2640 {
2641         AutoComplete *autocpl;
2642         
2643         autocpl= MEM_callocN(sizeof(AutoComplete), "AutoComplete");
2644         autocpl->maxlen= maxlen;
2645         autocpl->truncate= MEM_callocN(sizeof(char)*maxlen, "AutoCompleteTruncate");
2646         autocpl->startname= startname;
2647
2648         return autocpl;
2649 }
2650
2651 void autocomplete_do_name(AutoComplete *autocpl, const char *name)
2652 {
2653         char *truncate= autocpl->truncate;
2654         char *startname= autocpl->startname;
2655         int a;
2656
2657         for(a=0; a<autocpl->maxlen-1; a++) {
2658                 if(startname[a]==0 || startname[a]!=name[a])
2659                         break;
2660         }
2661         /* found a match */
2662         if(startname[a]==0) {
2663                 /* first match */
2664                 if(truncate[0]==0)
2665                         BLI_strncpy(truncate, name, autocpl->maxlen);
2666                 else {
2667                         /* remove from truncate what is not in bone->name */
2668                         for(a=0; a<autocpl->maxlen-1; a++) {
2669                                 if(name[a] == 0) {
2670                                         truncate[a]= 0;
2671                                         break;
2672                                 }
2673                                 else if(truncate[a]!=name[a])
2674                                         truncate[a]= 0;
2675                         }
2676                 }
2677         }
2678 }
2679
2680 void autocomplete_end(AutoComplete *autocpl, char *autoname)
2681 {       
2682         if(autocpl->truncate[0])
2683                 BLI_strncpy(autoname, autocpl->truncate, autocpl->maxlen);
2684         else {
2685                 if (autoname != autocpl->startname) /* dont copy a string over its self */
2686                         BLI_strncpy(autoname, autocpl->startname, autocpl->maxlen);
2687         }
2688         MEM_freeN(autocpl->truncate);
2689         MEM_freeN(autocpl);
2690 }
2691
2692 /* autocomplete callback for ID buttons */
2693 static void autocomplete_id(bContext *C, char *str, void *arg_v)
2694 {
2695         int blocktype= (intptr_t)arg_v;
2696         ListBase *listb= which_libbase(CTX_data_main(C), blocktype);
2697         
2698         if(listb==NULL) return;
2699         
2700         /* search if str matches the beginning of an ID struct */
2701         if(str[0]) {
2702                 AutoComplete *autocpl= autocomplete_begin(str, 22);
2703                 ID *id;
2704                 
2705                 for(id= listb->first; id; id= id->next)
2706                         autocomplete_do_name(autocpl, id->name+2);
2707
2708                 autocomplete_end(autocpl, str);
2709         }
2710 }
2711
2712 static uiBut *uiDefButBit(uiBlock *block, int type, int bit, int retval, char *str, short x1, short y1, short x2, short y2, void *poin, float min, float max, float a1, float a2,  char *tip)
2713 {
2714         int bitIdx= findBitIndex(bit);
2715         if (bitIdx==-1) {
2716                 return NULL;
2717         } else {
2718                 return uiDefBut(block, type|BIT|bitIdx, retval, str, x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2719         }
2720 }
2721 uiBut *uiDefButF(uiBlock *block, int type, int retval, char *str, short x1, short y1, short x2, short y2, float *poin, float min, float max, float a1, float a2,  char *tip)
2722 {
2723         return uiDefBut(block, type|FLO, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2724 }
2725 uiBut *uiDefButBitF(uiBlock *block, int type, int bit, int retval, char *str, short x1, short y1, short x2, short y2, float *poin, float min, float max, float a1, float a2,  char *tip)
2726 {
2727         return uiDefButBit(block, type|FLO, bit, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2728 }
2729 uiBut *uiDefButI(uiBlock *block, int type, int retval, char *str, short x1, short y1, short x2, short y2, int *poin, float min, float max, float a1, float a2,  char *tip)
2730 {
2731         return uiDefBut(block, type|INT, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2732 }
2733 uiBut *uiDefButBitI(uiBlock *block, int type, int bit, int retval, char *str, short x1, short y1, short x2, short y2, int *poin, float min, float max, float a1, float a2,  char *tip)
2734 {
2735         return uiDefButBit(block, type|INT, bit, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2736 }
2737 uiBut *uiDefButS(uiBlock *block, int type, int retval, char *str, short x1, short y1, short x2, short y2, short *poin, float min, float max, float a1, float a2,  char *tip)
2738 {
2739         return uiDefBut(block, type|SHO, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2740 }
2741 uiBut *uiDefButBitS(uiBlock *block, int type, int bit, int retval, char *str, short x1, short y1, short x2, short y2, short *poin, float min, float max, float a1, float a2,  char *tip)
2742 {
2743         return uiDefButBit(block, type|SHO, bit, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2744 }
2745 uiBut *uiDefButC(uiBlock *block, int type, int retval, char *str, short x1, short y1, short x2, short y2, char *poin, float min, float max, float a1, float a2,  char *tip)
2746 {
2747         return uiDefBut(block, type|CHA, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2748 }
2749 uiBut *uiDefButBitC(uiBlock *block, int type, int bit, int retval, char *str, short x1, short y1, short x2, short y2, char *poin, float min, float max, float a1, float a2,  char *tip)
2750 {
2751         return uiDefButBit(block, type|CHA, bit, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2752 }
2753 uiBut *uiDefButR(uiBlock *block, int type, int retval, char *str, short x1, short y1, short x2, short y2, PointerRNA *ptr, const char *propname, int index, float min, float max, float a1, float a2,  char *tip)
2754 {
2755         uiBut *but;
2756
2757         but= ui_def_but_rna(block, type, retval, str, x1, y1, x2, y2, ptr, propname, index, min, max, a1, a2, tip);
2758         if(but)
2759                 ui_check_but(but);
2760
2761         return but;
2762 }
2763 uiBut *uiDefButO(uiBlock *block, int type, char *opname, int opcontext, char *str, short x1, short y1, short x2, short y2, char *tip)
2764 {
2765         uiBut *but;
2766
2767         but= ui_def_but_operator(block, type, opname, opcontext, str, x1, y1, x2, y2, tip);
2768         if(but)
2769                 ui_check_but(but);
2770
2771         return but;
2772 }
2773
2774 /* if a1==1.0 then a2 is an extra icon blending factor (alpha 0.0 - 1.0) */
2775 uiBut *uiDefIconBut(uiBlock *block, int type, int retval, int icon, short x1, short y1, short x2, short y2, void *poin, float min, float max, float a1, float a2,  char *tip)
2776 {
2777         uiBut *but= ui_def_but(block, type, retval, "", x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2778         
2779         but->icon= (BIFIconID) icon;
2780         but->flag|= UI_HAS_ICON;
2781
2782         ui_check_but(but);
2783         
2784         return but;
2785 }
2786 static uiBut *uiDefIconButBit(uiBlock *block, int type, int bit, int retval, int icon, short x1, short y1, short x2, short y2, void *poin, float min, float max, float a1, float a2,  char *tip)
2787 {
2788         int bitIdx= findBitIndex(bit);
2789         if (bitIdx==-1) {
2790                 return NULL;
2791         } else {
2792                 return uiDefIconBut(block, type|BIT|bitIdx, retval, icon, x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2793         }
2794 }
2795
2796 uiBut *uiDefIconButF(uiBlock *block, int type, int retval, int icon, short x1, short y1, short x2, short y2, float *poin, float min, float max, float a1, float a2,  char *tip)
2797 {
2798         return uiDefIconBut(block, type|FLO, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2799 }
2800 uiBut *uiDefIconButBitF(uiBlock *block, int type, int bit, int retval, int icon, short x1, short y1, short x2, short y2, float *poin, float min, float max, float a1, float a2,  char *tip)
2801 {
2802         return uiDefIconButBit(block, type|FLO, bit, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2803 }
2804 uiBut *uiDefIconButI(uiBlock *block, int type, int retval, int icon, short x1, short y1, short x2, short y2, int *poin, float min, float max, float a1, float a2,  char *tip)
2805 {
2806         return uiDefIconBut(block, type|INT, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2807 }
2808 uiBut *uiDefIconButBitI(uiBlock *block, int type, int bit, int retval, int icon, short x1, short y1, short x2, short y2, int *poin, float min, float max, float a1, float a2,  char *tip)
2809 {
2810         return uiDefIconButBit(block, type|INT, bit, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2811 }
2812 uiBut *uiDefIconButS(uiBlock *block, int type, int retval, int icon, short x1, short y1, short x2, short y2, short *poin, float min, float max, float a1, float a2,  char *tip)
2813 {
2814         return uiDefIconBut(block, type|SHO, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2815 }
2816 uiBut *uiDefIconButBitS(uiBlock *block, int type, int bit, int retval, int icon, short x1, short y1, short x2, short y2, short *poin, float min, float max, float a1, float a2,  char *tip)
2817 {
2818         return uiDefIconButBit(block, type|SHO, bit, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2819 }
2820 uiBut *uiDefIconButC(uiBlock *block, int type, int retval, int icon, short x1, short y1, short x2, short y2, char *poin, float min, float max, float a1, float a2,  char *tip)
2821 {
2822         return uiDefIconBut(block, type|CHA, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2823 }
2824 uiBut *uiDefIconButBitC(uiBlock *block, int type, int bit, int retval, int icon, short x1, short y1, short x2, short y2, char *poin, float min, float max, float a1, float a2,  char *tip)
2825 {
2826         return uiDefIconButBit(block, type|CHA, bit, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2827 }
2828 uiBut *uiDefIconButR(uiBlock *block, int type, int retval, int icon, short x1, short y1, short x2, short y2, PointerRNA *ptr, const char *propname, int index, float min, float max, float a1, float a2,  char *tip)
2829 {
2830         uiBut *but;
2831
2832         but= ui_def_but_rna(block, type, retval, "", x1, y1, x2, y2, ptr, propname, index, min, max, a1, a2, tip);
2833         if(but) {
2834                 if(icon) {
2835                         but->icon= (BIFIconID) icon;
2836                         but->flag|= UI_HAS_ICON;
2837                 }
2838                 ui_check_but(but);
2839         }
2840
2841         return but;
2842 }
2843 uiBut *uiDefIconButO(uiBlock *block, int type, char *opname, int opcontext, int icon, short x1, short y1, short x2, short y2, char *tip)
2844 {
2845         uiBut *but;
2846
2847         but= ui_def_but_operator(block, type, opname, opcontext, "", x1, y1, x2, y2, tip);
2848         if(but) {
2849                 but->icon= (BIFIconID) icon;
2850                 but->flag|= UI_HAS_ICON;
2851                 ui_check_but(but);
2852         }
2853
2854         return but;
2855 }
2856
2857 /* Button containing both string label and icon */
2858 uiBut *uiDefIconTextBut(uiBlock *block, int type, int retval, int icon, char *str, short x1, short y1, short x2, short y2, void *poin, float min, float max, float a1, float a2,  char *tip)
2859 {
2860         uiBut *but= ui_def_but(block, type, retval, str, x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2861
2862         but->icon= (BIFIconID) icon;
2863         but->flag|= UI_HAS_ICON;
2864
2865         but->flag|= UI_ICON_LEFT;
2866
2867         ui_check_but(but);
2868
2869         return but;
2870 }
2871 static uiBut *uiDefIconTextButBit(uiBlock *block, int type, int bit, int retval, int icon, char *str, short x1, short y1, short x2, short y2, void *poin, float min, float max, float a1, float a2,  char *tip)
2872 {
2873         int bitIdx= findBitIndex(bit);
2874         if (bitIdx==-1) {
2875                 return NULL;
2876         } else {
2877                 return uiDefIconTextBut(block, type|BIT|bitIdx, retval, icon, str, x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2878         }
2879 }
2880
2881 uiBut *uiDefIconTextButF(uiBlock *block, int type, int retval, int icon, char *str, short x1, short y1, short x2, short y2, float *poin, float min, float max, float a1, float a2,  char *tip)
2882 {
2883         return uiDefIconTextBut(block, type|FLO, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2884 }
2885 uiBut *uiDefIconTextButBitF(uiBlock *block, int type, int bit, int retval, int icon, char *str, short x1, short y1, short x2, short y2, float *poin, float min, float max, float a1, float a2,  char *tip)
2886 {
2887         return uiDefIconTextButBit(block, type|FLO, bit, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2888 }
2889 uiBut *uiDefIconTextButI(uiBlock *block, int type, int retval, int icon, char *str, short x1, short y1, short x2, short y2, int *poin, float min, float max, float a1, float a2,  char *tip)
2890 {
2891         return uiDefIconTextBut(block, type|INT, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2892 }
2893 uiBut *uiDefIconTextButBitI(uiBlock *block, int type, int bit, int retval, int icon, char *str, short x1, short y1, short x2, short y2, int *poin, float min, float max, float a1, float a2,  char *tip)
2894 {
2895         return uiDefIconTextButBit(block, type|INT, bit, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2896 }
2897 uiBut *uiDefIconTextButS(uiBlock *block, int type, int retval, int icon, char *str, short x1, short y1, short x2, short y2, short *poin, float min, float max, float a1, float a2,  char *tip)
2898 {
2899         return uiDefIconTextBut(block, type|SHO, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2900 }
2901 uiBut *uiDefIconTextButBitS(uiBlock *block, int type, int bit, int retval, int icon, char *str, short x1, short y1, short x2, short y2, short *poin, float min, float max, float a1, float a2,  char *tip)
2902 {
2903         return uiDefIconTextButBit(block, type|SHO, bit, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2904 }
2905 uiBut *uiDefIconTextButC(uiBlock *block, int type, int retval, int icon, char *str, short x1, short y1, short x2, short y2, char *poin, float min, float max, float a1, float a2,  char *tip)
2906 {
2907         return uiDefIconTextBut(block, type|CHA, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2908 }
2909 uiBut *uiDefIconTextButBitC(uiBlock *block, int type, int bit, int retval, int icon, char *str, short x1, short y1, short x2, short y2, char *poin, float min, float max, float a1, float a2,  char *tip)
2910 {
2911         return uiDefIconTextButBit(block, type|CHA, bit, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2912 }
2913 uiBut *uiDefIconTextButR(uiBlock *block, int type, int retval, int icon, char *str, short x1, short y1, short x2, short y2, PointerRNA *ptr, const char *propname, int index, float min, float max, float a1, float a2,  char *tip)
2914 {
2915         uiBut *but;
2916
2917         but= ui_def_but_rna(block, type, retval, str, x1, y1, x2, y2, ptr, propname, index, min, max, a1, a2, tip);
2918         if(but) {
2919                 if(icon) {
2920                         but->icon= (BIFIconID) icon;
2921                         but->flag|= UI_HAS_ICON;
2922                 }
2923                 but->flag|= UI_ICON_LEFT;
2924                 ui_check_but(but);
2925         }
2926
2927         return but;
2928 }
2929 uiBut *uiDefIconTextButO(uiBlock *block, int type, char *opname, int opcontext, int icon, char *str, short x1, short y1, short x2, short y2, char *tip)
2930 {
2931         uiBut *but;
2932
2933         but= ui_def_but_operator(block, type, opname, opcontext, str, x1, y1, x2, y2, tip);
2934         if(but) {
2935                 but->icon= (BIFIconID) icon;
2936                 but->flag|= UI_HAS_ICON;
2937                 but->flag|= UI_ICON_LEFT;
2938                 ui_check_but(but);
2939         }
2940
2941         return but;
2942 }
2943
2944 /* END Button containing both string label and icon */
2945
2946 void uiSetButLink(uiBut *but, void **poin, void ***ppoin, short *tot, int from, int to)
2947 {
2948         uiLink *link;
2949         
2950         link= but->link= MEM_callocN(sizeof(uiLink), "new uilink");
2951         
2952         link->poin= poin;
2953         link->ppoin= ppoin;
2954         link->totlink= tot;
2955         link->fromcode= from;
2956         link->tocode= to;
2957 }
2958
2959 /* cruft to make uiBlock and uiBut private */
2960
2961 int uiBlocksGetYMin(ListBase *lb)
2962 {
2963         uiBlock *block;
2964         int min= 0;
2965         
2966         for (block= lb->first; block; block= block->next)
2967                 if (block==lb->first || block->miny<min)
2968                         min= block->miny;
2969                         
2970         return min;
2971 }
2972
2973 void uiBlockSetDirection(uiBlock *block, int direction)
2974 {
2975         block->direction= direction;
2976 }
2977
2978 /* this call escapes if there's alignment flags */
2979 void uiBlockFlipOrder(uiBlock *block)
2980 {
2981         ListBase lb;
2982         uiBut *but, *next;
2983         float centy, miny=10000, maxy= -10000;
2984
2985         if(U.uiflag & USER_MENUFIXEDORDER)
2986                 return;
2987         else if(block->flag & UI_BLOCK_NO_FLIP)
2988                 return;
2989         
2990         for(but= block->buttons.first; but; but= but->next) {
2991                 if(but->flag & UI_BUT_ALIGN) return;
2992                 if(but->y1 < miny) miny= but->y1;
2993                 if(but->y2 > maxy) maxy= but->y2;
2994         }
2995         /* mirror trick */
2996         centy= (miny+maxy)/2.0;
2997         for(but= block->buttons.first; but; but= but->next) {
2998                 but->y1 = centy-(but->y1-centy);
2999                 but->y2 = centy-(but->y2-centy);
3000                 SWAP(float, but->y1, but->y2);
3001         }
3002         
3003         /* also flip order in block itself, for example for arrowkey */
3004         lb.first= lb.last= NULL;
3005         but= block->buttons.first;
3006         while(but) {
3007                 next= but->next;
3008                 BLI_remlink(&block->buttons, but);
3009                 BLI_addtail(&lb, but);
3010                 but= next;
3011         }
3012         block->buttons= lb;
3013 }
3014
3015
3016 void uiBlockSetFlag(uiBlock *block, int flag)
3017 {
3018         block->flag|= flag;
3019 }
3020
3021 void uiBlockClearFlag(uiBlock *block, int flag)
3022 {
3023         block->flag&= ~flag;
3024 }
3025
3026 void uiBlockSetXOfs(uiBlock *block, int xofs)
3027 {
3028         block->xofs= xofs;
3029 }
3030
3031 void uiButSetFlag(uiBut *but, int flag)
3032 {
3033         but->flag|= flag;
3034 }
3035
3036 void uiButClearFlag(uiBut *but, int flag)
3037 {
3038         but->flag&= ~flag;
3039 }
3040
3041 int uiButGetRetVal(uiBut *but)
3042 {
3043         return but->retval;
3044 }
3045
3046 void uiButSetDragID(uiBut *but, ID *id)
3047 {
3048         but->dragtype= WM_DRAG_ID;
3049         but->dragpoin= (void *)id;
3050 }
3051
3052 void uiButSetDragRNA(uiBut *but, PointerRNA *ptr)
3053 {
3054         but->dragtype= WM_DRAG_RNA;
3055         but->dragpoin= (void *)ptr;
3056 }
3057
3058 void uiButSetDragPath(uiBut *but, const char *path)
3059 {
3060         but->dragtype= WM_DRAG_PATH;
3061         but->dragpoin= (void *)path;
3062 }
3063
3064 void uiButSetDragName(uiBut *but, const char *name)
3065 {
3066         but->dragtype= WM_DRAG_NAME;
3067         but->dragpoin= (void *)name;
3068 }
3069
3070 /* value from button itself */
3071 void uiButSetDragValue(uiBut *but)
3072 {
3073         but->dragtype= WM_DRAG_VALUE;
3074 }
3075
3076 void uiButSetDragImage(uiBut *but, const char *path, int icon, struct ImBuf *imb, float scale)
3077 {
3078         but->dragtype= WM_DRAG_PATH;
3079         but->icon= icon; /* no flag UI_HAS_ICON, so icon doesnt draw in button */
3080         but->dragpoin= (void *)path;
3081         but->imb= imb;
3082         but->imb_scale= scale;