]> git.saurik.com Git - wxWidgets.git/blame - src/common/appcmn.cpp
no real change; just reordered some implementations before real commit
[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{
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
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{
0728199b
PC
356 // process pending wx events before sending idle events
357 ProcessPendingEvents();
358
5109ae5d 359 wxIdleEvent event;
4629016d 360 bool needMore = false;
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 if (wxAppConsole::ProcessIdle())
371 needMore = true;
372
373 // 'Garbage' collection of windows deleted with Close().
374 DeletePendingObjects();
375
376#if wxUSE_LOG
377 // flush the logged messages if any
378 wxLog::FlushActive();
379#endif
e39af974
JS
380
381 wxUpdateUIEvent::ResetUpdateTime();
4629016d 382
5109ae5d 383 return needMore;
e39af974
JS
384}
385
e39af974 386// Send idle event to window and all subwindows
5109ae5d 387bool wxAppBase::SendIdleEvents(wxWindow* win, wxIdleEvent& event)
e39af974 388{
4629016d 389 bool needMore = false;
42d11c8e 390
5109ae5d 391 win->OnInternalIdle();
42d11c8e 392
b46b1d59
VZ
393 // should we send idle event to this window?
394 if ( wxIdleEvent::GetMode() == wxIDLE_PROCESS_ALL ||
395 win->HasExtraStyle(wxWS_EX_PROCESS_IDLE) )
e39af974 396 {
e39af974 397 event.SetEventObject(win);
85716ec3 398 win->HandleWindowEvent(event);
e39af974 399
5109ae5d 400 if (event.MoreRequested())
4629016d 401 needMore = true;
e39af974 402 }
222ed1d6 403 wxWindowList::compatibility_iterator node = win->GetChildren().GetFirst();
e39af974
JS
404 while ( node )
405 {
529b7f71
JS
406 wxWindow *child = node->GetData();
407 if (SendIdleEvents(child, event))
4629016d 408 needMore = true;
e39af974
JS
409
410 node = node->GetNext();
411 }
412
413 return needMore;
414}
415
bf188f1a 416// ----------------------------------------------------------------------------
e2478fde 417// wxGUIAppTraitsBase
bf188f1a
VZ
418// ----------------------------------------------------------------------------
419
bf188f1a 420#if wxUSE_LOG
bf188f1a 421
e2478fde
VZ
422wxLog *wxGUIAppTraitsBase::CreateLogTarget()
423{
d30ef769 424#if wxUSE_LOGGUI
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 (!)
784ee7d5
VZ
440 // stderr if available and message box otherwise on others
441 // (currently stderr only Windows if app running from console)
e2478fde
VZ
442#ifdef __UNIX__
443 return new wxMessageOutputStderr;
444#else // !__UNIX__
445 // wxMessageOutputMessageBox doesn't work under Motif
446 #ifdef __WXMOTIF__
447 return new wxMessageOutputLog;
a8ff046b 448 #elif wxUSE_MSGDLG
784ee7d5 449 return new wxMessageOutputBest(wxMSGOUT_PREFER_STDERR);
a8ff046b
VZ
450 #else
451 return new wxMessageOutputStderr;
e2478fde
VZ
452 #endif
453#endif // __UNIX__/!__UNIX__
bf188f1a
VZ
454}
455
e2478fde 456#if wxUSE_FONTMAP
bf188f1a 457
e2478fde
VZ
458wxFontMapper *wxGUIAppTraitsBase::CreateFontMapper()
459{
460 return new wxFontMapper;
bf188f1a
VZ
461}
462
e2478fde 463#endif // wxUSE_FONTMAP
bf188f1a 464
f0244295
VZ
465wxRendererNative *wxGUIAppTraitsBase::CreateRenderer()
466{
467 // use the default native renderer by default
468 return NULL;
469}
470
090a6d7a 471#ifdef __WXDEBUG__
e6e6fcc9 472
e2478fde
VZ
473bool wxGUIAppTraitsBase::ShowAssertDialog(const wxString& msg)
474{
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()
19a67f39
VZ
478 //
479 // under DFB the message dialog is not always functional right now
480 //
481 // and finally we can't use wxMessageBox() if it wasn't compiled in, of
482 // course
483#if defined(__WXMSW__) || defined(__WXDFB__) || !wxUSE_MSGDLG
e2478fde
VZ
484 return wxAppTraitsBase::ShowAssertDialog(msg);
485#else // wxUSE_MSGDLG
db9febdf
RR
486 wxString msgDlg = msg;
487
488#if wxUSE_STACKWALKER
489 // on Unix stack frame generation may take some time, depending on the
490 // size of the executable mainly... warn the user that we are working
491 wxFprintf(stderr, wxT("[Debug] Generating a stack trace... please wait"));
492 fflush(stderr);
493
494 const wxString stackTrace = GetAssertStackTrace();
495 if ( !stackTrace.empty() )
496 msgDlg << _T("\n\nCall stack:\n") << stackTrace;
497#endif // wxUSE_STACKWALKER
498
e2478fde
VZ
499 // this message is intentionally not translated -- it is for
500 // developpers only
e2478fde
VZ
501 msgDlg += wxT("\nDo you want to stop the program?\n")
502 wxT("You can also choose [Cancel] to suppress ")
503 wxT("further warnings.");
504
77ffb593 505 switch ( wxMessageBox(msgDlg, wxT("wxWidgets Debug Alert"),
e2478fde
VZ
506 wxYES_NO | wxCANCEL | wxICON_STOP ) )
507 {
508 case wxYES:
509 wxTrap();
510 break;
090a6d7a 511
e2478fde
VZ
512 case wxCANCEL:
513 // no more asserts
514 return true;
a5f1fd3e 515
e2478fde 516 //case wxNO: nothing to do
090a6d7a 517 }
090a6d7a 518
e2478fde
VZ
519 return false;
520#endif // !wxUSE_MSGDLG/wxUSE_MSGDLG
a5f1fd3e
VZ
521}
522
e2478fde
VZ
523#endif // __WXDEBUG__
524
525bool wxGUIAppTraitsBase::HasStderr()
a5f1fd3e 526{
e2478fde
VZ
527 // we consider that under Unix stderr always goes somewhere, even if the
528 // user doesn't always see it under GUI desktops
529#ifdef __UNIX__
530 return true;
a5f1fd3e 531#else
e2478fde 532 return false;
a5f1fd3e 533#endif
a5f1fd3e
VZ
534}
535
e2478fde 536void wxGUIAppTraitsBase::ScheduleForDestroy(wxObject *object)
a5f1fd3e 537{
e2478fde
VZ
538 if ( !wxPendingDelete.Member(object) )
539 wxPendingDelete.Append(object);
a5f1fd3e
VZ
540}
541
e2478fde 542void wxGUIAppTraitsBase::RemoveFromPendingDelete(wxObject *object)
a5f1fd3e 543{
e2478fde 544 wxPendingDelete.DeleteObject(object);
a5f1fd3e
VZ
545}
546