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