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