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