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/window.h"
30 #include "wx/bitmap.h"
32 #include "wx/msgdlg.h"
33 #include "wx/confbase.h"
37 #include "wx/apptrait.h"
38 #include "wx/cmdline.h"
39 #include "wx/evtloop.h"
40 #include "wx/msgout.h"
41 #include "wx/thread.h"
42 #include "wx/vidmode.h"
43 #include "wx/ptr_scpd.h"
45 #if defined(__WXMSW__)
46 #include "wx/msw/private.h" // includes windows.h for LOGFONT
50 #include "wx/fontmap.h"
51 #endif // wxUSE_FONTMAP
53 // DLL options compatibility check:
55 WX_CHECK_BUILD_OPTIONS("wxCore")
57 WXDLLIMPEXP_DATA_CORE(wxList
) wxPendingDelete
;
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
;
78 m_useBestVisual
= false;
79 m_forceTrueColour
= 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();
111 wxBitmap::InitStandardHandlers();
116 // ----------------------------------------------------------------------------
118 // ----------------------------------------------------------------------------
120 wxAppBase::~wxAppBase()
122 // this destructor is required for Darwin
125 void wxAppBase::CleanUp()
127 // clean up all the pending objects
128 DeletePendingObjects();
130 // and any remaining TLWs (they remove themselves from wxTopLevelWindows
131 // when destroyed, so iterate until none are left)
132 while ( !wxTopLevelWindows
.empty() )
134 // do not use Destroy() here as it only puts the TLW in pending list
135 // but we want to delete them now
136 delete wxTopLevelWindows
.GetFirst()->GetData();
139 // undo everything we did in Initialize() above
140 wxBitmap::CleanUpHandlers();
142 wxStockGDI::DeleteAll();
144 wxDeleteStockLists();
146 delete wxTheColourDatabase
;
147 wxTheColourDatabase
= NULL
;
149 delete wxPendingEvents
;
150 wxPendingEvents
= NULL
;
153 delete wxPendingEventsLocker
;
154 wxPendingEventsLocker
= NULL
;
157 // If we don't do the following, we get an apparent memory leak.
158 ((wxEvtHandler
&) wxDefaultValidator
).ClearEventLocker();
159 #endif // wxUSE_VALIDATORS
160 #endif // wxUSE_THREADS
163 // ----------------------------------------------------------------------------
165 // ----------------------------------------------------------------------------
167 wxWindow
* wxAppBase::GetTopWindow() const
169 wxWindow
* window
= m_topWindow
;
170 if (window
== NULL
&& wxTopLevelWindows
.GetCount() > 0)
171 window
= wxTopLevelWindows
.GetFirst()->GetData();
175 wxVideoMode
wxAppBase::GetDisplayMode() const
177 return wxVideoMode();
180 wxLayoutDirection
wxAppBase::GetLayoutDirection() const
183 const wxLocale
*const locale
= wxGetLocale();
186 const wxLanguageInfo
*const
187 info
= wxLocale::GetLanguageInfo(locale
->GetLanguage());
190 return info
->LayoutDirection
;
195 return wxLayout_Default
;
198 #if wxUSE_CMDLINE_PARSER
200 // ----------------------------------------------------------------------------
201 // GUI-specific command line options handling
202 // ----------------------------------------------------------------------------
204 #define OPTION_THEME _T("theme")
205 #define OPTION_MODE _T("mode")
207 void wxAppBase::OnInitCmdLine(wxCmdLineParser
& parser
)
209 // first add the standard non GUI options
210 wxAppConsole::OnInitCmdLine(parser
);
212 // the standard command line options
213 static const wxCmdLineEntryDesc cmdLineGUIDesc
[] =
215 #ifdef __WXUNIVERSAL__
220 gettext_noop("specify the theme to use"),
221 wxCMD_LINE_VAL_STRING
,
224 #endif // __WXUNIVERSAL__
226 #if defined(__WXMGL__)
227 // VS: this is not specific to wxMGL, all fullscreen (framebuffer) ports
228 // should provide this option. That's why it is in common/appcmn.cpp
229 // and not mgl/app.cpp
234 gettext_noop("specify display mode to use (e.g. 640x480-16)"),
235 wxCMD_LINE_VAL_STRING
,
251 parser
.SetDesc(cmdLineGUIDesc
);
254 bool wxAppBase::OnCmdLineParsed(wxCmdLineParser
& parser
)
256 #ifdef __WXUNIVERSAL__
258 if ( parser
.Found(OPTION_THEME
, &themeName
) )
260 wxTheme
*theme
= wxTheme::Create(themeName
);
263 wxLogError(_("Unsupported theme '%s'."), themeName
.c_str());
267 // Delete the defaultly created theme and set the new theme.
268 delete wxTheme::Get();
271 #endif // __WXUNIVERSAL__
273 #if defined(__WXMGL__)
275 if ( parser
.Found(OPTION_MODE
, &modeDesc
) )
278 if ( wxSscanf(modeDesc
.c_str(), _T("%ux%u-%u"), &w
, &h
, &bpp
) != 3 )
280 wxLogError(_("Invalid display mode specification '%s'."), modeDesc
.c_str());
284 if ( !SetDisplayMode(wxVideoMode(w
, h
, bpp
)) )
289 return wxAppConsole::OnCmdLineParsed(parser
);
292 #endif // wxUSE_CMDLINE_PARSER
294 // ----------------------------------------------------------------------------
295 // main event loop implementation
296 // ----------------------------------------------------------------------------
298 int wxAppBase::MainLoop()
300 wxEventLoopTiedPtr
mainLoop(&m_mainLoop
, new wxEventLoop
);
302 return m_mainLoop
->Run();
305 void wxAppBase::ExitMainLoop()
307 // we should exit from the main event loop, not just any currently active
308 // (e.g. modal dialog) event loop
309 if ( m_mainLoop
&& m_mainLoop
->IsRunning() )
315 bool wxAppBase::Pending()
317 // use the currently active message loop here, not m_mainLoop, because if
318 // we're showing a modal dialog (with its own event loop) currently the
319 // main event loop is not running anyhow
320 wxEventLoop
* const loop
= wxEventLoop::GetActive();
322 return loop
&& loop
->Pending();
325 bool wxAppBase::Dispatch()
327 // see comment in Pending()
328 wxEventLoop
* const loop
= wxEventLoop::GetActive();
330 return loop
&& loop
->Dispatch();
333 // ----------------------------------------------------------------------------
335 // ----------------------------------------------------------------------------
337 bool wxAppBase::OnInitGui()
339 #ifdef __WXUNIVERSAL__
340 if ( !wxTheme::Get() && !wxTheme::CreateDefault() )
342 #endif // __WXUNIVERSAL__
347 int wxAppBase::OnRun()
349 // see the comment in ctor: if the initial value hasn't been changed, use
350 // the default Yes from now on
351 if ( m_exitOnFrameDelete
== Later
)
353 m_exitOnFrameDelete
= Yes
;
355 //else: it has been changed, assume the user knows what he is doing
360 int wxAppBase::OnExit()
362 #ifdef __WXUNIVERSAL__
363 delete wxTheme::Set(NULL
);
364 #endif // __WXUNIVERSAL__
366 return wxAppConsole::OnExit();
369 void wxAppBase::Exit()
374 wxAppTraits
*wxAppBase::CreateTraits()
376 return new wxGUIAppTraits
;
379 // ----------------------------------------------------------------------------
381 // ----------------------------------------------------------------------------
383 void wxAppBase::SetActive(bool active
, wxWindow
* WXUNUSED(lastFocus
))
385 if ( active
== m_isActive
)
390 wxActivateEvent
event(wxEVT_ACTIVATE_APP
, active
);
391 event
.SetEventObject(this);
393 (void)ProcessEvent(event
);
396 // ----------------------------------------------------------------------------
398 // ----------------------------------------------------------------------------
400 void wxAppBase::DeletePendingObjects()
402 wxList::compatibility_iterator node
= wxPendingDelete
.GetFirst();
405 wxObject
*obj
= node
->GetData();
407 // remove it from the list first so that if we get back here somehow
408 // during the object deletion (e.g. wxYield called from its dtor) we
409 // wouldn't try to delete it the second time
410 if ( wxPendingDelete
.Member(obj
) )
411 wxPendingDelete
.Erase(node
);
415 // Deleting one object may have deleted other pending
416 // objects, so start from beginning of list again.
417 node
= wxPendingDelete
.GetFirst();
421 // Returns true if more time is needed.
422 bool wxAppBase::ProcessIdle()
425 bool needMore
= false;
426 wxWindowList::compatibility_iterator node
= wxTopLevelWindows
.GetFirst();
429 wxWindow
* win
= node
->GetData();
430 if (SendIdleEvents(win
, event
))
432 node
= node
->GetNext();
435 event
.SetEventObject(this);
436 (void) ProcessEvent(event
);
437 if (event
.MoreRequested())
440 wxUpdateUIEvent::ResetUpdateTime();
445 // Send idle event to window and all subwindows
446 bool wxAppBase::SendIdleEvents(wxWindow
* win
, wxIdleEvent
& event
)
448 bool needMore
= false;
450 win
->OnInternalIdle();
452 if (wxIdleEvent::CanSend(win
))
454 event
.SetEventObject(win
);
455 win
->GetEventHandler()->ProcessEvent(event
);
457 if (event
.MoreRequested())
460 wxWindowList::compatibility_iterator node
= win
->GetChildren().GetFirst();
463 wxWindow
*child
= node
->GetData();
464 if (SendIdleEvents(child
, event
))
467 node
= node
->GetNext();
473 void wxAppBase::OnIdle(wxIdleEvent
& WXUNUSED(event
))
475 // If there are pending events, we must process them: pending events
476 // are either events to the threads other than main or events posted
477 // with wxPostEvent() functions
478 // GRG: I have moved this here so that all pending events are processed
479 // before starting to delete any objects. This behaves better (in
480 // particular, wrt wxPostEvent) and is coherent with wxGTK's current
481 // behaviour. Changed Feb/2000 before 2.1.14
482 ProcessPendingEvents();
484 // 'Garbage' collection of windows deleted with Close().
485 DeletePendingObjects();
488 // flush the logged messages if any
489 wxLog::FlushActive();
494 // ----------------------------------------------------------------------------
495 // exceptions support
496 // ----------------------------------------------------------------------------
500 bool wxAppBase::OnExceptionInMainLoop()
504 // some compilers are too stupid to know that we never return after throw
505 #if defined(__DMC__) || (defined(_MSC_VER) && _MSC_VER < 1200)
510 #endif // wxUSE_EXCEPTIONS
512 // ----------------------------------------------------------------------------
513 // wxGUIAppTraitsBase
514 // ----------------------------------------------------------------------------
518 wxLog
*wxGUIAppTraitsBase::CreateLogTarget()
523 // we must have something!
524 return new wxLogStderr
;
530 wxMessageOutput
*wxGUIAppTraitsBase::CreateMessageOutput()
532 // The standard way of printing help on command line arguments (app --help)
533 // is (according to common practice):
534 // - console apps: to stderr (on any platform)
535 // - GUI apps: stderr on Unix platforms (!)
536 // message box under Windows and others
538 return new wxMessageOutputStderr
;
540 // wxMessageOutputMessageBox doesn't work under Motif
542 return new wxMessageOutputLog
;
544 return new wxMessageOutputMessageBox
;
546 #endif // __UNIX__/!__UNIX__
551 wxFontMapper
*wxGUIAppTraitsBase::CreateFontMapper()
553 return new wxFontMapper
;
556 #endif // wxUSE_FONTMAP
558 wxRendererNative
*wxGUIAppTraitsBase::CreateRenderer()
560 // use the default native renderer by default
566 bool wxGUIAppTraitsBase::ShowAssertDialog(const wxString
& msg
)
568 // under MSW we prefer to use the base class version using ::MessageBox()
569 // even if wxMessageBox() is available because it has less chances to
570 // double fault our app than our wxMessageBox()
571 #if defined(__WXMSW__) || !wxUSE_MSGDLG
572 return wxAppTraitsBase::ShowAssertDialog(msg
);
573 #else // wxUSE_MSGDLG
574 // this message is intentionally not translated -- it is for
576 wxString
msgDlg(msg
);
577 msgDlg
+= wxT("\nDo you want to stop the program?\n")
578 wxT("You can also choose [Cancel] to suppress ")
579 wxT("further warnings.");
581 switch ( wxMessageBox(msgDlg
, wxT("wxWidgets Debug Alert"),
582 wxYES_NO
| wxCANCEL
| wxICON_STOP
) )
592 //case wxNO: nothing to do
596 #endif // !wxUSE_MSGDLG/wxUSE_MSGDLG
599 #endif // __WXDEBUG__
601 bool wxGUIAppTraitsBase::HasStderr()
603 // we consider that under Unix stderr always goes somewhere, even if the
604 // user doesn't always see it under GUI desktops
612 void wxGUIAppTraitsBase::ScheduleForDestroy(wxObject
*object
)
614 if ( !wxPendingDelete
.Member(object
) )
615 wxPendingDelete
.Append(object
);
618 void wxGUIAppTraitsBase::RemoveFromPendingDelete(wxObject
*object
)
620 wxPendingDelete
.DeleteObject(object
);
625 #if defined(__WINDOWS__)
626 #include "wx/msw/gsockmsw.h"
627 #elif defined(__UNIX__) || defined(__DARWIN__) || defined(__OS2__)
628 #include "wx/unix/gsockunx.h"
629 #elif defined(__WXMAC__)
630 #include <MacHeaders.c>
631 #define OTUNIXERRORS 1
632 #include <OpenTransport.h>
633 #include <OpenTransportProviders.h>
634 #include <OpenTptInternet.h>
636 #include "wx/mac/gsockmac.h"
638 #error "Must include correct GSocket header here"
641 GSocketGUIFunctionsTable
* wxGUIAppTraitsBase::GetSocketGUIFunctionsTable()
643 #if defined(__WXMAC__) && !defined(__DARWIN__)
644 // NB: wxMac CFM does not have any GUI-specific functions in gsocket.c and
645 // so it doesn't need this table at all
647 #else // !__WXMAC__ || __DARWIN__
648 static GSocketGUIFunctionsTableConcrete table
;
650 #endif // !__WXMAC__ || __DARWIN__