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