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