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