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