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