Modified interface texture font support, which should work nicely with the freshly...
[blender.git] / source / blender / src / interface.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL/BL DUAL 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. The Blender
10  * Foundation also sells licenses for use in proprietary software under
11  * the Blender License.  See http://www.blender.org/BL/ for information
12  * about this.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22  *
23  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24  * All rights reserved.
25  *
26  * The Original Code is: all of this file.
27  *
28  * Contributor(s): none yet.
29  *
30  * ***** END GPL/BL DUAL LICENSE BLOCK *****
31  */
32
33 /* 
34      a full doc with API notes can be found in bf-blender/blender/doc/interface_API.txt
35
36  */
37  
38
39 #include <math.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <ctype.h>
43
44 #ifdef HAVE_CONFIG_H
45 #include <config.h>
46 #endif
47
48 #ifndef WIN32
49 #include <unistd.h>
50 #else
51 #include <io.h>
52 #include "BLI_winstuff.h"
53 #endif   
54
55 #include "MEM_guardedalloc.h"
56
57 #include "PIL_time.h"
58
59 #include "BMF_Api.h"
60 #include "BIF_language.h"
61 #ifdef INTERNATIONAL
62 #include "FTF_Api.h"
63 #endif // INTERNATIONAL
64
65 #include "BLI_blenlib.h"
66 #include "BLI_arithb.h"
67
68 #include "DNA_screen_types.h"
69 #include "DNA_space_types.h"
70 #include "DNA_userdef_types.h"
71 #include "DNA_vec_types.h"
72
73 #include "BKE_blender.h"
74 #include "BKE_utildefines.h"
75 #include "BKE_global.h"
76
77 #include "BIF_gl.h"
78 #include "BIF_graphics.h"
79 #include "BIF_keyval.h"
80 #include "BIF_mainqueue.h"
81 #include "BIF_resources.h"
82 #include "BIF_screen.h"
83 #include "BIF_toolbox.h"
84 #include "BIF_mywindow.h"
85 #include "BIF_space.h"
86 #include "BIF_glutil.h"
87 #include "BIF_interface.h"
88 #include "BIF_butspace.h"
89
90 #include "BSE_view.h"
91
92 #include "mydevice.h"
93 #include "interface.h"
94 #include "blendef.h"
95
96 /* naming conventions:
97  * 
98  * uiBlahBlah()         external function
99  * ui_blah_blah()       internal function
100  */
101
102 /***/
103 /* ************ GLOBALS ************* */
104
105 float UIwinmat[4][4];
106 static int UIlock= 0, UIafterval;
107 static char *UIlockstr=NULL;
108 static void (*UIafterfunc)(void *arg, int event);
109 static void *UIafterfunc_arg;
110
111 static uiFont UIfont[UI_ARRAY];  // no init needed
112 uiBut *UIbuttip;
113
114 /* ************* PROTOTYPES ***************** */
115
116 static void ui_set_but_val(uiBut *but, double value);
117 static void ui_set_ftf_font(uiBlock *block);
118 static void ui_do_but_tip(uiBut *buttip);
119
120 /* ****************************** */
121
122 static int uibut_contains_pt(uiBut *but, short *pt)
123 {
124         return ((but->x1<pt[0] && but->x2>=pt[0]) && 
125                         (but->y1<pt[1] && but->y2>=pt[1]));
126 }
127
128 static void uibut_do_func(uiBut *but)
129 {
130         if (but->func) {
131                 but->func(but->func_arg1, but->func_arg2);
132         }
133 }
134
135 /* ************* window matrix ************** */
136
137
138 void ui_graphics_to_window(int win, float *x, float *y) /* for rectwrite  */
139 {
140         float gx, gy;
141         int sx, sy;
142         int getsizex, getsizey;
143
144         bwin_getsize(win, &getsizex, &getsizey);
145         bwin_getsuborigin(win, &sx, &sy);
146
147         gx= *x;
148         gy= *y;
149         *x= ((float)sx) + ((float)getsizex)*(0.5+ 0.5*(gx*UIwinmat[0][0]+ gy*UIwinmat[1][0]+ UIwinmat[3][0]));
150         *y= ((float)sy) + ((float)getsizey)*(0.5+ 0.5*(gx*UIwinmat[0][1]+ gy*UIwinmat[1][1]+ UIwinmat[3][1]));
151 }
152
153
154
155 void ui_window_to_graphics(int win, float *x, float *y) /* for mouse cursor */
156 {
157         float a, b, c, d, e, f, px, py;
158         int getsizex, getsizey;
159                 
160         bwin_getsize(win, &getsizex, &getsizey);
161
162         a= .5*((float)getsizex)*UIwinmat[0][0];
163         b= .5*((float)getsizex)*UIwinmat[1][0];
164         c= .5*((float)getsizex)*(1.0+UIwinmat[3][0]);
165
166         d= .5*((float)getsizey)*UIwinmat[0][1];
167         e= .5*((float)getsizey)*UIwinmat[1][1];
168         f= .5*((float)getsizey)*(1.0+UIwinmat[3][1]);
169         
170         px= *x;
171         py= *y;
172         
173         *y=  (a*(py-f) + d*(c-px))/(a*e-d*b);
174         *x= (px- b*(*y)- c)/a;
175         
176 }
177
178
179 /* ************* SAVE UNDER ************ */
180
181 /* new method: 
182
183 OverDraw *ui_begin_overdraw(int minx, int miny, int maxx, int maxy);
184 - enforces mainwindow to become active
185 - grabs copy from frontbuffer, pastes in back
186
187 void ui_flush_overdraw(OverDraw *od);
188 - copies backbuffer to front
189
190 void ui_refresh_overdraw(Overdraw *od);
191 - pastes in back copy of frontbuffer again for fresh drawing
192
193 void ui_end_overdraw(OverDraw *od);
194 - puts back on frontbuffer saved image
195 - frees copy
196 - sets back active blender area
197 - signals backbuffer to be corrupt (sel buffer!)
198
199 */
200
201 /* frontbuffer updates now glCopyPixels too, with block->flush rect */
202
203 /* new idea for frontbuffer updates:
204
205 - hilites: with blended poly?
206
207 - full updates... thats harder, but:
208   - copy original
209   - before draw, always paste to backbuf
210   - flush
211   - always end with redraw event for full update
212
213 */
214
215 static void myglCopyPixels(int a, int b, int c, int d, int e)
216 {
217         if(G.rt==2) {
218                 unsigned int *buf= MEM_mallocN(4*c*d, "temp glcopypixels");
219                 glReadPixels(a, b, c, d, GL_RGBA, GL_UNSIGNED_BYTE, buf);
220                 glDrawPixels(c, d, GL_RGBA, GL_UNSIGNED_BYTE, buf);
221                 MEM_freeN(buf);
222         }
223         else glCopyPixels(a, b, c, d, e);
224 }
225
226 typedef struct {
227         short x, y, sx, sy, oldwin;
228         unsigned int *rect;
229 } uiOverDraw;
230
231
232 static uiOverDraw *ui_begin_overdraw(int minx, int miny, int maxx, int maxy)
233 {
234         uiOverDraw *od=NULL;
235         
236         // dirty patch removed for sun and sgi to mywindow.c commented out
237         
238         /* clip with actual window size */
239         if(minx < 0) minx= 0;
240         if(miny < 0) miny= 0;
241         if(maxx >= G.curscreen->sizex) maxx= G.curscreen->sizex-1;
242         if(maxy >= G.curscreen->sizey) maxy= G.curscreen->sizey-1;
243
244         if(minx<maxx && miny<maxy) {
245                 od= MEM_callocN(sizeof(uiOverDraw), "overdraw");        
246                 
247                 od->x= minx;
248                 od->y= miny;
249                 od->sx= maxx-minx;
250                 od->sy= maxy-miny;
251                 od->rect= MEM_mallocN(od->sx*od->sy*4, "temp_frontbuffer_image");
252
253                 od->oldwin= mywinget();
254                 mywinset(G.curscreen->mainwin);
255                 /* grab front */
256                 glReadBuffer(GL_FRONT);
257                 glReadPixels(od->x, od->y, od->sx, od->sy, GL_RGBA, GL_UNSIGNED_BYTE, od->rect);
258                 glReadBuffer(GL_BACK);
259                 /* paste in back */
260                 glDisable(GL_DITHER);
261                 glRasterPos2f(od->x, od->y);
262                 glDrawPixels(od->sx, od->sy, GL_RGBA, GL_UNSIGNED_BYTE, od->rect);
263                 glEnable(GL_DITHER);
264         }
265         
266         return od;
267 }
268
269 static void ui_flush_overdraw(uiOverDraw *od)
270 {
271
272         if(od==NULL) return;
273         glDisable(GL_DITHER);
274         glReadBuffer(GL_BACK);
275         glDrawBuffer(GL_FRONT);
276         glRasterPos2s(od->x, od->y);
277         myglCopyPixels(od->x, od->y, od->sx, od->sy, GL_COLOR);
278         glEnable(GL_DITHER);
279         glFlush();
280         glDrawBuffer(GL_BACK);
281 }
282
283 /* special flush version to enable transparent menus */
284 static void ui_block_flush_overdraw(uiBlock *block)
285 {
286         
287         if(block->flag & UI_BLOCK_LOOP) {
288                 char col[4];
289                 
290                 BIF_GetThemeColor4ubv(TH_MENU_BACK, col);
291                 if(col[3]!=255) {
292                         uiBut *bt;
293                         uiOverDraw *od= block->overdraw;
294
295                         /* completely draw all! */
296                         glRasterPos2s(od->x, od->y);
297                         glDrawPixels(od->sx, od->sy, GL_RGBA, GL_UNSIGNED_BYTE, od->rect);
298                         
299                         uiDrawMenuBox(block->minx, block->miny, block->maxx, block->maxy, block->flag);
300                         for (bt= block->buttons.first; bt; bt= bt->next) {
301                                 ui_draw_but(bt);
302                         }
303                 }
304         }
305         
306         ui_flush_overdraw(block->overdraw);
307 }
308
309 static void ui_end_overdraw(uiOverDraw *od)
310 {
311         if(od==NULL) return;
312         
313         glDisable(GL_DITHER);
314
315         // clear in back
316         glRasterPos2s(od->x, od->y);
317         glDrawPixels(od->sx, od->sy, GL_RGBA, GL_UNSIGNED_BYTE, od->rect);
318
319         // clear in front
320         glDrawBuffer(GL_FRONT);
321         glRasterPos2s(od->x, od->y);
322         glDrawPixels(od->sx, od->sy, GL_RGBA, GL_UNSIGNED_BYTE, od->rect);
323
324         glFlush();
325         glDrawBuffer(GL_BACK);
326         glEnable(GL_DITHER);
327         
328         if(od->oldwin) mywinset(od->oldwin);
329         
330         MEM_freeN(od->rect);
331         MEM_freeN(od);
332
333         markdirty_all_back();   // sets flags only
334 }
335
336 /* ****************** live updates for hilites and button presses *********** */
337
338 void ui_block_flush_back(uiBlock *block)
339 {
340         int minx, miny, sizex, sizey;
341         
342         /* note; this routine also has to work for block loop */
343         if(block->needflush==0) return;
344
345         /* exception, when we cannot use backbuffer for draw... */
346         if(block->flag & UI_BLOCK_FRONTBUFFER) {
347                 glFlush();
348                 glDrawBuffer(GL_BACK);
349                 block->needflush= 0;
350                 return;
351         }
352         
353         /* copy pixels works on window coords, so we move to window space */
354
355         ui_graphics_to_window(block->win, &block->flush.xmin, &block->flush.ymin);
356         ui_graphics_to_window(block->win, &block->flush.xmax, &block->flush.ymax);
357         minx= floor(block->flush.xmin);
358         miny= floor(block->flush.ymin);
359         sizex= ceil(block->flush.xmax-block->flush.xmin);
360         sizey= ceil(block->flush.ymax-block->flush.ymin);
361
362         if(sizex>0 && sizey>0) {
363                 glPushMatrix();
364                 mywinset(G.curscreen->mainwin);
365                 
366                 glDisable(GL_DITHER);
367                 glReadBuffer(GL_BACK);
368                 glDrawBuffer(GL_FRONT);
369                 glRasterPos2i(minx, miny);
370 #ifdef __sun__          
371                 myglCopyPixels(minx, miny+1, sizex, sizey, GL_COLOR);
372 #else
373                 myglCopyPixels(minx, miny, sizex, sizey, GL_COLOR);
374 #endif
375                 glEnable(GL_DITHER);
376                 glFlush();
377                 glDrawBuffer(GL_BACK);
378
379                 mywinset(block->win);
380                 glPopMatrix();
381                 
382                 markdirty_win_back(block->win);
383         }
384
385         block->needflush= 0; 
386 }
387
388 /* merge info for live updates in frontbuf */
389 void ui_block_set_flush(uiBlock *block, uiBut *but)
390 {
391         /* clear signal */
392         if(but==NULL) {
393                 block->needflush= 0; 
394
395                 block->flush.xmin= 0.0;
396                 block->flush.xmax= 0.0;
397         }
398         else {
399                 /* exception, when we cannot use backbuffer for draw... */
400                 if(block->flag & UI_BLOCK_FRONTBUFFER) {
401                         glDrawBuffer(GL_FRONT);
402                 }
403                 else if(block->needflush==0) {
404                         /* first rect */
405                         block->flush.xmin= but->x1;
406                         block->flush.xmax= but->x2;
407                         block->flush.ymin= but->y1;
408                         block->flush.ymax= but->y2;
409                         
410                 }
411                 else {
412                         /* union of rects */
413                         if(block->flush.xmin > but->x1) block->flush.xmin= but->x1;
414                         if(block->flush.xmax < but->x2) block->flush.xmax= but->x2;
415                         if(block->flush.ymin > but->y1) block->flush.ymin= but->y1;
416                         if(block->flush.ymax < but->y2) block->flush.ymax= but->y2;
417                 }
418                 
419                 block->needflush= 1;
420                 
421         }
422 }
423
424 /* ******************* copy and paste ********************  */
425
426 /* c = copy, v = paste */
427 /* return 1 when something changed */
428 static int ui_but_copy_paste(uiBut *but, char mode)
429 {
430         static char str[256]="";
431         static double butval=0.0;
432         static float rgb[3];
433         void *poin;
434         
435         poin= but->poin;
436                 
437                 
438         if ELEM3(but->type, NUM, NUMSLI, HSVSLI) {
439         
440                 if(mode=='c') {
441                         butval= ui_get_but_val(but);
442                 }
443                 else {
444                         ui_set_but_val(but, butval);
445                         ui_check_but(but);
446                         return 1;
447                 }
448         }
449         else if(but->type==COL) {
450                 
451                 if(mode=='c') {
452                         if(but->pointype==FLO) {
453                                 float *fp= (float *) poin;
454                                 rgb[0]= fp[0];
455                                 rgb[1]= fp[1];
456                                 rgb[2]= fp[2];  
457                         }       
458                         else if (but->pointype==CHA) {
459                                 char *cp= (char *) poin;
460                                 rgb[0]= (float)(cp[0]/255.0);
461                                 rgb[1]= (float)(cp[1]/255.0);
462                                 rgb[2]= (float)(cp[2]/255.0);
463                         }
464                         
465                 }
466                 else {
467                         if(but->pointype==FLO) {
468                                 float *fp= (float *) poin;
469                                 fp[0] = rgb[0];
470                                 fp[1] = rgb[1];
471                                 fp[2] = rgb[2];
472                                 return 1;
473                         }
474                         else if (but->pointype==CHA) {
475                                 char *cp= (char *) poin;
476                                 cp[0] = (char)(rgb[0]*255.0);
477                                 cp[1] = (char)(rgb[1]*255.0);
478                                 cp[2] = (char)(rgb[2]*255.0);
479                                 
480                                 return 1;
481                         }
482                         
483                 }
484         }
485         else if(but->type==TEX) {
486                 
487                 if(mode=='c') {
488                         strncpy(str, but->poin, but->max);
489                 }
490                 else {
491                         strncpy(but->poin, str, but->max);
492                         uibut_do_func(but);
493                         ui_check_but(but);
494                         return 1;
495                 }
496         }
497         else if(but->type==IDPOIN) {
498                 
499                 if(mode=='c') {
500                         ID *id= *but->idpoin_idpp;
501                         if(id) strncpy(str, id->name+2, 22);
502                 }
503                 else {
504                         but->idpoin_func(str, but->idpoin_idpp);
505                         ui_check_but(but);
506                         return 1;
507                 }
508         }
509                 
510                         
511         return 0;
512 }
513
514 /* ******************* block calc ************************* */
515
516 void uiTextBoundsBlock(uiBlock *block, int addval)
517 {
518         uiBut *bt;
519         int i = 0, j;
520         
521         bt= block->buttons.first;
522         while(bt) {
523                 if(bt->type!=SEPR) {
524                         int transopts= (U.transopts & USER_TR_BUTTONS);
525                         if(bt->type==TEX || bt->type==IDPOIN) transopts= 0;
526                         j= BIF_GetStringWidth(bt->font, bt->drawstr, transopts);
527
528                         if(j > i) i = j;
529                 }
530                 bt= bt->next;
531         }
532
533         
534         bt= block->buttons.first;
535         while(bt) {
536                 bt->x2 = i + addval;
537                 ui_check_but(bt);       // clips text again
538                 bt= bt->next;
539         }
540 }
541
542
543 void uiBoundsBlock(uiBlock *block, int addval)
544 {
545         uiBut *bt;
546         
547         if(block->buttons.first==NULL) {
548                 if(block->panel) {
549                         block->minx= 0.0; block->maxx= block->panel->sizex;
550                         block->miny= 0.0; block->maxy= block->panel->sizey;
551                 }
552         }
553         else {
554         
555                 block->minx= block->miny= 10000;
556                 block->maxx= block->maxy= -10000;
557                 
558                 bt= block->buttons.first;
559                 while(bt) {
560                         if(bt->x1 < block->minx) block->minx= bt->x1;
561                         if(bt->y1 < block->miny) block->miny= bt->y1;
562         
563                         if(bt->x2 > block->maxx) block->maxx= bt->x2;
564                         if(bt->y2 > block->maxy) block->maxy= bt->y2;
565                         
566                         bt= bt->next;
567                 }
568                 
569                 block->minx -= addval;
570                 block->miny -= addval;
571                 block->maxx += addval;
572                 block->maxy += addval;
573         }
574
575         block->safety.xmin= block->minx-40;
576         block->safety.ymin= block->miny-40;
577         block->safety.xmax= block->maxx+40;
578         block->safety.ymax= block->maxy+40;
579 }
580
581 static void ui_positionblock(uiBlock *block, uiBut *but)
582 {
583         /* position block relative to but */
584         uiBut *bt;
585         rctf butrct;
586         int xsize, ysize, xof=0, yof=0, centre;
587         short dir1= 0, dir2=0;
588         
589         /* first transform to screen coords, assuming matrix is stil OK */
590         /* the UIwinmat is in panelspace */
591
592         butrct.xmin= but->x1; butrct.xmax= but->x2;
593         butrct.ymin= but->y1; butrct.ymax= but->y2;
594
595         ui_graphics_to_window(block->win, &butrct.xmin, &butrct.ymin);
596         ui_graphics_to_window(block->win, &butrct.xmax, &butrct.ymax);
597         block->parentrct= butrct;       // will use that for pulldowns later
598
599         /* calc block rect */
600         if(block->buttons.first) {
601                 block->minx= block->miny= 10000;
602                 block->maxx= block->maxy= -10000;
603                 
604                 bt= block->buttons.first;
605                 while(bt) {
606                         if(bt->x1 < block->minx) block->minx= bt->x1;
607                         if(bt->y1 < block->miny) block->miny= bt->y1;
608
609                         if(bt->x2 > block->maxx) block->maxx= bt->x2;
610                         if(bt->y2 > block->maxy) block->maxy= bt->y2;
611                         
612                         bt= bt->next;
613                 }
614         }
615         else {
616                 /* we're nice and allow empty blocks too */
617                 block->minx= block->miny= 0;
618                 block->maxx= block->maxy= 20;
619         }
620
621         ui_graphics_to_window(block->win, &block->minx, &block->miny);
622         ui_graphics_to_window(block->win, &block->maxx, &block->maxy);
623
624         //block->minx-= 2.0; block->miny-= 2.0;
625         //block->maxx+= 2.0; block->maxy+= 2.0;
626         
627         xsize= block->maxx - block->minx+4; // 4 for shadow
628         ysize= block->maxy - block->miny+4;
629
630         if(but) {
631                 short left=0, right=0, top=0, down=0;
632
633                 if(block->direction & UI_CENTRE) centre= ysize/2;
634                 else centre= 0;
635
636                 if( butrct.xmin-xsize > 0.0) left= 1;
637                 if( butrct.xmax+xsize < G.curscreen->sizex) right= 1;
638                 if( butrct.ymin-ysize+centre > 0.0) down= 1;
639                 if( butrct.ymax+ysize-centre < G.curscreen->sizey) top= 1;
640                 
641                 dir1= block->direction & UI_DIRECTION;
642
643                 /* secundary directions */
644                 if(dir1 & (UI_TOP|UI_DOWN)) {
645                         if(dir1 & UI_LEFT) dir2= UI_LEFT;
646                         else if(dir1 & UI_RIGHT) dir2= UI_RIGHT;
647                         dir1 &= (UI_TOP|UI_DOWN);
648                 }
649
650                 if(dir2==0) if(dir1==UI_LEFT || dir1==UI_RIGHT) dir2= UI_DOWN;
651                 if(dir2==0) if(dir1==UI_TOP || dir1==UI_DOWN) dir2= UI_LEFT;
652                 
653                 /* no space at all? dont change */
654                 if(left || right) {
655                         if(dir1==UI_LEFT && left==0) dir1= UI_RIGHT;
656                         if(dir1==UI_RIGHT && right==0) dir1= UI_LEFT;
657                         /* this is aligning, not append! */
658                         if(dir2==UI_LEFT && right==0) dir2= UI_RIGHT;
659                         if(dir2==UI_RIGHT && left==0) dir2= UI_LEFT;
660                 }
661                 if(down || top) {
662                         if(dir1==UI_TOP && top==0) dir1= UI_DOWN;
663                         if(dir1==UI_DOWN && down==0) dir1= UI_TOP;
664                         if(dir2==UI_TOP && top==0) dir2= UI_DOWN;
665                         if(dir2==UI_DOWN && down==0) dir2= UI_TOP;
666                 }
667                 
668                 if(dir1==UI_LEFT) {
669                         xof= butrct.xmin - block->maxx;
670                         if(dir2==UI_TOP) yof= butrct.ymin - block->miny-centre;
671                         else yof= butrct.ymax - block->maxy+centre;
672                 }
673                 else if(dir1==UI_RIGHT) {
674                         xof= butrct.xmax - block->minx;
675                         if(dir2==UI_TOP) yof= butrct.ymin - block->miny-centre;
676                         else yof= butrct.ymax - block->maxy+centre;
677                 }
678                 else if(dir1==UI_TOP) {
679                         yof= butrct.ymax - block->miny;
680                         if(dir2==UI_RIGHT) xof= butrct.xmax - block->maxx;
681                         else xof= butrct.xmin - block->minx;
682                         // changed direction? 
683                         if((dir1 & block->direction)==0) {
684                                 if(block->direction & UI_SHIFT_FLIPPED)
685                                         xof+= dir2==UI_LEFT?25:-25;
686                                 uiBlockFlipOrder(block);
687                         }
688                 }
689                 else if(dir1==UI_DOWN) {
690                         yof= butrct.ymin - block->maxy;
691                         if(dir2==UI_RIGHT) xof= butrct.xmax - block->maxx;
692                         else xof= butrct.xmin - block->minx;
693                         // changed direction?
694                         if((dir1 & block->direction)==0) {
695                                 if(block->direction & UI_SHIFT_FLIPPED)
696                                         xof+= dir2==UI_LEFT?25:-25;
697                                 uiBlockFlipOrder(block);
698                         }
699                 }
700
701                 // apply requested offset in the block
702                 xof += block->xofs/block->aspect;
703                 yof += block->yofs/block->aspect;
704                 
705         }
706         
707         /* apply */
708         bt= block->buttons.first;
709         while(bt) {
710                 
711                 ui_graphics_to_window(block->win, &bt->x1, &bt->y1);
712                 ui_graphics_to_window(block->win, &bt->x2, &bt->y2);
713
714                 bt->x1 += xof;
715                 bt->x2 += xof;
716                 bt->y1 += yof;
717                 bt->y2 += yof;
718
719                 bt->aspect= 1.0;
720                 // ui_check_but recalculates drawstring size in pixels
721                 ui_check_but(bt);
722                 
723                 bt= bt->next;
724         }
725         
726         block->minx += xof;
727         block->miny += yof;
728         block->maxx += xof;
729         block->maxy += yof;
730         
731         /* safety calculus */
732         if(but) {
733                 float midx= (block->parentrct.xmin+block->parentrct.xmax)/2.0;
734                 float midy= (block->parentrct.ymin+block->parentrct.ymax)/2.0;
735                 
736                 /* when you are outside parent button, safety there should be smaller */
737                 
738                 // parent button to left
739                 if( midx < block->minx ) block->safety.xmin= block->minx-3; 
740                 else block->safety.xmin= block->minx-40;
741                 // parent button to right
742                 if( midx > block->maxx ) block->safety.xmax= block->maxx+3; 
743                 else block->safety.xmax= block->maxx+40;
744                 
745                 // parent button on bottom
746                 if( midy < block->miny ) block->safety.ymin= block->miny-3; 
747                 else block->safety.ymin= block->miny-40;
748                 // parent button on top
749                 if( midy > block->maxy ) block->safety.ymax= block->maxy+3; 
750                 else block->safety.ymax= block->maxy+40;
751                 
752                 // exception for switched pulldowns...
753                 if(dir1 && (dir1 & block->direction)==0) {
754                         if(dir2==UI_RIGHT) block->safety.xmax= block->maxx+3; 
755                         if(dir2==UI_LEFT) block->safety.xmin= block->minx-3; 
756                 }
757                 block->direction= dir1;
758         }
759         else {
760                 block->safety.xmin= block->minx-40;
761                 block->safety.ymin= block->miny-40;
762                 block->safety.xmax= block->maxx+40;
763                 block->safety.ymax= block->maxy+40;
764         }
765
766 }
767
768
769 void ui_autofill(uiBlock *block)
770 {
771         uiBut *but;
772         float *maxw, *maxh, startx = 0, starty, height = 0;
773         float totmaxh;
774         int rows=0, /*  cols=0, */ i, lasti;
775         
776         /* first count rows */
777         but= block->buttons.last;
778         rows= but->x1+1;
779
780         /* calculate max width / height for each row */
781         maxw= MEM_callocN(sizeof(float)*rows, "maxw");
782         maxh= MEM_callocN(sizeof(float)*rows, "maxh");
783         but= block->buttons.first;
784         while(but) {
785                 i= but->x1;
786                 if( maxh[i] < but->y2) maxh[i]= but->y2;
787                 maxw[i] += but->x2;
788                 but= but->next;
789         }
790         
791         totmaxh= 0.0;
792         for(i=0; i<rows; i++) totmaxh+= maxh[i];
793         
794         /* apply widths/heights */
795         starty= block->maxy;
796         but= block->buttons.first;
797         lasti= -1;
798         while(but) {
799                 // signal for aligning code
800                 but->flag |= UI_BUT_ALIGN_DOWN;
801                 
802                 i= but->x1;
803
804                 if(i!=lasti) {
805                         startx= block->minx;
806                         height= (maxh[i]*(block->maxy-block->miny))/totmaxh;
807                         starty-= height;
808                         lasti= i;
809                 }
810                 
811                 but->y1= starty+but->aspect;
812                 but->y2= but->y1+height-but->aspect;
813                 
814                 but->x2= (but->x2*(block->maxx-block->minx))/maxw[i];
815                 but->x1= startx+but->aspect;
816                 
817                 startx+= but->x2;
818                 but->x2+= but->x1-but->aspect;
819                 
820                 ui_check_but(but);
821                 
822                 but= but->next;
823         }
824         
825         uiBlockEndAlign(block);
826         
827         MEM_freeN(maxw); MEM_freeN(maxh);       
828         block->autofill= 0;
829 }
830
831 /* ************** LINK LINE DRAWING  ************* */
832
833 /* link line drawing is not part of buttons or theme.. so we stick with it here */
834
835 static void ui_draw_linkline(uiBut *but, uiLinkLine *line)
836 {
837         float vec1[2], vec2[2];
838
839         if(line->from==NULL || line->to==NULL) return;
840         
841         vec1[0]= (line->from->x1+line->from->x2)/2.0;
842         vec1[1]= (line->from->y1+line->from->y2)/2.0;
843         vec2[0]= (line->to->x1+line->to->x2)/2.0;
844         vec2[1]= (line->to->y1+line->to->y2)/2.0;
845         
846         if(line->flag & UI_SELECT) BIF_ThemeColorShade(but->themecol, 80);
847         else glColor3ub(0,0,0);
848         fdrawline(vec1[0], vec1[1], vec2[0], vec2[1]);
849 }
850
851 static void ui_draw_links(uiBlock *block)
852 {
853         uiBut *but;
854         uiLinkLine *line;
855         
856         but= block->buttons.first;
857         while(but) {
858                 if(but->type==LINK && but->link) {
859                         line= but->link->lines.first;
860                         while(line) {
861                                 ui_draw_linkline(but, line);
862                                 line= line->next;
863                         }
864                 }
865                 but= but->next;
866         }       
867 }
868
869 /* ************** BLOCK DRAWING FUNCTION ************* */
870
871
872 void uiDrawBlock(uiBlock *block)
873 {
874         uiBut *but;
875
876         /* handle pending stuff */
877         if(block->autofill) ui_autofill(block);
878         if(block->minx==0.0 && block->maxx==0.0) uiBoundsBlock(block, 0);
879         if(block->flag & UI_BUT_ALIGN) uiBlockEndAlign(block);
880         
881         uiPanelPush(block); // panel matrix
882         
883         if(block->flag & UI_BLOCK_LOOP) {
884                 uiDrawMenuBox(block->minx, block->miny, block->maxx, block->maxy, block->flag);
885         }
886         else if(block->panel) ui_draw_panel(block);
887         
888         if(block->drawextra) block->drawextra();
889
890         for (but= block->buttons.first; but; but= but->next) {
891                 ui_draw_but(but);
892         }
893
894         ui_draw_links(block);
895
896         uiPanelPop(block); // matrix restored
897 }
898
899 /* ************* MENUBUTS *********** */
900
901 typedef struct {
902         char *str;
903         int retval;
904         int icon;
905 } MenuEntry;
906
907 typedef struct {
908         char *instr;
909         char *title;
910         
911         MenuEntry *items;
912         int nitems, itemssize;
913 } MenuData;
914
915 static MenuData *menudata_new(char *instr) {
916         MenuData *md= MEM_mallocN(sizeof(*md), "MenuData");
917
918         md->instr= instr;
919         md->title= NULL;
920         md->items= NULL;
921         md->nitems= md->itemssize= 0;
922         
923         return md;
924 }
925
926 static void menudata_set_title(MenuData *md, char *title) {
927         if (!md->title)
928                 md->title= title;
929 }
930
931 static void menudata_add_item(MenuData *md, char *str, int retval, int icon) {
932         if (md->nitems==md->itemssize) {
933                 int nsize= md->itemssize?(md->itemssize<<1):1;
934                 MenuEntry *oitems= md->items;
935                 
936                 md->items= MEM_mallocN(nsize*sizeof(*md->items), "md->items");
937                 if (oitems) {
938                         memcpy(md->items, oitems, md->nitems*sizeof(*md->items));
939                         MEM_freeN(oitems);
940                 }
941                 
942                 md->itemssize= nsize;
943         }
944         
945         md->items[md->nitems].str= str;
946         md->items[md->nitems].retval= retval;
947         md->items[md->nitems].icon= icon;
948         md->nitems++;
949 }
950
951 static void menudata_free(MenuData *md) {
952         MEM_freeN(md->instr);
953         if (md->items)
954                 MEM_freeN(md->items);
955         MEM_freeN(md);
956 }
957
958         /**
959          * Parse menu description strings, string is of the
960          * form "[sss%t|]{(sss[%xNN]|), (%l|)}", ssss%t indicates the
961          * menu title, sss or sss%xNN indicates an option, 
962          * if %xNN is given then NN is the return value if
963          * that option is selected otherwise the return value
964          * is the index of the option (starting with 1). %l
965          * indicates a seperator.
966          * 
967          * @param str String to be parsed.
968          * @retval new menudata structure, free with menudata_free()
969          */
970 static MenuData *decompose_menu_string(char *str) 
971 {
972         char *instr= BLI_strdup(str);
973         MenuData *md= menudata_new(instr);
974         char *nitem= NULL, *s= instr;
975         int nicon=0, nretval= 1, nitem_is_title= 0;
976         
977         while (1) {
978                 char c= *s;
979
980                 if (c=='%') {
981                         if (s[1]=='x') {
982                                 nretval= atoi(s+2);
983
984                                 *s= '\0';
985                                 s++;
986                         } else if (s[1]=='t') {
987                                 nitem_is_title= 1;
988
989                                 *s= '\0';
990                                 s++;
991                         } else if (s[1]=='l') {
992                                 nitem= "%l";
993                                 s++;
994                         } else if (s[1]=='i') {
995                                 nicon= atoi(s+2);
996                                 s++;
997                         }
998                 } else if (c=='|' || c=='\0') {
999                         if (nitem) {
1000                                 *s= '\0';
1001
1002                                 if (nitem_is_title) {
1003                                         menudata_set_title(md, nitem);
1004                                         nitem_is_title= 0;
1005                                 } else {
1006                                         menudata_add_item(md, nitem, nretval, nicon);
1007                                         nretval= md->nitems+1;
1008                                 } 
1009                                 
1010                                 nitem= NULL;
1011                                 nicon= 0;
1012                         }
1013                         
1014                         if (c=='\0')
1015                                 break;
1016                 } else if (!nitem)
1017                         nitem= s;
1018                 
1019                 s++;
1020         }
1021         
1022         return md;
1023 }
1024
1025 static void ui_set_name_menu(uiBut *but, int value)
1026 {
1027         MenuData *md;
1028         int i;
1029         
1030         md= decompose_menu_string(but->str);
1031         for (i=0; i<md->nitems; i++)
1032                 if (md->items[i].retval==value)
1033                         strcpy(but->drawstr, md->items[i].str);
1034         menudata_free(md);
1035 }
1036
1037 static void ui_warp_pointer(short x, short y)
1038 {
1039         /* OSX has very poor mousewarp support, it sends events;
1040            this causes a menu being pressed immediately ... */
1041         #ifndef __APPLE__
1042         warp_pointer(x, y);
1043         #endif
1044 }
1045
1046
1047 static int ui_do_but_MENU(uiBut *but)
1048 {
1049         uiBlock *block;
1050         uiBut *bt;
1051         ListBase listb={NULL, NULL}, lb;
1052         double fvalue;
1053         int width, height=0, a, xmax, starty;
1054         short startx;
1055         int columns=1, rows=0, boxh, event;
1056         short  x1, y1, active= -1;
1057         short mval[2];
1058         MenuData *md;
1059
1060         but->flag |= UI_SELECT;
1061         ui_draw_but(but);
1062         ui_block_flush_back(but->block);        // flush because this button creates own blocks loop
1063
1064         block= uiNewBlock(&listb, "menu", UI_EMBOSSP, UI_HELV, but->win);
1065         block->flag= UI_BLOCK_LOOP|UI_BLOCK_REDRAW|UI_BLOCK_NUMSELECT;
1066         block->themecol= TH_MENU_ITEM;
1067         
1068         md= decompose_menu_string(but->str);
1069
1070         /* columns and row calculation */
1071         columns= (md->nitems+20)/20;
1072         if (columns<1) columns= 1;
1073         
1074         rows= (int) md->nitems/columns;
1075         if (rows<1) rows= 1;
1076         
1077         while (rows*columns<md->nitems) rows++;
1078                 
1079         /* size and location */
1080         if(md->title)
1081                 width= 1.5*but->aspect*strlen(md->title)+BIF_GetStringWidth(block->curfont, md->title, (U.transopts & USER_TR_MENUS));
1082         else
1083                 width= 0;
1084
1085         for(a=0; a<md->nitems; a++) {
1086                 xmax= but->aspect*BIF_GetStringWidth(block->curfont, md->items[a].str, (U.transopts & USER_TR_MENUS));
1087                 if(xmax>width) width= xmax;
1088         }
1089
1090         width+= 10;
1091         if (width < (but->x2 - but->x1)) width = (but->x2 - but->x1);
1092         if (width<50) width=50;
1093
1094         boxh= TBOXH;
1095         
1096         height= rows*boxh;
1097         if (md->title) height+= boxh;
1098         
1099         getmouseco_sc(mval);
1100         
1101         /* find active item */
1102         fvalue= ui_get_but_val(but);
1103         for(active=0; active<md->nitems; active++) {
1104                 if( md->items[active].retval== (int)fvalue ) break;
1105         }
1106         /* no active item? */
1107         if(active==md->nitems) {
1108                 if(md->title) active= -1;
1109                 else active= 0;
1110         }
1111
1112         /* for now disabled... works confusing because you think it's a title or so.... */
1113         active= -1;
1114
1115         /* here we go! */
1116         startx= but->x1;
1117         starty= but->y1;
1118         
1119         if(md->title) {
1120                 uiBut *bt;
1121                 uiSetCurFont(block, block->font+1);
1122                 bt= uiDefBut(block, LABEL, 0, md->title, startx, (short)(starty+rows*boxh), (short)width, (short)boxh, NULL, 0.0, 0.0, 0, 0, "");
1123                 uiSetCurFont(block, block->font);
1124                 bt->flag= UI_TEXT_LEFT;
1125         }
1126
1127         for(a=0; a<md->nitems; a++) {
1128                 
1129                 x1= but->x1 + width*((int)(md->nitems-a-1)/rows);
1130                 y1= but->y1 - boxh*(rows - ((md->nitems - a - 1)%rows)) + (rows*boxh);
1131
1132                 if (strcmp(md->items[md->nitems-a-1].str, "%l")==0) {
1133                         uiDefBut(block, SEPR, B_NOP, "", x1, y1,(short)(width-(rows>1)), (short)(boxh-1), NULL, 0.0, 0.0, 0, 0, "");
1134                 }
1135                 else if(md->items[md->nitems-a-1].icon) {
1136                         uiBut *bt= uiDefIconTextBut(block, BUTM|but->pointype, but->retval, md->items[md->nitems-a-1].icon ,md->items[md->nitems-a-1].str, x1, y1,(short)(width-(rows>1)), (short)(boxh-1), but->poin, (float) md->items[md->nitems-a-1].retval, 0.0, 0, 0, "");
1137                         if(active==a) bt->flag |= UI_ACTIVE;
1138                 }
1139                 else {
1140                         uiBut *bt= uiDefBut(block, BUTM|but->pointype, but->retval, md->items[md->nitems-a-1].str, x1, y1,(short)(width-(rows>1)), (short)(boxh-1), but->poin, (float) md->items[md->nitems-a-1].retval, 0.0, 0, 0, "");
1141                         if(active==a) bt->flag |= UI_ACTIVE;
1142                 }
1143         }
1144         
1145         /* the code up here has flipped locations, because of change of preferred order */
1146         /* thats why we have to switch list order too, to make arrowkeys work */
1147         
1148         lb.first= lb.last= NULL;
1149         bt= block->buttons.first;
1150         while(bt) {
1151                 uiBut *next= bt->next;
1152                 BLI_remlink(&block->buttons, bt);
1153                 BLI_addhead(&lb, bt);
1154                 bt= next;
1155         }
1156         block->buttons= lb;
1157
1158         /* and lets go */
1159         block->direction= UI_TOP;
1160         ui_positionblock(block, but);
1161         
1162         /* blocks can come (and get scaled) from a normal window, now we go to screenspace */
1163         block->win= G.curscreen->mainwin;
1164         for(bt= block->buttons.first; bt; bt= bt->next) bt->win= block->win;
1165         bwin_getsinglematrix(block->win, block->winmat);
1166
1167         event= uiDoBlocks(&listb, 0);
1168         
1169         menudata_free(md);
1170         
1171         but->flag &= ~UI_SELECT;
1172         ui_check_but(but);
1173         ui_draw_but(but);
1174         
1175         uibut_do_func(but);
1176
1177         return event;   
1178 }
1179
1180
1181
1182 /* ************* EVENTS ************* */
1183
1184 void uiGetMouse(int win, short *adr)
1185 {
1186         int x, y;
1187         float xwin, ywin;
1188         
1189         getmouseco_sc(adr);
1190         if (win == G.curscreen->mainwin) return;
1191         
1192         bwin_getsuborigin(win, &x, &y);
1193
1194         adr[0]-= x;
1195         adr[1]-= y;
1196
1197         xwin= adr[0];
1198         ywin= adr[1];
1199
1200         ui_window_to_graphics(win, &xwin, &ywin);
1201
1202         adr[0]= (short)(xwin+0.5);
1203         adr[1]= (short)(ywin+0.5);
1204 }
1205
1206 static void ui_is_but_sel(uiBut *but)
1207 {
1208         double value;
1209         int lvalue;
1210         short push=0, true=1;
1211
1212         value= ui_get_but_val(but);
1213
1214         if( but->type==TOGN ) true= 0;
1215
1216         if( but->bit ) {
1217                 lvalue= (int)value;
1218                 if( BTST(lvalue, (but->bitnr)) ) push= true;
1219                 else push= !true;
1220         }
1221         else {
1222                 switch(but->type) {
1223                 case BUT:
1224                         push= 0;
1225                         break;
1226                 case KEYEVT:
1227                         if (value==-1) push= 1;
1228                         break;
1229                 case TOG:
1230                 case TOGR:
1231                 case TOG3:
1232                 case ICONTOG:
1233                         if(value!=but->min) push= 1;
1234                         break;
1235                 case TOGN:
1236                         if(value==0.0) push= 1;
1237                         break;
1238                 case ROW:
1239                         if(value == but->max) push= 1;
1240                         break;
1241                 case COL:
1242                         push= 1;
1243                         break;
1244                 default:
1245                         push= 2;
1246                         break;
1247                 }
1248         }
1249         
1250         if(push==2);
1251         else if(push==1) but->flag |= UI_SELECT;
1252         else but->flag &= ~UI_SELECT;
1253 }
1254
1255 static int ui_do_but_BUT(uiBut *but)
1256 {
1257         int activated;
1258         
1259         do {
1260                 int oflag= but->flag;
1261                 short mval[2];
1262                         
1263                 uiGetMouse(mywinget(), mval);
1264
1265                 if (uibut_contains_pt(but, mval))
1266                         but->flag |= UI_SELECT;
1267                 else
1268                         but->flag &= ~UI_SELECT;
1269
1270                 if (but->flag != oflag) {
1271                         ui_draw_but(but);
1272                         ui_block_flush_back(but->block);
1273                 }
1274                 
1275                 PIL_sleep_ms(10);
1276         } while (get_mbut() & L_MOUSE);
1277
1278         activated= (but->flag & UI_SELECT);
1279
1280         if(activated) {
1281                 uibut_do_func(but);
1282         }
1283         
1284         but->flag &= ~UI_SELECT;
1285         ui_draw_but(but);
1286
1287         return activated?but->retval:0;
1288 }
1289
1290 static int ui_do_but_KEYEVT(uiBut *but)
1291 {
1292         unsigned short event= 0;
1293         short val;
1294
1295                 /* flag for ui_check_but */
1296         ui_set_but_val(but, -1);
1297         ui_check_but(but);
1298         ui_draw_but(but);
1299         ui_block_flush_back(but->block);
1300
1301         do {
1302                 event= extern_qread(&val);
1303         } while (!event || !val || ELEM(event, MOUSEX, MOUSEY));
1304
1305         if (!key_event_to_string(event)[0]) event= 0;
1306
1307         ui_set_but_val(but, (double) event);
1308         ui_check_but(but);
1309         ui_draw_but(but);
1310         
1311         return (event!=0);
1312 }
1313
1314 static int ui_do_but_TOG(uiBlock *block, uiBut *but)
1315 {
1316         uiBut *bt;
1317         double value;
1318         int w, lvalue, push;
1319         
1320         value= ui_get_but_val(but);
1321         lvalue= (int)value;
1322         
1323         if(but->bit) {
1324                 w= BTST(lvalue, but->bitnr);
1325                 if(w) lvalue = BCLR(lvalue, but->bitnr);
1326                 else lvalue = BSET(lvalue, but->bitnr);
1327                 
1328                 if(but->type==TOGR) {
1329                         if( (get_qual() & LR_SHIFTKEY)==0 ) {
1330                                 lvalue= 1<<(but->bitnr);
1331         
1332                                 ui_set_but_val(but, (double)lvalue);
1333
1334                                 bt= block->buttons.first;
1335                                 while(bt) {
1336                                         if( bt!=but && bt->poin==but->poin ) {
1337                                                 ui_is_but_sel(bt);
1338                                                 ui_draw_but(bt);
1339                                         }
1340                                         bt= bt->next;
1341                                 }
1342                         }
1343                         else {
1344                                 if(lvalue==0) lvalue= 1<<(but->bitnr);
1345                         }
1346                 }
1347                 ui_set_but_val(but, (double)lvalue);
1348                 if(but->type==ICONTOG) ui_check_but(but);
1349                 // no frontbuffer draw for this one
1350                 if((but->flag & UI_NO_HILITE)==0) ui_draw_but(but);
1351         }
1352         else {
1353                 
1354                 if(value==0.0) push= 1; 
1355                 else push= 0;
1356                 
1357                 if(but->type==TOGN) push= !push;
1358                 ui_set_but_val(but, (double)push);
1359                 if(but->type==ICONTOG) ui_check_but(but);               
1360                 // no frontbuffer draw for this one
1361                 if((but->flag & UI_NO_HILITE)==0) ui_draw_but(but);
1362         }
1363         
1364         /* no while loop...this button is used for viewmove */
1365
1366         uibut_do_func(but);
1367
1368         return but->retval;
1369 }
1370
1371 static int ui_do_but_ROW(uiBlock *block, uiBut *but)
1372 {
1373         uiBut *bt;
1374         
1375         ui_set_but_val(but, but->max);
1376         ui_draw_but(but);
1377
1378         bt= block->buttons.first;
1379         while(bt) {
1380                 if( bt!=but && bt->type==ROW ) {
1381                         if(bt->min==but->min) {
1382                                 ui_is_but_sel(bt);
1383                                 ui_draw_but(bt);
1384                         }
1385                 }
1386                 bt= bt->next;
1387         }
1388         return but->retval;
1389 }
1390
1391 static int ui_do_but_TEX(uiBut *but)
1392 {
1393         unsigned short dev;
1394         short x, mval[2], len=0, dodraw;
1395         char *str, backstr[UI_MAX_DRAW_STR];
1396         short capturing;
1397         
1398         str= (char *)but->poin;
1399         
1400         but->flag |= UI_SELECT;
1401
1402         uiGetMouse(mywinget(), mval);
1403
1404         /* calculate cursor pos with current mousecoords */
1405         BLI_strncpy(backstr, but->drawstr, UI_MAX_DRAW_STR);
1406         but->pos= strlen(backstr)-but->ofs;
1407
1408         while((but->aspect*BIF_GetStringWidth(but->font, backstr+but->ofs, 0) + but->x1) > mval[0]) {
1409                 if (but->pos <= 0) break;
1410                 but->pos--;
1411                 backstr[but->pos+but->ofs] = 0;
1412         }
1413         
1414         but->pos -= strlen(but->str);
1415         but->pos += but->ofs;
1416         if(but->pos<0) but->pos= 0;
1417
1418         /* backup */
1419         BLI_strncpy(backstr, but->poin, UI_MAX_DRAW_STR);
1420
1421         ui_draw_but(but);
1422         ui_block_flush_back(but->block);
1423         
1424         while (get_mbut() & L_MOUSE) BIF_wait_for_statechange();
1425         len= strlen(str);
1426         but->min= 0.0;
1427         
1428         capturing = TRUE;
1429         while(capturing) {
1430                 char ascii;
1431                 short val;
1432
1433                 dodraw= 0;
1434                 dev = extern_qread_ext(&val, &ascii);
1435
1436                 if(dev==INPUTCHANGE) break;
1437                 else if(get_mbut() & L_MOUSE) break;
1438                 else if(get_mbut() & R_MOUSE) break;
1439                 else if(dev==ESCKEY) break;
1440                 else if(dev==MOUSEX) val= 0;
1441                 else if(dev==MOUSEY) val= 0;
1442
1443                 if(ascii) {
1444                         if(len < but->max) {
1445                                 for(x= but->max; x>but->pos; x--)
1446                                         str[x]= str[x-1];
1447                                 str[but->pos]= ascii;
1448                                 but->pos++; 
1449                                 len++;
1450                                 str[len]= '\0';
1451                                 dodraw= 1;
1452                         }
1453                 }
1454                 else if(val) {
1455                 
1456                         switch (dev) {
1457                                 
1458                         case RIGHTARROWKEY:
1459                                 if(G.qual & LR_SHIFTKEY) but->pos= strlen(str);
1460                                 else but->pos++;
1461                                 if(but->pos>strlen(str)) but->pos= strlen(str);
1462                                 dodraw= 1;
1463                                 break;
1464                                 
1465                         case LEFTARROWKEY:
1466                                 if(G.qual & LR_SHIFTKEY) but->pos= 0;
1467                                 else if(but->pos>0) but->pos--;
1468                                 dodraw= 1;
1469                                 break;
1470
1471                         case ENDKEY:
1472                                 but->pos= strlen(str);
1473                                 dodraw= 1;
1474                                 break;
1475
1476                         case HOMEKEY:
1477                                 but->pos= 0;
1478                                 dodraw= 1;
1479                                 break;
1480                                 
1481                         case PADENTER:
1482                         case RETKEY:
1483                                 capturing = FALSE;
1484                                 break;
1485                                 
1486                         case DELKEY:
1487                                 if(but->pos>=0 && but->pos<strlen(str)) {
1488                                         for(x=but->pos; x<=strlen(str); x++)
1489                                                 str[x]= str[x+1];
1490                                         str[--len]='\0';
1491                                         dodraw= 1;
1492                                 }
1493                                 break;
1494
1495                         case BACKSPACEKEY:
1496                                 if(len!=0) {
1497                                         if(get_qual() & LR_SHIFTKEY) {
1498                                                 str[0]= 0;
1499                                                 but->pos= 0;
1500                                                 len= 0;
1501                                                 dodraw= 1;
1502                                         }
1503                                         else if(but->pos>0) {
1504                                                 for(x=but->pos; x<=strlen(str); x++)
1505                                                         str[x-1]= str[x];
1506                                                 but->pos--;
1507                                                 str[--len]='\0';
1508                                                 dodraw= 1;
1509                                         }
1510                                 } 
1511                                 break;
1512                         }
1513                 }
1514
1515                 
1516                 if(dodraw) {
1517                         ui_check_but(but);
1518                         ui_draw_but(but);
1519                         ui_block_flush_back(but->block);
1520                 }
1521         }
1522         
1523         if(dev==ESCKEY) strcpy(but->poin, backstr);
1524         but->pos= -1;
1525         but->flag &= ~UI_SELECT;
1526
1527         uibut_do_func(but);
1528
1529         ui_check_but(but);
1530         ui_draw_but(but);
1531         
1532         if(dev!=ESCKEY) return but->retval;
1533         else return 0;
1534 }
1535
1536
1537 static int uiActAsTextBut(uiBut *but)
1538 {
1539         void *but_func;
1540         double value;
1541         float min, max;
1542         int temp, retval, textleft;
1543         char str[UI_MAX_DRAW_STR], *point;
1544         
1545         
1546         value= ui_get_but_val(but);
1547         if( but->pointype==FLO ) {
1548                 if(but->a2) { /* amount of digits defined */
1549                         if(but->a2==1) sprintf(str, "%.1f", value);
1550                         else if(but->a2==2) sprintf(str, "%.2f", value);
1551                         else if(but->a2==3) sprintf(str, "%.3f", value);
1552                         else sprintf(str, "%.4f", value);
1553                 }
1554                 else sprintf(str, "%.3f", value);
1555         }
1556         else {
1557                 sprintf(str, "%d", (int)value);
1558         }
1559         /* store values before calling as text button */
1560         point= but->poin;
1561         but->poin= str;
1562         but_func= but->func;
1563         but->func= NULL;
1564         min= but->min;
1565         max= but->max;
1566         but->min= 0.0;
1567         but->max= 15.0;
1568         temp= but->type;
1569         but->type= TEX;
1570         textleft= but->flag & UI_TEXT_LEFT;
1571         but->flag |= UI_TEXT_LEFT;
1572         ui_check_but(but);
1573         
1574         retval= ui_do_but_TEX(but);
1575         
1576         /* restore values */
1577         but->type= temp;
1578         but->poin= point;
1579         but->func= but_func;
1580         but->min= min;
1581         but->max= max;
1582         if(textleft==0) but->flag &= ~UI_TEXT_LEFT;
1583
1584         if( but->pointype==FLO ) value= atof(str);
1585         else value= atoi(str);
1586
1587         if(value<min) value= min;
1588         if(value>max) value= max;
1589
1590         ui_set_but_val(but, value);
1591         ui_check_but(but);
1592         ui_draw_but(but);
1593         
1594         return retval;
1595 }
1596
1597 static int ui_do_but_NUM(uiBut *but)
1598 {
1599         double value;
1600         float deler, fstart, f, tempf;
1601         int lvalue, temp; /*  , firsttime=1; */
1602         short retval=0, qual, sx, mval[2], pos=0;
1603
1604         but->flag |= UI_SELECT;
1605         ui_draw_but(but);
1606         ui_block_flush_back(but->block);
1607         
1608         uiGetMouse(mywinget(), mval);
1609         value= ui_get_but_val(but);
1610         
1611         sx= mval[0];
1612         fstart= (value - but->min)/(but->max-but->min);
1613         f= fstart;
1614         
1615         temp= (int)value;
1616         tempf= value;
1617         
1618         if(get_qual() & LR_SHIFTKEY) {  /* make it textbut */
1619                 if( uiActAsTextBut(but) ) retval= but->retval;
1620         }
1621         else {
1622                 retval= but->retval;
1623                 /* firsttime: this button can be approached with enter as well */
1624                 while (get_mbut() & L_MOUSE) {
1625                         qual= get_qual();
1626                         
1627                         deler= 500;
1628                         if( but->pointype!=FLO ) {
1629         
1630                                 if( (but->max-but->min)<100 ) deler= 200.0;
1631                                 if( (but->max-but->min)<25 ) deler= 50.0;
1632         
1633                         }
1634                         if(qual & LR_SHIFTKEY) deler*= 10.0;
1635                         if(qual & LR_ALTKEY) deler*= 20.0;
1636         
1637                         uiGetMouse(mywinget(), mval);
1638                         
1639                         if(mval[0] != sx) {
1640                         
1641                                 f+= ((float)(mval[0]-sx))/deler;
1642                                 if(f>1.0) f= 1.0;
1643                                 if(f<0.0) f= 0.0;
1644                                 sx= mval[0];
1645                                 tempf= ( but->min + f*(but->max-but->min));
1646                                 
1647                                 if( but->pointype!=FLO ) {
1648                                         
1649                                         temp= floor(tempf+.5);
1650                                         
1651                                         if(tempf==but->min || tempf==but->max);
1652                                         else if(qual & LR_CTRLKEY) temp= 10*(temp/10);
1653                                         
1654                                         if( temp>=but->min && temp<=but->max) {
1655                                         
1656                                                 value= ui_get_but_val(but);
1657                                                 lvalue= (int)value;
1658                                                 
1659                                                 if(temp != lvalue ) {
1660                                                         pos= 1;
1661                                                         ui_set_but_val(but, (double)temp);
1662                                                         ui_check_but(but);
1663                                                         ui_draw_but(but);
1664                                                         ui_block_flush_back(but->block);
1665
1666                                                         uibut_do_func(but);
1667                                                 }
1668                                         }
1669         
1670                                 }
1671                                 else {
1672                                         temp= 0;
1673                                         if(qual & LR_CTRLKEY) {
1674                                                 if(tempf==but->min || tempf==but->max);
1675                                                 else if(but->max-but->min < 2.10) tempf= 0.1*floor(10*tempf);
1676                                                 else if(but->max-but->min < 21.0) tempf= floor(tempf);
1677                                                 else tempf= 10.0*floor(tempf/10.0);
1678                                         }
1679         
1680                                         if( tempf>=but->min && tempf<=but->max) {
1681                                                 value= ui_get_but_val(but);
1682                                                 
1683                                                 if(tempf != value ) {
1684                                                         pos= 1;
1685                                                         ui_set_but_val(but, tempf);
1686                                                         ui_check_but(but);
1687                                                         ui_draw_but(but);
1688                                                         ui_block_flush_back(but->block);
1689                                                 }
1690                                         }
1691         
1692                                 }
1693                         }
1694                         BIF_wait_for_statechange();
1695                 }
1696                 
1697                 /* click on the side arrows to increment/decrement, click inside
1698                 * to edit the value directly */
1699                 if(pos==0) {  /* plus 1 or minus 1 */
1700                         if( but->pointype!=FLO ) {
1701         
1702                                 if(sx < (but->x1 + (but->x2 - but->x1)/3 - 3)) {
1703                                         temp--;
1704                                         if( temp>=but->min && temp<=but->max) ui_set_but_val(but, (double)temp);
1705                                 }
1706                                 else if(sx > (but->x1 + (2*(but->x2 - but->x1)/3) + 3)) {
1707                                         temp++;
1708                                         if( temp>=but->min && temp<=but->max) ui_set_but_val(but, (double)temp);
1709                                 }
1710                                 else {
1711                                         if( uiActAsTextBut(but) ); else retval= 0;
1712                                 }
1713                         }
1714                         else {
1715                         
1716                                 if(sx < (but->x1 + (but->x2 - but->x1)/3 - 3)) {
1717                                         tempf-= 0.01*but->a1;
1718                                         if (tempf < but->min) tempf = but->min;
1719                                         ui_set_but_val(but, tempf);
1720                                 }
1721                                 else if(sx > but->x1 + (2*((but->x2 - but->x1)/3) + 3)) {
1722                                         tempf+= 0.01*but->a1;
1723                                         if (tempf < but->min) tempf = but->min;
1724                                         ui_set_but_val(but, tempf);
1725                                 }
1726                                 else {
1727                                         if( uiActAsTextBut(but) ); else retval= 0;
1728                                 }
1729                         }
1730                 }
1731         }
1732
1733         but->flag &= ~UI_SELECT;
1734         ui_check_but(but);
1735         ui_draw_but(but);       
1736         ui_block_flush_back(but->block);
1737         
1738         uibut_do_func(but);
1739         
1740         return retval;
1741 }
1742
1743 static int ui_do_but_TOG3(uiBut *but)
1744
1745
1746         if( but->pointype==SHO ) {
1747                 short *sp= (short *)but->poin;
1748                 
1749                 if( BTST(sp[1], but->bitnr)) {
1750                         sp[1]= BCLR(sp[1], but->bitnr);
1751                         sp[0]= BCLR(sp[0], but->bitnr);
1752                 }
1753                 else if( BTST(sp[0], but->bitnr)) {
1754                         sp[1]= BSET(sp[1], but->bitnr);
1755                 } else {
1756                         sp[0]= BSET(sp[0], but->bitnr);
1757                 }
1758         }
1759         else {
1760                 if( BTST(*(but->poin+2), but->bitnr)) {
1761                         *(but->poin+2)= BCLR(*(but->poin+2), but->bitnr);
1762                         *(but->poin)= BCLR(*(but->poin), but->bitnr);
1763                 }
1764                 else if( BTST(*(but->poin), but->bitnr)) {
1765                         *(but->poin+2)= BSET(*(but->poin+2), but->bitnr);
1766                 } else {
1767                         *(but->poin)= BSET(*(but->poin), but->bitnr);
1768                 }
1769         }
1770         
1771         ui_is_but_sel(but);
1772         ui_draw_but(but);
1773         
1774         return but->retval;
1775 }
1776
1777 static int ui_do_but_ICONROW(uiBut *but)
1778 {
1779         ListBase listb= {NULL, NULL};
1780         uiBlock *block;
1781         int a;
1782         
1783         but->flag |= UI_SELECT;
1784         ui_draw_but(but);
1785         ui_block_flush_back(but->block);        // flush because this button creates own blocks loop
1786         
1787         /* here we go! */
1788         block= uiNewBlock(&listb, "menu", UI_EMBOSSP, UI_HELV, but->win);
1789         block->flag= UI_BLOCK_LOOP|UI_BLOCK_REDRAW|UI_BLOCK_NUMSELECT;
1790         block->themecol= TH_MENU_ITEM;
1791         
1792         for(a=(int)but->min; a<=(int)but->max; a++) {
1793                 uiDefIconBut(block, BUTM|but->pointype, but->retval, but->icon+(a-but->min), 0, (short)(18*a), (short)(but->x2-but->x1-4), 18, but->poin, (float)a, 0.0, 0, 0, "");
1794         }
1795         block->direction= UI_TOP;       
1796         ui_positionblock(block, but);
1797         
1798         /* the block is made with but-win, but is handled in mainwin space...
1799            this is needs better implementation */
1800         block->win= G.curscreen->mainwin;
1801         
1802         uiDoBlocks(&listb, 0);
1803
1804         but->flag &= ~UI_SELECT;
1805         ui_check_but(but);
1806         ui_draw_but(but);       
1807         
1808         return but->retval;
1809 }
1810
1811 static int ui_do_but_ICONTEXTROW(uiBut *but)
1812 {
1813         uiBlock *block;
1814         ListBase listb={NULL, NULL};
1815         int width, a, xmax, ypos;
1816         MenuData *md;
1817
1818         but->flag |= UI_SELECT;
1819         ui_draw_but(but);
1820         ui_block_flush_back(but->block);        // flush because this button creates own blocks loop
1821
1822         block= uiNewBlock(&listb, "menu", UI_EMBOSSP, UI_HELV, but->win);
1823         block->flag= UI_BLOCK_LOOP|UI_BLOCK_REDRAW|UI_BLOCK_NUMSELECT;
1824         block->themecol= TH_MENU_ITEM;
1825
1826         md= decompose_menu_string(but->str);
1827
1828         /* size and location */
1829         /* expand menu width to fit labels */
1830         if(md->title)
1831                 width= 2*strlen(md->title)+BIF_GetStringWidth(block->curfont, md->title, (U.transopts & USER_TR_MENUS));
1832         else
1833                 width= 0;
1834
1835         for(a=0; a<md->nitems; a++) {
1836                 xmax= BIF_GetStringWidth(block->curfont, md->items[a].str, (U.transopts & USER_TR_MENUS));
1837                 if(xmax>width) width= xmax;
1838         }
1839
1840         width+= 30;
1841         if (width<50) width=50;
1842
1843         ypos = 0;
1844
1845         /* loop through the menu options and draw them out with icons & text labels */
1846         for(a=0; a<md->nitems; a++) {
1847
1848                 /* add a space if there's a separator (%l) */
1849                 if (strcmp(md->items[a].str, "%l")==0) {
1850                         ypos +=3;
1851                 }
1852                 else {
1853                         uiDefIconTextBut(block, BUTM|but->pointype, but->retval, (short)((but->icon)+(md->items[a].retval-but->min)), md->items[a].str, 0, ypos,(short)width, 19, but->poin, (float) md->items[a].retval, 0.0, 0, 0, "");
1854                         ypos += 20;
1855                 }
1856         }
1857
1858         block->direction= UI_TOP;
1859         ui_positionblock(block, but);
1860
1861         /* the block is made with but-win, but is handled in mainwin space...
1862            this is needs better implementation */
1863         block->win= G.curscreen->mainwin;
1864
1865         uiBoundsBlock(block, 3);
1866
1867         uiDoBlocks(&listb, 0);
1868         
1869         menudata_free(md);
1870
1871         but->flag &= ~UI_SELECT;
1872         ui_check_but(but);
1873         ui_draw_but(but);
1874
1875         uibut_do_func(but);
1876
1877         return but->retval;
1878
1879 }
1880
1881 static int ui_do_but_IDPOIN(uiBut *but)
1882 {
1883         char str[UI_MAX_DRAW_STR];
1884         ID *id;
1885         
1886         id= *but->idpoin_idpp;
1887         if(id) strcpy(str, id->name+2);
1888         else str[0]= 0;
1889         
1890         but->type= TEX;
1891         but->poin= str;
1892         but->min= 0.0;
1893         but->max= 22.0;
1894         ui_do_but_TEX(but);
1895         but->poin= NULL;
1896         but->type= IDPOIN;
1897         
1898         but->idpoin_func(str, but->idpoin_idpp);
1899         ui_check_but(but);
1900         ui_draw_but(but);
1901         
1902         return but->retval;
1903 }
1904
1905 static int ui_do_but_SLI(uiBut *but)
1906 {
1907         float f, fstart, tempf = 0.0, deler, value;
1908         int sx, h, temp, pos=0, lvalue, redraw;
1909         short mval[2], qual;
1910         float curmatrix[4][4];
1911
1912         value= ui_get_but_val(but);
1913         uiGetMouse(mywinget(), mval);
1914
1915         sx= mval[0];
1916         h= but->y2-but->y1;
1917         fstart= but->max-but->min;
1918         fstart= (value - but->min)/fstart;
1919         temp= 32767;
1920
1921         if( but->type==NUMSLI) deler= ( (but->x2-but->x1)/2 - 5.0*but->aspect);
1922         else if( but->type==HSVSLI) deler= ( (but->x2-but->x1)/2 - 5.0*but->aspect);
1923         else deler= (but->x2-but->x1- 5.0*but->aspect);
1924         
1925
1926         while (get_mbut() & L_MOUSE) {
1927         
1928                 qual= get_qual();
1929                 uiGetMouse(mywinget(), mval);
1930                 
1931                 f= (float)(mval[0]-sx)/deler +fstart;
1932                 
1933                 if (qual & LR_SHIFTKEY) {
1934                         f= (f-fstart)/10.0 + fstart;
1935                 }
1936
1937                 CLAMP(f, 0.0, 1.0);
1938                 tempf= but->min+f*(but->max-but->min);          
1939                 temp= floor(tempf+.5);
1940
1941                 if(qual & LR_CTRLKEY) {
1942                         if(tempf==but->min || tempf==but->max);
1943                         else if( but->pointype==FLO ) {
1944
1945                                 if(but->max-but->min < 2.10) tempf= 0.1*floor(10*tempf);
1946                                 else if(but->max-but->min < 21.0) tempf= floor(tempf);
1947                                 else tempf= 10.0*floor(tempf/10.0);
1948                         }
1949                         else {
1950                                 temp= 10*(temp/10);
1951                                 tempf= temp;
1952                         }
1953                 } 
1954         
1955                 value= ui_get_but_val(but);
1956                 lvalue= floor(value+0.5);
1957                 
1958                 if( but->pointype!=FLO )
1959                         redraw= (temp != lvalue);
1960                 else
1961                         redraw= (tempf != value);
1962
1963                 if (redraw) {
1964                         pos= 1;
1965                         ui_set_but_val(but, tempf);
1966                         ui_check_but(but);
1967                         ui_draw_but(but);
1968                         ui_block_flush_back(but->block);
1969                         
1970                         if(but->a1) {   /* color number */
1971                                 uiBut *bt= but->prev;
1972                                 while(bt) {
1973                                         if(bt->a2 == but->a1) ui_draw_but(bt);
1974                                         bt= bt->prev;
1975                                 }
1976                                 bt= but->next;
1977                                 while(bt) {
1978                                         if(bt->a2 == but->a1) ui_draw_but(bt);
1979                                         bt= bt->next;
1980                                 }
1981                         }
1982                         /* save current window matrix (global UIwinmat)
1983                            because button callback function MIGHT change it
1984                            - which has until now occured through the Python API
1985                         */
1986                         Mat4CpyMat4(curmatrix, UIwinmat);
1987                         uibut_do_func(but);
1988                         Mat4CpyMat4(UIwinmat, curmatrix);
1989                 } 
1990                 else BIF_wait_for_statechange();
1991         }
1992
1993         
1994         if(temp!=32767 && pos==0) {  /* plus 1 or minus 1 */
1995                 
1996                 if( but->type==SLI) f= (float)(mval[0]-but->x1)/(but->x2-but->x1-h);
1997                 else f= (float)(mval[0]- (but->x1+but->x2)/2)/( (but->x2-but->x1)/2 - h);
1998                 
1999                 f= but->min+f*(but->max-but->min);
2000                 
2001                 if( but->pointype!=FLO ) {
2002
2003                         if(f<temp) temp--;
2004                         else temp++;
2005                         if( temp>=but->min && temp<=but->max)
2006                                 ui_set_but_val(but, (float)temp);
2007                 
2008                 } 
2009                 else {
2010
2011                         if(f<tempf) tempf-=.01;
2012                         else tempf+=.01;
2013                         if( tempf>=but->min && tempf<=but->max)
2014                                 ui_set_but_val(but, tempf);
2015
2016                 }
2017         }
2018         ui_check_but(but);
2019         ui_draw_but(but);
2020         ui_block_flush_back(but->block);
2021         
2022         return but->retval;
2023 }
2024
2025 static int ui_do_but_NUMSLI(uiBut *but)
2026 {
2027         short mval[2];
2028
2029         /* first define if it's a slider or textbut */
2030         uiGetMouse(mywinget(), mval);
2031         
2032         if(mval[0]>= -6+(but->x1+but->x2)/2 ) { /* slider */
2033                 but->flag |= UI_SELECT;
2034                 ui_draw_but(but);
2035                 ui_do_but_SLI(but);
2036                 but->flag &= ~UI_SELECT;
2037         }
2038         else {
2039                 uiActAsTextBut(but);
2040                 uibut_do_func(but);     // this is done in ui_do_but_SLI() not in uiActAsTextBut()
2041         }
2042
2043         while(get_mbut() & L_MOUSE) BIF_wait_for_statechange();
2044         
2045         ui_draw_but(but);
2046         
2047         /* hsv patch */
2048         if(but->type==HSVSLI) {
2049         
2050                 if(but->str[0]=='H') {
2051                         ui_draw_but(but->next);
2052                         ui_draw_but(but->next->next);
2053                 } 
2054                 else if(but->str[0]=='S') {
2055                         ui_draw_but(but->next);
2056                         ui_draw_but(but->prev);
2057                 } 
2058                 else if(but->str[0]=='V') {
2059                         ui_draw_but(but->prev);
2060                         ui_draw_but(but->prev->prev);
2061                 }
2062         }
2063         
2064         return but->retval;
2065 }
2066
2067 static int ui_do_but_BLOCK(uiBut *but)
2068 {
2069         uiBlock *block;
2070         uiBut *bt;
2071         
2072         but->flag |= UI_SELECT;
2073         ui_draw_but(but);       
2074
2075         block= but->block_func(but->poin);
2076
2077         block->xofs = -2;       /* for proper alignment */
2078         
2079         /* only used for automatic toolbox, so can set the shift flag */
2080         if(but->flag & UI_MAKE_TOP) {
2081                 block->direction= UI_TOP|UI_SHIFT_FLIPPED;
2082                 uiBlockFlipOrder(block);
2083         }
2084         if(but->flag & UI_MAKE_DOWN) block->direction= UI_DOWN|UI_SHIFT_FLIPPED;
2085         if(but->flag & UI_MAKE_LEFT) block->direction |= UI_LEFT;
2086         if(but->flag & UI_MAKE_RIGHT) block->direction |= UI_RIGHT;
2087         
2088         ui_positionblock(block, but);
2089         block->flag |= UI_BLOCK_LOOP;
2090         
2091         /* blocks can come (and get scaled) from a normal window, now we go to screenspace */
2092         block->win= G.curscreen->mainwin;
2093         for(bt= block->buttons.first; bt; bt= bt->next) bt->win= block->win;
2094         bwin_getsinglematrix(block->win, block->winmat);
2095         
2096         /* postpone draw, this will cause a new window matrix, first finish all other buttons */
2097         block->flag |= UI_BLOCK_REDRAW;
2098         
2099         but->flag &= ~UI_SELECT;
2100         uibut_do_func(but);
2101         
2102         return 0;
2103 }
2104
2105 static int ui_do_but_BUTM(uiBut *but)
2106 {
2107
2108         ui_set_but_val(but, but->min);
2109         UIafterfunc= but->butm_func;
2110         UIafterfunc_arg= but->butm_func_arg;
2111         UIafterval= but->a2;
2112         
2113         return but->retval;
2114 }
2115
2116 static int ui_do_but_LABEL(uiBut *but)
2117 {
2118
2119         uibut_do_func(but);
2120         return but->retval;
2121 }
2122
2123 static uiBut *ui_get_valid_link_button(uiBlock *block, uiBut *but, short *mval)
2124 {
2125         uiBut *bt;
2126         
2127                 /* find button to link to */
2128         for (bt= block->buttons.first; bt; bt= bt->next)
2129                 if(bt!=but && uibut_contains_pt(bt, mval))
2130                         break;
2131
2132         if (bt) {
2133                 if (but->type==LINK && bt->type==INLINK) {
2134                         if( but->link->tocode == (int)bt->min ) {
2135                                 return bt;
2136                         }
2137                 }
2138                 else if(but->type==INLINK && bt->type==LINK) {
2139                         if( bt->link->tocode == (int)but->min ) {
2140                                 return bt;
2141                         }
2142                 }
2143         }
2144
2145         return NULL;
2146 }
2147
2148 static int ui_is_a_link(uiBut *from, uiBut *to)
2149 {
2150         uiLinkLine *line;
2151         uiLink *link;
2152         
2153         link= from->link;
2154         if(link) {
2155                 line= link->lines.first;
2156                 while(line) {
2157                         if(line->from==from && line->to==to) return 1;
2158                         line= line->next;
2159                 }
2160         }
2161         return 0;
2162 }
2163
2164 static uiBut *ui_find_inlink(uiBlock *block, void *poin)
2165 {
2166         uiBut *but;
2167         
2168         but= block->buttons.first;
2169         while(but) {
2170                 if(but->type==INLINK) {
2171                         if(but->poin == poin) return but;
2172                 }
2173                 but= but->next;
2174         }
2175         return NULL;
2176 }
2177
2178 static void ui_add_link_line(ListBase *listb, uiBut *but, uiBut *bt)
2179 {
2180         uiLinkLine *line;
2181         
2182         line= MEM_callocN(sizeof(uiLinkLine), "linkline");
2183         BLI_addtail(listb, line);
2184         line->from= but;
2185         line->to= bt;
2186 }
2187
2188
2189 void uiComposeLinks(uiBlock *block)
2190 {
2191         uiBut *but, *bt;
2192         uiLink *link;
2193         void ***ppoin;
2194         int a;
2195         
2196         but= block->buttons.first;
2197         while(but) {
2198                 if(but->type==LINK) {
2199                         link= but->link;
2200                         
2201                         /* for all pointers in the array */
2202                         if(link) {
2203                                 if(link->ppoin) {
2204                                         ppoin= link->ppoin;
2205                                         for(a=0; a < *(link->totlink); a++) {
2206                                                 bt= ui_find_inlink(block, (*ppoin)[a] );
2207                                                 if(bt) {
2208                                                         ui_add_link_line(&link->lines, but, bt);
2209                                                 }
2210                                         }
2211                                 }
2212                                 else if(link->poin) {
2213                                         bt= ui_find_inlink(block, *(link->poin) );
2214                                         if(bt) {
2215                                                 ui_add_link_line(&link->lines, but, bt);
2216                                         }
2217                                 }
2218                         }
2219                 }
2220                 but= but->next;
2221         }
2222 }
2223
2224 static void ui_add_link(uiBut *from, uiBut *to)
2225 {
2226         /* in 'from' we have to add a link to 'to' */
2227         uiLink *link;
2228         void **oldppoin;
2229         int a;
2230         
2231         if(ui_is_a_link(from, to)) {
2232                 printf("already exists\n");
2233                 return;
2234         }
2235         
2236         link= from->link;
2237
2238         /* are there more pointers allowed? */
2239         if(link->ppoin) {
2240                 oldppoin= *(link->ppoin);
2241                 
2242                 (*(link->totlink))++;
2243                 *(link->ppoin)= MEM_callocN( *(link->totlink)*sizeof(void *), "new link");
2244
2245                 for(a=0; a< (*(link->totlink))-1; a++) {
2246                         (*(link->ppoin))[a]= oldppoin[a];
2247                 }
2248                 (*(link->ppoin))[a]= to->poin;
2249                 
2250                 if(oldppoin) MEM_freeN(oldppoin);
2251         }
2252         else {
2253                 *(link->poin)= to->poin;
2254         }
2255         
2256 }
2257
2258 static int ui_do_but_LINK(uiBlock *block, uiBut *but)
2259 {
2260         /* 
2261          * This button only visualizes, the dobutton mode
2262          * can add a new link, but then the whole system
2263          * should be redrawn/initialized. 
2264          * 
2265          */
2266         uiBut *bt=0, *bto=NULL;
2267         short sval[2], mval[2], mvalo[2], first= 1;
2268
2269         uiGetMouse(curarea->win, sval);
2270         mvalo[0]= sval[0];
2271         mvalo[1]= sval[1];
2272         
2273         while (get_mbut() & L_MOUSE) {
2274                 uiGetMouse(curarea->win, mval);
2275
2276                 if(mval[0]!=mvalo[0] || mval[1]!=mvalo[1] || first) {                   
2277                                 /* clear completely, because of drawbuttons */
2278                         bt= ui_get_valid_link_button(block, but, mval);
2279                         if(bt) {
2280                                 bt->flag |= UI_ACTIVE;
2281                                 ui_draw_but(bt);
2282                         }
2283                         if(bto && bto!=bt) {
2284                                 bto->flag &= ~UI_ACTIVE;
2285                                 ui_draw_but(bto);
2286                         }
2287                         bto= bt;
2288
2289                         if (!first) {
2290                                 glutil_draw_front_xor_line(sval[0], sval[1], mvalo[0], mvalo[1]);
2291                         }
2292                         glutil_draw_front_xor_line(sval[0], sval[1], mval[0], mval[1]);
2293
2294                         mvalo[0]= mval[0];
2295                         mvalo[1]= mval[1];
2296
2297                         first= 0;
2298                 }
2299                 else BIF_wait_for_statechange();                
2300         }
2301         
2302         if (!first) {
2303                 glutil_draw_front_xor_line(sval[0], sval[1], mvalo[0], mvalo[1]);
2304         }
2305
2306         if(bt) {
2307                 if(but->type==LINK) ui_add_link(but, bt);
2308                 else ui_add_link(bt, but);
2309
2310                 scrarea_queue_winredraw(curarea);
2311         }
2312
2313         return 0;
2314 }
2315
2316 /* picker sizes S hsize, F full size, D spacer, B button/pallette height  */
2317 #define SPICK   110.0
2318 #define FPICK   180.0
2319 #define DPICK   6.0
2320 #define BPICK   24.0
2321
2322 #define UI_PALETTE_TOT 16
2323 /* note; in tot+1 the old color is stored */
2324 static float palette[UI_PALETTE_TOT+1][3]= {
2325 {0.93, 0.83, 0.81}, {0.88, 0.89, 0.73}, {0.69, 0.81, 0.57}, {0.51, 0.76, 0.64}, 
2326 {0.37, 0.56, 0.61}, {0.33, 0.29, 0.55}, {0.46, 0.21, 0.51}, {0.40, 0.12, 0.18}, 
2327 {1.0, 1.0, 1.0}, {0.85, 0.85, 0.85}, {0.7, 0.7, 0.7}, {0.56, 0.56, 0.56}, 
2328 {0.42, 0.42, 0.42}, {0.28, 0.28, 0.28}, {0.14, 0.14, 0.14}, {0.0, 0.0, 0.0}
2329 };  
2330
2331 /* for picker, while editing hsv */
2332 static void ui_set_but_hsv(uiBut *but)
2333 {
2334         float col[3];
2335         
2336         hsv_to_rgb(but->hsv[0], but->hsv[1], but->hsv[2], col, col+1, col+2);
2337         ui_set_but_vectorf(but, col);
2338 }
2339
2340 static void update_picker_buts(uiBlock *block, float *hsv)
2341 {
2342         uiBut *bt;
2343         float r, g, b;
2344         
2345         // this updates button strings, is hackish... but button pointers are on stack of caller function
2346         hsv_to_rgb(hsv[0], hsv[1], hsv[2], &r, &g, &b);
2347
2348         for(bt= block->buttons.first; bt; bt= bt->next) {
2349                 if(bt->type==HSVCUBE) {
2350                         VECCOPY(bt->hsv, hsv);
2351                         ui_set_but_hsv(bt);
2352                 }
2353                 else if(bt->str[1]==' ') {
2354                         if(bt->str[0]=='R') {
2355                                 ui_set_but_val(bt, r);
2356                                 ui_check_but(bt);
2357                         }
2358                         else if(bt->str[0]=='G') {
2359                                 ui_set_but_val(bt, g);
2360                                 ui_check_but(bt);
2361                         }
2362                         else if(bt->str[0]=='B') {
2363                                 ui_set_but_val(bt, b);
2364                                 ui_check_but(bt);
2365                         }
2366                         else if(bt->str[0]=='H') {
2367                                 ui_set_but_val(bt, hsv[0]);
2368                                 ui_check_but(bt);
2369                         }
2370                         else if(bt->str[0]=='S') {
2371                                 ui_set_but_val(bt, hsv[1]);
2372                                 ui_check_but(bt);
2373                         }
2374                         else if(bt->str[0]=='V') {
2375                                 ui_set_but_val(bt, hsv[2]);
2376                                 ui_check_but(bt);
2377                         }
2378                 }
2379         }
2380 }
2381
2382 /* bt1 is palette but, col1 is original color */
2383 /* callback to copy from/to palette */
2384 static void do_palette_cb(void *bt1, void *col1)
2385 {
2386         uiBut *but1= (uiBut *)bt1;
2387         uiBut *but;
2388         float *col= (float *)col1;
2389         float *fp, hsv[3];
2390         
2391         fp= (float *)but1->poin;
2392         
2393         if( (get_qual() & LR_CTRLKEY) ) {
2394                 VECCOPY(fp, col);
2395         }
2396         else {
2397                 VECCOPY(col, fp);
2398         }
2399         
2400         rgb_to_hsv(col[0], col[1], col[2], hsv, hsv+1, hsv+2);
2401         update_picker_buts(but1->block, hsv);
2402         
2403         for (but= but1->block->buttons.first; but; but= but->next) {
2404                 ui_draw_but(but);
2405         }
2406         but= but1->block->buttons.first;
2407         ui_block_flush_back(but->block);
2408 }
2409
2410 /* bt1 is num but, col1 is pointer to original color */
2411 /* callback to handle changes in num-buts in picker */
2412 static void do_palette1_cb(void *bt1, void *hsv1)
2413 {
2414         uiBut *but1= (uiBut *)bt1;
2415         uiBut *but;
2416         float *hsv= (float *)hsv1;
2417         float *fp= NULL;
2418         
2419         if(but1->str[0]=='R') fp= (float *)but1->poin;
2420         else if(but1->str[0]=='G') fp= ((float *)but1->poin)-1;
2421         else if(but1->str[0]=='B') fp= ((float *)but1->poin)-2;
2422         
2423         if(fp) {
2424                 rgb_to_hsv(fp[0], fp[1], fp[2], hsv, hsv+1, hsv+2);
2425         }
2426
2427         update_picker_buts(but1->block, hsv);   
2428         
2429         for (but= but1->block->buttons.first; but; but= but->next) {
2430                 ui_draw_but(but);
2431         }
2432         
2433         but= but1->block->buttons.first;
2434         ui_block_flush_back(but->block);
2435
2436 }
2437
2438 /* color picker, Gimp version. mode: 'f' = floating panel, 'p' =  popup */
2439 /* col = read/write to, hsv/old = memory for temporal use */
2440 void uiBlockPickerButtons(uiBlock *block, float *col, float *hsv, float *old, char mode, short retval)
2441 {
2442         uiBut *bt;
2443         float h, offs;
2444         int a;
2445
2446         VECCOPY(old, col);      // old color stored there, for palette_cb to work
2447         
2448         // the cube intersection
2449         bt= uiDefButF(block, HSVCUBE, retval, "",       0,DPICK+BPICK,FPICK,FPICK, col, 0.0, 0.0, 2, 0, "");
2450         uiButSetFlag(bt, UI_NO_HILITE);
2451
2452         bt= uiDefButF(block, HSVCUBE, retval, "",       0,0,FPICK,BPICK, col, 0.0, 0.0, 3, 0, "");
2453         uiButSetFlag(bt, UI_NO_HILITE);
2454
2455         // palette
2456         
2457         uiBlockSetEmboss(block, UI_EMBOSSP);
2458         
2459         bt=uiDefButF(block, COL, retval, "",            FPICK+DPICK, 0, BPICK,BPICK, old, 0.0, 0.0, -1, 0, "Old color, click to restore");
2460         uiButSetFunc(bt, do_palette_cb, bt, col);
2461         uiDefButF(block, COL, retval, "",               FPICK+DPICK, BPICK+DPICK, BPICK,60-BPICK-DPICK, col, 0.0, 0.0, -1, 0, "Active color");
2462
2463         h= (DPICK+BPICK+FPICK-64)/(UI_PALETTE_TOT/2.0);
2464         uiBlockBeginAlign(block);
2465         for(a= -1+UI_PALETTE_TOT/2; a>=0; a--) {
2466                 bt= uiDefButF(block, COL, retval, "",   FPICK+DPICK, 65.0+(float)a*h, BPICK/2, h, palette[a+UI_PALETTE_TOT/2], 0.0, 0.0, -1, 0, "Click to choose, hold CTRL to store in palette");
2467                 uiButSetFunc(bt, do_palette_cb, bt, col);
2468                 bt= uiDefButF(block, COL, retval, "",   FPICK+DPICK+BPICK/2, 65.0+(float)a*h, BPICK/2, h, palette[a], 0.0, 0.0, -1, 0, "Click to choose, hold CTRL to store in palette");               
2469                 uiButSetFunc(bt, do_palette_cb, bt, col);
2470         }
2471         uiBlockEndAlign(block);
2472         
2473         uiBlockSetEmboss(block, UI_EMBOSSX);
2474
2475         // buttons
2476         rgb_to_hsv(col[0], col[1], col[2], hsv, hsv+1, hsv+2);
2477
2478         offs= FPICK+2*DPICK+BPICK;
2479
2480
2481         uiBlockBeginAlign(block);
2482         bt= uiDefButF(block, NUM, retval, "R ", offs, 110, 80,20, col, 0.0, 1.0, 10, 2, "");
2483         uiButSetFunc(bt, do_palette1_cb, bt, hsv);
2484         bt= uiDefButF(block, NUM, retval, "G ", offs, 90, 80,20, col+1, 0.0, 1.0, 10, 2, "");
2485         uiButSetFunc(bt, do_palette1_cb, bt, hsv);
2486         bt= uiDefButF(block, NUM, retval, "B ", offs, 70, 80,20, col+2, 0.0, 1.0, 10, 2, "");
2487         uiButSetFunc(bt, do_palette1_cb, bt, hsv);
2488         
2489         uiBlockBeginAlign(block);
2490         bt= uiDefButF(block, NUM, retval, "H ", offs, 40, 80,20, hsv, 0.0, 1.0, 10, 2, "");
2491         uiButSetFunc(bt, do_palette1_cb, bt, hsv);
2492         bt= uiDefButF(block, NUM, retval, "S ", offs, 20, 80,20, hsv+1, 0.0, 1.0, 10, 2, "");
2493         uiButSetFunc(bt, do_palette1_cb, bt, hsv);
2494         bt= uiDefButF(block, NUM, retval, "V ", offs, 0, 80,20, hsv+2, 0.0, 1.0, 10, 2, "");
2495         uiButSetFunc(bt, do_palette1_cb, bt, hsv);
2496         uiBlockEndAlign(block);
2497 }
2498
2499
2500 static int ui_do_but_COL(uiBut *but)
2501 {
2502         uiBlock *block;
2503         uiBut *bt;
2504         ListBase listb={NULL, NULL};
2505         float hsv[3], old[3], *poin= NULL, colstore[3];
2506         short event;
2507         
2508         // signal to prevent calling up color picker
2509         if(but->a1 == -1) {
2510                 uibut_do_func(but);
2511                 return but->retval;
2512         }
2513         
2514         // enable char button too, use temporal colstore for color
2515         if(but->pointype!=FLO) {
2516                 if(but->pointype==CHA) {
2517                         ui_get_but_vectorf(but, colstore);
2518                         poin= colstore;
2519                 }
2520                 else return but->retval;
2521         }
2522         else poin= (float *)but->poin;
2523         
2524         block= uiNewBlock(&listb, "colorpicker", UI_EMBOSSX, UI_HELV, but->win);
2525         block->flag= UI_BLOCK_LOOP|UI_BLOCK_REDRAW;
2526         block->themecol= TH_BUT_NUM;
2527         
2528         uiBlockPickerButtons(block, poin, hsv, old, 'p', 0);
2529
2530         /* and lets go */
2531         block->direction= UI_TOP;
2532         ui_positionblock(block, but);
2533         uiBoundsBlock(block, 3);
2534         
2535         /* blocks can come from a normal window, but we go to screenspace */
2536         block->win= G.curscreen->mainwin;
2537         for(bt= block->buttons.first; bt; bt= bt->next) bt->win= block->win;
2538         bwin_getsinglematrix(block->win, block->winmat);
2539
2540         event= uiDoBlocks(&listb, 0);
2541         
2542         if(but->pointype==CHA) ui_set_but_vectorf(but, colstore);
2543         
2544         return but->retval;
2545
2546 }
2547
2548 static int ui_do_but_HSVCUBE(uiBut *but)
2549 {
2550         uiBut *bt;
2551         float x, y;
2552         short mval[2], mvalo[2];
2553         
2554         mvalo[0]= mvalo[1]= -32000;
2555         
2556         /* we work on persistant hsv, to prevent it being converted back and forth all the time */
2557                            
2558         while (get_mbut() & L_MOUSE) {
2559                 
2560                 uiGetMouse(mywinget(), mval);
2561
2562                 if(mval[0]!=mvalo[0] || mval[1]!=mvalo[1]) {                    
2563                         mvalo[0]= mval[0];
2564                         mvalo[1]= mval[1];
2565                         
2566                         /* relative position within box */
2567                         x= ((float)mval[0]-but->x1)/(but->x2-but->x1);
2568                         y= ((float)mval[1]-but->y1)/(but->y2-but->y1);
2569                         CLAMP(x, 0.0, 1.0);
2570                         CLAMP(y, 0.0, 1.0);
2571                         
2572                         if(but->a1==0) {
2573                                 but->hsv[0]= x; 
2574                                 but->hsv[2]= y; 
2575                                 // hsv_to_rgb(x, s, y, col, col+1, col+2);
2576                         }
2577                         else if(but->a1==1) {
2578                                 but->hsv[0]= x;                                 
2579                                 but->hsv[1]= y;                                 
2580                                 // hsv_to_rgb(x, y, v, col, col+1, col+2);
2581                         }
2582                         else if(but->a1==2) {
2583                                 but->hsv[2]= x; 
2584                                 but->hsv[1]= y; 
2585                                 // hsv_to_rgb(h, y, x, col, col+1, col+2);
2586                         }
2587                         else {
2588                                 but->hsv[0]= x; 
2589                                 // hsv_to_rgb(x, s, v, col, col+1, col+2);
2590                         }
2591         
2592                         ui_set_but_hsv(but);    // converts to rgb
2593                         
2594                         // update button values and strings
2595                         update_picker_buts(but->block, but->hsv);
2596
2597                         /* we redraw the entire block */
2598                         for (bt= but->block->buttons.first; bt; bt= bt->next) {
2599                                 if(but->poin == bt->poin) VECCOPY(bt->hsv, but->hsv);
2600                                 ui_draw_but(bt);
2601                         }
2602                         ui_block_flush_back(but->block);
2603                 }
2604                 else BIF_wait_for_statechange();
2605         }
2606
2607         return but->retval;
2608 }
2609
2610
2611 /* ************************************************ */
2612
2613 void uiSetButLock(int val, char *lockstr)
2614 {
2615         UIlock |= val;
2616         if (val) UIlockstr= lockstr;
2617 }
2618
2619 void uiClearButLock()
2620 {
2621         UIlock= 0;
2622         UIlockstr= NULL;
2623 }
2624
2625 /* ********************** NEXT/PREV for arrowkeys etc ************** */
2626
2627 static uiBut *ui_but_prev(uiBut *but)
2628 {
2629         while(but->prev) {
2630                 but= but->prev;
2631                 if(but->type!=LABEL && but->type!=SEPR) return but;
2632         }
2633         return NULL;
2634 }
2635
2636 static uiBut *ui_but_next(uiBut *but)
2637 {
2638         while(but->next) {
2639                 but= but->next;
2640                 if(but->type!=LABEL && but->type!=SEPR) return but;
2641         }
2642         return NULL;
2643 }
2644
2645 static uiBut *ui_but_first(uiBlock *block)
2646 {
2647         uiBut *but;
2648         
2649         but= block->buttons.first;
2650         while(but) {
2651                 if(but->type!=LABEL && but->type!=SEPR) return but;
2652                 but= but->next;
2653         }
2654         return NULL;
2655 }
2656
2657 static uiBut *ui_but_last(uiBlock *block)
2658 {
2659         uiBut *but;
2660         
2661         but= block->buttons.last;
2662         while(but) {
2663                 if(but->type!=LABEL && but->type!=SEPR) return but;
2664                 but= but->prev;
2665         }
2666         return NULL;
2667 }
2668
2669 /* *************************************************************** */
2670
2671 static void setup_file(uiBlock *block)
2672 {
2673         uiBut *but;
2674         FILE *fp;
2675
2676         fp= fopen("butsetup","w");
2677         if(fp==NULL);
2678         else {
2679                 but= block->buttons.first;
2680                 while(but) {
2681                         ui_check_but(but);
2682                         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);
2683                         but= but->next;
2684                 }
2685                 fclose(fp);
2686         }
2687 }
2688
2689
2690 static void edit_but(uiBlock *block, uiBut *but, uiEvent *uevent)
2691 {
2692         short dx, dy, mval[2], mvalo[2], didit=0;
2693         
2694         getmouseco_sc(mvalo);
2695         while(TRUE) {
2696                 if( !(get_mbut() & L_MOUSE) ) break;    
2697         
2698                 getmouseco_sc(mval);
2699                 dx= (mval[0]-mvalo[0]);
2700                 dy= (mval[1]-mvalo[1]);
2701                 
2702                 if(dx!=0 || dy!=0) {
2703                         mvalo[0]= mval[0];
2704                         mvalo[1]= mval[1];
2705                         
2706                         cpack(0xc0c0c0);
2707                         glRectf(but->x1-2, but->y1-2, but->x2+2, but->y2+2); 
2708                         
2709                         if((uevent->qual & LR_SHIFTKEY)==0) {
2710                                 but->x1 += dx;
2711                                 but->y1 += dy;
2712                         }
2713                         but->x2 += dx;
2714                         but->y2 += dy;
2715                         
2716                         ui_draw_but(but);
2717                         ui_block_flush_back(but->block);
2718                         didit= 1;
2719
2720                 }
2721                 /* idle for this poor code */
2722                 else PIL_sleep_ms(30);
2723         }
2724         if(didit) setup_file(block);
2725 }
2726
2727
2728 /* is called when LEFTMOUSE is pressed or released
2729  * return: butval or zero
2730  */
2731 static int ui_do_button(uiBlock *block, uiBut *but, uiEvent *uevent)
2732 {
2733         int retval= 0;
2734
2735         if(but->lock) {
2736                 if (but->lockstr) {
2737                         error("%s", but->lockstr);
2738                         return 0;
2739                 }
2740         } 
2741         else {
2742                 if( but->pointype ) {           /* there's a pointer needed */
2743                         if(but->poin==0 ) {
2744                                 printf("DoButton pointer error: %s\n",but->str);
2745                                 return 0;
2746                         }
2747                 }
2748         }
2749
2750         if(G.rt==1 && (uevent->qual & LR_CTRLKEY)) {
2751                 edit_but(block, but, uevent);
2752                 return 0;
2753         }
2754         
2755         block->flag |= UI_BLOCK_BUSY;
2756
2757         switch(but->type) {
2758         case BUT:
2759                 if(uevent->val) retval= ui_do_but_BUT(but);             
2760                 break;
2761
2762         case KEYEVT:
2763                 if(uevent->val) retval= ui_do_but_KEYEVT(but);
2764                 break;
2765
2766         case TOG: 
2767         case TOGR: 
2768         case ICONTOG: 
2769         case TOGN:
2770                 if(uevent->val) {
2771                         retval= ui_do_but_TOG(block, but);
2772                 }
2773                 break;
2774                 
2775         case ROW:
2776                 if(uevent->val) retval= ui_do_but_ROW(block, but);
2777                 break;
2778
2779         case SCROLL:
2780                 /* DrawBut(b, 1); */
2781                 /* do_scrollbut(b); */
2782                 /* DrawBut(b,0); */
2783                 break;
2784
2785         case NUM:
2786                 if(uevent->val) retval= ui_do_but_NUM(but);
2787                 break;
2788                 
2789         case SLI:
2790         case NUMSLI:
2791         case HSVSLI:
2792                 if(uevent->val) retval= ui_do_but_NUMSLI(but);
2793                 break;
2794                 
2795         case LABEL:     
2796                 if(uevent->val) retval= ui_do_but_LABEL(but);
2797                 break;
2798                 
2799         case TOG3:      
2800                 if(uevent->val) retval= ui_do_but_TOG3(but);
2801                 break;
2802                 
2803         case TEX:
2804                 if(uevent->val) retval= ui_do_but_TEX(but);
2805                 break;
2806                 
2807         case MENU:
2808                 if(uevent->val) retval= ui_do_but_MENU(but);
2809                 break;
2810                 
2811         case ICONROW:
2812                 if(uevent->val) retval= ui_do_but_ICONROW(but);         
2813                 break;
2814                 
2815         case ICONTEXTROW:
2816                 if(uevent->val) retval= ui_do_but_ICONTEXTROW(but);
2817                 break;
2818
2819         case IDPOIN:
2820                 if(uevent->val) retval= ui_do_but_IDPOIN(but);  
2821                 break;
2822         
2823         case BLOCK:
2824         case PULLDOWN:
2825                 if(uevent->val) {
2826                         retval= ui_do_but_BLOCK(but);
2827                         if(block->auto_open==0) block->auto_open= 1;
2828                 }
2829                 break;
2830
2831         case BUTM:
2832                 retval= ui_do_but_BUTM(but);
2833                 break;
2834
2835         case LINK:
2836         case INLINK:
2837                 retval= ui_do_but_LINK(block, but);
2838                 break;
2839                 
2840         case COL:
2841                 if(uevent->val) retval= ui_do_but_COL(but);
2842                 break;
2843                 
2844         case HSVCUBE:
2845                 retval= ui_do_but_HSVCUBE(but);
2846                 break;
2847         }
2848         
2849         block->flag &= ~UI_BLOCK_BUSY;
2850
2851         return retval;
2852 }
2853
2854 static void ui_delete_active_linkline(uiBlock *block)
2855 {
2856         uiBut *but;
2857         uiLink *link;
2858         uiLinkLine *line, *nline;
2859         int a, b;
2860         
2861         but= block->buttons.first;
2862         while(but) {
2863                 if(but->type==LINK && but->link) {
2864                         line= but->link->lines.first;
2865                         while(line) {
2866                                 
2867                                 nline= line->next;
2868                                 
2869                                 if(line->flag & UI_SELECT) {
2870                                         BLI_remlink(&but->link->lines, line);
2871
2872                                         link= line->from->link;
2873
2874                                         /* are there more pointers allowed? */
2875                                         if(link->ppoin) {
2876                                                 
2877                                                 if(*(link->totlink)==1) {
2878                                                         *(link->totlink)= 0;
2879                                                         MEM_freeN(*(link->ppoin));
2880                                                         *(link->ppoin)= NULL;
2881                                                 }
2882                                                 else {
2883                                                         b= 0;
2884                                                         for(a=0; a< (*(link->totlink)); a++) {
2885                                                                 
2886                                                                 if( (*(link->ppoin))[a] != line->to->poin ) {
2887                                                                         (*(link->ppoin))[b]= (*(link->ppoin))[a];
2888                                                                         b++;
2889                                                                 }
2890                                                         }       
2891                                                         (*(link->totlink))--;
2892                                                 }
2893                                         }
2894                                         else {
2895                                                 *(link->poin)= NULL;
2896                                         }
2897
2898                                         MEM_freeN(line);
2899                                 }
2900                                 line= nline;
2901                         }
2902                 }
2903                 but= but->next;
2904         }
2905         
2906         /* temporal! these buttons can be everywhere... */
2907         allqueue(REDRAWBUTSLOGIC, 0);
2908 }
2909
2910 static void ui_do_active_linklines(uiBlock *block, short *mval)
2911 {
2912         uiBut *but;
2913         uiLinkLine *line, *act= NULL;
2914         float mindist= 12.0, fac, v1[2], v2[2], v3[3];
2915         int foundone= 0; 
2916         
2917         if(mval) {
2918                 v1[0]= mval[0];
2919                 v1[1]= mval[1];
2920                 
2921                 /* find a line close to the mouse */
2922                 but= block->buttons.first;
2923                 while(but) {
2924                         if(but->type==LINK && but->link) {
2925                                 foundone= 1;
2926                                 line= but->link->lines.first;
2927                                 while(line) {
2928                                         v2[0]= line->from->x2;
2929                                         v2[1]= (line->from->y1+line->from->y2)/2.0;
2930                                         v3[0]= line->to->x1;
2931                                         v3[1]= (line->to->y1+line->to->y2)/2.0;
2932                                         
2933                                         fac= PdistVL2Dfl(v1, v2, v3);
2934                                         if(fac < mindist) {
2935                                                 mindist= fac;
2936                                                 act= line;
2937                                         }
2938                                         line= line->next;
2939                                 }
2940                         }
2941                         but= but->next;
2942                 }
2943         }
2944
2945         /* check for a 'found one' to prevent going to 'frontbuffer' mode.
2946                 this slows done gfx quite some, and at OSX the 'finish' forces a swapbuffer */
2947         if(foundone) {
2948                 glDrawBuffer(GL_FRONT);
2949                 
2950                 /* draw */
2951                 but= block->buttons.first;
2952                 while(but) {
2953                         if(but->type==LINK && but->link) {
2954                                 line= but->link->lines.first;
2955                                 while(line) {
2956                                         if(line==act) {
2957                                                 if((line->flag & UI_SELECT)==0) {
2958                                                         line->flag |= UI_SELECT;
2959                                                         ui_draw_linkline(but, line);
2960                                                 }
2961                                         }
2962                                         else if(line->flag & UI_SELECT) {
2963                                                 line->flag &= ~UI_SELECT;
2964                                                 ui_draw_linkline(but, line);
2965                                         }
2966                                         line= line->next;
2967                                 }
2968                         }
2969                         but= but->next;
2970                 }
2971                 glFlush();
2972                 glDrawBuffer(GL_BACK);
2973         }
2974 }
2975
2976
2977 /* only to be used to prevent an 'outside' event when using nested pulldowns */
2978 /* only one checks:
2979   - while mouse moves in triangular area defined old mouse position and left/right side of new menu
2980   - only for 1 second
2981   
2982   return 0: check outside
2983 */
2984 static int ui_mouse_motion_towards_block(uiBlock *block, uiEvent *uevent)
2985 {
2986         short mvalo[2], dx, dy, domx, domy;
2987         int counter=0;
2988
2989         if((block->direction & UI_TOP) || (block->direction & UI_DOWN)) return 0;
2990         if(uevent->event!= MOUSEX && uevent->event!= MOUSEY) return 0;
2991         
2992         /* calculate dominant direction */
2993         domx= ( -uevent->mval[0] + (block->maxx+block->minx)/2 );
2994         domy= ( -uevent->mval[1] + (block->maxy+block->miny)/2 );
2995         /* we need some accuracy */
2996         if( abs(domx)<4 ) return 0;
2997         
2998         uiGetMouse(mywinget(), mvalo);
2999         
3000         while(TRUE) {
3001                 uiGetMouse(mywinget(), uevent->mval);
3002                 
3003                 /* check inside, if so return */
3004                 if( block->minx <= uevent->mval[0] && block->maxx >= uevent->mval[0] ) {                
3005                         if( block->miny <= uevent->mval[1] && block->maxy >= uevent->mval[1] ) {
3006                                 return 1;
3007                         }
3008                 }
3009                 
3010                 /* check direction */
3011                 dx= uevent->mval[0] - mvalo[0];
3012                 dy= uevent->mval[1] - mvalo[1];
3013                                 
3014                 if( abs(dx)+abs(dy)>4 ) {  // threshold
3015                         /* menu to right */
3016                         if(domx>0) {
3017                                 int fac= (uevent->mval[0] - mvalo[0])*(mvalo[1] - (short)(block->maxy +20)) + (uevent->mval[1] - mvalo[1])*(-mvalo[0] + (short)block->minx);
3018                                 if( (fac>0)) {
3019                                         // printf("Left outside 1, Fac %d\n", fac); 
3020                                         return 0;
3021                                 }
3022                                 
3023                                 fac= (uevent->mval[0] - mvalo[0])*(mvalo[1] - (short)(block->miny-20)) + (uevent->mval[1] - mvalo[1])*(-mvalo[0] + (short)block->minx);
3024                                 if( (fac<0))  {
3025                                         //printf("Left outside 2, Fac %d\n", fac); 
3026                                         return 0;
3027                                 }
3028                         }
3029                         else {
3030                                 int fac= (uevent->mval[0] - mvalo[0])*(mvalo[1] - (short)(block->maxy+20)) + (uevent->mval[1] - mvalo[1])*(-mvalo[0] + (short)block->maxx);
3031                                 if( (fac<0)) {
3032                                         // printf("Left outside 1, Fac %d\n", fac); 
3033                                         return 0;
3034                                 }
3035                                 
3036                                 fac= (uevent->mval[0] - mvalo[0])*(mvalo[1] - (short)(block->miny-20)) + (uevent->mval[1] - mvalo[1])*(-mvalo[0] + (short)block->maxx);
3037                                 if( (fac>0))  {
3038                                         // printf("Left outside 2, Fac %d\n", fac); 
3039                                         return 0;
3040                                 }
3041                         }
3042                 }
3043                 
3044                 /* idle for this poor code */
3045                 PIL_sleep_ms(10);
3046                 counter++;
3047                 if(counter > 100) {
3048                         //printf("left because of timer (1 sec)\n");
3049                         return 0;
3050                 }
3051         }
3052         
3053         return 0;
3054 }
3055
3056
3057 static void ui_set_ftf_font(uiBlock *block)
3058 {
3059
3060 #ifdef INTERNATIONAL
3061         if(block->aspect<1.15) {
3062                 FTF_SetFontSize('l');
3063         }
3064         else if(block->aspect<1.59) {
3065                 FTF_SetFontSize('m');
3066         }
3067         else {
3068                 FTF_SetFontSize('s');
3069         }
3070 #endif
3071 }
3072
3073
3074 /* return: 
3075  * UI_NOTHING   pass event to other ui's
3076  * UI_CONT              don't pass event to other ui's
3077  * UI_RETURN    something happened, return, swallow event
3078  */
3079 static int ui_do_block(uiBlock *block, uiEvent *uevent)
3080 {
3081         uiBut *but, *bt;
3082         int butevent, event, retval=UI_NOTHING, count, act=0;
3083         int inside= 0, active=0;
3084         
3085         if(block->win != mywinget()) return UI_NOTHING;
3086
3087         /* filter some unwanted events */
3088         /* btw: we allow event==0 for first time in menus, draws the hilited item */
3089         if(uevent==0 || uevent->event==LEFTSHIFTKEY || uevent->event==RIGHTSHIFTKEY) return UI_NOTHING;
3090         
3091         if(block->flag & UI_BLOCK_ENTER_OK) {
3092                 if((uevent->event==RETKEY || uevent->event==PADENTER) && uevent->val) {
3093                         // printf("qual: %d %d %d\n", uevent->qual, get_qual(), G.qual);
3094                         if ((G.qual & LR_SHIFTKEY) == 0) {
3095                                 return UI_RETURN_OK;
3096                         }
3097                 }
3098         }               
3099
3100         ui_set_ftf_font(block); // sets just a pointer in ftf lib... the button dont have ftf handles
3101         
3102         // added this for panels in windows with buttons... 
3103         // maybe speed optimize should require test
3104         if((block->flag & UI_BLOCK_LOOP)==0) {
3105                 glMatrixMode(GL_PROJECTION);
3106                 bwin_load_winmatrix(block->win, block->winmat);
3107                 glMatrixMode(GL_MODELVIEW);
3108                 glLoadIdentity();
3109         }
3110
3111         Mat4CpyMat4(UIwinmat, block->winmat);
3112         uiPanelPush(block); // push matrix; no return without pop!
3113
3114         uiGetMouse(mywinget(), uevent->mval);   /* transformed mouseco */
3115
3116         /* check boundbox and panel events */
3117         if( block->minx <= uevent->mval[0] && block->maxx >= uevent->mval[0] ) {
3118                 // inside block
3119                 if( block->miny <= uevent->mval[1] && block->maxy >= uevent->mval[1] ) inside= 1;
3120                 
3121                 if(block->panel && block->panel->paneltab==NULL) {
3122                         
3123                         /* clicked at panel header? */
3124                         if( block->panel->flag & PNL_CLOSEDX) {
3125                                 if(block->minx <= uevent->mval[0] && block->minx+PNL_HEADER >= uevent->mval[0]) 
3126                                         inside= 2;
3127                         }
3128                         else if( (block->maxy <= uevent->mval[1]) && (block->maxy+PNL_HEADER >= uevent->mval[1]) )
3129                                 inside= 2;
3130                                 
3131                         if(inside) {    // this stuff should move to do_panel
3132                                 
3133                                 if(uevent->event==LEFTMOUSE) {
3134                                         if(inside==2) {
3135                                                 uiPanelPop(block);      // pop matrix; no return without pop!
3136                                                 ui_do_panel(block, uevent);
3137                                                 return UI_EXIT_LOOP;    // exit loops because of moving panels
3138                                         }
3139                                 }
3140                                 else if(uevent->event==ESCKEY) {
3141                                         if(block->handler) {
3142                                                 rem_blockhandler(curarea, block->handler);
3143                                                 addqueue(curarea->win, REDRAW, 1);
3144                                         }
3145                                 }
3146                                 else if(uevent->event==PADPLUSKEY || uevent->event==PADMINUS) {
3147                                         SpaceLink *sl= curarea->spacedata.first;
3148                                         if(curarea->spacetype!=SPACE_BUTS) {
3149                                                 if(uevent->event==PADPLUSKEY) sl->blockscale+= 0.1;
3150                                                 else sl->blockscale-= 0.1;
3151                                                 CLAMP(sl->blockscale, 0.6, 1.0);
3152                                                 addqueue(block->winq, REDRAW, 1);
3153                                                 retval= UI_CONT;
3154                                         }
3155                                 }
3156                         }
3157                 }
3158         }
3159         
3160         switch(uevent->event) {
3161         case LEFTARROWKEY:      // later on implement opening/closing sublevels of pupmenus
3162         case RIGHTARROWKEY:
3163                 break;
3164         
3165         case PAD8: case PAD2:
3166         case UPARROWKEY:
3167         case DOWNARROWKEY:
3168                 if(inside || (block->flag & UI_BLOCK_LOOP)) {
3169                         /* arrowkeys: only handle for block_loop blocks */
3170                         event= 0;
3171                         if(block->flag & UI_BLOCK_LOOP) {
3172                                 event= uevent->event;
3173                                 if(event==PAD8) event= UPARROWKEY;
3174                                 if(event==PAD2) event= DOWNARROWKEY;
3175                         }
3176                         else {
3177                                 if(uevent->event==PAD8) event= UPARROWKEY;
3178                                 if(uevent->event==PAD2) event= DOWNARROWKEY;
3179                         }
3180                         if(event && uevent->val) {
3181         
3182                                 for(but= block->buttons.first; but; but= but->next) {
3183                                         but->flag &= ~UI_MOUSE_OVER;
3184                 
3185                                         if(but->flag & UI_ACTIVE) {
3186                                                 but->flag &= ~UI_ACTIVE;
3187                                                 ui_draw_but(but);
3188         
3189                                                 if(event==UPARROWKEY) {
3190                                                         if(block->direction & UI_TOP) bt= ui_but_next(but);
3191                                                         else bt= ui_but_prev(but);
3192                                                 }
3193                                                 else {
3194                                                         if(block->direction & UI_TOP) bt= ui_but_prev(but);
3195                                                         else bt= ui_but_next(but);
3196                                                 }
3197                                                 
3198                                                 if(bt) {
3199                                                         bt->flag |= UI_ACTIVE;
3200                                                         ui_draw_but(bt);
3201                                                         break;
3202                                                 }
3203                                         }
3204                                 }
3205         
3206                                 /* nothing done */
3207                                 if(but==NULL) {
3208                                 
3209                                         if(event==UPARROWKEY) {
3210                                                 if(block->direction & UI_TOP) but= ui_but_first(block);
3211                                                 else but= ui_but_last(block);
3212                                         }
3213                                         else {
3214                                                 if(block->direction & UI_TOP) but= ui_but_last(block);
3215                                                 else but= ui_but_first(block);
3216                                         }
3217                                         if(but) {
3218                                                 but->flag |= UI_ACTIVE;
3219                                                 ui_draw_but(but);
3220                                         }
3221                                 }
3222                                 retval= UI_CONT;
3223                         }
3224                 }
3225                 break;
3226         
3227         case ONEKEY: act= 1;
3228         case TWOKEY: if(act==0) act= 2;
3229         case THREEKEY: if(act==0) act= 3;
3230         case FOURKEY: if(act==0) act= 4;
3231         case FIVEKEY: if(act==0) act= 5;
3232         case SIXKEY: if(act==0) act= 6;
3233         case SEVENKEY: if(act==0) act= 7;
3234         case EIGHTKEY: if(act==0) act= 8;
3235         case NINEKEY: if(act==0) act= 9;
3236         case ZEROKEY: if(act==0) act= 10;
3237         
3238                 if( block->flag & UI_BLOCK_NUMSELECT ) {
3239                         
3240                         if(get_qual() & LR_ALTKEY) act+= 10;
3241                         
3242                         count= 0;
3243                         for(but= block->buttons.first; but; but= but->next) {
3244                                 int doit= 0;
3245                                 
3246                                 if(but->type!=LABEL && but->type!=SEPR) count++;
3247                                 /* exception for menus like layer buts, with button aligning they're not drawn in order */
3248                                 if(but->type==TOGR) {
3249                                         if(but->bitnr==act-1) doit= 1;
3250                                 } else if(count==act) doit=1;
3251                                 
3252                                 if(doit) {
3253                                         but->flag |= UI_ACTIVE;
3254                                         if(uevent->val==1) ui_draw_but(but);
3255                                         else if(block->flag & UI_BLOCK_RET_1) { /* to make UI_BLOCK_RET_1 working */
3256                                                 uevent->event= RETKEY;
3257                                                 uevent->val= 1;                 
3258                                                 //addqueue(block->winq, RIGHTARROWKEY, 1); (why! (ton))
3259                                         }
3260                                         else { 
3261                                                 uevent->event= LEFTMOUSE;       /* to make sure the button is handled further on */
3262                                                 uevent->val= 1;                 
3263                                         }
3264                                 }
3265                                 else if(but->flag & UI_ACTIVE) {
3266                                         but->flag &= ~UI_ACTIVE;
3267                                         ui_draw_but(but);
3268                                 }