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