f16d633de6fbc962ae0d53ac94696aaf169dca62
[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 #include "BLI_utildefines.h"
38
39 #include "BKE_colortools.h"
40 #include "BKE_texture.h"
41
42
43 #include "IMB_imbuf.h"
44 #include "IMB_imbuf_types.h"
45
46 #include "BIF_gl.h"
47 #include "BIF_glutil.h"
48
49 #include "BLF_api.h"
50
51 #include "UI_interface.h"
52
53 /* own include */
54 #include "interface_intern.h"
55
56 #define UI_RB_ALPHA 16
57 #define UI_DISABLED_ALPHA_OFFS  -160
58
59 static int roundboxtype= 15;
60
61 void uiSetRoundBox(int type)
62 {
63         /* Not sure the roundbox function is the best place to change this
64          * if this is undone, its not that big a deal, only makes curves edges
65          * square for the  */
66         roundboxtype= type;
67
68         /* flags to set which corners will become rounded:
69
70         1------2
71         |      |
72         8------4
73         */
74         
75 }
76
77 int uiGetRoundBox(void)
78 {
79         return roundboxtype;
80 }
81
82 void uiDrawBox(int mode, float minx, float miny, float maxx, float maxy, float rad)
83 {
84         float vec[7][2]= {{0.195, 0.02}, {0.383, 0.067}, {0.55, 0.169}, {0.707, 0.293},
85                                           {0.831, 0.45}, {0.924, 0.617}, {0.98, 0.805}};
86         int a;
87         
88         /* mult */
89         for(a=0; a<7; a++) {
90                 vec[a][0]*= rad; vec[a][1]*= rad;
91         }
92
93         glBegin(mode);
94
95         /* start with corner right-bottom */
96         if(roundboxtype & 4) {
97                 glVertex2f(maxx-rad, miny);
98                 for(a=0; a<7; a++) {
99                         glVertex2f(maxx-rad+vec[a][0], miny+vec[a][1]);
100                 }
101                 glVertex2f(maxx, miny+rad);
102         }
103         else glVertex2f(maxx, miny);
104         
105         /* corner right-top */
106         if(roundboxtype & 2) {
107                 glVertex2f(maxx, maxy-rad);
108                 for(a=0; a<7; a++) {
109                         glVertex2f(maxx-vec[a][1], maxy-rad+vec[a][0]);
110                 }
111                 glVertex2f(maxx-rad, maxy);
112         }
113         else glVertex2f(maxx, maxy);
114         
115         /* corner left-top */
116         if(roundboxtype & 1) {
117                 glVertex2f(minx+rad, maxy);
118                 for(a=0; a<7; a++) {
119                         glVertex2f(minx+rad-vec[a][0], maxy-vec[a][1]);
120                 }
121                 glVertex2f(minx, maxy-rad);
122         }
123         else glVertex2f(minx, maxy);
124         
125         /* corner left-bottom */
126         if(roundboxtype & 8) {
127                 glVertex2f(minx, miny+rad);
128                 for(a=0; a<7; a++) {
129                         glVertex2f(minx+vec[a][1], miny+rad-vec[a][0]);
130                 }
131                 glVertex2f(minx+rad, miny);
132         }
133         else glVertex2f(minx, miny);
134         
135         glEnd();
136 }
137
138 static void round_box_shade_col(float *col1, float *col2, float fac)
139 {
140         float col[3];
141
142         col[0]= (fac*col1[0] + (1.0-fac)*col2[0]);
143         col[1]= (fac*col1[1] + (1.0-fac)*col2[1]);
144         col[2]= (fac*col1[2] + (1.0-fac)*col2[2]);
145         
146         glColor3fv(col);
147 }
148
149
150 /* linear horizontal shade within button or in outline */
151 /* view2d scrollers use it */
152 void uiDrawBoxShade(int mode, float minx, float miny, float maxx, float maxy, float rad, float shadetop, float shadedown)
153 {
154         float vec[7][2]= {{0.195, 0.02}, {0.383, 0.067}, {0.55, 0.169}, {0.707, 0.293},
155                                           {0.831, 0.45}, {0.924, 0.617}, {0.98, 0.805}};
156         float div= maxy-miny;
157         float coltop[3], coldown[3], color[4];
158         int a;
159         
160         /* mult */
161         for(a=0; a<7; a++) {
162                 vec[a][0]*= rad; vec[a][1]*= rad;
163         }
164         /* get current color, needs to be outside of glBegin/End */
165         glGetFloatv(GL_CURRENT_COLOR, color);
166
167         /* 'shade' defines strength of shading */       
168         coltop[0]= color[0]+shadetop; if(coltop[0]>1.0) coltop[0]= 1.0;
169         coltop[1]= color[1]+shadetop; if(coltop[1]>1.0) coltop[1]= 1.0;
170         coltop[2]= color[2]+shadetop; if(coltop[2]>1.0) coltop[2]= 1.0;
171         coldown[0]= color[0]+shadedown; if(coldown[0]<0.0) coldown[0]= 0.0;
172         coldown[1]= color[1]+shadedown; if(coldown[1]<0.0) coldown[1]= 0.0;
173         coldown[2]= color[2]+shadedown; if(coldown[2]<0.0) coldown[2]= 0.0;
174
175         glShadeModel(GL_SMOOTH);
176         glBegin(mode);
177
178         /* start with corner right-bottom */
179         if(roundboxtype & 4) {
180                 
181                 round_box_shade_col(coltop, coldown, 0.0);
182                 glVertex2f(maxx-rad, miny);
183                 
184                 for(a=0; a<7; a++) {
185                         round_box_shade_col(coltop, coldown, vec[a][1]/div);
186                         glVertex2f(maxx-rad+vec[a][0], miny+vec[a][1]);
187                 }
188                 
189                 round_box_shade_col(coltop, coldown, rad/div);
190                 glVertex2f(maxx, miny+rad);
191         }
192         else {
193                 round_box_shade_col(coltop, coldown, 0.0);
194                 glVertex2f(maxx, miny);
195         }
196         
197         /* corner right-top */
198         if(roundboxtype & 2) {
199                 
200                 round_box_shade_col(coltop, coldown, (div-rad)/div);
201                 glVertex2f(maxx, maxy-rad);
202                 
203                 for(a=0; a<7; a++) {
204                         round_box_shade_col(coltop, coldown, (div-rad+vec[a][1])/div);
205                         glVertex2f(maxx-vec[a][1], maxy-rad+vec[a][0]);
206                 }
207                 round_box_shade_col(coltop, coldown, 1.0);
208                 glVertex2f(maxx-rad, maxy);
209         }
210         else {
211                 round_box_shade_col(coltop, coldown, 1.0);
212                 glVertex2f(maxx, maxy);
213         }
214         
215         /* corner left-top */
216         if(roundboxtype & 1) {
217                 
218                 round_box_shade_col(coltop, coldown, 1.0);
219                 glVertex2f(minx+rad, maxy);
220                 
221                 for(a=0; a<7; a++) {
222                         round_box_shade_col(coltop, coldown, (div-vec[a][1])/div);
223                         glVertex2f(minx+rad-vec[a][0], maxy-vec[a][1]);
224                 }
225                 
226                 round_box_shade_col(coltop, coldown, (div-rad)/div);
227                 glVertex2f(minx, maxy-rad);
228         }
229         else {
230                 round_box_shade_col(coltop, coldown, 1.0);
231                 glVertex2f(minx, maxy);
232         }
233         
234         /* corner left-bottom */
235         if(roundboxtype & 8) {
236                 
237                 round_box_shade_col(coltop, coldown, rad/div);
238                 glVertex2f(minx, miny+rad);
239                 
240                 for(a=0; a<7; a++) {
241                         round_box_shade_col(coltop, coldown, (rad-vec[a][1])/div);
242                         glVertex2f(minx+vec[a][1], miny+rad-vec[a][0]);
243                 }
244                 
245                 round_box_shade_col(coltop, coldown, 0.0);
246                 glVertex2f(minx+rad, miny);
247         }
248         else {
249                 round_box_shade_col(coltop, coldown, 0.0);
250                 glVertex2f(minx, miny);
251         }
252         
253         glEnd();
254         glShadeModel(GL_FLAT);
255 }
256
257 /* linear vertical shade within button or in outline */
258 /* view2d scrollers use it */
259 void uiDrawBoxVerticalShade(int mode, float minx, float miny, float maxx, float maxy, float rad, float shadeLeft, float shadeRight)
260 {
261         float vec[7][2]= {{0.195, 0.02}, {0.383, 0.067}, {0.55, 0.169}, {0.707, 0.293},
262                                           {0.831, 0.45}, {0.924, 0.617}, {0.98, 0.805}};
263         float div= maxx-minx;
264         float colLeft[3], colRight[3], color[4];
265         int a;
266         
267         /* mult */
268         for(a=0; a<7; a++) {
269                 vec[a][0]*= rad; vec[a][1]*= rad;
270         }
271         /* get current color, needs to be outside of glBegin/End */
272         glGetFloatv(GL_CURRENT_COLOR, color);
273
274         /* 'shade' defines strength of shading */       
275         colLeft[0]= color[0]+shadeLeft; if(colLeft[0]>1.0) colLeft[0]= 1.0;
276         colLeft[1]= color[1]+shadeLeft; if(colLeft[1]>1.0) colLeft[1]= 1.0;
277         colLeft[2]= color[2]+shadeLeft; if(colLeft[2]>1.0) colLeft[2]= 1.0;
278         colRight[0]= color[0]+shadeRight; if(colRight[0]<0.0) colRight[0]= 0.0;
279         colRight[1]= color[1]+shadeRight; if(colRight[1]<0.0) colRight[1]= 0.0;
280         colRight[2]= color[2]+shadeRight; if(colRight[2]<0.0) colRight[2]= 0.0;
281
282         glShadeModel(GL_SMOOTH);
283         glBegin(mode);
284
285         /* start with corner right-bottom */
286         if(roundboxtype & 4) {
287                 round_box_shade_col(colLeft, colRight, 0.0);
288                 glVertex2f(maxx-rad, miny);
289                 
290                 for(a=0; a<7; a++) {
291                         round_box_shade_col(colLeft, colRight, vec[a][0]/div);
292                         glVertex2f(maxx-rad+vec[a][0], miny+vec[a][1]);
293                 }
294                 
295                 round_box_shade_col(colLeft, colRight, rad/div);
296                 glVertex2f(maxx, miny+rad);
297         }
298         else {
299                 round_box_shade_col(colLeft, colRight, 0.0);
300                 glVertex2f(maxx, miny);
301         }
302         
303         /* corner right-top */
304         if(roundboxtype & 2) {
305                 round_box_shade_col(colLeft, colRight, 0.0);
306                 glVertex2f(maxx, maxy-rad);
307                 
308                 for(a=0; a<7; a++) {
309                         
310                         round_box_shade_col(colLeft, colRight, (div-rad-vec[a][0])/div);
311                         glVertex2f(maxx-vec[a][1], maxy-rad+vec[a][0]);
312                 }
313                 round_box_shade_col(colLeft, colRight, (div-rad)/div);
314                 glVertex2f(maxx-rad, maxy);
315         }
316         else {
317                 round_box_shade_col(colLeft, colRight, 0.0);
318                 glVertex2f(maxx, maxy);
319         }
320         
321         /* corner left-top */
322         if(roundboxtype & 1) {
323                 round_box_shade_col(colLeft, colRight, (div-rad)/div);
324                 glVertex2f(minx+rad, maxy);
325                 
326                 for(a=0; a<7; a++) {
327                         round_box_shade_col(colLeft, colRight, (div-rad+vec[a][0])/div);
328                         glVertex2f(minx+rad-vec[a][0], maxy-vec[a][1]);
329                 }
330                 
331                 round_box_shade_col(colLeft, colRight, 1.0);
332                 glVertex2f(minx, maxy-rad);
333         }
334         else {
335                 round_box_shade_col(colLeft, colRight, 1.0);
336                 glVertex2f(minx, maxy);
337         }
338         
339         /* corner left-bottom */
340         if(roundboxtype & 8) {
341                 round_box_shade_col(colLeft, colRight, 1.0);
342                 glVertex2f(minx, miny+rad);
343                 
344                 for(a=0; a<7; a++) {
345                         round_box_shade_col(colLeft, colRight, (vec[a][0])/div);
346                         glVertex2f(minx+vec[a][1], miny+rad-vec[a][0]);
347                 }
348                 
349                 round_box_shade_col(colLeft, colRight, 1.0);
350                 glVertex2f(minx+rad, miny);
351         }
352         else {
353                 round_box_shade_col(colLeft, colRight, 1.0);
354                 glVertex2f(minx, miny);
355         }
356         
357         glEnd();
358         glShadeModel(GL_FLAT);
359 }
360
361 /* plain antialiased unfilled rectangle */
362 void uiRoundRect(float minx, float miny, float maxx, float maxy, float rad)
363 {
364         float color[4];
365         
366         if(roundboxtype & UI_RB_ALPHA) {
367                 glGetFloatv(GL_CURRENT_COLOR, color);
368                 color[3]= 0.5;
369                 glColor4fv(color);
370                 glEnable( GL_BLEND );
371         }
372         
373         /* set antialias line */
374         glEnable( GL_LINE_SMOOTH );
375         glEnable( GL_BLEND );
376
377         uiDrawBox(GL_LINE_LOOP, minx, miny, maxx, maxy, rad);
378    
379         glDisable( GL_BLEND );
380         glDisable( GL_LINE_SMOOTH );
381 }
382
383 /* plain fake antialiased unfilled round rectangle */
384 static void uiRoundRectFakeAA(float minx, float miny, float maxx, float maxy, float rad, float asp)
385 {
386         float color[4], alpha;
387         float raddiff;
388         int i, passes=4;
389         
390         /* get the color and divide up the alpha */
391         glGetFloatv(GL_CURRENT_COLOR, color);
392         alpha = 1; //color[3];
393         color[3]= 0.5*alpha/(float)passes;
394         glColor4fv(color);
395         
396         /* set the 'jitter amount' */
397         raddiff = (1/(float)passes) * asp;
398         
399         glEnable( GL_BLEND );
400         
401         /* draw lots of lines on top of each other */
402         for (i=passes; i>=(-passes); i--) {
403                 uiDrawBox(GL_LINE_LOOP, minx, miny, maxx, maxy, rad+(i*raddiff));
404         }
405         
406         glDisable( GL_BLEND );
407         
408         color[3] = alpha;
409         glColor4fv(color);
410 }
411
412 /* (old, used in outliner) plain antialiased filled box */
413 void uiRoundBox(float minx, float miny, float maxx, float maxy, float rad)
414 {
415         float color[4];
416         
417         if(roundboxtype & UI_RB_ALPHA) {
418                 glGetFloatv(GL_CURRENT_COLOR, color);
419                 color[3]= 0.5;
420                 glColor4fv(color);
421                 glEnable( GL_BLEND );
422         }
423         
424         /* solid part */
425         uiDrawBox(GL_POLYGON, minx, miny, maxx, maxy, rad);
426         
427         /* set antialias line */
428         glEnable( GL_LINE_SMOOTH );
429         glEnable( GL_BLEND );
430         
431         uiDrawBox(GL_LINE_LOOP, minx, miny, maxx, maxy, rad);
432         
433         glDisable( GL_BLEND );
434         glDisable( GL_LINE_SMOOTH );
435 }
436
437
438 /* ************** generic embossed rect, for window sliders etc ************* */
439
440
441 /* text_draw.c uses this */
442 void uiEmboss(float x1, float y1, float x2, float y2, int sel)
443 {
444         
445         /* below */
446         if(sel) glColor3ub(200,200,200);
447         else glColor3ub(50,50,50);
448         fdrawline(x1, y1, x2, y1);
449
450         /* right */
451         fdrawline(x2, y1, x2, y2);
452         
453         /* top */
454         if(sel) glColor3ub(50,50,50);
455         else glColor3ub(200,200,200);
456         fdrawline(x1, y2, x2, y2);
457
458         /* left */
459         fdrawline(x1, y1, x1, y2);
460         
461 }
462
463 /* ************** SPECIAL BUTTON DRAWING FUNCTIONS ************* */
464
465 void ui_draw_but_IMAGE(ARegion *UNUSED(ar), uiBut *UNUSED(but), uiWidgetColors *UNUSED(wcol), rcti *rect)
466 {
467         extern char datatoc_splash_png[];
468         extern int datatoc_splash_png_size;
469         ImBuf *ibuf;
470         //GLint scissor[4];
471         //int w, h;
472         
473         /* hardcoded to splash, loading and freeing every draw, eek! */
474         ibuf= IMB_ibImageFromMemory((unsigned char*)datatoc_splash_png, datatoc_splash_png_size, IB_rect);
475
476         if (!ibuf) return;
477         
478         /* scissor doesn't seem to be doing the right thing...?
479         //glColor4f(1.0, 0.f, 0.f, 1.f);
480         //fdrawbox(rect->xmin, rect->ymin, rect->xmax, rect->ymax)
481
482         w = (rect->xmax - rect->xmin);
483         h = (rect->ymax - rect->ymin);
484         // prevent drawing outside widget area
485         glGetIntegerv(GL_SCISSOR_BOX, scissor);
486         glScissor(ar->winrct.xmin + rect->xmin, ar->winrct.ymin + rect->ymin, w, h);
487         */
488         
489         glEnable(GL_BLEND);
490         glColor4f(0.0, 0.0, 0.0, 0.0);
491         
492         glaDrawPixelsSafe((float)rect->xmin, (float)rect->ymin, ibuf->x, ibuf->y, ibuf->x, GL_RGBA, GL_UNSIGNED_BYTE, ibuf->rect);
493         //glaDrawPixelsTex((float)rect->xmin, (float)rect->ymin, ibuf->x, ibuf->y, GL_UNSIGNED_BYTE, ibuf->rect);
494         
495         glDisable(GL_BLEND);
496         
497         /* 
498         // restore scissortest
499         glScissor(scissor[0], scissor[1], scissor[2], scissor[3]);
500         */
501         
502         IMB_freeImBuf(ibuf);
503 }
504
505 #if 0
506 #ifdef INTERNATIONAL
507 static void ui_draw_but_CHARTAB(uiBut *but)
508 {
509         /* XXX 2.50 bad global access */
510         /* Some local variables */
511         float sx, sy, ex, ey;
512         float width, height;
513         float butw, buth;
514         int x, y, cs;
515         wchar_t wstr[2];
516         unsigned char ustr[16];
517         PackedFile *pf;
518         int result = 0;
519         int charmax = G.charmax;
520         
521         /* FO_BUILTIN_NAME font in use. There are TTF FO_BUILTIN_NAME and non-TTF FO_BUILTIN_NAME fonts */
522         if(!strcmp(G.selfont->name, FO_BUILTIN_NAME))
523         {
524                 if(G.ui_international == TRUE)
525                 {
526                         charmax = 0xff;
527                 }
528                 else
529                 {
530                         charmax = 0xff;
531                 }
532         }
533
534         /* Category list exited without selecting the area */
535         if(G.charmax == 0)
536                 charmax = G.charmax = 0xffff;
537
538         /* Calculate the size of the button */
539         width = abs(rect->xmax - rect->xmin);
540         height = abs(rect->ymax - rect->ymin);
541         
542         butw = floor(width / 12);
543         buth = floor(height / 6);
544         
545         /* Initialize variables */
546         sx = rect->xmin;
547         ex = rect->xmin + butw;
548         sy = rect->ymin + height - buth;
549         ey = rect->ymin + height;
550
551         cs = G.charstart;
552
553         /* Set the font, in case it is not FO_BUILTIN_NAME font */
554         if(G.selfont && strcmp(G.selfont->name, FO_BUILTIN_NAME))
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                         char tmpStr[256];
565                         int err;
566
567                         BLI_strncpy(tmpStr, G.selfont->name, sizeof(tmpStr));
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 static void histogram_draw_one(float r, float g, float b, float alpha, float x, float y, float w, float h, float *data, int res)
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 static float polar_to_x(float center, float diam, float ampli, float angle)
965 {
966         return center + diam * ampli * cosf(angle);
967 }
968
969 static float polar_to_y(float center, float diam, float ampli, float angle)
970 {
971         return center + diam * ampli * sinf(angle);
972 }
973
974 static 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(unsigned 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((unsigned char *)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((unsigned char *)wcol->inner, -16);
1397         ui_draw_but_curve_grid(rect, zoomx, zoomy, offsx, offsy, 0.25f);
1398         /* grid, every 1.0 step */
1399         glColor3ubvShade((unsigned char *)wcol->inner, -24);
1400         ui_draw_but_curve_grid(rect, zoomx, zoomy, offsx, offsy, 1.0f);
1401         /* axes */
1402         glColor3ubvShade((unsigned char *)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         int i;
1579         float rad;
1580         float a;
1581         char alpha= 2;
1582         
1583         glEnable(GL_BLEND);
1584         
1585         if(radius > (rct->ymax-rct->ymin-10.0f)/2.0f)
1586                 rad= (rct->ymax-rct->ymin-10.0f)/2.0f;
1587         else
1588                 rad= radius;
1589
1590         i= 12;
1591         if(select) a= i*aspect; else a= i*aspect;
1592         for(; i--; a-=aspect) {
1593                 /* alpha ranges from 2 to 20 or so */
1594                 glColor4ub(0, 0, 0, alpha);
1595                 alpha+= 2;
1596                 
1597                 uiDrawBox(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         uiDrawBox(GL_LINE_LOOP, rct->xmin-0.5f, rct->ymin-0.5f, rct->xmax+0.5f, rct->ymax+0.5f, radius+0.5f);
1604         glDisable( GL_LINE_SMOOTH );
1605         
1606         glDisable(GL_BLEND);
1607 }
1608