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