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