Merge image related changes from the render branch. This includes the image
[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_global.h"
39 #include "BKE_texture.h"
40 #include "BKE_utildefines.h"
41
42 #include "IMB_imbuf.h"
43 #include "IMB_imbuf_types.h"
44
45 #include "BIF_gl.h"
46 #include "BIF_glutil.h"
47
48 #include "BLF_api.h"
49
50 #include "UI_interface.h"
51
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 gl_round_box(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 gl_round_box_shade(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 gl_round_box_vertical_shade(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         gl_round_box(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                 gl_round_box(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         gl_round_box(GL_POLYGON, minx, miny, maxx, maxy, rad);
424         
425         /* set antialias line */
426         glEnable( GL_LINE_SMOOTH );
427         glEnable( GL_BLEND );
428         
429         gl_round_box(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 *ar, uiBut *but, uiWidgetColors *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         gl_round_box(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 *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         gl_round_box(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 *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         gl_round_box(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_LUM) {
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_LUM)
882                 fdrawline(rect.xmin, yofs+h*0.075f, rect.xmax+1, yofs+h*0.075f);
883         
884         /* LUMA (1 channel) */
885         glBlendFunc(GL_ONE,GL_ONE);
886         glColor3f(alpha, alpha, alpha);
887         if (scopes->wavefrm_mode == SCOPES_WAVEFRM_LUM){
888
889                 glBlendFunc(GL_ONE,GL_ONE);
890                 
891                 glPushMatrix();
892                 glEnableClientState(GL_VERTEX_ARRAY);
893                 
894                 glTranslatef(rect.xmin, yofs, 0.f);
895                 glScalef(w, h, 0.f);
896                 glVertexPointer(2, GL_FLOAT, 0, scopes->waveform_1);
897                 glDrawArrays(GL_POINTS, 0, scopes->waveform_tot);
898                                 
899                 glDisableClientState(GL_VERTEX_ARRAY);
900                 glPopMatrix();
901
902                 /* min max */
903                 glColor3f(.5f, .5f, .5f);
904                 min= yofs+scopes->minmax[0][0]*h;
905                 max= yofs+scopes->minmax[0][1]*h;
906                 CLAMP(min, rect.ymin, rect.ymax);
907                 CLAMP(max, rect.ymin, rect.ymax);
908                 fdrawline(rect.xmax-3,min,rect.xmax-3,max);
909         }
910
911         /* RGB / YCC (3 channels) */
912         else if (ELEM4(scopes->wavefrm_mode, SCOPES_WAVEFRM_RGB, SCOPES_WAVEFRM_YCC_601, SCOPES_WAVEFRM_YCC_709, SCOPES_WAVEFRM_YCC_JPEG)) {
913                 int rgb = (scopes->wavefrm_mode == SCOPES_WAVEFRM_RGB);
914                 
915                 glBlendFunc(GL_ONE,GL_ONE);
916                 
917                 glPushMatrix();
918                 glEnableClientState(GL_VERTEX_ARRAY);
919                 
920                 glTranslatef(rect.xmin, yofs, 0.f);
921                 glScalef(w3, h, 0.f);
922                 
923                 glColor3fv((rgb)?colors_alpha[0]:colorsycc_alpha[0]);
924                 glVertexPointer(2, GL_FLOAT, 0, scopes->waveform_1);
925                 glDrawArrays(GL_POINTS, 0, scopes->waveform_tot);
926
927                 glTranslatef(1.f, 0.f, 0.f);
928                 glColor3fv((rgb)?colors_alpha[1]:colorsycc_alpha[1]);
929                 glVertexPointer(2, GL_FLOAT, 0, scopes->waveform_2);
930                 glDrawArrays(GL_POINTS, 0, scopes->waveform_tot);
931                 
932                 glTranslatef(1.f, 0.f, 0.f);
933                 glColor3fv((rgb)?colors_alpha[2]:colorsycc_alpha[2]);
934                 glVertexPointer(2, GL_FLOAT, 0, scopes->waveform_3);
935                 glDrawArrays(GL_POINTS, 0, scopes->waveform_tot);
936                 
937                 glDisableClientState(GL_VERTEX_ARRAY);
938                 glPopMatrix();
939
940                 
941                 /* min max */
942                 for (c=0; c<3; c++) {
943                         if (scopes->wavefrm_mode == SCOPES_WAVEFRM_RGB)
944                                 glColor3f(colors[c][0]*0.75, colors[c][1]*0.75, colors[c][2]*0.75);
945                         else
946                                 glColor3f(colorsycc[c][0]*0.75, colorsycc[c][1]*0.75, colorsycc[c][2]*0.75);
947                         min= yofs+scopes->minmax[c][0]*h;
948                         max= yofs+scopes->minmax[c][1]*h;
949                         CLAMP(min, rect.ymin, rect.ymax);
950                         CLAMP(max, rect.ymin, rect.ymax);
951                         fdrawline(rect.xmin+w+2+c*2,min,rect.xmin+w+2+c*2,max);
952                 }
953         }
954         
955         /* outline, scale gripper */
956         draw_scope_end(&rect, scissor);
957 }
958
959 float polar_to_x(float center, float diam, float ampli, float angle)
960 {
961         return center + diam * ampli * cosf(angle);
962 }
963
964 float polar_to_y(float center, float diam, float ampli, float angle)
965 {
966         return center + diam * ampli * sinf(angle);
967 }
968
969 void vectorscope_draw_target(float centerx, float centery, float diam, float r, float g, float b)
970 {
971         float y,u,v;
972         float tangle=0.f, tampli;
973         float dangle, dampli, dangle2, dampli2;
974
975         rgb_to_yuv(r,g,b, &y, &u, &v);
976         if (u>0 && v>=0) tangle=atanf(v/u);
977         else if (u>0 && v<0) tangle=atanf(v/u)+2*M_PI;
978         else if (u<0) tangle=atanf(v/u)+M_PI;
979         else if (u==0 && v>0) tangle=M_PI/2.0f;
980         else if (u==0 && v<0) tangle=-M_PI/2.0f;
981         tampli= sqrtf(u*u+v*v);
982
983         /* small target vary by 2.5 degree and 2.5 IRE unit */
984         glColor4f(1.0f, 1.0f, 1.0, 0.12f);
985         dangle= 2.5*M_PI/180.0f;
986         dampli= 2.5f/200.0f;
987         glBegin(GL_LINE_STRIP);
988         glVertex2f(polar_to_x(centerx,diam,tampli+dampli,tangle+dangle), polar_to_y(centery,diam,tampli+dampli,tangle+dangle));
989         glVertex2f(polar_to_x(centerx,diam,tampli-dampli,tangle+dangle), polar_to_y(centery,diam,tampli-dampli,tangle+dangle));
990         glVertex2f(polar_to_x(centerx,diam,tampli-dampli,tangle-dangle), polar_to_y(centery,diam,tampli-dampli,tangle-dangle));
991         glVertex2f(polar_to_x(centerx,diam,tampli+dampli,tangle-dangle), polar_to_y(centery,diam,tampli+dampli,tangle-dangle));
992         glVertex2f(polar_to_x(centerx,diam,tampli+dampli,tangle+dangle), polar_to_y(centery,diam,tampli+dampli,tangle+dangle));
993         glEnd();
994         /* big target vary by 10 degree and 20% amplitude */
995         glColor4f(1.0f, 1.0f, 1.0, 0.12f);
996         dangle= 10*M_PI/180.0f;
997         dampli= 0.2*tampli;
998         dangle2= 5.0f*M_PI/180.0f;
999         dampli2= 0.5f*dampli;
1000         glBegin(GL_LINE_STRIP);
1001         glVertex2f(polar_to_x(centerx,diam,tampli+dampli-dampli2,tangle+dangle), polar_to_y(centery,diam,tampli+dampli-dampli2,tangle+dangle));
1002         glVertex2f(polar_to_x(centerx,diam,tampli+dampli,tangle+dangle), polar_to_y(centery,diam,tampli+dampli,tangle+dangle));
1003         glVertex2f(polar_to_x(centerx,diam,tampli+dampli,tangle+dangle-dangle2), polar_to_y(centery,diam,tampli+dampli,tangle+dangle-dangle2));
1004         glEnd();
1005         glBegin(GL_LINE_STRIP);
1006         glVertex2f(polar_to_x(centerx,diam,tampli-dampli+dampli2,tangle+dangle), polar_to_y(centery ,diam,tampli-dampli+dampli2,tangle+dangle));
1007         glVertex2f(polar_to_x(centerx,diam,tampli-dampli,tangle+dangle), polar_to_y(centery,diam,tampli-dampli,tangle+dangle));
1008         glVertex2f(polar_to_x(centerx,diam,tampli-dampli,tangle+dangle-dangle2), polar_to_y(centery,diam,tampli-dampli,tangle+dangle-dangle2));
1009         glEnd();
1010         glBegin(GL_LINE_STRIP);
1011         glVertex2f(polar_to_x(centerx,diam,tampli-dampli+dampli2,tangle-dangle), polar_to_y(centery,diam,tampli-dampli+dampli2,tangle-dangle));
1012         glVertex2f(polar_to_x(centerx,diam,tampli-dampli,tangle-dangle), polar_to_y(centery,diam,tampli-dampli,tangle-dangle));
1013         glVertex2f(polar_to_x(centerx,diam,tampli-dampli,tangle-dangle+dangle2), polar_to_y(centery,diam,tampli-dampli,tangle-dangle+dangle2));
1014         glEnd();
1015         glBegin(GL_LINE_STRIP);
1016         glVertex2f(polar_to_x(centerx,diam,tampli+dampli-dampli2,tangle-dangle), polar_to_y(centery,diam,tampli+dampli-dampli2,tangle-dangle));
1017         glVertex2f(polar_to_x(centerx,diam,tampli+dampli,tangle-dangle), polar_to_y(centery,diam,tampli+dampli,tangle-dangle));
1018         glVertex2f(polar_to_x(centerx,diam,tampli+dampli,tangle-dangle+dangle2), polar_to_y(centery,diam,tampli+dampli,tangle-dangle+dangle2));
1019         glEnd();
1020 }
1021
1022 void ui_draw_but_VECTORSCOPE(ARegion *ar, uiBut *but, uiWidgetColors *wcol, rcti *recti)
1023 {
1024         Scopes *scopes = (Scopes *)but->poin;
1025         rctf rect;
1026         int i, j;
1027         int skina= 123; /* angle in degree of the skin tone line */
1028         float w, h, centerx, centery, diam;
1029         float alpha;
1030         float colors[6][3]={{.75,0,0},{.75,.75,0},{0,.75,0},{0,.75,.75},{0,0,.75},{.75,0,.75}};
1031         GLint scissor[4];
1032         
1033         if (scopes==NULL) return;
1034         
1035         rect.xmin = (float)recti->xmin+1;
1036         rect.xmax = (float)recti->xmax-1;
1037         rect.ymin = (float)recti->ymin+SCOPE_RESIZE_PAD+2;
1038         rect.ymax = (float)recti->ymax-1;
1039         
1040         w = rect.xmax - rect.xmin;
1041         h = rect.ymax - rect.ymin;
1042         centerx = rect.xmin + w/2;
1043         centery = rect.ymin + h/2;
1044         diam= (w<h)?w:h;
1045         
1046         alpha = scopes->vecscope_alpha*scopes->vecscope_alpha*scopes->vecscope_alpha;
1047                         
1048         glEnable(GL_BLEND);
1049         glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
1050         
1051         glColor4f(0.f, 0.f, 0.f, 0.3f);
1052         uiSetRoundBox(15);
1053         gl_round_box(GL_POLYGON, rect.xmin-1, rect.ymin-1, rect.xmax+1, rect.ymax+1, 3.0f);
1054
1055         /* need scissor test, hvectorscope can draw outside of boundary */
1056         glGetIntegerv(GL_VIEWPORT, scissor);
1057         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));
1058         
1059         glColor4f(1.f, 1.f, 1.f, 0.08f);
1060         /* draw grid elements */
1061         /* cross */
1062         fdrawline(centerx - (diam/2)-5, centery, centerx + (diam/2)+5, centery);
1063         fdrawline(centerx, centery - (diam/2)-5, centerx, centery + (diam/2)+5);
1064         /* circles */
1065         for(j=0; j<5; j++) {
1066                 glBegin(GL_LINE_STRIP);
1067                 for(i=0; i<=360; i=i+15) {
1068                         float a= i*M_PI/180.0;
1069                         float r= (j+1)/10.0f;
1070                         glVertex2f( polar_to_x(centerx,diam,r,a), polar_to_y(centery,diam,r,a));
1071                 }
1072                 glEnd();
1073         }
1074         /* skin tone line */
1075         glColor4f(1.f, 0.4f, 0.f, 0.2f);
1076         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),
1077                                 polar_to_x(centerx, diam, 0.1f, skina*M_PI/180.0), polar_to_y(centery,diam,0.1,skina*M_PI/180.0));
1078         /* saturation points */
1079         for(i=0; i<6; i++)
1080                 vectorscope_draw_target(centerx, centery, diam, colors[i][0], colors[i][1], colors[i][2]);
1081         
1082         /* pixel point cloud */
1083         glBlendFunc(GL_ONE,GL_ONE);
1084         glColor4f(alpha, alpha, alpha, alpha);
1085
1086         glPushMatrix();
1087         glEnableClientState(GL_VERTEX_ARRAY);
1088
1089         glTranslatef(centerx, centery, 0.f);
1090         glScalef(diam, diam, 0.f);
1091
1092         /*apparently this can sometimes be NULL? - joeedh*/
1093         if (scopes) {
1094                 glVertexPointer(2, GL_FLOAT, 0, scopes->vecscope);
1095                 glDrawArrays(GL_POINTS, 0, scopes->waveform_tot);
1096         }
1097
1098         glDisableClientState(GL_VERTEX_ARRAY);
1099         glPopMatrix();
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