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