themes: Workaround for bug where a background color of RGB 0,0,0 in Black color schem...
[ntk.git] / src / Fl_Dial.cxx
blobadc9b6dc09753b9186664a9ad4852e2b67e6f3b6
2 /*******************************************************************************/
3 /* Copyright (C) 2008 Jonathan Moore Liles */
4 /* */
5 /* This program is free software; you can redistribute it and/or modify it */
6 /* under the terms of the GNU General Public License as published by the */
7 /* Free Software Foundation; either version 2 of the License, or (at your */
8 /* option) any later version. */
9 /* */
10 /* This program is distributed in the hope that it will be useful, but WITHOUT */
11 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
12 /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for */
13 /* more details. */
14 /* */
15 /* You should have received a copy of the GNU General Public License along */
16 /* with This program; see the file COPYING. If not,write to the Free Software */
17 /* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 /*******************************************************************************/
20 #include <FL/Fl_Dial.H>
22 #include <FL/fl_draw.H>
23 #include <FL/Fl.H>
24 #include <string.h>
25 #include <stdio.h>
26 #include <math.h>
27 #include <algorithm>
29 #include <FL/Fl_Shared_Image.H>
31 class image_node {
33 public:
35 Fl_Image *original; /* originl image */
37 Fl_Image *scaled; /* downscaled image */
39 class image_node *next;
42 static image_node *_first = 0;
44 int Fl_Dial::_default_style = Fl_Dial::PLASTIC_DIAL;
45 Fl_Image *Fl_Dial::_default_image = 0;
47 /** This simple box is suitable for use with knob-type widgets. It
48 * comprises a border with shadow, and a cap with glare-lines akin
49 * to those seen on burnished aluminum knobs. */
50 static void
51 burnished_oval_box ( int x, int y, int w, int h, Fl_Color c )
53 /* draw background */
54 fl_color( fl_darker( c ) );
55 fl_pie( x, y, w, h, 0, 360 );
56 fl_color( fl_darker( fl_darker( c ) ) );
57 fl_pie( x, y, w, h, 180 + 215, 180 + 45 );
59 /* shrink */
60 x += 4;
61 y += 4;
62 w -= 7;
63 h -= 7;
65 /* draw cap */
66 fl_color( c );
67 fl_pie( x, y, w, h, 0, 360 );
69 /* draw glare */
71 const int a1 = 10;
72 const int a2 = 90;
74 fl_color( fl_color_average( FL_WHITE, c, 0.15f ) );
75 fl_pie( x, y, w, h, a1, a2 );
76 fl_pie( x, y, w, h, 180 + a1, 180 + a2 );
77 fl_color( fl_color_average( FL_WHITE, c, 0.25f ) );
79 const int d = (a2 - a1) / 2;
80 fl_pie( x, y, w, h, a1 + (d / 2), a2 - (d / 2) );
81 fl_pie( x, y, w, h, 180 + a1 + (d / 2), 180 + a2 - (d / 2) );
87 void
88 Fl_Dial::draw_box ( void )
92 static Fl_Widget *_mouse_inside = NULL;
94 int
95 Fl_Dial::handle ( int m )
97 /* Fl_Dial and friends should really handle mousewheel, but they don't in FTLK1 */
99 switch ( m )
101 case FL_ENTER:
102 _mouse_inside = this;
103 redraw();
104 return Fl_Dial_Base::handle(m) || 1;
105 case FL_LEAVE:
106 _mouse_inside = NULL;
107 redraw();
108 return Fl_Dial_Base::handle(m) || 1;
109 case FL_MOUSEWHEEL:
111 if ( this != Fl::belowmouse() )
112 return 0;
113 if (Fl::e_dy==0)
114 return 0;
116 const int steps = Fl::event_ctrl() ? 128 : 16;
118 const float step = fabs( maximum() - minimum() ) / (float)steps;
120 int dy = Fl::e_dy;
122 /* slider is in 'upside down' configuration, invert meaning of mousewheel */
123 if ( maximum() > minimum() )
124 dy = 0 - dy;
126 handle_drag(clamp(value() + step * dy));
127 return 1;
131 int X, Y, S;
133 get_knob_dimensions ( &X, &Y, &S );
135 return Fl_Dial_Base::handle( m, X, Y, S, S );
138 void
139 Fl_Dial::draw ( void )
141 int X, Y, S;
143 get_knob_dimensions ( &X, &Y, &S);
145 draw_box();
146 draw_label();
148 double angle = ( angle2() - angle1() ) * ( value() - minimum()) / ( maximum() - minimum() ) + angle1();
150 int t = type();
152 if ( t == PIXMAP_DIAL )
154 Fl_Image *im = pixmap();
156 if ( !im )
157 im = Fl_Dial::_default_image;
159 if ( im )
161 fl_push_clip( x(), y(), w(), h() );
163 int knob_width = im->h();
164 const int frames = im->w() / im->h();
166 const int index = (int)( ( frames - 1 ) * ( value() - minimum()) / ( maximum() - minimum() ));
168 /* if ( ( damage() == FL_DAMAGE_ALL ) || */
169 /* ( ( damage() & FL_DAMAGE_EXPOSE ) && */
170 /* index != _last_pixmap_index ) ) */
173 /* FIXME: Why doesn't this work? */
174 /* if ( ! active_r() ) */
175 /* { */
176 /* im->inactive(); */
177 /* } */
179 if ( w() >= knob_width )
181 im->draw( x() + ( w() / 2 ) - ( knob_width / 2 ),
182 y() + ( h() / 2 ) - ( knob_width / 2 ),
183 knob_width,
184 knob_width,
185 knob_width * index,
186 0 );
188 else
190 // while ( knob_width > w() )
191 knob_width = w();
193 image_node *i;
195 Fl_Image *scaled = 0;
197 for ( i = _first; i; i = i->next )
199 if ( i->original == im &&
200 i->scaled && i->scaled->h() == knob_width )
202 scaled = i->scaled;
203 break;
207 if ( ! scaled )
209 scaled = im->copy( knob_width * frames, knob_width );
211 i = new image_node();
213 i->original = im;
214 i->scaled = scaled;
215 i->next = _first;
216 _first = i;
219 scaled->draw( x() + ( w() / 2 ) - ( knob_width / 2 ),
220 y() + ( h() / 2 ) - ( knob_width / 2 ),
221 knob_width,
222 knob_width,
223 knob_width * index,
224 0 );
227 _last_pixmap_index = index;
230 fl_pop_clip();
232 goto done;
234 else
235 /* draw as plastic dial instead when image is missing */
236 t = PLASTIC_DIAL;
239 if ( t == ARC_DIAL )
241 /* fl_line_style( FL_SOLID, 0 ); */
242 if ( type() == ARC_DIAL )
243 fl_draw_box( box(), X, Y, S, S, color() );
245 /* shrink a bit */
246 X += S / 16.0;
247 Y += S / 16.0;
248 S -= S / 8;
250 fl_line_style( FL_SOLID, S / 6 );
252 /* background arc */
253 fl_color( fl_darker( color() ) );
254 fl_arc( X, Y, S, S, 270 - angle1(), 270 - angle2() );
256 /* foreground arc */
257 fl_color( selection_color() );
258 fl_arc( X, Y, S, S, 270 - angle1(), 270 - angle );
260 fl_line_style( FL_SOLID, 0 );
262 fl_color( fl_contrast( labelcolor(), color() ) );
264 else if ( t == PLASTIC_DIAL || t == BURNISHED_DIAL )
266 draw_knob(t);
268 draw_cursor( X, Y, S);
271 done:
273 if ( _mouse_inside == this )
275 /* TODO: Make this optional */
276 char s[128];
278 fl_font( FL_HELVETICA, 10 );
280 char buf[128];
281 format(buf);
283 snprintf( s, sizeof( s ), buf, value() );
285 fl_color( FL_FOREGROUND_COLOR );
286 fl_draw( s, X, Y, S, S, FL_ALIGN_CENTER );
290 void
291 Fl_Dial::get_knob_dimensions ( int *X, int *Y, int *S )
293 int ox, oy, ww, hh, side;
294 ox = x();
295 oy = y();
296 ww = w();
297 hh = h();
299 if (ww > hh)
301 side = hh;
302 ox = ox + (ww - side) / 2;
304 else
306 side = ww;
307 oy = oy + (hh - side) / 2;
309 side = w() > h() ? hh : ww;
311 *X = ox;
312 *Y = oy;
313 *S = side;
316 void
317 Fl_Dial::draw_cursor ( int ox, int oy, int side )
319 double angle;
321 // fl_color(fl_color_average(FL_BACKGROUND_COLOR, FL_BLACK, .7f));
323 angle = ( angle2() - angle1() ) * ( value() - minimum()) / ( maximum() - minimum() ) + angle1();
325 fl_color( fl_contrast( selection_color(), FL_BACKGROUND_COLOR ) );
327 fl_line_style( FL_SOLID, side / 8 );
329 const int d = 6;
331 /* account for edge conditions */
332 angle = angle < angle1() + d ? angle1() + d : angle;
333 angle = angle > angle2() - d ? angle2() - d : angle;
335 ox += side * 0.15;
336 oy += side * 0.15;
337 side -= side * 0.15 * 2;
339 fl_arc( ox, oy, side, side, 270 - (angle - d), 270 - (angle + d) );
340 // fl_arc( ox, oy, side, side, 270 - (angle + d), 270 - (angle - d) );
342 fl_line_style( FL_SOLID, 0 );
345 void
346 Fl_Dial::draw_knob ( int type )
348 int ox, oy, ww, hh, side;
350 get_knob_dimensions ( &ox, &oy, &side );
352 ww = w();
353 hh = h();
354 draw_label();
355 fl_clip(ox, oy, ww, hh);
357 int o = side * 0.15;
359 // background
360 /* fl_color(FL_BACKGROUND_COLOR); */
361 /* fl_rectf(ox, oy, side, side); */
363 /* scale color */
364 if ( damage() & FL_DAMAGE_ALL )
366 fl_color(fl_color_average(color(), FL_BACKGROUND2_COLOR, .6));
368 fl_pie(ox + 1, oy + 3, side - 2, side - 12, 0, 360);
370 // scale
372 draw_scale(ox, oy, side);
375 Fl_Color c = active_r() ? fl_color_average(FL_BACKGROUND_COLOR, FL_WHITE, .7) : FL_INACTIVE_COLOR;
377 if ( type == BURNISHED_DIAL )
379 burnished_oval_box( ox + o, oy + o, side - (o*2), side - (o*2), c );
381 else
384 fl_color(FL_BACKGROUND_COLOR);
386 fl_pie(ox + o, oy + o, side - (o*2), side - (o*2), 0, 360);
388 // shadow
390 fl_color(fl_color_average(FL_BACKGROUND_COLOR, FL_BLACK, .8f));
391 fl_pie(ox + o + 2, oy + o + 3, side - o*2, side - o*2, 0, 360);
392 /* fl_color(fl_color_average(FL_BACKGROUND_COLOR, FL_BLACK, .2f)); */
393 /* fl_pie(ox + o + 4, oy + o + 5, side - o*2, side - o*2, 0, 360); */
395 // knob edge
396 fl_color( c);
398 fl_arc(ox + o, oy + o, side - o*2, side - o*2, 0, 360);
400 fl_color(fl_color_average(FL_BACKGROUND_COLOR, FL_WHITE, .6));
402 fl_pie(ox + o, oy + o, side - o*2, side - o*2, 0, 360);
405 fl_pop_clip();
409 void
410 Fl_Dial::draw_scale ( int ox, int oy, int side )
412 float x1, y1, x2, y2, rds, cx, cy, ca, sa;
413 rds = side / 2;
414 cx = ox + side / 2;
415 cy = oy + side / 2;
416 if (_scaleticks == 0)
417 return;
418 double a_step = (10.0 * 3.14159 / 6.0) / _scaleticks;
419 double a_orig = -(3.14159 / 3.0);
420 for (int a = 0; a <= _scaleticks; a++)
422 double na = a_orig + a * a_step;
423 ca = cos(na);
424 sa = sin(na);
425 x1 = cx + (rds) * ca;
426 y1 = cy - (rds) * sa;
427 x2 = cx + (rds - 6) * ca;
428 y2 = cy - (rds - 6) * sa;
429 fl_color(FL_BACKGROUND_COLOR);
430 fl_line(x1, y1, x2, y2);
434 void
435 Fl_Dial::scaleticks ( int tck )
437 _scaleticks = tck;
438 if (_scaleticks < 0)
439 _scaleticks = 0;
440 if (_scaleticks > 31)
441 _scaleticks = 31;
442 if (visible())
443 damage(FL_DAMAGE_ALL);