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