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