fltk 1.3.0rc3
About: FLTK (Fast Light Tool Kit) is a cross-platform C++ GUI toolkit for UNIX/Linux (X11), Microsoft Windows, and MacOS X. Release candidate.
  SfR Fresh Dox: fltk-1.3.0rc3-source.tar.gz ("inofficial" and yet experimental doxygen-generated source code documentation)  

Fl_Dial.cxx

Go to the documentation of this file.
00001 //
00002 // "$Id: Fl_Dial.cxx 7903 2010-11-28 21:06:39Z matt $"
00003 //
00004 // Circular dial widget for the Fast Light Tool Kit (FLTK).
00005 //
00006 // Copyright 1998-2010 by Bill Spitzak and others.
00007 //
00008 // This library is free software; you can redistribute it and/or
00009 // modify it under the terms of the GNU Library General Public
00010 // License as published by the Free Software Foundation; either
00011 // version 2 of the License, or (at your option) any later version.
00012 //
00013 // This library is distributed in the hope that it will be useful,
00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016 // Library General Public License for more details.
00017 //
00018 // You should have received a copy of the GNU Library General Public
00019 // License along with this library; if not, write to the Free Software
00020 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00021 // USA.
00022 //
00023 // Please report all bugs and problems on the following page:
00024 //
00025 //     http://www.fltk.org/str.php
00026 //
00027 
00028 #include <FL/Fl.H>
00029 #include <FL/Fl_Dial.H>
00030 #include <FL/fl_draw.H>
00031 #include <stdlib.h>
00032 #include <FL/math.h>
00033 
00034 // All angles are measured with 0 to the right and counter-clockwise
00039 void Fl_Dial::draw(int X, int Y, int W, int H) {
00040   if (damage()&FL_DAMAGE_ALL) draw_box(box(), X, Y, W, H, color());
00041   X += Fl::box_dx(box());
00042   Y += Fl::box_dy(box());
00043   W -= Fl::box_dw(box());
00044   H -= Fl::box_dh(box());
00045   double angle = (a2-a1)*(value()-minimum())/(maximum()-minimum()) + a1;
00046   if (type() == FL_FILL_DIAL) {
00047     // foo: draw this nicely in certain round box types
00048     int foo = (box() > _FL_ROUND_UP_BOX && Fl::box_dx(box()));
00049     if (foo) {X--; Y--; W+=2; H+=2;}
00050     if (active_r()) fl_color(color());
00051     else fl_color(fl_inactive(color()));
00052     fl_pie(X, Y, W, H, 270-a1, angle > a1 ? 360+270-angle : 270-360-angle);
00053     if (active_r()) fl_color(selection_color());
00054     else fl_color(fl_inactive(selection_color()));
00055     fl_pie(X, Y, W, H, 270-angle, 270-a1);
00056     if (foo) {
00057       if (active_r()) fl_color(FL_FOREGROUND_COLOR);
00058       else fl_color(fl_inactive(FL_FOREGROUND_COLOR));
00059       fl_arc(X, Y, W, H, 0, 360);
00060     }
00061     return;
00062   }
00063   if (!(damage()&FL_DAMAGE_ALL)) {
00064     if (active_r()) fl_color(color());
00065     else fl_color(fl_inactive(color()));
00066     fl_pie(X+1, Y+1, W-2, H-2, 0, 360);
00067   }
00068   fl_push_matrix();
00069   fl_translate(X+W/2-.5, Y+H/2-.5);
00070   fl_scale(W-1, H-1);
00071   fl_rotate(45-angle);
00072   if (active_r()) fl_color(selection_color());
00073   else fl_color(fl_inactive(selection_color()));
00074   if (type()) { // FL_LINE_DIAL
00075     fl_begin_polygon();
00076     fl_vertex(0.0,   0.0);
00077     fl_vertex(-0.04, 0.0);
00078     fl_vertex(-0.25, 0.25);
00079     fl_vertex(0.0,   0.04);
00080     fl_end_polygon();
00081     if (active_r()) fl_color(FL_FOREGROUND_COLOR);
00082     else fl_color(fl_inactive(FL_FOREGROUND_COLOR));
00083     fl_begin_loop();
00084     fl_vertex(0.0,   0.0);
00085     fl_vertex(-0.04, 0.0);
00086     fl_vertex(-0.25, 0.25);
00087     fl_vertex(0.0,   0.04);
00088     fl_end_loop();
00089   } else {
00090     fl_begin_polygon(); fl_circle(-0.20, 0.20, 0.07); fl_end_polygon();
00091     if (active_r()) fl_color(FL_FOREGROUND_COLOR);
00092     else fl_color(fl_inactive(FL_FOREGROUND_COLOR));
00093     fl_begin_loop(); fl_circle(-0.20, 0.20, 0.07); fl_end_loop();
00094   }
00095   fl_pop_matrix();
00096 }
00097 
00101 void Fl_Dial::draw() {
00102   draw(x(), y(), w(), h());
00103   draw_label();
00104 }
00105 
00110 int Fl_Dial::handle(int event, int X, int Y, int W, int H) {
00111   switch (event) {
00112   case FL_PUSH: {
00113     Fl_Widget_Tracker wp(this);  
00114     handle_push();
00115     if (wp.deleted()) return 1; }
00116   case FL_DRAG: {
00117     int mx = (Fl::event_x()-X-W/2)*H;
00118     int my = (Fl::event_y()-Y-H/2)*W;
00119     if (!mx && !my) return 1;
00120     double angle = 270-atan2((float)-my, (float)mx)*180/M_PI;
00121     double oldangle = (a2-a1)*(value()-minimum())/(maximum()-minimum()) + a1;
00122     while (angle < oldangle-180) angle += 360;
00123     while (angle > oldangle+180) angle -= 360;
00124     double val;
00125     if ((a1<a2) ? (angle <= a1) : (angle >= a1)) {
00126       val = minimum();
00127     } else if ((a1<a2) ? (angle >= a2) : (angle <= a2)) {
00128       val = maximum();
00129     } else {
00130       val = minimum() + (maximum()-minimum())*(angle-a1)/(a2-a1);
00131     }
00132     handle_drag(clamp(round(val)));
00133   } return 1;
00134   case FL_RELEASE:
00135     handle_release();
00136     return 1;
00137   case FL_ENTER : /* FALLTHROUGH */
00138   case FL_LEAVE :
00139     return 1;
00140   default:
00141     return 0;
00142   }
00143 }
00144 
00148 int Fl_Dial::handle(int e) {
00149   return handle(e, x(), y(), w(), h());
00150 }
00151 
00152 Fl_Dial::Fl_Dial(int X, int Y, int W, int H, const char* l)
00157 : Fl_Valuator(X, Y, W, H, l) {
00158   box(FL_OVAL_BOX);
00159   selection_color(FL_INACTIVE_COLOR); // was 37
00160   a1 = 45;
00161   a2 = 315;
00162 }
00163 
00164 //
00165 // End of "$Id: Fl_Dial.cxx 7903 2010-11-28 21:06:39Z matt $".
00166 //