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