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