]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/common/appcmn.cpp | |
3 | // Purpose: wxAppConsole and wxAppBase methods common to all platforms | |
4 | // Author: Vadim Zeitlin | |
5 | // Modified by: | |
6 | // Created: 18.10.99 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Vadim Zeitlin | |
9 | // Licence: 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 | #if defined(__BORLANDC__) | |
24 | #pragma hdrstop | |
25 | #endif | |
26 | ||
27 | #ifndef WX_PRECOMP | |
28 | #include "wx/list.h" | |
29 | #include "wx/app.h" | |
30 | #include "wx/bitmap.h" | |
31 | #include "wx/intl.h" | |
32 | #include "wx/log.h" | |
33 | #include "wx/msgdlg.h" | |
34 | #include "wx/confbase.h" | |
35 | #include "wx/utils.h" | |
36 | #endif | |
37 | ||
38 | #include "wx/apptrait.h" | |
39 | #include "wx/cmdline.h" | |
40 | #include "wx/evtloop.h" | |
41 | #include "wx/msgout.h" | |
42 | #include "wx/thread.h" | |
43 | #include "wx/ptr_scpd.h" | |
44 | ||
45 | #if defined(__WXMSW__) | |
46 | #include "wx/msw/private.h" // includes windows.h for LOGFONT | |
47 | #endif | |
48 | ||
49 | #if wxUSE_FONTMAP | |
50 | #include "wx/fontmap.h" | |
51 | #endif // wxUSE_FONTMAP | |
52 | ||
53 | // DLL options compatibility check: | |
54 | #include "wx/build.h" | |
55 | WX_CHECK_BUILD_OPTIONS("wxCore") | |
56 | ||
57 | WXDLLIMPEXP_DATA_CORE(wxList) wxPendingDelete; | |
58 | ||
59 | // ---------------------------------------------------------------------------- | |
60 | // wxEventLoopPtr | |
61 | // ---------------------------------------------------------------------------- | |
62 | ||
63 | // this defines wxEventLoopPtr | |
64 | wxDEFINE_TIED_SCOPED_PTR_TYPE(wxEventLoop) | |
65 | ||
66 | // ============================================================================ | |
67 | // wxAppBase implementation | |
68 | // ============================================================================ | |
69 | ||
70 | // ---------------------------------------------------------------------------- | |
71 | // initialization | |
72 | // ---------------------------------------------------------------------------- | |
73 | ||
74 | wxAppBase::wxAppBase() | |
75 | { | |
76 | m_topWindow = (wxWindow *)NULL; | |
77 | m_useBestVisual = false; | |
78 | m_isActive = true; | |
79 | ||
80 | m_mainLoop = NULL; | |
81 | ||
82 | // We don't want to exit the app if the user code shows a dialog from its | |
83 | // OnInit() -- but this is what would happen if we set m_exitOnFrameDelete | |
84 | // to Yes initially as this dialog would be the last top level window. | |
85 | // OTOH, if we set it to No initially we'll have to overwrite it with Yes | |
86 | // when we enter our OnRun() because we do want the default behaviour from | |
87 | // then on. But this would be a problem if the user code calls | |
88 | // SetExitOnFrameDelete(false) from OnInit(). | |
89 | // | |
90 | // So we use the special "Later" value which is such that | |
91 | // GetExitOnFrameDelete() returns false for it but which we know we can | |
92 | // safely (i.e. without losing the effect of the users SetExitOnFrameDelete | |
93 | // call) overwrite in OnRun() | |
94 | m_exitOnFrameDelete = Later; | |
95 | } | |
96 | ||
97 | bool wxAppBase::Initialize(int& argcOrig, wxChar **argvOrig) | |
98 | { | |
99 | if ( !wxAppConsole::Initialize(argcOrig, argvOrig) ) | |
100 | return false; | |
101 | ||
102 | #if wxUSE_THREADS | |
103 | wxPendingEventsLocker = new wxCriticalSection; | |
104 | #endif | |
105 | ||
106 | wxInitializeStockLists(); | |
107 | ||
108 | wxBitmap::InitStandardHandlers(); | |
109 | ||
110 | return true; | |
111 | } | |
112 | ||
113 | // ---------------------------------------------------------------------------- | |
114 | // cleanup | |
115 | // ---------------------------------------------------------------------------- | |
116 | ||
117 | wxAppBase::~wxAppBase() | |
118 | { | |
119 | // this destructor is required for Darwin | |
120 | } | |
121 | ||
122 | void wxAppBase::CleanUp() | |
123 | { | |
124 | // clean up all the pending objects | |
125 | DeletePendingObjects(); | |
126 | ||
127 | // and any remaining TLWs (they remove themselves from wxTopLevelWindows | |
128 | // when destroyed, so iterate until none are left) | |
129 | while ( !wxTopLevelWindows.empty() ) | |
130 | { | |
131 | // do not use Destroy() here as it only puts the TLW in pending list | |
132 | // but we want to delete them now | |
133 | delete wxTopLevelWindows.GetFirst()->GetData(); | |
134 | } | |
135 | ||
136 | // undo everything we did in Initialize() above | |
137 | wxBitmap::CleanUpHandlers(); | |
138 | ||
139 | wxStockGDI::DeleteAll(); | |
140 | ||
141 | wxDeleteStockLists(); | |
142 | ||
143 | delete wxTheColourDatabase; | |
144 | wxTheColourDatabase = NULL; | |
145 | ||
146 | delete wxPendingEvents; | |
147 | wxPendingEvents = NULL; | |
148 | ||
149 | #if wxUSE_THREADS | |
150 | delete wxPendingEventsLocker; | |
151 | wxPendingEventsLocker = NULL; | |
152 | ||
153 | #if wxUSE_VALIDATORS | |
154 | // If we don't do the following, we get an apparent memory leak. | |
155 | ((wxEvtHandler&) wxDefaultValidator).ClearEventLocker(); | |
156 | #endif // wxUSE_VALIDATORS | |
157 | #endif // wxUSE_THREADS | |
158 | } | |
159 | ||
160 | #if wxUSE_CMDLINE_PARSER | |
161 | ||
162 | // ---------------------------------------------------------------------------- | |
163 | // GUI-specific command line options handling | |
164 | // ---------------------------------------------------------------------------- | |
165 | ||
166 | #define OPTION_THEME _T("theme") | |
167 | #define OPTION_MODE _T("mode") | |
168 | ||
169 | void wxAppBase::OnInitCmdLine(wxCmdLineParser& parser) | |
170 | { | |
171 | // first add the standard non GUI options | |
172 | wxAppConsole::OnInitCmdLine(parser); | |
173 | ||
174 | // the standard command line options | |
175 | static const wxCmdLineEntryDesc cmdLineGUIDesc[] = | |
176 | { | |
177 | #ifdef __WXUNIVERSAL__ | |
178 | { | |
179 | wxCMD_LINE_OPTION, | |
180 | wxEmptyString, | |
181 | OPTION_THEME, | |
182 | gettext_noop("specify the theme to use"), | |
183 | wxCMD_LINE_VAL_STRING, | |
184 | 0x0 | |
185 | }, | |
186 | #endif // __WXUNIVERSAL__ | |
187 | ||
188 | #if defined(__WXMGL__) | |
189 | // VS: this is not specific to wxMGL, all fullscreen (framebuffer) ports | |
190 | // should provide this option. That's why it is in common/appcmn.cpp | |
191 | // and not mgl/app.cpp | |
192 | { | |
193 | wxCMD_LINE_OPTION, | |
194 | wxEmptyString, | |
195 | OPTION_MODE, | |
196 | gettext_noop("specify display mode to use (e.g. 640x480-16)"), | |
197 | wxCMD_LINE_VAL_STRING, | |
198 | 0x0 | |
199 | }, | |
200 | #endif // __WXMGL__ | |
201 | ||
202 | // terminator | |
203 | { | |
204 | wxCMD_LINE_NONE, | |
205 | wxEmptyString, | |
206 | wxEmptyString, | |
207 | wxEmptyString, | |
208 | wxCMD_LINE_VAL_NONE, | |
209 | 0x0 | |
210 | } | |
211 | }; | |
212 | ||
213 | parser.SetDesc(cmdLineGUIDesc); | |
214 | } | |
215 | ||
216 | bool wxAppBase::OnCmdLineParsed(wxCmdLineParser& parser) | |
217 | { | |
218 | #ifdef __WXUNIVERSAL__ | |
219 | wxString themeName; | |
220 | if ( parser.Found(OPTION_THEME, &themeName) ) | |
221 | { | |
222 | wxTheme *theme = wxTheme::Create(themeName); | |
223 | if ( !theme ) | |
224 | { | |
225 | wxLogError(_("Unsupported theme '%s'."), themeName.c_str()); | |
226 | return false; | |
227 | } | |
228 | ||
229 | // Delete the defaultly created theme and set the new theme. | |
230 | delete wxTheme::Get(); | |
231 | wxTheme::Set(theme); | |
232 | } | |
233 | #endif // __WXUNIVERSAL__ | |
234 | ||
235 | #if defined(__WXMGL__) | |
236 | wxString modeDesc; | |
237 | if ( parser.Found(OPTION_MODE, &modeDesc) ) | |
238 | { | |
239 | unsigned w, h, bpp; | |
240 | if ( wxSscanf(modeDesc.c_str(), _T("%ux%u-%u"), &w, &h, &bpp) != 3 ) | |
241 | { | |
242 | wxLogError(_("Invalid display mode specification '%s'."), modeDesc.c_str()); | |
243 | return false; | |
244 | } | |
245 | ||
246 | if ( !SetDisplayMode(wxVideoMode(w, h, bpp)) ) | |
247 | return false; | |
248 | } | |
249 | #endif // __WXMGL__ | |
250 | ||
251 | return wxAppConsole::OnCmdLineParsed(parser); | |
252 | } | |
253 | ||
254 | #endif // wxUSE_CMDLINE_PARSER | |
255 | ||
256 | // ---------------------------------------------------------------------------- | |
257 | // main event loop implementation | |
258 | // ---------------------------------------------------------------------------- | |
259 | ||
260 | int wxAppBase::MainLoop() | |
261 | { | |
262 | wxEventLoopTiedPtr mainLoop(&m_mainLoop, new wxEventLoop); | |
263 | ||
264 | return m_mainLoop->Run(); | |
265 | } | |
266 | ||
267 | void wxAppBase::ExitMainLoop() | |
268 | { | |
269 | // we should exit from the main event loop, not just any currently active | |
270 | // (e.g. modal dialog) event loop | |
271 | if ( m_mainLoop && m_mainLoop->IsRunning() ) | |
272 | { | |
273 | m_mainLoop->Exit(0); | |
274 | } | |
275 | } | |
276 | ||
277 | bool wxAppBase::Pending() | |
278 | { | |
279 | // use the currently active message loop here, not m_mainLoop, because if | |
280 | // we're showing a modal dialog (with its own event loop) currently the | |
281 | // main event loop is not running anyhow | |
282 | wxEventLoop * const loop = wxEventLoop::GetActive(); | |
283 | ||
284 | return loop && loop->Pending(); | |
285 | } | |
286 | ||
287 | bool wxAppBase::Dispatch() | |
288 | { | |
289 | // see comment in Pending() | |
290 | wxEventLoop * const loop = wxEventLoop::GetActive(); | |
291 | ||
292 | return loop && loop->Dispatch(); | |
293 | } | |
294 | ||
295 | // ---------------------------------------------------------------------------- | |
296 | // OnXXX() hooks | |
297 | // ---------------------------------------------------------------------------- | |
298 | ||
299 | bool wxAppBase::OnInitGui() | |
300 | { | |
301 | #ifdef __WXUNIVERSAL__ | |
302 | if ( !wxTheme::Get() && !wxTheme::CreateDefault() ) | |
303 | return false; | |
304 | #endif // __WXUNIVERSAL__ | |
305 | ||
306 | return true; | |
307 | } | |
308 | ||
309 | int wxAppBase::OnRun() | |
310 | { | |
311 | // see the comment in ctor: if the initial value hasn't been changed, use | |
312 | // the default Yes from now on | |
313 | if ( m_exitOnFrameDelete == Later ) | |
314 | { | |
315 | m_exitOnFrameDelete = Yes; | |
316 | } | |
317 | //else: it has been changed, assume the user knows what he is doing | |
318 | ||
319 | return MainLoop(); | |
320 | } | |
321 | ||
322 | int wxAppBase::OnExit() | |
323 | { | |
324 | #ifdef __WXUNIVERSAL__ | |
325 | delete wxTheme::Set(NULL); | |
326 | #endif // __WXUNIVERSAL__ | |
327 | ||
328 | return wxAppConsole::OnExit(); | |
329 | } | |
330 | ||
331 | void wxAppBase::Exit() | |
332 | { | |
333 | ExitMainLoop(); | |
334 | } | |
335 | ||
336 | wxAppTraits *wxAppBase::CreateTraits() | |
337 | { | |
338 | return new wxGUIAppTraits; | |
339 | } | |
340 | ||
341 | // ---------------------------------------------------------------------------- | |
342 | // misc | |
343 | // ---------------------------------------------------------------------------- | |
344 | ||
345 | void wxAppBase::SetActive(bool active, wxWindow * WXUNUSED(lastFocus)) | |
346 | { | |
347 | if ( active == m_isActive ) | |
348 | return; | |
349 | ||
350 | m_isActive = active; | |
351 | ||
352 | wxActivateEvent event(wxEVT_ACTIVATE_APP, active); | |
353 | event.SetEventObject(this); | |
354 | ||
355 | (void)ProcessEvent(event); | |
356 | } | |
357 | ||
358 | // ---------------------------------------------------------------------------- | |
359 | // idle handling | |
360 | // ---------------------------------------------------------------------------- | |
361 | ||
362 | void wxAppBase::DeletePendingObjects() | |
363 | { | |
364 | wxList::compatibility_iterator node = wxPendingDelete.GetFirst(); | |
365 | while (node) | |
366 | { | |
367 | wxObject *obj = node->GetData(); | |
368 | ||
369 | delete obj; | |
370 | ||
371 | if (wxPendingDelete.Member(obj)) | |
372 | wxPendingDelete.Erase(node); | |
373 | ||
374 | // Deleting one object may have deleted other pending | |
375 | // objects, so start from beginning of list again. | |
376 | node = wxPendingDelete.GetFirst(); | |
377 | } | |
378 | } | |
379 | ||
380 | // Returns true if more time is needed. | |
381 | bool wxAppBase::ProcessIdle() | |
382 | { | |
383 | wxIdleEvent event; | |
384 | bool needMore = false; | |
385 | wxWindowList::compatibility_iterator node = wxTopLevelWindows.GetFirst(); | |
386 | while (node) | |
387 | { | |
388 | wxWindow* win = node->GetData(); | |
389 | if (SendIdleEvents(win, event)) | |
390 | needMore = true; | |
391 | node = node->GetNext(); | |
392 | } | |
393 | ||
394 | event.SetEventObject(this); | |
395 | (void) ProcessEvent(event); | |
396 | if (event.MoreRequested()) | |
397 | needMore = true; | |
398 | ||
399 | wxUpdateUIEvent::ResetUpdateTime(); | |
400 | ||
401 | return needMore; | |
402 | } | |
403 | ||
404 | // Send idle event to window and all subwindows | |
405 | bool wxAppBase::SendIdleEvents(wxWindow* win, wxIdleEvent& event) | |
406 | { | |
407 | bool needMore = false; | |
408 | ||
409 | win->OnInternalIdle(); | |
410 | ||
411 | if (wxIdleEvent::CanSend(win)) | |
412 | { | |
413 | event.SetEventObject(win); | |
414 | win->GetEventHandler()->ProcessEvent(event); | |
415 | ||
416 | if (event.MoreRequested()) | |
417 | needMore = true; | |
418 | } | |
419 | wxWindowList::compatibility_iterator node = win->GetChildren().GetFirst(); | |
420 | while ( node ) | |
421 | { | |
422 | wxWindow *child = node->GetData(); | |
423 | if (SendIdleEvents(child, event)) | |
424 | needMore = true; | |
425 | ||
426 | node = node->GetNext(); | |
427 | } | |
428 | ||
429 | return needMore; | |
430 | } | |
431 | ||
432 | void wxAppBase::OnIdle(wxIdleEvent& WXUNUSED(event)) | |
433 | { | |
434 | // If there are pending events, we must process them: pending events | |
435 | // are either events to the threads other than main or events posted | |
436 | // with wxPostEvent() functions | |
437 | // GRG: I have moved this here so that all pending events are processed | |
438 | // before starting to delete any objects. This behaves better (in | |
439 | // particular, wrt wxPostEvent) and is coherent with wxGTK's current | |
440 | // behaviour. Changed Feb/2000 before 2.1.14 | |
441 | ProcessPendingEvents(); | |
442 | ||
443 | // 'Garbage' collection of windows deleted with Close(). | |
444 | DeletePendingObjects(); | |
445 | ||
446 | #if wxUSE_LOG | |
447 | // flush the logged messages if any | |
448 | wxLog::FlushActive(); | |
449 | #endif // wxUSE_LOG | |
450 | ||
451 | } | |
452 | ||
453 | // ---------------------------------------------------------------------------- | |
454 | // exceptions support | |
455 | // ---------------------------------------------------------------------------- | |
456 | ||
457 | #if wxUSE_EXCEPTIONS | |
458 | ||
459 | bool wxAppBase::OnExceptionInMainLoop() | |
460 | { | |
461 | throw; | |
462 | ||
463 | // some compilers are too stupid to know that we never return after throw | |
464 | #if defined(__DMC__) || (defined(_MSC_VER) && _MSC_VER < 1200) | |
465 | return false; | |
466 | #endif | |
467 | } | |
468 | ||
469 | #endif // wxUSE_EXCEPTIONS | |
470 | ||
471 | // ---------------------------------------------------------------------------- | |
472 | // wxGUIAppTraitsBase | |
473 | // ---------------------------------------------------------------------------- | |
474 | ||
475 | #if wxUSE_LOG | |
476 | ||
477 | wxLog *wxGUIAppTraitsBase::CreateLogTarget() | |
478 | { | |
479 | #if wxUSE_LOGGUI | |
480 | return new wxLogGui; | |
481 | #else | |
482 | // we must have something! | |
483 | return new wxLogStderr; | |
484 | #endif | |
485 | } | |
486 | ||
487 | #endif // wxUSE_LOG | |
488 | ||
489 | wxMessageOutput *wxGUIAppTraitsBase::CreateMessageOutput() | |
490 | { | |
491 | // The standard way of printing help on command line arguments (app --help) | |
492 | // is (according to common practice): | |
493 | // - console apps: to stderr (on any platform) | |
494 | // - GUI apps: stderr on Unix platforms (!) | |
495 | // message box under Windows and others | |
496 | #ifdef __UNIX__ | |
497 | return new wxMessageOutputStderr; | |
498 | #else // !__UNIX__ | |
499 | // wxMessageOutputMessageBox doesn't work under Motif | |
500 | #ifdef __WXMOTIF__ | |
501 | return new wxMessageOutputLog; | |
502 | #else | |
503 | return new wxMessageOutputMessageBox; | |
504 | #endif | |
505 | #endif // __UNIX__/!__UNIX__ | |
506 | } | |
507 | ||
508 | #if wxUSE_FONTMAP | |
509 | ||
510 | wxFontMapper *wxGUIAppTraitsBase::CreateFontMapper() | |
511 | { | |
512 | return new wxFontMapper; | |
513 | } | |
514 | ||
515 | #endif // wxUSE_FONTMAP | |
516 | ||
517 | wxRendererNative *wxGUIAppTraitsBase::CreateRenderer() | |
518 | { | |
519 | // use the default native renderer by default | |
520 | return NULL; | |
521 | } | |
522 | ||
523 | #ifdef __WXDEBUG__ | |
524 | ||
525 | bool wxGUIAppTraitsBase::ShowAssertDialog(const wxString& msg) | |
526 | { | |
527 | // under MSW we prefer to use the base class version using ::MessageBox() | |
528 | // even if wxMessageBox() is available because it has less chances to | |
529 | // double fault our app than our wxMessageBox() | |
530 | #if defined(__WXMSW__) || !wxUSE_MSGDLG | |
531 | return wxAppTraitsBase::ShowAssertDialog(msg); | |
532 | #else // wxUSE_MSGDLG | |
533 | // this message is intentionally not translated -- it is for | |
534 | // developpers only | |
535 | wxString msgDlg(msg); | |
536 | msgDlg += wxT("\nDo you want to stop the program?\n") | |
537 | wxT("You can also choose [Cancel] to suppress ") | |
538 | wxT("further warnings."); | |
539 | ||
540 | switch ( wxMessageBox(msgDlg, wxT("wxWidgets Debug Alert"), | |
541 | wxYES_NO | wxCANCEL | wxICON_STOP ) ) | |
542 | { | |
543 | case wxYES: | |
544 | wxTrap(); | |
545 | break; | |
546 | ||
547 | case wxCANCEL: | |
548 | // no more asserts | |
549 | return true; | |
550 | ||
551 | //case wxNO: nothing to do | |
552 | } | |
553 | ||
554 | return false; | |
555 | #endif // !wxUSE_MSGDLG/wxUSE_MSGDLG | |
556 | } | |
557 | ||
558 | #endif // __WXDEBUG__ | |
559 | ||
560 | bool wxGUIAppTraitsBase::HasStderr() | |
561 | { | |
562 | // we consider that under Unix stderr always goes somewhere, even if the | |
563 | // user doesn't always see it under GUI desktops | |
564 | #ifdef __UNIX__ | |
565 | return true; | |
566 | #else | |
567 | return false; | |
568 | #endif | |
569 | } | |
570 | ||
571 | void wxGUIAppTraitsBase::ScheduleForDestroy(wxObject *object) | |
572 | { | |
573 | if ( !wxPendingDelete.Member(object) ) | |
574 | wxPendingDelete.Append(object); | |
575 | } | |
576 | ||
577 | void wxGUIAppTraitsBase::RemoveFromPendingDelete(wxObject *object) | |
578 | { | |
579 | wxPendingDelete.DeleteObject(object); | |
580 | } | |
581 | ||
582 | #if wxUSE_SOCKETS | |
583 | ||
584 | #if defined(__WINDOWS__) | |
585 | #include "wx/msw/gsockmsw.h" | |
586 | #elif defined(__UNIX__) || defined(__DARWIN__) || defined(__OS2__) | |
587 | #include "wx/unix/gsockunx.h" | |
588 | #elif defined(__WXMAC__) | |
589 | #include <MacHeaders.c> | |
590 | #define OTUNIXERRORS 1 | |
591 | #include <OpenTransport.h> | |
592 | #include <OpenTransportProviders.h> | |
593 | #include <OpenTptInternet.h> | |
594 | ||
595 | #include "wx/mac/gsockmac.h" | |
596 | #else | |
597 | #error "Must include correct GSocket header here" | |
598 | #endif | |
599 | ||
600 | GSocketGUIFunctionsTable* wxGUIAppTraitsBase::GetSocketGUIFunctionsTable() | |
601 | { | |
602 | #if defined(__WXMAC__) && !defined(__DARWIN__) | |
603 | // NB: wxMac CFM does not have any GUI-specific functions in gsocket.c and | |
604 | // so it doesn't need this table at all | |
605 | return NULL; | |
606 | #else // !__WXMAC__ || __DARWIN__ | |
607 | static GSocketGUIFunctionsTableConcrete table; | |
608 | return &table; | |
609 | #endif // !__WXMAC__ || __DARWIN__ | |
610 | } | |
611 | ||
612 | #endif |