* First commit merging 2.4-based sim_physics in to volume25 branch.
[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         if(U.unit_system == USER_UNIT_NONE)
1148                 return 0;
1149
1150         if(but->rnaprop==NULL)
1151                 return 0;
1152
1153         if(RNA_SUBTYPE_UNIT_VALUE(RNA_property_subtype(but->rnaprop))==0)
1154                 return 0;
1155
1156         return 1;
1157 }
1158
1159 double ui_get_but_val(uiBut *but)
1160 {
1161         PropertyRNA *prop;
1162         double value = 0.0;
1163
1164         if(but->editval) { return *(but->editval); }
1165         if(but->poin==NULL && but->rnapoin.data==NULL) return 0.0;
1166
1167         if(but->rnaprop) {
1168                 prop= but->rnaprop;
1169
1170                 switch(RNA_property_type(prop)) {
1171                         case PROP_BOOLEAN:
1172                                 if(RNA_property_array_length(prop))
1173                                         value= RNA_property_boolean_get_index(&but->rnapoin, prop, but->rnaindex);
1174                                 else
1175                                         value= RNA_property_boolean_get(&but->rnapoin, prop);
1176                                 break;
1177                         case PROP_INT:
1178                                 if(RNA_property_array_length(prop))
1179                                         value= RNA_property_int_get_index(&but->rnapoin, prop, but->rnaindex);
1180                                 else
1181                                         value= RNA_property_int_get(&but->rnapoin, prop);
1182                                 break;
1183                         case PROP_FLOAT:
1184                                 if(RNA_property_array_length(prop))
1185                                         value= RNA_property_float_get_index(&but->rnapoin, prop, but->rnaindex);
1186                                 else
1187                                         value= RNA_property_float_get(&but->rnapoin, prop);
1188                                 break;
1189                         case PROP_ENUM:
1190                                 value= RNA_property_enum_get(&but->rnapoin, prop);
1191                                 break;
1192                         default:
1193                                 value= 0.0;
1194                                 break;
1195                 }
1196         }
1197         else if(but->type== HSVSLI) {
1198                 float h, s, v, *fp;
1199                 
1200                 fp= (but->editvec)? but->editvec: (float *)but->poin;
1201                 rgb_to_hsv(fp[0], fp[1], fp[2], &h, &s, &v);
1202
1203                 switch(but->str[0]) {
1204                         case 'H': value= h; break;
1205                         case 'S': value= s; break;
1206                         case 'V': value= v; break;
1207                 }
1208         } 
1209         else if( but->pointype == CHA ) {
1210                 value= *(char *)but->poin;
1211         }
1212         else if( but->pointype == SHO ) {
1213                 value= *(short *)but->poin;
1214         } 
1215         else if( but->pointype == INT ) {
1216                 value= *(int *)but->poin;
1217         } 
1218         else if( but->pointype == FLO ) {
1219                 value= *(float *)but->poin;
1220         }
1221     
1222         return value;
1223 }
1224
1225 void ui_set_but_val(uiBut *but, double value)
1226 {
1227         PropertyRNA *prop;
1228
1229         /* value is a hsv value: convert to rgb */
1230         if(but->rnaprop) {
1231                 prop= but->rnaprop;
1232
1233                 if(RNA_property_editable(&but->rnapoin, prop)) {
1234                         switch(RNA_property_type(prop)) {
1235                                 case PROP_BOOLEAN:
1236                                         if(RNA_property_array_length(prop))
1237                                                 RNA_property_boolean_set_index(&but->rnapoin, prop, but->rnaindex, value);
1238                                         else
1239                                                 RNA_property_boolean_set(&but->rnapoin, prop, value);
1240                                         break;
1241                                 case PROP_INT:
1242                                         if(RNA_property_array_length(prop))
1243                                                 RNA_property_int_set_index(&but->rnapoin, prop, but->rnaindex, value);
1244                                         else
1245                                                 RNA_property_int_set(&but->rnapoin, prop, value);
1246                                         break;
1247                                 case PROP_FLOAT:
1248                                         if(RNA_property_array_length(prop))
1249                                                 RNA_property_float_set_index(&but->rnapoin, prop, but->rnaindex, value);
1250                                         else
1251                                                 RNA_property_float_set(&but->rnapoin, prop, value);
1252                                         break;
1253                                 case PROP_ENUM:
1254                                         RNA_property_enum_set(&but->rnapoin, prop, value);
1255                                         break;
1256                                 default:
1257                                         break;
1258                         }
1259                 }
1260         }
1261         else if(but->pointype==0);
1262         else if(but->type==HSVSLI ) {
1263                 float h, s, v, *fp;
1264                 
1265                 fp= (but->editvec)? but->editvec: (float *)but->poin;
1266                 rgb_to_hsv(fp[0], fp[1], fp[2], &h, &s, &v);
1267                 
1268                 switch(but->str[0]) {
1269                 case 'H': h= value; break;
1270                 case 'S': s= value; break;
1271                 case 'V': v= value; break;
1272                 }
1273                 
1274                 hsv_to_rgb(h, s, v, fp, fp+1, fp+2);
1275                 
1276         }
1277         else {
1278                 /* first do rounding */
1279                 if(but->pointype==CHA)
1280                         value= (char)floor(value+0.5);
1281                 else if(but->pointype==SHO ) {
1282                         /* gcc 3.2.1 seems to have problems 
1283                          * casting a double like 32772.0 to
1284                          * a short so we cast to an int, then 
1285                          to a short */
1286                         int gcckludge;
1287                         gcckludge = (int) floor(value+0.5);
1288                         value= (short)gcckludge;
1289                 }
1290                 else if(but->pointype==INT )
1291                         value= (int)floor(value+0.5);
1292                 else if(but->pointype==FLO ) {
1293                         float fval= (float)value;
1294                         if(fval>= -0.00001f && fval<= 0.00001f) fval= 0.0f;     /* prevent negative zero */
1295                         value= fval;
1296                 }
1297                 
1298                 /* then set value with possible edit override */
1299                 if(but->editval)
1300                         *but->editval= value;
1301                 else if(but->pointype==CHA)
1302                         *((char *)but->poin)= (char)value;
1303                 else if(but->pointype==SHO)
1304                         *((short *)but->poin)= (short)value;
1305                 else if(but->pointype==INT)
1306                         *((int *)but->poin)= (int)value;
1307                 else if(but->pointype==FLO)
1308                         *((float *)but->poin)= (float)value;
1309         }
1310
1311         /* update select flag */
1312         ui_is_but_sel(but);
1313 }
1314
1315 int ui_get_but_string_max_length(uiBut *but)
1316 {
1317         if(but->type == TEX)
1318                 return but->hardmax;
1319         else if(but->type == IDPOIN)
1320                 return sizeof(((ID*)NULL)->name)-2;
1321         else
1322                 return UI_MAX_DRAW_STR;
1323 }
1324
1325 static double ui_get_but_scale_unit(uiBut *but, double value)
1326 {
1327         int subtype= RNA_property_subtype(but->rnaprop);
1328
1329         if(subtype & PROP_UNIT_LENGTH) {
1330                 return value * U.unit_scale_length;
1331         }
1332         else if(subtype & PROP_UNIT_TIME) { /* WARNING - using evil_C :| */
1333                 Scene *scene= CTX_data_scene((bContext *)but->block->evil_C);
1334                 return FRA2TIME(value);
1335         }
1336         else {
1337                 return value;
1338         }
1339 }
1340
1341 static void ui_get_but_string_unit(uiBut *but, char *str, double value, int pad)
1342 {
1343         int do_split= U.unit_flag & USER_UNIT_OPT_SPLIT ? 1:0;
1344         int unit_type=  RNA_SUBTYPE_UNIT_VALUE(RNA_property_subtype(but->rnaprop));
1345         int precission= but->a2;
1346
1347         /* Sanity checks */
1348         if(precission>4)                precission= 4;
1349         else if(precission==0)  precission= 2;
1350
1351         bUnit_AsString(str, ui_get_but_scale_unit(but, value), precission, U.unit_system, unit_type, do_split, pad);
1352 }
1353
1354 static float ui_get_but_step_unit(uiBut *but, double value, float step_default)
1355 {
1356         int unit_type=  RNA_SUBTYPE_UNIT_VALUE(RNA_property_subtype(but->rnaprop));
1357         float step;
1358
1359         step = bUnit_ClosestScalar(ui_get_but_scale_unit(but, value), U.unit_system, unit_type);
1360
1361         if(step > 0.0) { /* -1 is an error value */
1362                 return (step/ui_get_but_scale_unit(but, 1.0))*100;
1363         }
1364         else {
1365                 return step_default;
1366         }
1367 }
1368
1369
1370 void ui_get_but_string(uiBut *but, char *str, int maxlen)
1371 {
1372         if(but->rnaprop && ELEM3(but->type, TEX, IDPOIN, SEARCH_MENU)) {
1373                 PropertyType type;
1374                 char *buf= NULL;
1375
1376                 type= RNA_property_type(but->rnaprop);
1377
1378                 if(type == PROP_STRING) {
1379                         /* RNA string */
1380                         buf= RNA_property_string_get_alloc(&but->rnapoin, but->rnaprop, str, maxlen);
1381                 }
1382                 else if(type == PROP_POINTER) {
1383                         /* RNA pointer */
1384                         PointerRNA ptr= RNA_property_pointer_get(&but->rnapoin, but->rnaprop);
1385                         buf= RNA_struct_name_get_alloc(&ptr, str, maxlen);
1386                 }
1387
1388                 if(!buf) {
1389                         BLI_strncpy(str, "", maxlen);
1390                 }
1391                 else if(buf && buf != str) {
1392                         /* string was too long, we have to truncate */
1393                         BLI_strncpy(str, buf, maxlen);
1394                         MEM_freeN(buf);
1395                 }
1396         }
1397         else if(but->type == IDPOIN) {
1398                 /* ID pointer */
1399                 ID *id= *(but->idpoin_idpp);
1400
1401                 if(id) BLI_strncpy(str, id->name+2, maxlen);
1402                 else BLI_strncpy(str, "", maxlen);
1403
1404                 return;
1405         }
1406         else if(but->type == TEX) {
1407                 /* string */
1408                 BLI_strncpy(str, but->poin, maxlen);
1409                 return;
1410         }
1411         else if(but->type == SEARCH_MENU) {
1412                 /* string */
1413                 BLI_strncpy(str, but->poin, maxlen);
1414                 return;
1415         }
1416         else if(ui_but_anim_expression_get(but, str, maxlen))
1417                 ; /* driver expression */
1418         else {
1419                 /* number editing */
1420                 double value;
1421
1422                 value= ui_get_but_val(but);
1423
1424                 if(ui_is_but_float(but)) {
1425                         if(ui_is_but_unit(but)) {
1426                                 ui_get_but_string_unit(but, str, value, 0);
1427                         }
1428                         else if(but->a2) { /* amount of digits defined */
1429                                 if(but->a2==1) BLI_snprintf(str, maxlen, "%.1f", value);
1430                                 else if(but->a2==2) BLI_snprintf(str, maxlen, "%.2f", value);
1431                                 else if(but->a2==3) BLI_snprintf(str, maxlen, "%.3f", value);
1432                                 else BLI_snprintf(str, maxlen, "%.4f", value);
1433                         }
1434                         else
1435                                 BLI_snprintf(str, maxlen, "%.3f", value);
1436                 }
1437                 else
1438                         BLI_snprintf(str, maxlen, "%d", (int)value);
1439         }
1440 }
1441
1442 int ui_set_but_string(bContext *C, uiBut *but, const char *str)
1443 {
1444         if(but->rnaprop && ELEM3(but->type, TEX, IDPOIN, SEARCH_MENU)) {
1445                 if(RNA_property_editable(&but->rnapoin, but->rnaprop)) {
1446                         PropertyType type;
1447
1448                         type= RNA_property_type(but->rnaprop);
1449
1450                         if(type == PROP_STRING) {
1451                                 /* RNA string */
1452                                 RNA_property_string_set(&but->rnapoin, but->rnaprop, str);
1453                                 return 1;
1454                         }
1455                         else if(type == PROP_POINTER) {
1456                                 /* RNA pointer */
1457                                 PointerRNA ptr, rptr;
1458                                 PropertyRNA *prop;
1459
1460                                 if(str == NULL || str[0] == '\0') {
1461                                         RNA_property_pointer_set(&but->rnapoin, but->rnaprop, PointerRNA_NULL);
1462                                         return 1;
1463                                 }
1464                                 else {
1465                                         ptr= but->rnasearchpoin;
1466                                         prop= but->rnasearchprop;
1467                                         
1468                                         if(prop && RNA_property_collection_lookup_string(&ptr, prop, str, &rptr))
1469                                                 RNA_property_pointer_set(&but->rnapoin, but->rnaprop, rptr);
1470
1471                                         return 1;
1472                                 }
1473
1474                                 return 0;
1475                         }
1476                 }
1477         }
1478         else if(but->type == IDPOIN) {
1479                 /* ID pointer */
1480         but->idpoin_func(C, (char*)str, but->idpoin_idpp);
1481                 return 1;
1482         }
1483         else if(but->type == TEX) {
1484                 /* string */
1485                 BLI_strncpy(but->poin, str, but->hardmax);
1486                 return 1;
1487         }
1488         else if(but->type == SEARCH_MENU) {
1489                 /* string */
1490                 BLI_strncpy(but->poin, str, but->hardmax);
1491                 return 1;
1492         }
1493         else if(ui_but_anim_expression_set(but, str)) {
1494                 /* driver expression */
1495                 return 1;
1496         }
1497         else {
1498                 /* number editing */
1499                 double value;
1500
1501 #ifndef DISABLE_PYTHON
1502                 {
1503                         char str_unit_convert[256];
1504                         int unit_type=  RNA_SUBTYPE_UNIT_VALUE(RNA_property_subtype(but->rnaprop));
1505
1506                         if(U.unit_system != USER_UNIT_NONE && unit_type) {
1507                                 /* ugly, use the draw string to get the value, this could cause problems if it includes some text which resolves to a unit */
1508                                 bUnit_ReplaceString(str_unit_convert, str, but->drawstr, ui_get_but_scale_unit(but, 1.0), U.unit_system, unit_type);
1509                         }
1510                         else {
1511                                 strcpy(str_unit_convert, str);
1512                         }
1513
1514                         if(BPY_button_eval(C, str_unit_convert, &value)) {
1515                                 value = ui_get_but_val(but); /* use its original value */
1516
1517                                 if(str[0])
1518                                         return 0;
1519                         }
1520                 }
1521 #else
1522                 value= atof(str);
1523 #endif
1524
1525                 if(!ui_is_but_float(but)) value= (int)value;
1526                 if(but->type==NUMABS) value= fabs(value);
1527
1528                 /* not that we use hard limits here */
1529                 if(value<but->hardmin) value= but->hardmin;
1530                 if(value>but->hardmax) value= but->hardmax;
1531
1532                 ui_set_but_val(but, value);
1533                 return 1;
1534         }
1535
1536         return 0;
1537 }
1538
1539 static double soft_range_round_up(double value, double max)
1540 {
1541         /* round up to .., 0.1, 0.2, 0.5, 1, 2, 5, 10, 20, 50, .. */
1542         double newmax= pow(10.0, ceil(log(value)/log(10.0)));
1543
1544         if(newmax*0.2 >= max && newmax*0.2 >= value)
1545                 return newmax*0.2;
1546         else if(newmax*0.5 >= max && newmax*0.5 >= value)
1547                 return newmax*0.5;
1548         else
1549                 return newmax;
1550 }
1551
1552 static double soft_range_round_down(double value, double max)
1553 {
1554         /* round down to .., 0.1, 0.2, 0.5, 1, 2, 5, 10, 20, 50, .. */
1555         double newmax= pow(10.0, floor(log(value)/log(10.0)));
1556
1557         if(newmax*5.0 <= max && newmax*5.0 <= value)
1558                 return newmax*5.0;
1559         else if(newmax*2.0 <= max && newmax*2.0 <= value)
1560                 return newmax*2.0;
1561         else
1562                 return newmax;
1563 }
1564
1565 void ui_set_but_soft_range(uiBut *but, double value)
1566 {
1567         PropertyType type;
1568         double softmin, softmax, step, precision;
1569         
1570         if(but->rnaprop) {
1571                 type= RNA_property_type(but->rnaprop);
1572
1573                 if(type == PROP_INT) {
1574                         int imin, imax, istep;
1575
1576                         RNA_property_int_ui_range(&but->rnapoin, but->rnaprop, &imin, &imax, &istep);
1577                         softmin= imin;
1578                         softmax= imax;
1579                         step= istep;
1580                         precision= 1;
1581                 }
1582                 else if(type == PROP_FLOAT) {
1583                         float fmin, fmax, fstep, fprecision;
1584
1585                         RNA_property_float_ui_range(&but->rnapoin, but->rnaprop, &fmin, &fmax, &fstep, &fprecision);
1586                         softmin= fmin;
1587                         softmax= fmax;
1588                         step= fstep;
1589                         precision= fprecision;
1590                 }
1591                 else
1592                         return;
1593
1594                 /* clamp button range to something reasonable in case
1595                  * we get -inf/inf from RNA properties */
1596                 softmin= MAX2(softmin, -1e4);
1597                 softmax= MIN2(softmax, 1e4);
1598
1599                 /* if the value goes out of the soft/max range, adapt the range */
1600                 if(value+1e-10 < softmin) {
1601                         if(value < 0.0)
1602                                 softmin= -soft_range_round_up(-value, -softmin);
1603                         else
1604                                 softmin= soft_range_round_down(value, softmin);
1605
1606                         if(softmin < but->hardmin)
1607                                 softmin= but->hardmin;
1608                 }
1609                 else if(value-1e-10 > softmax) {
1610                         if(value < 0.0)
1611                                 softmax= -soft_range_round_down(-value, -softmax);
1612                         else
1613                                 softmax= soft_range_round_up(value, softmax);
1614
1615                         if(softmax > but->hardmax)
1616                                 softmax= but->hardmax;
1617                 }
1618
1619                 but->softmin= softmin;
1620                 but->softmax= softmax;
1621         }
1622 }
1623
1624 /* ******************* Free ********************/
1625
1626 static void ui_free_link(uiLink *link)
1627 {
1628         if(link) {      
1629                 BLI_freelistN(&link->lines);
1630                 MEM_freeN(link);
1631         }
1632 }
1633
1634 /* can be called with C==NULL */
1635 static void ui_free_but(const bContext *C, uiBut *but)
1636 {
1637         if(but->opptr) {
1638                 WM_operator_properties_free(but->opptr);
1639                 MEM_freeN(but->opptr);
1640         }
1641         if(but->func_argN) MEM_freeN(but->func_argN);
1642         if(but->active) {
1643                 /* XXX solve later, buttons should be free-able without context? */
1644                 if(C) 
1645                         ui_button_active_cancel(C, but);
1646                 else
1647                         if(but->active) 
1648                                 MEM_freeN(but->active);
1649         }
1650         if(but->str && but->str != but->strdata) MEM_freeN(but->str);
1651         ui_free_link(but->link);
1652
1653         MEM_freeN(but);
1654 }
1655
1656 /* can be called with C==NULL */
1657 void uiFreeBlock(const bContext *C, uiBlock *block)
1658 {
1659         uiBut *but;
1660
1661         while( (but= block->buttons.first) ) {
1662                 BLI_remlink(&block->buttons, but);      
1663                 ui_free_but(C, but);
1664         }
1665
1666         CTX_store_free_list(&block->contexts);
1667
1668         BLI_freelistN(&block->saferct);
1669         
1670         MEM_freeN(block);
1671 }
1672
1673 /* can be called with C==NULL */
1674 void uiFreeBlocks(const bContext *C, ListBase *lb)
1675 {
1676         uiBlock *block;
1677         
1678         while( (block= lb->first) ) {
1679                 BLI_remlink(lb, block);
1680                 uiFreeBlock(C, block);
1681         }
1682 }
1683
1684 void uiFreeInactiveBlocks(const bContext *C, ListBase *lb)
1685 {
1686         uiBlock *block, *nextblock;
1687
1688         for(block=lb->first; block; block=nextblock) {
1689                 nextblock= block->next;
1690         
1691                 if(!block->handle) {
1692                         if(!block->active) {
1693                                 BLI_remlink(lb, block);
1694                                 uiFreeBlock(C, block);
1695                         }
1696                         else
1697                                 block->active= 0;
1698                 }
1699         }
1700 }
1701
1702 void uiBlockSetRegion(uiBlock *block, ARegion *region)
1703 {
1704         ListBase *lb;
1705         uiBlock *oldblock= NULL;
1706
1707         lb= &region->uiblocks;
1708         
1709         /* each listbase only has one block with this name, free block
1710          * if is already there so it can be rebuilt from scratch */
1711         if(lb) {
1712                 for (oldblock= lb->first; oldblock; oldblock= oldblock->next)
1713                         if (BLI_streq(oldblock->name, block->name))
1714                                 break;
1715
1716                 if (oldblock) {
1717                         oldblock->active= 0;
1718                         oldblock->panel= NULL;
1719                 }
1720         }
1721
1722         block->oldblock= oldblock;
1723
1724         /* at the beginning of the list! for dynamical menus/blocks */
1725         if(lb)
1726                 BLI_addhead(lb, block);
1727 }
1728
1729 uiBlock *uiBeginBlock(const bContext *C, ARegion *region, const char *name, short dt)
1730 {
1731         uiBlock *block;
1732         wmWindow *window;
1733         int getsizex, getsizey;
1734
1735         window= CTX_wm_window(C);
1736
1737         block= MEM_callocN(sizeof(uiBlock), "uiBlock");
1738         block->active= 1;
1739         block->dt= dt;
1740         block->evil_C= (void*)C; // XXX
1741         BLI_strncpy(block->name, name, sizeof(block->name));
1742
1743         if(region)
1744                 uiBlockSetRegion(block, region);
1745
1746         /* window matrix and aspect */
1747         if(region && region->swinid) {
1748                 wm_subwindow_getmatrix(window, region->swinid, block->winmat);
1749                 wm_subwindow_getsize(window, region->swinid, &getsizex, &getsizey);
1750
1751                 /* TODO - investigate why block->winmat[0][0] is negative
1752                  * in the image view when viewRedrawForce is called */
1753                 block->aspect= 2.0/fabs( (getsizex)*block->winmat[0][0]);
1754         }
1755         else {
1756                 /* no subwindow created yet, for menus for example, so we
1757                  * use the main window instead, since buttons are created
1758                  * there anyway */
1759                 wm_subwindow_getmatrix(window, window->screen->mainwin, block->winmat);
1760                 wm_subwindow_getsize(window, window->screen->mainwin, &getsizex, &getsizey);
1761
1762                 block->aspect= 2.0/fabs(getsizex*block->winmat[0][0]);
1763                 block->auto_open= 2;
1764                 block->flag |= UI_BLOCK_LOOP; /* tag as menu */
1765         }
1766
1767         return block;
1768 }
1769
1770 uiBlock *uiGetBlock(char *name, ARegion *ar)
1771 {
1772         uiBlock *block= ar->uiblocks.first;
1773         
1774         while(block) {
1775                 if( strcmp(name, block->name)==0 ) return block;
1776                 block= block->next;
1777         }
1778         
1779         return NULL;
1780 }
1781
1782 void uiBlockSetEmboss(uiBlock *block, short dt)
1783 {
1784         block->dt= dt;
1785 }
1786
1787 void ui_check_but(uiBut *but)
1788 {
1789         /* if something changed in the button */
1790         double value;
1791         float okwidth;
1792 //      int transopts= ui_translate_buttons();
1793         
1794         ui_is_but_sel(but);
1795         
1796 //      if(but->type==TEX || but->type==IDPOIN) transopts= 0;
1797
1798         /* test for min and max, icon sliders, etc */
1799         switch( but->type ) {
1800                 case NUM:
1801                 case SLI:
1802                 case SCROLL:
1803                 case NUMSLI:
1804                 case HSVSLI:
1805                         value= ui_get_but_val(but);
1806                         if(value < but->hardmin) ui_set_but_val(but, but->hardmin);
1807                         else if(value > but->hardmax) ui_set_but_val(but, but->hardmax);
1808                         break;
1809                         
1810                 case NUMABS:
1811                         value= fabs( ui_get_but_val(but) );
1812                         if(value < but->hardmin) ui_set_but_val(but, but->hardmin);
1813                         else if(value > but->hardmax) ui_set_but_val(but, but->hardmax);
1814                         break;
1815                         
1816                 case ICONTOG: 
1817                 case ICONTOGN:
1818                         if(!but->rnaprop || (RNA_property_flag(but->rnaprop) & PROP_ICONS_CONSECUTIVE)) {
1819                                 if(but->flag & UI_SELECT) but->iconadd= 1;
1820                                 else but->iconadd= 0;
1821                         }
1822                         break;
1823                         
1824                 case ICONROW:
1825                         if(!but->rnaprop || (RNA_property_flag(but->rnaprop) & PROP_ICONS_CONSECUTIVE)) {
1826                                 value= ui_get_but_val(but);
1827                                 but->iconadd= (int)value- (int)(but->hardmin);
1828                         }
1829                         break;
1830                         
1831                 case ICONTEXTROW:
1832                         if(!but->rnaprop || (RNA_property_flag(but->rnaprop) & PROP_ICONS_CONSECUTIVE)) {
1833                                 value= ui_get_but_val(but);
1834                                 but->iconadd= (int)value- (int)(but->hardmin);
1835                         }
1836                         break;
1837         }
1838         
1839         
1840         /* safety is 4 to enable small number buttons (like 'users') */
1841         okwidth= -4 + (but->x2 - but->x1); 
1842         
1843         /* name: */
1844         switch( but->type ) {
1845         
1846         case MENU:
1847         case ICONTEXTROW:
1848                 
1849                 if(but->x2 - but->x1 > 24) {
1850                         value= ui_get_but_val(but);
1851                         ui_set_name_menu(but, (int)value);
1852                 }
1853                 break;
1854         
1855         case NUM:
1856         case NUMSLI:
1857         case HSVSLI:
1858         case NUMABS:
1859
1860                 value= ui_get_but_val(but);
1861
1862                 if(ui_is_but_float(but)) {
1863                         if(value == FLT_MAX) sprintf(but->drawstr, "%sinf", but->str);
1864                         else if(value == -FLT_MAX) sprintf(but->drawstr, "%s-inf", but->str);
1865                         /* support length type buttons */
1866                         else if(ui_is_but_unit(but)) {
1867                                 char new_str[256];
1868
1869                                 if(U.unit_scale_length<0.0001) U.unit_scale_length= 1.0; // XXX do_versions
1870
1871                                 ui_get_but_string_unit(but, new_str, value, TRUE);
1872                                 sprintf(but->drawstr, "%s%s", but->str, new_str);
1873                         }
1874                         else if(but->a2) { /* amount of digits defined */
1875                                 if(but->a2==1) sprintf(but->drawstr, "%s%.1f", but->str, value);
1876                                 else if(but->a2==2) sprintf(but->drawstr, "%s%.2f", but->str, value);
1877                                 else if(but->a2==3) sprintf(but->drawstr, "%s%.3f", but->str, value);
1878                                 else sprintf(but->drawstr, "%s%.4f", but->str, value);
1879                         }
1880                         else {
1881                                 if(but->hardmax<10.001) sprintf(but->drawstr, "%s%.3f", but->str, value);
1882                                 else sprintf(but->drawstr, "%s%.2f", but->str, value);
1883                         }
1884                 }
1885                 else {
1886                         sprintf(but->drawstr, "%s%d", but->str, (int)value);
1887                 }
1888                         
1889                 if(but->rnaprop) {
1890                         PropertySubType pstype = RNA_property_subtype(but->rnaprop);
1891                         
1892                         if (pstype == PROP_PERCENTAGE)
1893                                 strcat(but->drawstr, "%");
1894                 }
1895                 break;
1896
1897         case LABEL:
1898                 if(ui_is_but_float(but)) {
1899                         value= ui_get_but_val(but);
1900                         if(but->a2) { /* amount of digits defined */
1901                                 if(but->a2==1) sprintf(but->drawstr, "%s%.1f", but->str, value);
1902                                 else if(but->a2==2) sprintf(but->drawstr, "%s%.2f", but->str, value);
1903                                 else if(but->a2==3) sprintf(but->drawstr, "%s%.3f", but->str, value);
1904                                 else sprintf(but->drawstr, "%s%.4f", but->str, value);
1905                         }
1906                         else {
1907                                 sprintf(but->drawstr, "%s%.2f", but->str, value);
1908                         }
1909                 }
1910                 else strcpy(but->drawstr, but->str);
1911                 
1912                 break;
1913
1914         case IDPOIN:
1915         case TEX:
1916         case SEARCH_MENU:
1917                 if(!but->editstr) {
1918                         char str[UI_MAX_DRAW_STR];
1919
1920                         ui_get_but_string(but, str, UI_MAX_DRAW_STR-strlen(but->str));
1921
1922                         strcpy(but->drawstr, but->str);
1923                         strcat(but->drawstr, str);
1924                 }
1925                 break;
1926         
1927         case KEYEVT:
1928                 strcpy(but->drawstr, but->str);
1929                 if (but->flag & UI_SELECT) {
1930                         strcat(but->drawstr, "Press a key");
1931                 } else {
1932                         strcat(but->drawstr, WM_key_event_string((short) ui_get_but_val(but)));
1933                 }
1934                 break;
1935                 
1936         case HOTKEYEVT:
1937                 if (but->flag & UI_SELECT) {
1938                         short *sp= (short *)but->func_arg3;
1939                         
1940                         strcpy(but->drawstr, but->str);
1941                         
1942                         if(*sp) {
1943                                 char *str= but->drawstr;
1944                                 
1945                                 if(*sp & KM_SHIFT)
1946                                         str= strcat(str, "Shift ");
1947                                 if(*sp & KM_CTRL)
1948                                         str= strcat(str, "Ctrl ");
1949                                 if(*sp & KM_ALT)
1950                                         str= strcat(str, "Alt ");
1951                                 if(*sp & KM_OSKEY)
1952                                         str= strcat(str, "Cmd ");
1953                         }
1954                         else
1955                                 strcat(but->drawstr, "Press a key  ");
1956                 } else {
1957                         /* XXX todo, button currently only used temporarily */
1958                         strcpy(but->drawstr, WM_key_event_string((short) ui_get_but_val(but)));
1959                 }
1960                 break;
1961                 
1962         case BUT_TOGDUAL:
1963                 /* trying to get the dual-icon to left of text... not very nice */
1964                 if(but->str[0]) {
1965                         strcpy(but->drawstr, "  ");
1966                         strcpy(but->drawstr+2, but->str);
1967                 }
1968                 break;
1969         default:
1970                 strcpy(but->drawstr, but->str);
1971                 
1972         }
1973
1974         /* if we are doing text editing, this will override the drawstr */
1975         if(but->editstr)
1976                 strcpy(but->drawstr, but->editstr);
1977         
1978         /* text clipping moved to widget drawing code itself */
1979 }
1980
1981
1982 void uiBlockBeginAlign(uiBlock *block)
1983 {
1984         /* if other align was active, end it */
1985         if(block->flag & UI_BUT_ALIGN) uiBlockEndAlign(block);
1986
1987         block->flag |= UI_BUT_ALIGN_DOWN;       
1988         block->alignnr++;
1989
1990         /* buttons declared after this call will get this align nr */ // XXX flag?
1991 }
1992
1993 static int buts_are_horiz(uiBut *but1, uiBut *but2)
1994 {
1995         float dx, dy;
1996         
1997         dx= fabs( but1->x2 - but2->x1);
1998         dy= fabs( but1->y1 - but2->y2);
1999         
2000         if(dx > dy) return 0;
2001         return 1;
2002 }
2003
2004 void uiBlockEndAlign(uiBlock *block)
2005 {
2006         block->flag &= ~UI_BUT_ALIGN;   // all 4 flags
2007 }
2008
2009 int ui_but_can_align(uiBut *but)
2010 {
2011         return !ELEM3(but->type, LABEL, OPTION, OPTIONN);
2012 }
2013
2014 static void ui_block_do_align_but(uiBlock *block, uiBut *first, int nr)
2015 {
2016         uiBut *prev, *but=NULL, *next;
2017         int flag= 0, cols=0, rows=0;
2018         
2019         /* auto align */
2020
2021         for(but=first; but && but->alignnr == nr; but=but->next) {
2022                 if(but->next && but->next->alignnr == nr) {
2023                         if(buts_are_horiz(but, but->next)) cols++;
2024                         else rows++;
2025                 }
2026         }
2027
2028         /* rows==0: 1 row, cols==0: 1 collumn */
2029         
2030         /* note;  how it uses 'flag' in loop below (either set it, or OR it) is confusing */
2031         for(but=first, prev=NULL; but && but->alignnr == nr; prev=but, but=but->next) {
2032                 next= but->next;
2033                 if(next && next->alignnr != nr)
2034                         next= NULL;
2035
2036                 /* clear old flag */
2037                 but->flag &= ~UI_BUT_ALIGN;
2038                 
2039                 if(flag==0) {   /* first case */
2040                         if(next) {
2041                                 if(buts_are_horiz(but, next)) {
2042                                         if(rows==0)
2043                                                 flag= UI_BUT_ALIGN_RIGHT;
2044                                         else 
2045                                                 flag= UI_BUT_ALIGN_DOWN|UI_BUT_ALIGN_RIGHT;
2046                                 }
2047                                 else {
2048                                         flag= UI_BUT_ALIGN_DOWN;
2049                                 }
2050                         }
2051                 }
2052                 else if(next==NULL) {   /* last case */
2053                         if(prev) {
2054                                 if(buts_are_horiz(prev, but)) {
2055                                         if(rows==0) 
2056                                                 flag= UI_BUT_ALIGN_LEFT;
2057                                         else
2058                                                 flag= UI_BUT_ALIGN_TOP|UI_BUT_ALIGN_LEFT;
2059                                 }
2060                                 else flag= UI_BUT_ALIGN_TOP;
2061                         }
2062                 }
2063                 else if(buts_are_horiz(but, next)) {
2064                         /* check if this is already second row */
2065                         if( prev && buts_are_horiz(prev, but)==0) {
2066                                 flag &= ~UI_BUT_ALIGN_LEFT;
2067                                 flag |= UI_BUT_ALIGN_TOP;
2068                                 /* exception case: bottom row */
2069                                 if(rows>0) {
2070                                         uiBut *bt= but;
2071                                         while(bt && bt->alignnr == nr) {
2072                                                 if(bt->next && bt->next->alignnr == nr && buts_are_horiz(bt, bt->next)==0 ) break; 
2073                                                 bt= bt->next;
2074                                         }
2075                                         if(bt==0 || bt->alignnr != nr) flag= UI_BUT_ALIGN_TOP|UI_BUT_ALIGN_RIGHT;
2076                                 }
2077                         }
2078                         else flag |= UI_BUT_ALIGN_LEFT;
2079                 }
2080                 else {
2081                         if(cols==0) {
2082                                 flag |= UI_BUT_ALIGN_TOP;
2083                         }
2084                         else {  /* next button switches to new row */
2085                                 
2086                                 if(prev && buts_are_horiz(prev, but))
2087                                    flag |= UI_BUT_ALIGN_LEFT;
2088                                 
2089                                 if( (flag & UI_BUT_ALIGN_TOP)==0) {     /* stil top row */
2090                                         if(prev)
2091                                                 flag= UI_BUT_ALIGN_DOWN|UI_BUT_ALIGN_LEFT;
2092                                         else 
2093                                                 flag |= UI_BUT_ALIGN_DOWN;
2094                                 }
2095                                 else 
2096                                         flag |= UI_BUT_ALIGN_TOP;
2097                         }
2098                 }
2099                 
2100                 but->flag |= flag;
2101                 
2102                 /* merge coordinates */
2103                 if(prev) {
2104                         // simple cases 
2105                         if(rows==0) {
2106                                 but->x1= (prev->x2+but->x1)/2.0;
2107                                 prev->x2= but->x1;
2108                         }
2109                         else if(cols==0) {
2110                                 but->y2= (prev->y1+but->y2)/2.0;
2111                                 prev->y1= but->y2;
2112                         }
2113                         else {
2114                                 if(buts_are_horiz(prev, but)) {
2115                                         but->x1= (prev->x2+but->x1)/2.0;
2116                                         prev->x2= but->x1;
2117                                         /* copy height too */
2118                                         but->y2= prev->y2;
2119                                 }
2120                                 else if(prev->prev && buts_are_horiz(prev->prev, prev)==0) {
2121                                         /* the previous button is a single one in its row */
2122                                         but->y2= (prev->y1+but->y2)/2.0;
2123                                         prev->y1= but->y2;
2124                                 }
2125                                 else {
2126                                         /* the previous button is not a single one in its row */
2127                                         but->y2= prev->y1;
2128                                 }
2129                         }
2130                 }
2131         }
2132 }
2133
2134 void ui_block_do_align(uiBlock *block)
2135 {
2136         uiBut *but;
2137         int nr;
2138
2139         /* align buttons with same align nr */
2140         for(but=block->buttons.first; but;) {
2141                 if(but->alignnr) {
2142                         nr= but->alignnr;
2143                         ui_block_do_align_but(block, but, nr);
2144
2145                         /* skip with same number */
2146                         for(; but && but->alignnr == nr; but=but->next);
2147
2148                         if(!but)
2149                                 break;
2150                 }
2151                 else
2152                         but= but->next;
2153         }
2154 }
2155
2156 /*
2157 ui_def_but is the function that draws many button types
2158
2159 for float buttons:
2160         "a1" Click Step (how much to change the value each click)
2161         "a2" Number of decimal point values to display. 0 defaults to 3 (0.000) 1,2,3, and a maximum of 4,
2162        all greater values will be clamped to 4.
2163
2164 */
2165 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)
2166 {
2167         uiBut *but;
2168         short slen;
2169         
2170         if(type & BUTPOIN) {            /* a pointer is required */
2171                 if(poin==NULL)
2172                         return NULL;
2173         }
2174
2175         but= MEM_callocN(sizeof(uiBut), "uiBut");
2176
2177         but->type= type & BUTTYPE;
2178         but->pointype= type & BUTPOIN;
2179         but->bit= type & BIT;
2180         but->bitnr= type & 31;
2181         but->icon = 0;
2182
2183         but->retval= retval;
2184         if( strlen(str)>=UI_MAX_NAME_STR-1 ) {
2185                 but->str= MEM_callocN( strlen(str)+2, "uiDefBut");
2186                 strcpy(but->str, str);
2187         }
2188         else {
2189                 but->str= but->strdata;
2190                 strcpy(but->str, str);
2191         }
2192         but->x1= x1; 
2193         but->y1= y1;
2194         but->x2= (x1+x2); 
2195         but->y2= (y1+y2);
2196         
2197         but->poin= poin;
2198         but->hardmin= but->softmin= min; 
2199         but->hardmax= but->softmax= max;
2200         but->a1= a1; 
2201         but->a2= a2;
2202         but->tip= tip;
2203         
2204         but->lock= block->lock;
2205         but->lockstr= block->lockstr;
2206         but->dt= block->dt;
2207
2208         but->aspect= 1.0f; //XXX block->aspect;
2209         but->block= block;              // pointer back, used for frontbuffer status, and picker
2210
2211         if((block->flag & UI_BUT_ALIGN) && ui_but_can_align(but))
2212                 but->alignnr= block->alignnr;
2213
2214         but->func= block->func;
2215         but->func_arg1= block->func_arg1;
2216         but->func_arg2= block->func_arg2;
2217         
2218         but->pos= -1;   /* cursor invisible */
2219
2220         if(ELEM(but->type, NUM, NUMABS)) {      /* add a space to name */
2221                 slen= strlen(but->str);
2222                 if(slen>0 && slen<UI_MAX_NAME_STR-2) {
2223                         if(but->str[slen-1]!=' ') {
2224                                 but->str[slen]= ' ';
2225                                 but->str[slen+1]= 0;
2226                         }
2227                 }
2228         }
2229         
2230         if(ELEM(but->type, HSVCUBE, HSVCIRCLE)) { /* hsv buttons temp storage */
2231                 float rgb[3];
2232                 ui_get_but_vectorf(but, rgb);
2233                 rgb_to_hsv(rgb[0], rgb[1], rgb[2], but->hsv, but->hsv+1, but->hsv+2);
2234         }
2235
2236         if((block->flag & UI_BLOCK_LOOP) || ELEM7(but->type, MENU, TEX, LABEL, IDPOIN, BLOCK, BUTM, SEARCH_MENU))
2237                 but->flag |= (UI_TEXT_LEFT|UI_ICON_LEFT);
2238         else if(but->type==BUT_TOGDUAL)
2239                 but->flag |= UI_ICON_LEFT;
2240
2241         but->flag |= (block->flag & UI_BUT_ALIGN);
2242
2243         if (but->lock) {
2244                 if (but->lockstr) {
2245                         but->flag |= UI_BUT_DISABLED;
2246                 }
2247         }
2248
2249         BLI_addtail(&block->buttons, but);
2250         
2251         if(block->curlayout)
2252                 ui_layout_add_but(block->curlayout, but);
2253
2254         return but;
2255 }
2256
2257 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)
2258 {
2259         uiBut *but;
2260         PropertyRNA *prop;
2261         PropertyType proptype;
2262         int freestr= 0, icon= 0;
2263
2264         prop= RNA_struct_find_property(ptr, propname);
2265
2266         if(prop) {
2267                 proptype= RNA_property_type(prop);
2268
2269                 /* use rna values if parameters are not specified */
2270                 if(!str) {
2271                         if(type == MENU && proptype == PROP_ENUM) {
2272                                 EnumPropertyItem *item;
2273                                 DynStr *dynstr;
2274                                 int i, totitem, value, free;
2275
2276                                 RNA_property_enum_items(block->evil_C, ptr, prop, &item, &totitem, &free);
2277                                 value= RNA_property_enum_get(ptr, prop);
2278
2279                                 dynstr= BLI_dynstr_new();
2280                                 BLI_dynstr_appendf(dynstr, "%s%%t", RNA_property_ui_name(prop));
2281                                 for(i=0; i<totitem; i++) {
2282                                         if(!item[i].identifier[0])
2283                                                 BLI_dynstr_append(dynstr, "|%l");
2284                                         else if(item[i].icon)
2285                                                 BLI_dynstr_appendf(dynstr, "|%s %%i%d %%x%d", item[i].name, item[i].icon, item[i].value);
2286                                         else
2287                                                 BLI_dynstr_appendf(dynstr, "|%s %%x%d", item[i].name, item[i].value);
2288
2289                                         if(value == item[i].value)
2290                                                 icon= item[i].icon;
2291                                 }
2292                                 str= BLI_dynstr_get_cstring(dynstr);
2293                                 BLI_dynstr_free(dynstr);
2294
2295                                 if(free)
2296                                         MEM_freeN(item);
2297
2298                                 freestr= 1;
2299                         }
2300                         else if(ELEM(type, ROW, LISTROW) && proptype == PROP_ENUM) {
2301                                 EnumPropertyItem *item;
2302                                 int i, totitem, free;
2303
2304                                 RNA_property_enum_items(block->evil_C, ptr, prop, &item, &totitem, &free);
2305                                 for(i=0; i<totitem; i++) {
2306                                         if(item[i].identifier[0] && item[i].value == (int)max) {
2307                                                 str= (char*)item[i].name;
2308                                                 icon= item[i].icon;
2309                                         }
2310                                 }
2311
2312                                 if(!str)
2313                                         str= (char*)RNA_property_ui_name(prop);
2314                                 if(free)
2315                                         MEM_freeN(item);
2316                         }
2317                         else {
2318                                 str= (char*)RNA_property_ui_name(prop);
2319                                 icon= RNA_property_ui_icon(prop);
2320                         }
2321                 }
2322
2323                 if(!tip) {
2324                         if(type == ROW && proptype == PROP_ENUM) {
2325                                 EnumPropertyItem *item;
2326                                 int i, totitem, free;
2327
2328                                 RNA_property_enum_items(block->evil_C, ptr, prop, &item, &totitem, &free);
2329
2330                                 for(i=0; i<totitem; i++) {
2331                                         if(item[i].identifier[0] && item[i].value == (int)max) {
2332                                                 if(item[i].description[0])
2333                                                         tip= (char*)item[i].description;
2334                                                 break;
2335                                         }
2336                                 }
2337
2338                                 if(free)
2339                                         MEM_freeN(item);
2340                         }
2341                 }
2342                 
2343                 if(!tip)
2344                         tip= (char*)RNA_property_ui_description(prop);
2345
2346                 if(min == max || a1 == -1 || a2 == -1) {
2347                         if(proptype == PROP_INT) {
2348                                 int hardmin, hardmax, softmin, softmax, step;
2349
2350                                 RNA_property_int_range(ptr, prop, &hardmin, &hardmax);
2351                                 RNA_property_int_ui_range(ptr, prop, &softmin, &softmax, &step);
2352
2353                                 if(!ELEM(type, ROW, LISTROW) && min == max) {
2354                                         min= hardmin;
2355                                         max= hardmax;
2356                                 }
2357                                 if(a1 == -1)
2358                                         a1= step;
2359                                 if(a2 == -1)
2360                                         a2= 0;
2361                         }
2362                         else if(proptype == PROP_FLOAT) {
2363                                 float hardmin, hardmax, softmin, softmax, step, precision;
2364
2365                                 RNA_property_float_range(ptr, prop, &hardmin, &hardmax);
2366                                 RNA_property_float_ui_range(ptr, prop, &softmin, &softmax, &step, &precision);
2367
2368                                 if(!ELEM(type, ROW, LISTROW) && min == max) {
2369                                         min= hardmin;
2370                                         max= hardmax;
2371                                 }
2372                                 if(a1 == -1)
2373                                         a1= step;
2374                                 if(a2 == -1)
2375                                         a2= precision;
2376                         }
2377                         else if(proptype == PROP_STRING) {
2378                                 min= 0;
2379                                 max= RNA_property_string_maxlength(prop);
2380                                 if(max == 0) /* interface code should ideally support unlimited length */
2381                                         max= UI_MAX_DRAW_STR; 
2382                         }
2383                 }
2384         }
2385         else
2386                 str= (char*)propname;
2387
2388         /* now create button */
2389         but= ui_def_but(block, type, retval, str, x1, y1, x2, y2, NULL, min, max, a1, a2, tip);
2390
2391         if(prop) {
2392                 but->rnapoin= *ptr;
2393                 but->rnaprop= prop;
2394
2395                 if(RNA_property_array_length(but->rnaprop))
2396                         but->rnaindex= index;
2397                 else
2398                         but->rnaindex= 0;
2399         }
2400
2401         if(icon) {
2402                 but->icon= (BIFIconID)icon;
2403                 but->flag |= UI_HAS_ICON;
2404                 but->flag|= UI_ICON_LEFT;
2405         }
2406         
2407         if (!prop || !RNA_property_editable(&but->rnapoin, prop)) {
2408                 but->flag |= UI_BUT_DISABLED;
2409                 but->lock = 1;
2410                 but->lockstr = "";
2411         }
2412
2413         /* If this button uses units, calculate the step from this */
2414         if(ui_is_but_unit(but))
2415                 but->a1= ui_get_but_step_unit(but, ui_get_but_val(but), but->a1);
2416
2417         if(freestr)
2418                 MEM_freeN(str);
2419         
2420         return but;
2421 }
2422
2423 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)
2424 {
2425         uiBut *but;
2426         wmOperatorType *ot;
2427         
2428         ot= WM_operatortype_find(opname, 0);
2429
2430         if(!str) {
2431                 if(ot) str= ot->name;
2432                 else str= opname;
2433         }
2434         
2435         if ((!tip || tip[0]=='\0') && ot && ot->description) {
2436                 tip= ot->description;
2437         }
2438
2439         but= ui_def_but(block, type, -1, str, x1, y1, x2, y2, NULL, 0, 0, 0, 0, tip);
2440         but->optype= ot;
2441         but->opcontext= opcontext;
2442
2443         if(!ot) {
2444                 but->flag |= UI_BUT_DISABLED;
2445                 but->lock = 1;
2446                 but->lockstr = "";
2447         }
2448
2449         return but;
2450 }
2451
2452 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)
2453 {
2454         uiBut *but= ui_def_but(block, type, retval, str, x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2455
2456         ui_check_but(but);
2457         
2458         return but;
2459 }
2460
2461         /* if _x_ is a power of two (only one bit) return the power,
2462          * otherwise return -1. 
2463          * (1<<findBitIndex(x))==x for powers of two.
2464          */
2465 static int findBitIndex(unsigned int x) {
2466         if (!x || (x&(x-1))!=0) {       /* x&(x-1) strips lowest bit */
2467                 return -1;
2468         } else {
2469                 int idx= 0;
2470
2471                 if (x&0xFFFF0000)       idx+=16, x>>=16;
2472                 if (x&0xFF00)           idx+=8, x>>=8;
2473                 if (x&0xF0)                     idx+=4, x>>=4;
2474                 if (x&0xC)                      idx+=2, x>>=2;
2475                 if (x&0x2)                      idx+=1;
2476
2477                 return idx;
2478         }
2479 }
2480
2481 /* autocomplete helper functions */
2482 struct AutoComplete {
2483         int maxlen;
2484         char *truncate;
2485         char *startname;
2486 };
2487
2488 AutoComplete *autocomplete_begin(char *startname, int maxlen)
2489 {
2490         AutoComplete *autocpl;
2491         
2492         autocpl= MEM_callocN(sizeof(AutoComplete), "AutoComplete");
2493         autocpl->maxlen= maxlen;
2494         autocpl->truncate= MEM_callocN(sizeof(char)*maxlen, "AutoCompleteTruncate");
2495         autocpl->startname= startname;
2496
2497         return autocpl;
2498 }
2499
2500 void autocomplete_do_name(AutoComplete *autocpl, const char *name)
2501 {
2502         char *truncate= autocpl->truncate;
2503         char *startname= autocpl->startname;
2504         int a;
2505
2506         for(a=0; a<autocpl->maxlen-1; a++) {
2507                 if(startname[a]==0 || startname[a]!=name[a])
2508                         break;
2509         }
2510         /* found a match */
2511         if(startname[a]==0) {
2512                 /* first match */
2513                 if(truncate[0]==0)
2514                         BLI_strncpy(truncate, name, autocpl->maxlen);
2515                 else {
2516                         /* remove from truncate what is not in bone->name */
2517                         for(a=0; a<autocpl->maxlen-1; a++) {
2518                                 if(name[a] == 0) {
2519                                         truncate[a]= 0;
2520                                         break;
2521                                 }
2522                                 else if(truncate[a]!=name[a])
2523                                         truncate[a]= 0;
2524                         }
2525                 }
2526         }
2527 }
2528
2529 void autocomplete_end(AutoComplete *autocpl, char *autoname)
2530 {       
2531         if(autocpl->truncate[0])
2532                 BLI_strncpy(autoname, autocpl->truncate, autocpl->maxlen);
2533         else {
2534                 if (autoname != autocpl->startname) /* dont copy a string over its self */
2535                         BLI_strncpy(autoname, autocpl->startname, autocpl->maxlen);
2536         }
2537         MEM_freeN(autocpl->truncate);
2538         MEM_freeN(autocpl);
2539 }
2540
2541 /* autocomplete callback for ID buttons */
2542 static void autocomplete_id(bContext *C, char *str, void *arg_v)
2543 {
2544         int blocktype= (intptr_t)arg_v;
2545         ListBase *listb= wich_libbase(CTX_data_main(C), blocktype);
2546         
2547         if(listb==NULL) return;
2548         
2549         /* search if str matches the beginning of an ID struct */
2550         if(str[0]) {
2551                 AutoComplete *autocpl= autocomplete_begin(str, 22);
2552                 ID *id;
2553                 
2554                 for(id= listb->first; id; id= id->next)
2555                         autocomplete_do_name(autocpl, id->name+2);
2556
2557                 autocomplete_end(autocpl, str);
2558         }
2559 }
2560
2561 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)
2562 {
2563         int bitIdx= findBitIndex(bit);
2564         if (bitIdx==-1) {
2565                 return NULL;
2566         } else {
2567                 return uiDefBut(block, type|BIT|bitIdx, retval, str, x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2568         }
2569 }
2570 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)
2571 {
2572         return uiDefBut(block, type|FLO, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2573 }
2574 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)
2575 {
2576         return uiDefButBit(block, type|FLO, bit, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2577 }
2578 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)
2579 {
2580         return uiDefBut(block, type|INT, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2581 }
2582 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)
2583 {
2584         return uiDefButBit(block, type|INT, bit, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2585 }
2586 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)
2587 {
2588         return uiDefBut(block, type|SHO, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2589 }
2590 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)
2591 {
2592         return uiDefButBit(block, type|SHO, bit, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2593 }
2594 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)
2595 {
2596         return uiDefBut(block, type|CHA, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2597 }
2598 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)
2599 {
2600         return uiDefButBit(block, type|CHA, bit, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2601 }
2602 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)
2603 {
2604         uiBut *but;
2605
2606         but= ui_def_but_rna(block, type, retval, str, x1, y1, x2, y2, ptr, propname, index, min, max, a1, a2, tip);
2607         if(but)
2608                 ui_check_but(but);
2609
2610         return but;
2611 }
2612 uiBut *uiDefButO(uiBlock *block, int type, char *opname, int opcontext, char *str, short x1, short y1, short x2, short y2, char *tip)
2613 {
2614         uiBut *but;
2615
2616         but= ui_def_but_operator(block, type, opname, opcontext, str, x1, y1, x2, y2, tip);
2617         if(but)
2618                 ui_check_but(but);
2619
2620         return but;
2621 }
2622
2623 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)
2624 {
2625         uiBut *but= ui_def_but(block, type, retval, "", x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2626         
2627         but->icon= (BIFIconID) icon;
2628         but->flag|= UI_HAS_ICON;
2629
2630         ui_check_but(but);
2631         
2632         return but;
2633 }
2634 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)
2635 {
2636         int bitIdx= findBitIndex(bit);
2637         if (bitIdx==-1) {
2638                 return NULL;
2639         } else {
2640                 return uiDefIconBut(block, type|BIT|bitIdx, retval, icon, x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2641         }
2642 }
2643
2644 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)
2645 {
2646         return uiDefIconBut(block, type|FLO, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2647 }
2648 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)
2649 {
2650         return uiDefIconButBit(block, type|FLO, bit, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2651 }
2652 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)
2653 {
2654         return uiDefIconBut(block, type|INT, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2655 }
2656 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)
2657 {
2658         return uiDefIconButBit(block, type|INT, bit, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2659 }
2660 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)
2661 {
2662         return uiDefIconBut(block, type|SHO, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2663 }
2664 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)
2665 {
2666         return uiDefIconButBit(block, type|SHO, bit, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2667 }
2668 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)
2669 {
2670         return uiDefIconBut(block, type|CHA, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2671 }
2672 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)
2673 {
2674         return uiDefIconButBit(block, type|CHA, bit, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2675 }
2676 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)
2677 {
2678         uiBut *but;
2679
2680         but= ui_def_but_rna(block, type, retval, "", x1, y1, x2, y2, ptr, propname, index, min, max, a1, a2, tip);
2681         if(but) {
2682                 if(icon) {
2683                         but->icon= (BIFIconID) icon;
2684                         but->flag|= UI_HAS_ICON;
2685                 }
2686                 ui_check_but(but);
2687         }
2688
2689         return but;
2690 }
2691 uiBut *uiDefIconButO(uiBlock *block, int type, char *opname, int opcontext, int icon, short x1, short y1, short x2, short y2, char *tip)
2692 {
2693         uiBut *but;
2694
2695         but= ui_def_but_operator(block, type, opname, opcontext, "", x1, y1, x2, y2, tip);
2696         if(but) {
2697                 but->icon= (BIFIconID) icon;
2698                 but->flag|= UI_HAS_ICON;
2699                 ui_check_but(but);
2700         }
2701
2702         return but;
2703 }
2704
2705 /* Button containing both string label and icon */
2706 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)
2707 {
2708         uiBut *but= ui_def_but(block, type, retval, str, x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2709
2710         but->icon= (BIFIconID) icon;
2711         but->flag|= UI_HAS_ICON;
2712
2713         but->flag|= UI_ICON_LEFT;
2714
2715         ui_check_but(but);
2716
2717         return but;
2718 }
2719 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)
2720 {
2721         int bitIdx= findBitIndex(bit);
2722         if (bitIdx==-1) {
2723                 return NULL;
2724         } else {
2725                 return uiDefIconTextBut(block, type|BIT|bitIdx, retval, icon, str, x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2726         }
2727 }
2728
2729 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)
2730 {
2731         return uiDefIconTextBut(block, type|FLO, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2732 }
2733 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)
2734 {
2735         return uiDefIconTextButBit(block, type|FLO, bit, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2736 }
2737 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)
2738 {
2739         return uiDefIconTextBut(block, type|INT, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2740 }
2741 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)
2742 {
2743         return uiDefIconTextButBit(block, type|INT, bit, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2744 }
2745 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)
2746 {
2747         return uiDefIconTextBut(block, type|SHO, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2748 }
2749 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)
2750 {
2751         return uiDefIconTextButBit(block, type|SHO, bit, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2752 }
2753 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)
2754 {
2755         return uiDefIconTextBut(block, type|CHA, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2756 }
2757 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)
2758 {
2759         return uiDefIconTextButBit(block, type|CHA, bit, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2760 }
2761 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)
2762 {
2763         uiBut *but;
2764
2765         but= ui_def_but_rna(block, type, retval, str, x1, y1, x2, y2, ptr, propname, index, min, max, a1, a2, tip);
2766         if(but) {
2767                 if(icon) {
2768                         but->icon= (BIFIconID) icon;
2769                         but->flag|= UI_HAS_ICON;
2770                 }
2771                 but->flag|= UI_ICON_LEFT;
2772                 ui_check_but(but);
2773         }
2774
2775         return but;
2776 }
2777 uiBut *uiDefIconTextButO(uiBlock *block, int type, char *opname, int opcontext, int icon, char *str, short x1, short y1, short x2, short y2, char *tip)
2778 {
2779         uiBut *but;
2780
2781         but= ui_def_but_operator(block, type, opname, opcontext, str, x1, y1, x2, y2, tip);
2782         if(but) {
2783                 but->icon= (BIFIconID) icon;
2784                 but->flag|= UI_HAS_ICON;
2785                 but->flag|= UI_ICON_LEFT;
2786                 ui_check_but(but);
2787         }
2788
2789         return but;
2790 }
2791
2792 /* END Button containing both string label and icon */
2793
2794 void uiSetButLink(uiBut *but, void **poin, void ***ppoin, short *tot, int from, int to)
2795 {
2796         uiLink *link;
2797         
2798         link= but->link= MEM_callocN(sizeof(uiLink), "new uilink");
2799         
2800         link->poin= poin;
2801         link->ppoin= ppoin;
2802         link->totlink= tot;
2803         link->fromcode= from;
2804         link->tocode= to;
2805 }
2806
2807 /* cruft to make uiBlock and uiBut private */
2808
2809 int uiBlocksGetYMin(ListBase *lb)
2810 {
2811         uiBlock *block;
2812         int min= 0;
2813         
2814         for (block= lb->first; block; block= block->next)
2815                 if (block==lb->first || block->miny<min)
2816                         min= block->miny;
2817                         
2818         return min;
2819 }
2820
2821 void uiBlockSetDirection(uiBlock *block, int direction)
2822 {
2823         block->direction= direction;
2824 }
2825
2826 /* this call escapes if there's alignment flags */
2827 void uiBlockFlipOrder(uiBlock *block)
2828 {
2829         ListBase lb;
2830         uiBut *but, *next;
2831         float centy, miny=10000, maxy= -10000;
2832
2833         if(U.uiflag & USER_MENUFIXEDORDER)
2834                 return;
2835         
2836         for(but= block->buttons.first; but; but= but->next) {
2837                 if(but->flag & UI_BUT_ALIGN) return;
2838                 if(but->y1 < miny) miny= but->y1;
2839                 if(but->y2 > maxy) maxy= but->y2;
2840         }
2841         /* mirror trick */
2842         centy= (miny+maxy)/2.0;
2843         for(but= block->buttons.first; but; but= but->next) {
2844                 but->y1 = centy-(but->y1-centy);
2845                 but->y2 = centy-(but->y2-centy);
2846                 SWAP(float, but->y1, but->y2);
2847         }
2848         
2849         /* also flip order in block itself, for example for arrowkey */
2850         lb.first= lb.last= NULL;
2851         but= block->buttons.first;
2852         while(but) {
2853                 next= but->next;
2854                 BLI_remlink(&block->buttons, but);
2855                 BLI_addtail(&lb, but);
2856                 but= next;
2857         }
2858         block->buttons= lb;
2859 }
2860
2861
2862 void uiBlockSetFlag(uiBlock *block, int flag)
2863 {
2864         block->flag|= flag;
2865 }
2866
2867 void uiBlockClearFlag(uiBlock *block, int flag)
2868 {
2869         block->flag&= ~flag;
2870 }
2871
2872 void uiBlockSetXOfs(uiBlock *block, int xofs)
2873 {
2874         block->xofs= xofs;
2875 }
2876
2877 void uiButSetFlag(uiBut *but, int flag)
2878 {
2879         but->flag|= flag;
2880 }
2881
2882 void uiButClearFlag(uiBut *but, int flag)
2883 {
2884         but->flag&= ~flag;
2885 }
2886
2887 int uiButGetRetVal(uiBut *but)
2888 {
2889         return but->retval;
2890 }
2891
2892 PointerRNA *uiButGetOperatorPtrRNA(uiBut *but)
2893 {
2894         if(but->optype && !but->opptr) {
2895                 but->opptr= MEM_callocN(sizeof(PointerRNA), "uiButOpPtr");
2896                 WM_operator_properties_create(but->opptr, but->optype->idname);
2897         }
2898
2899         return but->opptr;
2900 }
2901
2902 void uiBlockSetHandleFunc(uiBlock *block, uiBlockHandleFunc func, void *arg)
2903 {
2904         block->handle_func= func;
2905         block->handle_func_arg= arg;
2906 }
2907
2908 void uiBlockSetButmFunc(uiBlock *block, uiMenuHandleFunc func, void *arg)
2909 {
2910         block->butm_func= func;
2911         block->butm_func_arg= arg;
2912 }
2913
2914 void uiBlockSetFunc(uiBlock *block, uiButHandleFunc func, void *arg1, void *arg2)
2915 {
2916         block->func= func;
2917         block->func_arg1= arg1;
2918         block->func_arg2= arg2;
2919 }
2920
2921 void uiButSetRenameFunc(uiBut *but, uiButHandleRenameFunc func, void *arg1)
2922 {
2923         but->rename_func= func;
2924         but->rename_arg1= arg1;
2925 }
2926
2927 void uiBlockSetDrawExtraFunc(uiBlock *block, void (*func)(const bContext *C, void *idv, void *argv, rcti *rect), void *arg)
2928 {
2929         block->drawextra= func;
2930         block->drawextra_arg= arg;
2931 }
2932
2933 void uiButSetFunc(uiBut *but, uiButHandleFunc func, void *arg1, void *arg2)
2934 {
2935         but->func= func;
2936         but->func_arg1= arg1;
2937         but->func_arg2= arg2;
2938 }
2939
2940 void uiButSetNFunc(uiBut *but, uiButHandleNFunc funcN, void *argN, void *arg2)
2941 {
2942         but->funcN= funcN;
2943         but->func_argN= argN;
2944         but->func_arg2= arg2;
2945 }
2946
2947 void uiButSetCompleteFunc(uiBut *but, uiButCompleteFunc func, void *arg)
2948 {
2949         but->autocomplete_func= func;
2950         but->autofunc_arg= arg;
2951 }
2952
2953 uiBut *uiDefIDPoinBut(uiBlock *block, uiIDPoinFuncFP func, short blocktype, int retval, char *str, short x1, short y1, short x2, short y2, void *idpp, char *tip)
2954 {
2955         uiBut *but= ui_def_but(block, IDPOIN, retval, str, x1, y1, x2, y2, NULL, 0.0, 0.0, 0.0, 0.0, tip);
2956         but->idpoin_func= func;
2957         but->idpoin_idpp= (ID**) idpp;
2958         ui_check_but(but);
2959         
2960         if(blocktype)
2961                 uiButSetCompleteFunc(but, autocomplete_id, (void *)(intptr_t)blocktype);
2962
2963         return but;
2964 }
2965
2966 uiBut *uiDefBlockBut(uiBlock *block, uiBlockCreateFunc func, void *arg, char *str, short x1, short y1, short x2, short y2, char *tip)
2967 {
2968         uiBut *but= ui_def_but(block, BLOCK, 0, str, x1, y1, x2, y2, arg, 0.0, 0.0, 0.0, 0.0, tip);
2969         but->block_create_func= func;
2970         ui_check_but(but);
2971         return but;
2972 }
2973
2974 uiBut *uiDefBlockButN(uiBlock *block, uiBlockCreateFunc func, void *argN, 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, NULL, 0.0, 0.0, 0.0, 0.0, tip);
2977         but->block_create_func= func;
2978         but->func_argN= argN;
2979         ui_check_but(but);
2980         return but;
2981 }
2982
2983
2984 uiBut *uiDefPulldownBut(uiBlock *block, uiBlockCreateFunc func, void *arg, char *str, short x1, short y1, short x2, short y2, char *tip)
2985 {
2986         uiBut *but= ui_def_but(block, PULLDOWN, 0, str, x1, y1, x2, y2, arg, 0.0, 0.0, 0.0, 0.0, tip);
2987         but->block_create_func= func;
2988         ui_check_but(but);
2989         return but;
2990 }
2991
2992 uiBut *uiDefMenuBut(uiBlock *block, uiMenuCreateFunc 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->menu_create_func= func;
2996         ui_check_but(but);
2997         return but;
2998 }
2999
3000 uiBut *uiDefIconTextMenuBut(uiBlock *block, uiMenuCreateFunc func, void *arg, int icon, 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
3004         but->icon= (BIFIconID) icon;
3005         but->flag|= UI_HAS_ICON;
3006
3007         but->flag|= UI_ICON_LEFT;
3008         but->flag|= UI_ICON_SUBMENU;
3009
3010         but->menu_create_func= func;
3011         ui_check_but(but);
3012
3013         return but;
3014 }
3015
3016 /* Block button containing both string label and icon */
3017 uiBut *uiDefIconTextBlockBut(uiBlock *block, uiBlockCreateFunc func, void *arg, int icon, char *str, short x1, short y1, short x2, short y2, char *tip)
3018 {
3019         uiBut *but= ui_def_but(block, BLOCK, 0, str, x1, y1, x2, y2, arg, 0.0, 0.0, 0.0, 0.0, tip);
3020         
3021         /* XXX temp, old menu calls pass on icon arrow, which is now UI_ICON_SUBMENU flag */
3022         if(icon!=ICON_RIGHTARROW_THIN) {
3023                 but->icon= (BIFIconID) icon;
3024                 but->flag|= UI_ICON_LEFT;
3025         }
3026         but->flag|= UI_HAS_ICON;
3027         but->flag|= UI_ICON_SUBMENU;
3028
3029         but->block_create_func= func;
3030         ui_check_but(but);
3031         
3032         return but;
3033 }
3034
3035 /* Block button containing icon */
3036 uiBut *uiDefIconBlockBut(uiBlock *block, uiBlockCreateFunc func, void *arg, int retval, int icon, short x1, short y1, short x2, short y2, char *tip)
3037 {
3038         uiBut *but= ui_def_but(block, BLOCK, retval, "", x1, y1, x2, y2, arg, 0.0, 0.0, 0.0, 0.0, tip);
3039         
3040         but->icon= (BIFIconID) icon;
3041         but->flag|= UI_HAS_ICON;
3042         
3043         but->flag|= UI_ICON_LEFT;
3044         
3045         but->block_create_func= func;
3046         ui_check_but(but);
3047         
3048         return but;
3049 }
3050
3051 void uiDefKeyevtButS(uiBlock *block, int retval, char *str, short x1, short y1, short x2, short y2, short *spoin, char *tip)
3052 {
3053         uiBut *but= ui_def_but(block, KEYEVT|SHO, retval, str, x1, y1, x2, y2, spoin, 0.0, 0.0, 0.0, 0.0, tip);
3054         ui_check_but(but);
3055 }
3056
3057 /* short pointers hardcoded */
3058 /* modkeypoin will be set to KM_SHIFT, KM_ALT, KM_CTRL, KM_OSKEY bits */
3059 uiBut *uiDefHotKeyevtButS(uiBlock *block, int retval, char *str, short x1, short y1, short x2, short y2, short *keypoin, short *modkeypoin, char *tip)
3060 {
3061         uiBut *but= ui_def_but(block, HOTKEYEVT|SHO, retval, str, x1, y1, x2, y2, keypoin, 0.0, 0.0, 0.0, 0.0, tip);
3062         but->func_arg3= modkeypoin; /* XXX hrmf, abuse! */
3063         ui_check_but(but);
3064         return but;
3065 }
3066
3067
3068 /* arg is pointer to string/name, use uiButSetSearchFunc() below to make this work */
3069 uiBut *uiDefSearchBut(uiBlock *block, void *arg, int retval, int icon, int maxlen, short x1, short y1, short x2, short y2, char *tip)
3070 {
3071         uiBut *but= ui_def_but(block, SEARCH_MENU, retval, "", x1, y1, x2, y2, arg, 0.0, maxlen, 0.0, 0.0, tip);
3072         
3073         but->icon= (BIFIconID) icon;
3074         but->flag|= UI_HAS_ICON;
3075         
3076         but->flag|= UI_ICON_LEFT|UI_TEXT_LEFT;
3077         
3078         ui_check_but(but);
3079         
3080         return but;
3081 }
3082
3083 /* arg is user value, searchfunc and handlefunc both get it as arg */
3084 /* if active set, button opens with this item visible and selected */
3085 void uiButSetSearchFunc(uiBut *but, uiButSearchFunc sfunc, void *arg, uiButHandleFunc bfunc, void *active)
3086 {
3087         but->search_func= sfunc;
3088         but->search_arg= arg;
3089         
3090         uiButSetFunc(but, bfunc, arg, active);
3091 }
3092
3093 /* Program Init/Exit */
3094
3095 void UI_init(void)
3096 {
3097         ui_resources_init();
3098 }
3099
3100 /* after reading userdef file */
3101 void UI_init_userdef(void)
3102 {
3103         /* fix saved themes */
3104         init_userdef_do_versions();
3105         /* set default colors in default theme */
3106         ui_theme_init_userdef();
3107         
3108         uiStyleInit();
3109 }
3110
3111 void UI_exit(void)
3112 {
3113         ui_resources_free();
3114 }
3115