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