|
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) ![]() |
00001 // 00002 // "$Id: Fl_Clock.cxx 7903 2010-11-28 21:06:39Z matt $" 00003 // 00004 // Clock 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_Clock.H> 00030 #include <FL/fl_draw.H> 00031 #include <math.h> 00032 #include <time.h> 00033 #ifndef WIN32 00034 # include <sys/time.h> 00035 #endif /* !WIN32 */ 00036 00037 // Original clock display written by Paul Haeberli at SGI. 00038 // Modifications by Mark Overmars for Forms 00039 // Further changes by Bill Spitzak for fltk 00040 00041 const float hourhand[4][2] = {{-0.5f, 0}, {0, 1.5f}, {0.5f, 0}, {0, -7.0f}}; 00042 const float minhand[4][2] = {{-0.5f, 0}, {0, 1.5f}, {0.5f, 0}, {0, -11.5f}}; 00043 const float sechand[4][2] = {{-0.1f, 0}, {0, 2.0f}, {0.1f, 0}, {0, -11.5f}}; 00044 00045 static void drawhand(double ang,const float v[][2],Fl_Color fill,Fl_Color line) 00046 { 00047 fl_push_matrix(); 00048 fl_rotate(ang); 00049 fl_color(fill); fl_begin_polygon(); 00050 int i; for (i=0; i<4; i++) fl_vertex(v[i][0],v[i][1]); fl_end_polygon(); 00051 fl_color(line); fl_begin_loop(); 00052 for (i=0; i<4; i++) fl_vertex(v[i][0],v[i][1]); fl_end_loop(); 00053 fl_pop_matrix(); 00054 } 00055 00056 void Fl_Clock_Output::drawhands(Fl_Color fill, Fl_Color line) { 00057 if (!active_r()) { 00058 fill = fl_inactive(fill); 00059 line = fl_inactive(line); 00060 } 00061 drawhand(-360*(hour()+minute()/60.0)/12, hourhand, fill, line); 00062 drawhand(-360*(minute()+second()/60.0)/60, minhand, fill, line); 00063 drawhand(-360*(second()/60.0), sechand, fill, line); 00064 } 00065 00066 static void rect(double x, double y, double w, double h) { 00067 double r = x+w; 00068 double t = y+h; 00069 fl_begin_polygon(); 00070 fl_vertex(x, y); 00071 fl_vertex(r, y); 00072 fl_vertex(r, t); 00073 fl_vertex(x, t); 00074 fl_end_polygon(); 00075 } 00076 00081 void Fl_Clock_Output::draw(int X, int Y, int W, int H) { 00082 Fl_Color box_color = type()==FL_ROUND_CLOCK ? FL_GRAY : color(); 00083 Fl_Color shadow_color = fl_color_average(box_color, FL_BLACK, 0.5); 00084 draw_box(box(), X, Y, W, H, box_color); 00085 fl_push_matrix(); 00086 fl_translate(X+W/2.0-.5, Y+H/2.0-.5); 00087 fl_scale((W-1)/28.0, (H-1)/28.0); 00088 if (type() == FL_ROUND_CLOCK) { 00089 fl_color(active_r() ? color() : fl_inactive(color())); 00090 fl_begin_polygon(); fl_circle(0,0,14); fl_end_polygon(); 00091 fl_color(active_r() ? FL_FOREGROUND_COLOR : fl_inactive(FL_FOREGROUND_COLOR)); 00092 fl_begin_loop(); fl_circle(0,0,14); fl_end_loop(); 00093 } 00094 // draw the shadows: 00095 fl_push_matrix(); 00096 fl_translate(0.60, 0.60); 00097 drawhands(shadow_color, shadow_color); 00098 fl_pop_matrix(); 00099 // draw the tick marks: 00100 fl_push_matrix(); 00101 fl_color(active_r() ? FL_FOREGROUND_COLOR : fl_inactive(FL_FOREGROUND_COLOR)); 00102 for (int i=0; i<12; i++) { 00103 if (i==6) rect(-0.5, 9, 1, 2); 00104 else if (i==3 || i==0 || i== 9) rect(-0.5, 9.5, 1, 1); 00105 else rect(-0.25, 9.5, .5, 1); 00106 fl_rotate(-30); 00107 } 00108 fl_pop_matrix(); 00109 // draw the hands: 00110 drawhands(selection_color(), FL_FOREGROUND_COLOR); // color was 54 00111 fl_pop_matrix(); 00112 } 00113 00117 void Fl_Clock_Output::draw() { 00118 draw(x(), y(), w(), h()); 00119 draw_label(); 00120 } 00121 00128 void Fl_Clock_Output::value(int H, int m, int s) { 00129 if (H!=hour_ || m!=minute_ || s!=second_) { 00130 hour_ = H; minute_ = m; second_ = s; 00131 value_ = (H * 60 + m) * 60 + s; 00132 damage(FL_DAMAGE_CHILD); 00133 } 00134 } 00135 00142 void Fl_Clock_Output::value(ulong v) { 00143 value_ = v; 00144 struct tm *timeofday; 00145 // Some platforms, notably Windows, now use a 64-bit time_t value... 00146 time_t vv = (time_t)v; 00147 timeofday = localtime(&vv); 00148 value(timeofday->tm_hour, timeofday->tm_min, timeofday->tm_sec); 00149 } 00150 00157 Fl_Clock_Output::Fl_Clock_Output(int X, int Y, int W, int H, const char *L) 00158 : Fl_Widget(X, Y, W, H, L) { 00159 box(FL_UP_BOX); 00160 selection_color(fl_gray_ramp(5)); 00161 align(FL_ALIGN_BOTTOM); 00162 hour_ = 0; 00163 minute_ = 0; 00164 second_ = 0; 00165 value_ = 0; 00166 } 00167 00169 00176 Fl_Clock::Fl_Clock(int X, int Y, int W, int H, const char *L) 00177 : Fl_Clock_Output(X, Y, W, H, L) {} 00178 00186 Fl_Clock::Fl_Clock(uchar t, int X, int Y, int W, int H, const char *L) 00187 : Fl_Clock_Output(X, Y, W, H, L) { 00188 type(t); 00189 box(t==FL_ROUND_CLOCK ? FL_NO_BOX : FL_UP_BOX); 00190 } 00191 00192 static void tick(void *v) { 00193 ((Fl_Clock*)v)->value(time(0)); 00194 Fl::add_timeout(1.0, tick, v); 00195 } 00196 00197 int Fl_Clock::handle(int event) { 00198 switch (event) { 00199 case FL_SHOW: 00200 tick(this); 00201 break; 00202 case FL_HIDE: 00203 Fl::remove_timeout(tick, this); 00204 break; 00205 } 00206 return Fl_Clock_Output::handle(event); 00207 } 00208 00212 Fl_Clock::~Fl_Clock() { 00213 Fl::remove_timeout(tick, this); 00214 } 00215 00216 // 00217 // End of "$Id: Fl_Clock.cxx 7903 2010-11-28 21:06:39Z matt $". 00218 //