blob: 2adaabbea6b92be26a184587179c73d98005df23 [file] [log] [blame]
DRC2ff39b82011-07-28 08:38:59 +00001//
2// "$Id: Fl_Dial.cxx 7903 2010-11-28 21:06:39Z matt $"
3//
4// Circular dial widget for the Fast Light Tool Kit (FLTK).
5//
6// Copyright 1998-2010 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307
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/**
36 Draws dial at given position and size.
37 \param[in] X, Y, W, H position and size
38*/
39void Fl_Dial::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);
56 if (foo) {
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);
60 }
61 return;
62 }
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);
67 }
68 fl_push_matrix();
69 fl_translate(X+W/2-.5, Y+H/2-.5);
70 fl_scale(W-1, H-1);
71 fl_rotate(45-angle);
72 if (active_r()) fl_color(selection_color());
73 else fl_color(fl_inactive(selection_color()));
74 if (type()) { // FL_LINE_DIAL
75 fl_begin_polygon();
76 fl_vertex(0.0, 0.0);
77 fl_vertex(-0.04, 0.0);
78 fl_vertex(-0.25, 0.25);
79 fl_vertex(0.0, 0.04);
80 fl_end_polygon();
81 if (active_r()) fl_color(FL_FOREGROUND_COLOR);
82 else fl_color(fl_inactive(FL_FOREGROUND_COLOR));
83 fl_begin_loop();
84 fl_vertex(0.0, 0.0);
85 fl_vertex(-0.04, 0.0);
86 fl_vertex(-0.25, 0.25);
87 fl_vertex(0.0, 0.04);
88 fl_end_loop();
89 } else {
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();
94 }
95 fl_pop_matrix();
96}
97
98/**
99 Draws dial at current position and size.
100*/
101void Fl_Dial::draw() {
102 draw(x(), y(), w(), h());
103 draw_label();
104}
105
106/**
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.
109*/
110int Fl_Dial::handle(int event, int X, int Y, int W, int H) {
111 switch (event) {
112 case FL_PUSH: {
113 Fl_Widget_Tracker wp(this);
114 handle_push();
115 if (wp.deleted()) return 1; }
116 case FL_DRAG: {
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;
124 double val;
125 if ((a1<a2) ? (angle <= a1) : (angle >= a1)) {
126 val = minimum();
127 } else if ((a1<a2) ? (angle >= a2) : (angle <= a2)) {
128 val = maximum();
129 } else {
130 val = minimum() + (maximum()-minimum())*(angle-a1)/(a2-a1);
131 }
132 handle_drag(clamp(round(val)));
133 } return 1;
134 case FL_RELEASE:
135 handle_release();
136 return 1;
137 case FL_ENTER : /* FALLTHROUGH */
138 case FL_LEAVE :
139 return 1;
140 default:
141 return 0;
142 }
143}
144
145/**
146 Allow subclasses to handle event based on current position and size.
147*/
148int Fl_Dial::handle(int e) {
149 return handle(e, x(), y(), w(), h());
150}
151
152Fl_Dial::Fl_Dial(int X, int Y, int W, int H, const char* l)
153/**
154 Creates a new Fl_Dial widget using the given position, size,
155 and label string. The default type is FL_NORMAL_DIAL.
156*/
157: Fl_Valuator(X, Y, W, H, l) {
158 box(FL_OVAL_BOX);
159 selection_color(FL_INACTIVE_COLOR); // was 37
160 a1 = 45;
161 a2 = 315;
162}
163
164//
165// End of "$Id: Fl_Dial.cxx 7903 2010-11-28 21:06:39Z matt $".
166//