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