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