RNA
[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 static void ui_rna_ID_autocomplete(bContext *C, char *str, void *arg_but);
84
85 /* ************* translation ************** */
86
87 int ui_translate_buttons()
88 {
89         return (U.transopts & USER_TR_BUTTONS);
90 }
91
92 int ui_translate_menus()
93 {
94         return (U.transopts & USER_TR_MENUS);
95 }
96
97 int ui_translate_tooltips()
98 {
99         return (U.transopts & USER_TR_TOOLTIPS);
100 }
101
102 /* ************* window matrix ************** */
103
104 void ui_block_to_window_fl(const ARegion *ar, uiBlock *block, float *x, float *y)
105 {
106         float gx, gy;
107         int sx, sy, getsizex, getsizey;
108
109         getsizex= ar->winrct.xmax-ar->winrct.xmin+1;
110         getsizey= ar->winrct.ymax-ar->winrct.ymin+1;
111         sx= ar->winrct.xmin;
112         sy= ar->winrct.ymin;
113
114         gx= *x;
115         gy= *y;
116
117         if(block->panel) {
118                 gx += block->panel->ofsx;
119                 gy += block->panel->ofsy;
120         }
121
122         *x= ((float)sx) + ((float)getsizex)*(0.5+ 0.5*(gx*block->winmat[0][0]+ gy*block->winmat[1][0]+ block->winmat[3][0]));
123         *y= ((float)sy) + ((float)getsizey)*(0.5+ 0.5*(gx*block->winmat[0][1]+ gy*block->winmat[1][1]+ block->winmat[3][1]));
124 }
125
126 void ui_block_to_window(const ARegion *ar, uiBlock *block, int *x, int *y)
127 {
128         float fx, fy;
129
130         fx= *x;
131         fy= *y;
132
133         ui_block_to_window_fl(ar, block, &fx, &fy);
134
135         *x= (int)(fx+0.5f);
136         *y= (int)(fy+0.5f);
137 }
138
139 void ui_block_to_window_rct(const ARegion *ar, uiBlock *block, rctf *graph, rcti *winr)
140 {
141         rctf tmpr;
142
143         tmpr= *graph;
144         ui_block_to_window_fl(ar, block, &tmpr.xmin, &tmpr.ymin);
145         ui_block_to_window_fl(ar, block, &tmpr.xmax, &tmpr.ymax);
146
147         winr->xmin= tmpr.xmin;
148         winr->ymin= tmpr.ymin;
149         winr->xmax= tmpr.xmax;
150         winr->ymax= tmpr.ymax;
151 }
152
153 void ui_window_to_block_fl(const ARegion *ar, uiBlock *block, float *x, float *y)       /* for mouse cursor */
154 {
155         float a, b, c, d, e, f, px, py;
156         int sx, sy, getsizex, getsizey;
157
158         getsizex= ar->winrct.xmax-ar->winrct.xmin+1;
159         getsizey= ar->winrct.ymax-ar->winrct.ymin+1;
160         sx= ar->winrct.xmin;
161         sy= ar->winrct.ymin;
162
163         a= .5*((float)getsizex)*block->winmat[0][0];
164         b= .5*((float)getsizex)*block->winmat[1][0];
165         c= .5*((float)getsizex)*(1.0+block->winmat[3][0]);
166
167         d= .5*((float)getsizey)*block->winmat[0][1];
168         e= .5*((float)getsizey)*block->winmat[1][1];
169         f= .5*((float)getsizey)*(1.0+block->winmat[3][1]);
170
171         px= *x - sx;
172         py= *y - sy;
173
174         *y=  (a*(py-f) + d*(c-px))/(a*e-d*b);
175         *x= (px- b*(*y)- c)/a;
176
177         if(block->panel) {
178                 *x -= block->panel->ofsx;
179                 *y -= block->panel->ofsy;
180         }
181 }
182
183 void ui_window_to_block(const ARegion *ar, uiBlock *block, int *x, int *y)
184 {
185         float fx, fy;
186
187         fx= *x;
188         fy= *y;
189
190         ui_window_to_block_fl(ar, block, &fx, &fy);
191
192         *x= (int)(fx+0.5f);
193         *y= (int)(fy+0.5f);
194 }
195
196 void ui_window_to_region(const ARegion *ar, int *x, int *y)
197 {
198         *x-= ar->winrct.xmin;
199         *y-= ar->winrct.ymin;
200 }
201
202 /* ******************* block calc ************************* */
203
204 void ui_block_translate(uiBlock *block, int x, int y)
205 {
206         uiBut *bt;
207
208         for(bt= block->buttons.first; bt; bt=bt->next) {
209                 bt->x1 += x;
210                 bt->y1 += y;
211                 bt->x2 += x;
212                 bt->y2 += y;
213         }
214
215         block->minx += x;
216         block->miny += y;
217         block->maxx += x;
218         block->maxy += y;
219 }
220
221 static void ui_text_bounds_block(uiBlock *block, float offset)
222 {
223         uiStyle *style= U.uistyles.first;       // XXX pass on as arg
224         uiBut *bt;
225         int i = 0, j, x1addval= offset, nextcol;
226         
227         uiStyleFontSet(&style->widget);
228         
229         for(bt= block->buttons.first; bt; bt= bt->next) {
230                 if(bt->type!=SEPR) {
231                         //int transopts= ui_translate_buttons();
232                         //if(bt->type==TEX || bt->type==IDPOIN) transopts= 0;
233                         
234                         j= BLF_width(bt->drawstr);
235
236                         if(j > i) i = j;
237                 }
238         }
239
240         /* cope with multi collumns */
241         bt= block->buttons.first;
242         while(bt) {
243                 if(bt->next && bt->x1 < bt->next->x1)
244                         nextcol= 1;
245                 else nextcol= 0;
246                 
247                 bt->x1 = x1addval;
248                 bt->x2 = bt->x1 + i + block->bounds;
249                 
250                 ui_check_but(bt);       // clips text again
251                 
252                 if(nextcol)
253                         x1addval+= i + block->bounds;
254                 
255                 bt= bt->next;
256         }
257 }
258
259 void ui_bounds_block(uiBlock *block)
260 {
261         uiBut *bt;
262         int xof;
263         
264         if(block->buttons.first==NULL) {
265                 if(block->panel) {
266                         block->minx= 0.0; block->maxx= block->panel->sizex;
267                         block->miny= 0.0; block->maxy= block->panel->sizey;
268                 }
269         }
270         else {
271         
272                 block->minx= block->miny= 10000;
273                 block->maxx= block->maxy= -10000;
274                 
275                 bt= block->buttons.first;
276                 while(bt) {
277                         if(bt->x1 < block->minx) block->minx= bt->x1;
278                         if(bt->y1 < block->miny) block->miny= bt->y1;
279         
280                         if(bt->x2 > block->maxx) block->maxx= bt->x2;
281                         if(bt->y2 > block->maxy) block->maxy= bt->y2;
282                         
283                         bt= bt->next;
284                 }
285                 
286                 block->minx -= block->bounds;
287                 block->miny -= block->bounds;
288                 block->maxx += block->bounds;
289                 block->maxy += block->bounds;
290         }
291
292         /* hardcoded exception... but that one is annoying with larger safety */ 
293         bt= block->buttons.first;
294         if(bt && strncmp(bt->str, "ERROR", 5)==0) xof= 10;
295         else xof= 40;
296         
297         block->safety.xmin= block->minx-xof;
298         block->safety.ymin= block->miny-xof;
299         block->safety.xmax= block->maxx+xof;
300         block->safety.ymax= block->maxy+xof;
301 }
302
303 static void ui_popup_bounds_block(const bContext *C, uiBlock *block, int menu)
304 {
305         wmWindow *window= CTX_wm_window(C);
306         int startx, starty, endx, endy, width, height;
307         int oldbounds, mx, my, xmax, ymax;
308
309         oldbounds= block->bounds;
310
311         /* compute mouse position with user defined offset */
312         ui_bounds_block(block);
313         mx= window->eventstate->x + block->minx + block->mx;
314         my= window->eventstate->y + block->miny + block->my;
315
316         wm_window_get_size(window, &xmax, &ymax);
317
318         /* first we ensure wide enough text bounds */
319         if(menu) {
320                 if(block->flag & UI_BLOCK_LOOP) {
321                         block->bounds= 50;
322                         ui_text_bounds_block(block, block->minx);
323                 }
324         }
325
326         /* next we recompute bounds */
327         block->bounds= oldbounds;
328         ui_bounds_block(block);
329
330         /* and we adjust the position to fit within window */
331         width= block->maxx - block->minx;
332         height= block->maxy - block->miny;
333
334         startx= mx-(0.8*(width));
335         starty= my;
336         
337         if(startx<10)
338                 startx= 10;
339         if(starty<10)
340                 starty= 10;
341         
342         endx= startx+width;
343         endy= starty+height;
344         
345         if(endx>xmax) {
346                 endx= xmax-10;
347                 startx= endx-width;
348         }
349         if(endy>ymax-20) {
350                 endy= ymax-20;
351                 starty= endy-height;
352         }
353
354         ui_block_translate(block, startx - block->minx, starty - block->miny);
355
356         /* now recompute bounds and safety */
357         ui_bounds_block(block);
358 }
359
360 /* used for various cases */
361 void uiBoundsBlock(uiBlock *block, int addval)
362 {
363         if(block==NULL)
364                 return;
365         
366         block->bounds= addval;
367         block->dobounds= 1;
368 }
369
370 /* used for pulldowns */
371 void uiTextBoundsBlock(uiBlock *block, int addval)
372 {
373         block->bounds= addval;
374         block->dobounds= 2;
375 }
376
377 /* used for block popups */
378 void uiPopupBoundsBlock(uiBlock *block, int addval, int mx, int my)
379 {
380         block->bounds= addval;
381         block->dobounds= 3;
382         block->mx= mx;
383         block->my= my;
384 }
385
386 /* used for menu popups */
387 void uiMenuPopupBoundsBlock(uiBlock *block, int addval, int mx, int my)
388 {
389         block->bounds= addval;
390         block->dobounds= 4;
391         block->mx= mx;
392         block->my= my;
393 }
394
395 /* ************** LINK LINE DRAWING  ************* */
396
397 /* link line drawing is not part of buttons or theme.. so we stick with it here */
398
399 static void ui_draw_linkline(uiBut *but, uiLinkLine *line)
400 {
401         rcti rect;
402
403         if(line->from==NULL || line->to==NULL) return;
404         
405         rect.xmin= (line->from->x1+line->from->x2)/2.0;
406         rect.ymin= (line->from->y1+line->from->y2)/2.0;
407         rect.xmax= (line->to->x1+line->to->x2)/2.0;
408         rect.ymax= (line->to->y1+line->to->y2)/2.0;
409         
410         if(line->flag & UI_SELECT) 
411                 glColor3ub(100,100,100);
412         else 
413                 glColor3ub(0,0,0);
414
415         ui_draw_link_bezier(&rect);
416 }
417
418 static void ui_draw_links(uiBlock *block)
419 {
420         uiBut *but;
421         uiLinkLine *line;
422         
423         but= block->buttons.first;
424         while(but) {
425                 if(but->type==LINK && but->link) {
426                         line= but->link->lines.first;
427                         while(line) {
428                                 ui_draw_linkline(but, line);
429                                 line= line->next;
430                         }
431                 }
432                 but= but->next;
433         }       
434 }
435
436 /* ************** BLOCK ENDING FUNCTION ************* */
437
438 /* NOTE: if but->poin is allocated memory for every defbut, things fail... */
439 static int ui_but_equals_old(uiBut *but, uiBut *oldbut)
440 {
441         /* various properties are being compared here, hopfully sufficient
442          * to catch all cases, but it is simple to add more checks later */
443         if(but->retval != oldbut->retval) return 0;
444         if(but->rnapoin.data != oldbut->rnapoin.data) return 0;
445         if(but->rnaprop != oldbut->rnaprop)
446         if(but->rnaindex != oldbut->rnaindex) return 0;
447         if(but->func != oldbut->func) return 0;
448         if(but->funcN != oldbut->funcN) return 0;
449         if(oldbut->func_arg1 != oldbut && but->func_arg1 != oldbut->func_arg1) return 0;
450         if(oldbut->func_arg2 != oldbut && but->func_arg2 != oldbut->func_arg2) return 0;
451         if(!but->funcN && ((but->poin != oldbut->poin && (uiBut*)oldbut->poin != oldbut) || but->pointype != oldbut->pointype)) return 0;
452
453         return 1;
454 }
455
456 static int ui_but_update_from_old_block(const bContext *C, uiBlock *block, uiBut *but)
457 {
458         uiBlock *oldblock;
459         uiBut *oldbut;
460         int found= 0;
461
462         oldblock= block->oldblock;
463         if(!oldblock)
464                 return found;
465
466         for(oldbut=oldblock->buttons.first; oldbut; oldbut=oldbut->next) {
467                 if(ui_but_equals_old(oldbut, but)) {
468                         if(oldbut->active) {
469                                 but->flag= oldbut->flag;
470                                 but->active= oldbut->active;
471                                 but->pos= oldbut->pos;
472                                 but->editstr= oldbut->editstr;
473                                 but->editval= oldbut->editval;
474                                 but->editvec= oldbut->editvec;
475                                 but->editcoba= oldbut->editcoba;
476                                 but->editcumap= oldbut->editcumap;
477                                 but->selsta= oldbut->selsta;
478                                 but->selend= oldbut->selend;
479                                 but->softmin= oldbut->softmin;
480                                 but->softmax= oldbut->softmax;
481                                 but->linkto[0]= oldbut->linkto[0];
482                                 but->linkto[1]= oldbut->linkto[1];
483                                 found= 1;
484
485                                 oldbut->active= NULL;
486                         }
487
488                         /* ensures one button can get activated, and in case the buttons
489                          * draw are the same this gives O(1) lookup for each button */
490                         BLI_remlink(&oldblock->buttons, oldbut);
491                         ui_free_but(C, oldbut);
492                         
493                         break;
494                 }
495         }
496         
497         return found;
498 }
499
500 void ui_menu_block_set_keymaps(const bContext *C, uiBlock *block)
501 {
502         uiBut *but;
503         IDProperty *prop;
504         char buf[512], *butstr;
505
506         /* only do it before bounding */
507         if(block->minx != block->maxx)
508                 return;
509
510         for(but=block->buttons.first; but; but=but->next) {
511                 if(but->optype) {
512                         prop= (but->opptr)? but->opptr->data: NULL;
513
514                         if(WM_key_event_operator_string(C, but->optype->idname, but->opcontext, prop, buf, sizeof(buf))) {
515                                 butstr= MEM_mallocN(strlen(but->str)+strlen(buf)+2, "menu_block_set_keymaps");
516                                 strcpy(butstr, but->str);
517                                 strcat(butstr, "|");
518                                 strcat(butstr, buf);
519
520                                 but->str= but->strdata;
521                                 BLI_strncpy(but->str, butstr, sizeof(but->strdata));
522                                 MEM_freeN(butstr);
523
524                                 ui_check_but(but);
525                         }
526                 }
527         }
528 }
529
530 void uiEndBlock(const bContext *C, uiBlock *block)
531 {
532         uiBut *but;
533         Scene *scene= CTX_data_scene(C);
534
535         /* inherit flags from 'old' buttons that was drawn here previous, based
536          * on matching buttons, we need this to make button event handling non
537          * blocking, while still alowing buttons to be remade each redraw as it
538          * is expected by blender code */
539         for(but=block->buttons.first; but; but=but->next) {
540                 if(ui_but_update_from_old_block(C, block, but))
541                         ui_check_but(but);
542                 
543                 /* temp? Proper check for greying out */
544                 if(but->optype) {
545                         wmOperatorType *ot= but->optype;
546
547                         if(but->context)
548                                 CTX_store_set((bContext*)C, but->context);
549
550                         if(ot==NULL || (ot->poll && ot->poll((bContext *)C)==0)) {
551                                 but->flag |= UI_BUT_DISABLED;
552                                 but->lock = 1;
553                         }
554
555                         if(but->context)
556                                 CTX_store_set((bContext*)C, NULL);
557                 }
558
559                 /* only update soft range while not editing */
560                 if(but->rnaprop && !(but->editval || but->editstr || but->editvec))
561                         ui_set_but_soft_range(but, ui_get_but_val(but));
562
563                 ui_but_anim_flag(but, (scene)? scene->r.cfra: 0.0f);
564         }
565
566         if(block->oldblock) {
567                 block->auto_open= block->oldblock->auto_open;
568                 block->auto_open_last= block->oldblock->auto_open_last;
569                 block->tooltipdisabled= block->oldblock->tooltipdisabled;
570
571                 block->oldblock= NULL;
572         }
573
574         /* handle pending stuff */
575         if(block->layouts.first) uiBlockLayoutResolve(C, block, NULL, NULL);
576         ui_block_do_align(block);
577         if(block->flag & UI_BLOCK_LOOP) ui_menu_block_set_keymaps(C, block);
578         
579         /* after keymaps! */
580         if(block->dobounds == 1) ui_bounds_block(block);
581         else if(block->dobounds == 2) ui_text_bounds_block(block, 0.0f);
582         else if(block->dobounds) ui_popup_bounds_block(C, block, (block->dobounds == 4));
583
584         if(block->minx==0.0 && block->maxx==0.0) uiBoundsBlock(block, 0);
585         if(block->flag & UI_BUT_ALIGN) uiBlockEndAlign(block);
586
587         block->endblock= 1;
588 }
589
590 /* ************** BLOCK DRAWING FUNCTION ************* */
591
592 void ui_fontscale(short *points, float aspect)
593 {
594         if(aspect < 0.9f || aspect > 1.1f) {
595                 float pointsf= *points;
596                 
597                 /* for some reason scaling fonts goes too fast compared to widget size */
598                 aspect= sqrt(aspect);
599                 pointsf /= aspect;
600                 
601                 if(aspect > 1.0)
602                         *points= ceil(pointsf);
603                 else
604                         *points= floor(pointsf);
605         }
606 }
607
608 /* project button or block (but==NULL) to pixels in regionspace */
609 static void ui_but_to_pixelrect(rcti *rect, const ARegion *ar, uiBlock *block, uiBut *but)
610 {
611         float gx, gy;
612         float getsizex, getsizey;
613         
614         getsizex= ar->winx;
615         getsizey= ar->winy;
616
617         gx= (but?but->x1:block->minx) + (block->panel?block->panel->ofsx:0.0f);
618         gy= (but?but->y1:block->miny) + (block->panel?block->panel->ofsy:0.0f);
619         
620         rect->xmin= floor(getsizex*(0.5+ 0.5*(gx*block->winmat[0][0]+ gy*block->winmat[1][0]+ block->winmat[3][0])));
621         rect->ymin= floor(getsizey*(0.5+ 0.5*(gx*block->winmat[0][1]+ gy*block->winmat[1][1]+ block->winmat[3][1])));
622         
623         gx= (but?but->x2:block->maxx) + (block->panel?block->panel->ofsx:0.0f);
624         gy= (but?but->y2:block->maxy) + (block->panel?block->panel->ofsy:0.0f);
625         
626         rect->xmax= floor(getsizex*(0.5+ 0.5*(gx*block->winmat[0][0]+ gy*block->winmat[1][0]+ block->winmat[3][0])));
627         rect->ymax= floor(getsizey*(0.5+ 0.5*(gx*block->winmat[0][1]+ gy*block->winmat[1][1]+ block->winmat[3][1])));
628
629 }
630
631 /* uses local copy of style, to scale things down, and allow widgets to change stuff */
632 void uiDrawBlock(const bContext *C, uiBlock *block)
633 {
634         uiStyle style= *((uiStyle *)U.uistyles.first);  // XXX pass on as arg
635         ARegion *ar;
636         uiBut *but;
637         rcti rect;
638         
639         /* get menu region or area region */
640         ar= CTX_wm_menu(C);
641         if(!ar)
642                 ar= CTX_wm_region(C);
643
644         if(!block->endblock)
645                 uiEndBlock(C, block);
646
647         /* we set this only once */
648         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
649         
650         /* scale fonts */
651         ui_fontscale(&style.paneltitle.points, block->aspect);
652         ui_fontscale(&style.grouplabel.points, block->aspect);
653         ui_fontscale(&style.widgetlabel.points, block->aspect);
654         ui_fontscale(&style.widget.points, block->aspect);
655         
656         /* scale block min/max to rect */
657         ui_but_to_pixelrect(&rect, ar, block, NULL);
658         
659         /* pixel space for AA widgets */
660         wmPushMatrix();
661         wmLoadIdentity();
662         
663         wmOrtho2(-0.01f, ar->winx-0.01f, -0.01f, ar->winy-0.01f);
664         
665         /* back */
666         if(block->flag & UI_BLOCK_LOOP)
667                 ui_draw_menu_back(&style, block, &rect);
668         else if(block->panel)
669                 ui_draw_aligned_panel(ar, &style, block, &rect);
670
671         /* widgets */
672         for(but= block->buttons.first; but; but= but->next) {
673                 ui_but_to_pixelrect(&rect, ar, block, but);
674                 if(!(but->flag & UI_HIDDEN))
675                         ui_draw_but(C, ar, &style, but, &rect);
676         }
677         
678         /* restore matrix */
679         wmPopMatrix();
680         
681         ui_draw_links(block);
682 }
683
684 /* ************* EVENTS ************* */
685
686 static void ui_is_but_sel(uiBut *but)
687 {
688         double value;
689         int lvalue;
690         short push=0, true=1;
691
692         value= ui_get_but_val(but);
693
694         if(ELEM3(but->type, TOGN, ICONTOGN, OPTIONN)) true= 0;
695
696         if( but->bit ) {
697                 lvalue= (int)value;
698                 if( BTST(lvalue, (but->bitnr)) ) push= true;
699                 else push= !true;
700         }
701         else {
702                 switch(but->type) {
703                 case BUT:
704                         push= 2;
705                         break;
706                 case KEYEVT:
707                         if (value==-1) push= 1;
708                         break;
709                 case TOGBUT:
710                 case TOG:
711                 case TOGR:
712                 case TOG3:
713                 case BUT_TOGDUAL:
714                 case ICONTOG:
715                 case OPTION:
716                         if(value!=but->hardmin) push= 1;
717                         break;
718                 case ICONTOGN:
719                 case TOGN:
720                 case OPTIONN:
721                         if(value==0.0) push= 1;
722                         break;
723                 case ROW:
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 {
1317                 /* number */
1318                 double value;
1319
1320                 value= ui_get_but_val(but);
1321
1322                 if(ui_is_but_float(but)) {
1323                         if(but->a2) { /* amount of digits defined */
1324                                 if(but->a2==1) BLI_snprintf(str, maxlen, "%.1f", value);
1325                                 else if(but->a2==2) BLI_snprintf(str, maxlen, "%.2f", value);
1326                                 else if(but->a2==3) BLI_snprintf(str, maxlen, "%.3f", value);
1327                                 else BLI_snprintf(str, maxlen, "%.4f", value);
1328                         }
1329                         else
1330                                 BLI_snprintf(str, maxlen, "%.3f", value);
1331                 }
1332                 else
1333                         BLI_snprintf(str, maxlen, "%d", (int)value);
1334         }
1335 }
1336
1337 static void ui_rna_ID_collection(bContext *C, uiBut *but, PointerRNA *ptr, PropertyRNA **prop)
1338 {
1339         CollectionPropertyIterator iter;
1340         PropertyRNA *iterprop, *iprop;
1341         StructRNA *srna;
1342
1343         /* look for collection property in Main */
1344         RNA_pointer_create(NULL, &RNA_Main, CTX_data_main(C), ptr);
1345
1346         iterprop= RNA_struct_iterator_property(ptr->type);
1347         RNA_property_collection_begin(ptr, iterprop, &iter);
1348         *prop= NULL;
1349
1350         for(; iter.valid; RNA_property_collection_next(&iter)) {
1351                 iprop= iter.ptr.data;
1352
1353                 /* if it's a collection and has same pointer type, we've got it */
1354                 if(RNA_property_type(iprop) == PROP_COLLECTION) {
1355                         srna= RNA_property_pointer_type(ptr, iprop);
1356
1357                         if(RNA_property_pointer_type(ptr, but->rnaprop) == srna) {
1358                                 *prop= iprop;
1359                                 break;
1360                         }
1361                 }
1362         }
1363
1364         RNA_property_collection_end(&iter);
1365 }
1366
1367 /* autocomplete callback for RNA pointers */
1368 static void ui_rna_ID_autocomplete(bContext *C, char *str, void *arg_but)
1369 {
1370         uiBut *but= arg_but;
1371         AutoComplete *autocpl;
1372         CollectionPropertyIterator iter;
1373         PointerRNA ptr;
1374         PropertyRNA *prop;
1375         char *name;
1376         
1377         if(str[0]==0) return;
1378
1379         /* get the collection */
1380         ui_rna_ID_collection(C, but, &ptr, &prop);
1381         if(prop==NULL) return;
1382
1383         autocpl= autocomplete_begin(str, ui_get_but_string_max_length(but));
1384         RNA_property_collection_begin(&ptr, prop, &iter);
1385
1386         /* loop over items in collection */
1387         for(; iter.valid; RNA_property_collection_next(&iter)) {
1388                 name= RNA_struct_name_get_alloc(&iter.ptr, NULL, 0);
1389
1390                 /* test item name */
1391                 if(name) {
1392                         autocomplete_do_name(autocpl, name);
1393                         MEM_freeN(name);
1394                 }
1395         }
1396
1397         RNA_property_collection_end(&iter);
1398         autocomplete_end(autocpl, str);
1399 }
1400
1401 int ui_set_but_string(bContext *C, uiBut *but, const char *str)
1402 {
1403         if(but->rnaprop && ELEM3(but->type, TEX, IDPOIN, SEARCH_MENU)) {
1404                 if(RNA_property_editable(&but->rnapoin, but->rnaprop)) {
1405                         PropertyType type;
1406
1407                         type= RNA_property_type(but->rnaprop);
1408
1409                         if(type == PROP_STRING) {
1410                                 /* RNA string */
1411                                 RNA_property_string_set(&but->rnapoin, but->rnaprop, str);
1412                                 return 1;
1413                         }
1414                         else if(type == PROP_POINTER) {
1415                                 /* RNA pointer */
1416                                 PointerRNA ptr, rptr;
1417                                 PropertyRNA *prop;
1418
1419                                 /* XXX only ID pointers at the moment, needs to support
1420                                  * custom collection too for bones, vertex groups, .. */
1421                                 ui_rna_ID_collection(C, but, &ptr, &prop);
1422
1423                                 if(str == NULL || str[0] == '\0') {
1424                                         memset(&rptr, 0, sizeof(rptr));
1425                                         RNA_property_pointer_set(&but->rnapoin, but->rnaprop, rptr);
1426                                         return 1;
1427                                 }
1428                                 else if(prop && RNA_property_collection_lookup_string(&ptr, prop, str, &rptr)) {
1429                                         RNA_property_pointer_set(&but->rnapoin, but->rnaprop, rptr);
1430                                         return 1;
1431                                 }
1432                                 else
1433                                         return 0;
1434                         }
1435                 }
1436         }
1437         else if(but->type == IDPOIN) {
1438                 /* ID pointer */
1439         but->idpoin_func(C, (char*)str, but->idpoin_idpp);
1440                 return 1;
1441         }
1442         else if(but->type == TEX) {
1443                 /* string */
1444                 BLI_strncpy(but->poin, str, but->hardmax);
1445                 return 1;
1446         }
1447         else if(but->type == SEARCH_MENU) {
1448                 /* string */
1449                 BLI_strncpy(but->poin, str, but->hardmax);
1450                 return 1;
1451         }
1452         else {
1453                 double value;
1454
1455                 /* XXX 2.50 missing python api */
1456 #if 0
1457                 if(BPY_button_eval(str, &value)) {
1458                         BKE_report(CTX_reports(C), RPT_WARNING, "Invalid Python expression, check console");
1459                         value = 0.0f; /* Zero out value on error */
1460                         
1461                         if(str[0])
1462                                 return 0;
1463                 }
1464 #else
1465                 value= atof(str);
1466 #endif
1467
1468                 if(!ui_is_but_float(but)) value= (int)value;
1469                 if(but->type==NUMABS) value= fabs(value);
1470
1471                 /* not that we use hard limits here */
1472                 if(value<but->hardmin) value= but->hardmin;
1473                 if(value>but->hardmax) value= but->hardmax;
1474
1475                 ui_set_but_val(but, value);
1476                 return 1;
1477         }
1478
1479         return 0;
1480 }
1481
1482 static double soft_range_round_up(double value, double max)
1483 {
1484         /* round up to .., 0.1, 0.2, 0.5, 1, 2, 5, 10, 20, 50, .. */
1485         double newmax= pow(10.0, ceil(log(value)/log(10.0)));
1486
1487         if(newmax*0.2 >= max && newmax*0.2 >= value)
1488                 return newmax*0.2;
1489         else if(newmax*0.5 >= max && newmax*0.5 >= value)
1490                 return newmax*0.5;
1491         else
1492                 return newmax;
1493 }
1494
1495 static double soft_range_round_down(double value, double max)
1496 {
1497         /* round down to .., 0.1, 0.2, 0.5, 1, 2, 5, 10, 20, 50, .. */
1498         double newmax= pow(10.0, floor(log(value)/log(10.0)));
1499
1500         if(newmax*5.0 <= max && newmax*5.0 <= value)
1501                 return newmax*5.0;
1502         else if(newmax*2.0 <= max && newmax*2.0 <= value)
1503                 return newmax*2.0;
1504         else
1505                 return newmax;
1506 }
1507
1508 void ui_set_but_soft_range(uiBut *but, double value)
1509 {
1510         PropertyType type;
1511         double softmin, softmax, step, precision;
1512         
1513         if(but->rnaprop) {
1514                 type= RNA_property_type(but->rnaprop);
1515
1516                 if(type == PROP_INT) {
1517                         int imin, imax, istep;
1518
1519                         RNA_property_int_ui_range(&but->rnapoin, but->rnaprop, &imin, &imax, &istep);
1520                         softmin= imin;
1521                         softmax= imax;
1522                         step= istep;
1523                         precision= 1;
1524                 }
1525                 else if(type == PROP_FLOAT) {
1526                         float fmin, fmax, fstep, fprecision;
1527
1528                         RNA_property_float_ui_range(&but->rnapoin, but->rnaprop, &fmin, &fmax, &fstep, &fprecision);
1529                         softmin= fmin;
1530                         softmax= fmax;
1531                         step= fstep;
1532                         precision= fprecision;
1533                 }
1534                 else
1535                         return;
1536
1537                 /* clamp button range to something reasonable in case
1538                  * we get -inf/inf from RNA properties */
1539                 softmin= MAX2(softmin, -1e4);
1540                 softmax= MIN2(softmax, 1e4);
1541
1542                 /* if the value goes out of the soft/max range, adapt the range */
1543                 if(value+1e-10 < softmin) {
1544                         if(value < 0.0)
1545                                 softmin= -soft_range_round_up(-value, -softmin);
1546                         else
1547                                 softmin= soft_range_round_down(value, softmin);
1548
1549                         if(softmin < but->hardmin)
1550                                 softmin= but->hardmin;
1551                 }
1552                 else if(value-1e-10 > softmax) {
1553                         if(value < 0.0)
1554                                 softmax= -soft_range_round_down(-value, -softmax);
1555                         else
1556                                 softmax= soft_range_round_up(value, softmax);
1557
1558                         if(softmax > but->hardmax)
1559                                 softmax= but->hardmax;
1560                 }
1561
1562                 but->softmin= softmin;
1563                 but->softmax= softmax;
1564         }
1565 }
1566
1567 /* ******************* Free ********************/
1568
1569 static void ui_free_link(uiLink *link)
1570 {
1571         if(link) {      
1572                 BLI_freelistN(&link->lines);
1573                 MEM_freeN(link);
1574         }
1575 }
1576
1577 /* can be called with C==NULL */
1578 static void ui_free_but(const bContext *C, uiBut *but)
1579 {
1580         if(but->opptr) {
1581                 WM_operator_properties_free(but->opptr);
1582                 MEM_freeN(but->opptr);
1583         }
1584         if(but->func_argN) MEM_freeN(but->func_argN);
1585         if(but->active) {
1586                 /* XXX solve later, buttons should be free-able without context? */
1587                 if(C) 
1588                         ui_button_active_cancel(C, but);
1589                 else
1590                         if(but->active) 
1591                                 MEM_freeN(but->active);
1592         }
1593         if(but->str && but->str != but->strdata) MEM_freeN(but->str);
1594         ui_free_link(but->link);
1595
1596         MEM_freeN(but);
1597 }
1598
1599 /* can be called with C==NULL */
1600 void uiFreeBlock(const bContext *C, uiBlock *block)
1601 {
1602         uiBut *but;
1603
1604         while( (but= block->buttons.first) ) {
1605                 BLI_remlink(&block->buttons, but);      
1606                 ui_free_but(C, but);
1607         }
1608
1609         CTX_store_free_list(&block->contexts);
1610
1611         BLI_freelistN(&block->saferct);
1612         
1613         MEM_freeN(block);
1614 }
1615
1616 /* can be called with C==NULL */
1617 void uiFreeBlocks(const bContext *C, ListBase *lb)
1618 {
1619         uiBlock *block;
1620         
1621         while( (block= lb->first) ) {
1622                 BLI_remlink(lb, block);
1623                 uiFreeBlock(C, block);
1624         }
1625 }
1626
1627 void uiFreeInactiveBlocks(const bContext *C, ListBase *lb)
1628 {
1629         uiBlock *block, *nextblock;
1630
1631         for(block=lb->first; block; block=nextblock) {
1632                 nextblock= block->next;
1633         
1634                 if(!block->handle) {
1635                         if(!block->active) {
1636                                 BLI_remlink(lb, block);
1637                                 uiFreeBlock(C, block);
1638                         }
1639                         else
1640                                 block->active= 0;
1641                 }
1642         }
1643 }
1644
1645 void uiBlockSetRegion(uiBlock *block, ARegion *region)
1646 {
1647         ListBase *lb;
1648         uiBlock *oldblock= NULL;
1649
1650         lb= &region->uiblocks;
1651         
1652         /* each listbase only has one block with this name, free block
1653          * if is already there so it can be rebuilt from scratch */
1654         if(lb) {
1655                 for (oldblock= lb->first; oldblock; oldblock= oldblock->next)
1656                         if (BLI_streq(oldblock->name, block->name))
1657                                 break;
1658
1659                 if (oldblock) {
1660                         oldblock->active= 0;
1661                         oldblock->panel= NULL;
1662                 }
1663         }
1664
1665         block->oldblock= oldblock;
1666
1667         /* at the beginning of the list! for dynamical menus/blocks */
1668         if(lb)
1669                 BLI_addhead(lb, block);
1670 }
1671
1672 uiBlock *uiBeginBlock(const bContext *C, ARegion *region, const char *name, short dt)
1673 {
1674         uiBlock *block;
1675         wmWindow *window;
1676         int getsizex, getsizey;
1677
1678         window= CTX_wm_window(C);
1679
1680         block= MEM_callocN(sizeof(uiBlock), "uiBlock");
1681         block->active= 1;
1682         block->dt= dt;
1683         BLI_strncpy(block->name, name, sizeof(block->name));
1684
1685         if(region)
1686                 uiBlockSetRegion(block, region);
1687
1688         /* window matrix and aspect */
1689         if(region && region->swinid) {
1690                 wm_subwindow_getmatrix(window, region->swinid, block->winmat);
1691                 wm_subwindow_getsize(window, region->swinid, &getsizex, &getsizey);
1692
1693                 /* TODO - investigate why block->winmat[0][0] is negative
1694                  * in the image view when viewRedrawForce is called */
1695                 block->aspect= 2.0/fabs( (getsizex)*block->winmat[0][0]);
1696         }
1697         else {
1698                 /* no subwindow created yet, for menus for example, so we
1699                  * use the main window instead, since buttons are created
1700                  * there anyway */
1701                 wm_subwindow_getmatrix(window, window->screen->mainwin, block->winmat);
1702                 wm_subwindow_getsize(window, window->screen->mainwin, &getsizex, &getsizey);
1703
1704                 block->aspect= 2.0/fabs(getsizex*block->winmat[0][0]);
1705                 block->auto_open= 2;
1706                 block->flag |= UI_BLOCK_LOOP; /* tag as menu */
1707         }
1708
1709         return block;
1710 }
1711
1712 uiBlock *uiGetBlock(char *name, ARegion *ar)
1713 {
1714         uiBlock *block= ar->uiblocks.first;
1715         
1716         while(block) {
1717                 if( strcmp(name, block->name)==0 ) return block;
1718                 block= block->next;
1719         }
1720         
1721         return NULL;
1722 }
1723
1724 void uiBlockSetEmboss(uiBlock *block, short dt)
1725 {
1726         block->dt= dt;
1727 }
1728
1729 void ui_check_but(uiBut *but)
1730 {
1731         /* if something changed in the button */
1732         double value;
1733         float okwidth;
1734 //      int transopts= ui_translate_buttons();
1735         
1736         ui_is_but_sel(but);
1737         
1738 //      if(but->type==TEX || but->type==IDPOIN) transopts= 0;
1739
1740         /* test for min and max, icon sliders, etc */
1741         switch( but->type ) {
1742                 case NUM:
1743                 case SLI:
1744                 case SCROLL:
1745                 case NUMSLI:
1746                 case HSVSLI:
1747                         value= ui_get_but_val(but);
1748                         if(value < but->hardmin) ui_set_but_val(but, but->hardmin);
1749                         else if(value > but->hardmax) ui_set_but_val(but, but->hardmax);
1750                         break;
1751                         
1752                 case NUMABS:
1753                         value= fabs( ui_get_but_val(but) );
1754                         if(value < but->hardmin) ui_set_but_val(but, but->hardmin);
1755                         else if(value > but->hardmax) ui_set_but_val(but, but->hardmax);
1756                         break;
1757                         
1758                 case ICONTOG: 
1759                 case ICONTOGN:
1760                         if(!but->rnaprop || (RNA_property_flag(but->rnaprop) & PROP_ICONS_CONSECUTIVE)) {
1761                                 if(but->flag & UI_SELECT) but->iconadd= 1;
1762                                 else but->iconadd= 0;
1763                         }
1764                         break;
1765                         
1766                 case ICONROW:
1767                         if(!but->rnaprop || (RNA_property_flag(but->rnaprop) & PROP_ICONS_CONSECUTIVE)) {
1768                                 value= ui_get_but_val(but);
1769                                 but->iconadd= (int)value- (int)(but->hardmin);
1770                         }
1771                         break;
1772                         
1773                 case ICONTEXTROW:
1774                         if(!but->rnaprop || (RNA_property_flag(but->rnaprop) & PROP_ICONS_CONSECUTIVE)) {
1775                                 value= ui_get_but_val(but);
1776                                 but->iconadd= (int)value- (int)(but->hardmin);
1777                         }
1778                         break;
1779         }
1780         
1781         
1782         /* safety is 4 to enable small number buttons (like 'users') */
1783         okwidth= -4 + (but->x2 - but->x1); 
1784         
1785         /* name: */
1786         switch( but->type ) {
1787         
1788         case MENU:
1789         case ICONTEXTROW:
1790                 
1791                 if(but->x2 - but->x1 > 24) {
1792                         value= ui_get_but_val(but);
1793                         ui_set_name_menu(but, (int)value);
1794                 }
1795                 break;
1796         
1797         case NUM:
1798         case NUMSLI:
1799         case HSVSLI:
1800         case NUMABS:
1801
1802                 value= ui_get_but_val(but);
1803
1804                 if(ui_is_but_float(but)) {
1805                         if(value == FLT_MAX) sprintf(but->drawstr, "%sinf", but->str);
1806                         else if(value == -FLT_MAX) sprintf(but->drawstr, "%s-inf", but->str);
1807                         else if(but->a2) { /* amount of digits defined */
1808                                 if(but->a2==1) sprintf(but->drawstr, "%s%.1f", but->str, value);
1809                                 else if(but->a2==2) sprintf(but->drawstr, "%s%.2f", but->str, value);
1810                                 else if(but->a2==3) sprintf(but->drawstr, "%s%.3f", but->str, value);
1811                                 else sprintf(but->drawstr, "%s%.4f", but->str, value);
1812                         }
1813                         else {
1814                                 if(but->hardmax<10.001) sprintf(but->drawstr, "%s%.3f", but->str, value);
1815                                 else sprintf(but->drawstr, "%s%.2f", but->str, value);
1816                         }
1817                 }
1818                 else {
1819                         sprintf(but->drawstr, "%s%d", but->str, (int)value);
1820                 }
1821                         
1822                 if(but->rnaprop) {
1823                         PropertySubType pstype = RNA_property_subtype(but->rnaprop);
1824                         
1825                         if (pstype == PROP_PERCENTAGE)
1826                                 strcat(but->drawstr, "%");
1827                 }
1828                 break;
1829
1830         case LABEL:
1831                 if(ui_is_but_float(but)) {
1832                         value= ui_get_but_val(but);
1833                         if(but->a2) { /* amount of digits defined */
1834                                 if(but->a2==1) sprintf(but->drawstr, "%s%.1f", but->str, value);
1835                                 else if(but->a2==2) sprintf(but->drawstr, "%s%.2f", but->str, value);
1836                                 else if(but->a2==3) sprintf(but->drawstr, "%s%.3f", but->str, value);
1837                                 else sprintf(but->drawstr, "%s%.4f", but->str, value);
1838                         }
1839                         else {
1840                                 sprintf(but->drawstr, "%s%.2f", but->str, value);
1841                         }
1842                 }
1843                 else strcpy(but->drawstr, but->str);
1844                 
1845                 break;
1846
1847         case IDPOIN:
1848         case TEX:
1849         case SEARCH_MENU:
1850                 if(!but->editstr) {
1851                         char str[UI_MAX_DRAW_STR];
1852
1853                         ui_get_but_string(but, str, UI_MAX_DRAW_STR-strlen(but->str));
1854
1855                         strcpy(but->drawstr, but->str);
1856                         strcat(but->drawstr, str);
1857                 }
1858                 break;
1859         
1860         case KEYEVT:
1861                 strcpy(but->drawstr, but->str);
1862                 if (but->flag & UI_SELECT) {
1863                         strcat(but->drawstr, "Press a key");
1864                 } else {
1865                         strcat(but->drawstr, WM_key_event_string((short) ui_get_but_val(but)));
1866                 }
1867                 break;
1868
1869         case BUT_TOGDUAL:
1870                 /* trying to get the dual-icon to left of text... not very nice */
1871                 if(but->str[0]) {
1872                         strcpy(but->drawstr, "  ");
1873                         strcpy(but->drawstr+2, but->str);
1874                 }
1875                 break;
1876         default:
1877                 strcpy(but->drawstr, but->str);
1878                 
1879         }
1880
1881         /* if we are doing text editing, this will override the drawstr */
1882         if(but->editstr) {
1883                 strcpy(but->drawstr, but->str);
1884                 strcat(but->drawstr, but->editstr);
1885         }
1886         
1887         /* text clipping moved to widget drawing code itself */
1888 }
1889
1890
1891 void uiBlockBeginAlign(uiBlock *block)
1892 {
1893         /* if other align was active, end it */
1894         if(block->flag & UI_BUT_ALIGN) uiBlockEndAlign(block);
1895
1896         block->flag |= UI_BUT_ALIGN_DOWN;       
1897         block->alignnr++;
1898
1899         /* buttons declared after this call will get this align nr */ // XXX flag?
1900 }
1901
1902 static int buts_are_horiz(uiBut *but1, uiBut *but2)
1903 {
1904         float dx, dy;
1905         
1906         dx= fabs( but1->x2 - but2->x1);
1907         dy= fabs( but1->y1 - but2->y2);
1908         
1909         if(dx > dy) return 0;
1910         return 1;
1911 }
1912
1913 void uiBlockEndAlign(uiBlock *block)
1914 {
1915         block->flag &= ~UI_BUT_ALIGN;   // all 4 flags
1916 }
1917
1918 int ui_but_can_align(uiBut *but)
1919 {
1920         return !ELEM3(but->type, LABEL, OPTION, OPTIONN);
1921 }
1922
1923 static void ui_block_do_align_but(uiBlock *block, uiBut *first, int nr)
1924 {
1925         uiBut *prev, *but=NULL, *next;
1926         int flag= 0, cols=0, rows=0;
1927         
1928         /* auto align */
1929
1930         for(but=first; but && but->alignnr == nr; but=but->next) {
1931                 if(but->next && but->next->alignnr == nr) {
1932                         if(buts_are_horiz(but, but->next)) cols++;
1933                         else rows++;
1934                 }
1935         }
1936
1937         /* rows==0: 1 row, cols==0: 1 collumn */
1938         
1939         /* note;  how it uses 'flag' in loop below (either set it, or OR it) is confusing */
1940         for(but=first, prev=NULL; but && but->alignnr == nr; prev=but, but=but->next) {
1941                 next= but->next;
1942                 if(next && next->alignnr != nr)
1943                         next= NULL;
1944
1945                 /* clear old flag */
1946                 but->flag &= ~UI_BUT_ALIGN;
1947                 
1948                 if(flag==0) {   /* first case */
1949                         if(next) {
1950                                 if(buts_are_horiz(but, next)) {
1951                                         if(rows==0)
1952                                                 flag= UI_BUT_ALIGN_RIGHT;
1953                                         else 
1954                                                 flag= UI_BUT_ALIGN_DOWN|UI_BUT_ALIGN_RIGHT;
1955                                 }
1956                                 else {
1957                                         flag= UI_BUT_ALIGN_DOWN;
1958                                 }
1959                         }
1960                 }
1961                 else if(next==NULL) {   /* last case */
1962                         if(prev) {
1963                                 if(buts_are_horiz(prev, but)) {
1964                                         if(rows==0) 
1965                                                 flag= UI_BUT_ALIGN_LEFT;
1966                                         else
1967                                                 flag= UI_BUT_ALIGN_TOP|UI_BUT_ALIGN_LEFT;
1968                                 }
1969                                 else flag= UI_BUT_ALIGN_TOP;
1970                         }
1971                 }
1972                 else if(buts_are_horiz(but, next)) {
1973                         /* check if this is already second row */
1974                         if( prev && buts_are_horiz(prev, but)==0) {
1975                                 flag &= ~UI_BUT_ALIGN_LEFT;
1976                                 flag |= UI_BUT_ALIGN_TOP;
1977                                 /* exception case: bottom row */
1978                                 if(rows>0) {
1979                                         uiBut *bt= but;
1980                                         while(bt && bt->alignnr == nr) {
1981                                                 if(bt->next && bt->next->alignnr == nr && buts_are_horiz(bt, bt->next)==0 ) break; 
1982                                                 bt= bt->next;
1983                                         }
1984                                         if(bt==0 || bt->alignnr != nr) flag= UI_BUT_ALIGN_TOP|UI_BUT_ALIGN_RIGHT;
1985                                 }
1986                         }
1987                         else flag |= UI_BUT_ALIGN_LEFT;
1988                 }
1989                 else {
1990                         if(cols==0) {
1991                                 flag |= UI_BUT_ALIGN_TOP;
1992                         }
1993                         else {  /* next button switches to new row */
1994                                 
1995                                 if(prev && buts_are_horiz(prev, but))
1996                                    flag |= UI_BUT_ALIGN_LEFT;
1997                                 
1998                                 if( (flag & UI_BUT_ALIGN_TOP)==0) {     /* stil top row */
1999                                         if(prev)
2000                                                 flag= UI_BUT_ALIGN_DOWN|UI_BUT_ALIGN_LEFT;
2001                                         else 
2002                                                 flag |= UI_BUT_ALIGN_DOWN;
2003                                 }
2004                                 else 
2005                                         flag |= UI_BUT_ALIGN_TOP;
2006                         }
2007                 }
2008                 
2009                 but->flag |= flag;
2010                 
2011                 /* merge coordinates */
2012                 if(prev) {
2013                         // simple cases 
2014                         if(rows==0) {
2015                                 but->x1= (prev->x2+but->x1)/2.0;
2016                                 prev->x2= but->x1;
2017                         }
2018                         else if(cols==0) {
2019                                 but->y2= (prev->y1+but->y2)/2.0;
2020                                 prev->y1= but->y2;
2021                         }
2022                         else {
2023                                 if(buts_are_horiz(prev, but)) {
2024                                         but->x1= (prev->x2+but->x1)/2.0;
2025                                         prev->x2= but->x1;
2026                                         /* copy height too */
2027                                         but->y2= prev->y2;
2028                                 }
2029                                 else if(prev->prev && buts_are_horiz(prev->prev, prev)==0) {
2030                                         /* the previous button is a single one in its row */
2031                                         but->y2= (prev->y1+but->y2)/2.0;
2032                                         prev->y1= but->y2;
2033                                 }
2034                                 else {
2035                                         /* the previous button is not a single one in its row */
2036                                         but->y2= prev->y1;
2037                                 }
2038                         }
2039                 }
2040         }
2041 }
2042
2043 void ui_block_do_align(uiBlock *block)
2044 {
2045         uiBut *but;
2046         int nr;
2047
2048         /* align buttons with same align nr */
2049         for(but=block->buttons.first; but;) {
2050                 if(but->alignnr) {
2051                         nr= but->alignnr;
2052                         ui_block_do_align_but(block, but, nr);
2053
2054                         /* skip with same number */
2055                         for(; but && but->alignnr == nr; but=but->next);
2056
2057                         if(!but)
2058                                 break;
2059                 }
2060                 else
2061                         but= but->next;
2062         }
2063 }
2064
2065 /*
2066 ui_def_but is the function that draws many button types
2067
2068 for float buttons:
2069         "a1" Click Step (how much to change the value each click)
2070         "a2" Number of decimal point values to display. 0 defaults to 3 (0.000) 1,2,3, and a maximum of 4,
2071        all greater values will be clamped to 4.
2072
2073 */
2074 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)
2075 {
2076         uiBut *but;
2077         short slen;
2078         
2079         if(type & BUTPOIN) {            /* a pointer is required */
2080                 if(poin==NULL)
2081                         return NULL;
2082         }
2083
2084         but= MEM_callocN(sizeof(uiBut), "uiBut");
2085
2086         but->type= type & BUTTYPE;
2087         but->pointype= type & BUTPOIN;
2088         but->bit= type & BIT;
2089         but->bitnr= type & 31;
2090         but->icon = 0;
2091
2092         but->retval= retval;
2093         if( strlen(str)>=UI_MAX_NAME_STR-1 ) {
2094                 but->str= MEM_callocN( strlen(str)+2, "uiDefBut");
2095                 strcpy(but->str, str);
2096         }
2097         else {
2098                 but->str= but->strdata;
2099                 strcpy(but->str, str);
2100         }
2101         but->x1= x1; 
2102         but->y1= y1;
2103         but->x2= (x1+x2); 
2104         but->y2= (y1+y2);
2105         
2106         but->poin= poin;
2107         but->hardmin= but->softmin= min; 
2108         but->hardmax= but->softmax= max;
2109         but->a1= a1; 
2110         but->a2= a2;
2111         but->tip= tip;
2112         
2113         but->lock= block->lock;
2114         but->lockstr= block->lockstr;
2115         but->dt= block->dt;
2116
2117         but->aspect= 1.0f; //XXX block->aspect;
2118         but->block= block;              // pointer back, used for frontbuffer status, and picker
2119
2120         if((block->flag & UI_BUT_ALIGN) && ui_but_can_align(but))
2121                 but->alignnr= block->alignnr;
2122
2123         but->func= block->func;
2124         but->func_arg1= block->func_arg1;
2125         but->func_arg2= block->func_arg2;
2126         
2127         but->pos= -1;   /* cursor invisible */
2128
2129         if(ELEM(but->type, NUM, NUMABS)) {      /* add a space to name */
2130                 slen= strlen(but->str);
2131                 if(slen>0 && slen<UI_MAX_NAME_STR-2) {
2132                         if(but->str[slen-1]!=' ') {
2133                                 but->str[slen]= ' ';
2134                                 but->str[slen+1]= 0;
2135                         }
2136                 }
2137         }
2138         
2139         if(ELEM(but->type, HSVCUBE, HSVCIRCLE)) { /* hsv buttons temp storage */
2140                 float rgb[3];
2141                 ui_get_but_vectorf(but, rgb);
2142                 rgb_to_hsv(rgb[0], rgb[1], rgb[2], but->hsv, but->hsv+1, but->hsv+2);
2143         }
2144
2145         if((block->flag & UI_BLOCK_LOOP) || ELEM6(but->type, MENU, TEX, LABEL, IDPOIN, BLOCK, BUTM)) {
2146                 but->flag |= UI_TEXT_LEFT;
2147         }
2148         
2149         if(but->type==BUT_TOGDUAL) {
2150                 but->flag |= UI_ICON_LEFT;
2151         }
2152
2153         but->flag |= (block->flag & UI_BUT_ALIGN);
2154
2155         if (but->lock) {
2156                 if (but->lockstr) {
2157                         but->flag |= UI_BUT_DISABLED;
2158                 }
2159         }
2160
2161         BLI_addtail(&block->buttons, but);
2162         
2163         if(block->curlayout)
2164                 ui_layout_add_but(block->curlayout, but);
2165
2166         return but;
2167 }
2168
2169 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)
2170 {
2171         uiBut *but;
2172         PropertyRNA *prop;
2173         PropertyType proptype;
2174         int freestr= 0, icon= 0;
2175
2176         prop= RNA_struct_find_property(ptr, propname);
2177
2178         if(prop) {
2179                 proptype= RNA_property_type(prop);
2180
2181                 /* use rna values if parameters are not specified */
2182                 if(!str) {
2183                         if(type == MENU && proptype == PROP_ENUM) {
2184                                 const EnumPropertyItem *item;
2185                                 DynStr *dynstr;
2186                                 int i, totitem, value;
2187
2188                                 RNA_property_enum_items(ptr, prop, &item, &totitem);
2189                                 value= RNA_property_enum_get(ptr, prop);
2190
2191                                 dynstr= BLI_dynstr_new();
2192                                 BLI_dynstr_appendf(dynstr, "%s%%t", RNA_property_ui_name(prop));
2193                                 for(i=0; i<totitem; i++) {
2194                                         if(item[i].icon)
2195                                                 BLI_dynstr_appendf(dynstr, "|%s %%i%d %%x%d", item[i].name, item[i].icon, item[i].value);
2196                                         else
2197                                                 BLI_dynstr_appendf(dynstr, "|%s %%x%d", item[i].name, item[i].value);
2198
2199                                         if(value == item[i].value)
2200                                                 icon= item[i].icon;
2201                                 }
2202                                 str= BLI_dynstr_get_cstring(dynstr);
2203                                 BLI_dynstr_free(dynstr);
2204
2205                                 freestr= 1;
2206                         }
2207                         else if(type == ROW && proptype == PROP_ENUM) {
2208                                 const EnumPropertyItem *item;
2209                                 int i, totitem;
2210
2211                                 RNA_property_enum_items(ptr, prop, &item, &totitem);
2212                                 for(i=0; i<totitem; i++) {
2213                                         if(item[i].value == (int)max) {
2214                                                 str= (char*)item[i].name;
2215                                                 icon= item[i].icon;
2216                                         }
2217                                 }
2218
2219                                 if(!str)
2220                                         str= (char*)RNA_property_ui_name(prop);
2221                         }
2222                         else {
2223                                 str= (char*)RNA_property_ui_name(prop);
2224                                 icon= RNA_property_ui_icon(prop);
2225                         }
2226                 }
2227
2228                 if(!tip) {
2229                         if(type == ROW && proptype == PROP_ENUM) {
2230                                 const EnumPropertyItem *item;
2231                                 int i, totitem;
2232
2233                                 RNA_property_enum_items(ptr, prop, &item, &totitem);
2234
2235                                 for(i=0; i<totitem; i++) {
2236                                         if(item[i].value == (int)max) {
2237                                                 if(item[i].description[0])
2238                                                         tip= (char*)item[i].description;
2239                                                 break;
2240                                         }
2241                                 }
2242                         }
2243                 }
2244                 
2245                 if(!tip)
2246                         tip= (char*)RNA_property_ui_description(prop);
2247
2248                 if(min == max || a1 == -1 || a2 == -1) {
2249                         if(proptype == PROP_INT) {
2250                                 int hardmin, hardmax, softmin, softmax, step;
2251
2252                                 RNA_property_int_range(ptr, prop, &hardmin, &hardmax);
2253                                 RNA_property_int_ui_range(ptr, prop, &softmin, &softmax, &step);
2254
2255                                 if(min == max) {
2256                                         min= hardmin;
2257                                         max= hardmax;
2258                                 }
2259                                 if(a1 == -1)
2260                                         a1= step;
2261                                 if(a2 == -1)
2262                                         a2= 0;
2263                         }
2264                         else if(proptype == PROP_FLOAT) {
2265                                 float hardmin, hardmax, softmin, softmax, step, precision;
2266
2267                                 RNA_property_float_range(ptr, prop, &hardmin, &hardmax);
2268                                 RNA_property_float_ui_range(ptr, prop, &softmin, &softmax, &step, &precision);
2269
2270                                 if(min == max) {
2271                                         min= hardmin;
2272                                         max= hardmax;
2273                                 }
2274                                 if(a1 == -1)
2275                                         a1= step;
2276                                 if(a2 == -1)
2277                                         a2= precision;
2278                         }
2279                         else if(proptype == PROP_STRING) {
2280                                 min= 0;
2281                                 max= RNA_property_string_maxlength(prop);
2282                                 if(max == 0) /* interface code should ideally support unlimited length */
2283                                         max= UI_MAX_DRAW_STR; 
2284                         }
2285                 }
2286         }
2287         else
2288                 str= (char*)propname;
2289
2290         /* now create button */
2291         but= ui_def_but(block, type, retval, str, x1, y1, x2, y2, NULL, min, max, a1, a2, tip);
2292
2293         if(prop) {
2294                 but->rnapoin= *ptr;
2295                 but->rnaprop= prop;
2296
2297                 if(RNA_property_array_length(but->rnaprop))
2298                         but->rnaindex= index;
2299                 else
2300                         but->rnaindex= 0;
2301
2302                 if(type == IDPOIN)
2303                         uiButSetCompleteFunc(but, ui_rna_ID_autocomplete, but);
2304
2305         }
2306
2307         if(icon) {
2308                 but->icon= (BIFIconID)icon;
2309                 but->flag |= UI_HAS_ICON;
2310                 but->flag|= UI_ICON_LEFT;
2311         }
2312         
2313         if (!prop || !RNA_property_editable(&but->rnapoin, prop)) {
2314                 but->flag |= UI_BUT_DISABLED;
2315                 but->lock = 1;
2316                 but->lockstr = "";
2317         }
2318
2319         if(freestr)
2320                 MEM_freeN(str);
2321         
2322         return but;
2323 }
2324
2325 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)
2326 {
2327         uiBut *but;
2328         wmOperatorType *ot;
2329         
2330         ot= WM_operatortype_find(opname);
2331
2332         if(!str) {
2333                 if(ot) str= ot->name;
2334                 else str= opname;
2335         }
2336         
2337         if ((!tip || tip[0]=='\0') && ot && ot->description) {
2338                 tip= ot->description;
2339         }
2340
2341         but= ui_def_but(block, type, -1, str, x1, y1, x2, y2, NULL, 0, 0, 0, 0, tip);
2342         but->optype= ot;
2343         but->opcontext= opcontext;
2344
2345         if(!ot) {
2346                 but->flag |= UI_BUT_DISABLED;
2347                 but->lock = 1;
2348                 but->lockstr = "";
2349         }
2350
2351         return but;
2352 }
2353
2354 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)
2355 {
2356         uiBut *but= ui_def_but(block, type, retval, str, x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2357
2358         ui_check_but(but);
2359         
2360         return but;
2361 }
2362
2363         /* if _x_ is a power of two (only one bit) return the power,
2364          * otherwise return -1. 
2365          * (1<<findBitIndex(x))==x for powers of two.
2366          */
2367 static int findBitIndex(unsigned int x) {
2368         if (!x || (x&(x-1))!=0) {       /* x&(x-1) strips lowest bit */
2369                 return -1;
2370         } else {
2371                 int idx= 0;
2372
2373                 if (x&0xFFFF0000)       idx+=16, x>>=16;
2374                 if (x&0xFF00)           idx+=8, x>>=8;
2375                 if (x&0xF0)                     idx+=4, x>>=4;
2376                 if (x&0xC)                      idx+=2, x>>=2;
2377                 if (x&0x2)                      idx+=1;
2378
2379                 return idx;
2380         }
2381 }
2382
2383 /* autocomplete helper functions */
2384 struct AutoComplete {
2385         int maxlen;
2386         char *truncate;
2387         char *startname;
2388 };
2389
2390 AutoComplete *autocomplete_begin(char *startname, int maxlen)
2391 {
2392         AutoComplete *autocpl;
2393         
2394         autocpl= MEM_callocN(sizeof(AutoComplete), "AutoComplete");
2395         autocpl->maxlen= maxlen;
2396         autocpl->truncate= MEM_callocN(sizeof(char)*maxlen, "AutoCompleteTruncate");
2397         autocpl->startname= startname;
2398
2399         return autocpl;
2400 }
2401
2402 void autocomplete_do_name(AutoComplete *autocpl, const char *name)
2403 {
2404         char *truncate= autocpl->truncate;
2405         char *startname= autocpl->startname;
2406         int a;
2407
2408         for(a=0; a<autocpl->maxlen-1; a++) {
2409                 if(startname[a]==0 || startname[a]!=name[a])
2410                         break;
2411         }
2412         /* found a match */
2413         if(startname[a]==0) {
2414                 /* first match */
2415                 if(truncate[0]==0)
2416                         BLI_strncpy(truncate, name, autocpl->maxlen);
2417                 else {
2418                         /* remove from truncate what is not in bone->name */
2419                         for(a=0; a<autocpl->maxlen-1; a++) {
2420                                 if(truncate[a]!=name[a])
2421                                         truncate[a]= 0;
2422                         }
2423                 }
2424         }
2425 }
2426
2427 void autocomplete_end(AutoComplete *autocpl, char *autoname)
2428 {       
2429         if(autocpl->truncate[0])
2430                 BLI_strncpy(autoname, autocpl->truncate, autocpl->maxlen);
2431         else {
2432                 if (autoname != autocpl->startname) /* dont copy a string over its self */
2433                         BLI_strncpy(autoname, autocpl->startname, autocpl->maxlen);
2434         }
2435         MEM_freeN(autocpl->truncate);
2436         MEM_freeN(autocpl);
2437 }
2438
2439 /* autocomplete callback for ID buttons */
2440 static void autocomplete_id(bContext *C, char *str, void *arg_v)
2441 {
2442         int blocktype= (intptr_t)arg_v;
2443         ListBase *listb= wich_libbase(CTX_data_main(C), blocktype);
2444         
2445         if(listb==NULL) return;
2446         
2447         /* search if str matches the beginning of an ID struct */
2448         if(str[0]) {
2449                 AutoComplete *autocpl= autocomplete_begin(str, 22);
2450                 ID *id;
2451                 
2452                 for(id= listb->first; id; id= id->next)
2453                         autocomplete_do_name(autocpl, id->name+2);
2454
2455                 autocomplete_end(autocpl, str);
2456         }
2457 }
2458
2459 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)
2460 {
2461         int bitIdx= findBitIndex(bit);
2462         if (bitIdx==-1) {
2463                 return NULL;
2464         } else {
2465                 return uiDefBut(block, type|BIT|bitIdx, retval, str, x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2466         }
2467 }
2468 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)
2469 {
2470         return uiDefBut(block, type|FLO, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2471 }
2472 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)
2473 {
2474         return uiDefButBit(block, type|FLO, bit, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2475 }
2476 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)
2477 {
2478         return uiDefBut(block, type|INT, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2479 }
2480 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)
2481 {
2482         return uiDefButBit(block, type|INT, bit, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2483 }
2484 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)
2485 {
2486         return uiDefBut(block, type|SHO, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2487 }
2488 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)
2489 {
2490         return uiDefButBit(block, type|SHO, bit, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2491 }
2492 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)
2493 {
2494         return uiDefBut(block, type|CHA, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2495 }
2496 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)
2497 {
2498         return uiDefButBit(block, type|CHA, bit, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2499 }
2500 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)
2501 {
2502         uiBut *but;
2503
2504         but= ui_def_but_rna(block, type, retval, str, x1, y1, x2, y2, ptr, propname, index, min, max, a1, a2, tip);
2505         if(but)
2506                 ui_check_but(but);
2507
2508         return but;
2509 }
2510 uiBut *uiDefButO(uiBlock *block, int type, char *opname, int opcontext, char *str, short x1, short y1, short x2, short y2, char *tip)
2511 {
2512         uiBut *but;
2513
2514         but= ui_def_but_operator(block, type, opname, opcontext, str, x1, y1, x2, y2, tip);
2515         if(but)
2516                 ui_check_but(but);
2517
2518         return but;
2519 }
2520
2521 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)
2522 {
2523         uiBut *but= ui_def_but(block, type, retval, "", x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2524         
2525         but->icon= (BIFIconID) icon;
2526         but->flag|= UI_HAS_ICON;
2527
2528         ui_check_but(but);
2529         
2530         return but;
2531 }
2532 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)
2533 {
2534         int bitIdx= findBitIndex(bit);
2535         if (bitIdx==-1) {
2536                 return NULL;
2537         } else {
2538                 return uiDefIconBut(block, type|BIT|bitIdx, retval, icon, x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2539         }
2540 }
2541
2542 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)
2543 {
2544         return uiDefIconBut(block, type|FLO, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2545 }
2546 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)
2547 {
2548         return uiDefIconButBit(block, type|FLO, bit, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2549 }
2550 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)
2551 {
2552         return uiDefIconBut(block, type|INT, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2553 }
2554 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)
2555 {
2556         return uiDefIconButBit(block, type|INT, bit, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2557 }
2558 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)
2559 {
2560         return uiDefIconBut(block, type|SHO, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2561 }
2562 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)
2563 {
2564         return uiDefIconButBit(block, type|SHO, bit, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2565 }
2566 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)
2567 {
2568         return uiDefIconBut(block, type|CHA, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2569 }
2570 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)
2571 {
2572         return uiDefIconButBit(block, type|CHA, bit, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2573 }
2574 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)
2575 {
2576         uiBut *but;
2577
2578         but= ui_def_but_rna(block, type, retval, "", x1, y1, x2, y2, ptr, propname, index, min, max, a1, a2, tip);
2579         if(but) {
2580                 if(icon) {
2581                         but->icon= (BIFIconID) icon;
2582                         but->flag|= UI_HAS_ICON;
2583                 }
2584                 ui_check_but(but);
2585         }
2586
2587         return but;
2588 }
2589 uiBut *uiDefIconButO(uiBlock *block, int type, char *opname, int opcontext, int icon, short x1, short y1, short x2, short y2, char *tip)
2590 {
2591         uiBut *but;
2592
2593         but= ui_def_but_operator(block, type, opname, opcontext, "", x1, y1, x2, y2, tip);
2594         if(but) {
2595                 but->icon= (BIFIconID) icon;
2596                 but->flag|= UI_HAS_ICON;
2597                 ui_check_but(but);
2598         }
2599
2600         return but;
2601 }
2602
2603 /* Button containing both string label and icon */
2604 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)
2605 {
2606         uiBut *but= ui_def_but(block, type, retval, str, x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2607
2608         but->icon= (BIFIconID) icon;
2609         but->flag|= UI_HAS_ICON;
2610
2611         but->flag|= UI_ICON_LEFT;
2612
2613         ui_check_but(but);
2614
2615         return but;
2616 }
2617 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)
2618 {
2619         int bitIdx= findBitIndex(bit);
2620         if (bitIdx==-1) {
2621                 return NULL;
2622         } else {
2623                 return uiDefIconTextBut(block, type|BIT|bitIdx, retval, icon, str, x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2624         }
2625 }
2626
2627 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)
2628 {
2629         return uiDefIconTextBut(block, type|FLO, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2630 }
2631 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)
2632 {
2633         return uiDefIconTextButBit(block, type|FLO, bit, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2634 }
2635 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)
2636 {
2637         return uiDefIconTextBut(block, type|INT, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2638 }
2639 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)
2640 {
2641         return uiDefIconTextButBit(block, type|INT, bit, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2642 }
2643 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)
2644 {
2645         return uiDefIconTextBut(block, type|SHO, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2646 }
2647 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)
2648 {
2649         return uiDefIconTextButBit(block, type|SHO, bit, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2650 }
2651 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)
2652 {
2653         return uiDefIconTextBut(block, type|CHA, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2654 }
2655 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)
2656 {
2657         return uiDefIconTextButBit(block, type|CHA, bit, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2658 }
2659 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)
2660 {
2661         uiBut *but;
2662
2663         but= ui_def_but_rna(block, type, retval, str, x1, y1, x2, y2, ptr, propname, index, min, max, a1, a2, tip);
2664         if(but) {
2665                 if(icon) {
2666                         but->icon= (BIFIconID) icon;
2667                         but->flag|= UI_HAS_ICON;
2668                 }
2669                 but->flag|= UI_ICON_LEFT;
2670                 ui_check_but(but);
2671         }
2672
2673         return but;
2674 }
2675 uiBut *uiDefIconTextButO(uiBlock *block, int type, char *opname, int opcontext, int icon, char *str, short x1, short y1, short x2, short y2, char *tip)
2676 {
2677         uiBut *but;
2678
2679         but= ui_def_but_operator(block, type, opname, opcontext, str, x1, y1, x2, y2, tip);
2680         if(but) {
2681                 but->icon= (BIFIconID) icon;
2682                 but->flag|= UI_HAS_ICON;
2683                 but->flag|= UI_ICON_LEFT;
2684                 ui_check_but(but);
2685         }
2686
2687         return but;
2688 }
2689
2690 static int ui_menu_y(uiBlock *block)
2691 {
2692         uiBut *but= block->buttons.last;
2693
2694         if(but) return but->y1;
2695         else return 0;
2696 }
2697
2698 uiBut *uiDefMenuButO(uiBlock *block, char *opname, char *name)
2699 {
2700         int y= ui_menu_y(block) - MENU_ITEM_HEIGHT;
2701         return uiDefIconTextButO(block, BUT, opname, WM_OP_INVOKE_REGION_WIN, ICON_BLANK1, name, 0, y, MENU_WIDTH, MENU_ITEM_HEIGHT-1, NULL);
2702 }
2703
2704 uiBut *uiDefMenuSep(uiBlock *block)
2705 {
2706         int y= ui_menu_y(block) - MENU_SEP_HEIGHT;
2707         return uiDefBut(block, SEPR, 0, "", 0, y, MENU_WIDTH, MENU_SEP_HEIGHT, NULL, 0.0, 0.0, 0, 0, "");
2708 }
2709
2710 /* END Button containing both string label and icon */
2711
2712 void uiSetButLink(uiBut *but, void **poin, void ***ppoin, short *tot, int from, int to)
2713 {
2714         uiLink *link;
2715         
2716         link= but->link= MEM_callocN(sizeof(uiLink), "new uilink");
2717         
2718         link->poin= poin;
2719         link->ppoin= ppoin;
2720         link->totlink= tot;
2721         link->fromcode= from;
2722         link->tocode= to;
2723 }
2724
2725 /* cruft to make uiBlock and uiBut private */
2726
2727 int uiBlocksGetYMin(ListBase *lb)
2728 {
2729         uiBlock *block;
2730         int min= 0;
2731         
2732         for (block= lb->first; block; block= block->next)
2733                 if (block==lb->first || block->miny<min)
2734                         min= block->miny;
2735                         
2736         return min;
2737 }
2738
2739 void uiBlockSetDirection(uiBlock *block, int direction)
2740 {
2741         block->direction= direction;
2742 }
2743
2744 /* this call escapes if there's alignment flags */
2745 void uiBlockFlipOrder(uiBlock *block)
2746 {
2747         ListBase lb;
2748         uiBut *but, *next;
2749         float centy, miny=10000, maxy= -10000;
2750
2751         if(U.uiflag & USER_MENUFIXEDORDER)
2752                 return;
2753         
2754         for(but= block->buttons.first; but; but= but->next) {
2755                 if(but->flag & UI_BUT_ALIGN) return;
2756                 if(but->y1 < miny) miny= but->y1;
2757                 if(but->y2 > maxy) maxy= but->y2;
2758         }
2759         /* mirror trick */
2760         centy= (miny+maxy)/2.0;
2761         for(but= block->buttons.first; but; but= but->next) {
2762                 but->y1 = centy-(but->y1-centy);
2763                 but->y2 = centy-(but->y2-centy);
2764                 SWAP(float, but->y1, but->y2);
2765         }
2766         
2767         /* also flip order in block itself, for example for arrowkey */
2768         lb.first= lb.last= NULL;
2769         but= block->buttons.first;
2770         while(but) {
2771                 next= but->next;
2772                 BLI_remlink(&block->buttons, but);
2773                 BLI_addtail(&lb, but);
2774                 but= next;
2775         }
2776         block->buttons= lb;
2777 }
2778
2779
2780 void uiBlockSetFlag(uiBlock *block, int flag)
2781 {
2782         block->flag|= flag;
2783 }
2784
2785 void uiBlockClearFlag(uiBlock *block, int flag)
2786 {
2787         block->flag&= ~flag;
2788 }
2789
2790 void uiBlockSetXOfs(uiBlock *block, int xofs)
2791 {
2792         block->xofs= xofs;
2793 }
2794
2795 void uiButSetFlag(uiBut *but, int flag)
2796 {
2797         but->flag|= flag;
2798 }
2799
2800 void uiButClearFlag(uiBut *but, int flag)
2801 {
2802         but->flag&= ~flag;
2803 }
2804
2805 int uiButGetRetVal(uiBut *but)
2806 {
2807         return but->retval;
2808 }
2809
2810 PointerRNA *uiButGetOperatorPtrRNA(uiBut *but)
2811 {
2812         if(but->optype && !but->opptr) {
2813                 but->opptr= MEM_callocN(sizeof(PointerRNA), "uiButOpPtr");
2814                 WM_operator_properties_create(but->opptr, but->optype->idname);
2815         }
2816
2817         return but->opptr;
2818 }
2819
2820 void uiBlockSetHandleFunc(uiBlock *block, uiBlockHandleFunc func, void *arg)
2821 {
2822         block->handle_func= func;
2823         block->handle_func_arg= arg;
2824 }
2825
2826 void uiBlockSetButmFunc(uiBlock *block, uiMenuHandleFunc func, void *arg)
2827 {
2828         block->butm_func= func;
2829         block->butm_func_arg= arg;
2830 }
2831
2832 void uiBlockSetFunc(uiBlock *block, uiButHandleFunc func, void *arg1, void *arg2)
2833 {
2834         block->func= func;
2835         block->func_arg1= arg1;
2836         block->func_arg2= arg2;
2837 }
2838
2839 void uiBlockSetRenameFunc(uiBlock *block, uiButHandleRenameFunc func, void *arg1)
2840 {
2841         
2842 }
2843
2844 void uiBlockSetDrawExtraFunc(uiBlock *block, void (*func)(const bContext *C, void *idv, rcti *rect))
2845 {
2846         block->drawextra= func;
2847 }
2848
2849 void uiButSetFunc(uiBut *but, uiButHandleFunc func, void *arg1, void *arg2)
2850 {
2851         but->func= func;
2852         but->func_arg1= arg1;
2853         but->func_arg2= arg2;
2854 }
2855
2856 void uiButSetNFunc(uiBut *but, uiButHandleNFunc funcN, void *argN, void *arg2)
2857 {
2858         but->funcN= funcN;
2859         but->func_argN= argN;
2860         but->func_arg2= arg2;
2861 }
2862
2863 void uiButSetCompleteFunc(uiBut *but, uiButCompleteFunc func, void *arg)
2864 {
2865         but->autocomplete_func= func;
2866         but->autofunc_arg= arg;
2867 }
2868
2869 uiBut *uiDefIDPoinBut(uiBlock *block, uiIDPoinFuncFP func, short blocktype, int retval, char *str, short x1, short y1, short x2, short y2, void *idpp, char *tip)
2870 {
2871         uiBut *but= ui_def_but(block, IDPOIN, retval, str, x1, y1, x2, y2, NULL, 0.0, 0.0, 0.0, 0.0, tip);
2872         but->idpoin_func= func;
2873         but->idpoin_idpp= (ID**) idpp;
2874         ui_check_but(but);
2875         
2876         if(blocktype)
2877                 uiButSetCompleteFunc(but, autocomplete_id, (void *)(intptr_t)blocktype);
2878
2879         return but;
2880 }
2881
2882 uiBut *uiDefBlockBut(uiBlock *block, uiBlockCreateFunc func, void *arg, char *str, short x1, short y1, short x2, short y2, char *tip)
2883 {
2884         uiBut *but= ui_def_but(block, BLOCK, 0, str, x1, y1, x2, y2, arg, 0.0, 0.0, 0.0, 0.0, tip);
2885         but->block_create_func= func;
2886         ui_check_but(but);
2887         return but;
2888 }
2889
2890 uiBut *uiDefBlockButN(uiBlock *block, uiBlockCreateFunc func, void *argN, char *str, short x1, short y1, short x2, short y2, char *tip)
2891 {
2892         uiBut *but= ui_def_but(block, BLOCK, 0, str, x1, y1, x2, y2, NULL, 0.0, 0.0, 0.0, 0.0, tip);
2893         but->block_create_func= func;
2894         but->func_argN= argN;
2895         ui_check_but(but);
2896         return but;
2897 }
2898
2899
2900 uiBut *uiDefPulldownBut(uiBlock *block, uiBlockCreateFunc func, void *arg, char *str, short x1, short y1, short x2, short y2, char *tip)
2901 {
2902         uiBut *but= ui_def_but(block, PULLDOWN, 0, str, x1, y1, x2, y2, arg, 0.0, 0.0, 0.0, 0.0, tip);
2903         but->block_create_func= func;
2904         ui_check_but(but);
2905         return but;
2906 }
2907
2908 uiBut *uiDefMenuBut(uiBlock *block, uiMenuCreateFunc func, void *arg, char *str, short x1, short y1, short x2, short y2, char *tip)
2909 {
2910         uiBut *but= ui_def_but(block, PULLDOWN, 0, str, x1, y1, x2, y2, arg, 0.0, 0.0, 0.0, 0.0, tip);
2911         but->menu_create_func= func;
2912         ui_check_but(but);
2913         return but;
2914 }
2915
2916 uiBut *uiDefIconTextMenuBut(uiBlock *block, uiMenuCreateFunc func, void *arg, int icon, char *str, short x1, short y1, short x2, short y2, char *tip)
2917 {
2918         uiBut *but= ui_def_but(block, PULLDOWN, 0, str, x1, y1, x2, y2, arg, 0.0, 0.0, 0.0, 0.0, tip);
2919
2920         but->icon= (BIFIconID) icon;
2921         but->flag|= UI_HAS_ICON;
2922
2923         but->flag|= UI_ICON_LEFT;
2924         but->flag|= UI_ICON_SUBMENU;
2925
2926         but->menu_create_func= func;
2927         ui_check_but(but);
2928
2929         return but;
2930 }
2931
2932 /* Block button containing both string label and icon */
2933 uiBut *uiDefIconTextBlockBut(uiBlock *block, uiBlockCreateFunc func, void *arg, int icon, char *str, short x1, short y1, short x2, short y2, char *tip)
2934 {
2935         uiBut *but= ui_def_but(block, BLOCK, 0, str, x1, y1, x2, y2, arg, 0.0, 0.0, 0.0, 0.0, tip);
2936         
2937         /* XXX temp, old menu calls pass on icon arrow, which is now UI_ICON_SUBMENU flag */
2938         if(icon!=ICON_RIGHTARROW_THIN) {
2939                 but->icon= (BIFIconID) icon;
2940                 but->flag|= UI_ICON_LEFT;
2941         }
2942         but->flag|= UI_HAS_ICON;
2943         but->flag|= UI_ICON_SUBMENU;
2944
2945         but->block_create_func= func;
2946         ui_check_but(but);
2947         
2948         return but;
2949 }
2950
2951 /* Block button containing icon */
2952 uiBut *uiDefIconBlockBut(uiBlock *block, uiBlockCreateFunc func, void *arg, int retval, int icon, short x1, short y1, short x2, short y2, char *tip)
2953 {
2954         uiBut *but= ui_def_but(block, BLOCK, retval, "", x1, y1, x2, y2, arg, 0.0, 0.0, 0.0, 0.0, tip);
2955         
2956         but->icon= (BIFIconID) icon;
2957         but->flag|= UI_HAS_ICON;
2958         
2959         but->flag|= UI_ICON_LEFT;
2960         but->flag|= UI_ICON_SUBMENU;
2961         
2962         but->block_create_func= func;
2963         ui_check_but(but);
2964         
2965         return but;
2966 }
2967
2968 void uiDefKeyevtButS(uiBlock *block, int retval, char *str, short x1, short y1, short x2, short y2, short *spoin, char *tip)
2969 {
2970         uiBut *but= ui_def_but(block, KEYEVT|SHO, retval, str, x1, y1, x2, y2, spoin, 0.0, 0.0, 0.0, 0.0, tip);
2971         ui_check_but(but);
2972 }
2973
2974 /* arg is pointer to string/name, use uiButSetSearchFunc() below to make this work */
2975 uiBut *uiDefSearchBut(uiBlock *block, void *arg, int retval, int icon, int maxlen, short x1, short y1, short x2, short y2, char *tip)
2976 {
2977         uiBut *but= ui_def_but(block, SEARCH_MENU, retval, "", x1, y1, x2, y2, arg, 0.0, maxlen, 0.0, 0.0, tip);
2978         
2979         but->icon= (BIFIconID) icon;
2980         but->flag|= UI_HAS_ICON;
2981         
2982         but->flag|= UI_ICON_LEFT|UI_TEXT_LEFT;
2983         
2984         ui_check_but(but);
2985         
2986         return but;
2987 }
2988
2989 /* arg is user value, searchfunc and handlefunc both get it as arg */
2990 void uiButSetSearchFunc(uiBut *but, uiButSearchFunc sfunc, void *arg, uiButHandleFunc bfunc)
2991 {
2992         but->search_func= sfunc;
2993         but->search_arg= arg;
2994         
2995         uiButSetFunc(but, bfunc, arg, NULL);
2996 }
2997
2998
2999 /* Program Init/Exit */
3000
3001 void UI_init(void)
3002 {
3003         ui_resources_init();
3004 }
3005
3006 /* after reading userdef file */
3007 void UI_init_userdef(void)
3008 {
3009         /* fix saved themes */
3010         init_userdef_do_versions();
3011         /* set default colors in default theme */
3012         ui_theme_init_userdef();
3013         
3014         uiStyleInit();
3015 }
3016
3017 void UI_exit(void)
3018 {
3019         ui_resources_free();
3020 }
3021