73425eac0e16e1d4dfd2dbf50065dcfb3c186b6e
[blender.git] / source / blender / editors / interface / interface.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * Contributor(s): Blender Foundation 2002-2008, full recode.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 #include <float.h>
29 #include <math.h>
30 #include <string.h>
31  
32 #include "MEM_guardedalloc.h"
33
34 #include "DNA_ID.h"
35 #include "DNA_listBase.h"
36 #include "DNA_scene_types.h"
37 #include "DNA_screen_types.h"
38 #include "DNA_texture_types.h"
39 #include "DNA_userdef_types.h"
40
41 #include "BLI_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         StructRNA *srna;
1340
1341         /* look for collection property in Main */
1342         RNA_pointer_create(NULL, &RNA_Main, CTX_data_main(C), ptr);
1343
1344         *prop= NULL;
1345
1346         RNA_STRUCT_BEGIN(ptr, iprop) {
1347                 /* if it's a collection and has same pointer type, we've got it */
1348                 if(RNA_property_type(iprop) == PROP_COLLECTION) {
1349                         srna= RNA_property_pointer_type(ptr, iprop);
1350
1351                         if(RNA_property_pointer_type(ptr, but->rnaprop) == srna) {
1352                                 *prop= iprop;
1353                                 break;
1354                         }
1355                 }
1356         }
1357         RNA_STRUCT_END;
1358 }
1359
1360 /* autocomplete callback for RNA pointers */
1361 static void ui_rna_ID_autocomplete(bContext *C, char *str, void *arg_but)
1362 {
1363         uiBut *but= arg_but;
1364         AutoComplete *autocpl;
1365         PointerRNA ptr;
1366         PropertyRNA *prop;
1367         char *name;
1368         
1369         if(str[0]==0) return;
1370
1371         /* get the collection */
1372         ui_rna_ID_collection(C, but, &ptr, &prop);
1373         if(prop==NULL) return;
1374
1375         autocpl= autocomplete_begin(str, ui_get_but_string_max_length(but));
1376
1377         /* loop over items in collection */
1378         RNA_PROP_BEGIN(&ptr, itemptr, prop) {
1379                 name= RNA_struct_name_get_alloc(&itemptr, NULL, 0);
1380
1381                 /* test item name */
1382                 if(name) {
1383                         autocomplete_do_name(autocpl, name);
1384                         MEM_freeN(name);
1385                 }
1386         }
1387         RNA_PROP_END;
1388
1389         autocomplete_end(autocpl, str);
1390 }
1391
1392 int ui_set_but_string(bContext *C, uiBut *but, const char *str)
1393 {
1394         if(but->rnaprop && ELEM3(but->type, TEX, IDPOIN, SEARCH_MENU)) {
1395                 if(RNA_property_editable(&but->rnapoin, but->rnaprop)) {
1396                         PropertyType type;
1397
1398                         type= RNA_property_type(but->rnaprop);
1399
1400                         if(type == PROP_STRING) {
1401                                 /* RNA string */
1402                                 RNA_property_string_set(&but->rnapoin, but->rnaprop, str);
1403                                 return 1;
1404                         }
1405                         else if(type == PROP_POINTER) {
1406                                 /* RNA pointer */
1407                                 PointerRNA ptr, rptr;
1408                                 PropertyRNA *prop;
1409
1410                                 /* XXX only ID pointers at the moment, needs to support
1411                                  * custom collection too for bones, vertex groups, .. */
1412                                 ui_rna_ID_collection(C, but, &ptr, &prop);
1413
1414                                 if(str == NULL || str[0] == '\0') {
1415                                         memset(&rptr, 0, sizeof(rptr));
1416                                         RNA_property_pointer_set(&but->rnapoin, but->rnaprop, rptr);
1417                                         return 1;
1418                                 }
1419                                 else if(prop && RNA_property_collection_lookup_string(&ptr, prop, str, &rptr)) {
1420                                         RNA_property_pointer_set(&but->rnapoin, but->rnaprop, rptr);
1421                                         return 1;
1422                                 }
1423                                 else
1424                                         return 0;
1425                         }
1426                 }
1427         }
1428         else if(but->type == IDPOIN) {
1429                 /* ID pointer */
1430         but->idpoin_func(C, (char*)str, but->idpoin_idpp);
1431                 return 1;
1432         }
1433         else if(but->type == TEX) {
1434                 /* string */
1435                 BLI_strncpy(but->poin, str, but->hardmax);
1436                 return 1;
1437         }
1438         else if(but->type == SEARCH_MENU) {
1439                 /* string */
1440                 BLI_strncpy(but->poin, str, but->hardmax);
1441                 return 1;
1442         }
1443         else {
1444                 double value;
1445
1446                 /* XXX 2.50 missing python api */
1447 #if 0
1448                 if(BPY_button_eval(str, &value)) {
1449                         BKE_report(CTX_reports(C), RPT_WARNING, "Invalid Python expression, check console");
1450                         value = 0.0f; /* Zero out value on error */
1451                         
1452                         if(str[0])
1453                                 return 0;
1454                 }
1455 #else
1456                 value= atof(str);
1457 #endif
1458
1459                 if(!ui_is_but_float(but)) value= (int)value;
1460                 if(but->type==NUMABS) value= fabs(value);
1461
1462                 /* not that we use hard limits here */
1463                 if(value<but->hardmin) value= but->hardmin;
1464                 if(value>but->hardmax) value= but->hardmax;
1465
1466                 ui_set_but_val(but, value);
1467                 return 1;
1468         }
1469
1470         return 0;
1471 }
1472
1473 static double soft_range_round_up(double value, double max)
1474 {
1475         /* round up to .., 0.1, 0.2, 0.5, 1, 2, 5, 10, 20, 50, .. */
1476         double newmax= pow(10.0, ceil(log(value)/log(10.0)));
1477
1478         if(newmax*0.2 >= max && newmax*0.2 >= value)
1479                 return newmax*0.2;
1480         else if(newmax*0.5 >= max && newmax*0.5 >= value)
1481                 return newmax*0.5;
1482         else
1483                 return newmax;
1484 }
1485
1486 static double soft_range_round_down(double value, double max)
1487 {
1488         /* round down to .., 0.1, 0.2, 0.5, 1, 2, 5, 10, 20, 50, .. */
1489         double newmax= pow(10.0, floor(log(value)/log(10.0)));
1490
1491         if(newmax*5.0 <= max && newmax*5.0 <= value)
1492                 return newmax*5.0;
1493         else if(newmax*2.0 <= max && newmax*2.0 <= value)
1494                 return newmax*2.0;
1495         else
1496                 return newmax;
1497 }
1498
1499 void ui_set_but_soft_range(uiBut *but, double value)
1500 {
1501         PropertyType type;
1502         double softmin, softmax, step, precision;
1503         
1504         if(but->rnaprop) {
1505                 type= RNA_property_type(but->rnaprop);
1506
1507                 if(type == PROP_INT) {
1508                         int imin, imax, istep;
1509
1510                         RNA_property_int_ui_range(&but->rnapoin, but->rnaprop, &imin, &imax, &istep);
1511                         softmin= imin;
1512                         softmax= imax;
1513                         step= istep;
1514                         precision= 1;
1515                 }
1516                 else if(type == PROP_FLOAT) {
1517                         float fmin, fmax, fstep, fprecision;
1518
1519                         RNA_property_float_ui_range(&but->rnapoin, but->rnaprop, &fmin, &fmax, &fstep, &fprecision);
1520                         softmin= fmin;
1521                         softmax= fmax;
1522                         step= fstep;
1523                         precision= fprecision;
1524                 }
1525                 else
1526                         return;
1527
1528                 /* clamp button range to something reasonable in case
1529                  * we get -inf/inf from RNA properties */
1530                 softmin= MAX2(softmin, -1e4);
1531                 softmax= MIN2(softmax, 1e4);
1532
1533                 /* if the value goes out of the soft/max range, adapt the range */
1534                 if(value+1e-10 < softmin) {
1535                         if(value < 0.0)
1536                                 softmin= -soft_range_round_up(-value, -softmin);
1537                         else
1538                                 softmin= soft_range_round_down(value, softmin);
1539
1540                         if(softmin < but->hardmin)
1541                                 softmin= but->hardmin;
1542                 }
1543                 else if(value-1e-10 > softmax) {
1544                         if(value < 0.0)
1545                                 softmax= -soft_range_round_down(-value, -softmax);
1546                         else
1547                                 softmax= soft_range_round_up(value, softmax);
1548
1549                         if(softmax > but->hardmax)
1550                                 softmax= but->hardmax;
1551                 }
1552
1553                 but->softmin= softmin;
1554                 but->softmax= softmax;
1555         }
1556 }
1557
1558 /* ******************* Free ********************/
1559
1560 static void ui_free_link(uiLink *link)
1561 {
1562         if(link) {      
1563                 BLI_freelistN(&link->lines);
1564                 MEM_freeN(link);
1565         }
1566 }
1567
1568 /* can be called with C==NULL */
1569 static void ui_free_but(const bContext *C, uiBut *but)
1570 {
1571         if(but->opptr) {
1572                 WM_operator_properties_free(but->opptr);
1573                 MEM_freeN(but->opptr);
1574         }
1575         if(but->func_argN) MEM_freeN(but->func_argN);
1576         if(but->active) {
1577                 /* XXX solve later, buttons should be free-able without context? */
1578                 if(C) 
1579                         ui_button_active_cancel(C, but);
1580                 else
1581                         if(but->active) 
1582                                 MEM_freeN(but->active);
1583         }
1584         if(but->str && but->str != but->strdata) MEM_freeN(but->str);
1585         ui_free_link(but->link);
1586
1587         MEM_freeN(but);
1588 }
1589
1590 /* can be called with C==NULL */
1591 void uiFreeBlock(const bContext *C, uiBlock *block)
1592 {
1593         uiBut *but;
1594
1595         while( (but= block->buttons.first) ) {
1596                 BLI_remlink(&block->buttons, but);      
1597                 ui_free_but(C, but);
1598         }
1599
1600         CTX_store_free_list(&block->contexts);
1601
1602         BLI_freelistN(&block->saferct);
1603         
1604         MEM_freeN(block);
1605 }
1606
1607 /* can be called with C==NULL */
1608 void uiFreeBlocks(const bContext *C, ListBase *lb)
1609 {
1610         uiBlock *block;
1611         
1612         while( (block= lb->first) ) {
1613                 BLI_remlink(lb, block);
1614                 uiFreeBlock(C, block);
1615         }
1616 }
1617
1618 void uiFreeInactiveBlocks(const bContext *C, ListBase *lb)
1619 {
1620         uiBlock *block, *nextblock;
1621
1622         for(block=lb->first; block; block=nextblock) {
1623                 nextblock= block->next;
1624         
1625                 if(!block->handle) {
1626                         if(!block->active) {
1627                                 BLI_remlink(lb, block);
1628                                 uiFreeBlock(C, block);
1629                         }
1630                         else
1631                                 block->active= 0;
1632                 }
1633         }
1634 }
1635
1636 void uiBlockSetRegion(uiBlock *block, ARegion *region)
1637 {
1638         ListBase *lb;
1639         uiBlock *oldblock= NULL;
1640
1641         lb= &region->uiblocks;
1642         
1643         /* each listbase only has one block with this name, free block
1644          * if is already there so it can be rebuilt from scratch */
1645         if(lb) {
1646                 for (oldblock= lb->first; oldblock; oldblock= oldblock->next)
1647                         if (BLI_streq(oldblock->name, block->name))
1648                                 break;
1649
1650                 if (oldblock) {
1651                         oldblock->active= 0;
1652                         oldblock->panel= NULL;
1653                 }
1654         }
1655
1656         block->oldblock= oldblock;
1657
1658         /* at the beginning of the list! for dynamical menus/blocks */
1659         if(lb)
1660                 BLI_addhead(lb, block);
1661 }
1662
1663 uiBlock *uiBeginBlock(const bContext *C, ARegion *region, const char *name, short dt)
1664 {
1665         uiBlock *block;
1666         wmWindow *window;
1667         int getsizex, getsizey;
1668
1669         window= CTX_wm_window(C);
1670
1671         block= MEM_callocN(sizeof(uiBlock), "uiBlock");
1672         block->active= 1;
1673         block->dt= dt;
1674         BLI_strncpy(block->name, name, sizeof(block->name));
1675
1676         if(region)
1677                 uiBlockSetRegion(block, region);
1678
1679         /* window matrix and aspect */
1680         if(region && region->swinid) {
1681                 wm_subwindow_getmatrix(window, region->swinid, block->winmat);
1682                 wm_subwindow_getsize(window, region->swinid, &getsizex, &getsizey);
1683
1684                 /* TODO - investigate why block->winmat[0][0] is negative
1685                  * in the image view when viewRedrawForce is called */
1686                 block->aspect= 2.0/fabs( (getsizex)*block->winmat[0][0]);
1687         }
1688         else {
1689                 /* no subwindow created yet, for menus for example, so we
1690                  * use the main window instead, since buttons are created
1691                  * there anyway */
1692                 wm_subwindow_getmatrix(window, window->screen->mainwin, block->winmat);
1693                 wm_subwindow_getsize(window, window->screen->mainwin, &getsizex, &getsizey);
1694
1695                 block->aspect= 2.0/fabs(getsizex*block->winmat[0][0]);
1696                 block->auto_open= 2;
1697                 block->flag |= UI_BLOCK_LOOP; /* tag as menu */
1698         }
1699
1700         return block;
1701 }
1702
1703 uiBlock *uiGetBlock(char *name, ARegion *ar)
1704 {
1705         uiBlock *block= ar->uiblocks.first;
1706         
1707         while(block) {
1708                 if( strcmp(name, block->name)==0 ) return block;
1709                 block= block->next;
1710         }
1711         
1712         return NULL;
1713 }
1714
1715 void uiBlockSetEmboss(uiBlock *block, short dt)
1716 {
1717         block->dt= dt;
1718 }
1719
1720 void ui_check_but(uiBut *but)
1721 {
1722         /* if something changed in the button */
1723         double value;
1724         float okwidth;
1725 //      int transopts= ui_translate_buttons();
1726         
1727         ui_is_but_sel(but);
1728         
1729 //      if(but->type==TEX || but->type==IDPOIN) transopts= 0;
1730
1731         /* test for min and max, icon sliders, etc */
1732         switch( but->type ) {
1733                 case NUM:
1734                 case SLI:
1735                 case SCROLL:
1736                 case NUMSLI:
1737                 case HSVSLI:
1738                         value= ui_get_but_val(but);
1739                         if(value < but->hardmin) ui_set_but_val(but, but->hardmin);
1740                         else if(value > but->hardmax) ui_set_but_val(but, but->hardmax);
1741                         break;
1742                         
1743                 case NUMABS:
1744                         value= fabs( ui_get_but_val(but) );
1745                         if(value < but->hardmin) ui_set_but_val(but, but->hardmin);
1746                         else if(value > but->hardmax) ui_set_but_val(but, but->hardmax);
1747                         break;
1748                         
1749                 case ICONTOG: 
1750                 case ICONTOGN:
1751                         if(!but->rnaprop || (RNA_property_flag(but->rnaprop) & PROP_ICONS_CONSECUTIVE)) {
1752                                 if(but->flag & UI_SELECT) but->iconadd= 1;
1753                                 else but->iconadd= 0;
1754                         }
1755                         break;
1756                         
1757                 case ICONROW:
1758                         if(!but->rnaprop || (RNA_property_flag(but->rnaprop) & PROP_ICONS_CONSECUTIVE)) {
1759                                 value= ui_get_but_val(but);
1760                                 but->iconadd= (int)value- (int)(but->hardmin);
1761                         }
1762                         break;
1763                         
1764                 case ICONTEXTROW:
1765                         if(!but->rnaprop || (RNA_property_flag(but->rnaprop) & PROP_ICONS_CONSECUTIVE)) {
1766                                 value= ui_get_but_val(but);
1767                                 but->iconadd= (int)value- (int)(but->hardmin);
1768                         }
1769                         break;
1770         }
1771         
1772         
1773         /* safety is 4 to enable small number buttons (like 'users') */
1774         okwidth= -4 + (but->x2 - but->x1); 
1775         
1776         /* name: */
1777         switch( but->type ) {
1778         
1779         case MENU:
1780         case ICONTEXTROW:
1781                 
1782                 if(but->x2 - but->x1 > 24) {
1783                         value= ui_get_but_val(but);
1784                         ui_set_name_menu(but, (int)value);
1785                 }
1786                 break;
1787         
1788         case NUM:
1789         case NUMSLI:
1790         case HSVSLI:
1791         case NUMABS:
1792
1793                 value= ui_get_but_val(but);
1794
1795                 if(ui_is_but_float(but)) {
1796                         if(value == FLT_MAX) sprintf(but->drawstr, "%sinf", but->str);
1797                         else if(value == -FLT_MAX) sprintf(but->drawstr, "%s-inf", but->str);
1798                         else if(but->a2) { /* amount of digits defined */
1799                                 if(but->a2==1) sprintf(but->drawstr, "%s%.1f", but->str, value);
1800                                 else if(but->a2==2) sprintf(but->drawstr, "%s%.2f", but->str, value);
1801                                 else if(but->a2==3) sprintf(but->drawstr, "%s%.3f", but->str, value);
1802                                 else sprintf(but->drawstr, "%s%.4f", but->str, value);
1803                         }
1804                         else {
1805                                 if(but->hardmax<10.001) sprintf(but->drawstr, "%s%.3f", but->str, value);
1806                                 else sprintf(but->drawstr, "%s%.2f", but->str, value);
1807                         }
1808                 }
1809                 else {
1810                         sprintf(but->drawstr, "%s%d", but->str, (int)value);
1811                 }
1812                         
1813                 if(but->rnaprop) {
1814                         PropertySubType pstype = RNA_property_subtype(but->rnaprop);
1815                         
1816                         if (pstype == PROP_PERCENTAGE)
1817                                 strcat(but->drawstr, "%");
1818                 }
1819                 break;
1820
1821         case LABEL:
1822                 if(ui_is_but_float(but)) {
1823                         value= ui_get_but_val(but);
1824                         if(but->a2) { /* amount of digits defined */
1825                                 if(but->a2==1) sprintf(but->drawstr, "%s%.1f", but->str, value);
1826                                 else if(but->a2==2) sprintf(but->drawstr, "%s%.2f", but->str, value);
1827                                 else if(but->a2==3) sprintf(but->drawstr, "%s%.3f", but->str, value);
1828                                 else sprintf(but->drawstr, "%s%.4f", but->str, value);
1829                         }
1830                         else {
1831                                 sprintf(but->drawstr, "%s%.2f", but->str, value);
1832                         }
1833                 }
1834                 else strcpy(but->drawstr, but->str);
1835                 
1836                 break;
1837
1838         case IDPOIN:
1839         case TEX:
1840         case SEARCH_MENU:
1841                 if(!but->editstr) {
1842                         char str[UI_MAX_DRAW_STR];
1843
1844                         ui_get_but_string(but, str, UI_MAX_DRAW_STR-strlen(but->str));
1845
1846                         strcpy(but->drawstr, but->str);
1847                         strcat(but->drawstr, str);
1848                 }
1849                 break;
1850         
1851         case KEYEVT:
1852                 strcpy(but->drawstr, but->str);
1853                 if (but->flag & UI_SELECT) {
1854                         strcat(but->drawstr, "Press a key");
1855                 } else {
1856                         strcat(but->drawstr, WM_key_event_string((short) ui_get_but_val(but)));
1857                 }
1858                 break;
1859
1860         case BUT_TOGDUAL:
1861                 /* trying to get the dual-icon to left of text... not very nice */
1862                 if(but->str[0]) {
1863                         strcpy(but->drawstr, "  ");
1864                         strcpy(but->drawstr+2, but->str);
1865                 }
1866                 break;
1867         default:
1868                 strcpy(but->drawstr, but->str);
1869                 
1870         }
1871
1872         /* if we are doing text editing, this will override the drawstr */
1873         if(but->editstr) {
1874                 strcpy(but->drawstr, but->str);
1875                 strcat(but->drawstr, but->editstr);
1876         }
1877         
1878         /* text clipping moved to widget drawing code itself */
1879 }
1880
1881
1882 void uiBlockBeginAlign(uiBlock *block)
1883 {
1884         /* if other align was active, end it */
1885         if(block->flag & UI_BUT_ALIGN) uiBlockEndAlign(block);
1886
1887         block->flag |= UI_BUT_ALIGN_DOWN;       
1888         block->alignnr++;
1889
1890         /* buttons declared after this call will get this align nr */ // XXX flag?
1891 }
1892
1893 static int buts_are_horiz(uiBut *but1, uiBut *but2)
1894 {
1895         float dx, dy;
1896         
1897         dx= fabs( but1->x2 - but2->x1);
1898         dy= fabs( but1->y1 - but2->y2);
1899         
1900         if(dx > dy) return 0;
1901         return 1;
1902 }
1903
1904 void uiBlockEndAlign(uiBlock *block)
1905 {
1906         block->flag &= ~UI_BUT_ALIGN;   // all 4 flags
1907 }
1908
1909 int ui_but_can_align(uiBut *but)
1910 {
1911         return !ELEM3(but->type, LABEL, OPTION, OPTIONN);
1912 }
1913
1914 static void ui_block_do_align_but(uiBlock *block, uiBut *first, int nr)
1915 {
1916         uiBut *prev, *but=NULL, *next;
1917         int flag= 0, cols=0, rows=0;
1918         
1919         /* auto align */
1920
1921         for(but=first; but && but->alignnr == nr; but=but->next) {
1922                 if(but->next && but->next->alignnr == nr) {
1923                         if(buts_are_horiz(but, but->next)) cols++;
1924                         else rows++;
1925                 }
1926         }
1927
1928         /* rows==0: 1 row, cols==0: 1 collumn */
1929         
1930         /* note;  how it uses 'flag' in loop below (either set it, or OR it) is confusing */
1931         for(but=first, prev=NULL; but && but->alignnr == nr; prev=but, but=but->next) {
1932                 next= but->next;
1933                 if(next && next->alignnr != nr)
1934                         next= NULL;
1935
1936                 /* clear old flag */
1937                 but->flag &= ~UI_BUT_ALIGN;
1938                 
1939                 if(flag==0) {   /* first case */
1940                         if(next) {
1941                                 if(buts_are_horiz(but, next)) {
1942                                         if(rows==0)
1943                                                 flag= UI_BUT_ALIGN_RIGHT;
1944                                         else 
1945                                                 flag= UI_BUT_ALIGN_DOWN|UI_BUT_ALIGN_RIGHT;
1946                                 }
1947                                 else {
1948                                         flag= UI_BUT_ALIGN_DOWN;
1949                                 }
1950                         }
1951                 }
1952                 else if(next==NULL) {   /* last case */
1953                         if(prev) {
1954                                 if(buts_are_horiz(prev, but)) {
1955                                         if(rows==0) 
1956                                                 flag= UI_BUT_ALIGN_LEFT;
1957                                         else
1958                                                 flag= UI_BUT_ALIGN_TOP|UI_BUT_ALIGN_LEFT;
1959                                 }
1960                                 else flag= UI_BUT_ALIGN_TOP;
1961                         }
1962                 }
1963                 else if(buts_are_horiz(but, next)) {
1964                         /* check if this is already second row */
1965                         if( prev && buts_are_horiz(prev, but)==0) {
1966                                 flag &= ~UI_BUT_ALIGN_LEFT;
1967                                 flag |= UI_BUT_ALIGN_TOP;
1968                                 /* exception case: bottom row */
1969                                 if(rows>0) {
1970                                         uiBut *bt= but;
1971                                         while(bt && bt->alignnr == nr) {
1972                                                 if(bt->next && bt->next->alignnr == nr && buts_are_horiz(bt, bt->next)==0 ) break; 
1973                                                 bt= bt->next;
1974                                         }
1975                                         if(bt==0 || bt->alignnr != nr) flag= UI_BUT_ALIGN_TOP|UI_BUT_ALIGN_RIGHT;
1976                                 }
1977                         }
1978                         else flag |= UI_BUT_ALIGN_LEFT;
1979                 }
1980                 else {
1981                         if(cols==0) {
1982                                 flag |= UI_BUT_ALIGN_TOP;
1983                         }
1984                         else {  /* next button switches to new row */
1985                                 
1986                                 if(prev && buts_are_horiz(prev, but))
1987                                    flag |= UI_BUT_ALIGN_LEFT;
1988                                 
1989                                 if( (flag & UI_BUT_ALIGN_TOP)==0) {     /* stil top row */
1990                                         if(prev)
1991                                                 flag= UI_BUT_ALIGN_DOWN|UI_BUT_ALIGN_LEFT;
1992                                         else 
1993                                                 flag |= UI_BUT_ALIGN_DOWN;
1994                                 }
1995                                 else 
1996                                         flag |= UI_BUT_ALIGN_TOP;
1997                         }
1998                 }
1999                 
2000                 but->flag |= flag;
2001                 
2002                 /* merge coordinates */
2003                 if(prev) {
2004                         // simple cases 
2005                         if(rows==0) {
2006                                 but->x1= (prev->x2+but->x1)/2.0;
2007                                 prev->x2= but->x1;
2008                         }
2009                         else if(cols==0) {
2010                                 but->y2= (prev->y1+but->y2)/2.0;
2011                                 prev->y1= but->y2;
2012                         }
2013                         else {
2014                                 if(buts_are_horiz(prev, but)) {
2015                                         but->x1= (prev->x2+but->x1)/2.0;
2016                                         prev->x2= but->x1;
2017                                         /* copy height too */
2018                                         but->y2= prev->y2;
2019                                 }
2020                                 else if(prev->prev && buts_are_horiz(prev->prev, prev)==0) {
2021                                         /* the previous button is a single one in its row */
2022                                         but->y2= (prev->y1+but->y2)/2.0;
2023                                         prev->y1= but->y2;
2024                                 }
2025                                 else {
2026                                         /* the previous button is not a single one in its row */
2027                                         but->y2= prev->y1;
2028                                 }
2029                         }
2030                 }
2031         }
2032 }
2033
2034 void ui_block_do_align(uiBlock *block)
2035 {
2036         uiBut *but;
2037         int nr;
2038
2039         /* align buttons with same align nr */
2040         for(but=block->buttons.first; but;) {
2041                 if(but->alignnr) {
2042                         nr= but->alignnr;
2043                         ui_block_do_align_but(block, but, nr);
2044
2045                         /* skip with same number */
2046                         for(; but && but->alignnr == nr; but=but->next);
2047
2048                         if(!but)
2049                                 break;
2050                 }
2051                 else
2052                         but= but->next;
2053         }
2054 }
2055
2056 /*
2057 ui_def_but is the function that draws many button types
2058
2059 for float buttons:
2060         "a1" Click Step (how much to change the value each click)
2061         "a2" Number of decimal point values to display. 0 defaults to 3 (0.000) 1,2,3, and a maximum of 4,
2062        all greater values will be clamped to 4.
2063
2064 */
2065 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)
2066 {
2067         uiBut *but;
2068         short slen;
2069         
2070         if(type & BUTPOIN) {            /* a pointer is required */
2071                 if(poin==NULL)
2072                         return NULL;
2073         }
2074
2075         but= MEM_callocN(sizeof(uiBut), "uiBut");
2076
2077         but->type= type & BUTTYPE;
2078         but->pointype= type & BUTPOIN;
2079         but->bit= type & BIT;
2080         but->bitnr= type & 31;
2081         but->icon = 0;
2082
2083         but->retval= retval;
2084         if( strlen(str)>=UI_MAX_NAME_STR-1 ) {
2085                 but->str= MEM_callocN( strlen(str)+2, "uiDefBut");
2086                 strcpy(but->str, str);
2087         }
2088         else {
2089                 but->str= but->strdata;
2090                 strcpy(but->str, str);
2091         }
2092         but->x1= x1; 
2093         but->y1= y1;
2094         but->x2= (x1+x2); 
2095         but->y2= (y1+y2);
2096         
2097         but->poin= poin;
2098         but->hardmin= but->softmin= min; 
2099         but->hardmax= but->softmax= max;
2100         but->a1= a1; 
2101         but->a2= a2;
2102         but->tip= tip;
2103         
2104         but->lock= block->lock;
2105         but->lockstr= block->lockstr;
2106         but->dt= block->dt;
2107
2108         but->aspect= 1.0f; //XXX block->aspect;
2109         but->block= block;              // pointer back, used for frontbuffer status, and picker
2110
2111         if((block->flag & UI_BUT_ALIGN) && ui_but_can_align(but))
2112                 but->alignnr= block->alignnr;
2113
2114         but->func= block->func;
2115         but->func_arg1= block->func_arg1;
2116         but->func_arg2= block->func_arg2;
2117         
2118         but->pos= -1;   /* cursor invisible */
2119
2120         if(ELEM(but->type, NUM, NUMABS)) {      /* add a space to name */
2121                 slen= strlen(but->str);
2122                 if(slen>0 && slen<UI_MAX_NAME_STR-2) {
2123                         if(but->str[slen-1]!=' ') {
2124                                 but->str[slen]= ' ';
2125                                 but->str[slen+1]= 0;
2126                         }
2127                 }
2128         }
2129         
2130         if(ELEM(but->type, HSVCUBE, HSVCIRCLE)) { /* hsv buttons temp storage */
2131                 float rgb[3];
2132                 ui_get_but_vectorf(but, rgb);
2133                 rgb_to_hsv(rgb[0], rgb[1], rgb[2], but->hsv, but->hsv+1, but->hsv+2);
2134         }
2135
2136         if((block->flag & UI_BLOCK_LOOP) || ELEM6(but->type, MENU, TEX, LABEL, IDPOIN, BLOCK, BUTM)) {
2137                 but->flag |= UI_TEXT_LEFT;
2138         }
2139         
2140         if(but->type==BUT_TOGDUAL) {
2141                 but->flag |= UI_ICON_LEFT;
2142         }
2143
2144         but->flag |= (block->flag & UI_BUT_ALIGN);
2145
2146         if (but->lock) {
2147                 if (but->lockstr) {
2148                         but->flag |= UI_BUT_DISABLED;
2149                 }
2150         }
2151
2152         BLI_addtail(&block->buttons, but);
2153         
2154         if(block->curlayout)
2155                 ui_layout_add_but(block->curlayout, but);
2156
2157         return but;
2158 }
2159
2160 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)
2161 {
2162         uiBut *but;
2163         PropertyRNA *prop;
2164         PropertyType proptype;
2165         int freestr= 0, icon= 0;
2166
2167         prop= RNA_struct_find_property(ptr, propname);
2168
2169         if(prop) {
2170                 proptype= RNA_property_type(prop);
2171
2172                 /* use rna values if parameters are not specified */
2173                 if(!str) {
2174                         if(type == MENU && proptype == PROP_ENUM) {
2175                                 const EnumPropertyItem *item;
2176                                 DynStr *dynstr;
2177                                 int i, totitem, value;
2178
2179                                 RNA_property_enum_items(ptr, prop, &item, &totitem);
2180                                 value= RNA_property_enum_get(ptr, prop);
2181
2182                                 dynstr= BLI_dynstr_new();
2183                                 BLI_dynstr_appendf(dynstr, "%s%%t", RNA_property_ui_name(prop));
2184                                 for(i=0; i<totitem; i++) {
2185                                         if(item[i].icon)
2186                                                 BLI_dynstr_appendf(dynstr, "|%s %%i%d %%x%d", item[i].name, item[i].icon, item[i].value);
2187                                         else
2188                                                 BLI_dynstr_appendf(dynstr, "|%s %%x%d", item[i].name, item[i].value);
2189
2190                                         if(value == item[i].value)
2191                                                 icon= item[i].icon;
2192                                 }
2193                                 str= BLI_dynstr_get_cstring(dynstr);
2194                                 BLI_dynstr_free(dynstr);
2195
2196                                 freestr= 1;
2197                         }
2198                         else if(type == ROW && proptype == PROP_ENUM) {
2199                                 const EnumPropertyItem *item;
2200                                 int i, totitem;
2201
2202                                 RNA_property_enum_items(ptr, prop, &item, &totitem);
2203                                 for(i=0; i<totitem; i++) {
2204                                         if(item[i].value == (int)max) {
2205                                                 str= (char*)item[i].name;
2206                                                 icon= item[i].icon;
2207                                         }
2208                                 }
2209
2210                                 if(!str)
2211                                         str= (char*)RNA_property_ui_name(prop);
2212                         }
2213                         else {
2214                                 str= (char*)RNA_property_ui_name(prop);
2215                                 icon= RNA_property_ui_icon(prop);
2216                         }
2217                 }
2218
2219                 if(!tip) {
2220                         if(type == ROW && proptype == PROP_ENUM) {
2221                                 const EnumPropertyItem *item;
2222                                 int i, totitem;
2223
2224                                 RNA_property_enum_items(ptr, prop, &item, &totitem);
2225
2226                                 for(i=0; i<totitem; i++) {
2227                                         if(item[i].value == (int)max) {
2228                                                 if(item[i].description[0])
2229                                                         tip= (char*)item[i].description;
2230                                                 break;
2231                                         }
2232                                 }
2233                         }
2234                 }
2235                 
2236                 if(!tip)
2237                         tip= (char*)RNA_property_ui_description(prop);
2238
2239                 if(min == max || a1 == -1 || a2 == -1) {
2240                         if(proptype == PROP_INT) {
2241                                 int hardmin, hardmax, softmin, softmax, step;
2242
2243                                 RNA_property_int_range(ptr, prop, &hardmin, &hardmax);
2244                                 RNA_property_int_ui_range(ptr, prop, &softmin, &softmax, &step);
2245
2246                                 if(min == max) {
2247                                         min= hardmin;
2248                                         max= hardmax;
2249                                 }
2250                                 if(a1 == -1)
2251                                         a1= step;
2252                                 if(a2 == -1)
2253                                         a2= 0;
2254                         }
2255                         else if(proptype == PROP_FLOAT) {
2256                                 float hardmin, hardmax, softmin, softmax, step, precision;
2257
2258                                 RNA_property_float_range(ptr, prop, &hardmin, &hardmax);
2259                                 RNA_property_float_ui_range(ptr, prop, &softmin, &softmax, &step, &precision);
2260
2261                                 if(min == max) {
2262                                         min= hardmin;
2263                                         max= hardmax;
2264                                 }
2265                                 if(a1 == -1)
2266                                         a1= step;
2267                                 if(a2 == -1)
2268                                         a2= precision;
2269                         }
2270                         else if(proptype == PROP_STRING) {
2271                                 min= 0;
2272                                 max= RNA_property_string_maxlength(prop);
2273                                 if(max == 0) /* interface code should ideally support unlimited length */
2274                                         max= UI_MAX_DRAW_STR; 
2275                         }
2276                 }
2277         }
2278         else
2279                 str= (char*)propname;
2280
2281         /* now create button */
2282         but= ui_def_but(block, type, retval, str, x1, y1, x2, y2, NULL, min, max, a1, a2, tip);
2283
2284         if(prop) {
2285                 but->rnapoin= *ptr;
2286                 but->rnaprop= prop;
2287
2288                 if(RNA_property_array_length(but->rnaprop))
2289                         but->rnaindex= index;
2290                 else
2291                         but->rnaindex= 0;
2292
2293                 if(type == IDPOIN)
2294                         uiButSetCompleteFunc(but, ui_rna_ID_autocomplete, but);
2295
2296         }
2297
2298         if(icon) {
2299                 but->icon= (BIFIconID)icon;
2300                 but->flag |= UI_HAS_ICON;
2301                 but->flag|= UI_ICON_LEFT;
2302         }
2303         
2304         if (!prop || !RNA_property_editable(&but->rnapoin, prop)) {
2305                 but->flag |= UI_BUT_DISABLED;
2306                 but->lock = 1;
2307                 but->lockstr = "";
2308         }
2309
2310         if(freestr)
2311                 MEM_freeN(str);
2312         
2313         return but;
2314 }
2315
2316 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)
2317 {
2318         uiBut *but;
2319         wmOperatorType *ot;
2320         
2321         ot= WM_operatortype_find(opname);
2322
2323         if(!str) {
2324                 if(ot) str= ot->name;
2325                 else str= opname;
2326         }
2327         
2328         if ((!tip || tip[0]=='\0') && ot && ot->description) {
2329                 tip= ot->description;
2330         }
2331
2332         but= ui_def_but(block, type, -1, str, x1, y1, x2, y2, NULL, 0, 0, 0, 0, tip);
2333         but->optype= ot;
2334         but->opcontext= opcontext;
2335
2336         if(!ot) {
2337                 but->flag |= UI_BUT_DISABLED;
2338                 but->lock = 1;
2339                 but->lockstr = "";
2340         }
2341
2342         return but;
2343 }
2344
2345 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)
2346 {
2347         uiBut *but= ui_def_but(block, type, retval, str, x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2348
2349         ui_check_but(but);
2350         
2351         return but;
2352 }
2353
2354         /* if _x_ is a power of two (only one bit) return the power,
2355          * otherwise return -1. 
2356          * (1<<findBitIndex(x))==x for powers of two.
2357          */
2358 static int findBitIndex(unsigned int x) {
2359         if (!x || (x&(x-1))!=0) {       /* x&(x-1) strips lowest bit */
2360                 return -1;
2361         } else {
2362                 int idx= 0;
2363
2364                 if (x&0xFFFF0000)       idx+=16, x>>=16;
2365                 if (x&0xFF00)           idx+=8, x>>=8;
2366                 if (x&0xF0)                     idx+=4, x>>=4;
2367                 if (x&0xC)                      idx+=2, x>>=2;
2368                 if (x&0x2)                      idx+=1;
2369
2370                 return idx;
2371         }
2372 }
2373
2374 /* autocomplete helper functions */
2375 struct AutoComplete {
2376         int maxlen;
2377         char *truncate;
2378         char *startname;
2379 };
2380
2381 AutoComplete *autocomplete_begin(char *startname, int maxlen)
2382 {
2383         AutoComplete *autocpl;
2384         
2385         autocpl= MEM_callocN(sizeof(AutoComplete), "AutoComplete");
2386         autocpl->maxlen= maxlen;
2387         autocpl->truncate= MEM_callocN(sizeof(char)*maxlen, "AutoCompleteTruncate");
2388         autocpl->startname= startname;
2389
2390         return autocpl;
2391 }
2392
2393 void autocomplete_do_name(AutoComplete *autocpl, const char *name)
2394 {
2395         char *truncate= autocpl->truncate;
2396         char *startname= autocpl->startname;
2397         int a;
2398
2399         for(a=0; a<autocpl->maxlen-1; a++) {
2400                 if(startname[a]==0 || startname[a]!=name[a])
2401                         break;
2402         }
2403         /* found a match */
2404         if(startname[a]==0) {
2405                 /* first match */
2406                 if(truncate[0]==0)
2407                         BLI_strncpy(truncate, name, autocpl->maxlen);
2408                 else {
2409                         /* remove from truncate what is not in bone->name */
2410                         for(a=0; a<autocpl->maxlen-1; a++) {
2411                                 if(truncate[a]!=name[a])
2412                                         truncate[a]= 0;
2413                         }
2414                 }
2415         }
2416 }
2417
2418 void autocomplete_end(AutoComplete *autocpl, char *autoname)
2419 {       
2420         if(autocpl->truncate[0])
2421                 BLI_strncpy(autoname, autocpl->truncate, autocpl->maxlen);
2422         else {
2423                 if (autoname != autocpl->startname) /* dont copy a string over its self */
2424                         BLI_strncpy(autoname, autocpl->startname, autocpl->maxlen);
2425         }
2426         MEM_freeN(autocpl->truncate);
2427         MEM_freeN(autocpl);
2428 }
2429
2430 /* autocomplete callback for ID buttons */
2431 static void autocomplete_id(bContext *C, char *str, void *arg_v)
2432 {
2433         int blocktype= (intptr_t)arg_v;
2434         ListBase *listb= wich_libbase(CTX_data_main(C), blocktype);
2435         
2436         if(listb==NULL) return;
2437         
2438         /* search if str matches the beginning of an ID struct */
2439         if(str[0]) {
2440                 AutoComplete *autocpl= autocomplete_begin(str, 22);
2441                 ID *id;
2442                 
2443                 for(id= listb->first; id; id= id->next)
2444                         autocomplete_do_name(autocpl, id->name+2);
2445
2446                 autocomplete_end(autocpl, str);
2447         }
2448 }
2449
2450 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)
2451 {
2452         int bitIdx= findBitIndex(bit);
2453         if (bitIdx==-1) {
2454                 return NULL;
2455         } else {
2456                 return uiDefBut(block, type|BIT|bitIdx, retval, str, x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2457         }
2458 }
2459 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)
2460 {
2461         return uiDefBut(block, type|FLO, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2462 }
2463 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)
2464 {
2465         return uiDefButBit(block, type|FLO, bit, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2466 }
2467 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)
2468 {
2469         return uiDefBut(block, type|INT, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2470 }
2471 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)
2472 {
2473         return uiDefButBit(block, type|INT, bit, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2474 }
2475 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)
2476 {
2477         return uiDefBut(block, type|SHO, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2478 }
2479 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)
2480 {
2481         return uiDefButBit(block, type|SHO, bit, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2482 }
2483 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)
2484 {
2485         return uiDefBut(block, type|CHA, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2486 }
2487 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)
2488 {
2489         return uiDefButBit(block, type|CHA, bit, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2490 }
2491 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)
2492 {
2493         uiBut *but;
2494
2495         but= ui_def_but_rna(block, type, retval, str, x1, y1, x2, y2, ptr, propname, index, min, max, a1, a2, tip);
2496         if(but)
2497                 ui_check_but(but);
2498
2499         return but;
2500 }
2501 uiBut *uiDefButO(uiBlock *block, int type, char *opname, int opcontext, char *str, short x1, short y1, short x2, short y2, char *tip)
2502 {
2503         uiBut *but;
2504
2505         but= ui_def_but_operator(block, type, opname, opcontext, str, x1, y1, x2, y2, tip);
2506         if(but)
2507                 ui_check_but(but);
2508
2509         return but;
2510 }
2511
2512 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)
2513 {
2514         uiBut *but= ui_def_but(block, type, retval, "", x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2515         
2516         but->icon= (BIFIconID) icon;
2517         but->flag|= UI_HAS_ICON;
2518
2519         ui_check_but(but);
2520         
2521         return but;
2522 }
2523 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)
2524 {
2525         int bitIdx= findBitIndex(bit);
2526         if (bitIdx==-1) {
2527                 return NULL;
2528         } else {
2529                 return uiDefIconBut(block, type|BIT|bitIdx, retval, icon, x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2530         }
2531 }
2532
2533 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)
2534 {
2535         return uiDefIconBut(block, type|FLO, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2536 }
2537 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)
2538 {
2539         return uiDefIconButBit(block, type|FLO, bit, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2540 }
2541 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)
2542 {
2543         return uiDefIconBut(block, type|INT, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2544 }
2545 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)
2546 {
2547         return uiDefIconButBit(block, type|INT, bit, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2548 }
2549 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)
2550 {
2551         return uiDefIconBut(block, type|SHO, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2552 }
2553 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)
2554 {
2555         return uiDefIconButBit(block, type|SHO, bit, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2556 }
2557 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)
2558 {
2559         return uiDefIconBut(block, type|CHA, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2560 }
2561 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)
2562 {
2563         return uiDefIconButBit(block, type|CHA, bit, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2564 }
2565 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)
2566 {
2567         uiBut *but;
2568
2569         but= ui_def_but_rna(block, type, retval, "", x1, y1, x2, y2, ptr, propname, index, min, max, a1, a2, tip);
2570         if(but) {
2571                 if(icon) {
2572                         but->icon= (BIFIconID) icon;
2573                         but->flag|= UI_HAS_ICON;
2574                 }
2575                 ui_check_but(but);
2576         }
2577
2578         return but;
2579 }
2580 uiBut *uiDefIconButO(uiBlock *block, int type, char *opname, int opcontext, int icon, short x1, short y1, short x2, short y2, char *tip)
2581 {
2582         uiBut *but;
2583
2584         but= ui_def_but_operator(block, type, opname, opcontext, "", x1, y1, x2, y2, tip);
2585         if(but) {
2586                 but->icon= (BIFIconID) icon;
2587                 but->flag|= UI_HAS_ICON;
2588                 ui_check_but(but);
2589         }
2590
2591         return but;
2592 }
2593
2594 /* Button containing both string label and icon */
2595 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)
2596 {
2597         uiBut *but= ui_def_but(block, type, retval, str, x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2598
2599         but->icon= (BIFIconID) icon;
2600         but->flag|= UI_HAS_ICON;
2601
2602         but->flag|= UI_ICON_LEFT;
2603
2604         ui_check_but(but);
2605
2606         return but;
2607 }
2608 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)
2609 {
2610         int bitIdx= findBitIndex(bit);
2611         if (bitIdx==-1) {
2612                 return NULL;
2613         } else {
2614                 return uiDefIconTextBut(block, type|BIT|bitIdx, retval, icon, str, x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2615         }
2616 }
2617
2618 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)
2619 {
2620         return uiDefIconTextBut(block, type|FLO, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2621 }
2622 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)
2623 {
2624         return uiDefIconTextButBit(block, type|FLO, bit, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2625 }
2626 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)
2627 {
2628         return uiDefIconTextBut(block, type|INT, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2629 }
2630 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)
2631 {
2632         return uiDefIconTextButBit(block, type|INT, bit, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2633 }
2634 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)
2635 {
2636         return uiDefIconTextBut(block, type|SHO, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2637 }
2638 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)
2639 {
2640         return uiDefIconTextButBit(block, type|SHO, bit, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2641 }
2642 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)
2643 {
2644         return uiDefIconTextBut(block, type|CHA, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2645 }
2646 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)
2647 {
2648         return uiDefIconTextButBit(block, type|CHA, bit, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2649 }
2650 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)
2651 {
2652         uiBut *but;
2653
2654         but= ui_def_but_rna(block, type, retval, str, x1, y1, x2, y2, ptr, propname, index, min, max, a1, a2, tip);
2655         if(but) {
2656                 if(icon) {
2657                         but->icon= (BIFIconID) icon;
2658                         but->flag|= UI_HAS_ICON;
2659                 }
2660                 but->flag|= UI_ICON_LEFT;
2661                 ui_check_but(but);
2662         }
2663
2664         return but;
2665 }
2666 uiBut *uiDefIconTextButO(uiBlock *block, int type, char *opname, int opcontext, int icon, char *str, short x1, short y1, short x2, short y2, char *tip)
2667 {
2668         uiBut *but;
2669
2670         but= ui_def_but_operator(block, type, opname, opcontext, str, x1, y1, x2, y2, tip);
2671         if(but) {
2672                 but->icon= (BIFIconID) icon;
2673                 but->flag|= UI_HAS_ICON;
2674                 but->flag|= UI_ICON_LEFT;
2675                 ui_check_but(but);
2676         }
2677
2678         return but;
2679 }
2680
2681 static int ui_menu_y(uiBlock *block)
2682 {
2683         uiBut *but= block->buttons.last;
2684
2685         if(but) return but->y1;
2686         else return 0;
2687 }
2688
2689 uiBut *uiDefMenuButO(uiBlock *block, char *opname, char *name)
2690 {
2691         int y= ui_menu_y(block) - MENU_ITEM_HEIGHT;
2692         return uiDefIconTextButO(block, BUT, opname, WM_OP_INVOKE_REGION_WIN, ICON_BLANK1, name, 0, y, MENU_WIDTH, MENU_ITEM_HEIGHT-1, NULL);
2693 }
2694
2695 uiBut *uiDefMenuSep(uiBlock *block)
2696 {
2697         int y= ui_menu_y(block) - MENU_SEP_HEIGHT;
2698         return uiDefBut(block, SEPR, 0, "", 0, y, MENU_WIDTH, MENU_SEP_HEIGHT, NULL, 0.0, 0.0, 0, 0, "");
2699 }
2700
2701 /* END Button containing both string label and icon */
2702
2703 void uiSetButLink(uiBut *but, void **poin, void ***ppoin, short *tot, int from, int to)
2704 {
2705         uiLink *link;
2706         
2707         link= but->link= MEM_callocN(sizeof(uiLink), "new uilink");
2708         
2709         link->poin= poin;
2710         link->ppoin= ppoin;
2711         link->totlink= tot;
2712         link->fromcode= from;
2713         link->tocode= to;
2714 }
2715
2716 /* cruft to make uiBlock and uiBut private */
2717
2718 int uiBlocksGetYMin(ListBase *lb)
2719 {
2720         uiBlock *block;
2721         int min= 0;
2722         
2723         for (block= lb->first; block; block= block->next)
2724                 if (block==lb->first || block->miny<min)
2725                         min= block->miny;
2726                         
2727         return min;
2728 }
2729
2730 void uiBlockSetDirection(uiBlock *block, int direction)
2731 {
2732         block->direction= direction;
2733 }
2734
2735 /* this call escapes if there's alignment flags */
2736 void uiBlockFlipOrder(uiBlock *block)
2737 {
2738         ListBase lb;
2739         uiBut *but, *next;
2740         float centy, miny=10000, maxy= -10000;
2741
2742         if(U.uiflag & USER_MENUFIXEDORDER)
2743                 return;
2744         
2745         for(but= block->buttons.first; but; but= but->next) {
2746                 if(but->flag & UI_BUT_ALIGN) return;
2747                 if(but->y1 < miny) miny= but->y1;
2748                 if(but->y2 > maxy) maxy= but->y2;
2749         }
2750         /* mirror trick */
2751         centy= (miny+maxy)/2.0;
2752         for(but= block->buttons.first; but; but= but->next) {
2753                 but->y1 = centy-(but->y1-centy);
2754                 but->y2 = centy-(but->y2-centy);
2755                 SWAP(float, but->y1, but->y2);
2756         }
2757         
2758         /* also flip order in block itself, for example for arrowkey */
2759         lb.first= lb.last= NULL;
2760         but= block->buttons.first;
2761         while(but) {
2762                 next= but->next;
2763                 BLI_remlink(&block->buttons, but);
2764                 BLI_addtail(&lb, but);
2765                 but= next;
2766         }
2767         block->buttons= lb;
2768 }
2769
2770
2771 void uiBlockSetFlag(uiBlock *block, int flag)
2772 {
2773         block->flag|= flag;
2774 }
2775
2776 void uiBlockClearFlag(uiBlock *block, int flag)
2777 {
2778         block->flag&= ~flag;
2779 }
2780
2781 void uiBlockSetXOfs(uiBlock *block, int xofs)
2782 {
2783         block->xofs= xofs;
2784 }
2785
2786 void uiButSetFlag(uiBut *but, int flag)
2787 {
2788         but->flag|= flag;
2789 }
2790
2791 void uiButClearFlag(uiBut *but, int flag)
2792 {
2793         but->flag&= ~flag;
2794 }
2795
2796 int uiButGetRetVal(uiBut *but)
2797 {
2798         return but->retval;
2799 }
2800
2801 PointerRNA *uiButGetOperatorPtrRNA(uiBut *but)
2802 {
2803         if(but->optype && !but->opptr) {
2804                 but->opptr= MEM_callocN(sizeof(PointerRNA), "uiButOpPtr");
2805                 WM_operator_properties_create(but->opptr, but->optype->idname);
2806         }
2807
2808         return but->opptr;
2809 }
2810
2811 void uiBlockSetHandleFunc(uiBlock *block, uiBlockHandleFunc func, void *arg)
2812 {
2813         block->handle_func= func;
2814         block->handle_func_arg= arg;
2815 }
2816
2817 void uiBlockSetButmFunc(uiBlock *block, uiMenuHandleFunc func, void *arg)
2818 {
2819         block->butm_func= func;
2820         block->butm_func_arg= arg;
2821 }
2822
2823 void uiBlockSetFunc(uiBlock *block, uiButHandleFunc func, void *arg1, void *arg2)
2824 {
2825         block->func= func;
2826         block->func_arg1= arg1;
2827         block->func_arg2= arg2;
2828 }
2829
2830 void uiBlockSetRenameFunc(uiBlock *block, uiButHandleRenameFunc func, void *arg1)
2831 {
2832         
2833 }
2834
2835 void uiBlockSetDrawExtraFunc(uiBlock *block, void (*func)(const bContext *C, void *idv, rcti *rect))
2836 {
2837         block->drawextra= func;
2838 }
2839
2840 void uiButSetFunc(uiBut *but, uiButHandleFunc func, void *arg1, void *arg2)
2841 {
2842         but->func= func;
2843         but->func_arg1= arg1;
2844         but->func_arg2= arg2;
2845 }
2846
2847 void uiButSetNFunc(uiBut *but, uiButHandleNFunc funcN, void *argN, void *arg2)
2848 {
2849         but->funcN= funcN;
2850         but->func_argN= argN;
2851         but->func_arg2= arg2;
2852 }
2853
2854 void uiButSetCompleteFunc(uiBut *but, uiButCompleteFunc func, void *arg)
2855 {
2856         but->autocomplete_func= func;
2857         but->autofunc_arg= arg;
2858 }
2859
2860 uiBut *uiDefIDPoinBut(uiBlock *block, uiIDPoinFuncFP func, short blocktype, int retval, char *str, short x1, short y1, short x2, short y2, void *idpp, char *tip)
2861 {
2862         uiBut *but= ui_def_but(block, IDPOIN, retval, str, x1, y1, x2, y2, NULL, 0.0, 0.0, 0.0, 0.0, tip);
2863         but->idpoin_func= func;
2864         but->idpoin_idpp= (ID**) idpp;
2865         ui_check_but(but);
2866         
2867         if(blocktype)
2868                 uiButSetCompleteFunc(but, autocomplete_id, (void *)(intptr_t)blocktype);
2869
2870         return but;
2871 }
2872
2873 uiBut *uiDefBlockBut(uiBlock *block, uiBlockCreateFunc func, void *arg, char *str, short x1, short y1, short x2, short y2, char *tip)
2874 {
2875         uiBut *but= ui_def_but(block, BLOCK, 0, str, x1, y1, x2, y2, arg, 0.0, 0.0, 0.0, 0.0, tip);
2876         but->block_create_func= func;
2877         ui_check_but(but);
2878         return but;
2879 }
2880
2881 uiBut *uiDefBlockButN(uiBlock *block, uiBlockCreateFunc func, void *argN, char *str, short x1, short y1, short x2, short y2, char *tip)
2882 {
2883         uiBut *but= ui_def_but(block, BLOCK, 0, str, x1, y1, x2, y2, NULL, 0.0, 0.0, 0.0, 0.0, tip);
2884         but->block_create_func= func;
2885         but->func_argN= argN;
2886         ui_check_but(but);
2887         return but;
2888 }
2889
2890
2891 uiBut *uiDefPulldownBut(uiBlock *block, uiBlockCreateFunc func, void *arg, char *str, short x1, short y1, short x2, short y2, char *tip)
2892 {
2893         uiBut *but= ui_def_but(block, PULLDOWN, 0, str, x1, y1, x2, y2, arg, 0.0, 0.0, 0.0, 0.0, tip);
2894         but->block_create_func= func;
2895         ui_check_but(but);
2896         return but;
2897 }
2898
2899 uiBut *uiDefMenuBut(uiBlock *block, uiMenuCreateFunc func, void *arg, char *str, short x1, short y1, short x2, short y2, char *tip)
2900 {
2901         uiBut *but= ui_def_but(block, PULLDOWN, 0, str, x1, y1, x2, y2, arg, 0.0, 0.0, 0.0, 0.0, tip);
2902         but->menu_create_func= func;
2903         ui_check_but(but);
2904         return but;
2905 }
2906
2907 uiBut *uiDefIconTextMenuBut(uiBlock *block, uiMenuCreateFunc func, void *arg, int icon, char *str, short x1, short y1, short x2, short y2, char *tip)
2908 {
2909         uiBut *but= ui_def_but(block, PULLDOWN, 0, str, x1, y1, x2, y2, arg, 0.0, 0.0, 0.0, 0.0, tip);
2910
2911         but->icon= (BIFIconID) icon;
2912         but->flag|= UI_HAS_ICON;
2913
2914         but->flag|= UI_ICON_LEFT;
2915         but->flag|= UI_ICON_SUBMENU;
2916
2917         but->menu_create_func= func;
2918         ui_check_but(but);
2919
2920         return but;
2921 }
2922
2923 /* Block button containing both string label and icon */
2924 uiBut *uiDefIconTextBlockBut(uiBlock *block, uiBlockCreateFunc func, void *arg, int icon, char *str, short x1, short y1, short x2, short y2, char *tip)
2925 {
2926         uiBut *but= ui_def_but(block, BLOCK, 0, str, x1, y1, x2, y2, arg, 0.0, 0.0, 0.0, 0.0, tip);
2927         
2928         /* XXX temp, old menu calls pass on icon arrow, which is now UI_ICON_SUBMENU flag */
2929         if(icon!=ICON_RIGHTARROW_THIN) {
2930                 but->icon= (BIFIconID) icon;
2931                 but->flag|= UI_ICON_LEFT;
2932         }
2933         but->flag|= UI_HAS_ICON;
2934         but->flag|= UI_ICON_SUBMENU;
2935
2936         but->block_create_func= func;
2937         ui_check_but(but);
2938         
2939         return but;
2940 }
2941
2942 /* Block button containing icon */
2943 uiBut *uiDefIconBlockBut(uiBlock *block, uiBlockCreateFunc func, void *arg, int retval, int icon, short x1, short y1, short x2, short y2, char *tip)
2944 {
2945         uiBut *but= ui_def_but(block, BLOCK, retval, "", x1, y1, x2, y2, arg, 0.0, 0.0, 0.0, 0.0, tip);
2946         
2947         but->icon= (BIFIconID) icon;
2948         but->flag|= UI_HAS_ICON;
2949         
2950         but->flag|= UI_ICON_LEFT;
2951         but->flag|= UI_ICON_SUBMENU;
2952         
2953         but->block_create_func= func;
2954         ui_check_but(but);
2955         
2956         return but;
2957 }
2958
2959 void uiDefKeyevtButS(uiBlock *block, int retval, char *str, short x1, short y1, short x2, short y2, short *spoin, char *tip)
2960 {
2961         uiBut *but= ui_def_but(block, KEYEVT|SHO, retval, str, x1, y1, x2, y2, spoin, 0.0, 0.0, 0.0, 0.0, tip);
2962         ui_check_but(but);
2963 }
2964
2965 /* arg is pointer to string/name, use uiButSetSearchFunc() below to make this work */
2966 uiBut *uiDefSearchBut(uiBlock *block, void *arg, int retval, int icon, int maxlen, short x1, short y1, short x2, short y2, char *tip)
2967 {
2968         uiBut *but= ui_def_but(block, SEARCH_MENU, retval, "", x1, y1, x2, y2, arg, 0.0, maxlen, 0.0, 0.0, tip);
2969         
2970         but->icon= (BIFIconID) icon;
2971         but->flag|= UI_HAS_ICON;
2972         
2973         but->flag|= UI_ICON_LEFT|UI_TEXT_LEFT;
2974         
2975         ui_check_but(but);
2976         
2977         return but;
2978 }
2979
2980 /* arg is user value, searchfunc and handlefunc both get it as arg */
2981 void uiButSetSearchFunc(uiBut *but, uiButSearchFunc sfunc, void *arg, uiButHandleFunc bfunc)
2982 {
2983         but->search_func= sfunc;
2984         but->search_arg= arg;
2985         
2986         uiButSetFunc(but, bfunc, arg, NULL);
2987 }
2988
2989
2990 /* Program Init/Exit */
2991
2992 void UI_init(void)
2993 {
2994         ui_resources_init();
2995 }
2996
2997 /* after reading userdef file */
2998 void UI_init_userdef(void)
2999 {
3000         /* fix saved themes */
3001         init_userdef_do_versions();
3002         /* set default colors in default theme */
3003         ui_theme_init_userdef();
3004         
3005         uiStyleInit();
3006 }
3007
3008 void UI_exit(void)
3009 {
3010         ui_resources_free();
3011 }
3012