evtloop.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/evtloop.h
  3. // Purpose: wxEventLoop and related classes
  4. // Author: Vadim Zeitlin
  5. // Copyright: (C) 2008 Vadim Zeitlin
  6. // Licence: wxWindows licence
  7. /////////////////////////////////////////////////////////////////////////////
  8. /**
  9. @class wxEventLoopBase
  10. Base class for all event loop implementations.
  11. An event loop is a class which queries the queue of native events sent
  12. to the wxWidgets application and dispatches them to the appropriate
  13. wxEvtHandlers.
  14. An object of this class is created by wxAppTraits::CreateEventLoop() and
  15. used by wxApp to run the main application event loop.
  16. Temporary event loops are usually created by wxDialog::ShowModal().
  17. You can create your own event loop if you need, provided that you restore
  18. the main event loop once yours is destroyed (see wxEventLoopActivator).
  19. Notice that there can be more than one event loop at any given moment, e.g.
  20. an event handler called from the main loop can show a modal dialog, which
  21. starts its own loop resulting in two nested loops, with the modal dialog
  22. being the active one (its IsRunning() returns @true). And a handler for a
  23. button inside the modal dialog can, of course, create another modal dialog
  24. with its own event loop and so on. So in general event loops form a stack
  25. and only the event loop at the top of the stack is considered to be active.
  26. It is also the only loop that can be directly asked to terminate by calling
  27. Exit() (which is done by wxDialog::EndModal()), an outer event loop can't
  28. be stopped while an inner one is still running. It is however possible to
  29. ask an outer event loop to terminate as soon as all its nested loops exit
  30. and the control returns back to it by using ScheduleExit().
  31. @library{wxbase}
  32. @category{appmanagement}
  33. @see wxApp, wxEventLoopActivator
  34. */
  35. class wxEventLoopBase
  36. {
  37. public:
  38. /**
  39. Return the currently active (running) event loop.
  40. May return @NULL if there is no active event loop (e.g. during
  41. application startup or shutdown).
  42. */
  43. static wxEventLoopBase *GetActive();
  44. /**
  45. Set currently active (running) event loop.
  46. Called by wxEventLoopActivator, use an instance of this class instead
  47. of calling this method directly to ensure that the previously active
  48. event loop is restored.
  49. Results in a call to wxAppConsole::OnEventLoopEnter.
  50. */
  51. static void SetActive(wxEventLoopBase* loop);
  52. /**
  53. Returns @true if this is the main loop executed by wxApp::OnRun().
  54. */
  55. bool IsMain() const;
  56. /**
  57. @name Dispatch and processing
  58. */
  59. //@{
  60. /**
  61. Start the event loop, return the exit code when it is finished.
  62. Logically, this method calls Dispatch() in a loop until it returns
  63. @false and also takes care of generating idle events during each loop
  64. iteration. However not all implementations of this class really
  65. implement it like this (e.g. wxGTK does not) so you shouldn't rely on
  66. Dispatch() being called from inside this function.
  67. @return The argument passed to Exit() which terminated this event loop.
  68. */
  69. virtual int Run() = 0;
  70. /**
  71. Return true if this event loop is currently running.
  72. Notice that even if this event loop hasn't terminated yet but has just
  73. spawned a nested (e.g. modal) event loop, this method would return
  74. @false.
  75. */
  76. bool IsRunning() const;
  77. /**
  78. Use this to check whether the event loop was successfully created
  79. before using it
  80. */
  81. virtual bool IsOk() const;
  82. /**
  83. Exit the currently running loop with the given exit code.
  84. The loop will exit, i.e. its Run() method will return, during the next
  85. event loop iteration.
  86. Notice that this method can only be used if this event loop is the
  87. currently running one, i.e. its IsRunning() returns @true. If this is
  88. not the case, an assert failure is triggered and nothing is done as
  89. outer event loops can't be exited from immediately. Use ScheduleExit()
  90. if you'd like to exit this loop even if it doesn't run currently.
  91. */
  92. virtual void Exit(int rc = 0);
  93. /**
  94. Schedule an exit from the loop with the given exit code.
  95. This method is similar to Exit() but can be called even if this event
  96. loop is not the currently running one -- and if it is the active loop,
  97. then it works in exactly the same way as Exit().
  98. The loop will exit as soon as the control flow returns to it, i.e.
  99. after any nested loops terminate.
  100. @since 2.9.5
  101. */
  102. virtual void ScheduleExit(int rc = 0) = 0;
  103. /**
  104. Return true if any events are available.
  105. If this method returns @true, calling Dispatch() will not block.
  106. */
  107. virtual bool Pending() const = 0;
  108. /**
  109. Dispatches the next event in the windowing system event queue.
  110. Blocks until an event appears if there are none currently
  111. (use Pending() if this is not wanted).
  112. This can be used for programming event loops, e.g.
  113. @code
  114. while (evtloop->Pending())
  115. evtloop->Dispatch();
  116. @endcode
  117. @return @false if the event loop should stop and @true otherwise.
  118. @see Pending(), wxEventLoopBase
  119. */
  120. virtual bool Dispatch() = 0;
  121. /**
  122. Dispatch an event but not wait longer than the specified timeout for
  123. it.
  124. If an event is received before the specified @a timeout expires, it is
  125. processed and the function returns 1 normally or 0 if the event loop
  126. should quite. Otherwise, i.e. if the timeout expires, the functions
  127. returns -1 without processing any events.
  128. @param timeout
  129. The maximal time to wait for the events in milliseconds.
  130. @return
  131. 1 if an event was processed, 0 if the event loop should quit or -1
  132. if the timeout expired.
  133. */
  134. virtual int DispatchTimeout(unsigned long timeout) = 0;
  135. /**
  136. Called by wxWidgets to wake up the event loop even if it is currently
  137. blocked inside Dispatch().
  138. */
  139. virtual void WakeUp() = 0;
  140. //@}
  141. /**
  142. @name Idle handling
  143. */
  144. //@{
  145. /**
  146. Makes sure that idle events are sent again.
  147. */
  148. virtual void WakeUpIdle();
  149. /**
  150. This virtual function is called when the application becomes idle and
  151. normally just sends wxIdleEvent to all interested parties.
  152. It should return @true if more idle events are needed, @false if not.
  153. */
  154. virtual bool ProcessIdle();
  155. //@}
  156. /**
  157. @name Yield-related hooks
  158. */
  159. //@{
  160. /**
  161. Returns @true if called from inside Yield() or from inside YieldFor().
  162. */
  163. virtual bool IsYielding() const;
  164. /**
  165. Yields control to pending messages in the windowing system.
  166. This can be useful, for example, when a time-consuming process writes to a
  167. text window. Without an occasional yield, the text window will not be updated
  168. properly, and on systems with cooperative multitasking, such as Windows 3.1
  169. other processes will not respond.
  170. Caution should be exercised, however, since yielding may allow the
  171. user to perform actions which are not compatible with the current task.
  172. Disabling menu items or whole menus during processing can avoid unwanted
  173. reentrance of code: see ::wxSafeYield for a better function.
  174. You can avoid unwanted reentrancies also using IsYielding().
  175. Note that Yield() will not flush the message logs. This is intentional as
  176. calling Yield() is usually done to quickly update the screen and popping up
  177. a message box dialog may be undesirable. If you do wish to flush the log
  178. messages immediately (otherwise it will be done during the next idle loop
  179. iteration), call wxLog::FlushActive.
  180. Calling Yield() recursively is normally an error and an assert failure is
  181. raised in debug build if such situation is detected. However if the
  182. @a onlyIfNeeded parameter is @true, the method will just silently
  183. return @false instead.
  184. */
  185. bool Yield(bool onlyIfNeeded = false);
  186. /**
  187. Works like Yield() with @e onlyIfNeeded == @true, except that it allows
  188. the caller to specify a mask of the ::wxEventCategory values which
  189. indicates which events should be processed and which should instead
  190. be "delayed" (i.e. processed by the main loop later).
  191. Note that this is a safer alternative to Yield() since it ensures that
  192. only the events you're interested to will be processed; i.e. this method
  193. helps to avoid unwanted reentrancies.
  194. Note that currently only wxMSW and wxGTK do support selective yield of
  195. native events coming from the underlying GUI toolkit.
  196. wxWidgets events posted using wxEvtHandler::AddPendingEvent or
  197. wxEvtHandler::QueueEvent are instead selectively processed by all ports.
  198. @see wxEvent::GetEventCategory
  199. */
  200. bool YieldFor(long eventsToProcess);
  201. /**
  202. Returns @true if the given event category is allowed inside
  203. a YieldFor() call (i.e. compares the given category against the
  204. last mask passed to YieldFor()).
  205. @see wxEvent::GetEventCategory
  206. */
  207. virtual bool IsEventAllowedInsideYield(wxEventCategory cat) const;
  208. //@}
  209. protected:
  210. /**
  211. This function is called before the event loop terminates, whether this
  212. happens normally (because of Exit() call) or abnormally (because of an
  213. exception thrown from inside the loop).
  214. The default implementation calls wxAppConsole::OnEventLoopExit.
  215. */
  216. virtual void OnExit();
  217. };
  218. /**
  219. @class wxEventLoopActivator
  220. Makes an event loop temporarily active.
  221. This class is used to make the event loop active during its life-time,
  222. e.g.:
  223. @code
  224. class MyEventLoop : public wxEventLoopBase { ... };
  225. void RunMyLoop()
  226. {
  227. MyEventLoop loop;
  228. wxEventLoopActivator activate(&loop);
  229. ...
  230. } // the previously active event loop restored here
  231. @endcode
  232. @library{wxbase}
  233. @category{appmanagement}
  234. @see wxEventLoopBase
  235. */
  236. class wxEventLoopActivator
  237. {
  238. public:
  239. /**
  240. Makes the loop passed as the parameter currently active.
  241. This saves the current return value of wxEventLoopBase::GetActive() and
  242. then calls wxEventLoopBase::SetActive() with the given @a loop.
  243. */
  244. wxEventLoopActivator(wxEventLoopBase *loop);
  245. /**
  246. Restores the previously active event loop stored by the constructor.
  247. */
  248. ~wxEventLoopActivator();
  249. };
  250. /**
  251. @class wxGUIEventLoop
  252. A generic implementation of the GUI event loop.
  253. @library{wxbase}
  254. @category{appmanagement}
  255. */
  256. class wxGUIEventLoop : public wxEventLoopBase
  257. {
  258. public:
  259. wxGUIEventLoop();
  260. virtual ~wxGUIEventLoop();
  261. };