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