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