f4866237f46d11a4c500db5bcc4d6d62690a7986
[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/blender/doc/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(uiBut *but, 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(but, 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(ar, &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 *but, short *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(but, line);
1097                                                 }
1098                                         }
1099                                         else if(line->flag & UI_SELECT) {
1100                                                 line->flag &= ~UI_SELECT;
1101                                                 ui_draw_linkline(but, 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                 {
2140                         float rgb[3];
2141                         ui_get_but_vectorf(but, rgb);
2142                         rgb_to_hsv(rgb[0], rgb[1], rgb[2], but->hsv, but->hsv+1, but->hsv+2);
2143                 }
2144                 break;
2145         default:
2146                 strncpy(but->drawstr, but->str, UI_MAX_DRAW_STR);
2147                 
2148         }
2149
2150         /* if we are doing text editing, this will override the drawstr */
2151         if(but->editstr)
2152                 strncpy(but->drawstr, but->editstr, UI_MAX_DRAW_STR);
2153         
2154         /* text clipping moved to widget drawing code itself */
2155 }
2156
2157
2158 void uiBlockBeginAlign(uiBlock *block)
2159 {
2160         /* if other align was active, end it */
2161         if(block->flag & UI_BUT_ALIGN) uiBlockEndAlign(block);
2162
2163         block->flag |= UI_BUT_ALIGN_DOWN;       
2164         block->alignnr++;
2165
2166         /* buttons declared after this call will get this align nr */ // XXX flag?
2167 }
2168
2169 static int buts_are_horiz(uiBut *but1, uiBut *but2)
2170 {
2171         float dx, dy;
2172         
2173         dx= fabs( but1->x2 - but2->x1);
2174         dy= fabs( but1->y1 - but2->y2);
2175         
2176         if(dx > dy) return 0;
2177         return 1;
2178 }
2179
2180 void uiBlockEndAlign(uiBlock *block)
2181 {
2182         block->flag &= ~UI_BUT_ALIGN;   // all 4 flags
2183 }
2184
2185 int ui_but_can_align(uiBut *but)
2186 {
2187         return !ELEM3(but->type, LABEL, OPTION, OPTIONN);
2188 }
2189
2190 static void ui_block_do_align_but(uiBlock *block, uiBut *first, int nr)
2191 {
2192         uiBut *prev, *but=NULL, *next;
2193         int flag= 0, cols=0, rows=0;
2194         
2195         /* auto align */
2196
2197         for(but=first; but && but->alignnr == nr; but=but->next) {
2198                 if(but->next && but->next->alignnr == nr) {
2199                         if(buts_are_horiz(but, but->next)) cols++;
2200                         else rows++;
2201                 }
2202         }
2203
2204         /* rows==0: 1 row, cols==0: 1 collumn */
2205         
2206         /* note;  how it uses 'flag' in loop below (either set it, or OR it) is confusing */
2207         for(but=first, prev=NULL; but && but->alignnr == nr; prev=but, but=but->next) {
2208                 next= but->next;
2209                 if(next && next->alignnr != nr)
2210                         next= NULL;
2211
2212                 /* clear old flag */
2213                 but->flag &= ~UI_BUT_ALIGN;
2214                 
2215                 if(flag==0) {   /* first case */
2216                         if(next) {
2217                                 if(buts_are_horiz(but, next)) {
2218                                         if(rows==0)
2219                                                 flag= UI_BUT_ALIGN_RIGHT;
2220                                         else 
2221                                                 flag= UI_BUT_ALIGN_DOWN|UI_BUT_ALIGN_RIGHT;
2222                                 }
2223                                 else {
2224                                         flag= UI_BUT_ALIGN_DOWN;
2225                                 }
2226                         }
2227                 }
2228                 else if(next==NULL) {   /* last case */
2229                         if(prev) {
2230                                 if(buts_are_horiz(prev, but)) {
2231                                         if(rows==0) 
2232                                                 flag= UI_BUT_ALIGN_LEFT;
2233                                         else
2234                                                 flag= UI_BUT_ALIGN_TOP|UI_BUT_ALIGN_LEFT;
2235                                 }
2236                                 else flag= UI_BUT_ALIGN_TOP;
2237                         }
2238                 }
2239                 else if(buts_are_horiz(but, next)) {
2240                         /* check if this is already second row */
2241                         if( prev && buts_are_horiz(prev, but)==0) {
2242                                 flag &= ~UI_BUT_ALIGN_LEFT;
2243                                 flag |= UI_BUT_ALIGN_TOP;
2244                                 /* exception case: bottom row */
2245                                 if(rows>0) {
2246                                         uiBut *bt= but;
2247                                         while(bt && bt->alignnr == nr) {
2248                                                 if(bt->next && bt->next->alignnr == nr && buts_are_horiz(bt, bt->next)==0 ) break; 
2249                                                 bt= bt->next;
2250                                         }
2251                                         if(bt==0 || bt->alignnr != nr) flag= UI_BUT_ALIGN_TOP|UI_BUT_ALIGN_RIGHT;
2252                                 }
2253                         }
2254                         else flag |= UI_BUT_ALIGN_LEFT;
2255                 }
2256                 else {
2257                         if(cols==0) {
2258                                 flag |= UI_BUT_ALIGN_TOP;
2259                         }
2260                         else {  /* next button switches to new row */
2261                                 
2262                                 if(prev && buts_are_horiz(prev, but))
2263                                    flag |= UI_BUT_ALIGN_LEFT;
2264                                 
2265                                 if( (flag & UI_BUT_ALIGN_TOP)==0) {     /* stil top row */
2266                                         if(prev) {
2267                                                 if(next && buts_are_horiz(next, but))
2268                                                         flag = UI_BUT_ALIGN_DOWN|UI_BUT_ALIGN_LEFT;
2269                                                 else {
2270                                                         flag = UI_BUT_ALIGN_DOWN|UI_BUT_ALIGN_LEFT|UI_BUT_ALIGN_RIGHT;
2271                                                 }
2272                                         }
2273                                         else 
2274                                                 flag |= UI_BUT_ALIGN_DOWN;
2275                                 }
2276                                 else 
2277                                         flag |= UI_BUT_ALIGN_TOP;
2278                         }
2279                 }
2280                 
2281                 but->flag |= flag;
2282                 
2283                 /* merge coordinates */
2284                 if(prev) {
2285                         // simple cases 
2286                         if(rows==0) {
2287                                 but->x1= (prev->x2+but->x1)/2.0;
2288                                 prev->x2= but->x1;
2289                         }
2290                         else if(cols==0) {
2291                                 but->y2= (prev->y1+but->y2)/2.0;
2292                                 prev->y1= but->y2;
2293                         }
2294                         else {
2295                                 if(buts_are_horiz(prev, but)) {
2296                                         but->x1= (prev->x2+but->x1)/2.0;
2297                                         prev->x2= but->x1;
2298                                         /* copy height too */
2299                                         but->y2= prev->y2;
2300                                 }
2301                                 else if(prev->prev && buts_are_horiz(prev->prev, prev)==0) {
2302                                         /* the previous button is a single one in its row */
2303                                         but->y2= (prev->y1+but->y2)/2.0;
2304                                         prev->y1= but->y2;
2305                                 }
2306                                 else {
2307                                         /* the previous button is not a single one in its row */
2308                                         but->y2= prev->y1;
2309                                 }
2310                         }
2311                 }
2312         }
2313 }
2314
2315 void ui_block_do_align(uiBlock *block)
2316 {
2317         uiBut *but;
2318         int nr;
2319
2320         /* align buttons with same align nr */
2321         for(but=block->buttons.first; but;) {
2322                 if(but->alignnr) {
2323                         nr= but->alignnr;
2324                         ui_block_do_align_but(block, but, nr);
2325
2326                         /* skip with same number */
2327                         for(; but && but->alignnr == nr; but=but->next);
2328
2329                         if(!but)
2330                                 break;
2331                 }
2332                 else
2333                         but= but->next;
2334         }
2335 }
2336
2337 /*
2338 ui_def_but is the function that draws many button types
2339
2340 for float buttons:
2341         "a1" Click Step (how much to change the value each click)
2342         "a2" Number of decimal point values to display. 0 defaults to 3 (0.000) 1,2,3, and a maximum of 4,
2343            all greater values will be clamped to 4.
2344
2345 */
2346 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)
2347 {
2348         uiBut *but;
2349         short slen;
2350         
2351         if(type & BUTPOIN) {            /* a pointer is required */
2352                 if(poin==NULL)
2353                         return NULL;
2354         }
2355
2356         but= MEM_callocN(sizeof(uiBut), "uiBut");
2357
2358         but->type= type & BUTTYPE;
2359         but->pointype= type & BUTPOIN;
2360         but->bit= type & BIT;
2361         but->bitnr= type & 31;
2362         but->icon = 0;
2363         but->iconadd=0;
2364
2365         but->retval= retval;
2366         if( strlen(str)>=UI_MAX_NAME_STR-1 ) {
2367                 but->str= MEM_callocN( strlen(str)+2, "uiDefBut");
2368                 strcpy(but->str, str);
2369         }
2370         else {
2371                 but->str= but->strdata;
2372                 strcpy(but->str, str);
2373         }
2374         but->x1= x1; 
2375         but->y1= y1;
2376         but->x2= (x1+x2); 
2377         but->y2= (y1+y2);
2378         
2379         but->poin= poin;
2380         but->hardmin= but->softmin= min; 
2381         but->hardmax= but->softmax= max;
2382         but->a1= a1; 
2383         but->a2= a2;
2384         but->tip= tip;
2385         
2386         but->lock= block->lock;
2387         but->lockstr= block->lockstr;
2388         but->dt= block->dt;
2389
2390         but->aspect= 1.0f; //XXX block->aspect;
2391         but->block= block;              // pointer back, used for frontbuffer status, and picker
2392
2393         if((block->flag & UI_BUT_ALIGN) && ui_but_can_align(but))
2394                 but->alignnr= block->alignnr;
2395
2396         but->func= block->func;
2397         but->func_arg1= block->func_arg1;
2398         but->func_arg2= block->func_arg2;
2399
2400         but->funcN= block->funcN;
2401         if(block->func_argN)
2402                 but->func_argN= MEM_dupallocN(block->func_argN);
2403         
2404         but->pos= -1;   /* cursor invisible */
2405
2406         if(ELEM4(but->type, NUM, NUMABS, NUMSLI, HSVSLI)) {     /* add a space to name */
2407                 slen= strlen(but->str);
2408                 if(slen>0 && slen<UI_MAX_NAME_STR-2) {
2409                         if(but->str[slen-1]!=' ') {
2410                                 but->str[slen]= ' ';
2411                                 but->str[slen+1]= 0;
2412                         }
2413                 }
2414         }
2415
2416         if((block->flag & UI_BLOCK_LOOP) || ELEM8(but->type, MENU, TEX, LABEL, IDPOIN, BLOCK, BUTM, SEARCH_MENU, PROGRESSBAR))
2417                 but->flag |= (UI_TEXT_LEFT|UI_ICON_LEFT);
2418         else if(but->type==BUT_TOGDUAL)
2419                 but->flag |= UI_ICON_LEFT;
2420
2421         but->flag |= (block->flag & UI_BUT_ALIGN);
2422
2423         if (but->lock) {
2424                 if (but->lockstr) {
2425                         but->flag |= UI_BUT_DISABLED;
2426                 }
2427         }
2428
2429         if(ELEM8(but->type, BLOCK, BUT, LABEL, PULLDOWN, ROUNDBOX, LISTBOX, SEARCH_MENU, BUTM));
2430         else if(ELEM5(but->type, SCROLL, SEPR, LINK, INLINK, FTPREVIEW));
2431         else but->flag |= UI_BUT_UNDO;
2432
2433         BLI_addtail(&block->buttons, but);
2434         
2435         if(block->curlayout)
2436                 ui_layout_add_but(block->curlayout, but);
2437
2438         return but;
2439 }
2440
2441 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)
2442 {
2443         uiBut *but;
2444         PropertyRNA *prop;
2445         PropertyType proptype;
2446         int freestr= 0, icon= 0;
2447
2448         prop= RNA_struct_find_property(ptr, propname);
2449
2450         if(prop) {
2451                 proptype= RNA_property_type(prop);
2452
2453                 /* use rna values if parameters are not specified */
2454                 if(!str) {
2455                         if(type == MENU && proptype == PROP_ENUM) {
2456                                 EnumPropertyItem *item;
2457                                 DynStr *dynstr;
2458                                 int i, totitem, value, free;
2459
2460                                 RNA_property_enum_items(block->evil_C, ptr, prop, &item, &totitem, &free);
2461                                 value= RNA_property_enum_get(ptr, prop);
2462
2463                                 dynstr= BLI_dynstr_new();
2464                                 BLI_dynstr_appendf(dynstr, "%s%%t", RNA_property_ui_name(prop));
2465                                 for(i=0; i<totitem; i++) {
2466                                         if(!item[i].identifier[0]) {
2467                                                 if(item[i].name)
2468                                                         BLI_dynstr_appendf(dynstr, "|%s%%l", item[i].name);
2469                                                 else
2470                                                         BLI_dynstr_append(dynstr, "|%l");
2471                                         }
2472                                         else if(item[i].icon)
2473                                                 BLI_dynstr_appendf(dynstr, "|%s %%i%d %%x%d", item[i].name, item[i].icon, item[i].value);
2474                                         else
2475                                                 BLI_dynstr_appendf(dynstr, "|%s %%x%d", item[i].name, item[i].value);
2476
2477                                         if(value == item[i].value)
2478                                                 icon= item[i].icon;
2479                                 }
2480                                 str= BLI_dynstr_get_cstring(dynstr);
2481                                 BLI_dynstr_free(dynstr);
2482
2483                                 if(free)
2484                                         MEM_freeN(item);
2485
2486                                 freestr= 1;
2487                         }
2488                         else if(ELEM(type, ROW, LISTROW) && proptype == PROP_ENUM) {
2489                                 EnumPropertyItem *item;
2490                                 int i, totitem, free;
2491
2492                                 RNA_property_enum_items(block->evil_C, ptr, prop, &item, &totitem, &free);
2493                                 for(i=0; i<totitem; i++) {
2494                                         if(item[i].identifier[0] && item[i].value == (int)max) {
2495                                                 str= (char*)item[i].name;
2496                                                 icon= item[i].icon;
2497                                         }
2498                                 }
2499
2500                                 if(!str)
2501                                         str= (char*)RNA_property_ui_name(prop);
2502                                 if(free)
2503                                         MEM_freeN(item);
2504                         }
2505                         else {
2506                                 str= (char*)RNA_property_ui_name(prop);
2507                                 icon= RNA_property_ui_icon(prop);
2508                         }
2509                 }
2510
2511                 if(!tip) {
2512                         if(type == ROW && proptype == PROP_ENUM) {
2513                                 EnumPropertyItem *item;
2514                                 int i, totitem, free;
2515
2516                                 RNA_property_enum_items(block->evil_C, ptr, prop, &item, &totitem, &free);
2517
2518                                 for(i=0; i<totitem; i++) {
2519                                         if(item[i].identifier[0] && item[i].value == (int)max) {
2520                                                 if(item[i].description[0])
2521                                                         tip= (char*)item[i].description;
2522                                                 break;
2523                                         }
2524                                 }
2525
2526                                 if(free)
2527                                         MEM_freeN(item);
2528                         }
2529                 }
2530                 
2531                 if(!tip)
2532                         tip= (char*)RNA_property_ui_description(prop);
2533
2534                 if(min == max || a1 == -1 || a2 == -1) {
2535                         if(proptype == PROP_INT) {
2536                                 int hardmin, hardmax, softmin, softmax, step;
2537
2538                                 RNA_property_int_range(ptr, prop, &hardmin, &hardmax);
2539                                 RNA_property_int_ui_range(ptr, prop, &softmin, &softmax, &step);
2540
2541                                 if(!ELEM(type, ROW, LISTROW) && min == max) {
2542                                         min= hardmin;
2543                                         max= hardmax;
2544                                 }
2545                                 if(a1 == -1)
2546                                         a1= step;
2547                                 if(a2 == -1)
2548                                         a2= 0;
2549                         }
2550                         else if(proptype == PROP_FLOAT) {
2551                                 float hardmin, hardmax, softmin, softmax, step, precision;
2552
2553                                 RNA_property_float_range(ptr, prop, &hardmin, &hardmax);
2554                                 RNA_property_float_ui_range(ptr, prop, &softmin, &softmax, &step, &precision);
2555
2556                                 if(!ELEM(type, ROW, LISTROW) && min == max) {
2557                                         min= hardmin;
2558                                         max= hardmax;
2559                                 }
2560                                 if(a1 == -1)
2561                                         a1= step;
2562                                 if(a2 == -1)
2563                                         a2= precision;
2564                         }
2565                         else if(proptype == PROP_STRING) {
2566                                 min= 0;
2567                                 max= RNA_property_string_maxlength(prop);
2568                                 if(max == 0) /* interface code should ideally support unlimited length */
2569                                         max= UI_MAX_DRAW_STR; 
2570                         }
2571                 }
2572         }
2573         else {
2574                 printf("ui_def_but_rna: property not found: %s.%s\n", RNA_struct_identifier(ptr->type), propname);
2575                 str= (char*)propname;
2576         }
2577
2578         /* now create button */
2579         but= ui_def_but(block, type, retval, str, x1, y1, x2, y2, NULL, min, max, a1, a2, tip);
2580
2581         if(prop) {
2582                 but->rnapoin= *ptr;
2583                 but->rnaprop= prop;
2584
2585                 if(RNA_property_array_length(&but->rnapoin, but->rnaprop))
2586                         but->rnaindex= index;
2587                 else
2588                         but->rnaindex= 0;
2589         }
2590
2591         if(icon) {
2592                 but->icon= (BIFIconID)icon;
2593                 but->flag |= UI_HAS_ICON;
2594                 but->flag|= UI_ICON_LEFT;
2595         }
2596         
2597         if (!prop || !RNA_property_editable(&but->rnapoin, prop)) {
2598                 but->flag |= UI_BUT_DISABLED;
2599                 but->lock = 1;
2600                 but->lockstr = "";
2601         }
2602
2603         /* If this button uses units, calculate the step from this */
2604         if(ui_is_but_unit(but))
2605                 but->a1= ui_get_but_step_unit(but, but->a1);
2606
2607         if(freestr)
2608                 MEM_freeN(str);
2609         
2610         return but;
2611 }
2612
2613 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)
2614 {
2615         uiBut *but;
2616         wmOperatorType *ot;
2617         
2618         ot= WM_operatortype_find(opname, 0);
2619
2620         if(!str) {
2621                 if(ot) str= ot->name;
2622                 else str= opname;
2623         }
2624         
2625         if ((!tip || tip[0]=='\0') && ot && ot->description) {
2626                 tip= ot->description;
2627         }
2628
2629         but= ui_def_but(block, type, -1, str, x1, y1, x2, y2, NULL, 0, 0, 0, 0, tip);
2630         but->optype= ot;
2631         but->opcontext= opcontext;
2632
2633         if(!ot) {
2634                 but->flag |= UI_BUT_DISABLED;
2635                 but->lock = 1;
2636                 but->lockstr = "";
2637         }
2638
2639         return but;
2640 }
2641
2642 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)
2643 {
2644         uiBut *but= ui_def_but(block, type, retval, str, x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2645
2646         ui_check_but(but);
2647         
2648         return but;
2649 }
2650
2651         /* if _x_ is a power of two (only one bit) return the power,
2652          * otherwise return -1. 
2653          * (1<<findBitIndex(x))==x for powers of two.
2654          */
2655 static int findBitIndex(unsigned int x) {
2656         if (!x || (x&(x-1))!=0) {       /* x&(x-1) strips lowest bit */
2657                 return -1;
2658         } else {
2659                 int idx= 0;
2660
2661                 if (x&0xFFFF0000)       idx+=16, x>>=16;
2662                 if (x&0xFF00)           idx+=8, x>>=8;
2663                 if (x&0xF0)                     idx+=4, x>>=4;
2664                 if (x&0xC)                      idx+=2, x>>=2;
2665                 if (x&0x2)                      idx+=1;
2666
2667                 return idx;
2668         }
2669 }
2670
2671 /* autocomplete helper functions */
2672 struct AutoComplete {
2673         int maxlen;
2674         char *truncate;
2675         char *startname;
2676 };
2677
2678 AutoComplete *autocomplete_begin(char *startname, int maxlen)
2679 {
2680         AutoComplete *autocpl;
2681         
2682         autocpl= MEM_callocN(sizeof(AutoComplete), "AutoComplete");
2683         autocpl->maxlen= maxlen;
2684         autocpl->truncate= MEM_callocN(sizeof(char)*maxlen, "AutoCompleteTruncate");
2685         autocpl->startname= startname;
2686
2687         return autocpl;
2688 }
2689
2690 void autocomplete_do_name(AutoComplete *autocpl, const char *name)
2691 {
2692         char *truncate= autocpl->truncate;
2693         char *startname= autocpl->startname;
2694         int a;
2695
2696         for(a=0; a<autocpl->maxlen-1; a++) {
2697                 if(startname[a]==0 || startname[a]!=name[a])
2698                         break;
2699         }
2700         /* found a match */
2701         if(startname[a]==0) {
2702                 /* first match */
2703                 if(truncate[0]==0)
2704                         BLI_strncpy(truncate, name, autocpl->maxlen);
2705                 else {
2706                         /* remove from truncate what is not in bone->name */
2707                         for(a=0; a<autocpl->maxlen-1; a++) {
2708                                 if(name[a] == 0) {
2709                                         truncate[a]= 0;
2710                                         break;
2711                                 }
2712                                 else if(truncate[a]!=name[a])
2713                                         truncate[a]= 0;
2714                         }
2715                 }
2716         }
2717 }
2718
2719 void autocomplete_end(AutoComplete *autocpl, char *autoname)
2720 {       
2721         if(autocpl->truncate[0])
2722                 BLI_strncpy(autoname, autocpl->truncate, autocpl->maxlen);
2723         else {
2724                 if (autoname != autocpl->startname) /* dont copy a string over its self */
2725                         BLI_strncpy(autoname, autocpl->startname, autocpl->maxlen);
2726         }
2727         MEM_freeN(autocpl->truncate);
2728         MEM_freeN(autocpl);
2729 }
2730
2731 /* autocomplete callback for ID buttons */
2732 static void autocomplete_id(bContext *C, char *str, void *arg_v)
2733 {
2734         int blocktype= (intptr_t)arg_v;
2735         ListBase *listb= which_libbase(CTX_data_main(C), blocktype);
2736         
2737         if(listb==NULL) return;
2738         
2739         /* search if str matches the beginning of an ID struct */
2740         if(str[0]) {
2741                 AutoComplete *autocpl= autocomplete_begin(str, 22);
2742                 ID *id;
2743                 
2744                 for(id= listb->first; id; id= id->next)
2745                         autocomplete_do_name(autocpl, id->name+2);
2746
2747                 autocomplete_end(autocpl, str);
2748         }
2749 }
2750
2751 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)
2752 {
2753         int bitIdx= findBitIndex(bit);
2754         if (bitIdx==-1) {
2755                 return NULL;
2756         } else {
2757                 return uiDefBut(block, type|BIT|bitIdx, retval, str, x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2758         }
2759 }
2760 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)
2761 {
2762         return uiDefBut(block, type|FLO, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2763 }
2764 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)
2765 {
2766         return uiDefButBit(block, type|FLO, bit, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2767 }
2768 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)
2769 {
2770         return uiDefBut(block, type|INT, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2771 }
2772 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)
2773 {
2774         return uiDefButBit(block, type|INT, bit, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2775 }
2776 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)
2777 {
2778         return uiDefBut(block, type|SHO, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2779 }
2780 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)
2781 {
2782         return uiDefButBit(block, type|SHO, bit, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2783 }
2784 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)
2785 {
2786         return uiDefBut(block, type|CHA, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2787 }
2788 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)
2789 {
2790         return uiDefButBit(block, type|CHA, bit, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2791 }
2792 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)
2793 {
2794         uiBut *but;
2795
2796         but= ui_def_but_rna(block, type, retval, str, x1, y1, x2, y2, ptr, propname, index, min, max, a1, a2, tip);
2797         if(but)
2798                 ui_check_but(but);
2799
2800         return but;
2801 }
2802 uiBut *uiDefButO(uiBlock *block, int type, char *opname, int opcontext, char *str, short x1, short y1, short x2, short y2, char *tip)
2803 {
2804         uiBut *but;
2805
2806         but= ui_def_but_operator(block, type, opname, opcontext, str, x1, y1, x2, y2, tip);
2807         if(but)
2808                 ui_check_but(but);
2809
2810         return but;
2811 }
2812
2813 /* if a1==1.0 then a2 is an extra icon blending factor (alpha 0.0 - 1.0) */
2814 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)
2815 {
2816         uiBut *but= ui_def_but(block, type, retval, "", x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2817         
2818         but->icon= (BIFIconID) icon;
2819         but->flag|= UI_HAS_ICON;
2820
2821         ui_check_but(but);
2822         
2823         return but;
2824 }
2825 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)
2826 {
2827         int bitIdx= findBitIndex(bit);
2828         if (bitIdx==-1) {
2829                 return NULL;
2830         } else {
2831                 return uiDefIconBut(block, type|BIT|bitIdx, retval, icon, x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2832         }
2833 }
2834
2835 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)
2836 {
2837         return uiDefIconBut(block, type|FLO, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2838 }
2839 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)
2840 {
2841         return uiDefIconButBit(block, type|FLO, bit, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2842 }
2843 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)
2844 {
2845         return uiDefIconBut(block, type|INT, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2846 }
2847 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)
2848 {
2849         return uiDefIconButBit(block, type|INT, bit, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2850 }
2851 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)
2852 {
2853         return uiDefIconBut(block, type|SHO, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2854 }
2855 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)
2856 {
2857         return uiDefIconButBit(block, type|SHO, bit, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2858 }
2859 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)
2860 {
2861         return uiDefIconBut(block, type|CHA, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2862 }
2863 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)
2864 {
2865         return uiDefIconButBit(block, type|CHA, bit, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2866 }
2867 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)
2868 {
2869         uiBut *but;
2870
2871         but= ui_def_but_rna(block, type, retval, "", x1, y1, x2, y2, ptr, propname, index, min, max, a1, a2, tip);
2872         if(but) {
2873                 if(icon) {
2874                         but->icon= (BIFIconID) icon;
2875                         but->flag|= UI_HAS_ICON;
2876                 }
2877                 ui_check_but(but);
2878         }
2879
2880         return but;
2881 }
2882 uiBut *uiDefIconButO(uiBlock *block, int type, char *opname, int opcontext, int icon, short x1, short y1, short x2, short y2, char *tip)
2883 {
2884         uiBut *but;
2885
2886         but= ui_def_but_operator(block, type, opname, opcontext, "", x1, y1, x2, y2, tip);
2887         if(but) {
2888                 but->icon= (BIFIconID) icon;
2889                 but->flag|= UI_HAS_ICON;
2890                 ui_check_but(but);
2891         }
2892
2893         return but;
2894 }
2895
2896 /* Button containing both string label and icon */
2897 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)
2898 {
2899         uiBut *but= ui_def_but(block, type, retval, str, x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2900
2901         but->icon= (BIFIconID) icon;
2902         but->flag|= UI_HAS_ICON;
2903
2904         but->flag|= UI_ICON_LEFT;
2905
2906         ui_check_but(but);
2907
2908         return but;
2909 }
2910 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)
2911 {
2912         int bitIdx= findBitIndex(bit);
2913         if (bitIdx==-1) {
2914                 return NULL;
2915         } else {
2916                 return uiDefIconTextBut(block, type|BIT|bitIdx, retval, icon, str, x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2917         }
2918 }
2919
2920 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)
2921 {
2922         return uiDefIconTextBut(block, type|FLO, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2923 }
2924 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)
2925 {
2926         return uiDefIconTextButBit(block, type|FLO, bit, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2927 }
2928 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)
2929 {
2930         return uiDefIconTextBut(block, type|INT, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2931 }
2932 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)
2933 {
2934         return uiDefIconTextButBit(block, type|INT, bit, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2935 }
2936 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)
2937 {
2938         return uiDefIconTextBut(block, type|SHO, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2939 }
2940 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)
2941 {
2942         return uiDefIconTextButBit(block, type|SHO, bit, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2943 }
2944 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)
2945 {
2946         return uiDefIconTextBut(block, type|CHA, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2947 }
2948 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)
2949 {
2950         return uiDefIconTextButBit(block, type|CHA, bit, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2951 }
2952 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)
2953 {
2954         uiBut *but;
2955
2956         but= ui_def_but_rna(block, type, retval, str, x1, y1, x2, y2, ptr, propname, index, min, max, a1, a2, tip);
2957         if(but) {
2958                 if(icon) {
2959                         but->icon= (BIFIconID) icon;
2960                         but->flag|= UI_HAS_ICON;
2961                 }
2962                 but->flag|= UI_ICON_LEFT;
2963                 ui_check_but(but);
2964         }
2965
2966         return but;
2967 }
2968 uiBut *uiDefIconTextButO(uiBlock *block, int type, char *opname, int opcontext, int icon, char *str, short x1, short y1, short x2, short y2, char *tip)
2969 {
2970         uiBut *but;
2971
2972         but= ui_def_but_operator(block, type, opname, opcontext, str, x1, y1, x2, y2, tip);
2973         if(but) {
2974                 but->icon= (BIFIconID) icon;
2975                 but->flag|= UI_HAS_ICON;
2976                 but->flag|= UI_ICON_LEFT;
2977                 ui_check_but(but);
2978         }
2979
2980         return but;
2981 }
2982
2983 /* END Button containing both string label and icon */
2984
2985 void uiSetButLink(uiBut *but, void **poin, void ***ppoin, short *tot, int from, int to)
2986 {
2987         uiLink *link;
2988         
2989         link= but->link= MEM_callocN(sizeof(uiLink), "new uilink");
2990         
2991         link->poin= poin;
2992         link->ppoin= ppoin;
2993         link->totlink= tot;
2994         link->fromcode= from;
2995         link->tocode= to;
2996 }
2997
2998 /* cruft to make uiBlock and uiBut private */
2999
3000 int uiBlocksGetYMin(ListBase *lb)
3001 {
3002         uiBlock *block;
3003         int min= 0;
3004         
3005         for (block= lb->first; block; block= block->next)
3006                 if (block==lb->first || block->miny<min)
3007                         min= block->miny;
3008                         
3009         return min;
3010 }
3011
3012 void uiBlockSetDirection(uiBlock *block, int direction)
3013 {
3014         block->direction= direction;
3015 }
3016
3017 /* this call escapes if there's alignment flags */
3018 void uiBlockFlipOrder(uiBlock *block)
3019 {
3020         ListBase lb;
3021         uiBut *but, *next;
3022         float centy, miny=10000, maxy= -10000;
3023
3024         if(U.uiflag & USER_MENUFIXEDORDER)
3025                 return;
3026         else if(block->flag & UI_BLOCK_NO_FLIP)
3027                 return;
3028         
3029         for(but= block->buttons.first; but; but= but->next) {
3030                 if(but->flag & UI_BUT_ALIGN) return;
3031                 if(but->y1 < miny) miny= but->y1;
3032                 if(but->y2 > maxy) maxy= but->y2;
3033         }
3034         /* mirror trick */
3035         centy= (miny+maxy)/2.0;
3036         for(but= block->buttons.first; but; but= but->next) {
3037                 but->y1 = centy-(but->y1-centy);
3038                 but->y2 = centy-(but->y2-centy);
3039                 SWAP(float, but->y1, but->y2);
3040         }
3041         
3042         /* also flip order in block itself, for example for arrowkey */
3043         lb.first= lb.last= NULL;
3044         but= block->buttons.first;
3045         while(but) {
3046                 next= but->next;
3047                 BLI_remlink(&block->buttons, but);
3048                 BLI_addtail(&lb, but);
3049                 but= next;
3050         }
3051         block->buttons= lb;
3052 }
3053
3054
3055 void uiBlockSetFlag(uiBlock *block, int flag)
3056 {
3057         block->flag|= flag;
3058 }
3059
3060 void uiBlockClearFlag(uiBlock *block, int flag)
3061 {
3062         block->flag&= ~flag;
3063 }
3064
3065 void uiBlockSetXOfs(uiBlock *block, int xofs)
3066 {
3067         block->xofs= xofs;
3068 }
3069
3070 void uiButSetFlag(uiBut *but, int flag)
3071 {
3072         but->flag|= flag;
3073 }
3074
3075 void uiButClearFlag(uiBut *but, int flag)
3076 {
3077         but->flag&= ~flag;
3078 }
3079
3080 int uiButGetRetVal(uiBut *but)
3081 {
3082         return but->retval;
3083 }
3084
3085 void uiButSetDragID(uiBut *but, ID *id)
3086 {
3087         but->dragtype= WM_DRAG_ID;
3088         but->dragpoin= (void *)id;
3089 }
3090
3091 void uiButSetDragRNA(uiBut *but, PointerRNA *ptr)
3092 {
3093         but->dragtype= WM_DRAG_RNA;
3094         but->dragpoin= (void *)ptr;
3095 }
3096
3097 void uiButSetDragPath(uiBut *but, const char *path)
3098 {
3099         but->dragtype= WM_DRAG_PATH;
3100         but->dragpoin= (void *)path;
3101 }
3102
3103 void uiButSetDragName(uiBut *but, const char *name)
3104 {
3105         but->dragtype= WM_DRAG_NAME;
3106         but->dragpoin= (void *)name;
3107 }
3108
3109 /* value from button itself */
3110 void uiButSetDragValue(uiBut *but)
3111 {
3112         but->dragtype= WM_DRAG_VALUE;
3113 }
3114
3115 void uiButSetDragImage(uiBut *but, const char *path, int icon, struct ImBuf *imb, float scale)
3116 {
3117         but->dragtype= WM_DRAG_PATH;
3118         but->icon= icon; /* no flag UI_HAS_ICON, so icon doesnt draw in button */
3119         but->dragpoin= (void *)path;
3120         but->imb= imb;
3121         but->imb_scale= scale;
3122 }
3123
3124 PointerRNA *uiButGetOperatorPtrRNA(uiBut *but)
3125 {
3126         if(but->optype && !but->opptr) {
3127                 but->opptr= MEM_callocN(sizeof(PointerRNA), "uiButOpPtr");
3128                 WM_operator_properties_create_ptr(but->opptr, but->optype);
3129         }
3130
3131         return but->opptr;
3132 }
3133
3134 void uiBlockSetHandleFunc(uiBlock *block, uiBlockHandleFunc func, void *arg)
3135 {
3136         block->handle_func= func;
3137         block->handle_func_arg= arg;
3138 }
3139
3140 void uiBlockSetButmFunc(uiBlock *block, uiMenuHandleFunc func, void *arg)
3141 {
3142         block->butm_func= func;
3143         block->butm_func_arg= arg;
3144 }
3145