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