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