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