]> git.saurik.com Git - wxWidgets.git/blame - src/common/appcmn.cpp
Further WinCE mods
[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
VZ
42#include "wx/apptrait.h"
43#if wxUSE_FONTMAP
44 #include "wx/fontmap.h"
45#endif // wxUSE_FONTMAP
46#include "wx/msgout.h"
72cdf4c9 47#include "wx/thread.h"
bebc39e3 48#include "wx/utils.h"
a5f1fd3e 49
e2478fde
VZ
50// ============================================================================
51// wxAppBase implementation
52// ============================================================================
d54598dd 53
bf188f1a 54// ----------------------------------------------------------------------------
94826170 55// initialization
bf188f1a
VZ
56// ----------------------------------------------------------------------------
57
090a6d7a 58wxAppBase::wxAppBase()
697c5f51 59{
1e6feb95
VZ
60 m_topWindow = (wxWindow *)NULL;
61 m_useBestVisual = FALSE;
1e6feb95 62 m_isActive = TRUE;
1cbee0b4
VZ
63
64 // We don't want to exit the app if the user code shows a dialog from its
65 // OnInit() -- but this is what would happen if we set m_exitOnFrameDelete
66 // to Yes initially as this dialog would be the last top level window.
67 // OTOH, if we set it to No initially we'll have to overwrite it with Yes
68 // when we enter our OnRun() because we do want the default behaviour from
69 // then on. But this would be a problem if the user code calls
70 // SetExitOnFrameDelete(FALSE) from OnInit().
71 //
72 // So we use the special "Later" value which is such that
73 // GetExitOnFrameDelete() returns FALSE for it but which we know we can
74 // safely (i.e. without losing the effect of the users SetExitOnFrameDelete
75 // call) overwrite in OnRun()
76 m_exitOnFrameDelete = Later;
1e6feb95
VZ
77}
78
05e2b077 79bool wxAppBase::Initialize(int& argc, wxChar **argv)
94826170
VZ
80{
81 if ( !wxAppConsole::Initialize(argc, argv) )
82 return false;
83
94826170
VZ
84#if wxUSE_THREADS
85 wxPendingEventsLocker = new wxCriticalSection;
86#endif
87
222ed1d6 88 wxTheColourDatabase = new wxColourDatabase;
94826170
VZ
89 wxTheColourDatabase->Initialize();
90
91 wxInitializeStockLists();
92 wxInitializeStockObjects();
93
94 wxBitmap::InitStandardHandlers();
95
96 return true;
97}
98
99// ----------------------------------------------------------------------------
100// cleanup
101// ----------------------------------------------------------------------------
102
799ea011
GD
103wxAppBase::~wxAppBase()
104{
105 // this destructor is required for Darwin
106}
107
94826170
VZ
108void wxAppBase::CleanUp()
109{
110 // one last chance for pending objects to be cleaned up
111 DeletePendingObjects();
112
113 wxBitmap::CleanUpHandlers();
114
115 wxDeleteStockObjects();
116
117 wxDeleteStockLists();
118
119 delete wxTheColourDatabase;
120 wxTheColourDatabase = NULL;
121
122#if wxUSE_THREADS
123 delete wxPendingEvents;
124 wxPendingEvents = NULL;
125
126 delete wxPendingEventsLocker;
127 wxPendingEventsLocker = NULL;
128
129#if wxUSE_VALIDATORS
130 // If we don't do the following, we get an apparent memory leak.
131 ((wxEvtHandler&) wxDefaultValidator).ClearEventLocker();
132#endif // wxUSE_VALIDATORS
133#endif // wxUSE_THREADS
134}
135
136// ----------------------------------------------------------------------------
137// OnXXX() hooks
138// ----------------------------------------------------------------------------
139
1e6feb95
VZ
140bool wxAppBase::OnInitGui()
141{
142#ifdef __WXUNIVERSAL__
bf188f1a 143 if ( !wxTheme::Get() && !wxTheme::CreateDefault() )
1e6feb95
VZ
144 return FALSE;
145#endif // __WXUNIVERSAL__
146
147 return TRUE;
148}
1e6feb95 149
1cbee0b4
VZ
150int wxAppBase::OnRun()
151{
152 // see the comment in ctor: if the initial value hasn't been changed, use
153 // the default Yes from now on
154 if ( m_exitOnFrameDelete == Later )
155 {
156 m_exitOnFrameDelete = Yes;
157 }
158 //else: it has been changed, assume the user knows what he is doing
159
160 return MainLoop();
161}
162
e2478fde 163void wxAppBase::Exit()
1e6feb95 164{
e2478fde 165 ExitMainLoop();
1e6feb95
VZ
166}
167
e2478fde 168wxAppTraits *wxAppBase::CreateTraits()
a69be60b 169{
7843d11b 170 return new wxGUIAppTraits;
72cdf4c9
VZ
171}
172
1e6feb95
VZ
173// ----------------------------------------------------------------------------
174// misc
175// ----------------------------------------------------------------------------
176
6e169cf3 177void wxAppBase::SetActive(bool active, wxWindow * WXUNUSED(lastFocus))
7beba2fc 178{
66dfed9b
VZ
179 if ( active == m_isActive )
180 return;
181
1e6feb95 182 m_isActive = active;
66dfed9b
VZ
183
184 wxActivateEvent event(wxEVT_ACTIVATE_APP, active);
185 event.SetEventObject(this);
186
187 (void)ProcessEvent(event);
7beba2fc 188}
1e6feb95 189
94826170
VZ
190void wxAppBase::DeletePendingObjects()
191{
222ed1d6 192 wxList::compatibility_iterator node = wxPendingDelete.GetFirst();
94826170
VZ
193 while (node)
194 {
195 wxObject *obj = node->GetData();
196
197 delete obj;
198
199 if (wxPendingDelete.Member(obj))
222ed1d6 200 wxPendingDelete.Erase(node);
94826170
VZ
201
202 // Deleting one object may have deleted other pending
203 // objects, so start from beginning of list again.
204 node = wxPendingDelete.GetFirst();
205 }
206}
207
e39af974
JS
208// Returns TRUE if more time is needed.
209bool wxAppBase::ProcessIdle()
210{
222ed1d6 211 wxWindowList::compatibility_iterator node = wxTopLevelWindows.GetFirst();
e39af974
JS
212 node = wxTopLevelWindows.GetFirst();
213 while (node)
214 {
215 wxWindow* win = node->GetData();
216 win->ProcessInternalIdle();
217 node = node->GetNext();
218 }
219
220 wxIdleEvent event;
221 event.SetEventObject(this);
222 bool processed = ProcessEvent(event);
223
224 wxUpdateUIEvent::ResetUpdateTime();
225
226 return processed && event.MoreRequested();
227}
228
229// Send idle event to all top-level windows
230bool wxAppBase::SendIdleEvents()
231{
232 bool needMore = FALSE;
233
222ed1d6 234 wxWindowList::compatibility_iterator node = wxTopLevelWindows.GetFirst();
e39af974
JS
235 while (node)
236 {
237 wxWindow* win = node->GetData();
238 if (SendIdleEvents(win))
239 needMore = TRUE;
240 node = node->GetNext();
241 }
242
243 return needMore;
244}
245
246// Send idle event to window and all subwindows
247bool wxAppBase::SendIdleEvents(wxWindow* win)
248{
249 bool needMore = FALSE;
250
251 if (wxIdleEvent::CanSend(win))
252 {
253 wxIdleEvent event;
254 event.SetEventObject(win);
255 win->GetEventHandler()->ProcessEvent(event);
256
257 needMore = event.MoreRequested();
258 }
259
222ed1d6 260 wxWindowList::compatibility_iterator node = win->GetChildren().GetFirst();
e39af974
JS
261 while ( node )
262 {
263 wxWindow *win = node->GetData();
264 if (SendIdleEvents(win))
265 needMore = TRUE;
266
267 node = node->GetNext();
268 }
269
270 return needMore;
271}
272
273
bf188f1a 274// ----------------------------------------------------------------------------
e2478fde 275// wxGUIAppTraitsBase
bf188f1a
VZ
276// ----------------------------------------------------------------------------
277
bf188f1a 278#if wxUSE_LOG
bf188f1a 279
e2478fde
VZ
280wxLog *wxGUIAppTraitsBase::CreateLogTarget()
281{
282 return new wxLogGui;
bf188f1a
VZ
283}
284
bf188f1a
VZ
285#endif // wxUSE_LOG
286
e2478fde 287wxMessageOutput *wxGUIAppTraitsBase::CreateMessageOutput()
bf188f1a 288{
e2478fde
VZ
289 // The standard way of printing help on command line arguments (app --help)
290 // is (according to common practice):
291 // - console apps: to stderr (on any platform)
292 // - GUI apps: stderr on Unix platforms (!)
293 // message box under Windows and others
294#ifdef __UNIX__
295 return new wxMessageOutputStderr;
296#else // !__UNIX__
297 // wxMessageOutputMessageBox doesn't work under Motif
298 #ifdef __WXMOTIF__
299 return new wxMessageOutputLog;
300 #else
301 return new wxMessageOutputMessageBox;
302 #endif
303#endif // __UNIX__/!__UNIX__
bf188f1a
VZ
304}
305
e2478fde 306#if wxUSE_FONTMAP
bf188f1a 307
e2478fde
VZ
308wxFontMapper *wxGUIAppTraitsBase::CreateFontMapper()
309{
310 return new wxFontMapper;
bf188f1a
VZ
311}
312
e2478fde 313#endif // wxUSE_FONTMAP
bf188f1a 314
090a6d7a 315#ifdef __WXDEBUG__
e6e6fcc9 316
e2478fde
VZ
317bool wxGUIAppTraitsBase::ShowAssertDialog(const wxString& msg)
318{
319 // under MSW we prefer to use the base class version using ::MessageBox()
320 // even if wxMessageBox() is available because it has less chances to
321 // double fault our app than our wxMessageBox()
322#if defined(__WXMSW__) || !wxUSE_MSGDLG
323 return wxAppTraitsBase::ShowAssertDialog(msg);
324#else // wxUSE_MSGDLG
325 // this message is intentionally not translated -- it is for
326 // developpers only
327 wxString msgDlg(msg);
328 msgDlg += wxT("\nDo you want to stop the program?\n")
329 wxT("You can also choose [Cancel] to suppress ")
330 wxT("further warnings.");
331
332 switch ( wxMessageBox(msgDlg, wxT("wxWindows Debug Alert"),
333 wxYES_NO | wxCANCEL | wxICON_STOP ) )
334 {
335 case wxYES:
336 wxTrap();
337 break;
090a6d7a 338
e2478fde
VZ
339 case wxCANCEL:
340 // no more asserts
341 return true;
a5f1fd3e 342
e2478fde 343 //case wxNO: nothing to do
090a6d7a 344 }
090a6d7a 345
e2478fde
VZ
346 return false;
347#endif // !wxUSE_MSGDLG/wxUSE_MSGDLG
a5f1fd3e
VZ
348}
349
e2478fde
VZ
350#endif // __WXDEBUG__
351
352bool wxGUIAppTraitsBase::HasStderr()
a5f1fd3e 353{
e2478fde
VZ
354 // we consider that under Unix stderr always goes somewhere, even if the
355 // user doesn't always see it under GUI desktops
356#ifdef __UNIX__
357 return true;
a5f1fd3e 358#else
e2478fde 359 return false;
a5f1fd3e 360#endif
a5f1fd3e
VZ
361}
362
e2478fde 363void wxGUIAppTraitsBase::ScheduleForDestroy(wxObject *object)
a5f1fd3e 364{
e2478fde
VZ
365 if ( !wxPendingDelete.Member(object) )
366 wxPendingDelete.Append(object);
a5f1fd3e
VZ
367}
368
e2478fde 369void wxGUIAppTraitsBase::RemoveFromPendingDelete(wxObject *object)
a5f1fd3e 370{
e2478fde 371 wxPendingDelete.DeleteObject(object);
a5f1fd3e
VZ
372}
373