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