1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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 // ---------------------------------------------------------------------------
21 #pragma implementation "appbase.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
27 #if defined(__BORLANDC__)
36 #include "wx/msgdlg.h"
39 #include "wx/apptrait.h"
41 #include "wx/fontmap.h"
42 #endif // wxUSE_FONTMAP
43 #include "wx/msgout.h"
44 #include "wx/thread.h"
47 // ============================================================================
48 // wxAppBase implementation
49 // ============================================================================
51 // ----------------------------------------------------------------------------
53 // ----------------------------------------------------------------------------
55 wxAppBase::wxAppBase()
57 m_topWindow
= (wxWindow
*)NULL
;
58 m_useBestVisual
= FALSE
;
61 // We don't want to exit the app if the user code shows a dialog from its
62 // OnInit() -- but this is what would happen if we set m_exitOnFrameDelete
63 // to Yes initially as this dialog would be the last top level window.
64 // OTOH, if we set it to No initially we'll have to overwrite it with Yes
65 // when we enter our OnRun() because we do want the default behaviour from
66 // then on. But this would be a problem if the user code calls
67 // SetExitOnFrameDelete(FALSE) from OnInit().
69 // So we use the special "Later" value which is such that
70 // GetExitOnFrameDelete() returns FALSE for it but which we know we can
71 // safely (i.e. without losing the effect of the users SetExitOnFrameDelete
72 // call) overwrite in OnRun()
73 m_exitOnFrameDelete
= Later
;
76 bool wxAppBase::Initialize(int& argc
, wxChar
**argv
)
78 if ( !wxAppConsole::Initialize(argc
, argv
) )
82 wxPendingEventsLocker
= new wxCriticalSection
;
85 wxTheColourDatabase
= new wxColourDatabase(wxKEY_STRING
);
86 wxTheColourDatabase
->Initialize();
88 wxInitializeStockLists();
89 wxInitializeStockObjects();
91 wxBitmap::InitStandardHandlers();
96 // ----------------------------------------------------------------------------
98 // ----------------------------------------------------------------------------
100 wxAppBase::~wxAppBase()
102 // this destructor is required for Darwin
105 void wxAppBase::CleanUp()
107 // one last chance for pending objects to be cleaned up
108 DeletePendingObjects();
110 wxBitmap::CleanUpHandlers();
112 wxDeleteStockObjects();
114 wxDeleteStockLists();
116 delete wxTheColourDatabase
;
117 wxTheColourDatabase
= NULL
;
120 delete wxPendingEvents
;
121 wxPendingEvents
= NULL
;
123 delete wxPendingEventsLocker
;
124 wxPendingEventsLocker
= NULL
;
127 // If we don't do the following, we get an apparent memory leak.
128 ((wxEvtHandler
&) wxDefaultValidator
).ClearEventLocker();
129 #endif // wxUSE_VALIDATORS
130 #endif // wxUSE_THREADS
133 // ----------------------------------------------------------------------------
135 // ----------------------------------------------------------------------------
137 bool wxAppBase::OnInitGui()
139 #ifdef __WXUNIVERSAL__
140 if ( !wxTheme::Get() && !wxTheme::CreateDefault() )
142 #endif // __WXUNIVERSAL__
147 int wxAppBase::OnRun()
149 // see the comment in ctor: if the initial value hasn't been changed, use
150 // the default Yes from now on
151 if ( m_exitOnFrameDelete
== Later
)
153 m_exitOnFrameDelete
= Yes
;
155 //else: it has been changed, assume the user knows what he is doing
160 void wxAppBase::Exit()
165 wxAppTraits
*wxAppBase::CreateTraits()
167 return wxAppTraits::CreateGUI();
170 // ----------------------------------------------------------------------------
172 // ----------------------------------------------------------------------------
174 void wxAppBase::SetActive(bool active
, wxWindow
* WXUNUSED(lastFocus
))
176 if ( active
== m_isActive
)
181 wxActivateEvent
event(wxEVT_ACTIVATE_APP
, active
);
182 event
.SetEventObject(this);
184 (void)ProcessEvent(event
);
187 void wxAppBase::DeletePendingObjects()
189 wxNode
*node
= wxPendingDelete
.GetFirst();
192 wxObject
*obj
= node
->GetData();
196 if (wxPendingDelete
.Member(obj
))
199 // Deleting one object may have deleted other pending
200 // objects, so start from beginning of list again.
201 node
= wxPendingDelete
.GetFirst();
205 // ----------------------------------------------------------------------------
206 // wxGUIAppTraitsBase
207 // ----------------------------------------------------------------------------
211 wxLog
*wxGUIAppTraitsBase::CreateLogTarget()
218 wxMessageOutput
*wxGUIAppTraitsBase::CreateMessageOutput()
220 // The standard way of printing help on command line arguments (app --help)
221 // is (according to common practice):
222 // - console apps: to stderr (on any platform)
223 // - GUI apps: stderr on Unix platforms (!)
224 // message box under Windows and others
226 return new wxMessageOutputStderr
;
228 // wxMessageOutputMessageBox doesn't work under Motif
230 return new wxMessageOutputLog
;
232 return new wxMessageOutputMessageBox
;
234 #endif // __UNIX__/!__UNIX__
239 wxFontMapper
*wxGUIAppTraitsBase::CreateFontMapper()
241 return new wxFontMapper
;
244 #endif // wxUSE_FONTMAP
248 bool wxGUIAppTraitsBase::ShowAssertDialog(const wxString
& msg
)
250 // under MSW we prefer to use the base class version using ::MessageBox()
251 // even if wxMessageBox() is available because it has less chances to
252 // double fault our app than our wxMessageBox()
253 #if defined(__WXMSW__) || !wxUSE_MSGDLG
254 return wxAppTraitsBase::ShowAssertDialog(msg
);
255 #else // wxUSE_MSGDLG
256 // this message is intentionally not translated -- it is for
258 wxString
msgDlg(msg
);
259 msgDlg
+= wxT("\nDo you want to stop the program?\n")
260 wxT("You can also choose [Cancel] to suppress ")
261 wxT("further warnings.");
263 switch ( wxMessageBox(msgDlg
, wxT("wxWindows Debug Alert"),
264 wxYES_NO
| wxCANCEL
| wxICON_STOP
) )
274 //case wxNO: nothing to do
278 #endif // !wxUSE_MSGDLG/wxUSE_MSGDLG
281 #endif // __WXDEBUG__
283 bool wxGUIAppTraitsBase::HasStderr()
285 // we consider that under Unix stderr always goes somewhere, even if the
286 // user doesn't always see it under GUI desktops
294 void wxGUIAppTraitsBase::ScheduleForDestroy(wxObject
*object
)
296 if ( !wxPendingDelete
.Member(object
) )
297 wxPendingDelete
.Append(object
);
300 void wxGUIAppTraitsBase::RemoveFromPendingDelete(wxObject
*object
)
302 wxPendingDelete
.DeleteObject(object
);
305 // ----------------------------------------------------------------------------
307 // ----------------------------------------------------------------------------
309 wxAppTraits
*wxAppTraitsBase::CreateGUI()
311 return new wxGUIAppTraits
;