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