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