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