1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/appcmn.cpp
3 // Purpose: wxAppConsole and wxAppBase methods common to all platforms
4 // Author: Vadim Zeitlin
8 // Copyright: (c) Vadim Zeitlin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ---------------------------------------------------------------------------
18 // ---------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
23 #if defined(__BORLANDC__)
29 #include "wx/bitmap.h"
33 #include "wx/msgdlg.h"
34 #include "wx/bitmap.h"
35 #include "wx/confbase.h"
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"
44 #include "wx/ptr_scpd.h"
46 #if defined(__WXMSW__)
47 #include "wx/msw/private.h" // includes windows.h for LOGFONT
51 #include "wx/fontmap.h"
52 #endif // wxUSE_FONTMAP
54 // DLL options compatibility check:
56 WX_CHECK_BUILD_OPTIONS("wxCore")
59 // ----------------------------------------------------------------------------
61 // ----------------------------------------------------------------------------
63 // this defines wxEventLoopPtr
64 wxDEFINE_TIED_SCOPED_PTR_TYPE(wxEventLoop
)
66 // define it here as we don't have common/evtloopcmn.cpp for now
67 wxEventLoop
*wxEventLoopBase::ms_activeLoop
= NULL
;
69 // ============================================================================
70 // wxAppBase implementation
71 // ============================================================================
73 // ----------------------------------------------------------------------------
75 // ----------------------------------------------------------------------------
77 wxAppBase::wxAppBase()
79 m_topWindow
= (wxWindow
*)NULL
;
80 m_useBestVisual
= false;
83 #if wxUSE_EVTLOOP_IN_APP
85 #endif // wxUSE_EVTLOOP_IN_APP
87 // We don't want to exit the app if the user code shows a dialog from its
88 // OnInit() -- but this is what would happen if we set m_exitOnFrameDelete
89 // to Yes initially as this dialog would be the last top level window.
90 // OTOH, if we set it to No initially we'll have to overwrite it with Yes
91 // when we enter our OnRun() because we do want the default behaviour from
92 // then on. But this would be a problem if the user code calls
93 // SetExitOnFrameDelete(false) from OnInit().
95 // So we use the special "Later" value which is such that
96 // GetExitOnFrameDelete() returns false for it but which we know we can
97 // safely (i.e. without losing the effect of the users SetExitOnFrameDelete
98 // call) overwrite in OnRun()
99 m_exitOnFrameDelete
= Later
;
102 bool wxAppBase::Initialize(int& argcOrig
, wxChar
**argvOrig
)
104 if ( !wxAppConsole::Initialize(argcOrig
, argvOrig
) )
108 wxPendingEventsLocker
= new wxCriticalSection
;
111 wxInitializeStockLists();
112 wxInitializeStockObjects();
114 wxBitmap::InitStandardHandlers();
119 // ----------------------------------------------------------------------------
121 // ----------------------------------------------------------------------------
123 wxAppBase::~wxAppBase()
125 // this destructor is required for Darwin
128 void wxAppBase::CleanUp()
130 // clean up all the pending objects
131 DeletePendingObjects();
133 // and any remaining TLWs (they remove themselves from wxTopLevelWindows
134 // when destroyed, so iterate until none are left)
135 while ( !wxTopLevelWindows
.empty() )
137 // do not use Destroy() here as it only puts the TLW in pending list
138 // but we want to delete them now
139 delete wxTopLevelWindows
.GetFirst()->GetData();
142 // undo everything we did in Initialize() above
143 wxBitmap::CleanUpHandlers();
145 wxDeleteStockObjects();
147 wxDeleteStockLists();
149 delete wxTheColourDatabase
;
150 wxTheColourDatabase
= NULL
;
152 delete wxPendingEvents
;
153 wxPendingEvents
= NULL
;
156 delete wxPendingEventsLocker
;
157 wxPendingEventsLocker
= NULL
;
160 // If we don't do the following, we get an apparent memory leak.
161 ((wxEvtHandler
&) wxDefaultValidator
).ClearEventLocker();
162 #endif // wxUSE_VALIDATORS
163 #endif // wxUSE_THREADS
166 #if wxUSE_CMDLINE_PARSER
168 // ----------------------------------------------------------------------------
169 // GUI-specific command line options handling
170 // ----------------------------------------------------------------------------
172 #define OPTION_THEME _T("theme")
173 #define OPTION_MODE _T("mode")
175 void wxAppBase::OnInitCmdLine(wxCmdLineParser
& parser
)
177 // first add the standard non GUI options
178 wxAppConsole::OnInitCmdLine(parser
);
180 // the standard command line options
181 static const wxCmdLineEntryDesc cmdLineGUIDesc
[] =
183 #ifdef __WXUNIVERSAL__
188 gettext_noop("specify the theme to use"),
189 wxCMD_LINE_VAL_STRING
,
192 #endif // __WXUNIVERSAL__
194 #if defined(__WXMGL__)
195 // VS: this is not specific to wxMGL, all fullscreen (framebuffer) ports
196 // should provide this option. That's why it is in common/appcmn.cpp
197 // and not mgl/app.cpp
202 gettext_noop("specify display mode to use (e.g. 640x480-16)"),
203 wxCMD_LINE_VAL_STRING
,
219 parser
.SetDesc(cmdLineGUIDesc
);
222 bool wxAppBase::OnCmdLineParsed(wxCmdLineParser
& parser
)
224 #ifdef __WXUNIVERSAL__
226 if ( parser
.Found(OPTION_THEME
, &themeName
) )
228 wxTheme
*theme
= wxTheme::Create(themeName
);
231 wxLogError(_("Unsupported theme '%s'."), themeName
.c_str());
235 // Delete the defaultly created theme and set the new theme.
236 delete wxTheme::Get();
239 #endif // __WXUNIVERSAL__
241 #if defined(__WXMGL__)
243 if ( parser
.Found(OPTION_MODE
, &modeDesc
) )
246 if ( wxSscanf(modeDesc
.c_str(), _T("%ux%u-%u"), &w
, &h
, &bpp
) != 3 )
248 wxLogError(_("Invalid display mode specification '%s'."), modeDesc
.c_str());
252 if ( !SetDisplayMode(wxVideoMode(w
, h
, bpp
)) )
257 return wxAppConsole::OnCmdLineParsed(parser
);
260 #endif // wxUSE_CMDLINE_PARSER
262 // ----------------------------------------------------------------------------
263 // main event loop implementation
264 // ----------------------------------------------------------------------------
266 int wxAppBase::MainLoop()
268 #if wxUSE_EVTLOOP_IN_APP
269 wxEventLoopTiedPtr
mainLoop(&m_mainLoop
, new wxEventLoop
);
271 return m_mainLoop
->Run();
272 #else // !wxUSE_EVTLOOP_IN_APP
274 #endif // wxUSE_EVTLOOP_IN_APP/!wxUSE_EVTLOOP_IN_APP
277 void wxAppBase::ExitMainLoop()
279 #if wxUSE_EVTLOOP_IN_APP
280 // we should exit from the main event loop, not just any currently active
281 // (e.g. modal dialog) event loop
282 if ( m_mainLoop
&& m_mainLoop
->IsRunning() )
286 #endif // wxUSE_EVTLOOP_IN_APP
289 bool wxAppBase::Pending()
291 #if wxUSE_EVTLOOP_IN_APP
292 // use the currently active message loop here, not m_mainLoop, because if
293 // we're showing a modal dialog (with its own event loop) currently the
294 // main event loop is not running anyhow
295 wxEventLoop
* const loop
= wxEventLoop::GetActive();
297 return loop
&& loop
->Pending();
298 #else // wxUSE_EVTLOOP_IN_APP
300 #endif // wxUSE_EVTLOOP_IN_APP/!wxUSE_EVTLOOP_IN_APP
303 bool wxAppBase::Dispatch()
305 #if wxUSE_EVTLOOP_IN_APP
306 // see comment in Pending()
307 wxEventLoop
* const loop
= wxEventLoop::GetActive();
309 return loop
&& loop
->Dispatch();
310 #else // wxUSE_EVTLOOP_IN_APP
312 #endif // wxUSE_EVTLOOP_IN_APP/!wxUSE_EVTLOOP_IN_APP
315 // ----------------------------------------------------------------------------
317 // ----------------------------------------------------------------------------
319 bool wxAppBase::OnInitGui()
321 #ifdef __WXUNIVERSAL__
322 if ( !wxTheme::Get() && !wxTheme::CreateDefault() )
324 #endif // __WXUNIVERSAL__
329 int wxAppBase::OnRun()
331 // see the comment in ctor: if the initial value hasn't been changed, use
332 // the default Yes from now on
333 if ( m_exitOnFrameDelete
== Later
)
335 m_exitOnFrameDelete
= Yes
;
337 //else: it has been changed, assume the user knows what he is doing
342 int wxAppBase::OnExit()
344 #ifdef __WXUNIVERSAL__
345 delete wxTheme::Set(NULL
);
346 #endif // __WXUNIVERSAL__
348 return wxAppConsole::OnExit();
351 void wxAppBase::Exit()
356 wxAppTraits
*wxAppBase::CreateTraits()
358 return new wxGUIAppTraits
;
361 // ----------------------------------------------------------------------------
363 // ----------------------------------------------------------------------------
365 void wxAppBase::SetActive(bool active
, wxWindow
* WXUNUSED(lastFocus
))
367 if ( active
== m_isActive
)
372 wxActivateEvent
event(wxEVT_ACTIVATE_APP
, active
);
373 event
.SetEventObject(this);
375 (void)ProcessEvent(event
);
378 // ----------------------------------------------------------------------------
380 // ----------------------------------------------------------------------------
382 void wxAppBase::DeletePendingObjects()
384 wxList::compatibility_iterator node
= wxPendingDelete
.GetFirst();
387 wxObject
*obj
= node
->GetData();
391 if (wxPendingDelete
.Member(obj
))
392 wxPendingDelete
.Erase(node
);
394 // Deleting one object may have deleted other pending
395 // objects, so start from beginning of list again.
396 node
= wxPendingDelete
.GetFirst();
400 // Returns true if more time is needed.
401 bool wxAppBase::ProcessIdle()
404 bool needMore
= false;
405 wxWindowList::compatibility_iterator node
= wxTopLevelWindows
.GetFirst();
408 wxWindow
* win
= node
->GetData();
409 if (SendIdleEvents(win
, event
))
411 node
= node
->GetNext();
414 event
.SetEventObject(this);
415 (void) ProcessEvent(event
);
416 if (event
.MoreRequested())
419 wxUpdateUIEvent::ResetUpdateTime();
424 // Send idle event to window and all subwindows
425 bool wxAppBase::SendIdleEvents(wxWindow
* win
, wxIdleEvent
& event
)
427 bool needMore
= false;
429 win
->OnInternalIdle();
431 if (wxIdleEvent::CanSend(win
))
433 event
.SetEventObject(win
);
434 win
->GetEventHandler()->ProcessEvent(event
);
436 if (event
.MoreRequested())
439 wxWindowList::compatibility_iterator node
= win
->GetChildren().GetFirst();
442 wxWindow
*child
= node
->GetData();
443 if (SendIdleEvents(child
, event
))
446 node
= node
->GetNext();
452 void wxAppBase::OnIdle(wxIdleEvent
& WXUNUSED(event
))
454 // If there are pending events, we must process them: pending events
455 // are either events to the threads other than main or events posted
456 // with wxPostEvent() functions
457 // GRG: I have moved this here so that all pending events are processed
458 // before starting to delete any objects. This behaves better (in
459 // particular, wrt wxPostEvent) and is coherent with wxGTK's current
460 // behaviour. Changed Feb/2000 before 2.1.14
461 ProcessPendingEvents();
463 // 'Garbage' collection of windows deleted with Close().
464 DeletePendingObjects();
467 // flush the logged messages if any
468 wxLog::FlushActive();
473 // ----------------------------------------------------------------------------
474 // exceptions support
475 // ----------------------------------------------------------------------------
479 bool wxAppBase::OnExceptionInMainLoop()
483 // some compilers are too stupid to know that we never return after throw
484 #if defined(__DMC__) || (defined(_MSC_VER) && _MSC_VER < 1200)
489 #endif // wxUSE_EXCEPTIONS
491 // ----------------------------------------------------------------------------
492 // wxGUIAppTraitsBase
493 // ----------------------------------------------------------------------------
497 wxLog
*wxGUIAppTraitsBase::CreateLogTarget()
502 // we must have something!
503 return new wxLogStderr
;
509 wxMessageOutput
*wxGUIAppTraitsBase::CreateMessageOutput()
511 // The standard way of printing help on command line arguments (app --help)
512 // is (according to common practice):
513 // - console apps: to stderr (on any platform)
514 // - GUI apps: stderr on Unix platforms (!)
515 // message box under Windows and others
517 return new wxMessageOutputStderr
;
519 // wxMessageOutputMessageBox doesn't work under Motif
521 return new wxMessageOutputLog
;
523 return new wxMessageOutputMessageBox
;
525 #endif // __UNIX__/!__UNIX__
530 wxFontMapper
*wxGUIAppTraitsBase::CreateFontMapper()
532 return new wxFontMapper
;
535 #endif // wxUSE_FONTMAP
537 wxRendererNative
*wxGUIAppTraitsBase::CreateRenderer()
539 // use the default native renderer by default
545 bool wxGUIAppTraitsBase::ShowAssertDialog(const wxString
& msg
)
547 // under MSW we prefer to use the base class version using ::MessageBox()
548 // even if wxMessageBox() is available because it has less chances to
549 // double fault our app than our wxMessageBox()
550 #if defined(__WXMSW__) || !wxUSE_MSGDLG
551 return wxAppTraitsBase::ShowAssertDialog(msg
);
552 #else // wxUSE_MSGDLG
553 // this message is intentionally not translated -- it is for
555 wxString
msgDlg(msg
);
556 msgDlg
+= wxT("\nDo you want to stop the program?\n")
557 wxT("You can also choose [Cancel] to suppress ")
558 wxT("further warnings.");
560 switch ( wxMessageBox(msgDlg
, wxT("wxWidgets Debug Alert"),
561 wxYES_NO
| wxCANCEL
| wxICON_STOP
) )
571 //case wxNO: nothing to do
575 #endif // !wxUSE_MSGDLG/wxUSE_MSGDLG
578 #endif // __WXDEBUG__
580 bool wxGUIAppTraitsBase::HasStderr()
582 // we consider that under Unix stderr always goes somewhere, even if the
583 // user doesn't always see it under GUI desktops
591 void wxGUIAppTraitsBase::ScheduleForDestroy(wxObject
*object
)
593 if ( !wxPendingDelete
.Member(object
) )
594 wxPendingDelete
.Append(object
);
597 void wxGUIAppTraitsBase::RemoveFromPendingDelete(wxObject
*object
)
599 wxPendingDelete
.DeleteObject(object
);
604 #if defined(__WINDOWS__)
605 #include "wx/msw/gsockmsw.h"
606 #elif defined(__UNIX__) || defined(__DARWIN__) || defined(__OS2__)
607 #include "wx/unix/gsockunx.h"
608 #elif defined(__WXMAC__)
609 #include <MacHeaders.c>
610 #define OTUNIXERRORS 1
611 #include <OpenTransport.h>
612 #include <OpenTransportProviders.h>
613 #include <OpenTptInternet.h>
615 #include "wx/mac/gsockmac.h"
617 #error "Must include correct GSocket header here"
620 GSocketGUIFunctionsTable
* wxGUIAppTraitsBase::GetSocketGUIFunctionsTable()
622 #if defined(__WXMAC__) && !defined(__DARWIN__)
623 // NB: wxMac CFM does not have any GUI-specific functions in gsocket.c and
624 // so it doesn't need this table at all
626 #else // !__WXMAC__ || __DARWIN__
627 static GSocketGUIFunctionsTableConcrete table
;
629 #endif // !__WXMAC__ || __DARWIN__