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