Merging r39634 through r39716 from trunk into soc-2011-tomato
[blender-staging.git] / source / blender / editors / interface / interface_draw.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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * Contributor(s): Blender Foundation
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/editors/interface/interface_draw.c
29  *  \ingroup edinterface
30  */
31
32
33 #include <math.h>
34 #include <string.h>
35
36 #include "DNA_color_types.h"
37 #include "DNA_object_types.h"
38 #include "DNA_screen_types.h"
39 #include "DNA_movieclip_types.h"
40
41 #include "BLI_math.h"
42 #include "BLI_rect.h"
43 #include "BLI_utildefines.h"
44
45 #include "BKE_colortools.h"
46 #include "BKE_texture.h"
47
48
49 #include "IMB_imbuf.h"
50 #include "IMB_imbuf_types.h"
51
52 #include "BIF_gl.h"
53 #include "BIF_glutil.h"
54
55 #include "BLF_api.h"
56
57 #include "UI_interface.h"
58
59 /* own include */
60 #include "interface_intern.h"
61
62 #define UI_RB_ALPHA 16
63 #define UI_DISABLED_ALPHA_OFFS  -160
64
65 static int roundboxtype= 15;
66
67 void uiSetRoundBox(int type)
68 {
69         /* Not sure the roundbox function is the best place to change this
70          * if this is undone, its not that big a deal, only makes curves edges
71          * square for the  */
72         roundboxtype= type;
73
74         /* flags to set which corners will become rounded:
75
76         1------2
77         |      |
78         8------4
79         */
80         
81 }
82
83 int uiGetRoundBox(void)
84 {
85         return roundboxtype;
86 }
87
88 void uiDrawBox(int mode, float minx, float miny, float maxx, float maxy, float rad)
89 {
90         float vec[7][2]= {{0.195, 0.02}, {0.383, 0.067}, {0.55, 0.169}, {0.707, 0.293},
91                                           {0.831, 0.45}, {0.924, 0.617}, {0.98, 0.805}};
92         int a;
93         
94         /* mult */
95         for(a=0; a<7; a++) {
96                 vec[a][0]*= rad; vec[a][1]*= rad;
97         }
98
99         glBegin(mode);
100
101         /* start with corner right-bottom */
102         if(roundboxtype & 4) {
103                 glVertex2f(maxx-rad, miny);
104                 for(a=0; a<7; a++) {
105                         glVertex2f(maxx-rad+vec[a][0], miny+vec[a][1]);
106                 }
107                 glVertex2f(maxx, miny+rad);
108         }
109         else glVertex2f(maxx, miny);
110         
111         /* corner right-top */
112         if(roundboxtype & 2) {
113                 glVertex2f(maxx, maxy-rad);
114                 for(a=0; a<7; a++) {
115                         glVertex2f(maxx-vec[a][1], maxy-rad+vec[a][0]);
116                 }
117                 glVertex2f(maxx-rad, maxy);
118         }
119         else glVertex2f(maxx, maxy);
120         
121         /* corner left-top */
122         if(roundboxtype & 1) {
123                 glVertex2f(minx+rad, maxy);
124                 for(a=0; a<7; a++) {
125                         glVertex2f(minx+rad-vec[a][0], maxy-vec[a][1]);
126                 }
127                 glVertex2f(minx, maxy-rad);
128         }
129         else glVertex2f(minx, maxy);
130         
131         /* corner left-bottom */
132         if(roundboxtype & 8) {
133                 glVertex2f(minx, miny+rad);
134                 for(a=0; a<7; a++) {
135                         glVertex2f(minx+vec[a][1], miny+rad-vec[a][0]);
136                 }
137                 glVertex2f(minx+rad, miny);
138         }
139         else glVertex2f(minx, miny);
140         
141         glEnd();
142 }
143
144 static void round_box_shade_col(const float col1[3], float const col2[3], const float fac)
145 {
146         float col[3];
147
148         col[0]= (fac*col1[0] + (1.0f-fac)*col2[0]);
149         col[1]= (fac*col1[1] + (1.0f-fac)*col2[1]);
150         col[2]= (fac*col1[2] + (1.0f-fac)*col2[2]);
151         glColor3fv(col);
152 }
153
154 /* linear horizontal shade within button or in outline */
155 /* view2d scrollers use it */
156 void uiDrawBoxShade(int mode, float minx, float miny, float maxx, float maxy, float rad, float shadetop, float shadedown)
157 {
158         float vec[7][2]= {{0.195, 0.02}, {0.383, 0.067}, {0.55, 0.169}, {0.707, 0.293},
159                                           {0.831, 0.45}, {0.924, 0.617}, {0.98, 0.805}};
160         const float div= maxy - miny;
161         const float idiv= 1.0f / div;
162         float coltop[3], coldown[3], color[4];
163         int a;
164         
165         /* mult */
166         for(a=0; a<7; a++) {
167                 vec[a][0]*= rad; vec[a][1]*= rad;
168         }
169         /* get current color, needs to be outside of glBegin/End */
170         glGetFloatv(GL_CURRENT_COLOR, color);
171
172         /* 'shade' defines strength of shading */       
173         coltop[0]= color[0]+shadetop; if(coltop[0]>1.0f) coltop[0]= 1.0f;
174         coltop[1]= color[1]+shadetop; if(coltop[1]>1.0f) coltop[1]= 1.0f;
175         coltop[2]= color[2]+shadetop; if(coltop[2]>1.0f) coltop[2]= 1.0f;
176         coldown[0]= color[0]+shadedown; if(coldown[0]<0.0f) coldown[0]= 0.0f;
177         coldown[1]= color[1]+shadedown; if(coldown[1]<0.0f) coldown[1]= 0.0f;
178         coldown[2]= color[2]+shadedown; if(coldown[2]<0.0f) coldown[2]= 0.0f;
179
180         glShadeModel(GL_SMOOTH);
181         glBegin(mode);
182
183         /* start with corner right-bottom */
184         if(roundboxtype & 4) {
185                 
186                 round_box_shade_col(coltop, coldown, 0.0);
187                 glVertex2f(maxx-rad, miny);
188                 
189                 for(a=0; a<7; a++) {
190                         round_box_shade_col(coltop, coldown, vec[a][1]*idiv);
191                         glVertex2f(maxx-rad+vec[a][0], miny+vec[a][1]);
192                 }
193                 
194                 round_box_shade_col(coltop, coldown, rad*idiv);
195                 glVertex2f(maxx, miny+rad);
196         }
197         else {
198                 round_box_shade_col(coltop, coldown, 0.0);
199                 glVertex2f(maxx, miny);
200         }
201         
202         /* corner right-top */
203         if(roundboxtype & 2) {
204                 
205                 round_box_shade_col(coltop, coldown, (div-rad)*idiv);
206                 glVertex2f(maxx, maxy-rad);
207                 
208                 for(a=0; a<7; a++) {
209                         round_box_shade_col(coltop, coldown, (div-rad+vec[a][1])*idiv);
210                         glVertex2f(maxx-vec[a][1], maxy-rad+vec[a][0]);
211                 }
212                 round_box_shade_col(coltop, coldown, 1.0);
213                 glVertex2f(maxx-rad, maxy);
214         }
215         else {
216                 round_box_shade_col(coltop, coldown, 1.0);
217                 glVertex2f(maxx, maxy);
218         }
219         
220         /* corner left-top */
221         if(roundboxtype & 1) {
222                 
223                 round_box_shade_col(coltop, coldown, 1.0);
224                 glVertex2f(minx+rad, maxy);
225                 
226                 for(a=0; a<7; a++) {
227                         round_box_shade_col(coltop, coldown, (div-vec[a][1])*idiv);
228                         glVertex2f(minx+rad-vec[a][0], maxy-vec[a][1]);
229                 }
230                 
231                 round_box_shade_col(coltop, coldown, (div-rad)*idiv);
232                 glVertex2f(minx, maxy-rad);
233         }
234         else {
235                 round_box_shade_col(coltop, coldown, 1.0);
236                 glVertex2f(minx, maxy);
237         }
238         
239         /* corner left-bottom */
240         if(roundboxtype & 8) {
241                 
242                 round_box_shade_col(coltop, coldown, rad*idiv);
243                 glVertex2f(minx, miny+rad);
244                 
245                 for(a=0; a<7; a++) {
246                         round_box_shade_col(coltop, coldown, (rad-vec[a][1])*idiv);
247                         glVertex2f(minx+vec[a][1], miny+rad-vec[a][0]);
248                 }
249                 
250                 round_box_shade_col(coltop, coldown, 0.0);
251                 glVertex2f(minx+rad, miny);
252         }
253         else {
254                 round_box_shade_col(coltop, coldown, 0.0);
255                 glVertex2f(minx, miny);
256         }
257         
258         glEnd();
259         glShadeModel(GL_FLAT);
260 }
261
262 /* linear vertical shade within button or in outline */
263 /* view2d scrollers use it */
264 void uiDrawBoxVerticalShade(int mode, float minx, float miny, float maxx, float maxy, float rad, float shadeLeft, float shadeRight)
265 {
266         float vec[7][2]= {{0.195, 0.02}, {0.383, 0.067}, {0.55, 0.169}, {0.707, 0.293},
267                                           {0.831, 0.45}, {0.924, 0.617}, {0.98, 0.805}};
268         const float div= maxx - minx;
269         const float idiv= 1.0f / div;
270         float colLeft[3], colRight[3], color[4];
271         int a;
272         
273         /* mult */
274         for(a=0; a<7; a++) {
275                 vec[a][0]*= rad; vec[a][1]*= rad;
276         }
277         /* get current color, needs to be outside of glBegin/End */
278         glGetFloatv(GL_CURRENT_COLOR, color);
279
280         /* 'shade' defines strength of shading */       
281         colLeft[0]= color[0]+shadeLeft; if(colLeft[0]>1.0f) colLeft[0]= 1.0f;
282         colLeft[1]= color[1]+shadeLeft; if(colLeft[1]>1.0f) colLeft[1]= 1.0f;
283         colLeft[2]= color[2]+shadeLeft; if(colLeft[2]>1.0f) colLeft[2]= 1.0f;
284         colRight[0]= color[0]+shadeRight; if(colRight[0]<0.0f) colRight[0]= 0.0f;
285         colRight[1]= color[1]+shadeRight; if(colRight[1]<0.0f) colRight[1]= 0.0f;
286         colRight[2]= color[2]+shadeRight; if(colRight[2]<0.0f) colRight[2]= 0.0f;
287
288         glShadeModel(GL_SMOOTH);
289         glBegin(mode);
290
291         /* start with corner right-bottom */
292         if(roundboxtype & 4) {
293                 round_box_shade_col(colLeft, colRight, 0.0);
294                 glVertex2f(maxx-rad, miny);
295                 
296                 for(a=0; a<7; a++) {
297                         round_box_shade_col(colLeft, colRight, vec[a][0]*idiv);
298                         glVertex2f(maxx-rad+vec[a][0], miny+vec[a][1]);
299                 }
300                 
301                 round_box_shade_col(colLeft, colRight, rad*idiv);
302                 glVertex2f(maxx, miny+rad);
303         }
304         else {
305                 round_box_shade_col(colLeft, colRight, 0.0);
306                 glVertex2f(maxx, miny);
307         }
308         
309         /* corner right-top */
310         if(roundboxtype & 2) {
311                 round_box_shade_col(colLeft, colRight, 0.0);
312                 glVertex2f(maxx, maxy-rad);
313                 
314                 for(a=0; a<7; a++) {
315                         
316                         round_box_shade_col(colLeft, colRight, (div-rad-vec[a][0])*idiv);
317                         glVertex2f(maxx-vec[a][1], maxy-rad+vec[a][0]);
318                 }
319                 round_box_shade_col(colLeft, colRight, (div-rad)*idiv);
320                 glVertex2f(maxx-rad, maxy);
321         }
322         else {
323                 round_box_shade_col(colLeft, colRight, 0.0);
324                 glVertex2f(maxx, maxy);
325         }
326         
327         /* corner left-top */
328         if(roundboxtype & 1) {
329                 round_box_shade_col(colLeft, colRight, (div-rad)*idiv);
330                 glVertex2f(minx+rad, maxy);
331                 
332                 for(a=0; a<7; a++) {
333                         round_box_shade_col(colLeft, colRight, (div-rad+vec[a][0])*idiv);
334                         glVertex2f(minx+rad-vec[a][0], maxy-vec[a][1]);
335                 }
336                 
337                 round_box_shade_col(colLeft, colRight, 1.0);
338                 glVertex2f(minx, maxy-rad);
339         }
340         else {
341                 round_box_shade_col(colLeft, colRight, 1.0);
342                 glVertex2f(minx, maxy);
343         }
344         
345         /* corner left-bottom */
346         if(roundboxtype & 8) {
347                 round_box_shade_col(colLeft, colRight, 1.0);
348                 glVertex2f(minx, miny+rad);
349                 
350                 for(a=0; a<7; a++) {
351                         round_box_shade_col(colLeft, colRight, (vec[a][0])*idiv);
352                         glVertex2f(minx+vec[a][1], miny+rad-vec[a][0]);
353                 }
354                 
355                 round_box_shade_col(colLeft, colRight, 1.0);
356                 glVertex2f(minx+rad, miny);
357         }
358         else {
359                 round_box_shade_col(colLeft, colRight, 1.0);
360                 glVertex2f(minx, miny);
361         }
362         
363         glEnd();
364         glShadeModel(GL_FLAT);
365 }
366
367 /* plain antialiased unfilled rectangle */
368 void uiRoundRect(float minx, float miny, float maxx, float maxy, float rad)
369 {
370         float color[4];
371         
372         if(roundboxtype & UI_RB_ALPHA) {
373                 glGetFloatv(GL_CURRENT_COLOR, color);
374                 color[3]= 0.5;
375                 glColor4fv(color);
376                 glEnable( GL_BLEND );
377         }
378         
379         /* set antialias line */
380         glEnable( GL_LINE_SMOOTH );
381         glEnable( GL_BLEND );
382
383         uiDrawBox(GL_LINE_LOOP, minx, miny, maxx, maxy, rad);
384    
385         glDisable( GL_BLEND );
386         glDisable( GL_LINE_SMOOTH );
387 }
388
389 /* plain fake antialiased unfilled round rectangle */
390 #if 0 /* UNUSED 2.5 */
391 static void uiRoundRectFakeAA(float minx, float miny, float maxx, float maxy, float rad, float asp)
392 {
393         float color[4], alpha;
394         float raddiff;
395         int i, passes=4;
396         
397         /* get the color and divide up the alpha */
398         glGetFloatv(GL_CURRENT_COLOR, color);
399         alpha = 1; //color[3];
400         color[3]= 0.5*alpha/(float)passes;
401         glColor4fv(color);
402         
403         /* set the 'jitter amount' */
404         raddiff = (1/(float)passes) * asp;
405         
406         glEnable( GL_BLEND );
407         
408         /* draw lots of lines on top of each other */
409         for (i=passes; i>=(-passes); i--) {
410                 uiDrawBox(GL_LINE_LOOP, minx, miny, maxx, maxy, rad+(i*raddiff));
411         }
412         
413         glDisable( GL_BLEND );
414         
415         color[3] = alpha;
416         glColor4fv(color);
417 }
418 #endif
419
420 /* (old, used in outliner) plain antialiased filled box */
421 void uiRoundBox(float minx, float miny, float maxx, float maxy, float rad)
422 {
423         float color[4];
424         
425         if(roundboxtype & UI_RB_ALPHA) {
426                 glGetFloatv(GL_CURRENT_COLOR, color);
427                 color[3]= 0.5;
428                 glColor4fv(color);
429                 glEnable( GL_BLEND );
430         }
431         
432         /* solid part */
433         uiDrawBox(GL_POLYGON, minx, miny, maxx, maxy, rad);
434         
435         /* set antialias line */
436         glEnable( GL_LINE_SMOOTH );
437         glEnable( GL_BLEND );
438         
439         uiDrawBox(GL_LINE_LOOP, minx, miny, maxx, maxy, rad);
440         
441         glDisable( GL_BLEND );
442         glDisable( GL_LINE_SMOOTH );
443 }
444
445
446 /* ************** generic embossed rect, for window sliders etc ************* */
447
448
449 /* text_draw.c uses this */
450 void uiEmboss(float x1, float y1, float x2, float y2, int sel)
451 {
452         
453         /* below */
454         if(sel) glColor3ub(200,200,200);
455         else glColor3ub(50,50,50);
456         fdrawline(x1, y1, x2, y1);
457
458         /* right */
459         fdrawline(x2, y1, x2, y2);
460         
461         /* top */
462         if(sel) glColor3ub(50,50,50);
463         else glColor3ub(200,200,200);
464         fdrawline(x1, y2, x2, y2);
465
466         /* left */
467         fdrawline(x1, y1, x1, y2);
468         
469 }
470
471 /* ************** SPECIAL BUTTON DRAWING FUNCTIONS ************* */
472
473 void ui_draw_but_IMAGE(ARegion *UNUSED(ar), uiBut *UNUSED(but), uiWidgetColors *UNUSED(wcol), rcti *rect)
474 {
475 #ifdef WITH_HEADLESS
476         (void)rect;
477 #else
478         extern char datatoc_splash_png[];
479         extern int datatoc_splash_png_size;
480         ImBuf *ibuf;
481         //GLint scissor[4];
482         //int w, h;
483         
484         /* hardcoded to splash, loading and freeing every draw, eek! */
485         ibuf= IMB_ibImageFromMemory((unsigned char*)datatoc_splash_png, datatoc_splash_png_size, IB_rect);
486
487         if (!ibuf) return;
488         
489         /* scissor doesn't seem to be doing the right thing...?
490         //glColor4f(1.0, 0.f, 0.f, 1.f);
491         //fdrawbox(rect->xmin, rect->ymin, rect->xmax, rect->ymax)
492
493         w = (rect->xmax - rect->xmin);
494         h = (rect->ymax - rect->ymin);
495         // prevent drawing outside widget area
496         glGetIntegerv(GL_SCISSOR_BOX, scissor);
497         glScissor(ar->winrct.xmin + rect->xmin, ar->winrct.ymin + rect->ymin, w, h);
498         */
499         
500         glEnable(GL_BLEND);
501         glColor4f(0.0, 0.0, 0.0, 0.0);
502         
503         glaDrawPixelsSafe((float)rect->xmin, (float)rect->ymin, ibuf->x, ibuf->y, ibuf->x, GL_RGBA, GL_UNSIGNED_BYTE, ibuf->rect);
504         //glaDrawPixelsTex((float)rect->xmin, (float)rect->ymin, ibuf->x, ibuf->y, GL_UNSIGNED_BYTE, ibuf->rect);
505         
506         glDisable(GL_BLEND);
507         
508         /* 
509         // restore scissortest
510         glScissor(scissor[0], scissor[1], scissor[2], scissor[3]);
511         */
512         
513         IMB_freeImBuf(ibuf);
514 #endif
515 }
516
517 #if 0
518 #ifdef INTERNATIONAL
519 static void ui_draw_but_CHARTAB(uiBut *but)
520 {
521         /* XXX 2.50 bad global access */
522         /* Some local variables */
523         float sx, sy, ex, ey;
524         float width, height;
525         float butw, buth;
526         int x, y, cs;
527         wchar_t wstr[2];
528         unsigned char ustr[16];
529         PackedFile *pf;
530         int result = 0;
531         int charmax = G.charmax;
532         
533         /* FO_BUILTIN_NAME font in use. There are TTF FO_BUILTIN_NAME and non-TTF FO_BUILTIN_NAME fonts */
534         if(!strcmp(G.selfont->name, FO_BUILTIN_NAME)) {
535                 if(G.ui_international == TRUE) {
536                         charmax = 0xff;
537                 }
538                 else {
539                         charmax = 0xff;
540                 }
541         }
542
543         /* Category list exited without selecting the area */
544         if(G.charmax == 0)
545                 charmax = G.charmax = 0xffff;
546
547         /* Calculate the size of the button */
548         width = abs(rect->xmax - rect->xmin);
549         height = abs(rect->ymax - rect->ymin);
550         
551         butw = floor(width / 12);
552         buth = floor(height / 6);
553         
554         /* Initialize variables */
555         sx = rect->xmin;
556         ex = rect->xmin + butw;
557         sy = rect->ymin + height - buth;
558         ey = rect->ymin + height;
559
560         cs = G.charstart;
561
562         /* Set the font, in case it is not FO_BUILTIN_NAME font */
563         if(G.selfont && strcmp(G.selfont->name, FO_BUILTIN_NAME)) {
564                 // Is the font file packed, if so then use the packed file
565                 if(G.selfont->packedfile) {
566                         pf = G.selfont->packedfile;             
567                         FTF_SetFont(pf->data, pf->size, 14.0);
568                 }
569                 else {
570                         char tmpStr[256];
571                         int err;
572
573                         BLI_strncpy(tmpStr, G.selfont->name, sizeof(tmpStr));
574                         BLI_path_abs(tmpStr, G.main->name);
575                         err = FTF_SetFont((unsigned char *)tmpStr, 0, 14.0);
576                 }
577         }
578         else {
579                 if(G.ui_international == TRUE) {
580                         FTF_SetFont((unsigned char *) datatoc_bfont_ttf, datatoc_bfont_ttf_size, 14.0);
581                 }
582         }
583
584         /* Start drawing the button itself */
585         glShadeModel(GL_SMOOTH);
586
587         glColor3ub(200,  200,  200);
588         glRectf((rect->xmin), (rect->ymin), (rect->xmax), (rect->ymax));
589
590         glColor3ub(0,  0,  0);
591         for(y = 0; y < 6; y++) {
592                 // Do not draw more than the category allows
593                 if(cs > charmax) break;
594
595                 for(x = 0; x < 12; x++)
596                 {
597                         // Do not draw more than the category allows
598                         if(cs > charmax) break;
599
600                         // Draw one grid cell
601                         glBegin(GL_LINE_LOOP);
602                                 glVertex2f(sx, sy);
603                                 glVertex2f(ex, sy);
604                                 glVertex2f(ex, ey);
605                                 glVertex2f(sx, ey);                             
606                         glEnd();        
607
608                         // Draw character inside the cell
609                         memset(wstr, 0, sizeof(wchar_t)*2);
610                         memset(ustr, 0, 16);
611
612                         // Set the font to be either unicode or FO_BUILTIN_NAME 
613                         wstr[0] = cs;
614                         if(strcmp(G.selfont->name, FO_BUILTIN_NAME))
615                         {
616                                 wcs2utf8s((char *)ustr, (wchar_t *)wstr);
617                         }
618                         else
619                         {
620                                 if(G.ui_international == TRUE)
621                                 {
622                                         wcs2utf8s((char *)ustr, (wchar_t *)wstr);
623                                 }
624                                 else
625                                 {
626                                         ustr[0] = cs;
627                                         ustr[1] = 0;
628                                 }
629                         }
630
631                         if((G.selfont && strcmp(G.selfont->name, FO_BUILTIN_NAME)) || (G.selfont && !strcmp(G.selfont->name, FO_BUILTIN_NAME) && G.ui_international == TRUE))
632                         {
633                                 float wid;
634                                 float llx, lly, llz, urx, ury, urz;
635                                 float dx, dy;
636                                 float px, py;
637         
638                                 // Calculate the position
639                                 wid = FTF_GetStringWidth((char *) ustr, FTF_USE_GETTEXT | FTF_INPUT_UTF8);
640                                 FTF_GetBoundingBox((char *) ustr, &llx,&lly,&llz,&urx,&ury,&urz, FTF_USE_GETTEXT | FTF_INPUT_UTF8);
641                                 dx = urx-llx;
642                                 dy = ury-lly;
643
644                                 // This isn't fully functional since the but->aspect isn't working like I suspected
645                                 px = sx + ((butw/but->aspect)-dx)/2;
646                                 py = sy + ((buth/but->aspect)-dy)/2;
647
648                                 // Set the position and draw the character
649                                 ui_rasterpos_safe(px, py, but->aspect);
650                                 FTF_DrawString((char *) ustr, FTF_USE_GETTEXT | FTF_INPUT_UTF8);
651                         }
652                         else
653                         {
654                                 ui_rasterpos_safe(sx + butw/2, sy + buth/2, but->aspect);
655                                 UI_DrawString(but->font, (char *) ustr, 0);
656                         }
657         
658                         // Calculate the next position and character
659                         sx += butw; ex +=butw;
660                         cs++;
661                 }
662                 /* Add the y position and reset x position */
663                 sy -= buth; 
664                 ey -= buth;
665                 sx = rect->xmin;
666                 ex = rect->xmin + butw;
667         }       
668         glShadeModel(GL_FLAT);
669
670         /* Return Font Settings to original */
671         if(U.fontsize && U.fontname[0]) {
672                 result = FTF_SetFont((unsigned char *)U.fontname, 0, U.fontsize);
673         }
674         else if (U.fontsize) {
675                 result = FTF_SetFont((unsigned char *) datatoc_bfont_ttf, datatoc_bfont_ttf_size, U.fontsize);
676         }
677
678         if (result == 0) {
679                 result = FTF_SetFont((unsigned char *) datatoc_bfont_ttf, datatoc_bfont_ttf_size, 11);
680         }
681         
682         /* resets the font size */
683         if(G.ui_international == TRUE) {
684                 // uiSetCurFont(but->block, UI_HELV);
685         }
686 }
687
688 #endif // INTERNATIONAL
689 #endif
690
691 static void draw_scope_end(rctf *rect, GLint *scissor)
692 {
693         float scaler_x1, scaler_x2;
694         
695         /* restore scissortest */
696         glScissor(scissor[0], scissor[1], scissor[2], scissor[3]);
697         
698         glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
699         
700         /* scale widget */
701         scaler_x1 = rect->xmin + (rect->xmax - rect->xmin)/2 - SCOPE_RESIZE_PAD;
702         scaler_x2 = rect->xmin + (rect->xmax - rect->xmin)/2 + SCOPE_RESIZE_PAD;
703         
704         glColor4f(0.f, 0.f, 0.f, 0.25f);
705         fdrawline(scaler_x1, rect->ymin-4, scaler_x2, rect->ymin-4);
706         fdrawline(scaler_x1, rect->ymin-7, scaler_x2, rect->ymin-7);
707         glColor4f(1.f, 1.f, 1.f, 0.25f);
708         fdrawline(scaler_x1, rect->ymin-5, scaler_x2, rect->ymin-5);
709         fdrawline(scaler_x1, rect->ymin-8, scaler_x2, rect->ymin-8);
710         
711         /* outline */
712         glColor4f(0.f, 0.f, 0.f, 0.5f);
713         uiSetRoundBox(15);
714         uiDrawBox(GL_LINE_LOOP, rect->xmin-1, rect->ymin-1, rect->xmax+1, rect->ymax+1, 3.0f);
715 }
716
717 static void histogram_draw_one(float r, float g, float b, float alpha, float x, float y, float w, float h, float *data, int res)
718 {
719         int i;
720         
721         /* under the curve */
722         glBlendFunc(GL_SRC_ALPHA, GL_ONE);
723         glColor4f(r, g, b, alpha);
724         
725         glShadeModel(GL_FLAT);
726         glBegin(GL_QUAD_STRIP);
727         glVertex2f(x, y);
728         glVertex2f(x, y + (data[0]*h));
729         for (i=1; i < res; i++) {
730                 float x2 = x + i * (w/(float)res);
731                 glVertex2f(x2, y + (data[i]*h));
732                 glVertex2f(x2, y);
733         }
734         glEnd();
735         
736         /* curve outline */
737         glColor4f(0.f, 0.f, 0.f, 0.25f);
738         
739         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
740         glEnable(GL_LINE_SMOOTH);
741         glBegin(GL_LINE_STRIP);
742         for (i=0; i < res; i++) {
743                 float x2 = x + i * (w/(float)res);
744                 glVertex2f(x2, y + (data[i]*h));
745         }
746         glEnd();
747         glDisable(GL_LINE_SMOOTH);
748 }
749
750 void ui_draw_but_HISTOGRAM(ARegion *ar, uiBut *but, uiWidgetColors *UNUSED(wcol), rcti *recti)
751 {
752         Histogram *hist = (Histogram *)but->poin;
753         int res = hist->x_resolution;
754         rctf rect;
755         int i;
756         float w, h;
757         //float alpha;
758         GLint scissor[4];
759         
760         rect.xmin = (float)recti->xmin+1;
761         rect.xmax = (float)recti->xmax-1;
762         rect.ymin = (float)recti->ymin+SCOPE_RESIZE_PAD+2;
763         rect.ymax = (float)recti->ymax-1;
764         
765         w = rect.xmax - rect.xmin;
766         h = (rect.ymax - rect.ymin) * hist->ymax;
767         
768         glEnable(GL_BLEND);
769         glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
770         
771         glColor4f(0.f, 0.f, 0.f, 0.3f);
772         uiSetRoundBox(15);
773         uiDrawBox(GL_POLYGON, rect.xmin-1, rect.ymin-1, rect.xmax+1, rect.ymax+1, 3.0f);
774         
775         glColor4f(1.f, 1.f, 1.f, 0.08f);
776         /* draw grid lines here */
777         for (i=1; i<4; i++) {
778                 fdrawline(rect.xmin, rect.ymin+(i/4.f)*h, rect.xmax, rect.ymin+(i/4.f)*h);
779                 fdrawline(rect.xmin+(i/4.f)*w, rect.ymin, rect.xmin+(i/4.f)*w, rect.ymax);
780         }
781         
782         /* need scissor test, histogram can draw outside of boundary */
783         glGetIntegerv(GL_VIEWPORT, scissor);
784         glScissor(ar->winrct.xmin + (rect.xmin-1), ar->winrct.ymin+(rect.ymin-1), (rect.xmax+1)-(rect.xmin-1), (rect.ymax+1)-(rect.ymin-1));
785         
786         if (hist->mode == HISTO_MODE_LUMA)
787                 histogram_draw_one(1.0, 1.0, 1.0, 0.75, rect.xmin, rect.ymin, w, h, hist->data_luma, res);
788         else {
789                 if (hist->mode == HISTO_MODE_RGB || hist->mode == HISTO_MODE_R)
790                         histogram_draw_one(1.0, 0.0, 0.0, 0.75, rect.xmin, rect.ymin, w, h, hist->data_r, res);
791                 if (hist->mode == HISTO_MODE_RGB || hist->mode == HISTO_MODE_G)
792                         histogram_draw_one(0.0, 1.0, 0.0, 0.75, rect.xmin, rect.ymin, w, h, hist->data_g, res);
793                 if (hist->mode == HISTO_MODE_RGB || hist->mode == HISTO_MODE_B)
794                         histogram_draw_one(0.0, 0.0, 1.0, 0.75, rect.xmin, rect.ymin, w, h, hist->data_b, res);
795         }
796         
797         /* outline, scale gripper */
798         draw_scope_end(&rect, scissor);
799 }
800
801 void ui_draw_but_WAVEFORM(ARegion *ar, uiBut *but, uiWidgetColors *UNUSED(wcol), rcti *recti)
802 {
803         Scopes *scopes = (Scopes *)but->poin;
804         rctf rect;
805         int i, c;
806         float w, w3, h, alpha, yofs;
807         GLint scissor[4];
808         float colors[3][3]= MAT3_UNITY;
809         float colorsycc[3][3] = {{1,0,1},{1,1,0},{0,1,1}};
810         float colors_alpha[3][3], colorsycc_alpha[3][3]; /* colors  pre multiplied by alpha for speed up */
811         float min, max;
812         
813         if (scopes==NULL) return;
814         
815         rect.xmin = (float)recti->xmin+1;
816         rect.xmax = (float)recti->xmax-1;
817         rect.ymin = (float)recti->ymin+SCOPE_RESIZE_PAD+2;
818         rect.ymax = (float)recti->ymax-1;
819         
820         if (scopes->wavefrm_yfac < 0.5f )
821                 scopes->wavefrm_yfac =0.98f;
822         w = rect.xmax - rect.xmin-7;
823         h = (rect.ymax - rect.ymin)*scopes->wavefrm_yfac;
824         yofs= rect.ymin + (rect.ymax - rect.ymin -h)/2.0f;
825         w3=w/3.0f;
826         
827         /* log scale for alpha */
828         alpha = scopes->wavefrm_alpha*scopes->wavefrm_alpha;
829         
830         for(c=0; c<3; c++) {
831                 for(i=0; i<3; i++) {
832                         colors_alpha[c][i] = colors[c][i] * alpha;
833                         colorsycc_alpha[c][i] = colorsycc[c][i] * alpha;
834                 }
835         }
836                         
837         glEnable(GL_BLEND);
838         glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
839         
840         glColor4f(0.f, 0.f, 0.f, 0.3f);
841         uiSetRoundBox(15);
842         uiDrawBox(GL_POLYGON, rect.xmin-1, rect.ymin-1, rect.xmax+1, rect.ymax+1, 3.0f);
843         
844
845         /* need scissor test, waveform can draw outside of boundary */
846         glGetIntegerv(GL_VIEWPORT, scissor);
847         glScissor(ar->winrct.xmin + (rect.xmin-1), ar->winrct.ymin+(rect.ymin-1), (rect.xmax+1)-(rect.xmin-1), (rect.ymax+1)-(rect.ymin-1));
848
849         glColor4f(1.f, 1.f, 1.f, 0.08f);
850         /* draw grid lines here */
851         for (i=0; i<6; i++) {
852                 char str[4];
853                 sprintf(str,"%-3d",i*20);
854                 str[3]='\0';
855                 fdrawline(rect.xmin+22, yofs+(i/5.f)*h, rect.xmax+1, yofs+(i/5.f)*h);
856                 BLF_draw_default(rect.xmin+1, yofs-5+(i/5.f)*h, 0, str, sizeof(str)-1);
857                 /* in the loop because blf_draw reset it */
858                 glEnable(GL_BLEND);
859                 glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
860         }
861         /* 3 vertical separation */
862         if (scopes->wavefrm_mode!= SCOPES_WAVEFRM_LUMA) {
863                 for (i=1; i<3; i++) {
864                         fdrawline(rect.xmin+i*w3, rect.ymin, rect.xmin+i*w3, rect.ymax);
865                 }
866         }
867         
868         /* separate min max zone on the right */
869         fdrawline(rect.xmin+w, rect.ymin, rect.xmin+w, rect.ymax);
870         /* 16-235-240 level in case of ITU-R BT601/709 */
871         glColor4f(1.f, 0.4f, 0.f, 0.2f);
872         if (ELEM(scopes->wavefrm_mode, SCOPES_WAVEFRM_YCC_601, SCOPES_WAVEFRM_YCC_709)){
873                 fdrawline(rect.xmin+22, yofs+h*16.0f/255.0f, rect.xmax+1, yofs+h*16.0f/255.0f);
874                 fdrawline(rect.xmin+22, yofs+h*235.0f/255.0f, rect.xmin+w3, yofs+h*235.0f/255.0f);
875                 fdrawline(rect.xmin+3*w3, yofs+h*235.0f/255.0f, rect.xmax+1, yofs+h*235.0f/255.0f);
876                 fdrawline(rect.xmin+w3, yofs+h*240.0f/255.0f, rect.xmax+1, yofs+h*240.0f/255.0f);
877         }
878         /* 7.5 IRE black point level for NTSC */
879         if (scopes->wavefrm_mode== SCOPES_WAVEFRM_LUMA)
880                 fdrawline(rect.xmin, yofs+h*0.075f, rect.xmax+1, yofs+h*0.075f);
881
882         if (scopes->ok && scopes->waveform_1 != NULL) {
883                 
884                 /* LUMA (1 channel) */
885                 glBlendFunc(GL_ONE,GL_ONE);
886                 glColor3f(alpha, alpha, alpha);
887                 if (scopes->wavefrm_mode == SCOPES_WAVEFRM_LUMA){
888
889                         glBlendFunc(GL_ONE,GL_ONE);
890                         
891                         glPushMatrix();
892                         glEnableClientState(GL_VERTEX_ARRAY);
893                         
894                         glTranslatef(rect.xmin, yofs, 0.f);
895                         glScalef(w, h, 0.f);
896                         glVertexPointer(2, GL_FLOAT, 0, scopes->waveform_1);
897                         glDrawArrays(GL_POINTS, 0, scopes->waveform_tot);
898                                         
899                         glDisableClientState(GL_VERTEX_ARRAY);
900                         glPopMatrix();
901
902                         /* min max */
903                         glColor3f(.5f, .5f, .5f);
904                         min= yofs+scopes->minmax[0][0]*h;
905                         max= yofs+scopes->minmax[0][1]*h;
906                         CLAMP(min, rect.ymin, rect.ymax);
907                         CLAMP(max, rect.ymin, rect.ymax);
908                         fdrawline(rect.xmax-3,min,rect.xmax-3,max);
909                 }
910
911                 /* RGB / YCC (3 channels) */
912                 else if (ELEM4(scopes->wavefrm_mode, SCOPES_WAVEFRM_RGB, SCOPES_WAVEFRM_YCC_601, SCOPES_WAVEFRM_YCC_709, SCOPES_WAVEFRM_YCC_JPEG)) {
913                         int rgb = (scopes->wavefrm_mode == SCOPES_WAVEFRM_RGB);
914                         
915                         glBlendFunc(GL_ONE,GL_ONE);
916                         
917                         glPushMatrix();
918                         glEnableClientState(GL_VERTEX_ARRAY);
919                         
920                         glTranslatef(rect.xmin, yofs, 0.f);
921                         glScalef(w3, h, 0.f);
922                         
923                         glColor3fv((rgb)?colors_alpha[0]:colorsycc_alpha[0]);
924                         glVertexPointer(2, GL_FLOAT, 0, scopes->waveform_1);
925                         glDrawArrays(GL_POINTS, 0, scopes->waveform_tot);
926
927                         glTranslatef(1.f, 0.f, 0.f);
928                         glColor3fv((rgb)?colors_alpha[1]:colorsycc_alpha[1]);
929                         glVertexPointer(2, GL_FLOAT, 0, scopes->waveform_2);
930                         glDrawArrays(GL_POINTS, 0, scopes->waveform_tot);
931                         
932                         glTranslatef(1.f, 0.f, 0.f);
933                         glColor3fv((rgb)?colors_alpha[2]:colorsycc_alpha[2]);
934                         glVertexPointer(2, GL_FLOAT, 0, scopes->waveform_3);
935                         glDrawArrays(GL_POINTS, 0, scopes->waveform_tot);
936                         
937                         glDisableClientState(GL_VERTEX_ARRAY);
938                         glPopMatrix();
939
940                         
941                         /* min max */
942                         for (c=0; c<3; c++) {
943                                 if (scopes->wavefrm_mode == SCOPES_WAVEFRM_RGB)
944                                         glColor3f(colors[c][0]*0.75f, colors[c][1]*0.75f, colors[c][2]*0.75f);
945                                 else
946                                         glColor3f(colorsycc[c][0]*0.75f, colorsycc[c][1]*0.75f, colorsycc[c][2]*0.75f);
947                                 min= yofs+scopes->minmax[c][0]*h;
948                                 max= yofs+scopes->minmax[c][1]*h;
949                                 CLAMP(min, rect.ymin, rect.ymax);
950                                 CLAMP(max, rect.ymin, rect.ymax);
951                                 fdrawline(rect.xmin+w+2+c*2,min,rect.xmin+w+2+c*2,max);
952                         }
953                 }
954                 
955         }
956         
957         /* outline, scale gripper */
958         draw_scope_end(&rect, scissor);
959 }
960
961 static float polar_to_x(float center, float diam, float ampli, float angle)
962 {
963         return center + diam * ampli * cosf(angle);
964 }
965
966 static float polar_to_y(float center, float diam, float ampli, float angle)
967 {
968         return center + diam * ampli * sinf(angle);
969 }
970
971 static void vectorscope_draw_target(float centerx, float centery, float diam, float r, float g, float b)
972 {
973         float y,u,v;
974         float tangle=0.f, tampli;
975         float dangle, dampli, dangle2, dampli2;
976
977         rgb_to_yuv(r,g,b, &y, &u, &v);
978         if (u>0 && v>=0) tangle=atanf(v/u);
979         else if (u>0 && v<0) tangle= atanf(v/u) + 2.0f * (float)M_PI;
980         else if (u<0) tangle=atanf(v/u) + (float)M_PI;
981         else if (u==0 && v > 0.0f) tangle= (float)M_PI/2.0f;
982         else if (u==0 && v < 0.0f) tangle=-(float)M_PI/2.0f;
983         tampli= sqrtf(u*u+v*v);
984
985         /* small target vary by 2.5 degree and 2.5 IRE unit */
986         glColor4f(1.0f, 1.0f, 1.0, 0.12f);
987         dangle= 2.5f*(float)M_PI/180.0f;
988         dampli= 2.5f/200.0f;
989         glBegin(GL_LINE_STRIP);
990         glVertex2f(polar_to_x(centerx,diam,tampli+dampli,tangle+dangle), polar_to_y(centery,diam,tampli+dampli,tangle+dangle));
991         glVertex2f(polar_to_x(centerx,diam,tampli-dampli,tangle+dangle), polar_to_y(centery,diam,tampli-dampli,tangle+dangle));
992         glVertex2f(polar_to_x(centerx,diam,tampli-dampli,tangle-dangle), polar_to_y(centery,diam,tampli-dampli,tangle-dangle));
993         glVertex2f(polar_to_x(centerx,diam,tampli+dampli,tangle-dangle), polar_to_y(centery,diam,tampli+dampli,tangle-dangle));
994         glVertex2f(polar_to_x(centerx,diam,tampli+dampli,tangle+dangle), polar_to_y(centery,diam,tampli+dampli,tangle+dangle));
995         glEnd();
996         /* big target vary by 10 degree and 20% amplitude */
997         glColor4f(1.0f, 1.0f, 1.0, 0.12f);
998         dangle= 10.0f*(float)M_PI/180.0f;
999         dampli= 0.2f*tampli;
1000         dangle2= 5.0f*(float)M_PI/180.0f;
1001         dampli2= 0.5f*dampli;
1002         glBegin(GL_LINE_STRIP);
1003         glVertex2f(polar_to_x(centerx,diam,tampli+dampli-dampli2,tangle+dangle), polar_to_y(centery,diam,tampli+dampli-dampli2,tangle+dangle));
1004         glVertex2f(polar_to_x(centerx,diam,tampli+dampli,tangle+dangle), polar_to_y(centery,diam,tampli+dampli,tangle+dangle));
1005         glVertex2f(polar_to_x(centerx,diam,tampli+dampli,tangle+dangle-dangle2), polar_to_y(centery,diam,tampli+dampli,tangle+dangle-dangle2));
1006         glEnd();
1007         glBegin(GL_LINE_STRIP);
1008         glVertex2f(polar_to_x(centerx,diam,tampli-dampli+dampli2,tangle+dangle), polar_to_y(centery ,diam,tampli-dampli+dampli2,tangle+dangle));
1009         glVertex2f(polar_to_x(centerx,diam,tampli-dampli,tangle+dangle), polar_to_y(centery,diam,tampli-dampli,tangle+dangle));
1010         glVertex2f(polar_to_x(centerx,diam,tampli-dampli,tangle+dangle-dangle2), polar_to_y(centery,diam,tampli-dampli,tangle+dangle-dangle2));
1011         glEnd();
1012         glBegin(GL_LINE_STRIP);
1013         glVertex2f(polar_to_x(centerx,diam,tampli-dampli+dampli2,tangle-dangle), polar_to_y(centery,diam,tampli-dampli+dampli2,tangle-dangle));
1014         glVertex2f(polar_to_x(centerx,diam,tampli-dampli,tangle-dangle), polar_to_y(centery,diam,tampli-dampli,tangle-dangle));
1015         glVertex2f(polar_to_x(centerx,diam,tampli-dampli,tangle-dangle+dangle2), polar_to_y(centery,diam,tampli-dampli,tangle-dangle+dangle2));
1016         glEnd();
1017         glBegin(GL_LINE_STRIP);
1018         glVertex2f(polar_to_x(centerx,diam,tampli+dampli-dampli2,tangle-dangle), polar_to_y(centery,diam,tampli+dampli-dampli2,tangle-dangle));
1019         glVertex2f(polar_to_x(centerx,diam,tampli+dampli,tangle-dangle), polar_to_y(centery,diam,tampli+dampli,tangle-dangle));
1020         glVertex2f(polar_to_x(centerx,diam,tampli+dampli,tangle-dangle+dangle2), polar_to_y(centery,diam,tampli+dampli,tangle-dangle+dangle2));
1021         glEnd();
1022 }
1023
1024 void ui_draw_but_VECTORSCOPE(ARegion *ar, uiBut *but, uiWidgetColors *UNUSED(wcol), rcti *recti)
1025 {
1026         Scopes *scopes = (Scopes *)but->poin;
1027         rctf rect;
1028         int i, j;
1029         int skina= 123; /* angle in degree of the skin tone line */
1030         float w, h, centerx, centery, diam;
1031         float alpha;
1032         float colors[6][3]={{.75,0,0},{.75,.75,0},{0,.75,0},{0,.75,.75},{0,0,.75},{.75,0,.75}};
1033         GLint scissor[4];
1034         
1035         rect.xmin = (float)recti->xmin+1;
1036         rect.xmax = (float)recti->xmax-1;
1037         rect.ymin = (float)recti->ymin+SCOPE_RESIZE_PAD+2;
1038         rect.ymax = (float)recti->ymax-1;
1039         
1040         w = rect.xmax - rect.xmin;
1041         h = rect.ymax - rect.ymin;
1042         centerx = rect.xmin + w/2;
1043         centery = rect.ymin + h/2;
1044         diam= (w<h)?w:h;
1045         
1046         alpha = scopes->vecscope_alpha*scopes->vecscope_alpha*scopes->vecscope_alpha;
1047                         
1048         glEnable(GL_BLEND);
1049         glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
1050         
1051         glColor4f(0.f, 0.f, 0.f, 0.3f);
1052         uiSetRoundBox(15);
1053         uiDrawBox(GL_POLYGON, rect.xmin-1, rect.ymin-1, rect.xmax+1, rect.ymax+1, 3.0f);
1054
1055         /* need scissor test, hvectorscope can draw outside of boundary */
1056         glGetIntegerv(GL_VIEWPORT, scissor);
1057         glScissor(ar->winrct.xmin + (rect.xmin-1), ar->winrct.ymin+(rect.ymin-1), (rect.xmax+1)-(rect.xmin-1), (rect.ymax+1)-(rect.ymin-1));
1058         
1059         glColor4f(1.f, 1.f, 1.f, 0.08f);
1060         /* draw grid elements */
1061         /* cross */
1062         fdrawline(centerx - (diam/2)-5, centery, centerx + (diam/2)+5, centery);
1063         fdrawline(centerx, centery - (diam/2)-5, centerx, centery + (diam/2)+5);
1064         /* circles */
1065         for(j=0; j<5; j++) {
1066                 glBegin(GL_LINE_STRIP);
1067                 for(i=0; i<=360; i=i+15) {
1068                         float a= i*M_PI/180.0;
1069                         float r= (j+1)/10.0f;
1070                         glVertex2f( polar_to_x(centerx,diam,r,a), polar_to_y(centery,diam,r,a));
1071                 }
1072                 glEnd();
1073         }
1074         /* skin tone line */
1075         glColor4f(1.f, 0.4f, 0.f, 0.2f);
1076         fdrawline(      polar_to_x(centerx, diam, 0.5f, skina*M_PI/180.0), polar_to_y(centery,diam,0.5,skina*M_PI/180.0),
1077                                 polar_to_x(centerx, diam, 0.1f, skina*M_PI/180.0), polar_to_y(centery,diam,0.1,skina*M_PI/180.0));
1078         /* saturation points */
1079         for(i=0; i<6; i++)
1080                 vectorscope_draw_target(centerx, centery, diam, colors[i][0], colors[i][1], colors[i][2]);
1081         
1082         if (scopes->ok && scopes->vecscope != NULL) {
1083                 /* pixel point cloud */
1084                 glBlendFunc(GL_ONE,GL_ONE);
1085                 glColor3f(alpha, alpha, alpha);
1086
1087                 glPushMatrix();
1088                 glEnableClientState(GL_VERTEX_ARRAY);
1089
1090                 glTranslatef(centerx, centery, 0.f);
1091                 glScalef(diam, diam, 0.f);
1092
1093                 glVertexPointer(2, GL_FLOAT, 0, scopes->vecscope);
1094                 glDrawArrays(GL_POINTS, 0, scopes->waveform_tot);
1095                 
1096                 glDisableClientState(GL_VERTEX_ARRAY);
1097                 glPopMatrix();
1098         }
1099
1100         /* outline, scale gripper */
1101         draw_scope_end(&rect, scissor);
1102                 
1103         glDisable(GL_BLEND);
1104 }
1105
1106 void ui_draw_but_COLORBAND(uiBut *but, uiWidgetColors *UNUSED(wcol), rcti *rect)
1107 {
1108         ColorBand *coba;
1109         CBData *cbd;
1110         float x1, y1, sizex, sizey;
1111         float v3[2], v1[2], v2[2], v1a[2], v2a[2];
1112         int a;
1113         float pos, colf[4]= {0,0,0,0}; /* initialize incase the colorband isnt valid */
1114                 
1115         coba= (ColorBand *)(but->editcoba? but->editcoba: but->poin);
1116         if(coba==NULL) return;
1117         
1118         x1= rect->xmin;
1119         y1= rect->ymin;
1120         sizex= rect->xmax-x1;
1121         sizey= rect->ymax-y1;
1122
1123         /* first background, to show tranparency */
1124
1125         glColor4ub(UI_TRANSP_DARK, UI_TRANSP_DARK, UI_TRANSP_DARK, 255);
1126         glRectf(x1, y1, x1+sizex, y1+sizey);
1127         glEnable(GL_POLYGON_STIPPLE);
1128         glColor4ub(UI_TRANSP_LIGHT, UI_TRANSP_LIGHT, UI_TRANSP_LIGHT, 255);
1129         glPolygonStipple(checker_stipple_sml);
1130         glRectf(x1, y1, x1+sizex, y1+sizey);
1131         glDisable(GL_POLYGON_STIPPLE);
1132
1133         glShadeModel(GL_FLAT);
1134         glEnable(GL_BLEND);
1135         
1136         cbd= coba->data;
1137         
1138         v1[0]= v2[0]= x1;
1139         v1[1]= y1;
1140         v2[1]= y1+sizey;
1141         
1142         glBegin(GL_QUAD_STRIP);
1143         
1144         glColor4fv( &cbd->r );
1145         glVertex2fv(v1); glVertex2fv(v2);
1146         
1147         for( a = 1; a <= sizex; a++ ) {
1148                 pos = ((float)a) / (sizex-1);
1149                 do_colorband( coba, pos, colf );
1150                 if (but->block->color_profile != BLI_PR_NONE)
1151                         linearrgb_to_srgb_v3_v3(colf, colf);
1152                 
1153                 v1[0]=v2[0]= x1 + a;
1154                 
1155                 glColor4fv( colf );
1156                 glVertex2fv(v1); glVertex2fv(v2);
1157         }
1158         
1159         glEnd();
1160         glShadeModel(GL_FLAT);
1161         glDisable(GL_BLEND);
1162         
1163         /* outline */
1164         glColor4f(0.0, 0.0, 0.0, 1.0);
1165         fdrawbox(x1, y1, x1+sizex, y1+sizey);
1166         
1167         /* help lines */
1168         v1[0]= v2[0]=v3[0]= x1;
1169         v1[1]= y1;
1170         v1a[1]= y1+0.25f*sizey;
1171         v2[1]= y1+0.5f*sizey;
1172         v2a[1]= y1+0.75f*sizey;
1173         v3[1]= y1+sizey;
1174         
1175         
1176         cbd= coba->data;
1177         glBegin(GL_LINES);
1178         for(a=0; a<coba->tot; a++, cbd++) {
1179                 v1[0]=v2[0]=v3[0]=v1a[0]=v2a[0]= x1+ cbd->pos*sizex;
1180                 
1181                 if(a==coba->cur) {
1182                         glColor3ub(0, 0, 0);
1183                         glVertex2fv(v1);
1184                         glVertex2fv(v3);
1185                         glEnd();
1186                         
1187                         setlinestyle(2);
1188                         glBegin(GL_LINES);
1189                         glColor3ub(255, 255, 255);
1190                         glVertex2fv(v1);
1191                         glVertex2fv(v3);
1192                         glEnd();
1193                         setlinestyle(0);
1194                         glBegin(GL_LINES);
1195                         
1196                         /* glColor3ub(0, 0, 0);
1197                         glVertex2fv(v1);
1198                         glVertex2fv(v1a);
1199                         glColor3ub(255, 255, 255);
1200                         glVertex2fv(v1a);
1201                         glVertex2fv(v2);
1202                         glColor3ub(0, 0, 0);
1203                         glVertex2fv(v2);
1204                         glVertex2fv(v2a);
1205                         glColor3ub(255, 255, 255);
1206                         glVertex2fv(v2a);
1207                         glVertex2fv(v3);
1208                         */
1209                 }
1210                 else {
1211                         glColor3ub(0, 0, 0);
1212                         glVertex2fv(v1);
1213                         glVertex2fv(v2);
1214                         
1215                         glColor3ub(255, 255, 255);
1216                         glVertex2fv(v2);
1217                         glVertex2fv(v3);
1218                 }       
1219         }
1220         glEnd();
1221
1222 }
1223
1224 void ui_draw_but_NORMAL(uiBut *but, uiWidgetColors *wcol, rcti *rect)
1225 {
1226         static GLuint displist=0;
1227         int a, old[8];
1228         GLfloat diff[4], diffn[4]={1.0f, 1.0f, 1.0f, 1.0f};
1229         float vec0[4]={0.0f, 0.0f, 0.0f, 0.0f};
1230         float dir[4], size;
1231         
1232         /* store stuff */
1233         glGetMaterialfv(GL_FRONT, GL_DIFFUSE, diff);
1234                 
1235         /* backdrop */
1236         glColor3ubv((unsigned char*)wcol->inner);
1237         uiSetRoundBox(15);
1238         uiDrawBox(GL_POLYGON, rect->xmin, rect->ymin, rect->xmax, rect->ymax, 5.0f);
1239         
1240         /* sphere color */
1241         glMaterialfv(GL_FRONT, GL_DIFFUSE, diffn);
1242         glCullFace(GL_BACK); glEnable(GL_CULL_FACE);
1243         
1244         /* disable blender light */
1245         for(a=0; a<8; a++) {
1246                 old[a]= glIsEnabled(GL_LIGHT0+a);
1247                 glDisable(GL_LIGHT0+a);
1248         }
1249         
1250         /* own light */
1251         glEnable(GL_LIGHT7);
1252         glEnable(GL_LIGHTING);
1253         
1254         ui_get_but_vectorf(but, dir);
1255
1256         dir[3]= 0.0f;   /* glLight needs 4 args, 0.0 is sun */
1257         glLightfv(GL_LIGHT7, GL_POSITION, dir); 
1258         glLightfv(GL_LIGHT7, GL_DIFFUSE, diffn); 
1259         glLightfv(GL_LIGHT7, GL_SPECULAR, vec0); 
1260         glLightf(GL_LIGHT7, GL_CONSTANT_ATTENUATION, 1.0f);
1261         glLightf(GL_LIGHT7, GL_LINEAR_ATTENUATION, 0.0f);
1262         
1263         /* transform to button */
1264         glPushMatrix();
1265         glTranslatef(rect->xmin + 0.5f*(rect->xmax-rect->xmin), rect->ymin+ 0.5f*(rect->ymax-rect->ymin), 0.0f);
1266         
1267         if( rect->xmax-rect->xmin < rect->ymax-rect->ymin)
1268                 size= (rect->xmax-rect->xmin)/200.f;
1269         else
1270                 size= (rect->ymax-rect->ymin)/200.f;
1271         
1272         glScalef(size, size, size);
1273         
1274         if(displist==0) {
1275                 GLUquadricObj   *qobj;
1276                 
1277                 displist= glGenLists(1);
1278                 glNewList(displist, GL_COMPILE_AND_EXECUTE);
1279                 
1280                 qobj= gluNewQuadric();
1281                 gluQuadricDrawStyle(qobj, GLU_FILL); 
1282                 glShadeModel(GL_SMOOTH);
1283                 gluSphere( qobj, 100.0, 32, 24);
1284                 glShadeModel(GL_FLAT);
1285                 gluDeleteQuadric(qobj);  
1286                 
1287                 glEndList();
1288         }
1289         else glCallList(displist);
1290         
1291         /* restore */
1292         glDisable(GL_LIGHTING);
1293         glDisable(GL_CULL_FACE);
1294         glMaterialfv(GL_FRONT, GL_DIFFUSE, diff); 
1295         glDisable(GL_LIGHT7);
1296         
1297         /* AA circle */
1298         glEnable(GL_BLEND);
1299         glEnable(GL_LINE_SMOOTH );
1300         glColor3ubv((unsigned char*)wcol->inner);
1301         glutil_draw_lined_arc(0.0f, M_PI*2.0, 100.0f, 32);
1302         glDisable(GL_BLEND);
1303         glDisable(GL_LINE_SMOOTH );
1304
1305         /* matrix after circle */
1306         glPopMatrix();
1307
1308         /* enable blender light */
1309         for(a=0; a<8; a++) {
1310                 if(old[a])
1311                         glEnable(GL_LIGHT0+a);
1312         }
1313 }
1314
1315 static void ui_draw_but_curve_grid(rcti *rect, float zoomx, float zoomy, float offsx, float offsy, float step)
1316 {
1317         float dx, dy, fx, fy;
1318         
1319         glBegin(GL_LINES);
1320         dx= step*zoomx;
1321         fx= rect->xmin + zoomx*(-offsx);
1322         if(fx > rect->xmin) fx -= dx*(floorf(fx-rect->xmin));
1323         while(fx < rect->xmax) {
1324                 glVertex2f(fx, rect->ymin); 
1325                 glVertex2f(fx, rect->ymax);
1326                 fx+= dx;
1327         }
1328         
1329         dy= step*zoomy;
1330         fy= rect->ymin + zoomy*(-offsy);
1331         if(fy > rect->ymin) fy -= dy*(floorf(fy-rect->ymin));
1332         while(fy < rect->ymax) {
1333                 glVertex2f(rect->xmin, fy); 
1334                 glVertex2f(rect->xmax, fy);
1335                 fy+= dy;
1336         }
1337         glEnd();
1338         
1339 }
1340
1341 static void glColor3ubvShade(unsigned char *col, int shade)
1342 {
1343         glColor3ub(col[0]-shade>0?col[0]-shade:0, 
1344                            col[1]-shade>0?col[1]-shade:0,
1345                            col[2]-shade>0?col[2]-shade:0);
1346 }
1347
1348 void ui_draw_but_CURVE(ARegion *ar, uiBut *but, uiWidgetColors *wcol, rcti *rect)
1349 {
1350         CurveMapping *cumap;
1351         CurveMap *cuma;
1352         CurveMapPoint *cmp;
1353         float fx, fy, fac[2], zoomx, zoomy, offsx, offsy;
1354         GLint scissor[4];
1355         rcti scissor_new;
1356         int a;
1357
1358         cumap= (CurveMapping *)(but->editcumap? but->editcumap: but->poin);
1359         cuma= cumap->cm+cumap->cur;
1360         
1361         /* need scissor test, curve can draw outside of boundary */
1362         glGetIntegerv(GL_VIEWPORT, scissor);
1363         scissor_new.xmin= ar->winrct.xmin + rect->xmin;
1364         scissor_new.ymin= ar->winrct.ymin + rect->ymin;
1365         scissor_new.xmax= ar->winrct.xmin + rect->xmax;
1366         scissor_new.ymax= ar->winrct.ymin + rect->ymax;
1367         BLI_isect_rcti(&scissor_new, &ar->winrct, &scissor_new);
1368         glScissor(scissor_new.xmin, scissor_new.ymin, scissor_new.xmax-scissor_new.xmin, scissor_new.ymax-scissor_new.ymin);
1369         
1370         /* calculate offset and zoom */
1371         zoomx= (rect->xmax-rect->xmin-2.0f*but->aspect)/(cumap->curr.xmax - cumap->curr.xmin);
1372         zoomy= (rect->ymax-rect->ymin-2.0f*but->aspect)/(cumap->curr.ymax - cumap->curr.ymin);
1373         offsx= cumap->curr.xmin-but->aspect/zoomx;
1374         offsy= cumap->curr.ymin-but->aspect/zoomy;
1375         
1376         /* backdrop */
1377         if(cumap->flag & CUMA_DO_CLIP) {
1378                 glColor3ubvShade((unsigned char *)wcol->inner, -20);
1379                 glRectf(rect->xmin, rect->ymin, rect->xmax, rect->ymax);
1380                 glColor3ubv((unsigned char*)wcol->inner);
1381                 glRectf(rect->xmin + zoomx*(cumap->clipr.xmin-offsx),
1382                                 rect->ymin + zoomy*(cumap->clipr.ymin-offsy),
1383                                 rect->xmin + zoomx*(cumap->clipr.xmax-offsx),
1384                                 rect->ymin + zoomy*(cumap->clipr.ymax-offsy));
1385         }
1386         else {
1387                 glColor3ubv((unsigned char*)wcol->inner);
1388                 glRectf(rect->xmin, rect->ymin, rect->xmax, rect->ymax);
1389         }
1390                 
1391         /* grid, every .25 step */
1392         glColor3ubvShade((unsigned char *)wcol->inner, -16);
1393         ui_draw_but_curve_grid(rect, zoomx, zoomy, offsx, offsy, 0.25f);
1394         /* grid, every 1.0 step */
1395         glColor3ubvShade((unsigned char *)wcol->inner, -24);
1396         ui_draw_but_curve_grid(rect, zoomx, zoomy, offsx, offsy, 1.0f);
1397         /* axes */
1398         glColor3ubvShade((unsigned char *)wcol->inner, -50);
1399         glBegin(GL_LINES);
1400         glVertex2f(rect->xmin, rect->ymin + zoomy*(-offsy));
1401         glVertex2f(rect->xmax, rect->ymin + zoomy*(-offsy));
1402         glVertex2f(rect->xmin + zoomx*(-offsx), rect->ymin);
1403         glVertex2f(rect->xmin + zoomx*(-offsx), rect->ymax);
1404         glEnd();
1405         
1406         /* magic trigger for curve backgrounds */
1407         if (but->a1 != -1) {
1408                 if (but->a1 == UI_GRAD_H) {
1409                         rcti grid;
1410                         float col[3]= {0.0f, 0.0f, 0.0f}; /* dummy arg */
1411                         
1412                         grid.xmin = rect->xmin + zoomx*(-offsx);
1413                         grid.xmax = rect->xmax + zoomx*(-offsx);
1414                         grid.ymin = rect->ymin + zoomy*(-offsy);
1415                         grid.ymax = rect->ymax + zoomy*(-offsy);
1416                         
1417                         glEnable(GL_BLEND);
1418                         ui_draw_gradient(&grid, col, UI_GRAD_H, 0.5f);
1419                         glDisable(GL_BLEND);
1420                 }
1421         }
1422         
1423         
1424         /* cfra option */
1425         /* XXX 2.48
1426         if(cumap->flag & CUMA_DRAW_CFRA) {
1427                 glColor3ub(0x60, 0xc0, 0x40);
1428                 glBegin(GL_LINES);
1429                 glVertex2f(rect->xmin + zoomx*(cumap->sample[0]-offsx), rect->ymin);
1430                 glVertex2f(rect->xmin + zoomx*(cumap->sample[0]-offsx), rect->ymax);
1431                 glEnd();
1432         }*/
1433         /* sample option */
1434         /* XXX 2.48
1435          * if(cumap->flag & CUMA_DRAW_SAMPLE) {
1436                 if(cumap->cur==3) {
1437                         float lum= cumap->sample[0]*0.35f + cumap->sample[1]*0.45f + cumap->sample[2]*0.2f;
1438                         glColor3ub(240, 240, 240);
1439                         
1440                         glBegin(GL_LINES);
1441                         glVertex2f(rect->xmin + zoomx*(lum-offsx), rect->ymin);
1442                         glVertex2f(rect->xmin + zoomx*(lum-offsx), rect->ymax);
1443                         glEnd();
1444                 }
1445                 else {
1446                         if(cumap->cur==0)
1447                                 glColor3ub(240, 100, 100);
1448                         else if(cumap->cur==1)
1449                                 glColor3ub(100, 240, 100);
1450                         else
1451                                 glColor3ub(100, 100, 240);
1452                         
1453                         glBegin(GL_LINES);
1454                         glVertex2f(rect->xmin + zoomx*(cumap->sample[cumap->cur]-offsx), rect->ymin);
1455                         glVertex2f(rect->xmin + zoomx*(cumap->sample[cumap->cur]-offsx), rect->ymax);
1456                         glEnd();
1457                 }
1458         }*/
1459         
1460         /* the curve */
1461         glColor3ubv((unsigned char*)wcol->item);
1462         glEnable(GL_LINE_SMOOTH);
1463         glEnable(GL_BLEND);
1464         glBegin(GL_LINE_STRIP);
1465         
1466         if(cuma->table==NULL)
1467                 curvemapping_changed(cumap, 0); /* 0 = no remove doubles */
1468         cmp= cuma->table;
1469         
1470         /* first point */
1471         if((cuma->flag & CUMA_EXTEND_EXTRAPOLATE)==0)
1472                 glVertex2f(rect->xmin, rect->ymin + zoomy*(cmp[0].y-offsy));
1473         else {
1474                 fx= rect->xmin + zoomx*(cmp[0].x-offsx + cuma->ext_in[0]);
1475                 fy= rect->ymin + zoomy*(cmp[0].y-offsy + cuma->ext_in[1]);
1476                 glVertex2f(fx, fy);
1477         }
1478         for(a=0; a<=CM_TABLE; a++) {
1479                 fx= rect->xmin + zoomx*(cmp[a].x-offsx);
1480                 fy= rect->ymin + zoomy*(cmp[a].y-offsy);
1481                 glVertex2f(fx, fy);
1482         }
1483         /* last point */
1484         if((cuma->flag & CUMA_EXTEND_EXTRAPOLATE)==0)
1485                 glVertex2f(rect->xmax, rect->ymin + zoomy*(cmp[CM_TABLE].y-offsy));     
1486         else {
1487                 fx= rect->xmin + zoomx*(cmp[CM_TABLE].x-offsx - cuma->ext_out[0]);
1488                 fy= rect->ymin + zoomy*(cmp[CM_TABLE].y-offsy - cuma->ext_out[1]);
1489                 glVertex2f(fx, fy);
1490         }
1491         glEnd();
1492         glDisable(GL_LINE_SMOOTH);
1493         glDisable(GL_BLEND);
1494
1495         /* the points, use aspect to make them visible on edges */
1496         cmp= cuma->curve;
1497         glPointSize(3.0f);
1498         bglBegin(GL_POINTS);
1499         for(a=0; a<cuma->totpoint; a++) {
1500                 if(cmp[a].flag & SELECT)
1501                         UI_ThemeColor(TH_TEXT_HI);
1502                 else
1503                         UI_ThemeColor(TH_TEXT);
1504                 fac[0]= rect->xmin + zoomx*(cmp[a].x-offsx);
1505                 fac[1]= rect->ymin + zoomy*(cmp[a].y-offsy);
1506                 bglVertex2fv(fac);
1507         }
1508         bglEnd();
1509         glPointSize(1.0f);
1510         
1511         /* restore scissortest */
1512         glScissor(scissor[0], scissor[1], scissor[2], scissor[3]);
1513
1514         /* outline */
1515         glColor3ubv((unsigned char*)wcol->outline);
1516         fdrawbox(rect->xmin, rect->ymin, rect->xmax, rect->ymax);
1517 }
1518
1519 static ImBuf *scale_trackpreview_ibuf(ImBuf *ibuf, float zoomx, float zoomy)
1520 {
1521         ImBuf *scaleibuf;
1522         int x, y, w= ibuf->x*zoomx, h= ibuf->y*zoomy;
1523         scaleibuf= IMB_allocImBuf(w, h, 32, IB_rect);
1524
1525         for(y= 0; y<scaleibuf->y; y++) {
1526                 for (x= 0; x<scaleibuf->x; x++) {
1527                         int pixel= scaleibuf->x*y + x;
1528                         int orig_pixel= ibuf->x*(int)(((float)y)/zoomy) + (int)(((float)x)/zoomx);
1529                         char *rrgb= (char*)scaleibuf->rect + pixel*4;
1530                         char *orig_rrgb= (char*)ibuf->rect + orig_pixel*4;
1531                         rrgb[0]= orig_rrgb[0];
1532                         rrgb[1]= orig_rrgb[1];
1533                         rrgb[2]= orig_rrgb[2];
1534                         rrgb[3]= orig_rrgb[3];
1535                 }
1536         }
1537
1538         return scaleibuf;
1539 }
1540
1541 void ui_draw_but_TRACKPREVIEW(ARegion *ar, uiBut *but, uiWidgetColors *UNUSED(wcol), rcti *recti)
1542 {
1543         rctf rect;
1544         int ok= 0;
1545         GLint scissor[4];
1546         MovieClipScopes *scopes = (MovieClipScopes *)but->poin;
1547
1548         rect.xmin = (float)recti->xmin+1;
1549         rect.xmax = (float)recti->xmax-1;
1550         rect.ymin = (float)recti->ymin+SCOPE_RESIZE_PAD+2;
1551         rect.ymax = (float)recti->ymax-1;
1552
1553         glEnable(GL_BLEND);
1554         glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
1555
1556         /* need scissor test, preview image can draw outside of boundary */
1557         glGetIntegerv(GL_VIEWPORT, scissor);
1558         glScissor(ar->winrct.xmin + (rect.xmin-1), ar->winrct.ymin+(rect.ymin-1), (rect.xmax+1)-(rect.xmin-1), (rect.ymax+1)-(rect.ymin-1));
1559
1560         if(scopes->track_disabled) {
1561                 glColor4f(0.7f, 0.3f, 0.3f, 0.3f);
1562                 uiSetRoundBox(15);
1563                 uiDrawBox(GL_POLYGON, rect.xmin-1, rect.ymin-1, rect.xmax+1, rect.ymax+1, 3.0f);
1564
1565                 ok= 1;
1566         }
1567         else if(scopes->track_preview) {
1568                 int a, off_x, off_y;
1569                 float zoomx, zoomy;
1570                 ImBuf *drawibuf;
1571
1572                 glPushMatrix();
1573
1574                 /* draw content of pattern area */
1575                 glScissor(ar->winrct.xmin+rect.xmin, ar->winrct.ymin+rect.ymin, scissor[2], scissor[3]);
1576
1577                 zoomx= (rect.xmax-rect.xmin) / (scopes->track_preview->x-2.f);
1578                 zoomy= (rect.ymax-rect.ymin) / (scopes->track_preview->y-2.f);
1579
1580                 off_x= ((int)scopes->track_pos[0]-scopes->track_pos[0]-0.5)*zoomx;
1581                 off_y= ((int)scopes->track_pos[1]-scopes->track_pos[1]-0.5)*zoomy;
1582
1583                 drawibuf= scale_trackpreview_ibuf(scopes->track_preview, zoomx, zoomy);
1584                 glaDrawPixelsSafe(off_x+rect.xmin, off_y+rect.ymin, rect.xmax-rect.xmin+1.f-off_x, rect.ymax-rect.ymin+1.f-off_y, drawibuf->x, GL_RGBA, GL_UNSIGNED_BYTE, drawibuf->rect);
1585
1586                 IMB_freeImBuf(drawibuf);
1587
1588                 /* draw cross for pizel position */
1589                 glTranslatef(off_x+rect.xmin+scopes->track_pos[0]*zoomx, off_y+rect.ymin+scopes->track_pos[1]*zoomy, 0.f);
1590                 glScissor(ar->winrct.xmin + rect.xmin, ar->winrct.ymin+rect.ymin, rect.xmax-rect.xmin, rect.ymax-rect.ymin);
1591
1592                 for(a= 0; a< 2; a++) {
1593                         if(a==1) {
1594                                 glLineStipple(3, 0xaaaa);
1595                                 glEnable(GL_LINE_STIPPLE);
1596                                 UI_ThemeColor(TH_SEL_MARKER);
1597                         }
1598                         else {
1599                                 UI_ThemeColor(TH_MARKER_OUTLINE);
1600                         }
1601
1602                         glBegin(GL_LINES);
1603                                 glVertex2f(-10.0f, 0.0f);
1604                                 glVertex2f(10.0f, 0.0f);
1605                                 glVertex2f(0.0f, -10.0f);
1606                                 glVertex2f(0.0f, 10.0f);
1607                         glEnd();
1608                 }
1609
1610                 glDisable(GL_LINE_STIPPLE);
1611                 glPopMatrix();
1612
1613                 ok= 1;
1614         }
1615
1616         if(!ok) {
1617                 glColor4f(0.f, 0.f, 0.f, 0.3f);
1618                 uiSetRoundBox(15);
1619                 uiDrawBox(GL_POLYGON, rect.xmin-1, rect.ymin-1, rect.xmax+1, rect.ymax+1, 3.0f);
1620         }
1621
1622         /* outline, scale gripper */
1623         draw_scope_end(&rect, scissor);
1624
1625         glDisable(GL_BLEND);
1626 }
1627
1628 /* ****************************************************** */
1629
1630
1631 static void ui_shadowbox(float minx, float miny, float maxx, float maxy, float shadsize, unsigned char alpha)
1632 {
1633         glEnable(GL_BLEND);
1634         glShadeModel(GL_SMOOTH);
1635         
1636         /* right quad */
1637         glBegin(GL_POLYGON);
1638         glColor4ub(0, 0, 0, alpha);
1639         glVertex2f(maxx, miny);
1640         glVertex2f(maxx, maxy-0.3f*shadsize);
1641         glColor4ub(0, 0, 0, 0);
1642         glVertex2f(maxx+shadsize, maxy-0.75f*shadsize);
1643         glVertex2f(maxx+shadsize, miny);
1644         glEnd();
1645         
1646         /* corner shape */
1647         glBegin(GL_POLYGON);
1648         glColor4ub(0, 0, 0, alpha);
1649         glVertex2f(maxx, miny);
1650         glColor4ub(0, 0, 0, 0);
1651         glVertex2f(maxx+shadsize, miny);
1652         glVertex2f(maxx+0.7f*shadsize, miny-0.7f*shadsize);
1653         glVertex2f(maxx, miny-shadsize);
1654         glEnd();
1655         
1656         /* bottom quad */               
1657         glBegin(GL_POLYGON);
1658         glColor4ub(0, 0, 0, alpha);
1659         glVertex2f(minx+0.3f*shadsize, miny);
1660         glVertex2f(maxx, miny);
1661         glColor4ub(0, 0, 0, 0);
1662         glVertex2f(maxx, miny-shadsize);
1663         glVertex2f(minx+0.5f*shadsize, miny-shadsize);
1664         glEnd();
1665         
1666         glDisable(GL_BLEND);
1667         glShadeModel(GL_FLAT);
1668 }
1669
1670 void uiDrawBoxShadow(unsigned char alpha, float minx, float miny, float maxx, float maxy)
1671 {
1672         /* accumulated outline boxes to make shade not linear, is more pleasant */
1673         ui_shadowbox(minx, miny, maxx, maxy, 11.0, (20*alpha)>>8);
1674         ui_shadowbox(minx, miny, maxx, maxy, 7.0, (40*alpha)>>8);
1675         ui_shadowbox(minx, miny, maxx, maxy, 5.0, (80*alpha)>>8);
1676         
1677 }
1678
1679
1680 void ui_dropshadow(rctf *rct, float radius, float aspect, int UNUSED(select))
1681 {
1682         int i;
1683         float rad;
1684         float a;
1685         char alpha= 2;
1686         
1687         glEnable(GL_BLEND);
1688         
1689         if(radius > (rct->ymax-rct->ymin-10.0f)/2.0f)
1690                 rad= (rct->ymax-rct->ymin-10.0f)/2.0f;
1691         else
1692                 rad= radius;
1693
1694         i= 12;
1695 #if 0
1696         if(select) {
1697                 a= i*aspect; /* same as below */
1698         }
1699         else
1700 #endif
1701         {
1702                 a= i*aspect;
1703         }
1704
1705         for(; i--; a-=aspect) {
1706                 /* alpha ranges from 2 to 20 or so */
1707                 glColor4ub(0, 0, 0, alpha);
1708                 alpha+= 2;
1709                 
1710                 uiDrawBox(GL_POLYGON, rct->xmin - a, rct->ymin - a, rct->xmax + a, rct->ymax-10.0f + a, rad+a);
1711         }
1712         
1713         /* outline emphasis */
1714         glEnable( GL_LINE_SMOOTH );
1715         glColor4ub(0, 0, 0, 100);
1716         uiDrawBox(GL_LINE_LOOP, rct->xmin-0.5f, rct->ymin-0.5f, rct->xmax+0.5f, rct->ymax+0.5f, radius+0.5f);
1717         glDisable( GL_LINE_SMOOTH );
1718         
1719         glDisable(GL_BLEND);
1720 }
1721