]> git.saurik.com Git - wxWidgets.git/blame - src/common/appcmn.cpp
msvc compilation fix
[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{
5109ae5d
JS
213 wxIdleEvent event;
214 bool needMore = FALSE;
222ed1d6 215 wxWindowList::compatibility_iterator node = wxTopLevelWindows.GetFirst();
e39af974
JS
216 node = wxTopLevelWindows.GetFirst();
217 while (node)
218 {
219 wxWindow* win = node->GetData();
5109ae5d
JS
220 if (SendIdleEvents(win, event))
221 needMore = TRUE;
e39af974
JS
222 node = node->GetNext();
223 }
224
e39af974 225 event.SetEventObject(this);
5109ae5d
JS
226 (void) ProcessEvent(event);
227 if (event.MoreRequested())
228 needMore = TRUE;
e39af974
JS
229
230 wxUpdateUIEvent::ResetUpdateTime();
231
5109ae5d 232 return needMore;
e39af974
JS
233}
234
e39af974 235// Send idle event to window and all subwindows
5109ae5d 236bool wxAppBase::SendIdleEvents(wxWindow* win, wxIdleEvent& event)
e39af974
JS
237{
238 bool needMore = FALSE;
42d11c8e 239
5109ae5d 240 win->OnInternalIdle();
42d11c8e 241
e39af974
JS
242 if (wxIdleEvent::CanSend(win))
243 {
e39af974
JS
244 event.SetEventObject(win);
245 win->GetEventHandler()->ProcessEvent(event);
246
5109ae5d
JS
247 if (event.MoreRequested())
248 needMore = TRUE;
e39af974 249 }
222ed1d6 250 wxWindowList::compatibility_iterator node = win->GetChildren().GetFirst();
e39af974
JS
251 while ( node )
252 {
529b7f71
JS
253 wxWindow *child = node->GetData();
254 if (SendIdleEvents(child, event))
e39af974
JS
255 needMore = TRUE;
256
257 node = node->GetNext();
258 }
259
260 return needMore;
261}
262
fc7a2a60 263void wxAppBase::OnIdle(wxIdleEvent& WXUNUSED(event))
955a9197
JS
264{
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();
273
274 // 'Garbage' collection of windows deleted with Close().
275 DeletePendingObjects();
276
277#if wxUSE_LOG
278 // flush the logged messages if any
279 wxLog::FlushActive();
280#endif // wxUSE_LOG
281
282}
e39af974 283
bf188f1a 284// ----------------------------------------------------------------------------
e2478fde 285// wxGUIAppTraitsBase
bf188f1a
VZ
286// ----------------------------------------------------------------------------
287
bf188f1a 288#if wxUSE_LOG
bf188f1a 289
e2478fde
VZ
290wxLog *wxGUIAppTraitsBase::CreateLogTarget()
291{
292 return new wxLogGui;
bf188f1a
VZ
293}
294
bf188f1a
VZ
295#endif // wxUSE_LOG
296
e2478fde 297wxMessageOutput *wxGUIAppTraitsBase::CreateMessageOutput()
bf188f1a 298{
e2478fde
VZ
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
304#ifdef __UNIX__
305 return new wxMessageOutputStderr;
306#else // !__UNIX__
307 // wxMessageOutputMessageBox doesn't work under Motif
308 #ifdef __WXMOTIF__
309 return new wxMessageOutputLog;
310 #else
311 return new wxMessageOutputMessageBox;
312 #endif
313#endif // __UNIX__/!__UNIX__
bf188f1a
VZ
314}
315
e2478fde 316#if wxUSE_FONTMAP
bf188f1a 317
e2478fde
VZ
318wxFontMapper *wxGUIAppTraitsBase::CreateFontMapper()
319{
320 return new wxFontMapper;
bf188f1a
VZ
321}
322
e2478fde 323#endif // wxUSE_FONTMAP
bf188f1a 324
090a6d7a 325#ifdef __WXDEBUG__
e6e6fcc9 326
e2478fde
VZ
327bool wxGUIAppTraitsBase::ShowAssertDialog(const wxString& msg)
328{
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
336 // developpers only
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.");
341
342 switch ( wxMessageBox(msgDlg, wxT("wxWindows Debug Alert"),
343 wxYES_NO | wxCANCEL | wxICON_STOP ) )
344 {
345 case wxYES:
346 wxTrap();
347 break;
090a6d7a 348
e2478fde
VZ
349 case wxCANCEL:
350 // no more asserts
351 return true;
a5f1fd3e 352
e2478fde 353 //case wxNO: nothing to do
090a6d7a 354 }
090a6d7a 355
e2478fde
VZ
356 return false;
357#endif // !wxUSE_MSGDLG/wxUSE_MSGDLG
a5f1fd3e
VZ
358}
359
e2478fde
VZ
360#endif // __WXDEBUG__
361
362bool wxGUIAppTraitsBase::HasStderr()
a5f1fd3e 363{
e2478fde
VZ
364 // we consider that under Unix stderr always goes somewhere, even if the
365 // user doesn't always see it under GUI desktops
366#ifdef __UNIX__
367 return true;
a5f1fd3e 368#else
e2478fde 369 return false;
a5f1fd3e 370#endif
a5f1fd3e
VZ
371}
372
e2478fde 373void wxGUIAppTraitsBase::ScheduleForDestroy(wxObject *object)
a5f1fd3e 374{
e2478fde
VZ
375 if ( !wxPendingDelete.Member(object) )
376 wxPendingDelete.Append(object);
a5f1fd3e
VZ
377}
378
e2478fde 379void wxGUIAppTraitsBase::RemoveFromPendingDelete(wxObject *object)
a5f1fd3e 380{
e2478fde 381 wxPendingDelete.DeleteObject(object);
a5f1fd3e
VZ
382}
383