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