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