doxygen: blender/editors tagged.
[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.0-fac)*col2[0]);
148         col[1]= (fac*col1[1] + (1.0-fac)*col2[1]);
149         col[2]= (fac*col1[2] + (1.0-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.0) coltop[0]= 1.0;
174         coltop[1]= color[1]+shadetop; if(coltop[1]>1.0) coltop[1]= 1.0;
175         coltop[2]= color[2]+shadetop; if(coltop[2]>1.0) coltop[2]= 1.0;
176         coldown[0]= color[0]+shadedown; if(coldown[0]<0.0) coldown[0]= 0.0;
177         coldown[1]= color[1]+shadedown; if(coldown[1]<0.0) coldown[1]= 0.0;
178         coldown[2]= color[2]+shadedown; if(coldown[2]<0.0) coldown[2]= 0.0;
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.0) colLeft[0]= 1.0;
281         colLeft[1]= color[1]+shadeLeft; if(colLeft[1]>1.0) colLeft[1]= 1.0;
282         colLeft[2]= color[2]+shadeLeft; if(colLeft[2]>1.0) colLeft[2]= 1.0;
283         colRight[0]= color[0]+shadeRight; if(colRight[0]<0.0) colRight[0]= 0.0;
284         colRight[1]= color[1]+shadeRight; if(colRight[1]<0.0) colRight[1]= 0.0;
285         colRight[2]= color[2]+shadeRight; if(colRight[2]<0.0) colRight[2]= 0.0;
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         if (hist==NULL) { printf("hist is null \n"); return; }
769         
770         rect.xmin = (float)recti->xmin+1;
771         rect.xmax = (float)recti->xmax-1;
772         rect.ymin = (float)recti->ymin+SCOPE_RESIZE_PAD+2;
773         rect.ymax = (float)recti->ymax-1;
774         
775         w = rect.xmax - rect.xmin;
776         h = (rect.ymax - rect.ymin) * hist->ymax;
777         
778         glEnable(GL_BLEND);
779         glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
780         
781         glColor4f(0.f, 0.f, 0.f, 0.3f);
782         uiSetRoundBox(15);
783         uiDrawBox(GL_POLYGON, rect.xmin-1, rect.ymin-1, rect.xmax+1, rect.ymax+1, 3.0f);
784         
785         glColor4f(1.f, 1.f, 1.f, 0.08f);
786         /* draw grid lines here */
787         for (i=1; i<4; i++) {
788                 fdrawline(rect.xmin, rect.ymin+(i/4.f)*h, rect.xmax, rect.ymin+(i/4.f)*h);
789                 fdrawline(rect.xmin+(i/4.f)*w, rect.ymin, rect.xmin+(i/4.f)*w, rect.ymax);
790         }
791         
792         /* need scissor test, histogram can draw outside of boundary */
793         glGetIntegerv(GL_VIEWPORT, scissor);
794         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));
795         
796         if (hist->mode == HISTO_MODE_LUMA)
797                 histogram_draw_one(1.0, 1.0, 1.0, 0.75, rect.xmin, rect.ymin, w, h, hist->data_luma, res);
798         else {
799                 if (hist->mode == HISTO_MODE_RGB || hist->mode == HISTO_MODE_R)
800                         histogram_draw_one(1.0, 0.0, 0.0, 0.75, rect.xmin, rect.ymin, w, h, hist->data_r, res);
801                 if (hist->mode == HISTO_MODE_RGB || hist->mode == HISTO_MODE_G)
802                         histogram_draw_one(0.0, 1.0, 0.0, 0.75, rect.xmin, rect.ymin, w, h, hist->data_g, res);
803                 if (hist->mode == HISTO_MODE_RGB || hist->mode == HISTO_MODE_B)
804                         histogram_draw_one(0.0, 0.0, 1.0, 0.75, rect.xmin, rect.ymin, w, h, hist->data_b, res);
805         }
806         
807         /* outline, scale gripper */
808         draw_scope_end(&rect, scissor);
809 }
810
811 void ui_draw_but_WAVEFORM(ARegion *ar, uiBut *but, uiWidgetColors *UNUSED(wcol), rcti *recti)
812 {
813         Scopes *scopes = (Scopes *)but->poin;
814         rctf rect;
815         int i, c;
816         float w, w3, h, alpha, yofs;
817         GLint scissor[4];
818         float colors[3][3]= MAT3_UNITY;
819         float colorsycc[3][3] = {{1,0,1},{1,1,0},{0,1,1}};
820         float colors_alpha[3][3], colorsycc_alpha[3][3]; /* colors  pre multiplied by alpha for speed up */
821         float min, max;
822         
823         if (scopes==NULL) return;
824         
825         rect.xmin = (float)recti->xmin+1;
826         rect.xmax = (float)recti->xmax-1;
827         rect.ymin = (float)recti->ymin+SCOPE_RESIZE_PAD+2;
828         rect.ymax = (float)recti->ymax-1;
829         
830         if (scopes->wavefrm_yfac < 0.5f )
831                 scopes->wavefrm_yfac =0.98f;
832         w = rect.xmax - rect.xmin-7;
833         h = (rect.ymax - rect.ymin)*scopes->wavefrm_yfac;
834         yofs= rect.ymin + (rect.ymax - rect.ymin -h)/2.0f;
835         w3=w/3.0f;
836         
837         /* log scale for alpha */
838         alpha = scopes->wavefrm_alpha*scopes->wavefrm_alpha;
839         
840         for(c=0; c<3; c++) {
841                 for(i=0; i<3; i++) {
842                         colors_alpha[c][i] = colors[c][i] * alpha;
843                         colorsycc_alpha[c][i] = colorsycc[c][i] * alpha;
844                 }
845         }
846                         
847         glEnable(GL_BLEND);
848         glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
849         
850         glColor4f(0.f, 0.f, 0.f, 0.3f);
851         uiSetRoundBox(15);
852         uiDrawBox(GL_POLYGON, rect.xmin-1, rect.ymin-1, rect.xmax+1, rect.ymax+1, 3.0f);
853         
854
855         /* need scissor test, waveform can draw outside of boundary */
856         glGetIntegerv(GL_VIEWPORT, scissor);
857         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));
858
859         glColor4f(1.f, 1.f, 1.f, 0.08f);
860         /* draw grid lines here */
861         for (i=0; i<6; i++) {
862                 char str[4];
863                 sprintf(str,"%-3d",i*20);
864                 str[3]='\0';
865                 fdrawline(rect.xmin+22, yofs+(i/5.f)*h, rect.xmax+1, yofs+(i/5.f)*h);
866                 BLF_draw_default(rect.xmin+1, yofs-5+(i/5.f)*h, 0, str, sizeof(str)-1);
867                 /* in the loop because blf_draw reset it */
868                 glEnable(GL_BLEND);
869                 glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
870         }
871         /* 3 vertical separation */
872         if (scopes->wavefrm_mode!= SCOPES_WAVEFRM_LUMA) {
873                 for (i=1; i<3; i++) {
874                         fdrawline(rect.xmin+i*w3, rect.ymin, rect.xmin+i*w3, rect.ymax);
875                 }
876         }
877         
878         /* separate min max zone on the right */
879         fdrawline(rect.xmin+w, rect.ymin, rect.xmin+w, rect.ymax);
880         /* 16-235-240 level in case of ITU-R BT601/709 */
881         glColor4f(1.f, 0.4f, 0.f, 0.2f);
882         if (ELEM(scopes->wavefrm_mode, SCOPES_WAVEFRM_YCC_601, SCOPES_WAVEFRM_YCC_709)){
883                 fdrawline(rect.xmin+22, yofs+h*16.0f/255.0f, rect.xmax+1, yofs+h*16.0f/255.0f);
884                 fdrawline(rect.xmin+22, yofs+h*235.0f/255.0f, rect.xmin+w3, yofs+h*235.0f/255.0f);
885                 fdrawline(rect.xmin+3*w3, yofs+h*235.0f/255.0f, rect.xmax+1, yofs+h*235.0f/255.0f);
886                 fdrawline(rect.xmin+w3, yofs+h*240.0f/255.0f, rect.xmax+1, yofs+h*240.0f/255.0f);
887         }
888         /* 7.5 IRE black point level for NTSC */
889         if (scopes->wavefrm_mode== SCOPES_WAVEFRM_LUMA)
890                 fdrawline(rect.xmin, yofs+h*0.075f, rect.xmax+1, yofs+h*0.075f);
891
892         if (scopes->ok && scopes->waveform_1 != NULL) {
893                 
894                 /* LUMA (1 channel) */
895                 glBlendFunc(GL_ONE,GL_ONE);
896                 glColor3f(alpha, alpha, alpha);
897                 if (scopes->wavefrm_mode == SCOPES_WAVEFRM_LUMA){
898
899                         glBlendFunc(GL_ONE,GL_ONE);
900                         
901                         glPushMatrix();
902                         glEnableClientState(GL_VERTEX_ARRAY);
903                         
904                         glTranslatef(rect.xmin, yofs, 0.f);
905                         glScalef(w, h, 0.f);
906                         glVertexPointer(2, GL_FLOAT, 0, scopes->waveform_1);
907                         glDrawArrays(GL_POINTS, 0, scopes->waveform_tot);
908                                         
909                         glDisableClientState(GL_VERTEX_ARRAY);
910                         glPopMatrix();
911
912                         /* min max */
913                         glColor3f(.5f, .5f, .5f);
914                         min= yofs+scopes->minmax[0][0]*h;
915                         max= yofs+scopes->minmax[0][1]*h;
916                         CLAMP(min, rect.ymin, rect.ymax);
917                         CLAMP(max, rect.ymin, rect.ymax);
918                         fdrawline(rect.xmax-3,min,rect.xmax-3,max);
919                 }
920
921                 /* RGB / YCC (3 channels) */
922                 else if (ELEM4(scopes->wavefrm_mode, SCOPES_WAVEFRM_RGB, SCOPES_WAVEFRM_YCC_601, SCOPES_WAVEFRM_YCC_709, SCOPES_WAVEFRM_YCC_JPEG)) {
923                         int rgb = (scopes->wavefrm_mode == SCOPES_WAVEFRM_RGB);
924                         
925                         glBlendFunc(GL_ONE,GL_ONE);
926                         
927                         glPushMatrix();
928                         glEnableClientState(GL_VERTEX_ARRAY);
929                         
930                         glTranslatef(rect.xmin, yofs, 0.f);
931                         glScalef(w3, h, 0.f);
932                         
933                         glColor3fv((rgb)?colors_alpha[0]:colorsycc_alpha[0]);
934                         glVertexPointer(2, GL_FLOAT, 0, scopes->waveform_1);
935                         glDrawArrays(GL_POINTS, 0, scopes->waveform_tot);
936
937                         glTranslatef(1.f, 0.f, 0.f);
938                         glColor3fv((rgb)?colors_alpha[1]:colorsycc_alpha[1]);
939                         glVertexPointer(2, GL_FLOAT, 0, scopes->waveform_2);
940                         glDrawArrays(GL_POINTS, 0, scopes->waveform_tot);
941                         
942                         glTranslatef(1.f, 0.f, 0.f);
943                         glColor3fv((rgb)?colors_alpha[2]:colorsycc_alpha[2]);
944                         glVertexPointer(2, GL_FLOAT, 0, scopes->waveform_3);
945                         glDrawArrays(GL_POINTS, 0, scopes->waveform_tot);
946                         
947                         glDisableClientState(GL_VERTEX_ARRAY);
948                         glPopMatrix();
949
950                         
951                         /* min max */
952                         for (c=0; c<3; c++) {
953                                 if (scopes->wavefrm_mode == SCOPES_WAVEFRM_RGB)
954                                         glColor3f(colors[c][0]*0.75, colors[c][1]*0.75, colors[c][2]*0.75);
955                                 else
956                                         glColor3f(colorsycc[c][0]*0.75, colorsycc[c][1]*0.75, colorsycc[c][2]*0.75);
957                                 min= yofs+scopes->minmax[c][0]*h;
958                                 max= yofs+scopes->minmax[c][1]*h;
959                                 CLAMP(min, rect.ymin, rect.ymax);
960                                 CLAMP(max, rect.ymin, rect.ymax);
961                                 fdrawline(rect.xmin+w+2+c*2,min,rect.xmin+w+2+c*2,max);
962                         }
963                 }
964                 
965         }
966         
967         /* outline, scale gripper */
968         draw_scope_end(&rect, scissor);
969 }
970
971 static float polar_to_x(float center, float diam, float ampli, float angle)
972 {
973         return center + diam * ampli * cosf(angle);
974 }
975
976 static float polar_to_y(float center, float diam, float ampli, float angle)
977 {
978         return center + diam * ampli * sinf(angle);
979 }
980
981 static void vectorscope_draw_target(float centerx, float centery, float diam, float r, float g, float b)
982 {
983         float y,u,v;
984         float tangle=0.f, tampli;
985         float dangle, dampli, dangle2, dampli2;
986
987         rgb_to_yuv(r,g,b, &y, &u, &v);
988         if (u>0 && v>=0) tangle=atanf(v/u);
989         else if (u>0 && v<0) tangle=atanf(v/u)+2*M_PI;
990         else if (u<0) tangle=atanf(v/u)+M_PI;
991         else if (u==0 && v>0) tangle=M_PI/2.0f;
992         else if (u==0 && v<0) tangle=-M_PI/2.0f;
993         tampli= sqrtf(u*u+v*v);
994
995         /* small target vary by 2.5 degree and 2.5 IRE unit */
996         glColor4f(1.0f, 1.0f, 1.0, 0.12f);
997         dangle= 2.5*M_PI/180.0f;
998         dampli= 2.5f/200.0f;
999         glBegin(GL_LINE_STRIP);
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         glVertex2f(polar_to_x(centerx,diam,tampli+dampli,tangle-dangle), polar_to_y(centery,diam,tampli+dampli,tangle-dangle));
1004         glVertex2f(polar_to_x(centerx,diam,tampli+dampli,tangle+dangle), polar_to_y(centery,diam,tampli+dampli,tangle+dangle));
1005         glEnd();
1006         /* big target vary by 10 degree and 20% amplitude */
1007         glColor4f(1.0f, 1.0f, 1.0, 0.12f);
1008         dangle= 10*M_PI/180.0f;
1009         dampli= 0.2*tampli;
1010         dangle2= 5.0f*M_PI/180.0f;
1011         dampli2= 0.5f*dampli;
1012         glBegin(GL_LINE_STRIP);
1013         glVertex2f(polar_to_x(centerx,diam,tampli+dampli-dampli2,tangle+dangle), polar_to_y(centery,diam,tampli+dampli-dampli2,tangle+dangle));
1014         glVertex2f(polar_to_x(centerx,diam,tampli+dampli,tangle+dangle), polar_to_y(centery,diam,tampli+dampli,tangle+dangle));
1015         glVertex2f(polar_to_x(centerx,diam,tampli+dampli,tangle+dangle-dangle2), polar_to_y(centery,diam,tampli+dampli,tangle+dangle-dangle2));
1016         glEnd();
1017         glBegin(GL_LINE_STRIP);
1018         glVertex2f(polar_to_x(centerx,diam,tampli-dampli+dampli2,tangle+dangle), polar_to_y(centery ,diam,tampli-dampli+dampli2,tangle+dangle));
1019         glVertex2f(polar_to_x(centerx,diam,tampli-dampli,tangle+dangle), polar_to_y(centery,diam,tampli-dampli,tangle+dangle));
1020         glVertex2f(polar_to_x(centerx,diam,tampli-dampli,tangle+dangle-dangle2), polar_to_y(centery,diam,tampli-dampli,tangle+dangle-dangle2));
1021         glEnd();
1022         glBegin(GL_LINE_STRIP);
1023         glVertex2f(polar_to_x(centerx,diam,tampli-dampli+dampli2,tangle-dangle), polar_to_y(centery,diam,tampli-dampli+dampli2,tangle-dangle));
1024         glVertex2f(polar_to_x(centerx,diam,tampli-dampli,tangle-dangle), polar_to_y(centery,diam,tampli-dampli,tangle-dangle));
1025         glVertex2f(polar_to_x(centerx,diam,tampli-dampli,tangle-dangle+dangle2), polar_to_y(centery,diam,tampli-dampli,tangle-dangle+dangle2));
1026         glEnd();
1027         glBegin(GL_LINE_STRIP);
1028         glVertex2f(polar_to_x(centerx,diam,tampli+dampli-dampli2,tangle-dangle), polar_to_y(centery,diam,tampli+dampli-dampli2,tangle-dangle));
1029         glVertex2f(polar_to_x(centerx,diam,tampli+dampli,tangle-dangle), polar_to_y(centery,diam,tampli+dampli,tangle-dangle));
1030         glVertex2f(polar_to_x(centerx,diam,tampli+dampli,tangle-dangle+dangle2), polar_to_y(centery,diam,tampli+dampli,tangle-dangle+dangle2));
1031         glEnd();
1032 }
1033
1034 void ui_draw_but_VECTORSCOPE(ARegion *ar, uiBut *but, uiWidgetColors *UNUSED(wcol), rcti *recti)
1035 {
1036         Scopes *scopes = (Scopes *)but->poin;
1037         rctf rect;
1038         int i, j;
1039         int skina= 123; /* angle in degree of the skin tone line */
1040         float w, h, centerx, centery, diam;
1041         float alpha;
1042         float colors[6][3]={{.75,0,0},{.75,.75,0},{0,.75,0},{0,.75,.75},{0,0,.75},{.75,0,.75}};
1043         GLint scissor[4];
1044         
1045         rect.xmin = (float)recti->xmin+1;
1046         rect.xmax = (float)recti->xmax-1;
1047         rect.ymin = (float)recti->ymin+SCOPE_RESIZE_PAD+2;
1048         rect.ymax = (float)recti->ymax-1;
1049         
1050         w = rect.xmax - rect.xmin;
1051         h = rect.ymax - rect.ymin;
1052         centerx = rect.xmin + w/2;
1053         centery = rect.ymin + h/2;
1054         diam= (w<h)?w:h;
1055         
1056         alpha = scopes->vecscope_alpha*scopes->vecscope_alpha*scopes->vecscope_alpha;
1057                         
1058         glEnable(GL_BLEND);
1059         glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
1060         
1061         glColor4f(0.f, 0.f, 0.f, 0.3f);
1062         uiSetRoundBox(15);
1063         uiDrawBox(GL_POLYGON, rect.xmin-1, rect.ymin-1, rect.xmax+1, rect.ymax+1, 3.0f);
1064
1065         /* need scissor test, hvectorscope can draw outside of boundary */
1066         glGetIntegerv(GL_VIEWPORT, scissor);
1067         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));
1068         
1069         glColor4f(1.f, 1.f, 1.f, 0.08f);
1070         /* draw grid elements */
1071         /* cross */
1072         fdrawline(centerx - (diam/2)-5, centery, centerx + (diam/2)+5, centery);
1073         fdrawline(centerx, centery - (diam/2)-5, centerx, centery + (diam/2)+5);
1074         /* circles */
1075         for(j=0; j<5; j++) {
1076                 glBegin(GL_LINE_STRIP);
1077                 for(i=0; i<=360; i=i+15) {
1078                         float a= i*M_PI/180.0;
1079                         float r= (j+1)/10.0f;
1080                         glVertex2f( polar_to_x(centerx,diam,r,a), polar_to_y(centery,diam,r,a));
1081                 }
1082                 glEnd();
1083         }
1084         /* skin tone line */
1085         glColor4f(1.f, 0.4f, 0.f, 0.2f);
1086         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),
1087                                 polar_to_x(centerx, diam, 0.1f, skina*M_PI/180.0), polar_to_y(centery,diam,0.1,skina*M_PI/180.0));
1088         /* saturation points */
1089         for(i=0; i<6; i++)
1090                 vectorscope_draw_target(centerx, centery, diam, colors[i][0], colors[i][1], colors[i][2]);
1091         
1092         if (scopes->ok && scopes->vecscope != NULL) {
1093                 /* pixel point cloud */
1094                 glBlendFunc(GL_ONE,GL_ONE);
1095                 glColor3f(alpha, alpha, alpha);
1096
1097                 glPushMatrix();
1098                 glEnableClientState(GL_VERTEX_ARRAY);
1099
1100                 glTranslatef(centerx, centery, 0.f);
1101                 glScalef(diam, diam, 0.f);
1102
1103                 glVertexPointer(2, GL_FLOAT, 0, scopes->vecscope);
1104                 glDrawArrays(GL_POINTS, 0, scopes->waveform_tot);
1105                 
1106                 glDisableClientState(GL_VERTEX_ARRAY);
1107                 glPopMatrix();
1108         }
1109
1110         /* outline, scale gripper */
1111         draw_scope_end(&rect, scissor);
1112                 
1113         glDisable(GL_BLEND);
1114 }
1115
1116 void ui_draw_but_COLORBAND(uiBut *but, uiWidgetColors *UNUSED(wcol), rcti *rect)
1117 {
1118         ColorBand *coba;
1119         CBData *cbd;
1120         float x1, y1, sizex, sizey;
1121         float dx, v3[2], v1[2], v2[2], v1a[2], v2a[2];
1122         int a;
1123         float pos, colf[4]= {0,0,0,0}; /* initialize incase the colorband isnt valid */
1124                 
1125         coba= (ColorBand *)(but->editcoba? but->editcoba: but->poin);
1126         if(coba==NULL) return;
1127         
1128         x1= rect->xmin;
1129         y1= rect->ymin;
1130         sizex= rect->xmax-x1;
1131         sizey= rect->ymax-y1;
1132         
1133         /* first background, to show tranparency */
1134         dx= sizex/12.0;
1135         v1[0]= x1;
1136         for(a=0; a<12; a++) {
1137                 if(a & 1) glColor3f(0.3, 0.3, 0.3); else glColor3f(0.8, 0.8, 0.8);
1138                 glRectf(v1[0], y1, v1[0]+dx, y1+0.5*sizey);
1139                 if(a & 1) glColor3f(0.8, 0.8, 0.8); else glColor3f(0.3, 0.3, 0.3);
1140                 glRectf(v1[0], y1+0.5*sizey, v1[0]+dx, y1+sizey);
1141                 v1[0]+= dx;
1142         }
1143         
1144         glShadeModel(GL_FLAT);
1145         glEnable(GL_BLEND);
1146         
1147         cbd= coba->data;
1148         
1149         v1[0]= v2[0]= x1;
1150         v1[1]= y1;
1151         v2[1]= y1+sizey;
1152         
1153         glBegin(GL_QUAD_STRIP);
1154         
1155         glColor4fv( &cbd->r );
1156         glVertex2fv(v1); glVertex2fv(v2);
1157         
1158         for( a = 1; a <= sizex; a++ ) {
1159                 pos = ((float)a) / (sizex-1);
1160                 do_colorband( coba, pos, colf );
1161                 if (but->block->color_profile != BLI_PR_NONE)
1162                         linearrgb_to_srgb_v3_v3(colf, colf);
1163                 
1164                 v1[0]=v2[0]= x1 + a;
1165                 
1166                 glColor4fv( colf );
1167                 glVertex2fv(v1); glVertex2fv(v2);
1168         }
1169         
1170         glEnd();
1171         glShadeModel(GL_FLAT);
1172         glDisable(GL_BLEND);
1173         
1174         /* outline */
1175         glColor4f(0.0, 0.0, 0.0, 1.0);
1176         fdrawbox(x1, y1, x1+sizex, y1+sizey);
1177         
1178         /* help lines */
1179         v1[0]= v2[0]=v3[0]= x1;
1180         v1[1]= y1;
1181         v1a[1]= y1+0.25*sizey;
1182         v2[1]= y1+0.5*sizey;
1183         v2a[1]= y1+0.75*sizey;
1184         v3[1]= y1+sizey;
1185         
1186         
1187         cbd= coba->data;
1188         glBegin(GL_LINES);
1189         for(a=0; a<coba->tot; a++, cbd++) {
1190                 v1[0]=v2[0]=v3[0]=v1a[0]=v2a[0]= x1+ cbd->pos*sizex;
1191                 
1192                 if(a==coba->cur) {
1193                         glColor3ub(0, 0, 0);
1194                         glVertex2fv(v1);
1195                         glVertex2fv(v3);
1196                         glEnd();
1197                         
1198                         setlinestyle(2);
1199                         glBegin(GL_LINES);
1200                         glColor3ub(255, 255, 255);
1201                         glVertex2fv(v1);
1202                         glVertex2fv(v3);
1203                         glEnd();
1204                         setlinestyle(0);
1205                         glBegin(GL_LINES);
1206                         
1207                         /* glColor3ub(0, 0, 0);
1208                         glVertex2fv(v1);
1209                         glVertex2fv(v1a);
1210                         glColor3ub(255, 255, 255);
1211                         glVertex2fv(v1a);
1212                         glVertex2fv(v2);
1213                         glColor3ub(0, 0, 0);
1214                         glVertex2fv(v2);
1215                         glVertex2fv(v2a);
1216                         glColor3ub(255, 255, 255);
1217                         glVertex2fv(v2a);
1218                         glVertex2fv(v3);
1219                         */
1220                 }
1221                 else {
1222                         glColor3ub(0, 0, 0);
1223                         glVertex2fv(v1);
1224                         glVertex2fv(v2);
1225                         
1226                         glColor3ub(255, 255, 255);
1227                         glVertex2fv(v2);
1228                         glVertex2fv(v3);
1229                 }       
1230         }
1231         glEnd();
1232
1233 }
1234
1235 void ui_draw_but_NORMAL(uiBut *but, uiWidgetColors *wcol, rcti *rect)
1236 {
1237         static GLuint displist=0;
1238         int a, old[8];
1239         GLfloat diff[4], diffn[4]={1.0f, 1.0f, 1.0f, 1.0f};
1240         float vec0[4]={0.0f, 0.0f, 0.0f, 0.0f};
1241         float dir[4], size;
1242         
1243         /* store stuff */
1244         glGetMaterialfv(GL_FRONT, GL_DIFFUSE, diff);
1245                 
1246         /* backdrop */
1247         glColor3ubv((unsigned char*)wcol->inner);
1248         uiSetRoundBox(15);
1249         uiDrawBox(GL_POLYGON, rect->xmin, rect->ymin, rect->xmax, rect->ymax, 5.0f);
1250         
1251         /* sphere color */
1252         glMaterialfv(GL_FRONT, GL_DIFFUSE, diffn);
1253         glCullFace(GL_BACK); glEnable(GL_CULL_FACE);
1254         
1255         /* disable blender light */
1256         for(a=0; a<8; a++) {
1257                 old[a]= glIsEnabled(GL_LIGHT0+a);
1258                 glDisable(GL_LIGHT0+a);
1259         }
1260         
1261         /* own light */
1262         glEnable(GL_LIGHT7);
1263         glEnable(GL_LIGHTING);
1264         
1265         ui_get_but_vectorf(but, dir);
1266
1267         dir[3]= 0.0f;   /* glLight needs 4 args, 0.0 is sun */
1268         glLightfv(GL_LIGHT7, GL_POSITION, dir); 
1269         glLightfv(GL_LIGHT7, GL_DIFFUSE, diffn); 
1270         glLightfv(GL_LIGHT7, GL_SPECULAR, vec0); 
1271         glLightf(GL_LIGHT7, GL_CONSTANT_ATTENUATION, 1.0f);
1272         glLightf(GL_LIGHT7, GL_LINEAR_ATTENUATION, 0.0f);
1273         
1274         /* transform to button */
1275         glPushMatrix();
1276         glTranslatef(rect->xmin + 0.5f*(rect->xmax-rect->xmin), rect->ymin+ 0.5f*(rect->ymax-rect->ymin), 0.0f);
1277         
1278         if( rect->xmax-rect->xmin < rect->ymax-rect->ymin)
1279                 size= (rect->xmax-rect->xmin)/200.f;
1280         else
1281                 size= (rect->ymax-rect->ymin)/200.f;
1282         
1283         glScalef(size, size, size);
1284         
1285         if(displist==0) {
1286                 GLUquadricObj   *qobj;
1287                 
1288                 displist= glGenLists(1);
1289                 glNewList(displist, GL_COMPILE_AND_EXECUTE);
1290                 
1291                 qobj= gluNewQuadric();
1292                 gluQuadricDrawStyle(qobj, GLU_FILL); 
1293                 glShadeModel(GL_SMOOTH);
1294                 gluSphere( qobj, 100.0, 32, 24);
1295                 glShadeModel(GL_FLAT);
1296                 gluDeleteQuadric(qobj);  
1297                 
1298                 glEndList();
1299         }
1300         else glCallList(displist);
1301         
1302         /* restore */
1303         glDisable(GL_LIGHTING);
1304         glDisable(GL_CULL_FACE);
1305         glMaterialfv(GL_FRONT, GL_DIFFUSE, diff); 
1306         glDisable(GL_LIGHT7);
1307         
1308         /* AA circle */
1309         glEnable(GL_BLEND);
1310         glEnable(GL_LINE_SMOOTH );
1311         glColor3ubv((unsigned char*)wcol->inner);
1312         glutil_draw_lined_arc(0.0f, M_PI*2.0, 100.0f, 32);
1313         glDisable(GL_BLEND);
1314         glDisable(GL_LINE_SMOOTH );
1315
1316         /* matrix after circle */
1317         glPopMatrix();
1318
1319         /* enable blender light */
1320         for(a=0; a<8; a++) {
1321                 if(old[a])
1322                         glEnable(GL_LIGHT0+a);
1323         }
1324 }
1325
1326 static void ui_draw_but_curve_grid(rcti *rect, float zoomx, float zoomy, float offsx, float offsy, float step)
1327 {
1328         float dx, dy, fx, fy;
1329         
1330         glBegin(GL_LINES);
1331         dx= step*zoomx;
1332         fx= rect->xmin + zoomx*(-offsx);
1333         if(fx > rect->xmin) fx -= dx*( floor(fx-rect->xmin));
1334         while(fx < rect->xmax) {
1335                 glVertex2f(fx, rect->ymin); 
1336                 glVertex2f(fx, rect->ymax);
1337                 fx+= dx;
1338         }
1339         
1340         dy= step*zoomy;
1341         fy= rect->ymin + zoomy*(-offsy);
1342         if(fy > rect->ymin) fy -= dy*( floor(fy-rect->ymin));
1343         while(fy < rect->ymax) {
1344                 glVertex2f(rect->xmin, fy); 
1345                 glVertex2f(rect->xmax, fy);
1346                 fy+= dy;
1347         }
1348         glEnd();
1349         
1350 }
1351
1352 static void glColor3ubvShade(unsigned char *col, int shade)
1353 {
1354         glColor3ub(col[0]-shade>0?col[0]-shade:0, 
1355                            col[1]-shade>0?col[1]-shade:0,
1356                            col[2]-shade>0?col[2]-shade:0);
1357 }
1358
1359 void ui_draw_but_CURVE(ARegion *ar, uiBut *but, uiWidgetColors *wcol, rcti *rect)
1360 {
1361         CurveMapping *cumap;
1362         CurveMap *cuma;
1363         CurveMapPoint *cmp;
1364         float fx, fy, fac[2], zoomx, zoomy, offsx, offsy;
1365         GLint scissor[4];
1366         rcti scissor_new;
1367         int a;
1368
1369         cumap= (CurveMapping *)(but->editcumap? but->editcumap: but->poin);
1370         cuma= cumap->cm+cumap->cur;
1371         
1372         /* need scissor test, curve can draw outside of boundary */
1373         glGetIntegerv(GL_VIEWPORT, scissor);
1374         scissor_new.xmin= ar->winrct.xmin + rect->xmin;
1375         scissor_new.ymin= ar->winrct.ymin + rect->ymin;
1376         scissor_new.xmax= ar->winrct.xmin + rect->xmax;
1377         scissor_new.ymax= ar->winrct.ymin + rect->ymax;
1378         BLI_isect_rcti(&scissor_new, &ar->winrct, &scissor_new);
1379         glScissor(scissor_new.xmin, scissor_new.ymin, scissor_new.xmax-scissor_new.xmin, scissor_new.ymax-scissor_new.ymin);
1380         
1381         /* calculate offset and zoom */
1382         zoomx= (rect->xmax-rect->xmin-2.0*but->aspect)/(cumap->curr.xmax - cumap->curr.xmin);
1383         zoomy= (rect->ymax-rect->ymin-2.0*but->aspect)/(cumap->curr.ymax - cumap->curr.ymin);
1384         offsx= cumap->curr.xmin-but->aspect/zoomx;
1385         offsy= cumap->curr.ymin-but->aspect/zoomy;
1386         
1387         /* backdrop */
1388         if(cumap->flag & CUMA_DO_CLIP) {
1389                 glColor3ubvShade((unsigned char *)wcol->inner, -20);
1390                 glRectf(rect->xmin, rect->ymin, rect->xmax, rect->ymax);
1391                 glColor3ubv((unsigned char*)wcol->inner);
1392                 glRectf(rect->xmin + zoomx*(cumap->clipr.xmin-offsx),
1393                                 rect->ymin + zoomy*(cumap->clipr.ymin-offsy),
1394                                 rect->xmin + zoomx*(cumap->clipr.xmax-offsx),
1395                                 rect->ymin + zoomy*(cumap->clipr.ymax-offsy));
1396         }
1397         else {
1398                 glColor3ubv((unsigned char*)wcol->inner);
1399                 glRectf(rect->xmin, rect->ymin, rect->xmax, rect->ymax);
1400         }
1401                 
1402         /* grid, every .25 step */
1403         glColor3ubvShade((unsigned char *)wcol->inner, -16);
1404         ui_draw_but_curve_grid(rect, zoomx, zoomy, offsx, offsy, 0.25f);
1405         /* grid, every 1.0 step */
1406         glColor3ubvShade((unsigned char *)wcol->inner, -24);
1407         ui_draw_but_curve_grid(rect, zoomx, zoomy, offsx, offsy, 1.0f);
1408         /* axes */
1409         glColor3ubvShade((unsigned char *)wcol->inner, -50);
1410         glBegin(GL_LINES);
1411         glVertex2f(rect->xmin, rect->ymin + zoomy*(-offsy));
1412         glVertex2f(rect->xmax, rect->ymin + zoomy*(-offsy));
1413         glVertex2f(rect->xmin + zoomx*(-offsx), rect->ymin);
1414         glVertex2f(rect->xmin + zoomx*(-offsx), rect->ymax);
1415         glEnd();
1416         
1417         /* magic trigger for curve backgrounds */
1418         if (but->a1 != -1) {
1419                 if (but->a1 == UI_GRAD_H) {
1420                         rcti grid;
1421                         float col[3]= {0.0f, 0.0f, 0.0f}; /* dummy arg */
1422                         
1423                         grid.xmin = rect->xmin + zoomx*(-offsx);
1424                         grid.xmax = rect->xmax + zoomx*(-offsx);
1425                         grid.ymin = rect->ymin + zoomy*(-offsy);
1426                         grid.ymax = rect->ymax + zoomy*(-offsy);
1427                         
1428                         glEnable(GL_BLEND);
1429                         ui_draw_gradient(&grid, col, UI_GRAD_H, 0.5f);
1430                         glDisable(GL_BLEND);
1431                 }
1432         }
1433         
1434         
1435         /* cfra option */
1436         /* XXX 2.48
1437         if(cumap->flag & CUMA_DRAW_CFRA) {
1438                 glColor3ub(0x60, 0xc0, 0x40);
1439                 glBegin(GL_LINES);
1440                 glVertex2f(rect->xmin + zoomx*(cumap->sample[0]-offsx), rect->ymin);
1441                 glVertex2f(rect->xmin + zoomx*(cumap->sample[0]-offsx), rect->ymax);
1442                 glEnd();
1443         }*/
1444         /* sample option */
1445         /* XXX 2.48
1446          * if(cumap->flag & CUMA_DRAW_SAMPLE) {
1447                 if(cumap->cur==3) {
1448                         float lum= cumap->sample[0]*0.35f + cumap->sample[1]*0.45f + cumap->sample[2]*0.2f;
1449                         glColor3ub(240, 240, 240);
1450                         
1451                         glBegin(GL_LINES);
1452                         glVertex2f(rect->xmin + zoomx*(lum-offsx), rect->ymin);
1453                         glVertex2f(rect->xmin + zoomx*(lum-offsx), rect->ymax);
1454                         glEnd();
1455                 }
1456                 else {
1457                         if(cumap->cur==0)
1458                                 glColor3ub(240, 100, 100);
1459                         else if(cumap->cur==1)
1460                                 glColor3ub(100, 240, 100);
1461                         else
1462                                 glColor3ub(100, 100, 240);
1463                         
1464                         glBegin(GL_LINES);
1465                         glVertex2f(rect->xmin + zoomx*(cumap->sample[cumap->cur]-offsx), rect->ymin);
1466                         glVertex2f(rect->xmin + zoomx*(cumap->sample[cumap->cur]-offsx), rect->ymax);
1467                         glEnd();
1468                 }
1469         }*/
1470         
1471         /* the curve */
1472         glColor3ubv((unsigned char*)wcol->item);
1473         glEnable(GL_LINE_SMOOTH);
1474         glEnable(GL_BLEND);
1475         glBegin(GL_LINE_STRIP);
1476         
1477         if(cuma->table==NULL)
1478                 curvemapping_changed(cumap, 0); /* 0 = no remove doubles */
1479         cmp= cuma->table;
1480         
1481         /* first point */
1482         if((cuma->flag & CUMA_EXTEND_EXTRAPOLATE)==0)
1483                 glVertex2f(rect->xmin, rect->ymin + zoomy*(cmp[0].y-offsy));
1484         else {
1485                 fx= rect->xmin + zoomx*(cmp[0].x-offsx + cuma->ext_in[0]);
1486                 fy= rect->ymin + zoomy*(cmp[0].y-offsy + cuma->ext_in[1]);
1487                 glVertex2f(fx, fy);
1488         }
1489         for(a=0; a<=CM_TABLE; a++) {
1490                 fx= rect->xmin + zoomx*(cmp[a].x-offsx);
1491                 fy= rect->ymin + zoomy*(cmp[a].y-offsy);
1492                 glVertex2f(fx, fy);
1493         }
1494         /* last point */
1495         if((cuma->flag & CUMA_EXTEND_EXTRAPOLATE)==0)
1496                 glVertex2f(rect->xmax, rect->ymin + zoomy*(cmp[CM_TABLE].y-offsy));     
1497         else {
1498                 fx= rect->xmin + zoomx*(cmp[CM_TABLE].x-offsx - cuma->ext_out[0]);
1499                 fy= rect->ymin + zoomy*(cmp[CM_TABLE].y-offsy - cuma->ext_out[1]);
1500                 glVertex2f(fx, fy);
1501         }
1502         glEnd();
1503         glDisable(GL_LINE_SMOOTH);
1504         glDisable(GL_BLEND);
1505
1506         /* the points, use aspect to make them visible on edges */
1507         cmp= cuma->curve;
1508         glPointSize(3.0f);
1509         bglBegin(GL_POINTS);
1510         for(a=0; a<cuma->totpoint; a++) {
1511                 if(cmp[a].flag & SELECT)
1512                         UI_ThemeColor(TH_TEXT_HI);
1513                 else
1514                         UI_ThemeColor(TH_TEXT);
1515                 fac[0]= rect->xmin + zoomx*(cmp[a].x-offsx);
1516                 fac[1]= rect->ymin + zoomy*(cmp[a].y-offsy);
1517                 bglVertex2fv(fac);
1518         }
1519         bglEnd();
1520         glPointSize(1.0f);
1521         
1522         /* restore scissortest */
1523         glScissor(scissor[0], scissor[1], scissor[2], scissor[3]);
1524
1525         /* outline */
1526         glColor3ubv((unsigned char*)wcol->outline);
1527         fdrawbox(rect->xmin, rect->ymin, rect->xmax, rect->ymax);
1528 }
1529
1530
1531 /* ****************************************************** */
1532
1533
1534 static void ui_shadowbox(float minx, float miny, float maxx, float maxy, float shadsize, unsigned char alpha)
1535 {
1536         glEnable(GL_BLEND);
1537         glShadeModel(GL_SMOOTH);
1538         
1539         /* right quad */
1540         glBegin(GL_POLYGON);
1541         glColor4ub(0, 0, 0, alpha);
1542         glVertex2f(maxx, miny);
1543         glVertex2f(maxx, maxy-0.3*shadsize);
1544         glColor4ub(0, 0, 0, 0);
1545         glVertex2f(maxx+shadsize, maxy-0.75*shadsize);
1546         glVertex2f(maxx+shadsize, miny);
1547         glEnd();
1548         
1549         /* corner shape */
1550         glBegin(GL_POLYGON);
1551         glColor4ub(0, 0, 0, alpha);
1552         glVertex2f(maxx, miny);
1553         glColor4ub(0, 0, 0, 0);
1554         glVertex2f(maxx+shadsize, miny);
1555         glVertex2f(maxx+0.7*shadsize, miny-0.7*shadsize);
1556         glVertex2f(maxx, miny-shadsize);
1557         glEnd();
1558         
1559         /* bottom quad */               
1560         glBegin(GL_POLYGON);
1561         glColor4ub(0, 0, 0, alpha);
1562         glVertex2f(minx+0.3*shadsize, miny);
1563         glVertex2f(maxx, miny);
1564         glColor4ub(0, 0, 0, 0);
1565         glVertex2f(maxx, miny-shadsize);
1566         glVertex2f(minx+0.5*shadsize, miny-shadsize);
1567         glEnd();
1568         
1569         glDisable(GL_BLEND);
1570         glShadeModel(GL_FLAT);
1571 }
1572
1573 void uiDrawBoxShadow(unsigned char alpha, float minx, float miny, float maxx, float maxy)
1574 {
1575         /* accumulated outline boxes to make shade not linear, is more pleasant */
1576         ui_shadowbox(minx, miny, maxx, maxy, 11.0, (20*alpha)>>8);
1577         ui_shadowbox(minx, miny, maxx, maxy, 7.0, (40*alpha)>>8);
1578         ui_shadowbox(minx, miny, maxx, maxy, 5.0, (80*alpha)>>8);
1579         
1580 }
1581
1582
1583 void ui_dropshadow(rctf *rct, float radius, float aspect, int select)
1584 {
1585         int i;
1586         float rad;
1587         float a;
1588         char alpha= 2;
1589         
1590         glEnable(GL_BLEND);
1591         
1592         if(radius > (rct->ymax-rct->ymin-10.0f)/2.0f)
1593                 rad= (rct->ymax-rct->ymin-10.0f)/2.0f;
1594         else
1595                 rad= radius;
1596
1597         i= 12;
1598         if(select) a= i*aspect; else a= i*aspect;
1599         for(; i--; a-=aspect) {
1600                 /* alpha ranges from 2 to 20 or so */
1601                 glColor4ub(0, 0, 0, alpha);
1602                 alpha+= 2;
1603                 
1604                 uiDrawBox(GL_POLYGON, rct->xmin - a, rct->ymin - a, rct->xmax + a, rct->ymax-10.0f + a, rad+a);
1605         }
1606         
1607         /* outline emphasis */
1608         glEnable( GL_LINE_SMOOTH );
1609         glColor4ub(0, 0, 0, 100);
1610         uiDrawBox(GL_LINE_LOOP, rct->xmin-0.5f, rct->ymin-0.5f, rct->xmax+0.5f, rct->ymax+0.5f, radius+0.5f);
1611         glDisable( GL_LINE_SMOOTH );
1612         
1613         glDisable(GL_BLEND);
1614 }
1615