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