/src/wxwidgets/include/wx/app.h
Line | Count | Source |
1 | | ///////////////////////////////////////////////////////////////////////////// |
2 | | // Name: wx/app.h |
3 | | // Purpose: wxAppBase class and macros used for declaration of wxApp |
4 | | // derived class in the user code |
5 | | // Author: Julian Smart |
6 | | // Created: 01/02/97 |
7 | | // Copyright: (c) Julian Smart |
8 | | // Licence: wxWindows licence |
9 | | ///////////////////////////////////////////////////////////////////////////// |
10 | | |
11 | | #ifndef _WX_APP_H_BASE_ |
12 | | #define _WX_APP_H_BASE_ |
13 | | |
14 | | // ---------------------------------------------------------------------------- |
15 | | // headers we have to include here |
16 | | // ---------------------------------------------------------------------------- |
17 | | |
18 | | #include "wx/event.h" // for the base class |
19 | | #include "wx/eventfilter.h" // (and another one) |
20 | | #include "wx/build.h" |
21 | | #include "wx/cmdargs.h" // for wxCmdLineArgsArray used by wxApp::argv |
22 | | #include "wx/init.h" // we must declare wxEntry() |
23 | | #include "wx/intl.h" // for wxLayoutDirection |
24 | | #include "wx/log.h" // for wxDISABLE_DEBUG_LOGGING_IN_RELEASE_BUILD() |
25 | | |
26 | | class WXDLLIMPEXP_FWD_BASE wxAppConsole; |
27 | | class WXDLLIMPEXP_FWD_BASE wxAppTraits; |
28 | | class WXDLLIMPEXP_FWD_BASE wxCmdLineParser; |
29 | | class WXDLLIMPEXP_FWD_BASE wxEventLoopBase; |
30 | | class WXDLLIMPEXP_FWD_BASE wxMessageOutput; |
31 | | |
32 | | #if wxUSE_GUI |
33 | | struct WXDLLIMPEXP_FWD_CORE wxVideoMode; |
34 | | class WXDLLIMPEXP_FWD_CORE wxWindow; |
35 | | #endif |
36 | | |
37 | | // this macro should be used in any main() or equivalent functions defined in wx |
38 | | #define wxDISABLE_DEBUG_SUPPORT() \ |
39 | | wxDISABLE_ASSERTS_IN_RELEASE_BUILD(); \ |
40 | | wxDISABLE_DEBUG_LOGGING_IN_RELEASE_BUILD() |
41 | | |
42 | | // ---------------------------------------------------------------------------- |
43 | | // typedefs |
44 | | // ---------------------------------------------------------------------------- |
45 | | |
46 | | // the type of the function used to create a wxApp object on program start up |
47 | | typedef wxAppConsole* (*wxAppInitializerFunction)(); |
48 | | |
49 | | // ---------------------------------------------------------------------------- |
50 | | // constants |
51 | | // ---------------------------------------------------------------------------- |
52 | | |
53 | | enum |
54 | | { |
55 | | wxPRINT_WINDOWS = 1, |
56 | | wxPRINT_POSTSCRIPT = 2 |
57 | | }; |
58 | | |
59 | | // ---------------------------------------------------------------------------- |
60 | | // global variables |
61 | | // ---------------------------------------------------------------------------- |
62 | | |
63 | | // use of this list is strongly deprecated, use wxApp ScheduleForDestruction() |
64 | | // and IsScheduledForDestruction() methods instead of this list directly, it |
65 | | // is here for compatibility purposes only |
66 | | extern WXDLLIMPEXP_DATA_BASE(wxList) wxPendingDelete; |
67 | | |
68 | | // ---------------------------------------------------------------------------- |
69 | | // wxAppConsoleBase: wxApp for non-GUI applications |
70 | | // ---------------------------------------------------------------------------- |
71 | | |
72 | | class WXDLLIMPEXP_BASE wxAppConsoleBase : public wxEvtHandler, |
73 | | public wxEventFilter |
74 | | { |
75 | | public: |
76 | | // ctor and dtor |
77 | | wxAppConsoleBase(); |
78 | | virtual ~wxAppConsoleBase(); |
79 | | |
80 | | |
81 | | // the virtual functions which may/must be overridden in the derived class |
82 | | // ----------------------------------------------------------------------- |
83 | | |
84 | | // This is the very first function called for a newly created wxApp object, |
85 | | // it is used by the library to do the global initialization. If, for some |
86 | | // reason, you must override it (instead of just overriding OnInit(), as |
87 | | // usual, for app-specific initializations), do not forget to call the base |
88 | | // class version! |
89 | | virtual bool Initialize(int& argc, wxChar **argv); |
90 | | |
91 | | // This gives wxCocoa a chance to call OnInit() with a memory pool in place |
92 | 0 | virtual bool CallOnInit() { return OnInit(); } |
93 | | |
94 | | // Called before OnRun(), this is a good place to do initialization -- if |
95 | | // anything fails, return false from here to prevent the program from |
96 | | // continuing. The command line is normally parsed here, call the base |
97 | | // class OnInit() to do it. |
98 | | virtual bool OnInit(); |
99 | | |
100 | | // This is the replacement for the normal main(): all program work should |
101 | | // be done here. When OnRun() returns, the programs starts shutting down. |
102 | | virtual int OnRun(); |
103 | | |
104 | | // Called before the first events are handled, called from within MainLoop() |
105 | | virtual void OnLaunched(); |
106 | | |
107 | | // This is called by wxEventLoopBase::SetActive(): you should put the code |
108 | | // which needs an active event loop here. |
109 | | // Note that this function is called whenever an event loop is activated; |
110 | | // you may want to use wxEventLoopBase::IsMain() to perform initialization |
111 | | // specific for the app's main event loop. |
112 | 0 | virtual void OnEventLoopEnter(wxEventLoopBase* WXUNUSED(loop)) {} |
113 | | |
114 | | // This is only called if OnInit() returned true so it's a good place to do |
115 | | // any cleanup matching the initializations done there. |
116 | | virtual int OnExit(); |
117 | | |
118 | | // This is called by wxEventLoopBase::OnExit() for each event loop which |
119 | | // is exited. |
120 | 0 | virtual void OnEventLoopExit(wxEventLoopBase* WXUNUSED(loop)) {} |
121 | | |
122 | | // This is the very last function called on wxApp object before it is |
123 | | // destroyed. If you override it (instead of overriding OnExit() as usual) |
124 | | // do not forget to call the base class version! |
125 | | virtual void CleanUp(); |
126 | | |
127 | | // Called when a fatal exception occurs, this function should take care not |
128 | | // to do anything which might provoke a nested exception! It may be |
129 | | // overridden if you wish to react somehow in non-default way (core dump |
130 | | // under Unix, application crash under Windows) to fatal program errors, |
131 | | // however extreme care should be taken if you don't want this function to |
132 | | // crash. |
133 | 0 | virtual void OnFatalException() { } |
134 | | |
135 | | // Called from wxExit() function, should terminate the application a.s.a.p. |
136 | | virtual void Exit(); |
137 | | |
138 | | // Allows to set a custom process exit code if a fatal error happens. |
139 | | // This code is 255 by default, but can be changed if necessary, notably |
140 | | // set to -1 (which still maps to 255 under most systems, but to 127 when |
141 | | // using MSVC) if compatibility with the previous wx versions is important. |
142 | 0 | static void SetFatalErrorExitCode(int code) { ms_fatalErrorExitCode = code; } |
143 | 0 | static int GetFatalErrorExitCode() { return ms_fatalErrorExitCode; } |
144 | | |
145 | | // Allows to set a custom process exit code if OnInit() returns false. |
146 | | // By default, this exit code is 255, as for the fatal errors. |
147 | 0 | virtual void SetErrorExitCode(int code) { m_exitCode = code; } |
148 | 0 | int GetErrorExitCode() const { return m_exitCode; } |
149 | | |
150 | | |
151 | | // application info: name, description, vendor |
152 | | // ------------------------------------------- |
153 | | |
154 | | // NB: all these should be set by the application itself, there are no |
155 | | // reasonable default except for the application name which is taken to |
156 | | // be argv[0] |
157 | | |
158 | | // set/get the application name |
159 | | wxString GetAppName() const; |
160 | 0 | void SetAppName(const wxString& name) { m_appName = name; } |
161 | | |
162 | | // set/get the application display name: the display name is the name |
163 | | // shown to the user in titles, reports, etc while the app name is |
164 | | // used for paths, config, and other places the user doesn't see |
165 | | // |
166 | | // by default the display name is the same as app name or a capitalized |
167 | | // version of the program if app name was not set either but it's |
168 | | // usually better to set it explicitly to something nicer |
169 | | wxString GetAppDisplayName() const; |
170 | | |
171 | 0 | void SetAppDisplayName(const wxString& name) { m_appDisplayName = name; } |
172 | | |
173 | | // set/get the app class name |
174 | 0 | wxString GetClassName() const { return m_className; } |
175 | 0 | void SetClassName(const wxString& name) { m_className = name; } |
176 | | |
177 | | // set/get the vendor name |
178 | 0 | const wxString& GetVendorName() const { return m_vendorName; } |
179 | 0 | void SetVendorName(const wxString& name) { m_vendorName = name; } |
180 | | |
181 | | // set/get the vendor display name: the display name is shown |
182 | | // in titles/reports/dialogs to the user, while the vendor name |
183 | | // is used in some areas such as wxConfig, wxStandardPaths, etc |
184 | | const wxString& GetVendorDisplayName() const |
185 | 0 | { |
186 | 0 | return m_vendorDisplayName.empty() ? GetVendorName() |
187 | 0 | : m_vendorDisplayName; |
188 | 0 | } |
189 | | void SetVendorDisplayName(const wxString& name) |
190 | 0 | { |
191 | 0 | m_vendorDisplayName = name; |
192 | 0 | } |
193 | | |
194 | | |
195 | | // cmd line parsing stuff |
196 | | // ---------------------- |
197 | | |
198 | | // all of these methods may be overridden in the derived class to |
199 | | // customize the command line parsing (by default only a few standard |
200 | | // options are handled) |
201 | | // |
202 | | // you also need to call wxApp::OnInit() from YourApp::OnInit() for all |
203 | | // this to work |
204 | | |
205 | | #if wxUSE_CMDLINE_PARSER |
206 | | // this one is called from OnInit() to add all supported options |
207 | | // to the given parser (don't forget to call the base class version if you |
208 | | // override it!) |
209 | | virtual void OnInitCmdLine(wxCmdLineParser& parser); |
210 | | |
211 | | // called after successfully parsing the command line, return true |
212 | | // to continue and false to exit (don't forget to call the base class |
213 | | // version if you override it!) |
214 | | virtual bool OnCmdLineParsed(wxCmdLineParser& parser); |
215 | | |
216 | | // called if "--help" option was specified, return true to continue |
217 | | // and false to exit |
218 | | virtual bool OnCmdLineHelp(wxCmdLineParser& parser); |
219 | | |
220 | | // called if incorrect command line options were given, return |
221 | | // false to abort and true to continue |
222 | | virtual bool OnCmdLineError(wxCmdLineParser& parser); |
223 | | #endif // wxUSE_CMDLINE_PARSER |
224 | | |
225 | | |
226 | | // miscellaneous customization functions |
227 | | // ------------------------------------- |
228 | | |
229 | | // create the app traits object to which we delegate for everything which |
230 | | // either should be configurable by the user (then he can change the |
231 | | // default behaviour simply by overriding CreateTraits() and returning his |
232 | | // own traits object) or which is GUI/console dependent as then wxAppTraits |
233 | | // allows us to abstract the differences behind the common facade |
234 | | wxAppTraits *GetTraits(); |
235 | | |
236 | | // this function provides safer access to traits object than |
237 | | // wxTheApp->GetTraits() during startup or termination when the global |
238 | | // application object itself may be unavailable |
239 | | // |
240 | | // of course, it still returns nullptr in this case and the caller must check |
241 | | // for it |
242 | | static wxAppTraits *GetTraitsIfExists(); |
243 | | |
244 | | // Return some valid traits object. |
245 | | // |
246 | | // This method checks if we have wxTheApp and returns its traits if it does |
247 | | // exist and the traits are non-null, similarly to GetTraitsIfExists(), but |
248 | | // falls back to wxConsoleAppTraits to ensure that it always returns |
249 | | // something valid. |
250 | | static wxAppTraits& GetValidTraits(); |
251 | | |
252 | | // returns the main event loop instance, i.e. the event loop which is started |
253 | | // by OnRun() and which dispatches all events sent from the native toolkit |
254 | | // to the application (except when new event loops are temporarily set-up). |
255 | | // The returned value maybe null. Put initialization code which needs a |
256 | | // non-null main event loop into OnEventLoopEnter(). |
257 | | wxEventLoopBase* GetMainLoop() const |
258 | 0 | { return m_mainLoop; } |
259 | | |
260 | | // This function sets the C locale to the default locale for the current |
261 | | // environment. It is advised to call this to ensure that the underlying |
262 | | // toolkit uses the locale in which the numbers and monetary amounts are |
263 | | // shown in the format expected by user and so on. |
264 | | // |
265 | | // Notice that this does _not_ change the global C++ locale, you need to do |
266 | | // it explicitly if you want. |
267 | | // |
268 | | // Finally, notice that while this function is virtual, it is not supposed |
269 | | // to be overridden outside of the library itself. |
270 | | virtual void SetCLocale(); |
271 | | |
272 | | |
273 | | // event processing functions |
274 | | // -------------------------- |
275 | | |
276 | | // Implement the inherited wxEventFilter method but just return -1 from it |
277 | | // to indicate that default processing should take place. |
278 | | virtual int FilterEvent(wxEvent& event) override; |
279 | | |
280 | | // return true if we're running event loop, i.e. if the events can |
281 | | // (already) be dispatched |
282 | | static bool IsMainLoopRunning(); |
283 | | |
284 | | #if wxUSE_EXCEPTIONS |
285 | | // execute the functor to handle the given event |
286 | | // |
287 | | // this is a generalization of HandleEvent() below and the base class |
288 | | // implementation of CallEventHandler() still calls HandleEvent() for |
289 | | // compatibility for functors which are just wxEventFunctions (i.e. methods |
290 | | // of wxEvtHandler) |
291 | | virtual void CallEventHandler(wxEvtHandler *handler, |
292 | | wxEventFunctor& functor, |
293 | | wxEvent& event) const; |
294 | | |
295 | | // call the specified handler on the given object with the given event |
296 | | // |
297 | | // this method only exists to allow catching the exceptions thrown by any |
298 | | // event handler, it would lead to an extra (useless) virtual function call |
299 | | // if the exceptions were not used, so it doesn't even exist in that case |
300 | | virtual void HandleEvent(wxEvtHandler *handler, |
301 | | wxEventFunction func, |
302 | | wxEvent& event) const; |
303 | | |
304 | | // Called when an unhandled C++ exception occurs inside OnRun(): note that |
305 | | // the main event loop has already terminated by now and the program will |
306 | | // exit, if you need to really handle the exceptions you need to override |
307 | | // OnExceptionInMainLoop() |
308 | | virtual void OnUnhandledException(); |
309 | | |
310 | | // Call OnUnhandledException() on the current wxTheApp object if it exists. |
311 | | static void CallOnUnhandledException(); |
312 | | |
313 | | // Function called if an uncaught exception is caught inside the main |
314 | | // event loop: it may return true to continue running the event loop or |
315 | | // false to stop it. If this function rethrows the exception, as it does by |
316 | | // default, simply because there is no general way to handle exceptions, |
317 | | // StoreCurrentException() will be called to store it because in any case |
318 | | // the exception can't be allowed to escape. |
319 | | virtual bool OnExceptionInMainLoop(); |
320 | | |
321 | | // This function can be overridden to store the current exception, in view |
322 | | // of rethrowing it later when RethrowStoredException() is called. If the |
323 | | // exception was stored, return true. If the exception can't be stored, |
324 | | // i.e. if this function returns false, the program will abort after |
325 | | // calling OnUnhandledException(). |
326 | | // |
327 | | // The default implementation of this function when using C++98 compiler |
328 | | // just returns false, as there is no generic way to store an arbitrary |
329 | | // exception in C++98 and each application must do it on its own for the |
330 | | // exceptions it uses in its overridden version. When using C++11, the |
331 | | // default implementation uses std::current_exception() and returns true, |
332 | | // so it's normally not necessary to override this method when using C++11. |
333 | | virtual bool StoreCurrentException(); |
334 | | |
335 | | // If StoreCurrentException() is overridden, this function should be |
336 | | // overridden as well to rethrow the exceptions stored by it when the |
337 | | // control gets back to our code, i.e. when it's safe to do it. |
338 | | // |
339 | | // The default version does nothing when using C++98 and uses |
340 | | // std::rethrow_exception() in C++11. |
341 | | virtual void RethrowStoredException(); |
342 | | #else // !wxUSE_EXCEPTIONS |
343 | | // For convenience, still define this to allow the code using it to avoid |
344 | | // checking for wxUSE_EXCEPTIONS. |
345 | | static void CallOnUnhandledException() { } |
346 | | #endif // wxUSE_EXCEPTIONS/!wxUSE_EXCEPTIONS |
347 | | |
348 | | |
349 | | // pending events |
350 | | // -------------- |
351 | | |
352 | | // IMPORTANT: all these methods conceptually belong to wxEventLoopBase |
353 | | // but for many reasons we need to allow queuing of events |
354 | | // even when there's no event loop (e.g. in wxApp::OnInit); |
355 | | // this feature is used e.g. to queue events on secondary threads |
356 | | // or in wxPython to use wx.CallAfter before the GUI is initialized |
357 | | |
358 | | // process all events in the m_handlersWithPendingEvents list -- it is necessary |
359 | | // to call this function to process posted events. This happens during each |
360 | | // event loop iteration in GUI mode but if there is no main loop, it may be |
361 | | // also called directly. |
362 | | virtual void ProcessPendingEvents(); |
363 | | |
364 | | // check if there are pending events on global pending event list |
365 | | bool HasPendingEvents() const; |
366 | | |
367 | | // temporary suspends processing of the pending events |
368 | | void SuspendProcessingOfPendingEvents(); |
369 | | |
370 | | // resume processing of the pending events previously stopped because of a |
371 | | // call to SuspendProcessingOfPendingEvents() |
372 | | void ResumeProcessingOfPendingEvents(); |
373 | | |
374 | | // called by ~wxEvtHandler to (eventually) remove the handler from the list of |
375 | | // the handlers with pending events |
376 | | void RemovePendingEventHandler(wxEvtHandler* toRemove); |
377 | | |
378 | | // adds an event handler to the list of the handlers with pending events |
379 | | void AppendPendingEventHandler(wxEvtHandler* toAppend); |
380 | | |
381 | | // moves the event handler from the list of the handlers with pending events |
382 | | //to the list of the handlers with _delayed_ pending events |
383 | | void DelayPendingEventHandler(wxEvtHandler* toDelay); |
384 | | |
385 | | // deletes the current pending events |
386 | | void DeletePendingEvents(); |
387 | | |
388 | | |
389 | | // delayed destruction |
390 | | // ------------------- |
391 | | |
392 | | // If an object may have pending events for it, it shouldn't be deleted |
393 | | // immediately as this would result in a crash when trying to handle these |
394 | | // events: instead, it should be scheduled for destruction and really |
395 | | // destroyed only after processing all pending events. |
396 | | // |
397 | | // Notice that this is only possible if we have a running event loop, |
398 | | // otherwise the object is just deleted directly by ScheduleForDestruction() |
399 | | // and IsScheduledForDestruction() always returns false. |
400 | | |
401 | | // schedule the object for destruction in the near future |
402 | | void ScheduleForDestruction(wxObject *object); |
403 | | |
404 | | // return true if the object is scheduled for destruction |
405 | | bool IsScheduledForDestruction(wxObject *object) const; |
406 | | |
407 | | |
408 | | // wxEventLoop-related methods |
409 | | // --------------------------- |
410 | | |
411 | | // all these functions are forwarded to the corresponding methods of the |
412 | | // currently active event loop -- and do nothing if there is none |
413 | | virtual bool Pending(); |
414 | | virtual bool Dispatch(); |
415 | | |
416 | | virtual int MainLoop(); |
417 | | virtual void ExitMainLoop(); |
418 | | |
419 | | bool Yield(bool onlyIfNeeded = false); |
420 | | |
421 | | virtual void WakeUpIdle(); |
422 | | |
423 | | // this method is called by the active event loop when there are no events |
424 | | // to process |
425 | | // |
426 | | // by default it generates the idle events and if you override it in your |
427 | | // derived class you should call the base class version to ensure that idle |
428 | | // events are still sent out |
429 | | virtual bool ProcessIdle(); |
430 | | |
431 | | // this virtual function is overridden in GUI wxApp to always return true |
432 | | // as GUI applications always have an event loop -- but console ones may |
433 | | // have it or not, so it simply returns true if already have an event loop |
434 | | // running but false otherwise |
435 | | virtual bool UsesEventLoop() const; |
436 | | |
437 | | |
438 | | // debugging support |
439 | | // ----------------- |
440 | | |
441 | | // this function is called when an assert failure occurs, the base class |
442 | | // version does the normal processing (i.e. shows the usual assert failure |
443 | | // dialog box) |
444 | | // |
445 | | // the arguments are the location of the failed assert, the text of the |
446 | | // assert itself and the user-specified message |
447 | | virtual void OnAssertFailure(const wxChar *file, |
448 | | int line, |
449 | | const wxChar *func, |
450 | | const wxChar *cond, |
451 | | const wxChar *msg); |
452 | | |
453 | | // old version of the function without func parameter, for compatibility |
454 | | // only, override OnAssertFailure() in the new code |
455 | | virtual void OnAssert(const wxChar *file, |
456 | | int line, |
457 | | const wxChar *cond, |
458 | | const wxChar *msg); |
459 | | |
460 | | // check that the wxBuildOptions object (constructed in the application |
461 | | // itself, usually the one from wxIMPLEMENT_APP() macro) matches the build |
462 | | // options of the library and abort if it doesn't |
463 | | static bool CheckBuildOptions(const char *optionsSignature, |
464 | | const char *componentName); |
465 | | |
466 | | // implementation only from now on |
467 | | // ------------------------------- |
468 | | |
469 | | // helpers for dynamic wxApp construction |
470 | | static void SetInitializerFunction(wxAppInitializerFunction fn) |
471 | 0 | { ms_appInitFn = fn; } |
472 | | static wxAppInitializerFunction GetInitializerFunction() |
473 | 0 | { return ms_appInitFn; } |
474 | | |
475 | | // accessors for ms_appInstance field (external code might wish to modify |
476 | | // it, this is why we provide a setter here as well, but you should really |
477 | | // know what you're doing if you call it), wxTheApp is usually used instead |
478 | | // of GetInstance() |
479 | 0 | static wxAppConsole *GetInstance() { return ms_appInstance; } |
480 | 0 | static void SetInstance(wxAppConsole *app) { ms_appInstance = app; } |
481 | | |
482 | | // returns true for GUI wxApp subclasses |
483 | 0 | virtual bool IsGUI() const { return false; } |
484 | | |
485 | | |
486 | | // command line arguments (public for backwards compatibility) |
487 | | int argc; |
488 | | |
489 | | // this object is implicitly convertible to either "char**" (traditional |
490 | | // type of argv parameter of main()) or to "wchar_t **" (for compatibility |
491 | | // with Unicode build in previous wx versions and because the command line |
492 | | // can, in pr |
493 | | wxCmdLineArgsArray argv; |
494 | | |
495 | | protected: |
496 | | // This function must be called at the end of wxApp ctor to indicate that |
497 | | // wx part of the object is fully constructed. |
498 | | void WXAppConstructed(); |
499 | | |
500 | | // delete all objects in wxPendingDelete list |
501 | | // |
502 | | // called from ProcessPendingEvents() |
503 | | void DeletePendingObjects(); |
504 | | |
505 | | // the function which creates the traits object when GetTraits() needs it |
506 | | // for the first time |
507 | | virtual wxAppTraits *CreateTraits(); |
508 | | |
509 | | // function used for dynamic wxApp creation |
510 | | static wxAppInitializerFunction ms_appInitFn; |
511 | | |
512 | | // the one and only global application object |
513 | | static wxAppConsole *ms_appInstance; |
514 | | |
515 | | // create main loop from AppTraits or return nullptr if |
516 | | // there is no main loop implementation |
517 | | wxEventLoopBase *CreateMainLoop(); |
518 | | |
519 | | // application info (must be set from the user code) |
520 | | wxString m_vendorName, // vendor name ("acme") |
521 | | m_vendorDisplayName, // vendor display name (e.g. "ACME Inc") |
522 | | m_appName, // app name ("myapp") |
523 | | m_appDisplayName, // app display name ("My Application") |
524 | | m_className; // class name |
525 | | |
526 | | // allows customizing the application behaviour, created by GetTraits() |
527 | | // when first needed |
528 | | wxAppTraits *m_traits = nullptr; |
529 | | |
530 | | // the main event loop of the application (may be null if the loop hasn't |
531 | | // been started yet or has already terminated) |
532 | | wxEventLoopBase *m_mainLoop = nullptr; |
533 | | |
534 | | |
535 | | // pending events management vars: |
536 | | |
537 | | // the array of the handlers with pending events which needs to be processed |
538 | | // inside ProcessPendingEvents() |
539 | | wxEvtHandlerArray m_handlersWithPendingEvents; |
540 | | |
541 | | // helper array used by ProcessPendingEvents() to store the event handlers |
542 | | // which have pending events but of these events none can be processed right now |
543 | | // (because of a call to wxEventLoop::YieldFor() which asked to selectively process |
544 | | // pending events) |
545 | | wxEvtHandlerArray m_handlersWithPendingDelayedEvents; |
546 | | |
547 | | #if wxUSE_THREADS |
548 | | // this critical section protects both the lists above |
549 | | wxCriticalSection m_handlersWithPendingEventsLocker; |
550 | | #endif |
551 | | |
552 | | // flag modified by Suspend/ResumeProcessingOfPendingEvents() |
553 | | bool m_bDoPendingEventProcessing = true; |
554 | | |
555 | | private: |
556 | | // flag set to true at the end of wxApp ctor, call WXAppConstructed() to |
557 | | // set it |
558 | | bool m_fullyConstructed = false; |
559 | | |
560 | | // Exit code to use if a fatal error occurs when the application object |
561 | | // doesn't exist yet or is already destroyed. |
562 | | static int ms_fatalErrorExitCode; |
563 | | |
564 | | // Exit code to use if OnInit() returns false. |
565 | | int m_exitCode = ms_fatalErrorExitCode; |
566 | | |
567 | | |
568 | | friend class WXDLLIMPEXP_FWD_BASE wxEvtHandler; |
569 | | |
570 | | // the application object is a singleton anyhow, there is no sense in |
571 | | // copying it |
572 | | wxDECLARE_NO_COPY_CLASS(wxAppConsoleBase); |
573 | | }; |
574 | | |
575 | | #if defined(__UNIX__) && !defined(__WINDOWS__) |
576 | | #include "wx/unix/app.h" |
577 | | #else |
578 | | // this has to be a class and not a typedef as we forward declare it |
579 | | class wxAppConsole : public wxAppConsoleBase { }; |
580 | | #endif |
581 | | |
582 | | // ---------------------------------------------------------------------------- |
583 | | // wxAppBase: the common part of wxApp implementations for all platforms |
584 | | // ---------------------------------------------------------------------------- |
585 | | |
586 | | #if wxUSE_GUI |
587 | | |
588 | | class WXDLLIMPEXP_CORE wxAppBase : public wxAppConsole |
589 | | { |
590 | | public: |
591 | | wxAppBase(); |
592 | | virtual ~wxAppBase(); |
593 | | |
594 | | // the virtual functions which may/must be overridden in the derived class |
595 | | // ----------------------------------------------------------------------- |
596 | | |
597 | | // very first initialization function |
598 | | // |
599 | | // Override: very rarely |
600 | | virtual bool Initialize(int& argc, wxChar **argv) override; |
601 | | |
602 | | // a platform-dependent version of OnInit(): the code here is likely to |
603 | | // depend on the toolkit. default version does nothing. |
604 | | // |
605 | | // Override: rarely. |
606 | | virtual bool OnInitGui(); |
607 | | |
608 | | // called to start program execution - the default version just enters |
609 | | // the main GUI loop in which events are received and processed until |
610 | | // the last window is not deleted (if GetExitOnFrameDelete) or |
611 | | // ExitMainLoop() is called. In console mode programs, the execution |
612 | | // of the program really starts here |
613 | | // |
614 | | // Override: rarely in GUI applications, always in console ones. |
615 | | virtual int OnRun() override; |
616 | | |
617 | | // a matching function for OnInit() |
618 | | virtual int OnExit() override; |
619 | | |
620 | | // very last clean up function |
621 | | // |
622 | | // Override: very rarely |
623 | | virtual void CleanUp() override; |
624 | | |
625 | | |
626 | | // the worker functions - usually not used directly by the user code |
627 | | // ----------------------------------------------------------------- |
628 | | |
629 | | // safer alternatives to Yield(), using wxWindowDisabler |
630 | | virtual bool SafeYield(wxWindow *win, bool onlyIfNeeded); |
631 | | virtual bool SafeYieldFor(wxWindow *win, long eventsToProcess); |
632 | | |
633 | | // this virtual function is called in the GUI mode when the application |
634 | | // becomes idle and normally just sends wxIdleEvent to all interested |
635 | | // parties |
636 | | // |
637 | | // it should return true if more idle events are needed, false if not |
638 | | virtual bool ProcessIdle() override; |
639 | | |
640 | | // override base class version: GUI apps always use an event loop |
641 | | virtual bool UsesEventLoop() const override { return true; } |
642 | | |
643 | | |
644 | | // top level window functions |
645 | | // -------------------------- |
646 | | |
647 | | // return true if our app has focus |
648 | | virtual bool IsActive() const { return m_isActive; } |
649 | | |
650 | | // set the "main" top level window |
651 | | void SetTopWindow(wxWindow *win) { m_topWindow = win; } |
652 | | |
653 | | // return the "main" top level window (if it hadn't been set previously |
654 | | // with SetTopWindow(), will return just some top level window and, if |
655 | | // there are none, will return nullptr) |
656 | | virtual wxWindow *GetTopWindow() const; |
657 | | |
658 | | // convenient helper which is safe to use even if there is no wxApp at |
659 | | // all, it will just return nullptr in this case |
660 | | static wxWindow *GetMainTopWindow(); |
661 | | |
662 | | // control the exit behaviour: by default, the program will exit the |
663 | | // main loop (and so, usually, terminate) when the last top-level |
664 | | // program window is deleted. Beware that if you disable this behaviour |
665 | | // (with SetExitOnFrameDelete(false)), you'll have to call |
666 | | // ExitMainLoop() explicitly from somewhere. |
667 | | void SetExitOnFrameDelete(bool flag) |
668 | | { m_exitOnFrameDelete = flag ? Yes : No; } |
669 | | bool GetExitOnFrameDelete() const |
670 | | { return m_exitOnFrameDelete == Yes; } |
671 | | |
672 | | |
673 | | // display mode, visual, printing mode, ... |
674 | | // ------------------------------------------------------------------------ |
675 | | |
676 | | // Get display mode that is used use. This is only used in framebuffer |
677 | | // wxWin ports such as wxDFB. |
678 | | virtual wxVideoMode GetDisplayMode() const; |
679 | | // Set display mode to use. This is only used in framebuffer wxWin |
680 | | // ports such as wxDFB. This method should be called from |
681 | | // wxApp::OnInitGui |
682 | | virtual bool SetDisplayMode(const wxVideoMode& WXUNUSED(info)) { return true; } |
683 | | |
684 | | // set use of best visual flag (see below) |
685 | | void SetUseBestVisual( bool flag, bool forceTrueColour = false ) |
686 | | { m_useBestVisual = flag; m_forceTrueColour = forceTrueColour; } |
687 | | bool GetUseBestVisual() const { return m_useBestVisual; } |
688 | | |
689 | | // set/get printing mode: see wxPRINT_XXX constants. |
690 | | // |
691 | | // default behaviour is the normal one for Unix: always use PostScript |
692 | | // printing. |
693 | | virtual void SetPrintMode(int WXUNUSED(mode)) { } |
694 | | int GetPrintMode() const { return wxPRINT_POSTSCRIPT; } |
695 | | |
696 | | // Return the layout direction for the current locale or wxLayout_Default |
697 | | // if it's unknown |
698 | | virtual wxLayoutDirection GetLayoutDirection() const; |
699 | | |
700 | | // Change the theme used by the application, return true on success. |
701 | | virtual bool SetNativeTheme(const wxString& WXUNUSED(theme)) { return false; } |
702 | | |
703 | | // Request using system appearance (which is automatic for most platforms |
704 | | // but not MSW) or explicitly request dark or light appearance for this |
705 | | // application. |
706 | | enum class Appearance |
707 | | { |
708 | | System, |
709 | | Light, |
710 | | Dark |
711 | | }; |
712 | | |
713 | | enum class AppearanceResult |
714 | | { |
715 | | Failure, |
716 | | Ok, |
717 | | CannotChange |
718 | | }; |
719 | | |
720 | | virtual AppearanceResult SetAppearance(Appearance WXUNUSED(appearance)) |
721 | | { return AppearanceResult::Failure; } |
722 | | |
723 | | |
724 | | // command line parsing (GUI-specific) |
725 | | // ------------------------------------------------------------------------ |
726 | | |
727 | | #if wxUSE_CMDLINE_PARSER |
728 | | virtual bool OnCmdLineParsed(wxCmdLineParser& parser) override; |
729 | | virtual void OnInitCmdLine(wxCmdLineParser& parser) override; |
730 | | #endif |
731 | | |
732 | | // miscellaneous other stuff |
733 | | // ------------------------------------------------------------------------ |
734 | | |
735 | | // called by toolkit-specific code to set the app status: active (we have |
736 | | // focus) or not and also the last window which had focus before we were |
737 | | // deactivated |
738 | | virtual void SetActive(bool isActive, wxWindow *lastFocus); |
739 | | |
740 | | virtual bool IsGUI() const override { return true; } |
741 | | |
742 | | // returns non-null pointer only if we have a GUI application object: this |
743 | | // is only useful in the rare cases when the same code can be used in both |
744 | | // console and GUI applications, but needs to use GUI-specific functions if |
745 | | // the GUI is available |
746 | | static wxAppBase *GetGUIInstance() |
747 | | { |
748 | | return ms_appInstance && ms_appInstance->IsGUI() |
749 | | ? static_cast<wxAppBase*>(ms_appInstance) |
750 | | : nullptr; |
751 | | } |
752 | | |
753 | | protected: |
754 | | // override base class method to use GUI traits |
755 | | virtual wxAppTraits *CreateTraits() override; |
756 | | |
757 | | // Helper method deleting all existing top level windows: this is used |
758 | | // during the application shutdown. |
759 | | void DeleteAllTLWs(); |
760 | | |
761 | | |
762 | | // the main top level window (may be null) |
763 | | wxWindow *m_topWindow; |
764 | | |
765 | | // if Yes, exit the main loop when the last top level window is deleted, if |
766 | | // No don't do it and if Later -- only do it once we reach our OnRun() |
767 | | // |
768 | | // the explanation for using this strange scheme is given in appcmn.cpp |
769 | | enum |
770 | | { |
771 | | Later = -1, |
772 | | No, |
773 | | Yes |
774 | | } m_exitOnFrameDelete; |
775 | | |
776 | | // true if the app wants to use the best visual on systems where |
777 | | // more than one are available (Sun, SGI, XFree86 4.0 ?) |
778 | | bool m_useBestVisual; |
779 | | // force TrueColour just in case "best" isn't TrueColour |
780 | | bool m_forceTrueColour; |
781 | | |
782 | | // does any of our windows have focus? |
783 | | bool m_isActive; |
784 | | |
785 | | wxDECLARE_NO_COPY_CLASS(wxAppBase); |
786 | | }; |
787 | | |
788 | | // ---------------------------------------------------------------------------- |
789 | | // now include the declaration of the real class |
790 | | // ---------------------------------------------------------------------------- |
791 | | |
792 | | #if defined(__WXMSW__) |
793 | | #include "wx/msw/app.h" |
794 | | #elif defined(__WXDFB__) |
795 | | #include "wx/dfb/app.h" |
796 | | #elif defined(__WXGTK__) |
797 | | #include "wx/gtk/app.h" |
798 | | #elif defined(__WXX11__) |
799 | | #include "wx/x11/app.h" |
800 | | #elif defined(__WXMAC__) |
801 | | #include "wx/osx/app.h" |
802 | | #elif defined(__WXQT__) |
803 | | #include "wx/qt/app.h" |
804 | | #endif |
805 | | |
806 | | #else // !GUI |
807 | | |
808 | | // wxApp is defined in core and we cannot define another one in wxBase, |
809 | | // so use the preprocessor to allow using wxApp in console programs too |
810 | 0 | #define wxApp wxAppConsole |
811 | | |
812 | | #endif // GUI/!GUI |
813 | | |
814 | | // ---------------------------------------------------------------------------- |
815 | | // the global data |
816 | | // ---------------------------------------------------------------------------- |
817 | | |
818 | | // for compatibility, we define this macro to access the global application |
819 | | // object of type wxApp |
820 | | // |
821 | | // note that instead of using of wxTheApp in application code you should |
822 | | // consider using wxDECLARE_APP() after which you may call wxGetApp() which will |
823 | | // return the object of the correct type (i.e. MyApp and not wxApp) |
824 | | // |
825 | | // the cast is safe as in GUI build we only use wxApp, not wxAppConsole, and in |
826 | | // console mode it does nothing at all (but see also wxApp::GetGUIInstance()) |
827 | 0 | #define wxTheApp static_cast<wxApp*>(wxApp::GetInstance()) |
828 | | |
829 | | // ---------------------------------------------------------------------------- |
830 | | // global functions |
831 | | // ---------------------------------------------------------------------------- |
832 | | |
833 | | // event loop related functions only work in GUI programs |
834 | | // ------------------------------------------------------ |
835 | | |
836 | | // Force an exit from main loop |
837 | | WXDLLIMPEXP_BASE void wxExit(); |
838 | | |
839 | | // avoid redeclaring this function here if it had been already declared by |
840 | | // wx/utils.h, this results in warnings from g++ with -Wredundant-decls |
841 | | #ifndef wx_YIELD_DECLARED |
842 | | #define wx_YIELD_DECLARED |
843 | | |
844 | | // Yield to other apps/messages |
845 | | WXDLLIMPEXP_CORE bool wxYield(); |
846 | | |
847 | | #endif // wx_YIELD_DECLARED |
848 | | |
849 | | // Yield to other apps/messages |
850 | | WXDLLIMPEXP_BASE void wxWakeUpIdle(); |
851 | | |
852 | | // ---------------------------------------------------------------------------- |
853 | | // macros for dynamic creation of the application object |
854 | | // ---------------------------------------------------------------------------- |
855 | | |
856 | | // Having a global instance of this class allows wxApp to be aware of the app |
857 | | // creator function. wxApp can then call this function to create a new app |
858 | | // object. Convoluted, but necessary. |
859 | | |
860 | | class WXDLLIMPEXP_BASE wxAppInitializer |
861 | | { |
862 | | public: |
863 | | wxAppInitializer(wxAppInitializerFunction fn) |
864 | 0 | { wxApp::SetInitializerFunction(fn); } |
865 | | }; |
866 | | |
867 | | // the code below defines a wxIMPLEMENT_WXWIN_MAIN macro which you can use if |
868 | | // your compiler really, really wants main() to be in your main program (e.g. |
869 | | // hello.cpp). Now wxIMPLEMENT_APP should add this code if required. |
870 | | |
871 | | // For compilers that support it, prefer to use wmain() and let the CRT parse |
872 | | // the command line for us, for the others parse it ourselves under Windows to |
873 | | // ensure that wxWidgets console applications accept arbitrary Unicode strings |
874 | | // as command line parameters and not just those representable in the current |
875 | | // locale (under Unix UTF-8, capable of representing any Unicode string, is |
876 | | // almost always used and there is no way to retrieve the Unicode command line |
877 | | // anyhow). |
878 | | #if defined(__WINDOWS__) |
879 | | #ifdef __VISUALC__ |
880 | | #define wxIMPLEMENT_WXWIN_MAIN_CONSOLE \ |
881 | | int wmain(int argc, wchar_t **argv) \ |
882 | | { \ |
883 | | wxDISABLE_DEBUG_SUPPORT(); \ |
884 | | \ |
885 | | return wxEntry(argc, argv); \ |
886 | | } |
887 | | #else // No wmain(), use main() but don't trust its arguments. |
888 | | #define wxIMPLEMENT_WXWIN_MAIN_CONSOLE \ |
889 | | int main(int, char **) \ |
890 | | { \ |
891 | | wxDISABLE_DEBUG_SUPPORT(); \ |
892 | | \ |
893 | | return wxEntry(); \ |
894 | | } |
895 | | #endif |
896 | | #else // Use standard main() |
897 | | #define wxIMPLEMENT_WXWIN_MAIN_CONSOLE \ |
898 | | int main(int argc, char **argv) \ |
899 | | { \ |
900 | | wxDISABLE_DEBUG_SUPPORT(); \ |
901 | | \ |
902 | | return wxEntry(argc, argv); \ |
903 | | } |
904 | | #endif |
905 | | |
906 | | // port-specific header could have defined it already in some special way |
907 | | #ifndef wxIMPLEMENT_WXWIN_MAIN |
908 | | #define wxIMPLEMENT_WXWIN_MAIN wxIMPLEMENT_WXWIN_MAIN_CONSOLE |
909 | | #endif // defined(wxIMPLEMENT_WXWIN_MAIN) |
910 | | |
911 | | #if defined(__WXUNIVERSAL__) && wxUSE_GUI |
912 | | #include "wx/univ/theme.h" |
913 | | |
914 | | #ifdef wxUNIV_DEFAULT_THEME |
915 | | #define wxIMPLEMENT_WX_THEME_SUPPORT \ |
916 | | WX_USE_THEME(wxUNIV_DEFAULT_THEME); |
917 | | #else |
918 | | #define wxIMPLEMENT_WX_THEME_SUPPORT |
919 | | #endif |
920 | | #else |
921 | | #define wxIMPLEMENT_WX_THEME_SUPPORT |
922 | | #endif |
923 | | |
924 | | // Use this macro if you want to define your own main() or WinMain() function |
925 | | // and call wxEntry() from there. |
926 | | #define wxIMPLEMENT_APP_NO_MAIN(appname) \ |
927 | | appname& wxGetApp() { return *static_cast<appname*>(wxApp::GetInstance()); } \ |
928 | | wxAppConsole *wxCreateApp() \ |
929 | | { \ |
930 | | wxAppConsole::CheckBuildOptions(WX_BUILD_OPTIONS_SIGNATURE, \ |
931 | | "your program"); \ |
932 | | return new appname; \ |
933 | | } \ |
934 | | wxAppInitializer \ |
935 | | wxTheAppInitializer((wxAppInitializerFunction) wxCreateApp) |
936 | | |
937 | | // Same as wxIMPLEMENT_APP() normally but doesn't include themes support in |
938 | | // wxUniversal builds |
939 | | #define wxIMPLEMENT_APP_NO_THEMES(appname) \ |
940 | | wxIMPLEMENT_WXWIN_MAIN \ |
941 | | wxIMPLEMENT_APP_NO_MAIN(appname) |
942 | | |
943 | | // Use this macro exactly once, the argument is the name of the wxApp-derived |
944 | | // class which is the class of your application. |
945 | | #define wxIMPLEMENT_APP(appname) \ |
946 | | wxIMPLEMENT_WX_THEME_SUPPORT \ |
947 | | wxIMPLEMENT_APP_NO_THEMES(appname) |
948 | | |
949 | | // Same as wxIMPLEMENT_APP(), but for console applications. |
950 | | #define wxIMPLEMENT_APP_CONSOLE(appname) \ |
951 | | wxIMPLEMENT_WXWIN_MAIN_CONSOLE \ |
952 | | wxIMPLEMENT_APP_NO_MAIN(appname) |
953 | | |
954 | | // this macro can be used multiple times and just allows you to use wxGetApp() |
955 | | // function |
956 | | #define wxDECLARE_APP(appname) \ |
957 | | extern appname& wxGetApp() |
958 | | |
959 | | |
960 | | // declare the stuff defined by wxIMPLEMENT_APP() macro, it's not really needed |
961 | | // anywhere else but at the very least it suppresses icc warnings about |
962 | | // defining extern symbols without prior declaration, and it shouldn't do any |
963 | | // harm |
964 | | extern wxAppConsole *wxCreateApp(); |
965 | | extern wxAppInitializer wxTheAppInitializer; |
966 | | |
967 | | // ---------------------------------------------------------------------------- |
968 | | // Compatibility macro aliases |
969 | | // ---------------------------------------------------------------------------- |
970 | | |
971 | | // deprecated variants _not_ requiring a semicolon after them |
972 | | // (note that also some wx-prefixed macro do _not_ require a semicolon because |
973 | | // it's not always possible to force the compiler to require it) |
974 | | |
975 | | #define IMPLEMENT_WXWIN_MAIN_CONSOLE wxIMPLEMENT_WXWIN_MAIN_CONSOLE |
976 | | #define IMPLEMENT_WXWIN_MAIN wxIMPLEMENT_WXWIN_MAIN |
977 | | #define IMPLEMENT_WX_THEME_SUPPORT wxIMPLEMENT_WX_THEME_SUPPORT |
978 | | #define IMPLEMENT_APP_NO_MAIN(app) wxIMPLEMENT_APP_NO_MAIN(app); |
979 | | #define IMPLEMENT_APP_NO_THEMES(app) wxIMPLEMENT_APP_NO_THEMES(app); |
980 | | #define IMPLEMENT_APP(app) wxIMPLEMENT_APP(app); |
981 | | #define IMPLEMENT_APP_CONSOLE(app) wxIMPLEMENT_APP_CONSOLE(app); |
982 | | #define DECLARE_APP(app) wxDECLARE_APP(app); |
983 | | |
984 | | #endif // _WX_APP_H_BASE_ |