]> git.saurik.com Git - wxWidgets.git/blame - src/common/appcmn.cpp
In the object destructor, Disassociate the object from its Cocoa counterpart
[wxWidgets.git] / src / common / appcmn.cpp
CommitLineData
72cdf4c9
VZ
1/////////////////////////////////////////////////////////////////////////////
2// Name: common/appcmn.cpp
e2478fde 3// Purpose: wxAppConsole and wxAppBase methods common to all platforms
72cdf4c9
VZ
4// Author: Vadim Zeitlin
5// Modified by:
6// Created: 18.10.99
7// RCS-ID: $Id$
8// Copyright: (c) Vadim Zeitlin
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ---------------------------------------------------------------------------
17// headers
18// ---------------------------------------------------------------------------
19
20#ifdef __GNUG__
21 #pragma implementation "appbase.h"
22#endif
23
24// For compilers that support precompilation, includes "wx.h".
25#include "wx/wxprec.h"
26
27#if defined(__BORLANDC__)
28 #pragma hdrstop
29#endif
30
31#ifndef WX_PRECOMP
32 #include "wx/app.h"
b2e972ec 33 #include "wx/bitmap.h"
bf188f1a 34 #include "wx/intl.h"
e87271f3 35 #include "wx/list.h"
46446cc2 36 #include "wx/log.h"
e2478fde 37 #include "wx/msgdlg.h"
b3dfbbc9
MB
38 #include "wx/bitmap.h"
39 #include "wx/confbase.h"
72cdf4c9
VZ
40#endif
41
e2478fde 42#include "wx/apptrait.h"
e2478fde 43#include "wx/msgout.h"
72cdf4c9 44#include "wx/thread.h"
bebc39e3 45#include "wx/utils.h"
a5f1fd3e 46
1c193821
JS
47#if defined(__WXMSW__)
48 #include "wx/msw/private.h" // includes windows.h for LOGFONT
49#endif
50
51#if wxUSE_FONTMAP
52 #include "wx/fontmap.h"
53#endif // wxUSE_FONTMAP
54
e2478fde
VZ
55// ============================================================================
56// wxAppBase implementation
57// ============================================================================
d54598dd 58
bf188f1a 59// ----------------------------------------------------------------------------
94826170 60// initialization
bf188f1a
VZ
61// ----------------------------------------------------------------------------
62
090a6d7a 63wxAppBase::wxAppBase()
697c5f51 64{
1e6feb95
VZ
65 m_topWindow = (wxWindow *)NULL;
66 m_useBestVisual = FALSE;
1e6feb95 67 m_isActive = TRUE;
1cbee0b4
VZ
68
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().
76 //
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;
1e6feb95
VZ
82}
83
05e2b077 84bool wxAppBase::Initialize(int& argc, wxChar **argv)
94826170
VZ
85{
86 if ( !wxAppConsole::Initialize(argc, argv) )
87 return false;
88
94826170
VZ
89#if wxUSE_THREADS
90 wxPendingEventsLocker = new wxCriticalSection;
91#endif
92
94826170
VZ
93 wxInitializeStockLists();
94 wxInitializeStockObjects();
95
96 wxBitmap::InitStandardHandlers();
97
98 return true;
99}
100
101// ----------------------------------------------------------------------------
102// cleanup
103// ----------------------------------------------------------------------------
104
799ea011
GD
105wxAppBase::~wxAppBase()
106{
107 // this destructor is required for Darwin
108}
109
94826170
VZ
110void wxAppBase::CleanUp()
111{
112 // one last chance for pending objects to be cleaned up
113 DeletePendingObjects();
114
115 wxBitmap::CleanUpHandlers();
116
117 wxDeleteStockObjects();
118
119 wxDeleteStockLists();
120
121 delete wxTheColourDatabase;
122 wxTheColourDatabase = NULL;
123
124#if wxUSE_THREADS
125 delete wxPendingEvents;
126 wxPendingEvents = NULL;
127
128 delete wxPendingEventsLocker;
129 wxPendingEventsLocker = NULL;
130
131#if wxUSE_VALIDATORS
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
136}
137
138// ----------------------------------------------------------------------------
139// OnXXX() hooks
140// ----------------------------------------------------------------------------
141
1e6feb95
VZ
142bool wxAppBase::OnInitGui()
143{
144#ifdef __WXUNIVERSAL__
bf188f1a 145 if ( !wxTheme::Get() && !wxTheme::CreateDefault() )
1e6feb95
VZ
146 return FALSE;
147#endif // __WXUNIVERSAL__
148
149 return TRUE;
150}
1e6feb95 151
1cbee0b4
VZ
152int wxAppBase::OnRun()
153{
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 )
157 {
158 m_exitOnFrameDelete = Yes;
159 }
160 //else: it has been changed, assume the user knows what he is doing
161
162 return MainLoop();
163}
164
e2478fde 165void wxAppBase::Exit()
1e6feb95 166{
e2478fde 167 ExitMainLoop();
1e6feb95
VZ
168}
169
e2478fde 170wxAppTraits *wxAppBase::CreateTraits()
a69be60b 171{
7843d11b 172 return new wxGUIAppTraits;
72cdf4c9
VZ
173}
174
1e6feb95
VZ
175// ----------------------------------------------------------------------------
176// misc
177// ----------------------------------------------------------------------------
178
6e169cf3 179void wxAppBase::SetActive(bool active, wxWindow * WXUNUSED(lastFocus))
7beba2fc 180{
66dfed9b
VZ
181 if ( active == m_isActive )
182 return;
183
1e6feb95 184 m_isActive = active;
66dfed9b
VZ
185
186 wxActivateEvent event(wxEVT_ACTIVATE_APP, active);
187 event.SetEventObject(this);
188
189 (void)ProcessEvent(event);
7beba2fc 190}
1e6feb95 191
94826170
VZ
192void wxAppBase::DeletePendingObjects()
193{
222ed1d6 194 wxList::compatibility_iterator node = wxPendingDelete.GetFirst();
94826170
VZ
195 while (node)
196 {
197 wxObject *obj = node->GetData();
198
199 delete obj;
200
201 if (wxPendingDelete.Member(obj))
222ed1d6 202 wxPendingDelete.Erase(node);
94826170
VZ
203
204 // Deleting one object may have deleted other pending
205 // objects, so start from beginning of list again.
206 node = wxPendingDelete.GetFirst();
207 }
208}
209
e39af974
JS
210// Returns TRUE if more time is needed.
211bool wxAppBase::ProcessIdle()
212{
222ed1d6 213 wxWindowList::compatibility_iterator node = wxTopLevelWindows.GetFirst();
e39af974
JS
214 node = wxTopLevelWindows.GetFirst();
215 while (node)
216 {
217 wxWindow* win = node->GetData();
218 win->ProcessInternalIdle();
219 node = node->GetNext();
220 }
221
222 wxIdleEvent event;
223 event.SetEventObject(this);
224 bool processed = ProcessEvent(event);
225
226 wxUpdateUIEvent::ResetUpdateTime();
227
228 return processed && event.MoreRequested();
229}
230
231// Send idle event to all top-level windows
232bool wxAppBase::SendIdleEvents()
233{
234 bool needMore = FALSE;
235
222ed1d6 236 wxWindowList::compatibility_iterator node = wxTopLevelWindows.GetFirst();
e39af974
JS
237 while (node)
238 {
239 wxWindow* win = node->GetData();
240 if (SendIdleEvents(win))
241 needMore = TRUE;
242 node = node->GetNext();
243 }
244
245 return needMore;
246}
247
248// Send idle event to window and all subwindows
249bool wxAppBase::SendIdleEvents(wxWindow* win)
250{
251 bool needMore = FALSE;
252
253 if (wxIdleEvent::CanSend(win))
254 {
255 wxIdleEvent event;
256 event.SetEventObject(win);
257 win->GetEventHandler()->ProcessEvent(event);
258
259 needMore = event.MoreRequested();
260 }
261
222ed1d6 262 wxWindowList::compatibility_iterator node = win->GetChildren().GetFirst();
e39af974
JS
263 while ( node )
264 {
265 wxWindow *win = node->GetData();
266 if (SendIdleEvents(win))
267 needMore = TRUE;
268
269 node = node->GetNext();
270 }
271
272 return needMore;
273}
274
275
bf188f1a 276// ----------------------------------------------------------------------------
e2478fde 277// wxGUIAppTraitsBase
bf188f1a
VZ
278// ----------------------------------------------------------------------------
279
bf188f1a 280#if wxUSE_LOG
bf188f1a 281
e2478fde
VZ
282wxLog *wxGUIAppTraitsBase::CreateLogTarget()
283{
284 return new wxLogGui;
bf188f1a
VZ
285}
286
bf188f1a
VZ
287#endif // wxUSE_LOG
288
e2478fde 289wxMessageOutput *wxGUIAppTraitsBase::CreateMessageOutput()
bf188f1a 290{
e2478fde
VZ
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
296#ifdef __UNIX__
297 return new wxMessageOutputStderr;
298#else // !__UNIX__
299 // wxMessageOutputMessageBox doesn't work under Motif
300 #ifdef __WXMOTIF__
301 return new wxMessageOutputLog;
302 #else
303 return new wxMessageOutputMessageBox;
304 #endif
305#endif // __UNIX__/!__UNIX__
bf188f1a
VZ
306}
307
e2478fde 308#if wxUSE_FONTMAP
bf188f1a 309
e2478fde
VZ
310wxFontMapper *wxGUIAppTraitsBase::CreateFontMapper()
311{
312 return new wxFontMapper;
bf188f1a
VZ
313}
314
e2478fde 315#endif // wxUSE_FONTMAP
bf188f1a 316
090a6d7a 317#ifdef __WXDEBUG__
e6e6fcc9 318
e2478fde
VZ
319bool wxGUIAppTraitsBase::ShowAssertDialog(const wxString& msg)
320{
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
328 // developpers only
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.");
333
334 switch ( wxMessageBox(msgDlg, wxT("wxWindows Debug Alert"),
335 wxYES_NO | wxCANCEL | wxICON_STOP ) )
336 {
337 case wxYES:
338 wxTrap();
339 break;
090a6d7a 340
e2478fde
VZ
341 case wxCANCEL:
342 // no more asserts
343 return true;
a5f1fd3e 344
e2478fde 345 //case wxNO: nothing to do
090a6d7a 346 }
090a6d7a 347
e2478fde
VZ
348 return false;
349#endif // !wxUSE_MSGDLG/wxUSE_MSGDLG
a5f1fd3e
VZ
350}
351
e2478fde
VZ
352#endif // __WXDEBUG__
353
354bool wxGUIAppTraitsBase::HasStderr()
a5f1fd3e 355{
e2478fde
VZ
356 // we consider that under Unix stderr always goes somewhere, even if the
357 // user doesn't always see it under GUI desktops
358#ifdef __UNIX__
359 return true;
a5f1fd3e 360#else
e2478fde 361 return false;
a5f1fd3e 362#endif
a5f1fd3e
VZ
363}
364
e2478fde 365void wxGUIAppTraitsBase::ScheduleForDestroy(wxObject *object)
a5f1fd3e 366{
e2478fde
VZ
367 if ( !wxPendingDelete.Member(object) )
368 wxPendingDelete.Append(object);
a5f1fd3e
VZ
369}
370
e2478fde 371void wxGUIAppTraitsBase::RemoveFromPendingDelete(wxObject *object)
a5f1fd3e 372{
e2478fde 373 wxPendingDelete.DeleteObject(object);
a5f1fd3e
VZ
374}
375