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