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