Code holiday commit:
[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 than 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                 if(!(but->flag & (UI_HIDDEN|UI_SCROLLED))) {
896                         ui_but_to_pixelrect(&rect, ar, block, but);
897                 
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                         if(rect.xmin < rect.xmax && rect.ymin < rect.ymax)
901                                 ui_draw_but(C, ar, &style, but, &rect);
902                 }
903         }
904         
905         /* restore matrix */
906         glMatrixMode(GL_PROJECTION);
907         glPopMatrix();
908         glMatrixMode(GL_MODELVIEW);
909         glPopMatrix();
910         
911         ui_draw_links(block);
912 }
913
914 /* ************* EVENTS ************* */
915
916 static void ui_is_but_sel(uiBut *but)
917 {
918         double value; /* only initialized when needed, to avoid calling when not used */
919         short push=0, true=1;
920
921         if(ELEM3(but->type, TOGN, ICONTOGN, OPTIONN)) true= 0;
922
923         if( but->bit ) {
924                 int lvalue;
925                 value= ui_get_but_val(but);
926                 lvalue= (int)value;
927                 if( BTST(lvalue, (but->bitnr)) ) push= true;
928                 else push= !true;
929         }
930         else {
931                 switch(but->type) {
932                 case BUT:
933                         push= 2;
934                         break;
935                 case HOTKEYEVT:
936                 case KEYEVT:
937                         push= 2;
938                         break;
939                 case TOGBUT:
940                 case TOG:
941                 case TOGR:
942                 case TOG3:
943                 case BUT_TOGDUAL:
944                 case ICONTOG:
945                 case OPTION:
946                         value= ui_get_but_val(but);
947                         if(value != (double)but->hardmin) push= 1;
948                         break;
949                 case ICONTOGN:
950                 case TOGN:
951                 case OPTIONN:
952                         value= ui_get_but_val(but);
953                         if(value==0.0) push= 1;
954                         break;
955                 case ROW:
956                 case LISTROW:
957                         value= ui_get_but_val(but);
958                         /* support for rna enum buts */
959                         if(but->rnaprop && (RNA_property_flag(but->rnaprop) & PROP_ENUM_FLAG)) {
960                                 if((int)value & (int)but->hardmax) push= 1;
961                         }
962                         else {
963                                 if(value == (double)but->hardmax) push= 1;
964                         }
965                         break;
966                 case COL:
967                         push= 2;
968                         break;
969                 default:
970                         push= 2;
971                         break;
972                 }
973         }
974         
975         if(push==2);
976         else if(push==1) but->flag |= UI_SELECT;
977         else but->flag &= ~UI_SELECT;
978 }
979
980 static uiBut *ui_find_inlink(uiBlock *block, void *poin)
981 {
982         uiBut *but;
983         
984         but= block->buttons.first;
985         while(but) {
986                 if(but->type==INLINK) {
987                         if(but->poin == poin) return but;
988                 }
989                 but= but->next;
990         }
991         return NULL;
992 }
993
994 static void ui_add_link_line(ListBase *listb, uiBut *but, uiBut *bt)
995 {
996         uiLinkLine *line;
997         
998         line= MEM_callocN(sizeof(uiLinkLine), "linkline");
999         BLI_addtail(listb, line);
1000         line->from= but;
1001         line->to= bt;
1002 }
1003
1004 uiBut *uiFindInlink(uiBlock *block, void *poin)
1005 {
1006         return ui_find_inlink(block, poin);
1007 }
1008
1009 void uiComposeLinks(uiBlock *block)
1010 {
1011         uiBut *but, *bt;
1012         uiLink *link;
1013         void ***ppoin;
1014         int a;
1015         
1016         but= block->buttons.first;
1017         while(but) {
1018                 if(but->type==LINK) {
1019                         link= but->link;
1020                         
1021                         /* for all pointers in the array */
1022                         if(link) {
1023                                 if(link->ppoin) {
1024                                         ppoin= link->ppoin;
1025                                         for(a=0; a < *(link->totlink); a++) {
1026                                                 bt= ui_find_inlink(block, (*ppoin)[a] );
1027                                                 if(bt) {
1028                                                         ui_add_link_line(&link->lines, but, bt);
1029                                                 }
1030                                         }
1031                                 }
1032                                 else if(link->poin) {
1033                                         bt= ui_find_inlink(block, *(link->poin) );
1034                                         if(bt) {
1035                                                 ui_add_link_line(&link->lines, but, bt);
1036                                         }
1037                                 }
1038                         }
1039                 }
1040                 but= but->next;
1041         }
1042 }
1043
1044
1045 /* ************************************************ */
1046
1047 void uiBlockSetButLock(uiBlock *block, int val, const char *lockstr)
1048 {
1049         if(val) {
1050                 block->lock= val ? 1:0;
1051                 block->lockstr= lockstr;
1052         }
1053 }
1054
1055 void uiBlockClearButLock(uiBlock *block)
1056 {
1057         block->lock= 0;
1058         block->lockstr= NULL;
1059 }
1060
1061 /* *************************************************************** */
1062
1063 void ui_delete_linkline(uiLinkLine *line, uiBut *but)
1064 {
1065         uiLink *link;
1066         int a, b;
1067         
1068         BLI_remlink(&but->link->lines, line);
1069
1070         link= line->from->link;
1071
1072         /* are there more pointers allowed? */
1073         if(link->ppoin) {
1074                 
1075                 if(*(link->totlink)==1) {
1076                         *(link->totlink)= 0;
1077                         MEM_freeN(*(link->ppoin));
1078                         *(link->ppoin)= NULL;
1079                 }
1080                 else {
1081                         b= 0;
1082                         for(a=0; a< (*(link->totlink)); a++) {
1083                                 
1084                                 if( (*(link->ppoin))[a] != line->to->poin ) {
1085                                         (*(link->ppoin))[b]= (*(link->ppoin))[a];
1086                                         b++;
1087                                 }
1088                         }       
1089                         (*(link->totlink))--;
1090                 }
1091         }
1092         else {
1093                 *(link->poin)= NULL;
1094         }
1095
1096         MEM_freeN(line);
1097         //REDRAW
1098 }
1099
1100 /* *********************** data get/set ***********************
1101  * this either works with the pointed to data, or can work with
1102  * an edit override pointer while dragging for example */
1103
1104 /* for buttons pointing to color for example */
1105 void ui_get_but_vectorf(uiBut *but, float *vec)
1106 {
1107         PropertyRNA *prop;
1108         int a, tot;
1109
1110         if(but->editvec) {
1111                 VECCOPY(vec, but->editvec);
1112         }
1113
1114         if(but->rnaprop) {
1115                 prop= but->rnaprop;
1116
1117                 vec[0]= vec[1]= vec[2]= 0.0f;
1118
1119                 if(RNA_property_type(prop) == PROP_FLOAT) {
1120                         tot= RNA_property_array_length(&but->rnapoin, prop);
1121                         tot= MIN2(tot, 3);
1122
1123                         for(a=0; a<tot; a++)
1124                                 vec[a]= RNA_property_float_get_index(&but->rnapoin, prop, a);
1125                 }
1126         }
1127         else if(but->pointype == CHA) {
1128                 char *cp= (char *)but->poin;
1129                 vec[0]= ((float)cp[0])/255.0f;
1130                 vec[1]= ((float)cp[1])/255.0f;
1131                 vec[2]= ((float)cp[2])/255.0f;
1132         }
1133         else if(but->pointype == FLO) {
1134                 float *fp= (float *)but->poin;
1135                 VECCOPY(vec, fp);
1136         }
1137         else {
1138                 if (but->editvec==NULL) {
1139                         fprintf(stderr, "ui_get_but_vectorf: can't get color, should never happen\n");
1140                         vec[0]= vec[1]= vec[2]= 0.0f;
1141                 }
1142         }
1143 }
1144
1145 /* for buttons pointing to color for example */
1146 void ui_set_but_vectorf(uiBut *but, float *vec)
1147 {
1148         PropertyRNA *prop;
1149         int a, tot;
1150
1151         if(but->editvec) {
1152                 VECCOPY(but->editvec, vec);
1153         }
1154
1155         if(but->rnaprop) {
1156                 prop= but->rnaprop;
1157
1158                 if(RNA_property_type(prop) == PROP_FLOAT) {
1159                         tot= RNA_property_array_length(&but->rnapoin, prop);
1160                         tot= MIN2(tot, 3);
1161
1162                         for(a=0; a<tot; a++)
1163                                 RNA_property_float_set_index(&but->rnapoin, prop, a, vec[a]);
1164                 }
1165         }
1166         else if(but->pointype == CHA) {
1167                 char *cp= (char *)but->poin;
1168                 cp[0]= (char)(0.5f + vec[0]*255.0f);
1169                 cp[1]= (char)(0.5f + vec[1]*255.0f);
1170                 cp[2]= (char)(0.5f + vec[2]*255.0f);
1171         }
1172         else if(but->pointype == FLO) {
1173                 float *fp= (float *)but->poin;
1174                 VECCOPY(fp, vec);
1175         }
1176 }
1177
1178 int ui_is_but_float(uiBut *but)
1179 {
1180         if(but->pointype==FLO && but->poin)
1181                 return 1;
1182         
1183         if(but->rnaprop && RNA_property_type(but->rnaprop) == PROP_FLOAT)
1184                 return 1;
1185         
1186         return 0;
1187 }
1188
1189 int ui_is_but_unit(uiBut *but)
1190 {
1191         Scene *scene= CTX_data_scene((bContext *)but->block->evil_C);
1192         int unit_type= uiButGetUnitType(but);
1193
1194         if(unit_type == PROP_UNIT_NONE)
1195                 return 0;
1196
1197 #if 1 // removed so angle buttons get correct snapping
1198         if (scene->unit.system_rotation == USER_UNIT_ROT_RADIANS && unit_type == PROP_UNIT_ROTATION)
1199                 return 0;
1200 #endif
1201         
1202         /* for now disable time unit conversion */      
1203         if (unit_type == PROP_UNIT_TIME)
1204                 return 0;
1205
1206         if (scene->unit.system == USER_UNIT_NONE) {
1207                 if (unit_type != PROP_UNIT_ROTATION) {
1208                         return 0;
1209                 }
1210         }
1211
1212         return 1;
1213 }
1214
1215 int ui_is_but_rna_valid(uiBut *but)
1216 {
1217         if (but->rnaprop==NULL || RNA_struct_contains_property(&but->rnapoin, but->rnaprop)) {
1218                 return TRUE;
1219         }
1220         else {
1221                 printf("property removed %s: %p\n", but->drawstr, but->rnaprop);
1222                 return FALSE;
1223         }
1224 }
1225
1226 double ui_get_but_val(uiBut *but)
1227 {
1228         PropertyRNA *prop;
1229         double value = 0.0;
1230
1231         if(but->editval) { return *(but->editval); }
1232         if(but->poin==NULL && but->rnapoin.data==NULL) return 0.0;
1233
1234         if(but->rnaprop) {
1235                 prop= but->rnaprop;
1236
1237                 switch(RNA_property_type(prop)) {
1238                         case PROP_BOOLEAN:
1239                                 if(RNA_property_array_length(&but->rnapoin, prop))
1240                                         value= RNA_property_boolean_get_index(&but->rnapoin, prop, but->rnaindex);
1241                                 else
1242                                         value= RNA_property_boolean_get(&but->rnapoin, prop);
1243                                 break;
1244                         case PROP_INT:
1245                                 if(RNA_property_array_length(&but->rnapoin, prop))
1246                                         value= RNA_property_int_get_index(&but->rnapoin, prop, but->rnaindex);
1247                                 else
1248                                         value= RNA_property_int_get(&but->rnapoin, prop);
1249                                 break;
1250                         case PROP_FLOAT:
1251                                 if(RNA_property_array_length(&but->rnapoin, prop))
1252                                         value= RNA_property_float_get_index(&but->rnapoin, prop, but->rnaindex);
1253                                 else
1254                                         value= RNA_property_float_get(&but->rnapoin, prop);
1255                                 break;
1256                         case PROP_ENUM:
1257                                 value= RNA_property_enum_get(&but->rnapoin, prop);
1258                                 break;
1259                         default:
1260                                 value= 0.0;
1261                                 break;
1262                 }
1263         }
1264         else if(but->type== HSVSLI) {
1265                 float h, s, v, *fp;
1266                 
1267                 fp= (but->editvec)? but->editvec: (float *)but->poin;
1268                 rgb_to_hsv(fp[0], fp[1], fp[2], &h, &s, &v);
1269
1270                 switch(but->str[0]) {
1271                         case 'H': value= h; break;
1272                         case 'S': value= s; break;
1273                         case 'V': value= v; break;
1274                 }
1275         } 
1276         else if( but->pointype == CHA ) {
1277                 value= *(char *)but->poin;
1278         }
1279         else if( but->pointype == SHO ) {
1280                 value= *(short *)but->poin;
1281         } 
1282         else if( but->pointype == INT ) {
1283                 value= *(int *)but->poin;
1284         } 
1285         else if( but->pointype == FLO ) {
1286                 value= *(float *)but->poin;
1287         }
1288
1289         return value;
1290 }
1291
1292 void ui_set_but_val(uiBut *but, double value)
1293 {
1294         PropertyRNA *prop;
1295
1296         /* value is a hsv value: convert to rgb */
1297         if(but->rnaprop) {
1298                 prop= but->rnaprop;
1299
1300                 if(RNA_property_editable(&but->rnapoin, prop)) {
1301                         switch(RNA_property_type(prop)) {
1302                                 case PROP_BOOLEAN:
1303                                         if(RNA_property_array_length(&but->rnapoin, prop))
1304                                                 RNA_property_boolean_set_index(&but->rnapoin, prop, but->rnaindex, value);
1305                                         else
1306                                                 RNA_property_boolean_set(&but->rnapoin, prop, value);
1307                                         break;
1308                                 case PROP_INT:
1309                                         if(RNA_property_array_length(&but->rnapoin, prop))
1310                                                 RNA_property_int_set_index(&but->rnapoin, prop, but->rnaindex, (int)value);
1311                                         else
1312                                                 RNA_property_int_set(&but->rnapoin, prop, (int)value);
1313                                         break;
1314                                 case PROP_FLOAT:
1315                                         if(RNA_property_array_length(&but->rnapoin, prop))
1316                                                 RNA_property_float_set_index(&but->rnapoin, prop, but->rnaindex, value);
1317                                         else
1318                                                 RNA_property_float_set(&but->rnapoin, prop, value);
1319                                         break;
1320                                 case PROP_ENUM:
1321                                         if(RNA_property_flag(prop) & PROP_ENUM_FLAG) {
1322                                                 int ivalue= (int)value;
1323                                                 ivalue ^= RNA_property_enum_get(&but->rnapoin, prop); /* toggle for enum/flag buttons */
1324                                                 RNA_property_enum_set(&but->rnapoin, prop, ivalue);
1325                                         }
1326                                         else {
1327                                                 RNA_property_enum_set(&but->rnapoin, prop, value);
1328                                         }
1329                                         break;
1330                                 default:
1331                                         break;
1332                         }
1333                 }
1334         }
1335         else if(but->pointype==0);
1336         else if(but->type==HSVSLI ) {
1337                 float h, s, v, *fp;
1338                 
1339                 fp= (but->editvec)? but->editvec: (float *)but->poin;
1340                 rgb_to_hsv(fp[0], fp[1], fp[2], &h, &s, &v);
1341                 
1342                 switch(but->str[0]) {
1343                 case 'H': h= value; break;
1344                 case 'S': s= value; break;
1345                 case 'V': v= value; break;
1346                 }
1347                 
1348                 hsv_to_rgb(h, s, v, fp, fp+1, fp+2);
1349                 
1350         }
1351         else {
1352                 /* first do rounding */
1353                 if(but->pointype==CHA)
1354                         value= (char)floor(value+0.5);
1355                 else if(but->pointype==SHO ) {
1356                         /* gcc 3.2.1 seems to have problems 
1357                          * casting a double like 32772.0 to
1358                          * a short so we cast to an int, then 
1359                          to a short */
1360                         int gcckludge;
1361                         gcckludge = (int) floor(value+0.5);
1362                         value= (short)gcckludge;
1363                 }
1364                 else if(but->pointype==INT )
1365                         value= (int)floor(value+0.5);
1366                 else if(but->pointype==FLO ) {
1367                         float fval= (float)value;
1368                         if(fval>= -0.00001f && fval<= 0.00001f) fval= 0.0f;     /* prevent negative zero */
1369                         value= fval;
1370                 }
1371                 
1372                 /* then set value with possible edit override */
1373                 if(but->editval)
1374                         *but->editval= value;
1375                 else if(but->pointype==CHA)
1376                         *((char *)but->poin)= (char)value;
1377                 else if(but->pointype==SHO)
1378                         *((short *)but->poin)= (short)value;
1379                 else if(but->pointype==INT)
1380                         *((int *)but->poin)= (int)value;
1381                 else if(but->pointype==FLO)
1382                         *((float *)but->poin)= (float)value;
1383         }
1384
1385         /* update select flag */
1386         ui_is_but_sel(but);
1387 }
1388
1389 int ui_get_but_string_max_length(uiBut *but)
1390 {
1391         if(ELEM(but->type, TEX, SEARCH_MENU))
1392                 return but->hardmax;
1393         else if(but->type == IDPOIN)
1394                 return MAX_ID_NAME-2;
1395         else
1396                 return UI_MAX_DRAW_STR;
1397 }
1398
1399 static double ui_get_but_scale_unit(uiBut *but, double value)
1400 {
1401         Scene *scene= CTX_data_scene((bContext *)but->block->evil_C);
1402         int unit_type= uiButGetUnitType(but);
1403
1404         if(unit_type == PROP_UNIT_LENGTH) {
1405                 return value * (double)scene->unit.scale_length;
1406         }
1407         else if(unit_type == PROP_UNIT_AREA) {
1408                 return value * pow(scene->unit.scale_length, 2);
1409         }
1410         else if(unit_type == PROP_UNIT_VOLUME) {
1411                 return value * pow(scene->unit.scale_length, 3);
1412         }
1413         else if(unit_type == PROP_UNIT_TIME) { /* WARNING - using evil_C :| */
1414                 return FRA2TIME(value);
1415         }
1416         else {
1417                 return value;
1418         }
1419 }
1420
1421 /* str will be overwritten */
1422 void ui_convert_to_unit_alt_name(uiBut *but, char *str, int maxlen)
1423 {
1424         if(ui_is_but_unit(but)) {
1425                 int unit_type= uiButGetUnitType(but);
1426                 char *orig_str;
1427                 Scene *scene= CTX_data_scene((bContext *)but->block->evil_C);
1428                 
1429                 orig_str= MEM_callocN(sizeof(char)*maxlen + 1, "textedit sub str");
1430                 memcpy(orig_str, str, maxlen);
1431                 
1432                 bUnit_ToUnitAltName(str, maxlen, orig_str, scene->unit.system, unit_type>>16);
1433                 
1434                 MEM_freeN(orig_str);
1435         }
1436 }
1437
1438 static void ui_get_but_string_unit(uiBut *but, char *str, int len_max, double value, int pad)
1439 {
1440         Scene *scene= CTX_data_scene((bContext *)but->block->evil_C);
1441         int do_split= scene->unit.flag & USER_UNIT_OPT_SPLIT;
1442         int unit_type= uiButGetUnitType(but);
1443         int precision= but->a2;
1444
1445         if(scene->unit.scale_length<0.0001f) scene->unit.scale_length= 1.0f; // XXX do_versions
1446
1447         /* Sanity checks */
1448         if(precision>7)         precision= 7;
1449         else if(precision==0)   precision= 2;
1450
1451         bUnit_AsString(str, len_max, ui_get_but_scale_unit(but, value), precision, scene->unit.system, unit_type>>16, do_split, pad);
1452 }
1453
1454 static float ui_get_but_step_unit(uiBut *but, float step_default)
1455 {
1456         Scene *scene= CTX_data_scene((bContext *)but->block->evil_C);
1457         int unit_type= uiButGetUnitType(but)>>16;
1458         float step;
1459
1460         step = bUnit_ClosestScalar(ui_get_but_scale_unit(but, step_default), scene->unit.system, unit_type);
1461
1462         if(step > 0.0f) { /* -1 is an error value */
1463                 return (float)((double)step/ui_get_but_scale_unit(but, 1.0))*100.0f;
1464         }
1465         else {
1466                 return step_default;
1467         }
1468 }
1469
1470
1471 void ui_get_but_string(uiBut *but, char *str, int maxlen)
1472 {
1473         if(but->rnaprop && ELEM3(but->type, TEX, IDPOIN, SEARCH_MENU)) {
1474                 PropertyType type;
1475                 char *buf= NULL;
1476
1477                 type= RNA_property_type(but->rnaprop);
1478
1479                 if(type == PROP_STRING) {
1480                         /* RNA string */
1481                         buf= RNA_property_string_get_alloc(&but->rnapoin, but->rnaprop, str, maxlen);
1482                 }
1483                 else if(type == PROP_POINTER) {
1484                         /* RNA pointer */
1485                         PointerRNA ptr= RNA_property_pointer_get(&but->rnapoin, but->rnaprop);
1486                         buf= RNA_struct_name_get_alloc(&ptr, str, maxlen);
1487                 }
1488
1489                 if(!buf) {
1490                         str[0] = '\0';
1491                 }
1492                 else if(buf && buf != str) {
1493                         /* string was too long, we have to truncate */
1494                         BLI_strncpy(str, buf, maxlen);
1495                         MEM_freeN(buf);
1496                 }
1497         }
1498         else if(but->type == IDPOIN) {
1499                 /* ID pointer */
1500                 if(but->idpoin_idpp) { /* Can be NULL for ID properties by python */
1501                         ID *id= *(but->idpoin_idpp);
1502                         if(id) {
1503                                 BLI_strncpy(str, id->name+2, maxlen);
1504                                 return;
1505                         }
1506                 }
1507                 str[0] = '\0';
1508                 return;
1509         }
1510         else if(but->type == TEX) {
1511                 /* string */
1512                 BLI_strncpy(str, but->poin, maxlen);
1513                 return;
1514         }
1515         else if(but->type == SEARCH_MENU) {
1516                 /* string */
1517                 BLI_strncpy(str, but->poin, maxlen);
1518                 return;
1519         }
1520         else if(ui_but_anim_expression_get(but, str, maxlen))
1521                 ; /* driver expression */
1522         else {
1523                 /* number editing */
1524                 double value;
1525
1526                 value= ui_get_but_val(but);
1527
1528                 if(ui_is_but_float(but)) {
1529                         if(ui_is_but_unit(but)) {
1530                                 ui_get_but_string_unit(but, str, maxlen, value, 0);
1531                         }
1532                         else {
1533                                 int prec= (int)but->a2;
1534                                 if(prec==0) prec= 3;
1535                                 else CLAMP(prec, 1, 7);
1536
1537                                 BLI_snprintf(str, maxlen, "%.*f", prec, value);
1538                         }
1539                 }
1540                 else
1541                         BLI_snprintf(str, maxlen, "%d", (int)value);
1542         }
1543 }
1544
1545 #ifdef WITH_PYTHON
1546
1547 static int ui_set_but_string_eval_num_unit(bContext *C, uiBut *but, const char *str, double *value)
1548 {
1549         char str_unit_convert[256];
1550         const int unit_type= uiButGetUnitType(but);
1551         Scene *scene= CTX_data_scene((bContext *)but->block->evil_C);
1552
1553         BLI_strncpy(str_unit_convert, str, sizeof(str_unit_convert));
1554
1555         /* ugly, use the draw string to get the value, this could cause problems if it includes some text which resolves to a unit */
1556         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);
1557
1558         return (BPY_button_exec(C, str_unit_convert, value, TRUE) != -1);
1559 }
1560
1561 static int ui_set_but_string_eval_num(bContext *C, uiBut *but, const char *str, double *value)
1562 {
1563         int ok= FALSE;
1564
1565         if(str[0] != '\0') {
1566                 int is_unit_but= ui_is_but_unit(but);
1567                 /* only enable verbose if we won't run again with units */
1568                 if(BPY_button_exec(C, str, value, is_unit_but==FALSE) != -1) {
1569                         /* if the value parsed ok without unit conversion this button may still need a unit multiplier */
1570                         if(is_unit_but) {
1571                                 char str_new[128];
1572
1573                                 BLI_snprintf(str_new, sizeof(str_new), "%f", *value);
1574                                 ok= ui_set_but_string_eval_num_unit(C, but, str_new, value);
1575                         }
1576                         else {
1577                                 ok= TRUE; /* parse normal string via py (no unit conversion needed) */
1578                         }
1579                 }
1580                 else if(is_unit_but) {
1581                         /* parse failed, this is a unit but so run replacements and parse again */
1582                         ok= ui_set_but_string_eval_num_unit(C, but, str, value);
1583                 }
1584         }
1585
1586         return ok;
1587 }
1588
1589 #endif // WITH_PYTHON
1590
1591 int ui_set_but_string(bContext *C, uiBut *but, const char *str)
1592 {
1593         if(but->rnaprop && ELEM3(but->type, TEX, IDPOIN, SEARCH_MENU)) {
1594                 if(RNA_property_editable(&but->rnapoin, but->rnaprop)) {
1595                         PropertyType type;
1596
1597                         type= RNA_property_type(but->rnaprop);
1598
1599                         if(type == PROP_STRING) {
1600                                 /* RNA string */
1601                                 RNA_property_string_set(&but->rnapoin, but->rnaprop, str);
1602                                 return 1;
1603                         }
1604                         else if(type == PROP_POINTER) {
1605                                 /* RNA pointer */
1606                                 PointerRNA ptr, rptr;
1607                                 PropertyRNA *prop;
1608
1609                                 if(str == NULL || str[0] == '\0') {
1610                                         RNA_property_pointer_set(&but->rnapoin, but->rnaprop, PointerRNA_NULL);
1611                                         return 1;
1612                                 }
1613                                 else {
1614                                         ptr= but->rnasearchpoin;
1615                                         prop= but->rnasearchprop;
1616                                         
1617                                         if(prop && RNA_property_collection_lookup_string(&ptr, prop, str, &rptr))
1618                                                 RNA_property_pointer_set(&but->rnapoin, but->rnaprop, rptr);
1619
1620                                         return 1;
1621                                 }
1622
1623                                 return 0;
1624                         }
1625                 }
1626         }
1627         else if(but->type == IDPOIN) {
1628                 /* ID pointer */
1629                 but->idpoin_func(C, str, but->idpoin_idpp);
1630                 return 1;
1631         }
1632         else if(but->type == TEX) {
1633                 /* string */
1634                 BLI_strncpy(but->poin, str, but->hardmax);
1635                 return 1;
1636         }
1637         else if(but->type == SEARCH_MENU) {
1638                 /* string */
1639                 BLI_strncpy(but->poin, str, but->hardmax);
1640                 return 1;
1641         }
1642         else if(ui_but_anim_expression_set(but, str)) {
1643                 /* driver expression */
1644                 return 1;
1645         }
1646         else {
1647                 /* number editing */
1648                 double value;
1649
1650 #ifdef WITH_PYTHON
1651                 if(ui_set_but_string_eval_num(C, but, str, &value) == FALSE) {
1652                         return 0;
1653                 }
1654 #else
1655                 value= atof(str);
1656 #endif // WITH_PYTHON
1657
1658                 if(!ui_is_but_float(but)) value= (int)floor(value + 0.5);
1659                 if(but->type==NUMABS) value= fabs(value);
1660
1661                 /* not that we use hard limits here */
1662                 if(value < (double)but->hardmin) value= but->hardmin;
1663                 if(value > (double)but->hardmax) value= but->hardmax;
1664
1665                 ui_set_but_val(but, value);
1666                 return 1;
1667         }
1668
1669         return 0;
1670 }
1671
1672 void ui_set_but_default(bContext *C, short all)
1673 {
1674         PointerRNA ptr;
1675
1676         WM_operator_properties_create(&ptr, "UI_OT_reset_default_button");
1677         RNA_boolean_set(&ptr, "all", all);
1678         WM_operator_name_call(C, "UI_OT_reset_default_button", WM_OP_EXEC_DEFAULT, &ptr);
1679         WM_operator_properties_free(&ptr);
1680 }
1681
1682 static double soft_range_round_up(double value, double max)
1683 {
1684         /* round up to .., 0.1, 0.2, 0.5, 1, 2, 5, 10, 20, 50, .. */
1685         double newmax= pow(10.0, ceil(log(value)/log(10.0)));
1686
1687         if(newmax*0.2 >= max && newmax*0.2 >= value)
1688                 return newmax*0.2;
1689         else if(newmax*0.5 >= max && newmax*0.5 >= value)
1690                 return newmax*0.5;
1691         else
1692                 return newmax;
1693 }
1694
1695 static double soft_range_round_down(double value, double max)
1696 {
1697         /* round down to .., 0.1, 0.2, 0.5, 1, 2, 5, 10, 20, 50, .. */
1698         double newmax= pow(10.0, floor(log(value)/log(10.0)));
1699
1700         if(newmax*5.0 <= max && newmax*5.0 <= value)
1701                 return newmax*5.0;
1702         else if(newmax*2.0 <= max && newmax*2.0 <= value)
1703                 return newmax*2.0;
1704         else
1705                 return newmax;
1706 }
1707
1708 void ui_set_but_soft_range(uiBut *but, double value)
1709 {
1710         PropertyType type;
1711         double softmin, softmax /*, step, precision*/;
1712         
1713         if(but->rnaprop) {
1714                 type= RNA_property_type(but->rnaprop);
1715
1716                 /* clamp button range to something reasonable in case
1717                  * we get -inf/inf from RNA properties */
1718                 if(type == PROP_INT) {
1719                         int imin, imax, istep;
1720
1721                         RNA_property_int_ui_range(&but->rnapoin, but->rnaprop, &imin, &imax, &istep);
1722                         softmin= (imin == INT_MIN)? -1e4: imin;
1723                         softmax= (imin == INT_MAX)? 1e4: imax;
1724                         /*step= istep;*/ /*UNUSED*/
1725                         /*precision= 1;*/ /*UNUSED*/
1726                 }
1727                 else if(type == PROP_FLOAT) {
1728                         float fmin, fmax, fstep, fprecision;
1729
1730                         RNA_property_float_ui_range(&but->rnapoin, but->rnaprop, &fmin, &fmax, &fstep, &fprecision);
1731                         softmin= (fmin == -FLT_MAX)? (float)-1e4: fmin;
1732                         softmax= (fmax == FLT_MAX)? (float)1e4: fmax;
1733                         /*step= fstep;*/ /*UNUSED*/
1734                         /*precision= fprecision;*/ /*UNUSED*/
1735                 }
1736                 else
1737                         return;
1738
1739                 /* if the value goes out of the soft/max range, adapt the range */
1740                 if(value+1e-10 < softmin) {
1741                         if(value < 0.0)
1742                                 softmin= -soft_range_round_up(-value, -softmin);
1743                         else
1744                                 softmin= soft_range_round_down(value, softmin);
1745
1746                         if(softmin < (double)but->hardmin)
1747                                 softmin= (double)but->hardmin;
1748                 }
1749                 else if(value-1e-10 > softmax) {
1750                         if(value < 0.0)
1751                                 softmax= -soft_range_round_down(-value, -softmax);
1752                         else
1753                                 softmax= soft_range_round_up(value, softmax);
1754
1755                         if(softmax > (double)but->hardmax)
1756                                 softmax= but->hardmax;
1757                 }
1758
1759                 but->softmin= softmin;
1760                 but->softmax= softmax;
1761         }
1762 }
1763
1764 /* ******************* Free ********************/
1765
1766 static void ui_free_link(uiLink *link)
1767 {
1768         if(link) {      
1769                 BLI_freelistN(&link->lines);
1770                 MEM_freeN(link);
1771         }
1772 }
1773
1774 /* can be called with C==NULL */
1775 static void ui_free_but(const bContext *C, uiBut *but)
1776 {
1777         if(but->opptr) {
1778                 WM_operator_properties_free(but->opptr);
1779                 MEM_freeN(but->opptr);
1780         }
1781         if(but->func_argN) MEM_freeN(but->func_argN);
1782         if(but->active) {
1783                 /* XXX solve later, buttons should be free-able without context ideally,
1784                    however they may have open tooltips or popup windows, which need to
1785                    be closed using a context pointer */
1786                 if(C) 
1787                         ui_button_active_free(C, but);
1788                 else
1789                         if(but->active) 
1790                                 MEM_freeN(but->active);
1791         }
1792         if(but->str && but->str != but->strdata) MEM_freeN(but->str);
1793         ui_free_link(but->link);
1794
1795         MEM_freeN(but);
1796 }
1797
1798 /* can be called with C==NULL */
1799 void uiFreeBlock(const bContext *C, uiBlock *block)
1800 {
1801         uiBut *but;
1802
1803         while( (but= block->buttons.first) ) {
1804                 BLI_remlink(&block->buttons, but);      
1805                 ui_free_but(C, but);
1806         }
1807
1808         if(block->func_argN)
1809                 MEM_freeN(block->func_argN);
1810
1811         CTX_store_free_list(&block->contexts);
1812
1813         BLI_freelistN(&block->saferct);
1814         
1815         MEM_freeN(block);
1816 }
1817
1818 /* can be called with C==NULL */
1819 void uiFreeBlocks(const bContext *C, ListBase *lb)
1820 {
1821         uiBlock *block;
1822         
1823         while( (block= lb->first) ) {
1824                 BLI_remlink(lb, block);
1825                 uiFreeBlock(C, block);
1826         }
1827 }
1828
1829 void uiFreeInactiveBlocks(const bContext *C, ListBase *lb)
1830 {
1831         uiBlock *block, *nextblock;
1832
1833         for(block=lb->first; block; block=nextblock) {
1834                 nextblock= block->next;
1835         
1836                 if(!block->handle) {
1837                         if(!block->active) {
1838                                 BLI_remlink(lb, block);
1839                                 uiFreeBlock(C, block);
1840                         }
1841                         else
1842                                 block->active= 0;
1843                 }
1844         }
1845 }
1846
1847 void uiBlockSetRegion(uiBlock *block, ARegion *region)
1848 {
1849         ListBase *lb= &region->uiblocks;
1850         uiBlock *oldblock= NULL;
1851
1852         /* each listbase only has one block with this name, free block
1853          * if is already there so it can be rebuilt from scratch */
1854         if(lb) {
1855                 oldblock= BLI_findstring(lb, block->name, offsetof(uiBlock, name));
1856
1857                 if (oldblock) {
1858                         oldblock->active= 0;
1859                         oldblock->panel= NULL;
1860                 }
1861
1862                 /* at the beginning of the list! for dynamical menus/blocks */
1863                 BLI_addhead(lb, block);
1864         }
1865
1866         block->oldblock= oldblock;
1867 }
1868
1869 uiBlock *uiBeginBlock(const bContext *C, ARegion *region, const char *name, short dt)
1870 {
1871         uiBlock *block;
1872         wmWindow *window;
1873         Scene *scn;
1874         int getsizex, getsizey;
1875
1876         window= CTX_wm_window(C);
1877         scn = CTX_data_scene(C);
1878
1879         block= MEM_callocN(sizeof(uiBlock), "uiBlock");
1880         block->active= 1;
1881         block->dt= dt;
1882         block->evil_C= (void*)C; // XXX
1883         if (scn) block->color_profile= (scn->r.color_mgt_flag & R_COLOR_MANAGEMENT);
1884         BLI_strncpy(block->name, name, sizeof(block->name));
1885
1886         if(region)
1887                 uiBlockSetRegion(block, region);
1888
1889         /* window matrix and aspect */
1890         if(region && region->swinid) {
1891                 wm_subwindow_getmatrix(window, region->swinid, block->winmat);
1892                 wm_subwindow_getsize(window, region->swinid, &getsizex, &getsizey);
1893
1894                 /* TODO - investigate why block->winmat[0][0] is negative
1895                  * in the image view when viewRedrawForce is called */
1896                 block->aspect= 2.0/fabs( (getsizex)*block->winmat[0][0]);
1897         }
1898         else {
1899                 /* no subwindow created yet, for menus for example, so we
1900                  * use the main window instead, since buttons are created
1901                  * there anyway */
1902                 wm_subwindow_getmatrix(window, window->screen->mainwin, block->winmat);
1903                 wm_subwindow_getsize(window, window->screen->mainwin, &getsizex, &getsizey);
1904
1905                 block->aspect= 2.0/fabs(getsizex*block->winmat[0][0]);
1906                 block->auto_open= 2;
1907                 block->flag |= UI_BLOCK_LOOP; /* tag as menu */
1908         }
1909
1910         return block;
1911 }
1912
1913 uiBlock *uiGetBlock(const char *name, ARegion *ar)
1914 {
1915         return BLI_findstring(&ar->uiblocks, name, offsetof(uiBlock, name));
1916 }
1917
1918 void uiBlockSetEmboss(uiBlock *block, char dt)
1919 {
1920         block->dt= dt;
1921 }
1922
1923 void ui_check_but(uiBut *but)
1924 {
1925         /* if something changed in the button */
1926         double value;
1927 //      float okwidth; // UNUSED
1928 //      int transopts= ui_translate_buttons();
1929         
1930         ui_is_but_sel(but);
1931         
1932 //      if(but->type==TEX || but->type==IDPOIN) transopts= 0;
1933
1934         /* only update soft range while not editing */
1935         if(but->rnaprop && !(but->editval || but->editstr || but->editvec))
1936                 ui_set_but_soft_range(but, ui_get_but_val(but));
1937
1938         /* test for min and max, icon sliders, etc */
1939         switch( but->type ) {
1940                 case NUM:
1941                 case SLI:
1942                 case SCROLL:
1943                 case NUMSLI:
1944                 case HSVSLI:
1945                         value= ui_get_but_val(but);
1946                         if(value < (double)but->hardmin) ui_set_but_val(but, but->hardmin);
1947                         else if(value > (double)but->hardmax) ui_set_but_val(but, but->hardmax);
1948                         break;
1949                         
1950                 case NUMABS:
1951                         value= fabs( ui_get_but_val(but) );
1952                         if(value < (double)but->hardmin) ui_set_but_val(but, but->hardmin);
1953                         else if(value > (double)but->hardmax) ui_set_but_val(but, but->hardmax);
1954                         break;
1955                         
1956                 case ICONTOG: 
1957                 case ICONTOGN:
1958                         if(!but->rnaprop || (RNA_property_flag(but->rnaprop) & PROP_ICONS_CONSECUTIVE)) {
1959                                 if(but->flag & UI_SELECT) but->iconadd= 1;
1960                                 else but->iconadd= 0;
1961                         }
1962                         break;
1963                         
1964                 case ICONROW:
1965                         if(!but->rnaprop || (RNA_property_flag(but->rnaprop) & PROP_ICONS_CONSECUTIVE)) {
1966                                 value= ui_get_but_val(but);
1967                                 but->iconadd= (int)value- (int)(but->hardmin);
1968                         }
1969                         break;
1970                         
1971                 case ICONTEXTROW:
1972                         if(!but->rnaprop || (RNA_property_flag(but->rnaprop) & PROP_ICONS_CONSECUTIVE)) {
1973                                 value= ui_get_but_val(but);
1974                                 but->iconadd= (int)value- (int)(but->hardmin);
1975                         }
1976                         break;
1977         }
1978         
1979         
1980         /* safety is 4 to enable small number buttons (like 'users') */
1981         // okwidth= -4 + (but->x2 - but->x1); // UNUSED
1982         
1983         /* name: */
1984         switch( but->type ) {
1985         
1986         case MENU:
1987         case ICONTEXTROW:
1988                 
1989                 if(but->x2 - but->x1 > 24) {
1990                         value= ui_get_but_val(but);
1991                         ui_set_name_menu(but, (int)value);
1992                 }
1993                 break;
1994         
1995         case NUM:
1996         case NUMSLI:
1997         case HSVSLI:
1998         case NUMABS:
1999
2000                 value= ui_get_but_val(but);
2001
2002                 if(ui_is_but_float(but)) {
2003                         if(value == (double) FLT_MAX) sprintf(but->drawstr, "%sinf", but->str);
2004                         else if(value == (double) -FLT_MAX) sprintf(but->drawstr, "%s-inf", but->str);
2005                         /* support length type buttons */
2006                         else if(ui_is_but_unit(but)) {
2007                                 char new_str[sizeof(but->drawstr)];
2008                                 ui_get_but_string_unit(but, new_str, sizeof(new_str), value, TRUE);
2009                                 BLI_snprintf(but->drawstr, sizeof(but->drawstr), "%s%s", but->str, new_str);
2010                         }
2011                         else {
2012                                 int prec= (int)but->a2;
2013                                 if(prec==0) prec= (but->hardmax < 10.001f) ? 3 : 2;
2014                                 else CLAMP(prec, 1, 7);
2015
2016                                 BLI_snprintf(but->drawstr, sizeof(but->drawstr), "%s%.*f", but->str, prec, value);
2017                         }
2018                 }
2019                 else {
2020                         sprintf(but->drawstr, "%s%d", but->str, (int)value);
2021                 }
2022                         
2023                 if(but->rnaprop) {
2024                         PropertySubType pstype = RNA_property_subtype(but->rnaprop);
2025                         
2026                         if (pstype == PROP_PERCENTAGE)
2027                                 strcat(but->drawstr, "%");
2028                 }
2029                 break;
2030
2031         case LABEL:
2032                 if(ui_is_but_float(but)) {
2033                         int prec= (int)but->a2;
2034                         value= ui_get_but_val(but);
2035                         if(prec==0) prec= 3;
2036                         else CLAMP(prec, 1, 7);
2037
2038                         BLI_snprintf(but->drawstr, sizeof(but->drawstr), "%s%.*f", but->str, prec, value);
2039                 }
2040                 else {
2041                         strncpy(but->drawstr, but->str, UI_MAX_DRAW_STR);
2042                 }
2043                 
2044                 break;
2045
2046         case IDPOIN:
2047         case TEX:
2048         case SEARCH_MENU:
2049                 if(!but->editstr) {
2050                         char str[UI_MAX_DRAW_STR];
2051
2052                         ui_get_but_string(but, str, UI_MAX_DRAW_STR-strlen(but->str));
2053
2054                         BLI_snprintf(but->drawstr, sizeof(but->drawstr), "%s%s", but->str, str);
2055                 }
2056                 break;
2057         
2058         case KEYEVT:
2059                 strncpy(but->drawstr, but->str, UI_MAX_DRAW_STR);
2060                 if (but->flag & UI_SELECT) {
2061                         strcat(but->drawstr, "Press a key");
2062                 } else {
2063                         strcat(but->drawstr, WM_key_event_string((short) ui_get_but_val(but)));
2064                 }
2065                 break;
2066                 
2067         case HOTKEYEVT:
2068                 if (but->flag & UI_SELECT) {
2069                         but->drawstr[0]= '\0';
2070                         
2071                         if(but->modifier_key) {
2072                                 char *str= but->drawstr;
2073                                 
2074                                 if(but->modifier_key & KM_SHIFT)
2075                                         str= strcat(str, "Shift ");
2076                                 if(but->modifier_key & KM_CTRL)
2077                                         str= strcat(str, "Ctrl ");
2078                                 if(but->modifier_key & KM_ALT)
2079                                         str= strcat(str, "Alt ");
2080                                 if(but->modifier_key & KM_OSKEY)
2081                                         str= strcat(str, "Cmd ");
2082
2083                                 (void)str; /* UNUSED */
2084                         }
2085                         else
2086                                 strcat(but->drawstr, "Press a key  ");
2087                 }
2088                 else
2089                         strncpy(but->drawstr, but->str, UI_MAX_DRAW_STR);
2090
2091                 break;
2092                 
2093         case BUT_TOGDUAL:
2094                 /* trying to get the dual-icon to left of text... not very nice */
2095                 if(but->str[0]) {
2096                         strncpy(but->drawstr, "  ", UI_MAX_DRAW_STR);
2097                         strncpy(but->drawstr+2, but->str, UI_MAX_DRAW_STR-2);
2098                 }
2099                 break;
2100
2101         case HSVCUBE:
2102         case HSVCIRCLE:
2103                 break;
2104         default:
2105                 strncpy(but->drawstr, but->str, UI_MAX_DRAW_STR);
2106                 
2107         }
2108
2109         /* if we are doing text editing, this will override the drawstr */
2110         if(but->editstr)
2111                 strncpy(but->drawstr, but->editstr, UI_MAX_DRAW_STR);
2112         
2113         /* text clipping moved to widget drawing code itself */
2114 }
2115
2116
2117 void uiBlockBeginAlign(uiBlock *block)
2118 {
2119         /* if other align was active, end it */
2120         if(block->flag & UI_BUT_ALIGN) uiBlockEndAlign(block);
2121
2122         block->flag |= UI_BUT_ALIGN_DOWN;       
2123         block->alignnr++;
2124
2125         /* buttons declared after this call will get this align nr */ // XXX flag?
2126 }
2127
2128 static int buts_are_horiz(uiBut *but1, uiBut *but2)
2129 {
2130         float dx, dy;
2131         
2132         dx= fabs( but1->x2 - but2->x1);
2133         dy= fabs( but1->y1 - but2->y2);
2134         
2135         if(dx > dy) return 0;
2136         return 1;
2137 }
2138
2139 void uiBlockEndAlign(uiBlock *block)
2140 {
2141         block->flag &= ~UI_BUT_ALIGN;   // all 4 flags
2142 }
2143
2144 int ui_but_can_align(uiBut *but)
2145 {
2146         return !ELEM3(but->type, LABEL, OPTION, OPTIONN);
2147 }
2148
2149 static void ui_block_do_align_but(uiBut *first, int nr)
2150 {
2151         uiBut *prev, *but=NULL, *next;
2152         int flag= 0, cols=0, rows=0;
2153         
2154         /* auto align */
2155
2156         for(but=first; but && but->alignnr == nr; but=but->next) {
2157                 if(but->next && but->next->alignnr == nr) {
2158                         if(buts_are_horiz(but, but->next)) cols++;
2159                         else rows++;
2160                 }
2161         }
2162
2163         /* rows==0: 1 row, cols==0: 1 collumn */
2164         
2165         /* note;  how it uses 'flag' in loop below (either set it, or OR it) is confusing */
2166         for(but=first, prev=NULL; but && but->alignnr == nr; prev=but, but=but->next) {
2167                 next= but->next;
2168                 if(next && next->alignnr != nr)
2169                         next= NULL;
2170
2171                 /* clear old flag */
2172                 but->flag &= ~UI_BUT_ALIGN;
2173                         
2174                 if(flag==0) {   /* first case */
2175                         if(next) {
2176                                 if(buts_are_horiz(but, next)) {
2177                                         if(rows==0)
2178                                                 flag= UI_BUT_ALIGN_RIGHT;
2179                                         else 
2180                                                 flag= UI_BUT_ALIGN_DOWN|UI_BUT_ALIGN_RIGHT;
2181                                 }
2182                                 else {
2183                                         flag= UI_BUT_ALIGN_DOWN;
2184                                 }
2185                         }
2186                 }
2187                 else if(next==NULL) {   /* last case */
2188                         if(prev) {
2189                                 if(buts_are_horiz(prev, but)) {
2190                                         if(rows==0) 
2191                                                 flag= UI_BUT_ALIGN_LEFT;
2192                                         else
2193                                                 flag= UI_BUT_ALIGN_TOP|UI_BUT_ALIGN_LEFT;
2194                                 }
2195                                 else flag= UI_BUT_ALIGN_TOP;
2196                         }
2197                 }
2198                 else if(buts_are_horiz(but, next)) {
2199                         /* check if this is already second row */
2200                         if( prev && buts_are_horiz(prev, but)==0) {
2201                                 flag &= ~UI_BUT_ALIGN_LEFT;
2202                                 flag |= UI_BUT_ALIGN_TOP;
2203                                 /* exception case: bottom row */
2204                                 if(rows>0) {
2205                                         uiBut *bt= but;
2206                                         while(bt && bt->alignnr == nr) {
2207                                                 if(bt->next && bt->next->alignnr == nr && buts_are_horiz(bt, bt->next)==0 ) break; 
2208                                                 bt= bt->next;
2209                                         }
2210                                         if(bt==NULL || bt->alignnr != nr) flag= UI_BUT_ALIGN_TOP|UI_BUT_ALIGN_RIGHT;
2211                                 }
2212                         }
2213                         else flag |= UI_BUT_ALIGN_LEFT;
2214                 }
2215                 else {
2216                         if(cols==0) {
2217                                 flag |= UI_BUT_ALIGN_TOP;
2218                         }
2219                         else {  /* next button switches to new row */
2220                                 
2221                                 if(prev && buts_are_horiz(prev, but))
2222                                         flag |= UI_BUT_ALIGN_LEFT;
2223                                 else {
2224                                         flag &= ~UI_BUT_ALIGN_LEFT;
2225                                         flag |= UI_BUT_ALIGN_TOP;
2226                                 }
2227                                 
2228                                 if( (flag & UI_BUT_ALIGN_TOP)==0) {     /* stil top row */
2229                                         if(prev) {
2230                                                 if(next && buts_are_horiz(but, next))
2231                                                         flag = UI_BUT_ALIGN_DOWN|UI_BUT_ALIGN_LEFT|UI_BUT_ALIGN_RIGHT;
2232                                                 else {
2233                                                         /* last button in top row */
2234                                                         flag = UI_BUT_ALIGN_DOWN|UI_BUT_ALIGN_LEFT;
2235                                                 }
2236                                         }
2237                                         else 
2238                                                 flag |= UI_BUT_ALIGN_DOWN;
2239                                 }
2240                                 else 
2241                                         flag |= UI_BUT_ALIGN_TOP;
2242                         }
2243                 }
2244                 
2245                 but->flag |= flag;
2246                 
2247                 /* merge coordinates */
2248                 if(prev) {
2249                         // simple cases 
2250                         if(rows==0) {
2251                                 but->x1= (prev->x2+but->x1)/2.0f;
2252                                 prev->x2= but->x1;
2253                         }
2254                         else if(cols==0) {
2255                                 but->y2= (prev->y1+but->y2)/2.0f;
2256                                 prev->y1= but->y2;
2257                         }
2258                         else {
2259                                 if(buts_are_horiz(prev, but)) {
2260                                         but->x1= (prev->x2+but->x1)/2.0f;
2261                                         prev->x2= but->x1;
2262                                         /* copy height too */
2263                                         but->y2= prev->y2;
2264                                 }
2265                                 else if(prev->prev && buts_are_horiz(prev->prev, prev)==0) {
2266                                         /* the previous button is a single one in its row */
2267                                         but->y2= (prev->y1+but->y2)/2.0f;
2268                                         prev->y1= but->y2;
2269                                         
2270                                         but->x1= prev->x1;
2271                                         if(next && buts_are_horiz(but, next)==0)
2272                                                 but->x2= prev->x2;
2273                                 }
2274                                 else {
2275                                         /* the previous button is not a single one in its row */
2276                                         but->y2= prev->y1;
2277                                 }
2278                         }
2279                 }
2280         }
2281 }
2282
2283 void ui_block_do_align(uiBlock *block)
2284 {
2285         uiBut *but;
2286         int nr;
2287
2288         /* align buttons with same align nr */
2289         for(but=block->buttons.first; but;) {
2290                 if(but->alignnr) {
2291                         nr= but->alignnr;
2292                         ui_block_do_align_but(but, nr);
2293
2294                         /* skip with same number */
2295                         for(; but && but->alignnr == nr; but=but->next);
2296
2297                         if(!but)
2298                                 break;
2299                 }
2300                 else
2301                         but= but->next;
2302         }
2303 }
2304
2305 /*
2306 ui_def_but is the function that draws many button types
2307
2308 for float buttons:
2309         "a1" Click Step (how much to change the value each click)
2310         "a2" Number of decimal point values to display. 0 defaults to 3 (0.000) 1,2,3, and a maximum of 4,
2311            all greater values will be clamped to 4.
2312
2313 */
2314 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)
2315 {
2316         uiBut *but;
2317         int slen;
2318         
2319         if(type & BUTPOIN) {            /* a pointer is required */
2320                 if(poin==NULL)
2321                         return NULL;
2322         }
2323
2324         but= MEM_callocN(sizeof(uiBut), "uiBut");
2325
2326         but->type= type & BUTTYPE;
2327         but->pointype= type & BUTPOIN;
2328         but->bit= type & BIT;
2329         but->bitnr= type & 31;
2330         but->icon = 0;
2331         but->iconadd=0;
2332
2333         but->retval= retval;
2334
2335         slen= strlen(str);
2336         if(slen >= UI_MAX_NAME_STR-1) {
2337                 but->str= MEM_mallocN(slen+2, "ui_def_but str"); /* why +2 ? */
2338         }
2339         else {
2340                 but->str= but->strdata;
2341         }
2342         memcpy(but->str, str, slen+1);
2343
2344         but->x1= x1; 
2345         but->y1= y1;
2346         but->x2= (x1+x2); 
2347         but->y2= (y1+y2);
2348         
2349         but->poin= poin;
2350         but->hardmin= but->softmin= min; 
2351         but->hardmax= but->softmax= max;
2352         but->a1= a1; 
2353         but->a2= a2;
2354         but->tip= tip;
2355         
2356         but->lock= block->lock;
2357         but->lockstr= block->lockstr;
2358         but->dt= block->dt;
2359
2360         but->aspect= 1.0f; //XXX block->aspect;
2361         but->block= block;              // pointer back, used for frontbuffer status, and picker
2362
2363         if((block->flag & UI_BUT_ALIGN) && ui_but_can_align(but))
2364                 but->alignnr= block->alignnr;
2365
2366         but->func= block->func;
2367         but->func_arg1= block->func_arg1;
2368         but->func_arg2= block->func_arg2;
2369
2370         but->funcN= block->funcN;
2371         if(block->func_argN)
2372                 but->func_argN= MEM_dupallocN(block->func_argN);
2373         
2374         but->pos= -1;   /* cursor invisible */
2375
2376         if(ELEM4(but->type, NUM, NUMABS, NUMSLI, HSVSLI)) {     /* add a space to name */
2377                 slen= strlen(but->str);
2378                 if(slen>0 && slen<UI_MAX_NAME_STR-2) {
2379                         if(but->str[slen-1]!=' ') {
2380                                 but->str[slen]= ' ';
2381                                 but->str[slen+1]= 0;
2382                         }
2383                 }
2384         }
2385
2386         if((block->flag & UI_BLOCK_LOOP) || ELEM8(but->type, MENU, TEX, LABEL, IDPOIN, BLOCK, BUTM, SEARCH_MENU, PROGRESSBAR))
2387                 but->flag |= (UI_TEXT_LEFT|UI_ICON_LEFT);
2388         else if(but->type==BUT_TOGDUAL)
2389                 but->flag |= UI_ICON_LEFT;
2390
2391         but->flag |= (block->flag & UI_BUT_ALIGN);
2392
2393         if (but->lock) {
2394                 if (but->lockstr) {
2395                         but->flag |= UI_BUT_DISABLED;
2396                 }
2397         }
2398
2399         /* keep track of UI_interface.h */
2400         if(ELEM7(but->type, BLOCK, BUT, LABEL, PULLDOWN, ROUNDBOX, LISTBOX, BUTM));
2401         else if(ELEM3(but->type, SCROLL, SEPR, FTPREVIEW));
2402         else if(but->type >= SEARCH_MENU);
2403         else but->flag |= UI_BUT_UNDO;
2404
2405         BLI_addtail(&block->buttons, but);
2406         
2407         if(block->curlayout)
2408                 ui_layout_add_but(block->curlayout, but);
2409
2410         return but;
2411 }
2412
2413 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)
2414 {
2415         uiBut *but;
2416         PropertyRNA *prop;
2417         PropertyType proptype;
2418         int freestr= 0, icon= 0;
2419
2420         prop= RNA_struct_find_property(ptr, propname);
2421
2422         if(prop) {
2423                 proptype= RNA_property_type(prop);
2424
2425                 /* use rna values if parameters are not specified */
2426                 if(!str) {
2427                         if(type == MENU && proptype == PROP_ENUM) {
2428                                 EnumPropertyItem *item;
2429                                 DynStr *dynstr;
2430                                 int i, totitem, value, free;
2431
2432                                 RNA_property_enum_items(block->evil_C, ptr, prop, &item, &totitem, &free);
2433                                 value= RNA_property_enum_get(ptr, prop);
2434
2435                                 dynstr= BLI_dynstr_new();
2436                                 BLI_dynstr_appendf(dynstr, "%s%%t", RNA_property_ui_name(prop));
2437                                 for(i=0; i<totitem; i++) {
2438                                         if(!item[i].identifier[0]) {
2439                                                 if(item[i].name)
2440                                                         BLI_dynstr_appendf(dynstr, "|%s%%l", item[i].name);
2441                                                 else
2442                                                         BLI_dynstr_append(dynstr, "|%l");
2443                                         }
2444                                         else if(item[i].icon)
2445                                                 BLI_dynstr_appendf(dynstr, "|%s %%i%d %%x%d", item[i].name, item[i].icon, item[i].value);
2446                                         else
2447                                                 BLI_dynstr_appendf(dynstr, "|%s %%x%d", item[i].name, item[i].value);
2448
2449                                         if(value == item[i].value) {
2450                                                 icon= item[i].icon;
2451                                                 if(!tip)
2452                                                         tip= item[i].description;
2453                                         }
2454                                 }
2455                                 str= BLI_dynstr_get_cstring(dynstr);
2456                                 BLI_dynstr_free(dynstr);
2457
2458                                 if(free)
2459                                         MEM_freeN(item);
2460
2461                                 freestr= 1;
2462                         }
2463                         else if(ELEM(type, ROW, LISTROW) && proptype == PROP_ENUM) {
2464                                 EnumPropertyItem *item;
2465                                 int i, totitem, free;
2466
2467                                 RNA_property_enum_items(block->evil_C, ptr, prop, &item, &totitem, &free);
2468                                 for(i=0; i<totitem; i++) {
2469                                         if(item[i].identifier[0] && item[i].value == (int)max) {
2470                                                 str= item[i].name;
2471                                                 icon= item[i].icon;
2472                                         }
2473                                 }
2474
2475                                 if(!str)
2476                                         str= RNA_property_ui_name(prop);
2477                                 if(free)
2478                                         MEM_freeN(item);
2479                         }
2480                         else {
2481                                 str= RNA_property_ui_name(prop);
2482                                 icon= RNA_property_ui_icon(prop);
2483                         }
2484                 }
2485                 
2486                 if(!tip && proptype != PROP_ENUM)
2487                         tip= RNA_property_ui_description(prop);
2488
2489                 if(min == max || a1 == -1 || a2 == -1) {
2490                         if(proptype == PROP_INT) {
2491                                 int hardmin, hardmax, softmin, softmax, step;
2492
2493                                 RNA_property_int_range(ptr, prop, &hardmin, &hardmax);
2494                                 RNA_property_int_ui_range(ptr, prop, &softmin, &softmax, &step);
2495
2496                                 if(!ELEM(type, ROW, LISTROW) && min == max) {
2497                                         min= hardmin;
2498                                         max= hardmax;
2499                                 }
2500                                 if(a1 == -1)
2501                                         a1= step;
2502                                 if(a2 == -1)
2503                                         a2= 0;
2504                         }
2505                         else if(proptype == PROP_FLOAT) {
2506                                 float hardmin, hardmax, softmin, softmax, step, precision;
2507
2508                                 RNA_property_float_range(ptr, prop, &hardmin, &hardmax);
2509                                 RNA_property_float_ui_range(ptr, prop, &softmin, &softmax, &step, &precision);
2510
2511                                 if(!ELEM(type, ROW, LISTROW) && min == max) {
2512                                         min= hardmin;
2513                                         max= hardmax;
2514                                 }
2515                                 if(a1 == -1)
2516                                         a1= step;
2517                                 if(a2 == -1)
2518                                         a2= precision;
2519                         }
2520                         else if(proptype == PROP_STRING) {
2521                                 min= 0;
2522                                 max= RNA_property_string_maxlength(prop);
2523                                 if(max == 0) /* interface code should ideally support unlimited length */
2524                                         max= UI_MAX_DRAW_STR; 
2525                         }
2526                 }
2527         }
2528         else {
2529                 RNA_warning("ui_def_but_rna: property not found: %s.%s\n", RNA_struct_identifier(ptr->type), propname);
2530                 str= propname;
2531         }
2532
2533         /* now create button */
2534         but= ui_def_but(block, type, retval, str, x1, y1, x2, y2, NULL, min, max, a1, a2, tip);
2535
2536         if(prop) {
2537                 but->rnapoin= *ptr;
2538                 but->rnaprop= prop;
2539
2540                 if(RNA_property_array_length(&but->rnapoin, but->rnaprop))
2541                         but->rnaindex= index;
2542                 else
2543                         but->rnaindex= 0;
2544         }
2545
2546         if(icon) {
2547                 but->icon= (BIFIconID)icon;
2548                 but->flag |= UI_HAS_ICON;
2549                 but->flag|= UI_ICON_LEFT;
2550         }
2551         
2552         if (!prop || !RNA_property_editable(&but->rnapoin, prop)) {
2553                 but->flag |= UI_BUT_DISABLED;
2554                 but->lock = 1;
2555                 but->lockstr = "";
2556         }
2557
2558         /* If this button uses units, calculate the step from this */
2559         if(ui_is_but_unit(but))
2560                 but->a1= ui_get_but_step_unit(but, but->a1);
2561
2562         if(freestr)
2563                 MEM_freeN((void *)str);
2564         
2565         return but;
2566 }
2567
2568 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)
2569 {
2570         uiBut *but;
2571         wmOperatorType *ot;
2572         
2573         ot= WM_operatortype_find(opname, 0);
2574
2575         if(!str) {
2576                 if(ot) str= ot->name;
2577                 else str= opname;
2578         }
2579         
2580         if ((!tip || tip[0]=='\0') && ot && ot->description) {
2581                 tip= ot->description;
2582         }
2583
2584         but= ui_def_but(block, type, -1, str, x1, y1, x2, y2, NULL, 0, 0, 0, 0, tip);
2585         but->optype= ot;
2586         but->opcontext= opcontext;
2587
2588         if(!ot) {
2589                 but->flag |= UI_BUT_DISABLED;
2590                 but->lock = 1;
2591                 but->lockstr = "";
2592         }
2593
2594         return but;
2595 }
2596
2597 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)
2598 {
2599         uiBut *but;
2600         wmOperatorType *ot;
2601         
2602         ot= WM_operatortype_find(opname, 0);
2603
2604         if(!str) {
2605                 if(ot) str= ot->name;
2606                 else str= opname;
2607         }
2608         
2609         if ((!tip || tip[0]=='\0') && ot && ot->description) {
2610                 tip= ot->description;
2611         }
2612
2613         but= ui_def_but(block, type, -1, str, x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2614         but->optype= ot;
2615         but->opcontext= opcontext;
2616
2617         if(!ot) {
2618                 but->flag |= UI_BUT_DISABLED;
2619                 but->lock = 1;
2620                 but->lockstr = "";
2621         }
2622
2623         return but;
2624 }
2625
2626 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)
2627 {
2628         uiBut *but= ui_def_but(block, type, retval, str, x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2629
2630         ui_check_but(but);
2631         
2632         return but;
2633 }
2634
2635         /* if _x_ is a power of two (only one bit) return the power,
2636          * otherwise return -1. 
2637          * (1<<findBitIndex(x))==x for powers of two.
2638          */
2639 static int findBitIndex(unsigned int x) {
2640         if (!x || (x&(x-1))!=0) {       /* x&(x-1) strips lowest bit */
2641                 return -1;
2642         } else {
2643                 int idx= 0;
2644
2645                 if (x&0xFFFF0000)       idx+=16, x>>=16;
2646                 if (x&0xFF00)           idx+=8, x>>=8;
2647                 if (x&0xF0)                     idx+=4, x>>=4;
2648                 if (x&0xC)                      idx+=2, x>>=2;
2649                 if (x&0x2)                      idx+=1;
2650
2651                 return idx;
2652         }
2653 }
2654
2655 /* autocomplete helper functions */
2656 struct AutoComplete {
2657         int maxlen;
2658         char *truncate;
2659         const char *startname;
2660 };
2661
2662 AutoComplete *autocomplete_begin(const char *startname, int maxlen)
2663 {
2664         AutoComplete *autocpl;
2665         
2666         autocpl= MEM_callocN(sizeof(AutoComplete), "AutoComplete");
2667         autocpl->maxlen= maxlen;
2668         autocpl->truncate= MEM_callocN(sizeof(char)*maxlen, "AutoCompleteTruncate");
2669         autocpl->startname= startname;
2670
2671         return autocpl;
2672 }
2673
2674 void autocomplete_do_name(AutoComplete *autocpl, const char *name)
2675 {
2676         char *truncate= autocpl->truncate;
2677         const char *startname= autocpl->startname;
2678         int a;
2679
2680         for(a=0; a<autocpl->maxlen-1; a++) {
2681                 if(startname[a]==0 || startname[a]!=name[a])
2682                         break;
2683         }
2684         /* found a match */
2685         if(startname[a]==0) {
2686                 /* first match */
2687                 if(truncate[0]==0)
2688                         BLI_strncpy(truncate, name, autocpl->maxlen);
2689                 else {
2690                         /* remove from truncate what is not in bone->name */
2691                         for(a=0; a<autocpl->maxlen-1; a++) {
2692                                 if(name[a] == 0) {
2693                                         truncate[a]= 0;
2694                                         break;
2695                                 }
2696                                 else if(truncate[a]!=name[a])
2697                                         truncate[a]= 0;
2698                         }
2699                 }
2700         }
2701 }
2702
2703 void autocomplete_end(AutoComplete *autocpl, char *autoname)
2704 {       
2705         if(autocpl->truncate[0])
2706                 BLI_strncpy(autoname, autocpl->truncate, autocpl->maxlen);
2707         else {
2708                 if (autoname != autocpl->startname) /* dont copy a string over its self */
2709                         BLI_strncpy(autoname, autocpl->startname, autocpl->maxlen);
2710         }
2711         MEM_freeN(autocpl->truncate);
2712         MEM_freeN(autocpl);
2713 }
2714
2715 /* autocomplete callback for ID buttons */
2716 static void autocomplete_id(bContext *C, char *str, void *arg_v)
2717 {
2718         int blocktype= (intptr_t)arg_v;
2719         ListBase *listb= which_libbase(CTX_data_main(C), blocktype);
2720         
2721         if(listb==NULL) return;
2722         
2723         /* search if str matches the beginning of an ID struct */
2724         if(str[0]) {
2725                 AutoComplete *autocpl= autocomplete_begin(str, 22);
2726                 ID *id;
2727                 
2728                 for(id= listb->first; id; id= id->next)
2729                         autocomplete_do_name(autocpl, id->name+2);
2730
2731                 autocomplete_end(autocpl, str);
2732         }
2733 }
2734
2735 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)
2736 {
2737         int bitIdx= findBitIndex(bit);
2738         if (bitIdx==-1) {
2739                 return NULL;
2740         } else {
2741                 return uiDefBut(block, type|BIT|bitIdx, retval, str, x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2742         }
2743 }
2744 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)
2745 {
2746         return uiDefBut(block, type|FLO, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2747 }
2748 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)
2749 {
2750         return uiDefButBit(block, type|FLO, bit, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2751 }
2752 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)
2753 {
2754         return uiDefBut(block, type|INT, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2755 }
2756 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)
2757 {
2758         return uiDefButBit(block, type|INT, bit, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2759 }
2760 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)
2761 {
2762         return uiDefBut(block, type|SHO, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2763 }
2764 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)
2765 {
2766         return uiDefButBit(block, type|SHO, bit, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2767 }
2768 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)
2769 {
2770         return uiDefBut(block, type|CHA, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2771 }
2772 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)
2773 {
2774         return uiDefButBit(block, type|CHA, bit, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2775 }
2776 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)
2777 {
2778         uiBut *but;
2779
2780         but= ui_def_but_rna(block, type, retval, str, x1, y1, x2, y2, ptr, propname, index, min, max, a1, a2, tip);
2781         if(but)
2782                 ui_check_but(but);
2783
2784         return but;
2785 }
2786 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)
2787 {
2788         uiBut *but;
2789
2790         but= ui_def_but_operator(block, type, opname, opcontext, str, x1, y1, x2, y2, tip);
2791         if(but)
2792                 ui_check_but(but);
2793
2794         return but;
2795 }
2796
2797 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)
2798 {
2799         uiBut *but= ui_def_but_operator_text(block, type, opname, opcontext, str, x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2800
2801         if(but)
2802                 ui_check_but(but);
2803         
2804         return but;
2805 }
2806
2807 /* if a1==1.0 then a2 is an extra icon blending factor (alpha 0.0 - 1.0) */
2808 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)
2809 {
2810         uiBut *but= ui_def_but(block, type, retval, "", x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2811         
2812         but->icon= (BIFIconID) icon;
2813         but->flag|= UI_HAS_ICON;
2814
2815         ui_check_but(but);
2816         
2817         return but;
2818 }
2819 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)
2820 {
2821         int bitIdx= findBitIndex(bit);
2822         if (bitIdx==-1) {
2823                 return NULL;
2824         } else {
2825                 return uiDefIconBut(block, type|BIT|bitIdx, retval, icon, x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2826         }
2827 }
2828
2829 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)
2830 {
2831         return uiDefIconBut(block, type|FLO, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2832 }
2833 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)
2834 {
2835         return uiDefIconButBit(block, type|FLO, bit, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2836 }
2837 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)
2838 {
2839         return uiDefIconBut(block, type|INT, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2840 }
2841 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)
2842 {
2843         return uiDefIconButBit(block, type|INT, bit, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2844 }
2845 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)
2846 {
2847         return uiDefIconBut(block, type|SHO, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2848 }
2849 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)
2850 {
2851         return uiDefIconButBit(block, type|SHO, bit, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2852 }
2853 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)
2854 {
2855         return uiDefIconBut(block, type|CHA, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2856 }
2857 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)
2858 {
2859         return uiDefIconButBit(block, type|CHA, bit, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2860 }
2861 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)
2862 {
2863         uiBut *but;
2864
2865         but= ui_def_but_rna(block, type, retval, "", x1, y1, x2, y2, ptr, propname, index, min, max, a1, a2, tip);
2866         if(but) {
2867                 if(icon) {
2868                         but->icon= (BIFIconID) icon;
2869                         but->flag|= UI_HAS_ICON;
2870                 }
2871                 ui_check_but(but);
2872         }
2873
2874         return but;
2875 }
2876 uiBut *uiDefIconButO(uiBlock *block, int type, const char *opname, int opcontext, int icon, int x1, int y1, short x2, short y2, const char *tip)
2877 {
2878         uiBut *but;
2879
2880         but= ui_def_but_operator(block, type, opname, opcontext, "", x1, y1, x2, y2, tip);
2881         if(but) {
2882                 but->icon= (BIFIconID) icon;
2883                 but->flag|= UI_HAS_ICON;
2884                 ui_check_but(but);
2885         }
2886
2887         return but;
2888 }
2889
2890 /* Button containing both string label and icon */
2891 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)
2892 {
2893         uiBut *but= ui_def_but(block, type, retval, str, x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2894
2895         but->icon= (BIFIconID) icon;
2896         but->flag|= UI_HAS_ICON;
2897
2898         but->flag|= UI_ICON_LEFT;
2899
2900         ui_check_but(but);
2901
2902         return but;
2903 }
2904 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)
2905 {
2906         int bitIdx= findBitIndex(bit);
2907         if (bitIdx==-1) {
2908                 return NULL;
2909         } else {
2910                 return uiDefIconTextBut(block, type|BIT|bitIdx, retval, icon, str, x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2911         }
2912 }
2913
2914 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)
2915 {
2916         return uiDefIconTextBut(block, type|FLO, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2917 }
2918 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)
2919 {
2920         return uiDefIconTextButBit(block, type|FLO, bit, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2921 }
2922 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)
2923 {
2924         return uiDefIconTextBut(block, type|INT, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2925 }
2926 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)
2927 {
2928         return uiDefIconTextButBit(block, type|INT, bit, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2929 }
2930 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)
2931 {
2932         return uiDefIconTextBut(block, type|SHO, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2933 }
2934 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)
2935 {
2936         return uiDefIconTextButBit(block, type|SHO, bit, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2937 }
2938 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)
2939 {
2940         return uiDefIconTextBut(block, type|CHA, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2941 }
2942 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)
2943 {
2944         return uiDefIconTextButBit(block, type|CHA, bit, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2945 }
2946 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)
2947 {
2948         uiBut *but;
2949
2950         but= ui_def_but_rna(block, type, retval, str, x1, y1, x2, y2, ptr, propname, index, min, max, a1, a2, tip);
2951         if(but) {
2952                 if(icon) {
2953                         but->icon= (BIFIconID) icon;
2954                         but->flag|= UI_HAS_ICON;
2955                 }
2956                 but->flag|= UI_ICON_LEFT;
2957                 ui_check_but(but);
2958         }
2959
2960         return but;
2961 }
2962 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)
2963 {
2964         uiBut *but;
2965
2966         but= ui_def_but_operator(block, type, opname, opcontext, str, x1, y1, x2, y2, tip);
2967         if(but) {
2968                 but->icon= (BIFIconID) icon;
2969                 but->flag|= UI_HAS_ICON;
2970                 but->flag|= UI_ICON_LEFT;
2971                 ui_check_but(but);
2972         }
2973
2974         return but;
2975 }
2976
2977 /* END Button containing both string label and icon */
2978
2979 void uiSetButLink(uiBut *but, void **poin, void ***ppoin, short *tot, int from, int to)
2980 {
2981         uiLink *link;
2982         
2983         link= but->link= MEM_callocN(sizeof(uiLink), "new uilink");
2984         
2985         link->poin= poin;
2986         link->ppoin= ppoin;
2987         link->totlink= tot;
2988         link->fromcode= from;
2989         link->tocode= to;
2990 }
2991
2992 /* cruft to make uiBlock and uiBut private */
2993
2994 int uiBlocksGetYMin(ListBase *lb)
2995 {
2996         uiBlock *block;
2997         int min= 0;
2998         
2999         for (block= lb->first; block; block= block->next)
3000                 if (block==lb->first || block->miny<min)
3001                         min= block->miny;
3002                         
3003         return min;
3004 }
3005
3006 void uiBlockSetDirection(uiBlock *block, int direction)
3007 {
3008         block->direction= direction;
3009 }
3010
3011 /* this call escapes if there's alignment flags */
3012 void uiBlockFlipOrder(uiBlock *block)
3013 {
3014         ListBase lb;
3015         uiBut *but, *next;
3016         float centy, miny=10000, maxy= -10000;
3017
3018         if(U.uiflag & USER_MENUFIXEDORDER)
3019                 return;
3020         else if(block->flag & UI_BLOCK_NO_FLIP)
3021                 return;
3022         
3023         for(but= block->buttons.first; but; but= but->next) {
3024                 if(but->flag & UI_BUT_ALIGN) return;
3025                 if(but->y1 < miny) miny= but->y1;
3026                 if(but->y2 > maxy) maxy= but->y2;
3027         }
3028         /* mirror trick */
3029         centy= (miny+maxy)/2.0f;
3030         for(but= block->buttons.first; but; but= but->next) {
3031                 but->y1 = centy-(but->y1-centy);
3032                 but->y2 = centy-(but->y2-centy);
3033                 SWAP(float, but->y1, but->y2);
3034         }
3035         
3036         /* also flip order in block itself, for example for arrowkey */
3037         lb.first= lb.last= NULL;
3038         but= block->buttons.first;
3039         while(but) {
3040                 next= but->next;
3041                 BLI_remlink(&block->buttons, but);
3042                 BLI_addtail(&lb, but);
3043                 but= next;
3044         }
3045         block->buttons= lb;
3046 }
3047
3048
3049 void uiBlockSetFlag(uiBlock *block, int flag)
3050 {
3051         block->flag|= flag;
3052 }
3053
3054 void uiBlockClearFlag(uiBlock *block, int flag)
3055 {
3056         block->flag&= ~flag;
3057 }
3058
3059 void uiBlockSetXOfs(uiBlock *block, int xofs)
3060 {
3061         block->xofs= xofs;
3062 }
3063
3064 void uiButSetFlag(uiBut *but, int flag)
3065 {
3066         but->flag|= flag;
3067 }
3068
3069 void uiButClearFlag(uiBut *but, int flag)
3070 {
3071         but->flag&= ~flag;
3072 }
3073
3074 int uiButGetRetVal(uiBut *but)
3075 {
3076         return but->retval;
3077 }
3078
3079 void uiButSetDragID(uiBut *but, ID *id)
3080 {
3081         but->dragtype= WM_DRAG_ID;
3082         but->dragpoin= (void *)id;
3083 }
3084
3085 void uiButSetDragRNA(uiBut *but, PointerRNA *ptr)
3086 {
3087         but->dragtype= WM_DRAG_RNA;
3088         but->dragpoin= (void *)ptr;
3089 }
3090
3091 void uiButSetDragPath(uiBut *but, const char *path)
3092 {
3093         but->dragtype= WM_DRAG_PATH;
3094         but->dragpoin= (void *)path;
3095 }
3096
3097 void uiButSetDragName(uiBut *but, const char *name)
3098 {
3099         but->dragtype= WM_DRAG_NAME;
3100         but->dragpoin= (void *)name;
3101 }
3102
3103 /* value from button itself */
3104 void uiButSetDragValue(uiBut *but)
3105 {
3106         but->dragtype= WM_DRAG_VALUE;
3107 }
3108
3109 void uiButSetDragImage(uiBut *but, const char *path, int icon, struct ImBuf *imb, float scale)
3110 {
3111         but->dragtype= WM_DRAG_PATH;
3112         but->icon= icon; /* no flag UI_HAS_ICON, so icon doesnt draw in button */
3113         but->dragpoin= (void *)path;
3114         but->imb= imb;
3115         but->imb_scale= scale;
3116 }
3117
3118 PointerRNA *uiButGetOperatorPtrRNA(uiBut *but)
3119 {
3120         if(but->optype && !but->opptr) {
3121                 but->opptr= MEM_callocN(sizeof(PointerRNA), "uiButOpPtr");
3122                 WM_operator_properties_create_ptr(but->opptr, but->optype);
3123         }
3124
3125         return but->opptr;
3126 }
3127
3128 void uiButSetUnitType(uiBut *but, const int unit_type)
3129 {
3130         but->unit_type= (unsigned char)(unit_type>>16);
3131 }
3132
3133 int uiButGetUnitType(uiBut *but)
3134 {
3135         if(but->rnaprop) {
3136                 return RNA_SUBTYPE_UNIT(RNA_property_subtype(but->rnaprop));
3137         }
3138         else {
3139                 return ((int)but->unit_type)<<16;
3140         }
3141 }
3142
3143 void uiBlockSetHandleFunc(uiBlock *block, uiBlockHandleFunc func, void *arg)
3144 {
3145         block->handle_func= func;
3146         block->handle_func_arg= arg;
3147 }
3148
3149 void uiBlockSetButmFunc(uiBlock *block, uiMenuHandleFunc func, void *arg)
3150 {
3151         block->butm_func= func;
3152         block->butm_func_arg= arg;
3153 }
3154
3155 void uiBlockSetFunc(uiBlock *block, uiButHandleFunc func, void *arg1, void *arg2)
3156 {