Fix [#21516] UI artifacts in array modifier
[blender.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(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         UI_ThemeClearColor(TH_BACK);
753         
754         /* back */
755         if(block->flag & UI_BLOCK_LOOP)
756                 ui_draw_menu_back(&style, block, &rect);
757         else if(block->panel)
758                 ui_draw_aligned_panel(ar, &style, block, &rect);
759
760         /* widgets */
761         for(but= block->buttons.first; but; but= but->next) {
762                 ui_but_to_pixelrect(&rect, ar, block, but);
763                 if(!(but->flag & UI_HIDDEN))
764                         ui_draw_but(C, ar, &style, but, &rect);
765         }
766         
767         /* restore matrix */
768         glMatrixMode(GL_PROJECTION);
769         glPopMatrix();
770         glMatrixMode(GL_MODELVIEW);
771         glPopMatrix();
772         
773         ui_draw_links(block);
774 }
775
776 /* ************* EVENTS ************* */
777
778 static void ui_is_but_sel(uiBut *but)
779 {
780         double value;
781         int lvalue;
782         short push=0, true=1;
783
784         value= ui_get_but_val(but);
785
786         if(ELEM3(but->type, TOGN, ICONTOGN, OPTIONN)) true= 0;
787
788         if( but->bit ) {
789                 lvalue= (int)value;
790                 if( BTST(lvalue, (but->bitnr)) ) push= true;
791                 else push= !true;
792         }
793         else {
794                 switch(but->type) {
795                 case BUT:
796                         push= 2;
797                         break;
798                 case HOTKEYEVT:
799                 case KEYEVT:
800                         push= 2;
801                         break;
802                 case TOGBUT:
803                 case TOG:
804                 case TOGR:
805                 case TOG3:
806                 case BUT_TOGDUAL:
807                 case ICONTOG:
808                 case OPTION:
809                         if(value!=but->hardmin) push= 1;
810                         break;
811                 case ICONTOGN:
812                 case TOGN:
813                 case OPTIONN:
814                         if(value==0.0) push= 1;
815                         break;
816                 case ROW:
817                 case LISTROW:
818                         if(value == but->hardmax) push= 1;
819                         break;
820                 case COL:
821                         push= 2;
822                         break;
823                 default:
824                         push= 2;
825                         break;
826                 }
827         }
828         
829         if(push==2);
830         else if(push==1) but->flag |= UI_SELECT;
831         else but->flag &= ~UI_SELECT;
832 }
833
834 /* XXX 2.50 no links supported yet */
835
836 static int uibut_contains_pt(uiBut *but, short *mval)
837 {
838         return 0;
839
840 }
841
842 uiBut *ui_get_valid_link_button(uiBlock *block, uiBut *but, short *mval)
843 {
844         uiBut *bt;
845         
846                 /* find button to link to */
847         for (bt= block->buttons.first; bt; bt= bt->next)
848                 if(bt!=but && uibut_contains_pt(bt, mval))
849                         break;
850
851         if (bt) {
852                 if (but->type==LINK && bt->type==INLINK) {
853                         if( but->link->tocode == (int)bt->hardmin ) {
854                                 return bt;
855                         }
856                 }
857                 else if(but->type==INLINK && bt->type==LINK) {
858                         if( bt->link->tocode == (int)but->hardmin ) {
859                                 return bt;
860                         }
861                 }
862         }
863
864         return NULL;
865 }
866
867
868 static uiBut *ui_find_inlink(uiBlock *block, void *poin)
869 {
870         uiBut *but;
871         
872         but= block->buttons.first;
873         while(but) {
874                 if(but->type==INLINK) {
875                         if(but->poin == poin) return but;
876                 }
877                 but= but->next;
878         }
879         return NULL;
880 }
881
882 static void ui_add_link_line(ListBase *listb, uiBut *but, uiBut *bt)
883 {
884         uiLinkLine *line;
885         
886         line= MEM_callocN(sizeof(uiLinkLine), "linkline");
887         BLI_addtail(listb, line);
888         line->from= but;
889         line->to= bt;
890 }
891
892 uiBut *uiFindInlink(uiBlock *block, void *poin)
893 {
894         return ui_find_inlink(block, poin);
895 }
896
897 void uiComposeLinks(uiBlock *block)
898 {
899         uiBut *but, *bt;
900         uiLink *link;
901         void ***ppoin;
902         int a;
903         
904         but= block->buttons.first;
905         while(but) {
906                 if(but->type==LINK) {
907                         link= but->link;
908                         
909                         /* for all pointers in the array */
910                         if(link) {
911                                 if(link->ppoin) {
912                                         ppoin= link->ppoin;
913                                         for(a=0; a < *(link->totlink); a++) {
914                                                 bt= ui_find_inlink(block, (*ppoin)[a] );
915                                                 if(bt) {
916                                                         ui_add_link_line(&link->lines, but, bt);
917                                                 }
918                                         }
919                                 }
920                                 else if(link->poin) {
921                                         bt= ui_find_inlink(block, *(link->poin) );
922                                         if(bt) {
923                                                 ui_add_link_line(&link->lines, but, bt);
924                                         }
925                                 }
926                         }
927                 }
928                 but= but->next;
929         }
930 }
931
932
933 /* ************************************************ */
934
935 void uiBlockSetButLock(uiBlock *block, int val, char *lockstr)
936 {
937         if(val) {
938                 block->lock |= val;
939                 block->lockstr= lockstr;
940         }
941 }
942
943 void uiBlockClearButLock(uiBlock *block)
944 {
945         block->lock= 0;
946         block->lockstr= NULL;
947 }
948
949 /* *************************************************************** */
950
951 void ui_delete_linkline(uiLinkLine *line, uiBut *but)
952 {
953         uiLink *link;
954         int a, b;
955         
956         BLI_remlink(&but->link->lines, line);
957
958         link= line->from->link;
959
960         /* are there more pointers allowed? */
961         if(link->ppoin) {
962                 
963                 if(*(link->totlink)==1) {
964                         *(link->totlink)= 0;
965                         MEM_freeN(*(link->ppoin));
966                         *(link->ppoin)= NULL;
967                 }
968                 else {
969                         b= 0;
970                         for(a=0; a< (*(link->totlink)); a++) {
971                                 
972                                 if( (*(link->ppoin))[a] != line->to->poin ) {
973                                         (*(link->ppoin))[b]= (*(link->ppoin))[a];
974                                         b++;
975                                 }
976                         }       
977                         (*(link->totlink))--;
978                 }
979         }
980         else {
981                 *(link->poin)= NULL;
982         }
983
984         MEM_freeN(line);
985         //REDRAW
986 }
987 /* XXX 2.50 no links supported yet */
988 #if 0
989 static void ui_delete_active_linkline(uiBlock *block)
990 {
991         uiBut *but;
992         uiLink *link;
993         uiLinkLine *line, *nline;
994         int a, b;
995         
996         but= block->buttons.first;
997         while(but) {
998                 if(but->type==LINK && but->link) {
999                         line= but->link->lines.first;
1000                         while(line) {
1001                                 
1002                                 nline= line->next;
1003                                 
1004                                 if(line->flag & UI_SELECT) {
1005                                         BLI_remlink(&but->link->lines, line);
1006
1007                                         link= line->from->link;
1008
1009                                         /* are there more pointers allowed? */
1010                                         if(link->ppoin) {
1011                                                 
1012                                                 if(*(link->totlink)==1) {
1013                                                         *(link->totlink)= 0;
1014                                                         MEM_freeN(*(link->ppoin));
1015                                                         *(link->ppoin)= NULL;
1016                                                 }
1017                                                 else {
1018                                                         b= 0;
1019                                                         for(a=0; a< (*(link->totlink)); a++) {
1020                                                                 
1021                                                                 if( (*(link->ppoin))[a] != line->to->poin ) {
1022                                                                         (*(link->ppoin))[b]= (*(link->ppoin))[a];
1023                                                                         b++;
1024                                                                 }
1025                                                         }       
1026                                                         (*(link->totlink))--;
1027                                                 }
1028                                         }
1029                                         else {
1030                                                 *(link->poin)= NULL;
1031                                         }
1032
1033                                         MEM_freeN(line);
1034                                 }
1035                                 line= nline;
1036                         }
1037                 }
1038                 but= but->next;
1039         }
1040         
1041         /* temporal! these buttons can be everywhere... */
1042         allqueue(REDRAWBUTSLOGIC, 0);
1043 }
1044
1045 static void ui_do_active_linklines(uiBlock *block, short *mval)
1046 {
1047         uiBut *but;
1048         uiLinkLine *line, *act= NULL;
1049         float mindist= 12.0, fac, v1[2], v2[2], v3[3];
1050         int foundone= 0; 
1051         
1052         if(mval) {
1053                 v1[0]= mval[0];
1054                 v1[1]= mval[1];
1055                 
1056                 /* find a line close to the mouse */
1057                 but= block->buttons.first;
1058                 while(but) {
1059                         if(but->type==LINK && but->link) {
1060                                 foundone= 1;
1061                                 line= but->link->lines.first;
1062                                 while(line) {
1063                                         v2[0]= line->from->x2;
1064                                         v2[1]= (line->from->y1+line->from->y2)/2.0;
1065                                         v3[0]= line->to->x1;
1066                                         v3[1]= (line->to->y1+line->to->y2)/2.0;
1067                                         
1068                                         fac= dist_to_line_segment_v2(v1, v2, v3);
1069                                         if(fac < mindist) {
1070                                                 mindist= fac;
1071                                                 act= line;
1072                                         }
1073                                         line= line->next;
1074                                 }
1075                         }
1076                         but= but->next;
1077                 }
1078         }
1079
1080         /* check for a 'found one' to prevent going to 'frontbuffer' mode.
1081                 this slows done gfx quite some, and at OSX the 'finish' forces a swapbuffer */
1082         if(foundone) {
1083                 glDrawBuffer(GL_FRONT);
1084                 
1085                 /* draw */
1086                 but= block->buttons.first;
1087                 while(but) {
1088                         if(but->type==LINK && but->link) {
1089                                 line= but->link->lines.first;
1090                                 while(line) {
1091                                         if(line==act) {
1092                                                 if((line->flag & UI_SELECT)==0) {
1093                                                         line->flag |= UI_SELECT;
1094                                                         ui_draw_linkline(but, line);
1095                                                 }
1096                                         }
1097                                         else if(line->flag & UI_SELECT) {
1098                                                 line->flag &= ~UI_SELECT;
1099                                                 ui_draw_linkline(but, line);
1100                                         }
1101                                         line= line->next;
1102                                 }
1103                         }
1104                         but= but->next;
1105                 }
1106                 bglFlush();
1107                 glDrawBuffer(GL_BACK);
1108         }
1109 }
1110 #endif
1111
1112 /* ******************************************************* */
1113
1114 /* XXX 2.50 no screendump supported yet */
1115
1116 #if 0
1117 /* nasty but safe way to store screendump rect */
1118 static int scr_x=0, scr_y=0, scr_sizex=0, scr_sizey=0;
1119
1120 static void ui_set_screendump_bbox(uiBlock *block)
1121 {
1122         if(block) {
1123                 scr_x= block->minx;
1124                 scr_y= block->miny;
1125                 scr_sizex= block->maxx - block->minx;
1126                 scr_sizey= block->maxy - block->miny;
1127         }
1128         else {
1129                 scr_sizex= scr_sizey= 0;
1130         }
1131 }
1132
1133 /* used for making screenshots for menus, called in screendump.c */
1134 int uiIsMenu(int *x, int *y, int *sizex, int *sizey)
1135 {
1136         if(scr_sizex!=0 && scr_sizey!=0) {
1137                 *x= scr_x;
1138                 *y= scr_y;
1139                 *sizex= scr_sizex;
1140                 *sizey= scr_sizey;
1141                 return 1;
1142         }
1143         
1144         return 0;
1145 }
1146 #endif
1147
1148 /* *********************** data get/set ***********************
1149  * this either works with the pointed to data, or can work with
1150  * an edit override pointer while dragging for example */
1151
1152 /* for buttons pointing to color for example */
1153 void ui_get_but_vectorf(uiBut *but, float *vec)
1154 {
1155         PropertyRNA *prop;
1156         int a, tot;
1157
1158         if(but->editvec) {
1159                 VECCOPY(vec, but->editvec);
1160         }
1161
1162         if(but->rnaprop) {
1163                 prop= but->rnaprop;
1164
1165                 vec[0]= vec[1]= vec[2]= 0.0f;
1166
1167                 if(RNA_property_type(prop) == PROP_FLOAT) {
1168                         tot= RNA_property_array_length(&but->rnapoin, prop);
1169                         tot= MIN2(tot, 3);
1170
1171                         for(a=0; a<tot; a++)
1172                                 vec[a]= RNA_property_float_get_index(&but->rnapoin, prop, a);
1173                 }
1174         }
1175         else if(but->pointype == CHA) {
1176                 char *cp= (char *)but->poin;
1177                 vec[0]= ((float)cp[0])/255.0;
1178                 vec[1]= ((float)cp[1])/255.0;
1179                 vec[2]= ((float)cp[2])/255.0;
1180         }
1181         else if(but->pointype == FLO) {
1182                 float *fp= (float *)but->poin;
1183                 VECCOPY(vec, fp);
1184         }
1185         else {
1186                 if (but->editvec==NULL) {
1187                         fprintf(stderr, "ui_get_but_vectorf: can't get color, should never happen\n");
1188                         vec[0]= vec[1]= vec[2]= 0.0f;
1189                 }
1190         }
1191 }
1192
1193 /* for buttons pointing to color for example */
1194 void ui_set_but_vectorf(uiBut *but, float *vec)
1195 {
1196         PropertyRNA *prop;
1197         int a, tot;
1198
1199         if(but->editvec) {
1200                 VECCOPY(but->editvec, vec);
1201         }
1202
1203         if(but->rnaprop) {
1204                 prop= but->rnaprop;
1205
1206                 if(RNA_property_type(prop) == PROP_FLOAT) {
1207                         tot= RNA_property_array_length(&but->rnapoin, prop);
1208                         tot= MIN2(tot, 3);
1209
1210                         for(a=0; a<tot; a++)
1211                                 RNA_property_float_set_index(&but->rnapoin, prop, a, vec[a]);
1212                 }
1213         }
1214         else if(but->pointype == CHA) {
1215                 char *cp= (char *)but->poin;
1216                 cp[0]= (char)(0.5 +vec[0]*255.0);
1217                 cp[1]= (char)(0.5 +vec[1]*255.0);
1218                 cp[2]= (char)(0.5 +vec[2]*255.0);
1219         }
1220         else if(but->pointype == FLO) {
1221                 float *fp= (float *)but->poin;
1222                 VECCOPY(fp, vec);
1223         }
1224 }
1225
1226 int ui_is_but_float(uiBut *but)
1227 {
1228         if(but->pointype==FLO && but->poin)
1229                 return 1;
1230         
1231         if(but->rnaprop && RNA_property_type(but->rnaprop) == PROP_FLOAT)
1232                 return 1;
1233         
1234         return 0;
1235 }
1236
1237 int ui_is_but_unit(uiBut *but)
1238 {
1239         Scene *scene= CTX_data_scene((bContext *)but->block->evil_C);
1240         int unit_type;
1241         
1242         if(but->rnaprop==NULL)
1243                 return 0;
1244         
1245         unit_type = RNA_SUBTYPE_UNIT(RNA_property_subtype(but->rnaprop));
1246         
1247         if (scene->unit.flag & USER_UNIT_ROT_RADIANS && unit_type == PROP_UNIT_ROTATION)
1248                 return 0;
1249         
1250         /* for now disable time unit conversion */      
1251         if (unit_type == PROP_UNIT_TIME)
1252                 return 0;
1253
1254         if (scene->unit.system == USER_UNIT_NONE) {
1255            if (unit_type != PROP_UNIT_ROTATION)
1256                         return 0;
1257         }
1258
1259         if(unit_type == PROP_UNIT_NONE)
1260                 return 0;
1261
1262         return 1;
1263 }
1264
1265 double ui_get_but_val(uiBut *but)
1266 {
1267         PropertyRNA *prop;
1268         double value = 0.0;
1269
1270         if(but->editval) { return *(but->editval); }
1271         if(but->poin==NULL && but->rnapoin.data==NULL) return 0.0;
1272
1273         if(but->rnaprop) {
1274                 prop= but->rnaprop;
1275
1276                 switch(RNA_property_type(prop)) {
1277                         case PROP_BOOLEAN:
1278                                 if(RNA_property_array_length(&but->rnapoin, prop))
1279                                         value= RNA_property_boolean_get_index(&but->rnapoin, prop, but->rnaindex);
1280                                 else
1281                                         value= RNA_property_boolean_get(&but->rnapoin, prop);
1282                                 break;
1283                         case PROP_INT:
1284                                 if(RNA_property_array_length(&but->rnapoin, prop))
1285                                         value= RNA_property_int_get_index(&but->rnapoin, prop, but->rnaindex);
1286                                 else
1287                                         value= RNA_property_int_get(&but->rnapoin, prop);
1288                                 break;
1289                         case PROP_FLOAT:
1290                                 if(RNA_property_array_length(&but->rnapoin, prop))
1291                                         value= RNA_property_float_get_index(&but->rnapoin, prop, but->rnaindex);
1292                                 else
1293                                         value= RNA_property_float_get(&but->rnapoin, prop);
1294                                 break;
1295                         case PROP_ENUM:
1296                                 value= RNA_property_enum_get(&but->rnapoin, prop);
1297                                 break;
1298                         default:
1299                                 value= 0.0;
1300                                 break;
1301                 }
1302         }
1303         else if(but->type== HSVSLI) {
1304                 float h, s, v, *fp;
1305                 
1306                 fp= (but->editvec)? but->editvec: (float *)but->poin;
1307                 rgb_to_hsv(fp[0], fp[1], fp[2], &h, &s, &v);
1308
1309                 switch(but->str[0]) {
1310                         case 'H': value= h; break;
1311                         case 'S': value= s; break;
1312                         case 'V': value= v; break;
1313                 }
1314         } 
1315         else if( but->pointype == CHA ) {
1316                 value= *(char *)but->poin;
1317         }
1318         else if( but->pointype == SHO ) {
1319                 value= *(short *)but->poin;
1320         } 
1321         else if( but->pointype == INT ) {
1322                 value= *(int *)but->poin;
1323         } 
1324         else if( but->pointype == FLO ) {
1325                 value= *(float *)but->poin;
1326         }
1327     
1328         return value;
1329 }
1330
1331 void ui_set_but_val(uiBut *but, double value)
1332 {
1333         PropertyRNA *prop;
1334
1335         /* value is a hsv value: convert to rgb */
1336         if(but->rnaprop) {
1337                 prop= but->rnaprop;
1338
1339                 if(RNA_property_editable(&but->rnapoin, prop)) {
1340                         switch(RNA_property_type(prop)) {
1341                                 case PROP_BOOLEAN:
1342                                         if(RNA_property_array_length(&but->rnapoin, prop))
1343                                                 RNA_property_boolean_set_index(&but->rnapoin, prop, but->rnaindex, value);
1344                                         else
1345                                                 RNA_property_boolean_set(&but->rnapoin, prop, value);
1346                                         break;
1347                                 case PROP_INT:
1348                                         if(RNA_property_array_length(&but->rnapoin, prop))
1349                                                 RNA_property_int_set_index(&but->rnapoin, prop, but->rnaindex, value);
1350                                         else
1351                                                 RNA_property_int_set(&but->rnapoin, prop, value);
1352                                         break;
1353                                 case PROP_FLOAT:
1354                                         if(RNA_property_array_length(&but->rnapoin, prop))
1355                                                 RNA_property_float_set_index(&but->rnapoin, prop, but->rnaindex, value);
1356                                         else
1357                                                 RNA_property_float_set(&but->rnapoin, prop, value);
1358                                         break;
1359                                 case PROP_ENUM:
1360                                         RNA_property_enum_set(&but->rnapoin, prop, value);
1361                                         break;
1362                                 default:
1363                                         break;
1364                         }
1365                 }
1366         }
1367         else if(but->pointype==0);
1368         else if(but->type==HSVSLI ) {
1369                 float h, s, v, *fp;
1370                 
1371                 fp= (but->editvec)? but->editvec: (float *)but->poin;
1372                 rgb_to_hsv(fp[0], fp[1], fp[2], &h, &s, &v);
1373                 
1374                 switch(but->str[0]) {
1375                 case 'H': h= value; break;
1376                 case 'S': s= value; break;
1377                 case 'V': v= value; break;
1378                 }
1379                 
1380                 hsv_to_rgb(h, s, v, fp, fp+1, fp+2);
1381                 
1382         }
1383         else {
1384                 /* first do rounding */
1385                 if(but->pointype==CHA)
1386                         value= (char)floor(value+0.5);
1387                 else if(but->pointype==SHO ) {
1388                         /* gcc 3.2.1 seems to have problems 
1389                          * casting a double like 32772.0 to
1390                          * a short so we cast to an int, then 
1391                          to a short */
1392                         int gcckludge;
1393                         gcckludge = (int) floor(value+0.5);
1394                         value= (short)gcckludge;
1395                 }
1396                 else if(but->pointype==INT )
1397                         value= (int)floor(value+0.5);
1398                 else if(but->pointype==FLO ) {
1399                         float fval= (float)value;
1400                         if(fval>= -0.00001f && fval<= 0.00001f) fval= 0.0f;     /* prevent negative zero */
1401                         value= fval;
1402                 }
1403                 
1404                 /* then set value with possible edit override */
1405                 if(but->editval)
1406                         *but->editval= value;
1407                 else if(but->pointype==CHA)
1408                         *((char *)but->poin)= (char)value;
1409                 else if(but->pointype==SHO)
1410                         *((short *)but->poin)= (short)value;
1411                 else if(but->pointype==INT)
1412                         *((int *)but->poin)= (int)value;
1413                 else if(but->pointype==FLO)
1414                         *((float *)but->poin)= (float)value;
1415         }
1416
1417         /* update select flag */
1418         ui_is_but_sel(but);
1419 }
1420
1421 int ui_get_but_string_max_length(uiBut *but)
1422 {
1423         if(ELEM(but->type, TEX, SEARCH_MENU))
1424                 return but->hardmax;
1425         else if(but->type == IDPOIN)
1426                 return sizeof(((ID*)NULL)->name)-2;
1427         else
1428                 return UI_MAX_DRAW_STR;
1429 }
1430
1431 static double ui_get_but_scale_unit(uiBut *but, double value)
1432 {
1433         Scene *scene= CTX_data_scene((bContext *)but->block->evil_C);
1434         int subtype= RNA_SUBTYPE_UNIT(RNA_property_subtype(but->rnaprop));
1435
1436         if(subtype == PROP_UNIT_LENGTH) {
1437                 return value * scene->unit.scale_length;
1438         }
1439         else if(subtype == PROP_UNIT_AREA) {
1440                 return value * pow(scene->unit.scale_length, 2);
1441         }
1442         else if(subtype == PROP_UNIT_VOLUME) {
1443                 return value * pow(scene->unit.scale_length, 3);
1444         }
1445         else if(subtype == PROP_UNIT_TIME) { /* WARNING - using evil_C :| */
1446                 return FRA2TIME(value);
1447         }
1448         else {
1449                 return value;
1450         }
1451 }
1452
1453 static void ui_get_but_string_unit(uiBut *but, char *str, int len_max, double value, int pad)
1454 {
1455         Scene *scene= CTX_data_scene((bContext *)but->block->evil_C);
1456         int do_split= scene->unit.flag & USER_UNIT_OPT_SPLIT;
1457         int unit_type=  RNA_SUBTYPE_UNIT_VALUE(RNA_property_subtype(but->rnaprop));
1458         int precision= but->a2;
1459
1460         if(scene->unit.scale_length<0.0001) scene->unit.scale_length= 1.0; // XXX do_versions
1461
1462         /* Sanity checks */
1463         if(precision>4)         precision= 4;
1464         else if(precision==0)   precision= 2;
1465
1466         bUnit_AsString(str, len_max, ui_get_but_scale_unit(but, value), precision, scene->unit.system, unit_type, do_split, pad);
1467 }
1468
1469 static float ui_get_but_step_unit(uiBut *but, double value, float step_default)
1470 {
1471         Scene *scene= CTX_data_scene((bContext *)but->block->evil_C);
1472         int unit_type=  RNA_SUBTYPE_UNIT_VALUE(RNA_property_subtype(but->rnaprop));
1473         float step;
1474
1475         step = bUnit_ClosestScalar(ui_get_but_scale_unit(but, value), scene->unit.system, unit_type);
1476
1477         if(step > 0.0) { /* -1 is an error value */
1478                 return (step/ui_get_but_scale_unit(but, 1.0))*100;
1479         }
1480         else {
1481                 return step_default;
1482         }
1483 }
1484
1485
1486 void ui_get_but_string(uiBut *but, char *str, int maxlen)
1487 {
1488         if(but->rnaprop && ELEM3(but->type, TEX, IDPOIN, SEARCH_MENU)) {
1489                 PropertyType type;
1490                 char *buf= NULL;
1491
1492                 type= RNA_property_type(but->rnaprop);
1493
1494                 if(type == PROP_STRING) {
1495                         /* RNA string */
1496                         buf= RNA_property_string_get_alloc(&but->rnapoin, but->rnaprop, str, maxlen);
1497                 }
1498                 else if(type == PROP_POINTER) {
1499                         /* RNA pointer */
1500                         PointerRNA ptr= RNA_property_pointer_get(&but->rnapoin, but->rnaprop);
1501                         buf= RNA_struct_name_get_alloc(&ptr, str, maxlen);
1502                 }
1503
1504                 if(!buf) {
1505                         str[0] = '\0';
1506                 }
1507                 else if(buf && buf != str) {
1508                         /* string was too long, we have to truncate */
1509                         BLI_strncpy(str, buf, maxlen);
1510                         MEM_freeN(buf);
1511                 }
1512         }
1513         else if(but->type == IDPOIN) {
1514                 /* ID pointer */
1515                 if(but->idpoin_idpp) { /* Can be NULL for ID properties by python */
1516                         ID *id= *(but->idpoin_idpp);
1517                         if(id) {
1518                                 BLI_strncpy(str, id->name+2, maxlen);
1519                                 return;
1520                         }
1521                 }
1522                 str[0] = '\0';
1523                 return;
1524         }
1525         else if(but->type == TEX) {
1526                 /* string */
1527                 BLI_strncpy(str, but->poin, maxlen);
1528                 return;
1529         }
1530         else if(but->type == SEARCH_MENU) {
1531                 /* string */
1532                 BLI_strncpy(str, but->poin, maxlen);
1533                 return;
1534         }
1535         else if(ui_but_anim_expression_get(but, str, maxlen))
1536                 ; /* driver expression */
1537         else {
1538                 /* number editing */
1539                 double value;
1540
1541                 value= ui_get_but_val(but);
1542
1543                 if(ui_is_but_float(but)) {
1544                         if(ui_is_but_unit(but)) {
1545                                 ui_get_but_string_unit(but, str, maxlen, value, 0);
1546                         }
1547                         else if(but->a2) { /* amount of digits defined */
1548                                 if(but->a2==1) BLI_snprintf(str, maxlen, "%.1f", value);
1549                                 else if(but->a2==2) BLI_snprintf(str, maxlen, "%.2f", value);
1550                                 else if(but->a2==3) BLI_snprintf(str, maxlen, "%.3f", value);
1551                                 else BLI_snprintf(str, maxlen, "%.4f", value);
1552                         }
1553                         else
1554                                 BLI_snprintf(str, maxlen, "%.3f", value);
1555                 }
1556                 else
1557                         BLI_snprintf(str, maxlen, "%d", (int)value);
1558         }
1559 }
1560
1561 int ui_set_but_string(bContext *C, uiBut *but, const char *str)
1562 {
1563         if(but->rnaprop && ELEM3(but->type, TEX, IDPOIN, SEARCH_MENU)) {
1564                 if(RNA_property_editable(&but->rnapoin, but->rnaprop)) {
1565                         PropertyType type;
1566
1567                         type= RNA_property_type(but->rnaprop);
1568
1569                         if(type == PROP_STRING) {
1570                                 /* RNA string */
1571                                 RNA_property_string_set(&but->rnapoin, but->rnaprop, str);
1572                                 return 1;
1573                         }
1574                         else if(type == PROP_POINTER) {
1575                                 /* RNA pointer */
1576                                 PointerRNA ptr, rptr;
1577                                 PropertyRNA *prop;
1578
1579                                 if(str == NULL || str[0] == '\0') {
1580                                         RNA_property_pointer_set(&but->rnapoin, but->rnaprop, PointerRNA_NULL);
1581                                         return 1;
1582                                 }
1583                                 else {
1584                                         ptr= but->rnasearchpoin;
1585                                         prop= but->rnasearchprop;
1586                                         
1587                                         if(prop && RNA_property_collection_lookup_string(&ptr, prop, str, &rptr))
1588                                                 RNA_property_pointer_set(&but->rnapoin, but->rnaprop, rptr);
1589
1590                                         return 1;
1591                                 }
1592
1593                                 return 0;
1594                         }
1595                 }
1596         }
1597         else if(but->type == IDPOIN) {
1598                 /* ID pointer */
1599                 but->idpoin_func(C, (char*)str, but->idpoin_idpp);
1600                 return 1;
1601         }
1602         else if(but->type == TEX) {
1603                 /* string */
1604                 BLI_strncpy(but->poin, str, but->hardmax);
1605                 return 1;
1606         }
1607         else if(but->type == SEARCH_MENU) {
1608                 /* string */
1609                 BLI_strncpy(but->poin, str, but->hardmax);
1610                 return 1;
1611         }
1612         else if(ui_but_anim_expression_set(but, str)) {
1613                 /* driver expression */
1614                 return 1;
1615         }
1616         else {
1617                 /* number editing */
1618                 double value;
1619
1620 #ifndef DISABLE_PYTHON
1621                 {
1622                         char str_unit_convert[256];
1623                         int unit_type;
1624                         Scene *scene= CTX_data_scene((bContext *)but->block->evil_C);
1625
1626                         if(but->rnaprop)
1627                                 unit_type= RNA_SUBTYPE_UNIT_VALUE(RNA_property_subtype(but->rnaprop));
1628                         else
1629                                 unit_type= 0;
1630
1631                         BLI_strncpy(str_unit_convert, str, sizeof(str_unit_convert));
1632
1633                         if(ui_is_but_unit(but)) {
1634                                 /* ugly, use the draw string to get the value, this could cause problems if it includes some text which resolves to a unit */
1635                                 bUnit_ReplaceString(str_unit_convert, sizeof(str_unit_convert), but->drawstr, ui_get_but_scale_unit(but, 1.0), scene->unit.system, unit_type);
1636                         }
1637
1638                         if(BPY_button_eval(C, str_unit_convert, &value)) {
1639                                 value = ui_get_but_val(but); /* use its original value */
1640
1641                                 if(str[0])
1642                                         return 0;
1643                         }
1644                 }
1645 #else
1646                 value= atof(str);
1647 #endif
1648
1649                 if(!ui_is_but_float(but)) value= (int)value;
1650                 if(but->type==NUMABS) value= fabs(value);
1651
1652                 /* not that we use hard limits here */
1653                 if(value<but->hardmin) value= but->hardmin;
1654                 if(value>but->hardmax) value= but->hardmax;
1655
1656                 ui_set_but_val(but, value);
1657                 return 1;
1658         }
1659
1660         return 0;
1661 }
1662
1663 void ui_set_but_default(bContext *C, uiBut *but)
1664 {
1665         /* if there is a valid property that is editable... */
1666         if (but->rnapoin.data && but->rnaprop && RNA_property_editable(&but->rnapoin, but->rnaprop)) {
1667                 if(RNA_property_reset(&but->rnapoin, but->rnaprop, -1)) {
1668                         /* perform updates required for this property */
1669                         RNA_property_update(C, &but->rnapoin, but->rnaprop);
1670                 }
1671         }
1672 }
1673
1674 static double soft_range_round_up(double value, double max)
1675 {
1676         /* round up to .., 0.1, 0.2, 0.5, 1, 2, 5, 10, 20, 50, .. */
1677         double newmax= pow(10.0, ceil(log(value)/log(10.0)));
1678
1679         if(newmax*0.2 >= max && newmax*0.2 >= value)
1680                 return newmax*0.2;
1681         else if(newmax*0.5 >= max && newmax*0.5 >= value)
1682                 return newmax*0.5;
1683         else
1684                 return newmax;
1685 }
1686
1687 static double soft_range_round_down(double value, double max)
1688 {
1689         /* round down to .., 0.1, 0.2, 0.5, 1, 2, 5, 10, 20, 50, .. */
1690         double newmax= pow(10.0, floor(log(value)/log(10.0)));
1691
1692         if(newmax*5.0 <= max && newmax*5.0 <= value)
1693                 return newmax*5.0;
1694         else if(newmax*2.0 <= max && newmax*2.0 <= value)
1695                 return newmax*2.0;
1696         else
1697                 return newmax;
1698 }
1699
1700 void ui_set_but_soft_range(uiBut *but, double value)
1701 {
1702         PropertyType type;
1703         double softmin, softmax, step, precision;
1704         
1705         if(but->rnaprop) {
1706                 type= RNA_property_type(but->rnaprop);
1707
1708                 /* clamp button range to something reasonable in case
1709                  * we get -inf/inf from RNA properties */
1710                 if(type == PROP_INT) {
1711                         int imin, imax, istep;
1712
1713                         RNA_property_int_ui_range(&but->rnapoin, but->rnaprop, &imin, &imax, &istep);
1714                         softmin= (imin == INT_MIN)? -1e4: imin;
1715                         softmax= (imin == INT_MAX)? 1e4: imax;
1716                         step= istep;
1717                         precision= 1;
1718                 }
1719                 else if(type == PROP_FLOAT) {
1720                         float fmin, fmax, fstep, fprecision;
1721
1722                         RNA_property_float_ui_range(&but->rnapoin, but->rnaprop, &fmin, &fmax, &fstep, &fprecision);
1723                         softmin= (fmin == -FLT_MAX)? -1e4: fmin;
1724                         softmax= (fmax == FLT_MAX)? 1e4: fmax;
1725                         step= fstep;
1726                         precision= fprecision;
1727                 }
1728                 else
1729                         return;
1730
1731                 /* if the value goes out of the soft/max range, adapt the range */
1732                 if(value+1e-10 < softmin) {
1733                         if(value < 0.0)
1734                                 softmin= -soft_range_round_up(-value, -softmin);
1735                         else
1736                                 softmin= soft_range_round_down(value, softmin);
1737
1738                         if(softmin < but->hardmin)
1739                                 softmin= but->hardmin;
1740                 }
1741                 else if(value-1e-10 > softmax) {
1742                         if(value < 0.0)
1743                                 softmax= -soft_range_round_down(-value, -softmax);
1744                         else
1745                                 softmax= soft_range_round_up(value, softmax);
1746
1747                         if(softmax > but->hardmax)
1748                                 softmax= but->hardmax;
1749                 }
1750
1751                 but->softmin= softmin;
1752                 but->softmax= softmax;
1753         }
1754 }
1755
1756 /* ******************* Free ********************/
1757
1758 static void ui_free_link(uiLink *link)
1759 {
1760         if(link) {      
1761                 BLI_freelistN(&link->lines);
1762                 MEM_freeN(link);
1763         }
1764 }
1765
1766 /* can be called with C==NULL */
1767 static void ui_free_but(const bContext *C, uiBut *but)
1768 {
1769         if(but->opptr) {
1770                 WM_operator_properties_free(but->opptr);
1771                 MEM_freeN(but->opptr);
1772         }
1773         if(but->func_argN) MEM_freeN(but->func_argN);
1774         if(but->active) {
1775                 /* XXX solve later, buttons should be free-able without context? */
1776                 if(C) 
1777                         ui_button_active_cancel(C, but);
1778                 else
1779                         if(but->active) 
1780                                 MEM_freeN(but->active);
1781         }
1782         if(but->str && but->str != but->strdata) MEM_freeN(but->str);
1783         ui_free_link(but->link);
1784
1785         MEM_freeN(but);
1786 }
1787
1788 /* can be called with C==NULL */
1789 void uiFreeBlock(const bContext *C, uiBlock *block)
1790 {
1791         uiBut *but;
1792
1793         while( (but= block->buttons.first) ) {
1794                 BLI_remlink(&block->buttons, but);      
1795                 ui_free_but(C, but);
1796         }
1797
1798         if(block->func_argN)
1799                 MEM_freeN(block->func_argN);
1800
1801         CTX_store_free_list(&block->contexts);
1802
1803         BLI_freelistN(&block->saferct);
1804         
1805         MEM_freeN(block);
1806 }
1807
1808 /* can be called with C==NULL */
1809 void uiFreeBlocks(const bContext *C, ListBase *lb)
1810 {
1811         uiBlock *block;
1812         
1813         while( (block= lb->first) ) {
1814                 BLI_remlink(lb, block);
1815                 uiFreeBlock(C, block);
1816         }
1817 }
1818
1819 void uiFreeInactiveBlocks(const bContext *C, ListBase *lb)
1820 {
1821         uiBlock *block, *nextblock;
1822
1823         for(block=lb->first; block; block=nextblock) {
1824                 nextblock= block->next;
1825         
1826                 if(!block->handle) {
1827                         if(!block->active) {
1828                                 BLI_remlink(lb, block);
1829                                 uiFreeBlock(C, block);
1830                         }
1831                         else
1832                                 block->active= 0;
1833                 }
1834         }
1835 }
1836
1837 void uiBlockSetRegion(uiBlock *block, ARegion *region)
1838 {
1839         ListBase *lb;
1840         uiBlock *oldblock= NULL;
1841
1842         lb= &region->uiblocks;
1843         
1844         /* each listbase only has one block with this name, free block
1845          * if is already there so it can be rebuilt from scratch */
1846         if(lb) {
1847                 for (oldblock= lb->first; oldblock; oldblock= oldblock->next)
1848                         if (BLI_streq(oldblock->name, block->name))
1849                                 break;
1850
1851                 if (oldblock) {
1852                         oldblock->active= 0;
1853                         oldblock->panel= NULL;
1854                 }
1855         }
1856
1857         block->oldblock= oldblock;
1858
1859         /* at the beginning of the list! for dynamical menus/blocks */
1860         if(lb)
1861                 BLI_addhead(lb, block);
1862 }
1863
1864 uiBlock *uiBeginBlock(const bContext *C, ARegion *region, const char *name, short dt)
1865 {
1866         uiBlock *block;
1867         wmWindow *window;
1868         Scene *scn;
1869         int getsizex, getsizey;
1870
1871         window= CTX_wm_window(C);
1872         scn = CTX_data_scene(C);
1873
1874         block= MEM_callocN(sizeof(uiBlock), "uiBlock");
1875         block->active= 1;
1876         block->dt= dt;
1877         block->evil_C= (void*)C; // XXX
1878         if (scn) block->color_profile= (scn->r.color_mgt_flag & R_COLOR_MANAGEMENT);
1879         BLI_strncpy(block->name, name, sizeof(block->name));
1880
1881         if(region)
1882                 uiBlockSetRegion(block, region);
1883
1884         /* window matrix and aspect */
1885         if(region && region->swinid) {
1886                 wm_subwindow_getmatrix(window, region->swinid, block->winmat);
1887                 wm_subwindow_getsize(window, region->swinid, &getsizex, &getsizey);
1888
1889                 /* TODO - investigate why block->winmat[0][0] is negative
1890                  * in the image view when viewRedrawForce is called */
1891                 block->aspect= 2.0/fabs( (getsizex)*block->winmat[0][0]);
1892         }
1893         else {
1894                 /* no subwindow created yet, for menus for example, so we
1895                  * use the main window instead, since buttons are created
1896                  * there anyway */
1897                 wm_subwindow_getmatrix(window, window->screen->mainwin, block->winmat);
1898                 wm_subwindow_getsize(window, window->screen->mainwin, &getsizex, &getsizey);
1899
1900                 block->aspect= 2.0/fabs(getsizex*block->winmat[0][0]);
1901                 block->auto_open= 2;
1902                 block->flag |= UI_BLOCK_LOOP; /* tag as menu */
1903         }
1904
1905         return block;
1906 }
1907
1908 uiBlock *uiGetBlock(char *name, ARegion *ar)
1909 {
1910         uiBlock *block= ar->uiblocks.first;
1911         
1912         while(block) {
1913                 if( strcmp(name, block->name)==0 ) return block;
1914                 block= block->next;
1915         }
1916         
1917         return NULL;
1918 }
1919
1920 void uiBlockSetEmboss(uiBlock *block, short dt)
1921 {
1922         block->dt= dt;
1923 }
1924
1925 void ui_check_but(uiBut *but)
1926 {
1927         /* if something changed in the button */
1928         double value;
1929         float okwidth;
1930 //      int transopts= ui_translate_buttons();
1931         
1932         ui_is_but_sel(but);
1933         
1934 //      if(but->type==TEX || but->type==IDPOIN) transopts= 0;
1935
1936         /* only update soft range while not editing */
1937         if(but->rnaprop && !(but->editval || but->editstr || but->editvec))
1938                 ui_set_but_soft_range(but, ui_get_but_val(but));
1939
1940         /* test for min and max, icon sliders, etc */
1941         switch( but->type ) {
1942                 case NUM:
1943                 case SLI:
1944                 case SCROLL:
1945                 case NUMSLI:
1946                 case HSVSLI:
1947                         value= ui_get_but_val(but);
1948                         if(value < but->hardmin) ui_set_but_val(but, but->hardmin);
1949                         else if(value > but->hardmax) ui_set_but_val(but, but->hardmax);
1950                         break;
1951                         
1952                 case NUMABS:
1953                         value= fabs( ui_get_but_val(but) );
1954                         if(value < but->hardmin) ui_set_but_val(but, but->hardmin);
1955                         else if(value > but->hardmax) ui_set_but_val(but, but->hardmax);
1956                         break;
1957                         
1958                 case ICONTOG: 
1959                 case ICONTOGN:
1960                         if(!but->rnaprop || (RNA_property_flag(but->rnaprop) & PROP_ICONS_CONSECUTIVE)) {
1961                                 if(but->flag & UI_SELECT) but->iconadd= 1;
1962                                 else but->iconadd= 0;
1963                         }
1964                         break;
1965                         
1966                 case ICONROW:
1967                         if(!but->rnaprop || (RNA_property_flag(but->rnaprop) & PROP_ICONS_CONSECUTIVE)) {
1968                                 value= ui_get_but_val(but);
1969                                 but->iconadd= (int)value- (int)(but->hardmin);
1970                         }
1971                         break;
1972                         
1973                 case ICONTEXTROW:
1974                         if(!but->rnaprop || (RNA_property_flag(but->rnaprop) & PROP_ICONS_CONSECUTIVE)) {
1975                                 value= ui_get_but_val(but);
1976                                 but->iconadd= (int)value- (int)(but->hardmin);
1977                         }
1978                         break;
1979         }
1980         
1981         
1982         /* safety is 4 to enable small number buttons (like 'users') */
1983         okwidth= -4 + (but->x2 - but->x1); 
1984         
1985         /* name: */
1986         switch( but->type ) {
1987         
1988         case MENU:
1989         case ICONTEXTROW:
1990                 
1991                 if(but->x2 - but->x1 > 24) {
1992                         value= ui_get_but_val(but);
1993                         ui_set_name_menu(but, (int)value);
1994                 }
1995                 break;
1996         
1997         case NUM:
1998         case NUMSLI:
1999         case HSVSLI:
2000         case NUMABS:
2001
2002                 value= ui_get_but_val(but);
2003
2004                 if(ui_is_but_float(but)) {
2005                         if(value == FLT_MAX) sprintf(but->drawstr, "%sinf", but->str);
2006                         else if(value == -FLT_MAX) sprintf(but->drawstr, "%s-inf", but->str);
2007                         /* support length type buttons */
2008                         else if(ui_is_but_unit(but)) {
2009                                 char new_str[sizeof(but->drawstr)];
2010                                 ui_get_but_string_unit(but, new_str, sizeof(new_str), value, TRUE);
2011                                 BLI_snprintf(but->drawstr, sizeof(but->drawstr), "%s%s", but->str, new_str);
2012                         }
2013                         else if(but->a2) { /* amount of digits defined */
2014                                 if(but->a2==1) sprintf(but->drawstr, "%s%.1f", but->str, value);
2015                                 else if(but->a2==2) sprintf(but->drawstr, "%s%.2f", but->str, value);
2016                                 else if(but->a2==3) sprintf(but->drawstr, "%s%.3f", but->str, value);
2017                                 else sprintf(but->drawstr, "%s%.4f", but->str, value);
2018                         }
2019                         else {
2020                                 if(but->hardmax<10.001) sprintf(but->drawstr, "%s%.3f", but->str, value);
2021                                 else sprintf(but->drawstr, "%s%.2f", but->str, value);
2022                         }
2023                 }
2024                 else {
2025                         sprintf(but->drawstr, "%s%d", but->str, (int)value);
2026                 }
2027                         
2028                 if(but->rnaprop) {
2029                         PropertySubType pstype = RNA_property_subtype(but->rnaprop);
2030                         
2031                         if (pstype == PROP_PERCENTAGE)
2032                                 strcat(but->drawstr, "%");
2033                 }
2034                 break;
2035
2036         case LABEL:
2037                 if(ui_is_but_float(but)) {
2038                         value= ui_get_but_val(but);
2039                         if(but->a2) { /* amount of digits defined */
2040                                 if(but->a2==1) sprintf(but->drawstr, "%s%.1f", but->str, value);
2041                                 else if(but->a2==2) sprintf(but->drawstr, "%s%.2f", but->str, value);
2042                                 else if(but->a2==3) sprintf(but->drawstr, "%s%.3f", but->str, value);
2043                                 else sprintf(but->drawstr, "%s%.4f", but->str, value);
2044                         }
2045                         else {
2046                                 sprintf(but->drawstr, "%s%.2f", but->str, value);
2047                         }
2048                 }
2049                 else strncpy(but->drawstr, but->str, UI_MAX_DRAW_STR);
2050                 
2051                 break;
2052
2053         case IDPOIN:
2054         case TEX:
2055         case SEARCH_MENU:
2056                 if(!but->editstr) {
2057                         char str[UI_MAX_DRAW_STR];
2058
2059                         ui_get_but_string(but, str, UI_MAX_DRAW_STR-strlen(but->str));
2060
2061                         strcpy(but->drawstr, but->str);
2062                         strcat(but->drawstr, str);
2063                 }
2064                 break;
2065         
2066         case KEYEVT:
2067                 strncpy(but->drawstr, but->str, UI_MAX_DRAW_STR);
2068                 if (but->flag & UI_SELECT) {
2069                         strcat(but->drawstr, "Press a key");
2070                 } else {
2071                         strcat(but->drawstr, WM_key_event_string((short) ui_get_but_val(but)));
2072                 }
2073                 break;
2074                 
2075         case HOTKEYEVT:
2076                 if (but->flag & UI_SELECT) {
2077                         but->drawstr[0]= '\0';
2078                         
2079                         if(but->modifier_key) {
2080                                 char *str= but->drawstr;
2081                                 
2082                                 if(but->modifier_key & KM_SHIFT)
2083                                         str= strcat(str, "Shift ");
2084                                 if(but->modifier_key & KM_CTRL)
2085                                         str= strcat(str, "Ctrl ");
2086                                 if(but->modifier_key & KM_ALT)
2087                                         str= strcat(str, "Alt ");
2088                                 if(but->modifier_key & KM_OSKEY)
2089                                         str= strcat(str, "Cmd ");
2090                         }
2091                         else
2092                                 strcat(but->drawstr, "Press a key  ");
2093                 }
2094                 else
2095                         strncpy(but->drawstr, but->str, UI_MAX_DRAW_STR);
2096
2097                 break;
2098                 
2099         case BUT_TOGDUAL:
2100                 /* trying to get the dual-icon to left of text... not very nice */
2101                 if(but->str[0]) {
2102                         strncpy(but->drawstr, "  ", UI_MAX_DRAW_STR);
2103                         strncpy(but->drawstr+2, but->str, UI_MAX_DRAW_STR-2);
2104                 }
2105                 break;
2106
2107         case HSVCUBE:
2108         case HSVCIRCLE:
2109                 {
2110                         float rgb[3];
2111                         ui_get_but_vectorf(but, rgb);
2112                         rgb_to_hsv(rgb[0], rgb[1], rgb[2], but->hsv, but->hsv+1, but->hsv+2);
2113                 }
2114                 break;
2115         default:
2116                 strncpy(but->drawstr, but->str, UI_MAX_DRAW_STR);
2117                 
2118         }
2119
2120         /* if we are doing text editing, this will override the drawstr */
2121         if(but->editstr)
2122                 strncpy(but->drawstr, but->editstr, UI_MAX_DRAW_STR);
2123         
2124         /* text clipping moved to widget drawing code itself */
2125 }
2126
2127
2128 void uiBlockBeginAlign(uiBlock *block)
2129 {
2130         /* if other align was active, end it */
2131         if(block->flag & UI_BUT_ALIGN) uiBlockEndAlign(block);
2132
2133         block->flag |= UI_BUT_ALIGN_DOWN;       
2134         block->alignnr++;
2135
2136         /* buttons declared after this call will get this align nr */ // XXX flag?
2137 }
2138
2139 static int buts_are_horiz(uiBut *but1, uiBut *but2)
2140 {
2141         float dx, dy;
2142         
2143         dx= fabs( but1->x2 - but2->x1);
2144         dy= fabs( but1->y1 - but2->y2);
2145         
2146         if(dx > dy) return 0;
2147         return 1;
2148 }
2149
2150 void uiBlockEndAlign(uiBlock *block)
2151 {
2152         block->flag &= ~UI_BUT_ALIGN;   // all 4 flags
2153 }
2154
2155 int ui_but_can_align(uiBut *but)
2156 {
2157         return !ELEM3(but->type, LABEL, OPTION, OPTIONN);
2158 }
2159
2160 static void ui_block_do_align_but(uiBlock *block, uiBut *first, int nr)
2161 {
2162         uiBut *prev, *but=NULL, *next;
2163         int flag= 0, cols=0, rows=0;
2164         
2165         /* auto align */
2166
2167         for(but=first; but && but->alignnr == nr; but=but->next) {
2168                 if(but->next && but->next->alignnr == nr) {
2169                         if(buts_are_horiz(but, but->next)) cols++;
2170                         else rows++;
2171                 }
2172         }
2173
2174         /* rows==0: 1 row, cols==0: 1 collumn */
2175         
2176         /* note;  how it uses 'flag' in loop below (either set it, or OR it) is confusing */
2177         for(but=first, prev=NULL; but && but->alignnr == nr; prev=but, but=but->next) {
2178                 next= but->next;
2179                 if(next && next->alignnr != nr)
2180                         next= NULL;
2181
2182                 /* clear old flag */
2183                 but->flag &= ~UI_BUT_ALIGN;
2184                 
2185                 if(flag==0) {   /* first case */
2186                         if(next) {
2187                                 if(buts_are_horiz(but, next)) {
2188                                         if(rows==0)
2189                                                 flag= UI_BUT_ALIGN_RIGHT;
2190                                         else 
2191                                                 flag= UI_BUT_ALIGN_DOWN|UI_BUT_ALIGN_RIGHT;
2192                                 }
2193                                 else {
2194                                         flag= UI_BUT_ALIGN_DOWN;
2195                                 }
2196                         }
2197                 }
2198                 else if(next==NULL) {   /* last case */
2199                         if(prev) {
2200                                 if(buts_are_horiz(prev, but)) {
2201                                         if(rows==0) 
2202                                                 flag= UI_BUT_ALIGN_LEFT;
2203                                         else
2204                                                 flag= UI_BUT_ALIGN_TOP|UI_BUT_ALIGN_LEFT;
2205                                 }
2206                                 else flag= UI_BUT_ALIGN_TOP;
2207                         }
2208                 }
2209                 else if(buts_are_horiz(but, next)) {
2210                         /* check if this is already second row */
2211                         if( prev && buts_are_horiz(prev, but)==0) {
2212                                 flag &= ~UI_BUT_ALIGN_LEFT;
2213                                 flag |= UI_BUT_ALIGN_TOP;
2214                                 /* exception case: bottom row */
2215                                 if(rows>0) {
2216                                         uiBut *bt= but;
2217                                         while(bt && bt->alignnr == nr) {
2218                                                 if(bt->next && bt->next->alignnr == nr && buts_are_horiz(bt, bt->next)==0 ) break; 
2219                                                 bt= bt->next;
2220                                         }
2221                                         if(bt==0 || bt->alignnr != nr) flag= UI_BUT_ALIGN_TOP|UI_BUT_ALIGN_RIGHT;
2222                                 }
2223                         }
2224                         else flag |= UI_BUT_ALIGN_LEFT;
2225                 }
2226                 else {
2227                         if(cols==0) {
2228                                 flag |= UI_BUT_ALIGN_TOP;
2229                         }
2230                         else {  /* next button switches to new row */
2231                                 
2232                                 if(prev && buts_are_horiz(prev, but))
2233                                    flag |= UI_BUT_ALIGN_LEFT;
2234                                 
2235                                 if( (flag & UI_BUT_ALIGN_TOP)==0) {     /* stil top row */
2236                                         if(prev)
2237                                                 flag= UI_BUT_ALIGN_DOWN|UI_BUT_ALIGN_LEFT;
2238                                         else 
2239                                                 flag |= UI_BUT_ALIGN_DOWN;
2240                                 }
2241                                 else 
2242                                         flag |= UI_BUT_ALIGN_TOP;
2243                         }
2244                 }
2245                 
2246                 but->flag |= flag;
2247                 
2248                 /* merge coordinates */
2249                 if(prev) {
2250                         // simple cases 
2251                         if(rows==0) {
2252                                 but->x1= (prev->x2+but->x1)/2.0;
2253                                 prev->x2= but->x1;
2254                         }
2255                         else if(cols==0) {
2256                                 but->y2= (prev->y1+but->y2)/2.0;
2257                                 prev->y1= but->y2;
2258                         }
2259                         else {
2260                                 if(buts_are_horiz(prev, but)) {
2261                                         but->x1= (prev->x2+but->x1)/2.0;
2262                                         prev->x2= but->x1;
2263                                         /* copy height too */
2264                                         but->y2= prev->y2;
2265                                 }
2266                                 else if(prev->prev && buts_are_horiz(prev->prev, prev)==0) {
2267                                         /* the previous button is a single one in its row */
2268                                         but->y2= (prev->y1+but->y2)/2.0;
2269                                         prev->y1= but->y2;
2270                                 }
2271                                 else {
2272                                         /* the previous button is not a single one in its row */
2273                                         but->y2= prev->y1;
2274                                 }
2275                         }
2276                 }
2277         }
2278 }
2279
2280 void ui_block_do_align(uiBlock *block)
2281 {
2282         uiBut *but;
2283         int nr;
2284
2285         /* align buttons with same align nr */
2286         for(but=block->buttons.first; but;) {
2287                 if(but->alignnr) {
2288                         nr= but->alignnr;
2289                         ui_block_do_align_but(block, but, nr);
2290
2291                         /* skip with same number */
2292                         for(; but && but->alignnr == nr; but=but->next);
2293
2294                         if(!but)
2295                                 break;
2296                 }
2297                 else
2298                         but= but->next;
2299         }
2300 }
2301
2302 /*
2303 ui_def_but is the function that draws many button types
2304
2305 for float buttons:
2306         "a1" Click Step (how much to change the value each click)
2307         "a2" Number of decimal point values to display. 0 defaults to 3 (0.000) 1,2,3, and a maximum of 4,
2308            all greater values will be clamped to 4.
2309
2310 */
2311 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)
2312 {
2313         uiBut *but;
2314         short slen;
2315         
2316         if(type & BUTPOIN) {            /* a pointer is required */
2317                 if(poin==NULL)
2318                         return NULL;
2319         }
2320
2321         but= MEM_callocN(sizeof(uiBut), "uiBut");
2322
2323         but->type= type & BUTTYPE;
2324         but->pointype= type & BUTPOIN;
2325         but->bit= type & BIT;
2326         but->bitnr= type & 31;
2327         but->icon = 0;
2328         but->iconadd=0;
2329
2330         but->retval= retval;
2331         if( strlen(str)>=UI_MAX_NAME_STR-1 ) {
2332                 but->str= MEM_callocN( strlen(str)+2, "uiDefBut");
2333                 strcpy(but->str, str);
2334         }
2335         else {
2336                 but->str= but->strdata;
2337                 strcpy(but->str, str);
2338         }
2339         but->x1= x1; 
2340         but->y1= y1;
2341         but->x2= (x1+x2); 
2342         but->y2= (y1+y2);
2343         
2344         but->poin= poin;
2345         but->hardmin= but->softmin= min; 
2346         but->hardmax= but->softmax= max;
2347         but->a1= a1; 
2348         but->a2= a2;
2349         but->tip= tip;
2350         
2351         but->lock= block->lock;
2352         but->lockstr= block->lockstr;
2353         but->dt= block->dt;
2354
2355         but->aspect= 1.0f; //XXX block->aspect;
2356         but->block= block;              // pointer back, used for frontbuffer status, and picker
2357
2358         if((block->flag & UI_BUT_ALIGN) && ui_but_can_align(but))
2359                 but->alignnr= block->alignnr;
2360
2361         but->func= block->func;
2362         but->func_arg1= block->func_arg1;
2363         but->func_arg2= block->func_arg2;
2364
2365         but->funcN= block->funcN;
2366         if(block->func_argN)
2367                 but->func_argN= MEM_dupallocN(block->func_argN);
2368         
2369         but->pos= -1;   /* cursor invisible */
2370
2371         if(ELEM4(but->type, NUM, NUMABS, NUMSLI, HSVSLI)) {     /* add a space to name */
2372                 slen= strlen(but->str);
2373                 if(slen>0 && slen<UI_MAX_NAME_STR-2) {
2374                         if(but->str[slen-1]!=' ') {
2375                                 but->str[slen]= ' ';
2376                                 but->str[slen+1]= 0;
2377                         }
2378                 }
2379         }
2380
2381         if((block->flag & UI_BLOCK_LOOP) || ELEM7(but->type, MENU, TEX, LABEL, IDPOIN, BLOCK, BUTM, SEARCH_MENU))
2382                 but->flag |= (UI_TEXT_LEFT|UI_ICON_LEFT);
2383         else if(but->type==BUT_TOGDUAL)
2384                 but->flag |= UI_ICON_LEFT;
2385
2386         but->flag |= (block->flag & UI_BUT_ALIGN);
2387
2388         if (but->lock) {
2389                 if (but->lockstr) {
2390                         but->flag |= UI_BUT_DISABLED;
2391                 }
2392         }
2393
2394         if(ELEM8(but->type, BLOCK, BUT, LABEL, PULLDOWN, ROUNDBOX, LISTBOX, SEARCH_MENU, BUTM));
2395         else if(ELEM5(but->type, SCROLL, SEPR, LINK, INLINK, FTPREVIEW));
2396         else but->flag |= UI_BUT_UNDO;
2397
2398         BLI_addtail(&block->buttons, but);
2399         
2400         if(block->curlayout)
2401                 ui_layout_add_but(block->curlayout, but);
2402
2403         return but;
2404 }
2405
2406 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)
2407 {
2408         uiBut *but;
2409         PropertyRNA *prop;
2410         PropertyType proptype;
2411         int freestr= 0, icon= 0;
2412
2413         prop= RNA_struct_find_property(ptr, propname);
2414
2415         if(prop) {
2416                 proptype= RNA_property_type(prop);
2417
2418                 /* use rna values if parameters are not specified */
2419                 if(!str) {
2420                         if(type == MENU && proptype == PROP_ENUM) {
2421                                 EnumPropertyItem *item;
2422                                 DynStr *dynstr;
2423                                 int i, totitem, value, free;
2424
2425                                 RNA_property_enum_items(block->evil_C, ptr, prop, &item, &totitem, &free);
2426                                 value= RNA_property_enum_get(ptr, prop);
2427
2428                                 dynstr= BLI_dynstr_new();
2429                                 BLI_dynstr_appendf(dynstr, "%s%%t", RNA_property_ui_name(prop));
2430                                 for(i=0; i<totitem; i++) {
2431                                         if(!item[i].identifier[0]) {
2432                                                 if(item[i].name)
2433                                                         BLI_dynstr_appendf(dynstr, "|%s%%l", item[i].name);
2434                                                 else
2435                                                         BLI_dynstr_append(dynstr, "|%l");
2436                                         }
2437                                         else if(item[i].icon)
2438                                                 BLI_dynstr_appendf(dynstr, "|%s %%i%d %%x%d", item[i].name, item[i].icon, item[i].value);
2439                                         else
2440                                                 BLI_dynstr_appendf(dynstr, "|%s %%x%d", item[i].name, item[i].value);
2441
2442                                         if(value == item[i].value)
2443                                                 icon= item[i].icon;
2444                                 }
2445                                 str= BLI_dynstr_get_cstring(dynstr);
2446                                 BLI_dynstr_free(dynstr);
2447
2448                                 if(free)
2449                                         MEM_freeN(item);
2450
2451                                 freestr= 1;
2452                         }
2453                         else if(ELEM(type, ROW, LISTROW) && proptype == PROP_ENUM) {
2454                                 EnumPropertyItem *item;
2455                                 int i, totitem, free;
2456
2457                                 RNA_property_enum_items(block->evil_C, ptr, prop, &item, &totitem, &free);
2458                                 for(i=0; i<totitem; i++) {
2459                                         if(item[i].identifier[0] && item[i].value == (int)max) {
2460                                                 str= (char*)item[i].name;
2461                                                 icon= item[i].icon;
2462                                         }
2463                                 }
2464
2465                                 if(!str)
2466                                         str= (char*)RNA_property_ui_name(prop);
2467                                 if(free)
2468                                         MEM_freeN(item);
2469                         }
2470                         else {
2471                                 str= (char*)RNA_property_ui_name(prop);
2472                                 icon= RNA_property_ui_icon(prop);
2473                         }
2474                 }
2475
2476                 if(!tip) {
2477                         if(type == ROW && proptype == PROP_ENUM) {
2478                                 EnumPropertyItem *item;
2479                                 int i, totitem, free;
2480
2481                                 RNA_property_enum_items(block->evil_C, ptr, prop, &item, &totitem, &free);
2482
2483                                 for(i=0; i<totitem; i++) {
2484                                         if(item[i].identifier[0] && item[i].value == (int)max) {
2485                                                 if(item[i].description[0])
2486                                                         tip= (char*)item[i].description;
2487                                                 break;
2488                                         }
2489                                 }
2490
2491                                 if(free)
2492                                         MEM_freeN(item);
2493                         }
2494                 }
2495                 
2496                 if(!tip)
2497                         tip= (char*)RNA_property_ui_description(prop);
2498
2499                 if(min == max || a1 == -1 || a2 == -1) {
2500                         if(proptype == PROP_INT) {
2501                                 int hardmin, hardmax, softmin, softmax, step;
2502
2503                                 RNA_property_int_range(ptr, prop, &hardmin, &hardmax);
2504                                 RNA_property_int_ui_range(ptr, prop, &softmin, &softmax, &step);
2505
2506                                 if(!ELEM(type, ROW, LISTROW) && min == max) {
2507                                         min= hardmin;
2508                                         max= hardmax;
2509                                 }
2510                                 if(a1 == -1)
2511                                         a1= step;
2512                                 if(a2 == -1)
2513                                         a2= 0;
2514                         }
2515                         else if(proptype == PROP_FLOAT) {
2516                                 float hardmin, hardmax, softmin, softmax, step, precision;
2517
2518                                 RNA_property_float_range(ptr, prop, &hardmin, &hardmax);
2519                                 RNA_property_float_ui_range(ptr, prop, &softmin, &softmax, &step, &precision);
2520
2521                                 if(!ELEM(type, ROW, LISTROW) && min == max) {
2522                                         min= hardmin;
2523                                         max= hardmax;
2524                                 }
2525                                 if(a1 == -1)
2526                                         a1= step;
2527                                 if(a2 == -1)
2528                                         a2= precision;
2529                         }
2530                         else if(proptype == PROP_STRING) {
2531                                 min= 0;
2532                                 max= RNA_property_string_maxlength(prop);
2533                                 if(max == 0) /* interface code should ideally support unlimited length */
2534                                         max= UI_MAX_DRAW_STR; 
2535                         }
2536                 }
2537         }
2538         else
2539                 str= (char*)propname;
2540
2541         /* now create button */
2542         but= ui_def_but(block, type, retval, str, x1, y1, x2, y2, NULL, min, max, a1, a2, tip);
2543
2544         if(prop) {
2545                 but->rnapoin= *ptr;
2546                 but->rnaprop= prop;
2547
2548                 if(RNA_property_array_length(&but->rnapoin, but->rnaprop))
2549                         but->rnaindex= index;
2550                 else
2551                         but->rnaindex= 0;
2552         }
2553
2554         if(icon) {
2555                 but->icon= (BIFIconID)icon;
2556                 but->flag |= UI_HAS_ICON;
2557                 but->flag|= UI_ICON_LEFT;
2558         }
2559         
2560         if (!prop || !RNA_property_editable(&but->rnapoin, prop)) {
2561                 but->flag |= UI_BUT_DISABLED;
2562                 but->lock = 1;
2563                 but->lockstr = "";
2564         }
2565
2566         /* If this button uses units, calculate the step from this */
2567         if(ui_is_but_unit(but))
2568                 but->a1= ui_get_but_step_unit(but, ui_get_but_val(but), but->a1);
2569
2570         if(freestr)
2571                 MEM_freeN(str);
2572         
2573         return but;
2574 }
2575
2576 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)
2577 {
2578         uiBut *but;
2579         wmOperatorType *ot;
2580         
2581         ot= WM_operatortype_find(opname, 0);
2582
2583         if(!str) {
2584                 if(ot) str= ot->name;
2585                 else str= opname;
2586         }
2587         
2588         if ((!tip || tip[0]=='\0') && ot && ot->description) {
2589                 tip= ot->description;
2590         }
2591
2592         but= ui_def_but(block, type, -1, str, x1, y1, x2, y2, NULL, 0, 0, 0, 0, tip);
2593         but->optype= ot;
2594         but->opcontext= opcontext;
2595
2596         if(!ot) {
2597                 but->flag |= UI_BUT_DISABLED;
2598                 but->lock = 1;
2599                 but->lockstr = "";
2600         }
2601
2602         return but;
2603 }
2604
2605 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)
2606 {
2607         uiBut *but= ui_def_but(block, type, retval, str, x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2608
2609         ui_check_but(but);
2610         
2611         return but;
2612 }
2613
2614         /* if _x_ is a power of two (only one bit) return the power,
2615          * otherwise return -1. 
2616          * (1<<findBitIndex(x))==x for powers of two.
2617          */
2618 static int findBitIndex(unsigned int x) {
2619         if (!x || (x&(x-1))!=0) {       /* x&(x-1) strips lowest bit */
2620                 return -1;
2621         } else {
2622                 int idx= 0;
2623
2624                 if (x&0xFFFF0000)       idx+=16, x>>=16;
2625                 if (x&0xFF00)           idx+=8, x>>=8;
2626                 if (x&0xF0)                     idx+=4, x>>=4;
2627                 if (x&0xC)                      idx+=2, x>>=2;
2628                 if (x&0x2)                      idx+=1;
2629
2630                 return idx;
2631         }
2632 }
2633
2634 /* autocomplete helper functions */
2635 struct AutoComplete {
2636         int maxlen;
2637         char *truncate;
2638         char *startname;
2639 };
2640
2641 AutoComplete *autocomplete_begin(char *startname, int maxlen)
2642 {
2643         AutoComplete *autocpl;
2644         
2645         autocpl= MEM_callocN(sizeof(AutoComplete), "AutoComplete");
2646         autocpl->maxlen= maxlen;
2647         autocpl->truncate= MEM_callocN(sizeof(char)*maxlen, "AutoCompleteTruncate");
2648         autocpl->startname= startname;
2649
2650         return autocpl;
2651 }
2652
2653 void autocomplete_do_name(AutoComplete *autocpl, const char *name)
2654 {
2655         char *truncate= autocpl->truncate;
2656         char *startname= autocpl->startname;
2657         int a;
2658
2659         for(a=0; a<autocpl->maxlen-1; a++) {
2660                 if(startname[a]==0 || startname[a]!=name[a])
2661                         break;
2662         }
2663         /* found a match */
2664         if(startname[a]==0) {
2665                 /* first match */
2666                 if(truncate[0]==0)
2667                         BLI_strncpy(truncate, name, autocpl->maxlen);
2668                 else {
2669                         /* remove from truncate what is not in bone->name */
2670                         for(a=0; a<autocpl->maxlen-1; a++) {
2671                                 if(name[a] == 0) {
2672                                         truncate[a]= 0;
2673                                         break;
2674                                 }
2675                                 else if(truncate[a]!=name[a])
2676                                         truncate[a]= 0;
2677                         }
2678                 }
2679         }
2680 }
2681
2682 void autocomplete_end(AutoComplete *autocpl, char *autoname)
2683 {       
2684         if(autocpl->truncate[0])
2685                 BLI_strncpy(autoname, autocpl->truncate, autocpl->maxlen);
2686         else {
2687                 if (autoname != autocpl->startname) /* dont copy a string over its self */
2688                         BLI_strncpy(autoname, autocpl->startname, autocpl->maxlen);
2689         }
2690         MEM_freeN(autocpl->truncate);
2691         MEM_freeN(autocpl);
2692 }
2693
2694 /* autocomplete callback for ID buttons */
2695 static void autocomplete_id(bContext *C, char *str, void *arg_v)
2696 {
2697         int blocktype= (intptr_t)arg_v;
2698         ListBase *listb= which_libbase(CTX_data_main(C), blocktype);
2699         
2700         if(listb==NULL) return;
2701         
2702         /* search if str matches the beginning of an ID struct */
2703         if(str[0]) {
2704                 AutoComplete *autocpl= autocomplete_begin(str, 22);
2705                 ID *id;
2706                 
2707                 for(id= listb->first; id; id= id->next)
2708                         autocomplete_do_name(autocpl, id->name+2);
2709
2710                 autocomplete_end(autocpl, str);
2711         }
2712 }
2713
2714 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)
2715 {
2716         int bitIdx= findBitIndex(bit);
2717         if (bitIdx==-1) {
2718                 return NULL;
2719         } else {
2720                 return uiDefBut(block, type|BIT|bitIdx, retval, str, x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2721         }
2722 }
2723 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)
2724 {
2725         return uiDefBut(block, type|FLO, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2726 }
2727 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)
2728 {
2729         return uiDefButBit(block, type|FLO, bit, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2730 }
2731 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)
2732 {
2733         return uiDefBut(block, type|INT, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2734 }
2735 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)
2736 {
2737         return uiDefButBit(block, type|INT, bit, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2738 }
2739 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)
2740 {
2741         return uiDefBut(block, type|SHO, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2742 }
2743 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)
2744 {
2745         return uiDefButBit(block, type|SHO, bit, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2746 }
2747 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)
2748 {
2749         return uiDefBut(block, type|CHA, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2750 }
2751 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)
2752 {
2753         return uiDefButBit(block, type|CHA, bit, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2754 }
2755 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)
2756 {
2757         uiBut *but;
2758
2759         but= ui_def_but_rna(block, type, retval, str, x1, y1, x2, y2, ptr, propname, index, min, max, a1, a2, tip);
2760         if(but)
2761                 ui_check_but(but);
2762
2763         return but;
2764 }
2765 uiBut *uiDefButO(uiBlock *block, int type, char *opname, int opcontext, char *str, short x1, short y1, short x2, short y2, char *tip)
2766 {
2767         uiBut *but;
2768
2769         but= ui_def_but_operator(block, type, opname, opcontext, str, x1, y1, x2, y2, tip);
2770         if(but)
2771                 ui_check_but(but);
2772
2773         return but;
2774 }
2775
2776 /* if a1==1.0 then a2 is an extra icon blending factor (alpha 0.0 - 1.0) */
2777 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)
2778 {
2779         uiBut *but= ui_def_but(block, type, retval, "", x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2780         
2781         but->icon= (BIFIconID) icon;
2782         but->flag|= UI_HAS_ICON;
2783
2784         ui_check_but(but);
2785         
2786         return but;
2787 }
2788 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)
2789 {
2790         int bitIdx= findBitIndex(bit);
2791         if (bitIdx==-1) {
2792                 return NULL;
2793         } else {
2794                 return uiDefIconBut(block, type|BIT|bitIdx, retval, icon, x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2795         }
2796 }
2797
2798 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)
2799 {
2800         return uiDefIconBut(block, type|FLO, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2801 }
2802 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)
2803 {
2804         return uiDefIconButBit(block, type|FLO, bit, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2805 }
2806 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)
2807 {
2808         return uiDefIconBut(block, type|INT, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2809 }
2810 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)
2811 {
2812         return uiDefIconButBit(block, type|INT, bit, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2813 }
2814 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)
2815 {
2816         return uiDefIconBut(block, type|SHO, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2817 }
2818 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)
2819 {
2820         return uiDefIconButBit(block, type|SHO, bit, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2821 }
2822 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)
2823 {
2824         return uiDefIconBut(block, type|CHA, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2825 }
2826 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)
2827 {
2828         return uiDefIconButBit(block, type|CHA, bit, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2829 }
2830 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)
2831 {
2832         uiBut *but;
2833
2834         but= ui_def_but_rna(block, type, retval, "", x1, y1, x2, y2, ptr, propname, index, min, max, a1, a2, tip);
2835         if(but) {
2836                 if(icon) {
2837                         but->icon= (BIFIconID) icon;
2838                         but->flag|= UI_HAS_ICON;
2839                 }
2840                 ui_check_but(but);
2841         }
2842
2843         return but;
2844 }
2845 uiBut *uiDefIconButO(uiBlock *block, int type, char *opname, int opcontext, int icon, short x1, short y1, short x2, short y2, char *tip)
2846 {
2847         uiBut *but;
2848
2849         but= ui_def_but_operator(block, type, opname, opcontext, "", x1, y1, x2, y2, tip);
2850         if(but) {
2851                 but->icon= (BIFIconID) icon;
2852                 but->flag|= UI_HAS_ICON;
2853                 ui_check_but(but);
2854         }
2855
2856         return but;
2857 }
2858
2859 /* Button containing both string label and icon */
2860 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)
2861 {
2862         uiBut *but= ui_def_but(block, type, retval, str, x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2863
2864         but->icon= (BIFIconID) icon;
2865         but->flag|= UI_HAS_ICON;
2866
2867         but->flag|= UI_ICON_LEFT;
2868
2869         ui_check_but(but);
2870
2871         return but;
2872 }
2873 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)
2874 {
2875         int bitIdx= findBitIndex(bit);
2876         if (bitIdx==-1) {
2877                 return NULL;
2878         } else {
2879                 return uiDefIconTextBut(block, type|BIT|bitIdx, retval, icon, str, x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2880         }
2881 }
2882
2883 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)
2884 {
2885         return uiDefIconTextBut(block, type|FLO, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2886 }
2887 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)
2888 {
2889         return uiDefIconTextButBit(block, type|FLO, bit, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2890 }
2891 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)
2892 {
2893         return uiDefIconTextBut(block, type|INT, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2894 }
2895 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)
2896 {
2897         return uiDefIconTextButBit(block, type|INT, bit, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2898 }
2899 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)
2900 {
2901         return uiDefIconTextBut(block, type|SHO, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2902 }
2903 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)
2904 {
2905         return uiDefIconTextButBit(block, type|SHO, bit, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2906 }
2907 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)
2908 {
2909         return uiDefIconTextBut(block, type|CHA, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2910 }
2911 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)
2912 {
2913         return uiDefIconTextButBit(block, type|CHA, bit, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2914 }
2915 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)
2916 {
2917         uiBut *but;
2918
2919         but= ui_def_but_rna(block, type, retval, str, x1, y1, x2, y2, ptr, propname, index, min, max, a1, a2, tip);
2920         if(but) {
2921                 if(icon) {
2922                         but->icon= (BIFIconID) icon;
2923                         but->flag|= UI_HAS_ICON;
2924                 }
2925                 but->flag|= UI_ICON_LEFT;
2926                 ui_check_but(but);
2927         }
2928
2929         return but;
2930 }
2931 uiBut *uiDefIconTextButO(uiBlock *block, int type, char *opname, int opcontext, int icon, char *str, short x1, short y1, short x2, short y2, char *tip)
2932 {
2933         uiBut *but;
2934
2935         but= ui_def_but_operator(block, type, opname, opcontext, str, x1, y1, x2, y2, tip);
2936         if(but) {
2937                 but->icon= (BIFIconID) icon;
2938                 but->flag|= UI_HAS_ICON;
2939                 but->flag|= UI_ICON_LEFT;
2940                 ui_check_but(but);
2941         }
2942
2943         return but;
2944 }
2945
2946 /* END Button containing both string label and icon */
2947
2948 void uiSetButLink(uiBut *but, void **poin, void ***ppoin, short *tot, int from, int to)
2949 {
2950         uiLink *link;
2951         
2952         link= but->link= MEM_callocN(sizeof(uiLink), "new uilink");
2953         
2954         link->poin= poin;
2955         link->ppoin= ppoin;
2956         link->totlink= tot;
2957         link->fromcode= from;
2958         link->tocode= to;
2959 }
2960
2961 /* cruft to make uiBlock and uiBut private */
2962
2963 int uiBlocksGetYMin(ListBase *lb)
2964 {
2965         uiBlock *block;
2966         int min= 0;
2967         
2968         for (block= lb->first; block; block= block->next)
2969                 if (block==lb->first || block->miny<min)
2970                         min= block->miny;
2971                         
2972         return min;
2973 }
2974
2975 void uiBlockSetDirection(uiBlock *block, int direction)
2976 {
2977         block->direction= direction;
2978 }
2979
2980 /* this call escapes if there's alignment flags */
2981 void uiBlockFlipOrder(uiBlock *block)
2982 {
2983         ListBase lb;
2984         uiBut *but, *next;
2985         float centy, miny=10000, maxy= -10000;
2986
2987         if(U.uiflag & USER_MENUFIXEDORDER)
2988                 return;
2989         else if(block->flag & UI_BLOCK_NO_FLIP)
2990                 return;
2991         
2992         for(but= block->buttons.first; but; but= but->next) {
2993                 if(but->flag & UI_BUT_ALIGN) return;
2994                 if(but->y1 < miny) miny= but->y1;
2995                 if(but->y2 > maxy) maxy= but->y2;
2996         }
2997         /* mirror trick */
2998         centy= (miny+maxy)/2.0;
2999         for(but= block->buttons.first; but; but= but->next) {
3000                 but->y1 = centy-(but->y1-centy);
3001                 but->y2 = centy-(but->y2-centy);
3002                 SWAP(float, but->y1, but->y2);
3003         }
3004         
3005         /* also flip order in block itself, for example for arrowkey */
3006         lb.first= lb.last= NULL;
3007         but= block->buttons.first;
3008         while(but) {
3009                 next= but->next;
3010                 BLI_remlink(&block->buttons, but);
3011                 BLI_addtail(&lb, but);
3012                 but= next;
3013         }
3014         block->buttons= lb;
3015 }
3016
3017
3018 void uiBlockSetFlag(uiBlock *block, int flag)
3019 {
3020         block->flag|= flag;
3021 }
3022
3023 void uiBlockClearFlag(uiBlock *block, int flag)
3024 {
3025         block->flag&= ~flag;
3026 }
3027
3028 void uiBlockSetXOfs(uiBlock *block, int xofs)
3029 {
3030         block->xofs= xofs;
3031 }
3032
3033 void uiButSetFlag(uiBut *but, int flag)
3034 {
3035         but->flag|= flag;
3036 }
3037
3038 void uiButClearFlag(uiBut *but, int flag)
3039 {
3040         but->flag&= ~flag;
3041 }
3042
3043 int uiButGetRetVal(uiBut *but)
3044 {
3045         return but->retval;
3046 }
3047
3048 void uiButSetDragID(uiBut *but, ID *id)
3049 {
3050         but->dragtype= WM_DRAG_ID;
3051         but->dragpoin= (void *)id;
3052 }
3053
3054 void uiButSetDragRNA(uiBut *but, PointerRNA *ptr)
3055 {
3056         but->dragtype= WM_DRAG_RNA;
3057         but->dragpoin= (void *)ptr;
3058 }
3059
3060 void uiButSetDragPath(uiBut *but, const char *path)
3061 {
3062         but->dragtype= WM_DRAG_PATH;
3063         but->dragpoin= (void *)path;
3064 }
3065
3066 void uiButSetDragName(uiBut *but, const char *name)
3067 {
3068         but->dragtype= WM_DRAG_NAME;
3069         but->dragpoin= (void *)name;
3070 }
3071
3072 /* value from button itself */
3073 void uiButSetDragValue(uiBut *but)
3074 {
3075         but->dragtype= WM_DRAG_VALUE;
3076 }
3077
3078 void uiButSetDragImage(uiBut *but, const char *path, int icon, struct ImBuf *imb, float scale)
3079 {
3080         but->dragtype= WM_DRAG_PATH;
3081         but->icon= icon; /* no flag UI_HAS_ICON, so icon doesnt draw in button */
3082         but->dragpoin= (void *)path;
3083         but->imb= imb;
3084         but->imb_scale= scale;
3085 }
3086
3087 PointerRNA *uiButGetOperatorPtrRNA(uiBut *but)
3088 {
3089         if(but->optype && !but->opptr) {
3090                 but->opptr= MEM_callocN(sizeof(PointerRNA), "uiButOpPtr");
3091                 WM_operator_properties_create_ptr(but->opptr, but->optype);
3092         }
3093
3094         return but->opptr;
3095 }
3096
3097 void uiBlockSetHandleFunc(uiBlock *block, uiBlockHandleFunc func, void *arg)
3098 {
3099         block->handle_func= func;
3100         block->handle_func_arg= arg;
3101 }
3102
3103 void uiBlockSetButmFunc(uiBlock *block, uiMenuHandleFunc func, void *arg)
3104 {
3105         block->butm_func= func;
3106         block->butm_func_arg= arg;
3107 }
3108
3109 void uiBlockSetFunc(uiBlock *block, uiButHandleFunc func, void *arg1, void *arg2)
3110 {
3111         block->func= func;
3112         block->func_arg1= arg1;
3113         block->func_arg2= arg2;
3114 }
3115
3116 void uiBlockSetNFunc(uiBlock *block, uiButHandleFunc func, void *argN, void *arg2)
3117 {
3118         if(block->func_argN)
3119                 MEM_freeN(block->func_argN);
3120
3121         block->funcN= func;
3122         block->func_argN= argN;
3123         block->func_arg2= arg2;
3124 }
3125
3126 void uiButSetRenameFunc(uiBut *but, uiButHandleRenameFunc func, void *arg1)
3127 {
3128         but->rename_func= func;
3129         but->rename_arg1= arg1;
3130 }
3131
3132 void uiBlockSetDrawExtraFunc(uiBlock *block, void (*func)(const bContext *C, void *idv, void *arg1, void *arg2, rcti *rect), void *arg1, void *arg2)
3133 {
3134         block->drawextra= func;
3135         block->drawextra_arg1= arg1;
3136         block->drawextra_arg2= arg2;
3137 }
3138
3139 void uiButSetFunc(uiBut *but, uiButHandleFunc func, void *arg1, void *arg2)
3140 {
3141         but->func= func;
3142         but->func_arg1= arg1;
3143         but->func_arg2= arg2;
3144 }
3145
3146 void uiButSetNFunc(uiBut *but, uiButHandleNFunc funcN, void *argN, void *arg2)
3147 {
3148         if(but->func_argN)
3149                 MEM_freeN(but->func_argN);
3150
3151         but->funcN= funcN;
3152         but->func_argN= argN;
3153         but->func_arg2= arg2;
3154 }
3155
3156 void uiButSetCompleteFunc(uiBut *but, uiButCompleteFunc func, void *arg)
3157 {
3158         but->autocomplete_func= func;
3159         but->autofunc_arg= arg;
3160 }
3161
3162 uiBut *uiDefIDPoinBut(uiBlock *block, uiIDPoinFuncFP func, short blocktype, int retval, char *str, short x1, short y1, short x2, short y2, void *idpp, char *tip)
3163 {
3164         uiBut *but= ui_def_but(block, IDPOIN, retval, str, x1, y1, x2, y2, NULL, 0.0, 0.0, 0.0, 0.0, tip);
3165         but->idpoin_func= func;
3166         but->idpoin_idpp= (ID**) idpp;
3167         ui_check_but(but);
3168         
3169         if(blocktype)
3170                 uiButSetCompleteFunc(but, autocomplete_id, (void *)(intptr_t)blocktype);
3171