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