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