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