]> git.saurik.com Git - wxWidgets.git/blame - src/common/appcmn.cpp
Apply patch [ 1554746 ] wxXmlNode::InsertChild fix
[wxWidgets.git] / src / common / appcmn.cpp
CommitLineData
72cdf4c9 1/////////////////////////////////////////////////////////////////////////////
127eab18 2// Name: src/common/appcmn.cpp
e2478fde 3// Purpose: wxAppConsole and 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
8ecff181 28 #include "wx/list.h"
72cdf4c9 29 #include "wx/app.h"
b2e972ec 30 #include "wx/bitmap.h"
bf188f1a 31 #include "wx/intl.h"
46446cc2 32 #include "wx/log.h"
e2478fde 33 #include "wx/msgdlg.h"
b3dfbbc9 34 #include "wx/confbase.h"
de6185e2 35 #include "wx/utils.h"
72cdf4c9
VZ
36#endif
37
e2478fde 38#include "wx/apptrait.h"
b913d3ed 39#include "wx/cmdline.h"
1bf77ee5 40#include "wx/evtloop.h"
e2478fde 41#include "wx/msgout.h"
72cdf4c9 42#include "wx/thread.h"
1bf77ee5 43#include "wx/ptr_scpd.h"
a5f1fd3e 44
82ef81ed
WS
45#if defined(__WXMSW__)
46 #include "wx/msw/private.h" // includes windows.h for LOGFONT
1c193821
JS
47#endif
48
49#if wxUSE_FONTMAP
50 #include "wx/fontmap.h"
51#endif // wxUSE_FONTMAP
52
34fdf762
VS
53// DLL options compatibility check:
54#include "wx/build.h"
55WX_CHECK_BUILD_OPTIONS("wxCore")
56
e7445ff8 57WXDLLIMPEXP_DATA_CORE(wxList) wxPendingDelete;
1bf77ee5
VZ
58
59// ----------------------------------------------------------------------------
60// wxEventLoopPtr
61// ----------------------------------------------------------------------------
62
63// this defines wxEventLoopPtr
259c43f6 64wxDEFINE_TIED_SCOPED_PTR_TYPE(wxEventLoop)
1bf77ee5 65
e2478fde
VZ
66// ============================================================================
67// wxAppBase implementation
68// ============================================================================
d54598dd 69
bf188f1a 70// ----------------------------------------------------------------------------
94826170 71// initialization
bf188f1a
VZ
72// ----------------------------------------------------------------------------
73
090a6d7a 74wxAppBase::wxAppBase()
697c5f51 75{
1e6feb95 76 m_topWindow = (wxWindow *)NULL;
4629016d
WS
77 m_useBestVisual = false;
78 m_isActive = true;
1cbee0b4 79
1bf77ee5
VZ
80 m_mainLoop = NULL;
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
VZ
102#if wxUSE_THREADS
103 wxPendingEventsLocker = new wxCriticalSection;
104#endif
105
94826170 106 wxInitializeStockLists();
94826170
VZ
107
108 wxBitmap::InitStandardHandlers();
109
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
94826170
VZ
146 delete wxPendingEvents;
147 wxPendingEvents = NULL;
148
c8b1e804 149#if wxUSE_THREADS
94826170
VZ
150 delete wxPendingEventsLocker;
151 wxPendingEventsLocker = NULL;
152
b913d3ed
VZ
153 #if wxUSE_VALIDATORS
154 // If we don't do the following, we get an apparent memory leak.
155 ((wxEvtHandler&) wxDefaultValidator).ClearEventLocker();
156 #endif // wxUSE_VALIDATORS
94826170
VZ
157#endif // wxUSE_THREADS
158}
159
b913d3ed
VZ
160#if wxUSE_CMDLINE_PARSER
161
162// ----------------------------------------------------------------------------
163// GUI-specific command line options handling
164// ----------------------------------------------------------------------------
165
166#define OPTION_THEME _T("theme")
167#define OPTION_MODE _T("mode")
168
169void wxAppBase::OnInitCmdLine(wxCmdLineParser& parser)
170{
9c13e5ef
VZ
171 // first add the standard non GUI options
172 wxAppConsole::OnInitCmdLine(parser);
173
b913d3ed
VZ
174 // the standard command line options
175 static const wxCmdLineEntryDesc cmdLineGUIDesc[] =
176 {
177#ifdef __WXUNIVERSAL__
178 {
179 wxCMD_LINE_OPTION,
b494c48b 180 wxEmptyString,
b913d3ed
VZ
181 OPTION_THEME,
182 gettext_noop("specify the theme to use"),
183 wxCMD_LINE_VAL_STRING,
184 0x0
185 },
186#endif // __WXUNIVERSAL__
187
188#if defined(__WXMGL__)
189 // VS: this is not specific to wxMGL, all fullscreen (framebuffer) ports
190 // should provide this option. That's why it is in common/appcmn.cpp
191 // and not mgl/app.cpp
192 {
193 wxCMD_LINE_OPTION,
b494c48b 194 wxEmptyString,
b913d3ed
VZ
195 OPTION_MODE,
196 gettext_noop("specify display mode to use (e.g. 640x480-16)"),
197 wxCMD_LINE_VAL_STRING,
198 0x0
199 },
200#endif // __WXMGL__
201
202 // terminator
203 {
204 wxCMD_LINE_NONE,
b494c48b
WS
205 wxEmptyString,
206 wxEmptyString,
207 wxEmptyString,
b913d3ed
VZ
208 wxCMD_LINE_VAL_NONE,
209 0x0
210 }
211 };
212
213 parser.SetDesc(cmdLineGUIDesc);
214}
215
216bool wxAppBase::OnCmdLineParsed(wxCmdLineParser& parser)
217{
218#ifdef __WXUNIVERSAL__
219 wxString themeName;
220 if ( parser.Found(OPTION_THEME, &themeName) )
221 {
222 wxTheme *theme = wxTheme::Create(themeName);
223 if ( !theme )
224 {
225 wxLogError(_("Unsupported theme '%s'."), themeName.c_str());
4629016d 226 return false;
b913d3ed
VZ
227 }
228
229 // Delete the defaultly created theme and set the new theme.
230 delete wxTheme::Get();
231 wxTheme::Set(theme);
232 }
233#endif // __WXUNIVERSAL__
234
235#if defined(__WXMGL__)
236 wxString modeDesc;
237 if ( parser.Found(OPTION_MODE, &modeDesc) )
238 {
239 unsigned w, h, bpp;
240 if ( wxSscanf(modeDesc.c_str(), _T("%ux%u-%u"), &w, &h, &bpp) != 3 )
241 {
242 wxLogError(_("Invalid display mode specification '%s'."), modeDesc.c_str());
4629016d 243 return false;
b913d3ed
VZ
244 }
245
1c53456f 246 if ( !SetDisplayMode(wxVideoMode(w, h, bpp)) )
4629016d 247 return false;
b913d3ed
VZ
248 }
249#endif // __WXMGL__
250
251 return wxAppConsole::OnCmdLineParsed(parser);
252}
253
254#endif // wxUSE_CMDLINE_PARSER
255
1bf77ee5
VZ
256// ----------------------------------------------------------------------------
257// main event loop implementation
258// ----------------------------------------------------------------------------
259
260int wxAppBase::MainLoop()
261{
efbfda9d 262 wxEventLoopTiedPtr mainLoop(&m_mainLoop, new wxEventLoop);
1bf77ee5
VZ
263
264 return m_mainLoop->Run();
265}
266
267void wxAppBase::ExitMainLoop()
268{
269 // we should exit from the main event loop, not just any currently active
270 // (e.g. modal dialog) event loop
dd435a79 271 if ( m_mainLoop && m_mainLoop->IsRunning() )
1bf77ee5
VZ
272 {
273 m_mainLoop->Exit(0);
274 }
275}
276
277bool wxAppBase::Pending()
278{
279 // use the currently active message loop here, not m_mainLoop, because if
280 // we're showing a modal dialog (with its own event loop) currently the
281 // main event loop is not running anyhow
282 wxEventLoop * const loop = wxEventLoop::GetActive();
283
284 return loop && loop->Pending();
285}
286
287bool wxAppBase::Dispatch()
288{
289 // see comment in Pending()
290 wxEventLoop * const loop = wxEventLoop::GetActive();
291
d1fc6f06 292 return loop && loop->Dispatch();
1bf77ee5
VZ
293}
294
94826170
VZ
295// ----------------------------------------------------------------------------
296// OnXXX() hooks
297// ----------------------------------------------------------------------------
298
1e6feb95
VZ
299bool wxAppBase::OnInitGui()
300{
301#ifdef __WXUNIVERSAL__
bf188f1a 302 if ( !wxTheme::Get() && !wxTheme::CreateDefault() )
4629016d 303 return false;
1e6feb95
VZ
304#endif // __WXUNIVERSAL__
305
4629016d 306 return true;
1e6feb95 307}
1e6feb95 308
1cbee0b4
VZ
309int wxAppBase::OnRun()
310{
311 // see the comment in ctor: if the initial value hasn't been changed, use
312 // the default Yes from now on
313 if ( m_exitOnFrameDelete == Later )
314 {
315 m_exitOnFrameDelete = Yes;
316 }
317 //else: it has been changed, assume the user knows what he is doing
318
319 return MainLoop();
320}
321
b913d3ed
VZ
322int wxAppBase::OnExit()
323{
324#ifdef __WXUNIVERSAL__
325 delete wxTheme::Set(NULL);
326#endif // __WXUNIVERSAL__
327
328 return wxAppConsole::OnExit();
329}
330
e2478fde 331void wxAppBase::Exit()
1e6feb95 332{
e2478fde 333 ExitMainLoop();
1e6feb95
VZ
334}
335
e2478fde 336wxAppTraits *wxAppBase::CreateTraits()
a69be60b 337{
7843d11b 338 return new wxGUIAppTraits;
72cdf4c9
VZ
339}
340
1e6feb95
VZ
341// ----------------------------------------------------------------------------
342// misc
343// ----------------------------------------------------------------------------
344
6e169cf3 345void wxAppBase::SetActive(bool active, wxWindow * WXUNUSED(lastFocus))
7beba2fc 346{
66dfed9b
VZ
347 if ( active == m_isActive )
348 return;
349
1e6feb95 350 m_isActive = active;
66dfed9b
VZ
351
352 wxActivateEvent event(wxEVT_ACTIVATE_APP, active);
353 event.SetEventObject(this);
354
355 (void)ProcessEvent(event);
7beba2fc 356}
1e6feb95 357
2dc62891
VZ
358// ----------------------------------------------------------------------------
359// idle handling
360// ----------------------------------------------------------------------------
361
94826170
VZ
362void wxAppBase::DeletePendingObjects()
363{
222ed1d6 364 wxList::compatibility_iterator node = wxPendingDelete.GetFirst();
94826170
VZ
365 while (node)
366 {
367 wxObject *obj = node->GetData();
368
73865dad
VZ
369 // remove it from the list first so that if we get back here somehow
370 // during the object deletion (e.g. wxYield called from its dtor) we
371 // wouldn't try to delete it the second time
372 if ( wxPendingDelete.Member(obj) )
222ed1d6 373 wxPendingDelete.Erase(node);
94826170 374
73865dad
VZ
375 delete obj;
376
94826170
VZ
377 // Deleting one object may have deleted other pending
378 // objects, so start from beginning of list again.
379 node = wxPendingDelete.GetFirst();
380 }
381}
382
4629016d 383// Returns true if more time is needed.
e39af974
JS
384bool wxAppBase::ProcessIdle()
385{
5109ae5d 386 wxIdleEvent event;
4629016d 387 bool needMore = false;
222ed1d6 388 wxWindowList::compatibility_iterator node = wxTopLevelWindows.GetFirst();
e39af974
JS
389 while (node)
390 {
391 wxWindow* win = node->GetData();
5109ae5d 392 if (SendIdleEvents(win, event))
4629016d 393 needMore = true;
e39af974
JS
394 node = node->GetNext();
395 }
396
e39af974 397 event.SetEventObject(this);
5109ae5d
JS
398 (void) ProcessEvent(event);
399 if (event.MoreRequested())
4629016d 400 needMore = true;
e39af974
JS
401
402 wxUpdateUIEvent::ResetUpdateTime();
4629016d 403
5109ae5d 404 return needMore;
e39af974
JS
405}
406
e39af974 407// Send idle event to window and all subwindows
5109ae5d 408bool wxAppBase::SendIdleEvents(wxWindow* win, wxIdleEvent& event)
e39af974 409{
4629016d 410 bool needMore = false;
42d11c8e 411
5109ae5d 412 win->OnInternalIdle();
42d11c8e 413
e39af974
JS
414 if (wxIdleEvent::CanSend(win))
415 {
e39af974
JS
416 event.SetEventObject(win);
417 win->GetEventHandler()->ProcessEvent(event);
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
fc7a2a60 435void wxAppBase::OnIdle(wxIdleEvent& WXUNUSED(event))
955a9197
JS
436{
437 // If there are pending events, we must process them: pending events
438 // are either events to the threads other than main or events posted
439 // with wxPostEvent() functions
440 // GRG: I have moved this here so that all pending events are processed
441 // before starting to delete any objects. This behaves better (in
442 // particular, wrt wxPostEvent) and is coherent with wxGTK's current
443 // behaviour. Changed Feb/2000 before 2.1.14
444 ProcessPendingEvents();
445
446 // 'Garbage' collection of windows deleted with Close().
447 DeletePendingObjects();
448
449#if wxUSE_LOG
450 // flush the logged messages if any
451 wxLog::FlushActive();
452#endif // wxUSE_LOG
453
454}
e39af974 455
2dc62891
VZ
456// ----------------------------------------------------------------------------
457// exceptions support
458// ----------------------------------------------------------------------------
459
460#if wxUSE_EXCEPTIONS
461
462bool wxAppBase::OnExceptionInMainLoop()
463{
464 throw;
465
466 // some compilers are too stupid to know that we never return after throw
467#if defined(__DMC__) || (defined(_MSC_VER) && _MSC_VER < 1200)
468 return false;
469#endif
470}
471
472#endif // wxUSE_EXCEPTIONS
473
bf188f1a 474// ----------------------------------------------------------------------------
e2478fde 475// wxGUIAppTraitsBase
bf188f1a
VZ
476// ----------------------------------------------------------------------------
477
bf188f1a 478#if wxUSE_LOG
bf188f1a 479
e2478fde
VZ
480wxLog *wxGUIAppTraitsBase::CreateLogTarget()
481{
461dae94 482#if wxUSE_LOGGUI
e2478fde 483 return new wxLogGui;
461dae94 484#else
fa6416df 485 // we must have something!
461dae94
VZ
486 return new wxLogStderr;
487#endif
bf188f1a
VZ
488}
489
bf188f1a
VZ
490#endif // wxUSE_LOG
491
e2478fde 492wxMessageOutput *wxGUIAppTraitsBase::CreateMessageOutput()
bf188f1a 493{
e2478fde
VZ
494 // The standard way of printing help on command line arguments (app --help)
495 // is (according to common practice):
496 // - console apps: to stderr (on any platform)
497 // - GUI apps: stderr on Unix platforms (!)
498 // message box under Windows and others
499#ifdef __UNIX__
500 return new wxMessageOutputStderr;
501#else // !__UNIX__
502 // wxMessageOutputMessageBox doesn't work under Motif
503 #ifdef __WXMOTIF__
504 return new wxMessageOutputLog;
505 #else
506 return new wxMessageOutputMessageBox;
507 #endif
508#endif // __UNIX__/!__UNIX__
bf188f1a
VZ
509}
510
e2478fde 511#if wxUSE_FONTMAP
bf188f1a 512
e2478fde
VZ
513wxFontMapper *wxGUIAppTraitsBase::CreateFontMapper()
514{
515 return new wxFontMapper;
bf188f1a
VZ
516}
517
e2478fde 518#endif // wxUSE_FONTMAP
bf188f1a 519
f0244295
VZ
520wxRendererNative *wxGUIAppTraitsBase::CreateRenderer()
521{
522 // use the default native renderer by default
523 return NULL;
524}
525
090a6d7a 526#ifdef __WXDEBUG__
e6e6fcc9 527
e2478fde
VZ
528bool wxGUIAppTraitsBase::ShowAssertDialog(const wxString& msg)
529{
530 // under MSW we prefer to use the base class version using ::MessageBox()
531 // even if wxMessageBox() is available because it has less chances to
532 // double fault our app than our wxMessageBox()
533#if defined(__WXMSW__) || !wxUSE_MSGDLG
534 return wxAppTraitsBase::ShowAssertDialog(msg);
535#else // wxUSE_MSGDLG
536 // this message is intentionally not translated -- it is for
537 // developpers only
538 wxString msgDlg(msg);
539 msgDlg += wxT("\nDo you want to stop the program?\n")
540 wxT("You can also choose [Cancel] to suppress ")
541 wxT("further warnings.");
542
77ffb593 543 switch ( wxMessageBox(msgDlg, wxT("wxWidgets Debug Alert"),
e2478fde
VZ
544 wxYES_NO | wxCANCEL | wxICON_STOP ) )
545 {
546 case wxYES:
547 wxTrap();
548 break;
090a6d7a 549
e2478fde
VZ
550 case wxCANCEL:
551 // no more asserts
552 return true;
a5f1fd3e 553
e2478fde 554 //case wxNO: nothing to do
090a6d7a 555 }
090a6d7a 556
e2478fde
VZ
557 return false;
558#endif // !wxUSE_MSGDLG/wxUSE_MSGDLG
a5f1fd3e
VZ
559}
560
e2478fde
VZ
561#endif // __WXDEBUG__
562
563bool wxGUIAppTraitsBase::HasStderr()
a5f1fd3e 564{
e2478fde
VZ
565 // we consider that under Unix stderr always goes somewhere, even if the
566 // user doesn't always see it under GUI desktops
567#ifdef __UNIX__
568 return true;
a5f1fd3e 569#else
e2478fde 570 return false;
a5f1fd3e 571#endif
a5f1fd3e
VZ
572}
573
e2478fde 574void wxGUIAppTraitsBase::ScheduleForDestroy(wxObject *object)
a5f1fd3e 575{
e2478fde
VZ
576 if ( !wxPendingDelete.Member(object) )
577 wxPendingDelete.Append(object);
a5f1fd3e
VZ
578}
579
e2478fde 580void wxGUIAppTraitsBase::RemoveFromPendingDelete(wxObject *object)
a5f1fd3e 581{
e2478fde 582 wxPendingDelete.DeleteObject(object);
a5f1fd3e
VZ
583}
584
38bb138f
VS
585#if wxUSE_SOCKETS
586
ae3036a2 587#if defined(__WINDOWS__)
38bb138f 588 #include "wx/msw/gsockmsw.h"
ae3036a2
RN
589#elif defined(__UNIX__) || defined(__DARWIN__) || defined(__OS2__)
590 #include "wx/unix/gsockunx.h"
69aa21ac 591#elif defined(__WXMAC__)
127eab18
WS
592 #include <MacHeaders.c>
593 #define OTUNIXERRORS 1
594 #include <OpenTransport.h>
595 #include <OpenTransportProviders.h>
596 #include <OpenTptInternet.h>
69aa21ac 597
127eab18 598 #include "wx/mac/gsockmac.h"
38bb138f
VS
599#else
600 #error "Must include correct GSocket header here"
601#endif
602
603GSocketGUIFunctionsTable* wxGUIAppTraitsBase::GetSocketGUIFunctionsTable()
604{
27a52ff9
GD
605#if defined(__WXMAC__) && !defined(__DARWIN__)
606 // NB: wxMac CFM does not have any GUI-specific functions in gsocket.c and
14a39351
VS
607 // so it doesn't need this table at all
608 return NULL;
27a52ff9 609#else // !__WXMAC__ || __DARWIN__
1d2ec8e5
DE
610 static GSocketGUIFunctionsTableConcrete table;
611 return &table;
1d2ec8e5 612#endif // !__WXMAC__ || __DARWIN__
38bb138f
VS
613}
614
615#endif