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