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