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