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