summaryrefslogtreecommitdiff
path: root/src/fl_plastic.cxx
blob: 20570cfa11f11b14ae38ac5b5a3a45e0c4c07508 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
//
// "$Id: fl_plastic.cxx,v 1.1.2.19 2003/05/19 14:56:42 easysw Exp $"
//
// "Plastic" drawing routines for the Fast Light Tool Kit (FLTK).
//
// These box types provide a cross between Aqua and KDE buttons; kindof
// like translucent plastic buttons...
//
// Copyright 2001-2003 by Michael Sweet.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Library General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Library General Public License for more details.
//
// You should have received a copy of the GNU Library General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
// USA.
//
// Please report all bugs and problems to "fltk-bugs@fltk.org".
//

// Box drawing code for an obscure box type.
// These box types are in seperate files so they are not linked
// in if not used.

#include <FL/Fl.H>
#include <FL/fl_draw.H>
#include "flstring.h"

//
// Uncomment the following line to restore the old plastic box type
// appearance.
//

//#define USE_OLD_PLASTIC_BOX
#define USE_OLD_PLASTIC_COLOR

extern uchar *fl_gray_ramp();

inline Fl_Color shade_color(uchar gc, Fl_Color bc) {
#ifdef USE_OLD_PLASTIC_COLOR
  return fl_color_average((Fl_Color)gc, bc, 0.75f);
#else
  unsigned	grgb = Fl::get_color((Fl_Color)gc),
		brgb = Fl::get_color(bc);
  int		red, green, blue, gray;


  gray  = ((grgb >> 24) & 255);
  red   = gray * ((brgb >> 24) & 255) / 255 + gray * gray / 510;
  gray  = ((grgb >> 16) & 255);
  green = gray * ((brgb >> 16) & 255) / 255 + gray * gray / 510;
  gray  = ((grgb >> 8) & 255);
  blue  = gray * ((brgb >> 8) & 255) / 255 + gray * gray / 510;

  if (red > 255)
    red = 255;

  if (green > 255)
    green = 255;

  if (blue > 255)
    blue = 255;

  if (Fl::draw_box_active())
    return fl_rgb_color(red, green, blue);
  else
    return fl_color_average(FL_GRAY, fl_rgb_color(red, green, blue), 0.75f);
#endif // USE_OLD_PLASTIC_COLOR
}


static void shade_frame(int x, int y, int w, int h, const char *c, Fl_Color bc) {
  uchar *g = fl_gray_ramp();
  int b = strlen(c) / 4 + 1;

  for (x += b, y += b, w -= 2 * b, h -= 2 * b; b > 1; b --)
  {
    // Draw lines around the perimeter of the button, 4 colors per
    // circuit.
    fl_color(shade_color(g[*c++], bc));
    fl_line(x, y + h + b, x + w - 1, y + h + b, x + w + b - 1, y + h);
    fl_color(shade_color(g[*c++], bc));
    fl_line(x + w + b - 1, y + h, x + w + b - 1, y, x + w - 1, y - b);
    fl_color(shade_color(g[*c++], bc));
    fl_line(x + w - 1, y - b, x, y - b, x - b, y);
    fl_color(shade_color(g[*c++], bc));
    fl_line(x - b, y, x - b, y + h, x, y + h + b);
  }
}


static void shade_rect(int x, int y, int w, int h, const char *c, Fl_Color bc)
{
  uchar		*g = fl_gray_ramp();
  int		i, j;
  int		clen = strlen(c) - 1;
  int		chalf = clen / 2;
  int		cstep = 1;

  if (h < (w * 2)) {
    // Horizontal shading...
    if (clen >= h) cstep = 2;

    for (i = 0, j = 0; j < chalf; i ++, j += cstep) {
      // Draw the top line and points...
      fl_color(shade_color(g[c[i]], bc));
      fl_xyline(x + 1, y + i, x + w - 1);

      fl_color(shade_color(g[c[i] - 2], bc));
      fl_point(x, y + i + 1);
      fl_point(x + w - 1, y + i + 1);

      // Draw the bottom line and points...
      fl_color(shade_color(g[c[clen - i]], bc));
      fl_xyline(x + 1, y + h - i, x + w - 1);

      fl_color(shade_color(g[c[clen - i] - 2], bc));
      fl_point(x, y + h - i);
      fl_point(x + w - 1, y + h - i);
    }

    // Draw the interior and sides...
    i = chalf / cstep;

    fl_color(shade_color(g[c[chalf]], bc));
    fl_rectf(x + 1, y + i, w - 2, h - 2 * i + 1);

    fl_color(shade_color(g[c[chalf] - 2], bc));
    fl_yxline(x, y + i, y + h - i);
    fl_yxline(x + w - 1, y + i, y + h - i);
  } else {
    // Vertical shading...
    if (clen >= w) cstep = 2;

    for (i = 0, j = 0; j < chalf; i ++, j += cstep) {
      // Draw the left line and points...
      fl_color(shade_color(g[c[i]], bc));
      fl_yxline(x + i, y + 1, y + h - 1);

      fl_color(shade_color(g[c[i] - 2], bc));
      fl_point(x + i + 1, y);
      fl_point(x + i + 1, y + h - 1);

      // Draw the right line and points...
      fl_color(shade_color(g[c[clen - i]], bc));
      fl_yxline(x + w - 1 - i, y + 1, y + h - 1);

      fl_color(shade_color(g[c[clen - i] - 2], bc));
      fl_point(x + w - 1 - i, y);
      fl_point(x + w - 1 - i, y + h - 1);
    }

    // Draw the interior, top, and bottom...
    i = chalf / cstep;

    fl_color(shade_color(g[c[chalf]], bc));
    fl_rectf(x + i, y + 1, w - 2 * i, h - 2);

    fl_color(shade_color(g[c[chalf] - 2], bc));
    fl_xyline(x + i, y, x + w - i);
    fl_xyline(x + i, y + h - 1, x + w - i);
  }
}


static void up_frame(int x, int y, int w, int h, Fl_Color c) {
  shade_frame(x, y, w, h - 1, "KLDIIJLM", c);
}


static void up_box(int x, int y, int w, int h, Fl_Color c) {
#ifdef USE_OLD_PLASTIC_BOX
  shade_rect(x + 2, y + 2, w - 4, h - 5, "RVQNOPQRSTUVWVQ", c);
  up_frame(x, y, w, h, c);
#else
  shade_rect(x + 1, y + 1, w - 2, h - 3, "RVQNOPQRSTUVWVQ", c);
  shade_frame(x, y, w, h - 1, "IJLM", c);
#endif // USE_OLD_PLASTIC_BOX
}


static void thin_up_box(int x, int y, int w, int h, Fl_Color c) {
#ifdef USE_OLD_PLASTIC_BOX
  shade_rect(x + 2, y + 2, w - 4, h - 5, "RVQNOPQRSTUVWVQ", c);
  up_frame(x, y, w, h, c);
#else
  shade_rect(x + 1, y + 1, w - 2, h - 3, "RQOQSUWQ", c);
  shade_frame(x, y, w, h - 1, "IJLM", c);
#endif // USE_OLD_PLASTIC_BOX
}


static void down_frame(int x, int y, int w, int h, Fl_Color c) {
  shade_frame(x, y, w, h - 1, "LLRRTTLL", c);
}


static void down_box(int x, int y, int w, int h, Fl_Color c) {
  shade_rect(x + 2, y + 2, w - 4, h - 5, "STUVWWWVT", c);

  down_frame(x, y, w, h, c);
}


extern void fl_internal_boxtype(Fl_Boxtype, Fl_Box_Draw_F*);


Fl_Boxtype fl_define_FL_PLASTIC_UP_BOX() {
  fl_internal_boxtype(_FL_PLASTIC_UP_BOX, up_box);
  fl_internal_boxtype(_FL_PLASTIC_DOWN_BOX, down_box);
  fl_internal_boxtype(_FL_PLASTIC_UP_FRAME, up_frame);
  fl_internal_boxtype(_FL_PLASTIC_DOWN_FRAME, down_frame);
  fl_internal_boxtype(_FL_PLASTIC_THIN_UP_BOX, thin_up_box);
  fl_internal_boxtype(_FL_PLASTIC_THIN_DOWN_BOX, down_box);

  return _FL_PLASTIC_UP_BOX;
}


//
// End of "$Id: fl_plastic.cxx,v 1.1.2.19 2003/05/19 14:56:42 easysw Exp $".
//