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