implement wxEventLoop::DispatchTimeout() for wxGTK (thanks Paul) and rewrote it to...
[wxWidgets.git] / src / gtk / evtloop.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/evtloop.cpp
3 // Purpose: implements wxEventLoop for GTK+
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 10.07.01
7 // RCS-ID: $Id$
8 // Copyright: (c) 2001 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // License: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #include "wx/evtloop.h"
28
29 #ifndef WX_PRECOMP
30 #include "wx/app.h"
31 #endif // WX_PRECOMP
32
33 #include <gtk/gtk.h>
34
35 // ============================================================================
36 // wxEventLoop implementation
37 // ============================================================================
38
39 // ----------------------------------------------------------------------------
40 // wxEventLoop running and exiting
41 // ----------------------------------------------------------------------------
42
43 wxGUIEventLoop::wxGUIEventLoop()
44 {
45 m_exitcode = 0;
46 }
47
48 int wxGUIEventLoop::Run()
49 {
50 // event loops are not recursive, you need to create another loop!
51 wxCHECK_MSG( !IsRunning(), -1, "can't reenter a message loop" );
52
53 wxEventLoopActivator activate(this);
54
55 gtk_main();
56
57 OnExit();
58
59 return m_exitcode;
60 }
61
62 void wxGUIEventLoop::Exit(int rc)
63 {
64 wxCHECK_RET( IsRunning(), "can't call Exit() if not running" );
65
66 m_exitcode = rc;
67
68 gtk_main_quit();
69 }
70
71 void wxGUIEventLoop::WakeUp()
72 {
73 // TODO: idle events handling should really be done by wxEventLoop itself
74 // but for now it's completely in gtk/app.cpp so just call there when
75 // we have wxTheApp and hope that it doesn't matter that we do
76 // nothing when we don't...
77 if ( wxTheApp )
78 wxTheApp->WakeUpIdle();
79 }
80
81 // ----------------------------------------------------------------------------
82 // wxEventLoop message processing dispatching
83 // ----------------------------------------------------------------------------
84
85 bool wxGUIEventLoop::Pending() const
86 {
87 if ( wxTheApp )
88 {
89 // this avoids false positives from our idle source
90 return wxTheApp->EventsPending();
91 }
92
93 return gtk_events_pending() != 0;
94 }
95
96 bool wxGUIEventLoop::Dispatch()
97 {
98 wxCHECK_MSG( IsRunning(), false, _T("can't call Dispatch() if not running") );
99
100 // gtk_main_iteration() returns TRUE only if gtk_main_quit() was called
101 return !gtk_main_iteration();
102 }
103
104 extern "C" {
105 static gboolean wx_event_loop_timeout(void* data)
106 {
107 bool* expired = static_cast<bool*>(data);
108 *expired = true;
109
110 // return FALSE to remove this timeout
111 return FALSE;
112 }
113 }
114
115 int wxGUIEventLoop::DispatchTimeout(unsigned long timeout)
116 {
117 bool expired = false;
118 const unsigned id = g_timeout_add(timeout, wx_event_loop_timeout, &expired);
119 bool quit = gtk_main_iteration() != 0;
120
121 if ( expired )
122 return -1;
123
124 g_source_remove(id);
125
126 return !quit;
127 }
128
129