51c38ee859f6b669d3ea753e7ea9333988754c79
[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_math.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= dist_to_line_segment_v2(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                         if(but->flag & UI_BUT_IMMEDIATE)
1962                                 strcpy(but->drawstr, but->str);
1963                         else
1964                                 strcpy(but->drawstr, "");
1965                         
1966                         if(*sp) {
1967                                 char *str= but->drawstr;
1968                                 
1969                                 if(*sp & KM_SHIFT)
1970                                         str= strcat(str, "Shift ");
1971                                 if(*sp & KM_CTRL)
1972                                         str= strcat(str, "Ctrl ");
1973                                 if(*sp & KM_ALT)
1974                                         str= strcat(str, "Alt ");
1975                                 if(*sp & KM_OSKEY)
1976                                         str= strcat(str, "Cmd ");
1977                         }
1978                         else
1979                                 strcat(but->drawstr, "Press a key  ");
1980                 }
1981                 else
1982                         strcpy(but->drawstr, but->str);
1983
1984                 break;
1985                 
1986         case BUT_TOGDUAL:
1987                 /* trying to get the dual-icon to left of text... not very nice */
1988                 if(but->str[0]) {
1989                         strcpy(but->drawstr, "  ");
1990                         strcpy(but->drawstr+2, but->str);
1991                 }
1992                 break;
1993         default:
1994                 strcpy(but->drawstr, but->str);
1995                 
1996         }
1997
1998         /* if we are doing text editing, this will override the drawstr */
1999         if(but->editstr)
2000                 strcpy(but->drawstr, but->editstr);
2001         
2002         /* text clipping moved to widget drawing code itself */
2003 }
2004
2005
2006 void uiBlockBeginAlign(uiBlock *block)
2007 {
2008         /* if other align was active, end it */
2009         if(block->flag & UI_BUT_ALIGN) uiBlockEndAlign(block);
2010
2011         block->flag |= UI_BUT_ALIGN_DOWN;       
2012         block->alignnr++;
2013
2014         /* buttons declared after this call will get this align nr */ // XXX flag?
2015 }
2016
2017 static int buts_are_horiz(uiBut *but1, uiBut *but2)
2018 {
2019         float dx, dy;
2020         
2021         dx= fabs( but1->x2 - but2->x1);
2022         dy= fabs( but1->y1 - but2->y2);
2023         
2024         if(dx > dy) return 0;
2025         return 1;
2026 }
2027
2028 void uiBlockEndAlign(uiBlock *block)
2029 {
2030         block->flag &= ~UI_BUT_ALIGN;   // all 4 flags
2031 }
2032
2033 int ui_but_can_align(uiBut *but)
2034 {
2035         return !ELEM3(but->type, LABEL, OPTION, OPTIONN);
2036 }
2037
2038 static void ui_block_do_align_but(uiBlock *block, uiBut *first, int nr)
2039 {
2040         uiBut *prev, *but=NULL, *next;
2041         int flag= 0, cols=0, rows=0;
2042         
2043         /* auto align */
2044
2045         for(but=first; but && but->alignnr == nr; but=but->next) {
2046                 if(but->next && but->next->alignnr == nr) {
2047                         if(buts_are_horiz(but, but->next)) cols++;
2048                         else rows++;
2049                 }
2050         }
2051
2052         /* rows==0: 1 row, cols==0: 1 collumn */
2053         
2054         /* note;  how it uses 'flag' in loop below (either set it, or OR it) is confusing */
2055         for(but=first, prev=NULL; but && but->alignnr == nr; prev=but, but=but->next) {
2056                 next= but->next;
2057                 if(next && next->alignnr != nr)
2058                         next= NULL;
2059
2060                 /* clear old flag */
2061                 but->flag &= ~UI_BUT_ALIGN;
2062                 
2063                 if(flag==0) {   /* first case */
2064                         if(next) {
2065                                 if(buts_are_horiz(but, next)) {
2066                                         if(rows==0)
2067                                                 flag= UI_BUT_ALIGN_RIGHT;
2068                                         else 
2069                                                 flag= UI_BUT_ALIGN_DOWN|UI_BUT_ALIGN_RIGHT;
2070                                 }
2071                                 else {
2072                                         flag= UI_BUT_ALIGN_DOWN;
2073                                 }
2074                         }
2075                 }
2076                 else if(next==NULL) {   /* last case */
2077                         if(prev) {
2078                                 if(buts_are_horiz(prev, but)) {
2079                                         if(rows==0) 
2080                                                 flag= UI_BUT_ALIGN_LEFT;
2081                                         else
2082                                                 flag= UI_BUT_ALIGN_TOP|UI_BUT_ALIGN_LEFT;
2083                                 }
2084                                 else flag= UI_BUT_ALIGN_TOP;
2085                         }
2086                 }
2087                 else if(buts_are_horiz(but, next)) {
2088                         /* check if this is already second row */
2089                         if( prev && buts_are_horiz(prev, but)==0) {
2090                                 flag &= ~UI_BUT_ALIGN_LEFT;
2091                                 flag |= UI_BUT_ALIGN_TOP;
2092                                 /* exception case: bottom row */
2093                                 if(rows>0) {
2094                                         uiBut *bt= but;
2095                                         while(bt && bt->alignnr == nr) {
2096                                                 if(bt->next && bt->next->alignnr == nr && buts_are_horiz(bt, bt->next)==0 ) break; 
2097                                                 bt= bt->next;
2098                                         }
2099                                         if(bt==0 || bt->alignnr != nr) flag= UI_BUT_ALIGN_TOP|UI_BUT_ALIGN_RIGHT;
2100                                 }
2101                         }
2102                         else flag |= UI_BUT_ALIGN_LEFT;
2103                 }
2104                 else {
2105                         if(cols==0) {
2106                                 flag |= UI_BUT_ALIGN_TOP;
2107                         }
2108                         else {  /* next button switches to new row */
2109                                 
2110                                 if(prev && buts_are_horiz(prev, but))
2111                                    flag |= UI_BUT_ALIGN_LEFT;
2112                                 
2113                                 if( (flag & UI_BUT_ALIGN_TOP)==0) {     /* stil top row */
2114                                         if(prev)
2115                                                 flag= UI_BUT_ALIGN_DOWN|UI_BUT_ALIGN_LEFT;
2116                                         else 
2117                                                 flag |= UI_BUT_ALIGN_DOWN;
2118                                 }
2119                                 else 
2120                                         flag |= UI_BUT_ALIGN_TOP;
2121                         }
2122                 }
2123                 
2124                 but->flag |= flag;
2125                 
2126                 /* merge coordinates */
2127                 if(prev) {
2128                         // simple cases 
2129                         if(rows==0) {
2130                                 but->x1= (prev->x2+but->x1)/2.0;
2131                                 prev->x2= but->x1;
2132                         }
2133                         else if(cols==0) {
2134                                 but->y2= (prev->y1+but->y2)/2.0;
2135                                 prev->y1= but->y2;
2136                         }
2137                         else {
2138                                 if(buts_are_horiz(prev, but)) {
2139                                         but->x1= (prev->x2+but->x1)/2.0;
2140                                         prev->x2= but->x1;
2141                                         /* copy height too */
2142                                         but->y2= prev->y2;
2143                                 }
2144                                 else if(prev->prev && buts_are_horiz(prev->prev, prev)==0) {
2145                                         /* the previous button is a single one in its row */
2146                                         but->y2= (prev->y1+but->y2)/2.0;
2147                                         prev->y1= but->y2;
2148                                 }
2149                                 else {
2150                                         /* the previous button is not a single one in its row */
2151                                         but->y2= prev->y1;
2152                                 }
2153                         }
2154                 }
2155         }
2156 }
2157
2158 void ui_block_do_align(uiBlock *block)
2159 {
2160         uiBut *but;
2161         int nr;
2162
2163         /* align buttons with same align nr */
2164         for(but=block->buttons.first; but;) {
2165                 if(but->alignnr) {
2166                         nr= but->alignnr;
2167                         ui_block_do_align_but(block, but, nr);
2168
2169                         /* skip with same number */
2170                         for(; but && but->alignnr == nr; but=but->next);
2171
2172                         if(!but)
2173                                 break;
2174                 }
2175                 else
2176                         but= but->next;
2177         }
2178 }
2179
2180 /*
2181 ui_def_but is the function that draws many button types
2182
2183 for float buttons:
2184         "a1" Click Step (how much to change the value each click)
2185         "a2" Number of decimal point values to display. 0 defaults to 3 (0.000) 1,2,3, and a maximum of 4,
2186        all greater values will be clamped to 4.
2187
2188 */
2189 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)
2190 {
2191         uiBut *but;
2192         short slen;
2193         
2194         if(type & BUTPOIN) {            /* a pointer is required */
2195                 if(poin==NULL)
2196                         return NULL;
2197         }
2198
2199         but= MEM_callocN(sizeof(uiBut), "uiBut");
2200
2201         but->type= type & BUTTYPE;
2202         but->pointype= type & BUTPOIN;
2203         but->bit= type & BIT;
2204         but->bitnr= type & 31;
2205         but->icon = 0;
2206
2207         but->retval= retval;
2208         if( strlen(str)>=UI_MAX_NAME_STR-1 ) {
2209                 but->str= MEM_callocN( strlen(str)+2, "uiDefBut");
2210                 strcpy(but->str, str);
2211         }
2212         else {
2213                 but->str= but->strdata;
2214                 strcpy(but->str, str);
2215         }
2216         but->x1= x1; 
2217         but->y1= y1;
2218         but->x2= (x1+x2); 
2219         but->y2= (y1+y2);
2220         
2221         but->poin= poin;
2222         but->hardmin= but->softmin= min; 
2223         but->hardmax= but->softmax= max;
2224         but->a1= a1; 
2225         but->a2= a2;
2226         but->tip= tip;
2227         
2228         but->lock= block->lock;
2229         but->lockstr= block->lockstr;
2230         but->dt= block->dt;
2231
2232         but->aspect= 1.0f; //XXX block->aspect;
2233         but->block= block;              // pointer back, used for frontbuffer status, and picker
2234
2235         if((block->flag & UI_BUT_ALIGN) && ui_but_can_align(but))
2236                 but->alignnr= block->alignnr;
2237
2238         but->func= block->func;
2239         but->func_arg1= block->func_arg1;
2240         but->func_arg2= block->func_arg2;
2241
2242         but->funcN= block->funcN;
2243         if(block->func_argN)
2244                 but->func_argN= MEM_dupallocN(block->func_argN);
2245         
2246         but->pos= -1;   /* cursor invisible */
2247
2248         if(ELEM(but->type, NUM, NUMABS)) {      /* add a space to name */
2249                 slen= strlen(but->str);
2250                 if(slen>0 && slen<UI_MAX_NAME_STR-2) {
2251                         if(but->str[slen-1]!=' ') {
2252                                 but->str[slen]= ' ';
2253                                 but->str[slen+1]= 0;
2254                         }
2255                 }
2256         }
2257         
2258         if(ELEM(but->type, HSVCUBE, HSVCIRCLE)) { /* hsv buttons temp storage */
2259                 float rgb[3];
2260                 ui_get_but_vectorf(but, rgb);
2261                 rgb_to_hsv(rgb[0], rgb[1], rgb[2], but->hsv, but->hsv+1, but->hsv+2);
2262         }
2263
2264         if((block->flag & UI_BLOCK_LOOP) || ELEM7(but->type, MENU, TEX, LABEL, IDPOIN, BLOCK, BUTM, SEARCH_MENU))
2265                 but->flag |= (UI_TEXT_LEFT|UI_ICON_LEFT);
2266         else if(but->type==BUT_TOGDUAL)
2267                 but->flag |= UI_ICON_LEFT;
2268
2269         but->flag |= (block->flag & UI_BUT_ALIGN);
2270
2271         if (but->lock) {
2272                 if (but->lockstr) {
2273                         but->flag |= UI_BUT_DISABLED;
2274                 }
2275         }
2276
2277         if(ELEM8(but->type, BLOCK, BUT, LABEL, PULLDOWN, ROUNDBOX, LISTBOX, SEARCH_MENU, BUTM));
2278         else if(ELEM5(but->type, SCROLL, SEPR, LINK, INLINK, FTPREVIEW));
2279         else but->flag |= UI_BUT_UNDO;
2280
2281         BLI_addtail(&block->buttons, but);
2282         
2283         if(block->curlayout)
2284                 ui_layout_add_but(block->curlayout, but);
2285
2286         return but;
2287 }
2288
2289 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)
2290 {
2291         uiBut *but;
2292         PropertyRNA *prop;
2293         PropertyType proptype;
2294         int freestr= 0, icon= 0;
2295
2296         prop= RNA_struct_find_property(ptr, propname);
2297
2298         if(prop) {
2299                 proptype= RNA_property_type(prop);
2300
2301                 /* use rna values if parameters are not specified */
2302                 if(!str) {
2303                         if(type == MENU && proptype == PROP_ENUM) {
2304                                 EnumPropertyItem *item;
2305                                 DynStr *dynstr;
2306                                 int i, totitem, value, free;
2307
2308                                 RNA_property_enum_items(block->evil_C, ptr, prop, &item, &totitem, &free);
2309                                 value= RNA_property_enum_get(ptr, prop);
2310
2311                                 dynstr= BLI_dynstr_new();
2312                                 BLI_dynstr_appendf(dynstr, "%s%%t", RNA_property_ui_name(prop));
2313                                 for(i=0; i<totitem; i++) {
2314                                         if(!item[i].identifier[0]) {
2315                                                 if(item[i].name)
2316                                                         BLI_dynstr_appendf(dynstr, "|%s%%l", item[i].name);
2317                                                 else
2318                                                         BLI_dynstr_append(dynstr, "|%l");
2319                                         }
2320                                         else if(item[i].icon)
2321                                                 BLI_dynstr_appendf(dynstr, "|%s %%i%d %%x%d", item[i].name, item[i].icon, item[i].value);
2322                                         else
2323                                                 BLI_dynstr_appendf(dynstr, "|%s %%x%d", item[i].name, item[i].value);
2324
2325                                         if(value == item[i].value)
2326                                                 icon= item[i].icon;
2327                                 }
2328                                 str= BLI_dynstr_get_cstring(dynstr);
2329                                 BLI_dynstr_free(dynstr);
2330
2331                                 if(free)
2332                                         MEM_freeN(item);
2333
2334                                 freestr= 1;
2335                         }
2336                         else if(ELEM(type, ROW, LISTROW) && proptype == PROP_ENUM) {
2337                                 EnumPropertyItem *item;
2338                                 int i, totitem, free;
2339
2340                                 RNA_property_enum_items(block->evil_C, ptr, prop, &item, &totitem, &free);
2341                                 for(i=0; i<totitem; i++) {
2342                                         if(item[i].identifier[0] && item[i].value == (int)max) {
2343                                                 str= (char*)item[i].name;
2344                                                 icon= item[i].icon;
2345                                         }
2346                                 }
2347
2348                                 if(!str)
2349                                         str= (char*)RNA_property_ui_name(prop);
2350                                 if(free)
2351                                         MEM_freeN(item);
2352                         }
2353                         else {
2354                                 str= (char*)RNA_property_ui_name(prop);
2355                                 icon= RNA_property_ui_icon(prop);
2356                         }
2357                 }
2358
2359                 if(!tip) {
2360                         if(type == ROW && proptype == PROP_ENUM) {
2361                                 EnumPropertyItem *item;
2362                                 int i, totitem, free;
2363
2364                                 RNA_property_enum_items(block->evil_C, ptr, prop, &item, &totitem, &free);
2365
2366                                 for(i=0; i<totitem; i++) {
2367                                         if(item[i].identifier[0] && item[i].value == (int)max) {
2368                                                 if(item[i].description[0])
2369                                                         tip= (char*)item[i].description;
2370                                                 break;
2371                                         }
2372                                 }
2373
2374                                 if(free)
2375                                         MEM_freeN(item);
2376                         }
2377                 }
2378                 
2379                 if(!tip)
2380                         tip= (char*)RNA_property_ui_description(prop);
2381
2382                 if(min == max || a1 == -1 || a2 == -1) {
2383                         if(proptype == PROP_INT) {
2384                                 int hardmin, hardmax, softmin, softmax, step;
2385
2386                                 RNA_property_int_range(ptr, prop, &hardmin, &hardmax);
2387                                 RNA_property_int_ui_range(ptr, prop, &softmin, &softmax, &step);
2388
2389                                 if(!ELEM(type, ROW, LISTROW) && min == max) {
2390                                         min= hardmin;
2391                                         max= hardmax;
2392                                 }
2393                                 if(a1 == -1)
2394                                         a1= step;
2395                                 if(a2 == -1)
2396                                         a2= 0;
2397                         }
2398                         else if(proptype == PROP_FLOAT) {
2399                                 float hardmin, hardmax, softmin, softmax, step, precision;
2400
2401                                 RNA_property_float_range(ptr, prop, &hardmin, &hardmax);
2402                                 RNA_property_float_ui_range(ptr, prop, &softmin, &softmax, &step, &precision);
2403
2404                                 if(!ELEM(type, ROW, LISTROW) && min == max) {
2405                                         min= hardmin;
2406                                         max= hardmax;
2407                                 }
2408                                 if(a1 == -1)
2409                                         a1= step;
2410                                 if(a2 == -1)
2411                                         a2= precision;
2412                         }
2413                         else if(proptype == PROP_STRING) {
2414                                 min= 0;
2415                                 max= RNA_property_string_maxlength(prop);
2416                                 if(max == 0) /* interface code should ideally support unlimited length */
2417                                         max= UI_MAX_DRAW_STR; 
2418                         }
2419                 }
2420         }
2421         else
2422                 str= (char*)propname;
2423
2424         /* now create button */
2425         but= ui_def_but(block, type, retval, str, x1, y1, x2, y2, NULL, min, max, a1, a2, tip);
2426
2427         if(prop) {
2428                 but->rnapoin= *ptr;
2429                 but->rnaprop= prop;
2430
2431                 if(RNA_property_array_length(&but->rnapoin, but->rnaprop))
2432                         but->rnaindex= index;
2433                 else
2434                         but->rnaindex= 0;
2435         }
2436
2437         if(icon) {
2438                 but->icon= (BIFIconID)icon;
2439                 but->flag |= UI_HAS_ICON;
2440                 but->flag|= UI_ICON_LEFT;
2441         }
2442         
2443         if (!prop || !RNA_property_editable(&but->rnapoin, prop)) {
2444                 but->flag |= UI_BUT_DISABLED;
2445                 but->lock = 1;
2446                 but->lockstr = "";
2447         }
2448
2449         /* If this button uses units, calculate the step from this */
2450         if(ui_is_but_unit(but))
2451                 but->a1= ui_get_but_step_unit(but, ui_get_but_val(but), but->a1);
2452
2453         if(freestr)
2454                 MEM_freeN(str);
2455         
2456         return but;
2457 }
2458
2459 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)
2460 {
2461         uiBut *but;
2462         wmOperatorType *ot;
2463         
2464         ot= WM_operatortype_find(opname, 0);
2465
2466         if(!str) {
2467                 if(ot) str= ot->name;
2468                 else str= opname;
2469         }
2470         
2471         if ((!tip || tip[0]=='\0') && ot && ot->description) {
2472                 tip= ot->description;
2473         }
2474
2475         but= ui_def_but(block, type, -1, str, x1, y1, x2, y2, NULL, 0, 0, 0, 0, tip);
2476         but->optype= ot;
2477         but->opcontext= opcontext;
2478
2479         if(!ot) {
2480                 but->flag |= UI_BUT_DISABLED;
2481                 but->lock = 1;
2482                 but->lockstr = "";
2483         }
2484
2485         return but;
2486 }
2487
2488 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)
2489 {
2490         uiBut *but= ui_def_but(block, type, retval, str, x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2491
2492         ui_check_but(but);
2493         
2494         return but;
2495 }
2496
2497         /* if _x_ is a power of two (only one bit) return the power,
2498          * otherwise return -1. 
2499          * (1<<findBitIndex(x))==x for powers of two.
2500          */
2501 static int findBitIndex(unsigned int x) {
2502         if (!x || (x&(x-1))!=0) {       /* x&(x-1) strips lowest bit */
2503                 return -1;
2504         } else {
2505                 int idx= 0;
2506
2507                 if (x&0xFFFF0000)       idx+=16, x>>=16;
2508                 if (x&0xFF00)           idx+=8, x>>=8;
2509                 if (x&0xF0)                     idx+=4, x>>=4;
2510                 if (x&0xC)                      idx+=2, x>>=2;
2511                 if (x&0x2)                      idx+=1;
2512
2513                 return idx;
2514         }
2515 }
2516
2517 /* autocomplete helper functions */
2518 struct AutoComplete {
2519         int maxlen;
2520         char *truncate;
2521         char *startname;
2522 };
2523
2524 AutoComplete *autocomplete_begin(char *startname, int maxlen)
2525 {
2526         AutoComplete *autocpl;
2527         
2528         autocpl= MEM_callocN(sizeof(AutoComplete), "AutoComplete");
2529         autocpl->maxlen= maxlen;
2530         autocpl->truncate= MEM_callocN(sizeof(char)*maxlen, "AutoCompleteTruncate");
2531         autocpl->startname= startname;
2532
2533         return autocpl;
2534 }
2535
2536 void autocomplete_do_name(AutoComplete *autocpl, const char *name)
2537 {
2538         char *truncate= autocpl->truncate;
2539         char *startname= autocpl->startname;
2540         int a;
2541
2542         for(a=0; a<autocpl->maxlen-1; a++) {
2543                 if(startname[a]==0 || startname[a]!=name[a])
2544                         break;
2545         }
2546         /* found a match */
2547         if(startname[a]==0) {
2548                 /* first match */
2549                 if(truncate[0]==0)
2550                         BLI_strncpy(truncate, name, autocpl->maxlen);
2551                 else {
2552                         /* remove from truncate what is not in bone->name */
2553                         for(a=0; a<autocpl->maxlen-1; a++) {
2554                                 if(name[a] == 0) {
2555                                         truncate[a]= 0;
2556                                         break;
2557                                 }
2558                                 else if(truncate[a]!=name[a])
2559                                         truncate[a]= 0;
2560                         }
2561                 }
2562         }
2563 }
2564
2565 void autocomplete_end(AutoComplete *autocpl, char *autoname)
2566 {       
2567         if(autocpl->truncate[0])
2568                 BLI_strncpy(autoname, autocpl->truncate, autocpl->maxlen);
2569         else {
2570                 if (autoname != autocpl->startname) /* dont copy a string over its self */
2571                         BLI_strncpy(autoname, autocpl->startname, autocpl->maxlen);
2572         }
2573         MEM_freeN(autocpl->truncate);
2574         MEM_freeN(autocpl);
2575 }
2576
2577 /* autocomplete callback for ID buttons */
2578 static void autocomplete_id(bContext *C, char *str, void *arg_v)
2579 {
2580         int blocktype= (intptr_t)arg_v;
2581         ListBase *listb= wich_libbase(CTX_data_main(C), blocktype);
2582         
2583         if(listb==NULL) return;
2584         
2585         /* search if str matches the beginning of an ID struct */
2586         if(str[0]) {
2587                 AutoComplete *autocpl= autocomplete_begin(str, 22);
2588                 ID *id;
2589                 
2590                 for(id= listb->first; id; id= id->next)
2591                         autocomplete_do_name(autocpl, id->name+2);
2592
2593                 autocomplete_end(autocpl, str);
2594         }
2595 }
2596
2597 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)
2598 {
2599         int bitIdx= findBitIndex(bit);
2600         if (bitIdx==-1) {
2601                 return NULL;
2602         } else {
2603                 return uiDefBut(block, type|BIT|bitIdx, retval, str, x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2604         }
2605 }
2606 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)
2607 {
2608         return uiDefBut(block, type|FLO, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2609 }
2610 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)
2611 {
2612         return uiDefButBit(block, type|FLO, bit, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2613 }
2614 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)
2615 {
2616         return uiDefBut(block, type|INT, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2617 }
2618 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)
2619 {
2620         return uiDefButBit(block, type|INT, bit, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2621 }
2622 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)
2623 {
2624         return uiDefBut(block, type|SHO, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2625 }
2626 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)
2627 {
2628         return uiDefButBit(block, type|SHO, bit, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2629 }
2630 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)
2631 {
2632         return uiDefBut(block, type|CHA, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2633 }
2634 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)
2635 {
2636         return uiDefButBit(block, type|CHA, bit, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2637 }
2638 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)
2639 {
2640         uiBut *but;
2641
2642         but= ui_def_but_rna(block, type, retval, str, x1, y1, x2, y2, ptr, propname, index, min, max, a1, a2, tip);
2643         if(but)
2644                 ui_check_but(but);
2645
2646         return but;
2647 }
2648 uiBut *uiDefButO(uiBlock *block, int type, char *opname, int opcontext, char *str, short x1, short y1, short x2, short y2, char *tip)
2649 {
2650         uiBut *but;
2651
2652         but= ui_def_but_operator(block, type, opname, opcontext, str, x1, y1, x2, y2, tip);
2653         if(but)
2654                 ui_check_but(but);
2655
2656         return but;
2657 }
2658
2659 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)
2660 {
2661         uiBut *but= ui_def_but(block, type, retval, "", x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2662         
2663         but->icon= (BIFIconID) icon;
2664         but->flag|= UI_HAS_ICON;
2665
2666         ui_check_but(but);
2667         
2668         return but;
2669 }
2670 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)
2671 {
2672         int bitIdx= findBitIndex(bit);
2673         if (bitIdx==-1) {
2674                 return NULL;
2675         } else {
2676                 return uiDefIconBut(block, type|BIT|bitIdx, retval, icon, x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2677         }
2678 }
2679
2680 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)
2681 {
2682         return uiDefIconBut(block, type|FLO, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2683 }
2684 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)
2685 {
2686         return uiDefIconButBit(block, type|FLO, bit, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2687 }
2688 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)
2689 {
2690         return uiDefIconBut(block, type|INT, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2691 }
2692 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)
2693 {
2694         return uiDefIconButBit(block, type|INT, bit, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2695 }
2696 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)
2697 {
2698         return uiDefIconBut(block, type|SHO, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2699 }
2700 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)
2701 {
2702         return uiDefIconButBit(block, type|SHO, bit, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2703 }
2704 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)
2705 {
2706         return uiDefIconBut(block, type|CHA, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2707 }
2708 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)
2709 {
2710         return uiDefIconButBit(block, type|CHA, bit, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2711 }
2712 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)
2713 {
2714         uiBut *but;
2715
2716         but= ui_def_but_rna(block, type, retval, "", x1, y1, x2, y2, ptr, propname, index, min, max, a1, a2, tip);
2717         if(but) {
2718                 if(icon) {
2719                         but->icon= (BIFIconID) icon;
2720                         but->flag|= UI_HAS_ICON;
2721                 }
2722                 ui_check_but(but);
2723         }
2724
2725         return but;
2726 }
2727 uiBut *uiDefIconButO(uiBlock *block, int type, char *opname, int opcontext, int icon, short x1, short y1, short x2, short y2, char *tip)
2728 {
2729         uiBut *but;
2730
2731         but= ui_def_but_operator(block, type, opname, opcontext, "", x1, y1, x2, y2, tip);
2732         if(but) {
2733                 but->icon= (BIFIconID) icon;
2734                 but->flag|= UI_HAS_ICON;
2735                 ui_check_but(but);
2736         }
2737
2738         return but;
2739 }
2740
2741 /* Button containing both string label and icon */
2742 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)
2743 {
2744         uiBut *but= ui_def_but(block, type, retval, str, x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2745
2746         but->icon= (BIFIconID) icon;
2747         but->flag|= UI_HAS_ICON;
2748
2749         but->flag|= UI_ICON_LEFT;
2750
2751         ui_check_but(but);
2752
2753         return but;
2754 }
2755 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)
2756 {
2757         int bitIdx= findBitIndex(bit);
2758         if (bitIdx==-1) {
2759                 return NULL;
2760         } else {
2761                 return uiDefIconTextBut(block, type|BIT|bitIdx, retval, icon, str, x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2762         }
2763 }
2764
2765 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)
2766 {
2767         return uiDefIconTextBut(block, type|FLO, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2768 }
2769 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)
2770 {
2771         return uiDefIconTextButBit(block, type|FLO, bit, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2772 }
2773 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)
2774 {
2775         return uiDefIconTextBut(block, type|INT, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2776 }
2777 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)
2778 {
2779         return uiDefIconTextButBit(block, type|INT, bit, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2780 }
2781 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)
2782 {
2783         return uiDefIconTextBut(block, type|SHO, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2784 }
2785 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)
2786 {
2787         return uiDefIconTextButBit(block, type|SHO, bit, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2788 }
2789 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)
2790 {
2791         return uiDefIconTextBut(block, type|CHA, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2792 }
2793 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)
2794 {
2795         return uiDefIconTextButBit(block, type|CHA, bit, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2796 }
2797 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)
2798 {
2799         uiBut *but;
2800
2801         but= ui_def_but_rna(block, type, retval, str, x1, y1, x2, y2, ptr, propname, index, min, max, a1, a2, tip);
2802         if(but) {
2803                 if(icon) {
2804                         but->icon= (BIFIconID) icon;
2805                         but->flag|= UI_HAS_ICON;
2806                 }
2807                 but->flag|= UI_ICON_LEFT;
2808                 ui_check_but(but);
2809         }
2810
2811         return but;
2812 }
2813 uiBut *uiDefIconTextButO(uiBlock *block, int type, char *opname, int opcontext, int icon, char *str, short x1, short y1, short x2, short y2, char *tip)
2814 {
2815         uiBut *but;
2816
2817         but= ui_def_but_operator(block, type, opname, opcontext, str, x1, y1, x2, y2, tip);
2818         if(but) {
2819                 but->icon= (BIFIconID) icon;
2820                 but->flag|= UI_HAS_ICON;
2821                 but->flag|= UI_ICON_LEFT;
2822                 ui_check_but(but);
2823         }
2824
2825         return but;
2826 }
2827
2828 /* END Button containing both string label and icon */
2829
2830 void uiSetButLink(uiBut *but, void **poin, void ***ppoin, short *tot, int from, int to)
2831 {
2832         uiLink *link;
2833         
2834         link= but->link= MEM_callocN(sizeof(uiLink), "new uilink");
2835         
2836         link->poin= poin;
2837         link->ppoin= ppoin;
2838         link->totlink= tot;
2839         link->fromcode= from;
2840         link->tocode= to;
2841 }
2842
2843 /* cruft to make uiBlock and uiBut private */
2844
2845 int uiBlocksGetYMin(ListBase *lb)
2846 {
2847         uiBlock *block;
2848         int min= 0;
2849         
2850         for (block= lb->first; block; block= block->next)
2851                 if (block==lb->first || block->miny<min)
2852                         min= block->miny;
2853                         
2854         return min;
2855 }
2856
2857 void uiBlockSetDirection(uiBlock *block, int direction)
2858 {
2859         block->direction= direction;
2860 }
2861
2862 /* this call escapes if there's alignment flags */
2863 void uiBlockFlipOrder(uiBlock *block)
2864 {
2865         ListBase lb;
2866         uiBut *but, *next;
2867         float centy, miny=10000, maxy= -10000;
2868
2869         if(U.uiflag & USER_MENUFIXEDORDER)
2870                 return;
2871         else if(block->flag & UI_BLOCK_NO_FLIP)
2872                 return;
2873         
2874         for(but= block->buttons.first; but; but= but->next) {
2875                 if(but->flag & UI_BUT_ALIGN) return;
2876                 if(but->y1 < miny) miny= but->y1;
2877                 if(but->y2 > maxy) maxy= but->y2;
2878         }
2879         /* mirror trick */
2880         centy= (miny+maxy)/2.0;
2881         for(but= block->buttons.first; but; but= but->next) {
2882                 but->y1 = centy-(but->y1-centy);
2883                 but->y2 = centy-(but->y2-centy);
2884                 SWAP(float, but->y1, but->y2);
2885         }
2886         
2887         /* also flip order in block itself, for example for arrowkey */
2888         lb.first= lb.last= NULL;
2889         but= block->buttons.first;
2890         while(but) {
2891                 next= but->next;
2892                 BLI_remlink(&block->buttons, but);
2893                 BLI_addtail(&lb, but);
2894                 but= next;
2895         }
2896         block->buttons= lb;
2897 }
2898
2899
2900 void uiBlockSetFlag(uiBlock *block, int flag)
2901 {
2902         block->flag|= flag;
2903 }
2904
2905 void uiBlockClearFlag(uiBlock *block, int flag)
2906 {
2907         block->flag&= ~flag;
2908 }
2909
2910 void uiBlockSetXOfs(uiBlock *block, int xofs)
2911 {
2912         block->xofs= xofs;
2913 }
2914
2915 void uiButSetFlag(uiBut *but, int flag)
2916 {
2917         but->flag|= flag;
2918 }
2919
2920 void uiButClearFlag(uiBut *but, int flag)
2921 {
2922         but->flag&= ~flag;
2923 }
2924
2925 int uiButGetRetVal(uiBut *but)
2926 {
2927         return but->retval;
2928 }
2929
2930 PointerRNA *uiButGetOperatorPtrRNA(uiBut *but)
2931 {
2932         if(but->optype && !but->opptr) {
2933                 but->opptr= MEM_callocN(sizeof(PointerRNA), "uiButOpPtr");
2934                 WM_operator_properties_create(but->opptr, but->optype->idname);
2935         }
2936
2937         return but->opptr;
2938 }
2939
2940 void uiBlockSetHandleFunc(uiBlock *block, uiBlockHandleFunc func, void *arg)
2941 {
2942         block->handle_func= func;
2943         block->handle_func_arg= arg;
2944 }
2945
2946 void uiBlockSetButmFunc(uiBlock *block, uiMenuHandleFunc func, void *arg)
2947 {
2948         block->butm_func= func;
2949         block->butm_func_arg= arg;
2950 }
2951
2952 void uiBlockSetFunc(uiBlock *block, uiButHandleFunc func, void *arg1, void *arg2)
2953 {
2954         block->func= func;
2955         block->func_arg1= arg1;
2956         block->func_arg2= arg2;
2957 }
2958
2959 void uiBlockSetNFunc(uiBlock *block, uiButHandleFunc func, void *argN, void *arg2)
2960 {
2961         if(block->func_argN)
2962                 MEM_freeN(block->func_argN);
2963
2964         block->funcN= func;
2965         block->func_argN= argN;
2966         block->func_arg2= arg2;
2967 }
2968
2969 void uiButSetRenameFunc(uiBut *but, uiButHandleRenameFunc func, void *arg1)
2970 {
2971         but->rename_func= func;
2972         but->rename_arg1= arg1;
2973 }
2974
2975 void uiBlockSetDrawExtraFunc(uiBlock *block, void (*func)(const bContext *C, void *idv, void *arg1, void *arg2, rcti *rect), void *arg1, void *arg2)
2976 {
2977         block->drawextra= func;
2978         block->drawextra_arg1= arg1;
2979         block->drawextra_arg2= arg2;
2980 }
2981
2982 void uiButSetFunc(uiBut *but, uiButHandleFunc func, void *arg1, void *arg2)
2983 {
2984         but->func= func;
2985         but->func_arg1= arg1;
2986         but->func_arg2= arg2;
2987 }
2988
2989 void uiButSetNFunc(uiBut *but, uiButHandleNFunc funcN, void *argN, void *arg2)
2990 {
2991         if(but->func_argN)
2992                 MEM_freeN(but->func_argN);
2993
2994         but->funcN= funcN;
2995         but->func_argN= argN;
2996         but->func_arg2= arg2;
2997 }
2998
2999 void uiButSetCompleteFunc(uiBut *but, uiButCompleteFunc func, void *arg)
3000 {
3001         but->autocomplete_func= func;
3002         but->autofunc_arg= arg;
3003 }
3004
3005 uiBut *uiDefIDPoinBut(uiBlock *block, uiIDPoinFuncFP func, short blocktype, int retval, char *str, short x1, short y1, short x2, short y2, void *idpp, char *tip)
3006 {
3007         uiBut *but= ui_def_but(block, IDPOIN, retval, str, x1, y1, x2, y2, NULL, 0.0, 0.0, 0.0, 0.0, tip);
3008         but->idpoin_func= func;
3009         but->idpoin_idpp= (ID**) idpp;
3010         ui_check_but(but);
3011         
3012         if(blocktype)
3013                 uiButSetCompleteFunc(but, autocomplete_id, (void *)(intptr_t)blocktype);
3014
3015         return but;
3016 }
3017
3018 uiBut *uiDefBlockBut(uiBlock *block, uiBlockCreateFunc func, void *arg, char *str, short x1, short y1, short x2, short y2, char *tip)
3019 {
3020         uiBut *but= ui_def_but(block, BLOCK, 0, str, x1, y1, x2, y2, arg, 0.0, 0.0, 0.0, 0.0, tip);
3021         but->block_create_func= func;
3022         ui_check_but(but);
3023         return but;
3024 }
3025
3026 uiBut *uiDefBlockButN(uiBlock *block, uiBlockCreateFunc func, void *argN, char *str, short x1, short y1, short x2, short y2, char *tip)
3027 {
3028         uiBut *but= ui_def_but(block, BLOCK, 0, str, x1, y1, x2, y2, NULL, 0.0, 0.0, 0.0, 0.0, tip);
3029         but->block_create_func= func;
3030         if(but->func_argN)
3031                 MEM_freeN(but->func_argN);
3032         but->func_argN= argN;
3033         ui_check_but(but);
3034         return but;
3035 }
3036
3037
3038 uiBut *uiDefPulldownBut(uiBlock *block, uiBlockCreateFunc func, void *arg, char *str, short x1, short y1, short x2, short y2, char *tip)
3039 {
3040         uiBut *but= ui_def_but(block, PULLDOWN, 0, str, x1, y1, x2, y2, arg, 0.0, 0.0, 0.0, 0.0, tip);
3041         but->block_create_func= func;
3042         ui_check_but(but);
3043         return but;
3044 }
3045
3046 uiBut *uiDefMenuBut(uiBlock *block, uiMenuCreateFunc func, void *arg, char *str, short x1, short y1, short x2, short y2, char *tip)
3047 {
3048         uiBut *but= ui_def_but(block, PULLDOWN, 0, str, x1, y1, x2, y2, arg, 0.0, 0.0, 0.0, 0.0, tip);
3049         but->menu_create_func= func;
3050         ui_check_but(but);
3051         return but;
3052 }
3053
3054 uiBut *uiDefIconTextMenuBut(uiBlock *block, uiMenuCreateFunc func, void *arg, int icon, char *str, short x1, short y1, short x2, short y2, char *tip)
3055 {
3056         uiBut *but= ui_def_but(block, PULLDOWN, 0, str, x1, y1, x2, y2, arg, 0.0, 0.0, 0.0, 0.0, tip);
3057
3058         but->icon= (BIFIconID) icon;
3059         but->flag|= UI_HAS_ICON;
3060
3061         but->flag|= UI_ICON_LEFT;
3062         but->flag|= UI_ICON_SUBMENU;
3063
3064         but->menu_create_func= func;
3065         ui_check_but(but);
3066
3067         return but;
3068 }
3069
3070 uiBut *uiDefIconMenuBut(uiBlock *block, uiMenuCreateFunc func, void *arg, int icon, short x1, short y1, short x2, short y2, char *tip)
3071 {
3072         uiBut *but= ui_def_but(block, PULLDOWN, 0, "", x1, y1, x2, y2, arg, 0.0, 0.0, 0.0, 0.0, tip);
3073
3074         but->icon= (BIFIconID) icon;
3075         but->flag |= UI_HAS_ICON;
3076         but->flag &=~ UI_ICON_LEFT;
3077
3078         but->menu_create_func= func;
3079         ui_check_but(but);
3080
3081         return but;
3082 }
3083
3084 /* Block button containing both string label and icon */
3085 uiBut *uiDefIconTextBlockBut(uiBlock *block, uiBlockCreateFunc func, void *arg, int icon, char *str, short x1, short y1, short x2, short y2, char *tip)
3086 {
3087         uiBut *but= ui_def_but(block, BLOCK, 0, str, x1, y1, x2, y2, arg, 0.0, 0.0, 0.0, 0.0, tip);
3088         
3089         /* XXX temp, old menu calls pass on icon arrow, which is now UI_ICON_SUBMENU flag */
3090         if(icon!=ICON_RIGHTARROW_THIN) {
3091                 but->icon= (BIFIconID) icon;
3092                 but->flag|= UI_ICON_LEFT;
3093         }
3094         but->flag|= UI_HAS_ICON;
3095         but->flag|= UI_ICON_SUBMENU;
3096
3097         but->block_create_func= func;
3098         ui_check_but(but);
3099         
3100         return but;
3101 }
3102
3103 /* Block button containing icon */
3104 uiBut *uiDefIconBlockBut(uiBlock *block, uiBlockCreateFunc func, void *arg, int retval, int icon, short x1, short y1, short x2, short y2, char *tip)
3105 {
3106         uiBut *but= ui_def_but(block, BLOCK, retval, "", x1, y1, x2, y2, arg, 0.0, 0.0, 0.0, 0.0, tip);
3107         
3108         but->icon= (BIFIconID) icon;
3109         but->flag|= UI_HAS_ICON;
3110         
3111         but->flag|= UI_ICON_LEFT;
3112         
3113         but->block_create_func= func;
3114         ui_check_but(but);
3115         
3116         return but;
3117 }
3118
3119 uiBut *uiDefKeyevtButS(uiBlock *block, int retval, char *str, short x1, short y1, short x2, short y2, short *spoin, char *tip)
3120 {
3121         uiBut *but= ui_def_but(block, KEYEVT|SHO, retval, str, x1, y1, x2, y2, spoin, 0.0, 0.0, 0.0, 0.0, tip);
3122         ui_check_but(but);
3123         return but;
3124 }
3125
3126 /* short pointers hardcoded */
3127 /* modkeypoin will be set to KM_SHIFT, KM_ALT, KM_CTRL, KM_OSKEY bits */
3128 uiBut *uiDefHotKeyevtButS(uiBlock *block, int retval, char *str, short x1, short y1, short x2, short y2, short *keypoin, short *modkeypoin, char *tip)
3129 {
3130         uiBut *but= ui_def_but(block, HOTKEYEVT|SHO, retval, str, x1, y1, x2, y2, keypoin, 0.0, 0.0, 0.0, 0.0, tip);
3131         but->func_arg3= modkeypoin; /* XXX hrmf, abuse! */
3132         ui_check_but(but);
3133         return but;
3134 }
3135
3136
3137 /* arg is pointer to string/name, use uiButSetSearchFunc() below to make this work */
3138 uiBut *uiDefSearchBut(uiBlock *block, void *arg, int retval, int icon, int maxlen, short x1, short y1, short x2, short y2, char *tip)
3139 {
3140         uiBut *but= ui_def_but(block, SEARCH_MENU, retval, "", x1, y1, x2, y2, arg, 0.0, maxlen, 0.0, 0.0, tip);
3141         
3142         but->icon= (BIFIconID) icon;
3143         but->flag|= UI_HAS_ICON;
3144         
3145         but->flag|= UI_ICON_LEFT|UI_TEXT_LEFT;
3146         
3147         ui_check_but(but);
3148         
3149         return but;
3150 }
3151
3152 /* arg is user value, searchfunc and handlefunc both get it as arg */
3153 /* if active set, button opens with this item visible and selected */
3154 void uiButSetSearchFunc(uiBut *but, uiButSearchFunc sfunc, void *arg, uiButHandleFunc bfunc, void *active)
3155 {
3156         but->search_func= sfunc;
3157         but->search_arg= arg;
3158         
3159         uiButSetFunc(but, bfunc, arg, active);
3160 }
3161
3162 /* Program Init/Exit */
3163
3164 void UI_init(void)
3165 {
3166         ui_resources_init();
3167 }
3168
3169 /* after reading userdef file */
3170 void UI_init_userdef(void)
3171 {
3172         /* fix saved themes */
3173         init_userdef_do_versions();
3174         /* set default colors in default theme */
3175         ui_theme_init_userdef();
3176