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