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