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