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