1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/appcmn.cpp
3 // Purpose: wxAppConsole and wxAppBase methods common to all platforms
4 // Author: Vadim Zeitlin
8 // Copyright: (c) Vadim Zeitlin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ---------------------------------------------------------------------------
18 // ---------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
23 #if defined(__BORLANDC__)
29 #include "wx/bitmap.h"
33 #include "wx/msgdlg.h"
34 #include "wx/bitmap.h"
35 #include "wx/confbase.h"
38 #include "wx/apptrait.h"
39 #include "wx/cmdline.h"
40 #include "wx/evtloop.h"
41 #include "wx/msgout.h"
42 #include "wx/thread.h"
44 #include "wx/ptr_scpd.h"
45 #include "wx/evtloop.h"
47 #if defined(__WXMSW__)
48 #include "wx/msw/private.h" // includes windows.h for LOGFONT
52 #include "wx/fontmap.h"
53 #endif // wxUSE_FONTMAP
55 // DLL options compatibility check:
57 WX_CHECK_BUILD_OPTIONS("wxCore")
60 // ----------------------------------------------------------------------------
62 // ----------------------------------------------------------------------------
64 // this defines wxEventLoopPtr
65 #if wxUSE_EVTLOOP_IN_APP
66 wxDEFINE_TIED_SCOPED_PTR_TYPE(wxEventLoop
)
69 // ============================================================================
70 // wxAppBase implementation
71 // ============================================================================
73 // ----------------------------------------------------------------------------
75 // ----------------------------------------------------------------------------
77 wxAppBase::wxAppBase()
79 m_topWindow
= (wxWindow
*)NULL
;
80 m_useBestVisual
= false;
83 #if wxUSE_EVTLOOP_IN_APP
85 #endif // wxUSE_EVTLOOP_IN_APP
87 // We don't want to exit the app if the user code shows a dialog from its
88 // OnInit() -- but this is what would happen if we set m_exitOnFrameDelete
89 // to Yes initially as this dialog would be the last top level window.
90 // OTOH, if we set it to No initially we'll have to overwrite it with Yes
91 // when we enter our OnRun() because we do want the default behaviour from
92 // then on. But this would be a problem if the user code calls
93 // SetExitOnFrameDelete(false) from OnInit().
95 // So we use the special "Later" value which is such that
96 // GetExitOnFrameDelete() returns false for it but which we know we can
97 // safely (i.e. without losing the effect of the users SetExitOnFrameDelete
98 // call) overwrite in OnRun()
99 m_exitOnFrameDelete
= Later
;
102 bool wxAppBase::Initialize(int& argcOrig
, wxChar
**argvOrig
)
104 if ( !wxAppConsole::Initialize(argcOrig
, argvOrig
) )
108 wxPendingEventsLocker
= new wxCriticalSection
;
111 wxInitializeStockLists();
112 wxInitializeStockObjects();
114 wxBitmap::InitStandardHandlers();
119 // ----------------------------------------------------------------------------
121 // ----------------------------------------------------------------------------
123 wxAppBase::~wxAppBase()
125 // this destructor is required for Darwin
128 void wxAppBase::CleanUp()
130 // clean up all the pending objects
131 DeletePendingObjects();
133 // and any remaining TLWs (they remove themselves from wxTopLevelWindows
134 // when destroyed, so iterate until none are left)
135 while ( !wxTopLevelWindows
.empty() )
137 // do not use Destroy() here as it only puts the TLW in pending list
138 // but we want to delete them now
139 delete wxTopLevelWindows
.GetFirst()->GetData();
142 // undo everything we did in Initialize() above
143 wxBitmap::CleanUpHandlers();
145 wxDeleteStockObjects();
147 wxDeleteStockLists();
149 delete wxTheColourDatabase
;
150 wxTheColourDatabase
= NULL
;
152 delete wxPendingEvents
;
153 wxPendingEvents
= NULL
;
156 delete wxPendingEventsLocker
;
157 wxPendingEventsLocker
= NULL
;
160 // If we don't do the following, we get an apparent memory leak.
161 ((wxEvtHandler
&) wxDefaultValidator
).ClearEventLocker();
162 #endif // wxUSE_VALIDATORS
163 #endif // wxUSE_THREADS
166 #if wxUSE_CMDLINE_PARSER
168 // ----------------------------------------------------------------------------
169 // GUI-specific command line options handling
170 // ----------------------------------------------------------------------------
172 #define OPTION_THEME _T("theme")
173 #define OPTION_MODE _T("mode")
175 void wxAppBase::OnInitCmdLine(wxCmdLineParser
& parser
)
177 // first add the standard non GUI options
178 wxAppConsole::OnInitCmdLine(parser
);
180 // the standard command line options
181 static const wxCmdLineEntryDesc cmdLineGUIDesc
[] =
183 #ifdef __WXUNIVERSAL__
188 gettext_noop("specify the theme to use"),
189 wxCMD_LINE_VAL_STRING
,
192 #endif // __WXUNIVERSAL__
194 #if defined(__WXMGL__)
195 // VS: this is not specific to wxMGL, all fullscreen (framebuffer) ports
196 // should provide this option. That's why it is in common/appcmn.cpp
197 // and not mgl/app.cpp
202 gettext_noop("specify display mode to use (e.g. 640x480-16)"),
203 wxCMD_LINE_VAL_STRING
,
219 parser
.SetDesc(cmdLineGUIDesc
);
222 bool wxAppBase::OnCmdLineParsed(wxCmdLineParser
& parser
)
224 #ifdef __WXUNIVERSAL__
226 if ( parser
.Found(OPTION_THEME
, &themeName
) )
228 wxTheme
*theme
= wxTheme::Create(themeName
);
231 wxLogError(_("Unsupported theme '%s'."), themeName
.c_str());
235 // Delete the defaultly created theme and set the new theme.
236 delete wxTheme::Get();
239 #endif // __WXUNIVERSAL__
241 #if defined(__WXMGL__)
243 if ( parser
.Found(OPTION_MODE
, &modeDesc
) )
246 if ( wxSscanf(modeDesc
.c_str(), _T("%ux%u-%u"), &w
, &h
, &bpp
) != 3 )
248 wxLogError(_("Invalid display mode specification '%s'."), modeDesc
.c_str());
252 if ( !SetDisplayMode(wxVideoMode(w
, h
, bpp
)) )
257 return wxAppConsole::OnCmdLineParsed(parser
);
260 #endif // wxUSE_CMDLINE_PARSER
262 // ----------------------------------------------------------------------------
263 // main event loop implementation
264 // ----------------------------------------------------------------------------
266 int wxAppBase::MainLoop()
268 #if wxUSE_EVTLOOP_IN_APP
269 wxEventLoopTiedPtr
mainLoop(&m_mainLoop
, new wxEventLoop
);
271 return m_mainLoop
->Run();
272 #else // !wxUSE_EVTLOOP_IN_APP
274 #endif // wxUSE_EVTLOOP_IN_APP/!wxUSE_EVTLOOP_IN_APP
277 void wxAppBase::ExitMainLoop()
279 #if wxUSE_EVTLOOP_IN_APP
280 // we should exit from the main event loop, not just any currently active
281 // (e.g. modal dialog) event loop
282 if ( m_mainLoop
&& m_mainLoop
->IsRunning() )
286 #endif // wxUSE_EVTLOOP_IN_APP
289 bool wxAppBase::Pending()
291 #if wxUSE_EVTLOOP_IN_APP
292 // use the currently active message loop here, not m_mainLoop, because if
293 // we're showing a modal dialog (with its own event loop) currently the
294 // main event loop is not running anyhow
295 wxEventLoop
* const loop
= wxEventLoop::GetActive();
297 return loop
&& loop
->Pending();
298 #else // wxUSE_EVTLOOP_IN_APP
300 #endif // wxUSE_EVTLOOP_IN_APP/!wxUSE_EVTLOOP_IN_APP
303 bool wxAppBase::Dispatch()
305 #if wxUSE_EVTLOOP_IN_APP
306 // see comment in Pending()
307 wxEventLoop
* const loop
= wxEventLoop::GetActive();
309 return loop
&& loop
->Dispatch();
310 #else // wxUSE_EVTLOOP_IN_APP
312 #endif // wxUSE_EVTLOOP_IN_APP/!wxUSE_EVTLOOP_IN_APP
315 // ----------------------------------------------------------------------------
317 // ----------------------------------------------------------------------------
319 bool wxAppBase::OnInitGui()
321 #ifdef __WXUNIVERSAL__
322 if ( !wxTheme::Get() && !wxTheme::CreateDefault() )
324 #endif // __WXUNIVERSAL__
329 int wxAppBase::OnRun()
331 // see the comment in ctor: if the initial value hasn't been changed, use
332 // the default Yes from now on
333 if ( m_exitOnFrameDelete
== Later
)
335 m_exitOnFrameDelete
= Yes
;
337 //else: it has been changed, assume the user knows what he is doing
342 int wxAppBase::OnExit()
344 #ifdef __WXUNIVERSAL__
345 delete wxTheme::Set(NULL
);
346 #endif // __WXUNIVERSAL__
348 return wxAppConsole::OnExit();
351 void wxAppBase::Exit()
356 wxAppTraits
*wxAppBase::CreateTraits()
358 return new wxGUIAppTraits
;
361 // ----------------------------------------------------------------------------
363 // ----------------------------------------------------------------------------
365 void wxAppBase::SetActive(bool active
, wxWindow
* WXUNUSED(lastFocus
))
367 if ( active
== m_isActive
)
372 wxActivateEvent
event(wxEVT_ACTIVATE_APP
, active
);
373 event
.SetEventObject(this);
375 (void)ProcessEvent(event
);
378 void wxAppBase::DeletePendingObjects()
380 wxList::compatibility_iterator node
= wxPendingDelete
.GetFirst();
383 wxObject
*obj
= node
->GetData();
387 if (wxPendingDelete
.Member(obj
))
388 wxPendingDelete
.Erase(node
);
390 // Deleting one object may have deleted other pending
391 // objects, so start from beginning of list again.
392 node
= wxPendingDelete
.GetFirst();
396 // Returns true if more time is needed.
397 bool wxAppBase::ProcessIdle()
400 bool needMore
= false;
401 wxWindowList::compatibility_iterator node
= wxTopLevelWindows
.GetFirst();
404 wxWindow
* win
= node
->GetData();
405 if (SendIdleEvents(win
, event
))
407 node
= node
->GetNext();
410 event
.SetEventObject(this);
411 (void) ProcessEvent(event
);
412 if (event
.MoreRequested())
415 wxUpdateUIEvent::ResetUpdateTime();
420 // Send idle event to window and all subwindows
421 bool wxAppBase::SendIdleEvents(wxWindow
* win
, wxIdleEvent
& event
)
423 bool needMore
= false;
425 win
->OnInternalIdle();
427 if (wxIdleEvent::CanSend(win
))
429 event
.SetEventObject(win
);
430 win
->GetEventHandler()->ProcessEvent(event
);
432 if (event
.MoreRequested())
435 wxWindowList::compatibility_iterator node
= win
->GetChildren().GetFirst();
438 wxWindow
*child
= node
->GetData();
439 if (SendIdleEvents(child
, event
))
442 node
= node
->GetNext();
448 void wxAppBase::OnIdle(wxIdleEvent
& WXUNUSED(event
))
450 // If there are pending events, we must process them: pending events
451 // are either events to the threads other than main or events posted
452 // with wxPostEvent() functions
453 // GRG: I have moved this here so that all pending events are processed
454 // before starting to delete any objects. This behaves better (in
455 // particular, wrt wxPostEvent) and is coherent with wxGTK's current
456 // behaviour. Changed Feb/2000 before 2.1.14
457 ProcessPendingEvents();
459 // 'Garbage' collection of windows deleted with Close().
460 DeletePendingObjects();
463 // flush the logged messages if any
464 wxLog::FlushActive();
469 // ----------------------------------------------------------------------------
470 // exception handling
471 // ----------------------------------------------------------------------------
475 void wxAppBase::HandleEvent(wxEvtHandler
*handler
,
476 wxEventFunction func
,
477 wxEvent
& event
) const
479 // by default, call wxApp::OnExceptionInMainLoop if an exception occurs
482 handler
->DoHandleEvent(func
, event
);
486 if ( !wxConstCast(this, wxAppBase
)->OnExceptionInMainLoop() )
488 #if wxUSE_EVTLOOP_IN_APP
489 wxEventLoop
*loop
= wxEventLoop::GetActive();
493 wxConstCast(this, wxAppBase
)->ExitMainLoop();
496 //else: continue running the event loop
500 #endif // wxUSE_EXCEPTIONS
502 // ----------------------------------------------------------------------------
503 // wxGUIAppTraitsBase
504 // ----------------------------------------------------------------------------
508 wxLog
*wxGUIAppTraitsBase::CreateLogTarget()
513 // we must have something!
514 return new wxLogStderr
;
520 wxMessageOutput
*wxGUIAppTraitsBase::CreateMessageOutput()
522 // The standard way of printing help on command line arguments (app --help)
523 // is (according to common practice):
524 // - console apps: to stderr (on any platform)
525 // - GUI apps: stderr on Unix platforms (!)
526 // message box under Windows and others
528 return new wxMessageOutputStderr
;
530 // wxMessageOutputMessageBox doesn't work under Motif
532 return new wxMessageOutputLog
;
534 return new wxMessageOutputMessageBox
;
536 #endif // __UNIX__/!__UNIX__
541 wxFontMapper
*wxGUIAppTraitsBase::CreateFontMapper()
543 return new wxFontMapper
;
546 #endif // wxUSE_FONTMAP
548 wxRendererNative
*wxGUIAppTraitsBase::CreateRenderer()
550 // use the default native renderer by default
556 bool wxGUIAppTraitsBase::ShowAssertDialog(const wxString
& msg
)
558 // under MSW we prefer to use the base class version using ::MessageBox()
559 // even if wxMessageBox() is available because it has less chances to
560 // double fault our app than our wxMessageBox()
561 #if defined(__WXMSW__) || !wxUSE_MSGDLG
562 return wxAppTraitsBase::ShowAssertDialog(msg
);
563 #else // wxUSE_MSGDLG
564 // this message is intentionally not translated -- it is for
566 wxString
msgDlg(msg
);
567 msgDlg
+= wxT("\nDo you want to stop the program?\n")
568 wxT("You can also choose [Cancel] to suppress ")
569 wxT("further warnings.");
571 switch ( wxMessageBox(msgDlg
, wxT("wxWidgets Debug Alert"),
572 wxYES_NO
| wxCANCEL
| wxICON_STOP
) )
582 //case wxNO: nothing to do
586 #endif // !wxUSE_MSGDLG/wxUSE_MSGDLG
589 #endif // __WXDEBUG__
591 bool wxGUIAppTraitsBase::HasStderr()
593 // we consider that under Unix stderr always goes somewhere, even if the
594 // user doesn't always see it under GUI desktops
602 void wxGUIAppTraitsBase::ScheduleForDestroy(wxObject
*object
)
604 if ( !wxPendingDelete
.Member(object
) )
605 wxPendingDelete
.Append(object
);
608 void wxGUIAppTraitsBase::RemoveFromPendingDelete(wxObject
*object
)
610 wxPendingDelete
.DeleteObject(object
);
615 #if defined(__WINDOWS__)
616 #include "wx/msw/gsockmsw.h"
617 #elif defined(__UNIX__) || defined(__DARWIN__) || defined(__OS2__)
618 #include "wx/unix/gsockunx.h"
619 #elif defined(__WXMAC__)
620 #include <MacHeaders.c>
621 #define OTUNIXERRORS 1
622 #include <OpenTransport.h>
623 #include <OpenTransportProviders.h>
624 #include <OpenTptInternet.h>
626 #include "wx/mac/gsockmac.h"
628 #error "Must include correct GSocket header here"
631 GSocketGUIFunctionsTable
* wxGUIAppTraitsBase::GetSocketGUIFunctionsTable()
633 #if defined(__WXMAC__) && !defined(__DARWIN__)
634 // NB: wxMac CFM does not have any GUI-specific functions in gsocket.c and
635 // so it doesn't need this table at all
637 #else // !__WXMAC__ || __DARWIN__
638 static GSocketGUIFunctionsTableConcrete table
;
640 #endif // !__WXMAC__ || __DARWIN__