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