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