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