Update NTK.
[nondaw.git] / FL / Fl_Blink_Button.H
blob90f2ed23d33a242f63df241019fc6918e6de570a
2 /*******************************************************************************/
3 /* Copyright (C) 2008 Jonathan Moore Liles                                     */
4 /*                                                                             */
5 /* This program is free software; you can redistribute it and/or modify it     */
6 /* under the terms of the GNU General Public License as published by the       */
7 /* Free Software Foundation; either version 2 of the License, or (at your      */
8 /* option) any later version.                                                  */
9 /*                                                                             */
10 /* This program is distributed in the hope that it will be useful, but WITHOUT */
11 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or       */
12 /* FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for   */
13 /* more details.                                                               */
14 /*                                                                             */
15 /* You should have received a copy of the GNU General Public License along     */
16 /* with This program; see the file COPYING.  If not,write to the Free Software */
17 /* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
18 /*******************************************************************************/
20 #include <FL/Fl_Button.H>
21 #include <FL/Fl.H>
23 /* Kind of like Fl_Light_Button except that the whole thing is the
24  * indicator and it can optionally blink */
26 class Fl_Blink_Button : public Fl_Button
28     bool _on;
29     float _blink_interval;
30     bool _blinking;
32     static void
33     update_cb ( void *v )
34         {
35             ((Fl_Blink_Button*)v)->update_cb();
36         }
38     void
39     update_cb ( void )
40         {
41             Fl::repeat_timeout( _blink_interval, update_cb, this );
43             _on = ! _on;
45             redraw();
46         }
47     
48 public:
50     static const float SLOW = 0.5f;
51     static const float MEDIUM = 0.3f;
52     static const float FAST = 0.1f;
53     static const float DEFAULT = 0.5f;
55     Fl_Blink_Button ( int X, int Y, int W, int H, const char *L )
56         : Fl_Button( X, Y, W, H, L )
57         {
58             _blinking = true;
59             _on = false;
61             _blink_interval = DEFAULT;
63             type( FL_TOGGLE_BUTTON );
64         }
66     virtual
67     ~Fl_Blink_Button ()
68         {
69             if ( value() )
70                 Fl::remove_timeout( update_cb, this );
71         }
72     
73     void blink ( bool b )
74         {
75             _blinking = b;
76             if ( ! b )
77                 _on = true;
78         }
80     bool blink ( void ) const
81         {
82             return _blinking;
83         }
85     void
86     blink_interval ( float v )
87         {
88             _blink_interval = v;
89             if ( value() )
90             {
91                 Fl::remove_timeout( update_cb, this );
92                 Fl::add_timeout( _blink_interval, update_cb, this );
93             }
94         }
96     virtual void value ( float v )
97         {
98             if ( v )
99             {
100                 if ( _blinking )
101                     Fl::add_timeout( _blink_interval, update_cb, this );
102                 Fl_Button::value( v );
103                 redraw();
104             }
105             else
106             {
107                 Fl_Button::value( v );
108                 Fl::remove_timeout( update_cb, this );
109                 redraw();
110             }
112         }
114     virtual float value ( void ) { return Fl_Button::value(); }
116     virtual void
117     draw ( void )
118         {
119             draw_box( value() ? box() : down_box(), x(), y(), w(), h(), ( value() != 0 && _on ) ? selection_color() : color() );
120             draw_label();
121         }