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