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