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