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