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