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