00001 #ifndef GTKUTIL_TIMER_H_ 00002 #define GTKUTIL_TIMER_H_ 00003 00004 #include <gtk/gtkmain.h> 00005 00006 /* greebo: This is an encapsulation of the gtk_timeout_ methods that 00007 * periodically call a certain function in the given intervals (resolution: 1 ms). 00008 * 00009 * Instantiate this Timer with a timeout in ms and a GTK-compatible callback. 00010 * (e.g. a static member function that can be cast by using G_CALLBACK()). 00011 */ 00012 namespace gtkutil 00013 { 00014 class Timer 00015 { 00016 // The handler ID for the connected timer 00017 guint _timerID; 00018 00019 // The timeout interval in milliseconds 00020 unsigned long _timeout; 00021 00022 typedef gboolean (*TimerCallback) (gpointer data); 00023 00024 // The timer callback function (GTK compatible) 00025 TimerCallback _callback; 00026 00027 // The pointer to pass (e.g. a pointer to a class instance) 00028 gpointer _data; 00029 00030 public: 00031 // Constructor (instantiate with timeout in millisec. and a static callback function) 00032 Timer (const unsigned long timeout, TimerCallback callback, gpointer data); 00033 00034 // Sets the timeout to the given value (in ms). The changes get 00035 // activated when enable() is called the next time. 00036 void setTimeout (const unsigned long timeout); 00037 00038 // Sets the callback to the given <callback> 00039 void setCallback (TimerCallback callback); 00040 00041 // Sets the callback to the given <callback> 00042 void setCallbackData (gpointer data); 00043 00044 // Starts the timer and saves the handler id locally 00045 void enable (); 00046 00047 // Disables the timer and resets the handler ID 00048 void disable (); 00049 00050 bool isEnabled () const; 00051 00052 }; // class Timer 00053 00054 } // namespace gtkutil 00055 00056 #endif /*GTKUTIL_TIMER_H_*/