1 
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 /*******************************************************************************/
19 
20 #include <FL/Fl_Dial.H>
21 
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>
28 
29 #include <FL/Fl_Shared_Image.H>
30 
31 class image_node {
32 
33 public:
34 
35     Fl_Image *original;                                     /* originl image */
36 
37     Fl_Image *scaled;                                        /* downscaled image */
38 
39     class image_node *next;
40 };
41 
42 static image_node *_first = 0;
43 
44 int Fl_Dial::_default_style = Fl_Dial::PLASTIC_DIAL;
45 Fl_Image *Fl_Dial::_default_image = 0;
46 
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
burnished_oval_box(int x,int y,int w,int h,Fl_Color c)51 burnished_oval_box ( int x, int y, int w, int h, Fl_Color c )
52 {
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 );
58 
59     /* shrink */
60     x += 4;
61     y += 4;
62     w -= 7;
63     h -= 7;
64 
65     /* draw cap */
66     fl_color( c );
67     fl_pie( x, y, w, h, 0, 360 );
68 
69     /* draw glare */
70 
71     const int a1 = 10;
72     const int a2 = 90;
73 
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 ) );
78 
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) );
82 }
83 
84 
85 
86 
87 void
draw_box(void)88 Fl_Dial::draw_box ( void )
89 {
90 }
91 
92 static Fl_Widget *_mouse_inside = NULL;
93 
94 int
handle(int m)95 Fl_Dial::handle ( int m )
96 {
97     /* Fl_Dial and friends should really handle mousewheel, but they don't in FTLK1 */
98 
99     switch ( m )
100     {
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:
110         {
111             if ( this != Fl::belowmouse() )
112                 return 0;
113             if (Fl::e_dy==0)
114                 return 0;
115 
116             const int steps = Fl::event_ctrl() ? 128 : 16;
117 
118             const float step = fabs( maximum() - minimum() ) / (float)steps;
119 
120             int dy = Fl::e_dy;
121 
122             /* slider is in 'upside down' configuration, invert meaning of mousewheel */
123             if ( maximum() > minimum() )
124                 dy = 0 - dy;
125 
126             handle_drag(clamp(value() + step * dy));
127             return 1;
128         }
129     }
130 
131     int X, Y, S;
132 
133     get_knob_dimensions ( &X, &Y, &S );
134 
135     return Fl_Dial_Base::handle( m, X, Y, S, S );
136 }
137 
138 void
draw(void)139 Fl_Dial::draw ( void )
140 {
141     int X, Y, S;
142 
143     get_knob_dimensions ( &X, &Y, &S);
144 
145     draw_box();
146     draw_label();
147 
148     double angle = ( angle2() - angle1() ) * ( value() - minimum()) / ( maximum() - minimum() ) + angle1();
149 
150     int t = type();
151 
152     if ( t == PIXMAP_DIAL )
153     {
154         Fl_Image *im = pixmap();
155 
156         if ( !im )
157             im = Fl_Dial::_default_image;
158 
159         if ( im )
160         {
161             fl_push_clip( x(), y(), w(), h() );
162 
163             int knob_width = im->h();
164             const int frames = im->w() / im->h();
165 
166             const int index = (int)( ( frames - 1 ) * ( value() - minimum()) / ( maximum() - minimum() ));
167 
168             /* if ( ( damage() == FL_DAMAGE_ALL ) || */
169             /*      ( ( damage() & FL_DAMAGE_EXPOSE ) && */
170             /*        index != _last_pixmap_index ) ) */
171             {
172 
173                 /* FIXME: Why doesn't this work? */
174                 /* if ( ! active_r() ) */
175                 /* { */
176                 /*     im->inactive(); */
177                 /* } */
178 
179                 if ( w() >= knob_width )
180                 {
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 );
187                 }
188                 else
189                 {
190 //                    while ( knob_width > w() )
191                     knob_width = w();
192 
193                     image_node *i;
194 
195                     Fl_Image *scaled = 0;
196 
197                     for ( i = _first; i; i = i->next )
198                     {
199                         if ( i->original == im &&
200                              i->scaled && i->scaled->h() == knob_width )
201                         {
202                             scaled = i->scaled;
203                             break;
204                         }
205                     }
206 
207                     if ( ! scaled )
208                     {
209                         scaled = im->copy( knob_width * frames, knob_width );
210 
211                         i = new image_node();
212 
213                         i->original = im;
214                         i->scaled = scaled;
215                         i->next = _first;
216                         _first = i;
217                     }
218 
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 );
225                 }
226 
227                 _last_pixmap_index = index;
228             }
229 
230             fl_pop_clip();
231 
232             goto done;
233         }
234         else
235             /* draw as plastic dial instead when image is missing */
236             t = PLASTIC_DIAL;
237     }
238 
239     if ( t == ARC_DIAL )
240     {
241         /* fl_line_style( FL_SOLID, 0 ); */
242         if ( type() == ARC_DIAL )
243             fl_draw_box( box(), X, Y, S, S, color() );
244 
245         /* shrink a bit */
246         X += S / 16.0;
247         Y += S / 16.0;
248         S -= S / 8;
249 
250         fl_line_style( FL_SOLID, S / 6 );
251 
252         /* background arc */
253         fl_color( fl_darker( color() ) );
254         fl_arc( X, Y, S, S, 270 - angle1(), 270 - angle2() );
255 
256         /* foreground arc */
257         fl_color( selection_color() );
258         fl_arc( X, Y, S, S, 270 - angle1(), 270 - angle  );
259 
260         fl_line_style( FL_SOLID, 0 );
261 
262         fl_color( fl_contrast( labelcolor(), color() ) );
263     }
264     else if ( t == PLASTIC_DIAL || t == BURNISHED_DIAL )
265     {
266         draw_knob(t);
267 
268         draw_cursor( X, Y, S);
269     }
270 
271 done:
272 
273     if ( _mouse_inside == this )
274     {
275         /* TODO: Make this optional */
276         char s[128];
277 
278         fl_font( FL_HELVETICA, 10 );
279 
280         char buf[128];
281         format(buf);
282 
283         snprintf( s, sizeof( s ), buf, value()  );
284 
285         fl_color( FL_FOREGROUND_COLOR );
286         fl_draw( s, X, Y, S, S, FL_ALIGN_CENTER );
287     }
288 }
289 
290 void
get_knob_dimensions(int * X,int * Y,int * S)291 Fl_Dial::get_knob_dimensions ( int *X, int *Y, int *S )
292 {
293     int ox, oy, ww, hh, side;
294     ox = x();
295     oy = y();
296     ww = w();
297     hh = h();
298 
299     if (ww > hh)
300     {
301         side = hh;
302         ox = ox + (ww - side) / 2;
303     }
304     else
305     {
306         side = ww;
307         oy = oy + (hh - side) / 2;
308     }
309     side = w() > h() ? hh : ww;
310 
311     *X = ox;
312     *Y = oy;
313     *S = side;
314 }
315 
316 void
draw_cursor(int ox,int oy,int side)317 Fl_Dial::draw_cursor ( int ox, int oy, int side )
318 {
319     double angle;
320 
321 //    fl_color(fl_color_average(FL_BACKGROUND_COLOR, FL_BLACK, .7f));
322 
323     angle = ( angle2() - angle1() ) * ( value() - minimum()) / ( maximum() - minimum() ) + angle1();
324 
325     fl_color( fl_contrast( selection_color(), FL_BACKGROUND_COLOR ) );
326 
327     fl_line_style( FL_SOLID, side / 8 );
328 
329     const int d = 6;
330 
331     /* account for edge conditions */
332     angle = angle < angle1() + d ? angle1() + d : angle;
333     angle = angle > angle2() - d ? angle2() - d : angle;
334 
335     ox += side * 0.15;
336     oy += side * 0.15;
337     side -= side * 0.15 * 2;
338 
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) );
341 
342     fl_line_style( FL_SOLID, 0 );
343 }
344 
345 void
draw_knob(int type)346 Fl_Dial::draw_knob ( int type )
347 {
348     int ox, oy, ww, hh, side;
349 
350     get_knob_dimensions ( &ox, &oy, &side );
351 
352     ww = w();
353     hh = h();
354     draw_label();
355     fl_clip(ox, oy, ww, hh);
356 
357     int o = side * 0.15;
358 
359     // background
360     /* fl_color(FL_BACKGROUND_COLOR); */
361     /* fl_rectf(ox, oy, side, side); */
362 
363     /* scale color */
364     if ( damage() & FL_DAMAGE_ALL )
365     {
366         fl_color(fl_color_average(color(), FL_BACKGROUND2_COLOR, .6));
367 
368         fl_pie(ox + 1, oy + 3, side - 2, side - 12, 0, 360);
369 
370         // scale
371 
372         draw_scale(ox, oy, side);
373     }
374 
375     Fl_Color c = active_r() ? fl_color_average(FL_BACKGROUND_COLOR, FL_WHITE, .7) : FL_INACTIVE_COLOR;
376 
377     if ( type == BURNISHED_DIAL )
378     {
379         burnished_oval_box( ox + o, oy + o, side - (o*2), side - (o*2), c );
380     }
381     else
382     {
383 
384         fl_color(FL_BACKGROUND_COLOR);
385 
386         fl_pie(ox + o, oy + o, side - (o*2), side - (o*2), 0, 360);
387 
388         // shadow
389 
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); */
394 
395         // knob edge
396         fl_color( c);
397 
398         fl_arc(ox + o, oy + o, side - o*2, side - o*2, 0, 360);
399 
400         fl_color(fl_color_average(FL_BACKGROUND_COLOR, FL_WHITE, .6));
401 
402         fl_pie(ox + o, oy + o, side - o*2, side - o*2, 0, 360);
403 
404     }
405     fl_pop_clip();
406 }
407 
408 
409 void
draw_scale(int ox,int oy,int side)410 Fl_Dial::draw_scale ( int ox, int oy, int side )
411 {
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++)
421     {
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);
431     }
432 }
433 
434 void
scaleticks(int tck)435 Fl_Dial::scaleticks ( int tck )
436 {
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);
444 }
445