]> git.saurik.com Git - wxWidgets.git/blob - src/common/appcmn.cpp
xti extensions
[wxWidgets.git] / src / common / appcmn.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: common/appcmn.cpp
3 // Purpose: wxAppConsole and wxAppBase methods common to all platforms
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"
33 #include "wx/bitmap.h"
34 #include "wx/intl.h"
35 #include "wx/list.h"
36 #include "wx/log.h"
37 #include "wx/msgdlg.h"
38 #include "wx/bitmap.h"
39 #include "wx/confbase.h"
40 #endif
41
42 #include "wx/apptrait.h"
43 #include "wx/cmdline.h"
44 #include "wx/msgout.h"
45 #include "wx/thread.h"
46 #include "wx/utils.h"
47
48 #if defined(__WXMSW__)
49 #include "wx/msw/private.h" // includes windows.h for LOGFONT
50 #endif
51
52 #if wxUSE_FONTMAP
53 #include "wx/fontmap.h"
54 #endif // wxUSE_FONTMAP
55
56 // DLL options compatibility check:
57 #include "wx/build.h"
58 WX_CHECK_BUILD_OPTIONS("wxCore")
59
60 // ============================================================================
61 // wxAppBase implementation
62 // ============================================================================
63
64 // ----------------------------------------------------------------------------
65 // initialization
66 // ----------------------------------------------------------------------------
67
68 wxAppBase::wxAppBase()
69 {
70 m_topWindow = (wxWindow *)NULL;
71 m_useBestVisual = FALSE;
72 m_isActive = TRUE;
73
74 // We don't want to exit the app if the user code shows a dialog from its
75 // OnInit() -- but this is what would happen if we set m_exitOnFrameDelete
76 // to Yes initially as this dialog would be the last top level window.
77 // OTOH, if we set it to No initially we'll have to overwrite it with Yes
78 // when we enter our OnRun() because we do want the default behaviour from
79 // then on. But this would be a problem if the user code calls
80 // SetExitOnFrameDelete(FALSE) from OnInit().
81 //
82 // So we use the special "Later" value which is such that
83 // GetExitOnFrameDelete() returns FALSE for it but which we know we can
84 // safely (i.e. without losing the effect of the users SetExitOnFrameDelete
85 // call) overwrite in OnRun()
86 m_exitOnFrameDelete = Later;
87 }
88
89 bool wxAppBase::Initialize(int& argc, wxChar **argv)
90 {
91 if ( !wxAppConsole::Initialize(argc, argv) )
92 return false;
93
94 #if wxUSE_THREADS
95 wxPendingEventsLocker = new wxCriticalSection;
96 #endif
97
98 wxInitializeStockLists();
99 wxInitializeStockObjects();
100
101 wxBitmap::InitStandardHandlers();
102
103 return true;
104 }
105
106 // ----------------------------------------------------------------------------
107 // cleanup
108 // ----------------------------------------------------------------------------
109
110 wxAppBase::~wxAppBase()
111 {
112 // this destructor is required for Darwin
113 }
114
115 void wxAppBase::CleanUp()
116 {
117 // one last chance for pending objects to be cleaned up
118 DeletePendingObjects();
119
120 wxBitmap::CleanUpHandlers();
121
122 wxDeleteStockObjects();
123
124 wxDeleteStockLists();
125
126 delete wxTheColourDatabase;
127 wxTheColourDatabase = NULL;
128
129 #if wxUSE_THREADS
130 delete wxPendingEvents;
131 wxPendingEvents = NULL;
132
133 delete wxPendingEventsLocker;
134 wxPendingEventsLocker = NULL;
135
136 #if wxUSE_VALIDATORS
137 // If we don't do the following, we get an apparent memory leak.
138 ((wxEvtHandler&) wxDefaultValidator).ClearEventLocker();
139 #endif // wxUSE_VALIDATORS
140 #endif // wxUSE_THREADS
141 }
142
143 #if wxUSE_CMDLINE_PARSER
144
145 // ----------------------------------------------------------------------------
146 // GUI-specific command line options handling
147 // ----------------------------------------------------------------------------
148
149 #define OPTION_THEME _T("theme")
150 #define OPTION_MODE _T("mode")
151
152 void wxAppBase::OnInitCmdLine(wxCmdLineParser& parser)
153 {
154 // first add the standard non GUI options
155 wxAppConsole::OnInitCmdLine(parser);
156
157 // the standard command line options
158 static const wxCmdLineEntryDesc cmdLineGUIDesc[] =
159 {
160 #ifdef __WXUNIVERSAL__
161 {
162 wxCMD_LINE_OPTION,
163 _T(""),
164 OPTION_THEME,
165 gettext_noop("specify the theme to use"),
166 wxCMD_LINE_VAL_STRING,
167 0x0
168 },
169 #endif // __WXUNIVERSAL__
170
171 #if defined(__WXMGL__)
172 // VS: this is not specific to wxMGL, all fullscreen (framebuffer) ports
173 // should provide this option. That's why it is in common/appcmn.cpp
174 // and not mgl/app.cpp
175 {
176 wxCMD_LINE_OPTION,
177 _T(""),
178 OPTION_MODE,
179 gettext_noop("specify display mode to use (e.g. 640x480-16)"),
180 wxCMD_LINE_VAL_STRING,
181 0x0
182 },
183 #endif // __WXMGL__
184
185 // terminator
186 {
187 wxCMD_LINE_NONE,
188 _T(""),
189 _T(""),
190 _T(""),
191 wxCMD_LINE_VAL_NONE,
192 0x0
193 }
194 };
195
196 parser.SetDesc(cmdLineGUIDesc);
197 }
198
199 bool wxAppBase::OnCmdLineParsed(wxCmdLineParser& parser)
200 {
201 #ifdef __WXUNIVERSAL__
202 wxString themeName;
203 if ( parser.Found(OPTION_THEME, &themeName) )
204 {
205 wxTheme *theme = wxTheme::Create(themeName);
206 if ( !theme )
207 {
208 wxLogError(_("Unsupported theme '%s'."), themeName.c_str());
209 return FALSE;
210 }
211
212 // Delete the defaultly created theme and set the new theme.
213 delete wxTheme::Get();
214 wxTheme::Set(theme);
215 }
216 #endif // __WXUNIVERSAL__
217
218 #if defined(__WXMGL__)
219 wxString modeDesc;
220 if ( parser.Found(OPTION_MODE, &modeDesc) )
221 {
222 unsigned w, h, bpp;
223 if ( wxSscanf(modeDesc.c_str(), _T("%ux%u-%u"), &w, &h, &bpp) != 3 )
224 {
225 wxLogError(_("Invalid display mode specification '%s'."), modeDesc.c_str());
226 return FALSE;
227 }
228
229 if ( !SetDisplayMode(wxDisplayModeInfo(w, h, bpp)) )
230 return FALSE;
231 }
232 #endif // __WXMGL__
233
234 return wxAppConsole::OnCmdLineParsed(parser);
235 }
236
237 #endif // wxUSE_CMDLINE_PARSER
238
239 // ----------------------------------------------------------------------------
240 // OnXXX() hooks
241 // ----------------------------------------------------------------------------
242
243 bool wxAppBase::OnInitGui()
244 {
245 #ifdef __WXUNIVERSAL__
246 if ( !wxTheme::Get() && !wxTheme::CreateDefault() )
247 return FALSE;
248 #endif // __WXUNIVERSAL__
249
250 return TRUE;
251 }
252
253 int wxAppBase::OnRun()
254 {
255 // see the comment in ctor: if the initial value hasn't been changed, use
256 // the default Yes from now on
257 if ( m_exitOnFrameDelete == Later )
258 {
259 m_exitOnFrameDelete = Yes;
260 }
261 //else: it has been changed, assume the user knows what he is doing
262
263 return MainLoop();
264 }
265
266 int wxAppBase::OnExit()
267 {
268 #ifdef __WXUNIVERSAL__
269 delete wxTheme::Set(NULL);
270 #endif // __WXUNIVERSAL__
271
272 return wxAppConsole::OnExit();
273 }
274
275 void wxAppBase::Exit()
276 {
277 ExitMainLoop();
278 }
279
280 wxAppTraits *wxAppBase::CreateTraits()
281 {
282 return new wxGUIAppTraits;
283 }
284
285 // ----------------------------------------------------------------------------
286 // misc
287 // ----------------------------------------------------------------------------
288
289 void wxAppBase::SetActive(bool active, wxWindow * WXUNUSED(lastFocus))
290 {
291 if ( active == m_isActive )
292 return;
293
294 m_isActive = active;
295
296 wxActivateEvent event(wxEVT_ACTIVATE_APP, active);
297 event.SetEventObject(this);
298
299 (void)ProcessEvent(event);
300 }
301
302 void wxAppBase::DeletePendingObjects()
303 {
304 wxList::compatibility_iterator node = wxPendingDelete.GetFirst();
305 while (node)
306 {
307 wxObject *obj = node->GetData();
308
309 delete obj;
310
311 if (wxPendingDelete.Member(obj))
312 wxPendingDelete.Erase(node);
313
314 // Deleting one object may have deleted other pending
315 // objects, so start from beginning of list again.
316 node = wxPendingDelete.GetFirst();
317 }
318 }
319
320 // Returns TRUE if more time is needed.
321 bool wxAppBase::ProcessIdle()
322 {
323 wxIdleEvent event;
324 bool needMore = FALSE;
325 wxWindowList::compatibility_iterator node = wxTopLevelWindows.GetFirst();
326 node = wxTopLevelWindows.GetFirst();
327 while (node)
328 {
329 wxWindow* win = node->GetData();
330 if (SendIdleEvents(win, event))
331 needMore = TRUE;
332 node = node->GetNext();
333 }
334
335 event.SetEventObject(this);
336 (void) ProcessEvent(event);
337 if (event.MoreRequested())
338 needMore = TRUE;
339
340 wxUpdateUIEvent::ResetUpdateTime();
341
342 return needMore;
343 }
344
345 // Send idle event to window and all subwindows
346 bool wxAppBase::SendIdleEvents(wxWindow* win, wxIdleEvent& event)
347 {
348 bool needMore = FALSE;
349
350 win->OnInternalIdle();
351
352 if (wxIdleEvent::CanSend(win))
353 {
354 event.SetEventObject(win);
355 win->GetEventHandler()->ProcessEvent(event);
356
357 if (event.MoreRequested())
358 needMore = TRUE;
359 }
360 wxWindowList::compatibility_iterator node = win->GetChildren().GetFirst();
361 while ( node )
362 {
363 wxWindow *child = node->GetData();
364 if (SendIdleEvents(child, event))
365 needMore = TRUE;
366
367 node = node->GetNext();
368 }
369
370 return needMore;
371 }
372
373 void wxAppBase::OnIdle(wxIdleEvent& WXUNUSED(event))
374 {
375 // If there are pending events, we must process them: pending events
376 // are either events to the threads other than main or events posted
377 // with wxPostEvent() functions
378 // GRG: I have moved this here so that all pending events are processed
379 // before starting to delete any objects. This behaves better (in
380 // particular, wrt wxPostEvent) and is coherent with wxGTK's current
381 // behaviour. Changed Feb/2000 before 2.1.14
382 ProcessPendingEvents();
383
384 // 'Garbage' collection of windows deleted with Close().
385 DeletePendingObjects();
386
387 #if wxUSE_LOG
388 // flush the logged messages if any
389 wxLog::FlushActive();
390 #endif // wxUSE_LOG
391
392 }
393
394 // ----------------------------------------------------------------------------
395 // wxGUIAppTraitsBase
396 // ----------------------------------------------------------------------------
397
398 #if wxUSE_LOG
399
400 wxLog *wxGUIAppTraitsBase::CreateLogTarget()
401 {
402 return new wxLogGui;
403 }
404
405 #endif // wxUSE_LOG
406
407 wxMessageOutput *wxGUIAppTraitsBase::CreateMessageOutput()
408 {
409 // The standard way of printing help on command line arguments (app --help)
410 // is (according to common practice):
411 // - console apps: to stderr (on any platform)
412 // - GUI apps: stderr on Unix platforms (!)
413 // message box under Windows and others
414 #ifdef __UNIX__
415 return new wxMessageOutputStderr;
416 #else // !__UNIX__
417 // wxMessageOutputMessageBox doesn't work under Motif
418 #ifdef __WXMOTIF__
419 return new wxMessageOutputLog;
420 #else
421 return new wxMessageOutputMessageBox;
422 #endif
423 #endif // __UNIX__/!__UNIX__
424 }
425
426 #if wxUSE_FONTMAP
427
428 wxFontMapper *wxGUIAppTraitsBase::CreateFontMapper()
429 {
430 return new wxFontMapper;
431 }
432
433 #endif // wxUSE_FONTMAP
434
435 wxRendererNative *wxGUIAppTraitsBase::CreateRenderer()
436 {
437 // use the default native renderer by default
438 return NULL;
439 }
440
441 #ifdef __WXDEBUG__
442
443 bool wxGUIAppTraitsBase::ShowAssertDialog(const wxString& msg)
444 {
445 // under MSW we prefer to use the base class version using ::MessageBox()
446 // even if wxMessageBox() is available because it has less chances to
447 // double fault our app than our wxMessageBox()
448 #if defined(__WXMSW__) || !wxUSE_MSGDLG
449 return wxAppTraitsBase::ShowAssertDialog(msg);
450 #else // wxUSE_MSGDLG
451 // this message is intentionally not translated -- it is for
452 // developpers only
453 wxString msgDlg(msg);
454 msgDlg += wxT("\nDo you want to stop the program?\n")
455 wxT("You can also choose [Cancel] to suppress ")
456 wxT("further warnings.");
457
458 switch ( wxMessageBox(msgDlg, wxT("wxWindows Debug Alert"),
459 wxYES_NO | wxCANCEL | wxICON_STOP ) )
460 {
461 case wxYES:
462 wxTrap();
463 break;
464
465 case wxCANCEL:
466 // no more asserts
467 return true;
468
469 //case wxNO: nothing to do
470 }
471
472 return false;
473 #endif // !wxUSE_MSGDLG/wxUSE_MSGDLG
474 }
475
476 #endif // __WXDEBUG__
477
478 bool wxGUIAppTraitsBase::HasStderr()
479 {
480 // we consider that under Unix stderr always goes somewhere, even if the
481 // user doesn't always see it under GUI desktops
482 #ifdef __UNIX__
483 return true;
484 #else
485 return false;
486 #endif
487 }
488
489 void wxGUIAppTraitsBase::ScheduleForDestroy(wxObject *object)
490 {
491 if ( !wxPendingDelete.Member(object) )
492 wxPendingDelete.Append(object);
493 }
494
495 void wxGUIAppTraitsBase::RemoveFromPendingDelete(wxObject *object)
496 {
497 wxPendingDelete.DeleteObject(object);
498 }
499