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