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;
85 // We don't want to exit the app if the user code shows a dialog from its
86 // OnInit() -- but this is what would happen if we set m_exitOnFrameDelete
87 // to Yes initially as this dialog would be the last top level window.
88 // OTOH, if we set it to No initially we'll have to overwrite it with Yes
89 // when we enter our OnRun() because we do want the default behaviour from
90 // then on. But this would be a problem if the user code calls
91 // SetExitOnFrameDelete(false) from OnInit().
93 // So we use the special "Later" value which is such that
94 // GetExitOnFrameDelete() returns false for it but which we know we can
95 // safely (i.e. without losing the effect of the users SetExitOnFrameDelete
96 // call) overwrite in OnRun()
97 m_exitOnFrameDelete
= Later
;
100 bool wxAppBase::Initialize(int& argcOrig
, wxChar
**argvOrig
)
102 if ( !wxAppConsole::Initialize(argcOrig
, argvOrig
) )
106 wxPendingEventsLocker
= new wxCriticalSection
;
109 wxInitializeStockLists();
110 wxInitializeStockObjects();
112 wxBitmap::InitStandardHandlers();
117 // ----------------------------------------------------------------------------
119 // ----------------------------------------------------------------------------
121 wxAppBase::~wxAppBase()
123 // this destructor is required for Darwin
126 void wxAppBase::CleanUp()
128 // clean up all the pending objects
129 DeletePendingObjects();
131 // and any remaining TLWs (they remove themselves from wxTopLevelWindows
132 // when destroyed, so iterate until none are left)
133 while ( !wxTopLevelWindows
.empty() )
135 // do not use Destroy() here as it only puts the TLW in pending list
136 // but we want to delete them now
137 delete wxTopLevelWindows
.GetFirst()->GetData();
140 // undo everything we did in Initialize() above
141 wxBitmap::CleanUpHandlers();
143 wxDeleteStockObjects();
145 wxDeleteStockLists();
147 delete wxTheColourDatabase
;
148 wxTheColourDatabase
= NULL
;
150 delete wxPendingEvents
;
151 wxPendingEvents
= NULL
;
154 delete wxPendingEventsLocker
;
155 wxPendingEventsLocker
= NULL
;
158 // If we don't do the following, we get an apparent memory leak.
159 ((wxEvtHandler
&) wxDefaultValidator
).ClearEventLocker();
160 #endif // wxUSE_VALIDATORS
161 #endif // wxUSE_THREADS
164 #if wxUSE_CMDLINE_PARSER
166 // ----------------------------------------------------------------------------
167 // GUI-specific command line options handling
168 // ----------------------------------------------------------------------------
170 #define OPTION_THEME _T("theme")
171 #define OPTION_MODE _T("mode")
173 void wxAppBase::OnInitCmdLine(wxCmdLineParser
& parser
)
175 // first add the standard non GUI options
176 wxAppConsole::OnInitCmdLine(parser
);
178 // the standard command line options
179 static const wxCmdLineEntryDesc cmdLineGUIDesc
[] =
181 #ifdef __WXUNIVERSAL__
186 gettext_noop("specify the theme to use"),
187 wxCMD_LINE_VAL_STRING
,
190 #endif // __WXUNIVERSAL__
192 #if defined(__WXMGL__)
193 // VS: this is not specific to wxMGL, all fullscreen (framebuffer) ports
194 // should provide this option. That's why it is in common/appcmn.cpp
195 // and not mgl/app.cpp
200 gettext_noop("specify display mode to use (e.g. 640x480-16)"),
201 wxCMD_LINE_VAL_STRING
,
217 parser
.SetDesc(cmdLineGUIDesc
);
220 bool wxAppBase::OnCmdLineParsed(wxCmdLineParser
& parser
)
222 #ifdef __WXUNIVERSAL__
224 if ( parser
.Found(OPTION_THEME
, &themeName
) )
226 wxTheme
*theme
= wxTheme::Create(themeName
);
229 wxLogError(_("Unsupported theme '%s'."), themeName
.c_str());
233 // Delete the defaultly created theme and set the new theme.
234 delete wxTheme::Get();
237 #endif // __WXUNIVERSAL__
239 #if defined(__WXMGL__)
241 if ( parser
.Found(OPTION_MODE
, &modeDesc
) )
244 if ( wxSscanf(modeDesc
.c_str(), _T("%ux%u-%u"), &w
, &h
, &bpp
) != 3 )
246 wxLogError(_("Invalid display mode specification '%s'."), modeDesc
.c_str());
250 if ( !SetDisplayMode(wxVideoMode(w
, h
, bpp
)) )
255 return wxAppConsole::OnCmdLineParsed(parser
);
258 #endif // wxUSE_CMDLINE_PARSER
260 // ----------------------------------------------------------------------------
261 // main event loop implementation
262 // ----------------------------------------------------------------------------
264 int wxAppBase::MainLoop()
266 wxEventLoopTiedPtr
mainLoop(&m_mainLoop
, new wxEventLoop
);
268 return m_mainLoop
->Run();
271 void wxAppBase::ExitMainLoop()
273 // we should exit from the main event loop, not just any currently active
274 // (e.g. modal dialog) event loop
275 if ( m_mainLoop
&& m_mainLoop
->IsRunning() )
281 bool wxAppBase::Pending()
283 // use the currently active message loop here, not m_mainLoop, because if
284 // we're showing a modal dialog (with its own event loop) currently the
285 // main event loop is not running anyhow
286 wxEventLoop
* const loop
= wxEventLoop::GetActive();
288 return loop
&& loop
->Pending();
291 bool wxAppBase::Dispatch()
293 // see comment in Pending()
294 wxEventLoop
* const loop
= wxEventLoop::GetActive();
296 return loop
&& loop
->Dispatch();
299 // ----------------------------------------------------------------------------
301 // ----------------------------------------------------------------------------
303 bool wxAppBase::OnInitGui()
305 #ifdef __WXUNIVERSAL__
306 if ( !wxTheme::Get() && !wxTheme::CreateDefault() )
308 #endif // __WXUNIVERSAL__
313 int wxAppBase::OnRun()
315 // see the comment in ctor: if the initial value hasn't been changed, use
316 // the default Yes from now on
317 if ( m_exitOnFrameDelete
== Later
)
319 m_exitOnFrameDelete
= Yes
;
321 //else: it has been changed, assume the user knows what he is doing
326 int wxAppBase::OnExit()
328 #ifdef __WXUNIVERSAL__
329 delete wxTheme::Set(NULL
);
330 #endif // __WXUNIVERSAL__
332 return wxAppConsole::OnExit();
335 void wxAppBase::Exit()
340 wxAppTraits
*wxAppBase::CreateTraits()
342 return new wxGUIAppTraits
;
345 // ----------------------------------------------------------------------------
347 // ----------------------------------------------------------------------------
349 void wxAppBase::SetActive(bool active
, wxWindow
* WXUNUSED(lastFocus
))
351 if ( active
== m_isActive
)
356 wxActivateEvent
event(wxEVT_ACTIVATE_APP
, active
);
357 event
.SetEventObject(this);
359 (void)ProcessEvent(event
);
362 // ----------------------------------------------------------------------------
364 // ----------------------------------------------------------------------------
366 void wxAppBase::DeletePendingObjects()
368 wxList::compatibility_iterator node
= wxPendingDelete
.GetFirst();
371 wxObject
*obj
= node
->GetData();
375 if (wxPendingDelete
.Member(obj
))
376 wxPendingDelete
.Erase(node
);
378 // Deleting one object may have deleted other pending
379 // objects, so start from beginning of list again.
380 node
= wxPendingDelete
.GetFirst();
384 // Returns true if more time is needed.
385 bool wxAppBase::ProcessIdle()
388 bool needMore
= false;
389 wxWindowList::compatibility_iterator node
= wxTopLevelWindows
.GetFirst();
392 wxWindow
* win
= node
->GetData();
393 if (SendIdleEvents(win
, event
))
395 node
= node
->GetNext();
398 event
.SetEventObject(this);
399 (void) ProcessEvent(event
);
400 if (event
.MoreRequested())
403 wxUpdateUIEvent::ResetUpdateTime();
408 // Send idle event to window and all subwindows
409 bool wxAppBase::SendIdleEvents(wxWindow
* win
, wxIdleEvent
& event
)
411 bool needMore
= false;
413 win
->OnInternalIdle();
415 if (wxIdleEvent::CanSend(win
))
417 event
.SetEventObject(win
);
418 win
->GetEventHandler()->ProcessEvent(event
);
420 if (event
.MoreRequested())
423 wxWindowList::compatibility_iterator node
= win
->GetChildren().GetFirst();
426 wxWindow
*child
= node
->GetData();
427 if (SendIdleEvents(child
, event
))
430 node
= node
->GetNext();
436 void wxAppBase::OnIdle(wxIdleEvent
& WXUNUSED(event
))
438 // If there are pending events, we must process them: pending events
439 // are either events to the threads other than main or events posted
440 // with wxPostEvent() functions
441 // GRG: I have moved this here so that all pending events are processed
442 // before starting to delete any objects. This behaves better (in
443 // particular, wrt wxPostEvent) and is coherent with wxGTK's current
444 // behaviour. Changed Feb/2000 before 2.1.14
445 ProcessPendingEvents();
447 // 'Garbage' collection of windows deleted with Close().
448 DeletePendingObjects();
451 // flush the logged messages if any
452 wxLog::FlushActive();
457 // ----------------------------------------------------------------------------
458 // exceptions support
459 // ----------------------------------------------------------------------------
463 bool wxAppBase::OnExceptionInMainLoop()
467 // some compilers are too stupid to know that we never return after throw
468 #if defined(__DMC__) || (defined(_MSC_VER) && _MSC_VER < 1200)
473 #endif // wxUSE_EXCEPTIONS
475 // ----------------------------------------------------------------------------
476 // wxGUIAppTraitsBase
477 // ----------------------------------------------------------------------------
481 wxLog
*wxGUIAppTraitsBase::CreateLogTarget()
486 // we must have something!
487 return new wxLogStderr
;
493 wxMessageOutput
*wxGUIAppTraitsBase::CreateMessageOutput()
495 // The standard way of printing help on command line arguments (app --help)
496 // is (according to common practice):
497 // - console apps: to stderr (on any platform)
498 // - GUI apps: stderr on Unix platforms (!)
499 // message box under Windows and others
501 return new wxMessageOutputStderr
;
503 // wxMessageOutputMessageBox doesn't work under Motif
505 return new wxMessageOutputLog
;
507 return new wxMessageOutputMessageBox
;
509 #endif // __UNIX__/!__UNIX__
514 wxFontMapper
*wxGUIAppTraitsBase::CreateFontMapper()
516 return new wxFontMapper
;
519 #endif // wxUSE_FONTMAP
521 wxRendererNative
*wxGUIAppTraitsBase::CreateRenderer()
523 // use the default native renderer by default
529 bool wxGUIAppTraitsBase::ShowAssertDialog(const wxString
& msg
)
531 // under MSW we prefer to use the base class version using ::MessageBox()
532 // even if wxMessageBox() is available because it has less chances to
533 // double fault our app than our wxMessageBox()
534 #if defined(__WXMSW__) || !wxUSE_MSGDLG
535 return wxAppTraitsBase::ShowAssertDialog(msg
);
536 #else // wxUSE_MSGDLG
537 // this message is intentionally not translated -- it is for
539 wxString
msgDlg(msg
);
540 msgDlg
+= wxT("\nDo you want to stop the program?\n")
541 wxT("You can also choose [Cancel] to suppress ")
542 wxT("further warnings.");
544 switch ( wxMessageBox(msgDlg
, wxT("wxWidgets Debug Alert"),
545 wxYES_NO
| wxCANCEL
| wxICON_STOP
) )
555 //case wxNO: nothing to do
559 #endif // !wxUSE_MSGDLG/wxUSE_MSGDLG
562 #endif // __WXDEBUG__
564 bool wxGUIAppTraitsBase::HasStderr()
566 // we consider that under Unix stderr always goes somewhere, even if the
567 // user doesn't always see it under GUI desktops
575 void wxGUIAppTraitsBase::ScheduleForDestroy(wxObject
*object
)
577 if ( !wxPendingDelete
.Member(object
) )
578 wxPendingDelete
.Append(object
);
581 void wxGUIAppTraitsBase::RemoveFromPendingDelete(wxObject
*object
)
583 wxPendingDelete
.DeleteObject(object
);
588 #if defined(__WINDOWS__)
589 #include "wx/msw/gsockmsw.h"
590 #elif defined(__UNIX__) || defined(__DARWIN__) || defined(__OS2__)
591 #include "wx/unix/gsockunx.h"
592 #elif defined(__WXMAC__)
593 #include <MacHeaders.c>
594 #define OTUNIXERRORS 1
595 #include <OpenTransport.h>
596 #include <OpenTransportProviders.h>
597 #include <OpenTptInternet.h>
599 #include "wx/mac/gsockmac.h"
601 #error "Must include correct GSocket header here"
604 GSocketGUIFunctionsTable
* wxGUIAppTraitsBase::GetSocketGUIFunctionsTable()
606 #if defined(__WXMAC__) && !defined(__DARWIN__)
607 // NB: wxMac CFM does not have any GUI-specific functions in gsocket.c and
608 // so it doesn't need this table at all
610 #else // !__WXMAC__ || __DARWIN__
611 static GSocketGUIFunctionsTableConcrete table
;
613 #endif // !__WXMAC__ || __DARWIN__