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