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