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__)
33 #include "wx/bitmap.h"
37 #include "wx/msgdlg.h"
38 #include "wx/bitmap.h"
39 #include "wx/confbase.h"
42 #include "wx/apptrait.h"
43 #include "wx/msgout.h"
44 #include "wx/thread.h"
47 #if defined(__WXMSW__)
48 #include "wx/msw/private.h" // includes windows.h for LOGFONT
52 #include "wx/fontmap.h"
53 #endif // wxUSE_FONTMAP
55 // ============================================================================
56 // wxAppBase implementation
57 // ============================================================================
59 // ----------------------------------------------------------------------------
61 // ----------------------------------------------------------------------------
63 wxAppBase::wxAppBase()
65 m_topWindow
= (wxWindow
*)NULL
;
66 m_useBestVisual
= FALSE
;
69 // We don't want to exit the app if the user code shows a dialog from its
70 // OnInit() -- but this is what would happen if we set m_exitOnFrameDelete
71 // to Yes initially as this dialog would be the last top level window.
72 // OTOH, if we set it to No initially we'll have to overwrite it with Yes
73 // when we enter our OnRun() because we do want the default behaviour from
74 // then on. But this would be a problem if the user code calls
75 // SetExitOnFrameDelete(FALSE) from OnInit().
77 // So we use the special "Later" value which is such that
78 // GetExitOnFrameDelete() returns FALSE for it but which we know we can
79 // safely (i.e. without losing the effect of the users SetExitOnFrameDelete
80 // call) overwrite in OnRun()
81 m_exitOnFrameDelete
= Later
;
84 bool wxAppBase::Initialize(int& argc
, wxChar
**argv
)
86 if ( !wxAppConsole::Initialize(argc
, argv
) )
90 wxPendingEventsLocker
= new wxCriticalSection
;
93 wxInitializeStockLists();
94 wxInitializeStockObjects();
96 wxBitmap::InitStandardHandlers();
101 // ----------------------------------------------------------------------------
103 // ----------------------------------------------------------------------------
105 wxAppBase::~wxAppBase()
107 // this destructor is required for Darwin
110 void wxAppBase::CleanUp()
112 // one last chance for pending objects to be cleaned up
113 DeletePendingObjects();
115 wxBitmap::CleanUpHandlers();
117 wxDeleteStockObjects();
119 wxDeleteStockLists();
121 delete wxTheColourDatabase
;
122 wxTheColourDatabase
= NULL
;
125 delete wxPendingEvents
;
126 wxPendingEvents
= NULL
;
128 delete wxPendingEventsLocker
;
129 wxPendingEventsLocker
= NULL
;
132 // If we don't do the following, we get an apparent memory leak.
133 ((wxEvtHandler
&) wxDefaultValidator
).ClearEventLocker();
134 #endif // wxUSE_VALIDATORS
135 #endif // wxUSE_THREADS
138 // ----------------------------------------------------------------------------
140 // ----------------------------------------------------------------------------
142 bool wxAppBase::OnInitGui()
144 #ifdef __WXUNIVERSAL__
145 if ( !wxTheme::Get() && !wxTheme::CreateDefault() )
147 #endif // __WXUNIVERSAL__
152 int wxAppBase::OnRun()
154 // see the comment in ctor: if the initial value hasn't been changed, use
155 // the default Yes from now on
156 if ( m_exitOnFrameDelete
== Later
)
158 m_exitOnFrameDelete
= Yes
;
160 //else: it has been changed, assume the user knows what he is doing
165 void wxAppBase::Exit()
170 wxAppTraits
*wxAppBase::CreateTraits()
172 return new wxGUIAppTraits
;
175 // ----------------------------------------------------------------------------
177 // ----------------------------------------------------------------------------
179 void wxAppBase::SetActive(bool active
, wxWindow
* WXUNUSED(lastFocus
))
181 if ( active
== m_isActive
)
186 wxActivateEvent
event(wxEVT_ACTIVATE_APP
, active
);
187 event
.SetEventObject(this);
189 (void)ProcessEvent(event
);
192 void wxAppBase::DeletePendingObjects()
194 wxList::compatibility_iterator node
= wxPendingDelete
.GetFirst();
197 wxObject
*obj
= node
->GetData();
201 if (wxPendingDelete
.Member(obj
))
202 wxPendingDelete
.Erase(node
);
204 // Deleting one object may have deleted other pending
205 // objects, so start from beginning of list again.
206 node
= wxPendingDelete
.GetFirst();
210 // Returns TRUE if more time is needed.
211 bool wxAppBase::ProcessIdle()
213 wxWindowList::compatibility_iterator node
= wxTopLevelWindows
.GetFirst();
214 node
= wxTopLevelWindows
.GetFirst();
217 wxWindow
* win
= node
->GetData();
218 win
->ProcessInternalIdle();
219 node
= node
->GetNext();
223 event
.SetEventObject(this);
224 bool processed
= ProcessEvent(event
);
226 wxUpdateUIEvent::ResetUpdateTime();
228 return processed
&& event
.MoreRequested();
231 // Send idle event to all top-level windows
232 bool wxAppBase::SendIdleEvents()
234 bool needMore
= FALSE
;
236 wxWindowList::compatibility_iterator node
= wxTopLevelWindows
.GetFirst();
239 wxWindow
* win
= node
->GetData();
240 if (SendIdleEvents(win
))
242 node
= node
->GetNext();
248 // Send idle event to window and all subwindows
249 bool wxAppBase::SendIdleEvents(wxWindow
* win
)
251 bool needMore
= FALSE
;
253 if (wxIdleEvent::CanSend(win
))
256 event
.SetEventObject(win
);
257 win
->GetEventHandler()->ProcessEvent(event
);
259 needMore
= event
.MoreRequested();
262 wxWindowList::compatibility_iterator node
= win
->GetChildren().GetFirst();
265 wxWindow
*win
= node
->GetData();
266 if (SendIdleEvents(win
))
269 node
= node
->GetNext();
276 // ----------------------------------------------------------------------------
277 // wxGUIAppTraitsBase
278 // ----------------------------------------------------------------------------
282 wxLog
*wxGUIAppTraitsBase::CreateLogTarget()
289 wxMessageOutput
*wxGUIAppTraitsBase::CreateMessageOutput()
291 // The standard way of printing help on command line arguments (app --help)
292 // is (according to common practice):
293 // - console apps: to stderr (on any platform)
294 // - GUI apps: stderr on Unix platforms (!)
295 // message box under Windows and others
297 return new wxMessageOutputStderr
;
299 // wxMessageOutputMessageBox doesn't work under Motif
301 return new wxMessageOutputLog
;
303 return new wxMessageOutputMessageBox
;
305 #endif // __UNIX__/!__UNIX__
310 wxFontMapper
*wxGUIAppTraitsBase::CreateFontMapper()
312 return new wxFontMapper
;
315 #endif // wxUSE_FONTMAP
319 bool wxGUIAppTraitsBase::ShowAssertDialog(const wxString
& msg
)
321 // under MSW we prefer to use the base class version using ::MessageBox()
322 // even if wxMessageBox() is available because it has less chances to
323 // double fault our app than our wxMessageBox()
324 #if defined(__WXMSW__) || !wxUSE_MSGDLG
325 return wxAppTraitsBase::ShowAssertDialog(msg
);
326 #else // wxUSE_MSGDLG
327 // this message is intentionally not translated -- it is for
329 wxString
msgDlg(msg
);
330 msgDlg
+= wxT("\nDo you want to stop the program?\n")
331 wxT("You can also choose [Cancel] to suppress ")
332 wxT("further warnings.");
334 switch ( wxMessageBox(msgDlg
, wxT("wxWindows Debug Alert"),
335 wxYES_NO
| wxCANCEL
| wxICON_STOP
) )
345 //case wxNO: nothing to do
349 #endif // !wxUSE_MSGDLG/wxUSE_MSGDLG
352 #endif // __WXDEBUG__
354 bool wxGUIAppTraitsBase::HasStderr()
356 // we consider that under Unix stderr always goes somewhere, even if the
357 // user doesn't always see it under GUI desktops
365 void wxGUIAppTraitsBase::ScheduleForDestroy(wxObject
*object
)
367 if ( !wxPendingDelete
.Member(object
) )
368 wxPendingDelete
.Append(object
);
371 void wxGUIAppTraitsBase::RemoveFromPendingDelete(wxObject
*object
)
373 wxPendingDelete
.DeleteObject(object
);