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