RNA
[blender.git] / source / blender / editors / interface / interface.c
1 /**
2  * $Id: interface.c 16882 2008-10-02 12:29:45Z ton $
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): none yet.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  */
29
30 #include <float.h>
31 #include <math.h>
32 #include <string.h>
33  
34 #include "MEM_guardedalloc.h"
35
36 #include "DNA_ID.h"
37 #include "DNA_listBase.h"
38 #include "DNA_screen_types.h"
39 #include "DNA_texture_types.h"
40 #include "DNA_userdef_types.h"
41
42 #include "BLI_arithb.h"
43 #include "BLI_blenlib.h"
44 #include "BLI_dynstr.h"
45
46 #include "BKE_global.h"
47 #include "BKE_screen.h"
48 #include "BKE_texture.h"
49 #include "BKE_utildefines.h"
50
51 #include "BIF_gl.h"
52 #include "BIF_glutil.h"
53
54 #include "UI_interface.h"
55 #include "UI_text.h"
56
57 #include "BMF_Api.h"
58 #ifdef INTERNATIONAL
59 #include "FTF_Api.h"
60 #endif
61
62 #include "ED_screen.h"
63
64 #include "WM_api.h"
65 #include "WM_types.h"
66 #include "wm_subwindow.h"
67 #include "wm_window.h"
68
69 #include "RNA_access.h"
70 #include "RNA_types.h"
71
72 #include "interface.h"
73
74 /* 
75  * a full doc with API notes can be found in bf-blender/blender/doc/interface_API.txt
76  * 
77  * uiBlahBlah()         external function
78  * ui_blah_blah()       internal function
79  */
80
81 static void ui_free_but(uiBut *but);
82
83 /* ************ GLOBALS ************* */
84
85 static uiFont UIfont[UI_ARRAY];  // no init needed
86
87 /* ************* translation ************** */
88
89 /* XXX 2.50 missing from context */
90 int ui_translate_buttons()
91 {
92         return (U.transopts & USER_TR_BUTTONS);
93 }
94
95 int ui_translate_menus()
96 {
97         return (U.transopts & USER_TR_MENUS);
98 }
99
100 int ui_translate_tooltips()
101 {
102         return (U.transopts & USER_TR_TOOLTIPS);
103 }
104
105 /* ************* window matrix ************** */
106
107 void ui_block_to_window_fl(const ARegion *ar, uiBlock *block, float *x, float *y)
108 {
109         float gx, gy;
110         int sx, sy, getsizex, getsizey;
111
112         getsizex= ar->winrct.xmax-ar->winrct.xmin;
113         getsizey= ar->winrct.ymax-ar->winrct.ymin;
114         sx= ar->winrct.xmin;
115         sy= ar->winrct.ymin;
116
117         gx= *x;
118         gy= *y;
119         *x= ((float)sx) + ((float)getsizex)*(0.5+ 0.5*(gx*block->winmat[0][0]+ gy*block->winmat[1][0]+ block->winmat[3][0]));
120         *y= ((float)sy) + ((float)getsizey)*(0.5+ 0.5*(gx*block->winmat[0][1]+ gy*block->winmat[1][1]+ block->winmat[3][1]));
121 }
122
123 void ui_block_to_window(const ARegion *ar, uiBlock *block, int *x, int *y)
124 {
125         float fx, fy;
126
127         fx= *x;
128         fy= *y;
129
130         ui_block_to_window_fl(ar, block, &fx, &fy);
131
132         *x= (int)(fx+0.5f);
133         *y= (int)(fy+0.5f);
134 }
135
136 void ui_block_to_window_rct(const ARegion *ar, uiBlock *block, rctf *graph, rcti *winr)
137 {
138         rctf tmpr;
139
140         tmpr= *graph;
141         ui_block_to_window_fl(ar, block, &tmpr.xmin, &tmpr.ymin);
142         ui_block_to_window_fl(ar, block, &tmpr.xmax, &tmpr.ymax);
143
144         winr->xmin= tmpr.xmin;
145         winr->ymin= tmpr.ymin;
146         winr->xmax= tmpr.xmax;
147         winr->ymax= tmpr.ymax;
148 }
149
150 void ui_window_to_block_fl(const ARegion *ar, uiBlock *block, float *x, float *y)       /* for mouse cursor */
151 {
152         float a, b, c, d, e, f, px, py;
153         int sx, sy, getsizex, getsizey;
154
155         getsizex= ar->winrct.xmax-ar->winrct.xmin;
156         getsizey= ar->winrct.ymax-ar->winrct.ymin;
157         sx= ar->winrct.xmin;
158         sy= ar->winrct.ymin;
159
160         a= .5*((float)getsizex)*block->winmat[0][0];
161         b= .5*((float)getsizex)*block->winmat[1][0];
162         c= .5*((float)getsizex)*(1.0+block->winmat[3][0]);
163
164         d= .5*((float)getsizey)*block->winmat[0][1];
165         e= .5*((float)getsizey)*block->winmat[1][1];
166         f= .5*((float)getsizey)*(1.0+block->winmat[3][1]);
167
168         px= *x - sx;
169         py= *y - sy;
170
171         *y=  (a*(py-f) + d*(c-px))/(a*e-d*b);
172         *x= (px- b*(*y)- c)/a;
173 }
174
175 void ui_window_to_block(const ARegion *ar, uiBlock *block, int *x, int *y)
176 {
177         float fx, fy;
178
179         fx= *x;
180         fy= *y;
181
182         ui_window_to_block_fl(ar, block, &fx, &fy);
183
184         *x= (int)(fx+0.5f);
185         *y= (int)(fy+0.5f);
186 }
187
188 /* ******************* block calc ************************* */
189
190 /* only for pulldowns */
191 void uiTextBoundsBlock(uiBlock *block, int addval)
192 {
193         uiBut *bt;
194         int i = 0, j, x1addval= 0, nextcol;
195         
196         bt= block->buttons.first;
197         while(bt) {
198                 if(bt->type!=SEPR) {
199                         int transopts= ui_translate_buttons();
200                         if(bt->type==TEX || bt->type==IDPOIN) transopts= 0;
201                         j= UI_GetStringWidth(bt->font, bt->drawstr, transopts);
202
203                         if(j > i) i = j;
204                 }
205                 bt= bt->next;
206         }
207
208         /* cope with multi collumns */
209         bt= block->buttons.first;
210         while(bt) {
211                 if(bt->next && bt->x1 < bt->next->x1)
212                         nextcol= 1;
213                 else nextcol= 0;
214                 
215                 bt->x1 = x1addval;
216                 bt->x2 = bt->x1 + i + addval;
217                 
218                 ui_check_but(bt);       // clips text again
219                 
220                 if(nextcol)
221                         x1addval+= i + addval;
222                 
223                 bt= bt->next;
224         }
225 }
226
227 void uiBoundsBlock(uiBlock *block, int addval)
228 {
229         uiBut *bt;
230         int xof;
231         
232         if(block==NULL)
233                 return;
234         
235         if(block->buttons.first==NULL) {
236                 if(block->panel) {
237                         block->minx= 0.0; block->maxx= block->panel->sizex;
238                         block->miny= 0.0; block->maxy= block->panel->sizey;
239                 }
240         }
241         else {
242         
243                 block->minx= block->miny= 10000;
244                 block->maxx= block->maxy= -10000;
245                 
246                 bt= block->buttons.first;
247                 while(bt) {
248                         if(bt->x1 < block->minx) block->minx= bt->x1;
249                         if(bt->y1 < block->miny) block->miny= bt->y1;
250         
251                         if(bt->x2 > block->maxx) block->maxx= bt->x2;
252                         if(bt->y2 > block->maxy) block->maxy= bt->y2;
253                         
254                         bt= bt->next;
255                 }
256                 
257                 block->minx -= addval;
258                 block->miny -= addval;
259                 block->maxx += addval;
260                 block->maxy += addval;
261         }
262
263         /* hardcoded exception... but that one is annoying with larger safety */ 
264         bt= block->buttons.first;
265         if(bt && strncmp(bt->str, "ERROR", 5)==0) xof= 10;
266         else xof= 40;
267         
268         block->safety.xmin= block->minx-xof;
269         block->safety.ymin= block->miny-xof;
270         block->safety.xmax= block->maxx+xof;
271         block->safety.ymax= block->maxy+xof;
272 }
273
274 void uiBlockTranslate(uiBlock *block, int x, int y)
275 {
276         uiBut *bt;
277
278         for(bt= block->buttons.first; bt; bt=bt->next) {
279                 bt->x1 += x;
280                 bt->y1 += y;
281                 bt->x2 += x;
282                 bt->y2 += y;
283         }
284
285         block->minx += x;
286         block->miny += y;
287         block->maxx += x;
288         block->maxy += y;
289 }
290
291 void uiBlockOrigin(uiBlock *block)
292 {
293         uiBut *bt;
294         int minx= 10000, miny= 10000;
295
296         for(bt= block->buttons.first; bt; bt=bt->next) {
297                 if(bt->x1 < minx) minx= bt->x1;
298                 if(bt->y1 < miny) miny= bt->y1;
299         }
300
301         uiBlockTranslate(block, -minx, -miny);
302 }
303
304 void ui_autofill(uiBlock *block)
305 {
306         uiBut *but;
307         float *maxw, *maxh, startx = 0, starty, height = 0;
308         float totmaxh;
309         int rows=0, /*  cols=0, */ i, lasti;
310         
311         /* first count rows */
312         but= block->buttons.last;
313         rows= but->x1+1;
314
315         /* calculate max width / height for each row */
316         maxw= MEM_callocN(sizeof(float)*rows, "maxw");
317         maxh= MEM_callocN(sizeof(float)*rows, "maxh");
318         but= block->buttons.first;
319         while(but) {
320                 i= but->x1;
321                 if( maxh[i] < but->y2) maxh[i]= but->y2;
322                 maxw[i] += but->x2;
323                 but= but->next;
324         }
325         
326         totmaxh= 0.0;
327         for(i=0; i<rows; i++) totmaxh+= maxh[i];
328         
329         /* apply widths/heights */
330         starty= block->maxy;
331         but= block->buttons.first;
332         lasti= -1;
333         while(but) {
334                 // signal for aligning code
335                 but->flag |= UI_BUT_ALIGN_DOWN;
336                 
337                 i= but->x1;
338
339                 if(i!=lasti) {
340                         startx= block->minx;
341                         height= (maxh[i]*(block->maxy-block->miny))/totmaxh;
342                         starty-= height;
343                         lasti= i;
344                 }
345                 
346                 but->y1= starty+but->aspect;
347                 but->y2= but->y1+height-but->aspect;
348                 
349                 but->x2= (but->x2*(block->maxx-block->minx))/maxw[i];
350                 but->x1= startx+but->aspect;
351                 
352                 startx+= but->x2;
353                 but->x2+= but->x1-but->aspect;
354                 
355                 ui_check_but(but);
356                 
357                 but= but->next;
358         }
359         
360         uiBlockEndAlign(block);
361         
362         MEM_freeN(maxw); MEM_freeN(maxh);       
363         block->autofill= 0;
364 }
365
366 /* ************** LINK LINE DRAWING  ************* */
367
368 /* link line drawing is not part of buttons or theme.. so we stick with it here */
369
370 static void ui_draw_linkline(uiBut *but, uiLinkLine *line)
371 {
372         float vec1[2], vec2[2];
373
374         if(line->from==NULL || line->to==NULL) return;
375         
376         vec1[0]= (line->from->x1+line->from->x2)/2.0;
377         vec1[1]= (line->from->y1+line->from->y2)/2.0;
378         vec2[0]= (line->to->x1+line->to->x2)/2.0;
379         vec2[1]= (line->to->y1+line->to->y2)/2.0;
380         
381         if(line->flag & UI_SELECT) UI_ThemeColorShade(but->themecol, 80);
382         else glColor3ub(0,0,0);
383         fdrawline(vec1[0], vec1[1], vec2[0], vec2[1]);
384 }
385
386 static void ui_draw_links(uiBlock *block)
387 {
388         uiBut *but;
389         uiLinkLine *line;
390         
391         but= block->buttons.first;
392         while(but) {
393                 if(but->type==LINK && but->link) {
394                         line= but->link->lines.first;
395                         while(line) {
396                                 ui_draw_linkline(but, line);
397                                 line= line->next;
398                         }
399                 }
400                 but= but->next;
401         }       
402 }
403
404 /* ************** BLOCK ENDING FUNCTION ************* */
405
406 static int ui_but_equals_old(uiBut *but, uiBut *oldbut)
407 {
408         /* various properties are being compared here, hopfully sufficient
409          * to catch all cases, but it is simple to add more checks later */
410         if(but->retval != oldbut->retval) return 0;
411         if(but->poin != oldbut->poin || but->pointype != oldbut->pointype) return 0;
412         if(but->rnapoin.data != oldbut->rnapoin.data) return 0;
413         if(but->rnaprop != oldbut->rnaprop)
414         if(but->rnaindex != oldbut->rnaindex) return 0;
415         if(but->func != oldbut->func) return 0;
416         if(but->func_arg1 != oldbut->func_arg1) return 0;
417         if(but->func_arg2 != oldbut->func_arg2) return 0;
418         if(but->func_arg3 != oldbut->func_arg3) return 0;
419
420         return 1;
421 }
422
423 static int ui_but_update_from_old_block(uiBlock *block, uiBut *but)
424 {
425         uiBlock *oldblock;
426         uiBut *oldbut;
427         int found= 0;
428
429         oldblock= block->oldblock;
430         if(!oldblock)
431                 return found;
432
433         for(oldbut=oldblock->buttons.first; oldbut; oldbut=oldbut->next) {
434                 if(ui_but_equals_old(oldbut, but)) {
435                         if(oldbut->activate) {
436                                 but->flag= oldbut->flag;
437                                 but->activate= oldbut->activate;
438                                 but->pos= oldbut->pos;
439                                 but->editstr= oldbut->editstr;
440                                 but->editval= oldbut->editval;
441                                 but->editvec= oldbut->editvec;
442                                 but->editcoba= oldbut->editcoba;
443                                 but->editcumap= oldbut->editcumap;
444                                 but->selsta= oldbut->selsta;
445                                 but->selend= oldbut->selend;
446                                 found= 1;
447                         }
448
449                         /* ensures one button can get activated, and in case the buttons
450                          * draw are the same this gives O(1) lookup for each button */
451                         BLI_remlink(&oldblock->buttons, oldbut);
452                         ui_free_but(oldbut);
453                         
454                         break;
455                 }
456         }
457         
458         return found;
459 }
460
461 void uiEndBlock(uiBlock *block)
462 {
463         uiBut *but;
464
465         /* inherit flags from 'old' buttons that was drawn here previous, based
466          * on matching buttons, we need this to make button event handling non
467          * blocking, while still alowing buttons to be remade each redraw as it
468          * is expected by blender code */
469         for(but=block->buttons.first; but; but=but->next)
470                 if(ui_but_update_from_old_block(block, but))
471                         ui_check_but(but);
472
473         if(block->oldblock) {
474                 block->auto_open= block->oldblock->auto_open;
475                 block->auto_open_last= block->oldblock->auto_open_last;
476
477                 uiFreeBlock(block->oldblock);
478                 block->oldblock= NULL;
479         }
480         
481         /* handle pending stuff */
482         if(block->autofill) ui_autofill(block);
483         if(block->minx==0.0 && block->maxx==0.0) uiBoundsBlock(block, 0);
484         if(block->flag & UI_BUT_ALIGN) uiBlockEndAlign(block);
485 }
486
487 /* ************** BLOCK DRAWING FUNCTION ************* */
488
489 void uiDrawBlock(uiBlock *block)
490 {
491         uiBut *but;
492
493         /* we set this only once */
494         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
495
496         /* XXX 2.50 no panels yet */
497         //uiPanelPush(block); // panel matrix
498         
499         if(block->flag & UI_BLOCK_LOOP) {
500                 uiDrawMenuBox(block->minx, block->miny, block->maxx, block->maxy, block->flag);
501         }
502         else {
503                 /* XXX 2.50 no panels yet */
504                 //if(block->panel) ui_draw_panel(block);
505         }               
506
507         /* XXX 2.50 need context here? */
508         //if(block->drawextra) block->drawextra(curarea, block);
509
510         for (but= block->buttons.first; but; but= but->next)
511                 ui_draw_but(but);
512
513         ui_draw_links(block);
514
515         /* XXX 2.50 no panels yet */
516         //uiPanelPop(block); // matrix restored
517 }
518
519 /* ************* EVENTS ************* */
520
521 static void ui_is_but_sel(uiBut *but)
522 {
523         double value;
524         int lvalue;
525         short push=0, true=1;
526
527         value= ui_get_but_val(but);
528
529         if( but->type==TOGN  || but->type==ICONTOGN) true= 0;
530
531         if( but->bit ) {
532                 lvalue= (int)value;
533                 if( BTST(lvalue, (but->bitnr)) ) push= true;
534                 else push= !true;
535         }
536         else {
537                 switch(but->type) {
538                 case BUT:
539                         push= 2;
540                         break;
541                 case KEYEVT:
542                         if (value==-1) push= 1;
543                         break;
544                 case TOG:
545                 case TOGR:
546                 case TOG3:
547                 case BUT_TOGDUAL:
548                 case ICONTOG:
549                         if(value!=but->min) push= 1;
550                         break;
551                 case ICONTOGN:
552                 case TOGN:
553                         if(value==0.0) push= 1;
554                         break;
555                 case ROW:
556                         if(value == but->max) push= 1;
557                         break;
558                 case COL:
559                         push= 1;
560                         break;
561                 default:
562                         push= 2;
563                         break;
564                 }
565         }
566         
567         if(push==2);
568         else if(push==1) but->flag |= UI_SELECT;
569         else but->flag &= ~UI_SELECT;
570 }
571
572 /* XXX 2.50 no links supported yet */
573
574 #if 0
575 static uiBut *ui_get_valid_link_button(uiBlock *block, uiBut *but, short *mval)
576 {
577         uiBut *bt;
578         
579                 /* find button to link to */
580         for (bt= block->buttons.first; bt; bt= bt->next)
581                 if(bt!=but && uibut_contains_pt(bt, mval))
582                         break;
583
584         if (bt) {
585                 if (but->type==LINK && bt->type==INLINK) {
586                         if( but->link->tocode == (int)bt->min ) {
587                                 return bt;
588                         }
589                 }
590                 else if(but->type==INLINK && bt->type==LINK) {
591                         if( bt->link->tocode == (int)but->min ) {
592                                 return bt;
593                         }
594                 }
595         }
596
597         return NULL;
598 }
599
600 static int ui_is_a_link(uiBut *from, uiBut *to)
601 {
602         uiLinkLine *line;
603         uiLink *link;
604         
605         link= from->link;
606         if(link) {
607                 line= link->lines.first;
608                 while(line) {
609                         if(line->from==from && line->to==to) return 1;
610                         line= line->next;
611                 }
612         }
613         return 0;
614 }
615
616 static uiBut *ui_find_inlink(uiBlock *block, void *poin)
617 {
618         uiBut *but;
619         
620         but= block->buttons.first;
621         while(but) {
622                 if(but->type==INLINK) {
623                         if(but->poin == poin) return but;
624                 }
625                 but= but->next;
626         }
627         return NULL;
628 }
629
630 static void ui_add_link_line(ListBase *listb, uiBut *but, uiBut *bt)
631 {
632         uiLinkLine *line;
633         
634         line= MEM_callocN(sizeof(uiLinkLine), "linkline");
635         BLI_addtail(listb, line);
636         line->from= but;
637         line->to= bt;
638 }
639
640 uiBut *uiFindInlink(uiBlock *block, void *poin)
641 {
642         return ui_find_inlink(block, poin);
643 }
644
645 void uiComposeLinks(uiBlock *block)
646 {
647         uiBut *but, *bt;
648         uiLink *link;
649         void ***ppoin;
650         int a;
651         
652         but= block->buttons.first;
653         while(but) {
654                 if(but->type==LINK) {
655                         link= but->link;
656                         
657                         /* for all pointers in the array */
658                         if(link) {
659                                 if(link->ppoin) {
660                                         ppoin= link->ppoin;
661                                         for(a=0; a < *(link->totlink); a++) {
662                                                 bt= ui_find_inlink(block, (*ppoin)[a] );
663                                                 if(bt) {
664                                                         ui_add_link_line(&link->lines, but, bt);
665                                                 }
666                                         }
667                                 }
668                                 else if(link->poin) {
669                                         bt= ui_find_inlink(block, *(link->poin) );
670                                         if(bt) {
671                                                 ui_add_link_line(&link->lines, but, bt);
672                                         }
673                                 }
674                         }
675                 }
676                 but= but->next;
677         }
678 }
679
680 static void ui_add_link(uiBut *from, uiBut *to)
681 {
682         /* in 'from' we have to add a link to 'to' */
683         uiLink *link;
684         void **oldppoin;
685         int a;
686         
687         if(ui_is_a_link(from, to)) {
688                 printf("already exists\n");
689                 return;
690         }
691         
692         link= from->link;
693
694         /* are there more pointers allowed? */
695         if(link->ppoin) {
696                 oldppoin= *(link->ppoin);
697                 
698                 (*(link->totlink))++;
699                 *(link->ppoin)= MEM_callocN( *(link->totlink)*sizeof(void *), "new link");
700
701                 for(a=0; a< (*(link->totlink))-1; a++) {
702                         (*(link->ppoin))[a]= oldppoin[a];
703                 }
704                 (*(link->ppoin))[a]= to->poin;
705                 
706                 if(oldppoin) MEM_freeN(oldppoin);
707         }
708         else {
709                 *(link->poin)= to->poin;
710         }
711         
712 }
713
714 static int ui_do_but_LINK(uiBlock *block, uiBut *but)
715 {
716         /* 
717          * This button only visualizes, the dobutton mode
718          * can add a new link, but then the whole system
719          * should be redrawn/initialized. 
720          * 
721          */
722         uiBut *bt=0, *bto=NULL;
723         short sval[2], mval[2], mvalo[2], first= 1;
724
725         uiGetMouse(curarea->win, sval);
726         mvalo[0]= sval[0];
727         mvalo[1]= sval[1];
728         
729         while (get_mbut() & L_MOUSE) {
730                 uiGetMouse(curarea->win, mval);
731
732                 if(mval[0]!=mvalo[0] || mval[1]!=mvalo[1] || first) {                   
733                                 /* clear completely, because of drawbuttons */
734                         bt= ui_get_valid_link_button(block, but, mval);
735                         if(bt) {
736                                 bt->flag |= UI_ACTIVE;
737                                 ui_draw_but(bt);
738                         }
739                         if(bto && bto!=bt) {
740                                 bto->flag &= ~UI_ACTIVE;
741                                 ui_draw_but(bto);
742                         }
743                         bto= bt;
744
745                         if (!first) {
746                                 glutil_draw_front_xor_line(sval[0], sval[1], mvalo[0], mvalo[1]);
747                         }
748                         glutil_draw_front_xor_line(sval[0], sval[1], mval[0], mval[1]);
749
750                         mvalo[0]= mval[0];
751                         mvalo[1]= mval[1];
752
753                         first= 0;
754                 }
755                 else UI_wait_for_statechange();         
756         }
757         
758         if (!first) {
759                 glutil_draw_front_xor_line(sval[0], sval[1], mvalo[0], mvalo[1]);
760         }
761
762         if(bt) {
763                 if(but->type==LINK) ui_add_link(but, bt);
764                 else ui_add_link(bt, but);
765
766                 scrarea_queue_winredraw(curarea);
767         }
768
769         return 0;
770 }
771 #endif
772
773 /* ************************************************ */
774
775 void uiBlockSetButLock(uiBlock *block, int val, char *lockstr)
776 {
777         block->lock |= val;
778         if(val) block->lockstr= lockstr;
779 }
780
781 void uiBlockClearButLock(uiBlock *block)
782 {
783         block->lock= 0;
784         block->lockstr= NULL;
785 }
786
787 /* *************************************************************** */
788
789 /* XXX 2.50 no button editing */
790
791 #if 0
792 static void setup_file(uiBlock *block)
793 {
794         uiBut *but;
795         FILE *fp;
796
797         fp= fopen("butsetup","w");
798         if(fp==NULL);
799         else {
800                 but= block->buttons.first;
801                 while(but) {
802                         ui_check_but(but);
803                         fprintf(fp,"%d,%d,%d,%d   %s %s\n", (int)but->x1, (int)but->y1, (int)( but->x2-but->x1), (int)(but->y2-but->y1), but->str, but->tip);
804                         but= but->next;
805                 }
806                 fclose(fp);
807         }
808 }
809
810
811 static void edit_but(uiBlock *block, uiBut *but, uiEvent *uevent)
812 {
813         short dx, dy, mval[2], mvalo[2], didit=0;
814         
815         getmouseco_sc(mvalo);
816         while(TRUE) {
817                 if( !(get_mbut() & L_MOUSE) ) break;    
818         
819                 getmouseco_sc(mval);
820                 dx= (mval[0]-mvalo[0]);
821                 dy= (mval[1]-mvalo[1]);
822                 
823                 if(dx!=0 || dy!=0) {
824                         mvalo[0]= mval[0];
825                         mvalo[1]= mval[1];
826                         
827                         cpack(0xc0c0c0);
828                         glRectf(but->x1-2, but->y1-2, but->x2+2, but->y2+2); 
829                         
830                         if((uevent->qual & LR_SHIFTKEY)==0) {
831                                 but->x1 += dx;
832                                 but->y1 += dy;
833                         }
834                         but->x2 += dx;
835                         but->y2 += dy;
836                         
837                         ui_draw_but(but);
838                         ui_block_flush_back(but->block);
839                         didit= 1;
840
841                 }
842                 /* idle for this poor code */
843                 else PIL_sleep_ms(30);
844         }
845         if(didit) setup_file(block);
846 }
847 #endif
848
849 /* XXX 2.50 no links supported yet */
850 #if 0
851 static void ui_delete_active_linkline(uiBlock *block)
852 {
853         uiBut *but;
854         uiLink *link;
855         uiLinkLine *line, *nline;
856         int a, b;
857         
858         but= block->buttons.first;
859         while(but) {
860                 if(but->type==LINK && but->link) {
861                         line= but->link->lines.first;
862                         while(line) {
863                                 
864                                 nline= line->next;
865                                 
866                                 if(line->flag & UI_SELECT) {
867                                         BLI_remlink(&but->link->lines, line);
868
869                                         link= line->from->link;
870
871                                         /* are there more pointers allowed? */
872                                         if(link->ppoin) {
873                                                 
874                                                 if(*(link->totlink)==1) {
875                                                         *(link->totlink)= 0;
876                                                         MEM_freeN(*(link->ppoin));
877                                                         *(link->ppoin)= NULL;
878                                                 }
879                                                 else {
880                                                         b= 0;
881                                                         for(a=0; a< (*(link->totlink)); a++) {
882                                                                 
883                                                                 if( (*(link->ppoin))[a] != line->to->poin ) {
884                                                                         (*(link->ppoin))[b]= (*(link->ppoin))[a];
885                                                                         b++;
886                                                                 }
887                                                         }       
888                                                         (*(link->totlink))--;
889                                                 }
890                                         }
891                                         else {
892                                                 *(link->poin)= NULL;
893                                         }
894
895                                         MEM_freeN(line);
896                                 }
897                                 line= nline;
898                         }
899                 }
900                 but= but->next;
901         }
902         
903         /* temporal! these buttons can be everywhere... */
904         allqueue(REDRAWBUTSLOGIC, 0);
905 }
906
907 static void ui_do_active_linklines(uiBlock *block, short *mval)
908 {
909         uiBut *but;
910         uiLinkLine *line, *act= NULL;
911         float mindist= 12.0, fac, v1[2], v2[2], v3[3];
912         int foundone= 0; 
913         
914         if(mval) {
915                 v1[0]= mval[0];
916                 v1[1]= mval[1];
917                 
918                 /* find a line close to the mouse */
919                 but= block->buttons.first;
920                 while(but) {
921                         if(but->type==LINK && but->link) {
922                                 foundone= 1;
923                                 line= but->link->lines.first;
924                                 while(line) {
925                                         v2[0]= line->from->x2;
926                                         v2[1]= (line->from->y1+line->from->y2)/2.0;
927                                         v3[0]= line->to->x1;
928                                         v3[1]= (line->to->y1+line->to->y2)/2.0;
929                                         
930                                         fac= PdistVL2Dfl(v1, v2, v3);
931                                         if(fac < mindist) {
932                                                 mindist= fac;
933                                                 act= line;
934                                         }
935                                         line= line->next;
936                                 }
937                         }
938                         but= but->next;
939                 }
940         }
941
942         /* check for a 'found one' to prevent going to 'frontbuffer' mode.
943                 this slows done gfx quite some, and at OSX the 'finish' forces a swapbuffer */
944         if(foundone) {
945                 glDrawBuffer(GL_FRONT);
946                 
947                 /* draw */
948                 but= block->buttons.first;
949                 while(but) {
950                         if(but->type==LINK && but->link) {
951                                 line= but->link->lines.first;
952                                 while(line) {
953                                         if(line==act) {
954                                                 if((line->flag & UI_SELECT)==0) {
955                                                         line->flag |= UI_SELECT;
956                                                         ui_draw_linkline(but, line);
957                                                 }
958                                         }
959                                         else if(line->flag & UI_SELECT) {
960                                                 line->flag &= ~UI_SELECT;
961                                                 ui_draw_linkline(but, line);
962                                         }
963                                         line= line->next;
964                                 }
965                         }
966                         but= but->next;
967                 }
968                 bglFlush();
969                 glDrawBuffer(GL_BACK);
970         }
971 }
972 #endif
973
974 /* ******************************************************* */
975
976 /* XXX 2.50 no screendump supported yet */
977
978 #if 0
979 /* nasty but safe way to store screendump rect */
980 static int scr_x=0, scr_y=0, scr_sizex=0, scr_sizey=0;
981
982 static void ui_set_screendump_bbox(uiBlock *block)
983 {
984         if(block) {
985                 scr_x= block->minx;
986                 scr_y= block->miny;
987                 scr_sizex= block->maxx - block->minx;
988                 scr_sizey= block->maxy - block->miny;
989         }
990         else {
991                 scr_sizex= scr_sizey= 0;
992         }
993 }
994
995 /* used for making screenshots for menus, called in screendump.c */
996 int uiIsMenu(int *x, int *y, int *sizex, int *sizey)
997 {
998         if(scr_sizex!=0 && scr_sizey!=0) {
999                 *x= scr_x;
1000                 *y= scr_y;
1001                 *sizex= scr_sizex;
1002                 *sizey= scr_sizey;
1003                 return 1;
1004         }
1005         
1006         return 0;
1007 }
1008 #endif
1009
1010 /* *********************** data get/set ***********************
1011  * this either works with the pointed to data, or can work with
1012  * an edit override pointer while dragging for example */
1013
1014 /* for buttons pointing to color for example */
1015 void ui_get_but_vectorf(uiBut *but, float *vec)
1016 {
1017         void *poin;
1018         int pointype;
1019
1020         poin= (but->editvec)? (void*)but->editvec: but->poin;
1021         pointype= (but->editvec)? FLO: but->pointype;
1022
1023         if(!but->editvec && pointype == CHA) {
1024                 char *cp= (char *)poin;
1025                 vec[0]= ((float)cp[0])/255.0;
1026                 vec[1]= ((float)cp[1])/255.0;
1027                 vec[2]= ((float)cp[2])/255.0;
1028         }
1029         else if(pointype == FLO) {
1030                 float *fp= (float *)poin;
1031                 VECCOPY(vec, fp);
1032         }
1033 }
1034
1035 /* for buttons pointing to color for example */
1036 void ui_set_but_vectorf(uiBut *but, float *vec)
1037 {
1038         void *poin;
1039         int pointype;
1040
1041         poin= (but->editvec)? (void*)but->editvec: but->poin;
1042         pointype= (but->editvec)? FLO: but->pointype;
1043
1044         if(!but->editvec && but->pointype == CHA) {
1045                 char *cp= (char *)poin;
1046                 cp[0]= (char)(0.5 +vec[0]*255.0);
1047                 cp[1]= (char)(0.5 +vec[1]*255.0);
1048                 cp[2]= (char)(0.5 +vec[2]*255.0);
1049         }
1050         else if( but->pointype == FLO ) {
1051                 float *fp= (float *)poin;
1052                 VECCOPY(fp, vec);
1053         }
1054 }
1055
1056 int ui_is_but_float(uiBut *but)
1057 {
1058         if(but->pointype==FLO && but->poin)
1059                 return 1;
1060         
1061         if(but->rnaprop && RNA_property_type(&but->rnapoin, but->rnaprop) == PROP_FLOAT)
1062                 return 1;
1063         
1064         return 0;
1065 }
1066
1067 double ui_get_but_val(uiBut *but)
1068 {
1069         PropertyRNA *prop;
1070         double value = 0.0;
1071
1072         if(but->editval) { return *(but->editval); }
1073         if(but->poin==NULL && but->rnapoin.data==NULL) return 0.0;
1074
1075         if(but->rnaprop) {
1076                 prop= but->rnaprop;
1077
1078                 switch(RNA_property_type(&but->rnapoin, prop)) {
1079                         case PROP_BOOLEAN:
1080                                 if(RNA_property_array_length(&but->rnapoin, prop))
1081                                         value= RNA_property_boolean_get_array(&but->rnapoin, prop, but->rnaindex);
1082                                 else
1083                                         value= RNA_property_boolean_get(&but->rnapoin, prop);
1084                                 break;
1085                         case PROP_INT:
1086                                 if(RNA_property_array_length(&but->rnapoin, prop))
1087                                         value= RNA_property_int_get_array(&but->rnapoin, prop, but->rnaindex);
1088                                 else
1089                                         value= RNA_property_int_get(&but->rnapoin, prop);
1090                                 break;
1091                         case PROP_FLOAT:
1092                                 if(RNA_property_array_length(&but->rnapoin, prop))
1093                                         value= RNA_property_float_get_array(&but->rnapoin, prop, but->rnaindex);
1094                                 else
1095                                         value= RNA_property_float_get(&but->rnapoin, prop);
1096                                 break;
1097                         case PROP_ENUM:
1098                                 value= RNA_property_enum_get(&but->rnapoin, prop);
1099                                 break;
1100                         default:
1101                                 value= 0.0;
1102                                 break;
1103                 }
1104         }
1105         else if(but->type== HSVSLI) {
1106                 float h, s, v, *fp;
1107                 
1108                 fp= (but->editvec)? but->editvec: (float *)but->poin;
1109                 rgb_to_hsv(fp[0], fp[1], fp[2], &h, &s, &v);
1110
1111                 switch(but->str[0]) {
1112                         case 'H': value= h; break;
1113                         case 'S': value= s; break;
1114                         case 'V': value= v; break;
1115                 }
1116         } 
1117         else if( but->pointype == CHA ) {
1118                 value= *(char *)but->poin;
1119         }
1120         else if( but->pointype == SHO ) {
1121                 value= *(short *)but->poin;
1122         } 
1123         else if( but->pointype == INT ) {
1124                 value= *(int *)but->poin;
1125         } 
1126         else if( but->pointype == FLO ) {
1127                 value= *(float *)but->poin;
1128         }
1129     
1130         return value;
1131 }
1132
1133 void ui_set_but_val(uiBut *but, double value)
1134 {
1135         PropertyRNA *prop;
1136
1137         /* value is a hsv value: convert to rgb */
1138         if(but->rnaprop) {
1139                 prop= but->rnaprop;
1140
1141                 if(RNA_property_editable(&but->rnapoin, prop)) {
1142                         switch(RNA_property_type(&but->rnapoin, prop)) {
1143                                 case PROP_BOOLEAN:
1144                                         if(RNA_property_array_length(&but->rnapoin, prop))
1145                                                 RNA_property_boolean_set_array(&but->rnapoin, prop, but->rnaindex, value);
1146                                         else
1147                                                 RNA_property_boolean_set(&but->rnapoin, prop, value);
1148                                         break;
1149                                 case PROP_INT:
1150                                         if(RNA_property_array_length(&but->rnapoin, prop))
1151                                                 RNA_property_int_set_array(&but->rnapoin, prop, but->rnaindex, value);
1152                                         else
1153                                                 RNA_property_int_set(&but->rnapoin, prop, value);
1154                                         break;
1155                                 case PROP_FLOAT:
1156                                         if(RNA_property_array_length(&but->rnapoin, prop))
1157                                                 RNA_property_float_set_array(&but->rnapoin, prop, but->rnaindex, value);
1158                                         else
1159                                                 RNA_property_float_set(&but->rnapoin, prop, value);
1160                                         break;
1161                                 case PROP_ENUM:
1162                                         RNA_property_enum_set(&but->rnapoin, prop, value);
1163                                         break;
1164                                 default:
1165                                         break;
1166                         }
1167                 }
1168         }
1169         else if(but->pointype==0);
1170         else if(but->type==HSVSLI ) {
1171                 float h, s, v, *fp;
1172                 
1173                 fp= (but->editvec)? but->editvec: (float *)but->poin;
1174                 rgb_to_hsv(fp[0], fp[1], fp[2], &h, &s, &v);
1175                 
1176                 switch(but->str[0]) {
1177                 case 'H': h= value; break;
1178                 case 'S': s= value; break;
1179                 case 'V': v= value; break;
1180                 }
1181                 
1182                 hsv_to_rgb(h, s, v, fp, fp+1, fp+2);
1183                 
1184         }
1185         else {
1186                 /* first do rounding */
1187                 if(but->pointype==CHA)
1188                         value= (char)floor(value+0.5);
1189                 else if(but->pointype==SHO ) {
1190                         /* gcc 3.2.1 seems to have problems 
1191                          * casting a double like 32772.0 to
1192                          * a short so we cast to an int, then 
1193                          to a short */
1194                         int gcckludge;
1195                         gcckludge = (int) floor(value+0.5);
1196                         value= (short)gcckludge;
1197                 }
1198                 else if(but->pointype==INT )
1199                         value= (int)floor(value+0.5);
1200                 else if(but->pointype==FLO ) {
1201                         float fval= (float)value;
1202                         if(fval>= -0.00001f && fval<= 0.00001f) fval= 0.0f;     /* prevent negative zero */
1203                         value= fval;
1204                 }
1205                 
1206                 /* then set value with possible edit override */
1207                 if(but->editval)
1208                         *but->editval= value;
1209                 else if(but->pointype==CHA)
1210                         *((char *)but->poin)= (char)value;
1211                 else if(but->pointype==SHO)
1212                         *((short *)but->poin)= (short)value;
1213                 else if(but->pointype==INT)
1214                         *((int *)but->poin)= (int)value;
1215                 else if(but->pointype==FLO)
1216                         *((float *)but->poin)= (float)value;
1217         }
1218
1219         /* update select flag */
1220         ui_is_but_sel(but);
1221 }
1222
1223 void ui_get_but_string(uiBut *but, char *str, int maxlen)
1224 {
1225         if(but->rnaprop) {
1226                 char *buf;
1227                 
1228                 buf= RNA_property_string_get_alloc(&but->rnapoin, but->rnaprop, str, maxlen);
1229
1230                 if(buf != str) {
1231                         /* string was too long, we have to truncate */
1232                         BLI_strncpy(str, buf, maxlen);
1233                         MEM_freeN(buf);
1234                 }
1235         }
1236         else
1237                 BLI_strncpy(str, but->poin, maxlen);
1238
1239 }
1240
1241 void ui_set_but_string(uiBut *but, const char *str)
1242 {
1243         if(but->rnaprop)
1244                 RNA_property_string_set(&but->rnapoin, but->rnaprop, str);
1245         else
1246                 BLI_strncpy(but->poin, str, but->max);
1247 }
1248
1249 /* ******************* Font ********************/
1250
1251 static void ui_set_ftf_font(float aspect)
1252 {
1253 #ifdef INTERNATIONAL
1254         if(aspect<1.15) {
1255                 FTF_SetFontSize('l');
1256         }
1257         else if(aspect<1.59) {
1258                 FTF_SetFontSize('m');
1259         }
1260         else {
1261                 FTF_SetFontSize('s');
1262         }
1263 #endif
1264 }
1265
1266 void uiSetCurFont(uiBlock *block, int index)
1267 {
1268         ui_set_ftf_font(block->aspect);
1269         
1270         if(block->aspect<0.60) {
1271                 block->curfont= UIfont[index].xl;
1272         }
1273         else if(block->aspect<1.15) {
1274                 block->curfont= UIfont[index].large;
1275         }
1276         else if(block->aspect<1.59) {
1277                 block->curfont= UIfont[index].medium;           
1278         }
1279         else {
1280                 block->curfont= UIfont[index].small;            
1281         }
1282
1283         if(block->curfont==NULL) block->curfont= UIfont[index].large;   
1284         if(block->curfont==NULL) block->curfont= UIfont[index].medium;  
1285         if(block->curfont==NULL) printf("error block no font %s\n", block->name);
1286         
1287 }
1288
1289 /* called by node editor */
1290 void *uiSetCurFont_ext(float aspect)
1291 {
1292         void *curfont;
1293         
1294         ui_set_ftf_font(aspect);
1295         
1296         if(aspect<0.60) {
1297                 curfont= UIfont[0].xl;
1298         }
1299         else if(aspect<1.15) {
1300                 curfont= UIfont[0].large;
1301         }
1302         else if(aspect<1.59) {
1303                 curfont= UIfont[0].medium;              
1304         }
1305         else {
1306                 curfont= UIfont[0].small;               
1307         }
1308         
1309         if(curfont==NULL) curfont= UIfont[0].large;     
1310         if(curfont==NULL) curfont= UIfont[0].medium;    
1311         
1312         return curfont;
1313 }
1314
1315 void uiDefFont(unsigned int index, void *xl, void *large, void *medium, void *small)
1316 {
1317         if(index>=UI_ARRAY) return;
1318         
1319         UIfont[index].xl= xl;
1320         UIfont[index].large= large;
1321         UIfont[index].medium= medium;
1322         UIfont[index].small= small;
1323 }
1324
1325 /* ******************* Free ********************/
1326
1327 static void ui_free_link(uiLink *link)
1328 {
1329         if(link) {      
1330                 BLI_freelistN(&link->lines);
1331                 MEM_freeN(link);
1332         }
1333 }
1334
1335 static void ui_free_but(uiBut *but)
1336 {
1337         if(but->str && but->str != but->strdata) MEM_freeN(but->str);
1338         ui_free_link(but->link);
1339
1340         MEM_freeN(but);
1341 }
1342
1343 void uiFreeBlock(uiBlock *block)
1344 {
1345         uiBut *but;
1346
1347         while( (but= block->buttons.first) ) {
1348                 BLI_remlink(&block->buttons, but);      
1349                 ui_free_but(but);
1350         }
1351
1352         if(block->panel) block->panel->active= 0;
1353         BLI_freelistN(&block->saferct);
1354
1355         
1356         MEM_freeN(block);
1357 }
1358
1359 void uiFreeBlocks(ListBase *lb)
1360 {
1361         uiBlock *block;
1362         
1363         while( (block= lb->first) ) {
1364                 BLI_remlink(lb, block);
1365                 uiFreeBlock(block);
1366         }
1367 }
1368
1369 uiBlock *uiBeginBlock(wmWindow *window, ARegion *region, char *name, short dt, short font)
1370 {
1371         ListBase *lb;
1372         uiBlock *block, *oldblock= NULL;
1373         int getsizex, getsizey;
1374
1375         lb= &region->uiblocks;
1376         
1377         /* each listbase only has one block with this name, free block
1378          * if is already there so it can be rebuilt from scratch */
1379         if(lb) {
1380                 for (oldblock= lb->first; oldblock; oldblock= oldblock->next)
1381                         if (BLI_streq(oldblock->name, name))
1382                                 break;
1383
1384                 if (oldblock)
1385                         BLI_remlink(lb, oldblock);
1386         }
1387         
1388         block= MEM_callocN(sizeof(uiBlock), "uiBlock");
1389         block->oldblock= oldblock;
1390
1391         /* at the beginning of the list! for dynamical menus/blocks */
1392         if(lb)
1393                 BLI_addhead(lb, block);
1394
1395         BLI_strncpy(block->name, name, sizeof(block->name));
1396
1397 #if 0
1398         /* draw win */
1399         block->win= win;
1400         /* window where queue event should be added, pretty weak this way!
1401            this is because the 'mainwin' pup menu's */
1402         block->winq= mywinget();
1403 #endif
1404
1405         block->dt= dt;
1406         block->themecol= TH_AUTO;
1407
1408         /* window matrix and aspect */
1409         if(region->swinid) {
1410                 wm_subwindow_getmatrix(window, region->swinid, block->winmat);
1411                 wm_subwindow_getsize(window, region->swinid, &getsizex, &getsizey);
1412
1413                 /* TODO - investigate why block->winmat[0][0] is negative
1414                  * in the image view when viewRedrawForce is called */
1415                 block->aspect= 2.0/fabs( (getsizex)*block->winmat[0][0]);
1416         }
1417         else {
1418                 /* no subwindow created yet, for menus for example, so we
1419                  * use the main window instead, since buttons are created
1420                  * there anyway */
1421                 wm_subwindow_getmatrix(window, window->winid, block->winmat);
1422                 wm_subwindow_getsize(window, window->winid, &getsizex, &getsizey);
1423
1424                 block->aspect= 2.0/fabs(getsizex*block->winmat[0][0]);
1425                 block->auto_open= 2;
1426         }
1427
1428         uiSetCurFont(block, font);
1429
1430         return block;
1431 }
1432
1433 uiBlock *uiGetBlock(char *name, ARegion *ar)
1434 {
1435         uiBlock *block= ar->uiblocks.first;
1436         
1437         while(block) {
1438                 if( strcmp(name, block->name)==0 ) return block;
1439                 block= block->next;
1440         }
1441         
1442         return NULL;
1443 }
1444
1445 void ui_check_but(uiBut *but)
1446 {
1447         /* if something changed in the button */
1448         ID *id;
1449         double value;
1450         float okwidth;
1451         int transopts= ui_translate_buttons();
1452         short pos;
1453         
1454         ui_is_but_sel(but);
1455         
1456         if(but->type==TEX || but->type==IDPOIN) transopts= 0;
1457
1458         /* test for min and max, icon sliders, etc */
1459         switch( but->type ) {
1460                 case NUM:
1461                 case SLI:
1462                 case SCROLL:
1463                 case NUMSLI:
1464                 case HSVSLI:
1465                         value= ui_get_but_val(but);
1466                         if(value < but->min) ui_set_but_val(but, but->min);
1467                         else if(value > but->max) ui_set_but_val(but, but->max);
1468                         break;
1469                         
1470                 case NUMABS:
1471                         value= fabs( ui_get_but_val(but) );
1472                         if(value < but->min) ui_set_but_val(but, but->min);
1473                         else if(value > but->max) ui_set_but_val(but, but->max);
1474                         break;
1475                         
1476                 case ICONTOG: 
1477                 case ICONTOGN:
1478                         if(but->flag & UI_SELECT) but->iconadd= 1;
1479                         else but->iconadd= 0;
1480                         break;
1481                         
1482                 case ICONROW:
1483                         value= ui_get_but_val(but);
1484                         but->iconadd= (int)value- (int)(but->min);
1485                         break;
1486                         
1487                 case ICONTEXTROW:
1488                         value= ui_get_but_val(but);
1489                         but->iconadd= (int)value- (int)(but->min);
1490                         break;
1491         }
1492         
1493         
1494         /* safety is 4 to enable small number buttons (like 'users') */
1495         if(but->type==NUMSLI || but->type==HSVSLI) 
1496                 okwidth= -4 + (but->x2 - but->x1)/2.0;
1497         else 
1498                 okwidth= -4 + (but->x2 - but->x1); 
1499         
1500         /* name: */
1501         switch( but->type ) {
1502         
1503         case MENU:
1504         case ICONTEXTROW:
1505                 
1506                 if(but->x2 - but->x1 > 24) {
1507                         value= ui_get_but_val(but);
1508                         ui_set_name_menu(but, (int)value);
1509                 }
1510                 break;
1511         
1512         case NUM:
1513         case NUMSLI:
1514         case HSVSLI:
1515         case NUMABS:
1516
1517                 value= ui_get_but_val(but);
1518
1519                 if(ui_is_but_float(but)) {
1520                         if(value == FLT_MAX) sprintf(but->drawstr, "%sFLT_MAX", but->str);
1521                         else if(value == -FLT_MAX) sprintf(but->drawstr, "%s-FLT_MAX", but->str);
1522                         else if(but->a2) { /* amount of digits defined */
1523                                 if(but->a2==1) sprintf(but->drawstr, "%s%.1f", but->str, value);
1524                                 else if(but->a2==2) sprintf(but->drawstr, "%s%.2f", but->str, value);
1525                                 else if(but->a2==3) sprintf(but->drawstr, "%s%.3f", but->str, value);
1526                                 else sprintf(but->drawstr, "%s%.4f", but->str, value);
1527                         }
1528                         else {
1529                                 if(but->max<10.001) sprintf(but->drawstr, "%s%.3f", but->str, value);
1530                                 else sprintf(but->drawstr, "%s%.2f", but->str, value);
1531                         }
1532                 }
1533                 else {
1534                         sprintf(but->drawstr, "%s%d", but->str, (int)value);
1535                 }
1536                 break;
1537
1538         case LABEL:
1539                 if(ui_is_but_float(but)) {
1540                         value= ui_get_but_val(but);
1541                         if(but->a2) { /* amount of digits defined */
1542                                 if(but->a2==1) sprintf(but->drawstr, "%s%.1f", but->str, value);
1543                                 else if(but->a2==2) sprintf(but->drawstr, "%s%.2f", but->str, value);
1544                                 else if(but->a2==3) sprintf(but->drawstr, "%s%.3f", but->str, value);
1545                                 else sprintf(but->drawstr, "%s%.4f", but->str, value);
1546                         }
1547                         else {
1548                                 sprintf(but->drawstr, "%s%.2f", but->str, value);
1549                         }
1550                 }
1551                 else strcpy(but->drawstr, but->str);
1552                 
1553                 break;
1554
1555         case IDPOIN:
1556                 id= *(but->idpoin_idpp);
1557                 strcpy(but->drawstr, but->str);
1558                 if(id) strcat(but->drawstr, id->name+2);
1559                 break;
1560         
1561         case TEX:
1562                 if(!but->editstr) {
1563                         char str[UI_MAX_DRAW_STR];
1564
1565                         ui_get_but_string(but, str, UI_MAX_DRAW_STR-strlen(but->str));
1566
1567                         strcpy(but->drawstr, but->str);
1568                         strcat(but->drawstr, str);
1569                 }
1570                 break;
1571         
1572         case KEYEVT:
1573                 strcpy(but->drawstr, but->str);
1574                 if (but->flag & UI_SELECT) {
1575                         strcat(but->drawstr, "Press a key");
1576                 } else {
1577                         /* XXX 2.50 function not supported */
1578                         /* strcat(but->drawstr, key_event_to_string((short) ui_get_but_val(but))); */
1579                 }
1580                 break;
1581
1582         case BUT_TOGDUAL:
1583                 /* trying to get the dual-icon to left of text... not very nice */
1584                 if(but->str[0]) {
1585                         strcpy(but->drawstr, "  ");
1586                         strcpy(but->drawstr+2, but->str);
1587                 }
1588                 break;
1589         default:
1590                 strcpy(but->drawstr, but->str);
1591                 
1592         }
1593
1594         /* if we are doing text editing, this will override the drawstr */
1595         if(but->editstr) {
1596                 strcpy(but->drawstr, but->str);
1597                 strcat(but->drawstr, but->editstr);
1598         }
1599
1600         if(but->drawstr[0]) {
1601                 but->strwidth= but->aspect*UI_GetStringWidth(but->font, but->drawstr, transopts);
1602                 // here should be check for less space for icon offsets...
1603                 if(but->type==MENU) okwidth -= 15;
1604         }
1605         else
1606                 but->strwidth= 0;
1607
1608                 /* automatic width */
1609         if(but->x2==0.0f && but->x1 > 0.0f) {
1610                 but->x2= (but->x1+but->strwidth+6); 
1611         }
1612
1613         if(but->strwidth==0) but->drawstr[0]= 0;
1614         else if(but->type==BUTM || but->type==BLOCK);   // no clip string, uiTextBoundsBlock is used (hack!)
1615         else {
1616
1617                 /* calc but->ofs, to draw the string shorter if too long */
1618                 but->ofs= 0;
1619
1620                 while(but->strwidth > (int)okwidth ) {
1621         
1622                         if ELEM3(but->type, NUM, NUMABS, TEX) { // only these cut off left
1623                                 but->ofs++;
1624                                 but->strwidth= but->aspect*UI_GetStringWidth(but->font, but->drawstr+but->ofs, transopts);
1625                                 
1626                                 /* textbut exception */
1627                                 if(but->editstr && but->pos != -1) {
1628                                         pos= but->pos+strlen(but->str);
1629                                         if(pos-1 < but->ofs) {
1630                                                 pos= but->ofs-pos+1;
1631                                                 but->ofs -= pos;
1632                                                 if(but->ofs<0) {
1633                                                         but->ofs= 0;
1634                                                         pos--;
1635                                                 }
1636                                                 but->drawstr[ strlen(but->drawstr)-pos ]= 0;
1637                                         }
1638                                 }
1639                         }
1640                         else {
1641                                 but->drawstr[ strlen(but->drawstr)-1 ]= 0;
1642                                 but->strwidth= but->aspect*UI_GetStringWidth(but->font, but->drawstr, transopts);
1643                         }
1644                         
1645                         if(but->strwidth < 10) break;
1646                 }
1647         }
1648 }
1649
1650 static int ui_auto_themecol(uiBut *but)
1651 {
1652
1653         switch(but->type) {
1654         case BUT:
1655                 return TH_BUT_ACTION;
1656         case ROW:
1657         case TOG:
1658         case TOG3:
1659         case TOGR:
1660         case TOGN:
1661         case BUT_TOGDUAL:
1662                 return TH_BUT_SETTING;
1663         case SLI:
1664         case NUM:
1665         case NUMSLI:
1666         case NUMABS:
1667         case HSVSLI:
1668                 return TH_BUT_NUM;
1669         case TEX:
1670                 return TH_BUT_TEXTFIELD;
1671         case PULLDOWN:
1672         case BLOCK:
1673         case MENU:
1674         case BUTM:
1675                 // (weak!) detect if it is a blockloop
1676                 if(but->block->dt == UI_EMBOSSP) return TH_MENU_ITEM;
1677                 return TH_BUT_POPUP;
1678         case ROUNDBOX:
1679                 return TH_PANEL;
1680         default:
1681                 return TH_BUT_NEUTRAL;
1682         }
1683 }
1684
1685 void uiBlockBeginAlign(uiBlock *block)
1686 {
1687         /* if other align was active, end it */
1688         if(block->flag & UI_BUT_ALIGN) uiBlockEndAlign(block);
1689
1690         block->flag |= UI_BUT_ALIGN_DOWN;       
1691         /* buttons declared after this call will this align flag */
1692 }
1693
1694 static int buts_are_horiz(uiBut *but1, uiBut *but2)
1695 {
1696         float dx, dy;
1697         
1698         dx= fabs( but1->x2 - but2->x1);
1699         dy= fabs( but1->y1 - but2->y2);
1700         
1701         if(dx > dy) return 0;
1702         return 1;
1703 }
1704
1705 void uiBlockEndAlign(uiBlock *block)
1706 {
1707         uiBut *prev, *but=NULL, *next;
1708         int flag= 0, cols=0, rows=0;
1709         int theme= UI_GetThemeValue(TH_BUT_DRAWTYPE);
1710         
1711         if ( !(ELEM3(theme, TH_MINIMAL, TH_SHADED, TH_ROUNDED)) ) {
1712                 block->flag &= ~UI_BUT_ALIGN;   // all 4 flags
1713                 return;
1714         }
1715         
1716         /* auto align:
1717                 - go back to first button of align start (ALIGN_DOWN)
1718                 - compare triples, and define flags
1719         */
1720         prev= block->buttons.last;
1721         while(prev) {
1722                 if( (prev->flag & UI_BUT_ALIGN_DOWN)) but= prev;
1723                 else break;
1724                 
1725                 if(but && but->next) {
1726                         if(buts_are_horiz(but, but->next)) cols++;
1727                         else rows++;
1728                 }
1729                 
1730                 prev= prev->prev;
1731         }
1732         if(but==NULL) return;
1733         
1734         /* rows==0: 1 row, cols==0: 1 collumn */
1735         
1736         /* note;  how it uses 'flag' in loop below (either set it, or OR it) is confusing */
1737         prev= NULL;
1738         while(but) {
1739                 next= but->next;
1740                 
1741                 /* clear old flag */
1742                 but->flag &= ~UI_BUT_ALIGN_DOWN;
1743                 
1744                 if(flag==0) {   /* first case */
1745                         if(next) {
1746                                 if(buts_are_horiz(but, next)) {
1747                                         if(rows==0)
1748                                                 flag= UI_BUT_ALIGN_RIGHT;
1749                                         else 
1750                                                 flag= UI_BUT_ALIGN_DOWN|UI_BUT_ALIGN_RIGHT;
1751                                 }
1752                                 else {
1753                                         flag= UI_BUT_ALIGN_DOWN;
1754                                 }
1755                         }
1756                 }
1757                 else if(next==NULL) {   /* last case */
1758                         if(prev) {
1759                                 if(buts_are_horiz(prev, but)) {
1760                                         if(rows==0) 
1761                                                 flag= UI_BUT_ALIGN_LEFT;
1762                                         else
1763                                                 flag= UI_BUT_ALIGN_TOP|UI_BUT_ALIGN_LEFT;
1764                                 }
1765                                 else flag= UI_BUT_ALIGN_TOP;
1766                         }
1767                 }
1768                 else if(buts_are_horiz(but, next)) {
1769                         /* check if this is already second row */
1770                         if( prev && buts_are_horiz(prev, but)==0) {
1771                                 flag |= UI_BUT_ALIGN_TOP;
1772                                 /* exception case: bottom row */
1773                                 if(rows>0) {
1774                                         uiBut *bt= but;
1775                                         while(bt) {
1776                                                 if(bt->next && buts_are_horiz(bt, bt->next)==0 ) break; 
1777                                                 bt= bt->next;
1778                                         }
1779                                         if(bt==0) flag= UI_BUT_ALIGN_TOP|UI_BUT_ALIGN_RIGHT;
1780                                 }
1781                         }
1782                         else flag |= UI_BUT_ALIGN_LEFT;
1783                 }
1784                 else {
1785                         if(cols==0) {
1786                                 flag |= UI_BUT_ALIGN_TOP;
1787                         }
1788                         else {  /* next button switches to new row */
1789                                 if( (flag & UI_BUT_ALIGN_TOP)==0) {     /* stil top row */
1790                                         if(prev)
1791                                                 flag= UI_BUT_ALIGN_DOWN|UI_BUT_ALIGN_LEFT;
1792                                         else 
1793                                                 flag |= UI_BUT_ALIGN_DOWN;
1794                                 }
1795                                 else 
1796                                         flag |= UI_BUT_ALIGN_TOP;
1797                         }
1798                 }
1799                 
1800                 but->flag |= flag;
1801                 
1802                 /* merge coordinates */
1803                 if(prev) {
1804                         // simple cases 
1805                         if(rows==0) {
1806                                 but->x1= (prev->x2+but->x1)/2.0;
1807                                 prev->x2= but->x1;
1808                         }
1809                         else if(cols==0) {
1810                                 but->y2= (prev->y1+but->y2)/2.0;
1811                                 prev->y1= but->y2;
1812                         }
1813                         else {
1814                                 if(buts_are_horiz(prev, but)) {
1815                                         but->x1= (prev->x2+but->x1)/2.0;
1816                                         prev->x2= but->x1;
1817                                         /* copy height too */
1818                                         but->y2= prev->y2;
1819                                 }
1820                                 else if(prev->prev && buts_are_horiz(prev->prev, prev)==0) {
1821                                         /* the previous button is a single one in its row */
1822                                         but->y2= (prev->y1+but->y2)/2.0;
1823                                         prev->y1= but->y2;
1824                                 }
1825                                 else {
1826                                         /* the previous button is not a single one in its row */
1827                                         but->y2= prev->y1;
1828                                 }
1829                         }
1830                 }
1831                 
1832                 prev= but;
1833                 but= next;
1834         }
1835         
1836         block->flag &= ~UI_BUT_ALIGN;   // all 4 flags
1837 }
1838
1839 #if 0
1840 static void uiBlockEndAligno(uiBlock *block)
1841 {
1842         uiBut *but;
1843         
1844         /* correct last defined button */
1845         but= block->buttons.last;
1846         if(but) {
1847                 /* vertical align case */
1848                 if( (block->flag & UI_BUT_ALIGN) == (UI_BUT_ALIGN_TOP|UI_BUT_ALIGN_DOWN) ) {
1849                         but->flag &= ~UI_BUT_ALIGN_DOWN;
1850                 }
1851                 /* horizontal align case */
1852                 if( (block->flag & UI_BUT_ALIGN) == (UI_BUT_ALIGN_LEFT|UI_BUT_ALIGN_RIGHT) ) {
1853                         but->flag &= ~UI_BUT_ALIGN_RIGHT;
1854                 }
1855                 /* else do nothing, manually provided flags */
1856         }
1857         block->flag &= ~UI_BUT_ALIGN;   // all 4 flags
1858 }
1859 #endif
1860
1861 /*
1862 ui_def_but is the function that draws many button types
1863
1864 for float buttons:
1865         "a1" Click Step (how much to change the value each click)
1866         "a2" Number of decimal point values to display. 0 defaults to 3 (0.000) 1,2,3, and a maximum of 4,
1867        all greater values will be clamped to 4.
1868
1869 */
1870 static uiBut *ui_def_but(uiBlock *block, int type, int retval, char *str, short x1, short y1, short x2, short y2, void *poin, float min, float max, float a1, float a2,  char *tip)
1871 {
1872         uiBut *but;
1873         short slen;
1874         
1875         if(type & BUTPOIN) {            /* a pointer is required */
1876                 if(poin==NULL) {
1877                         /* if pointer is zero, button is removed and not drawn */
1878                         UI_ThemeColor(block->themecol);
1879                         glRects(x1,  y1,  x1+x2,  y1+y2);
1880                         return NULL;
1881                 }
1882         }
1883
1884         but= MEM_callocN(sizeof(uiBut), "uiBut");
1885
1886         but->type= type & BUTTYPE;
1887         but->pointype= type & BUTPOIN;
1888         but->bit= type & BIT;
1889         but->bitnr= type & 31;
1890         but->icon = 0;
1891
1892         BLI_addtail(&block->buttons, but);
1893
1894         but->retval= retval;
1895         if( strlen(str)>=UI_MAX_NAME_STR-1 ) {
1896                 but->str= MEM_callocN( strlen(str)+2, "uiDefBut");
1897                 strcpy(but->str, str);
1898         }
1899         else {
1900                 but->str= but->strdata;
1901                 strcpy(but->str, str);
1902         }
1903         but->x1= x1; 
1904         but->y1= y1;
1905         if(block->autofill) {
1906                 but->x2= x2; 
1907                 but->y2= y2;
1908         }
1909         else {
1910                 but->x2= (x1+x2); 
1911                 but->y2= (y1+y2);
1912         }
1913         but->poin= poin;
1914         but->min= min; 
1915         but->max= max;
1916         but->a1= a1; 
1917         but->a2= a2;
1918         but->tip= tip;
1919         
1920         but->font= block->curfont;
1921         
1922         but->lock= block->lock;
1923         but->lockstr= block->lockstr;
1924
1925         but->aspect= block->aspect;
1926         but->win= block->win;
1927         but->block= block;              // pointer back, used for frontbuffer status, and picker
1928
1929         if(block->themecol==TH_AUTO) but->themecol= ui_auto_themecol(but);
1930         else but->themecol= block->themecol;
1931         
1932         if (but->type==BUTM) {
1933                 but->butm_func= block->butm_func;
1934                 but->butm_func_arg= block->butm_func_arg;
1935         } else {
1936                 but->func= block->func;
1937                 but->func_arg1= block->func_arg1;
1938                 but->func_arg2= block->func_arg2;
1939         }
1940
1941         ui_set_embossfunc(but, block->dt);
1942         
1943         but->pos= -1;   /* cursor invisible */
1944
1945         if(ELEM(but->type, NUM, NUMABS)) {      /* add a space to name */
1946                 slen= strlen(but->str);
1947                 if(slen>0 && slen<UI_MAX_NAME_STR-2) {
1948                         if(but->str[slen-1]!=' ') {
1949                                 but->str[slen]= ' ';
1950                                 but->str[slen+1]= 0;
1951                         }
1952                 }
1953         }
1954         
1955         if(but->type==HSVCUBE) { /* hsv buttons temp storage */
1956                 float rgb[3];
1957                 ui_get_but_vectorf(but, rgb);
1958                 rgb_to_hsv(rgb[0], rgb[1], rgb[2], but->hsv, but->hsv+1, but->hsv+2);
1959         }
1960
1961         if ELEM8(but->type, HSVSLI , NUMSLI, MENU, TEX, LABEL, IDPOIN, BLOCK, BUTM) {
1962                 but->flag |= UI_TEXT_LEFT;
1963         }
1964         
1965         if(but->type==BUT_TOGDUAL) {
1966                 but->flag |= UI_ICON_LEFT;
1967         }
1968
1969         if(but->type==ROUNDBOX)
1970                 but->flag |= UI_NO_HILITE;
1971
1972         but->flag |= (block->flag & UI_BUT_ALIGN);
1973         if(block->flag & UI_BLOCK_NO_HILITE)
1974                 but->flag |= UI_NO_HILITE;
1975
1976         return but;
1977 }
1978
1979 uiBut *uiDefBut(uiBlock *block, int type, int retval, char *str, short x1, short y1, short x2, short y2, void *poin, float min, float max, float a1, float a2,  char *tip)
1980 {
1981         uiBut *but= ui_def_but(block, type, retval, str, x1, y1, x2, y2, poin, min, max, a1, a2, tip);
1982
1983         ui_check_but(but);
1984         
1985         return but;
1986 }
1987
1988         /* if _x_ is a power of two (only one bit) return the power,
1989          * otherwise return -1. 
1990          * (1<<findBitIndex(x))==x for powers of two.
1991          */
1992 static int findBitIndex(unsigned int x) {
1993         if (!x || (x&(x-1))!=0) {       /* x&(x-1) strips lowest bit */
1994                 return -1;
1995         } else {
1996                 int idx= 0;
1997
1998                 if (x&0xFFFF0000)       idx+=16, x>>=16;
1999                 if (x&0xFF00)           idx+=8, x>>=8;
2000                 if (x&0xF0)                     idx+=4, x>>=4;
2001                 if (x&0xC)                      idx+=2, x>>=2;
2002                 if (x&0x2)                      idx+=1;
2003
2004                 return idx;
2005         }
2006 }
2007
2008 /* autocomplete helper functions */
2009 struct AutoComplete {
2010         int maxlen;
2011         char *truncate;
2012         char *startname;
2013 };
2014
2015 AutoComplete *autocomplete_begin(char *startname, int maxlen)
2016 {
2017         AutoComplete *autocpl;
2018         
2019         autocpl= MEM_callocN(sizeof(AutoComplete), "AutoComplete");
2020         autocpl->maxlen= maxlen;
2021         autocpl->truncate= MEM_callocN(sizeof(char)*maxlen, "AutoCompleteTruncate");
2022         autocpl->startname= startname;
2023
2024         return autocpl;
2025 }
2026
2027 void autocomplete_do_name(AutoComplete *autocpl, const char *name)
2028 {
2029         char *truncate= autocpl->truncate;
2030         char *startname= autocpl->startname;
2031         int a;
2032
2033         for(a=0; a<autocpl->maxlen-1; a++) {
2034                 if(startname[a]==0 || startname[a]!=name[a])
2035                         break;
2036         }
2037         /* found a match */
2038         if(startname[a]==0) {
2039                 /* first match */
2040                 if(truncate[0]==0)
2041                         BLI_strncpy(truncate, name, autocpl->maxlen);
2042                 else {
2043                         /* remove from truncate what is not in bone->name */
2044                         for(a=0; a<autocpl->maxlen-1; a++) {
2045                                 if(truncate[a]!=name[a])
2046                                         truncate[a]= 0;
2047                         }
2048                 }
2049         }
2050 }
2051
2052 void autocomplete_end(AutoComplete *autocpl, char *autoname)
2053 {
2054         if(autocpl->truncate[0])
2055                 BLI_strncpy(autoname, autocpl->truncate, autocpl->maxlen);
2056         else
2057                 BLI_strncpy(autoname, autocpl->startname, autocpl->maxlen);
2058
2059         MEM_freeN(autocpl->truncate);
2060         MEM_freeN(autocpl);
2061 }
2062
2063 /* autocomplete callback for ID buttons */
2064 static void autocomplete_id(char *str, void *arg_v)
2065 {
2066         /* XXX 2.48 int blocktype= (intptr_t)arg_v; */
2067         ListBase *listb= NULL /* XXX 2.50 needs context, wich_libbase(G.main, blocktype) */;
2068         
2069         if(listb==NULL) return;
2070         
2071         /* search if str matches the beginning of an ID struct */
2072         if(str[0]) {
2073                 AutoComplete *autocpl= autocomplete_begin(str, 22);
2074                 ID *id;
2075                 
2076                 for(id= listb->first; id; id= id->next)
2077                         autocomplete_do_name(autocpl, id->name+2);
2078
2079                 autocomplete_end(autocpl, str);
2080         }
2081 }
2082
2083 static uiBut *uiDefButBit(uiBlock *block, int type, int bit, int retval, char *str, short x1, short y1, short x2, short y2, void *poin, float min, float max, float a1, float a2,  char *tip)
2084 {
2085         int bitIdx= findBitIndex(bit);
2086         if (bitIdx==-1) {
2087                 return NULL;
2088         } else {
2089                 return uiDefBut(block, type|BIT|bitIdx, retval, str, x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2090         }
2091 }
2092 uiBut *uiDefButF(uiBlock *block, int type, int retval, char *str, short x1, short y1, short x2, short y2, float *poin, float min, float max, float a1, float a2,  char *tip)
2093 {
2094         return uiDefBut(block, type|FLO, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2095 }
2096 uiBut *uiDefButBitF(uiBlock *block, int type, int bit, int retval, char *str, short x1, short y1, short x2, short y2, float *poin, float min, float max, float a1, float a2,  char *tip)
2097 {
2098         return uiDefButBit(block, type|FLO, bit, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2099 }
2100 uiBut *uiDefButI(uiBlock *block, int type, int retval, char *str, short x1, short y1, short x2, short y2, int *poin, float min, float max, float a1, float a2,  char *tip)
2101 {
2102         return uiDefBut(block, type|INT, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2103 }
2104 uiBut *uiDefButBitI(uiBlock *block, int type, int bit, int retval, char *str, short x1, short y1, short x2, short y2, int *poin, float min, float max, float a1, float a2,  char *tip)
2105 {
2106         return uiDefButBit(block, type|INT, bit, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2107 }
2108 uiBut *uiDefButS(uiBlock *block, int type, int retval, char *str, short x1, short y1, short x2, short y2, short *poin, float min, float max, float a1, float a2,  char *tip)
2109 {
2110         return uiDefBut(block, type|SHO, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2111 }
2112 uiBut *uiDefButBitS(uiBlock *block, int type, int bit, int retval, char *str, short x1, short y1, short x2, short y2, short *poin, float min, float max, float a1, float a2,  char *tip)
2113 {
2114         return uiDefButBit(block, type|SHO, bit, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2115 }
2116 uiBut *uiDefButC(uiBlock *block, int type, int retval, char *str, short x1, short y1, short x2, short y2, char *poin, float min, float max, float a1, float a2,  char *tip)
2117 {
2118         return uiDefBut(block, type|CHA, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2119 }
2120 uiBut *uiDefButBitC(uiBlock *block, int type, int bit, int retval, char *str, short x1, short y1, short x2, short y2, char *poin, float min, float max, float a1, float a2,  char *tip)
2121 {
2122         return uiDefButBit(block, type|CHA, bit, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2123 }
2124
2125 uiBut *uiDefIconBut(uiBlock *block, int type, int retval, int icon, short x1, short y1, short x2, short y2, void *poin, float min, float max, float a1, float a2,  char *tip)
2126 {
2127         uiBut *but= ui_def_but(block, type, retval, "", x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2128         
2129         but->icon= (BIFIconID) icon;
2130         but->flag|= UI_HAS_ICON;
2131
2132         ui_check_but(but);
2133         
2134         return but;
2135 }
2136 static uiBut *uiDefIconButBit(uiBlock *block, int type, int bit, int retval, int icon, short x1, short y1, short x2, short y2, void *poin, float min, float max, float a1, float a2,  char *tip)
2137 {
2138         int bitIdx= findBitIndex(bit);
2139         if (bitIdx==-1) {
2140                 return NULL;
2141         } else {
2142                 return uiDefIconBut(block, type|BIT|bitIdx, retval, icon, x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2143         }
2144 }
2145
2146 uiBut *uiDefIconButF(uiBlock *block, int type, int retval, int icon, short x1, short y1, short x2, short y2, float *poin, float min, float max, float a1, float a2,  char *tip)
2147 {
2148         return uiDefIconBut(block, type|FLO, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2149 }
2150 uiBut *uiDefIconButBitF(uiBlock *block, int type, int bit, int retval, int icon, short x1, short y1, short x2, short y2, float *poin, float min, float max, float a1, float a2,  char *tip)
2151 {
2152         return uiDefIconButBit(block, type|FLO, bit, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2153 }
2154 uiBut *uiDefIconButI(uiBlock *block, int type, int retval, int icon, short x1, short y1, short x2, short y2, int *poin, float min, float max, float a1, float a2,  char *tip)
2155 {
2156         return uiDefIconBut(block, type|INT, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2157 }
2158 uiBut *uiDefIconButBitI(uiBlock *block, int type, int bit, int retval, int icon, short x1, short y1, short x2, short y2, int *poin, float min, float max, float a1, float a2,  char *tip)
2159 {
2160         return uiDefIconButBit(block, type|INT, bit, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2161 }
2162 uiBut *uiDefIconButS(uiBlock *block, int type, int retval, int icon, short x1, short y1, short x2, short y2, short *poin, float min, float max, float a1, float a2,  char *tip)
2163 {
2164         return uiDefIconBut(block, type|SHO, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2165 }
2166 uiBut *uiDefIconButBitS(uiBlock *block, int type, int bit, int retval, int icon, short x1, short y1, short x2, short y2, short *poin, float min, float max, float a1, float a2,  char *tip)
2167 {
2168         return uiDefIconButBit(block, type|SHO, bit, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2169 }
2170 uiBut *uiDefIconButC(uiBlock *block, int type, int retval, int icon, short x1, short y1, short x2, short y2, char *poin, float min, float max, float a1, float a2,  char *tip)
2171 {
2172         return uiDefIconBut(block, type|CHA, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2173 }
2174 uiBut *uiDefIconButBitC(uiBlock *block, int type, int bit, int retval, int icon, short x1, short y1, short x2, short y2, char *poin, float min, float max, float a1, float a2,  char *tip)
2175 {
2176         return uiDefIconButBit(block, type|CHA, bit, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2177 }
2178
2179 /* Button containing both string label and icon */
2180 uiBut *uiDefIconTextBut(uiBlock *block, int type, int retval, int icon, char *str, short x1, short y1, short x2, short y2, void *poin, float min, float max, float a1, float a2,  char *tip)
2181 {
2182         uiBut *but= ui_def_but(block, type, retval, str, x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2183
2184         but->icon= (BIFIconID) icon;
2185         but->flag|= UI_HAS_ICON;
2186
2187         but->flag|= UI_ICON_LEFT;
2188
2189         ui_check_but(but);
2190
2191         return but;
2192 }
2193 static uiBut *uiDefIconTextButBit(uiBlock *block, int type, int bit, int retval, int icon, char *str, short x1, short y1, short x2, short y2, void *poin, float min, float max, float a1, float a2,  char *tip)
2194 {
2195         int bitIdx= findBitIndex(bit);
2196         if (bitIdx==-1) {
2197                 return NULL;
2198         } else {
2199                 return uiDefIconTextBut(block, type|BIT|bitIdx, retval, icon, str, x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2200         }
2201 }
2202
2203 uiBut *uiDefIconTextButF(uiBlock *block, int type, int retval, int icon, char *str, short x1, short y1, short x2, short y2, float *poin, float min, float max, float a1, float a2,  char *tip)
2204 {
2205         return uiDefIconTextBut(block, type|FLO, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2206 }
2207 uiBut *uiDefIconTextButBitF(uiBlock *block, int type, int bit, int retval, int icon, char *str, short x1, short y1, short x2, short y2, float *poin, float min, float max, float a1, float a2,  char *tip)
2208 {
2209         return uiDefIconTextButBit(block, type|FLO, bit, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2210 }
2211 uiBut *uiDefIconTextButI(uiBlock *block, int type, int retval, int icon, char *str, short x1, short y1, short x2, short y2, int *poin, float min, float max, float a1, float a2,  char *tip)
2212 {
2213         return uiDefIconTextBut(block, type|INT, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2214 }
2215 uiBut *uiDefIconTextButBitI(uiBlock *block, int type, int bit, int retval, int icon, char *str, short x1, short y1, short x2, short y2, int *poin, float min, float max, float a1, float a2,  char *tip)
2216 {
2217         return uiDefIconTextButBit(block, type|INT, bit, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2218 }
2219 uiBut *uiDefIconTextButS(uiBlock *block, int type, int retval, int icon, char *str, short x1, short y1, short x2, short y2, short *poin, float min, float max, float a1, float a2,  char *tip)
2220 {
2221         return uiDefIconTextBut(block, type|SHO, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2222 }
2223 uiBut *uiDefIconTextButBitS(uiBlock *block, int type, int bit, int retval, int icon, char *str, short x1, short y1, short x2, short y2, short *poin, float min, float max, float a1, float a2,  char *tip)
2224 {
2225         return uiDefIconTextButBit(block, type|SHO, bit, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2226 }
2227 uiBut *uiDefIconTextButC(uiBlock *block, int type, int retval, int icon, char *str, short x1, short y1, short x2, short y2, char *poin, float min, float max, float a1, float a2,  char *tip)
2228 {
2229         return uiDefIconTextBut(block, type|CHA, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2230 }
2231 uiBut *uiDefIconTextButBitC(uiBlock *block, int type, int bit, int retval, int icon, char *str, short x1, short y1, short x2, short y2, char *poin, float min, float max, float a1, float a2,  char *tip)
2232 {
2233         return uiDefIconTextButBit(block, type|CHA, bit, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2234 }
2235
2236 uiBut *uiDefRNABut(uiBlock *block, int retval, PointerRNA *ptr, PropertyRNA *prop, int index, short x1, short y1, short x2, short y2)
2237 {
2238         uiBut *but;
2239
2240         switch(RNA_property_type(ptr, prop)) {
2241                 case PROP_BOOLEAN: {
2242                         int value, length;
2243
2244                         length= RNA_property_array_length(ptr, prop);
2245
2246                         if(length)
2247                                 value= RNA_property_boolean_get_array(ptr, prop, index);
2248                         else
2249                                 value= RNA_property_boolean_get(ptr, prop);
2250
2251                         but= ui_def_but(block, TOG, 0, (value)? "True": "False", x1, y1, x2, y2, NULL, 0, 0, 0, 0, (char*)RNA_property_ui_description(ptr, prop));
2252                         break;
2253                 }
2254                 case PROP_INT: {
2255                         int softmin, softmax, step;
2256
2257                         RNA_property_int_ui_range(ptr, prop, &softmin, &softmax, &step);
2258                         but= ui_def_but(block, NUM, 0, "", x1, y1, x2, y2, NULL, softmin, softmax, step, 0, (char*)RNA_property_ui_description(ptr, prop));
2259                         break;
2260                 }
2261                 case PROP_FLOAT: {
2262                         float softmin, softmax, step, precision;
2263
2264                         RNA_property_float_ui_range(ptr, prop, &softmin, &softmax, &step, &precision);
2265                         but= ui_def_but(block, NUM, 0, "", x1, y1, x2, y2, NULL, softmin, softmax, step, precision, (char*)RNA_property_ui_description(ptr, prop));
2266                         break;
2267                 }
2268                 case PROP_ENUM: {
2269                         const EnumPropertyItem *item;
2270                         DynStr *dynstr;
2271                         char *menu;
2272                         int i, totitem;
2273
2274                         RNA_property_enum_items(ptr, prop, &item, &totitem);
2275
2276                         dynstr= BLI_dynstr_new();
2277                         BLI_dynstr_appendf(dynstr, "%s%%t", RNA_property_ui_name(ptr, prop));
2278                         for(i=0; i<totitem; i++)
2279                                 BLI_dynstr_appendf(dynstr, "|%s %%x%d", item[i].name, item[i].value);
2280                         menu= BLI_dynstr_get_cstring(dynstr);
2281                         BLI_dynstr_free(dynstr);
2282
2283                         but= ui_def_but(block, MENU, 0, menu, x1, y1, x2, y2, NULL, 0, 0, 0, 0, (char*)RNA_property_ui_description(ptr, prop));
2284                         MEM_freeN(menu);
2285                         break;
2286                 }
2287                 case PROP_STRING: {
2288                         int maxlength;
2289
2290                         maxlength= RNA_property_string_maxlength(ptr, prop);
2291                         if(maxlength == 0)
2292                                 /* interface code should ideally support unlimited length */
2293                                 maxlength= UI_MAX_DRAW_STR; 
2294
2295                         but= ui_def_but(block, TEX, 0, "", x1, y1, x2, y2, NULL, 0, maxlength, 0, 0, (char*)RNA_property_ui_description(ptr, prop));
2296                         break;
2297                 }
2298                 case PROP_POINTER: {
2299                         PointerRNA pptr;
2300                         PropertyRNA *nameprop;
2301                         char name[256]= "", *nameptr= name;
2302
2303                         RNA_property_pointer_get(ptr, prop, &pptr);
2304
2305                         if(pptr.data) {
2306                                 nameprop= RNA_struct_name_property(&pptr);
2307                                 if(pptr.type && nameprop)
2308                                         nameptr= RNA_property_string_get_alloc(&pptr, nameprop, name, sizeof(name));
2309                                 else
2310                                         strcpy(nameptr, "->");
2311                         }
2312
2313                         but= ui_def_but(block, BUT, 0, nameptr, x1, y1, x2, y2, NULL, 0, 0, 0, 0, (char*)RNA_property_ui_description(ptr, prop));
2314                         but->flag |= UI_TEXT_LEFT;
2315
2316                         if(nameptr != name)
2317                                 MEM_freeN(nameptr);
2318
2319                         break;
2320                 }
2321                 default:
2322                         but= NULL;
2323                         break;
2324         }
2325
2326         if(but) {
2327                 but->rnapoin= *ptr;
2328                 but->rnaprop= prop;
2329                 but->rnaindex= index;
2330
2331                 ui_check_but(but);
2332         }
2333
2334         return but;
2335 }
2336
2337 /* END Button containing both string label and icon */
2338
2339 void uiAutoBlock(uiBlock *block, float minx, float miny, float sizex, float sizey, int flag)
2340 {
2341         block->minx= minx;
2342         block->maxx= minx+sizex;
2343         block->miny= miny;
2344         block->maxy= miny+sizey;
2345         
2346         block->autofill= flag;  /* also check for if it has to be done */
2347
2348 }
2349
2350 void uiSetButLink(uiBut *but, void **poin, void ***ppoin, short *tot, int from, int to)
2351 {
2352         uiLink *link;
2353         
2354         link= but->link= MEM_callocN(sizeof(uiLink), "new uilink");
2355         
2356         link->poin= poin;
2357         link->ppoin= ppoin;
2358         link->totlink= tot;
2359         link->fromcode= from;
2360         link->tocode= to;
2361 }
2362
2363 /* cruft to make uiBlock and uiBut private */
2364
2365 int uiBlocksGetYMin(ListBase *lb)
2366 {
2367         uiBlock *block;
2368         int min= 0;
2369         
2370         for (block= lb->first; block; block= block->next)
2371                 if (block==lb->first || block->miny<min)
2372                         min= block->miny;
2373                         
2374         return min;
2375 }
2376
2377 int uiBlockGetCol(uiBlock *block)
2378 {
2379         return block->themecol;
2380 }
2381 void uiBlockSetCol(uiBlock *block, int col)
2382 {
2383         block->themecol= col;
2384 }
2385 void uiBlockSetEmboss(uiBlock *block, int emboss)
2386 {
2387         block->dt= emboss;
2388 }
2389 void uiBlockSetDirection(uiBlock *block, int direction)
2390 {
2391         block->direction= direction;
2392 }
2393
2394 /* this call escapes if there's alignment flags */
2395 void uiBlockFlipOrder(uiBlock *block)
2396 {
2397         ListBase lb;
2398         uiBut *but, *next;
2399         float centy, miny=10000, maxy= -10000;
2400
2401 //      if(U.uiflag & USER_PLAINMENUS)
2402 //              return;
2403         
2404         for(but= block->buttons.first; but; but= but->next) {
2405                 if(but->flag & UI_BUT_ALIGN) return;
2406                 if(but->y1 < miny) miny= but->y1;
2407                 if(but->y2 > maxy) maxy= but->y2;
2408         }
2409         /* mirror trick */
2410         centy= (miny+maxy)/2.0;
2411         for(but= block->buttons.first; but; but= but->next) {
2412                 but->y1 = centy-(but->y1-centy);
2413                 but->y2 = centy-(but->y2-centy);
2414                 SWAP(float, but->y1, but->y2);
2415         }
2416         
2417         /* also flip order in block itself, for example for arrowkey */
2418         lb.first= lb.last= NULL;
2419         but= block->buttons.first;
2420         while(but) {
2421                 next= but->next;
2422                 BLI_remlink(&block->buttons, but);
2423                 BLI_addtail(&lb, but);
2424                 but= next;
2425         }
2426         block->buttons= lb;
2427 }
2428
2429
2430 void uiBlockSetFlag(uiBlock *block, int flag)
2431 {
2432         block->flag= flag;
2433 }
2434 void uiBlockSetXOfs(uiBlock *block, int xofs)
2435 {
2436         block->xofs= xofs;
2437 }
2438 void* uiBlockGetCurFont(uiBlock *block)
2439 {
2440         return block->curfont;
2441 }
2442
2443 void uiButSetFlag(uiBut *but, int flag)
2444 {
2445         but->flag|= flag;
2446 }
2447 void uiButClearFlag(uiBut *but, int flag)
2448 {
2449         but->flag&= ~flag;
2450 }
2451
2452 int uiButGetRetVal(uiBut *but)
2453 {
2454         return but->retval;
2455 }
2456
2457 /* Call this function BEFORE adding buttons to the block */
2458 void uiBlockSetButmFunc(uiBlock *block, void (*menufunc)(void *arg, int event), void *arg)
2459 {
2460         block->butm_func= menufunc;
2461         block->butm_func_arg= arg;
2462 }
2463
2464 void uiBlockSetFunc(uiBlock *block, void (*func)(void *arg1, void *arg2), void *arg1, void *arg2)
2465 {
2466         block->func= func;
2467         block->func_arg1= arg1;
2468         block->func_arg2= arg2;
2469 }
2470
2471 void uiBlockSetDrawExtraFunc(uiBlock *block, void (*func)())
2472 {
2473         block->drawextra= func;
2474 }
2475
2476 void uiButSetFunc(uiBut *but, void (*func)(void *arg1, void *arg2), void *arg1, void *arg2)
2477 {
2478         but->func= func;
2479         but->func_arg1= arg1;
2480         but->func_arg2= arg2;
2481 }
2482
2483 void uiButSetFunc3(uiBut *but, void (*func)(void *arg1, void *arg2, void *arg3), void *arg1, void *arg2, void *arg3)
2484 {
2485         but->func3= func;
2486         but->func_arg1= arg1;
2487         but->func_arg2= arg2;
2488         but->func_arg3= arg3;
2489 }
2490
2491 void uiButSetCompleteFunc(uiBut *but, void (*func)(char *str, void *arg), void *arg)
2492 {
2493         but->autocomplete_func= func;
2494         but->autofunc_arg= arg;
2495 }
2496
2497 uiBut *uiDefIDPoinBut(uiBlock *block, uiIDPoinFuncFP func, short blocktype, int retval, char *str, short x1, short y1, short x2, short y2, void *idpp, char *tip)
2498 {
2499         uiBut *but= ui_def_but(block, IDPOIN, retval, str, x1, y1, x2, y2, NULL, 0.0, 0.0, 0.0, 0.0, tip);
2500         but->idpoin_func= func;
2501         but->idpoin_idpp= (ID**) idpp;
2502         ui_check_but(but);
2503         
2504         if(blocktype)
2505                 uiButSetCompleteFunc(but, autocomplete_id, 0 /* XXX 2.48 (void *)(intptr_t)blocktype*/);
2506
2507         return but;
2508 }
2509
2510 uiBut *uiDefBlockBut(uiBlock *block, uiBlockFuncFP func, void *arg, char *str, short x1, short y1, short x2, short y2, char *tip)
2511 {
2512         uiBut *but= ui_def_but(block, BLOCK, 0, str, x1, y1, x2, y2, arg, 0.0, 0.0, 0.0, 0.0, tip);
2513         but->block_func= func;
2514         ui_check_but(but);
2515         return but;
2516 }
2517
2518 uiBut *uiDefPulldownBut(uiBlock *block, uiBlockFuncFP func, void *arg, char *str, short x1, short y1, short x2, short y2, char *tip)
2519 {
2520         uiBut *but= ui_def_but(block, PULLDOWN, 0, str, x1, y1, x2, y2, arg, 0.0, 0.0, 0.0, 0.0, tip);
2521         but->block_func= func;
2522         ui_check_but(but);
2523         return but;
2524 }
2525
2526 /* Block button containing both string label and icon */
2527 uiBut *uiDefIconTextBlockBut(uiBlock *block, uiBlockFuncFP func, void *arg, int icon, char *str, short x1, short y1, short x2, short y2, char *tip)
2528 {
2529         uiBut *but= ui_def_but(block, BLOCK, 0, str, x1, y1, x2, y2, arg, 0.0, 0.0, 0.0, 0.0, tip);
2530         
2531         but->icon= (BIFIconID) icon;
2532         but->flag|= UI_HAS_ICON;
2533
2534         but->flag|= UI_ICON_LEFT;
2535         but->flag|= UI_ICON_RIGHT;
2536
2537         but->block_func= func;
2538         ui_check_but(but);
2539         
2540         return but;
2541 }
2542
2543 /* Block button containing icon */
2544 uiBut *uiDefIconBlockBut(uiBlock *block, uiBlockFuncFP func, void *arg, int retval, int icon, short x1, short y1, short x2, short y2, char *tip)
2545 {
2546         uiBut *but= ui_def_but(block, BLOCK, retval, "", x1, y1, x2, y2, arg, 0.0, 0.0, 0.0, 0.0, tip);
2547         
2548         but->icon= (BIFIconID) icon;
2549         but->flag|= UI_HAS_ICON;
2550         
2551         but->flag|= UI_ICON_LEFT;
2552         but->flag|= UI_ICON_RIGHT;
2553         
2554         but->block_func= func;
2555         ui_check_but(but);
2556         
2557         return but;
2558 }
2559
2560 void uiDefKeyevtButS(uiBlock *block, int retval, char *str, short x1, short y1, short x2, short y2, short *spoin, char *tip)
2561 {
2562         uiBut *but= ui_def_but(block, KEYEVT|SHO, retval, str, x1, y1, x2, y2, spoin, 0.0, 0.0, 0.0, 0.0, tip);
2563         ui_check_but(but);
2564 }
2565
2566 /* Program Init/Exit */
2567
2568 void UI_init(void)
2569 {
2570         uiDefFont(UI_HELVB,
2571                                 BMF_GetFont(BMF_kHelveticaBold14),
2572                                 BMF_GetFont(BMF_kHelveticaBold12),
2573                                 BMF_GetFont(BMF_kHelveticaBold10),
2574                                 BMF_GetFont(BMF_kHelveticaBold8));
2575         uiDefFont(UI_HELV,
2576                                 BMF_GetFont(BMF_kHelvetica12),
2577                                 BMF_GetFont(BMF_kHelvetica12),
2578                                 BMF_GetFont(BMF_kHelvetica10),
2579                                 BMF_GetFont(BMF_kHelveticaBold8));
2580
2581         ui_resources_init();
2582 }
2583
2584 void UI_init_userdef()
2585 {
2586         ui_text_init_userdef();
2587         ui_theme_init_userdef();
2588 }
2589
2590 void UI_exit(void)
2591 {
2592         ui_resources_free();
2593 }
2594