]>
git.saurik.com Git - wxWidgets.git/blob - interface/wx/evtloop.h
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxEventLoop and related classes
4 // Author: Vadim Zeitlin
5 // Copyright: (C) 2008 Vadim Zeitlin
7 // Licence: wxWindows license
8 /////////////////////////////////////////////////////////////////////////////
11 @class wxEventLoopBase
13 Base class for all event loop implementations.
15 An object of this class is created by wxAppTraits::CreateEventLoop() and
16 used by wxApp to run the main application event loop.
19 @category{appmanagement}
21 @see wxApp, wxEventLoopActivator
27 Return the currently active (running) event loop.
29 May return @NULL if there is no active event loop (e.g. during
30 application startup or shutdown).
32 static wxEventLoopBase
*GetActive();
35 Set currently active (running) event loop.
37 Called by wxEventLoopActivator, use an instance of this class instead
38 of calling this method directly to ensure that the previously active
39 event loop is restored.
41 static void SetActive(wxEventLoopBase
* loop
);
45 @name Dispatch and processing
50 Start the event loop, return the exit code when it is finished.
52 Logically, this method calls Dispatch() in a loop until it returns
53 @false and also takes care of generating idle events during each loop
54 iteration. However not all implementations of this class really
55 implement it like this (e.g. wxGTK does not) so you shouldn't rely on
56 Dispatch() being called from inside this function.
58 @return The argument passed to Exit() which terminated this event loop.
60 virtual int Run() = 0;
63 Return true if this event loop is currently running.
65 Notice that even if this event loop hasn't terminated yet but has just
66 spawned a nested (e.g. modal) event loop, this method would return
69 bool IsRunning() const;
72 Use this to check whether the event loop was successfully created
75 virtual bool IsOk() const;
78 Exit from the loop with the given exit code.
80 virtual void Exit(int rc
= 0) = 0;
83 Return true if any events are available.
85 If this method returns @true, calling Dispatch() will not block.
87 virtual bool Pending() const = 0;
90 Dispatches the next event in the windowing system event queue.
91 Blocks until an event appears if there are none currently
92 (use Pending() if this is not wanted).
94 This can be used for programming event loops, e.g.
97 while (evtloop->Pending())
101 @return @false if the event loop should stop and @true otherwise.
103 @see Pending(), wxEventLoopBase
105 virtual bool Dispatch() = 0;
108 Dispatch an event but not wait longer than the specified timeout for
111 If an event is received before the specified @a timeout expires, it is
112 processed and the function returns 1 normally or 0 if the event loop
113 should quite. Otherwise, i.e. if the timeout expires, the functions
114 returns -1 without processing any events.
117 The maximal time to wait for the events in milliseconds.
120 1 if an event was processed, 0 if the event loop should quit or -1
121 if the timeout expired.
123 virtual int DispatchTimeout(unsigned long timeout
) = 0;
126 Called by wxWidgets to wake up the event loop even if it is currently
127 blocked inside Dispatch().
129 virtual void WakeUp() = 0;
140 Process all pending events; it is necessary to call this function to
141 process posted events.
143 This happens during each event loop iteration in GUI mode but
144 it may be also called directly.
146 virtual void ProcessPendingEvents();
149 Returns @true if there are pending events on the internal pending event list.
151 bool HasPendingEvents() const;
154 Temporary suspends processing of the pending events.
156 @see ResumeProcessingOfPendingEvents()
158 void SuspendProcessingOfPendingEvents();
161 Resume processing of the pending events previously stopped because of a
162 call to SuspendProcessingOfPendingEvents().
164 void ResumeProcessingOfPendingEvents();
175 Makes sure that idle events are sent again.
177 virtual void WakeUpIdle();
180 This virtual function is called when the application becomes idle and
181 normally just sends wxIdleEvent to all interested parties.
183 It should return @true if more idle events are needed, @false if not.
185 virtual bool ProcessIdle();
191 @name Yield-related hooks
196 Returns @true if called from inside Yield().
198 virtual bool IsYielding() const;
201 Yields control to pending messages in the windowing system.
203 This can be useful, for example, when a time-consuming process writes to a
204 text window. Without an occasional yield, the text window will not be updated
205 properly, and on systems with cooperative multitasking, such as Windows 3.1
206 other processes will not respond.
208 Caution should be exercised, however, since yielding may allow the
209 user to perform actions which are not compatible with the current task.
210 Disabling menu items or whole menus during processing can avoid unwanted
211 reentrance of code: see ::wxSafeYield for a better function.
212 You can avoid unwanted reentrancies also using IsYielding().
214 Note that Yield() will not flush the message logs. This is intentional as
215 calling Yield() is usually done to quickly update the screen and popping up
216 a message box dialog may be undesirable. If you do wish to flush the log
217 messages immediately (otherwise it will be done during the next idle loop
218 iteration), call wxLog::FlushActive.
220 Calling Yield() recursively is normally an error and an assert failure is
221 raised in debug build if such situation is detected. However if the
222 @a onlyIfNeeded parameter is @true, the method will just silently
223 return @false instead.
225 bool Yield(bool onlyIfNeeded
= false);
228 Works like Yield() with @e onlyIfNeeded == @true, except that it allows
229 the caller to specify a mask of the ::wxEventCategory values which
230 indicates which events should be processed and which should instead
231 be "delayed" (i.e. processed by the main loop later).
233 Note that this is a safer alternative to Yield() since it ensures that
234 only the events you're interested to will be processed; i.e. this method
235 helps to avoid unwanted reentrancies.
237 Note that currently only wxMSW and wxGTK do support selective yield of
238 native events coming from the underlying GUI toolkit.
239 wxWidgets events posted using wxEvtHandler::AddPendingEvent or
240 wxEvtHandler::QueueEvent are instead selectively processed by all ports.
242 @see wxEvent::GetEventCategory
244 bool YieldFor(long eventsToProcess
);
247 Returns @true if the given event category is allowed inside
248 a YieldFor() call (i.e. compares the given category against the
249 last mask passed to YieldFor()).
251 @see wxEvent::GetEventCategory
253 virtual bool IsEventAllowedInsideYield(wxEventCategory cat
) const;
260 This function is called before the event loop terminates, whether this
261 happens normally (because of Exit() call) or abnormally (because of an
262 exception thrown from inside the loop).
264 Default version does nothing.
266 virtual void OnExit();
270 @class wxEventLoopActivator
272 Makes an event loop temporarily active.
274 This class is used to make the event loop active during its life-time,
277 class MyEventLoop : public wxEventLoopBase { ... };
282 wxEventLoopActivator activate(&loop);
285 } // the previously active event loop restored here
289 @category{appmanagement}
293 class wxEventLoopActivator
297 Makes the loop passed as the parameter currently active.
299 This saves the current return value of wxEventLoopBase::GetActive() and
300 then calls wxEventLoopBase::SetActive() with the given @a loop.
302 wxEventLoopActivator(wxEventLoopBase
*loop
);
305 Restores the previously active event loop stored by the constructor.
307 ~wxEventLoopActivator();