fltk  1.3.5-source
About: FLTK (Fast Light Tool Kit) is a cross-platform C++ GUI toolkit for UNIX/Linux (X11), Microsoft Windows, and MacOS X.
  Fossies Dox: fltk-1.3.5-source.tar.bz2  ("inofficial" and yet experimental doxygen-generated source code documentation)  

fl_plastic.cxx
Go to the documentation of this file.
1 //
2 // "$Id$"
3 //
4 // "Plastic" drawing routines for the Fast Light Tool Kit (FLTK).
5 //
6 // These box types provide a cross between Aqua and KDE buttons; kindof
7 // like translucent plastic buttons...
8 //
9 // Copyright 2001-2010 by Michael Sweet.
10 //
11 // This library is free software. Distribution and use rights are outlined in
12 // the file "COPYING" which should have been included with this file. If this
13 // file is missing or damaged, see the license at:
14 //
15 // http://www.fltk.org/COPYING.php
16 //
17 // Please report all bugs and problems on the following page:
18 //
19 // http://www.fltk.org/str.php
20 //
21 
22 // Box drawing code for an obscure box type.
23 // These box types are in separate files so they are not linked
24 // in if not used.
25 
26 #include <FL/Fl.H>
27 #include <FL/fl_draw.H>
28 #include "flstring.h"
29 
30 //
31 // Uncomment the following line to restore the old plastic box type
32 // appearance.
33 //
34 
35 //#define USE_OLD_PLASTIC_BOX
36 #define USE_OLD_PLASTIC_COLOR
37 
38 extern const uchar *fl_gray_ramp();
39 
41 #ifdef USE_OLD_PLASTIC_COLOR
42  return fl_color_average((Fl_Color)gc, bc, 0.75f);
43 #else
44  unsigned grgb = Fl::get_color((Fl_Color)gc),
45  brgb = Fl::get_color(bc);
46  int red, green, blue, gray;
47 
48 
49  gray = ((grgb >> 24) & 255);
50  red = gray * ((brgb >> 24) & 255) / 255 + gray * gray / 510;
51  gray = ((grgb >> 16) & 255);
52  green = gray * ((brgb >> 16) & 255) / 255 + gray * gray / 510;
53  gray = ((grgb >> 8) & 255);
54  blue = gray * ((brgb >> 8) & 255) / 255 + gray * gray / 510;
55 
56  if (red > 255)
57  red = 255;
58 
59  if (green > 255)
60  green = 255;
61 
62  if (blue > 255)
63  blue = 255;
64 
65  if (Fl::draw_box_active())
66  return fl_rgb_color(red, green, blue);
67  else
68  return fl_color_average(FL_GRAY, fl_rgb_color(red, green, blue), 0.75f);
69 #endif // USE_OLD_PLASTIC_COLOR
70 }
71 
72 
73 static void frame_rect(int x, int y, int w, int h, const char *c, Fl_Color bc) {
74  const uchar *g = fl_gray_ramp();
75  int b = ((int) strlen(c)) / 4 + 1;
76 
77  for (x += b, y += b, w -= 2 * b, h -= 2 * b; b > 1; b --)
78  {
79  // Draw lines around the perimeter of the button, 4 colors per
80  // circuit.
81  fl_color(shade_color(g[(int)*c++], bc));
82  fl_line(x, y + h + b, x + w - 1, y + h + b, x + w + b - 1, y + h);
83  fl_color(shade_color(g[(int)*c++], bc));
84  fl_line(x + w + b - 1, y + h, x + w + b - 1, y, x + w - 1, y - b);
85  fl_color(shade_color(g[(int)*c++], bc));
86  fl_line(x + w - 1, y - b, x, y - b, x - b, y);
87  fl_color(shade_color(g[(int)*c++], bc));
88  fl_line(x - b, y, x - b, y + h, x, y + h + b);
89  }
90 }
91 
92 
93 static void frame_round(int x, int y, int w, int h, const char *c, Fl_Color bc) {
94  const uchar *g = fl_gray_ramp();
95  size_t b = strlen(c) / 4 + 1;
96 
97  if (w==h) {
98  for (; b > 1; b --, x ++, y ++, w -= 2, h -= 2)
99  {
100  fl_color(shade_color(g[(int)*c++], bc));
101  fl_arc(x, y, w, h, 45.0, 135.0);
102  fl_color(shade_color(g[(int)*c++], bc));
103  fl_arc(x, y, w, h, 315.0, 405.0);
104  fl_color(shade_color(g[(int)*c++], bc));
105  fl_arc(x, y, w, h, 225.0, 315.0);
106  fl_color(shade_color(g[(int)*c++], bc));
107  fl_arc(x, y, w, h, 135.0, 225.0);
108  }
109  } else if (w>h) {
110  int d = h/2;
111  for (; b > 1; d--, b --, x ++, y ++, w -= 2, h -= 2)
112  {
113  fl_color(shade_color(g[(int)*c++], bc));
114  fl_arc(x, y, h, h, 90.0, 135.0);
115  fl_xyline(x+d, y, x+w-d);
116  fl_arc(x+w-h, y, h, h, 45.0, 90.0);
117  fl_color(shade_color(g[(int)*c++], bc));
118  fl_arc(x+w-h, y, h, h, 315.0, 405.0);
119  fl_color(shade_color(g[(int)*c++], bc));
120  fl_arc(x+w-h, y, h, h, 270.0, 315.0);
121  fl_xyline(x+d, y+h-1, x+w-d);
122  fl_arc(x, y, h, h, 225.0, 270.0);
123  fl_color(shade_color(g[(int)*c++], bc));
124  fl_arc(x, y, h, h, 135.0, 225.0);
125  }
126  } else if (w<h) {
127  int d = w/2;
128  for (; b > 1; d--, b --, x ++, y ++, w -= 2, h -= 2)
129  {
130  fl_color(shade_color(g[(int)*c++], bc));
131  fl_arc(x, y, w, w, 45.0, 135.0);
132  fl_color(shade_color(g[(int)*c++], bc));
133  fl_arc(x, y, w, w, 0.0, 45.0);
134  fl_yxline(x+w-1, y+d, y+h-d);
135  fl_arc(x, y+h-w, w, w, 315.0, 360.0);
136  fl_color(shade_color(g[(int)*c++], bc));
137  fl_arc(x, y+h-w, w, w, 225.0, 315.0);
138  fl_color(shade_color(g[(int)*c++], bc));
139  fl_arc(x, y+h-w, w, w, 180.0, 225.0);
140  fl_yxline(x, y+d, y+h-d);
141  fl_arc(x, y, w, w, 135.0, 180.0);
142  }
143  }
144 }
145 
146 
147 static void shade_rect(int x, int y, int w, int h, const char *c, Fl_Color bc) {
148  const uchar *g = fl_gray_ramp();
149  int i, j;
150  int clen = (int) strlen(c) - 1;
151  int chalf = clen / 2;
152  int cstep = 1;
153 
154  if (h < (w * 2)) {
155  // Horizontal shading...
156  if (clen >= h) cstep = 2;
157 
158  for (i = 0, j = 0; j < chalf; i ++, j += cstep) {
159  // Draw the top line and points...
160  fl_color(shade_color(g[(int)c[i]], bc));
161  fl_xyline(x + 1, y + i, x + w - 2);
162 
163  fl_color(shade_color(g[c[i] - 2], bc));
164  fl_point(x, y + i + 1);
165  fl_point(x + w - 1, y + i + 1);
166 
167  // Draw the bottom line and points...
168  fl_color(shade_color(g[(int)c[clen - i]], bc));
169  fl_xyline(x + 1, y + h - i, x + w - 2);
170 
171  fl_color(shade_color(g[c[clen - i] - 2], bc));
172  fl_point(x, y + h - i);
173  fl_point(x + w - 1, y + h - i);
174  }
175 
176  // Draw the interior and sides...
177  i = chalf / cstep;
178 
179  fl_color(shade_color(g[(int)c[chalf]], bc));
180  fl_rectf(x + 1, y + i, w - 2, h - 2 * i + 1);
181 
182  fl_color(shade_color(g[c[chalf] - 2], bc));
183  fl_yxline(x, y + i, y + h - i);
184  fl_yxline(x + w - 1, y + i, y + h - i);
185  } else {
186  // Vertical shading...
187  if (clen >= w) cstep = 2;
188 
189  for (i = 0, j = 0; j < chalf; i ++, j += cstep) {
190  // Draw the left line and points...
191  fl_color(shade_color(g[(int)c[i]], bc));
192  fl_yxline(x + i, y + 1, y + h - 1);
193 
194  fl_color(shade_color(g[c[i] - 2], bc));
195  fl_point(x + i + 1, y);
196  fl_point(x + i + 1, y + h);
197 
198  // Draw the right line and points...
199  fl_color(shade_color(g[(int)c[clen - i]], bc));
200  fl_yxline(x + w - 1 - i, y + 1, y + h - 1);
201 
202  fl_color(shade_color(g[c[clen - i] - 2], bc));
203  fl_point(x + w - 2 - i, y);
204  fl_point(x + w - 2 - i, y + h);
205  }
206 
207  // Draw the interior, top, and bottom...
208  i = chalf / cstep;
209 
210  fl_color(shade_color(g[(int)c[chalf]], bc));
211  fl_rectf(x + i, y + 1, w - 2 * i, h - 1);
212 
213  fl_color(shade_color(g[c[chalf] - 2], bc));
214  fl_xyline(x + i, y, x + w - i);
215  fl_xyline(x + i, y + h, x + w - i);
216  }
217 }
218 
219 static void shade_round(int x, int y, int w, int h, const char *c, Fl_Color bc) {
220  const uchar *g = fl_gray_ramp();
221  int i;
222  int clen = (int) (strlen(c) - 1);
223  int chalf = clen / 2;
224 
225  if (w>h) {
226  int d = h/2;
227  const int na = 8;
228  for (i=0; i<chalf; i++, d--, x++, y++, w-=2, h-=2)
229  {
230  fl_color(shade_color(g[(int)c[i]], bc));
231  fl_pie(x, y, h, h, 90.0, 135.0+i*na);
232  fl_xyline(x+d, y, x+w-d);
233  fl_pie(x+w-h, y, h, h, 45.0+i*na, 90.0);
234  fl_color(shade_color(g[(int)c[i] - 2], bc));
235  fl_pie(x+w-h, y, h, h, 315.0+i*na, 405.0+i*na);
236  fl_color(shade_color(g[(int)c[clen - i]], bc));
237  fl_pie(x+w-h, y, h, h, 270.0, 315.0+i*na);
238  fl_xyline(x+d, y+h-1, x+w-d);
239  fl_pie(x, y, h, h, 225.0+i*na, 270.0);
240  fl_color(shade_color(g[c[(int)clen - i] - 2], bc));
241  fl_pie(x, y, h, h, 135.0+i*na, 225.0+i*na);
242  }
243  fl_color(shade_color(g[(int)c[chalf]], bc));
244  fl_rectf(x+d, y, w-h+1, h+1);
245  fl_pie(x, y, h, h, 90.0, 270.0);
246  fl_pie(x+w-h, y, h, h, 270.0, 90.0);
247  } else {
248  int d = w/2;
249  const int na = 8;
250  for (i=0; i<chalf; i++, d--, x++, y++, w-=2, h-=2)
251  {
252  fl_color(shade_color(g[(int)c[i]], bc));
253  fl_pie(x, y, w, w, 45.0+i*na, 135.0+i*na);
254  fl_color(shade_color(g[c[i] - 2], bc));
255  fl_pie(x, y, w, w, 0.0, 45.0+i*na);
256  fl_yxline(x+w-1, y+d, y+h-d);
257  fl_pie(x, y+h-w, w, w, 315.0+i*na, 360.0);
258  fl_color(shade_color(g[(int)c[clen - i]], bc));
259  fl_pie(x, y+h-w, w, w, 225.0+i*na, 315.0+i*na);
260  fl_color(shade_color(g[c[clen - i] - 2], bc));
261  fl_pie(x, y+h-w, w, w, 180.0, 225.0+i*na);
262  fl_yxline(x, y+d, y+h-d);
263  fl_pie(x, y, w, w, 135.0+i*na, 180.0);
264  }
265  fl_color(shade_color(g[(int)c[chalf]], bc));
266  fl_rectf(x, y+d, w+1, h-w+1);
267  fl_pie(x, y, w, w, 0.0, 180.0);
268  fl_pie(x, y+h-w, w, w, 180.0, 360.0);
269  }
270 }
271 
272 
273 static void up_frame(int x, int y, int w, int h, Fl_Color c) {
274  frame_rect(x, y, w, h - 1, "KLDIIJLM", c);
275 }
276 
277 
278 static void narrow_thin_box(int x, int y, int w, int h, Fl_Color c) {
279  if (h<=0 || w<=0) return;
280  const uchar *g = fl_gray_ramp();
281  fl_color(shade_color(g[(int)'R'], c));
282  fl_rectf(x+1, y+1, w-2, h-2);
283  fl_color(shade_color(g[(int)'I'], c));
284  if (w > 1) {
285  fl_xyline(x+1, y, x+w-2);
286  fl_xyline(x+1, y+h-1, x+w-2);
287  }
288  if (h > 1) {
289  fl_yxline(x, y+1, y+h-2);
290  fl_yxline(x+w-1, y+1, y+h-2);
291  }
292 }
293 
294 
295 static void thin_up_box(int x, int y, int w, int h, Fl_Color c) {
296 #ifdef USE_OLD_PLASTIC_BOX
297  shade_rect(x + 2, y + 2, w - 4, h - 5, "RVQNOPQRSTUVWVQ", c);
298  up_frame(x, y, w, h, c);
299 #else
300  if (w > 4 && h > 4) {
301  shade_rect(x + 1, y + 1, w - 2, h - 3, "RQOQSUWQ", c);
302  frame_rect(x, y, w, h - 1, "IJLM", c);
303  } else {
304  narrow_thin_box(x, y, w, h, c);
305  }
306 #endif // USE_OLD_PLASTIC_BOX
307 }
308 
309 
310 static void up_box(int x, int y, int w, int h, Fl_Color c) {
311 #ifdef USE_OLD_PLASTIC_BOX
312  shade_rect(x + 2, y + 2, w - 4, h - 5, "RVQNOPQRSTUVWVQ", c);
313  up_frame(x, y, w, h, c);
314 #else
315  if (w > 8 && h > 8) {
316  shade_rect(x + 1, y + 1, w - 2, h - 3, "RVQNOPQRSTUVWVQ", c);
317  frame_rect(x, y, w, h - 1, "IJLM", c);
318  } else {
319  thin_up_box(x, y, w, h, c);
320  }
321 #endif // USE_OLD_PLASTIC_BOX
322 }
323 
324 
325 static void up_round(int x, int y, int w, int h, Fl_Color c) {
326  shade_round(x, y, w, h, "RVQNOPQRSTUVWVQ", c);
327  frame_round(x, y, w, h, "IJLM", c);
328 }
329 
330 
331 static void down_frame(int x, int y, int w, int h, Fl_Color c) {
332  frame_rect(x, y, w, h - 1, "LLLLTTRR", c);
333 }
334 
335 
336 static void down_box(int x, int y, int w, int h, Fl_Color c) {
337  if (w > 6 && h > 6) {
338  shade_rect(x + 2, y + 2, w - 4, h - 5, "STUVWWWVT", c);
339  down_frame(x, y, w, h, c);
340  }
341  else {
342  narrow_thin_box(x, y, w, h, c);
343  }
344 }
345 
346 
347 static void down_round(int x, int y, int w, int h, Fl_Color c) {
348  shade_round(x, y, w, h, "STUVWWWVT", c);
349  frame_round(x, y, w, h, "IJLM", c);
350 }
351 
352 
354 
355 
365 
366  return _FL_PLASTIC_UP_BOX;
367 }
368 
369 
370 //
371 // End of "$Id$".
372 //
Fl::draw_box_active
static int draw_box_active()
Definition: fl_boxtype.cxx:58
down_frame
static void down_frame(int x, int y, int w, int h, Fl_Color c)
Definition: fl_plastic.cxx:331
Fl_Box_Draw_F
void() Fl_Box_Draw_F(int x, int y, int w, int h, Fl_Color color)
Definition: Fl.H:89
Fl.H
fl_line
void fl_line(int x, int y, int x1, int y1)
Definition: fl_draw.H:223
fl_rectf
void fl_rectf(int x, int y, int w, int h)
Definition: fl_draw.H:206
Fl_Color
unsigned int Fl_Color
Definition: Enumerations.H:934
_FL_PLASTIC_UP_FRAME
plastic version of FL_UP_FRAME
Definition: Enumerations.H:637
Fl_Boxtype
Fl_Boxtype
Definition: Enumerations.H:603
fl_rgb_color
Fl_Color fl_rgb_color(uchar r, uchar g, uchar b)
Definition: Enumerations.H:997
fl_color
void fl_color(Fl_Color c)
Definition: fl_draw.H:52
shade_color
Fl_Color shade_color(uchar gc, Fl_Color bc)
Definition: fl_plastic.cxx:40
fl_point
void fl_point(int x, int y)
Definition: fl_draw.H:147
fl_gray_ramp
const uchar * fl_gray_ramp()
Definition: fl_boxtype.cxx:60
narrow_thin_box
static void narrow_thin_box(int x, int y, int w, int h, Fl_Color c)
Definition: fl_plastic.cxx:278
b
long b
Definition: jpegint.h:397
_FL_PLASTIC_DOWN_BOX
plastic version of FL_DOWN_BOX
Definition: Enumerations.H:636
up_round
static void up_round(int x, int y, int w, int h, Fl_Color c)
Definition: fl_plastic.cxx:325
Fl::get_color
static unsigned get_color(Fl_Color i)
Definition: fl_color.cxx:359
up_frame
static void up_frame(int x, int y, int w, int h, Fl_Color c)
Definition: fl_plastic.cxx:273
frame_rect
static void frame_rect(int x, int y, int w, int h, const char *c, Fl_Color bc)
Definition: fl_plastic.cxx:73
fl_xyline
void fl_xyline(int x, int y, int x1)
Definition: fl_draw.H:255
fl_draw.H
utility header to pull drawing functions together
_FL_PLASTIC_UP_BOX
plastic version of FL_UP_BOX
Definition: Enumerations.H:635
fl_internal_boxtype
void fl_internal_boxtype(Fl_Boxtype, Fl_Box_Draw_F *)
Definition: fl_boxtype.cxx:404
fl_arc
void fl_arc(int x, int y, int w, int h, double a1, double a2)
Definition: fl_draw.H:304
fl_define_FL_PLASTIC_UP_BOX
Fl_Boxtype fl_define_FL_PLASTIC_UP_BOX()
Definition: fl_plastic.cxx:356
_FL_PLASTIC_DOWN_FRAME
plastic version of FL_DOWN_FRAME
Definition: Enumerations.H:638
gc
GC gc
Definition: test.c:72
fl_pie
void fl_pie(int x, int y, int w, int h, double a1, double a2)
Definition: fl_draw.H:317
fl_yxline
void fl_yxline(int x, int y, int y1)
Definition: fl_draw.H:270
down_round
static void down_round(int x, int y, int w, int h, Fl_Color c)
Definition: fl_plastic.cxx:347
thin_up_box
static void thin_up_box(int x, int y, int w, int h, Fl_Color c)
Definition: fl_plastic.cxx:295
down_box
static void down_box(int x, int y, int w, int h, Fl_Color c)
Definition: fl_plastic.cxx:336
shade_round
static void shade_round(int x, int y, int w, int h, const char *c, Fl_Color bc)
Definition: fl_plastic.cxx:219
frame_round
static void frame_round(int x, int y, int w, int h, const char *c, Fl_Color bc)
Definition: fl_plastic.cxx:93
x
int x
Definition: test.c:73
shade_rect
static void shade_rect(int x, int y, int w, int h, const char *c, Fl_Color bc)
Definition: fl_plastic.cxx:147
y
int y
Definition: test.c:74
_FL_PLASTIC_THIN_DOWN_BOX
plastic version of FL_THIN_DOWN_BOX
Definition: Enumerations.H:640
FL_GRAY
#define FL_GRAY
Definition: Enumerations.H:978
f
Fl_Box_Draw_F * f
Definition: fl_boxtype.cxx:285
flstring.h
_FL_PLASTIC_ROUND_DOWN_BOX
plastic version of FL_ROUND_DOWN_BOX
Definition: Enumerations.H:642
fl_color_average
Fl_Color fl_color_average(Fl_Color c1, Fl_Color c2, float weight)
Definition: fl_color.cxx:402
up_box
static void up_box(int x, int y, int w, int h, Fl_Color c)
Definition: fl_plastic.cxx:310
_FL_PLASTIC_THIN_UP_BOX
plastic version of FL_THIN_UP_BOX
Definition: Enumerations.H:639
uchar
unsigned char uchar
Definition: fl_types.h:30
_FL_PLASTIC_ROUND_UP_BOX
plastic version of FL_ROUND_UP_BOX
Definition: Enumerations.H:641