]> git.saurik.com Git - wxWidgets.git/blame - src/common/appcmn.cpp
avoid undefined behavior from token paste resulting in more than one token
[wxWidgets.git] / src / common / appcmn.cpp
CommitLineData
72cdf4c9 1/////////////////////////////////////////////////////////////////////////////
127eab18 2// Name: src/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
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{
1e6feb95 74 m_topWindow = (wxWindow *)NULL;
b46b1d59 75
4629016d 76 m_useBestVisual = false;
515a31bf 77 m_forceTrueColour = false;
1cbee0b4 78
b46b1d59 79 m_isActive = true;
1bf77ee5 80
1cbee0b4
VZ
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
4629016d 87 // SetExitOnFrameDelete(false) from OnInit().
1cbee0b4
VZ
88 //
89 // So we use the special "Later" value which is such that
4629016d 90 // GetExitOnFrameDelete() returns false for it but which we know we can
1cbee0b4
VZ
91 // safely (i.e. without losing the effect of the users SetExitOnFrameDelete
92 // call) overwrite in OnRun()
93 m_exitOnFrameDelete = Later;
1e6feb95
VZ
94}
95
a54930e0 96bool wxAppBase::Initialize(int& argcOrig, wxChar **argvOrig)
94826170 97{
a54930e0 98 if ( !wxAppConsole::Initialize(argcOrig, argvOrig) )
94826170
VZ
99 return false;
100
94826170 101 wxInitializeStockLists();
94826170
VZ
102
103 wxBitmap::InitStandardHandlers();
104
105 return true;
106}
107
108// ----------------------------------------------------------------------------
109// cleanup
110// ----------------------------------------------------------------------------
111
799ea011
GD
112wxAppBase::~wxAppBase()
113{
114 // this destructor is required for Darwin
115}
116
94826170
VZ
117void wxAppBase::CleanUp()
118{
07460370 119 // clean up all the pending objects
94826170
VZ
120 DeletePendingObjects();
121
07460370
VZ
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 }
4055ed82 130
07460370 131 // undo everything we did in Initialize() above
94826170
VZ
132 wxBitmap::CleanUpHandlers();
133
f516d986 134 wxStockGDI::DeleteAll();
94826170
VZ
135
136 wxDeleteStockLists();
137
138 delete wxTheColourDatabase;
139 wxTheColourDatabase = NULL;
140
68d2c3be 141 wxAppConsole::CleanUp();
94826170
VZ
142}
143
475a93b7
VZ
144// ----------------------------------------------------------------------------
145// various accessors
5ff14574
PC
146// ----------------------------------------------------------------------------
147
148wxWindow* 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
156wxVideoMode wxAppBase::GetDisplayMode() const
157{
158 return wxVideoMode();
159}
160
475a93b7
VZ
161wxLayoutDirection 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
b913d3ed
VZ
179#if wxUSE_CMDLINE_PARSER
180
181// ----------------------------------------------------------------------------
182// GUI-specific command line options handling
183// ----------------------------------------------------------------------------
184
c2e45372
VZ
185#define OPTION_THEME "theme"
186#define OPTION_MODE "mode"
b913d3ed
VZ
187
188void wxAppBase::OnInitCmdLine(wxCmdLineParser& parser)
189{
9c13e5ef
VZ
190 // first add the standard non GUI options
191 wxAppConsole::OnInitCmdLine(parser);
192
b913d3ed
VZ
193 // the standard command line options
194 static const wxCmdLineEntryDesc cmdLineGUIDesc[] =
195 {
196#ifdef __WXUNIVERSAL__
197 {
198 wxCMD_LINE_OPTION,
0d5ab92f 199 NULL,
b913d3ed
VZ
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,
0d5ab92f 213 NULL,
b913d3ed
VZ
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
0d5ab92f 222 wxCMD_LINE_DESC_END
b913d3ed
VZ
223 };
224
225 parser.SetDesc(cmdLineGUIDesc);
226}
227
228bool 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());
4629016d 238 return false;
b913d3ed
VZ
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());
4629016d 255 return false;
b913d3ed
VZ
256 }
257
1c53456f 258 if ( !SetDisplayMode(wxVideoMode(w, h, bpp)) )
4629016d 259 return false;
b913d3ed
VZ
260 }
261#endif // __WXMGL__
262
263 return wxAppConsole::OnCmdLineParsed(parser);
264}
265
266#endif // wxUSE_CMDLINE_PARSER
267
94826170
VZ
268// ----------------------------------------------------------------------------
269// OnXXX() hooks
270// ----------------------------------------------------------------------------
271
1e6feb95
VZ
272bool wxAppBase::OnInitGui()
273{
274#ifdef __WXUNIVERSAL__
bf188f1a 275 if ( !wxTheme::Get() && !wxTheme::CreateDefault() )
4629016d 276 return false;
1e6feb95
VZ
277#endif // __WXUNIVERSAL__
278
4629016d 279 return true;
1e6feb95 280}
1e6feb95 281
1cbee0b4
VZ
282int 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
b46b1d59 292 return wxAppConsole::OnRun();
1cbee0b4
VZ
293}
294
b913d3ed
VZ
295int wxAppBase::OnExit()
296{
297#ifdef __WXUNIVERSAL__
298 delete wxTheme::Set(NULL);
299#endif // __WXUNIVERSAL__
300
301 return wxAppConsole::OnExit();
302}
303
e2478fde 304wxAppTraits *wxAppBase::CreateTraits()
a69be60b 305{
7843d11b 306 return new wxGUIAppTraits;
72cdf4c9
VZ
307}
308
1e6feb95
VZ
309// ----------------------------------------------------------------------------
310// misc
311// ----------------------------------------------------------------------------
312
6e169cf3 313void wxAppBase::SetActive(bool active, wxWindow * WXUNUSED(lastFocus))
7beba2fc 314{
66dfed9b
VZ
315 if ( active == m_isActive )
316 return;
317
1e6feb95 318 m_isActive = active;
66dfed9b
VZ
319
320 wxActivateEvent event(wxEVT_ACTIVATE_APP, active);
321 event.SetEventObject(this);
322
323 (void)ProcessEvent(event);
7beba2fc 324}
1e6feb95 325
2dc62891
VZ
326// ----------------------------------------------------------------------------
327// idle handling
328// ----------------------------------------------------------------------------
329
94826170
VZ
330void wxAppBase::DeletePendingObjects()
331{
222ed1d6 332 wxList::compatibility_iterator node = wxPendingDelete.GetFirst();
94826170
VZ
333 while (node)
334 {
335 wxObject *obj = node->GetData();
336
73865dad
VZ
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) )
222ed1d6 341 wxPendingDelete.Erase(node);
94826170 342
73865dad
VZ
343 delete obj;
344
94826170
VZ
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
4629016d 351// Returns true if more time is needed.
e39af974
JS
352bool wxAppBase::ProcessIdle()
353{
0728199b
PC
354 // process pending wx events before sending idle events
355 ProcessPendingEvents();
356
5109ae5d 357 wxIdleEvent event;
4629016d 358 bool needMore = false;
222ed1d6 359 wxWindowList::compatibility_iterator node = wxTopLevelWindows.GetFirst();
e39af974
JS
360 while (node)
361 {
362 wxWindow* win = node->GetData();
5109ae5d 363 if (SendIdleEvents(win, event))
4629016d 364 needMore = true;
e39af974
JS
365 node = node->GetNext();
366 }
367
0728199b
PC
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
e39af974
JS
378
379 wxUpdateUIEvent::ResetUpdateTime();
4629016d 380
5109ae5d 381 return needMore;
e39af974
JS
382}
383
e39af974 384// Send idle event to window and all subwindows
5109ae5d 385bool wxAppBase::SendIdleEvents(wxWindow* win, wxIdleEvent& event)
e39af974 386{
4629016d 387 bool needMore = false;
42d11c8e 388
5109ae5d 389 win->OnInternalIdle();
42d11c8e 390
b46b1d59
VZ
391 // should we send idle event to this window?
392 if ( wxIdleEvent::GetMode() == wxIDLE_PROCESS_ALL ||
393 win->HasExtraStyle(wxWS_EX_PROCESS_IDLE) )
e39af974 394 {
e39af974
JS
395 event.SetEventObject(win);
396 win->GetEventHandler()->ProcessEvent(event);
397
5109ae5d 398 if (event.MoreRequested())
4629016d 399 needMore = true;
e39af974 400 }
222ed1d6 401 wxWindowList::compatibility_iterator node = win->GetChildren().GetFirst();
e39af974
JS
402 while ( node )
403 {
529b7f71
JS
404 wxWindow *child = node->GetData();
405 if (SendIdleEvents(child, event))
4629016d 406 needMore = true;
e39af974
JS
407
408 node = node->GetNext();
409 }
410
411 return needMore;
412}
413
bf188f1a 414// ----------------------------------------------------------------------------
e2478fde 415// wxGUIAppTraitsBase
bf188f1a
VZ
416// ----------------------------------------------------------------------------
417
bf188f1a 418#if wxUSE_LOG
bf188f1a 419
e2478fde
VZ
420wxLog *wxGUIAppTraitsBase::CreateLogTarget()
421{
4a7ebfc1
DE
422// DE: One day I'll remove this but right now the generic dialog used for this
423// just doesn't work right at all on wxCocoa.
424#if wxUSE_LOGGUI && !defined(__WXCOCOA__)
e2478fde 425 return new wxLogGui;
461dae94 426#else
fa6416df 427 // we must have something!
461dae94
VZ
428 return new wxLogStderr;
429#endif
bf188f1a
VZ
430}
431
bf188f1a
VZ
432#endif // wxUSE_LOG
433
e2478fde 434wxMessageOutput *wxGUIAppTraitsBase::CreateMessageOutput()
bf188f1a 435{
e2478fde
VZ
436 // The standard way of printing help on command line arguments (app --help)
437 // is (according to common practice):
438 // - console apps: to stderr (on any platform)
439 // - GUI apps: stderr on Unix platforms (!)
440 // message box under Windows and others
441#ifdef __UNIX__
442 return new wxMessageOutputStderr;
443#else // !__UNIX__
444 // wxMessageOutputMessageBox doesn't work under Motif
445 #ifdef __WXMOTIF__
446 return new wxMessageOutputLog;
a8ff046b 447 #elif wxUSE_MSGDLG
e2478fde 448 return new wxMessageOutputMessageBox;
a8ff046b
VZ
449 #else
450 return new wxMessageOutputStderr;
e2478fde
VZ
451 #endif
452#endif // __UNIX__/!__UNIX__
bf188f1a
VZ
453}
454
e2478fde 455#if wxUSE_FONTMAP
bf188f1a 456
e2478fde
VZ
457wxFontMapper *wxGUIAppTraitsBase::CreateFontMapper()
458{
459 return new wxFontMapper;
bf188f1a
VZ
460}
461
e2478fde 462#endif // wxUSE_FONTMAP
bf188f1a 463
f0244295
VZ
464wxRendererNative *wxGUIAppTraitsBase::CreateRenderer()
465{
466 // use the default native renderer by default
467 return NULL;
468}
469
090a6d7a 470#ifdef __WXDEBUG__
e6e6fcc9 471
e2478fde
VZ
472bool wxGUIAppTraitsBase::ShowAssertDialog(const wxString& msg)
473{
db9febdf 474#if defined(__WXMSW__) || !wxUSE_MSGDLG
e2478fde
VZ
475 // under MSW we prefer to use the base class version using ::MessageBox()
476 // even if wxMessageBox() is available because it has less chances to
477 // double fault our app than our wxMessageBox()
e2478fde
VZ
478 return wxAppTraitsBase::ShowAssertDialog(msg);
479#else // wxUSE_MSGDLG
db9febdf
RR
480 wxString msgDlg = msg;
481
482#if wxUSE_STACKWALKER
483 // on Unix stack frame generation may take some time, depending on the
484 // size of the executable mainly... warn the user that we are working
485 wxFprintf(stderr, wxT("[Debug] Generating a stack trace... please wait"));
486 fflush(stderr);
487
488 const wxString stackTrace = GetAssertStackTrace();
489 if ( !stackTrace.empty() )
490 msgDlg << _T("\n\nCall stack:\n") << stackTrace;
491#endif // wxUSE_STACKWALKER
492
e2478fde
VZ
493 // this message is intentionally not translated -- it is for
494 // developpers only
e2478fde
VZ
495 msgDlg += wxT("\nDo you want to stop the program?\n")
496 wxT("You can also choose [Cancel] to suppress ")
497 wxT("further warnings.");
498
77ffb593 499 switch ( wxMessageBox(msgDlg, wxT("wxWidgets Debug Alert"),
e2478fde
VZ
500 wxYES_NO | wxCANCEL | wxICON_STOP ) )
501 {
502 case wxYES:
503 wxTrap();
504 break;
090a6d7a 505
e2478fde
VZ
506 case wxCANCEL:
507 // no more asserts
508 return true;
a5f1fd3e 509
e2478fde 510 //case wxNO: nothing to do
090a6d7a 511 }
090a6d7a 512
e2478fde
VZ
513 return false;
514#endif // !wxUSE_MSGDLG/wxUSE_MSGDLG
a5f1fd3e
VZ
515}
516
e2478fde
VZ
517#endif // __WXDEBUG__
518
519bool wxGUIAppTraitsBase::HasStderr()
a5f1fd3e 520{
e2478fde
VZ
521 // we consider that under Unix stderr always goes somewhere, even if the
522 // user doesn't always see it under GUI desktops
523#ifdef __UNIX__
524 return true;
a5f1fd3e 525#else
e2478fde 526 return false;
a5f1fd3e 527#endif
a5f1fd3e
VZ
528}
529
e2478fde 530void wxGUIAppTraitsBase::ScheduleForDestroy(wxObject *object)
a5f1fd3e 531{
e2478fde
VZ
532 if ( !wxPendingDelete.Member(object) )
533 wxPendingDelete.Append(object);
a5f1fd3e
VZ
534}
535
e2478fde 536void wxGUIAppTraitsBase::RemoveFromPendingDelete(wxObject *object)
a5f1fd3e 537{
e2478fde 538 wxPendingDelete.DeleteObject(object);
a5f1fd3e
VZ
539}
540