1 //
2 // "$Id: Fl_Dial.cxx 5472 2006-09-20 03:03:14Z mike $"
3 //
4 // Circular dial widget for the Fast Light Tool Kit (FLTK).
5 //
6 // Copyright 1998-2006 by Bill Spitzak and others.
7 //
8 // This library is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU Library General Public
10 // License as published by the Free Software Foundation; either
11 // version 2 of the License, or (at your option) any later version.
12 //
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 // Library General Public License for more details.
17 //
18 // You should have received a copy of the GNU Library General Public
19 // License along with this library; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
21 // USA.
22 //
23 // Please report all bugs and problems on the following page:
24 //
25 //     http://www.fltk.org/str.php
26 //
27 
28 #include <FL/Fl.H>
29 #include <FL/Fl_Dial.H>
30 #include <FL/fl_draw.H>
31 #include <stdlib.h>
32 #include <FL/math.h>
33 
34 // All angles are measured with 0 to the right and counter-clockwise
35 
draw(int X,int Y,int W,int H)36 void Fl_Dial::draw(int X, int Y, int W, int H) {
37   if (damage()&FL_DAMAGE_ALL) draw_box(box(), X, Y, W, H, color());
38   X += Fl::box_dx(box());
39   Y += Fl::box_dy(box());
40   W -= Fl::box_dw(box());
41   H -= Fl::box_dh(box());
42   double angle = (a2-a1)*(value()-minimum())/(maximum()-minimum()) + a1;
43   if (type() == FL_FILL_DIAL) {
44     // foo: draw this nicely in certain round box types
45     int foo = (box() > _FL_ROUND_UP_BOX && Fl::box_dx(box()));
46     if (foo) {X--; Y--; W+=2; H+=2;}
47     if (active_r()) fl_color(color());
48     else fl_color(fl_inactive(color()));
49     fl_pie(X, Y, W, H, 270-a1, angle > a1 ? 360+270-angle : 270-360-angle);
50     if (active_r()) fl_color(selection_color());
51     else fl_color(fl_inactive(selection_color()));
52     fl_pie(X, Y, W, H, 270-angle, 270-a1);
53     if (foo) {
54       if (active_r()) fl_color(FL_FOREGROUND_COLOR);
55       else fl_color(fl_inactive(FL_FOREGROUND_COLOR));
56       fl_arc(X, Y, W, H, 0, 360);
57     }
58     return;
59   }
60   if (!(damage()&FL_DAMAGE_ALL)) {
61     if (active_r()) fl_color(color());
62     else fl_color(fl_inactive(color()));
63     fl_pie(X+1, Y+1, W-2, H-2, 0, 360);
64   }
65   fl_push_matrix();
66   fl_translate(X+W/2-.5, Y+H/2-.5);
67   fl_scale(W-1, H-1);
68   fl_rotate(45-angle);
69   if (active_r()) fl_color(selection_color());
70   else fl_color(fl_inactive(selection_color()));
71   if (type()) { // FL_LINE_DIAL
72     fl_begin_polygon();
73     fl_vertex(0.0,   0.0);
74     fl_vertex(-0.04, 0.0);
75     fl_vertex(-0.25, 0.25);
76     fl_vertex(0.0,   0.04);
77     fl_end_polygon();
78     if (active_r()) fl_color(FL_FOREGROUND_COLOR);
79     else fl_color(fl_inactive(FL_FOREGROUND_COLOR));
80     fl_begin_loop();
81     fl_vertex(0.0,   0.0);
82     fl_vertex(-0.04, 0.0);
83     fl_vertex(-0.25, 0.25);
84     fl_vertex(0.0,   0.04);
85     fl_end_loop();
86   } else {
87     fl_begin_polygon(); fl_circle(-0.20, 0.20, 0.07); fl_end_polygon();
88     if (active_r()) fl_color(FL_FOREGROUND_COLOR);
89     else fl_color(fl_inactive(FL_FOREGROUND_COLOR));
90     fl_begin_loop(); fl_circle(-0.20, 0.20, 0.07); fl_end_loop();
91   }
92   fl_pop_matrix();
93 }
94 
draw()95 void Fl_Dial::draw() {
96   draw(x(), y(), w(), h());
97   draw_label();
98 }
99 
handle(int event,int X,int Y,int W,int H)100 int Fl_Dial::handle(int event, int X, int Y, int W, int H) {
101   switch (event) {
102   case FL_PUSH:
103     handle_push();
104   case FL_DRAG: {
105     int mx = (Fl::event_x()-X-W/2)*H;
106     int my = (Fl::event_y()-Y-H/2)*W;
107     if (!mx && !my) return 1;
108     double angle = 270-atan2((float)-my, (float)mx)*180/M_PI;
109     double oldangle = (a2-a1)*(value()-minimum())/(maximum()-minimum()) + a1;
110     while (angle < oldangle-180) angle += 360;
111     while (angle > oldangle+180) angle -= 360;
112     double val;
113     if ((a1<a2) ? (angle <= a1) : (angle >= a1)) {
114       val = minimum();
115     } else if ((a1<a2) ? (angle >= a2) : (angle <= a2)) {
116       val = maximum();
117     } else {
118       val = minimum() + (maximum()-minimum())*(angle-a1)/(a2-a1);
119     }
120     handle_drag(clamp(round(val)));
121   } return 1;
122   case FL_RELEASE:
123     handle_release();
124     return 1;
125   case FL_ENTER :
126   case FL_LEAVE :
127     return 1;
128   default:
129     return 0;
130   }
131 }
132 
handle(int e)133 int Fl_Dial::handle(int e) {
134   return handle(e, x(), y(), w(), h());
135 }
136 
Fl_Dial(int X,int Y,int W,int H,const char * l)137 Fl_Dial::Fl_Dial(int X, int Y, int W, int H, const char* l)
138   : Fl_Valuator(X, Y, W, H, l) {
139   box(FL_OVAL_BOX);
140   selection_color(FL_INACTIVE_COLOR); // was 37
141   a1 = 45;
142   a2 = 315;
143 }
144 
145 //
146 // End of "$Id: Fl_Dial.cxx 5472 2006-09-20 03:03:14Z mike $".
147 //
148