]> git.saurik.com Git - wxWidgets.git/blob - src/common/appcmn.cpp
PCH-less compilation fix
[wxWidgets.git] / src / common / appcmn.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/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 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #if defined(__BORLANDC__)
24 #pragma hdrstop
25 #endif
26
27 #ifndef WX_PRECOMP
28 #include "wx/app.h"
29 #include "wx/window.h"
30 #include "wx/bitmap.h"
31 #include "wx/log.h"
32 #include "wx/msgdlg.h"
33 #include "wx/confbase.h"
34 #include "wx/utils.h"
35 #include "wx/wxcrtvararg.h"
36 #endif
37
38 #include "wx/apptrait.h"
39 #include "wx/cmdline.h"
40 #include "wx/msgout.h"
41 #include "wx/thread.h"
42 #include "wx/vidmode.h"
43
44 #ifdef __WXDEBUG__
45 #if wxUSE_STACKWALKER
46 #include "wx/stackwalk.h"
47 #endif // wxUSE_STACKWALKER
48 #endif // __WXDEBUG__
49
50 #if defined(__WXMSW__)
51 #include "wx/msw/private.h" // includes windows.h for LOGFONT
52 #endif
53
54 #if wxUSE_FONTMAP
55 #include "wx/fontmap.h"
56 #endif // wxUSE_FONTMAP
57
58 // DLL options compatibility check:
59 #include "wx/build.h"
60 WX_CHECK_BUILD_OPTIONS("wxCore")
61
62 WXDLLIMPEXP_DATA_CORE(wxList) wxPendingDelete;
63
64 // ============================================================================
65 // wxAppBase implementation
66 // ============================================================================
67
68 // ----------------------------------------------------------------------------
69 // initialization
70 // ----------------------------------------------------------------------------
71
72 wxAppBase::wxAppBase()
73 {
74 m_topWindow = (wxWindow *)NULL;
75
76 m_useBestVisual = false;
77 m_forceTrueColour = false;
78
79 m_isActive = true;
80
81 // We don't want to exit the app if the user code shows a dialog from its
82 // OnInit() -- but this is what would happen if we set m_exitOnFrameDelete
83 // to Yes initially as this dialog would be the last top level window.
84 // OTOH, if we set it to No initially we'll have to overwrite it with Yes
85 // when we enter our OnRun() because we do want the default behaviour from
86 // then on. But this would be a problem if the user code calls
87 // SetExitOnFrameDelete(false) from OnInit().
88 //
89 // So we use the special "Later" value which is such that
90 // GetExitOnFrameDelete() returns false for it but which we know we can
91 // safely (i.e. without losing the effect of the users SetExitOnFrameDelete
92 // call) overwrite in OnRun()
93 m_exitOnFrameDelete = Later;
94 }
95
96 bool wxAppBase::Initialize(int& argcOrig, wxChar **argvOrig)
97 {
98 if ( !wxAppConsole::Initialize(argcOrig, argvOrig) )
99 return false;
100
101 wxInitializeStockLists();
102
103 wxBitmap::InitStandardHandlers();
104
105 return true;
106 }
107
108 // ----------------------------------------------------------------------------
109 // cleanup
110 // ----------------------------------------------------------------------------
111
112 wxAppBase::~wxAppBase()
113 {
114 // this destructor is required for Darwin
115 }
116
117 void wxAppBase::CleanUp()
118 {
119 // clean up all the pending objects
120 DeletePendingObjects();
121
122 // and any remaining TLWs (they remove themselves from wxTopLevelWindows
123 // when destroyed, so iterate until none are left)
124 while ( !wxTopLevelWindows.empty() )
125 {
126 // do not use Destroy() here as it only puts the TLW in pending list
127 // but we want to delete them now
128 delete wxTopLevelWindows.GetFirst()->GetData();
129 }
130
131 // undo everything we did in Initialize() above
132 wxBitmap::CleanUpHandlers();
133
134 wxStockGDI::DeleteAll();
135
136 wxDeleteStockLists();
137
138 delete wxTheColourDatabase;
139 wxTheColourDatabase = NULL;
140
141 wxAppConsole::CleanUp();
142 }
143
144 // ----------------------------------------------------------------------------
145 // various accessors
146 // ----------------------------------------------------------------------------
147
148 wxWindow* wxAppBase::GetTopWindow() const
149 {
150 wxWindow* window = m_topWindow;
151 if (window == NULL && wxTopLevelWindows.GetCount() > 0)
152 window = wxTopLevelWindows.GetFirst()->GetData();
153 return window;
154 }
155
156 wxVideoMode wxAppBase::GetDisplayMode() const
157 {
158 return wxVideoMode();
159 }
160
161 wxLayoutDirection wxAppBase::GetLayoutDirection() const
162 {
163 #if wxUSE_INTL
164 const wxLocale *const locale = wxGetLocale();
165 if ( locale )
166 {
167 const wxLanguageInfo *const
168 info = wxLocale::GetLanguageInfo(locale->GetLanguage());
169
170 if ( info )
171 return info->LayoutDirection;
172 }
173 #endif // wxUSE_INTL
174
175 // we don't know
176 return wxLayout_Default;
177 }
178
179 #if wxUSE_CMDLINE_PARSER
180
181 // ----------------------------------------------------------------------------
182 // GUI-specific command line options handling
183 // ----------------------------------------------------------------------------
184
185 #define OPTION_THEME "theme"
186 #define OPTION_MODE "mode"
187
188 void wxAppBase::OnInitCmdLine(wxCmdLineParser& parser)
189 {
190 // first add the standard non GUI options
191 wxAppConsole::OnInitCmdLine(parser);
192
193 // the standard command line options
194 static const wxCmdLineEntryDesc cmdLineGUIDesc[] =
195 {
196 #ifdef __WXUNIVERSAL__
197 {
198 wxCMD_LINE_OPTION,
199 NULL,
200 OPTION_THEME,
201 gettext_noop("specify the theme to use"),
202 wxCMD_LINE_VAL_STRING,
203 0x0
204 },
205 #endif // __WXUNIVERSAL__
206
207 #if defined(__WXMGL__)
208 // VS: this is not specific to wxMGL, all fullscreen (framebuffer) ports
209 // should provide this option. That's why it is in common/appcmn.cpp
210 // and not mgl/app.cpp
211 {
212 wxCMD_LINE_OPTION,
213 NULL,
214 OPTION_MODE,
215 gettext_noop("specify display mode to use (e.g. 640x480-16)"),
216 wxCMD_LINE_VAL_STRING,
217 0x0
218 },
219 #endif // __WXMGL__
220
221 // terminator
222 wxCMD_LINE_DESC_END
223 };
224
225 parser.SetDesc(cmdLineGUIDesc);
226 }
227
228 bool wxAppBase::OnCmdLineParsed(wxCmdLineParser& parser)
229 {
230 #ifdef __WXUNIVERSAL__
231 wxString themeName;
232 if ( parser.Found(OPTION_THEME, &themeName) )
233 {
234 wxTheme *theme = wxTheme::Create(themeName);
235 if ( !theme )
236 {
237 wxLogError(_("Unsupported theme '%s'."), themeName.c_str());
238 return false;
239 }
240
241 // Delete the defaultly created theme and set the new theme.
242 delete wxTheme::Get();
243 wxTheme::Set(theme);
244 }
245 #endif // __WXUNIVERSAL__
246
247 #if defined(__WXMGL__)
248 wxString modeDesc;
249 if ( parser.Found(OPTION_MODE, &modeDesc) )
250 {
251 unsigned w, h, bpp;
252 if ( wxSscanf(modeDesc.c_str(), _T("%ux%u-%u"), &w, &h, &bpp) != 3 )
253 {
254 wxLogError(_("Invalid display mode specification '%s'."), modeDesc.c_str());
255 return false;
256 }
257
258 if ( !SetDisplayMode(wxVideoMode(w, h, bpp)) )
259 return false;
260 }
261 #endif // __WXMGL__
262
263 return wxAppConsole::OnCmdLineParsed(parser);
264 }
265
266 #endif // wxUSE_CMDLINE_PARSER
267
268 // ----------------------------------------------------------------------------
269 // OnXXX() hooks
270 // ----------------------------------------------------------------------------
271
272 bool wxAppBase::OnInitGui()
273 {
274 #ifdef __WXUNIVERSAL__
275 if ( !wxTheme::Get() && !wxTheme::CreateDefault() )
276 return false;
277 #endif // __WXUNIVERSAL__
278
279 return true;
280 }
281
282 int wxAppBase::OnRun()
283 {
284 // see the comment in ctor: if the initial value hasn't been changed, use
285 // the default Yes from now on
286 if ( m_exitOnFrameDelete == Later )
287 {
288 m_exitOnFrameDelete = Yes;
289 }
290 //else: it has been changed, assume the user knows what he is doing
291
292 return wxAppConsole::OnRun();
293 }
294
295 int wxAppBase::OnExit()
296 {
297 #ifdef __WXUNIVERSAL__
298 delete wxTheme::Set(NULL);
299 #endif // __WXUNIVERSAL__
300
301 return wxAppConsole::OnExit();
302 }
303
304 wxAppTraits *wxAppBase::CreateTraits()
305 {
306 return new wxGUIAppTraits;
307 }
308
309 // ----------------------------------------------------------------------------
310 // misc
311 // ----------------------------------------------------------------------------
312
313 void wxAppBase::SetActive(bool active, wxWindow * WXUNUSED(lastFocus))
314 {
315 if ( active == m_isActive )
316 return;
317
318 m_isActive = active;
319
320 wxActivateEvent event(wxEVT_ACTIVATE_APP, active);
321 event.SetEventObject(this);
322
323 (void)ProcessEvent(event);
324 }
325
326 // ----------------------------------------------------------------------------
327 // idle handling
328 // ----------------------------------------------------------------------------
329
330 void wxAppBase::DeletePendingObjects()
331 {
332 wxList::compatibility_iterator node = wxPendingDelete.GetFirst();
333 while (node)
334 {
335 wxObject *obj = node->GetData();
336
337 // remove it from the list first so that if we get back here somehow
338 // during the object deletion (e.g. wxYield called from its dtor) we
339 // wouldn't try to delete it the second time
340 if ( wxPendingDelete.Member(obj) )
341 wxPendingDelete.Erase(node);
342
343 delete obj;
344
345 // Deleting one object may have deleted other pending
346 // objects, so start from beginning of list again.
347 node = wxPendingDelete.GetFirst();
348 }
349 }
350
351 // Returns true if more time is needed.
352 bool wxAppBase::ProcessIdle()
353 {
354 // process pending wx events before sending idle events
355 ProcessPendingEvents();
356
357 wxIdleEvent event;
358 bool needMore = false;
359 wxWindowList::compatibility_iterator node = wxTopLevelWindows.GetFirst();
360 while (node)
361 {
362 wxWindow* win = node->GetData();
363 if (SendIdleEvents(win, event))
364 needMore = true;
365 node = node->GetNext();
366 }
367
368 if (wxAppConsole::ProcessIdle())
369 needMore = true;
370
371 // 'Garbage' collection of windows deleted with Close().
372 DeletePendingObjects();
373
374 #if wxUSE_LOG
375 // flush the logged messages if any
376 wxLog::FlushActive();
377 #endif
378
379 wxUpdateUIEvent::ResetUpdateTime();
380
381 return needMore;
382 }
383
384 // Send idle event to window and all subwindows
385 bool wxAppBase::SendIdleEvents(wxWindow* win, wxIdleEvent& event)
386 {
387 bool needMore = false;
388
389 win->OnInternalIdle();
390
391 // should we send idle event to this window?
392 if ( wxIdleEvent::GetMode() == wxIDLE_PROCESS_ALL ||
393 win->HasExtraStyle(wxWS_EX_PROCESS_IDLE) )
394 {
395 event.SetEventObject(win);
396 win->HandleWindowEvent(event);
397
398 if (event.MoreRequested())
399 needMore = true;
400 }
401 wxWindowList::compatibility_iterator node = win->GetChildren().GetFirst();
402 while ( node )
403 {
404 wxWindow *child = node->GetData();
405 if (SendIdleEvents(child, event))
406 needMore = true;
407
408 node = node->GetNext();
409 }
410
411 return needMore;
412 }
413
414 // ----------------------------------------------------------------------------
415 // wxGUIAppTraitsBase
416 // ----------------------------------------------------------------------------
417
418 #if wxUSE_LOG
419
420 wxLog *wxGUIAppTraitsBase::CreateLogTarget()
421 {
422 #if wxUSE_LOGGUI
423 return new wxLogGui;
424 #else
425 // we must have something!
426 return new wxLogStderr;
427 #endif
428 }
429
430 #endif // wxUSE_LOG
431
432 wxMessageOutput *wxGUIAppTraitsBase::CreateMessageOutput()
433 {
434 // The standard way of printing help on command line arguments (app --help)
435 // is (according to common practice):
436 // - console apps: to stderr (on any platform)
437 // - GUI apps: stderr on Unix platforms (!)
438 // stderr if available and message box otherwise on others
439 // (currently stderr only Windows if app running from console)
440 #ifdef __UNIX__
441 return new wxMessageOutputStderr;
442 #else // !__UNIX__
443 // wxMessageOutputMessageBox doesn't work under Motif
444 #ifdef __WXMOTIF__
445 return new wxMessageOutputLog;
446 #elif wxUSE_MSGDLG
447 return new wxMessageOutputBest(wxMSGOUT_PREFER_STDERR);
448 #else
449 return new wxMessageOutputStderr;
450 #endif
451 #endif // __UNIX__/!__UNIX__
452 }
453
454 #if wxUSE_FONTMAP
455
456 wxFontMapper *wxGUIAppTraitsBase::CreateFontMapper()
457 {
458 return new wxFontMapper;
459 }
460
461 #endif // wxUSE_FONTMAP
462
463 wxRendererNative *wxGUIAppTraitsBase::CreateRenderer()
464 {
465 // use the default native renderer by default
466 return NULL;
467 }
468
469 #ifdef __WXDEBUG__
470
471 bool wxGUIAppTraitsBase::ShowAssertDialog(const wxString& msg)
472 {
473 // under MSW we prefer to use the base class version using ::MessageBox()
474 // even if wxMessageBox() is available because it has less chances to
475 // double fault our app than our wxMessageBox()
476 //
477 // under DFB the message dialog is not always functional right now
478 //
479 // and finally we can't use wxMessageBox() if it wasn't compiled in, of
480 // course
481 #if defined(__WXMSW__) || defined(__WXDFB__) || !wxUSE_MSGDLG
482 return wxAppTraitsBase::ShowAssertDialog(msg);
483 #else // wxUSE_MSGDLG
484 wxString msgDlg = msg;
485
486 #if wxUSE_STACKWALKER
487 // on Unix stack frame generation may take some time, depending on the
488 // size of the executable mainly... warn the user that we are working
489 wxFprintf(stderr, wxT("[Debug] Generating a stack trace... please wait"));
490 fflush(stderr);
491
492 const wxString stackTrace = GetAssertStackTrace();
493 if ( !stackTrace.empty() )
494 msgDlg << _T("\n\nCall stack:\n") << stackTrace;
495 #endif // wxUSE_STACKWALKER
496
497 // this message is intentionally not translated -- it is for
498 // developpers only
499 msgDlg += wxT("\nDo you want to stop the program?\n")
500 wxT("You can also choose [Cancel] to suppress ")
501 wxT("further warnings.");
502
503 switch ( wxMessageBox(msgDlg, wxT("wxWidgets Debug Alert"),
504 wxYES_NO | wxCANCEL | wxICON_STOP ) )
505 {
506 case wxYES:
507 wxTrap();
508 break;
509
510 case wxCANCEL:
511 // no more asserts
512 return true;
513
514 //case wxNO: nothing to do
515 }
516
517 return false;
518 #endif // !wxUSE_MSGDLG/wxUSE_MSGDLG
519 }
520
521 #endif // __WXDEBUG__
522
523 bool wxGUIAppTraitsBase::HasStderr()
524 {
525 // we consider that under Unix stderr always goes somewhere, even if the
526 // user doesn't always see it under GUI desktops
527 #ifdef __UNIX__
528 return true;
529 #else
530 return false;
531 #endif
532 }
533
534 void wxGUIAppTraitsBase::ScheduleForDestroy(wxObject *object)
535 {
536 if ( !wxPendingDelete.Member(object) )
537 wxPendingDelete.Append(object);
538 }
539
540 void wxGUIAppTraitsBase::RemoveFromPendingDelete(wxObject *object)
541 {
542 wxPendingDelete.DeleteObject(object);
543 }
544