2 // "$Id: Fl_Dial_Base.cxx 7903 2010-11-28 21:06:39Z matt $"
4 // Circular dial widget for the Fast Light Tool Kit (FLTK).
6 // Copyright 1998-2010 by Bill Spitzak and others.
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.
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.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23 // Please report all bugs and problems on the following page:
25 // http://www.fltk.org/str.php
29 #include <FL/Fl_Dial_Base.H>
30 #include <FL/fl_draw.H>
34 // All angles are measured with 0 to the right and counter-clockwise
36 Draws dial at given position and size.
37 \param[in] X, Y, W, H position and size
39 void Fl_Dial_Base::draw(int X
, int Y
, int W
, int H
) {
40 if (damage()&FL_DAMAGE_ALL
) draw_box(box(), X
, Y
, W
, H
, color());
41 X
+= Fl::box_dx(box());
42 Y
+= Fl::box_dy(box());
43 W
-= Fl::box_dw(box());
44 H
-= Fl::box_dh(box());
45 double angle
= (a2
-a1
)*(value()-minimum())/(maximum()-minimum()) + a1
;
46 if (type() == FL_FILL_DIAL
) {
47 // foo: draw this nicely in certain round box types
48 int foo
= (box() > _FL_ROUND_UP_BOX
&& Fl::box_dx(box()));
49 if (foo
) {X
--; Y
--; W
+=2; H
+=2;}
50 if (active_r()) fl_color(color());
51 else fl_color(fl_inactive(color()));
52 fl_pie(X
, Y
, W
, H
, 270-a1
, angle
> a1
? 360+270-angle
: 270-360-angle
);
53 if (active_r()) fl_color(selection_color());
54 else fl_color(fl_inactive(selection_color()));
55 fl_pie(X
, Y
, W
, H
, 270-angle
, 270-a1
);
57 if (active_r()) fl_color(FL_FOREGROUND_COLOR
);
58 else fl_color(fl_inactive(FL_FOREGROUND_COLOR
));
59 fl_arc(X
, Y
, W
, H
, 0, 360);
63 if (!(damage()&FL_DAMAGE_ALL
)) {
64 if (active_r()) fl_color(color());
65 else fl_color(fl_inactive(color()));
66 fl_pie(X
+1, Y
+1, W
-2, H
-2, 0, 360);
69 fl_translate(X
+W
/2-.5, Y
+H
/2-.5);
72 if (active_r()) fl_color(selection_color());
73 else fl_color(fl_inactive(selection_color()));
74 if (type()) { // FL_LINE_DIAL
77 fl_vertex(-0.04, 0.0);
78 fl_vertex(-0.25, 0.25);
81 if (active_r()) fl_color(FL_FOREGROUND_COLOR
);
82 else fl_color(fl_inactive(FL_FOREGROUND_COLOR
));
85 fl_vertex(-0.04, 0.0);
86 fl_vertex(-0.25, 0.25);
90 fl_begin_polygon(); fl_circle(-0.20, 0.20, 0.07); fl_end_polygon();
91 if (active_r()) fl_color(FL_FOREGROUND_COLOR
);
92 else fl_color(fl_inactive(FL_FOREGROUND_COLOR
));
93 fl_begin_loop(); fl_circle(-0.20, 0.20, 0.07); fl_end_loop();
99 Draws dial at current position and size.
101 void Fl_Dial_Base::draw() {
102 draw(x(), y(), w(), h());
107 Allows subclasses to handle event based on given position and size.
108 \param[in] event, X, Y, W, H event to handle, related position and size.
110 int Fl_Dial_Base::handle(int event
, int X
, int Y
, int W
, int H
) {
113 Fl_Widget_Tracker
wp(this);
115 if (wp
.deleted()) return 1; }
117 int mx
= (Fl::event_x()-X
-W
/2)*H
;
118 int my
= (Fl::event_y()-Y
-H
/2)*W
;
119 if (!mx
&& !my
) return 1;
120 double angle
= 270-atan2((float)-my
, (float)mx
)*180/M_PI
;
121 double oldangle
= (a2
-a1
)*(value()-minimum())/(maximum()-minimum()) + a1
;
122 while (angle
< oldangle
-180) angle
+= 360;
123 while (angle
> oldangle
+180) angle
-= 360;
125 if ((a1
<a2
) ? (angle
<= a1
) : (angle
>= a1
)) {
127 } else if ((a1
<a2
) ? (angle
>= a2
) : (angle
<= a2
)) {
130 val
= minimum() + (maximum()-minimum())*(angle
-a1
)/(a2
-a1
);
132 handle_drag(clamp(round(val
)));
137 case FL_ENTER
: /* FALLTHROUGH */
146 Allow subclasses to handle event based on current position and size.
148 int Fl_Dial_Base::handle(int e
) {
149 return handle(e
, x(), y(), w(), h());
152 Fl_Dial_Base::Fl_Dial_Base(int X
, int Y
, int W
, int H
, const char* l
)
154 Creates a new Fl_Dial_Base widget using the given position, size,
155 and label string. The default type is FL_NORMAL_DIAL.
157 : Fl_Valuator(X
, Y
, W
, H
, l
) {
159 selection_color(FL_INACTIVE_COLOR
); // was 37
165 // End of "$Id: Fl_Dial_Base.cxx 7903 2010-11-28 21:06:39Z matt $".