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/window.h"
30 #include "wx/bitmap.h"
32 #include "wx/msgdlg.h"
33 #include "wx/confbase.h"
37 #include "wx/apptrait.h"
38 #include "wx/cmdline.h"
39 #include "wx/evtloop.h"
40 #include "wx/msgout.h"
41 #include "wx/thread.h"
42 #include "wx/vidmode.h"
43 #include "wx/ptr_scpd.h"
45 #if defined(__WXMSW__)
46 #include "wx/msw/private.h" // includes windows.h for LOGFONT
50 #include "wx/fontmap.h"
51 #endif // wxUSE_FONTMAP
53 // DLL options compatibility check:
55 WX_CHECK_BUILD_OPTIONS("wxCore")
57 WXDLLIMPEXP_DATA_CORE(wxList
) wxPendingDelete
;
59 // ----------------------------------------------------------------------------
61 // ----------------------------------------------------------------------------
63 // this defines wxEventLoopPtr
64 wxDEFINE_TIED_SCOPED_PTR_TYPE(wxEventLoop
)
66 // ============================================================================
67 // wxAppBase implementation
68 // ============================================================================
70 // ----------------------------------------------------------------------------
72 // ----------------------------------------------------------------------------
74 wxAppBase::wxAppBase()
76 m_topWindow
= (wxWindow
*)NULL
;
77 m_useBestVisual
= false;
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
88 // SetExitOnFrameDelete(false) from OnInit().
90 // So we use the special "Later" value which is such that
91 // GetExitOnFrameDelete() returns false for it but which we know we can
92 // safely (i.e. without losing the effect of the users SetExitOnFrameDelete
93 // call) overwrite in OnRun()
94 m_exitOnFrameDelete
= Later
;
97 bool wxAppBase::Initialize(int& argcOrig
, wxChar
**argvOrig
)
99 if ( !wxAppConsole::Initialize(argcOrig
, argvOrig
) )
103 wxPendingEventsLocker
= new wxCriticalSection
;
106 wxInitializeStockLists();
108 wxBitmap::InitStandardHandlers();
113 // ----------------------------------------------------------------------------
115 // ----------------------------------------------------------------------------
117 wxAppBase::~wxAppBase()
119 // this destructor is required for Darwin
122 void wxAppBase::CleanUp()
124 // clean up all the pending objects
125 DeletePendingObjects();
127 // and any remaining TLWs (they remove themselves from wxTopLevelWindows
128 // when destroyed, so iterate until none are left)
129 while ( !wxTopLevelWindows
.empty() )
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();
136 // undo everything we did in Initialize() above
137 wxBitmap::CleanUpHandlers();
139 wxStockGDI::DeleteAll();
141 wxDeleteStockLists();
143 delete wxTheColourDatabase
;
144 wxTheColourDatabase
= NULL
;
146 delete wxPendingEvents
;
147 wxPendingEvents
= NULL
;
150 delete wxPendingEventsLocker
;
151 wxPendingEventsLocker
= NULL
;
154 // If we don't do the following, we get an apparent memory leak.
155 ((wxEvtHandler
&) wxDefaultValidator
).ClearEventLocker();
156 #endif // wxUSE_VALIDATORS
157 #endif // wxUSE_THREADS
160 // ----------------------------------------------------------------------------
162 wxWindow
* wxAppBase::GetTopWindow() const
164 wxWindow
* window
= m_topWindow
;
165 if (window
== NULL
&& wxTopLevelWindows
.GetCount() > 0)
166 window
= wxTopLevelWindows
.GetFirst()->GetData();
170 wxVideoMode
wxAppBase::GetDisplayMode() const
172 return wxVideoMode();
175 #if wxUSE_CMDLINE_PARSER
177 // ----------------------------------------------------------------------------
178 // GUI-specific command line options handling
179 // ----------------------------------------------------------------------------
181 #define OPTION_THEME _T("theme")
182 #define OPTION_MODE _T("mode")
184 void wxAppBase::OnInitCmdLine(wxCmdLineParser
& parser
)
186 // first add the standard non GUI options
187 wxAppConsole::OnInitCmdLine(parser
);
189 // the standard command line options
190 static const wxCmdLineEntryDesc cmdLineGUIDesc
[] =
192 #ifdef __WXUNIVERSAL__
197 gettext_noop("specify the theme to use"),
198 wxCMD_LINE_VAL_STRING
,
201 #endif // __WXUNIVERSAL__
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
211 gettext_noop("specify display mode to use (e.g. 640x480-16)"),
212 wxCMD_LINE_VAL_STRING
,
228 parser
.SetDesc(cmdLineGUIDesc
);
231 bool wxAppBase::OnCmdLineParsed(wxCmdLineParser
& parser
)
233 #ifdef __WXUNIVERSAL__
235 if ( parser
.Found(OPTION_THEME
, &themeName
) )
237 wxTheme
*theme
= wxTheme::Create(themeName
);
240 wxLogError(_("Unsupported theme '%s'."), themeName
.c_str());
244 // Delete the defaultly created theme and set the new theme.
245 delete wxTheme::Get();
248 #endif // __WXUNIVERSAL__
250 #if defined(__WXMGL__)
252 if ( parser
.Found(OPTION_MODE
, &modeDesc
) )
255 if ( wxSscanf(modeDesc
.c_str(), _T("%ux%u-%u"), &w
, &h
, &bpp
) != 3 )
257 wxLogError(_("Invalid display mode specification '%s'."), modeDesc
.c_str());
261 if ( !SetDisplayMode(wxVideoMode(w
, h
, bpp
)) )
266 return wxAppConsole::OnCmdLineParsed(parser
);
269 #endif // wxUSE_CMDLINE_PARSER
271 // ----------------------------------------------------------------------------
272 // main event loop implementation
273 // ----------------------------------------------------------------------------
275 int wxAppBase::MainLoop()
277 wxEventLoopTiedPtr
mainLoop(&m_mainLoop
, new wxEventLoop
);
279 return m_mainLoop
->Run();
282 void wxAppBase::ExitMainLoop()
284 // we should exit from the main event loop, not just any currently active
285 // (e.g. modal dialog) event loop
286 if ( m_mainLoop
&& m_mainLoop
->IsRunning() )
292 bool wxAppBase::Pending()
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();
299 return loop
&& loop
->Pending();
302 bool wxAppBase::Dispatch()
304 // see comment in Pending()
305 wxEventLoop
* const loop
= wxEventLoop::GetActive();
307 return loop
&& loop
->Dispatch();
310 // ----------------------------------------------------------------------------
312 // ----------------------------------------------------------------------------
314 bool wxAppBase::OnInitGui()
316 #ifdef __WXUNIVERSAL__
317 if ( !wxTheme::Get() && !wxTheme::CreateDefault() )
319 #endif // __WXUNIVERSAL__
324 int wxAppBase::OnRun()
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
)
330 m_exitOnFrameDelete
= Yes
;
332 //else: it has been changed, assume the user knows what he is doing
337 int wxAppBase::OnExit()
339 #ifdef __WXUNIVERSAL__
340 delete wxTheme::Set(NULL
);
341 #endif // __WXUNIVERSAL__
343 return wxAppConsole::OnExit();
346 void wxAppBase::Exit()
351 wxAppTraits
*wxAppBase::CreateTraits()
353 return new wxGUIAppTraits
;
356 // ----------------------------------------------------------------------------
358 // ----------------------------------------------------------------------------
360 void wxAppBase::SetActive(bool active
, wxWindow
* WXUNUSED(lastFocus
))
362 if ( active
== m_isActive
)
367 wxActivateEvent
event(wxEVT_ACTIVATE_APP
, active
);
368 event
.SetEventObject(this);
370 (void)ProcessEvent(event
);
373 // ----------------------------------------------------------------------------
375 // ----------------------------------------------------------------------------
377 void wxAppBase::DeletePendingObjects()
379 wxList::compatibility_iterator node
= wxPendingDelete
.GetFirst();
382 wxObject
*obj
= node
->GetData();
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
) )
388 wxPendingDelete
.Erase(node
);
392 // Deleting one object may have deleted other pending
393 // objects, so start from beginning of list again.
394 node
= wxPendingDelete
.GetFirst();
398 // Returns true if more time is needed.
399 bool wxAppBase::ProcessIdle()
402 bool needMore
= false;
403 wxWindowList::compatibility_iterator node
= wxTopLevelWindows
.GetFirst();
406 wxWindow
* win
= node
->GetData();
407 if (SendIdleEvents(win
, event
))
409 node
= node
->GetNext();
412 event
.SetEventObject(this);
413 (void) ProcessEvent(event
);
414 if (event
.MoreRequested())
417 wxUpdateUIEvent::ResetUpdateTime();
422 // Send idle event to window and all subwindows
423 bool wxAppBase::SendIdleEvents(wxWindow
* win
, wxIdleEvent
& event
)
425 bool needMore
= false;
427 win
->OnInternalIdle();
429 if (wxIdleEvent::CanSend(win
))
431 event
.SetEventObject(win
);
432 win
->GetEventHandler()->ProcessEvent(event
);
434 if (event
.MoreRequested())
437 wxWindowList::compatibility_iterator node
= win
->GetChildren().GetFirst();
440 wxWindow
*child
= node
->GetData();
441 if (SendIdleEvents(child
, event
))
444 node
= node
->GetNext();
450 void wxAppBase::OnIdle(wxIdleEvent
& WXUNUSED(event
))
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();
461 // 'Garbage' collection of windows deleted with Close().
462 DeletePendingObjects();
465 // flush the logged messages if any
466 wxLog::FlushActive();
471 // ----------------------------------------------------------------------------
472 // exceptions support
473 // ----------------------------------------------------------------------------
477 bool wxAppBase::OnExceptionInMainLoop()
481 // some compilers are too stupid to know that we never return after throw
482 #if defined(__DMC__) || (defined(_MSC_VER) && _MSC_VER < 1200)
487 #endif // wxUSE_EXCEPTIONS
489 // ----------------------------------------------------------------------------
490 // wxGUIAppTraitsBase
491 // ----------------------------------------------------------------------------
495 wxLog
*wxGUIAppTraitsBase::CreateLogTarget()
500 // we must have something!
501 return new wxLogStderr
;
507 wxMessageOutput
*wxGUIAppTraitsBase::CreateMessageOutput()
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
515 return new wxMessageOutputStderr
;
517 // wxMessageOutputMessageBox doesn't work under Motif
519 return new wxMessageOutputLog
;
521 return new wxMessageOutputMessageBox
;
523 #endif // __UNIX__/!__UNIX__
528 wxFontMapper
*wxGUIAppTraitsBase::CreateFontMapper()
530 return new wxFontMapper
;
533 #endif // wxUSE_FONTMAP
535 wxRendererNative
*wxGUIAppTraitsBase::CreateRenderer()
537 // use the default native renderer by default
543 bool wxGUIAppTraitsBase::ShowAssertDialog(const wxString
& msg
)
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
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.");
558 switch ( wxMessageBox(msgDlg
, wxT("wxWidgets Debug Alert"),
559 wxYES_NO
| wxCANCEL
| wxICON_STOP
) )
569 //case wxNO: nothing to do
573 #endif // !wxUSE_MSGDLG/wxUSE_MSGDLG
576 #endif // __WXDEBUG__
578 bool wxGUIAppTraitsBase::HasStderr()
580 // we consider that under Unix stderr always goes somewhere, even if the
581 // user doesn't always see it under GUI desktops
589 void wxGUIAppTraitsBase::ScheduleForDestroy(wxObject
*object
)
591 if ( !wxPendingDelete
.Member(object
) )
592 wxPendingDelete
.Append(object
);
595 void wxGUIAppTraitsBase::RemoveFromPendingDelete(wxObject
*object
)
597 wxPendingDelete
.DeleteObject(object
);
602 #if defined(__WINDOWS__)
603 #include "wx/msw/gsockmsw.h"
604 #elif defined(__UNIX__) || defined(__DARWIN__) || defined(__OS2__)
605 #include "wx/unix/gsockunx.h"
606 #elif defined(__WXMAC__)
607 #include <MacHeaders.c>
608 #define OTUNIXERRORS 1
609 #include <OpenTransport.h>
610 #include <OpenTransportProviders.h>
611 #include <OpenTptInternet.h>
613 #include "wx/mac/gsockmac.h"
615 #error "Must include correct GSocket header here"
618 GSocketGUIFunctionsTable
* wxGUIAppTraitsBase::GetSocketGUIFunctionsTable()
620 #if defined(__WXMAC__) && !defined(__DARWIN__)
621 // NB: wxMac CFM does not have any GUI-specific functions in gsocket.c and
622 // so it doesn't need this table at all
624 #else // !__WXMAC__ || __DARWIN__
625 static GSocketGUIFunctionsTableConcrete table
;
627 #endif // !__WXMAC__ || __DARWIN__