Cycles: working towards texture workflow design
[blender.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 GLubyte checker_stipple_sml[32*32/8] =
163 {
164         255,0,255,0,255,0,255,0,255,0,255,0,255,0,255,0, \
165         255,0,255,0,255,0,255,0,255,0,255,0,255,0,255,0, \
166         0,255,0,255,0,255,0,255,0,255,0,255,0,255,0,255, \
167         0,255,0,255,0,255,0,255,0,255,0,255,0,255,0,255, \
168         255,0,255,0,255,0,255,0,255,0,255,0,255,0,255,0, \
169         255,0,255,0,255,0,255,0,255,0,255,0,255,0,255,0, \
170         0,255,0,255,0,255,0,255,0,255,0,255,0,255,0,255, \
171         0,255,0,255,0,255,0,255,0,255,0,255,0,255,0,255, \
172 };
173
174 /* ************************************************* */
175
176 void ui_draw_anti_tria(float x1, float y1, float x2, float y2, float x3, float y3)
177 {
178         float color[4];
179         int j;
180         
181         glEnable(GL_BLEND);
182         glGetFloatv(GL_CURRENT_COLOR, color);
183         color[3]= 0.125;
184         glColor4fv(color);
185         
186         /* for each AA step */
187         for(j=0; j<8; j++) {
188                 glTranslatef(1.0f * jit[j][0], 1.0f * jit[j][1], 0.0f);
189
190                 glBegin(GL_POLYGON);
191                 glVertex2f(x1, y1);
192                 glVertex2f(x2, y2);
193                 glVertex2f(x3, y3);
194                 glEnd();
195                 
196                 glTranslatef(-1.0f * jit[j][0], -1.0f * jit[j][1], 0.0f);
197         }
198
199         glDisable(GL_BLEND);
200         
201 }
202
203 static void widget_init(uiWidgetBase *wtb)
204 {
205         wtb->totvert= wtb->halfwayvert= 0;
206         wtb->tria1.tot= 0;
207         wtb->tria2.tot= 0;
208         
209         wtb->inner= 1;
210         wtb->outline= 1;
211         wtb->emboss= 0;
212         wtb->shadedir= 1;
213 }
214
215 /* helper call, makes shadow rect, with 'sun' above menu, so only shadow to left/right/bottom */
216 /* return tot */
217 static int round_box_shadow_edges(float (*vert)[2], rcti *rect, float rad, int roundboxalign, float step)
218 {
219         float vec[9][2];
220         float minx, miny, maxx, maxy;
221         int a, tot= 0;
222         
223         rad+= step;
224         
225         if(2.0f*rad > rect->ymax-rect->ymin)
226                 rad= 0.5f*(rect->ymax-rect->ymin);
227         
228         minx= rect->xmin-step;
229         miny= rect->ymin-step;
230         maxx= rect->xmax+step;
231         maxy= rect->ymax+step;
232         
233         /* mult */
234         for(a=0; a<9; a++) {
235                 vec[a][0]= rad*cornervec[a][0]; 
236                 vec[a][1]= rad*cornervec[a][1]; 
237         }
238         
239         /* start with left-top, anti clockwise */
240         if(roundboxalign & 1) {
241                 for(a=0; a<9; a++, tot++) {
242                         vert[tot][0]= minx+rad-vec[a][0];
243                         vert[tot][1]= maxy-vec[a][1];
244                 }
245         }
246         else {
247                 for(a=0; a<9; a++, tot++) {
248                         vert[tot][0]= minx;
249                         vert[tot][1]= maxy;
250                 }
251         }
252         
253         if(roundboxalign & 8) {
254                 for(a=0; a<9; a++, tot++) {
255                         vert[tot][0]= minx+vec[a][1];
256                         vert[tot][1]= miny+rad-vec[a][0];
257                 }
258         }
259         else {
260                 for(a=0; a<9; a++, tot++) {
261                         vert[tot][0]= minx;
262                         vert[tot][1]= miny;
263                 }
264         }
265         
266         if(roundboxalign & 4) {
267                 for(a=0; a<9; a++, tot++) {
268                         vert[tot][0]= maxx-rad+vec[a][0];
269                         vert[tot][1]= miny+vec[a][1];
270                 }
271         }
272         else {
273                 for(a=0; a<9; a++, tot++) {
274                         vert[tot][0]= maxx;
275                         vert[tot][1]= miny;
276                 }
277         }
278         
279         if(roundboxalign & 2) {
280                 for(a=0; a<9; a++, tot++) {
281                         vert[tot][0]= maxx-vec[a][1];
282                         vert[tot][1]= maxy-rad+vec[a][0];
283                 }
284         }
285         else {
286                 for(a=0; a<9; a++, tot++) {
287                         vert[tot][0]= maxx;
288                         vert[tot][1]= maxy;
289                 }
290         }
291         return tot;
292 }
293
294 /* this call has 1 extra arg to allow mask outline */
295 static void round_box__edges(uiWidgetBase *wt, int roundboxalign, rcti *rect, float rad, float radi)
296 {
297         float vec[9][2], veci[9][2];
298         float minx= rect->xmin, miny= rect->ymin, maxx= rect->xmax, maxy= rect->ymax;
299         float minxi= minx + 1.0f; /* boundbox inner */
300         float maxxi= maxx - 1.0f;
301         float minyi= miny + 1.0f;
302         float maxyi= maxy - 1.0f;
303         float facxi= (maxxi!=minxi) ? 1.0f/(maxxi-minxi) : 0.0f; /* for uv, can divide by zero */
304         float facyi= (maxyi!=minyi) ? 1.0f/(maxyi-minyi) : 0.0f;
305         int a, tot= 0, minsize;
306         const int hnum= ((roundboxalign & (1|2))==(1|2) || (roundboxalign & (4|8))==(4|8)) ? 1 : 2;
307         const int vnum= ((roundboxalign & (1|8))==(1|8) || (roundboxalign & (2|4))==(2|4)) ? 1 : 2;
308
309         minsize= MIN2((rect->xmax-rect->xmin)*hnum, (rect->ymax-rect->ymin)*vnum);
310         
311         if(2.0f*rad > minsize)
312                 rad= 0.5f*minsize;
313
314         if(2.0f*(radi+1.0f) > minsize)
315                 radi= 0.5f*minsize - 1.0f;
316         
317         /* mult */
318         for(a=0; a<9; a++) {
319                 veci[a][0]= radi*cornervec[a][0]; 
320                 veci[a][1]= radi*cornervec[a][1]; 
321                 vec[a][0]= rad*cornervec[a][0]; 
322                 vec[a][1]= rad*cornervec[a][1]; 
323         }
324         
325         /* corner left-bottom */
326         if(roundboxalign & 8) {
327                 
328                 for(a=0; a<9; a++, tot++) {
329                         wt->inner_v[tot][0]= minxi+veci[a][1];
330                         wt->inner_v[tot][1]= minyi+radi-veci[a][0];
331                         
332                         wt->outer_v[tot][0]= minx+vec[a][1];
333                         wt->outer_v[tot][1]= miny+rad-vec[a][0];
334                         
335                         wt->inner_uv[tot][0]= facxi*(wt->inner_v[tot][0] - minxi);
336                         wt->inner_uv[tot][1]= facyi*(wt->inner_v[tot][1] - minyi);
337                 }
338         }
339         else {
340                 wt->inner_v[tot][0]= minxi;
341                 wt->inner_v[tot][1]= minyi;
342                 
343                 wt->outer_v[tot][0]= minx;
344                 wt->outer_v[tot][1]= miny;
345
346                 wt->inner_uv[tot][0]= 0.0f;
347                 wt->inner_uv[tot][1]= 0.0f;
348                 
349                 tot++;
350         }
351         
352         /* corner right-bottom */
353         if(roundboxalign & 4) {
354                 
355                 for(a=0; a<9; a++, tot++) {
356                         wt->inner_v[tot][0]= maxxi-radi+veci[a][0];
357                         wt->inner_v[tot][1]= minyi+veci[a][1];
358                         
359                         wt->outer_v[tot][0]= maxx-rad+vec[a][0];
360                         wt->outer_v[tot][1]= miny+vec[a][1];
361                         
362                         wt->inner_uv[tot][0]= facxi*(wt->inner_v[tot][0] - minxi);
363                         wt->inner_uv[tot][1]= facyi*(wt->inner_v[tot][1] - minyi);
364                 }
365         }
366         else {
367                 wt->inner_v[tot][0]= maxxi;
368                 wt->inner_v[tot][1]= minyi;
369                 
370                 wt->outer_v[tot][0]= maxx;
371                 wt->outer_v[tot][1]= miny;
372
373                 wt->inner_uv[tot][0]= 1.0f;
374                 wt->inner_uv[tot][1]= 0.0f;
375                 
376                 tot++;
377         }
378         
379         wt->halfwayvert= tot;
380         
381         /* corner right-top */
382         if(roundboxalign & 2) {
383                 
384                 for(a=0; a<9; a++, tot++) {
385                         wt->inner_v[tot][0]= maxxi-veci[a][1];
386                         wt->inner_v[tot][1]= maxyi-radi+veci[a][0];
387                         
388                         wt->outer_v[tot][0]= maxx-vec[a][1];
389                         wt->outer_v[tot][1]= maxy-rad+vec[a][0];
390                         
391                         wt->inner_uv[tot][0]= facxi*(wt->inner_v[tot][0] - minxi);
392                         wt->inner_uv[tot][1]= facyi*(wt->inner_v[tot][1] - minyi);
393                 }
394         }
395         else {
396                 wt->inner_v[tot][0]= maxxi;
397                 wt->inner_v[tot][1]= maxyi;
398                 
399                 wt->outer_v[tot][0]= maxx;
400                 wt->outer_v[tot][1]= maxy;
401                 
402                 wt->inner_uv[tot][0]= 1.0f;
403                 wt->inner_uv[tot][1]= 1.0f;
404                 
405                 tot++;
406         }
407         
408         /* corner left-top */
409         if(roundboxalign & 1) {
410                 
411                 for(a=0; a<9; a++, tot++) {
412                         wt->inner_v[tot][0]= minxi+radi-veci[a][0];
413                         wt->inner_v[tot][1]= maxyi-veci[a][1];
414                         
415                         wt->outer_v[tot][0]= minx+rad-vec[a][0];
416                         wt->outer_v[tot][1]= maxy-vec[a][1];
417                         
418                         wt->inner_uv[tot][0]= facxi*(wt->inner_v[tot][0] - minxi);
419                         wt->inner_uv[tot][1]= facyi*(wt->inner_v[tot][1] - minyi);
420                 }
421                 
422         }
423         else {
424                 
425                 wt->inner_v[tot][0]= minxi;
426                 wt->inner_v[tot][1]= maxyi;
427                 
428                 wt->outer_v[tot][0]= minx;
429                 wt->outer_v[tot][1]= maxy;
430                 
431                 wt->inner_uv[tot][0]= 0.0f;
432                 wt->inner_uv[tot][1]= 1.0f;
433                 
434                 tot++;
435         }
436                 
437         wt->totvert= tot;
438 }
439
440 static void round_box_edges(uiWidgetBase *wt, int roundboxalign, rcti *rect, float rad)
441 {
442         round_box__edges(wt, roundboxalign, rect, rad, rad-1.0f);
443 }
444
445
446 /* based on button rect, return scaled array of triangles */
447 static void widget_num_tria(uiWidgetTrias *tria, rcti *rect, float triasize, char where)
448 {
449         float centx, centy, sizex, sizey, minsize;
450         int a, i1=0, i2=1;
451         
452         minsize= MIN2(rect->xmax-rect->xmin, rect->ymax-rect->ymin);
453         
454         /* center position and size */
455         centx= (float)rect->xmin + 0.5f*minsize;
456         centy= (float)rect->ymin + 0.5f*minsize;
457         sizex= sizey= -0.5f*triasize*minsize;
458
459         if(where=='r') {
460                 centx= (float)rect->xmax - 0.5f*minsize;
461                 sizex= -sizex;
462         }       
463         else if(where=='t') {
464                 centy= (float)rect->ymax - 0.5f*minsize;
465                 sizey= -sizey;
466                 i2=0; i1= 1;
467         }       
468         else if(where=='b') {
469                 sizex= -sizex;
470                 i2=0; i1= 1;
471         }       
472         
473         for(a=0; a<3; a++) {
474                 tria->vec[a][0]= sizex*num_tria_vert[a][i1] + centx;
475                 tria->vec[a][1]= sizey*num_tria_vert[a][i2] + centy;
476         }
477         
478         tria->tot= 1;
479         tria->index= num_tria_face;
480 }
481
482 static void widget_scroll_circle(uiWidgetTrias *tria, rcti *rect, float triasize, char where)
483 {
484         float centx, centy, sizex, sizey, minsize;
485         int a, i1=0, i2=1;
486         
487         minsize= MIN2(rect->xmax-rect->xmin, rect->ymax-rect->ymin);
488         
489         /* center position and size */
490         centx= (float)rect->xmin + 0.5f*minsize;
491         centy= (float)rect->ymin + 0.5f*minsize;
492         sizex= sizey= -0.5f*triasize*minsize;
493
494         if(where=='r') {
495                 centx= (float)rect->xmax - 0.5f*minsize;
496                 sizex= -sizex;
497         }       
498         else if(where=='t') {
499                 centy= (float)rect->ymax - 0.5f*minsize;
500                 sizey= -sizey;
501                 i2=0; i1= 1;
502         }       
503         else if(where=='b') {
504                 sizex= -sizex;
505                 i2=0; i1= 1;
506         }       
507         
508         for(a=0; a<16; a++) {
509                 tria->vec[a][0]= sizex*scroll_circle_vert[a][i1] + centx;
510                 tria->vec[a][1]= sizey*scroll_circle_vert[a][i2] + centy;
511         }
512         
513         tria->tot= 14;
514         tria->index= scroll_circle_face;
515 }
516
517 static void widget_trias_draw(uiWidgetTrias *tria)
518 {
519         int a;
520         
521         glBegin(GL_TRIANGLES);
522         for(a=0; a<tria->tot; a++) {
523                 glVertex2fv(tria->vec[ tria->index[a][0] ]);
524                 glVertex2fv(tria->vec[ tria->index[a][1] ]);
525                 glVertex2fv(tria->vec[ tria->index[a][2] ]);
526         }
527         glEnd();
528         
529 }
530
531 static void widget_menu_trias(uiWidgetTrias *tria, rcti *rect)
532 {
533         float centx, centy, size, asp;
534         int a;
535                 
536         /* center position and size */
537         centx= rect->xmax - 0.5f*(rect->ymax-rect->ymin);
538         centy= rect->ymin + 0.5f*(rect->ymax-rect->ymin);
539         size= 0.4f*(rect->ymax-rect->ymin);
540         
541         /* XXX exception */
542         asp= ((float)rect->xmax-rect->xmin)/((float)rect->ymax-rect->ymin);
543         if(asp > 1.2f && asp < 2.6f)
544                 centx= rect->xmax - 0.3f*(rect->ymax-rect->ymin);
545         
546         for(a=0; a<6; a++) {
547                 tria->vec[a][0]= size*menu_tria_vert[a][0] + centx;
548                 tria->vec[a][1]= size*menu_tria_vert[a][1] + centy;
549         }
550
551         tria->tot= 2;
552         tria->index= menu_tria_face;
553 }
554
555 static void widget_check_trias(uiWidgetTrias *tria, rcti *rect)
556 {
557         float centx, centy, size;
558         int a;
559         
560         /* center position and size */
561         centx= rect->xmin + 0.5f*(rect->ymax-rect->ymin);
562         centy= rect->ymin + 0.5f*(rect->ymax-rect->ymin);
563         size= 0.5f*(rect->ymax-rect->ymin);
564         
565         for(a=0; a<6; a++) {
566                 tria->vec[a][0]= size*check_tria_vert[a][0] + centx;
567                 tria->vec[a][1]= size*check_tria_vert[a][1] + centy;
568         }
569         
570         tria->tot= 4;
571         tria->index= check_tria_face;
572 }
573
574
575 /* prepares shade colors */
576 static void shadecolors4(char *coltop, char *coldown, const char *color, short shadetop, short shadedown)
577 {
578         
579         coltop[0]= CLAMPIS(color[0]+shadetop, 0, 255);
580         coltop[1]= CLAMPIS(color[1]+shadetop, 0, 255);
581         coltop[2]= CLAMPIS(color[2]+shadetop, 0, 255);
582         coltop[3]= color[3];
583
584         coldown[0]= CLAMPIS(color[0]+shadedown, 0, 255);
585         coldown[1]= CLAMPIS(color[1]+shadedown, 0, 255);
586         coldown[2]= CLAMPIS(color[2]+shadedown, 0, 255);
587         coldown[3]= color[3];   
588 }
589
590 static void round_box_shade_col4(const char col1[4], const char col2[4], const float fac)
591 {
592         unsigned char col[4];
593         const int faci= FTOCHAR(fac);
594         const int facm= 255-faci;
595
596         col[0]= (faci*col1[0] + facm*col2[0])>>8;
597         col[1]= (faci*col1[1] + facm*col2[1])>>8;
598         col[2]= (faci*col1[2] + facm*col2[2])>>8;
599         col[3]= (faci*col1[3] + facm*col2[3])>>8;
600
601         glColor4ubv(col);
602 }
603
604 static void widgetbase_outline(uiWidgetBase *wtb)
605 {
606         int a;
607         
608         /* outline */
609         glBegin(GL_QUAD_STRIP);
610         for(a=0; a<wtb->totvert; a++) {
611                 glVertex2fv(wtb->outer_v[a]);
612                 glVertex2fv(wtb->inner_v[a]);
613         }
614         glVertex2fv(wtb->outer_v[0]);
615         glVertex2fv(wtb->inner_v[0]);
616         glEnd();
617 }
618
619 static void widgetbase_draw(uiWidgetBase *wtb, uiWidgetColors *wcol)
620 {
621         int j, a;
622         
623         glEnable(GL_BLEND);
624
625         /* backdrop non AA */
626         if(wtb->inner) {
627                 if(wcol->shaded==0) {
628                         if (wcol->alpha_check) {
629                                 float x_mid= 0.0f; /* used for dumb clamping of values */
630
631                                 /* dark checkers */
632                                 glColor4ub(UI_TRANSP_DARK, UI_TRANSP_DARK, UI_TRANSP_DARK, 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(UI_TRANSP_LIGHT, UI_TRANSP_LIGHT, UI_TRANSP_LIGHT, 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             UI_DPI_FAC*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[UI_MAX_DRAW_STR];
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                 else if(but->type==MENU && (but->flag & UI_BUT_NODE_LINK)) {
1146                         /* node link hacking */
1147                         int tmp = rect->xmin;
1148                         rect->xmin = rect->xmax - (rect->ymax - rect->ymin) - 1;
1149                         widget_draw_icon(but, ICON_LAYER_USED, 1.0f, rect);
1150                         rect->xmin = tmp;
1151                 }
1152                 
1153                 /* If there's an icon too (made with uiDefIconTextBut) then draw the icon
1154                 and offset the text label to accommodate it */
1155                 
1156                 if (but->flag & UI_HAS_ICON) {
1157                         widget_draw_icon(but, but->icon+but->iconadd, 1.0f, rect);
1158                         
1159                         rect->xmin += UI_icon_get_width(but->icon+but->iconadd);
1160                         
1161                         if(but->editstr || (but->flag & UI_TEXT_LEFT)) 
1162                                 rect->xmin += 5;
1163                 }
1164                 else if((but->flag & UI_TEXT_LEFT)) 
1165                         rect->xmin += 5;
1166                 
1167                 /* always draw text for textbutton cursor */
1168                 widget_draw_text(fstyle, wcol, but, rect);
1169
1170         }
1171 }
1172
1173
1174
1175 /* *********************** widget types ************************************* */
1176
1177
1178 /*   uiWidgetStateColors
1179         char inner_anim[4];
1180         char inner_anim_sel[4];
1181         char inner_key[4];
1182         char inner_key_sel[4];
1183         char inner_driven[4];
1184         char inner_driven_sel[4];
1185         float blend;
1186  
1187 */
1188
1189 static struct uiWidgetStateColors wcol_state_colors= {
1190         {115, 190, 76, 255},
1191         {90, 166, 51, 255},
1192         {240, 235, 100, 255},
1193         {215, 211, 75, 255},
1194         {180, 0, 255, 255},
1195         {153, 0, 230, 255},
1196         0.5f, 0.0f
1197 };
1198
1199 /*  uiWidgetColors
1200         float outline[3];
1201         float inner[4];
1202         float inner_sel[4];
1203         float item[3];
1204         float text[3];
1205         float text_sel[3];
1206
1207         short shaded;
1208         float shadetop, shadedown;
1209 */      
1210
1211 static struct uiWidgetColors wcol_num= {
1212         {25, 25, 25, 255},
1213         {160, 160, 160, 255},
1214         {153, 153, 153, 255},
1215         {90, 90, 90, 255},
1216         
1217         {0, 0, 0, 255},
1218         {255, 255, 255, 255},
1219         
1220         1,
1221         -5, 0
1222 };
1223
1224 static struct uiWidgetColors wcol_numslider= {
1225         {25, 25, 25, 255},
1226         {160, 160, 160, 255},
1227         {153, 153, 153, 255},
1228         {128, 128, 128, 255},
1229         
1230         {0, 0, 0, 255},
1231         {255, 255, 255, 255},
1232         
1233         1,
1234         -5, 0
1235 };
1236
1237 static struct uiWidgetColors wcol_text= {
1238         {25, 25, 25, 255},
1239         {153, 153, 153, 255},
1240         {153, 153, 153, 255},
1241         {90, 90, 90, 255},
1242         
1243         {0, 0, 0, 255},
1244         {255, 255, 255, 255},
1245         
1246         1,
1247         0, 5
1248 };
1249
1250 static struct uiWidgetColors wcol_option= {
1251         {0, 0, 0, 255},
1252         {60, 60, 60, 255},
1253         {60, 60, 60, 255},
1254         {255, 255, 255, 255},
1255         
1256         {0, 0, 0, 255},
1257         {255, 255, 255, 255},
1258         
1259         1,
1260         5, -5
1261 };
1262
1263 /* button that shows popup */
1264 static struct uiWidgetColors wcol_menu= {
1265         {0, 0, 0, 255},
1266         {70, 70, 70, 255},
1267         {70, 70, 70, 255},
1268         {45, 45, 45, 255},
1269         
1270         {255, 255, 255, 255},
1271         {204, 204, 204, 255},
1272         
1273         1,
1274         5, -5
1275 };
1276
1277 /* button that starts pulldown */
1278 static struct uiWidgetColors wcol_pulldown= {
1279         {0, 0, 0, 255},
1280         {63, 63, 63, 255},
1281         {86, 128, 194, 255},
1282         {255, 255, 255, 255},
1283         
1284         {0, 0, 0, 255},
1285         {0, 0, 0, 255},
1286         
1287         0,
1288         25, -20
1289 };
1290
1291 /* button inside menu */
1292 static struct uiWidgetColors wcol_menu_item= {
1293         {0, 0, 0, 255},
1294         {0, 0, 0, 0},
1295         {86, 128, 194, 255},
1296         {255, 255, 255, 255},
1297         
1298         {255, 255, 255, 255},
1299         {0, 0, 0, 255},
1300         
1301         0,
1302         38, 0
1303 };
1304
1305 /* backdrop menu + title text color */
1306 static struct uiWidgetColors wcol_menu_back= {
1307         {0, 0, 0, 255},
1308         {25, 25, 25, 230},
1309         {45, 45, 45, 230},
1310         {100, 100, 100, 255},
1311         
1312         {160, 160, 160, 255},
1313         {255, 255, 255, 255},
1314         
1315         0,
1316         25, -20
1317 };
1318
1319
1320 static struct uiWidgetColors wcol_radio= {
1321         {0, 0, 0, 255},
1322         {70, 70, 70, 255},
1323         {86, 128, 194, 255},
1324         {255, 255, 255, 255},
1325         
1326         {230, 230, 230, 255},
1327         {0, 0, 0, 255},
1328         
1329         1,
1330         5, -5
1331 };
1332
1333 static struct uiWidgetColors wcol_regular= {
1334         {25, 25, 25, 255},
1335         {153, 153, 153, 255},
1336         {100, 100, 100, 255},
1337         {25, 25, 25, 255},
1338         
1339         {0, 0, 0, 255},
1340         {255, 255, 255, 255},
1341         
1342         0,
1343         0, 0
1344 };
1345
1346 static struct uiWidgetColors wcol_tool= {
1347         {25, 25, 25, 255},
1348         {153, 153, 153, 255},
1349         {100, 100, 100, 255},
1350         {25, 25, 25, 255},
1351         
1352         {0, 0, 0, 255},
1353         {255, 255, 255, 255},
1354         
1355         1,
1356         5, -5
1357 };
1358
1359 static struct uiWidgetColors wcol_box= {
1360         {25, 25, 25, 255},
1361         {128, 128, 128, 255},
1362         {100, 100, 100, 255},
1363         {25, 25, 25, 255},
1364         
1365         {0, 0, 0, 255},
1366         {255, 255, 255, 255},
1367         
1368         0,
1369         0, 0
1370 };
1371
1372 static struct uiWidgetColors wcol_toggle= {
1373         {25, 25, 25, 255},
1374         {153, 153, 153, 255},
1375         {100, 100, 100, 255},
1376         {25, 25, 25, 255},
1377         
1378         {0, 0, 0, 255},
1379         {255, 255, 255, 255},
1380         
1381         0,
1382         0, 0
1383 };
1384
1385 static struct uiWidgetColors wcol_scroll= {
1386         {50, 50, 50, 180},
1387         {80, 80, 80, 180},
1388         {100, 100, 100, 180},
1389         {128, 128, 128, 255},
1390         
1391         {0, 0, 0, 255},
1392         {255, 255, 255, 255},
1393         
1394         1,
1395         5, -5
1396 };
1397
1398 static struct uiWidgetColors wcol_progress= {
1399         {0, 0, 0, 255},
1400         {190, 190, 190, 255},
1401         {100, 100, 100, 180},
1402         {68, 68, 68, 255},
1403         
1404         {0, 0, 0, 255},
1405         {255, 255, 255, 255},
1406         
1407         0,
1408         0, 0
1409 };
1410
1411 static struct uiWidgetColors wcol_list_item= {
1412         {0, 0, 0, 255},
1413         {0, 0, 0, 0},
1414         {86, 128, 194, 255},
1415         {0, 0, 0, 255},
1416         
1417         {0, 0, 0, 255},
1418         {0, 0, 0, 255},
1419         
1420         0,
1421         0, 0
1422 };
1423
1424 /* free wcol struct to play with */
1425 static struct uiWidgetColors wcol_tmp= {
1426         {0, 0, 0, 255},
1427         {128, 128, 128, 255},
1428         {100, 100, 100, 255},
1429         {25, 25, 25, 255},
1430         
1431         {0, 0, 0, 255},
1432         {255, 255, 255, 255},
1433         
1434         0,
1435         0, 0
1436 };
1437
1438
1439 /* called for theme init (new theme) and versions */
1440 void ui_widget_color_init(ThemeUI *tui)
1441 {
1442         tui->wcol_regular= wcol_regular;
1443         tui->wcol_tool= wcol_tool;
1444         tui->wcol_text= wcol_text;
1445         tui->wcol_radio= wcol_radio;
1446         tui->wcol_option= wcol_option;
1447         tui->wcol_toggle= wcol_toggle;
1448         tui->wcol_num= wcol_num;
1449         tui->wcol_numslider= wcol_numslider;
1450         tui->wcol_menu= wcol_menu;
1451         tui->wcol_pulldown= wcol_pulldown;
1452         tui->wcol_menu_back= wcol_menu_back;
1453         tui->wcol_menu_item= wcol_menu_item;
1454         tui->wcol_box= wcol_box;
1455         tui->wcol_scroll= wcol_scroll;
1456         tui->wcol_list_item= wcol_list_item;
1457         tui->wcol_progress= wcol_progress;
1458
1459         tui->wcol_state= wcol_state_colors;
1460 }
1461
1462 /* ************ button callbacks, state ***************** */
1463
1464 static void widget_state_blend(char *cp, const char *cpstate, const float fac)
1465 {
1466         if(fac != 0.0f) {
1467                 cp[0]= (int)((1.0f-fac)*cp[0] + fac*cpstate[0]);
1468                 cp[1]= (int)((1.0f-fac)*cp[1] + fac*cpstate[1]);
1469                 cp[2]= (int)((1.0f-fac)*cp[2] + fac*cpstate[2]);
1470         }
1471 }
1472
1473 /* copy colors from theme, and set changes in it based on state */
1474 static void widget_state(uiWidgetType *wt, int state)
1475 {
1476         uiWidgetStateColors *wcol_state= wt->wcol_state;
1477
1478         wt->wcol= *(wt->wcol_theme);
1479         
1480         if(state & UI_SELECT) {
1481                 QUATCOPY(wt->wcol.inner, wt->wcol.inner_sel)
1482
1483                 if(state & UI_BUT_ANIMATED_KEY)
1484                         widget_state_blend(wt->wcol.inner, wcol_state->inner_key_sel, wcol_state->blend);
1485                 else if(state & UI_BUT_ANIMATED)
1486                         widget_state_blend(wt->wcol.inner, wcol_state->inner_anim_sel, wcol_state->blend);
1487                 else if(state & UI_BUT_DRIVEN)
1488                         widget_state_blend(wt->wcol.inner, wcol_state->inner_driven_sel, wcol_state->blend);
1489
1490                 VECCOPY(wt->wcol.text, wt->wcol.text_sel);
1491                 
1492                 if(state & UI_SELECT)
1493                         SWAP(short, wt->wcol.shadetop, wt->wcol.shadedown);
1494         }
1495         else {
1496                 if(state & UI_BUT_ANIMATED_KEY)
1497                         widget_state_blend(wt->wcol.inner, wcol_state->inner_key, wcol_state->blend);
1498                 else if(state & UI_BUT_ANIMATED)
1499                         widget_state_blend(wt->wcol.inner, wcol_state->inner_anim, wcol_state->blend);
1500                 else if(state & UI_BUT_DRIVEN)
1501                         widget_state_blend(wt->wcol.inner, wcol_state->inner_driven, wcol_state->blend);
1502
1503                 if(state & UI_ACTIVE) { /* mouse over? */
1504                         wt->wcol.inner[0]= wt->wcol.inner[0]>=240? 255 : wt->wcol.inner[0]+15;
1505                         wt->wcol.inner[1]= wt->wcol.inner[1]>=240? 255 : wt->wcol.inner[1]+15;
1506                         wt->wcol.inner[2]= wt->wcol.inner[2]>=240? 255 : wt->wcol.inner[2]+15;
1507                 }
1508         }
1509
1510         if(state & UI_BUT_REDALERT) {
1511                 char red[4]= {255, 0, 0};
1512                 widget_state_blend(wt->wcol.inner, red, 0.4f);
1513         }
1514         if(state & UI_BUT_NODE_ACTIVE) {
1515                 char blue[4]= {86, 128, 194};
1516                 widget_state_blend(wt->wcol.inner, blue, 0.3f);
1517         }
1518 }
1519
1520 /* sliders use special hack which sets 'item' as inner when drawing filling */
1521 static void widget_state_numslider(uiWidgetType *wt, int state)
1522 {
1523         uiWidgetStateColors *wcol_state= wt->wcol_state;
1524         float blend= wcol_state->blend - 0.2f; // XXX special tweak to make sure that bar will still be visible
1525
1526         /* call this for option button */
1527         widget_state(wt, state);
1528         
1529         /* now, set the inner-part so that it reflects state settings too */
1530         // TODO: maybe we should have separate settings for the blending colors used for this case?
1531         if(state & UI_SELECT) {
1532                 
1533                 if(state & UI_BUT_ANIMATED_KEY)
1534                         widget_state_blend(wt->wcol.item, wcol_state->inner_key_sel, blend);
1535                 else if(state & UI_BUT_ANIMATED)
1536                         widget_state_blend(wt->wcol.item, wcol_state->inner_anim_sel, blend);
1537                 else if(state & UI_BUT_DRIVEN)
1538                         widget_state_blend(wt->wcol.item, wcol_state->inner_driven_sel, blend);
1539                 
1540                 if(state & UI_SELECT)
1541                         SWAP(short, wt->wcol.shadetop, wt->wcol.shadedown);
1542         }
1543         else {
1544                 if(state & UI_BUT_ANIMATED_KEY)
1545                         widget_state_blend(wt->wcol.item, wcol_state->inner_key, blend);
1546                 else if(state & UI_BUT_ANIMATED)
1547                         widget_state_blend(wt->wcol.item, wcol_state->inner_anim, blend);
1548                 else if(state & UI_BUT_DRIVEN)
1549                         widget_state_blend(wt->wcol.item, wcol_state->inner_driven, blend);
1550         }
1551 }
1552
1553 /* labels use theme colors for text */
1554 static void widget_state_label(uiWidgetType *wt, int state)
1555 {
1556         /* call this for option button */
1557         widget_state(wt, state);
1558
1559         if(state & UI_SELECT)
1560                 UI_GetThemeColor4ubv(TH_TEXT_HI, (unsigned char *)wt->wcol.text);
1561         else
1562                 UI_GetThemeColor4ubv(TH_TEXT, (unsigned char *)wt->wcol.text);
1563         
1564 }
1565
1566 /* labels use theme colors for text */
1567 static void widget_state_option_menu(uiWidgetType *wt, int state)
1568 {
1569         
1570         /* call this for option button */
1571         widget_state(wt, state);
1572         
1573         /* if not selected we get theme from menu back */
1574         if(state & UI_SELECT)
1575                 UI_GetThemeColor4ubv(TH_TEXT_HI, (unsigned char *)wt->wcol.text);
1576         else {
1577                 bTheme *btheme= U.themes.first; /* XXX */
1578
1579                 VECCOPY(wt->wcol.text, btheme->tui.wcol_menu_back.text);
1580         }
1581 }
1582
1583
1584 static void widget_state_nothing(uiWidgetType *wt, int UNUSED(state))
1585 {
1586         wt->wcol= *(wt->wcol_theme);
1587 }       
1588
1589 /* special case, button that calls pulldown */
1590 static void widget_state_pulldown(uiWidgetType *wt, int state)
1591 {
1592         wt->wcol= *(wt->wcol_theme);
1593         
1594         QUATCOPY(wt->wcol.inner, wt->wcol.inner_sel);
1595         VECCOPY(wt->wcol.outline, wt->wcol.inner);
1596
1597         if(state & UI_ACTIVE)
1598                 VECCOPY(wt->wcol.text, wt->wcol.text_sel);
1599 }
1600
1601 /* special case, menu items */
1602 static void widget_state_menu_item(uiWidgetType *wt, int state)
1603 {
1604         wt->wcol= *(wt->wcol_theme);
1605         
1606         if(state & (UI_BUT_DISABLED|UI_BUT_INACTIVE)) {
1607                 wt->wcol.text[0]= 0.5f*(wt->wcol.text[0]+wt->wcol.text_sel[0]);
1608                 wt->wcol.text[1]= 0.5f*(wt->wcol.text[1]+wt->wcol.text_sel[1]);
1609                 wt->wcol.text[2]= 0.5f*(wt->wcol.text[2]+wt->wcol.text_sel[2]);
1610         }
1611         else if(state & UI_ACTIVE) {
1612                 QUATCOPY(wt->wcol.inner, wt->wcol.inner_sel);
1613                 VECCOPY(wt->wcol.text, wt->wcol.text_sel);
1614         }
1615 }
1616
1617
1618 /* ************ menu backdrop ************************* */
1619
1620 /* outside of rect, rad to left/bottom/right */
1621 static void widget_softshadow(rcti *rect, int roundboxalign, float radin, float radout)
1622 {
1623         uiWidgetBase wtb;
1624         rcti rect1= *rect;
1625         float alpha, alphastep;
1626         int step, tot, a;
1627         
1628         /* prevent tooltips to not show round shadow */
1629         if( 2.0f*radout > 0.2f*(rect1.ymax-rect1.ymin) )
1630                 rect1.ymax -= 0.2f*(rect1.ymax-rect1.ymin);
1631         else
1632                 rect1.ymax -= 2.0f*radout;
1633         
1634         /* inner part */
1635         tot= round_box_shadow_edges(wtb.inner_v, &rect1, radin, roundboxalign & 12, 0.0f);
1636         
1637         /* inverse linear shadow alpha */
1638         alpha= 0.15;
1639         alphastep= 0.67;
1640         
1641         for(step= 1; step<=radout; step++, alpha*=alphastep) {
1642                 round_box_shadow_edges(wtb.outer_v, &rect1, radin, 15, (float)step);
1643                 
1644                 glColor4f(0.0f, 0.0f, 0.0f, alpha);
1645                 
1646                 glBegin(GL_QUAD_STRIP);
1647                 for(a=0; a<tot; a++) {
1648                         glVertex2fv(wtb.outer_v[a]);
1649                         glVertex2fv(wtb.inner_v[a]);
1650                 }
1651                 glEnd();
1652         }
1653         
1654 }
1655
1656 static void widget_menu_back(uiWidgetColors *wcol, rcti *rect, int flag, int direction)
1657 {
1658         uiWidgetBase wtb;
1659         int roundboxalign= 15;
1660         
1661         widget_init(&wtb);
1662         
1663         /* menu is 2nd level or deeper */
1664         if (flag & UI_BLOCK_POPUP) {
1665                 //rect->ymin -= 4.0;
1666                 //rect->ymax += 4.0;
1667         }
1668         else if (direction == UI_DOWN) {
1669                 roundboxalign= 12;
1670                 rect->ymin -= 4.0;
1671         } 
1672         else if (direction == UI_TOP) {
1673                 roundboxalign= 3;
1674                 rect->ymax += 4.0;
1675         }
1676         
1677         glEnable(GL_BLEND);
1678         widget_softshadow(rect, roundboxalign, 5.0f, 8.0f);
1679         
1680         round_box_edges(&wtb, roundboxalign, rect, 5.0f);
1681         wtb.emboss= 0;
1682         widgetbase_draw(&wtb, wcol);
1683         
1684         glDisable(GL_BLEND);
1685 }
1686
1687
1688 static void ui_hsv_cursor(float x, float y)
1689 {
1690         
1691         glPushMatrix();
1692         glTranslatef(x, y, 0.0f);
1693         
1694         glColor3f(1.0f, 1.0f, 1.0f);
1695         glutil_draw_filled_arc(0.0f, M_PI*2.0, 3.0f, 8);
1696         
1697         glEnable(GL_BLEND);
1698         glEnable(GL_LINE_SMOOTH );
1699         glColor3f(0.0f, 0.0f, 0.0f);
1700         glutil_draw_lined_arc(0.0f, M_PI*2.0, 3.0f, 12);
1701         glDisable(GL_BLEND);
1702         glDisable(GL_LINE_SMOOTH );
1703         
1704         glPopMatrix();
1705         
1706 }
1707
1708 void ui_hsvcircle_vals_from_pos(float *valrad, float *valdist, rcti *rect, float mx, float my)
1709 {
1710         /* duplication of code... well, simple is better now */
1711         float centx= (float)(rect->xmin + rect->xmax)/2;
1712         float centy= (float)(rect->ymin + rect->ymax)/2;
1713         float radius, dist;
1714         
1715         if( rect->xmax-rect->xmin > rect->ymax-rect->ymin )
1716                 radius= (float)(rect->ymax - rect->ymin)/2; 
1717         else
1718                 radius= (float)(rect->xmax - rect->xmin)/2; 
1719
1720         mx-= centx;
1721         my-= centy;
1722         dist= sqrt( mx*mx + my*my);
1723         if(dist < radius)
1724                 *valdist= dist/radius;
1725         else
1726                 *valdist= 1.0f;
1727         
1728         *valrad= atan2f(mx, my)/(2.0f*(float)M_PI) + 0.5f;
1729 }
1730
1731 static void ui_draw_but_HSVCIRCLE(uiBut *but, uiWidgetColors *wcol, rcti *rect)
1732 {
1733         /* gouraud triangle fan */
1734         float radstep, ang= 0.0f;
1735         float centx, centy, radius;
1736         float rgb[3], hsvo[3], hsv[3], col[3], colcent[3];
1737         int a, tot= 32;
1738         int color_profile = but->block->color_profile;
1739         
1740         if (but->rnaprop && RNA_property_subtype(but->rnaprop) == PROP_COLOR_GAMMA)
1741                 color_profile = BLI_PR_NONE;
1742         
1743         radstep= 2.0f*(float)M_PI/(float)tot;
1744         centx= (float)(rect->xmin + rect->xmax)/2;
1745         centy= (float)(rect->ymin + rect->ymax)/2;
1746         
1747         if( rect->xmax-rect->xmin > rect->ymax-rect->ymin )
1748                 radius= (float)(rect->ymax - rect->ymin)/2; 
1749         else
1750                 radius= (float)(rect->xmax - rect->xmin)/2; 
1751         
1752         /* color */
1753         ui_get_but_vectorf(but, rgb);
1754         copy_v3_v3(hsv, ui_block_hsv_get(but->block));
1755         rgb_to_hsv_compat(rgb[0], rgb[1], rgb[2], hsv, hsv+1, hsv+2);
1756         copy_v3_v3(hsvo, hsv);
1757         
1758         /* exception: if 'lock' is set
1759          * lock the value of the color wheel to 1.
1760          * Useful for color correction tools where you're only interested in hue. */
1761         if (but->flag & UI_BUT_COLOR_LOCK)
1762                 hsv[2] = 1.f;
1763         else if (color_profile)
1764                 hsv[2] = linearrgb_to_srgb(hsv[2]);
1765         
1766         hsv_to_rgb(0.f, 0.f, hsv[2], colcent, colcent+1, colcent+2);
1767         
1768         glShadeModel(GL_SMOOTH);
1769
1770         glBegin(GL_TRIANGLE_FAN);
1771         glColor3fv(colcent);
1772         glVertex2f( centx, centy);
1773         
1774         for(a=0; a<=tot; a++, ang+=radstep) {
1775                 float si= sin(ang);
1776                 float co= cos(ang);
1777                 
1778                 ui_hsvcircle_vals_from_pos(hsv, hsv+1, rect, centx + co*radius, centy + si*radius);
1779                 CLAMP(hsv[2], 0.0f, 1.0f); /* for display only */
1780
1781                 hsv_to_rgb(hsv[0], hsv[1], hsv[2], col, col+1, col+2);
1782                 glColor3fv(col);
1783                 glVertex2f( centx + co*radius, centy + si*radius);
1784         }
1785         glEnd();
1786         
1787         glShadeModel(GL_FLAT);
1788         
1789         /* fully rounded outline */
1790         glPushMatrix();
1791         glTranslatef(centx, centy, 0.0f);
1792         glEnable(GL_BLEND);
1793         glEnable(GL_LINE_SMOOTH );
1794         glColor3ubv((unsigned char*)wcol->outline);
1795         glutil_draw_lined_arc(0.0f, M_PI*2.0, radius, tot + 1);
1796         glDisable(GL_BLEND);
1797         glDisable(GL_LINE_SMOOTH );
1798         glPopMatrix();
1799
1800         /* cursor */
1801         ang= 2.0f*(float)M_PI*hsvo[0] + 0.5f*(float)M_PI;
1802
1803         if(but->flag & UI_BUT_COLOR_CUBIC)
1804                 radius= (1.0f - powf(1.0f - hsvo[1], 3.0f)) *radius;
1805         else
1806                 radius= hsvo[1] * radius;
1807
1808         ui_hsv_cursor(centx + cosf(-ang)*radius, centy + sinf(-ang)*radius);
1809         
1810 }
1811
1812 /* ************ custom buttons, old stuff ************** */
1813
1814 /* draws in resolution of 20x4 colors */
1815 void ui_draw_gradient(rcti *rect, float *hsv, int type, float alpha)
1816 {
1817         int a;
1818         float h= hsv[0], s= hsv[1], v= hsv[2];
1819         float dx, dy, sx1, sx2, sy;
1820         float col0[4][3];       // left half, rect bottom to top
1821         float col1[4][3];       // right half, rect bottom to top
1822
1823         /* draw series of gouraud rects */
1824         glShadeModel(GL_SMOOTH);
1825         
1826         switch(type) {
1827                 case UI_GRAD_SV:
1828                         hsv_to_rgb(h, 0.0, 0.0,   &col1[0][0], &col1[0][1], &col1[0][2]);
1829                         hsv_to_rgb(h, 0.333, 0.0, &col1[1][0], &col1[1][1], &col1[1][2]);
1830                         hsv_to_rgb(h, 0.666, 0.0, &col1[2][0], &col1[2][1], &col1[2][2]);
1831                         hsv_to_rgb(h, 1.0, 0.0,   &col1[3][0], &col1[3][1], &col1[3][2]);
1832                         break;
1833                 case UI_GRAD_HV:
1834                         hsv_to_rgb(0.0, s, 0.0,   &col1[0][0], &col1[0][1], &col1[0][2]);
1835                         hsv_to_rgb(0.0, s, 0.333, &col1[1][0], &col1[1][1], &col1[1][2]);
1836                         hsv_to_rgb(0.0, s, 0.666, &col1[2][0], &col1[2][1], &col1[2][2]);
1837                         hsv_to_rgb(0.0, s, 1.0,   &col1[3][0], &col1[3][1], &col1[3][2]);
1838                         break;
1839                 case UI_GRAD_HS:
1840                         hsv_to_rgb(0.0, 0.0, v,   &col1[0][0], &col1[0][1], &col1[0][2]);
1841                         hsv_to_rgb(0.0, 0.333, v, &col1[1][0], &col1[1][1], &col1[1][2]);
1842                         hsv_to_rgb(0.0, 0.666, v, &col1[2][0], &col1[2][1], &col1[2][2]);
1843                         hsv_to_rgb(0.0, 1.0, v,   &col1[3][0], &col1[3][1], &col1[3][2]);
1844                         break;
1845                 case UI_GRAD_H:
1846                         hsv_to_rgb(0.0, 1.0, 1.0,   &col1[0][0], &col1[0][1], &col1[0][2]);
1847                         VECCOPY(col1[1], col1[0]);
1848                         VECCOPY(col1[2], col1[0]);
1849                         VECCOPY(col1[3], col1[0]);
1850                         break;
1851                 case UI_GRAD_S:
1852                         hsv_to_rgb(1.0, 0.0, 1.0,   &col1[1][0], &col1[1][1], &col1[1][2]);
1853                         VECCOPY(col1[0], col1[1]);
1854                         VECCOPY(col1[2], col1[1]);
1855                         VECCOPY(col1[3], col1[1]);
1856                         break;
1857                 case UI_GRAD_V:
1858                         hsv_to_rgb(1.0, 1.0, 0.0,   &col1[2][0], &col1[2][1], &col1[2][2]);
1859                         VECCOPY(col1[0], col1[2]);
1860                         VECCOPY(col1[1], col1[2]);
1861                         VECCOPY(col1[3], col1[2]);
1862                         break;
1863                 default:
1864                         assert(!"invalid 'type' argument");
1865                         hsv_to_rgb(1.0, 1.0, 1.0,   &col1[2][0], &col1[2][1], &col1[2][2]);
1866                         VECCOPY(col1[0], col1[2]);
1867                         VECCOPY(col1[1], col1[2]);
1868                         VECCOPY(col1[3], col1[2]);                      
1869         }
1870         
1871         /* old below */
1872         
1873         for(dx=0.0f; dx<1.0f; dx+= 0.05f) {
1874                 // previous color
1875                 VECCOPY(col0[0], col1[0]);
1876                 VECCOPY(col0[1], col1[1]);
1877                 VECCOPY(col0[2], col1[2]);
1878                 VECCOPY(col0[3], col1[3]);
1879                 
1880                 // new color
1881                 switch(type) {
1882                         case UI_GRAD_SV:
1883                                 hsv_to_rgb(h, 0.0, dx,   &col1[0][0], &col1[0][1], &col1[0][2]);
1884                                 hsv_to_rgb(h, 0.333, dx, &col1[1][0], &col1[1][1], &col1[1][2]);
1885                                 hsv_to_rgb(h, 0.666, dx, &col1[2][0], &col1[2][1], &col1[2][2]);
1886                                 hsv_to_rgb(h, 1.0, dx,   &col1[3][0], &col1[3][1], &col1[3][2]);
1887                                 break;
1888                         case UI_GRAD_HV:
1889                                 hsv_to_rgb(dx, s, 0.0,   &col1[0][0], &col1[0][1], &col1[0][2]);
1890                                 hsv_to_rgb(dx, s, 0.333, &col1[1][0], &col1[1][1], &col1[1][2]);
1891                                 hsv_to_rgb(dx, s, 0.666, &col1[2][0], &col1[2][1], &col1[2][2]);
1892                                 hsv_to_rgb(dx, s, 1.0,   &col1[3][0], &col1[3][1], &col1[3][2]);
1893                                 break;
1894                         case UI_GRAD_HS:
1895                                 hsv_to_rgb(dx, 0.0, v,   &col1[0][0], &col1[0][1], &col1[0][2]);
1896                                 hsv_to_rgb(dx, 0.333, v, &col1[1][0], &col1[1][1], &col1[1][2]);
1897                                 hsv_to_rgb(dx, 0.666, v, &col1[2][0], &col1[2][1], &col1[2][2]);
1898                                 hsv_to_rgb(dx, 1.0, v,   &col1[3][0], &col1[3][1], &col1[3][2]);
1899                                 break;
1900                         case UI_GRAD_H:
1901                                 hsv_to_rgb(dx, 1.0, 1.0,   &col1[0][0], &col1[0][1], &col1[0][2]);
1902                                 VECCOPY(col1[1], col1[0]);
1903                                 VECCOPY(col1[2], col1[0]);
1904                                 VECCOPY(col1[3], col1[0]);
1905                                 break;
1906                         case UI_GRAD_S:
1907                                 hsv_to_rgb(h, dx, 1.0,   &col1[1][0], &col1[1][1], &col1[1][2]);
1908                                 VECCOPY(col1[0], col1[1]);
1909                                 VECCOPY(col1[2], col1[1]);
1910                                 VECCOPY(col1[3], col1[1]);
1911                                 break;
1912                         case UI_GRAD_V:
1913                                 hsv_to_rgb(h, 1.0, dx,   &col1[2][0], &col1[2][1], &col1[2][2]);
1914                                 VECCOPY(col1[0], col1[2]);
1915                                 VECCOPY(col1[1], col1[2]);
1916                                 VECCOPY(col1[3], col1[2]);
1917                                 break;
1918                 }
1919                 
1920                 // rect
1921                 sx1= rect->xmin + dx*(rect->xmax-rect->xmin);
1922                 sx2= rect->xmin + (dx+0.05f)*(rect->xmax-rect->xmin);
1923                 sy= rect->ymin;
1924                 dy= (rect->ymax-rect->ymin)/3.0;
1925                 
1926                 glBegin(GL_QUADS);
1927                 for(a=0; a<3; a++, sy+=dy) {
1928                         glColor4f(col0[a][0], col0[a][1], col0[a][2], alpha);
1929                         glVertex2f(sx1, sy);
1930                         
1931                         glColor4f(col1[a][0], col1[a][1], col1[a][2], alpha);
1932                         glVertex2f(sx2, sy);
1933
1934                         glColor4f(col1[a+1][0], col1[a+1][1], col1[a+1][2], alpha);
1935                         glVertex2f(sx2, sy+dy);
1936                         
1937                         glColor4f(col0[a+1][0], col0[a+1][1], col0[a+1][2], alpha);
1938                         glVertex2f(sx1, sy+dy);
1939                 }
1940                 glEnd();
1941         }
1942         
1943         glShadeModel(GL_FLAT);
1944         
1945 }
1946
1947
1948
1949 static void ui_draw_but_HSVCUBE(uiBut *but, rcti *rect)
1950 {
1951         float rgb[3], h,s,v;
1952         float x=0.0f, y=0.0f;
1953         float *hsv= ui_block_hsv_get(but->block);
1954         float hsvn[3];
1955         
1956         h= hsv[0];
1957         s= hsv[1];
1958         v= hsv[2];
1959         
1960         ui_get_but_vectorf(but, rgb);
1961         rgb_to_hsv_compat(rgb[0], rgb[1], rgb[2], &h, &s, &v);
1962
1963         hsvn[0]= h;
1964         hsvn[1]= s;
1965         hsvn[2]= v;
1966         
1967         ui_draw_gradient(rect, hsvn, but->a1, 1.f);
1968         
1969         switch((int)but->a1) {
1970                 case UI_GRAD_SV:
1971                         x= v; y= s; break;
1972                 case UI_GRAD_HV:
1973                         x= h; y= v; break;
1974                 case UI_GRAD_HS:
1975                         x= h; y= s; break;
1976                 case UI_GRAD_H:
1977                         x= h; y= 0.5; break;
1978                 case UI_GRAD_S:
1979                         x= s; y= 0.5; break;
1980                 case UI_GRAD_V:
1981                         x= v; y= 0.5; break;
1982         }
1983         
1984         /* cursor */
1985         x= rect->xmin + x*(rect->xmax-rect->xmin);
1986         y= rect->ymin + y*(rect->ymax-rect->ymin);
1987         CLAMP(x, rect->xmin+3.0f, rect->xmax-3.0f);
1988         CLAMP(y, rect->ymin+3.0f, rect->ymax-3.0f);
1989         
1990         ui_hsv_cursor(x, y);
1991         
1992         /* outline */
1993         glColor3ub(0,  0,  0);
1994         fdrawbox((rect->xmin), (rect->ymin), (rect->xmax), (rect->ymax));
1995 }
1996
1997 /* vertical 'value' slider, using new widget code */
1998 static void ui_draw_but_HSV_v(uiBut *but, rcti *rect)
1999 {
2000         uiWidgetBase wtb;
2001         float rad= 0.5f*(rect->xmax - rect->xmin);
2002         float x, y;
2003         float rgb[3], hsv[3], v, range;
2004         int color_profile = but->block->color_profile;
2005         
2006         if (but->rnaprop && RNA_property_subtype(but->rnaprop) == PROP_COLOR_GAMMA)
2007                 color_profile = BLI_PR_NONE;
2008
2009         ui_get_but_vectorf(but, rgb);
2010         rgb_to_hsv(rgb[0], rgb[1], rgb[2], hsv, hsv+1, hsv+2);
2011         v = hsv[2];
2012         
2013         if (color_profile)
2014                 v = linearrgb_to_srgb(v);
2015
2016         /* map v from property range to [0,1] */
2017         range = but->softmax - but->softmin;
2018         v =     (v - but->softmin)/range;
2019         
2020         widget_init(&wtb);
2021         
2022         /* fully rounded */
2023         round_box_edges(&wtb, 15, rect, rad);
2024         
2025         /* setup temp colors */
2026         wcol_tmp.outline[0]= wcol_tmp.outline[1]= wcol_tmp.outline[2]= 0;
2027         wcol_tmp.inner[0]= wcol_tmp.inner[1]= wcol_tmp.inner[2]= 128;
2028         wcol_tmp.shadetop= 127;
2029         wcol_tmp.shadedown= -128;
2030         wcol_tmp.shaded= 1;
2031         
2032         widgetbase_draw(&wtb, &wcol_tmp);
2033
2034         /* cursor */
2035         x= rect->xmin + 0.5f * (rect->xmax-rect->xmin);
2036         y= rect->ymin + v * (rect->ymax-rect->ymin);
2037         CLAMP(y, rect->ymin+3.0f, rect->ymax-3.0f);
2038         
2039         ui_hsv_cursor(x, y);
2040         
2041 }
2042
2043
2044 /* ************ separator, for menus etc ***************** */
2045 static void ui_draw_separator(rcti *rect,  uiWidgetColors *wcol)
2046 {
2047         int y = rect->ymin + (rect->ymax - rect->ymin)/2 - 1;
2048         unsigned char col[4];
2049         
2050         col[0] = wcol->text[0];
2051         col[1] = wcol->text[1];
2052         col[2] = wcol->text[2];
2053         col[3] = 7;
2054         
2055         glEnable(GL_BLEND);
2056         glColor4ubv(col);
2057         sdrawline(rect->xmin, y, rect->xmax, y);
2058         glDisable(GL_BLEND);
2059 }
2060
2061 /* ************ button callbacks, draw ***************** */
2062
2063 static void widget_numbut(uiWidgetColors *wcol, rcti *rect, int state, int roundboxalign)
2064 {
2065         uiWidgetBase wtb;
2066         float rad= 0.5f*(rect->ymax - rect->ymin);
2067         float textofs = rad*0.75f;
2068
2069         if(state & UI_SELECT)
2070                 SWAP(short, wcol->shadetop, wcol->shadedown);
2071         
2072         widget_init(&wtb);
2073         
2074         /* fully rounded */
2075         round_box_edges(&wtb, roundboxalign, rect, rad);
2076         
2077         /* decoration */
2078         if(!(state & UI_TEXTINPUT)) {
2079                 widget_num_tria(&wtb.tria1, rect, 0.6f, 'l');
2080                 widget_num_tria(&wtb.tria2, rect, 0.6f, 'r');
2081         }
2082
2083         widgetbase_draw(&wtb, wcol);
2084         
2085         /* text space */
2086         rect->xmin += textofs;
2087         rect->xmax -= textofs;
2088 }
2089
2090 //static int ui_link_bezier_points(rcti *rect, float coord_array[][2], int resol)
2091 int ui_link_bezier_points(rcti *rect, float coord_array[][2], int resol)
2092 {
2093         float dist, vec[4][2];
2094
2095         vec[0][0]= rect->xmin;
2096         vec[0][1]= rect->ymin;
2097         vec[3][0]= rect->xmax;
2098         vec[3][1]= rect->ymax;
2099         
2100         dist= 0.5f*ABS(vec[0][0] - vec[3][0]);
2101         
2102         vec[1][0]= vec[0][0]+dist;
2103         vec[1][1]= vec[0][1];
2104         
2105         vec[2][0]= vec[3][0]-dist;
2106         vec[2][1]= vec[3][1];
2107         
2108         forward_diff_bezier(vec[0][0], vec[1][0], vec[2][0], vec[3][0], coord_array[0], resol, sizeof(float)*2);
2109         forward_diff_bezier(vec[0][1], vec[1][1], vec[2][1], vec[3][1], coord_array[0]+1, resol, sizeof(float)*2);
2110         
2111         return 1;
2112 }
2113
2114 #define LINK_RESOL      24
2115 void ui_draw_link_bezier(rcti *rect)
2116 {
2117         float coord_array[LINK_RESOL+1][2];
2118         
2119         if(ui_link_bezier_points(rect, coord_array, LINK_RESOL)) {
2120                 /* we can reuse the dist variable here to increment the GL curve eval amount*/
2121                 // const float dist= 1.0f/(float)LINK_RESOL; // UNUSED
2122                 int i;
2123
2124                 glEnable(GL_BLEND);
2125                 glEnable(GL_LINE_SMOOTH);
2126                 
2127                 glBegin(GL_LINE_STRIP);
2128                 for(i=0; i<=LINK_RESOL; i++) {
2129                         glVertex2fv(coord_array[i]);
2130                 }
2131                 glEnd();
2132                 
2133                 glDisable(GL_BLEND);
2134                 glDisable(GL_LINE_SMOOTH);
2135
2136         }
2137 }
2138
2139 /* function in use for buttons and for view2d sliders */
2140 void uiWidgetScrollDraw(uiWidgetColors *wcol, rcti *rect, rcti *slider, int state)
2141 {
2142         uiWidgetBase wtb;
2143         float rad;
2144         int horizontal;
2145         short outline=0;
2146
2147         widget_init(&wtb);
2148
2149         /* determine horizontal/vertical */
2150         horizontal= (rect->xmax - rect->xmin > rect->ymax - rect->ymin);
2151         
2152         if(horizontal)
2153                 rad= 0.5f*(rect->ymax - rect->ymin);
2154         else
2155                 rad= 0.5f*(rect->xmax - rect->xmin);
2156         
2157         wtb.shadedir= (horizontal)? 1: 0;
2158         
2159         /* draw back part, colors swapped and shading inverted */
2160         if(horizontal)
2161                 SWAP(short, wcol->shadetop, wcol->shadedown);
2162         
2163         round_box_edges(&wtb, 15, rect, rad); 
2164         widgetbase_draw(&wtb, wcol);
2165         
2166         /* slider */
2167         if(slider->xmax-slider->xmin<2 || slider->ymax-slider->ymin<2);
2168         else {
2169                 
2170                 SWAP(short, wcol->shadetop, wcol->shadedown);
2171                 
2172                 QUATCOPY(wcol->inner, wcol->item);
2173                 
2174                 if(wcol->shadetop>wcol->shadedown)
2175                         wcol->shadetop+= 20;    /* XXX violates themes... */
2176                 else wcol->shadedown+= 20;
2177                 
2178                 if(state & UI_SCROLL_PRESSED) {
2179                         wcol->inner[0]= wcol->inner[0]>=250? 255 : wcol->inner[0]+5;
2180                         wcol->inner[1]= wcol->inner[1]>=250? 255 : wcol->inner[1]+5;
2181                         wcol->inner[2]= wcol->inner[2]>=250? 255 : wcol->inner[2]+5;
2182                 }
2183
2184                 /* draw */
2185                 wtb.emboss= 0; /* only emboss once */
2186                 
2187                 /* exception for progress bar */
2188                 if (state & UI_SCROLL_NO_OUTLINE)       
2189                         SWAP(short, outline, wtb.outline);
2190                 
2191                 round_box_edges(&wtb, 15, slider, rad); 
2192                 
2193                 if(state & UI_SCROLL_ARROWS) {
2194                         if(wcol->item[0] > 48) wcol->item[0]-= 48;
2195                         if(wcol->item[1] > 48) wcol->item[1]-= 48;
2196                         if(wcol->item[2] > 48) wcol->item[2]-= 48;
2197                         wcol->item[3]= 255;
2198                         
2199                         if(horizontal) {
2200                                 widget_scroll_circle(&wtb.tria1, slider, 0.6f, 'l');
2201                                 widget_scroll_circle(&wtb.tria2, slider, 0.6f, 'r');
2202                         }
2203                         else {
2204                                 widget_scroll_circle(&wtb.tria1, slider, 0.6f, 'b');
2205                                 widget_scroll_circle(&wtb.tria2, slider, 0.6f, 't');
2206                         }
2207                 }
2208                 widgetbase_draw(&wtb, wcol);
2209                 
2210                 if (state & UI_SCROLL_NO_OUTLINE)
2211                         SWAP(short, outline, wtb.outline);
2212         }       
2213 }
2214
2215 static void widget_scroll(uiBut *but, uiWidgetColors *wcol, rcti *rect, int state, int UNUSED(roundboxalign))
2216 {
2217         rcti rect1;
2218         double value;
2219         float fac, size, min;
2220         int horizontal;
2221
2222         /* calculate slider part */
2223         value= ui_get_but_val(but);
2224
2225         size= (but->softmax + but->a1 - but->softmin);
2226         size= MAX2(size, 2);
2227         
2228         /* position */
2229         rect1= *rect;
2230
2231         /* determine horizontal/vertical */
2232         horizontal= (rect->xmax - rect->xmin > rect->ymax - rect->ymin);
2233         
2234         if(horizontal) {
2235                 fac= (rect->xmax - rect->xmin)/(size);
2236                 rect1.xmin= rect1.xmin + ceilf(fac*((float)value - but->softmin));
2237                 rect1.xmax= rect1.xmin + ceilf(fac*(but->a1 - but->softmin));
2238
2239                 /* ensure minimium size */
2240                 min= rect->ymax - rect->ymin;
2241
2242                 if(rect1.xmax - rect1.xmin < min) {
2243                         rect1.xmax= rect1.xmin + min;
2244
2245                         if(rect1.xmax > rect->xmax) {
2246                                 rect1.xmax= rect->xmax;
2247                                 rect1.xmin= MAX2(rect1.xmax - min, rect->xmin);
2248                         }
2249                 }
2250         }
2251         else {
2252                 fac= (rect->ymax - rect->ymin)/(size);
2253                 rect1.ymax= rect1.ymax - ceilf(fac*((float)value - but->softmin));
2254                 rect1.ymin= rect1.ymax - ceilf(fac*(but->a1 - but->softmin));
2255
2256                 /* ensure minimium size */
2257                 min= rect->xmax - rect->xmin;
2258
2259                 if(rect1.ymax - rect1.ymin < min) {
2260                         rect1.ymax= rect1.ymin + min;
2261
2262                         if(rect1.ymax > rect->ymax) {
2263                                 rect1.ymax= rect->ymax;
2264                                 rect1.ymin= MAX2(rect1.ymax - min, rect->ymin);
2265                         }
2266                 }
2267         }
2268
2269         if(state & UI_SELECT)
2270                 state= UI_SCROLL_PRESSED;
2271         else
2272                 state= 0;
2273         uiWidgetScrollDraw(wcol, rect, &rect1, state);
2274 }
2275
2276 static void widget_progressbar(uiBut *but, uiWidgetColors *wcol, rcti *rect, int UNUSED(state), int UNUSED(roundboxalign))
2277 {
2278         rcti rect_prog = *rect, rect_bar = *rect;
2279         float value = but->a1;
2280         float w, min;
2281         
2282         /* make the progress bar a proportion of the original height */
2283         /* hardcoded 4px high for now */
2284         rect_prog.ymax = rect_prog.ymin + 4;
2285         rect_bar.ymax = rect_bar.ymin + 4;
2286         
2287         w = value * (rect_prog.xmax - rect_prog.xmin);
2288         
2289         /* ensure minimium size */
2290         min= rect_prog.ymax - rect_prog.ymin;
2291         w = MAX2(w, min);
2292         
2293         rect_bar.xmax = rect_bar.xmin + w;
2294                 
2295         uiWidgetScrollDraw(wcol, &rect_prog, &rect_bar, UI_SCROLL_NO_OUTLINE);
2296         
2297         /* raise text a bit */
2298         rect->ymin += 6;
2299         rect->xmin -= 6;
2300 }
2301
2302 static void widget_link(uiBut *but, uiWidgetColors *UNUSED(wcol), rcti *rect, int UNUSED(state), int UNUSED(roundboxalign))
2303 {
2304         
2305         if(but->flag & UI_SELECT) {
2306                 rcti rectlink;
2307                 
2308                 UI_ThemeColor(TH_TEXT_HI);
2309                 
2310                 rectlink.xmin= (rect->xmin+rect->xmax)/2;
2311                 rectlink.ymin= (rect->ymin+rect->ymax)/2;
2312                 rectlink.xmax= but->linkto[0];
2313                 rectlink.ymax= but->linkto[1];
2314                 
2315                 ui_draw_link_bezier(&rectlink);
2316         }
2317 }
2318
2319 static void widget_numslider(uiBut *but, uiWidgetColors *wcol, rcti *rect, int state, int roundboxalign)
2320 {
2321         uiWidgetBase wtb, wtb1;
2322         rcti rect1;
2323         double value;
2324         float offs, toffs, fac;
2325         char outline[3];
2326
2327         widget_init(&wtb);
2328         widget_init(&wtb1);
2329         
2330         /* backdrop first */
2331         
2332         /* fully rounded */
2333         offs= 0.5f*(rect->ymax - rect->ymin);
2334         toffs = offs*0.75f;
2335         round_box_edges(&wtb, roundboxalign, rect, offs);
2336
2337         wtb.outline= 0;
2338         widgetbase_draw(&wtb, wcol);
2339         
2340         /* draw left/right parts only when not in text editing */
2341         if(!(state & UI_TEXTINPUT)) {
2342                 
2343                         /* slider part */
2344                 VECCOPY(outline, wcol->outline);
2345                 VECCOPY(wcol->outline, wcol->item);
2346                 VECCOPY(wcol->inner, wcol->item);
2347
2348                 if(!(state & UI_SELECT))
2349                         SWAP(short, wcol->shadetop, wcol->shadedown);
2350                 
2351                 rect1= *rect;
2352                 
2353                 value= ui_get_but_val(but);
2354                 fac= ((float)value-but->softmin)*(rect1.xmax - rect1.xmin - offs)/(but->softmax - but->softmin);
2355                 
2356                 /* left part of slider, always rounded */
2357                 rect1.xmax= rect1.xmin + ceil(offs+1.0f);
2358                 round_box_edges(&wtb1, roundboxalign & ~6, &rect1, offs);
2359                 wtb1.outline= 0;
2360                 widgetbase_draw(&wtb1, wcol);
2361                 
2362                 /* right part of slider, interpolate roundness */
2363                 rect1.xmax= rect1.xmin + fac + offs;
2364                 rect1.xmin+=  floor(offs-1.0f);
2365                 if(rect1.xmax + offs > rect->xmax)
2366                         offs*= (rect1.xmax + offs - rect->xmax)/offs;
2367                 else 
2368                         offs= 0.0f;
2369                 round_box_edges(&wtb1, roundboxalign & ~9, &rect1, offs);
2370                 
2371                 widgetbase_draw(&wtb1, wcol);
2372                 VECCOPY(wcol->outline, outline);
2373                 
2374                 if(!(state & UI_SELECT))
2375                         SWAP(short, wcol->shadetop, wcol->shadedown);
2376         }
2377         
2378         /* outline */
2379         wtb.outline= 1;
2380         wtb.inner= 0;
2381         widgetbase_draw(&wtb, wcol);
2382         
2383         /* text space */
2384         rect->xmin += toffs;
2385         rect->xmax -= toffs;
2386 }
2387
2388 /* I think 3 is sufficient border to indicate keyed status */
2389 #define SWATCH_KEYED_BORDER 3
2390
2391 static void widget_swatch(uiBut *but, uiWidgetColors *wcol, rcti *rect, int state, int roundboxalign)
2392 {
2393         uiWidgetBase wtb;
2394         float col[4];
2395         int color_profile = but->block->color_profile;
2396         
2397         col[3]= 1.0f;
2398
2399         if (but->rnaprop) {
2400                 if (RNA_property_subtype(but->rnaprop) == PROP_COLOR_GAMMA)
2401                         color_profile = BLI_PR_NONE;
2402
2403                 if(RNA_property_array_length(&but->rnapoin, but->rnaprop)==4) {
2404                         col[3]= RNA_property_float_get_index(&but->rnapoin, but->rnaprop, 3);
2405                 }
2406         }
2407         
2408         widget_init(&wtb);
2409         
2410         /* half rounded */
2411         round_box_edges(&wtb, roundboxalign, rect, 5.0f);
2412                 
2413         ui_get_but_vectorf(but, col);
2414         
2415         if(state & (UI_BUT_ANIMATED|UI_BUT_ANIMATED_KEY|UI_BUT_DRIVEN|UI_BUT_REDALERT)) {
2416                 // draw based on state - color for keyed etc
2417                 widgetbase_draw(&wtb, wcol);
2418                 
2419                 // inset to draw swatch color
2420                 rect->xmin+= SWATCH_KEYED_BORDER;
2421                 rect->xmax-= SWATCH_KEYED_BORDER;
2422                 rect->ymin+= SWATCH_KEYED_BORDER;
2423                 rect->ymax-= SWATCH_KEYED_BORDER;
2424                 
2425                 round_box_edges(&wtb, roundboxalign, rect, 5.0f);
2426         }
2427         
2428         if (color_profile)
2429                 linearrgb_to_srgb_v3_v3(col, col);
2430         
2431         wcol->inner[0]= FTOCHAR(col[0]);
2432         wcol->inner[1]= FTOCHAR(col[1]);
2433         wcol->inner[2]= FTOCHAR(col[2]);
2434         wcol->inner[3]= FTOCHAR(col[3]);
2435         wcol->shaded = 0;
2436         wcol->alpha_check = (wcol->inner[3] < 255);
2437
2438         widgetbase_draw(&wtb, wcol);
2439         
2440 }
2441
2442 static void widget_icon_has_anim(uiBut *UNUSED(but), uiWidgetColors *wcol, rcti *rect, int state, int UNUSED(roundboxalign))
2443 {
2444         if(state & (UI_BUT_ANIMATED|UI_BUT_ANIMATED_KEY|UI_BUT_DRIVEN|UI_BUT_REDALERT)) {
2445                 uiWidgetBase wtb;
2446         
2447                 widget_init(&wtb);
2448                 wtb.outline= 0;
2449                 
2450                 /* rounded */
2451                 round_box_edges(&wtb, 15, rect, 10.0f);
2452                 widgetbase_draw(&wtb, wcol);
2453         }       
2454 }
2455
2456
2457 static void widget_textbut(uiWidgetColors *wcol, rcti *rect, int state, int roundboxalign)
2458 {
2459         uiWidgetBase wtb;
2460         
2461         if(state & UI_SELECT)
2462                 SWAP(short, wcol->shadetop, wcol->shadedown);
2463         
2464         widget_init(&wtb);
2465         
2466         /* half rounded */
2467         round_box_edges(&wtb, roundboxalign, rect, 4.0f);
2468         
2469         widgetbase_draw(&wtb, wcol);
2470
2471 }
2472
2473
2474 static void widget_menubut(uiWidgetColors *wcol, rcti *rect, int UNUSED(state), int roundboxalign)
2475 {
2476         uiWidgetBase wtb;
2477         
2478         widget_init(&wtb);
2479         
2480         /* half rounded */
2481         round_box_edges(&wtb, roundboxalign, rect, 4.0f);
2482         
2483         /* decoration */
2484         widget_menu_trias(&wtb.tria1, rect);
2485         
2486         widgetbase_draw(&wtb, wcol);
2487         
2488         /* text space */
2489         rect->xmax -= (rect->ymax-rect->ymin);
2490 }
2491
2492 static void widget_menuiconbut(uiWidgetColors *wcol, rcti *rect, int UNUSED(state), int roundboxalign)
2493 {
2494         uiWidgetBase wtb;
2495         
2496         widget_init(&wtb);
2497         
2498         /* half rounded */
2499         round_box_edges(&wtb, roundboxalign, rect, 4.0f);
2500         
2501         /* decoration */
2502         widgetbase_draw(&wtb, wcol);
2503 }
2504
2505 static void widget_menunodebut(uiWidgetColors *wcol, rcti *rect, int UNUSED(state), int roundboxalign)
2506 {
2507         /* silly node link button hacks */
2508         uiWidgetBase wtb;
2509         unsigned char tmp[4];
2510         unsigned char tmp2[4];
2511         
2512         widget_init(&wtb);
2513         
2514         /* half rounded */
2515         round_box_edges(&wtb, roundboxalign, rect, 4.0f);
2516
2517         memcpy(tmp, wcol->inner, sizeof(char)*4);
2518         memcpy(tmp2, wcol->outline, sizeof(char)*4);
2519         wcol->inner[0] += 15;
2520         wcol->inner[1] += 15;
2521         wcol->inner[2] += 15;
2522         wcol->outline[0] += 15;
2523         wcol->outline[1] += 15;
2524         wcol->outline[2] += 15;
2525         
2526         /* decoration */
2527         widgetbase_draw(&wtb, wcol);
2528
2529         memcpy(wcol->inner, tmp, sizeof(char)*4);
2530         memcpy(wcol->outline, tmp2, sizeof(char)*4);
2531 }
2532
2533 static void widget_pulldownbut(uiWidgetColors *wcol, rcti *rect, int state, int UNUSED(roundboxalign))
2534 {
2535         if(state & UI_ACTIVE) {
2536                 uiWidgetBase wtb;
2537                 float rad= 0.5f*(rect->ymax - rect->ymin); // 4.0f
2538                 
2539                 widget_init(&wtb);
2540                 
2541                 /* half rounded */
2542                 round_box_edges(&wtb, 15, rect, rad);
2543                 
2544                 widgetbase_draw(&wtb, wcol);
2545         }
2546 }
2547
2548 static void widget_menu_itembut(uiWidgetColors *wcol, rcti *rect, int UNUSED(state), int UNUSED(roundboxalign))
2549 {
2550         uiWidgetBase wtb;
2551         
2552         widget_init(&wtb);
2553         
2554         /* not rounded, no outline */
2555         wtb.outline= 0;
2556         round_box_edges(&wtb, 0, rect, 0.0f);
2557         
2558         widgetbase_draw(&wtb, wcol);
2559 }
2560
2561 static void widget_list_itembut(uiWidgetColors *wcol, rcti *rect, int UNUSED(state), int UNUSED(roundboxalign))
2562 {
2563         uiWidgetBase wtb;
2564         
2565         widget_init(&wtb);
2566         
2567         /* rounded, but no outline */
2568         wtb.outline= 0;
2569         round_box_edges(&wtb, 15, rect, 4.0f);
2570         
2571         widgetbase_draw(&wtb, wcol);
2572 }
2573
2574 static void widget_optionbut(uiWidgetColors *wcol, rcti *rect, int state, int UNUSED(roundboxalign))
2575 {
2576         uiWidgetBase wtb;
2577         rcti recttemp= *rect;
2578         int delta;
2579         
2580         widget_init(&wtb);
2581         
2582         /* square */
2583         recttemp.xmax= recttemp.xmin + (recttemp.ymax-recttemp.ymin);
2584         
2585         /* smaller */
2586         delta= 1 + (recttemp.ymax-recttemp.ymin)/8;
2587         recttemp.xmin+= delta;
2588         recttemp.ymin+= delta;
2589         recttemp.xmax-= delta;
2590         recttemp.ymax-= delta;
2591         
2592         /* half rounded */
2593         round_box_edges(&wtb, 15, &recttemp, 4.0f);
2594         
2595         /* decoration */
2596         if(state & UI_SELECT) {
2597                 widget_check_trias(&wtb.tria1, &recttemp);
2598         }
2599         
2600         widgetbase_draw(&wtb, wcol);
2601         
2602         /* text space */
2603         rect->xmin += (rect->ymax-rect->ymin)*0.7 + delta;
2604 }
2605
2606
2607 static void widget_radiobut(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_box(uiBut *but, uiWidgetColors *wcol, rcti *rect, int UNUSED(state), int roundboxalign)
2621 {
2622         uiWidgetBase wtb;
2623         char old_col[3];
2624         
2625         widget_init(&wtb);
2626         
2627         VECCOPY(old_col, wcol->inner);
2628         
2629         /* abuse but->hsv - if it's non-zero, use this color as the box's background */
2630         if (but->col[3]) {
2631                 wcol->inner[0] = but->col[0];
2632                 wcol->inner[1] = but->col[1];
2633                 wcol->inner[2] = but->col[2];
2634         }
2635         
2636         /* half rounded */
2637         round_box_edges(&wtb, roundboxalign, rect, 4.0f);
2638         
2639         widgetbase_draw(&wtb, wcol);
2640         
2641         /* store the box bg as gl clearcolor, to retrieve later when drawing semi-transparent rects
2642          * over the top to indicate disabled buttons */
2643         /* XXX, this doesnt work right since the color applies to buttons outside the box too. */
2644         glClearColor(wcol->inner[0]/255.0, wcol->inner[1]/255.0, wcol->inner[2]/255.0, 1.0);
2645         
2646         VECCOPY(wcol->inner, old_col);
2647 }
2648
2649 static void widget_but(uiWidgetColors *wcol, rcti *rect, int UNUSED(state), int roundboxalign)
2650 {
2651         uiWidgetBase wtb;
2652         
2653         widget_init(&wtb);
2654         
2655         /* half rounded */
2656         round_box_edges(&wtb, roundboxalign, rect, 4.0f);
2657                 
2658         widgetbase_draw(&wtb, wcol);
2659
2660 }
2661
2662 static void widget_roundbut(uiWidgetColors *wcol, rcti *rect, int UNUSED(state), int roundboxalign)
2663 {
2664         uiWidgetBase wtb;
2665         float rad= 5.0f; //0.5f*(rect->ymax - rect->ymin);
2666         
2667         widget_init(&wtb);
2668         
2669         /* half rounded */
2670         round_box_edges(&wtb, roundboxalign, rect, rad);
2671
2672         widgetbase_draw(&wtb, wcol);
2673 }
2674
2675 static void widget_draw_extra_mask(const bContext *C, uiBut *but, uiWidgetType *wt, rcti *rect)
2676 {
2677         uiWidgetBase wtb;
2678         unsigned char col[4];
2679         
2680         /* state copy! */
2681         wt->wcol= *(wt->wcol_theme);
2682         
2683         widget_init(&wtb);
2684         
2685         if(but->block->drawextra) {
2686                 /* note: drawextra can change rect +1 or -1, to match round errors of existing previews */
2687                 but->block->drawextra(C, but->poin, but->block->drawextra_arg1, but->block->drawextra_arg2, rect);
2688                 
2689                 /* make mask to draw over image */
2690                 UI_GetThemeColor3ubv(TH_BACK, col);
2691                 glColor3ubv(col);
2692                 
2693                 round_box__edges(&wtb, 15, rect, 0.0f, 4.0);
2694                 widgetbase_outline(&wtb);
2695         }
2696         
2697         /* outline */
2698         round_box_edges(&wtb, 15, rect, 5.0f);
2699         wtb.outline= 1;
2700         wtb.inner= 0;
2701         widgetbase_draw(&wtb, &wt->wcol);
2702         
2703 }
2704
2705
2706 static void widget_disabled(rcti *rect)
2707 {
2708         float col[4];
2709         
2710         glEnable(GL_BLEND);
2711         
2712         /* can't use theme TH_BACK or TH_PANEL... undefined */
2713         glGetFloatv(GL_COLOR_CLEAR_VALUE, col);
2714         glColor4f(col[0], col[1], col[2], 0.5f);
2715
2716         /* need -1 and +1 to make it work right for aligned buttons,
2717          * but problem may be somewhere else? */
2718         glRectf(rect->xmin-1, rect->ymin-1, rect->xmax, rect->ymax+1);
2719         
2720         glDisable(GL_BLEND);
2721 }
2722
2723 static uiWidgetType *widget_type(uiWidgetTypeEnum type)
2724 {
2725         bTheme *btheme= U.themes.first;
2726         static uiWidgetType wt;
2727         
2728         /* defaults */
2729         wt.wcol_theme= &btheme->tui.wcol_regular;
2730         wt.wcol_state= &btheme->tui.wcol_state;
2731         wt.state= widget_state;
2732         wt.draw= widget_but;
2733         wt.custom= NULL;
2734         wt.text= widget_draw_text_icon;
2735         
2736         switch(type) {
2737                 case UI_WTYPE_REGULAR:
2738                         break;
2739
2740                 case UI_WTYPE_LABEL:
2741                         wt.draw= NULL;
2742                         wt.state= widget_state_label;
2743                         break;
2744                         
2745                 case UI_WTYPE_TOGGLE:
2746                         wt.wcol_theme= &btheme->tui.wcol_toggle;
2747                         break;
2748                         
2749                 case UI_WTYPE_OPTION:
2750                         wt.wcol_theme= &btheme->tui.wcol_option;
2751                         wt.draw= widget_optionbut;
2752                         break;
2753                         
2754                 case UI_WTYPE_RADIO:
2755                         wt.wcol_theme= &btheme->tui.wcol_radio;
2756                         wt.draw= widget_radiobut;
2757                         break;
2758
2759                 case UI_WTYPE_NUMBER:
2760                         wt.wcol_theme= &btheme->tui.wcol_num;
2761                         wt.draw= widget_numbut;
2762                         break;
2763                         
2764                 case UI_WTYPE_SLIDER:
2765                         wt.wcol_theme= &btheme->tui.wcol_numslider;
2766                         wt.custom= widget_numslider;
2767                         wt.state= widget_state_numslider;
2768                         break;
2769                         
2770                 case UI_WTYPE_EXEC:
2771                         wt.wcol_theme= &btheme->tui.wcol_tool;
2772                         wt.draw= widget_roundbut;
2773                         break;
2774                         
2775                         
2776                         /* strings */
2777                 case UI_WTYPE_NAME:
2778                         wt.wcol_theme= &btheme->tui.wcol_text;
2779                         wt.draw= widget_textbut;
2780                         break;
2781                         
2782                 case UI_WTYPE_NAME_LINK:
2783                         break;
2784                         
2785                 case UI_WTYPE_POINTER_LINK:
2786                         break;
2787                         
2788                 case UI_WTYPE_FILENAME:
2789                         break;
2790                         
2791                         
2792                         /* start menus */
2793                 case UI_WTYPE_MENU_RADIO:
2794                         wt.wcol_theme= &btheme->tui.wcol_menu;
2795                         wt.draw= widget_menubut;
2796                         break;
2797
2798                 case UI_WTYPE_MENU_ICON_RADIO:
2799                         wt.wcol_theme= &btheme->tui.wcol_menu;
2800                         wt.draw= widget_menuiconbut;
2801                         break;
2802                         
2803                 case UI_WTYPE_MENU_POINTER_LINK:
2804                         wt.wcol_theme= &btheme->tui.wcol_menu;
2805                         wt.draw= widget_menubut;
2806                         break;
2807
2808                 case UI_WTYPE_MENU_NODE_LINK:
2809                         wt.wcol_theme= &btheme->tui.wcol_menu;
2810                         wt.draw= widget_menunodebut;
2811                         break;
2812                         
2813                 case UI_WTYPE_PULLDOWN:
2814                         wt.wcol_theme= &btheme->tui.wcol_pulldown;
2815                         wt.draw= widget_pulldownbut;
2816                         wt.state= widget_state_pulldown;
2817                         break;
2818                         
2819                         /* in menus */
2820                 case UI_WTYPE_MENU_ITEM:
2821                         wt.wcol_theme= &btheme->tui.wcol_menu_item;
2822                         wt.draw= widget_menu_itembut;
2823                         wt.state= widget_state_menu_item;
2824                         break;
2825                         
2826                 case UI_WTYPE_MENU_BACK:
2827                         wt.wcol_theme= &btheme->tui.wcol_menu_back;
2828                         wt.draw= widget_menu_back;
2829                         break;
2830                         
2831                         /* specials */
2832                 case UI_WTYPE_ICON:
2833                         wt.custom= widget_icon_has_anim;
2834                         break;
2835                         
2836                 case UI_WTYPE_SWATCH:
2837                         wt.custom= widget_swatch;
2838                         break;
2839                         
2840                 case UI_WTYPE_BOX:
2841                         wt.custom= widget_box;
2842                         wt.wcol_theme= &btheme->tui.wcol_box;
2843                         break;
2844                         
2845                 case UI_WTYPE_RGB_PICKER:
2846                         break;
2847                         
2848                 case UI_WTYPE_NORMAL:
2849                         break;
2850
2851                 case UI_WTYPE_SCROLL:
2852                         wt.wcol_theme= &btheme->tui.wcol_scroll;
2853                         wt.state= widget_state_nothing;
2854                         wt.custom= widget_scroll;
2855                         break;
2856
2857                 case UI_WTYPE_LISTITEM:
2858                         wt.wcol_theme= &btheme->tui.wcol_list_item;
2859                         wt.draw= widget_list_itembut;
2860                         break;
2861                         
2862                 case UI_WTYPE_PROGRESSBAR:
2863                         wt.wcol_theme= &btheme->tui.wcol_progress;
2864                         wt.custom= widget_progressbar;
2865                         break;
2866         }
2867         
2868         return &wt;
2869 }
2870
2871
2872 static int widget_roundbox_set(uiBut *but, rcti *rect)
2873 {
2874         /* alignment */
2875         if(but->flag & UI_BUT_ALIGN) {
2876                 
2877                 if(but->flag & UI_BUT_ALIGN_TOP)
2878                         rect->ymax+= 1;
2879                 if(but->flag & UI_BUT_ALIGN_LEFT)
2880                         rect->xmin-= 1;
2881                 
2882                 switch(but->flag & UI_BUT_ALIGN) {
2883                         case UI_BUT_ALIGN_TOP:
2884                                 return (12);
2885                                 break;
2886                         case UI_BUT_ALIGN_DOWN:
2887                                 return (3);
2888                                 break;
2889                         case UI_BUT_ALIGN_LEFT:
2890                                 return (6);
2891                                 break;
2892                         case UI_BUT_ALIGN_RIGHT:
2893                                 return (9);
2894                                 break;
2895                                 
2896                         case UI_BUT_ALIGN_DOWN|UI_BUT_ALIGN_RIGHT:
2897                                 return (1);
2898                                 break;
2899                         case UI_BUT_ALIGN_DOWN|UI_BUT_ALIGN_LEFT:
2900                                 return (2);
2901                                 break;
2902                         case UI_BUT_ALIGN_TOP|UI_BUT_ALIGN_RIGHT:
2903                                 return (8);
2904                                 break;
2905                         case UI_BUT_ALIGN_TOP|UI_BUT_ALIGN_LEFT:
2906                                 return (4);
2907                                 break;
2908                                 
2909                         default:
2910                                 return (0);
2911                                 break;
2912                 }
2913         } 
2914         return 15;
2915 }
2916
2917 /* conversion from old to new buttons, so still messy */
2918 void ui_draw_but(const bContext *C, ARegion *ar, uiStyle *style, uiBut *but, rcti *rect)
2919 {
2920         bTheme *btheme= U.themes.first;
2921         ThemeUI *tui= &btheme->tui;
2922         uiFontStyle *fstyle= &style->widget;
2923         uiWidgetType *wt= NULL;
2924
2925         /* handle menus separately */
2926         if(but->dt==UI_EMBOSSP) {
2927                 switch (but->type) {
2928                         case LABEL:
2929                                 widget_draw_text_icon(&style->widgetlabel, &tui->wcol_menu_back, but, rect);
2930                                 break;
2931                         case SEPR:
2932                                 ui_draw_separator(rect, &tui->wcol_menu_item);
2933                                 break;
2934                                 
2935                         default:
2936                                 wt= widget_type(UI_WTYPE_MENU_ITEM);
2937                 }
2938         }
2939         else if(but->dt==UI_EMBOSSN) {
2940                 /* "nothing" */
2941                 wt= widget_type(UI_WTYPE_ICON);
2942         }
2943         else {
2944                 
2945                 switch (but->type) {
2946                         case LABEL:
2947                                 if(but->block->flag & UI_BLOCK_LOOP)
2948                                         widget_draw_text_icon(&style->widgetlabel, &tui->wcol_menu_back, but, rect);
2949                                 else {
2950                                         wt= widget_type(UI_WTYPE_LABEL);
2951                                         fstyle= &style->widgetlabel;
2952                                 }
2953                                 break;
2954                                 
2955                         case SEPR:
2956                                 break;
2957                                 
2958                         case BUT:
2959                                 wt= widget_type(UI_WTYPE_EXEC);
2960                                 break;
2961
2962                         case NUM:
2963                                 wt= widget_type(UI_WTYPE_NUMBER);
2964                                 break;
2965                                 
2966                         case NUMSLI:
2967                         case HSVSLI:
2968                                 wt= widget_type(UI_WTYPE_SLIDER);
2969                                 break;
2970                                 
2971                         case ROW:
2972                                 wt= widget_type(UI_WTYPE_RADIO);
2973                                 break;
2974
2975                         case LISTROW:
2976                                 wt= widget_type(UI_WTYPE_LISTITEM);
2977                                 break;
2978                                 
2979                         case TEX:
2980                                 wt= widget_type(UI_WTYPE_NAME);
2981                                 break;
2982                                 
2983                         case SEARCH_MENU:
2984                                 wt= widget_type(UI_WTYPE_NAME);
2985                                 if(but->block->flag & UI_BLOCK_LOOP)
2986                                         wt->wcol_theme= &btheme->tui.wcol_menu_back;
2987                                 break;
2988                                 
2989                         case TOGBUT:
2990                         case TOG:
2991                         case TOGN:
2992                         case TOG3:
2993                                 wt= widget_type(UI_WTYPE_TOGGLE);
2994                                 break;
2995                                 
2996                         case OPTION:
2997                         case OPTIONN:
2998                                 if (!(but->flag & UI_HAS_ICON)) {
2999                                         wt= widget_type(UI_WTYPE_OPTION);
3000                                         but->flag |= UI_TEXT_LEFT;
3001                                 }
3002                                 else
3003                                         wt= widget_type(UI_WTYPE_TOGGLE);
3004                                 
3005                                 /* option buttons have strings outside, on menus use different colors */
3006                                 if(but->block->flag & UI_BLOCK_LOOP)
3007                                         wt->state= widget_state_option_menu;
3008                                 
3009                                 break;
3010                                 
3011                         case MENU:
3012                         case BLOCK:
3013                         case ICONTEXTROW:
3014                                 if(but->flag & UI_BUT_NODE_LINK)
3015                                         wt= widget_type(UI_WTYPE_MENU_NODE_LINK);
3016                                 else if(!but->str[0] && but->icon)
3017                                         wt= widget_type(UI_WTYPE_MENU_ICON_RADIO);
3018                                 else
3019                                         wt= widget_type(UI_WTYPE_MENU_RADIO);
3020                                 break;
3021                                 
3022                         case PULLDOWN:
3023                                 wt= widget_type(UI_WTYPE_PULLDOWN);
3024                                 break;
3025                         
3026                         case BUTM:
3027                                 wt= widget_type(UI_WTYPE_MENU_ITEM);
3028                                 break;
3029                                 
3030                         case COL:
3031                                 wt= widget_type(UI_WTYPE_SWATCH);
3032                                 break;
3033                                 
3034                         case ROUNDBOX:
3035                         case LISTBOX:
3036                                 wt= widget_type(UI_WTYPE_BOX);
3037                                 break;
3038                                 
3039                         case LINK:
3040                         case INLINK:
3041                                 wt= widget_type(UI_WTYPE_ICON);
3042                                 wt->custom= widget_link;
3043                                 
3044                                 break;
3045                         
3046                         case BUT_EXTRA:
3047                                 widget_draw_extra_mask(C, but, widget_type(UI_WTYPE_BOX), rect);
3048                                 break;
3049                                 
3050                         case HSVCUBE:
3051                                 if(but->a1 == UI_GRAD_V_ALT) // vertical V slider, uses new widget draw now
3052                                         ui_draw_but_HSV_v(but, rect);
3053                                 else  // other HSV pickers...
3054                                         ui_draw_but_HSVCUBE(but, rect);
3055                                 break;
3056                                 
3057                         case HSVCIRCLE:
3058                                 ui_draw_but_HSVCIRCLE(but, &tui->wcol_regular, rect);
3059                                 break;
3060                                 
3061                         case BUT_COLORBAND:
3062                                 ui_draw_but_COLORBAND(but, &tui->wcol_regular, rect);
3063                                 break;
3064                                 
3065                         case BUT_NORMAL:
3066                                 ui_draw_but_NORMAL(but, &tui->wcol_regular, rect);
3067                                 break;
3068                                 
3069                         case BUT_IMAGE:
3070                                 ui_draw_but_IMAGE(ar, but, &tui->wcol_regular, rect);
3071                                 break;
3072                         
3073                         case HISTOGRAM:
3074                                 ui_draw_but_HISTOGRAM(ar, but, &tui->wcol_regular, rect);
3075                                 break;
3076                                 
3077                         case WAVEFORM:
3078                                 ui_draw_but_WAVEFORM(ar, but, &tui->wcol_regular, rect);
3079                                 break;
3080                                 
3081                         case VECTORSCOPE:
3082                                 ui_draw_but_VECTORSCOPE(ar, but, &tui->wcol_regular, rect);
3083                                 break;
3084                                         
3085                         case BUT_CURVE:
3086                                 ui_draw_but_CURVE(ar, but, &tui->wcol_regular, rect);
3087                                 break;
3088                                 
3089                         case PROGRESSBAR:
3090                                 wt= widget_type(UI_WTYPE_PROGRESSBAR);
3091                                 fstyle= &style->widgetlabel;
3092                                 break;
3093
3094                         case SCROLL:
3095                                 wt= widget_type(UI_WTYPE_SCROLL);
3096                                 break;
3097
3098                         default:
3099                                 wt= widget_type(UI_WTYPE_REGULAR);
3100                 }
3101         }
3102         
3103         if(wt) {
3104                 rcti disablerect= *rect; /* rect gets clipped smaller for text */
3105                 int roundboxalign, state;
3106                 
3107                 roundboxalign= widget_roundbox_set(but, rect);
3108
3109                 state= but->flag;
3110                 if(but->editstr) state |= UI_TEXTINPUT;
3111                 
3112                 wt->state(wt, state);
3113                 if(wt->custom)
3114                         wt->custom(but, &wt->wcol, rect, state, roundboxalign);
3115                 else if(wt->draw)
3116                         wt->draw(&wt->wcol, rect, state, roundboxalign);
3117                 wt->text(fstyle, &wt->wcol, but, rect);
3118                 
3119                 if(state & (UI_BUT_DISABLED|UI_BUT_INACTIVE))
3120                         if(but->dt!=UI_EMBOSSP)
3121                                 widget_disabled(&disablerect);
3122         }
3123 }
3124