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