DRC | 2ff39b8 | 2011-07-28 08:38:59 +0000 | [diff] [blame^] | 1 | // |
| 2 | // "$Id: Fl_Clock.cxx 7903 2010-11-28 21:06:39Z matt $" |
| 3 | // |
| 4 | // Clock 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_Clock.H> |
| 30 | #include <FL/fl_draw.H> |
| 31 | #include <math.h> |
| 32 | #include <time.h> |
| 33 | #ifndef WIN32 |
| 34 | # include <sys/time.h> |
| 35 | #endif /* !WIN32 */ |
| 36 | |
| 37 | // Original clock display written by Paul Haeberli at SGI. |
| 38 | // Modifications by Mark Overmars for Forms |
| 39 | // Further changes by Bill Spitzak for fltk |
| 40 | |
| 41 | const float hourhand[4][2] = {{-0.5f, 0}, {0, 1.5f}, {0.5f, 0}, {0, -7.0f}}; |
| 42 | const float minhand[4][2] = {{-0.5f, 0}, {0, 1.5f}, {0.5f, 0}, {0, -11.5f}}; |
| 43 | const float sechand[4][2] = {{-0.1f, 0}, {0, 2.0f}, {0.1f, 0}, {0, -11.5f}}; |
| 44 | |
| 45 | static void drawhand(double ang,const float v[][2],Fl_Color fill,Fl_Color line) |
| 46 | { |
| 47 | fl_push_matrix(); |
| 48 | fl_rotate(ang); |
| 49 | fl_color(fill); fl_begin_polygon(); |
| 50 | int i; for (i=0; i<4; i++) fl_vertex(v[i][0],v[i][1]); fl_end_polygon(); |
| 51 | fl_color(line); fl_begin_loop(); |
| 52 | for (i=0; i<4; i++) fl_vertex(v[i][0],v[i][1]); fl_end_loop(); |
| 53 | fl_pop_matrix(); |
| 54 | } |
| 55 | |
| 56 | void Fl_Clock_Output::drawhands(Fl_Color fill, Fl_Color line) { |
| 57 | if (!active_r()) { |
| 58 | fill = fl_inactive(fill); |
| 59 | line = fl_inactive(line); |
| 60 | } |
| 61 | drawhand(-360*(hour()+minute()/60.0)/12, hourhand, fill, line); |
| 62 | drawhand(-360*(minute()+second()/60.0)/60, minhand, fill, line); |
| 63 | drawhand(-360*(second()/60.0), sechand, fill, line); |
| 64 | } |
| 65 | |
| 66 | static void rect(double x, double y, double w, double h) { |
| 67 | double r = x+w; |
| 68 | double t = y+h; |
| 69 | fl_begin_polygon(); |
| 70 | fl_vertex(x, y); |
| 71 | fl_vertex(r, y); |
| 72 | fl_vertex(r, t); |
| 73 | fl_vertex(x, t); |
| 74 | fl_end_polygon(); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | Draw clock with the given position and size. |
| 79 | \param[in] X, Y, W, H position and size |
| 80 | */ |
| 81 | void Fl_Clock_Output::draw(int X, int Y, int W, int H) { |
| 82 | Fl_Color box_color = type()==FL_ROUND_CLOCK ? FL_GRAY : color(); |
| 83 | Fl_Color shadow_color = fl_color_average(box_color, FL_BLACK, 0.5); |
| 84 | draw_box(box(), X, Y, W, H, box_color); |
| 85 | fl_push_matrix(); |
| 86 | fl_translate(X+W/2.0-.5, Y+H/2.0-.5); |
| 87 | fl_scale((W-1)/28.0, (H-1)/28.0); |
| 88 | if (type() == FL_ROUND_CLOCK) { |
| 89 | fl_color(active_r() ? color() : fl_inactive(color())); |
| 90 | fl_begin_polygon(); fl_circle(0,0,14); fl_end_polygon(); |
| 91 | fl_color(active_r() ? FL_FOREGROUND_COLOR : fl_inactive(FL_FOREGROUND_COLOR)); |
| 92 | fl_begin_loop(); fl_circle(0,0,14); fl_end_loop(); |
| 93 | } |
| 94 | // draw the shadows: |
| 95 | fl_push_matrix(); |
| 96 | fl_translate(0.60, 0.60); |
| 97 | drawhands(shadow_color, shadow_color); |
| 98 | fl_pop_matrix(); |
| 99 | // draw the tick marks: |
| 100 | fl_push_matrix(); |
| 101 | fl_color(active_r() ? FL_FOREGROUND_COLOR : fl_inactive(FL_FOREGROUND_COLOR)); |
| 102 | for (int i=0; i<12; i++) { |
| 103 | if (i==6) rect(-0.5, 9, 1, 2); |
| 104 | else if (i==3 || i==0 || i== 9) rect(-0.5, 9.5, 1, 1); |
| 105 | else rect(-0.25, 9.5, .5, 1); |
| 106 | fl_rotate(-30); |
| 107 | } |
| 108 | fl_pop_matrix(); |
| 109 | // draw the hands: |
| 110 | drawhands(selection_color(), FL_FOREGROUND_COLOR); // color was 54 |
| 111 | fl_pop_matrix(); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | Draw clock with current position and size. |
| 116 | */ |
| 117 | void Fl_Clock_Output::draw() { |
| 118 | draw(x(), y(), w(), h()); |
| 119 | draw_label(); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | Set the displayed time. |
| 124 | Set the time in hours, minutes, and seconds. |
| 125 | \param[in] H, m, s displayed time |
| 126 | \see hour(), minute(), second() |
| 127 | */ |
| 128 | void Fl_Clock_Output::value(int H, int m, int s) { |
| 129 | if (H!=hour_ || m!=minute_ || s!=second_) { |
| 130 | hour_ = H; minute_ = m; second_ = s; |
| 131 | value_ = (H * 60 + m) * 60 + s; |
| 132 | damage(FL_DAMAGE_CHILD); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | Set the displayed time. |
| 138 | Set the time in seconds since the UNIX epoch (January 1, 1970). |
| 139 | \param[in] v seconds since epoch |
| 140 | \see value() |
| 141 | */ |
| 142 | void Fl_Clock_Output::value(ulong v) { |
| 143 | value_ = v; |
| 144 | struct tm *timeofday; |
| 145 | // Some platforms, notably Windows, now use a 64-bit time_t value... |
| 146 | time_t vv = (time_t)v; |
| 147 | timeofday = localtime(&vv); |
| 148 | value(timeofday->tm_hour, timeofday->tm_min, timeofday->tm_sec); |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | Create a new Fl_Clock_Output widget with the given position, size and label. |
| 153 | The default boxtype is \c FL_NO_BOX. |
| 154 | \param[in] X, Y, W, H position and size of the widget |
| 155 | \param[in] L widget label, default is no label |
| 156 | */ |
| 157 | Fl_Clock_Output::Fl_Clock_Output(int X, int Y, int W, int H, const char *L) |
| 158 | : Fl_Widget(X, Y, W, H, L) { |
| 159 | box(FL_UP_BOX); |
| 160 | selection_color(fl_gray_ramp(5)); |
| 161 | align(FL_ALIGN_BOTTOM); |
| 162 | hour_ = 0; |
| 163 | minute_ = 0; |
| 164 | second_ = 0; |
| 165 | value_ = 0; |
| 166 | } |
| 167 | |
| 168 | //////////////////////////////////////////////////////////////// |
| 169 | |
| 170 | /** |
| 171 | Create an Fl_Clock widget using the given position, size, and label string. |
| 172 | The default boxtype is \c FL_NO_BOX. |
| 173 | \param[in] X, Y, W, H position and size of the widget |
| 174 | \param[in] L widget label, default is no label |
| 175 | */ |
| 176 | Fl_Clock::Fl_Clock(int X, int Y, int W, int H, const char *L) |
| 177 | : Fl_Clock_Output(X, Y, W, H, L) {} |
| 178 | |
| 179 | /** |
| 180 | Create an Fl_Clock widget using the given boxtype, position, size, and |
| 181 | label string. |
| 182 | \param[in] t boxtype |
| 183 | \param[in] X, Y, W, H position and size of the widget |
| 184 | \param[in] L widget label, default is no label |
| 185 | */ |
| 186 | Fl_Clock::Fl_Clock(uchar t, int X, int Y, int W, int H, const char *L) |
| 187 | : Fl_Clock_Output(X, Y, W, H, L) { |
| 188 | type(t); |
| 189 | box(t==FL_ROUND_CLOCK ? FL_NO_BOX : FL_UP_BOX); |
| 190 | } |
| 191 | |
| 192 | static void tick(void *v) { |
| 193 | ((Fl_Clock*)v)->value(time(0)); |
| 194 | Fl::add_timeout(1.0, tick, v); |
| 195 | } |
| 196 | |
| 197 | int Fl_Clock::handle(int event) { |
| 198 | switch (event) { |
| 199 | case FL_SHOW: |
| 200 | tick(this); |
| 201 | break; |
| 202 | case FL_HIDE: |
| 203 | Fl::remove_timeout(tick, this); |
| 204 | break; |
| 205 | } |
| 206 | return Fl_Clock_Output::handle(event); |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | The destructor removes the clock. |
| 211 | */ |
| 212 | Fl_Clock::~Fl_Clock() { |
| 213 | Fl::remove_timeout(tick, this); |
| 214 | } |
| 215 | |
| 216 | // |
| 217 | // End of "$Id: Fl_Clock.cxx 7903 2010-11-28 21:06:39Z matt $". |
| 218 | // |