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