8f40b2e4bfd21ec52e5bacfae56ef62e61340aab
[blender-staging.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         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(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 ELEM3(but->type, TOG, ROW, TOGN) {
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+strlen(but->str);
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, pos, ch;
784                 short selsta_tmp, selend_tmp, selsta_draw, selwidth_draw;
785                 
786                 if ((but->selend - but->selsta) > 0) {
787                         /* XXX weak, why is this? (ton) */
788                         t= but->str[0]?1:-2;
789                         
790                         /* text button selection */
791                         selsta_tmp = but->selsta + strlen(but->str);
792                         selend_tmp = but->selend + strlen(but->str);
793                         
794                         if(but->drawstr[0]!=0) {
795                                 ch= but->drawstr[selsta_tmp];
796                                 but->drawstr[selsta_tmp]= 0;
797                                 
798                                 selsta_draw = BLF_width(but->drawstr+but->ofs) + t;
799                                 
800                                 but->drawstr[selsta_tmp]= ch;
801                                 
802                                 ch= but->drawstr[selend_tmp];
803                                 but->drawstr[selend_tmp]= 0;
804                                 
805                                 selwidth_draw = BLF_width(but->drawstr+but->ofs) + t;
806                                 
807                                 but->drawstr[selend_tmp]= ch;
808                                 
809                                 glColor3ubv(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+strlen(but->str);
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) + 1;
821                                         
822                                         but->drawstr[pos]= ch;
823                                 }
824                                 else t= 1;
825                                 
826                                 glColor3ub(255,0,0);
827                                 glRects(rect->xmin+t, rect->ymin+2, rect->xmin+t+2, rect->ymax-2);
828                         }
829                 }
830         }
831         //      ui_rasterpos_safe(x, y, but->aspect);
832 //      if(but->type==IDPOIN) transopts= 0;     // no translation, of course!
833 //      else transopts= ui_translate_buttons();
834         
835         /* cut string in 2 parts - only for menu entries */
836         if(ELEM5(but->type, SLI, NUM, TEX, NUMSLI, NUMABS)==0) {
837                 cpoin= strchr(but->drawstr, '|');
838                 if(cpoin) *cpoin= 0;            
839         }
840         
841         glColor3ubv(wcol->text);
842         uiStyleFontDraw(fstyle, rect, but->drawstr+but->ofs);
843
844         /* part text right aligned */
845         if(cpoin) {
846                 fstyle->align= UI_STYLE_TEXT_RIGHT;
847                 rect->xmax-=5;
848                 uiStyleFontDraw(fstyle, rect, cpoin+1);
849                 *cpoin= '|';
850         }
851 }
852
853 /* draws text and icons for buttons */
854 static void widget_draw_text_icon(uiFontStyle *fstyle, uiWidgetColors *wcol, uiBut *but, rcti *rect)
855 {
856         
857         if(but==NULL) return;
858         
859         /* cutting off from left part */
860         if ELEM3(but->type, NUM, NUMABS, TEX) { 
861                 ui_text_leftclip(fstyle, but, rect);
862         }
863         else but->ofs= 0;
864         
865         /* check for button text label */
866         if (but->type == ICONTEXTROW) {
867                 widget_draw_icon(but, (BIFIconID) (but->icon+but->iconadd), 0, rect);
868         }
869         else {
870                                 
871                 if(but->type==BUT_TOGDUAL) {
872                         int dualset= 0;
873                         if(but->pointype==SHO)
874                                 dualset= BTST( *(((short *)but->poin)+1), but->bitnr);
875                         else if(but->pointype==INT)
876                                 dualset= BTST( *(((int *)but->poin)+1), but->bitnr);
877                         
878                         widget_draw_icon(but, ICON_DOT, dualset?0:-100, rect);
879                 }
880                 
881                 /* If there's an icon too (made with uiDefIconTextBut) then draw the icon
882                 and offset the text label to accomodate it */
883                 
884                 if (but->flag & UI_HAS_ICON) {
885                         widget_draw_icon(but, but->icon+but->iconadd, 0, rect);
886                         
887                         rect->xmin += UI_icon_get_width(but->icon+but->iconadd);
888                         
889                         if(but->editstr || (but->flag & UI_TEXT_LEFT)) 
890                                 rect->xmin += 5;
891                 }
892                 else if(but->flag & UI_TEXT_LEFT)
893                         rect->xmin += 5;
894                 
895                 /* always draw text for textbutton cursor */
896                 widget_draw_text(fstyle, wcol, but, rect);
897
898         }
899 }
900
901
902
903 /* *********************** widget types ************************************* */
904
905
906 /*   uiWidgetStateColors
907  char inner_anim[4];
908  char inner_anim_sel[4];
909  char inner_key[4];
910  char inner_key_sel[4];
911  char inner_driven[4];
912  char inner_driven_sel[4];
913  float blend;
914  
915 */
916
917 static struct uiWidgetStateColors wcol_state= {
918         {115, 190, 76, 255},
919         {90, 166, 51, 255},
920         {240, 235, 100, 255},
921         {215, 211, 75, 255},
922         {180, 0, 255, 255},
923         {153, 0, 230, 255},
924         0.5f, 0.0f
925 };
926
927 /*  uiWidgetColors
928  float outline[3];
929  float inner[4];
930  float inner_sel[4];
931  float item[3];
932  float text[3];
933  float text_sel[3];
934
935  short shaded;
936  float shadetop, shadedown;
937 */      
938
939 static struct uiWidgetColors wcol_num= {
940         {25, 25, 25, 255},
941         {180, 180, 180, 255},
942         {153, 153, 153, 255},
943         {90, 90, 90, 255},
944         
945         {0, 0, 0, 255},
946         {255, 255, 255, 255},
947         
948         1,
949         -20, 0
950 };
951
952 static struct uiWidgetColors wcol_numslider= {
953         {25, 25, 25, 255},
954         {180, 180, 180, 255},
955         {153, 153, 153, 255},
956         {128, 128, 128, 255},
957         
958         {0, 0, 0, 255},
959         {255, 255, 255, 255},
960         
961         1,
962         -20, 0
963 };
964
965 static struct uiWidgetColors wcol_text= {
966         {25, 25, 25, 255},
967         {153, 153, 153, 255},
968         {153, 153, 153, 255},
969         {90, 90, 90, 255},
970         
971         {0, 0, 0, 255},
972         {255, 255, 255, 255},
973         
974         1,
975         0, 25
976 };
977
978 static struct uiWidgetColors wcol_option= {
979         {0, 0, 0, 255},
980         {70, 70, 70, 255},
981         {70, 70, 70, 255},
982         {255, 255, 255, 255},
983         
984         {0, 0, 0, 255},
985         {255, 255, 255, 255},
986         
987         1,
988         15, -15
989 };
990
991 /* button that shows popup */
992 static struct uiWidgetColors wcol_menu= {
993         {0, 0, 0, 255},
994         {70, 70, 70, 255},
995         {70, 70, 70, 255},
996         {255, 255, 255, 255},
997         
998         {255, 255, 255, 255},
999         {204, 204, 204, 255},
1000         
1001         1,
1002         15, -15
1003 };
1004
1005 /* button that starts pulldown */
1006 static struct uiWidgetColors wcol_pulldown= {
1007         {0, 0, 0, 255},
1008         {63, 63, 63, 255},
1009         {86, 128, 194, 255},
1010         {255, 255, 255, 255},
1011         
1012         {0, 0, 0, 255},
1013         {0, 0, 0, 255},
1014         
1015         0,
1016         25, -20
1017 };
1018
1019 /* button inside menu */
1020 static struct uiWidgetColors wcol_menu_item= {
1021         {0, 0, 0, 255},
1022         {0, 0, 0, 0},
1023         {86, 128, 194, 255},
1024         {255, 255, 255, 255},
1025         
1026         {255, 255, 255, 255},
1027         {0, 0, 0, 255},
1028         
1029         0,
1030         38, 0
1031 };
1032
1033 /* backdrop menu + title text color */
1034 static struct uiWidgetColors wcol_menu_back= {
1035         {0, 0, 0, 255},
1036         {25, 25, 25, 230},
1037         {45, 45, 45, 230},
1038         {100, 100, 100, 255},
1039         
1040         {255, 255, 255, 255},
1041         {255, 255, 255, 255},
1042         
1043         0,
1044         25, -20
1045 };
1046
1047
1048 static struct uiWidgetColors wcol_radio= {
1049         {0, 0, 0, 255},
1050         {70, 70, 70, 255},
1051         {86, 128, 194, 255},
1052         {255, 255, 255, 255},
1053         
1054         {255, 255, 255, 255},
1055         {0, 0, 0, 255},
1056         
1057         1,
1058         15, -15
1059 };
1060
1061 static struct uiWidgetColors wcol_regular= {
1062         {25, 25, 25, 255},
1063         {153, 153, 153, 255},
1064         {100, 100, 100, 255},
1065         {25, 25, 25, 255},
1066         
1067         {0, 0, 0, 255},
1068         {255, 255, 255, 255},
1069         
1070         0,
1071         0, 0
1072 };
1073
1074 static struct uiWidgetColors wcol_tool= {
1075         {25, 25, 25, 255},
1076         {153, 153, 153, 255},
1077         {100, 100, 100, 255},
1078         {25, 25, 25, 255},
1079         
1080         {0, 0, 0, 255},
1081         {255, 255, 255, 255},
1082         
1083         1,
1084         25, -25
1085 };
1086
1087 static struct uiWidgetColors wcol_box= {
1088         {25, 25, 25, 255},
1089         {128, 128, 128, 255},
1090         {100, 100, 100, 255},
1091         {25, 25, 25, 255},
1092         
1093         {0, 0, 0, 255},
1094         {255, 255, 255, 255},
1095         
1096         0,
1097         0, 0
1098 };
1099
1100 static struct uiWidgetColors wcol_toggle= {
1101         {25, 25, 25, 255},
1102         {153, 153, 153, 255},
1103         {100, 100, 100, 255},
1104         {25, 25, 25, 255},
1105         
1106         {0, 0, 0, 255},
1107         {255, 255, 255, 255},
1108         
1109         0,
1110         0, 0
1111 };
1112
1113 static struct uiWidgetColors wcol_scroll= {
1114         {50, 50, 50, 180},
1115         {80, 80, 80, 180},
1116         {100, 100, 100, 180},
1117         {180, 180, 180, 255},
1118         
1119         {0, 0, 0, 255},
1120         {255, 255, 255, 255},
1121         
1122         1,
1123         10, -20
1124 };
1125
1126 /* free wcol struct to play with */
1127 static struct uiWidgetColors wcol_tmp= {
1128         {0, 0, 0, 255},
1129         {128, 128, 128, 255},
1130         {100, 100, 100, 255},
1131         {25, 25, 25, 255},
1132         
1133         {0, 0, 0, 255},
1134         {255, 255, 255, 255},
1135         
1136         0,
1137         0, 0
1138 };
1139
1140
1141 /* called for theme init (new theme) and versions */
1142 void ui_widget_color_init(ThemeUI *tui)
1143 {
1144         tui->wcol_regular= wcol_regular;
1145         tui->wcol_tool= wcol_tool;
1146         tui->wcol_text= wcol_text;
1147         tui->wcol_radio= wcol_radio;
1148         tui->wcol_option= wcol_option;
1149         tui->wcol_toggle= wcol_toggle;
1150         tui->wcol_num= wcol_num;
1151         tui->wcol_numslider= wcol_numslider;
1152         tui->wcol_menu= wcol_menu;
1153         tui->wcol_pulldown= wcol_pulldown;
1154         tui->wcol_menu_back= wcol_menu_back;
1155         tui->wcol_menu_item= wcol_menu_item;
1156         tui->wcol_box= wcol_box;
1157         tui->wcol_scroll= wcol_scroll;
1158
1159         tui->wcol_state= wcol_state;
1160 }
1161
1162 /* ************ button callbacks, state ***************** */
1163
1164 static void widget_state_blend(char *cp, char *cpstate, float fac)
1165 {
1166         if(fac != 0.0f) {
1167                 cp[0]= (int)((1.0f-fac)*cp[0] + fac*cpstate[0]);
1168                 cp[1]= (int)((1.0f-fac)*cp[1] + fac*cpstate[1]);
1169                 cp[2]= (int)((1.0f-fac)*cp[2] + fac*cpstate[2]);
1170         }
1171 }
1172
1173 /* copy colors from theme, and set changes in it based on state */
1174 static void widget_state(uiWidgetType *wt, int state)
1175 {
1176         uiWidgetStateColors *wcol_state= wt->wcol_state;
1177
1178         wt->wcol= *(wt->wcol_theme);
1179         
1180         if(state & UI_SELECT) {
1181                 QUATCOPY(wt->wcol.inner, wt->wcol.inner_sel)
1182
1183                 if(state & UI_BUT_ANIMATED_KEY)
1184                         widget_state_blend(wt->wcol.inner, wcol_state->inner_key_sel, wcol_state->blend);
1185                 else if(state & UI_BUT_ANIMATED)
1186                         widget_state_blend(wt->wcol.inner, wcol_state->inner_anim_sel, wcol_state->blend);
1187                 else if(state & UI_BUT_DRIVEN)
1188                         widget_state_blend(wt->wcol.inner, wcol_state->inner_driven_sel, wcol_state->blend);
1189
1190                 VECCOPY(wt->wcol.text, wt->wcol.text_sel);
1191                 
1192                 /* only flip shade if it's not "pushed in" already */
1193                 if(wt->wcol.shaded && wt->wcol.shadetop>wt->wcol.shadedown) {
1194                         SWAP(short, wt->wcol.shadetop, wt->wcol.shadedown);
1195                 }
1196         }
1197         else {
1198                 if(state & UI_BUT_ANIMATED_KEY)
1199                         widget_state_blend(wt->wcol.inner, wcol_state->inner_key, wcol_state->blend);
1200                 else if(state & UI_BUT_ANIMATED)
1201                         widget_state_blend(wt->wcol.inner, wcol_state->inner_anim, wcol_state->blend);
1202                 else if(state & UI_BUT_DRIVEN)
1203                         widget_state_blend(wt->wcol.inner, wcol_state->inner_driven, wcol_state->blend);
1204
1205                 if(state & UI_ACTIVE) { /* mouse over? */
1206                         wt->wcol.inner[0]= wt->wcol.inner[0]>=240? 255 : wt->wcol.inner[0]+15;
1207                         wt->wcol.inner[1]= wt->wcol.inner[1]>=240? 255 : wt->wcol.inner[1]+15;
1208                         wt->wcol.inner[2]= wt->wcol.inner[2]>=240? 255 : wt->wcol.inner[2]+15;
1209                 }
1210         }
1211 }
1212
1213 /* labels use theme colors for text */
1214 static void widget_state_label(uiWidgetType *wt, int state)
1215 {
1216         /* call this for option button */
1217         widget_state(wt, state);
1218
1219         if(state & UI_SELECT)
1220                 UI_GetThemeColor4ubv(TH_TEXT_HI, wt->wcol.text);
1221         else
1222                 UI_GetThemeColor4ubv(TH_TEXT, wt->wcol.text);
1223         
1224 }
1225
1226 static void widget_state_nothing(uiWidgetType *wt, int state)
1227 {
1228         wt->wcol= *(wt->wcol_theme);
1229 }       
1230
1231 /* special case, button that calls pulldown */
1232 static void widget_state_pulldown(uiWidgetType *wt, int state)
1233 {
1234         wt->wcol= *(wt->wcol_theme);
1235         
1236         QUATCOPY(wt->wcol.inner, wt->wcol.inner_sel);
1237         VECCOPY(wt->wcol.outline, wt->wcol.inner);
1238
1239         if(state & UI_ACTIVE)
1240                 VECCOPY(wt->wcol.text, wt->wcol.text_sel);
1241 }
1242
1243 /* special case, menu items */
1244 static void widget_state_menu_item(uiWidgetType *wt, int state)
1245 {
1246         wt->wcol= *(wt->wcol_theme);
1247         
1248         if(state & (UI_BUT_DISABLED|UI_BUT_INACTIVE)) {
1249                 wt->wcol.text[0]= 0.5f*(wt->wcol.text[0]+wt->wcol.text_sel[0]);
1250                 wt->wcol.text[1]= 0.5f*(wt->wcol.text[1]+wt->wcol.text_sel[1]);
1251                 wt->wcol.text[2]= 0.5f*(wt->wcol.text[2]+wt->wcol.text_sel[2]);
1252         }
1253         else if(state & UI_ACTIVE) {
1254                 QUATCOPY(wt->wcol.inner, wt->wcol.inner_sel);
1255                 VECCOPY(wt->wcol.text, wt->wcol.text_sel);
1256                 
1257                 wt->wcol.shaded= 1;
1258         }
1259 }
1260
1261
1262 /* ************ menu backdrop ************************* */
1263
1264 /* outside of rect, rad to left/bottom/right */
1265 static void widget_softshadow(rcti *rect, int roundboxalign, float radin, float radout)
1266 {
1267         uiWidgetBase wtb;
1268         rcti rect1= *rect;
1269         float alpha, alphastep;
1270         int step, tot, a;
1271         
1272         /* prevent tooltips to not show round shadow */
1273         if( 2.0f*radout > 0.2f*(rect1.ymax-rect1.ymin) )
1274                 rect1.ymax -= 0.2f*(rect1.ymax-rect1.ymin);
1275         else
1276                 rect1.ymax -= 2.0f*radout;
1277         
1278         /* inner part */
1279         tot= round_box_shadow_edges(wtb.inner_v, &rect1, radin, roundboxalign & 12, 0.0f);
1280         
1281         /* inverse linear shadow alpha */
1282         alpha= 0.15;
1283         alphastep= 0.67;
1284         
1285         for(step= 1; step<=radout; step++, alpha*=alphastep) {
1286                 round_box_shadow_edges(wtb.outer_v, &rect1, radin, 15, (float)step);
1287                 
1288                 glColor4f(0.0f, 0.0f, 0.0f, alpha);
1289                 
1290                 glBegin(GL_QUAD_STRIP);
1291                 for(a=0; a<tot; a++) {
1292                         glVertex2fv(wtb.outer_v[a]);
1293                         glVertex2fv(wtb.inner_v[a]);
1294                 }
1295                 glEnd();
1296         }
1297         
1298 }
1299
1300 static void widget_menu_back(uiWidgetColors *wcol, rcti *rect, int flag, int direction)
1301 {
1302         uiWidgetBase wtb;
1303         int roundboxalign= 15;
1304         
1305         widget_init(&wtb);
1306         
1307         /* menu is 2nd level or deeper */
1308         if (flag & UI_BLOCK_POPUP) {
1309                 //rect->ymin -= 4.0;
1310                 //rect->ymax += 4.0;
1311         }
1312         else if (direction == UI_DOWN) {
1313                 roundboxalign= 12;
1314                 rect->ymin -= 4.0;
1315         } 
1316         else if (direction == UI_TOP) {
1317                 roundboxalign= 3;
1318                 rect->ymax += 4.0;
1319         }
1320         
1321         glEnable(GL_BLEND);
1322         widget_softshadow(rect, roundboxalign, 5.0f, 8.0f);
1323         
1324         round_box_edges(&wtb, roundboxalign, rect, 5.0f);
1325         wtb.emboss= 0;
1326         widgetbase_draw(&wtb, wcol);
1327         
1328         glDisable(GL_BLEND);
1329 }
1330
1331
1332 static void ui_hsv_cursor(float x, float y)
1333 {
1334         
1335         glPushMatrix();
1336         glTranslatef(x, y, 0.0f);
1337         
1338         glColor3f(1.0f, 1.0f, 1.0f);
1339         glutil_draw_filled_arc(0.0f, M_PI*2.0, 3.0f, 8);
1340         
1341         glEnable(GL_BLEND);
1342         glEnable(GL_LINE_SMOOTH );
1343         glColor3f(0.0f, 0.0f, 0.0f);
1344         glutil_draw_lined_arc(0.0f, M_PI*2.0, 3.0f, 12);
1345         glDisable(GL_BLEND);
1346         glDisable(GL_LINE_SMOOTH );
1347         
1348         glPopMatrix();
1349         
1350 }
1351
1352 void ui_hsvcircle_vals_from_pos(float *valrad, float *valdist, rcti *rect, float mx, float my)
1353 {
1354         /* duplication of code... well, simple is better now */
1355         float centx= (float)(rect->xmin + rect->xmax)/2;
1356         float centy= (float)(rect->ymin + rect->ymax)/2;
1357         float radius, dist;
1358         
1359         if( rect->xmax-rect->xmin > rect->ymax-rect->ymin )
1360                 radius= (float)(rect->ymax - rect->ymin)/2; 
1361         else
1362                 radius= (float)(rect->xmax - rect->xmin)/2; 
1363
1364         mx-= centx;
1365         my-= centy;
1366         dist= sqrt( mx*mx + my*my);
1367         if(dist < radius)
1368                 *valdist= dist/radius;
1369         else
1370                 *valdist= 1.0f;
1371         
1372         *valrad= atan2(mx, my)/(2.0f*M_PI) + 0.5f;
1373 }
1374
1375 void ui_draw_but_HSVCIRCLE(uiBut *but, rcti *rect)
1376 {
1377         /* gouraud triangle fan */
1378         float radstep, ang= 0.0f;
1379         float centx, centy, radius;
1380         float hsv[3], col[3], colcent[3];
1381         int a, tot= 32;
1382         
1383         radstep= 2.0f*M_PI/(float)tot;
1384         centx= (float)(rect->xmin + rect->xmax)/2;
1385         centy= (float)(rect->ymin + rect->ymax)/2;
1386         
1387         if( rect->xmax-rect->xmin > rect->ymax-rect->ymin )
1388                 radius= (float)(rect->ymax - rect->ymin)/2; 
1389         else
1390                 radius= (float)(rect->xmax - rect->xmin)/2; 
1391         
1392         /* color */
1393         VECCOPY(hsv, but->hsv);
1394         hsv[0]= hsv[1]= 0.0f;
1395         hsv_to_rgb(hsv[0], hsv[1], hsv[2], colcent, colcent+1, colcent+2);
1396         
1397         glShadeModel(GL_SMOOTH);
1398
1399         glBegin(GL_TRIANGLE_FAN);
1400         glColor3fv(colcent);
1401         glVertex2f( centx, centy);
1402         
1403         for(a=0; a<=tot; a++, ang+=radstep) {
1404                 float si= sin(ang);
1405                 float co= cos(ang);
1406                 
1407                 ui_hsvcircle_vals_from_pos(hsv, hsv+1, rect, centx + co*radius, centy + si*radius);
1408                 hsv_to_rgb(hsv[0], hsv[1], hsv[2], col, col+1, col+2);
1409                 glColor3fv(col);
1410                 glVertex2f( centx + co*radius, centy + si*radius);
1411         }
1412         glEnd();
1413         
1414         glShadeModel(GL_FLAT);
1415         
1416         /* fully rounded outline */
1417         glPushMatrix();
1418         glTranslatef(centx, centy, 0.0f);
1419         glEnable(GL_BLEND);
1420         glEnable(GL_LINE_SMOOTH );
1421         glColor3f(0.0f, 0.0f, 0.0f);
1422         glutil_draw_lined_arc(0.0f, M_PI*2.0, radius, tot);
1423         glDisable(GL_BLEND);
1424         glDisable(GL_LINE_SMOOTH );
1425         glPopMatrix();
1426
1427         /* cursor */
1428         ang= 2.0f*M_PI*but->hsv[0] + 0.5f*M_PI;
1429         radius= but->hsv[1]*radius;
1430         ui_hsv_cursor(centx + cos(-ang)*radius, centy + sin(-ang)*radius);
1431         
1432 }
1433
1434 /* ************ custom buttons, old stuff ************** */
1435
1436 /* draws in resolution of 20x4 colors */
1437 static void ui_draw_but_HSVCUBE(uiBut *but, rcti *rect)
1438 {
1439         int a;
1440         float h,s,v;
1441         float dx, dy, sx1, sx2, sy, x, y;
1442         float col0[4][3];       // left half, rect bottom to top
1443         float col1[4][3];       // right half, rect bottom to top
1444         
1445         h= but->hsv[0];
1446         s= but->hsv[1];
1447         v= but->hsv[2];
1448         
1449         /* draw series of gouraud rects */
1450         glShadeModel(GL_SMOOTH);
1451         
1452         if(but->a1==0) {        // H and V vary
1453                 hsv_to_rgb(0.0, s, 0.0,   &col1[0][0], &col1[0][1], &col1[0][2]);
1454                 hsv_to_rgb(0.0, s, 0.333, &col1[1][0], &col1[1][1], &col1[1][2]);
1455                 hsv_to_rgb(0.0, s, 0.666, &col1[2][0], &col1[2][1], &col1[2][2]);
1456                 hsv_to_rgb(0.0, s, 1.0,   &col1[3][0], &col1[3][1], &col1[3][2]);
1457                 x= h; y= v;
1458         }
1459         else if(but->a1==1) {   // H and S vary
1460                 hsv_to_rgb(0.0, 0.0, v,   &col1[0][0], &col1[0][1], &col1[0][2]);
1461                 hsv_to_rgb(0.0, 0.333, v, &col1[1][0], &col1[1][1], &col1[1][2]);
1462                 hsv_to_rgb(0.0, 0.666, v, &col1[2][0], &col1[2][1], &col1[2][2]);
1463                 hsv_to_rgb(0.0, 1.0, v,   &col1[3][0], &col1[3][1], &col1[3][2]);
1464                 x= h; y= s;
1465         }
1466         else if(but->a1==2) {   // S and V vary
1467                 hsv_to_rgb(h, 0.0, 0.0,   &col1[0][0], &col1[0][1], &col1[0][2]);
1468                 hsv_to_rgb(h, 0.333, 0.0, &col1[1][0], &col1[1][1], &col1[1][2]);
1469                 hsv_to_rgb(h, 0.666, 0.0, &col1[2][0], &col1[2][1], &col1[2][2]);
1470                 hsv_to_rgb(h, 1.0, 0.0,   &col1[3][0], &col1[3][1], &col1[3][2]);
1471                 x= v; y= s;
1472         }
1473         else if(but->a1==3) {           // only hue slider
1474                 hsv_to_rgb(0.0, 1.0, 1.0,   &col1[0][0], &col1[0][1], &col1[0][2]);
1475                 VECCOPY(col1[1], col1[0]);
1476                 VECCOPY(col1[2], col1[0]);
1477                 VECCOPY(col1[3], col1[0]);
1478                 x= h; y= 0.5;
1479         }
1480         
1481         for(dx=0.0; dx<1.0; dx+= 0.05) {
1482                 // previous color
1483                 VECCOPY(col0[0], col1[0]);
1484                 VECCOPY(col0[1], col1[1]);
1485                 VECCOPY(col0[2], col1[2]);
1486                 VECCOPY(col0[3], col1[3]);
1487                 
1488                 // new color
1489                 if(but->a1==0) {        // H and V vary
1490                         hsv_to_rgb(dx, s, 0.0,   &col1[0][0], &col1[0][1], &col1[0][2]);
1491                         hsv_to_rgb(dx, s, 0.333, &col1[1][0], &col1[1][1], &col1[1][2]);
1492                         hsv_to_rgb(dx, s, 0.666, &col1[2][0], &col1[2][1], &col1[2][2]);
1493                         hsv_to_rgb(dx, s, 1.0,   &col1[3][0], &col1[3][1], &col1[3][2]);
1494                 }
1495                 else if(but->a1==1) {   // H and S vary
1496                         hsv_to_rgb(dx, 0.0, v,   &col1[0][0], &col1[0][1], &col1[0][2]);
1497                         hsv_to_rgb(dx, 0.333, v, &col1[1][0], &col1[1][1], &col1[1][2]);
1498                         hsv_to_rgb(dx, 0.666, v, &col1[2][0], &col1[2][1], &col1[2][2]);
1499                         hsv_to_rgb(dx, 1.0, v,   &col1[3][0], &col1[3][1], &col1[3][2]);
1500                 }
1501                 else if(but->a1==2) {   // S and V vary
1502                         hsv_to_rgb(h, 0.0, dx,   &col1[0][0], &col1[0][1], &col1[0][2]);
1503                         hsv_to_rgb(h, 0.333, dx, &col1[1][0], &col1[1][1], &col1[1][2]);
1504                         hsv_to_rgb(h, 0.666, dx, &col1[2][0], &col1[2][1], &col1[2][2]);
1505                         hsv_to_rgb(h, 1.0, dx,   &col1[3][0], &col1[3][1], &col1[3][2]);
1506                 }
1507                 else if(but->a1==3) {   // only H
1508                         hsv_to_rgb(dx, 1.0, 1.0,   &col1[0][0], &col1[0][1], &col1[0][2]);
1509                         VECCOPY(col1[1], col1[0]);
1510                         VECCOPY(col1[2], col1[0]);
1511                         VECCOPY(col1[3], col1[0]);
1512                 }
1513                 
1514                 // rect
1515                 sx1= rect->xmin + dx*(rect->xmax-rect->xmin);
1516                 sx2= rect->xmin + (dx+0.05)*(rect->xmax-rect->xmin);
1517                 sy= rect->ymin;
1518                 dy= (rect->ymax-rect->ymin)/3.0;
1519                 
1520                 glBegin(GL_QUADS);
1521                 for(a=0; a<3; a++, sy+=dy) {
1522                         glColor3fv(col0[a]);
1523                         glVertex2f(sx1, sy);
1524                         
1525                         glColor3fv(col1[a]);
1526                         glVertex2f(sx2, sy);
1527                         
1528                         glColor3fv(col1[a+1]);
1529                         glVertex2f(sx2, sy+dy);
1530                         
1531                         glColor3fv(col0[a+1]);
1532                         glVertex2f(sx1, sy+dy);
1533                 }
1534                 glEnd();
1535         }
1536         
1537         glShadeModel(GL_FLAT);
1538         
1539         /* cursor */
1540         x= rect->xmin + x*(rect->xmax-rect->xmin);
1541         y= rect->ymin + y*(rect->ymax-rect->ymin);
1542         CLAMP(x, rect->xmin+3.0, rect->xmax-3.0);
1543         CLAMP(y, rect->ymin+3.0, rect->ymax-3.0);
1544         
1545         ui_hsv_cursor(x, y);
1546         
1547         /* outline */
1548         glColor3ub(0,  0,  0);
1549         fdrawbox((rect->xmin), (rect->ymin), (rect->xmax), (rect->ymax));
1550 }
1551
1552 /* vertical 'value' slider, using new widget code */
1553 static void ui_draw_but_HSV_v(uiBut *but, rcti *rect)
1554 {
1555         uiWidgetBase wtb;
1556         float rad= 0.5f*(rect->xmax - rect->xmin);
1557         float x, y;
1558         
1559         widget_init(&wtb);
1560         
1561         /* fully rounded */
1562         round_box_edges(&wtb, 15, rect, rad);
1563         
1564         /* setup temp colors */
1565         wcol_tmp.outline[0]= wcol_tmp.outline[1]= wcol_tmp.outline[2]= 0;
1566         wcol_tmp.inner[0]= wcol_tmp.inner[1]= wcol_tmp.inner[2]= 128;
1567         wcol_tmp.shadetop= 127;
1568         wcol_tmp.shadedown= -128;
1569         wcol_tmp.shaded= 1;
1570         
1571         widgetbase_draw(&wtb, &wcol_tmp);
1572
1573         /* cursor */
1574         x= rect->xmin + 0.5f*(rect->xmax-rect->xmin);
1575         y= rect->ymin + but->hsv[2]*(rect->ymax-rect->ymin);
1576         CLAMP(y, rect->ymin+3.0, rect->ymax-3.0);
1577         
1578         ui_hsv_cursor(x, y);
1579         
1580 }
1581
1582 /* ************ button callbacks, draw ***************** */
1583
1584 static void widget_numbut(uiWidgetColors *wcol, rcti *rect, int state, int roundboxalign)
1585 {
1586         uiWidgetBase wtb;
1587         float rad= 0.5f*(rect->ymax - rect->ymin);
1588         
1589         widget_init(&wtb);
1590         
1591         /* fully rounded */
1592         round_box_edges(&wtb, roundboxalign, rect, rad);
1593         
1594         /* decoration */
1595         if(!(state & UI_TEXTINPUT)) {
1596                 widget_num_tria(&wtb.tria1, rect, 0.6f, 'l');
1597                 widget_num_tria(&wtb.tria2, rect, 0.6f, 'r');
1598         }       
1599         widgetbase_draw(&wtb, wcol);
1600         
1601         /* text space */
1602         rect->xmin += (rect->ymax-rect->ymin);
1603         rect->xmax -= (rect->ymax-rect->ymin);
1604
1605 }
1606
1607
1608 static int ui_link_bezier_points(rcti *rect, float coord_array[][2], int resol)
1609 {
1610         float dist, vec[4][2];
1611
1612         vec[0][0]= rect->xmin;
1613         vec[0][1]= rect->ymin;
1614         vec[3][0]= rect->xmax;
1615         vec[3][1]= rect->ymax;
1616         
1617         dist= 0.5f*ABS(vec[0][0] - vec[3][0]);
1618         
1619         vec[1][0]= vec[0][0]+dist;
1620         vec[1][1]= vec[0][1];
1621         
1622         vec[2][0]= vec[3][0]-dist;
1623         vec[2][1]= vec[3][1];
1624         
1625         forward_diff_bezier(vec[0][0], vec[1][0], vec[2][0], vec[3][0], coord_array[0], resol, 2);
1626         forward_diff_bezier(vec[0][1], vec[1][1], vec[2][1], vec[3][1], coord_array[0]+1, resol, 2);
1627         
1628         return 1;
1629 }
1630
1631 #define LINK_RESOL      24
1632 void ui_draw_link_bezier(rcti *rect)
1633 {
1634         float coord_array[LINK_RESOL+1][2];
1635         
1636         if(ui_link_bezier_points(rect, coord_array, LINK_RESOL)) {
1637                 float dist;
1638                 int i;
1639                 
1640                 /* we can reuse the dist variable here to increment the GL curve eval amount*/
1641                 dist = 1.0f/(float)LINK_RESOL;
1642                 
1643                 glEnable(GL_BLEND);
1644                 glEnable(GL_LINE_SMOOTH);
1645                 
1646                 glBegin(GL_LINE_STRIP);
1647                 for(i=0; i<=LINK_RESOL; i++) {
1648                         glVertex2fv(coord_array[i]);
1649                 }
1650                 glEnd();
1651                 
1652                 glDisable(GL_BLEND);
1653                 glDisable(GL_LINE_SMOOTH);
1654
1655         }
1656 }
1657
1658 /* function in use for buttons and for view2d sliders */
1659 void uiWidgetScrollDraw(uiWidgetColors *wcol, rcti *rect, rcti *slider, int state)
1660 {
1661         uiWidgetBase wtb;
1662         float rad;
1663         int horizontal;
1664
1665         widget_init(&wtb);
1666
1667         /* determine horizontal/vertical */
1668         horizontal= (rect->xmax - rect->xmin > rect->ymax - rect->ymin);
1669         
1670         if(horizontal)
1671                 rad= 0.5f*(rect->ymax - rect->ymin);
1672         else
1673                 rad= 0.5f*(rect->xmax - rect->xmin);
1674         
1675         wtb.shadedir= (horizontal)? 1: 0;
1676         
1677         /* draw back part, colors swapped and shading inverted */
1678         if(horizontal)
1679                 SWAP(short, wcol->shadetop, wcol->shadedown);
1680         
1681         round_box_edges(&wtb, 15, rect, rad); 
1682         widgetbase_draw(&wtb, wcol);
1683         
1684         /* slider */
1685         if(slider->xmax-slider->xmin<2 || slider->ymax-slider->ymin<2);
1686         else {
1687                 
1688                 SWAP(short, wcol->shadetop, wcol->shadedown);
1689                 
1690                 QUATCOPY(wcol->inner, wcol->item);
1691                 
1692                 if(wcol->shadetop>wcol->shadedown)
1693                         wcol->shadetop+= 20;    /* XXX violates themes... */
1694                 else wcol->shadedown+= 20;
1695                 
1696                 if(state & UI_SCROLL_PRESSED)
1697                         SWAP(short, wcol->shadetop, wcol->shadedown);
1698
1699                 /* draw */
1700                 wtb.emboss= 0; /* only emboss once */
1701                 
1702                 round_box_edges(&wtb, 15, slider, rad); 
1703                 
1704                 if(state & UI_SCROLL_ARROWS) {
1705                         if(wcol->item[0] > 48) wcol->item[0]-= 48;
1706                         if(wcol->item[1] > 48) wcol->item[1]-= 48;
1707                         if(wcol->item[2] > 48) wcol->item[2]-= 48;
1708                         wcol->item[3]= 255;
1709                         
1710                         if(horizontal) {
1711                                 widget_num_tria(&wtb.tria1, slider, 0.6f, 'l');
1712                                 widget_num_tria(&wtb.tria2, slider, 0.6f, 'r');
1713                         }
1714                         else {
1715                                 widget_num_tria(&wtb.tria1, slider, 0.6f, 'b');
1716                                 widget_num_tria(&wtb.tria2, slider, 0.6f, 't');
1717                         }
1718                 }
1719                 widgetbase_draw(&wtb, wcol);
1720         }       
1721 }
1722
1723 static void widget_scroll(uiBut *but, uiWidgetColors *wcol, rcti *rect, int state, int roundboxalign)
1724 {
1725         rcti rect1;
1726         double value;
1727         float fac, size;
1728         int horizontal;
1729
1730         /* calculate slider part */
1731         value= ui_get_but_val(but);
1732
1733         size= (but->softmax + but->a1 - but->softmin);
1734         size= MAX2(size, 2);
1735         
1736         /* position */
1737         rect1= *rect;
1738
1739         /* determine horizontal/vertical */
1740         horizontal= (rect->xmax - rect->xmin > rect->ymax - rect->ymin);
1741         
1742         if(horizontal) {
1743                 fac= (rect->xmax - rect->xmin)/(size);
1744                 rect1.xmin= rect1.xmin + ceil(fac*(value - but->softmin));
1745                 rect1.xmax= rect1.xmin + ceil(fac*(but->a1 - but->softmin));
1746         }
1747         else {
1748                 fac= (rect->ymax - rect->ymin)/(size);
1749                 rect1.ymax= rect1.ymax - ceil(fac*(value - but->softmin));
1750                 rect1.ymin= rect1.ymax - ceil(fac*(but->a1 - but->softmin));
1751         }
1752
1753         if(state & UI_SELECT)
1754                 state= UI_SCROLL_PRESSED;
1755         else
1756                 state= 0;
1757         uiWidgetScrollDraw(wcol, rect, &rect1, state);
1758
1759 }
1760
1761 static void widget_link(uiBut *but, uiWidgetColors *wcol, rcti *rect, int state, int roundboxalign)
1762 {
1763
1764         if(but->flag & UI_SELECT) {
1765                 rcti rectlink;
1766                 
1767                 UI_ThemeColor(TH_TEXT_HI);
1768                 
1769                 rectlink.xmin= (rect->xmin+rect->xmax)/2;
1770                 rectlink.ymin= (rect->ymin+rect->ymax)/2;
1771                 rectlink.xmax= but->linkto[0];
1772                 rectlink.ymax= but->linkto[1];
1773                 
1774                 ui_draw_link_bezier(&rectlink);
1775         }
1776 }
1777
1778
1779 static void widget_numslider(uiBut *but, uiWidgetColors *wcol, rcti *rect, int state, int roundboxalign)
1780 {
1781         uiWidgetBase wtb, wtb1;
1782         rcti rect1;
1783         double value;
1784         float offs, fac;
1785         char outline[3];
1786         
1787         widget_init(&wtb);
1788         widget_init(&wtb1);
1789         
1790         /* backdrop first */
1791         
1792         /* fully rounded */
1793         offs= 0.5f*(rect->ymax - rect->ymin);
1794         round_box_edges(&wtb, roundboxalign, rect, offs);
1795
1796         wtb.outline= 0;
1797         widgetbase_draw(&wtb, wcol);
1798         
1799         /* slider part */
1800         VECCOPY(outline, wcol->outline);
1801         VECCOPY(wcol->outline, wcol->item);
1802         VECCOPY(wcol->inner, wcol->item);
1803         SWAP(short, wcol->shadetop, wcol->shadedown);
1804         
1805         rect1= *rect;
1806         
1807         value= ui_get_but_val(but);
1808         fac= (value-but->softmin)*(rect1.xmax - rect1.xmin - offs)/(but->softmax - but->softmin);
1809         
1810         /* left part of slider, always rounded */
1811         rect1.xmax= rect1.xmin + ceil(offs+1.0f);
1812         round_box_edges(&wtb1, roundboxalign & ~6, &rect1, offs);
1813         wtb1.outline= 0;
1814         widgetbase_draw(&wtb1, wcol);
1815         
1816         /* right part of slider, interpolate roundness */
1817         rect1.xmax= rect1.xmin + fac + offs;
1818         rect1.xmin+=  floor(offs-1.0f);
1819         if(rect1.xmax + offs > rect->xmax)
1820                 offs*= (rect1.xmax + offs - rect->xmax)/offs;
1821         else 
1822                 offs= 0.0f;
1823         round_box_edges(&wtb1, roundboxalign & ~9, &rect1, offs);
1824         
1825         widgetbase_draw(&wtb1, wcol);
1826         VECCOPY(wcol->outline, outline);
1827         SWAP(short, wcol->shadetop, wcol->shadedown);
1828         
1829         /* outline */
1830         wtb.outline= 1;
1831         wtb.inner= 0;
1832         widgetbase_draw(&wtb, wcol);
1833         
1834         /* text space */
1835         rect->xmin += (rect->ymax-rect->ymin);
1836         rect->xmax -= (rect->ymax-rect->ymin);
1837         
1838 }
1839
1840 static void widget_swatch(uiBut *but, uiWidgetColors *wcol, rcti *rect, int state, int roundboxalign)
1841 {
1842         uiWidgetBase wtb;
1843         float col[4];
1844         
1845         widget_init(&wtb);
1846         
1847         /* half rounded */
1848         round_box_edges(&wtb, roundboxalign, rect, 4.0f);
1849                 
1850         ui_get_but_vectorf(but, col);
1851         wcol->inner[0]= FTOCHAR(col[0]);
1852         wcol->inner[1]= FTOCHAR(col[1]);
1853         wcol->inner[2]= FTOCHAR(col[2]);
1854         
1855         widgetbase_draw(&wtb, wcol);
1856         
1857 }
1858
1859
1860 static void widget_textbut(uiWidgetColors *wcol, rcti *rect, int state, int roundboxalign)
1861 {
1862         uiWidgetBase wtb;
1863         
1864         widget_init(&wtb);
1865         
1866         /* half rounded */
1867         round_box_edges(&wtb, roundboxalign, rect, 5.0f);
1868         
1869         widgetbase_draw(&wtb, wcol);
1870
1871 }
1872
1873
1874 static void widget_menubut(uiWidgetColors *wcol, rcti *rect, int state, int roundboxalign)
1875 {
1876         uiWidgetBase wtb;
1877         
1878         widget_init(&wtb);
1879         
1880         /* half rounded */
1881         round_box_edges(&wtb, roundboxalign, rect, 4.0f);
1882         
1883         /* decoration */
1884         widget_menu_trias(&wtb.tria1, rect);
1885         
1886         widgetbase_draw(&wtb, wcol);
1887         
1888         /* text space */
1889         rect->xmax -= (rect->ymax-rect->ymin);
1890         
1891 }
1892
1893 static void widget_pulldownbut(uiWidgetColors *wcol, rcti *rect, int state, int roundboxalign)
1894 {
1895         if(state & UI_ACTIVE) {
1896                 uiWidgetBase wtb;
1897                 float rad= 0.5f*(rect->ymax - rect->ymin);
1898                 
1899                 widget_init(&wtb);
1900                 
1901                 /* fully rounded */
1902                 round_box_edges(&wtb, roundboxalign, rect, rad);
1903                 
1904                 widgetbase_draw(&wtb, wcol);
1905         }
1906 }
1907
1908 static void widget_menu_itembut(uiWidgetColors *wcol, rcti *rect, int state, int roundboxalign)
1909 {
1910         uiWidgetBase wtb;
1911         
1912         widget_init(&wtb);
1913         
1914         /* not rounded, no outline */
1915         wtb.outline= 0;
1916         round_box_edges(&wtb, 0, rect, 0.0f);
1917         
1918         widgetbase_draw(&wtb, wcol);
1919 }
1920
1921
1922 static void widget_optionbut(uiWidgetColors *wcol, rcti *rect, int state, int roundboxalign)
1923 {
1924         uiWidgetBase wtb;
1925         rcti recttemp= *rect;
1926         int delta;
1927         
1928         widget_init(&wtb);
1929         
1930         /* square */
1931         recttemp.xmax= recttemp.xmin + (recttemp.ymax-recttemp.ymin);
1932         
1933         /* smaller */
1934         delta= 1 + (recttemp.ymax-recttemp.ymin)/8;
1935         recttemp.xmin+= delta;
1936         recttemp.ymin+= delta;
1937         recttemp.xmax-= delta;
1938         recttemp.ymax-= delta;
1939         
1940         /* half rounded */
1941         round_box_edges(&wtb, 15, &recttemp, 4.0f);
1942         
1943         /* decoration */
1944         if(state & UI_SELECT) {
1945                 widget_check_trias(&wtb.tria1, &recttemp);
1946         }
1947         
1948         widgetbase_draw(&wtb, wcol);
1949         
1950         /* text space */
1951         rect->xmin += (rect->ymax-rect->ymin)*0.7 + delta;
1952 }
1953
1954
1955 static void widget_radiobut(uiWidgetColors *wcol, rcti *rect, int state, int roundboxalign)
1956 {
1957         uiWidgetBase wtb;
1958         
1959         widget_init(&wtb);
1960         
1961         /* half rounded */
1962         round_box_edges(&wtb, roundboxalign, rect, 4.0f);
1963         
1964         widgetbase_draw(&wtb, wcol);
1965
1966 }
1967
1968 static void widget_but(uiWidgetColors *wcol, rcti *rect, int state, int roundboxalign)
1969 {
1970         uiWidgetBase wtb;
1971         
1972         widget_init(&wtb);
1973         
1974         /* half rounded */
1975         round_box_edges(&wtb, roundboxalign, rect, 4.0f);
1976                 
1977         widgetbase_draw(&wtb, wcol);
1978
1979 }
1980
1981 static void widget_roundbut(uiWidgetColors *wcol, rcti *rect, int state, int roundboxalign)
1982 {
1983         uiWidgetBase wtb;
1984         float rad= 0.5f*(rect->ymax - rect->ymin);
1985         
1986         widget_init(&wtb);
1987         
1988         /* fully rounded */
1989         round_box_edges(&wtb, roundboxalign, rect, rad);
1990
1991         widgetbase_draw(&wtb, wcol);
1992 }
1993
1994 static void widget_draw_extra_mask(const bContext *C, uiBut *but, uiWidgetType *wt, rcti *rect)
1995 {
1996         uiWidgetBase wtb;
1997         char col[4];
1998         
1999         /* state copy! */
2000         wt->wcol= *(wt->wcol_theme);
2001         
2002         widget_init(&wtb);
2003         
2004         if(but->block->drawextra) {
2005                 /* note: drawextra can change rect +1 or -1, to match round errors of existing previews */
2006                 but->block->drawextra(C, but->poin, rect);
2007                 
2008                 /* make mask to draw over image */
2009                 UI_GetThemeColor3ubv(TH_BACK, col);
2010                 glColor3ubv(col);
2011                 
2012                 round_box__edges(&wtb, 15, rect, 0.0f, 4.0);
2013                 widgetbase_outline(&wtb);
2014         }
2015         
2016         /* outline */
2017         round_box_edges(&wtb, 15, rect, 5.0f);
2018         wtb.outline= 1;
2019         wtb.inner= 0;
2020         widgetbase_draw(&wtb, &wt->wcol);
2021         
2022 }
2023
2024
2025 static void widget_disabled(rcti *rect)
2026 {
2027         float col[4];
2028         
2029         glEnable(GL_BLEND);
2030         
2031         /* can't use theme TH_BACK or TH_PANEL... undefined */
2032         glGetFloatv(GL_COLOR_CLEAR_VALUE, col);
2033         glColor4f(col[0], col[1], col[2], 0.5f);
2034         /* need -1 and +1 to make it work right for aligned buttons,
2035          * but problem may be somewhere else? */
2036         glRectf(rect->xmin-1, rect->ymin, rect->xmax, rect->ymax+1);
2037
2038         glDisable(GL_BLEND);
2039 }
2040
2041 static uiWidgetType *widget_type(uiWidgetTypeEnum type)
2042 {
2043         bTheme *btheme= U.themes.first;
2044         static uiWidgetType wt;
2045         
2046         /* defaults */
2047         wt.wcol_theme= &btheme->tui.wcol_regular;
2048         wt.wcol_state= &btheme->tui.wcol_state;
2049         wt.state= widget_state;
2050         wt.draw= widget_but;
2051         wt.custom= NULL;
2052         wt.text= widget_draw_text_icon;
2053         
2054         switch(type) {
2055                 case UI_WTYPE_REGULAR:
2056                         break;
2057
2058                 case UI_WTYPE_LABEL:
2059                         wt.draw= NULL;
2060                         wt.state= widget_state_label;
2061                         break;
2062                         
2063                 case UI_WTYPE_TOGGLE:
2064                         wt.wcol_theme= &btheme->tui.wcol_toggle;
2065                         break;
2066                         
2067                 case UI_WTYPE_OPTION:
2068                         wt.wcol_theme= &btheme->tui.wcol_option;
2069                         wt.draw= widget_optionbut;
2070                         wt.state= widget_state_label;
2071                         break;
2072                         
2073                 case UI_WTYPE_RADIO:
2074                         wt.wcol_theme= &btheme->tui.wcol_radio;
2075                         wt.draw= widget_radiobut;
2076                         break;
2077
2078                 case UI_WTYPE_NUMBER:
2079                         wt.wcol_theme= &btheme->tui.wcol_num;
2080                         wt.draw= widget_numbut;
2081                         break;
2082                         
2083                 case UI_WTYPE_SLIDER:
2084                         wt.wcol_theme= &btheme->tui.wcol_numslider;
2085                         wt.custom= widget_numslider;
2086                         break;
2087                         
2088                 case UI_WTYPE_EXEC:
2089                         wt.wcol_theme= &btheme->tui.wcol_tool;
2090                         wt.draw= widget_roundbut;
2091                         break;
2092                         
2093                         
2094                         /* strings */
2095                 case UI_WTYPE_NAME:
2096                         wt.wcol_theme= &btheme->tui.wcol_text;
2097                         wt.draw= widget_textbut;
2098                         break;
2099                         
2100                 case UI_WTYPE_NAME_LINK:
2101                         break;
2102                         
2103                 case UI_WTYPE_POINTER_LINK:
2104                         break;
2105                         
2106                 case UI_WTYPE_FILENAME:
2107                         break;
2108                         
2109                         
2110                         /* start menus */
2111                 case UI_WTYPE_MENU_RADIO:
2112                         wt.wcol_theme= &btheme->tui.wcol_menu;
2113                         wt.draw= widget_menubut;
2114                         break;
2115                         
2116                 case UI_WTYPE_MENU_POINTER_LINK:
2117                         wt.wcol_theme= &btheme->tui.wcol_menu;
2118                         wt.draw= widget_menubut;
2119                         break;
2120                         
2121                         
2122                 case UI_WTYPE_PULLDOWN:
2123                         wt.wcol_theme= &btheme->tui.wcol_pulldown;
2124                         wt.draw= widget_pulldownbut;
2125                         wt.state= widget_state_pulldown;
2126                         break;
2127                         
2128                         /* in menus */
2129                 case UI_WTYPE_MENU_ITEM:
2130                         wt.wcol_theme= &btheme->tui.wcol_menu_item;
2131                         wt.draw= widget_menu_itembut;
2132                         wt.state= widget_state_menu_item;
2133                         break;
2134                         
2135                 case UI_WTYPE_MENU_BACK:
2136                         wt.wcol_theme= &btheme->tui.wcol_menu_back;
2137                         wt.draw= widget_menu_back;
2138                         break;
2139                         
2140                         /* specials */
2141                 case UI_WTYPE_ICON:
2142                         wt.draw= NULL;
2143                         break;
2144                         
2145                 case UI_WTYPE_SWATCH:
2146                         wt.custom= widget_swatch;
2147                         break;
2148                         
2149                 case UI_WTYPE_BOX:
2150                         wt.wcol_theme= &btheme->tui.wcol_box;
2151                         break;
2152                         
2153                 case UI_WTYPE_RGB_PICKER:
2154                         break;
2155                         
2156                 case UI_WTYPE_NORMAL:
2157                         break;
2158
2159                 case UI_WTYPE_SCROLL:
2160                         wt.wcol_theme= &btheme->tui.wcol_scroll;
2161                         wt.state= widget_state_nothing;
2162                         wt.custom= widget_scroll;
2163                         break;
2164         }
2165         
2166         return &wt;
2167 }
2168
2169
2170 static int widget_roundbox_set(uiBut *but, rcti *rect)
2171 {
2172         /* alignment */
2173         if(but->flag & UI_BUT_ALIGN) {
2174                 
2175                 if(but->flag & UI_BUT_ALIGN_TOP)
2176                         rect->ymax+= 1;
2177                 if(but->flag & UI_BUT_ALIGN_LEFT)
2178                         rect->xmin-= 1;
2179                 
2180                 switch(but->flag & UI_BUT_ALIGN) {
2181                         case UI_BUT_ALIGN_TOP:
2182                                 return (12);
2183                                 break;
2184                         case UI_BUT_ALIGN_DOWN:
2185                                 return (3);
2186                                 break;
2187                         case UI_BUT_ALIGN_LEFT:
2188                                 return (6);
2189                                 break;
2190                         case UI_BUT_ALIGN_RIGHT:
2191                                 return (9);
2192                                 break;
2193                                 
2194                         case UI_BUT_ALIGN_DOWN|UI_BUT_ALIGN_RIGHT:
2195                                 return (1);
2196                                 break;
2197                         case UI_BUT_ALIGN_DOWN|UI_BUT_ALIGN_LEFT:
2198                                 return (2);
2199                                 break;
2200                         case UI_BUT_ALIGN_TOP|UI_BUT_ALIGN_RIGHT:
2201                                 return (8);
2202                                 break;
2203                         case UI_BUT_ALIGN_TOP|UI_BUT_ALIGN_LEFT:
2204                                 return (4);
2205                                 break;
2206                                 
2207                         default:
2208                                 return (0);
2209                                 break;
2210                 }
2211         } 
2212         return 15;
2213 }
2214
2215 /* conversion from old to new buttons, so still messy */
2216 void ui_draw_but(const bContext *C, ARegion *ar, uiStyle *style, uiBut *but, rcti *rect)
2217 {
2218         bTheme *btheme= U.themes.first;
2219         ThemeUI *tui= &btheme->tui;
2220         uiFontStyle *fstyle= &style->widget;
2221         uiWidgetType *wt= NULL;
2222         
2223         /* handle menus seperately */
2224         if(but->dt==UI_EMBOSSP) {
2225                 switch (but->type) {
2226                         case LABEL:
2227                                 widget_draw_text_icon(&style->widgetlabel, &tui->wcol_menu_back, but, rect);
2228                                 break;
2229                         case SEPR:
2230                                 break;
2231                                 
2232                         default:
2233                                 wt= widget_type(UI_WTYPE_MENU_ITEM);
2234                 }
2235         }
2236         else if(but->dt==UI_EMBOSSN) {
2237                 /* "nothing" */
2238                 wt= widget_type(UI_WTYPE_ICON);
2239         }
2240         else {
2241                 
2242                 switch (but->type) {
2243                         case LABEL:
2244                                 if(but->block->flag & UI_BLOCK_LOOP)
2245                                         widget_draw_text_icon(&style->widgetlabel, &tui->wcol_menu_back, but, rect);
2246                                 else {
2247                                         wt= widget_type(UI_WTYPE_LABEL);
2248                                         fstyle= &style->widgetlabel;
2249                                 }
2250                                 break;
2251                                 
2252                         case SEPR:
2253                                 break;
2254                                 
2255                         case BUT:
2256                                 wt= widget_type(UI_WTYPE_EXEC);
2257                                 break;
2258
2259                         case NUM:
2260                                 wt= widget_type(UI_WTYPE_NUMBER);
2261                                 break;
2262                                 
2263                         case NUMSLI:
2264                         case HSVSLI:
2265                                 wt= widget_type(UI_WTYPE_SLIDER);
2266                                 break;
2267                                 
2268                         case ROW:
2269                                 wt= widget_type(UI_WTYPE_RADIO);
2270                                 break;
2271                                 
2272                         case TEX:
2273                                 wt= widget_type(UI_WTYPE_NAME);
2274                                 break;
2275                                 
2276                         case SEARCH_MENU:
2277                                 wt= widget_type(UI_WTYPE_NAME);
2278                                 if(but->block->flag & UI_BLOCK_LOOP)
2279                                         wt->wcol_theme= &btheme->tui.wcol_menu_back;
2280                                 break;
2281                                 
2282                         case TOGBUT:
2283                         case TOG:
2284                         case TOGN:
2285                         case TOG3:
2286                                 wt= widget_type(UI_WTYPE_TOGGLE);
2287                                 break;
2288                                 
2289                         case OPTION:
2290                         case OPTIONN:
2291                                 if (!(but->flag & UI_HAS_ICON)) {
2292                                         wt= widget_type(UI_WTYPE_OPTION);
2293                                         but->flag |= UI_TEXT_LEFT;
2294                                 }
2295                                 else
2296                                         wt= widget_type(UI_WTYPE_TOGGLE);
2297                                 break;
2298                                 
2299                         case MENU:
2300                         case BLOCK:
2301                         case ICONTEXTROW:
2302                                 wt= widget_type(UI_WTYPE_MENU_RADIO);
2303                                 break;
2304                                 
2305                         case PULLDOWN:
2306                                 wt= widget_type(UI_WTYPE_PULLDOWN);
2307                                 break;
2308                         
2309                         case BUTM:
2310                                 wt= widget_type(UI_WTYPE_MENU_ITEM);
2311                                 break;
2312                                 
2313                         case COL:
2314                                 wt= widget_type(UI_WTYPE_SWATCH);
2315                                 break;
2316                                 
2317                         case ROUNDBOX:
2318                                 wt= widget_type(UI_WTYPE_BOX);
2319                                 break;
2320                                 
2321                         case LINK:
2322                         case INLINK:
2323                                 wt= widget_type(UI_WTYPE_ICON);
2324                                 wt->custom= widget_link;
2325                                 
2326                                 break;
2327                         
2328                         case BUT_EXTRA:
2329                                 widget_draw_extra_mask(C, but, widget_type(UI_WTYPE_BOX), rect);
2330                                 break;
2331                                 
2332                         case HSVCUBE:
2333                                 if(but->a1==4) // vertical V slider, uses new widget draw now
2334                                         ui_draw_but_HSV_v(but, rect);
2335                                 else  // other HSV pickers...
2336                                         ui_draw_but_HSVCUBE(but, rect);
2337                                 break;
2338                                 
2339                         case HSVCIRCLE:
2340                                 ui_draw_but_HSVCIRCLE(but, rect);
2341                                 break;
2342                                 
2343                         case BUT_COLORBAND:
2344                                 ui_draw_but_COLORBAND(but, &tui->wcol_regular, rect);
2345                                 break;
2346                                 
2347                         case BUT_NORMAL:
2348                                 ui_draw_but_NORMAL(but, &tui->wcol_regular, rect);
2349                                 break;
2350                                 
2351                         case BUT_CURVE:
2352                                 ui_draw_but_CURVE(ar, but, &tui->wcol_regular, rect);
2353                                 break;
2354
2355                         case SCROLL:
2356                                 wt= widget_type(UI_WTYPE_SCROLL);
2357                                 break;
2358
2359                         default:
2360                                 wt= widget_type(UI_WTYPE_REGULAR);
2361                 }
2362         }
2363         
2364         if(wt) {
2365                 rcti disablerect= *rect; /* rect gets clipped smaller for text */
2366                 int roundboxalign, state;
2367                 
2368                 roundboxalign= widget_roundbox_set(but, rect);
2369
2370                 state= but->flag;
2371                 if(but->editstr) state |= UI_TEXTINPUT;
2372                 
2373                 wt->state(wt, state);
2374                 if(wt->custom)
2375                         wt->custom(but, &wt->wcol, rect, state, roundboxalign);
2376                 else if(wt->draw)
2377                         wt->draw(&wt->wcol, rect, state, roundboxalign);
2378                 wt->text(fstyle, &wt->wcol, but, rect);
2379                 
2380                 if(state & (UI_BUT_DISABLED|UI_BUT_INACTIVE))
2381                         if(but->dt!=UI_EMBOSSP)
2382                                 widget_disabled(&disablerect);
2383         }
2384 }
2385
2386 void ui_draw_menu_back(uiStyle *style, uiBlock *block, rcti *rect)
2387 {
2388         uiWidgetType *wt= widget_type(UI_WTYPE_MENU_BACK);
2389         
2390         wt->state(wt, 0);
2391         if(block)
2392                 wt->draw(&wt->wcol, rect, block->flag, block->direction);
2393         else
2394                 wt->draw(&wt->wcol, rect, 0, 0);
2395         
2396 }
2397
2398 void ui_draw_search_back(uiStyle *style, uiBlock *block, rcti *rect)
2399 {
2400         uiWidgetType *wt= widget_type(UI_WTYPE_BOX);
2401         
2402         glEnable(GL_BLEND);
2403         widget_softshadow(rect, 15, 5.0f, 8.0f);
2404         glDisable(GL_BLEND);
2405
2406         wt->state(wt, 0);
2407         if(block)
2408                 wt->draw(&wt->wcol, rect, block->flag, 15);
2409         else
2410                 wt->draw(&wt->wcol, rect, 0, 15);
2411         
2412 }
2413
2414
2415 /* helper call to draw a menu item without button */
2416 /* state: UI_ACTIVE or 0 */
2417 void ui_draw_menu_item(uiFontStyle *fstyle, rcti *rect, char *name, int iconid, int state)
2418 {
2419         uiWidgetType *wt= widget_type(UI_WTYPE_MENU_ITEM);
2420         rcti _rect= *rect;
2421         char *cpoin;
2422         
2423         wt->state(wt, state);
2424         wt->draw(&wt->wcol, rect, 0, 0);
2425         
2426         uiStyleFontSet(fstyle);
2427         fstyle->align= UI_STYLE_TEXT_LEFT;
2428         
2429         /* text location offset */
2430         rect->xmin+=5;
2431         if(iconid) rect->xmin+= ICON_HEIGHT;
2432
2433         /* cut string in 2 parts? */
2434         cpoin= strchr(name, '|');
2435         if(cpoin) {
2436                 *cpoin= 0;
2437                 rect->xmax -= BLF_width(cpoin+1) + 10;
2438         }
2439         
2440         glColor3ubv(wt->wcol.text);
2441         uiStyleFontDraw(fstyle, rect, name);
2442         
2443         /* part text right aligned */
2444         if(cpoin) {
2445                 fstyle->align= UI_STYLE_TEXT_RIGHT;
2446                 rect->xmax= _rect.xmax - 5;
2447                 uiStyleFontDraw(fstyle, rect, cpoin+1);
2448                 *cpoin= '|';
2449         }
2450         
2451         /* restore rect, was messed with */
2452         *rect= _rect;
2453
2454         if(iconid) {
2455                 int xs= rect->xmin+4;
2456                 int ys= 1 + (rect->ymin+rect->ymax- ICON_HEIGHT)/2;
2457                 glEnable(GL_BLEND);
2458                 UI_icon_draw_aspect_blended(xs, ys, iconid, 1.2f, 0); /* XXX scale weak get from fstyle? */
2459                 glDisable(GL_BLEND);
2460         }
2461 }
2462