1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "appbase.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
27 #if defined(__BORLANDC__)
33 #include "wx/bitmap.h"
37 #include "wx/msgdlg.h"
38 #include "wx/bitmap.h"
39 #include "wx/confbase.h"
42 #include "wx/apptrait.h"
43 #include "wx/cmdline.h"
44 #include "wx/evtloop.h"
45 #include "wx/msgout.h"
46 #include "wx/thread.h"
48 #include "wx/ptr_scpd.h"
50 #if defined(__WXMSW__) && !defined(__PALMOS__)
51 #include "wx/msw/private.h" // includes windows.h for LOGFONT
55 #include "wx/fontmap.h"
56 #endif // wxUSE_FONTMAP
58 // DLL options compatibility check:
60 WX_CHECK_BUILD_OPTIONS("wxCore")
63 // ----------------------------------------------------------------------------
65 // ----------------------------------------------------------------------------
67 // this defines wxEventLoopPtr
68 wxDEFINE_TIED_SCOPED_PTR_TYPE(wxEventLoop
);
70 // ============================================================================
71 // wxAppBase implementation
72 // ============================================================================
74 // ----------------------------------------------------------------------------
76 // ----------------------------------------------------------------------------
78 wxAppBase::wxAppBase()
80 m_topWindow
= (wxWindow
*)NULL
;
81 m_useBestVisual
= false;
84 #if wxUSE_EVTLOOP_IN_APP
86 #endif // wxUSE_EVTLOOP_IN_APP
88 // We don't want to exit the app if the user code shows a dialog from its
89 // OnInit() -- but this is what would happen if we set m_exitOnFrameDelete
90 // to Yes initially as this dialog would be the last top level window.
91 // OTOH, if we set it to No initially we'll have to overwrite it with Yes
92 // when we enter our OnRun() because we do want the default behaviour from
93 // then on. But this would be a problem if the user code calls
94 // SetExitOnFrameDelete(false) from OnInit().
96 // So we use the special "Later" value which is such that
97 // GetExitOnFrameDelete() returns false for it but which we know we can
98 // safely (i.e. without losing the effect of the users SetExitOnFrameDelete
99 // call) overwrite in OnRun()
100 m_exitOnFrameDelete
= Later
;
103 bool wxAppBase::Initialize(int& argc
, wxChar
**argv
)
105 if ( !wxAppConsole::Initialize(argc
, argv
) )
109 wxPendingEventsLocker
= new wxCriticalSection
;
112 wxInitializeStockLists();
113 wxInitializeStockObjects();
115 wxBitmap::InitStandardHandlers();
120 // ----------------------------------------------------------------------------
122 // ----------------------------------------------------------------------------
124 wxAppBase::~wxAppBase()
126 // this destructor is required for Darwin
129 void wxAppBase::CleanUp()
131 // clean up all the pending objects
132 DeletePendingObjects();
134 // and any remaining TLWs (they remove themselves from wxTopLevelWindows
135 // when destroyed, so iterate until none are left)
136 while ( !wxTopLevelWindows
.empty() )
138 // do not use Destroy() here as it only puts the TLW in pending list
139 // but we want to delete them now
140 delete wxTopLevelWindows
.GetFirst()->GetData();
143 // undo everything we did in Initialize() above
144 wxBitmap::CleanUpHandlers();
146 wxDeleteStockObjects();
148 wxDeleteStockLists();
150 delete wxTheColourDatabase
;
151 wxTheColourDatabase
= NULL
;
153 delete wxPendingEvents
;
154 wxPendingEvents
= NULL
;
157 delete wxPendingEventsLocker
;
158 wxPendingEventsLocker
= NULL
;
161 // If we don't do the following, we get an apparent memory leak.
162 ((wxEvtHandler
&) wxDefaultValidator
).ClearEventLocker();
163 #endif // wxUSE_VALIDATORS
164 #endif // wxUSE_THREADS
167 #if wxUSE_CMDLINE_PARSER
169 // ----------------------------------------------------------------------------
170 // GUI-specific command line options handling
171 // ----------------------------------------------------------------------------
173 #define OPTION_THEME _T("theme")
174 #define OPTION_MODE _T("mode")
176 void wxAppBase::OnInitCmdLine(wxCmdLineParser
& parser
)
178 // first add the standard non GUI options
179 wxAppConsole::OnInitCmdLine(parser
);
181 // the standard command line options
182 static const wxCmdLineEntryDesc cmdLineGUIDesc
[] =
184 #ifdef __WXUNIVERSAL__
189 gettext_noop("specify the theme to use"),
190 wxCMD_LINE_VAL_STRING
,
193 #endif // __WXUNIVERSAL__
195 #if defined(__WXMGL__)
196 // VS: this is not specific to wxMGL, all fullscreen (framebuffer) ports
197 // should provide this option. That's why it is in common/appcmn.cpp
198 // and not mgl/app.cpp
203 gettext_noop("specify display mode to use (e.g. 640x480-16)"),
204 wxCMD_LINE_VAL_STRING
,
220 parser
.SetDesc(cmdLineGUIDesc
);
223 bool wxAppBase::OnCmdLineParsed(wxCmdLineParser
& parser
)
225 #ifdef __WXUNIVERSAL__
227 if ( parser
.Found(OPTION_THEME
, &themeName
) )
229 wxTheme
*theme
= wxTheme::Create(themeName
);
232 wxLogError(_("Unsupported theme '%s'."), themeName
.c_str());
236 // Delete the defaultly created theme and set the new theme.
237 delete wxTheme::Get();
240 #endif // __WXUNIVERSAL__
242 #if defined(__WXMGL__)
244 if ( parser
.Found(OPTION_MODE
, &modeDesc
) )
247 if ( wxSscanf(modeDesc
.c_str(), _T("%ux%u-%u"), &w
, &h
, &bpp
) != 3 )
249 wxLogError(_("Invalid display mode specification '%s'."), modeDesc
.c_str());
253 if ( !SetDisplayMode(wxVideoMode(w
, h
, bpp
)) )
258 return wxAppConsole::OnCmdLineParsed(parser
);
261 #endif // wxUSE_CMDLINE_PARSER
263 // ----------------------------------------------------------------------------
264 // main event loop implementation
265 // ----------------------------------------------------------------------------
267 int wxAppBase::MainLoop()
269 #if wxUSE_EVTLOOP_IN_APP
270 wxEventLoopTiedPtr
mainLoop(&m_mainLoop
, new wxEventLoop
);
272 return m_mainLoop
->Run();
273 #else // !wxUSE_EVTLOOP_IN_APP
275 #endif // wxUSE_EVTLOOP_IN_APP/!wxUSE_EVTLOOP_IN_APP
278 void wxAppBase::ExitMainLoop()
280 #if wxUSE_EVTLOOP_IN_APP
281 // we should exit from the main event loop, not just any currently active
282 // (e.g. modal dialog) event loop
283 if ( m_mainLoop
&& m_mainLoop
->IsRunning() )
287 #endif // wxUSE_EVTLOOP_IN_APP
290 bool wxAppBase::Pending()
292 #if wxUSE_EVTLOOP_IN_APP
293 // use the currently active message loop here, not m_mainLoop, because if
294 // we're showing a modal dialog (with its own event loop) currently the
295 // main event loop is not running anyhow
296 wxEventLoop
* const loop
= wxEventLoop::GetActive();
298 return loop
&& loop
->Pending();
299 #else // wxUSE_EVTLOOP_IN_APP
301 #endif // wxUSE_EVTLOOP_IN_APP/!wxUSE_EVTLOOP_IN_APP
304 bool wxAppBase::Dispatch()
306 #if wxUSE_EVTLOOP_IN_APP
307 // see comment in Pending()
308 wxEventLoop
* const loop
= wxEventLoop::GetActive();
310 return loop
&& loop
->Dispatch();
311 #else // wxUSE_EVTLOOP_IN_APP
313 #endif // wxUSE_EVTLOOP_IN_APP/!wxUSE_EVTLOOP_IN_APP
316 // ----------------------------------------------------------------------------
318 // ----------------------------------------------------------------------------
320 bool wxAppBase::OnInitGui()
322 #ifdef __WXUNIVERSAL__
323 if ( !wxTheme::Get() && !wxTheme::CreateDefault() )
325 #endif // __WXUNIVERSAL__
330 int wxAppBase::OnRun()
332 // see the comment in ctor: if the initial value hasn't been changed, use
333 // the default Yes from now on
334 if ( m_exitOnFrameDelete
== Later
)
336 m_exitOnFrameDelete
= Yes
;
338 //else: it has been changed, assume the user knows what he is doing
343 int wxAppBase::OnExit()
345 #ifdef __WXUNIVERSAL__
346 delete wxTheme::Set(NULL
);
347 #endif // __WXUNIVERSAL__
349 return wxAppConsole::OnExit();
352 void wxAppBase::Exit()
357 wxAppTraits
*wxAppBase::CreateTraits()
359 return new wxGUIAppTraits
;
362 // ----------------------------------------------------------------------------
364 // ----------------------------------------------------------------------------
366 void wxAppBase::SetActive(bool active
, wxWindow
* WXUNUSED(lastFocus
))
368 if ( active
== m_isActive
)
373 wxActivateEvent
event(wxEVT_ACTIVATE_APP
, active
);
374 event
.SetEventObject(this);
376 (void)ProcessEvent(event
);
379 void wxAppBase::DeletePendingObjects()
381 wxList::compatibility_iterator node
= wxPendingDelete
.GetFirst();
384 wxObject
*obj
= node
->GetData();
388 if (wxPendingDelete
.Member(obj
))
389 wxPendingDelete
.Erase(node
);
391 // Deleting one object may have deleted other pending
392 // objects, so start from beginning of list again.
393 node
= wxPendingDelete
.GetFirst();
397 // Returns true if more time is needed.
398 bool wxAppBase::ProcessIdle()
401 bool needMore
= false;
402 wxWindowList::compatibility_iterator node
= wxTopLevelWindows
.GetFirst();
405 wxWindow
* win
= node
->GetData();
406 if (SendIdleEvents(win
, event
))
408 node
= node
->GetNext();
411 event
.SetEventObject(this);
412 (void) ProcessEvent(event
);
413 if (event
.MoreRequested())
416 wxUpdateUIEvent::ResetUpdateTime();
421 // Send idle event to window and all subwindows
422 bool wxAppBase::SendIdleEvents(wxWindow
* win
, wxIdleEvent
& event
)
424 bool needMore
= false;
426 win
->OnInternalIdle();
428 if (wxIdleEvent::CanSend(win
))
430 event
.SetEventObject(win
);
431 win
->GetEventHandler()->ProcessEvent(event
);
433 if (event
.MoreRequested())
436 wxWindowList::compatibility_iterator node
= win
->GetChildren().GetFirst();
439 wxWindow
*child
= node
->GetData();
440 if (SendIdleEvents(child
, event
))
443 node
= node
->GetNext();
449 void wxAppBase::OnIdle(wxIdleEvent
& WXUNUSED(event
))
451 // If there are pending events, we must process them: pending events
452 // are either events to the threads other than main or events posted
453 // with wxPostEvent() functions
454 // GRG: I have moved this here so that all pending events are processed
455 // before starting to delete any objects. This behaves better (in
456 // particular, wrt wxPostEvent) and is coherent with wxGTK's current
457 // behaviour. Changed Feb/2000 before 2.1.14
458 ProcessPendingEvents();
460 // 'Garbage' collection of windows deleted with Close().
461 DeletePendingObjects();
464 // flush the logged messages if any
465 wxLog::FlushActive();
470 // ----------------------------------------------------------------------------
471 // wxGUIAppTraitsBase
472 // ----------------------------------------------------------------------------
476 wxLog
*wxGUIAppTraitsBase::CreateLogTarget()
481 // wem ust have something!
482 return new wxLogStderr
;
488 wxMessageOutput
*wxGUIAppTraitsBase::CreateMessageOutput()
490 // The standard way of printing help on command line arguments (app --help)
491 // is (according to common practice):
492 // - console apps: to stderr (on any platform)
493 // - GUI apps: stderr on Unix platforms (!)
494 // message box under Windows and others
496 return new wxMessageOutputStderr
;
498 // wxMessageOutputMessageBox doesn't work under Motif
500 return new wxMessageOutputLog
;
502 return new wxMessageOutputMessageBox
;
504 #endif // __UNIX__/!__UNIX__
509 wxFontMapper
*wxGUIAppTraitsBase::CreateFontMapper()
511 return new wxFontMapper
;
514 #endif // wxUSE_FONTMAP
516 wxRendererNative
*wxGUIAppTraitsBase::CreateRenderer()
518 // use the default native renderer by default
524 bool wxGUIAppTraitsBase::ShowAssertDialog(const wxString
& msg
)
526 // under MSW we prefer to use the base class version using ::MessageBox()
527 // even if wxMessageBox() is available because it has less chances to
528 // double fault our app than our wxMessageBox()
529 #if defined(__WXMSW__) || !wxUSE_MSGDLG
530 return wxAppTraitsBase::ShowAssertDialog(msg
);
531 #else // wxUSE_MSGDLG
532 // this message is intentionally not translated -- it is for
534 wxString
msgDlg(msg
);
535 msgDlg
+= wxT("\nDo you want to stop the program?\n")
536 wxT("You can also choose [Cancel] to suppress ")
537 wxT("further warnings.");
539 switch ( wxMessageBox(msgDlg
, wxT("wxWidgets Debug Alert"),
540 wxYES_NO
| wxCANCEL
| wxICON_STOP
) )
550 //case wxNO: nothing to do
554 #endif // !wxUSE_MSGDLG/wxUSE_MSGDLG
557 #endif // __WXDEBUG__
559 bool wxGUIAppTraitsBase::HasStderr()
561 // we consider that under Unix stderr always goes somewhere, even if the
562 // user doesn't always see it under GUI desktops
570 void wxGUIAppTraitsBase::ScheduleForDestroy(wxObject
*object
)
572 if ( !wxPendingDelete
.Member(object
) )
573 wxPendingDelete
.Append(object
);
576 void wxGUIAppTraitsBase::RemoveFromPendingDelete(wxObject
*object
)
578 wxPendingDelete
.DeleteObject(object
);
583 #if defined(__UNIX__) || defined(__DARWIN__) || defined(__OS2__)
584 #include "wx/unix/gsockunx.h"
585 #elif defined(__WINDOWS__)
586 #include "wx/msw/gsockmsw.h"
587 #elif defined(__WXMAC__)
588 #include <MacHeaders.c>
589 #define OTUNIXERRORS 1
590 #include <OpenTransport.h>
591 #include <OpenTransportProviders.h>
592 #include <OpenTptInternet.h>
594 #include "wx/mac/gsockmac.h"
596 #error "Must include correct GSocket header here"
599 GSocketGUIFunctionsTable
* wxGUIAppTraitsBase::GetSocketGUIFunctionsTable()
601 #if defined(__WXMAC__) && !defined(__DARWIN__)
602 // NB: wxMac CFM does not have any GUI-specific functions in gsocket.c and
603 // so it doesn't need this table at all
605 #else // !__WXMAC__ || __DARWIN__
606 static GSocketGUIFunctionsTableConcrete table
;
608 #endif // !__WXMAC__ || __DARWIN__