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