fix from Ron for one-shot timers
[wxWidgets.git] / src / gtk1 / timer.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: timer.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10
11 #ifdef __GNUG__
12 #pragma implementation "timer.h"
13 #endif
14
15 #include "wx/timer.h"
16
17 #include "gtk/gtk.h"
18 /*
19 #include "glib.h"
20 */
21
22 //-----------------------------------------------------------------------------
23 // global functions
24 //-----------------------------------------------------------------------------
25
26 /*
27 static GTimer *g_timer = (GTimer*) NULL;
28
29 void wxStartTimer()
30 {
31 if (g_timer)
32 {
33 g_timer_rest( g_timer );
34 }
35 else
36 {
37 g_timer = g_timer_new();
38 g_timer_start( g_timer );
39 }
40 }
41
42 long wxGetElapsedTime( bool resetTimer )
43 {
44 gulong res = 0;
45 if (g_timer)
46 {
47 g_timer_elapsed( g_timer, &res );
48 if (resetTimer) g_timer_reset( g_timer );
49 }
50
51 return res;
52 }
53
54 bool wxGetLocalTime( long *timeZone, int *dstObserved )
55 {
56 }
57
58 long wxGetCurrentTime()
59 {
60 }
61 */
62
63
64 //-----------------------------------------------------------------------------
65 // wxTimer
66 //-----------------------------------------------------------------------------
67
68 IMPLEMENT_ABSTRACT_CLASS(wxTimer,wxObject)
69
70 static gint timeout_callback( gpointer data )
71 {
72 wxTimer *timer = (wxTimer*)data;
73
74 #if (GTK_MINOR_VERSION > 0)
75 /* when getting called from GDK's timer handler we
76 are no longer within GDK's grab on the GUI
77 thread so we must lock it here ourselves */
78 GDK_THREADS_ENTER ();
79 #endif
80
81 timer->Notify();
82
83 #if (GTK_MINOR_VERSION > 0)
84 /* release lock again */
85 GDK_THREADS_LEAVE ();
86 #endif
87
88 if (timer->OneShot())
89 return FALSE;
90
91 return TRUE;
92 }
93
94 wxTimer::wxTimer()
95 {
96 m_tag = -1;
97 m_time = 1000;
98 m_oneShot = FALSE;
99 }
100
101 wxTimer::~wxTimer()
102 {
103 Stop();
104 }
105
106 bool wxTimer::Start( int millisecs, bool oneShot )
107 {
108 if (millisecs != -1)
109 {
110 m_time = millisecs;
111 }
112
113 m_oneShot = oneShot;
114
115 m_tag = gtk_timeout_add( millisecs, timeout_callback, this );
116
117 return TRUE;
118 }
119
120 void wxTimer::Stop()
121 {
122 if (m_tag != -1)
123 {
124 gtk_timeout_remove( m_tag );
125 m_tag = -1;
126 }
127 }
128