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