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