bugfix [#23355] Square Color picker moving by itself and locking up
[blender-staging.git] / source / blender / editors / interface / interface_widgets.c
1 /**
2 * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version. 
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2009 Blender Foundation.
19  * All rights reserved.
20  * 
21  * Contributor(s): Blender Foundation
22  *
23  * ***** END GPL LICENSE BLOCK *****
24  */
25
26 #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], hsvo[3], hsv[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         copy_v3_v3(hsv, ui_block_hsv_get(but->block));
1668         rgb_to_hsv_compat(rgb[0], rgb[1], rgb[2], hsv, hsv+1, hsv+2);
1669         copy_v3_v3(hsvo, hsv);
1670         
1671         /* exception: if 'lock' is set
1672          * lock the value of the color wheel to 1.
1673          * Useful for color correction tools where you're only interested in hue. */
1674         if (but->flag & UI_BUT_COLOR_LOCK)
1675                 hsv[2] = 1.f;
1676         else if (color_profile)
1677                 hsv[2] = linearrgb_to_srgb(hsv[2]);
1678         
1679         hsv_to_rgb(0.f, 0.f, hsv[2], colcent, colcent+1, colcent+2);
1680         
1681         glShadeModel(GL_SMOOTH);
1682
1683         glBegin(GL_TRIANGLE_FAN);
1684         glColor3fv(colcent);
1685         glVertex2f( centx, centy);
1686         
1687         for(a=0; a<=tot; a++, ang+=radstep) {
1688                 float si= sin(ang);
1689                 float co= cos(ang);
1690                 
1691                 ui_hsvcircle_vals_from_pos(hsv, hsv+1, rect, centx + co*radius, centy + si*radius);
1692                 CLAMP(hsv[2], 0.0f, 1.0f); /* for display only */
1693
1694                 hsv_to_rgb(hsv[0], hsv[1], hsv[2], col, col+1, col+2);
1695                 glColor3fv(col);
1696                 glVertex2f( centx + co*radius, centy + si*radius);
1697         }
1698         glEnd();
1699         
1700         glShadeModel(GL_FLAT);
1701         
1702         /* fully rounded outline */
1703         glPushMatrix();
1704         glTranslatef(centx, centy, 0.0f);
1705         glEnable(GL_BLEND);
1706         glEnable(GL_LINE_SMOOTH );
1707         glColor3ubv((unsigned char*)wcol->outline);
1708         glutil_draw_lined_arc(0.0f, M_PI*2.0, radius, tot + 1);
1709         glDisable(GL_BLEND);
1710         glDisable(GL_LINE_SMOOTH );
1711         glPopMatrix();
1712
1713         /* cursor */
1714         ang= 2.0f*M_PI*hsvo[0] + 0.5f*M_PI;
1715
1716         if(but->flag & UI_BUT_COLOR_CUBIC)
1717                 radius= (1.0f - pow(1.0f - hsvo[1], 3.0f)) *radius;
1718         else
1719                 radius= hsvo[1] * radius;
1720
1721         ui_hsv_cursor(centx + cos(-ang)*radius, centy + sin(-ang)*radius);
1722         
1723 }
1724
1725 /* ************ custom buttons, old stuff ************** */
1726
1727 /* draws in resolution of 20x4 colors */
1728 void ui_draw_gradient(rcti *rect, float *rgb, int type, float alpha)
1729 {
1730         int a;
1731         float h, s, v;
1732         float dx, dy, sx1, sx2, sy;
1733         float col0[4][3];       // left half, rect bottom to top
1734         float col1[4][3];       // right half, rect bottom to top
1735         
1736         rgb_to_hsv(rgb[0], rgb[1], rgb[2], &h, &s, &v);
1737         
1738         /* draw series of gouraud rects */
1739         glShadeModel(GL_SMOOTH);
1740         
1741         switch(type) {
1742                 case UI_GRAD_SV:
1743                         hsv_to_rgb(h, 0.0, 0.0,   &col1[0][0], &col1[0][1], &col1[0][2]);
1744                         hsv_to_rgb(h, 0.333, 0.0, &col1[1][0], &col1[1][1], &col1[1][2]);
1745                         hsv_to_rgb(h, 0.666, 0.0, &col1[2][0], &col1[2][1], &col1[2][2]);
1746                         hsv_to_rgb(h, 1.0, 0.0,   &col1[3][0], &col1[3][1], &col1[3][2]);
1747                         break;
1748                 case UI_GRAD_HV:
1749                         hsv_to_rgb(0.0, s, 0.0,   &col1[0][0], &col1[0][1], &col1[0][2]);
1750                         hsv_to_rgb(0.0, s, 0.333, &col1[1][0], &col1[1][1], &col1[1][2]);
1751                         hsv_to_rgb(0.0, s, 0.666, &col1[2][0], &col1[2][1], &col1[2][2]);
1752                         hsv_to_rgb(0.0, s, 1.0,   &col1[3][0], &col1[3][1], &col1[3][2]);
1753                         break;
1754                 case UI_GRAD_HS:
1755                         hsv_to_rgb(0.0, 0.0, v,   &col1[0][0], &col1[0][1], &col1[0][2]);
1756                         hsv_to_rgb(0.0, 0.333, v, &col1[1][0], &col1[1][1], &col1[1][2]);
1757                         hsv_to_rgb(0.0, 0.666, v, &col1[2][0], &col1[2][1], &col1[2][2]);
1758                         hsv_to_rgb(0.0, 1.0, v,   &col1[3][0], &col1[3][1], &col1[3][2]);
1759                         break;
1760                 case UI_GRAD_H:
1761                         hsv_to_rgb(0.0, 1.0, 1.0,   &col1[0][0], &col1[0][1], &col1[0][2]);
1762                         VECCOPY(col1[1], col1[0]);
1763                         VECCOPY(col1[2], col1[0]);
1764                         VECCOPY(col1[3], col1[0]);
1765                         break;
1766                 case UI_GRAD_S:
1767                         hsv_to_rgb(1.0, 0.0, 1.0,   &col1[1][0], &col1[1][1], &col1[1][2]);
1768                         VECCOPY(col1[0], col1[1]);
1769                         VECCOPY(col1[2], col1[1]);
1770                         VECCOPY(col1[3], col1[1]);
1771                         break;
1772                 case UI_GRAD_V:
1773                         hsv_to_rgb(1.0, 1.0, 0.0,   &col1[2][0], &col1[2][1], &col1[2][2]);
1774                         VECCOPY(col1[0], col1[2]);
1775                         VECCOPY(col1[1], col1[2]);
1776                         VECCOPY(col1[3], col1[2]);
1777                         break;
1778         }
1779         
1780         /* old below */
1781         
1782         for(dx=0.0; dx<1.0; dx+= 0.05) {
1783                 // previous color
1784                 VECCOPY(col0[0], col1[0]);
1785                 VECCOPY(col0[1], col1[1]);
1786                 VECCOPY(col0[2], col1[2]);
1787                 VECCOPY(col0[3], col1[3]);
1788                 
1789                 // new color
1790                 switch(type) {
1791                         case UI_GRAD_SV:
1792                                 hsv_to_rgb(h, 0.0, dx,   &col1[0][0], &col1[0][1], &col1[0][2]);
1793                                 hsv_to_rgb(h, 0.333, dx, &col1[1][0], &col1[1][1], &col1[1][2]);
1794                                 hsv_to_rgb(h, 0.666, dx, &col1[2][0], &col1[2][1], &col1[2][2]);
1795                                 hsv_to_rgb(h, 1.0, dx,   &col1[3][0], &col1[3][1], &col1[3][2]);
1796                                 break;
1797                         case UI_GRAD_HV:
1798                                 hsv_to_rgb(dx, s, 0.0,   &col1[0][0], &col1[0][1], &col1[0][2]);
1799                                 hsv_to_rgb(dx, s, 0.333, &col1[1][0], &col1[1][1], &col1[1][2]);
1800                                 hsv_to_rgb(dx, s, 0.666, &col1[2][0], &col1[2][1], &col1[2][2]);
1801                                 hsv_to_rgb(dx, s, 1.0,   &col1[3][0], &col1[3][1], &col1[3][2]);
1802                                 break;
1803                         case UI_GRAD_HS:
1804                                 hsv_to_rgb(dx, 0.0, v,   &col1[0][0], &col1[0][1], &col1[0][2]);
1805                                 hsv_to_rgb(dx, 0.333, v, &col1[1][0], &col1[1][1], &col1[1][2]);
1806                                 hsv_to_rgb(dx, 0.666, v, &col1[2][0], &col1[2][1], &col1[2][2]);
1807                                 hsv_to_rgb(dx, 1.0, v,   &col1[3][0], &col1[3][1], &col1[3][2]);
1808                                 break;
1809                         case UI_GRAD_H:
1810                                 hsv_to_rgb(dx, 1.0, 1.0,   &col1[0][0], &col1[0][1], &col1[0][2]);
1811                                 VECCOPY(col1[1], col1[0]);
1812                                 VECCOPY(col1[2], col1[0]);
1813                                 VECCOPY(col1[3], col1[0]);
1814                                 break;
1815                         case UI_GRAD_S:
1816                                 hsv_to_rgb(h, dx, 1.0,   &col1[1][0], &col1[1][1], &col1[1][2]);
1817                                 VECCOPY(col1[0], col1[1]);
1818                                 VECCOPY(col1[2], col1[1]);
1819                                 VECCOPY(col1[3], col1[1]);
1820                                 break;
1821                         case UI_GRAD_V:
1822                                 hsv_to_rgb(h, 1.0, dx,   &col1[2][0], &col1[2][1], &col1[2][2]);
1823                                 VECCOPY(col1[0], col1[2]);
1824                                 VECCOPY(col1[1], col1[2]);
1825                                 VECCOPY(col1[3], col1[2]);
1826                                 break;
1827                 }
1828                 
1829                 // rect
1830                 sx1= rect->xmin + dx*(rect->xmax-rect->xmin);
1831                 sx2= rect->xmin + (dx+0.05)*(rect->xmax-rect->xmin);
1832                 sy= rect->ymin;
1833                 dy= (rect->ymax-rect->ymin)/3.0;
1834                 
1835                 glBegin(GL_QUADS);
1836                 for(a=0; a<3; a++, sy+=dy) {
1837                         glColor4f(col0[a][0], col0[a][1], col0[a][2], alpha);
1838                         glVertex2f(sx1, sy);
1839                         
1840                         glColor4f(col1[a][0], col1[a][1], col1[a][2], alpha);
1841                         glVertex2f(sx2, sy);
1842
1843                         glColor4f(col1[a+1][0], col1[a+1][1], col1[a+1][2], alpha);
1844                         glVertex2f(sx2, sy+dy);
1845                         
1846                         glColor4f(col0[a+1][0], col0[a+1][1], col0[a+1][2], alpha);
1847                         glVertex2f(sx1, sy+dy);
1848                 }
1849                 glEnd();
1850         }
1851         
1852         glShadeModel(GL_FLAT);
1853         
1854 }
1855
1856
1857
1858 static void ui_draw_but_HSVCUBE(uiBut *but, rcti *rect)
1859 {
1860         float rgb[3], h,s,v;
1861         float x=0.0f, y=0.0f;
1862         float *hsv= ui_block_hsv_get(but->block);
1863         
1864         h= hsv[0];
1865         s= hsv[1];
1866         v= hsv[2];
1867         
1868         ui_get_but_vectorf(but, rgb);
1869         rgb_to_hsv_compat(rgb[0], rgb[1], rgb[2], &h, &s, &v);
1870         
1871         ui_draw_gradient(rect, rgb, but->a1, 1.f);
1872         
1873         switch((int)but->a1) {
1874                 case UI_GRAD_SV:
1875                         x= v; y= s; break;
1876                 case UI_GRAD_HV:
1877                         x= h; y= v; break;
1878                 case UI_GRAD_HS:
1879                         x= h; y= s; break;
1880                 case UI_GRAD_H:
1881                         x= h; y= 0.5; break;
1882                 case UI_GRAD_S:
1883                         x= s; y= 0.5; break;
1884                 case UI_GRAD_V:
1885                         x= v; y= 0.5; break;
1886         }
1887         
1888         /* cursor */
1889         x= rect->xmin + x*(rect->xmax-rect->xmin);
1890         y= rect->ymin + y*(rect->ymax-rect->ymin);
1891         CLAMP(x, rect->xmin+3.0, rect->xmax-3.0);
1892         CLAMP(y, rect->ymin+3.0, rect->ymax-3.0);
1893         
1894         ui_hsv_cursor(x, y);
1895         
1896         /* outline */
1897         glColor3ub(0,  0,  0);
1898         fdrawbox((rect->xmin), (rect->ymin), (rect->xmax), (rect->ymax));
1899 }
1900
1901 /* vertical 'value' slider, using new widget code */
1902 static void ui_draw_but_HSV_v(uiBut *but, rcti *rect)
1903 {
1904         uiWidgetBase wtb;
1905         float rad= 0.5f*(rect->xmax - rect->xmin);
1906         float x, y;
1907         float rgb[3], hsv[3], v, range;
1908         int color_profile = but->block->color_profile;
1909         
1910         if (but->rnaprop && RNA_property_subtype(but->rnaprop) == PROP_COLOR_GAMMA)
1911                 color_profile = BLI_PR_NONE;
1912
1913         ui_get_but_vectorf(but, rgb);
1914         rgb_to_hsv(rgb[0], rgb[1], rgb[2], hsv, hsv+1, hsv+2);
1915         v = hsv[2];
1916         
1917         if (color_profile)
1918                 v = linearrgb_to_srgb(v);
1919
1920         /* map v from property range to [0,1] */
1921         range = but->softmax - but->softmin;
1922         v =     (v - but->softmin)/range;
1923         
1924         widget_init(&wtb);
1925         
1926         /* fully rounded */
1927         round_box_edges(&wtb, 15, rect, rad);
1928         
1929         /* setup temp colors */
1930         wcol_tmp.outline[0]= wcol_tmp.outline[1]= wcol_tmp.outline[2]= 0;
1931         wcol_tmp.inner[0]= wcol_tmp.inner[1]= wcol_tmp.inner[2]= 128;
1932         wcol_tmp.shadetop= 127;
1933         wcol_tmp.shadedown= -128;
1934         wcol_tmp.shaded= 1;
1935         
1936         widgetbase_draw(&wtb, &wcol_tmp);
1937
1938         /* cursor */
1939         x= rect->xmin + 0.5f * (rect->xmax-rect->xmin);
1940         y= rect->ymin + v * (rect->ymax-rect->ymin);
1941         CLAMP(y, rect->ymin+3.0, rect->ymax-3.0);
1942         
1943         ui_hsv_cursor(x, y);
1944         
1945 }
1946
1947
1948 /* ************ separator, for menus etc ***************** */
1949 static void ui_draw_separator(uiBut *but, rcti *rect,  uiWidgetColors *wcol)
1950 {
1951         int y = rect->ymin + (rect->ymax - rect->ymin)/2 - 1;
1952         unsigned char col[4];
1953         
1954         col[0] = wcol->text[0];
1955         col[1] = wcol->text[1];
1956         col[2] = wcol->text[2];
1957         col[3] = 7;
1958         
1959         glEnable(GL_BLEND);
1960         glColor4ubv(col);
1961         sdrawline(rect->xmin, y, rect->xmax, y);
1962         glDisable(GL_BLEND);
1963 }
1964
1965 /* ************ button callbacks, draw ***************** */
1966
1967 static void widget_numbut(uiWidgetColors *wcol, rcti *rect, int state, int roundboxalign)
1968 {
1969         uiWidgetBase wtb;
1970         float rad= 0.5f*(rect->ymax - rect->ymin);
1971         float textofs = rad*0.75;
1972
1973         if(state & UI_SELECT)
1974                 SWAP(short, wcol->shadetop, wcol->shadedown);
1975         
1976         widget_init(&wtb);
1977         
1978         /* fully rounded */
1979         round_box_edges(&wtb, roundboxalign, rect, rad);
1980         
1981         /* decoration */
1982         if(!(state & UI_TEXTINPUT)) {
1983                 widget_num_tria(&wtb.tria1, rect, 0.6f, 'l');
1984                 widget_num_tria(&wtb.tria2, rect, 0.6f, 'r');
1985         }
1986
1987         widgetbase_draw(&wtb, wcol);
1988         
1989         /* text space */
1990         rect->xmin += textofs;
1991         rect->xmax -= textofs;
1992 }
1993
1994 //static int ui_link_bezier_points(rcti *rect, float coord_array[][2], int resol)
1995 int ui_link_bezier_points(rcti *rect, float coord_array[][2], int resol)
1996 {
1997         float dist, vec[4][2];
1998
1999         vec[0][0]= rect->xmin;
2000         vec[0][1]= rect->ymin;
2001         vec[3][0]= rect->xmax;
2002         vec[3][1]= rect->ymax;
2003         
2004         dist= 0.5f*ABS(vec[0][0] - vec[3][0]);
2005         
2006         vec[1][0]= vec[0][0]+dist;
2007         vec[1][1]= vec[0][1];
2008         
2009         vec[2][0]= vec[3][0]-dist;
2010         vec[2][1]= vec[3][1];
2011         
2012         forward_diff_bezier(vec[0][0], vec[1][0], vec[2][0], vec[3][0], coord_array[0], resol, sizeof(float)*2);
2013         forward_diff_bezier(vec[0][1], vec[1][1], vec[2][1], vec[3][1], coord_array[0]+1, resol, sizeof(float)*2);
2014         
2015         return 1;
2016 }
2017
2018 #define LINK_RESOL      24
2019 void ui_draw_link_bezier(rcti *rect)
2020 {
2021         float coord_array[LINK_RESOL+1][2];
2022         
2023         if(ui_link_bezier_points(rect, coord_array, LINK_RESOL)) {
2024                 float dist;
2025                 int i;
2026                 
2027                 /* we can reuse the dist variable here to increment the GL curve eval amount*/
2028                 dist = 1.0f/(float)LINK_RESOL;
2029                 
2030                 glEnable(GL_BLEND);
2031                 glEnable(GL_LINE_SMOOTH);
2032                 
2033                 glBegin(GL_LINE_STRIP);
2034                 for(i=0; i<=LINK_RESOL; i++) {
2035                         glVertex2fv(coord_array[i]);
2036                 }
2037                 glEnd();
2038                 
2039                 glDisable(GL_BLEND);
2040                 glDisable(GL_LINE_SMOOTH);
2041
2042         }
2043 }
2044
2045 /* function in use for buttons and for view2d sliders */
2046 void uiWidgetScrollDraw(uiWidgetColors *wcol, rcti *rect, rcti *slider, int state)
2047 {
2048         uiWidgetBase wtb;
2049         float rad;
2050         int horizontal;
2051         short outline=0;
2052
2053         widget_init(&wtb);
2054
2055         /* determine horizontal/vertical */
2056         horizontal= (rect->xmax - rect->xmin > rect->ymax - rect->ymin);
2057         
2058         if(horizontal)
2059                 rad= 0.5f*(rect->ymax - rect->ymin);
2060         else
2061                 rad= 0.5f*(rect->xmax - rect->xmin);
2062         
2063         wtb.shadedir= (horizontal)? 1: 0;
2064         
2065         /* draw back part, colors swapped and shading inverted */
2066         if(horizontal)
2067                 SWAP(short, wcol->shadetop, wcol->shadedown);
2068         
2069         round_box_edges(&wtb, 15, rect, rad); 
2070         widgetbase_draw(&wtb, wcol);
2071         
2072         /* slider */
2073         if(slider->xmax-slider->xmin<2 || slider->ymax-slider->ymin<2);
2074         else {
2075                 
2076                 SWAP(short, wcol->shadetop, wcol->shadedown);
2077                 
2078                 QUATCOPY(wcol->inner, wcol->item);
2079                 
2080                 if(wcol->shadetop>wcol->shadedown)
2081                         wcol->shadetop+= 20;    /* XXX violates themes... */
2082                 else wcol->shadedown+= 20;
2083                 
2084                 if(state & UI_SCROLL_PRESSED) {
2085                         wcol->inner[0]= wcol->inner[0]>=250? 255 : wcol->inner[0]+5;
2086                         wcol->inner[1]= wcol->inner[1]>=250? 255 : wcol->inner[1]+5;
2087                         wcol->inner[2]= wcol->inner[2]>=250? 255 : wcol->inner[2]+5;
2088                 }
2089
2090                 /* draw */
2091                 wtb.emboss= 0; /* only emboss once */
2092                 
2093                 /* exception for progress bar */
2094                 if (state & UI_SCROLL_NO_OUTLINE)       
2095                         SWAP(short, outline, wtb.outline);
2096                 
2097                 round_box_edges(&wtb, 15, slider, rad); 
2098                 
2099                 if(state & UI_SCROLL_ARROWS) {
2100                         if(wcol->item[0] > 48) wcol->item[0]-= 48;
2101                         if(wcol->item[1] > 48) wcol->item[1]-= 48;
2102                         if(wcol->item[2] > 48) wcol->item[2]-= 48;
2103                         wcol->item[3]= 255;
2104                         
2105                         if(horizontal) {
2106                                 widget_scroll_circle(&wtb.tria1, slider, 0.6f, 'l');
2107                                 widget_scroll_circle(&wtb.tria2, slider, 0.6f, 'r');
2108                         }
2109                         else {
2110                                 widget_scroll_circle(&wtb.tria1, slider, 0.6f, 'b');
2111                                 widget_scroll_circle(&wtb.tria2, slider, 0.6f, 't');
2112                         }
2113                 }
2114                 widgetbase_draw(&wtb, wcol);
2115                 
2116                 if (state & UI_SCROLL_NO_OUTLINE)
2117                         SWAP(short, outline, wtb.outline);
2118         }       
2119 }
2120
2121 static void widget_scroll(uiBut *but, uiWidgetColors *wcol, rcti *rect, int state, int roundboxalign)
2122 {
2123         rcti rect1;
2124         double value;
2125         float fac, size, min;
2126         int horizontal;
2127
2128         /* calculate slider part */
2129         value= ui_get_but_val(but);
2130
2131         size= (but->softmax + but->a1 - but->softmin);
2132         size= MAX2(size, 2);
2133         
2134         /* position */
2135         rect1= *rect;
2136
2137         /* determine horizontal/vertical */
2138         horizontal= (rect->xmax - rect->xmin > rect->ymax - rect->ymin);
2139         
2140         if(horizontal) {
2141                 fac= (rect->xmax - rect->xmin)/(size);
2142                 rect1.xmin= rect1.xmin + ceil(fac*(value - but->softmin));
2143                 rect1.xmax= rect1.xmin + ceil(fac*(but->a1 - but->softmin));
2144
2145                 /* ensure minimium size */
2146                 min= rect->ymax - rect->ymin;
2147
2148                 if(rect1.xmax - rect1.xmin < min) {
2149                         rect1.xmax= rect1.xmin + min;
2150
2151                         if(rect1.xmax > rect->xmax) {
2152                                 rect1.xmax= rect->xmax;
2153                                 rect1.xmin= MAX2(rect1.xmax - min, rect->xmin);
2154                         }
2155                 }
2156         }
2157         else {
2158                 fac= (rect->ymax - rect->ymin)/(size);
2159                 rect1.ymax= rect1.ymax - ceil(fac*(value - but->softmin));
2160                 rect1.ymin= rect1.ymax - ceil(fac*(but->a1 - but->softmin));
2161
2162                 /* ensure minimium size */
2163                 min= rect->xmax - rect->xmin;
2164
2165                 if(rect1.ymax - rect1.ymin < min) {
2166                         rect1.ymax= rect1.ymin + min;
2167
2168                         if(rect1.ymax > rect->ymax) {
2169                                 rect1.ymax= rect->ymax;
2170                                 rect1.ymin= MAX2(rect1.ymax - min, rect->ymin);
2171                         }
2172                 }
2173         }
2174
2175         if(state & UI_SELECT)
2176                 state= UI_SCROLL_PRESSED;
2177         else
2178                 state= 0;
2179         uiWidgetScrollDraw(wcol, rect, &rect1, state);
2180 }
2181
2182 static void widget_progressbar(uiBut *but, uiWidgetColors *wcol, rcti *rect, int state, int roundboxalign)
2183 {
2184         rcti rect_prog = *rect, rect_bar = *rect;
2185         float value = but->a1;
2186         float w, min;
2187         
2188         /* make the progress bar a proportion of the original height */
2189         /* hardcoded 4px high for now */
2190         rect_prog.ymax = rect_prog.ymin + 4;
2191         rect_bar.ymax = rect_bar.ymin + 4;
2192         
2193         w = value * (rect_prog.xmax - rect_prog.xmin);
2194         
2195         /* ensure minimium size */
2196         min= rect_prog.ymax - rect_prog.ymin;
2197         w = MAX2(w, min);
2198         
2199         rect_bar.xmax = rect_bar.xmin + w;
2200                 
2201         uiWidgetScrollDraw(wcol, &rect_prog, &rect_bar, UI_SCROLL_NO_OUTLINE);
2202         
2203         /* raise text a bit */
2204         rect->ymin += 6;
2205         rect->xmin -= 6;
2206 }
2207
2208 static void widget_link(uiBut *but, uiWidgetColors *wcol, rcti *rect, int state, int roundboxalign)
2209 {
2210         
2211         if(but->flag & UI_SELECT) {
2212                 rcti rectlink;
2213                 
2214                 UI_ThemeColor(TH_TEXT_HI);
2215                 
2216                 rectlink.xmin= (rect->xmin+rect->xmax)/2;
2217                 rectlink.ymin= (rect->ymin+rect->ymax)/2;
2218                 rectlink.xmax= but->linkto[0];
2219                 rectlink.ymax= but->linkto[1];
2220                 
2221                 ui_draw_link_bezier(&rectlink);
2222         }
2223 }
2224
2225 static void widget_numslider(uiBut *but, uiWidgetColors *wcol, rcti *rect, int state, int roundboxalign)
2226 {
2227         uiWidgetBase wtb, wtb1;
2228         rcti rect1;
2229         double value;
2230         float offs, toffs, fac;
2231         char outline[3];
2232         
2233         widget_init(&wtb);
2234         widget_init(&wtb1);
2235         
2236         /* backdrop first */
2237         
2238         /* fully rounded */
2239         offs= 0.5f*(rect->ymax - rect->ymin);
2240         toffs = offs*0.75f;
2241         round_box_edges(&wtb, roundboxalign, rect, offs);
2242
2243         wtb.outline= 0;
2244         widgetbase_draw(&wtb, wcol);
2245         
2246         /* slider part */
2247         VECCOPY(outline, wcol->outline);
2248         VECCOPY(wcol->outline, wcol->item);
2249         VECCOPY(wcol->inner, wcol->item);
2250
2251         if(!(state & UI_SELECT))
2252                 SWAP(short, wcol->shadetop, wcol->shadedown);
2253         
2254         rect1= *rect;
2255         
2256         value= ui_get_but_val(but);
2257         fac= (value-but->softmin)*(rect1.xmax - rect1.xmin - offs)/(but->softmax - but->softmin);
2258         
2259         /* left part of slider, always rounded */
2260         rect1.xmax= rect1.xmin + ceil(offs+1.0f);
2261         round_box_edges(&wtb1, roundboxalign & ~6, &rect1, offs);
2262         wtb1.outline= 0;
2263         widgetbase_draw(&wtb1, wcol);
2264         
2265         /* right part of slider, interpolate roundness */
2266         rect1.xmax= rect1.xmin + fac + offs;
2267         rect1.xmin+=  floor(offs-1.0f);
2268         if(rect1.xmax + offs > rect->xmax)
2269                 offs*= (rect1.xmax + offs - rect->xmax)/offs;
2270         else 
2271                 offs= 0.0f;
2272         round_box_edges(&wtb1, roundboxalign & ~9, &rect1, offs);
2273         
2274         widgetbase_draw(&wtb1, wcol);
2275         VECCOPY(wcol->outline, outline);
2276         
2277         if(!(state & UI_SELECT))
2278                 SWAP(short, wcol->shadetop, wcol->shadedown);
2279         
2280         /* outline */
2281         wtb.outline= 1;
2282         wtb.inner= 0;
2283         widgetbase_draw(&wtb, wcol);
2284         
2285         /* text space */
2286         rect->xmin += toffs;
2287         rect->xmax -= toffs;
2288 }
2289
2290 /* I think 3 is sufficient border to indicate keyed status */
2291 #define SWATCH_KEYED_BORDER 3
2292
2293 static void widget_swatch(uiBut *but, uiWidgetColors *wcol, rcti *rect, int state, int roundboxalign)
2294 {
2295         uiWidgetBase wtb;
2296         float col[4];
2297         int color_profile = but->block->color_profile;
2298         
2299         col[3]= 1.0f;
2300
2301         if (but->rnaprop) {
2302                 if (RNA_property_subtype(but->rnaprop) == PROP_COLOR_GAMMA)
2303                         color_profile = BLI_PR_NONE;
2304
2305                 if(RNA_property_array_length(&but->rnapoin, but->rnaprop)==4) {
2306                         col[3]= RNA_property_float_get_index(&but->rnapoin, but->rnaprop, 3);
2307                 }
2308         }
2309         
2310         widget_init(&wtb);
2311         
2312         /* half rounded */
2313         round_box_edges(&wtb, roundboxalign, rect, 5.0f);
2314                 
2315         ui_get_but_vectorf(but, col);
2316         
2317         if(state & (UI_BUT_ANIMATED|UI_BUT_ANIMATED_KEY|UI_BUT_DRIVEN|UI_BUT_REDALERT)) {
2318                 // draw based on state - colour for keyed etc
2319                 widgetbase_draw(&wtb, wcol);
2320                 
2321                 // inset to draw swatch colour
2322                 rect->xmin+= SWATCH_KEYED_BORDER;
2323                 rect->xmax-= SWATCH_KEYED_BORDER;
2324                 rect->ymin+= SWATCH_KEYED_BORDER;
2325                 rect->ymax-= SWATCH_KEYED_BORDER;
2326                 
2327                 round_box_edges(&wtb, roundboxalign, rect, 5.0f);
2328         }
2329         
2330         if (color_profile)
2331                 linearrgb_to_srgb_v3_v3(col, col);
2332         
2333         wcol->inner[0]= FTOCHAR(col[0]);
2334         wcol->inner[1]= FTOCHAR(col[1]);
2335         wcol->inner[2]= FTOCHAR(col[2]);
2336         wcol->inner[3]= FTOCHAR(col[3]);
2337         wcol->shaded = 0;
2338         wcol->alpha_check = (wcol->inner[3] < 255);
2339
2340         widgetbase_draw(&wtb, wcol);
2341         
2342 }
2343
2344
2345 static void widget_textbut(uiWidgetColors *wcol, rcti *rect, int state, int roundboxalign)
2346 {
2347         uiWidgetBase wtb;
2348         
2349         if(state & UI_SELECT)
2350                 SWAP(short, wcol->shadetop, wcol->shadedown);
2351         
2352         widget_init(&wtb);
2353         
2354         /* half rounded */
2355         round_box_edges(&wtb, roundboxalign, rect, 4.0f);
2356         
2357         widgetbase_draw(&wtb, wcol);
2358
2359 }
2360
2361
2362 static void widget_menubut(uiWidgetColors *wcol, rcti *rect, int state, int roundboxalign)
2363 {
2364         uiWidgetBase wtb;
2365         
2366         widget_init(&wtb);
2367         
2368         /* half rounded */
2369         round_box_edges(&wtb, roundboxalign, rect, 4.0f);
2370         
2371         /* decoration */
2372         widget_menu_trias(&wtb.tria1, rect);
2373         
2374         widgetbase_draw(&wtb, wcol);
2375         
2376         /* text space */
2377         rect->xmax -= (rect->ymax-rect->ymin);
2378 }
2379
2380 static void widget_menuiconbut(uiWidgetColors *wcol, rcti *rect, int state, int roundboxalign)
2381 {
2382         uiWidgetBase wtb;
2383         
2384         widget_init(&wtb);
2385         
2386         /* half rounded */
2387         round_box_edges(&wtb, roundboxalign, rect, 4.0f);
2388         
2389         /* decoration */
2390         widgetbase_draw(&wtb, wcol);
2391 }
2392
2393 static void widget_pulldownbut(uiWidgetColors *wcol, rcti *rect, int state, int roundboxalign)
2394 {
2395         if(state & UI_ACTIVE) {
2396                 uiWidgetBase wtb;
2397                 float rad= 0.5f*(rect->ymax - rect->ymin); // 4.0f
2398                 
2399                 widget_init(&wtb);
2400                 
2401                 /* half rounded */
2402                 round_box_edges(&wtb, 15, rect, rad);
2403                 
2404                 widgetbase_draw(&wtb, wcol);
2405         }
2406 }
2407
2408 static void widget_menu_itembut(uiWidgetColors *wcol, rcti *rect, int state, int roundboxalign)
2409 {
2410         uiWidgetBase wtb;
2411         
2412         widget_init(&wtb);
2413         
2414         /* not rounded, no outline */
2415         wtb.outline= 0;
2416         round_box_edges(&wtb, 0, rect, 0.0f);
2417         
2418         widgetbase_draw(&wtb, wcol);
2419 }
2420
2421 static void widget_list_itembut(uiWidgetColors *wcol, rcti *rect, int state, int roundboxalign)
2422 {
2423         uiWidgetBase wtb;
2424         
2425         widget_init(&wtb);
2426         
2427         /* rounded, but no outline */
2428         wtb.outline= 0;
2429         round_box_edges(&wtb, 15, rect, 4.0f);
2430         
2431         widgetbase_draw(&wtb, wcol);
2432 }
2433
2434 static void widget_optionbut(uiWidgetColors *wcol, rcti *rect, int state, int roundboxalign)
2435 {
2436         uiWidgetBase wtb;
2437         rcti recttemp= *rect;
2438         int delta;
2439         
2440         widget_init(&wtb);
2441         
2442         /* square */
2443         recttemp.xmax= recttemp.xmin + (recttemp.ymax-recttemp.ymin);
2444         
2445         /* smaller */
2446         delta= 1 + (recttemp.ymax-recttemp.ymin)/8;
2447         recttemp.xmin+= delta;
2448         recttemp.ymin+= delta;
2449         recttemp.xmax-= delta;
2450         recttemp.ymax-= delta;
2451         
2452         /* half rounded */
2453         round_box_edges(&wtb, 15, &recttemp, 4.0f);
2454         
2455         /* decoration */
2456         if(state & UI_SELECT) {
2457                 widget_check_trias(&wtb.tria1, &recttemp);
2458         }
2459         
2460         widgetbase_draw(&wtb, wcol);
2461         
2462         /* text space */
2463         rect->xmin += (rect->ymax-rect->ymin)*0.7 + delta;
2464 }
2465
2466
2467 static void widget_radiobut(uiWidgetColors *wcol, rcti *rect, int state, int roundboxalign)
2468 {
2469         uiWidgetBase wtb;
2470         
2471         widget_init(&wtb);
2472         
2473         /* half rounded */
2474         round_box_edges(&wtb, roundboxalign, rect, 4.0f);
2475         
2476         widgetbase_draw(&wtb, wcol);
2477
2478 }
2479
2480 static void widget_box(uiBut *but, uiWidgetColors *wcol, rcti *rect, int state, int roundboxalign)
2481 {
2482         uiWidgetBase wtb;
2483         char old_col[3];
2484         
2485         widget_init(&wtb);
2486         
2487         VECCOPY(old_col, wcol->inner);
2488         
2489         /* abuse but->hsv - if it's non-zero, use this colour as the box's background */
2490         if (but->col[3]) {
2491                 wcol->inner[0] = but->col[0];
2492                 wcol->inner[1] = but->col[1];
2493                 wcol->inner[2] = but->col[2];
2494         }
2495         
2496         /* half rounded */
2497         round_box_edges(&wtb, roundboxalign, rect, 4.0f);
2498         
2499         widgetbase_draw(&wtb, wcol);
2500         
2501         /* store the box bg as gl clearcolor, to retrieve later when drawing semi-transparent rects
2502          * over the top to indicate disabled buttons */
2503         glClearColor(wcol->inner[0]/255.0, wcol->inner[1]/255.0, wcol->inner[2]/255.0, 1.0);
2504         
2505         VECCOPY(wcol->inner, old_col);
2506 }
2507
2508 static void widget_but(uiWidgetColors *wcol, rcti *rect, int state, int roundboxalign)
2509 {
2510         uiWidgetBase wtb;
2511         
2512         widget_init(&wtb);
2513         
2514         /* half rounded */
2515         round_box_edges(&wtb, roundboxalign, rect, 4.0f);
2516                 
2517         widgetbase_draw(&wtb, wcol);
2518
2519 }
2520
2521 static void widget_roundbut(uiWidgetColors *wcol, rcti *rect, int state, int roundboxalign)
2522 {
2523         uiWidgetBase wtb;
2524         float rad= 5.0f; //0.5f*(rect->ymax - rect->ymin);
2525         
2526         widget_init(&wtb);
2527         
2528         /* half rounded */
2529         round_box_edges(&wtb, roundboxalign, rect, rad);
2530
2531         widgetbase_draw(&wtb, wcol);
2532 }
2533
2534 static void widget_draw_extra_mask(const bContext *C, uiBut *but, uiWidgetType *wt, rcti *rect)
2535 {
2536         uiWidgetBase wtb;
2537         char col[4];
2538         
2539         /* state copy! */
2540         wt->wcol= *(wt->wcol_theme);
2541         
2542         widget_init(&wtb);
2543         
2544         if(but->block->drawextra) {
2545                 /* note: drawextra can change rect +1 or -1, to match round errors of existing previews */
2546                 but->block->drawextra(C, but->poin, but->block->drawextra_arg1, but->block->drawextra_arg2, rect);
2547                 
2548                 /* make mask to draw over image */
2549                 UI_GetThemeColor3ubv(TH_BACK, col);
2550                 glColor3ubv((unsigned char*)col);
2551                 
2552                 round_box__edges(&wtb, 15, rect, 0.0f, 4.0);
2553                 widgetbase_outline(&wtb);
2554         }
2555         
2556         /* outline */
2557         round_box_edges(&wtb, 15, rect, 5.0f);
2558         wtb.outline= 1;
2559         wtb.inner= 0;
2560         widgetbase_draw(&wtb, &wt->wcol);
2561         
2562 }
2563
2564
2565 static void widget_disabled(rcti *rect)
2566 {
2567         float col[4];
2568         
2569         glEnable(GL_BLEND);
2570         
2571         /* can't use theme TH_BACK or TH_PANEL... undefined */
2572         glGetFloatv(GL_COLOR_CLEAR_VALUE, col);
2573         glColor4f(col[0], col[1], col[2], 0.5f);
2574
2575         /* need -1 and +1 to make it work right for aligned buttons,
2576          * but problem may be somewhere else? */
2577         glRectf(rect->xmin-1, rect->ymin-1, rect->xmax, rect->ymax+1);
2578         
2579         glDisable(GL_BLEND);
2580 }
2581
2582 static uiWidgetType *widget_type(uiWidgetTypeEnum type)
2583 {
2584         bTheme *btheme= U.themes.first;
2585         static uiWidgetType wt;
2586         
2587         /* defaults */
2588         wt.wcol_theme= &btheme->tui.wcol_regular;
2589         wt.wcol_state= &btheme->tui.wcol_state;
2590         wt.state= widget_state;
2591         wt.draw= widget_but;
2592         wt.custom= NULL;
2593         wt.text= widget_draw_text_icon;
2594         
2595         switch(type) {
2596                 case UI_WTYPE_REGULAR:
2597                         break;
2598
2599                 case UI_WTYPE_LABEL:
2600                         wt.draw= NULL;
2601                         wt.state= widget_state_label;
2602                         break;
2603                         
2604                 case UI_WTYPE_TOGGLE:
2605                         wt.wcol_theme= &btheme->tui.wcol_toggle;
2606                         break;
2607                         
2608                 case UI_WTYPE_OPTION:
2609                         wt.wcol_theme= &btheme->tui.wcol_option;
2610                         wt.draw= widget_optionbut;
2611                         break;
2612                         
2613                 case UI_WTYPE_RADIO:
2614                         wt.wcol_theme= &btheme->tui.wcol_radio;
2615                         wt.draw= widget_radiobut;
2616                         break;
2617
2618                 case UI_WTYPE_NUMBER:
2619                         wt.wcol_theme= &btheme->tui.wcol_num;
2620                         wt.draw= widget_numbut;
2621                         break;
2622                         
2623                 case UI_WTYPE_SLIDER:
2624                         wt.wcol_theme= &btheme->tui.wcol_numslider;
2625                         wt.custom= widget_numslider;
2626                         wt.state= widget_state_numslider;
2627                         break;
2628                         
2629                 case UI_WTYPE_EXEC:
2630                         wt.wcol_theme= &btheme->tui.wcol_tool;
2631                         wt.draw= widget_roundbut;
2632                         break;
2633                         
2634                         
2635                         /* strings */
2636                 case UI_WTYPE_NAME:
2637                         wt.wcol_theme= &btheme->tui.wcol_text;
2638                         wt.draw= widget_textbut;
2639                         break;
2640                         
2641                 case UI_WTYPE_NAME_LINK:
2642                         break;
2643                         
2644                 case UI_WTYPE_POINTER_LINK:
2645                         break;
2646                         
2647                 case UI_WTYPE_FILENAME:
2648                         break;
2649                         
2650                         
2651                         /* start menus */
2652                 case UI_WTYPE_MENU_RADIO:
2653                         wt.wcol_theme= &btheme->tui.wcol_menu;
2654                         wt.draw= widget_menubut;
2655                         break;
2656
2657                 case UI_WTYPE_MENU_ICON_RADIO:
2658                         wt.wcol_theme= &btheme->tui.wcol_menu;
2659                         wt.draw= widget_menuiconbut;
2660                         break;
2661                         
2662                 case UI_WTYPE_MENU_POINTER_LINK:
2663                         wt.wcol_theme= &btheme->tui.wcol_menu;
2664                         wt.draw= widget_menubut;
2665                         break;
2666                         
2667                 case UI_WTYPE_PULLDOWN:
2668                         wt.wcol_theme= &btheme->tui.wcol_pulldown;
2669                         wt.draw= widget_pulldownbut;
2670                         wt.state= widget_state_pulldown;
2671                         break;
2672                         
2673                         /* in menus */
2674                 case UI_WTYPE_MENU_ITEM:
2675                         wt.wcol_theme= &btheme->tui.wcol_menu_item;
2676                         wt.draw= widget_menu_itembut;
2677                         wt.state= widget_state_menu_item;
2678                         break;
2679                         
2680                 case UI_WTYPE_MENU_BACK:
2681                         wt.wcol_theme= &btheme->tui.wcol_menu_back;
2682                         wt.draw= widget_menu_back;
2683                         break;
2684                         
2685                         /* specials */
2686                 case UI_WTYPE_ICON:
2687                         wt.draw= NULL;
2688                         break;
2689                         
2690                 case UI_WTYPE_SWATCH:
2691                         wt.custom= widget_swatch;
2692                         break;
2693                         
2694                 case UI_WTYPE_BOX:
2695                         wt.custom= widget_box;
2696                         wt.wcol_theme= &btheme->tui.wcol_box;
2697                         break;
2698                         
2699                 case UI_WTYPE_RGB_PICKER:
2700                         break;
2701                         
2702                 case UI_WTYPE_NORMAL:
2703                         break;
2704
2705                 case UI_WTYPE_SCROLL:
2706                         wt.wcol_theme= &btheme->tui.wcol_scroll;
2707                         wt.state= widget_state_nothing;
2708                         wt.custom= widget_scroll;
2709                         break;
2710
2711                 case UI_WTYPE_LISTITEM:
2712                         wt.wcol_theme= &btheme->tui.wcol_list_item;
2713                         wt.draw= widget_list_itembut;
2714                         break;
2715                         
2716                 case UI_WTYPE_PROGRESSBAR:
2717                         wt.wcol_theme= &btheme->tui.wcol_progress;
2718                         wt.custom= widget_progressbar;
2719                         break;
2720         }
2721         
2722         return &wt;
2723 }
2724
2725
2726 static int widget_roundbox_set(uiBut *but, rcti *rect)
2727 {
2728         /* alignment */
2729         if(but->flag & UI_BUT_ALIGN) {
2730                 
2731                 if(but->flag & UI_BUT_ALIGN_TOP)
2732                         rect->ymax+= 1;
2733                 if(but->flag & UI_BUT_ALIGN_LEFT)
2734                         rect->xmin-= 1;
2735                 
2736                 switch(but->flag & UI_BUT_ALIGN) {
2737                         case UI_BUT_ALIGN_TOP:
2738                                 return (12);
2739                                 break;
2740                         case UI_BUT_ALIGN_DOWN:
2741                                 return (3);
2742                                 break;
2743                         case UI_BUT_ALIGN_LEFT:
2744                                 return (6);
2745                                 break;
2746                         case UI_BUT_ALIGN_RIGHT:
2747                                 return (9);
2748                                 break;
2749                                 
2750                         case UI_BUT_ALIGN_DOWN|UI_BUT_ALIGN_RIGHT:
2751                                 return (1);
2752                                 break;
2753                         case UI_BUT_ALIGN_DOWN|UI_BUT_ALIGN_LEFT:
2754                                 return (2);
2755                                 break;
2756                         case UI_BUT_ALIGN_TOP|UI_BUT_ALIGN_RIGHT:
2757                                 return (8);
2758                                 break;
2759                         case UI_BUT_ALIGN_TOP|UI_BUT_ALIGN_LEFT:
2760                                 return (4);
2761                                 break;
2762                                 
2763                         default:
2764                                 return (0);
2765                                 break;
2766                 }
2767         } 
2768         return 15;
2769 }
2770
2771 /* conversion from old to new buttons, so still messy */
2772 void ui_draw_but(const bContext *C, ARegion *ar, uiStyle *style, uiBut *but, rcti *rect)
2773 {
2774         bTheme *btheme= U.themes.first;
2775         ThemeUI *tui= &btheme->tui;
2776         uiFontStyle *fstyle= &style->widget;
2777         uiWidgetType *wt= NULL;
2778         
2779         /* handle menus separately */
2780         if(but->dt==UI_EMBOSSP) {
2781                 switch (but->type) {
2782                         case LABEL:
2783                                 widget_draw_text_icon(&style->widgetlabel, &tui->wcol_menu_back, but, rect);
2784                                 break;
2785                         case SEPR:
2786                                 ui_draw_separator(but, rect, &tui->wcol_menu_item);
2787                                 break;
2788                                 
2789                         default:
2790                                 wt= widget_type(UI_WTYPE_MENU_ITEM);
2791                 }
2792         }
2793         else if(but->dt==UI_EMBOSSN) {
2794                 /* "nothing" */
2795                 wt= widget_type(UI_WTYPE_ICON);
2796         }
2797         else {
2798                 
2799                 switch (but->type) {
2800                         case LABEL:
2801                                 if(but->block->flag & UI_BLOCK_LOOP)
2802                                         widget_draw_text_icon(&style->widgetlabel, &tui->wcol_menu_back, but, rect);
2803                                 else {
2804                                         wt= widget_type(UI_WTYPE_LABEL);
2805                                         fstyle= &style->widgetlabel;
2806                                 }
2807                                 break;
2808                                 
2809                         case SEPR:
2810                                 break;
2811                                 
2812                         case BUT:
2813                                 wt= widget_type(UI_WTYPE_EXEC);
2814                                 break;
2815
2816                         case NUM:
2817                                 wt= widget_type(UI_WTYPE_NUMBER);
2818                                 break;
2819                                 
2820                         case NUMSLI:
2821                         case HSVSLI:
2822                                 wt= widget_type(UI_WTYPE_SLIDER);
2823                                 break;
2824                                 
2825                         case ROW:
2826                                 wt= widget_type(UI_WTYPE_RADIO);
2827                                 break;
2828
2829                         case LISTROW:
2830                                 wt= widget_type(UI_WTYPE_LISTITEM);
2831                                 break;
2832                                 
2833                         case TEX:
2834                                 wt= widget_type(UI_WTYPE_NAME);
2835                                 break;
2836                                 
2837                         case SEARCH_MENU:
2838                                 wt= widget_type(UI_WTYPE_NAME);
2839                                 if(but->block->flag & UI_BLOCK_LOOP)
2840                                         wt->wcol_theme= &btheme->tui.wcol_menu_back;
2841                                 break;
2842                                 
2843                         case TOGBUT:
2844                         case TOG:
2845                         case TOGN:
2846                         case TOG3:
2847                                 wt= widget_type(UI_WTYPE_TOGGLE);
2848                                 break;
2849                                 
2850                         case OPTION:
2851                         case OPTIONN:
2852                                 if (!(but->flag & UI_HAS_ICON)) {
2853                                         wt= widget_type(UI_WTYPE_OPTION);
2854                                         but->flag |= UI_TEXT_LEFT;
2855                                 }
2856                                 else
2857                                         wt= widget_type(UI_WTYPE_TOGGLE);
2858                                 break;
2859                                 
2860                         case MENU:
2861                         case BLOCK:
2862                         case ICONTEXTROW:
2863                                 if(!but->str[0] && but->icon)
2864                                         wt= widget_type(UI_WTYPE_MENU_ICON_RADIO);
2865                                 else
2866                                         wt= widget_type(UI_WTYPE_MENU_RADIO);
2867                                 break;
2868                                 
2869                         case PULLDOWN:
2870                                 wt= widget_type(UI_WTYPE_PULLDOWN);
2871                                 break;
2872                         
2873                         case BUTM:
2874                                 wt= widget_type(UI_WTYPE_MENU_ITEM);
2875                                 break;
2876                                 
2877                         case COL:
2878                                 wt= widget_type(UI_WTYPE_SWATCH);
2879                                 break;
2880                                 
2881                         case ROUNDBOX:
2882                         case LISTBOX:
2883                                 wt= widget_type(UI_WTYPE_BOX);
2884                                 break;
2885                                 
2886                         case LINK:
2887                         case INLINK:
2888                                 wt= widget_type(UI_WTYPE_ICON);
2889                                 wt->custom= widget_link;
2890                                 
2891                                 break;
2892                         
2893                         case BUT_EXTRA:
2894                                 widget_draw_extra_mask(C, but, widget_type(UI_WTYPE_BOX), rect);
2895                                 break;
2896                                 
2897                         case HSVCUBE:
2898                                 if(but->a1==9) // vertical V slider, uses new widget draw now
2899                                         ui_draw_but_HSV_v(but, rect);
2900                                 else  // other HSV pickers...
2901                                         ui_draw_but_HSVCUBE(but, rect);
2902                                 break;
2903                                 
2904                         case HSVCIRCLE:
2905                                 ui_draw_but_HSVCIRCLE(but, &tui->wcol_regular, rect);
2906                                 break;
2907                                 
2908                         case BUT_COLORBAND:
2909                                 ui_draw_but_COLORBAND(but, &tui->wcol_regular, rect);
2910                                 break;
2911                                 
2912                         case BUT_NORMAL:
2913                                 ui_draw_but_NORMAL(but, &tui->wcol_regular, rect);
2914                                 break;
2915                                 
2916                         case BUT_IMAGE:
2917                                 ui_draw_but_IMAGE(ar, but, &tui->wcol_regular, rect);
2918                                 break;
2919                         
2920                         case HISTOGRAM:
2921                                 ui_draw_but_HISTOGRAM(ar, but, &tui->wcol_regular, rect);
2922                                 break;
2923                                 
2924                         case WAVEFORM:
2925                                 ui_draw_but_WAVEFORM(ar, but, &tui->wcol_regular, rect);
2926                                 break;
2927                                 
2928                         case VECTORSCOPE:
2929                                 ui_draw_but_VECTORSCOPE(ar, but, &tui->wcol_regular, rect);
2930                                 break;
2931                                         
2932                         case BUT_CURVE:
2933                                 ui_draw_but_CURVE(ar, but, &tui->wcol_regular, rect);
2934                                 break;
2935                                 
2936                         case PROGRESSBAR:
2937                                 wt= widget_type(UI_WTYPE_PROGRESSBAR);
2938                                 fstyle= &style->widgetlabel;
2939                                 break;
2940
2941                         case SCROLL:
2942                                 wt= widget_type(UI_WTYPE_SCROLL);
2943                                 break;
2944
2945                         default:
2946                                 wt= widget_type(UI_WTYPE_REGULAR);
2947                 }
2948         }
2949         
2950         if(wt) {
2951                 rcti disablerect= *rect; /* rect gets clipped smaller for text */
2952                 int roundboxalign, state;
2953                 
2954                 roundboxalign= widget_roundbox_set(but, rect);
2955
2956                 state= but->flag;
2957                 if(but->editstr) state |= UI_TEXTINPUT;
2958                 
2959                 wt->state(wt, state);
2960                 if(wt->custom)
2961                         wt->custom(but, &wt->wcol, rect, state, roundboxalign);
2962                 else if(wt->draw)
2963                         wt->draw(&wt->wcol, rect, state, roundboxalign);
2964                 wt->text(fstyle, &wt->wcol, but, rect);
2965                 
2966                 if(state & (UI_BUT_DISABLED|UI_BUT_INACTIVE))
2967                         if(but->dt!=UI_EMBOSSP)
2968                                 widget_disabled(&disablerect);
2969         }
2970 }
2971
2972 void ui_draw_menu_back(uiStyle *style, uiBlock *block, rcti *rect)
2973 {
2974         uiWidgetType *wt= widget_type(UI_WTYPE_MENU_BACK);
2975         
2976         wt->state(wt, 0);
2977         if(block)
2978                 wt->draw(&wt->wcol, rect, block->flag, block->direction);
2979         else
2980                 wt->draw(&wt->wcol, rect, 0, 0);
2981         
2982 }
2983
2984 void ui_draw_search_back(uiStyle *style, uiBlock *block, rcti *rect)
2985 {
2986         uiWidgetType *wt= widget_type(UI_WTYPE_BOX);
2987         
2988         glEnable(GL_BLEND);
2989         widget_softshadow(rect, 15, 5.0f, 8.0f);
2990         glDisable(GL_BLEND);
2991
2992         wt->state(wt, 0);
2993         if(block)
2994                 wt->draw(&wt->wcol, rect, block->flag, 15);
2995         else
2996                 wt->draw(&wt->wcol, rect, 0, 15);
2997         
2998 }
2999
3000
3001 /* helper call to draw a menu item without button */
3002 /* state: UI_ACTIVE or 0 */
3003 void ui_draw_menu_item(uiFontStyle *fstyle, rcti *rect, char *name, int iconid, int state)
3004 {
3005         uiWidgetType *wt= widget_type(UI_WTYPE_MENU_ITEM);
3006         rcti _rect= *rect;
3007         char *cpoin;
3008         
3009         wt->state(wt, state);
3010         wt->draw(&wt->wcol, rect, 0, 0);
3011         
3012         uiStyleFontSet(fstyle);
3013         fstyle->align= UI_STYLE_TEXT_LEFT;
3014         
3015         /* text location offset */
3016         rect->xmin+=5;
3017         if(iconid) rect->xmin+= ICON_HEIGHT;
3018
3019         /* cut string in 2 parts? */
3020         cpoin= strchr(name, '|');
3021         if(cpoin) {
3022                 *cpoin= 0;
3023                 rect->xmax -= BLF_width(fstyle->uifont_id, cpoin+1) + 10;
3024         }
3025         
3026         glColor3ubv((unsigned char*)wt->wcol.text);
3027         uiStyleFontDraw(fstyle, rect, name);
3028         
3029         /* part text right aligned */
3030         if(cpoin) {
3031                 fstyle->align= UI_STYLE_TEXT_RIGHT;
3032                 rect->xmax= _rect.xmax - 5;
3033                 uiStyleFontDraw(fstyle, rect, cpoin+1);
3034                 *cpoin= '|';
3035         }
3036         
3037         /* restore rect, was messed with */
3038         *rect= _rect;
3039
3040         if(iconid) {
3041                 int xs= rect->xmin+4;
3042                 int ys= 1 + (rect->ymin+rect->ymax- ICON_HEIGHT)/2;
3043                 glEnable(GL_BLEND);
3044                 UI_icon_draw_aspect(xs, ys, iconid, 1.2f, 0.5f); /* XXX scale weak get from fstyle? */
3045                 glDisable(GL_BLEND);
3046         }
3047 }
3048
3049 void ui_draw_preview_item(uiFontStyle *fstyle, rcti *rect, char *name, int iconid, int state)
3050 {
3051         rcti trect = *rect;
3052         
3053         uiWidgetType *wt= widget_type(UI_WTYPE_MENU_ITEM);
3054         
3055         wt->state(wt, state);
3056         wt->draw(&wt->wcol, rect, 0, 0);
3057         
3058         widget_draw_preview(iconid, 1.f, 1.f, rect);
3059         
3060         if (state == UI_ACTIVE)
3061                 glColor3ubv((unsigned char*)wt->wcol.text);