use 'const char *' by default with RNA functions except when the value is flagged...
[blender.git] / source / blender / editors / interface / interface.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * Contributor(s): Blender Foundation 2002-2008, full recode.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 #include <float.h>
29 #include <limits.h>
30 #include <math.h>
31 #include <string.h>
32  
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                 if(!(but->flag & UI_HIDDEN) &&
763                         /* XXX: figure out why invalid coordinates happen when closing render window */
764                         /* and material preview is redrawn in main window (temp fix for bug #23848) */
765                         rect.xmin < rect.xmax && rect.ymin < rect.ymax)
766                         ui_draw_but(C, ar, &style, but, &rect);
767         }
768         
769         /* restore matrix */
770         glMatrixMode(GL_PROJECTION);
771         glPopMatrix();
772         glMatrixMode(GL_MODELVIEW);
773         glPopMatrix();
774         
775         ui_draw_links(block);
776 }
777
778 /* ************* EVENTS ************* */
779
780 static void ui_is_but_sel(uiBut *but)
781 {
782         double value;
783         int lvalue;
784         short push=0, true=1;
785
786         value= ui_get_but_val(but);
787
788         if(ELEM3(but->type, TOGN, ICONTOGN, OPTIONN)) true= 0;
789
790         if( but->bit ) {
791                 lvalue= (int)value;
792                 if( BTST(lvalue, (but->bitnr)) ) push= true;
793                 else push= !true;
794         }
795         else {
796                 switch(but->type) {
797                 case BUT:
798                         push= 2;
799                         break;
800                 case HOTKEYEVT:
801                 case KEYEVT:
802                         push= 2;
803                         break;
804                 case TOGBUT:
805                 case TOG:
806                 case TOGR:
807                 case TOG3:
808                 case BUT_TOGDUAL:
809                 case ICONTOG:
810                 case OPTION:
811                         if(value!=but->hardmin) push= 1;
812                         break;
813                 case ICONTOGN:
814                 case TOGN:
815                 case OPTIONN:
816                         if(value==0.0) push= 1;
817                         break;
818                 case ROW:
819                 case LISTROW:
820                         /* support for rna enum buts */
821                         if(but->rnaprop && (RNA_property_flag(but->rnaprop) & PROP_ENUM_FLAG)) {
822                                 if((int)value & (int)but->hardmax) push= 1;
823                         }
824                         else {
825                                 if(value == but->hardmax) push= 1;
826                         }
827                         break;
828                 case COL:
829                         push= 2;
830                         break;
831                 default:
832                         push= 2;
833                         break;
834                 }
835         }
836         
837         if(push==2);
838         else if(push==1) but->flag |= UI_SELECT;
839         else but->flag &= ~UI_SELECT;
840 }
841
842 /* XXX 2.50 no links supported yet */
843
844 static int uibut_contains_pt(uiBut *UNUSED(but), short *UNUSED(mval))
845 {
846         return 0;
847
848 }
849
850 uiBut *ui_get_valid_link_button(uiBlock *block, uiBut *but, short *mval)
851 {
852         uiBut *bt;
853         
854                 /* find button to link to */
855         for (bt= block->buttons.first; bt; bt= bt->next)
856                 if(bt!=but && uibut_contains_pt(bt, mval))
857                         break;
858
859         if (bt) {
860                 if (but->type==LINK && bt->type==INLINK) {
861                         if( but->link->tocode == (int)bt->hardmin ) {
862                                 return bt;
863                         }
864                 }
865                 else if(but->type==INLINK && bt->type==LINK) {
866                         if( bt->link->tocode == (int)but->hardmin ) {
867                                 return bt;
868                         }
869                 }
870         }
871
872         return NULL;
873 }
874
875
876 static uiBut *ui_find_inlink(uiBlock *block, void *poin)
877 {
878         uiBut *but;
879         
880         but= block->buttons.first;
881         while(but) {
882                 if(but->type==INLINK) {
883                         if(but->poin == poin) return but;
884                 }
885                 but= but->next;
886         }
887         return NULL;
888 }
889
890 static void ui_add_link_line(ListBase *listb, uiBut *but, uiBut *bt)
891 {
892         uiLinkLine *line;
893         
894         line= MEM_callocN(sizeof(uiLinkLine), "linkline");
895         BLI_addtail(listb, line);
896         line->from= but;
897         line->to= bt;
898 }
899
900 uiBut *uiFindInlink(uiBlock *block, void *poin)
901 {
902         return ui_find_inlink(block, poin);
903 }
904
905 void uiComposeLinks(uiBlock *block)
906 {
907         uiBut *but, *bt;
908         uiLink *link;
909         void ***ppoin;
910         int a;
911         
912         but= block->buttons.first;
913         while(but) {
914                 if(but->type==LINK) {
915                         link= but->link;
916                         
917                         /* for all pointers in the array */
918                         if(link) {
919                                 if(link->ppoin) {
920                                         ppoin= link->ppoin;
921                                         for(a=0; a < *(link->totlink); a++) {
922                                                 bt= ui_find_inlink(block, (*ppoin)[a] );
923                                                 if(bt) {
924                                                         ui_add_link_line(&link->lines, but, bt);
925                                                 }
926                                         }
927                                 }
928                                 else if(link->poin) {
929                                         bt= ui_find_inlink(block, *(link->poin) );
930                                         if(bt) {
931                                                 ui_add_link_line(&link->lines, but, bt);
932                                         }
933                                 }
934                         }
935                 }
936                 but= but->next;
937         }
938 }
939
940
941 /* ************************************************ */
942
943 void uiBlockSetButLock(uiBlock *block, int val, char *lockstr)
944 {
945         if(val) {
946                 block->lock= val ? 1:0;
947                 block->lockstr= lockstr;
948         }
949 }
950
951 void uiBlockClearButLock(uiBlock *block)
952 {
953         block->lock= 0;
954         block->lockstr= NULL;
955 }
956
957 /* *************************************************************** */
958
959 void ui_delete_linkline(uiLinkLine *line, uiBut *but)
960 {
961         uiLink *link;
962         int a, b;
963         
964         BLI_remlink(&but->link->lines, line);
965
966         link= line->from->link;
967
968         /* are there more pointers allowed? */
969         if(link->ppoin) {
970                 
971                 if(*(link->totlink)==1) {
972                         *(link->totlink)= 0;
973                         MEM_freeN(*(link->ppoin));
974                         *(link->ppoin)= NULL;
975                 }
976                 else {
977                         b= 0;
978                         for(a=0; a< (*(link->totlink)); a++) {
979                                 
980                                 if( (*(link->ppoin))[a] != line->to->poin ) {
981                                         (*(link->ppoin))[b]= (*(link->ppoin))[a];
982                                         b++;
983                                 }
984                         }       
985                         (*(link->totlink))--;
986                 }
987         }
988         else {
989                 *(link->poin)= NULL;
990         }
991
992         MEM_freeN(line);
993         //REDRAW
994 }
995 /* XXX 2.50 no links supported yet */
996 #if 0
997 static void ui_delete_active_linkline(uiBlock *block)
998 {
999         uiBut *but;
1000         uiLink *link;
1001         uiLinkLine *line, *nline;
1002         int a, b;
1003         
1004         but= block->buttons.first;
1005         while(but) {
1006                 if(but->type==LINK && but->link) {
1007                         line= but->link->lines.first;
1008                         while(line) {
1009                                 
1010                                 nline= line->next;
1011                                 
1012                                 if(line->flag & UI_SELECT) {
1013                                         BLI_remlink(&but->link->lines, line);
1014
1015                                         link= line->from->link;
1016
1017                                         /* are there more pointers allowed? */
1018                                         if(link->ppoin) {
1019                                                 
1020                                                 if(*(link->totlink)==1) {
1021                                                         *(link->totlink)= 0;
1022                                                         MEM_freeN(*(link->ppoin));
1023                                                         *(link->ppoin)= NULL;
1024                                                 }
1025                                                 else {
1026                                                         b= 0;
1027                                                         for(a=0; a< (*(link->totlink)); a++) {
1028                                                                 
1029                                                                 if( (*(link->ppoin))[a] != line->to->poin ) {
1030                                                                         (*(link->ppoin))[b]= (*(link->ppoin))[a];
1031                                                                         b++;
1032                                                                 }
1033                                                         }       
1034                                                         (*(link->totlink))--;
1035                                                 }
1036                                         }
1037                                         else {
1038                                                 *(link->poin)= NULL;
1039                                         }
1040
1041                                         MEM_freeN(line);
1042                                 }
1043                                 line= nline;
1044                         }
1045                 }
1046                 but= but->next;
1047         }
1048         
1049         /* temporal! these buttons can be everywhere... */
1050         allqueue(REDRAWBUTSLOGIC, 0);
1051 }
1052
1053 static void ui_do_active_linklines(uiBlock *block, short *mval)
1054 {
1055         uiBut *but;
1056         uiLinkLine *line, *act= NULL;
1057         float mindist= 12.0, fac, v1[2], v2[2], v3[3];
1058         int foundone= 0; 
1059         
1060         if(mval) {
1061                 v1[0]= mval[0];
1062                 v1[1]= mval[1];
1063                 
1064                 /* find a line close to the mouse */
1065                 but= block->buttons.first;
1066                 while(but) {
1067                         if(but->type==LINK && but->link) {
1068                                 foundone= 1;
1069                                 line= but->link->lines.first;
1070                                 while(line) {
1071                                         v2[0]= line->from->x2;
1072                                         v2[1]= (line->from->y1+line->from->y2)/2.0;
1073                                         v3[0]= line->to->x1;
1074                                         v3[1]= (line->to->y1+line->to->y2)/2.0;
1075                                         
1076                                         fac= dist_to_line_segment_v2(v1, v2, v3);
1077                                         if(fac < mindist) {
1078                                                 mindist= fac;
1079                                                 act= line;
1080                                         }
1081                                         line= line->next;
1082                                 }
1083                         }
1084                         but= but->next;
1085                 }
1086         }
1087
1088         /* check for a 'found one' to prevent going to 'frontbuffer' mode.
1089                 this slows done gfx quite some, and at OSX the 'finish' forces a swapbuffer */
1090         if(foundone) {
1091                 glDrawBuffer(GL_FRONT);
1092                 
1093                 /* draw */
1094                 but= block->buttons.first;
1095                 while(but) {
1096                         if(but->type==LINK && but->link) {
1097                                 line= but->link->lines.first;
1098                                 while(line) {
1099                                         if(line==act) {
1100                                                 if((line->flag & UI_SELECT)==0) {
1101                                                         line->flag |= UI_SELECT;
1102                                                         ui_draw_linkline(line);
1103                                                 }
1104                                         }
1105                                         else if(line->flag & UI_SELECT) {
1106                                                 line->flag &= ~UI_SELECT;
1107                                                 ui_draw_linkline(line);
1108                                         }
1109                                         line= line->next;
1110                                 }
1111                         }
1112                         but= but->next;
1113                 }
1114                 bglFlush();
1115                 glDrawBuffer(GL_BACK);
1116         }
1117 }
1118 #endif
1119
1120 /* ******************************************************* */
1121
1122 /* XXX 2.50 no screendump supported yet */
1123
1124 #if 0
1125 /* nasty but safe way to store screendump rect */
1126 static int scr_x=0, scr_y=0, scr_sizex=0, scr_sizey=0;
1127
1128 static void ui_set_screendump_bbox(uiBlock *block)
1129 {
1130         if(block) {
1131                 scr_x= block->minx;
1132                 scr_y= block->miny;
1133                 scr_sizex= block->maxx - block->minx;
1134                 scr_sizey= block->maxy - block->miny;
1135         }
1136         else {
1137                 scr_sizex= scr_sizey= 0;
1138         }
1139 }
1140
1141 /* used for making screenshots for menus, called in screendump.c */
1142 int uiIsMenu(int *x, int *y, int *sizex, int *sizey)
1143 {
1144         if(scr_sizex!=0 && scr_sizey!=0) {
1145                 *x= scr_x;
1146                 *y= scr_y;
1147                 *sizex= scr_sizex;
1148                 *sizey= scr_sizey;
1149                 return 1;
1150         }
1151         
1152         return 0;
1153 }
1154 #endif
1155
1156 /* *********************** data get/set ***********************
1157  * this either works with the pointed to data, or can work with
1158  * an edit override pointer while dragging for example */
1159
1160 /* for buttons pointing to color for example */
1161 void ui_get_but_vectorf(uiBut *but, float *vec)
1162 {
1163         PropertyRNA *prop;
1164         int a, tot;
1165
1166         if(but->editvec) {
1167                 VECCOPY(vec, but->editvec);
1168         }
1169
1170         if(but->rnaprop) {
1171                 prop= but->rnaprop;
1172
1173                 vec[0]= vec[1]= vec[2]= 0.0f;
1174
1175                 if(RNA_property_type(prop) == PROP_FLOAT) {
1176                         tot= RNA_property_array_length(&but->rnapoin, prop);
1177                         tot= MIN2(tot, 3);
1178
1179                         for(a=0; a<tot; a++)
1180                                 vec[a]= RNA_property_float_get_index(&but->rnapoin, prop, a);
1181                 }
1182         }
1183         else if(but->pointype == CHA) {
1184                 char *cp= (char *)but->poin;
1185                 vec[0]= ((float)cp[0])/255.0;
1186                 vec[1]= ((float)cp[1])/255.0;
1187                 vec[2]= ((float)cp[2])/255.0;
1188         }
1189         else if(but->pointype == FLO) {
1190                 float *fp= (float *)but->poin;
1191                 VECCOPY(vec, fp);
1192         }
1193         else {
1194                 if (but->editvec==NULL) {
1195                         fprintf(stderr, "ui_get_but_vectorf: can't get color, should never happen\n");
1196                         vec[0]= vec[1]= vec[2]= 0.0f;
1197                 }
1198         }
1199 }
1200
1201 /* for buttons pointing to color for example */
1202 void ui_set_but_vectorf(uiBut *but, float *vec)
1203 {
1204         PropertyRNA *prop;
1205         int a, tot;
1206
1207         if(but->editvec) {
1208                 VECCOPY(but->editvec, vec);
1209         }
1210
1211         if(but->rnaprop) {
1212                 prop= but->rnaprop;
1213
1214                 if(RNA_property_type(prop) == PROP_FLOAT) {
1215                         tot= RNA_property_array_length(&but->rnapoin, prop);
1216                         tot= MIN2(tot, 3);
1217
1218                         for(a=0; a<tot; a++)
1219                                 RNA_property_float_set_index(&but->rnapoin, prop, a, vec[a]);
1220                 }
1221         }
1222         else if(but->pointype == CHA) {
1223                 char *cp= (char *)but->poin;
1224                 cp[0]= (char)(0.5 +vec[0]*255.0);
1225                 cp[1]= (char)(0.5 +vec[1]*255.0);
1226                 cp[2]= (char)(0.5 +vec[2]*255.0);
1227         }
1228         else if(but->pointype == FLO) {
1229                 float *fp= (float *)but->poin;
1230                 VECCOPY(fp, vec);
1231         }
1232 }
1233
1234 int ui_is_but_float(uiBut *but)
1235 {
1236         if(but->pointype==FLO && but->poin)
1237                 return 1;
1238         
1239         if(but->rnaprop && RNA_property_type(but->rnaprop) == PROP_FLOAT)
1240                 return 1;
1241         
1242         return 0;
1243 }
1244
1245 int ui_is_but_unit(uiBut *but)
1246 {
1247         Scene *scene= CTX_data_scene((bContext *)but->block->evil_C);
1248         int unit_type;
1249         
1250         if(but->rnaprop==NULL)
1251                 return 0;
1252
1253         unit_type = RNA_SUBTYPE_UNIT(RNA_property_subtype(but->rnaprop));
1254
1255 #if 0 // removed so angle buttons get correct snapping
1256         if (scene->unit.flag & USER_UNIT_ROT_RADIANS && unit_type == PROP_UNIT_ROTATION)
1257                 return 0;
1258 #endif
1259         
1260         /* for now disable time unit conversion */      
1261         if (unit_type == PROP_UNIT_TIME)
1262                 return 0;
1263
1264         if (scene->unit.system == USER_UNIT_NONE) {
1265            if (unit_type != PROP_UNIT_ROTATION)
1266                         return 0;
1267         }
1268
1269         if(unit_type == PROP_UNIT_NONE)
1270                 return 0;
1271
1272         return 1;
1273 }
1274
1275 double ui_get_but_val(uiBut *but)
1276 {
1277         PropertyRNA *prop;
1278         double value = 0.0;
1279
1280         if(but->editval) { return *(but->editval); }
1281         if(but->poin==NULL && but->rnapoin.data==NULL) return 0.0;
1282
1283         if(but->rnaprop) {
1284                 prop= but->rnaprop;
1285
1286                 switch(RNA_property_type(prop)) {
1287                         case PROP_BOOLEAN:
1288                                 if(RNA_property_array_length(&but->rnapoin, prop))
1289                                         value= RNA_property_boolean_get_index(&but->rnapoin, prop, but->rnaindex);
1290                                 else
1291                                         value= RNA_property_boolean_get(&but->rnapoin, prop);
1292                                 break;
1293                         case PROP_INT:
1294                                 if(RNA_property_array_length(&but->rnapoin, prop))
1295                                         value= RNA_property_int_get_index(&but->rnapoin, prop, but->rnaindex);
1296                                 else
1297                                         value= RNA_property_int_get(&but->rnapoin, prop);
1298                                 break;
1299                         case PROP_FLOAT:
1300                                 if(RNA_property_array_length(&but->rnapoin, prop))
1301                                         value= RNA_property_float_get_index(&but->rnapoin, prop, but->rnaindex);
1302                                 else
1303                                         value= RNA_property_float_get(&but->rnapoin, prop);
1304                                 break;
1305                         case PROP_ENUM:
1306                                 value= RNA_property_enum_get(&but->rnapoin, prop);
1307                                 break;
1308                         default:
1309                                 value= 0.0;
1310                                 break;
1311                 }
1312         }
1313         else if(but->type== HSVSLI) {
1314                 float h, s, v, *fp;
1315                 
1316                 fp= (but->editvec)? but->editvec: (float *)but->poin;
1317                 rgb_to_hsv(fp[0], fp[1], fp[2], &h, &s, &v);
1318
1319                 switch(but->str[0]) {
1320                         case 'H': value= h; break;
1321                         case 'S': value= s; break;
1322                         case 'V': value= v; break;
1323                 }
1324         } 
1325         else if( but->pointype == CHA ) {
1326                 value= *(char *)but->poin;
1327         }
1328         else if( but->pointype == SHO ) {
1329                 value= *(short *)but->poin;
1330         } 
1331         else if( but->pointype == INT ) {
1332                 value= *(int *)but->poin;
1333         } 
1334         else if( but->pointype == FLO ) {
1335                 value= *(float *)but->poin;
1336         }
1337     
1338         return value;
1339 }
1340
1341 void ui_set_but_val(uiBut *but, double value)
1342 {
1343         PropertyRNA *prop;
1344
1345         /* value is a hsv value: convert to rgb */
1346         if(but->rnaprop) {
1347                 prop= but->rnaprop;
1348
1349                 if(RNA_property_editable(&but->rnapoin, prop)) {
1350                         switch(RNA_property_type(prop)) {
1351                                 case PROP_BOOLEAN:
1352                                         if(RNA_property_array_length(&but->rnapoin, prop))
1353                                                 RNA_property_boolean_set_index(&but->rnapoin, prop, but->rnaindex, value);
1354                                         else
1355                                                 RNA_property_boolean_set(&but->rnapoin, prop, value);
1356                                         break;
1357                                 case PROP_INT:
1358                                         if(RNA_property_array_length(&but->rnapoin, prop))
1359                                                 RNA_property_int_set_index(&but->rnapoin, prop, but->rnaindex, value);
1360                                         else
1361                                                 RNA_property_int_set(&but->rnapoin, prop, value);
1362                                         break;
1363                                 case PROP_FLOAT:
1364                                         if(RNA_property_array_length(&but->rnapoin, prop))
1365                                                 RNA_property_float_set_index(&but->rnapoin, prop, but->rnaindex, value);
1366                                         else
1367                                                 RNA_property_float_set(&but->rnapoin, prop, value);
1368                                         break;
1369                                 case PROP_ENUM:
1370                                         if(RNA_property_flag(prop) & PROP_ENUM_FLAG) {
1371                                                 int ivalue= (int)value;
1372                                                 ivalue ^= RNA_property_enum_get(&but->rnapoin, prop); /* toggle for enum/flag buttons */
1373                                                 RNA_property_enum_set(&but->rnapoin, prop, ivalue);
1374                                         }
1375                                         else {
1376                                                 RNA_property_enum_set(&but->rnapoin, prop, value);
1377                                         }
1378                                         break;
1379                                 default:
1380                                         break;
1381                         }
1382                 }
1383         }
1384         else if(but->pointype==0);
1385         else if(but->type==HSVSLI ) {
1386                 float h, s, v, *fp;
1387                 
1388                 fp= (but->editvec)? but->editvec: (float *)but->poin;
1389                 rgb_to_hsv(fp[0], fp[1], fp[2], &h, &s, &v);
1390                 
1391                 switch(but->str[0]) {
1392                 case 'H': h= value; break;
1393                 case 'S': s= value; break;
1394                 case 'V': v= value; break;
1395                 }
1396                 
1397                 hsv_to_rgb(h, s, v, fp, fp+1, fp+2);
1398                 
1399         }
1400         else {
1401                 /* first do rounding */
1402                 if(but->pointype==CHA)
1403                         value= (char)floor(value+0.5);
1404                 else if(but->pointype==SHO ) {
1405                         /* gcc 3.2.1 seems to have problems 
1406                          * casting a double like 32772.0 to
1407                          * a short so we cast to an int, then 
1408                          to a short */
1409                         int gcckludge;
1410                         gcckludge = (int) floor(value+0.5);
1411                         value= (short)gcckludge;
1412                 }
1413                 else if(but->pointype==INT )
1414                         value= (int)floor(value+0.5);
1415                 else if(but->pointype==FLO ) {
1416                         float fval= (float)value;
1417                         if(fval>= -0.00001f && fval<= 0.00001f) fval= 0.0f;     /* prevent negative zero */
1418                         value= fval;
1419                 }
1420                 
1421                 /* then set value with possible edit override */
1422                 if(but->editval)
1423                         *but->editval= value;
1424                 else if(but->pointype==CHA)
1425                         *((char *)but->poin)= (char)value;
1426                 else if(but->pointype==SHO)
1427                         *((short *)but->poin)= (short)value;
1428                 else if(but->pointype==INT)
1429                         *((int *)but->poin)= (int)value;
1430                 else if(but->pointype==FLO)
1431                         *((float *)but->poin)= (float)value;
1432         }
1433
1434         /* update select flag */
1435         ui_is_but_sel(but);
1436 }
1437
1438 int ui_get_but_string_max_length(uiBut *but)
1439 {
1440         if(ELEM(but->type, TEX, SEARCH_MENU))
1441                 return but->hardmax;
1442         else if(but->type == IDPOIN)
1443                 return sizeof(((ID*)NULL)->name)-2;
1444         else
1445                 return UI_MAX_DRAW_STR;
1446 }
1447
1448 static double ui_get_but_scale_unit(uiBut *but, double value)
1449 {
1450         Scene *scene= CTX_data_scene((bContext *)but->block->evil_C);
1451         int subtype= RNA_SUBTYPE_UNIT(RNA_property_subtype(but->rnaprop));
1452
1453         if(subtype == PROP_UNIT_LENGTH) {
1454                 return value * scene->unit.scale_length;
1455         }
1456         else if(subtype == PROP_UNIT_AREA) {
1457                 return value * pow(scene->unit.scale_length, 2);
1458         }
1459         else if(subtype == PROP_UNIT_VOLUME) {
1460                 return value * pow(scene->unit.scale_length, 3);
1461         }
1462         else if(subtype == PROP_UNIT_TIME) { /* WARNING - using evil_C :| */
1463                 return FRA2TIME(value);
1464         }
1465         else {
1466                 return value;
1467         }
1468 }
1469
1470 /* str will be overwritten */
1471 void ui_convert_to_unit_alt_name(uiBut *but, char *str, int maxlen)
1472 {
1473         if(ui_is_but_unit(but)) {
1474                 int unit_type= RNA_SUBTYPE_UNIT_VALUE(RNA_property_subtype(but->rnaprop));
1475                 char *orig_str;
1476                 Scene *scene= CTX_data_scene((bContext *)but->block->evil_C);
1477                 
1478                 orig_str= MEM_callocN(sizeof(char)*maxlen + 1, "textedit sub str");
1479                 memcpy(orig_str, str, maxlen);
1480                 
1481                 bUnit_ToUnitAltName(str, maxlen, orig_str, scene->unit.system, unit_type);
1482                 
1483                 MEM_freeN(orig_str);
1484         }
1485 }
1486
1487 static void ui_get_but_string_unit(uiBut *but, char *str, int len_max, double value, int pad)
1488 {
1489         Scene *scene= CTX_data_scene((bContext *)but->block->evil_C);
1490         int do_split= scene->unit.flag & USER_UNIT_OPT_SPLIT;
1491         int unit_type=  RNA_SUBTYPE_UNIT_VALUE(RNA_property_subtype(but->rnaprop));
1492         int precision= but->a2;
1493
1494         if(scene->unit.scale_length<0.0001) scene->unit.scale_length= 1.0; // XXX do_versions
1495
1496         /* Sanity checks */
1497         if(precision>4)         precision= 4;
1498         else if(precision==0)   precision= 2;
1499
1500         bUnit_AsString(str, len_max, ui_get_but_scale_unit(but, value), precision, scene->unit.system, unit_type, do_split, pad);
1501 }
1502
1503 static float ui_get_but_step_unit(uiBut *but, float step_default)
1504 {
1505         Scene *scene= CTX_data_scene((bContext *)but->block->evil_C);
1506         int unit_type=  RNA_SUBTYPE_UNIT_VALUE(RNA_property_subtype(but->rnaprop));
1507         float step;
1508
1509         step = bUnit_ClosestScalar(ui_get_but_scale_unit(but, step_default), scene->unit.system, unit_type);
1510
1511         if(step > 0.0) { /* -1 is an error value */
1512                 return (step/ui_get_but_scale_unit(but, 1.0))*100;
1513         }
1514         else {
1515                 return step_default;
1516         }
1517 }
1518
1519
1520 void ui_get_but_string(uiBut *but, char *str, int maxlen)
1521 {
1522         if(but->rnaprop && ELEM3(but->type, TEX, IDPOIN, SEARCH_MENU)) {
1523                 PropertyType type;
1524                 char *buf= NULL;
1525
1526                 type= RNA_property_type(but->rnaprop);
1527
1528                 if(type == PROP_STRING) {
1529                         /* RNA string */
1530                         buf= RNA_property_string_get_alloc(&but->rnapoin, but->rnaprop, str, maxlen);
1531                 }
1532                 else if(type == PROP_POINTER) {
1533                         /* RNA pointer */
1534                         PointerRNA ptr= RNA_property_pointer_get(&but->rnapoin, but->rnaprop);
1535                         buf= RNA_struct_name_get_alloc(&ptr, str, maxlen);
1536                 }
1537
1538                 if(!buf) {
1539                         str[0] = '\0';
1540                 }
1541                 else if(buf && buf != str) {
1542                         /* string was too long, we have to truncate */
1543                         BLI_strncpy(str, buf, maxlen);
1544                         MEM_freeN(buf);
1545                 }
1546         }
1547         else if(but->type == IDPOIN) {
1548                 /* ID pointer */
1549                 if(but->idpoin_idpp) { /* Can be NULL for ID properties by python */
1550                         ID *id= *(but->idpoin_idpp);
1551                         if(id) {
1552                                 BLI_strncpy(str, id->name+2, maxlen);
1553                                 return;
1554                         }
1555                 }
1556                 str[0] = '\0';
1557                 return;
1558         }
1559         else if(but->type == TEX) {
1560                 /* string */
1561                 BLI_strncpy(str, but->poin, maxlen);
1562                 return;
1563         }
1564         else if(but->type == SEARCH_MENU) {
1565                 /* string */
1566                 BLI_strncpy(str, but->poin, maxlen);
1567                 return;
1568         }
1569         else if(ui_but_anim_expression_get(but, str, maxlen))
1570                 ; /* driver expression */
1571         else {
1572                 /* number editing */
1573                 double value;
1574
1575                 value= ui_get_but_val(but);
1576
1577                 if(ui_is_but_float(but)) {
1578                         if(ui_is_but_unit(but)) {
1579                                 ui_get_but_string_unit(but, str, maxlen, value, 0);
1580                         }
1581                         else if(but->a2) { /* amount of digits defined */
1582                                 if(but->a2==1) BLI_snprintf(str, maxlen, "%.1f", value);
1583                                 else if(but->a2==2) BLI_snprintf(str, maxlen, "%.2f", value);
1584                                 else if(but->a2==3) BLI_snprintf(str, maxlen, "%.3f", value);
1585                                 else BLI_snprintf(str, maxlen, "%.4f", value);
1586                         }
1587                         else
1588                                 BLI_snprintf(str, maxlen, "%.3f", value);
1589                 }
1590                 else
1591                         BLI_snprintf(str, maxlen, "%d", (int)value);
1592         }
1593 }
1594
1595 int ui_set_but_string(bContext *C, uiBut *but, const char *str)
1596 {
1597         if(but->rnaprop && ELEM3(but->type, TEX, IDPOIN, SEARCH_MENU)) {
1598                 if(RNA_property_editable(&but->rnapoin, but->rnaprop)) {
1599                         PropertyType type;
1600
1601                         type= RNA_property_type(but->rnaprop);
1602
1603                         if(type == PROP_STRING) {
1604                                 /* RNA string */
1605                                 RNA_property_string_set(&but->rnapoin, but->rnaprop, str);
1606                                 return 1;
1607                         }
1608                         else if(type == PROP_POINTER) {
1609                                 /* RNA pointer */
1610                                 PointerRNA ptr, rptr;
1611                                 PropertyRNA *prop;
1612
1613                                 if(str == NULL || str[0] == '\0') {
1614                                         RNA_property_pointer_set(&but->rnapoin, but->rnaprop, PointerRNA_NULL);
1615                                         return 1;
1616                                 }
1617                                 else {
1618                                         ptr= but->rnasearchpoin;
1619                                         prop= but->rnasearchprop;
1620                                         
1621                                         if(prop && RNA_property_collection_lookup_string(&ptr, prop, str, &rptr))
1622                                                 RNA_property_pointer_set(&but->rnapoin, but->rnaprop, rptr);
1623
1624                                         return 1;
1625                                 }
1626
1627                                 return 0;
1628                         }
1629                 }
1630         }
1631         else if(but->type == IDPOIN) {
1632                 /* ID pointer */
1633                 but->idpoin_func(C, (char*)str, but->idpoin_idpp);
1634                 return 1;
1635         }
1636         else if(but->type == TEX) {
1637                 /* string */
1638                 BLI_strncpy(but->poin, str, but->hardmax);
1639                 return 1;
1640         }
1641         else if(but->type == SEARCH_MENU) {
1642                 /* string */
1643                 BLI_strncpy(but->poin, str, but->hardmax);
1644                 return 1;
1645         }
1646         else if(ui_but_anim_expression_set(but, str)) {
1647                 /* driver expression */
1648                 return 1;
1649         }
1650         else {
1651                 /* number editing */
1652                 double value;
1653
1654 #ifdef WITH_PYTHON
1655                 {
1656                         char str_unit_convert[256];
1657                         int unit_type;
1658                         Scene *scene= CTX_data_scene((bContext *)but->block->evil_C);
1659
1660                         if(but->rnaprop)
1661                                 unit_type= RNA_SUBTYPE_UNIT_VALUE(RNA_property_subtype(but->rnaprop));
1662                         else
1663                                 unit_type= 0;
1664
1665                         BLI_strncpy(str_unit_convert, str, sizeof(str_unit_convert));
1666
1667                         if(ui_is_but_unit(but)) {
1668                                 /* ugly, use the draw string to get the value, this could cause problems if it includes some text which resolves to a unit */
1669                                 bUnit_ReplaceString(str_unit_convert, sizeof(str_unit_convert), but->drawstr, ui_get_but_scale_unit(but, 1.0), scene->unit.system, unit_type);
1670                         }
1671
1672                         if(BPY_eval_button(C, str_unit_convert, &value)) {
1673                                 value = ui_get_but_val(but); /* use its original value */
1674
1675                                 if(str[0])
1676                                         return 0;
1677                         }
1678                 }
1679 #else
1680                 value= atof(str);
1681 #endif // WITH_PYTHON
1682
1683                 if(!ui_is_but_float(but)) value= (int)floor(value + 0.5);
1684                 if(but->type==NUMABS) value= fabs(value);
1685
1686                 /* not that we use hard limits here */
1687                 if(value<but->hardmin) value= but->hardmin;
1688                 if(value>but->hardmax) value= but->hardmax;
1689
1690                 ui_set_but_val(but, value);
1691                 return 1;
1692         }
1693
1694         return 0;
1695 }
1696
1697 void ui_set_but_default(bContext *C, uiBut *but)
1698 {
1699         /* if there is a valid property that is editable... */
1700         if (but->rnapoin.data && but->rnaprop && RNA_property_editable(&but->rnapoin, but->rnaprop)) {
1701                 if(RNA_property_reset(&but->rnapoin, but->rnaprop, -1)) {
1702                         /* perform updates required for this property */
1703                         RNA_property_update(C, &but->rnapoin, but->rnaprop);
1704                 }
1705         }
1706 }
1707
1708 static double soft_range_round_up(double value, double max)
1709 {
1710         /* round up to .., 0.1, 0.2, 0.5, 1, 2, 5, 10, 20, 50, .. */
1711         double newmax= pow(10.0, ceil(log(value)/log(10.0)));
1712
1713         if(newmax*0.2 >= max && newmax*0.2 >= value)
1714                 return newmax*0.2;
1715         else if(newmax*0.5 >= max && newmax*0.5 >= value)
1716                 return newmax*0.5;
1717         else
1718                 return newmax;
1719 }
1720
1721 static double soft_range_round_down(double value, double max)
1722 {
1723         /* round down to .., 0.1, 0.2, 0.5, 1, 2, 5, 10, 20, 50, .. */
1724         double newmax= pow(10.0, floor(log(value)/log(10.0)));
1725
1726         if(newmax*5.0 <= max && newmax*5.0 <= value)
1727                 return newmax*5.0;
1728         else if(newmax*2.0 <= max && newmax*2.0 <= value)
1729                 return newmax*2.0;
1730         else
1731                 return newmax;
1732 }
1733
1734 void ui_set_but_soft_range(uiBut *but, double value)
1735 {
1736         PropertyType type;
1737         double softmin, softmax, step, precision;
1738         
1739         if(but->rnaprop) {
1740                 type= RNA_property_type(but->rnaprop);
1741
1742                 /* clamp button range to something reasonable in case
1743                  * we get -inf/inf from RNA properties */
1744                 if(type == PROP_INT) {
1745                         int imin, imax, istep;
1746
1747                         RNA_property_int_ui_range(&but->rnapoin, but->rnaprop, &imin, &imax, &istep);
1748                         softmin= (imin == INT_MIN)? -1e4: imin;
1749                         softmax= (imin == INT_MAX)? 1e4: imax;
1750                         step= istep;
1751                         precision= 1;
1752                 }
1753                 else if(type == PROP_FLOAT) {
1754                         float fmin, fmax, fstep, fprecision;
1755
1756                         RNA_property_float_ui_range(&but->rnapoin, but->rnaprop, &fmin, &fmax, &fstep, &fprecision);
1757                         softmin= (fmin == -FLT_MAX)? -1e4: fmin;
1758                         softmax= (fmax == FLT_MAX)? 1e4: fmax;
1759                         step= fstep;
1760                         precision= fprecision;
1761                 }
1762                 else
1763                         return;
1764
1765                 /* if the value goes out of the soft/max range, adapt the range */
1766                 if(value+1e-10 < softmin) {
1767                         if(value < 0.0)
1768                                 softmin= -soft_range_round_up(-value, -softmin);
1769                         else
1770                                 softmin= soft_range_round_down(value, softmin);
1771
1772                         if(softmin < but->hardmin)
1773                                 softmin= but->hardmin;
1774                 }
1775                 else if(value-1e-10 > softmax) {
1776                         if(value < 0.0)
1777                                 softmax= -soft_range_round_down(-value, -softmax);
1778                         else
1779                                 softmax= soft_range_round_up(value, softmax);
1780
1781                         if(softmax > but->hardmax)
1782                                 softmax= but->hardmax;
1783                 }
1784
1785                 but->softmin= softmin;
1786                 but->softmax= softmax;
1787         }
1788 }
1789
1790 /* ******************* Free ********************/
1791
1792 static void ui_free_link(uiLink *link)
1793 {
1794         if(link) {      
1795                 BLI_freelistN(&link->lines);
1796                 MEM_freeN(link);
1797         }
1798 }
1799
1800 /* can be called with C==NULL */
1801 static void ui_free_but(const bContext *C, uiBut *but)
1802 {
1803         if(but->opptr) {
1804                 WM_operator_properties_free(but->opptr);
1805                 MEM_freeN(but->opptr);
1806         }
1807         if(but->func_argN) MEM_freeN(but->func_argN);
1808         if(but->active) {
1809                 /* XXX solve later, buttons should be free-able without context ideally,
1810                    however they may have open tooltips or popup windows, which need to
1811                    be closed using a context pointer */
1812                 if(C) 
1813                         ui_button_active_free(C, but);
1814                 else
1815                         if(but->active) 
1816                                 MEM_freeN(but->active);
1817         }
1818         if(but->str && but->str != but->strdata) MEM_freeN(but->str);
1819         ui_free_link(but->link);
1820
1821         MEM_freeN(but);
1822 }
1823
1824 /* can be called with C==NULL */
1825 void uiFreeBlock(const bContext *C, uiBlock *block)
1826 {
1827         uiBut *but;
1828
1829         while( (but= block->buttons.first) ) {
1830                 BLI_remlink(&block->buttons, but);      
1831                 ui_free_but(C, but);
1832         }
1833
1834         if(block->func_argN)
1835                 MEM_freeN(block->func_argN);
1836
1837         CTX_store_free_list(&block->contexts);
1838
1839         BLI_freelistN(&block->saferct);
1840         
1841         MEM_freeN(block);
1842 }
1843
1844 /* can be called with C==NULL */
1845 void uiFreeBlocks(const bContext *C, ListBase *lb)
1846 {
1847         uiBlock *block;
1848         
1849         while( (block= lb->first) ) {
1850                 BLI_remlink(lb, block);
1851                 uiFreeBlock(C, block);
1852         }
1853 }
1854
1855 void uiFreeInactiveBlocks(const bContext *C, ListBase *lb)
1856 {
1857         uiBlock *block, *nextblock;
1858
1859         for(block=lb->first; block; block=nextblock) {
1860                 nextblock= block->next;
1861         
1862                 if(!block->handle) {
1863                         if(!block->active) {
1864                                 BLI_remlink(lb, block);
1865                                 uiFreeBlock(C, block);
1866                         }
1867                         else
1868                                 block->active= 0;
1869                 }
1870         }
1871 }
1872
1873 void uiBlockSetRegion(uiBlock *block, ARegion *region)
1874 {
1875         ListBase *lb;
1876         uiBlock *oldblock= NULL;
1877
1878         lb= &region->uiblocks;
1879         
1880         /* each listbase only has one block with this name, free block
1881          * if is already there so it can be rebuilt from scratch */
1882         if(lb) {
1883                 for (oldblock= lb->first; oldblock; oldblock= oldblock->next)
1884                         if (BLI_streq(oldblock->name, block->name))
1885                                 break;
1886
1887                 if (oldblock) {
1888                         oldblock->active= 0;
1889                         oldblock->panel= NULL;
1890                 }
1891         }
1892
1893         block->oldblock= oldblock;
1894
1895         /* at the beginning of the list! for dynamical menus/blocks */
1896         if(lb)
1897                 BLI_addhead(lb, block);
1898 }
1899
1900 uiBlock *uiBeginBlock(const bContext *C, ARegion *region, const char *name, short dt)
1901 {
1902         uiBlock *block;
1903         wmWindow *window;
1904         Scene *scn;
1905         int getsizex, getsizey;
1906
1907         window= CTX_wm_window(C);
1908         scn = CTX_data_scene(C);
1909
1910         block= MEM_callocN(sizeof(uiBlock), "uiBlock");
1911         block->active= 1;
1912         block->dt= dt;
1913         block->evil_C= (void*)C; // XXX
1914         if (scn) block->color_profile= (scn->r.color_mgt_flag & R_COLOR_MANAGEMENT);
1915         BLI_strncpy(block->name, name, sizeof(block->name));
1916
1917         if(region)
1918                 uiBlockSetRegion(block, region);
1919
1920         /* window matrix and aspect */
1921         if(region && region->swinid) {
1922                 wm_subwindow_getmatrix(window, region->swinid, block->winmat);
1923                 wm_subwindow_getsize(window, region->swinid, &getsizex, &getsizey);
1924
1925                 /* TODO - investigate why block->winmat[0][0] is negative
1926                  * in the image view when viewRedrawForce is called */
1927                 block->aspect= 2.0/fabs( (getsizex)*block->winmat[0][0]);
1928         }
1929         else {
1930                 /* no subwindow created yet, for menus for example, so we
1931                  * use the main window instead, since buttons are created
1932                  * there anyway */
1933                 wm_subwindow_getmatrix(window, window->screen->mainwin, block->winmat);
1934                 wm_subwindow_getsize(window, window->screen->mainwin, &getsizex, &getsizey);
1935
1936                 block->aspect= 2.0/fabs(getsizex*block->winmat[0][0]);
1937                 block->auto_open= 2;
1938                 block->flag |= UI_BLOCK_LOOP; /* tag as menu */
1939         }
1940
1941         return block;
1942 }
1943
1944 uiBlock *uiGetBlock(const char *name, ARegion *ar)
1945 {
1946         uiBlock *block= ar->uiblocks.first;
1947         
1948         while(block) {
1949                 if( strcmp(name, block->name)==0 ) return block;
1950                 block= block->next;
1951         }
1952         
1953         return NULL;
1954 }
1955
1956 void uiBlockSetEmboss(uiBlock *block, char dt)
1957 {
1958         block->dt= dt;
1959 }
1960
1961 void ui_check_but(uiBut *but)
1962 {
1963         /* if something changed in the button */
1964         double value;
1965         float okwidth;
1966 //      int transopts= ui_translate_buttons();
1967         
1968         ui_is_but_sel(but);
1969         
1970 //      if(but->type==TEX || but->type==IDPOIN) transopts= 0;
1971
1972         /* only update soft range while not editing */
1973         if(but->rnaprop && !(but->editval || but->editstr || but->editvec))
1974                 ui_set_but_soft_range(but, ui_get_but_val(but));
1975
1976         /* test for min and max, icon sliders, etc */
1977         switch( but->type ) {
1978                 case NUM:
1979                 case SLI:
1980                 case SCROLL:
1981                 case NUMSLI:
1982                 case HSVSLI:
1983                         value= ui_get_but_val(but);
1984                         if(value < but->hardmin) ui_set_but_val(but, but->hardmin);
1985                         else if(value > but->hardmax) ui_set_but_val(but, but->hardmax);
1986                         break;
1987                         
1988                 case NUMABS:
1989                         value= fabs( ui_get_but_val(but) );
1990                         if(value < but->hardmin) ui_set_but_val(but, but->hardmin);
1991                         else if(value > but->hardmax) ui_set_but_val(but, but->hardmax);
1992                         break;
1993                         
1994                 case ICONTOG: 
1995                 case ICONTOGN:
1996                         if(!but->rnaprop || (RNA_property_flag(but->rnaprop) & PROP_ICONS_CONSECUTIVE)) {
1997                                 if(but->flag & UI_SELECT) but->iconadd= 1;
1998                                 else but->iconadd= 0;
1999                         }
2000                         break;
2001                         
2002                 case ICONROW:
2003                         if(!but->rnaprop || (RNA_property_flag(but->rnaprop) & PROP_ICONS_CONSECUTIVE)) {
2004                                 value= ui_get_but_val(but);
2005                                 but->iconadd= (int)value- (int)(but->hardmin);
2006                         }
2007                         break;
2008                         
2009                 case ICONTEXTROW:
2010                         if(!but->rnaprop || (RNA_property_flag(but->rnaprop) & PROP_ICONS_CONSECUTIVE)) {
2011                                 value= ui_get_but_val(but);
2012                                 but->iconadd= (int)value- (int)(but->hardmin);
2013                         }
2014                         break;
2015         }
2016         
2017         
2018         /* safety is 4 to enable small number buttons (like 'users') */
2019         okwidth= -4 + (but->x2 - but->x1); 
2020         
2021         /* name: */
2022         switch( but->type ) {
2023         
2024         case MENU:
2025         case ICONTEXTROW:
2026                 
2027                 if(but->x2 - but->x1 > 24) {
2028                         value= ui_get_but_val(but);
2029                         ui_set_name_menu(but, (int)value);
2030                 }
2031                 break;
2032         
2033         case NUM:
2034         case NUMSLI:
2035         case HSVSLI:
2036         case NUMABS:
2037
2038                 value= ui_get_but_val(but);
2039
2040                 if(ui_is_but_float(but)) {
2041                         if(value == FLT_MAX) sprintf(but->drawstr, "%sinf", but->str);
2042                         else if(value == -FLT_MAX) sprintf(but->drawstr, "%s-inf", but->str);
2043                         /* support length type buttons */
2044                         else if(ui_is_but_unit(but)) {
2045                                 char new_str[sizeof(but->drawstr)];
2046                                 ui_get_but_string_unit(but, new_str, sizeof(new_str), value, TRUE);
2047                                 BLI_snprintf(but->drawstr, sizeof(but->drawstr), "%s%s", but->str, new_str);
2048                         }
2049                         else if(but->a2) { /* amount of digits defined */
2050                                 if(but->a2==1) sprintf(but->drawstr, "%s%.1f", but->str, value);
2051                                 else if(but->a2==2) sprintf(but->drawstr, "%s%.2f", but->str, value);
2052                                 else if(but->a2==3) sprintf(but->drawstr, "%s%.3f", but->str, value);
2053                                 else sprintf(but->drawstr, "%s%.4f", but->str, value);
2054                         }
2055                         else {
2056                                 if(but->hardmax<10.001) sprintf(but->drawstr, "%s%.3f", but->str, value);
2057                                 else sprintf(but->drawstr, "%s%.2f", but->str, value);
2058                         }
2059                 }
2060                 else {
2061                         sprintf(but->drawstr, "%s%d", but->str, (int)value);
2062                 }
2063                         
2064                 if(but->rnaprop) {
2065                         PropertySubType pstype = RNA_property_subtype(but->rnaprop);
2066                         
2067                         if (pstype == PROP_PERCENTAGE)
2068                                 strcat(but->drawstr, "%");
2069                 }
2070                 break;
2071
2072         case LABEL:
2073                 if(ui_is_but_float(but)) {
2074                         value= ui_get_but_val(but);
2075                         if(but->a2) { /* amount of digits defined */
2076                                 if(but->a2==1) sprintf(but->drawstr, "%s%.1f", but->str, value);
2077                                 else if(but->a2==2) sprintf(but->drawstr, "%s%.2f", but->str, value);
2078                                 else if(but->a2==3) sprintf(but->drawstr, "%s%.3f", but->str, value);
2079                                 else sprintf(but->drawstr, "%s%.4f", but->str, value);
2080                         }
2081                         else {
2082                                 sprintf(but->drawstr, "%s%.2f", but->str, value);
2083                         }
2084                 }
2085                 else strncpy(but->drawstr, but->str, UI_MAX_DRAW_STR);
2086                 
2087                 break;
2088
2089         case IDPOIN:
2090         case TEX:
2091         case SEARCH_MENU:
2092                 if(!but->editstr) {
2093                         char str[UI_MAX_DRAW_STR];
2094
2095                         ui_get_but_string(but, str, UI_MAX_DRAW_STR-strlen(but->str));
2096
2097                         strcpy(but->drawstr, but->str);
2098                         strcat(but->drawstr, str);
2099                 }
2100                 break;
2101         
2102         case KEYEVT:
2103                 strncpy(but->drawstr, but->str, UI_MAX_DRAW_STR);
2104                 if (but->flag & UI_SELECT) {
2105                         strcat(but->drawstr, "Press a key");
2106                 } else {
2107                         strcat(but->drawstr, WM_key_event_string((short) ui_get_but_val(but)));
2108                 }
2109                 break;
2110                 
2111         case HOTKEYEVT:
2112                 if (but->flag & UI_SELECT) {
2113                         but->drawstr[0]= '\0';
2114                         
2115                         if(but->modifier_key) {
2116                                 char *str= but->drawstr;
2117                                 
2118                                 if(but->modifier_key & KM_SHIFT)
2119                                         str= strcat(str, "Shift ");
2120                                 if(but->modifier_key & KM_CTRL)
2121                                         str= strcat(str, "Ctrl ");
2122                                 if(but->modifier_key & KM_ALT)
2123                                         str= strcat(str, "Alt ");
2124                                 if(but->modifier_key & KM_OSKEY)
2125                                         str= strcat(str, "Cmd ");
2126                         }
2127                         else
2128                                 strcat(but->drawstr, "Press a key  ");
2129                 }
2130                 else
2131                         strncpy(but->drawstr, but->str, UI_MAX_DRAW_STR);
2132
2133                 break;
2134                 
2135         case BUT_TOGDUAL:
2136                 /* trying to get the dual-icon to left of text... not very nice */
2137                 if(but->str[0]) {
2138                         strncpy(but->drawstr, "  ", UI_MAX_DRAW_STR);
2139                         strncpy(but->drawstr+2, but->str, UI_MAX_DRAW_STR-2);
2140                 }
2141                 break;
2142
2143         case HSVCUBE:
2144         case HSVCIRCLE:
2145                 break;
2146         default:
2147                 strncpy(but->drawstr, but->str, UI_MAX_DRAW_STR);
2148                 
2149         }
2150
2151         /* if we are doing text editing, this will override the drawstr */
2152         if(but->editstr)
2153                 strncpy(but->drawstr, but->editstr, UI_MAX_DRAW_STR);
2154         
2155         /* text clipping moved to widget drawing code itself */
2156 }
2157
2158
2159 void uiBlockBeginAlign(uiBlock *block)
2160 {
2161         /* if other align was active, end it */
2162         if(block->flag & UI_BUT_ALIGN) uiBlockEndAlign(block);
2163
2164         block->flag |= UI_BUT_ALIGN_DOWN;       
2165         block->alignnr++;
2166
2167         /* buttons declared after this call will get this align nr */ // XXX flag?
2168 }
2169
2170 static int buts_are_horiz(uiBut *but1, uiBut *but2)
2171 {
2172         float dx, dy;
2173         
2174         dx= fabs( but1->x2 - but2->x1);
2175         dy= fabs( but1->y1 - but2->y2);
2176         
2177         if(dx > dy) return 0;
2178         return 1;
2179 }
2180
2181 void uiBlockEndAlign(uiBlock *block)
2182 {
2183         block->flag &= ~UI_BUT_ALIGN;   // all 4 flags
2184 }
2185
2186 int ui_but_can_align(uiBut *but)
2187 {
2188         return !ELEM3(but->type, LABEL, OPTION, OPTIONN);
2189 }
2190
2191 static void ui_block_do_align_but(uiBut *first, int nr)
2192 {
2193         uiBut *prev, *but=NULL, *next;
2194         int flag= 0, cols=0, rows=0;
2195         
2196         /* auto align */
2197
2198         for(but=first; but && but->alignnr == nr; but=but->next) {
2199                 if(but->next && but->next->alignnr == nr) {
2200                         if(buts_are_horiz(but, but->next)) cols++;
2201                         else rows++;
2202                 }
2203         }
2204
2205         /* rows==0: 1 row, cols==0: 1 collumn */
2206         
2207         /* note;  how it uses 'flag' in loop below (either set it, or OR it) is confusing */
2208         for(but=first, prev=NULL; but && but->alignnr == nr; prev=but, but=but->next) {
2209                 next= but->next;
2210                 if(next && next->alignnr != nr)
2211                         next= NULL;
2212
2213                 /* clear old flag */
2214                 but->flag &= ~UI_BUT_ALIGN;
2215                 
2216                 if(flag==0) {   /* first case */
2217                         if(next) {
2218                                 if(buts_are_horiz(but, next)) {
2219                                         if(rows==0)
2220                                                 flag= UI_BUT_ALIGN_RIGHT;
2221                                         else 
2222                                                 flag= UI_BUT_ALIGN_DOWN|UI_BUT_ALIGN_RIGHT;
2223                                 }
2224                                 else {
2225                                         flag= UI_BUT_ALIGN_DOWN;
2226                                 }
2227                         }
2228                 }
2229                 else if(next==NULL) {   /* last case */
2230                         if(prev) {
2231                                 if(buts_are_horiz(prev, but)) {
2232                                         if(rows==0) 
2233                                                 flag= UI_BUT_ALIGN_LEFT;
2234                                         else
2235                                                 flag= UI_BUT_ALIGN_TOP|UI_BUT_ALIGN_LEFT;
2236                                 }
2237                                 else flag= UI_BUT_ALIGN_TOP;
2238                         }
2239                 }
2240                 else if(buts_are_horiz(but, next)) {
2241                         /* check if this is already second row */
2242                         if( prev && buts_are_horiz(prev, but)==0) {
2243                                 flag &= ~UI_BUT_ALIGN_LEFT;
2244                                 flag |= UI_BUT_ALIGN_TOP;
2245                                 /* exception case: bottom row */
2246                                 if(rows>0) {
2247                                         uiBut *bt= but;
2248                                         while(bt && bt->alignnr == nr) {
2249                                                 if(bt->next && bt->next->alignnr == nr && buts_are_horiz(bt, bt->next)==0 ) break; 
2250                                                 bt= bt->next;
2251                                         }
2252                                         if(bt==0 || bt->alignnr != nr) flag= UI_BUT_ALIGN_TOP|UI_BUT_ALIGN_RIGHT;
2253                                 }
2254                         }
2255                         else flag |= UI_BUT_ALIGN_LEFT;
2256                 }
2257                 else {
2258                         if(cols==0) {
2259                                 flag |= UI_BUT_ALIGN_TOP;
2260                         }
2261                         else {  /* next button switches to new row */
2262                                 
2263                                 if(prev && buts_are_horiz(prev, but))
2264                                    flag |= UI_BUT_ALIGN_LEFT;
2265                                 
2266                                 if( (flag & UI_BUT_ALIGN_TOP)==0) {     /* stil top row */
2267                                         if(prev) {
2268                                                 if(next && buts_are_horiz(next, but))
2269                                                         flag = UI_BUT_ALIGN_DOWN|UI_BUT_ALIGN_LEFT;
2270                                                 else {
2271                                                         flag = UI_BUT_ALIGN_DOWN|UI_BUT_ALIGN_LEFT|UI_BUT_ALIGN_RIGHT;
2272                                                 }
2273                                         }
2274                                         else 
2275                                                 flag |= UI_BUT_ALIGN_DOWN;
2276                                 }
2277                                 else 
2278                                         flag |= UI_BUT_ALIGN_TOP;
2279                         }
2280                 }
2281                 
2282                 but->flag |= flag;
2283                 
2284                 /* merge coordinates */
2285                 if(prev) {
2286                         // simple cases 
2287                         if(rows==0) {
2288                                 but->x1= (prev->x2+but->x1)/2.0;
2289                                 prev->x2= but->x1;
2290                         }
2291                         else if(cols==0) {
2292                                 but->y2= (prev->y1+but->y2)/2.0;
2293                                 prev->y1= but->y2;
2294                         }
2295                         else {
2296                                 if(buts_are_horiz(prev, but)) {
2297                                         but->x1= (prev->x2+but->x1)/2.0;
2298                                         prev->x2= but->x1;
2299                                         /* copy height too */
2300                                         but->y2= prev->y2;
2301                                 }
2302                                 else if(prev->prev && buts_are_horiz(prev->prev, prev)==0) {
2303                                         /* the previous button is a single one in its row */
2304                                         but->y2= (prev->y1+but->y2)/2.0;
2305                                         prev->y1= but->y2;
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, short x1, short y1, short x2, short y2, void *poin, float min, float max, float a1, float a2, const char *tip)
2348 {
2349         uiBut *but;
2350         short 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         if( strlen(str)>=UI_MAX_NAME_STR-1 ) {
2368                 but->str= MEM_callocN( strlen(str)+2, "uiDefBut");
2369                 strcpy(but->str, str);
2370         }
2371         else {
2372                 but->str= but->strdata;
2373                 strcpy(but->str, str);
2374         }
2375         but->x1= x1; 
2376         but->y1= y1;
2377         but->x2= (x1+x2); 
2378         but->y2= (y1+y2);
2379         
2380         but->poin= poin;
2381         but->hardmin= but->softmin= min; 
2382         but->hardmax= but->softmax= max;
2383         but->a1= a1; 
2384         but->a2= a2;
2385         but->tip= tip;
2386         
2387         but->lock= block->lock;
2388         but->lockstr= block->lockstr;
2389         but->dt= block->dt;
2390
2391         but->aspect= 1.0f; //XXX block->aspect;
2392         but->block= block;              // pointer back, used for frontbuffer status, and picker
2393
2394         if((block->flag & UI_BUT_ALIGN) && ui_but_can_align(but))
2395                 but->alignnr= block->alignnr;
2396
2397         but->func= block->func;
2398         but->func_arg1= block->func_arg1;
2399         but->func_arg2= block->func_arg2;
2400
2401         but->funcN= block->funcN;
2402         if(block->func_argN)
2403                 but->func_argN= MEM_dupallocN(block->func_argN);
2404         
2405         but->pos= -1;   /* cursor invisible */
2406
2407         if(ELEM4(but->type, NUM, NUMABS, NUMSLI, HSVSLI)) {     /* add a space to name */
2408                 slen= strlen(but->str);
2409                 if(slen>0 && slen<UI_MAX_NAME_STR-2) {
2410                         if(but->str[slen-1]!=' ') {
2411                                 but->str[slen]= ' ';
2412                                 but->str[slen+1]= 0;
2413                         }
2414                 }
2415         }
2416
2417         if((block->flag & UI_BLOCK_LOOP) || ELEM8(but->type, MENU, TEX, LABEL, IDPOIN, BLOCK, BUTM, SEARCH_MENU, PROGRESSBAR))
2418                 but->flag |= (UI_TEXT_LEFT|UI_ICON_LEFT);
2419         else if(but->type==BUT_TOGDUAL)
2420                 but->flag |= UI_ICON_LEFT;
2421
2422         but->flag |= (block->flag & UI_BUT_ALIGN);
2423
2424         if (but->lock) {
2425                 if (but->lockstr) {
2426                         but->flag |= UI_BUT_DISABLED;
2427                 }
2428         }
2429
2430         if(ELEM8(but->type, BLOCK, BUT, LABEL, PULLDOWN, ROUNDBOX, LISTBOX, SEARCH_MENU, BUTM));
2431         else if(ELEM5(but->type, SCROLL, SEPR, LINK, INLINK, FTPREVIEW));
2432         else but->flag |= UI_BUT_UNDO;
2433
2434         BLI_addtail(&block->buttons, but);
2435         
2436         if(block->curlayout)
2437                 ui_layout_add_but(block->curlayout, but);
2438
2439         return but;
2440 }
2441
2442 static uiBut *ui_def_but_rna(uiBlock *block, int type, int retval, const char *str, short x1, short y1, short x2, short y2, PointerRNA *ptr, const char *propname, int index, float min, float max, float a1, float a2,  char *tip)
2443 {
2444         uiBut *but;
2445         PropertyRNA *prop;
2446         PropertyType proptype;
2447         int freestr= 0, icon= 0;
2448
2449         prop= RNA_struct_find_property(ptr, propname);
2450
2451         if(prop) {
2452                 proptype= RNA_property_type(prop);
2453
2454                 /* use rna values if parameters are not specified */
2455                 if(!str) {
2456                         if(type == MENU && proptype == PROP_ENUM) {
2457                                 EnumPropertyItem *item;
2458                                 DynStr *dynstr;
2459                                 int i, totitem, value, free;
2460
2461                                 RNA_property_enum_items(block->evil_C, ptr, prop, &item, &totitem, &free);
2462                                 value= RNA_property_enum_get(ptr, prop);
2463
2464                                 dynstr= BLI_dynstr_new();
2465                                 BLI_dynstr_appendf(dynstr, "%s%%t", RNA_property_ui_name(prop));
2466                                 for(i=0; i<totitem; i++) {
2467                                         if(!item[i].identifier[0]) {
2468                                                 if(item[i].name)
2469                                                         BLI_dynstr_appendf(dynstr, "|%s%%l", item[i].name);
2470                                                 else
2471                                                         BLI_dynstr_append(dynstr, "|%l");
2472                                         }
2473                                         else if(item[i].icon)
2474                                                 BLI_dynstr_appendf(dynstr, "|%s %%i%d %%x%d", item[i].name, item[i].icon, item[i].value);
2475                                         else
2476                                                 BLI_dynstr_appendf(dynstr, "|%s %%x%d", item[i].name, item[i].value);
2477
2478                                         if(value == item[i].value)
2479                                                 icon= item[i].icon;
2480                                 }
2481                                 str= BLI_dynstr_get_cstring(dynstr);
2482                                 BLI_dynstr_free(dynstr);
2483
2484                                 if(free)
2485                                         MEM_freeN(item);
2486
2487                                 freestr= 1;
2488                         }
2489                         else if(ELEM(type, ROW, LISTROW) && proptype == PROP_ENUM) {
2490                                 EnumPropertyItem *item;
2491                                 int i, totitem, free;
2492
2493                                 RNA_property_enum_items(block->evil_C, ptr, prop, &item, &totitem, &free);
2494                                 for(i=0; i<totitem; i++) {
2495                                         if(item[i].identifier[0] && item[i].value == (int)max) {
2496                                                 str= (char*)item[i].name;
2497                                                 icon= item[i].icon;
2498                                         }
2499                                 }
2500
2501                                 if(!str)
2502                                         str= (char*)RNA_property_ui_name(prop);
2503                                 if(free)
2504                                         MEM_freeN(item);
2505                         }
2506                         else {
2507                                 str= (char*)RNA_property_ui_name(prop);
2508                                 icon= RNA_property_ui_icon(prop);
2509                         }
2510                 }
2511
2512                 if(!tip) {
2513                         if(type == ROW && proptype == PROP_ENUM) {
2514                                 EnumPropertyItem *item;
2515                                 int i, totitem, free;
2516
2517                                 RNA_property_enum_items(block->evil_C, ptr, prop, &item, &totitem, &free);
2518
2519                                 for(i=0; i<totitem; i++) {
2520                                         if(item[i].identifier[0] && item[i].value == (int)max) {
2521                                                 if(item[i].description[0])
2522                                                         tip= (char*)item[i].description;
2523                                                 break;
2524                                         }
2525                                 }
2526
2527                                 if(free)
2528                                         MEM_freeN(item);
2529                         }
2530                 }
2531                 
2532                 if(!tip)
2533                         tip= (char*)RNA_property_ui_description(prop);
2534
2535                 if(min == max || a1 == -1 || a2 == -1) {
2536                         if(proptype == PROP_INT) {
2537                                 int hardmin, hardmax, softmin, softmax, step;
2538
2539                                 RNA_property_int_range(ptr, prop, &hardmin, &hardmax);
2540                                 RNA_property_int_ui_range(ptr, prop, &softmin, &softmax, &step);
2541
2542                                 if(!ELEM(type, ROW, LISTROW) && min == max) {
2543                                         min= hardmin;
2544                                         max= hardmax;
2545                                 }
2546                                 if(a1 == -1)
2547                                         a1= step;
2548                                 if(a2 == -1)
2549                                         a2= 0;
2550                         }
2551                         else if(proptype == PROP_FLOAT) {
2552                                 float hardmin, hardmax, softmin, softmax, step, precision;
2553
2554                                 RNA_property_float_range(ptr, prop, &hardmin, &hardmax);
2555                                 RNA_property_float_ui_range(ptr, prop, &softmin, &softmax, &step, &precision);
2556
2557                                 if(!ELEM(type, ROW, LISTROW) && min == max) {
2558                                         min= hardmin;
2559                                         max= hardmax;
2560                                 }
2561                                 if(a1 == -1)
2562                                         a1= step;
2563                                 if(a2 == -1)
2564                                         a2= precision;
2565                         }
2566                         else if(proptype == PROP_STRING) {
2567                                 min= 0;
2568                                 max= RNA_property_string_maxlength(prop);
2569                                 if(max == 0) /* interface code should ideally support unlimited length */
2570                                         max= UI_MAX_DRAW_STR; 
2571                         }
2572                 }
2573         }
2574         else {
2575                 printf("ui_def_but_rna: property not found: %s.%s\n", RNA_struct_identifier(ptr->type), propname);
2576                 str= (char*)propname;
2577         }
2578
2579         /* now create button */
2580         but= ui_def_but(block, type, retval, str, x1, y1, x2, y2, NULL, min, max, a1, a2, tip);
2581
2582         if(prop) {
2583                 but->rnapoin= *ptr;
2584                 but->rnaprop= prop;
2585
2586                 if(RNA_property_array_length(&but->rnapoin, but->rnaprop))
2587                         but->rnaindex= index;
2588                 else
2589                         but->rnaindex= 0;
2590         }
2591
2592         if(icon) {
2593                 but->icon= (BIFIconID)icon;
2594                 but->flag |= UI_HAS_ICON;
2595                 but->flag|= UI_ICON_LEFT;
2596         }
2597         
2598         if (!prop || !RNA_property_editable(&but->rnapoin, prop)) {
2599                 but->flag |= UI_BUT_DISABLED;
2600                 but->lock = 1;
2601                 but->lockstr = "";
2602         }
2603
2604         /* If this button uses units, calculate the step from this */
2605         if(ui_is_but_unit(but))
2606                 but->a1= ui_get_but_step_unit(but, but->a1);
2607
2608         if(freestr)
2609                 MEM_freeN((void *)str);
2610         
2611         return but;
2612 }
2613
2614 static uiBut *ui_def_but_operator(uiBlock *block, int type, const char *opname, int opcontext, const char *str, short x1, short y1, short x2, short y2, const char *tip)
2615 {
2616         uiBut *but;
2617         wmOperatorType *ot;
2618         
2619         ot= WM_operatortype_find(opname, 0);
2620
2621         if(!str) {
2622                 if(ot) str= ot->name;
2623                 else str= opname;
2624         }
2625         
2626         if ((!tip || tip[0]=='\0') && ot && ot->description) {
2627                 tip= ot->description;
2628         }
2629
2630         but= ui_def_but(block, type, -1, str, x1, y1, x2, y2, NULL, 0, 0, 0, 0, tip);
2631         but->optype= ot;
2632         but->opcontext= opcontext;
2633
2634         if(!ot) {
2635                 but->flag |= UI_BUT_DISABLED;
2636                 but->lock = 1;
2637                 but->lockstr = "";
2638         }
2639
2640         return but;
2641 }
2642
2643 static uiBut *ui_def_but_operator_text(uiBlock *block, int type, const char *opname, int opcontext, const char *str, short x1, short y1, short x2, short y2, void *poin, float min, float max, float a1, float a2, const char *tip)
2644 {
2645         uiBut *but;
2646         wmOperatorType *ot;
2647         
2648         ot= WM_operatortype_find(opname, 0);
2649
2650         if(!str) {
2651                 if(ot) str= ot->name;
2652                 else str= opname;
2653         }
2654         
2655         if ((!tip || tip[0]=='\0') && ot && ot->description) {
2656                 tip= ot->description;
2657         }
2658
2659         but= ui_def_but(block, type, -1, str, x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2660         but->optype= ot;
2661         but->opcontext= opcontext;
2662
2663         if(!ot) {
2664                 but->flag |= UI_BUT_DISABLED;
2665                 but->lock = 1;
2666                 but->lockstr = "";
2667         }
2668
2669         return but;
2670 }
2671
2672 uiBut *uiDefBut(uiBlock *block, int type, int retval, const char *str, short x1, short y1, short x2, short y2, void *poin, float min, float max, float a1, float a2,  char *tip)
2673 {
2674         uiBut *but= ui_def_but(block, type, retval, str, x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2675
2676         ui_check_but(but);
2677         
2678         return but;
2679 }
2680
2681         /* if _x_ is a power of two (only one bit) return the power,
2682          * otherwise return -1. 
2683          * (1<<findBitIndex(x))==x for powers of two.
2684          */
2685 static int findBitIndex(unsigned int x) {
2686         if (!x || (x&(x-1))!=0) {       /* x&(x-1) strips lowest bit */
2687                 return -1;
2688         } else {
2689                 int idx= 0;
2690
2691                 if (x&0xFFFF0000)       idx+=16, x>>=16;
2692                 if (x&0xFF00)           idx+=8, x>>=8;
2693                 if (x&0xF0)                     idx+=4, x>>=4;
2694                 if (x&0xC)                      idx+=2, x>>=2;
2695                 if (x&0x2)                      idx+=1;
2696
2697                 return idx;
2698         }
2699 }
2700
2701 /* autocomplete helper functions */
2702 struct AutoComplete {
2703         int maxlen;
2704         char *truncate;
2705         char *startname;
2706 };
2707
2708 AutoComplete *autocomplete_begin(char *startname, int maxlen)
2709 {
2710         AutoComplete *autocpl;
2711         
2712         autocpl= MEM_callocN(sizeof(AutoComplete), "AutoComplete");
2713         autocpl->maxlen= maxlen;
2714         autocpl->truncate= MEM_callocN(sizeof(char)*maxlen, "AutoCompleteTruncate");
2715         autocpl->startname= startname;
2716
2717         return autocpl;
2718 }
2719
2720 void autocomplete_do_name(AutoComplete *autocpl, const char *name)
2721 {
2722         char *truncate= autocpl->truncate;
2723         char *startname= autocpl->startname;
2724         int a;
2725
2726         for(a=0; a<autocpl->maxlen-1; a++) {
2727                 if(startname[a]==0 || startname[a]!=name[a])
2728                         break;
2729         }
2730         /* found a match */
2731         if(startname[a]==0) {
2732                 /* first match */
2733                 if(truncate[0]==0)
2734                         BLI_strncpy(truncate, name, autocpl->maxlen);
2735                 else {
2736                         /* remove from truncate what is not in bone->name */
2737                         for(a=0; a<autocpl->maxlen-1; a++) {
2738                                 if(name[a] == 0) {
2739                                         truncate[a]= 0;
2740                                         break;
2741                                 }
2742                                 else if(truncate[a]!=name[a])
2743                                         truncate[a]= 0;
2744                         }
2745                 }
2746         }
2747 }
2748
2749 void autocomplete_end(AutoComplete *autocpl, char *autoname)
2750 {       
2751         if(autocpl->truncate[0])
2752                 BLI_strncpy(autoname, autocpl->truncate, autocpl->maxlen);
2753         else {
2754                 if (autoname != autocpl->startname) /* dont copy a string over its self */
2755                         BLI_strncpy(autoname, autocpl->startname, autocpl->maxlen);
2756         }
2757         MEM_freeN(autocpl->truncate);
2758         MEM_freeN(autocpl);
2759 }
2760
2761 /* autocomplete callback for ID buttons */
2762 static void autocomplete_id(bContext *C, char *str, void *arg_v)
2763 {
2764         int blocktype= (intptr_t)arg_v;
2765         ListBase *listb= which_libbase(CTX_data_main(C), blocktype);
2766         
2767         if(listb==NULL) return;
2768         
2769         /* search if str matches the beginning of an ID struct */
2770         if(str[0]) {
2771                 AutoComplete *autocpl= autocomplete_begin(str, 22);
2772                 ID *id;
2773                 
2774                 for(id= listb->first; id; id= id->next)
2775                         autocomplete_do_name(autocpl, id->name+2);
2776
2777                 autocomplete_end(autocpl, str);
2778         }
2779 }
2780
2781 static uiBut *uiDefButBit(uiBlock *block, int type, int bit, int retval, char *str, short x1, short y1, short x2, short y2, void *poin, float min, float max, float a1, float a2,  char *tip)
2782 {
2783         int bitIdx= findBitIndex(bit);
2784         if (bitIdx==-1) {
2785                 return NULL;
2786         } else {
2787                 return uiDefBut(block, type|BIT|bitIdx, retval, str, x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2788         }
2789 }
2790 uiBut *uiDefButF(uiBlock *block, int type, int retval, char *str, short x1, short y1, short x2, short y2, float *poin, float min, float max, float a1, float a2,  char *tip)
2791 {
2792         return uiDefBut(block, type|FLO, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2793 }
2794 uiBut *uiDefButBitF(uiBlock *block, int type, int bit, int retval, char *str, short x1, short y1, short x2, short y2, float *poin, float min, float max, float a1, float a2,  char *tip)
2795 {
2796         return uiDefButBit(block, type|FLO, bit, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2797 }
2798 uiBut *uiDefButI(uiBlock *block, int type, int retval, char *str, short x1, short y1, short x2, short y2, int *poin, float min, float max, float a1, float a2,  char *tip)
2799 {
2800         return uiDefBut(block, type|INT, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2801 }
2802 uiBut *uiDefButBitI(uiBlock *block, int type, int bit, int retval, char *str, short x1, short y1, short x2, short y2, int *poin, float min, float max, float a1, float a2,  char *tip)
2803 {
2804         return uiDefButBit(block, type|INT, bit, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2805 }
2806 uiBut *uiDefButS(uiBlock *block, int type, int retval, char *str, short x1, short y1, short x2, short y2, short *poin, float min, float max, float a1, float a2,  char *tip)
2807 {
2808         return uiDefBut(block, type|SHO, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2809 }
2810 uiBut *uiDefButBitS(uiBlock *block, int type, int bit, int retval, char *str, short x1, short y1, short x2, short y2, short *poin, float min, float max, float a1, float a2,  char *tip)
2811 {
2812         return uiDefButBit(block, type|SHO, bit, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2813 }
2814 uiBut *uiDefButC(uiBlock *block, int type, int retval, char *str, short x1, short y1, short x2, short y2, char *poin, float min, float max, float a1, float a2,  char *tip)
2815 {
2816         return uiDefBut(block, type|CHA, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2817 }
2818 uiBut *uiDefButBitC(uiBlock *block, int type, int bit, int retval, char *str, short x1, short y1, short x2, short y2, char *poin, float min, float max, float a1, float a2,  char *tip)
2819 {
2820         return uiDefButBit(block, type|CHA, bit, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2821 }
2822 uiBut *uiDefButR(uiBlock *block, int type, int retval, const char *str, short x1, short y1, short x2, short y2, PointerRNA *ptr, const char *propname, int index, float min, float max, float a1, float a2,  char *tip)
2823 {
2824         uiBut *but;
2825
2826         but= ui_def_but_rna(block, type, retval, str, x1, y1, x2, y2, ptr, propname, index, min, max, a1, a2, tip);
2827         if(but)
2828                 ui_check_but(but);
2829
2830         return but;
2831 }
2832 uiBut *uiDefButO(uiBlock *block, int type, const char *opname, int opcontext, char *str, short x1, short y1, short x2, short y2, char *tip)
2833 {
2834         uiBut *but;
2835
2836         but= ui_def_but_operator(block, type, opname, opcontext, str, x1, y1, x2, y2, tip);
2837         if(but)
2838                 ui_check_but(but);
2839
2840         return but;
2841 }
2842
2843 uiBut *uiDefButTextO(uiBlock *block, int type, const char *opname, int opcontext, char *str, short x1, short y1, short x2, short y2, void *poin, float min, float max, float a1, float a2,  char *tip)
2844 {
2845         uiBut *but= ui_def_but_operator_text(block, type, opname, opcontext, str, x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2846
2847         if(but)
2848                 ui_check_but(but);
2849         
2850         return but;
2851 }
2852
2853 /* if a1==1.0 then a2 is an extra icon blending factor (alpha 0.0 - 1.0) */
2854 uiBut *uiDefIconBut(uiBlock *block, int type, int retval, int icon, short x1, short y1, short x2, short y2, void *poin, float min, float max, float a1, float a2,  char *tip)
2855 {
2856         uiBut *but= ui_def_but(block, type, retval, "", x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2857         
2858         but->icon= (BIFIconID) icon;
2859         but->flag|= UI_HAS_ICON;
2860
2861         ui_check_but(but);
2862         
2863         return but;
2864 }
2865 static uiBut *uiDefIconButBit(uiBlock *block, int type, int bit, int retval, int icon, short x1, short y1, short x2, short y2, void *poin, float min, float max, float a1, float a2,  char *tip)
2866 {
2867         int bitIdx= findBitIndex(bit);
2868         if (bitIdx==-1) {
2869                 return NULL;
2870         } else {
2871                 return uiDefIconBut(block, type|BIT|bitIdx, retval, icon, x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2872         }
2873 }
2874
2875 uiBut *uiDefIconButF(uiBlock *block, int type, int retval, int icon, short x1, short y1, short x2, short y2, float *poin, float min, float max, float a1, float a2,  char *tip)
2876 {
2877         return uiDefIconBut(block, type|FLO, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2878 }
2879 uiBut *uiDefIconButBitF(uiBlock *block, int type, int bit, int retval, int icon, short x1, short y1, short x2, short y2, float *poin, float min, float max, float a1, float a2,  char *tip)
2880 {
2881         return uiDefIconButBit(block, type|FLO, bit, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2882 }
2883 uiBut *uiDefIconButI(uiBlock *block, int type, int retval, int icon, short x1, short y1, short x2, short y2, int *poin, float min, float max, float a1, float a2,  char *tip)
2884 {
2885         return uiDefIconBut(block, type|INT, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2886 }
2887 uiBut *uiDefIconButBitI(uiBlock *block, int type, int bit, int retval, int icon, short x1, short y1, short x2, short y2, int *poin, float min, float max, float a1, float a2,  char *tip)
2888 {
2889         return uiDefIconButBit(block, type|INT, bit, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2890 }
2891 uiBut *uiDefIconButS(uiBlock *block, int type, int retval, int icon, short x1, short y1, short x2, short y2, short *poin, float min, float max, float a1, float a2,  char *tip)
2892 {
2893         return uiDefIconBut(block, type|SHO, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2894 }
2895 uiBut *uiDefIconButBitS(uiBlock *block, int type, int bit, int retval, int icon, short x1, short y1, short x2, short y2, short *poin, float min, float max, float a1, float a2,  char *tip)
2896 {
2897         return uiDefIconButBit(block, type|SHO, bit, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2898 }
2899 uiBut *uiDefIconButC(uiBlock *block, int type, int retval, int icon, short x1, short y1, short x2, short y2, char *poin, float min, float max, float a1, float a2,  char *tip)
2900 {
2901         return uiDefIconBut(block, type|CHA, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2902 }
2903 uiBut *uiDefIconButBitC(uiBlock *block, int type, int bit, int retval, int icon, short x1, short y1, short x2, short y2, char *poin, float min, float max, float a1, float a2,  char *tip)
2904 {
2905         return uiDefIconButBit(block, type|CHA, bit, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2906 }
2907 uiBut *uiDefIconButR(uiBlock *block, int type, int retval, int icon, short x1, short y1, short x2, short y2, PointerRNA *ptr, const char *propname, int index, float min, float max, float a1, float a2,  char *tip)
2908 {
2909         uiBut *but;
2910
2911         but= ui_def_but_rna(block, type, retval, "", x1, y1, x2, y2, ptr, propname, index, min, max, a1, a2, tip);
2912         if(but) {
2913                 if(icon) {
2914                         but->icon= (BIFIconID) icon;
2915                         but->flag|= UI_HAS_ICON;
2916                 }
2917                 ui_check_but(but);
2918         }
2919
2920         return but;
2921 }
2922 uiBut *uiDefIconButO(uiBlock *block, int type, const char *opname, int opcontext, int icon, short x1, short y1, short x2, short y2, char *tip)
2923 {
2924         uiBut *but;
2925
2926         but= ui_def_but_operator(block, type, opname, opcontext, "", x1, y1, x2, y2, tip);
2927         if(but) {
2928                 but->icon= (BIFIconID) icon;
2929                 but->flag|= UI_HAS_ICON;
2930                 ui_check_but(but);
2931         }
2932
2933         return but;
2934 }
2935
2936 /* Button containing both string label and icon */
2937 uiBut *uiDefIconTextBut(uiBlock *block, int type, int retval, int icon, const char *str, short x1, short y1, short x2, short y2, void *poin, float min, float max, float a1, float a2,  char *tip)
2938 {
2939         uiBut *but= ui_def_but(block, type, retval, str, x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2940
2941         but->icon= (BIFIconID) icon;
2942         but->flag|= UI_HAS_ICON;
2943
2944         but->flag|= UI_ICON_LEFT;
2945
2946         ui_check_but(but);
2947
2948         return but;
2949 }
2950 static uiBut *uiDefIconTextButBit(uiBlock *block, int type, int bit, int retval, int icon, const char *str, short x1, short y1, short x2, short y2, void *poin, float min, float max, float a1, float a2,  char *tip)
2951 {
2952         int bitIdx= findBitIndex(bit);
2953         if (bitIdx==-1) {
2954                 return NULL;
2955         } else {
2956                 return uiDefIconTextBut(block, type|BIT|bitIdx, retval, icon, str, x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2957         }
2958 }
2959
2960 uiBut *uiDefIconTextButF(uiBlock *block, int type, int retval, int icon, const char *str, short x1, short y1, short x2, short y2, float *poin, float min, float max, float a1, float a2,  char *tip)
2961 {
2962         return uiDefIconTextBut(block, type|FLO, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2963 }
2964 uiBut *uiDefIconTextButBitF(uiBlock *block, int type, int bit, int retval, int icon, const char *str, short x1, short y1, short x2, short y2, float *poin, float min, float max, float a1, float a2,  char *tip)
2965 {
2966         return uiDefIconTextButBit(block, type|FLO, bit, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2967 }
2968 uiBut *uiDefIconTextButI(uiBlock *block, int type, int retval, int icon, const char *str, short x1, short y1, short x2, short y2, int *poin, float min, float max, float a1, float a2,  char *tip)
2969 {
2970         return uiDefIconTextBut(block, type|INT, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2971 }
2972 uiBut *uiDefIconTextButBitI(uiBlock *block, int type, int bit, int retval, int icon, const char *str, short x1, short y1, short x2, short y2, int *poin, float min, float max, float a1, float a2,  char *tip)
2973 {
2974         return uiDefIconTextButBit(block, type|INT, bit, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2975 }
2976 uiBut *uiDefIconTextButS(uiBlock *block, int type, int retval, int icon, const char *str, short x1, short y1, short x2, short y2, short *poin, float min, float max, float a1, float a2,  char *tip)
2977 {
2978         return uiDefIconTextBut(block, type|SHO, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2979 }
2980 uiBut *uiDefIconTextButBitS(uiBlock *block, int type, int bit, int retval, int icon, const char *str, short x1, short y1, short x2, short y2, short *poin, float min, float max, float a1, float a2,  char *tip)
2981 {
2982         return uiDefIconTextButBit(block, type|SHO, bit, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2983 }
2984 uiBut *uiDefIconTextButC(uiBlock *block, int type, int retval, int icon, const char *str, short x1, short y1, short x2, short y2, char *poin, float min, float max, float a1, float a2,  char *tip)
2985 {
2986         return uiDefIconTextBut(block, type|CHA, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2987 }
2988 uiBut *uiDefIconTextButBitC(uiBlock *block, int type, int bit, int retval, int icon, const char *str, short x1, short y1, short x2, short y2, char *poin, float min, float max, float a1, float a2,  char *tip)
2989 {
2990         return uiDefIconTextButBit(block, type|CHA, bit, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2991 }
2992 uiBut *uiDefIconTextButR(uiBlock *block, int type, int retval, int icon, const char *str, short x1, short y1, short x2, short y2, PointerRNA *ptr, const char *propname, int index, float min, float max, float a1, float a2,  char *tip)
2993 {
2994         uiBut *but;
2995
2996         but= ui_def_but_rna(block, type, retval, str, x1, y1, x2, y2, ptr, propname, index, min, max, a1, a2, tip);
2997         if(but) {
2998                 if(icon) {
2999                         but->icon= (BIFIconID) icon;
3000                         but->flag|= UI_HAS_ICON;
3001                 }
3002                 but->flag|= UI_ICON_LEFT;
3003                 ui_check_but(but);
3004         }
3005
3006         return but;
3007 }
3008 uiBut *uiDefIconTextButO(uiBlock *block, int type, const char *opname, int opcontext, int icon, const char *str, short x1, short y1, short x2, short y2, char *tip)
3009 {
3010         uiBut *but;
3011
3012         but= ui_def_but_operator(block, type, opname, opcontext, str, x1, y1, x2, y2, tip);
3013         if(but) {
3014                 but->icon= (BIFIconID) icon;
3015                 but->flag|= UI_HAS_ICON;
3016                 but->flag|= UI_ICON_LEFT;
3017                 ui_check_but(but);
3018         }
3019
3020         return but;
3021 }
3022
3023 /* END Button containing both string label and icon */
3024
3025 void uiSetButLink(uiBut *but, void **poin, void ***ppoin, short *tot, int from, int to)
3026 {
3027         uiLink *link;
3028         
3029         link= but->link= MEM_callocN(sizeof(uiLink), "new uilink");
3030         
3031         link->poin= poin;
3032         link->ppoin= ppoin;
3033         link->totlink= tot;
3034         link->fromcode= from;
3035         link->tocode= to;
3036 }
3037
3038 /* cruft to make uiBlock and uiBut private */
3039
3040 int uiBlocksGetYMin(ListBase *lb)
3041 {
3042         uiBlock *block;
3043         int min= 0;
3044         
3045         for (block= lb->first; block; block= block->next)
3046                 if (block==lb->first || block->miny<min)
3047                         min= block->miny;
3048                         
3049         return min;
3050 }
3051
3052 void uiBlockSetDirection(uiBlock *block, int direction)
3053 {
3054         block->direction= direction;
3055 }
3056
3057 /* this call escapes if there's alignment flags */
3058 void uiBlockFlipOrder(uiBlock *block)
3059 {
3060         ListBase lb;
3061         uiBut *but, *next;
3062         float centy, miny=10000, maxy= -10000;
3063
3064         if(U.uiflag & USER_MENUFIXEDORDER)
3065                 return;
3066         else if(block->flag & UI_BLOCK_NO_FLIP)
3067                 return;
3068         
3069         for(but= block->buttons.first; but; but= but->next) {
3070                 if(but->flag & UI_BUT_ALIGN) return;
3071                 if(but->y1 < miny) miny= but->y1;
3072                 if(but->y2 > maxy) maxy= but->y2;
3073         }
3074         /* mirror trick */
3075         centy= (miny+maxy)/2.0;
3076         for(but= block->buttons.first; but; but= but->next) {
3077                 but->y1 = centy-(but->y1-centy);
3078                 but->y2 = centy-(but->y2-centy);
3079                 SWAP(float, but->y1, but->y2);
3080         }
3081         
3082         /* also flip order in block itself, for example for arrowkey */
3083         lb.first= lb.last= NULL;
3084         but= block->buttons.first;
3085         while(but) {
3086                 next= but->next;
3087                 BLI_remlink(&block->buttons, but);
3088                 BLI_addtail(&lb, but);
3089                 but= next;
3090         }
3091         block->buttons= lb;
3092 }
3093
3094
3095 void uiBlockSetFlag(uiBlock *block, int flag)
3096 {
3097         block->flag|= flag;
3098 }
3099
3100 void uiBlockClearFlag(uiBlock *block, int flag)
3101 {
3102         block->flag&= ~flag;
3103 }
3104
3105 void uiBlockSetXOfs(uiBlock *block, int xofs)
3106 {
3107         block->xofs= xofs;
3108 }
3109
3110 void uiButSetFlag(uiBut *but, int flag)
3111 {
3112         but->flag|= flag;
3113 }
3114
3115 void uiButClearFlag(uiBut *but, int flag)
3116 {
3117         but->flag&= ~flag;
3118 }
3119
3120 int uiButGetRetVal(uiBut *but)
3121 {
3122         return but->retval;
3123 }
3124
3125 void uiButSetDragID(uiBut *but, ID *id)
3126 {
3127         but->dragtype= WM_DRAG_ID;
3128         but->dragpoin= (void *)id;
3129 }
3130
3131 void uiButSetDragRNA(uiBut *but, PointerRNA *ptr)
3132 {
3133         but->dragtype= WM_DRAG_RNA;
3134         but->dragpoin= (void *)ptr;
3135 }
3136
3137 void uiButSetDragPath(uiBut *but, const char *path)
3138 {
3139         but->dragtype= WM_DRAG_PATH;
3140         but->dragpoin= (void *)path;
3141 }
3142
3143 void uiButSetDragName(uiBut *but, const char *name)
3144 {
3145         but->dragtype= WM_DRAG_NAME;