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 // ============================================================================
67 // wxAppBase implementation
68 // ============================================================================
70 // ----------------------------------------------------------------------------
72 // ----------------------------------------------------------------------------
74 wxAppBase::wxAppBase()
76 m_topWindow
= (wxWindow
*)NULL
;
77 m_useBestVisual
= false;
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().
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
;
97 bool wxAppBase::Initialize(int& argcOrig
, wxChar
**argvOrig
)
99 if ( !wxAppConsole::Initialize(argcOrig
, argvOrig
) )
103 wxPendingEventsLocker
= new wxCriticalSection
;
106 wxInitializeStockLists();
107 wxInitializeStockObjects();
109 wxBitmap::InitStandardHandlers();
114 // ----------------------------------------------------------------------------
116 // ----------------------------------------------------------------------------
118 wxAppBase::~wxAppBase()
120 // this destructor is required for Darwin
123 void wxAppBase::CleanUp()
125 // clean up all the pending objects
126 DeletePendingObjects();
128 // and any remaining TLWs (they remove themselves from wxTopLevelWindows
129 // when destroyed, so iterate until none are left)
130 while ( !wxTopLevelWindows
.empty() )
132 // do not use Destroy() here as it only puts the TLW in pending list
133 // but we want to delete them now
134 delete wxTopLevelWindows
.GetFirst()->GetData();
137 // undo everything we did in Initialize() above
138 wxBitmap::CleanUpHandlers();
140 wxDeleteStockObjects();
142 wxDeleteStockLists();
144 delete wxTheColourDatabase
;
145 wxTheColourDatabase
= NULL
;
147 delete wxPendingEvents
;
148 wxPendingEvents
= NULL
;
151 delete wxPendingEventsLocker
;
152 wxPendingEventsLocker
= NULL
;
155 // If we don't do the following, we get an apparent memory leak.
156 ((wxEvtHandler
&) wxDefaultValidator
).ClearEventLocker();
157 #endif // wxUSE_VALIDATORS
158 #endif // wxUSE_THREADS
161 #if wxUSE_CMDLINE_PARSER
163 // ----------------------------------------------------------------------------
164 // GUI-specific command line options handling
165 // ----------------------------------------------------------------------------
167 #define OPTION_THEME _T("theme")
168 #define OPTION_MODE _T("mode")
170 void wxAppBase::OnInitCmdLine(wxCmdLineParser
& parser
)
172 // first add the standard non GUI options
173 wxAppConsole::OnInitCmdLine(parser
);
175 // the standard command line options
176 static const wxCmdLineEntryDesc cmdLineGUIDesc
[] =
178 #ifdef __WXUNIVERSAL__
183 gettext_noop("specify the theme to use"),
184 wxCMD_LINE_VAL_STRING
,
187 #endif // __WXUNIVERSAL__
189 #if defined(__WXMGL__)
190 // VS: this is not specific to wxMGL, all fullscreen (framebuffer) ports
191 // should provide this option. That's why it is in common/appcmn.cpp
192 // and not mgl/app.cpp
197 gettext_noop("specify display mode to use (e.g. 640x480-16)"),
198 wxCMD_LINE_VAL_STRING
,
214 parser
.SetDesc(cmdLineGUIDesc
);
217 bool wxAppBase::OnCmdLineParsed(wxCmdLineParser
& parser
)
219 #ifdef __WXUNIVERSAL__
221 if ( parser
.Found(OPTION_THEME
, &themeName
) )
223 wxTheme
*theme
= wxTheme::Create(themeName
);
226 wxLogError(_("Unsupported theme '%s'."), themeName
.c_str());
230 // Delete the defaultly created theme and set the new theme.
231 delete wxTheme::Get();
234 #endif // __WXUNIVERSAL__
236 #if defined(__WXMGL__)
238 if ( parser
.Found(OPTION_MODE
, &modeDesc
) )
241 if ( wxSscanf(modeDesc
.c_str(), _T("%ux%u-%u"), &w
, &h
, &bpp
) != 3 )
243 wxLogError(_("Invalid display mode specification '%s'."), modeDesc
.c_str());
247 if ( !SetDisplayMode(wxVideoMode(w
, h
, bpp
)) )
252 return wxAppConsole::OnCmdLineParsed(parser
);
255 #endif // wxUSE_CMDLINE_PARSER
257 // ----------------------------------------------------------------------------
258 // main event loop implementation
259 // ----------------------------------------------------------------------------
261 int wxAppBase::MainLoop()
263 wxEventLoopTiedPtr
mainLoop(&m_mainLoop
, new wxEventLoop
);
265 return m_mainLoop
->Run();
268 void wxAppBase::ExitMainLoop()
270 // we should exit from the main event loop, not just any currently active
271 // (e.g. modal dialog) event loop
272 if ( m_mainLoop
&& m_mainLoop
->IsRunning() )
278 bool wxAppBase::Pending()
280 // use the currently active message loop here, not m_mainLoop, because if
281 // we're showing a modal dialog (with its own event loop) currently the
282 // main event loop is not running anyhow
283 wxEventLoop
* const loop
= wxEventLoop::GetActive();
285 return loop
&& loop
->Pending();
288 bool wxAppBase::Dispatch()
290 // see comment in Pending()
291 wxEventLoop
* const loop
= wxEventLoop::GetActive();
293 return loop
&& loop
->Dispatch();
296 // ----------------------------------------------------------------------------
298 // ----------------------------------------------------------------------------
300 bool wxAppBase::OnInitGui()
302 #ifdef __WXUNIVERSAL__
303 if ( !wxTheme::Get() && !wxTheme::CreateDefault() )
305 #endif // __WXUNIVERSAL__
310 int wxAppBase::OnRun()
312 // see the comment in ctor: if the initial value hasn't been changed, use
313 // the default Yes from now on
314 if ( m_exitOnFrameDelete
== Later
)
316 m_exitOnFrameDelete
= Yes
;
318 //else: it has been changed, assume the user knows what he is doing
323 int wxAppBase::OnExit()
325 #ifdef __WXUNIVERSAL__
326 delete wxTheme::Set(NULL
);
327 #endif // __WXUNIVERSAL__
329 return wxAppConsole::OnExit();
332 void wxAppBase::Exit()
337 wxAppTraits
*wxAppBase::CreateTraits()
339 return new wxGUIAppTraits
;
342 // ----------------------------------------------------------------------------
344 // ----------------------------------------------------------------------------
346 void wxAppBase::SetActive(bool active
, wxWindow
* WXUNUSED(lastFocus
))
348 if ( active
== m_isActive
)
353 wxActivateEvent
event(wxEVT_ACTIVATE_APP
, active
);
354 event
.SetEventObject(this);
356 (void)ProcessEvent(event
);
359 // ----------------------------------------------------------------------------
361 // ----------------------------------------------------------------------------
363 void wxAppBase::DeletePendingObjects()
365 wxList::compatibility_iterator node
= wxPendingDelete
.GetFirst();
368 wxObject
*obj
= node
->GetData();
372 if (wxPendingDelete
.Member(obj
))
373 wxPendingDelete
.Erase(node
);
375 // Deleting one object may have deleted other pending
376 // objects, so start from beginning of list again.
377 node
= wxPendingDelete
.GetFirst();
381 // Returns true if more time is needed.
382 bool wxAppBase::ProcessIdle()
385 bool needMore
= false;
386 wxWindowList::compatibility_iterator node
= wxTopLevelWindows
.GetFirst();
389 wxWindow
* win
= node
->GetData();
390 if (SendIdleEvents(win
, event
))
392 node
= node
->GetNext();
395 event
.SetEventObject(this);
396 (void) ProcessEvent(event
);
397 if (event
.MoreRequested())
400 wxUpdateUIEvent::ResetUpdateTime();
405 // Send idle event to window and all subwindows
406 bool wxAppBase::SendIdleEvents(wxWindow
* win
, wxIdleEvent
& event
)
408 bool needMore
= false;
410 win
->OnInternalIdle();
412 if (wxIdleEvent::CanSend(win
))
414 event
.SetEventObject(win
);
415 win
->GetEventHandler()->ProcessEvent(event
);
417 if (event
.MoreRequested())
420 wxWindowList::compatibility_iterator node
= win
->GetChildren().GetFirst();
423 wxWindow
*child
= node
->GetData();
424 if (SendIdleEvents(child
, event
))
427 node
= node
->GetNext();
433 void wxAppBase::OnIdle(wxIdleEvent
& WXUNUSED(event
))
435 // If there are pending events, we must process them: pending events
436 // are either events to the threads other than main or events posted
437 // with wxPostEvent() functions
438 // GRG: I have moved this here so that all pending events are processed
439 // before starting to delete any objects. This behaves better (in
440 // particular, wrt wxPostEvent) and is coherent with wxGTK's current
441 // behaviour. Changed Feb/2000 before 2.1.14
442 ProcessPendingEvents();
444 // 'Garbage' collection of windows deleted with Close().
445 DeletePendingObjects();
448 // flush the logged messages if any
449 wxLog::FlushActive();
454 // ----------------------------------------------------------------------------
455 // exceptions support
456 // ----------------------------------------------------------------------------
460 bool wxAppBase::OnExceptionInMainLoop()
464 // some compilers are too stupid to know that we never return after throw
465 #if defined(__DMC__) || (defined(_MSC_VER) && _MSC_VER < 1200)
470 #endif // wxUSE_EXCEPTIONS
472 // ----------------------------------------------------------------------------
473 // wxGUIAppTraitsBase
474 // ----------------------------------------------------------------------------
478 wxLog
*wxGUIAppTraitsBase::CreateLogTarget()
483 // we must have something!
484 return new wxLogStderr
;
490 wxMessageOutput
*wxGUIAppTraitsBase::CreateMessageOutput()
492 // The standard way of printing help on command line arguments (app --help)
493 // is (according to common practice):
494 // - console apps: to stderr (on any platform)
495 // - GUI apps: stderr on Unix platforms (!)
496 // message box under Windows and others
498 return new wxMessageOutputStderr
;
500 // wxMessageOutputMessageBox doesn't work under Motif
502 return new wxMessageOutputLog
;
504 return new wxMessageOutputMessageBox
;
506 #endif // __UNIX__/!__UNIX__
511 wxFontMapper
*wxGUIAppTraitsBase::CreateFontMapper()
513 return new wxFontMapper
;
516 #endif // wxUSE_FONTMAP
518 wxRendererNative
*wxGUIAppTraitsBase::CreateRenderer()
520 // use the default native renderer by default
526 bool wxGUIAppTraitsBase::ShowAssertDialog(const wxString
& msg
)
528 // under MSW we prefer to use the base class version using ::MessageBox()
529 // even if wxMessageBox() is available because it has less chances to
530 // double fault our app than our wxMessageBox()
531 #if defined(__WXMSW__) || !wxUSE_MSGDLG
532 return wxAppTraitsBase::ShowAssertDialog(msg
);
533 #else // wxUSE_MSGDLG
534 // this message is intentionally not translated -- it is for
536 wxString
msgDlg(msg
);
537 msgDlg
+= wxT("\nDo you want to stop the program?\n")
538 wxT("You can also choose [Cancel] to suppress ")
539 wxT("further warnings.");
541 switch ( wxMessageBox(msgDlg
, wxT("wxWidgets Debug Alert"),
542 wxYES_NO
| wxCANCEL
| wxICON_STOP
) )
552 //case wxNO: nothing to do
556 #endif // !wxUSE_MSGDLG/wxUSE_MSGDLG
559 #endif // __WXDEBUG__
561 bool wxGUIAppTraitsBase::HasStderr()
563 // we consider that under Unix stderr always goes somewhere, even if the
564 // user doesn't always see it under GUI desktops
572 void wxGUIAppTraitsBase::ScheduleForDestroy(wxObject
*object
)
574 if ( !wxPendingDelete
.Member(object
) )
575 wxPendingDelete
.Append(object
);
578 void wxGUIAppTraitsBase::RemoveFromPendingDelete(wxObject
*object
)
580 wxPendingDelete
.DeleteObject(object
);
585 #if defined(__WINDOWS__)
586 #include "wx/msw/gsockmsw.h"
587 #elif defined(__UNIX__) || defined(__DARWIN__) || defined(__OS2__)
588 #include "wx/unix/gsockunx.h"
589 #elif defined(__WXMAC__)
590 #include <MacHeaders.c>
591 #define OTUNIXERRORS 1
592 #include <OpenTransport.h>
593 #include <OpenTransportProviders.h>
594 #include <OpenTptInternet.h>
596 #include "wx/mac/gsockmac.h"
598 #error "Must include correct GSocket header here"
601 GSocketGUIFunctionsTable
* wxGUIAppTraitsBase::GetSocketGUIFunctionsTable()
603 #if defined(__WXMAC__) && !defined(__DARWIN__)
604 // NB: wxMac CFM does not have any GUI-specific functions in gsocket.c and
605 // so it doesn't need this table at all
607 #else // !__WXMAC__ || __DARWIN__
608 static GSocketGUIFunctionsTableConcrete table
;
610 #endif // !__WXMAC__ || __DARWIN__