fix [#27139] Value Slider, some colours are two coloured
[blender-staging.git] / source / blender / editors / interface / interface_widgets.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) 2009 Blender Foundation.
19  * All rights reserved.
20  * 
21  * Contributor(s): Blender Foundation
22  *
23  * ***** END GPL LICENSE BLOCK *****
24  */
25
26 /** \file blender/editors/interface/interface_widgets.c
27  *  \ingroup edinterface
28  */
29
30
31 #include <limits.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <assert.h>
35
36
37 #include "DNA_screen_types.h"
38 #include "DNA_userdef_types.h"
39
40 #include "BLI_math.h"
41 #include "BLI_listbase.h"
42 #include "BLI_rect.h"
43 #include "BLI_string.h"
44 #include "BLI_utildefines.h"
45
46 #include "BKE_context.h"
47 #include "BKE_curve.h"
48 #include "BKE_utildefines.h"
49
50 #include "RNA_access.h"
51
52 #include "BIF_gl.h"
53 #include "BIF_glutil.h"
54
55 #include "BLF_api.h"
56
57 #include "UI_interface.h"
58 #include "UI_interface_icons.h"
59
60
61 #include "interface_intern.h"
62
63 /* ************** widget base functions ************** */
64 /*
65          - in: roundbox codes for corner types and radius
66          - return: array of [size][2][x,y] points, the edges of the roundbox, + UV coords
67  
68          - draw black box with alpha 0 on exact button boundbox
69          - for ever AA step:
70                 - draw the inner part for a round filled box, with color blend codes or texture coords
71                 - draw outline in outline color
72                 - draw outer part, bottom half, extruded 1 pixel to bottom, for emboss shadow
73                 - draw extra decorations
74          - draw background color box with alpha 1 on exact button boundbox
75  
76  */
77
78 /* fill this struct with polygon info to draw AA'ed */
79 /* it has outline, back, and two optional tria meshes */
80
81 typedef struct uiWidgetTrias {
82         int tot;
83         
84         float vec[32][2];
85         int (*index)[3];
86         
87 } uiWidgetTrias;
88
89 typedef struct uiWidgetBase {
90         
91         int totvert, halfwayvert;
92         float outer_v[64][2];
93         float inner_v[64][2];
94         float inner_uv[64][2];
95         
96         short inner, outline, emboss; /* set on/off */
97         short shadedir;
98         
99         uiWidgetTrias tria1;
100         uiWidgetTrias tria2;
101         
102 } uiWidgetBase;
103
104 /* uiWidgetType: for time being only for visual appearance,
105    later, a handling callback can be added too 
106 */
107 typedef struct uiWidgetType {
108         
109         /* pointer to theme color definition */
110         uiWidgetColors *wcol_theme;
111         uiWidgetStateColors *wcol_state;
112         
113         /* converted colors for state */
114         uiWidgetColors wcol;
115         
116         void (*state)(struct uiWidgetType *, int state);
117         void (*draw)(uiWidgetColors *, rcti *, int state, int roundboxalign);
118         void (*custom)(uiBut *, uiWidgetColors *, rcti *, int state, int roundboxalign);
119         void (*text)(uiFontStyle *, uiWidgetColors *, uiBut *, rcti *);
120         
121 } uiWidgetType;
122
123
124 /* *********************** draw data ************************** */
125
126 static float cornervec[9][2]= {{0.0, 0.0}, {0.195, 0.02}, {0.383, 0.067}, {0.55, 0.169}, 
127 {0.707, 0.293}, {0.831, 0.45}, {0.924, 0.617}, {0.98, 0.805}, {1.0, 1.0}};
128
129 static float jit[8][2]= {{0.468813 , -0.481430}, {-0.155755 , -0.352820}, 
130 {0.219306 , -0.238501},  {-0.393286 , -0.110949}, {-0.024699 , 0.013908}, 
131 {0.343805 , 0.147431}, {-0.272855 , 0.269918}, {0.095909 , 0.388710}};
132
133 static float num_tria_vert[3][2]= { 
134 {-0.352077, 0.532607}, {-0.352077, -0.549313}, {0.330000, -0.008353}};
135
136 static int num_tria_face[1][3]= {
137 {0, 1, 2}};
138
139 static float scroll_circle_vert[16][2]= {
140 {0.382684, 0.923879}, {0.000001, 1.000000}, {-0.382683, 0.923880}, {-0.707107, 0.707107},
141 {-0.923879, 0.382684}, {-1.000000, 0.000000}, {-0.923880, -0.382684}, {-0.707107, -0.707107},
142 {-0.382683, -0.923880}, {0.000000, -1.000000}, {0.382684, -0.923880}, {0.707107, -0.707107},
143 {0.923880, -0.382684}, {1.000000, -0.000000}, {0.923880, 0.382683}, {0.707107, 0.707107}};
144
145 static int scroll_circle_face[14][3]= {
146 {0, 1, 2}, {2, 0, 3}, {3, 0, 15}, {3, 15, 4}, {4, 15, 14}, {4, 14, 5}, {5, 14, 13}, {5, 13, 6}, 
147 {6, 13, 12}, {6, 12, 7}, {7, 12, 11}, {7, 11, 8}, {8, 11, 10}, {8, 10, 9}};
148
149 static float menu_tria_vert[6][2]= {
150 {-0.41, 0.16}, {0.41, 0.16}, {0, 0.82}, 
151 {0, -0.82}, {-0.41, -0.16}, {0.41, -0.16}};
152
153 static int menu_tria_face[2][3]= {{2, 0, 1}, {3, 5, 4}};
154
155 static float check_tria_vert[6][2]= {
156 {-0.578579, 0.253369},  {-0.392773, 0.412794},  {-0.004241, -0.328551}, 
157 {-0.003001, 0.034320},  {1.055313, 0.864744},   {0.866408, 1.026895}};
158
159 static int check_tria_face[4][3]= {
160 {3, 2, 4}, {3, 4, 5}, {1, 0, 3}, {0, 2, 3}};
161
162 /* ************************************************* */
163
164 void ui_draw_anti_tria(float x1, float y1, float x2, float y2, float x3, float y3)
165 {
166         float color[4];
167         int j;
168         
169         glEnable(GL_BLEND);
170         glGetFloatv(GL_CURRENT_COLOR, color);
171         color[3]= 0.125;
172         glColor4fv(color);
173         
174         /* for each AA step */
175         for(j=0; j<8; j++) {
176                 glTranslatef(1.0f * jit[j][0], 1.0f * jit[j][1], 0.0f);
177
178                 glBegin(GL_POLYGON);
179                 glVertex2f(x1, y1);
180                 glVertex2f(x2, y2);
181                 glVertex2f(x3, y3);
182                 glEnd();
183                 
184                 glTranslatef(-1.0f * jit[j][0], -1.0f * jit[j][1], 0.0f);
185         }
186
187         glDisable(GL_BLEND);
188         
189 }
190
191 static void widget_init(uiWidgetBase *wtb)
192 {
193         wtb->totvert= wtb->halfwayvert= 0;
194         wtb->tria1.tot= 0;
195         wtb->tria2.tot= 0;
196         
197         wtb->inner= 1;
198         wtb->outline= 1;
199         wtb->emboss= 1;
200         wtb->shadedir= 1;
201 }
202
203 /* helper call, makes shadow rect, with 'sun' above menu, so only shadow to left/right/bottom */
204 /* return tot */
205 static int round_box_shadow_edges(float (*vert)[2], rcti *rect, float rad, int roundboxalign, float step)
206 {
207         float vec[9][2];
208         float minx, miny, maxx, maxy;
209         int a, tot= 0;
210         
211         rad+= step;
212         
213         if(2.0f*rad > rect->ymax-rect->ymin)
214                 rad= 0.5f*(rect->ymax-rect->ymin);
215         
216         minx= rect->xmin-step;
217         miny= rect->ymin-step;
218         maxx= rect->xmax+step;
219         maxy= rect->ymax+step;
220         
221         /* mult */
222         for(a=0; a<9; a++) {
223                 vec[a][0]= rad*cornervec[a][0]; 
224                 vec[a][1]= rad*cornervec[a][1]; 
225         }
226         
227         /* start with left-top, anti clockwise */
228         if(roundboxalign & 1) {
229                 for(a=0; a<9; a++, tot++) {
230                         vert[tot][0]= minx+rad-vec[a][0];
231                         vert[tot][1]= maxy-vec[a][1];
232                 }
233         }
234         else {
235                 for(a=0; a<9; a++, tot++) {
236                         vert[tot][0]= minx;
237                         vert[tot][1]= maxy;
238                 }
239         }
240         
241         if(roundboxalign & 8) {
242                 for(a=0; a<9; a++, tot++) {
243                         vert[tot][0]= minx+vec[a][1];
244                         vert[tot][1]= miny+rad-vec[a][0];
245                 }
246         }
247         else {
248                 for(a=0; a<9; a++, tot++) {
249                         vert[tot][0]= minx;
250                         vert[tot][1]= miny;
251                 }
252         }
253         
254         if(roundboxalign & 4) {
255                 for(a=0; a<9; a++, tot++) {
256                         vert[tot][0]= maxx-rad+vec[a][0];
257                         vert[tot][1]= miny+vec[a][1];
258                 }
259         }
260         else {
261                 for(a=0; a<9; a++, tot++) {
262                         vert[tot][0]= maxx;
263                         vert[tot][1]= miny;
264                 }
265         }
266         
267         if(roundboxalign & 2) {
268                 for(a=0; a<9; a++, tot++) {
269                         vert[tot][0]= maxx-vec[a][1];
270                         vert[tot][1]= maxy-rad+vec[a][0];
271                 }
272         }
273         else {
274                 for(a=0; a<9; a++, tot++) {
275                         vert[tot][0]= maxx;
276                         vert[tot][1]= maxy;
277                 }
278         }
279         return tot;
280 }
281
282 /* this call has 1 extra arg to allow mask outline */
283 static void round_box__edges(uiWidgetBase *wt, int roundboxalign, rcti *rect, float rad, float radi)
284 {
285         float vec[9][2], veci[9][2];
286         float minx= rect->xmin, miny= rect->ymin, maxx= rect->xmax, maxy= rect->ymax;
287         float minxi= minx + 1.0f; /* boundbox inner */
288         float maxxi= maxx - 1.0f;
289         float minyi= miny + 1.0f;
290         float maxyi= maxy - 1.0f;
291         float facxi= (maxxi!=minxi) ? 1.0f/(maxxi-minxi) : 0.0f; /* for uv, can divide by zero */
292         float facyi= (maxyi!=minyi) ? 1.0f/(maxyi-minyi) : 0.0f;
293         int a, tot= 0, minsize;
294         const int hnum= ((roundboxalign & (1|2))==(1|2) || (roundboxalign & (4|8))==(4|8)) ? 1 : 2;
295         const int vnum= ((roundboxalign & (1|8))==(1|8) || (roundboxalign & (2|4))==(2|4)) ? 1 : 2;
296
297         minsize= MIN2((rect->xmax-rect->xmin)*hnum, (rect->ymax-rect->ymin)*vnum);
298         
299         if(2.0f*rad > minsize)
300                 rad= 0.5f*minsize;
301
302         if(2.0f*(radi+1.0f) > minsize)
303                 radi= 0.5f*minsize - 1.0f;
304         
305         /* mult */
306         for(a=0; a<9; a++) {
307                 veci[a][0]= radi*cornervec[a][0]; 
308                 veci[a][1]= radi*cornervec[a][1]; 
309                 vec[a][0]= rad*cornervec[a][0]; 
310                 vec[a][1]= rad*cornervec[a][1]; 
311         }
312         
313         /* corner left-bottom */
314         if(roundboxalign & 8) {
315                 
316                 for(a=0; a<9; a++, tot++) {
317                         wt->inner_v[tot][0]= minxi+veci[a][1];
318                         wt->inner_v[tot][1]= minyi+radi-veci[a][0];
319                         
320                         wt->outer_v[tot][0]= minx+vec[a][1];
321                         wt->outer_v[tot][1]= miny+rad-vec[a][0];
322                         
323                         wt->inner_uv[tot][0]= facxi*(wt->inner_v[tot][0] - minxi);
324                         wt->inner_uv[tot][1]= facyi*(wt->inner_v[tot][1] - minyi);
325                 }
326         }
327         else {
328                 wt->inner_v[tot][0]= minxi;
329                 wt->inner_v[tot][1]= minyi;
330                 
331                 wt->outer_v[tot][0]= minx;
332                 wt->outer_v[tot][1]= miny;
333
334                 wt->inner_uv[tot][0]= 0.0f;
335                 wt->inner_uv[tot][1]= 0.0f;
336                 
337                 tot++;
338         }
339         
340         /* corner right-bottom */
341         if(roundboxalign & 4) {
342                 
343                 for(a=0; a<9; a++, tot++) {
344                         wt->inner_v[tot][0]= maxxi-radi+veci[a][0];
345                         wt->inner_v[tot][1]= minyi+veci[a][1];
346                         
347                         wt->outer_v[tot][0]= maxx-rad+vec[a][0];
348                         wt->outer_v[tot][1]= miny+vec[a][1];
349                         
350                         wt->inner_uv[tot][0]= facxi*(wt->inner_v[tot][0] - minxi);
351                         wt->inner_uv[tot][1]= facyi*(wt->inner_v[tot][1] - minyi);
352                 }
353         }
354         else {
355                 wt->inner_v[tot][0]= maxxi;
356                 wt->inner_v[tot][1]= minyi;
357                 
358                 wt->outer_v[tot][0]= maxx;
359                 wt->outer_v[tot][1]= miny;
360
361                 wt->inner_uv[tot][0]= 1.0f;
362                 wt->inner_uv[tot][1]= 0.0f;
363                 
364                 tot++;
365         }
366         
367         wt->halfwayvert= tot;
368         
369         /* corner right-top */
370         if(roundboxalign & 2) {
371                 
372                 for(a=0; a<9; a++, tot++) {
373                         wt->inner_v[tot][0]= maxxi-veci[a][1];
374                         wt->inner_v[tot][1]= maxyi-radi+veci[a][0];
375                         
376                         wt->outer_v[tot][0]= maxx-vec[a][1];
377                         wt->outer_v[tot][1]= maxy-rad+vec[a][0];
378                         
379                         wt->inner_uv[tot][0]= facxi*(wt->inner_v[tot][0] - minxi);
380                         wt->inner_uv[tot][1]= facyi*(wt->inner_v[tot][1] - minyi);
381                 }
382         }
383         else {
384                 wt->inner_v[tot][0]= maxxi;
385                 wt->inner_v[tot][1]= maxyi;
386                 
387                 wt->outer_v[tot][0]= maxx;
388                 wt->outer_v[tot][1]= maxy;
389                 
390                 wt->inner_uv[tot][0]= 1.0f;
391                 wt->inner_uv[tot][1]= 1.0f;
392                 
393                 tot++;
394         }
395         
396         /* corner left-top */
397         if(roundboxalign & 1) {
398                 
399                 for(a=0; a<9; a++, tot++) {
400                         wt->inner_v[tot][0]= minxi+radi-veci[a][0];
401                         wt->inner_v[tot][1]= maxyi-veci[a][1];
402                         
403                         wt->outer_v[tot][0]= minx+rad-vec[a][0];
404                         wt->outer_v[tot][1]= maxy-vec[a][1];
405                         
406                         wt->inner_uv[tot][0]= facxi*(wt->inner_v[tot][0] - minxi);
407                         wt->inner_uv[tot][1]= facyi*(wt->inner_v[tot][1] - minyi);
408                 }
409                 
410         }
411         else {
412                 
413                 wt->inner_v[tot][0]= minxi;
414                 wt->inner_v[tot][1]= maxyi;
415                 
416                 wt->outer_v[tot][0]= minx;
417                 wt->outer_v[tot][1]= maxy;
418                 
419                 wt->inner_uv[tot][0]= 0.0f;
420                 wt->inner_uv[tot][1]= 1.0f;
421                 
422                 tot++;
423         }
424                 
425         wt->totvert= tot;
426 }
427
428 static void round_box_edges(uiWidgetBase *wt, int roundboxalign, rcti *rect, float rad)
429 {
430         round_box__edges(wt, roundboxalign, rect, rad, rad-1.0f);
431 }
432
433
434 /* based on button rect, return scaled array of triangles */
435 static void widget_num_tria(uiWidgetTrias *tria, rcti *rect, float triasize, char where)
436 {
437         float centx, centy, sizex, sizey, minsize;
438         int a, i1=0, i2=1;
439         
440         minsize= MIN2(rect->xmax-rect->xmin, rect->ymax-rect->ymin);
441         
442         /* center position and size */
443         centx= (float)rect->xmin + 0.5f*minsize;
444         centy= (float)rect->ymin + 0.5f*minsize;
445         sizex= sizey= -0.5f*triasize*minsize;
446
447         if(where=='r') {
448                 centx= (float)rect->xmax - 0.5f*minsize;
449                 sizex= -sizex;
450         }       
451         else if(where=='t') {
452                 centy= (float)rect->ymax - 0.5f*minsize;
453                 sizey= -sizey;
454                 i2=0; i1= 1;
455         }       
456         else if(where=='b') {
457                 sizex= -sizex;
458                 i2=0; i1= 1;
459         }       
460         
461         for(a=0; a<3; a++) {
462                 tria->vec[a][0]= sizex*num_tria_vert[a][i1] + centx;
463                 tria->vec[a][1]= sizey*num_tria_vert[a][i2] + centy;
464         }
465         
466         tria->tot= 1;
467         tria->index= num_tria_face;
468 }
469
470 static void widget_scroll_circle(uiWidgetTrias *tria, rcti *rect, float triasize, char where)
471 {
472         float centx, centy, sizex, sizey, minsize;
473         int a, i1=0, i2=1;
474         
475         minsize= MIN2(rect->xmax-rect->xmin, rect->ymax-rect->ymin);
476         
477         /* center position and size */
478         centx= (float)rect->xmin + 0.5f*minsize;
479         centy= (float)rect->ymin + 0.5f*minsize;
480         sizex= sizey= -0.5f*triasize*minsize;
481
482         if(where=='r') {
483                 centx= (float)rect->xmax - 0.5f*minsize;
484                 sizex= -sizex;
485         }       
486         else if(where=='t') {
487                 centy= (float)rect->ymax - 0.5f*minsize;
488                 sizey= -sizey;
489                 i2=0; i1= 1;
490         }       
491         else if(where=='b') {
492                 sizex= -sizex;
493                 i2=0; i1= 1;
494         }       
495         
496         for(a=0; a<16; a++) {
497                 tria->vec[a][0]= sizex*scroll_circle_vert[a][i1] + centx;
498                 tria->vec[a][1]= sizey*scroll_circle_vert[a][i2] + centy;
499         }
500         
501         tria->tot= 14;
502         tria->index= scroll_circle_face;
503 }
504
505 static void widget_trias_draw(uiWidgetTrias *tria)
506 {
507         int a;
508         
509         glBegin(GL_TRIANGLES);
510         for(a=0; a<tria->tot; a++) {
511                 glVertex2fv(tria->vec[ tria->index[a][0] ]);
512                 glVertex2fv(tria->vec[ tria->index[a][1] ]);
513                 glVertex2fv(tria->vec[ tria->index[a][2] ]);
514         }
515         glEnd();
516         
517 }
518
519 static void widget_menu_trias(uiWidgetTrias *tria, rcti *rect)
520 {
521         float centx, centy, size, asp;
522         int a;
523                 
524         /* center position and size */
525         centx= rect->xmax - 0.5f*(rect->ymax-rect->ymin);
526         centy= rect->ymin + 0.5f*(rect->ymax-rect->ymin);
527         size= 0.4f*(rect->ymax-rect->ymin);
528         
529         /* XXX exception */
530         asp= ((float)rect->xmax-rect->xmin)/((float)rect->ymax-rect->ymin);
531         if(asp > 1.2f && asp < 2.6f)
532                 centx= rect->xmax - 0.3f*(rect->ymax-rect->ymin);
533         
534         for(a=0; a<6; a++) {
535                 tria->vec[a][0]= size*menu_tria_vert[a][0] + centx;
536                 tria->vec[a][1]= size*menu_tria_vert[a][1] + centy;
537         }
538
539         tria->tot= 2;
540         tria->index= menu_tria_face;
541 }
542
543 static void widget_check_trias(uiWidgetTrias *tria, rcti *rect)
544 {
545         float centx, centy, size;
546         int a;
547         
548         /* center position and size */
549         centx= rect->xmin + 0.5f*(rect->ymax-rect->ymin);
550         centy= rect->ymin + 0.5f*(rect->ymax-rect->ymin);
551         size= 0.5f*(rect->ymax-rect->ymin);
552         
553         for(a=0; a<6; a++) {
554                 tria->vec[a][0]= size*check_tria_vert[a][0] + centx;
555                 tria->vec[a][1]= size*check_tria_vert[a][1] + centy;
556         }
557         
558         tria->tot= 4;
559         tria->index= check_tria_face;
560 }
561
562
563 /* prepares shade colors */
564 static void shadecolors4(char *coltop, char *coldown, const char *color, short shadetop, short shadedown)
565 {
566         
567         coltop[0]= CLAMPIS(color[0]+shadetop, 0, 255);
568         coltop[1]= CLAMPIS(color[1]+shadetop, 0, 255);
569         coltop[2]= CLAMPIS(color[2]+shadetop, 0, 255);
570         coltop[3]= color[3];
571
572         coldown[0]= CLAMPIS(color[0]+shadedown, 0, 255);
573         coldown[1]= CLAMPIS(color[1]+shadedown, 0, 255);
574         coldown[2]= CLAMPIS(color[2]+shadedown, 0, 255);
575         coldown[3]= color[3];   
576 }
577
578 static void round_box_shade_col4(const char col1[4], const char col2[4], const float fac)
579 {
580         unsigned char col[4];
581         const int faci= FTOCHAR(fac);
582         const int facm= 255-faci;
583
584         col[0]= (faci*col1[0] + facm*col2[0])>>8;
585         col[1]= (faci*col1[1] + facm*col2[1])>>8;
586         col[2]= (faci*col1[2] + facm*col2[2])>>8;
587         col[3]= (faci*col1[3] + facm*col2[3])>>8;
588
589         glColor4ubv(col);
590 }
591
592 static void widgetbase_outline(uiWidgetBase *wtb)
593 {
594         int a;
595         
596         /* outline */
597         glBegin(GL_QUAD_STRIP);
598         for(a=0; a<wtb->totvert; a++) {
599                 glVertex2fv(wtb->outer_v[a]);
600                 glVertex2fv(wtb->inner_v[a]);
601         }
602         glVertex2fv(wtb->outer_v[0]);
603         glVertex2fv(wtb->inner_v[0]);
604         glEnd();
605 }
606
607 static void widgetbase_draw(uiWidgetBase *wtb, uiWidgetColors *wcol)
608 {
609         int j, a;
610         
611         glEnable(GL_BLEND);
612
613         /* backdrop non AA */
614         if(wtb->inner) {
615                 if(wcol->shaded==0) {
616                         if (wcol->alpha_check) {
617                                 GLubyte checker_stipple_sml[32*32/8] =
618                                 {
619                                         255,0,255,0,255,0,255,0,255,0,255,0,255,0,255,0, \
620                                         255,0,255,0,255,0,255,0,255,0,255,0,255,0,255,0, \
621                                         0,255,0,255,0,255,0,255,0,255,0,255,0,255,0,255, \
622                                         0,255,0,255,0,255,0,255,0,255,0,255,0,255,0,255, \
623                                         255,0,255,0,255,0,255,0,255,0,255,0,255,0,255,0, \
624                                         255,0,255,0,255,0,255,0,255,0,255,0,255,0,255,0, \
625                                         0,255,0,255,0,255,0,255,0,255,0,255,0,255,0,255, \
626                                         0,255,0,255,0,255,0,255,0,255,0,255,0,255,0,255, \
627                                 };
628
629                                 float x_mid= 0.0f; /* used for dumb clamping of values */
630
631                                 /* dark checkers */
632                                 glColor4ub(100, 100, 100, 255);
633                                 glBegin(GL_POLYGON);
634                                 for(a=0; a<wtb->totvert; a++) {
635                                         glVertex2fv(wtb->inner_v[a]);
636                                 }
637                                 glEnd();
638
639                                 /* light checkers */
640                                 glEnable(GL_POLYGON_STIPPLE);
641                                 glColor4ub(160, 160, 160, 255);
642                                 glPolygonStipple(checker_stipple_sml);
643                                 glBegin(GL_POLYGON);
644                                 for(a=0; a<wtb->totvert; a++) {
645                                         glVertex2fv(wtb->inner_v[a]);
646                                 }
647                                 glEnd();
648                                 glDisable(GL_POLYGON_STIPPLE);
649
650                                 /* alpha fill */
651                                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
652
653                                 glColor4ubv((unsigned char*)wcol->inner);
654                                 glBegin(GL_POLYGON);
655                                 for(a=0; a<wtb->totvert; a++) {
656                                         glVertex2fv(wtb->inner_v[a]);
657                                         x_mid += wtb->inner_v[a][0];
658                                 }
659                                 x_mid /= wtb->totvert;
660                                 glEnd();
661
662                                 /* 1/2 solid color */
663                                 glColor4ub(wcol->inner[0], wcol->inner[1], wcol->inner[2], 255);
664                                 glBegin(GL_POLYGON);
665                                 for(a=0; a<wtb->totvert; a++)
666                                         glVertex2f(MIN2(wtb->inner_v[a][0], x_mid), wtb->inner_v[a][1]);
667                                 glEnd();
668                         }
669                         else {
670                                 /* simple fill */
671                                 glColor4ubv((unsigned char*)wcol->inner);
672                                 glBegin(GL_POLYGON);
673                                 for(a=0; a<wtb->totvert; a++)
674                                         glVertex2fv(wtb->inner_v[a]);
675                                 glEnd();
676                         }
677                 }
678                 else {
679                         char col1[4], col2[4];
680                         
681                         shadecolors4(col1, col2, wcol->inner, wcol->shadetop, wcol->shadedown);
682                         
683                         glShadeModel(GL_SMOOTH);
684                         glBegin(GL_POLYGON);
685                         for(a=0; a<wtb->totvert; a++) {
686                                 round_box_shade_col4(col1, col2, wtb->inner_uv[a][wtb->shadedir]);
687                                 glVertex2fv(wtb->inner_v[a]);
688                         }
689                         glEnd();
690                         glShadeModel(GL_FLAT);
691                 }
692         }
693         
694         /* for each AA step */
695         if(wtb->outline) {
696                 for(j=0; j<8; j++) {
697                         glTranslatef(1.0f * jit[j][0], 1.0f * jit[j][1], 0.0f);
698                         
699                         /* outline */
700                         glColor4ub(wcol->outline[0], wcol->outline[1], wcol->outline[2], 32);
701                         glBegin(GL_QUAD_STRIP);
702                         for(a=0; a<wtb->totvert; a++) {
703                                 glVertex2fv(wtb->outer_v[a]);
704                                 glVertex2fv(wtb->inner_v[a]);
705                         }
706                         glVertex2fv(wtb->outer_v[0]);
707                         glVertex2fv(wtb->inner_v[0]);
708                         glEnd();
709                 
710                         /* emboss bottom shadow */
711                         if(wtb->emboss) {
712                                 glColor4f(1.0f, 1.0f, 1.0f, 0.02f);
713                                 glBegin(GL_QUAD_STRIP);
714                                 for(a=0; a<wtb->halfwayvert; a++) {
715                                         glVertex2fv(wtb->outer_v[a]);
716                                         glVertex2f(wtb->outer_v[a][0], wtb->outer_v[a][1]-1.0f);
717                                 }
718                                 glEnd();
719                         }
720                         
721                         glTranslatef(-1.0f * jit[j][0], -1.0f * jit[j][1], 0.0f);
722                 }
723         }
724         
725         /* decoration */
726         if(wtb->tria1.tot || wtb->tria2.tot) {
727                 /* for each AA step */
728                 for(j=0; j<8; j++) {
729                         glTranslatef(1.0f * jit[j][0], 1.0f * jit[j][1], 0.0f);
730
731                         if(wtb->tria1.tot) {
732                                 glColor4ub(wcol->item[0], wcol->item[1], wcol->item[2], 32);
733                                 widget_trias_draw(&wtb->tria1);
734                         }
735                         if(wtb->tria2.tot) {
736                                 glColor4ub(wcol->item[0], wcol->item[1], wcol->item[2], 32);
737                                 widget_trias_draw(&wtb->tria2);
738                         }
739                 
740                         glTranslatef(-1.0f * jit[j][0], -1.0f * jit[j][1], 0.0f);
741                 }
742         }
743
744         glDisable(GL_BLEND);
745         
746 }
747
748 /* *********************** text/icon ************************************** */
749
750 #define PREVIEW_PAD     4
751
752 static void widget_draw_preview(BIFIconID icon, float UNUSED(alpha), rcti *rect)
753 {
754         int w, h, size;
755
756         if(icon==ICON_NONE)
757                 return;
758
759         w = rect->xmax - rect->xmin;
760         h = rect->ymax - rect->ymin;
761         size = MIN2(w, h);
762         size -= PREVIEW_PAD*2;  /* padding */
763
764         if(size > 0) {
765                 int x = rect->xmin + w/2 - size/2;
766                 int y = rect->ymin + h/2 - size/2;
767
768                 UI_icon_draw_preview_aspect_size(x, y, icon, 1.0f, size);
769         }
770 }
771
772
773 /* icons have been standardized... and this call draws in untransformed coordinates */
774 #define ICON_HEIGHT             16.0f
775
776 static void widget_draw_icon(uiBut *but, BIFIconID icon, float alpha, rcti *rect)
777 {
778         int xs=0, ys=0;
779         float aspect, height;
780         
781         if (but->flag & UI_ICON_PREVIEW) {
782                 widget_draw_preview(icon, alpha, rect);
783                 return;
784         }
785         
786         /* this icon doesn't need draw... */
787         if(icon==ICON_BLANK1 && (but->flag & UI_ICON_SUBMENU)==0) return;
788         
789         /* we need aspect from block, for menus... these buttons are scaled in uiPositionBlock() */
790         aspect= but->block->aspect;
791         if(aspect != but->aspect) {
792                 /* prevent scaling up icon in pupmenu */
793                 if (aspect < 1.0f) {                    
794                         height= ICON_HEIGHT;
795                         aspect = 1.0f;
796                         
797                 }
798                 else 
799                         height= ICON_HEIGHT/aspect;
800         }
801         else
802                 height= ICON_HEIGHT;
803         
804         /* calculate blend color */
805         if ELEM4(but->type, TOG, ROW, TOGN, LISTROW) {
806                 if(but->flag & UI_SELECT);
807                 else if(but->flag & UI_ACTIVE);
808                 else alpha= 0.5f;
809         }
810         
811         /* extra feature allows more alpha blending */
812         if(but->type==LABEL && but->a1==1.0f) alpha *= but->a2;
813         
814         glEnable(GL_BLEND);
815         
816         if(icon && icon!=ICON_BLANK1) {
817                 if(but->flag & UI_ICON_LEFT) {
818                         if (but->type==BUT_TOGDUAL) {
819                                 if (but->drawstr[0]) {
820                                         xs= rect->xmin-1;
821                                 } else {
822                                         xs= (rect->xmin+rect->xmax- height)/2;
823                                 }
824                         }
825                         else if (but->block->flag & UI_BLOCK_LOOP) {
826                                 if(but->type==SEARCH_MENU)
827                                         xs= rect->xmin+4;
828                                 else
829                                         xs= rect->xmin+1;
830                         }
831                         else if ((but->type==ICONROW) || (but->type==ICONTEXTROW)) {
832                                 xs= rect->xmin+3;
833                         }
834                         else {
835                                 xs= rect->xmin+4;
836                         }
837                         ys= (rect->ymin+rect->ymax- height)/2;
838                 }
839                 else {
840                         xs= (rect->xmin+rect->xmax- height)/2;
841                         ys= (rect->ymin+rect->ymax- height)/2;
842                 }
843                 
844                 /* to indicate draggable */
845                 if(but->dragpoin && (but->flag & UI_ACTIVE)) {
846                         float rgb[3]= {1.25f, 1.25f, 1.25f};
847                         UI_icon_draw_aspect_color(xs, ys, icon, aspect, rgb);
848                 }
849                 else
850                         UI_icon_draw_aspect(xs, ys, icon, aspect, alpha);
851         }
852         
853         if(but->flag & UI_ICON_SUBMENU) {
854                 xs= rect->xmax-17;
855                 ys= (rect->ymin+rect->ymax- height)/2;
856                 
857                 UI_icon_draw_aspect(xs, ys, ICON_RIGHTARROW_THIN, aspect, alpha);
858         }
859         
860         glDisable(GL_BLEND);
861 }
862
863 /* sets but->ofs to make sure text is correctly visible */
864 static void ui_text_leftclip(uiFontStyle *fstyle, uiBut *but, rcti *rect)
865 {
866         int border= (but->flag & UI_BUT_ALIGN_RIGHT)? 8: 10;
867         int okwidth= rect->xmax-rect->xmin - border;
868         
869         if (but->flag & UI_HAS_ICON) okwidth -= 16;
870         
871         /* need to set this first */
872         uiStyleFontSet(fstyle);
873         
874         if (fstyle->kerning==1) /* for BLF_width */
875                 BLF_enable(fstyle->uifont_id, BLF_KERNING_DEFAULT);
876
877         /* if text editing we define ofs dynamically */
878         if(but->editstr && but->pos >= 0) {
879                 if(but->ofs > but->pos)
880                         but->ofs= but->pos;
881         }
882         else but->ofs= 0;
883         
884         but->strwidth= BLF_width(fstyle->uifont_id, but->drawstr + but->ofs);
885         
886         while(but->strwidth > okwidth) {
887                 
888                 /* textbut exception, clip right when... */
889                 if(but->editstr && but->pos >= 0) {
890                         float width;
891                         char buf[256];
892                         
893                         /* copy draw string */
894                         BLI_strncpy(buf, but->drawstr, sizeof(buf));
895                         /* string position of cursor */
896                         buf[but->pos]= 0;
897                         width= BLF_width(fstyle->uifont_id, buf+but->ofs);
898                         
899                         /* if cursor is at 20 pixels of right side button we clip left */
900                         if(width > okwidth-20)
901                                 but->ofs++;
902                         else {
903                                 /* shift string to the left */
904                                 if(width < 20 && but->ofs > 0)
905                                         but->ofs--;
906                                 but->drawstr[ strlen(but->drawstr)-1 ]= 0;
907                         }
908                 }
909                 else
910                         but->ofs++;
911
912                 but->strwidth= BLF_width(fstyle->uifont_id, but->drawstr+but->ofs);
913                 
914                 if(but->strwidth < 10) break;
915         }
916         
917         if (fstyle->kerning==1)
918                 BLF_disable(fstyle->uifont_id, BLF_KERNING_DEFAULT);
919 }
920
921 static void ui_text_label_rightclip(uiFontStyle *fstyle, uiBut *but, rcti *rect)
922 {
923         int border= (but->flag & UI_BUT_ALIGN_RIGHT)? 8: 10;
924         int okwidth= rect->xmax-rect->xmin - border;
925         char *cpoin=NULL;
926         char *cpend = but->drawstr + strlen(but->drawstr);
927         
928         /* need to set this first */
929         uiStyleFontSet(fstyle);
930         
931         if (fstyle->kerning==1) /* for BLF_width */
932                 BLF_enable(fstyle->uifont_id, BLF_KERNING_DEFAULT);
933         
934         but->strwidth= BLF_width(fstyle->uifont_id, but->drawstr);
935         but->ofs= 0;
936         
937         /* find the space after ':' separator */
938         cpoin= strrchr(but->drawstr, ':');
939         
940         if (cpoin && (cpoin < cpend-2)) {
941                 char *cp2 = cpoin;
942                 
943                 /* chop off the leading text, starting from the right */
944                 while (but->strwidth > okwidth && cp2 > but->drawstr) {
945                         /* shift the text after and including cp2 back by 1 char, +1 to include null terminator */
946                         memmove(cp2-1, cp2, strlen(cp2)+1);
947                         cp2--;
948                         
949                         but->strwidth= BLF_width(fstyle->uifont_id, but->drawstr+but->ofs);
950                         if(but->strwidth < 10) break;
951                 }
952         
953         
954                 /* after the leading text is gone, chop off the : and following space, with ofs */
955                 while ((but->strwidth > okwidth) && (but->ofs < 2))
956                 {
957                         but->ofs++;
958                         but->strwidth= BLF_width(fstyle->uifont_id, but->drawstr+but->ofs);
959                         if(but->strwidth < 10) break;
960                 }
961                 
962         }
963
964         /* once the label's gone, chop off the least significant digits */
965         while(but->strwidth > okwidth ) {
966                 int pos= strlen(but->drawstr);
967                 
968                 but->drawstr[ pos-1 ] = 0;
969                 pos--;
970                 
971                 but->strwidth= BLF_width(fstyle->uifont_id, but->drawstr+but->ofs);
972                 if(but->strwidth < 10) break;
973         }
974         
975         if (fstyle->kerning==1)
976                 BLF_disable(fstyle->uifont_id, BLF_KERNING_DEFAULT);
977 }
978
979
980 static void widget_draw_text(uiFontStyle *fstyle, uiWidgetColors *wcol, uiBut *but, rcti *rect)
981 {
982 //      int transopts;
983         char *cpoin = NULL;
984         
985         /* for underline drawing */
986         float font_xofs, font_yofs;
987
988         uiStyleFontSet(fstyle);
989         
990         if(but->editstr || (but->flag & UI_TEXT_LEFT))
991                 fstyle->align= UI_STYLE_TEXT_LEFT;
992         else
993                 fstyle->align= UI_STYLE_TEXT_CENTER;                    
994         
995         if (fstyle->kerning==1) /* for BLF_width */
996                 BLF_enable(fstyle->uifont_id, BLF_KERNING_DEFAULT);
997         
998         /* text button selection and cursor */
999         if(but->editstr && but->pos != -1) {
1000                 short t=0, pos=0, ch;
1001                 short selsta_tmp, selend_tmp, selsta_draw, selwidth_draw;
1002
1003                 if ((but->selend - but->selsta) > 0) {
1004                         /* text button selection */
1005                         selsta_tmp = but->selsta;
1006                         selend_tmp = but->selend;
1007                         
1008                         if(but->drawstr[0]!=0) {
1009
1010                                 if (but->selsta >= but->ofs) {
1011                                         ch= but->drawstr[selsta_tmp];
1012                                         but->drawstr[selsta_tmp]= 0;
1013                                         
1014                                         selsta_draw = BLF_width(fstyle->uifont_id, but->drawstr+but->ofs);
1015                                         
1016                                         but->drawstr[selsta_tmp]= ch;
1017                                 } else
1018                                         selsta_draw = 0;
1019                                 
1020                                 ch= but->drawstr[selend_tmp];
1021                                 but->drawstr[selend_tmp]= 0;
1022                                 
1023                                 selwidth_draw = BLF_width(fstyle->uifont_id, but->drawstr+but->ofs);
1024                                 
1025                                 but->drawstr[selend_tmp]= ch;
1026
1027                                 glColor3ubv((unsigned char*)wcol->item);
1028                                 glRects(rect->xmin+selsta_draw, rect->ymin+2, rect->xmin+selwidth_draw, rect->ymax-2);
1029                         }
1030                 } else {
1031                         /* text cursor */
1032                         pos= but->pos;
1033                         if(pos >= but->ofs) {
1034                                 if(but->drawstr[0]!=0) {
1035                                         ch= but->drawstr[pos];
1036                                         but->drawstr[pos]= 0;
1037                                         
1038                                         t= BLF_width(fstyle->uifont_id, but->drawstr+but->ofs) / but->aspect;
1039                                         
1040                                         but->drawstr[pos]= ch;
1041                                 }
1042
1043                                 glColor3f(0.20, 0.6, 0.9);
1044                                 glRects(rect->xmin+t, rect->ymin+2, rect->xmin+t+2, rect->ymax-2);
1045                         }
1046                 }
1047         }
1048         
1049         if (fstyle->kerning == 1)
1050                 BLF_disable(fstyle->uifont_id, BLF_KERNING_DEFAULT);
1051         
1052         //      ui_rasterpos_safe(x, y, but->aspect);
1053 //      if(but->type==IDPOIN) transopts= 0;     // no translation, of course!
1054 //      else transopts= ui_translate_buttons();
1055         
1056         /* cut string in 2 parts - only for menu entries */
1057         if((but->block->flag & UI_BLOCK_LOOP)) {
1058                 if(ELEM5(but->type, SLI, NUM, TEX, NUMSLI, NUMABS)==0) {
1059                         cpoin= strchr(but->drawstr, '|');
1060                         if(cpoin) *cpoin= 0;            
1061                 }
1062         }
1063         
1064         glColor3ubv((unsigned char*)wcol->text);
1065
1066         uiStyleFontDrawExt(fstyle, rect, but->drawstr+but->ofs, &font_xofs, &font_yofs);
1067
1068         if(but->menu_key != '\0') {
1069                 char fixedbuf[128];
1070                 char *str;
1071
1072                 BLI_strncpy(fixedbuf, but->drawstr + but->ofs, sizeof(fixedbuf));
1073
1074                 str= strchr(fixedbuf, but->menu_key-32); /* upper case */
1075                 if(str==NULL)
1076                         str= strchr(fixedbuf, but->menu_key);
1077
1078                 if(str) {
1079                         int ul_index= -1;
1080                         float ul_advance;
1081
1082                         ul_index= (int)(str - fixedbuf);
1083
1084                         if (fstyle->kerning == 1) {
1085                                 BLF_enable(fstyle->uifont_id, BLF_KERNING_DEFAULT);
1086                         }
1087
1088                         fixedbuf[ul_index]= '\0';
1089                         ul_advance= BLF_width(fstyle->uifont_id, fixedbuf);
1090
1091                         BLF_position(fstyle->uifont_id, rect->xmin+font_xofs + ul_advance, rect->ymin+font_yofs, 0.0f);
1092                         BLF_draw(fstyle->uifont_id, "_", 2);
1093
1094                         if (fstyle->kerning == 1) {
1095                                 BLF_disable(fstyle->uifont_id, BLF_KERNING_DEFAULT);
1096                         }
1097                 }
1098         }
1099
1100         /* part text right aligned */
1101         if(cpoin) {
1102                 fstyle->align= UI_STYLE_TEXT_RIGHT;
1103                 rect->xmax-=5;
1104                 uiStyleFontDraw(fstyle, rect, cpoin+1);
1105                 *cpoin= '|';
1106         }
1107 }
1108
1109 /* draws text and icons for buttons */
1110 static void widget_draw_text_icon(uiFontStyle *fstyle, uiWidgetColors *wcol, uiBut *but, rcti *rect)
1111 {
1112         
1113         if(but==NULL) return;
1114
1115         /* clip but->drawstr to fit in available space */
1116         if (but->editstr && but->pos >= 0) {
1117                 ui_text_leftclip(fstyle, but, rect);
1118         }
1119         else if (ELEM4(but->type, NUM, NUMABS, NUMSLI, SLI)) {
1120                 ui_text_label_rightclip(fstyle, but, rect);
1121         }
1122         else if (ELEM(but->type, TEX, SEARCH_MENU)) {
1123                 ui_text_leftclip(fstyle, but, rect);
1124         }
1125         else if ((but->block->flag & UI_BLOCK_LOOP) && (but->type == BUT)) {
1126                 ui_text_leftclip(fstyle, but, rect);
1127         }
1128         else but->ofs= 0;
1129         
1130         /* check for button text label */
1131         if (but->type == ICONTEXTROW) {
1132                 widget_draw_icon(but, (BIFIconID) (but->icon+but->iconadd), 1.0f, rect);
1133         }
1134         else {
1135                                 
1136                 if(but->type==BUT_TOGDUAL) {
1137                         int dualset= 0;
1138                         if(but->pointype==SHO)
1139                                 dualset= BTST( *(((short *)but->poin)+1), but->bitnr);
1140                         else if(but->pointype==INT)
1141                                 dualset= BTST( *(((int *)but->poin)+1), but->bitnr);
1142                         
1143                         widget_draw_icon(but, ICON_DOT, dualset?1.0f:0.25f, rect);
1144                 }
1145                 
1146                 /* If there's an icon too (made with uiDefIconTextBut) then draw the icon
1147                 and offset the text label to accommodate it */
1148                 
1149                 if (but->flag & UI_HAS_ICON) {
1150                         widget_draw_icon(but, but->icon+but->iconadd, 1.0f, rect);
1151                         
1152                         rect->xmin += UI_icon_get_width(but->icon+but->iconadd);
1153                         
1154                         if(but->editstr || (but->flag & UI_TEXT_LEFT)) 
1155                                 rect->xmin += 5;
1156                 }
1157                 else if((but->flag & UI_TEXT_LEFT)) 
1158                         rect->xmin += 5;
1159                 
1160                 /* always draw text for textbutton cursor */
1161                 widget_draw_text(fstyle, wcol, but, rect);
1162
1163         }
1164 }
1165
1166
1167
1168 /* *********************** widget types ************************************* */
1169
1170
1171 /*   uiWidgetStateColors
1172         char inner_anim[4];
1173         char inner_anim_sel[4];
1174         char inner_key[4];
1175         char inner_key_sel[4];
1176         char inner_driven[4];
1177         char inner_driven_sel[4];
1178         float blend;
1179  
1180 */
1181
1182 static struct uiWidgetStateColors wcol_state_colors= {
1183         {115, 190, 76, 255},
1184         {90, 166, 51, 255},
1185         {240, 235, 100, 255},
1186         {215, 211, 75, 255},
1187         {180, 0, 255, 255},
1188         {153, 0, 230, 255},
1189         0.5f, 0.0f
1190 };
1191
1192 /*  uiWidgetColors
1193         float outline[3];
1194         float inner[4];
1195         float inner_sel[4];
1196         float item[3];
1197         float text[3];
1198         float text_sel[3];
1199
1200         short shaded;
1201         float shadetop, shadedown;
1202 */      
1203
1204 static struct uiWidgetColors wcol_num= {
1205         {25, 25, 25, 255},
1206         {180, 180, 180, 255},
1207         {153, 153, 153, 255},
1208         {90, 90, 90, 255},
1209         
1210         {0, 0, 0, 255},
1211         {255, 255, 255, 255},
1212         
1213         1,
1214         -20, 0
1215 };
1216
1217 static struct uiWidgetColors wcol_numslider= {
1218         {25, 25, 25, 255},
1219         {180, 180, 180, 255},
1220         {153, 153, 153, 255},
1221         {128, 128, 128, 255},
1222         
1223         {0, 0, 0, 255},
1224         {255, 255, 255, 255},
1225         
1226         1,
1227         -20, 0
1228 };
1229
1230 static struct uiWidgetColors wcol_text= {
1231         {25, 25, 25, 255},
1232         {153, 153, 153, 255},
1233         {153, 153, 153, 255},
1234         {90, 90, 90, 255},
1235         
1236         {0, 0, 0, 255},
1237         {255, 255, 255, 255},
1238         
1239         1,
1240         0, 25
1241 };
1242
1243 static struct uiWidgetColors wcol_option= {
1244         {0, 0, 0, 255},
1245         {70, 70, 70, 255},
1246         {70, 70, 70, 255},
1247         {255, 255, 255, 255},
1248         
1249         {0, 0, 0, 255},
1250         {255, 255, 255, 255},
1251         
1252         1,
1253         15, -15
1254 };
1255
1256 /* button that shows popup */
1257 static struct uiWidgetColors wcol_menu= {
1258         {0, 0, 0, 255},
1259         {70, 70, 70, 255},
1260         {70, 70, 70, 255},
1261         {255, 255, 255, 255},
1262         
1263         {255, 255, 255, 255},
1264         {204, 204, 204, 255},
1265         
1266         1,
1267         15, -15
1268 };
1269
1270 /* button that starts pulldown */
1271 static struct uiWidgetColors wcol_pulldown= {
1272         {0, 0, 0, 255},
1273         {63, 63, 63, 255},
1274         {86, 128, 194, 255},
1275         {255, 255, 255, 255},
1276         
1277         {0, 0, 0, 255},
1278         {0, 0, 0, 255},
1279         
1280         0,
1281         25, -20
1282 };
1283
1284 /* button inside menu */
1285 static struct uiWidgetColors wcol_menu_item= {
1286         {0, 0, 0, 255},
1287         {0, 0, 0, 0},
1288         {86, 128, 194, 255},
1289         {255, 255, 255, 255},
1290         
1291         {255, 255, 255, 255},
1292         {0, 0, 0, 255},
1293         
1294         0,
1295         38, 0
1296 };
1297
1298 /* backdrop menu + title text color */
1299 static struct uiWidgetColors wcol_menu_back= {
1300         {0, 0, 0, 255},
1301         {25, 25, 25, 230},
1302         {45, 45, 45, 230},
1303         {100, 100, 100, 255},
1304         
1305         {160, 160, 160, 255},
1306         {255, 255, 255, 255},
1307         
1308         0,
1309         25, -20
1310 };
1311
1312
1313 static struct uiWidgetColors wcol_radio= {
1314         {0, 0, 0, 255},
1315         {70, 70, 70, 255},
1316         {86, 128, 194, 255},
1317         {255, 255, 255, 255},
1318         
1319         {255, 255, 255, 255},
1320         {0, 0, 0, 255},
1321         
1322         1,
1323         15, -15
1324 };
1325
1326 static struct uiWidgetColors wcol_regular= {
1327         {25, 25, 25, 255},
1328         {153, 153, 153, 255},
1329         {100, 100, 100, 255},
1330         {25, 25, 25, 255},
1331         
1332         {0, 0, 0, 255},
1333         {255, 255, 255, 255},
1334         
1335         0,
1336         0, 0
1337 };
1338
1339 static struct uiWidgetColors wcol_tool= {
1340         {25, 25, 25, 255},
1341         {153, 153, 153, 255},
1342         {100, 100, 100, 255},
1343         {25, 25, 25, 255},
1344         
1345         {0, 0, 0, 255},
1346         {255, 255, 255, 255},
1347         
1348         1,
1349         15, -15
1350 };
1351
1352 static struct uiWidgetColors wcol_box= {
1353         {25, 25, 25, 255},
1354         {128, 128, 128, 255},
1355         {100, 100, 100, 255},
1356         {25, 25, 25, 255},
1357         
1358         {0, 0, 0, 255},
1359         {255, 255, 255, 255},
1360         
1361         0,
1362         0, 0
1363 };
1364
1365 static struct uiWidgetColors wcol_toggle= {
1366         {25, 25, 25, 255},
1367         {153, 153, 153, 255},
1368         {100, 100, 100, 255},
1369         {25, 25, 25, 255},
1370         
1371         {0, 0, 0, 255},
1372         {255, 255, 255, 255},
1373         
1374         0,
1375         0, 0
1376 };
1377
1378 static struct uiWidgetColors wcol_scroll= {
1379         {50, 50, 50, 180},
1380         {80, 80, 80, 180},
1381         {100, 100, 100, 180},
1382         {128, 128, 128, 255},
1383         
1384         {0, 0, 0, 255},
1385         {255, 255, 255, 255},
1386         
1387         1,
1388         5, -5
1389 };
1390
1391 static struct uiWidgetColors wcol_progress= {
1392         {0, 0, 0, 255},
1393         {190, 190, 190, 255},
1394         {100, 100, 100, 180},
1395         {68, 68, 68, 255},
1396         
1397         {0, 0, 0, 255},
1398         {255, 255, 255, 255},
1399         
1400         0,
1401         0, 0
1402 };
1403
1404 static struct uiWidgetColors wcol_list_item= {
1405         {0, 0, 0, 255},
1406         {0, 0, 0, 0},
1407         {86, 128, 194, 255},
1408         {0, 0, 0, 255},
1409         
1410         {0, 0, 0, 255},
1411         {0, 0, 0, 255},
1412         
1413         0,
1414         0, 0
1415 };
1416
1417 /* free wcol struct to play with */
1418 static struct uiWidgetColors wcol_tmp= {
1419         {0, 0, 0, 255},
1420         {128, 128, 128, 255},
1421         {100, 100, 100, 255},
1422         {25, 25, 25, 255},
1423         
1424         {0, 0, 0, 255},
1425         {255, 255, 255, 255},
1426         
1427         0,
1428         0, 0
1429 };
1430
1431
1432 /* called for theme init (new theme) and versions */
1433 void ui_widget_color_init(ThemeUI *tui)
1434 {
1435         tui->wcol_regular= wcol_regular;
1436         tui->wcol_tool= wcol_tool;
1437         tui->wcol_text= wcol_text;
1438         tui->wcol_radio= wcol_radio;
1439         tui->wcol_option= wcol_option;
1440         tui->wcol_toggle= wcol_toggle;
1441         tui->wcol_num= wcol_num;
1442         tui->wcol_numslider= wcol_numslider;
1443         tui->wcol_menu= wcol_menu;
1444         tui->wcol_pulldown= wcol_pulldown;
1445         tui->wcol_menu_back= wcol_menu_back;
1446         tui->wcol_menu_item= wcol_menu_item;
1447         tui->wcol_box= wcol_box;
1448         tui->wcol_scroll= wcol_scroll;
1449         tui->wcol_list_item= wcol_list_item;
1450         tui->wcol_progress= wcol_progress;
1451
1452         tui->wcol_state= wcol_state_colors;
1453 }
1454
1455 /* ************ button callbacks, state ***************** */
1456
1457 static void widget_state_blend(char *cp, const char *cpstate, const float fac)
1458 {
1459         if(fac != 0.0f) {
1460                 cp[0]= (int)((1.0f-fac)*cp[0] + fac*cpstate[0]);
1461                 cp[1]= (int)((1.0f-fac)*cp[1] + fac*cpstate[1]);
1462                 cp[2]= (int)((1.0f-fac)*cp[2] + fac*cpstate[2]);
1463         }
1464 }
1465
1466 /* copy colors from theme, and set changes in it based on state */
1467 static void widget_state(uiWidgetType *wt, int state)
1468 {
1469         uiWidgetStateColors *wcol_state= wt->wcol_state;
1470
1471         wt->wcol= *(wt->wcol_theme);
1472         
1473         if(state & UI_SELECT) {
1474                 QUATCOPY(wt->wcol.inner, wt->wcol.inner_sel)
1475
1476                 if(state & UI_BUT_ANIMATED_KEY)
1477                         widget_state_blend(wt->wcol.inner, wcol_state->inner_key_sel, wcol_state->blend);
1478                 else if(state & UI_BUT_ANIMATED)
1479                         widget_state_blend(wt->wcol.inner, wcol_state->inner_anim_sel, wcol_state->blend);
1480                 else if(state & UI_BUT_DRIVEN)
1481                         widget_state_blend(wt->wcol.inner, wcol_state->inner_driven_sel, wcol_state->blend);
1482
1483                 VECCOPY(wt->wcol.text, wt->wcol.text_sel);
1484                 
1485                 if(state & UI_SELECT)
1486                         SWAP(short, wt->wcol.shadetop, wt->wcol.shadedown);
1487         }
1488         else {
1489                 if(state & UI_BUT_ANIMATED_KEY)
1490                         widget_state_blend(wt->wcol.inner, wcol_state->inner_key, wcol_state->blend);
1491                 else if(state & UI_BUT_ANIMATED)
1492                         widget_state_blend(wt->wcol.inner, wcol_state->inner_anim, wcol_state->blend);
1493                 else if(state & UI_BUT_DRIVEN)
1494                         widget_state_blend(wt->wcol.inner, wcol_state->inner_driven, wcol_state->blend);
1495
1496                 if(state & UI_ACTIVE) { /* mouse over? */
1497                         wt->wcol.inner[0]= wt->wcol.inner[0]>=240? 255 : wt->wcol.inner[0]+15;
1498                         wt->wcol.inner[1]= wt->wcol.inner[1]>=240? 255 : wt->wcol.inner[1]+15;
1499                         wt->wcol.inner[2]= wt->wcol.inner[2]>=240? 255 : wt->wcol.inner[2]+15;
1500                 }
1501         }
1502
1503         if(state & UI_BUT_REDALERT) {
1504                 char red[4]= {255, 0, 0};
1505                 widget_state_blend(wt->wcol.inner, red, 0.4f);
1506         }
1507 }
1508
1509 /* sliders use special hack which sets 'item' as inner when drawing filling */
1510 static void widget_state_numslider(uiWidgetType *wt, int state)
1511 {
1512         uiWidgetStateColors *wcol_state= wt->wcol_state;
1513         float blend= wcol_state->blend - 0.2f; // XXX special tweak to make sure that bar will still be visible
1514
1515         /* call this for option button */
1516         widget_state(wt, state);
1517         
1518         /* now, set the inner-part so that it reflects state settings too */
1519         // TODO: maybe we should have separate settings for the blending colors used for this case?
1520         if(state & UI_SELECT) {
1521                 
1522                 if(state & UI_BUT_ANIMATED_KEY)
1523                         widget_state_blend(wt->wcol.item, wcol_state->inner_key_sel, blend);
1524                 else if(state & UI_BUT_ANIMATED)
1525                         widget_state_blend(wt->wcol.item, wcol_state->inner_anim_sel, blend);
1526                 else if(state & UI_BUT_DRIVEN)
1527                         widget_state_blend(wt->wcol.item, wcol_state->inner_driven_sel, blend);
1528                 
1529                 if(state & UI_SELECT)
1530                         SWAP(short, wt->wcol.shadetop, wt->wcol.shadedown);
1531         }
1532         else {
1533                 if(state & UI_BUT_ANIMATED_KEY)
1534                         widget_state_blend(wt->wcol.item, wcol_state->inner_key, blend);
1535                 else if(state & UI_BUT_ANIMATED)
1536                         widget_state_blend(wt->wcol.item, wcol_state->inner_anim, blend);
1537                 else if(state & UI_BUT_DRIVEN)
1538                         widget_state_blend(wt->wcol.item, wcol_state->inner_driven, blend);
1539         }
1540 }
1541
1542 /* labels use theme colors for text */
1543 static void widget_state_label(uiWidgetType *wt, int state)
1544 {
1545         /* call this for option button */
1546         widget_state(wt, state);
1547
1548         if(state & UI_SELECT)
1549                 UI_GetThemeColor4ubv(TH_TEXT_HI, (unsigned char *)wt->wcol.text);
1550         else
1551                 UI_GetThemeColor4ubv(TH_TEXT, (unsigned char *)wt->wcol.text);
1552         
1553 }
1554
1555 /* labels use theme colors for text */
1556 static void widget_state_option_menu(uiWidgetType *wt, int state)
1557 {
1558         
1559         /* call this for option button */
1560         widget_state(wt, state);
1561         
1562         /* if not selected we get theme from menu back */
1563         if(state & UI_SELECT)
1564                 UI_GetThemeColor4ubv(TH_TEXT_HI, (unsigned char *)wt->wcol.text);
1565         else {
1566                 bTheme *btheme= U.themes.first; /* XXX */
1567
1568                 VECCOPY(wt->wcol.text, btheme->tui.wcol_menu_back.text);
1569         }
1570 }
1571
1572
1573 static void widget_state_nothing(uiWidgetType *wt, int UNUSED(state))
1574 {
1575         wt->wcol= *(wt->wcol_theme);
1576 }       
1577
1578 /* special case, button that calls pulldown */
1579 static void widget_state_pulldown(uiWidgetType *wt, int state)
1580 {
1581         wt->wcol= *(wt->wcol_theme);
1582         
1583         QUATCOPY(wt->wcol.inner, wt->wcol.inner_sel);
1584         VECCOPY(wt->wcol.outline, wt->wcol.inner);
1585
1586         if(state & UI_ACTIVE)
1587                 VECCOPY(wt->wcol.text, wt->wcol.text_sel);
1588 }
1589
1590 /* special case, menu items */
1591 static void widget_state_menu_item(uiWidgetType *wt, int state)
1592 {
1593         wt->wcol= *(wt->wcol_theme);
1594         
1595         if(state & (UI_BUT_DISABLED|UI_BUT_INACTIVE)) {
1596                 wt->wcol.text[0]= 0.5f*(wt->wcol.text[0]+wt->wcol.text_sel[0]);
1597                 wt->wcol.text[1]= 0.5f*(wt->wcol.text[1]+wt->wcol.text_sel[1]);
1598                 wt->wcol.text[2]= 0.5f*(wt->wcol.text[2]+wt->wcol.text_sel[2]);
1599         }
1600         else if(state & UI_ACTIVE) {
1601                 QUATCOPY(wt->wcol.inner, wt->wcol.inner_sel);
1602                 VECCOPY(wt->wcol.text, wt->wcol.text_sel);
1603                 
1604                 wt->wcol.shaded= 1;
1605         }
1606 }
1607
1608
1609 /* ************ menu backdrop ************************* */
1610
1611 /* outside of rect, rad to left/bottom/right */
1612 static void widget_softshadow(rcti *rect, int roundboxalign, float radin, float radout)
1613 {
1614         uiWidgetBase wtb;
1615         rcti rect1= *rect;
1616         float alpha, alphastep;
1617         int step, tot, a;
1618         
1619         /* prevent tooltips to not show round shadow */
1620         if( 2.0f*radout > 0.2f*(rect1.ymax-rect1.ymin) )
1621                 rect1.ymax -= 0.2f*(rect1.ymax-rect1.ymin);
1622         else
1623                 rect1.ymax -= 2.0f*radout;
1624         
1625         /* inner part */
1626         tot= round_box_shadow_edges(wtb.inner_v, &rect1, radin, roundboxalign & 12, 0.0f);
1627         
1628         /* inverse linear shadow alpha */
1629         alpha= 0.15;
1630         alphastep= 0.67;
1631         
1632         for(step= 1; step<=radout; step++, alpha*=alphastep) {
1633                 round_box_shadow_edges(wtb.outer_v, &rect1, radin, 15, (float)step);
1634                 
1635                 glColor4f(0.0f, 0.0f, 0.0f, alpha);
1636                 
1637                 glBegin(GL_QUAD_STRIP);
1638                 for(a=0; a<tot; a++) {
1639                         glVertex2fv(wtb.outer_v[a]);
1640                         glVertex2fv(wtb.inner_v[a]);
1641                 }
1642                 glEnd();
1643         }
1644         
1645 }
1646
1647 static void widget_menu_back(uiWidgetColors *wcol, rcti *rect, int flag, int direction)
1648 {
1649         uiWidgetBase wtb;
1650         int roundboxalign= 15;
1651         
1652         widget_init(&wtb);
1653         
1654         /* menu is 2nd level or deeper */
1655         if (flag & UI_BLOCK_POPUP) {
1656                 //rect->ymin -= 4.0;
1657                 //rect->ymax += 4.0;
1658         }
1659         else if (direction == UI_DOWN) {
1660                 roundboxalign= 12;
1661                 rect->ymin -= 4.0;
1662         } 
1663         else if (direction == UI_TOP) {
1664                 roundboxalign= 3;
1665                 rect->ymax += 4.0;
1666         }
1667         
1668         glEnable(GL_BLEND);
1669         widget_softshadow(rect, roundboxalign, 5.0f, 8.0f);
1670         
1671         round_box_edges(&wtb, roundboxalign, rect, 5.0f);
1672         wtb.emboss= 0;
1673         widgetbase_draw(&wtb, wcol);
1674         
1675         glDisable(GL_BLEND);
1676 }
1677
1678
1679 static void ui_hsv_cursor(float x, float y)
1680 {
1681         
1682         glPushMatrix();
1683         glTranslatef(x, y, 0.0f);
1684         
1685         glColor3f(1.0f, 1.0f, 1.0f);
1686         glutil_draw_filled_arc(0.0f, M_PI*2.0, 3.0f, 8);
1687         
1688         glEnable(GL_BLEND);
1689         glEnable(GL_LINE_SMOOTH );
1690         glColor3f(0.0f, 0.0f, 0.0f);
1691         glutil_draw_lined_arc(0.0f, M_PI*2.0, 3.0f, 12);
1692         glDisable(GL_BLEND);
1693         glDisable(GL_LINE_SMOOTH );
1694         
1695         glPopMatrix();
1696         
1697 }
1698
1699 void ui_hsvcircle_vals_from_pos(float *valrad, float *valdist, rcti *rect, float mx, float my)
1700 {
1701         /* duplication of code... well, simple is better now */
1702         float centx= (float)(rect->xmin + rect->xmax)/2;
1703         float centy= (float)(rect->ymin + rect->ymax)/2;
1704         float radius, dist;
1705         
1706         if( rect->xmax-rect->xmin > rect->ymax-rect->ymin )
1707                 radius= (float)(rect->ymax - rect->ymin)/2; 
1708         else
1709                 radius= (float)(rect->xmax - rect->xmin)/2; 
1710
1711         mx-= centx;
1712         my-= centy;
1713         dist= sqrt( mx*mx + my*my);
1714         if(dist < radius)
1715                 *valdist= dist/radius;
1716         else
1717                 *valdist= 1.0f;
1718         
1719         *valrad= atan2f(mx, my)/(2.0f*(float)M_PI) + 0.5f;
1720 }
1721
1722 static void ui_draw_but_HSVCIRCLE(uiBut *but, uiWidgetColors *wcol, rcti *rect)
1723 {
1724         /* gouraud triangle fan */
1725         float radstep, ang= 0.0f;
1726         float centx, centy, radius;
1727         float rgb[3], hsvo[3], hsv[3], col[3], colcent[3];
1728         int a, tot= 32;
1729         int color_profile = but->block->color_profile;
1730         
1731         if (but->rnaprop && RNA_property_subtype(but->rnaprop) == PROP_COLOR_GAMMA)
1732                 color_profile = BLI_PR_NONE;
1733         
1734         radstep= 2.0f*(float)M_PI/(float)tot;
1735         centx= (float)(rect->xmin + rect->xmax)/2;
1736         centy= (float)(rect->ymin + rect->ymax)/2;
1737         
1738         if( rect->xmax-rect->xmin > rect->ymax-rect->ymin )
1739                 radius= (float)(rect->ymax - rect->ymin)/2; 
1740         else
1741                 radius= (float)(rect->xmax - rect->xmin)/2; 
1742         
1743         /* color */
1744         ui_get_but_vectorf(but, rgb);
1745         copy_v3_v3(hsv, ui_block_hsv_get(but->block));
1746         rgb_to_hsv_compat(rgb[0], rgb[1], rgb[2], hsv, hsv+1, hsv+2);
1747         copy_v3_v3(hsvo, hsv);
1748         
1749         /* exception: if 'lock' is set
1750          * lock the value of the color wheel to 1.
1751          * Useful for color correction tools where you're only interested in hue. */
1752         if (but->flag & UI_BUT_COLOR_LOCK)
1753                 hsv[2] = 1.f;
1754         else if (color_profile)
1755                 hsv[2] = linearrgb_to_srgb(hsv[2]);
1756         
1757         hsv_to_rgb(0.f, 0.f, hsv[2], colcent, colcent+1, colcent+2);
1758         
1759         glShadeModel(GL_SMOOTH);
1760
1761         glBegin(GL_TRIANGLE_FAN);
1762         glColor3fv(colcent);
1763         glVertex2f( centx, centy);
1764         
1765         for(a=0; a<=tot; a++, ang+=radstep) {
1766                 float si= sin(ang);
1767                 float co= cos(ang);
1768                 
1769                 ui_hsvcircle_vals_from_pos(hsv, hsv+1, rect, centx + co*radius, centy + si*radius);
1770                 CLAMP(hsv[2], 0.0f, 1.0f); /* for display only */
1771
1772                 hsv_to_rgb(hsv[0], hsv[1], hsv[2], col, col+1, col+2);
1773                 glColor3fv(col);
1774                 glVertex2f( centx + co*radius, centy + si*radius);
1775         }
1776         glEnd();
1777         
1778         glShadeModel(GL_FLAT);
1779         
1780         /* fully rounded outline */
1781         glPushMatrix();
1782         glTranslatef(centx, centy, 0.0f);
1783         glEnable(GL_BLEND);
1784         glEnable(GL_LINE_SMOOTH );
1785         glColor3ubv((unsigned char*)wcol->outline);
1786         glutil_draw_lined_arc(0.0f, M_PI*2.0, radius, tot + 1);
1787         glDisable(GL_BLEND);
1788         glDisable(GL_LINE_SMOOTH );
1789         glPopMatrix();
1790
1791         /* cursor */
1792         ang= 2.0f*(float)M_PI*hsvo[0] + 0.5f*(float)M_PI;
1793
1794         if(but->flag & UI_BUT_COLOR_CUBIC)
1795                 radius= (1.0f - powf(1.0f - hsvo[1], 3.0f)) *radius;
1796         else
1797                 radius= hsvo[1] * radius;
1798
1799         ui_hsv_cursor(centx + cosf(-ang)*radius, centy + sinf(-ang)*radius);
1800         
1801 }
1802
1803 /* ************ custom buttons, old stuff ************** */
1804
1805 /* draws in resolution of 20x4 colors */
1806 void ui_draw_gradient(rcti *rect, float *hsv, int type, float alpha)
1807 {
1808         int a;
1809         float h= hsv[0], s= hsv[1], v= hsv[2];
1810         float dx, dy, sx1, sx2, sy;
1811         float col0[4][3];       // left half, rect bottom to top
1812         float col1[4][3];       // right half, rect bottom to top
1813
1814         /* draw series of gouraud rects */
1815         glShadeModel(GL_SMOOTH);
1816         
1817         switch(type) {
1818                 case UI_GRAD_SV:
1819                         hsv_to_rgb(h, 0.0, 0.0,   &col1[0][0], &col1[0][1], &col1[0][2]);
1820                         hsv_to_rgb(h, 0.333, 0.0, &col1[1][0], &col1[1][1], &col1[1][2]);
1821                         hsv_to_rgb(h, 0.666, 0.0, &col1[2][0], &col1[2][1], &col1[2][2]);
1822                         hsv_to_rgb(h, 1.0, 0.0,   &col1[3][0], &col1[3][1], &col1[3][2]);
1823                         break;
1824                 case UI_GRAD_HV:
1825                         hsv_to_rgb(0.0, s, 0.0,   &col1[0][0], &col1[0][1], &col1[0][2]);
1826                         hsv_to_rgb(0.0, s, 0.333, &col1[1][0], &col1[1][1], &col1[1][2]);
1827                         hsv_to_rgb(0.0, s, 0.666, &col1[2][0], &col1[2][1], &col1[2][2]);
1828                         hsv_to_rgb(0.0, s, 1.0,   &col1[3][0], &col1[3][1], &col1[3][2]);
1829                         break;
1830                 case UI_GRAD_HS:
1831                         hsv_to_rgb(0.0, 0.0, v,   &col1[0][0], &col1[0][1], &col1[0][2]);
1832                         hsv_to_rgb(0.0, 0.333, v, &col1[1][0], &col1[1][1], &col1[1][2]);
1833                         hsv_to_rgb(0.0, 0.666, v, &col1[2][0], &col1[2][1], &col1[2][2]);
1834                         hsv_to_rgb(0.0, 1.0, v,   &col1[3][0], &col1[3][1], &col1[3][2]);
1835                         break;
1836                 case UI_GRAD_H:
1837                         hsv_to_rgb(0.0, 1.0, 1.0,   &col1[0][0], &col1[0][1], &col1[0][2]);
1838                         VECCOPY(col1[1], col1[0]);
1839                         VECCOPY(col1[2], col1[0]);
1840                         VECCOPY(col1[3], col1[0]);
1841                         break;
1842                 case UI_GRAD_S:
1843                         hsv_to_rgb(1.0, 0.0, 1.0,   &col1[1][0], &col1[1][1], &col1[1][2]);
1844                         VECCOPY(col1[0], col1[1]);
1845                         VECCOPY(col1[2], col1[1]);
1846                         VECCOPY(col1[3], col1[1]);
1847                         break;
1848                 case UI_GRAD_V:
1849                         hsv_to_rgb(1.0, 1.0, 0.0,   &col1[2][0], &col1[2][1], &col1[2][2]);
1850                         VECCOPY(col1[0], col1[2]);
1851                         VECCOPY(col1[1], col1[2]);
1852                         VECCOPY(col1[3], col1[2]);
1853                         break;
1854                 default:
1855                         assert(!"invalid 'type' argument");
1856                         hsv_to_rgb(1.0, 1.0, 1.0,   &col1[2][0], &col1[2][1], &col1[2][2]);
1857                         VECCOPY(col1[0], col1[2]);
1858                         VECCOPY(col1[1], col1[2]);
1859                         VECCOPY(col1[3], col1[2]);                      
1860         }
1861         
1862         /* old below */
1863         
1864         for(dx=0.0f; dx<1.0f; dx+= 0.05f) {
1865                 // previous color
1866                 VECCOPY(col0[0], col1[0]);
1867                 VECCOPY(col0[1], col1[1]);
1868                 VECCOPY(col0[2], col1[2]);
1869                 VECCOPY(col0[3], col1[3]);
1870                 
1871                 // new color
1872                 switch(type) {
1873                         case UI_GRAD_SV:
1874                                 hsv_to_rgb(h, 0.0, dx,   &col1[0][0], &col1[0][1], &col1[0][2]);
1875                                 hsv_to_rgb(h, 0.333, dx, &col1[1][0], &col1[1][1], &col1[1][2]);
1876                                 hsv_to_rgb(h, 0.666, dx, &col1[2][0], &col1[2][1], &col1[2][2]);
1877                                 hsv_to_rgb(h, 1.0, dx,   &col1[3][0], &col1[3][1], &col1[3][2]);
1878                                 break;
1879                         case UI_GRAD_HV:
1880                                 hsv_to_rgb(dx, s, 0.0,   &col1[0][0], &col1[0][1], &col1[0][2]);
1881                                 hsv_to_rgb(dx, s, 0.333, &col1[1][0], &col1[1][1], &col1[1][2]);
1882                                 hsv_to_rgb(dx, s, 0.666, &col1[2][0], &col1[2][1], &col1[2][2]);
1883                                 hsv_to_rgb(dx, s, 1.0,   &col1[3][0], &col1[3][1], &col1[3][2]);
1884                                 break;
1885                         case UI_GRAD_HS:
1886                                 hsv_to_rgb(dx, 0.0, v,   &col1[0][0], &col1[0][1], &col1[0][2]);
1887                                 hsv_to_rgb(dx, 0.333, v, &col1[1][0], &col1[1][1], &col1[1][2]);
1888                                 hsv_to_rgb(dx, 0.666, v, &col1[2][0], &col1[2][1], &col1[2][2]);
1889                                 hsv_to_rgb(dx, 1.0, v,   &col1[3][0], &col1[3][1], &col1[3][2]);
1890                                 break;
1891                         case UI_GRAD_H:
1892                                 hsv_to_rgb(dx, 1.0, 1.0,   &col1[0][0], &col1[0][1], &col1[0][2]);
1893                                 VECCOPY(col1[1], col1[0]);
1894                                 VECCOPY(col1[2], col1[0]);
1895                                 VECCOPY(col1[3], col1[0]);
1896                                 break;
1897                         case UI_GRAD_S:
1898                                 hsv_to_rgb(h, dx, 1.0,   &col1[1][0], &col1[1][1], &col1[1][2]);
1899                                 VECCOPY(col1[0], col1[1]);
1900                                 VECCOPY(col1[2], col1[1]);
1901                                 VECCOPY(col1[3], col1[1]);
1902                                 break;
1903                         case UI_GRAD_V:
1904                                 hsv_to_rgb(h, 1.0, dx,   &col1[2][0], &col1[2][1], &col1[2][2]);
1905                                 VECCOPY(col1[0], col1[2]);
1906                                 VECCOPY(col1[1], col1[2]);
1907                                 VECCOPY(col1[3], col1[2]);
1908                                 break;
1909                 }
1910                 
1911                 // rect
1912                 sx1= rect->xmin + dx*(rect->xmax-rect->xmin);
1913                 sx2= rect->xmin + (dx+0.05f)*(rect->xmax-rect->xmin);
1914                 sy= rect->ymin;
1915                 dy= (rect->ymax-rect->ymin)/3.0;
1916                 
1917                 glBegin(GL_QUADS);
1918                 for(a=0; a<3; a++, sy+=dy) {
1919                         glColor4f(col0[a][0], col0[a][1], col0[a][2], alpha);
1920                         glVertex2f(sx1, sy);
1921                         
1922                         glColor4f(col1[a][0], col1[a][1], col1[a][2], alpha);
1923                         glVertex2f(sx2, sy);
1924
1925                         glColor4f(col1[a+1][0], col1[a+1][1], col1[a+1][2], alpha);
1926                         glVertex2f(sx2, sy+dy);
1927                         
1928                         glColor4f(col0[a+1][0], col0[a+1][1], col0[a+1][2], alpha);
1929                         glVertex2f(sx1, sy+dy);
1930                 }
1931                 glEnd();
1932         }
1933         
1934         glShadeModel(GL_FLAT);
1935         
1936 }
1937
1938
1939
1940 static void ui_draw_but_HSVCUBE(uiBut *but, rcti *rect)
1941 {
1942         float rgb[3], h,s,v;
1943         float x=0.0f, y=0.0f;
1944         float *hsv= ui_block_hsv_get(but->block);
1945         float hsvn[3];
1946         
1947         h= hsv[0];
1948         s= hsv[1];
1949         v= hsv[2];
1950         
1951         ui_get_but_vectorf(but, rgb);
1952         rgb_to_hsv_compat(rgb[0], rgb[1], rgb[2], &h, &s, &v);
1953
1954         hsvn[0]= h;
1955         hsvn[1]= s;
1956         hsvn[2]= v;
1957         
1958         ui_draw_gradient(rect, hsvn, but->a1, 1.f);
1959         
1960         switch((int)but->a1) {
1961                 case UI_GRAD_SV:
1962                         x= v; y= s; break;
1963                 case UI_GRAD_HV:
1964                         x= h; y= v; break;
1965                 case UI_GRAD_HS:
1966                         x= h; y= s; break;
1967                 case UI_GRAD_H:
1968                         x= h; y= 0.5; break;
1969                 case UI_GRAD_S:
1970                         x= s; y= 0.5; break;
1971                 case UI_GRAD_V:
1972                         x= v; y= 0.5; break;
1973         }
1974         
1975         /* cursor */
1976         x= rect->xmin + x*(rect->xmax-rect->xmin);
1977         y= rect->ymin + y*(rect->ymax-rect->ymin);
1978         CLAMP(x, rect->xmin+3.0f, rect->xmax-3.0f);
1979         CLAMP(y, rect->ymin+3.0f, rect->ymax-3.0f);
1980         
1981         ui_hsv_cursor(x, y);
1982         
1983         /* outline */
1984         glColor3ub(0,  0,  0);
1985         fdrawbox((rect->xmin), (rect->ymin), (rect->xmax), (rect->ymax));
1986 }
1987
1988 /* vertical 'value' slider, using new widget code */
1989 static void ui_draw_but_HSV_v(uiBut *but, rcti *rect)
1990 {
1991         uiWidgetBase wtb;
1992         float rad= 0.5f*(rect->xmax - rect->xmin);
1993         float x, y;
1994         float rgb[3], hsv[3], v, range;
1995         int color_profile = but->block->color_profile;
1996         
1997         if (but->rnaprop && RNA_property_subtype(but->rnaprop) == PROP_COLOR_GAMMA)
1998                 color_profile = BLI_PR_NONE;
1999
2000         ui_get_but_vectorf(but, rgb);
2001         rgb_to_hsv(rgb[0], rgb[1], rgb[2], hsv, hsv+1, hsv+2);
2002         v = hsv[2];
2003         
2004         if (color_profile)
2005                 v = linearrgb_to_srgb(v);
2006
2007         /* map v from property range to [0,1] */
2008         range = but->softmax - but->softmin;
2009         v =     (v - but->softmin)/range;
2010         
2011         widget_init(&wtb);
2012         
2013         /* fully rounded */
2014         round_box_edges(&wtb, 15, rect, rad);
2015         
2016         /* setup temp colors */
2017         wcol_tmp.outline[0]= wcol_tmp.outline[1]= wcol_tmp.outline[2]= 0;
2018         wcol_tmp.inner[0]= wcol_tmp.inner[1]= wcol_tmp.inner[2]= 128;
2019         wcol_tmp.shadetop= 127;
2020         wcol_tmp.shadedown= -128;
2021         wcol_tmp.shaded= 1;
2022         
2023         widgetbase_draw(&wtb, &wcol_tmp);
2024
2025         /* cursor */
2026         x= rect->xmin + 0.5f * (rect->xmax-rect->xmin);
2027         y= rect->ymin + v * (rect->ymax-rect->ymin);
2028         CLAMP(y, rect->ymin+3.0f, rect->ymax-3.0f);
2029         
2030         ui_hsv_cursor(x, y);
2031         
2032 }
2033
2034
2035 /* ************ separator, for menus etc ***************** */
2036 static void ui_draw_separator(rcti *rect,  uiWidgetColors *wcol)
2037 {
2038         int y = rect->ymin + (rect->ymax - rect->ymin)/2 - 1;
2039         unsigned char col[4];
2040         
2041         col[0] = wcol->text[0];
2042         col[1] = wcol->text[1];
2043         col[2] = wcol->text[2];
2044         col[3] = 7;
2045         
2046         glEnable(GL_BLEND);
2047         glColor4ubv(col);
2048         sdrawline(rect->xmin, y, rect->xmax, y);
2049         glDisable(GL_BLEND);
2050 }
2051
2052 /* ************ button callbacks, draw ***************** */
2053
2054 static void widget_numbut(uiWidgetColors *wcol, rcti *rect, int state, int roundboxalign)
2055 {
2056         uiWidgetBase wtb;
2057         float rad= 0.5f*(rect->ymax - rect->ymin);
2058         float textofs = rad*0.75f;
2059
2060         if(state & UI_SELECT)
2061                 SWAP(short, wcol->shadetop, wcol->shadedown);
2062         
2063         widget_init(&wtb);
2064         
2065         /* fully rounded */
2066         round_box_edges(&wtb, roundboxalign, rect, rad);
2067         
2068         /* decoration */
2069         if(!(state & UI_TEXTINPUT)) {
2070                 widget_num_tria(&wtb.tria1, rect, 0.6f, 'l');
2071                 widget_num_tria(&wtb.tria2, rect, 0.6f, 'r');
2072         }
2073
2074         widgetbase_draw(&wtb, wcol);
2075         
2076         /* text space */
2077         rect->xmin += textofs;
2078         rect->xmax -= textofs;
2079 }
2080
2081 //static int ui_link_bezier_points(rcti *rect, float coord_array[][2], int resol)
2082 int ui_link_bezier_points(rcti *rect, float coord_array[][2], int resol)
2083 {
2084         float dist, vec[4][2];
2085
2086         vec[0][0]= rect->xmin;
2087         vec[0][1]= rect->ymin;
2088         vec[3][0]= rect->xmax;
2089         vec[3][1]= rect->ymax;
2090         
2091         dist= 0.5f*ABS(vec[0][0] - vec[3][0]);
2092         
2093         vec[1][0]= vec[0][0]+dist;
2094         vec[1][1]= vec[0][1];
2095         
2096         vec[2][0]= vec[3][0]-dist;
2097         vec[2][1]= vec[3][1];
2098         
2099         forward_diff_bezier(vec[0][0], vec[1][0], vec[2][0], vec[3][0], coord_array[0], resol, sizeof(float)*2);
2100         forward_diff_bezier(vec[0][1], vec[1][1], vec[2][1], vec[3][1], coord_array[0]+1, resol, sizeof(float)*2);
2101         
2102         return 1;
2103 }
2104
2105 #define LINK_RESOL      24
2106 void ui_draw_link_bezier(rcti *rect)
2107 {
2108         float coord_array[LINK_RESOL+1][2];
2109         
2110         if(ui_link_bezier_points(rect, coord_array, LINK_RESOL)) {
2111                 /* we can reuse the dist variable here to increment the GL curve eval amount*/
2112                 // const float dist= 1.0f/(float)LINK_RESOL; // UNUSED
2113                 int i;
2114
2115                 glEnable(GL_BLEND);
2116                 glEnable(GL_LINE_SMOOTH);
2117                 
2118                 glBegin(GL_LINE_STRIP);
2119                 for(i=0; i<=LINK_RESOL; i++) {
2120                         glVertex2fv(coord_array[i]);
2121                 }
2122                 glEnd();
2123                 
2124                 glDisable(GL_BLEND);
2125                 glDisable(GL_LINE_SMOOTH);
2126
2127         }
2128 }
2129
2130 /* function in use for buttons and for view2d sliders */
2131 void uiWidgetScrollDraw(uiWidgetColors *wcol, rcti *rect, rcti *slider, int state)
2132 {
2133         uiWidgetBase wtb;
2134         float rad;
2135         int horizontal;
2136         short outline=0;
2137
2138         widget_init(&wtb);
2139
2140         /* determine horizontal/vertical */
2141         horizontal= (rect->xmax - rect->xmin > rect->ymax - rect->ymin);
2142         
2143         if(horizontal)
2144                 rad= 0.5f*(rect->ymax - rect->ymin);
2145         else
2146                 rad= 0.5f*(rect->xmax - rect->xmin);
2147         
2148         wtb.shadedir= (horizontal)? 1: 0;
2149         
2150         /* draw back part, colors swapped and shading inverted */
2151         if(horizontal)
2152                 SWAP(short, wcol->shadetop, wcol->shadedown);
2153         
2154         round_box_edges(&wtb, 15, rect, rad); 
2155         widgetbase_draw(&wtb, wcol);
2156         
2157         /* slider */
2158         if(slider->xmax-slider->xmin<2 || slider->ymax-slider->ymin<2);
2159         else {
2160                 
2161                 SWAP(short, wcol->shadetop, wcol->shadedown);
2162                 
2163                 QUATCOPY(wcol->inner, wcol->item);
2164                 
2165                 if(wcol->shadetop>wcol->shadedown)
2166                         wcol->shadetop+= 20;    /* XXX violates themes... */
2167                 else wcol->shadedown+= 20;
2168                 
2169                 if(state & UI_SCROLL_PRESSED) {
2170                         wcol->inner[0]= wcol->inner[0]>=250? 255 : wcol->inner[0]+5;
2171                         wcol->inner[1]= wcol->inner[1]>=250? 255 : wcol->inner[1]+5;
2172                         wcol->inner[2]= wcol->inner[2]>=250? 255 : wcol->inner[2]+5;
2173                 }
2174
2175                 /* draw */
2176                 wtb.emboss= 0; /* only emboss once */
2177                 
2178                 /* exception for progress bar */
2179                 if (state & UI_SCROLL_NO_OUTLINE)       
2180                         SWAP(short, outline, wtb.outline);
2181                 
2182                 round_box_edges(&wtb, 15, slider, rad); 
2183                 
2184                 if(state & UI_SCROLL_ARROWS) {
2185                         if(wcol->item[0] > 48) wcol->item[0]-= 48;
2186                         if(wcol->item[1] > 48) wcol->item[1]-= 48;
2187                         if(wcol->item[2] > 48) wcol->item[2]-= 48;
2188                         wcol->item[3]= 255;
2189                         
2190                         if(horizontal) {
2191                                 widget_scroll_circle(&wtb.tria1, slider, 0.6f, 'l');
2192                                 widget_scroll_circle(&wtb.tria2, slider, 0.6f, 'r');
2193                         }
2194                         else {
2195                                 widget_scroll_circle(&wtb.tria1, slider, 0.6f, 'b');
2196                                 widget_scroll_circle(&wtb.tria2, slider, 0.6f, 't');
2197                         }
2198                 }
2199                 widgetbase_draw(&wtb, wcol);
2200                 
2201                 if (state & UI_SCROLL_NO_OUTLINE)
2202                         SWAP(short, outline, wtb.outline);
2203         }       
2204 }
2205
2206 static void widget_scroll(uiBut *but, uiWidgetColors *wcol, rcti *rect, int state, int UNUSED(roundboxalign))
2207 {
2208         rcti rect1;
2209         double value;
2210         float fac, size, min;
2211         int horizontal;
2212
2213         /* calculate slider part */
2214         value= ui_get_but_val(but);
2215
2216         size= (but->softmax + but->a1 - but->softmin);
2217         size= MAX2(size, 2);
2218         
2219         /* position */
2220         rect1= *rect;
2221
2222         /* determine horizontal/vertical */
2223         horizontal= (rect->xmax - rect->xmin > rect->ymax - rect->ymin);
2224         
2225         if(horizontal) {
2226                 fac= (rect->xmax - rect->xmin)/(size);
2227                 rect1.xmin= rect1.xmin + ceilf(fac*((float)value - but->softmin));
2228                 rect1.xmax= rect1.xmin + ceilf(fac*(but->a1 - but->softmin));
2229
2230                 /* ensure minimium size */
2231                 min= rect->ymax - rect->ymin;
2232
2233                 if(rect1.xmax - rect1.xmin < min) {
2234                         rect1.xmax= rect1.xmin + min;
2235
2236                         if(rect1.xmax > rect->xmax) {
2237                                 rect1.xmax= rect->xmax;
2238                                 rect1.xmin= MAX2(rect1.xmax - min, rect->xmin);
2239                         }
2240                 }
2241         }
2242         else {
2243                 fac= (rect->ymax - rect->ymin)/(size);
2244                 rect1.ymax= rect1.ymax - ceilf(fac*((float)value - but->softmin));
2245                 rect1.ymin= rect1.ymax - ceilf(fac*(but->a1 - but->softmin));
2246
2247                 /* ensure minimium size */
2248                 min= rect->xmax - rect->xmin;
2249
2250                 if(rect1.ymax - rect1.ymin < min) {
2251                         rect1.ymax= rect1.ymin + min;
2252
2253                         if(rect1.ymax > rect->ymax) {
2254                                 rect1.ymax= rect->ymax;
2255                                 rect1.ymin= MAX2(rect1.ymax - min, rect->ymin);
2256                         }
2257                 }
2258         }
2259
2260         if(state & UI_SELECT)
2261                 state= UI_SCROLL_PRESSED;
2262         else
2263                 state= 0;
2264         uiWidgetScrollDraw(wcol, rect, &rect1, state);
2265 }
2266
2267 static void widget_progressbar(uiBut *but, uiWidgetColors *wcol, rcti *rect, int UNUSED(state), int UNUSED(roundboxalign))
2268 {
2269         rcti rect_prog = *rect, rect_bar = *rect;
2270         float value = but->a1;
2271         float w, min;
2272         
2273         /* make the progress bar a proportion of the original height */
2274         /* hardcoded 4px high for now */
2275         rect_prog.ymax = rect_prog.ymin + 4;
2276         rect_bar.ymax = rect_bar.ymin + 4;
2277         
2278         w = value * (rect_prog.xmax - rect_prog.xmin);
2279         
2280         /* ensure minimium size */
2281         min= rect_prog.ymax - rect_prog.ymin;
2282         w = MAX2(w, min);
2283         
2284         rect_bar.xmax = rect_bar.xmin + w;
2285                 
2286         uiWidgetScrollDraw(wcol, &rect_prog, &rect_bar, UI_SCROLL_NO_OUTLINE);
2287         
2288         /* raise text a bit */
2289         rect->ymin += 6;
2290         rect->xmin -= 6;
2291 }
2292
2293 static void widget_link(uiBut *but, uiWidgetColors *UNUSED(wcol), rcti *rect, int UNUSED(state), int UNUSED(roundboxalign))
2294 {
2295         
2296         if(but->flag & UI_SELECT) {
2297                 rcti rectlink;
2298                 
2299                 UI_ThemeColor(TH_TEXT_HI);
2300                 
2301                 rectlink.xmin= (rect->xmin+rect->xmax)/2;
2302                 rectlink.ymin= (rect->ymin+rect->ymax)/2;
2303                 rectlink.xmax= but->linkto[0];
2304                 rectlink.ymax= but->linkto[1];
2305                 
2306                 ui_draw_link_bezier(&rectlink);
2307         }
2308 }
2309
2310 static void widget_numslider(uiBut *but, uiWidgetColors *wcol, rcti *rect, int state, int roundboxalign)
2311 {
2312         uiWidgetBase wtb, wtb1;
2313         rcti rect1;
2314         double value;
2315         float offs, toffs, fac;
2316         char outline[3];
2317
2318         widget_init(&wtb);
2319         widget_init(&wtb1);
2320         
2321         /* backdrop first */
2322         
2323         /* fully rounded */
2324         offs= 0.5f*(rect->ymax - rect->ymin);
2325         toffs = offs*0.75f;
2326         round_box_edges(&wtb, roundboxalign, rect, offs);
2327
2328         wtb.outline= 0;
2329         widgetbase_draw(&wtb, wcol);
2330         
2331         /* slider part */
2332         VECCOPY(outline, wcol->outline);
2333         VECCOPY(wcol->outline, wcol->item);
2334         VECCOPY(wcol->inner, wcol->item);
2335
2336         if(!(state & UI_SELECT))
2337                 SWAP(short, wcol->shadetop, wcol->shadedown);
2338         
2339         rect1= *rect;
2340         
2341         value= ui_get_but_val(but);
2342         fac= ((float)value-but->softmin)*(rect1.xmax - rect1.xmin - offs)/(but->softmax - but->softmin);
2343         
2344         /* left part of slider, always rounded */
2345         rect1.xmax= rect1.xmin + ceil(offs+1.0f);
2346         round_box_edges(&wtb1, roundboxalign & ~6, &rect1, offs);
2347         wtb1.outline= 0;
2348         widgetbase_draw(&wtb1, wcol);
2349         
2350         /* right part of slider, interpolate roundness */
2351         rect1.xmax= rect1.xmin + fac + offs;
2352         rect1.xmin+=  floor(offs-1.0f);
2353         if(rect1.xmax + offs > rect->xmax)
2354                 offs*= (rect1.xmax + offs - rect->xmax)/offs;
2355         else 
2356                 offs= 0.0f;
2357         round_box_edges(&wtb1, roundboxalign & ~9, &rect1, offs);
2358         
2359         widgetbase_draw(&wtb1, wcol);
2360         VECCOPY(wcol->outline, outline);
2361         
2362         if(!(state & UI_SELECT))
2363                 SWAP(short, wcol->shadetop, wcol->shadedown);
2364         
2365         /* outline */
2366         wtb.outline= 1;
2367         wtb.inner= 0;
2368         widgetbase_draw(&wtb, wcol);
2369         
2370         /* text space */
2371         rect->xmin += toffs;
2372         rect->xmax -= toffs;
2373 }
2374
2375 /* I think 3 is sufficient border to indicate keyed status */
2376 #define SWATCH_KEYED_BORDER 3
2377
2378 static void widget_swatch(uiBut *but, uiWidgetColors *wcol, rcti *rect, int state, int roundboxalign)
2379 {
2380         uiWidgetBase wtb;
2381         float col[4];
2382         int color_profile = but->block->color_profile;
2383         
2384         col[3]= 1.0f;
2385
2386         if (but->rnaprop) {
2387                 if (RNA_property_subtype(but->rnaprop) == PROP_COLOR_GAMMA)
2388                         color_profile = BLI_PR_NONE;
2389
2390                 if(RNA_property_array_length(&but->rnapoin, but->rnaprop)==4) {
2391                         col[3]= RNA_property_float_get_index(&but->rnapoin, but->rnaprop, 3);
2392                 }
2393         }
2394         
2395         widget_init(&wtb);
2396         
2397         /* half rounded */
2398         round_box_edges(&wtb, roundboxalign, rect, 5.0f);
2399                 
2400         ui_get_but_vectorf(but, col);
2401         
2402         if(state & (UI_BUT_ANIMATED|UI_BUT_ANIMATED_KEY|UI_BUT_DRIVEN|UI_BUT_REDALERT)) {
2403                 // draw based on state - color for keyed etc
2404                 widgetbase_draw(&wtb, wcol);
2405                 
2406                 // inset to draw swatch color
2407                 rect->xmin+= SWATCH_KEYED_BORDER;
2408                 rect->xmax-= SWATCH_KEYED_BORDER;
2409                 rect->ymin+= SWATCH_KEYED_BORDER;
2410                 rect->ymax-= SWATCH_KEYED_BORDER;
2411                 
2412                 round_box_edges(&wtb, roundboxalign, rect, 5.0f);
2413         }
2414         
2415         if (color_profile)
2416                 linearrgb_to_srgb_v3_v3(col, col);
2417         
2418         wcol->inner[0]= FTOCHAR(col[0]);
2419         wcol->inner[1]= FTOCHAR(col[1]);
2420         wcol->inner[2]= FTOCHAR(col[2]);
2421         wcol->inner[3]= FTOCHAR(col[3]);
2422         wcol->shaded = 0;
2423         wcol->alpha_check = (wcol->inner[3] < 255);
2424
2425         widgetbase_draw(&wtb, wcol);
2426         
2427 }
2428
2429 static void widget_icon_has_anim(uiBut *UNUSED(but), uiWidgetColors *wcol, rcti *rect, int state, int UNUSED(roundboxalign))
2430 {
2431         if(state & (UI_BUT_ANIMATED|UI_BUT_ANIMATED_KEY|UI_BUT_DRIVEN|UI_BUT_REDALERT)) {
2432                 uiWidgetBase wtb;
2433         
2434                 widget_init(&wtb);
2435                 wtb.outline= 0;
2436                 
2437                 /* rounded */
2438                 round_box_edges(&wtb, 15, rect, 10.0f);
2439                 widgetbase_draw(&wtb, wcol);
2440         }       
2441 }
2442
2443
2444 static void widget_textbut(uiWidgetColors *wcol, rcti *rect, int state, int roundboxalign)
2445 {
2446         uiWidgetBase wtb;
2447         
2448         if(state & UI_SELECT)
2449                 SWAP(short, wcol->shadetop, wcol->shadedown);
2450         
2451         widget_init(&wtb);
2452         
2453         /* half rounded */
2454         round_box_edges(&wtb, roundboxalign, rect, 4.0f);
2455         
2456         widgetbase_draw(&wtb, wcol);
2457
2458 }
2459
2460
2461 static void widget_menubut(uiWidgetColors *wcol, rcti *rect, int UNUSED(state), int roundboxalign)
2462 {
2463         uiWidgetBase wtb;
2464         
2465         widget_init(&wtb);
2466         
2467         /* half rounded */
2468         round_box_edges(&wtb, roundboxalign, rect, 4.0f);
2469         
2470         /* decoration */
2471         widget_menu_trias(&wtb.tria1, rect);
2472         
2473         widgetbase_draw(&wtb, wcol);
2474         
2475         /* text space */
2476         rect->xmax -= (rect->ymax-rect->ymin);
2477 }
2478
2479 static void widget_menuiconbut(uiWidgetColors *wcol, rcti *rect, int UNUSED(state), int roundboxalign)
2480 {
2481         uiWidgetBase wtb;
2482         
2483         widget_init(&wtb);
2484         
2485         /* half rounded */
2486         round_box_edges(&wtb, roundboxalign, rect, 4.0f);
2487         
2488         /* decoration */
2489         widgetbase_draw(&wtb, wcol);
2490 }
2491
2492 static void widget_pulldownbut(uiWidgetColors *wcol, rcti *rect, int state, int UNUSED(roundboxalign))
2493 {
2494         if(state & UI_ACTIVE) {
2495                 uiWidgetBase wtb;
2496                 float rad= 0.5f*(rect->ymax - rect->ymin); // 4.0f
2497                 
2498                 widget_init(&wtb);
2499                 
2500                 /* half rounded */
2501                 round_box_edges(&wtb, 15, rect, rad);
2502                 
2503                 widgetbase_draw(&wtb, wcol);
2504         }
2505 }
2506
2507 static void widget_menu_itembut(uiWidgetColors *wcol, rcti *rect, int UNUSED(state), int UNUSED(roundboxalign))
2508 {
2509         uiWidgetBase wtb;
2510         
2511         widget_init(&wtb);
2512         
2513         /* not rounded, no outline */
2514         wtb.outline= 0;
2515         round_box_edges(&wtb, 0, rect, 0.0f);
2516         
2517         widgetbase_draw(&wtb, wcol);
2518 }
2519
2520 static void widget_list_itembut(uiWidgetColors *wcol, rcti *rect, int UNUSED(state), int UNUSED(roundboxalign))
2521 {
2522         uiWidgetBase wtb;
2523         
2524         widget_init(&wtb);
2525         
2526         /* rounded, but no outline */
2527         wtb.outline= 0;
2528         round_box_edges(&wtb, 15, rect, 4.0f);
2529         
2530         widgetbase_draw(&wtb, wcol);
2531 }
2532
2533 static void widget_optionbut(uiWidgetColors *wcol, rcti *rect, int state, int UNUSED(roundboxalign))
2534 {
2535         uiWidgetBase wtb;
2536         rcti recttemp= *rect;
2537         int delta;
2538         
2539         widget_init(&wtb);
2540         
2541         /* square */
2542         recttemp.xmax= recttemp.xmin + (recttemp.ymax-recttemp.ymin);
2543         
2544         /* smaller */
2545         delta= 1 + (recttemp.ymax-recttemp.ymin)/8;
2546         recttemp.xmin+= delta;
2547         recttemp.ymin+= delta;
2548         recttemp.xmax-= delta;
2549         recttemp.ymax-= delta;
2550         
2551         /* half rounded */
2552         round_box_edges(&wtb, 15, &recttemp, 4.0f);
2553         
2554         /* decoration */
2555         if(state & UI_SELECT) {
2556                 widget_check_trias(&wtb.tria1, &recttemp);
2557         }
2558         
2559         widgetbase_draw(&wtb, wcol);
2560         
2561         /* text space */
2562         rect->xmin += (rect->ymax-rect->ymin)*0.7 + delta;
2563 }
2564
2565
2566 static void widget_radiobut(uiWidgetColors *wcol, rcti *rect, int UNUSED(state), int roundboxalign)
2567 {
2568         uiWidgetBase wtb;
2569         
2570         widget_init(&wtb);
2571         
2572         /* half rounded */
2573         round_box_edges(&wtb, roundboxalign, rect, 4.0f);
2574         
2575         widgetbase_draw(&wtb, wcol);
2576
2577 }
2578
2579 static void widget_box(uiBut *but, uiWidgetColors *wcol, rcti *rect, int UNUSED(state), int roundboxalign)
2580 {
2581         uiWidgetBase wtb;
2582         char old_col[3];
2583         
2584         widget_init(&wtb);
2585         
2586         VECCOPY(old_col, wcol->inner);
2587         
2588         /* abuse but->hsv - if it's non-zero, use this color as the box's background */
2589         if (but->col[3]) {
2590                 wcol->inner[0] = but->col[0];
2591                 wcol->inner[1] = but->col[1];
2592                 wcol->inner[2] = but->col[2];
2593         }
2594         
2595         /* half rounded */
2596         round_box_edges(&wtb, roundboxalign, rect, 4.0f);
2597         
2598         widgetbase_draw(&wtb, wcol);
2599         
2600         /* store the box bg as gl clearcolor, to retrieve later when drawing semi-transparent rects
2601          * over the top to indicate disabled buttons */
2602         glClearColor(wcol->inner[0]/255.0, wcol->inner[1]/255.0, wcol->inner[2]/255.0, 1.0);
2603         
2604         VECCOPY(wcol->inner, old_col);
2605 }
2606
2607 static void widget_but(uiWidgetColors *wcol, rcti *rect, int UNUSED(state), int roundboxalign)
2608 {
2609         uiWidgetBase wtb;
2610         
2611         widget_init(&wtb);
2612         
2613         /* half rounded */
2614         round_box_edges(&wtb, roundboxalign, rect, 4.0f);
2615                 
2616         widgetbase_draw(&wtb, wcol);
2617
2618 }
2619
2620 static void widget_roundbut(uiWidgetColors *wcol, rcti *rect, int UNUSED(state), int roundboxalign)
2621 {
2622         uiWidgetBase wtb;
2623         float rad= 5.0f; //0.5f*(rect->ymax - rect->ymin);
2624         
2625         widget_init(&wtb);
2626         
2627         /* half rounded */
2628         round_box_edges(&wtb, roundboxalign, rect, rad);
2629
2630         widgetbase_draw(&wtb, wcol);
2631 }
2632
2633 static void widget_draw_extra_mask(const bContext *C, uiBut *but, uiWidgetType *wt, rcti *rect)
2634 {
2635         uiWidgetBase wtb;
2636         unsigned char col[4];
2637         
2638         /* state copy! */
2639         wt->wcol= *(wt->wcol_theme);
2640         
2641         widget_init(&wtb);
2642         
2643         if(but->block->drawextra) {
2644                 /* note: drawextra can change rect +1 or -1, to match round errors of existing previews */
2645                 but->block->drawextra(C, but->poin, but->block->drawextra_arg1, but->block->drawextra_arg2, rect);
2646                 
2647                 /* make mask to draw over image */
2648                 UI_GetThemeColor3ubv(TH_BACK, col);
2649                 glColor3ubv(col);
2650                 
2651                 round_box__edges(&wtb, 15, rect, 0.0f, 4.0);
2652                 widgetbase_outline(&wtb);
2653         }
2654         
2655         /* outline */
2656         round_box_edges(&wtb, 15, rect, 5.0f);
2657         wtb.outline= 1;
2658         wtb.inner= 0;
2659         widgetbase_draw(&wtb, &wt->wcol);
2660         
2661 }
2662
2663
2664 static void widget_disabled(rcti *rect)
2665 {
2666         float col[4];
2667         
2668         glEnable(GL_BLEND);
2669         
2670         /* can't use theme TH_BACK or TH_PANEL... undefined */
2671         glGetFloatv(GL_COLOR_CLEAR_VALUE, col);
2672         glColor4f(col[0], col[1], col[2], 0.5f);
2673
2674         /* need -1 and +1 to make it work right for aligned buttons,
2675          * but problem may be somewhere else? */
2676         glRectf(rect->xmin-1, rect->ymin-1, rect->xmax, rect->ymax+1);
2677         
2678         glDisable(GL_BLEND);
2679 }
2680
2681 static uiWidgetType *widget_type(uiWidgetTypeEnum type)
2682 {
2683         bTheme *btheme= U.themes.first;
2684         static uiWidgetType wt;
2685         
2686         /* defaults */
2687         wt.wcol_theme= &btheme->tui.wcol_regular;
2688         wt.wcol_state= &btheme->tui.wcol_state;
2689         wt.state= widget_state;
2690         wt.draw= widget_but;
2691         wt.custom= NULL;
2692         wt.text= widget_draw_text_icon;
2693         
2694         switch(type) {
2695                 case UI_WTYPE_REGULAR:
2696                         break;
2697
2698                 case UI_WTYPE_LABEL:
2699                         wt.draw= NULL;
2700                         wt.state= widget_state_label;
2701                         break;
2702                         
2703                 case UI_WTYPE_TOGGLE:
2704                         wt.wcol_theme= &btheme->tui.wcol_toggle;
2705                         break;
2706                         
2707                 case UI_WTYPE_OPTION:
2708                         wt.wcol_theme= &btheme->tui.wcol_option;
2709                         wt.draw= widget_optionbut;
2710                         break;
2711                         
2712                 case UI_WTYPE_RADIO:
2713                         wt.wcol_theme= &btheme->tui.wcol_radio;
2714                         wt.draw= widget_radiobut;
2715                         break;
2716
2717                 case UI_WTYPE_NUMBER:
2718                         wt.wcol_theme= &btheme->tui.wcol_num;
2719                         wt.draw= widget_numbut;
2720                         break;
2721                         
2722                 case UI_WTYPE_SLIDER:
2723                         wt.wcol_theme= &btheme->tui.wcol_numslider;
2724                         wt.custom= widget_numslider;
2725                         wt.state= widget_state_numslider;
2726                         break;
2727                         
2728                 case UI_WTYPE_EXEC:
2729                         wt.wcol_theme= &btheme->tui.wcol_tool;
2730                         wt.draw= widget_roundbut;
2731                         break;
2732                         
2733                         
2734                         /* strings */
2735                 case UI_WTYPE_NAME:
2736                         wt.wcol_theme= &btheme->tui.wcol_text;
2737                         wt.draw= widget_textbut;
2738                         break;
2739                         
2740                 case UI_WTYPE_NAME_LINK:
2741                         break;
2742                         
2743                 case UI_WTYPE_POINTER_LINK:
2744                         break;
2745                         
2746                 case UI_WTYPE_FILENAME:
2747                         break;
2748                         
2749                         
2750                         /* start menus */
2751                 case UI_WTYPE_MENU_RADIO:
2752                         wt.wcol_theme= &btheme->tui.wcol_menu;
2753                         wt.draw= widget_menubut;
2754                         break;
2755
2756                 case UI_WTYPE_MENU_ICON_RADIO:
2757                         wt.wcol_theme= &btheme->tui.wcol_menu;
2758                         wt.draw= widget_menuiconbut;
2759                         break;
2760                         
2761                 case UI_WTYPE_MENU_POINTER_LINK:
2762                         wt.wcol_theme= &btheme->tui.wcol_menu;
2763                         wt.draw= widget_menubut;
2764                         break;
2765                         
2766                 case UI_WTYPE_PULLDOWN:
2767                         wt.wcol_theme= &btheme->tui.wcol_pulldown;
2768                         wt.draw= widget_pulldownbut;
2769                         wt.state= widget_state_pulldown;
2770                         break;
2771                         
2772                         /* in menus */
2773                 case UI_WTYPE_MENU_ITEM:
2774                         wt.wcol_theme= &btheme->tui.wcol_menu_item;
2775                         wt.draw= widget_menu_itembut;
2776                         wt.state= widget_state_menu_item;
2777                         break;
2778                         
2779                 case UI_WTYPE_MENU_BACK:
2780                         wt.wcol_theme= &btheme->tui.wcol_menu_back;
2781                         wt.draw= widget_menu_back;
2782                         break;
2783                         
2784                         /* specials */
2785                 case UI_WTYPE_ICON:
2786                         wt.custom= widget_icon_has_anim;
2787                         break;
2788                         
2789                 case UI_WTYPE_SWATCH:
2790                         wt.custom= widget_swatch;
2791                         break;
2792                         
2793                 case UI_WTYPE_BOX:
2794                         wt.custom= widget_box;
2795                         wt.wcol_theme= &btheme->tui.wcol_box;
2796                         break;
2797                         
2798                 case UI_WTYPE_RGB_PICKER:
2799                         break;
2800                         
2801                 case UI_WTYPE_NORMAL:
2802                         break;
2803
2804                 case UI_WTYPE_SCROLL:
2805                         wt.wcol_theme= &btheme->tui.wcol_scroll;
2806                         wt.state= widget_state_nothing;
2807                         wt.custom= widget_scroll;
2808                         break;
2809
2810                 case UI_WTYPE_LISTITEM:
2811                         wt.wcol_theme= &btheme->tui.wcol_list_item;
2812                         wt.draw= widget_list_itembut;
2813                         break;
2814                         
2815                 case UI_WTYPE_PROGRESSBAR:
2816                         wt.wcol_theme= &btheme->tui.wcol_progress;
2817                         wt.custom= widget_progressbar;
2818                         break;
2819         }
2820         
2821         return &wt;
2822 }
2823
2824
2825 static int widget_roundbox_set(uiBut *but, rcti *rect)
2826 {
2827         /* alignment */
2828         if(but->flag & UI_BUT_ALIGN) {
2829                 
2830                 if(but->flag & UI_BUT_ALIGN_TOP)
2831                         rect->ymax+= 1;
2832                 if(but->flag & UI_BUT_ALIGN_LEFT)
2833                         rect->xmin-= 1;
2834                 
2835                 switch(but->flag & UI_BUT_ALIGN) {
2836                         case UI_BUT_ALIGN_TOP:
2837                                 return (12);
2838                                 break;
2839                         case UI_BUT_ALIGN_DOWN:
2840                                 return (3);
2841                                 break;
2842                         case UI_BUT_ALIGN_LEFT:
2843                                 return (6);
2844                                 break;
2845                         case UI_BUT_ALIGN_RIGHT:
2846                                 return (9);
2847                                 break;
2848                                 
2849                         case UI_BUT_ALIGN_DOWN|UI_BUT_ALIGN_RIGHT:
2850                                 return (1);
2851                                 break;
2852                         case UI_BUT_ALIGN_DOWN|UI_BUT_ALIGN_LEFT:
2853                                 return (2);
2854                                 break;
2855                         case UI_BUT_ALIGN_TOP|UI_BUT_ALIGN_RIGHT:
2856                                 return (8);
2857                                 break;
2858                         case UI_BUT_ALIGN_TOP|UI_BUT_ALIGN_LEFT:
2859                                 return (4);
2860                                 break;
2861                                 
2862                         default:
2863                                 return (0);
2864                                 break;
2865                 }
2866         } 
2867         return 15;
2868 }
2869
2870 /* conversion from old to new buttons, so still messy */
2871 void ui_draw_but(const bContext *C, ARegion *ar, uiStyle *style, uiBut *but, rcti *rect)
2872 {
2873         bTheme *btheme= U.themes.first;
2874         ThemeUI *tui= &btheme->tui;
2875         uiFontStyle *fstyle= &style->widget;
2876         uiWidgetType *wt= NULL;
2877         
2878         /* handle menus separately */
2879         if(but->dt==UI_EMBOSSP) {
2880                 switch (but->type) {
2881                         case LABEL:
2882                                 widget_draw_text_icon(&style->widgetlabel, &tui->wcol_menu_back, but, rect);
2883                                 break;
2884                         case SEPR:
2885                                 ui_draw_separator(rect, &tui->wcol_menu_item);
2886                                 break;
2887                                 
2888                         default:
2889                                 wt= widget_type(UI_WTYPE_MENU_ITEM);
2890                 }
2891         }
2892         else if(but->dt==UI_EMBOSSN) {
2893                 /* "nothing" */
2894                 wt= widget_type(UI_WTYPE_ICON);
2895         }
2896         else {
2897                 
2898                 switch (but->type) {
2899                         case LABEL:
2900                                 if(but->block->flag & UI_BLOCK_LOOP)
2901                                         widget_draw_text_icon(&style->widgetlabel, &tui->wcol_menu_back, but, rect);
2902                                 else {
2903                                         wt= widget_type(UI_WTYPE_LABEL);
2904                                         fstyle= &style->widgetlabel;
2905                                 }
2906                                 break;
2907                                 
2908                         case SEPR:
2909                                 break;
2910                                 
2911                         case BUT:
2912                                 wt= widget_type(UI_WTYPE_EXEC);
2913                                 break;
2914
2915                         case NUM:
2916                                 wt= widget_type(UI_WTYPE_NUMBER);
2917                                 break;
2918                                 
2919                         case NUMSLI:
2920                         case HSVSLI:
2921                                 wt= widget_type(UI_WTYPE_SLIDER);
2922                                 break;
2923                                 
2924                         case ROW:
2925                                 wt= widget_type(UI_WTYPE_RADIO);
2926                                 break;
2927
2928                         case LISTROW:
2929                                 wt= widget_type(UI_WTYPE_LISTITEM);
2930                                 break;
2931                                 
2932                         case TEX:
2933                                 wt= widget_type(UI_WTYPE_NAME);
2934                                 break;
2935                                 
2936                         case SEARCH_MENU:
2937                                 wt= widget_type(UI_WTYPE_NAME);
2938                                 if(but->block->flag & UI_BLOCK_LOOP)
2939                                         wt->wcol_theme= &btheme->tui.wcol_menu_back;
2940                                 break;
2941                                 
2942                         case TOGBUT:
2943                         case TOG:
2944                         case TOGN:
2945                         case TOG3:
2946                                 wt= widget_type(UI_WTYPE_TOGGLE);
2947                                 break;
2948                                 
2949                         case OPTION:
2950                         case OPTIONN:
2951                                 if (!(but->flag & UI_HAS_ICON)) {
2952                                         wt= widget_type(UI_WTYPE_OPTION);
2953                                         but->flag |= UI_TEXT_LEFT;
2954                                 }
2955                                 else
2956                                         wt= widget_type(UI_WTYPE_TOGGLE);
2957                                 
2958                                 /* option buttons have strings outside, on menus use different colors */
2959                                 if(but->block->flag & UI_BLOCK_LOOP)
2960                                         wt->state= widget_state_option_menu;
2961                                 
2962                                 break;
2963                                 
2964                         case MENU:
2965                         case BLOCK:
2966                         case ICONTEXTROW:
2967                                 if(!but->str[0] && but->icon)
2968                                         wt= widget_type(UI_WTYPE_MENU_ICON_RADIO);
2969                                 else
2970                                         wt= widget_type(UI_WTYPE_MENU_RADIO);
2971                                 break;
2972                                 
2973                         case PULLDOWN:
2974                                 wt= widget_type(UI_WTYPE_PULLDOWN);
2975                                 break;
2976                         
2977                         case BUTM:
2978                                 wt= widget_type(UI_WTYPE_MENU_ITEM);
2979                                 break;
2980                                 
2981                         case COL:
2982                                 wt= widget_type(UI_WTYPE_SWATCH);
2983                                 break;
2984                                 
2985                         case ROUNDBOX:
2986                         case LISTBOX:
2987                                 wt= widget_type(UI_WTYPE_BOX);
2988                                 break;
2989                                 
2990                         case LINK:
2991                         case INLINK:
2992                                 wt= widget_type(UI_WTYPE_ICON);
2993                                 wt->custom= widget_link;
2994                                 
2995                                 break;
2996                         
2997                         case BUT_EXTRA:
2998                                 widget_draw_extra_mask(C, but, widget_type(UI_WTYPE_BOX), rect);
2999                                 break;
3000                                 
3001                         case HSVCUBE:
3002                                 if(but->a1 == UI_GRAD_V_ALT) // vertical V slider, uses new widget draw now
3003                                         ui_draw_but_HSV_v(but, rect);
3004                                 else  // other HSV pickers...
3005                                         ui_draw_but_HSVCUBE(but, rect);
3006                                 break;
3007                                 
3008                         case HSVCIRCLE:
3009                                 ui_draw_but_HSVCIRCLE(but, &tui->wcol_regular, rect);
3010                                 break;
3011                                 
3012                         case BUT_COLORBAND:
3013                                 ui_draw_but_COLORBAND(but, &tui->wcol_regular, rect);
3014                                 break;
3015                                 
3016                         case BUT_NORMAL:
3017                                 ui_draw_but_NORMAL(but, &tui->wcol_regular, rect);
3018                                 break;
3019                                 
3020                         case BUT_IMAGE:
3021                                 ui_draw_but_IMAGE(ar, but, &tui->wcol_regular, rect);
3022                                 break;
3023                         
3024                         case HISTOGRAM:
3025                                 ui_draw_but_HISTOGRAM(ar, but, &tui->wcol_regular, rect);
3026                                 break;
3027                                 
3028                         case WAVEFORM:
3029                                 ui_draw_but_WAVEFORM(ar, but, &tui->wcol_regular, rect);
3030                                 break;
3031                                 
3032                         case VECTORSCOPE:
3033                                 ui_draw_but_VECTORSCOPE(ar, but, &tui->wcol_regular, rect);
3034                                 break;
3035                                         
3036                         case BUT_CURVE:
3037                                 ui_draw_but_CURVE(ar, but, &tui->wcol_regular, rect);
3038                                 break;
3039                                 
3040                         case PROGRESSBAR:
3041                                 wt= widget_type(UI_WTYPE_PROGRESSBAR);
3042                                 fstyle= &style->widgetlabel;
3043                                 break;
3044
3045                         case SCROLL:
3046                                 wt= widget_type(UI_WTYPE_SCROLL);
3047                                 break;
3048
3049                         default:
3050                                 wt= widget_type(UI_WTYPE_REGULAR);