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()
214 bool needMore
= FALSE
;
215 wxWindowList::compatibility_iterator node
= wxTopLevelWindows
.GetFirst();
216 node
= wxTopLevelWindows
.GetFirst();
219 wxWindow
* win
= node
->GetData();
220 if (SendIdleEvents(win
, event
))
222 node
= node
->GetNext();
225 event
.SetEventObject(this);
226 (void) ProcessEvent(event
);
227 if (event
.MoreRequested())
230 wxUpdateUIEvent::ResetUpdateTime();
235 // Send idle event to window and all subwindows
236 bool wxAppBase::SendIdleEvents(wxWindow
* win
, wxIdleEvent
& event
)
238 bool needMore
= FALSE
;
240 win
->OnInternalIdle();
241 if (wxIdleEvent::CanSend(win
))
243 event
.SetEventObject(win
);
244 win
->GetEventHandler()->ProcessEvent(event
);
246 if (event
.MoreRequested())
250 wxWindowList::compatibility_iterator node
= win
->GetChildren().GetFirst();
253 wxWindow
*win
= node
->GetData();
254 if (SendIdleEvents(win
, event
))
257 node
= node
->GetNext();
263 void wxAppBase::OnIdle(wxIdleEvent
& event
)
265 // If there are pending events, we must process them: pending events
266 // are either events to the threads other than main or events posted
267 // with wxPostEvent() functions
268 // GRG: I have moved this here so that all pending events are processed
269 // before starting to delete any objects. This behaves better (in
270 // particular, wrt wxPostEvent) and is coherent with wxGTK's current
271 // behaviour. Changed Feb/2000 before 2.1.14
272 ProcessPendingEvents();
274 // 'Garbage' collection of windows deleted with Close().
275 DeletePendingObjects();
278 // flush the logged messages if any
279 wxLog::FlushActive();
284 // ----------------------------------------------------------------------------
285 // wxGUIAppTraitsBase
286 // ----------------------------------------------------------------------------
290 wxLog
*wxGUIAppTraitsBase::CreateLogTarget()
297 wxMessageOutput
*wxGUIAppTraitsBase::CreateMessageOutput()
299 // The standard way of printing help on command line arguments (app --help)
300 // is (according to common practice):
301 // - console apps: to stderr (on any platform)
302 // - GUI apps: stderr on Unix platforms (!)
303 // message box under Windows and others
305 return new wxMessageOutputStderr
;
307 // wxMessageOutputMessageBox doesn't work under Motif
309 return new wxMessageOutputLog
;
311 return new wxMessageOutputMessageBox
;
313 #endif // __UNIX__/!__UNIX__
318 wxFontMapper
*wxGUIAppTraitsBase::CreateFontMapper()
320 return new wxFontMapper
;
323 #endif // wxUSE_FONTMAP
327 bool wxGUIAppTraitsBase::ShowAssertDialog(const wxString
& msg
)
329 // under MSW we prefer to use the base class version using ::MessageBox()
330 // even if wxMessageBox() is available because it has less chances to
331 // double fault our app than our wxMessageBox()
332 #if defined(__WXMSW__) || !wxUSE_MSGDLG
333 return wxAppTraitsBase::ShowAssertDialog(msg
);
334 #else // wxUSE_MSGDLG
335 // this message is intentionally not translated -- it is for
337 wxString
msgDlg(msg
);
338 msgDlg
+= wxT("\nDo you want to stop the program?\n")
339 wxT("You can also choose [Cancel] to suppress ")
340 wxT("further warnings.");
342 switch ( wxMessageBox(msgDlg
, wxT("wxWindows Debug Alert"),
343 wxYES_NO
| wxCANCEL
| wxICON_STOP
) )
353 //case wxNO: nothing to do
357 #endif // !wxUSE_MSGDLG/wxUSE_MSGDLG
360 #endif // __WXDEBUG__
362 bool wxGUIAppTraitsBase::HasStderr()
364 // we consider that under Unix stderr always goes somewhere, even if the
365 // user doesn't always see it under GUI desktops
373 void wxGUIAppTraitsBase::ScheduleForDestroy(wxObject
*object
)
375 if ( !wxPendingDelete
.Member(object
) )
376 wxPendingDelete
.Append(object
);
379 void wxGUIAppTraitsBase::RemoveFromPendingDelete(wxObject
*object
)
381 wxPendingDelete
.DeleteObject(object
);