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