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__)
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_SCOPED_PTR_TYPE(wxEventLoop
);
70 // but we need a smart pointer tied to wxAppBase::m_mainLoop, so we define
71 // another helper class
72 class wxTiedEventLoopPtr
: public wxEventLoopPtr
75 wxTiedEventLoopPtr(wxEventLoop
**ppEvtLoop
, wxEventLoop
*pLoop
)
76 : wxEventLoopPtr(*ppEvtLoop
= pLoop
), m_ppEvtLoop(ppEvtLoop
)
80 ~wxTiedEventLoopPtr() { *m_ppEvtLoop
= NULL
; }
83 wxEventLoop
**m_ppEvtLoop
;
86 // ============================================================================
87 // wxAppBase implementation
88 // ============================================================================
90 // ----------------------------------------------------------------------------
92 // ----------------------------------------------------------------------------
94 wxAppBase::wxAppBase()
96 m_topWindow
= (wxWindow
*)NULL
;
97 m_useBestVisual
= FALSE
;
100 #if wxUSE_EVTLOOP_IN_APP
102 #endif // wxUSE_EVTLOOP_IN_APP
104 // We don't want to exit the app if the user code shows a dialog from its
105 // OnInit() -- but this is what would happen if we set m_exitOnFrameDelete
106 // to Yes initially as this dialog would be the last top level window.
107 // OTOH, if we set it to No initially we'll have to overwrite it with Yes
108 // when we enter our OnRun() because we do want the default behaviour from
109 // then on. But this would be a problem if the user code calls
110 // SetExitOnFrameDelete(FALSE) from OnInit().
112 // So we use the special "Later" value which is such that
113 // GetExitOnFrameDelete() returns FALSE for it but which we know we can
114 // safely (i.e. without losing the effect of the users SetExitOnFrameDelete
115 // call) overwrite in OnRun()
116 m_exitOnFrameDelete
= Later
;
119 bool wxAppBase::Initialize(int& argc
, wxChar
**argv
)
121 if ( !wxAppConsole::Initialize(argc
, argv
) )
125 wxPendingEventsLocker
= new wxCriticalSection
;
128 wxInitializeStockLists();
129 wxInitializeStockObjects();
131 wxBitmap::InitStandardHandlers();
136 // ----------------------------------------------------------------------------
138 // ----------------------------------------------------------------------------
140 wxAppBase::~wxAppBase()
142 // this destructor is required for Darwin
145 void wxAppBase::CleanUp()
147 // one last chance for pending objects to be cleaned up
148 DeletePendingObjects();
150 wxBitmap::CleanUpHandlers();
152 wxDeleteStockObjects();
154 wxDeleteStockLists();
156 delete wxTheColourDatabase
;
157 wxTheColourDatabase
= NULL
;
160 delete wxPendingEvents
;
161 wxPendingEvents
= NULL
;
163 delete wxPendingEventsLocker
;
164 wxPendingEventsLocker
= NULL
;
167 // If we don't do the following, we get an apparent memory leak.
168 ((wxEvtHandler
&) wxDefaultValidator
).ClearEventLocker();
169 #endif // wxUSE_VALIDATORS
170 #endif // wxUSE_THREADS
173 #if wxUSE_CMDLINE_PARSER
175 // ----------------------------------------------------------------------------
176 // GUI-specific command line options handling
177 // ----------------------------------------------------------------------------
179 #define OPTION_THEME _T("theme")
180 #define OPTION_MODE _T("mode")
182 void wxAppBase::OnInitCmdLine(wxCmdLineParser
& parser
)
184 // first add the standard non GUI options
185 wxAppConsole::OnInitCmdLine(parser
);
187 // the standard command line options
188 static const wxCmdLineEntryDesc cmdLineGUIDesc
[] =
190 #ifdef __WXUNIVERSAL__
195 gettext_noop("specify the theme to use"),
196 wxCMD_LINE_VAL_STRING
,
199 #endif // __WXUNIVERSAL__
201 #if defined(__WXMGL__)
202 // VS: this is not specific to wxMGL, all fullscreen (framebuffer) ports
203 // should provide this option. That's why it is in common/appcmn.cpp
204 // and not mgl/app.cpp
209 gettext_noop("specify display mode to use (e.g. 640x480-16)"),
210 wxCMD_LINE_VAL_STRING
,
226 parser
.SetDesc(cmdLineGUIDesc
);
229 bool wxAppBase::OnCmdLineParsed(wxCmdLineParser
& parser
)
231 #ifdef __WXUNIVERSAL__
233 if ( parser
.Found(OPTION_THEME
, &themeName
) )
235 wxTheme
*theme
= wxTheme::Create(themeName
);
238 wxLogError(_("Unsupported theme '%s'."), themeName
.c_str());
242 // Delete the defaultly created theme and set the new theme.
243 delete wxTheme::Get();
246 #endif // __WXUNIVERSAL__
248 #if defined(__WXMGL__)
250 if ( parser
.Found(OPTION_MODE
, &modeDesc
) )
253 if ( wxSscanf(modeDesc
.c_str(), _T("%ux%u-%u"), &w
, &h
, &bpp
) != 3 )
255 wxLogError(_("Invalid display mode specification '%s'."), modeDesc
.c_str());
259 if ( !SetDisplayMode(wxDisplayModeInfo(w
, h
, bpp
)) )
264 return wxAppConsole::OnCmdLineParsed(parser
);
267 #endif // wxUSE_CMDLINE_PARSER
269 // ----------------------------------------------------------------------------
270 // main event loop implementation
271 // ----------------------------------------------------------------------------
273 int wxAppBase::MainLoop()
275 #if wxUSE_EVTLOOP_IN_APP
276 wxTiedEventLoopPtr
mainLoop(&m_mainLoop
, new wxEventLoop
);
278 return m_mainLoop
->Run();
279 #else // !wxUSE_EVTLOOP_IN_APP
281 #endif // wxUSE_EVTLOOP_IN_APP/!wxUSE_EVTLOOP_IN_APP
284 void wxAppBase::ExitMainLoop()
286 #if wxUSE_EVTLOOP_IN_APP
287 // we should exit from the main event loop, not just any currently active
288 // (e.g. modal dialog) event loop
293 #endif // wxUSE_EVTLOOP_IN_APP
296 bool wxAppBase::Pending()
298 #if wxUSE_EVTLOOP_IN_APP
299 // use the currently active message loop here, not m_mainLoop, because if
300 // we're showing a modal dialog (with its own event loop) currently the
301 // main event loop is not running anyhow
302 wxEventLoop
* const loop
= wxEventLoop::GetActive();
304 return loop
&& loop
->Pending();
305 #else // wxUSE_EVTLOOP_IN_APP
307 #endif // wxUSE_EVTLOOP_IN_APP/!wxUSE_EVTLOOP_IN_APP
310 bool wxAppBase::Dispatch()
312 #if wxUSE_EVTLOOP_IN_APP
313 // see comment in Pending()
314 wxEventLoop
* const loop
= wxEventLoop::GetActive();
316 return loop
&& loop
->Dispatch();
317 #else // wxUSE_EVTLOOP_IN_APP
319 #endif // wxUSE_EVTLOOP_IN_APP/!wxUSE_EVTLOOP_IN_APP
322 // ----------------------------------------------------------------------------
324 // ----------------------------------------------------------------------------
326 bool wxAppBase::OnInitGui()
328 #ifdef __WXUNIVERSAL__
329 if ( !wxTheme::Get() && !wxTheme::CreateDefault() )
331 #endif // __WXUNIVERSAL__
336 int wxAppBase::OnRun()
338 // see the comment in ctor: if the initial value hasn't been changed, use
339 // the default Yes from now on
340 if ( m_exitOnFrameDelete
== Later
)
342 m_exitOnFrameDelete
= Yes
;
344 //else: it has been changed, assume the user knows what he is doing
349 int wxAppBase::OnExit()
351 #ifdef __WXUNIVERSAL__
352 delete wxTheme::Set(NULL
);
353 #endif // __WXUNIVERSAL__
355 return wxAppConsole::OnExit();
358 void wxAppBase::Exit()
363 wxAppTraits
*wxAppBase::CreateTraits()
365 return new wxGUIAppTraits
;
368 // ----------------------------------------------------------------------------
370 // ----------------------------------------------------------------------------
372 void wxAppBase::SetActive(bool active
, wxWindow
* WXUNUSED(lastFocus
))
374 if ( active
== m_isActive
)
379 wxActivateEvent
event(wxEVT_ACTIVATE_APP
, active
);
380 event
.SetEventObject(this);
382 (void)ProcessEvent(event
);
385 void wxAppBase::DeletePendingObjects()
387 wxList::compatibility_iterator node
= wxPendingDelete
.GetFirst();
390 wxObject
*obj
= node
->GetData();
394 if (wxPendingDelete
.Member(obj
))
395 wxPendingDelete
.Erase(node
);
397 // Deleting one object may have deleted other pending
398 // objects, so start from beginning of list again.
399 node
= wxPendingDelete
.GetFirst();
403 // Returns TRUE if more time is needed.
404 bool wxAppBase::ProcessIdle()
407 bool needMore
= FALSE
;
408 wxWindowList::compatibility_iterator node
= wxTopLevelWindows
.GetFirst();
411 wxWindow
* win
= node
->GetData();
412 if (SendIdleEvents(win
, event
))
414 node
= node
->GetNext();
417 event
.SetEventObject(this);
418 (void) ProcessEvent(event
);
419 if (event
.MoreRequested())
422 wxUpdateUIEvent::ResetUpdateTime();
427 // Send idle event to window and all subwindows
428 bool wxAppBase::SendIdleEvents(wxWindow
* win
, wxIdleEvent
& event
)
430 bool needMore
= FALSE
;
432 win
->OnInternalIdle();
434 if (wxIdleEvent::CanSend(win
))
436 event
.SetEventObject(win
);
437 win
->GetEventHandler()->ProcessEvent(event
);
439 if (event
.MoreRequested())
442 wxWindowList::compatibility_iterator node
= win
->GetChildren().GetFirst();
445 wxWindow
*child
= node
->GetData();
446 if (SendIdleEvents(child
, event
))
449 node
= node
->GetNext();
455 void wxAppBase::OnIdle(wxIdleEvent
& WXUNUSED(event
))
457 // If there are pending events, we must process them: pending events
458 // are either events to the threads other than main or events posted
459 // with wxPostEvent() functions
460 // GRG: I have moved this here so that all pending events are processed
461 // before starting to delete any objects. This behaves better (in
462 // particular, wrt wxPostEvent) and is coherent with wxGTK's current
463 // behaviour. Changed Feb/2000 before 2.1.14
464 ProcessPendingEvents();
466 // 'Garbage' collection of windows deleted with Close().
467 DeletePendingObjects();
470 // flush the logged messages if any
471 wxLog::FlushActive();
476 // ----------------------------------------------------------------------------
477 // wxGUIAppTraitsBase
478 // ----------------------------------------------------------------------------
482 wxLog
*wxGUIAppTraitsBase::CreateLogTarget()
489 wxMessageOutput
*wxGUIAppTraitsBase::CreateMessageOutput()
491 // The standard way of printing help on command line arguments (app --help)
492 // is (according to common practice):
493 // - console apps: to stderr (on any platform)
494 // - GUI apps: stderr on Unix platforms (!)
495 // message box under Windows and others
497 return new wxMessageOutputStderr
;
499 // wxMessageOutputMessageBox doesn't work under Motif
501 return new wxMessageOutputLog
;
503 return new wxMessageOutputMessageBox
;
505 #endif // __UNIX__/!__UNIX__
510 wxFontMapper
*wxGUIAppTraitsBase::CreateFontMapper()
512 return new wxFontMapper
;
515 #endif // wxUSE_FONTMAP
517 wxRendererNative
*wxGUIAppTraitsBase::CreateRenderer()
519 // use the default native renderer by default
525 bool wxGUIAppTraitsBase::ShowAssertDialog(const wxString
& msg
)
527 // under MSW we prefer to use the base class version using ::MessageBox()
528 // even if wxMessageBox() is available because it has less chances to
529 // double fault our app than our wxMessageBox()
530 #if defined(__WXMSW__) || !wxUSE_MSGDLG
531 return wxAppTraitsBase::ShowAssertDialog(msg
);
532 #else // wxUSE_MSGDLG
533 // this message is intentionally not translated -- it is for
535 wxString
msgDlg(msg
);
536 msgDlg
+= wxT("\nDo you want to stop the program?\n")
537 wxT("You can also choose [Cancel] to suppress ")
538 wxT("further warnings.");
540 switch ( wxMessageBox(msgDlg
, wxT("wxWindows Debug Alert"),
541 wxYES_NO
| wxCANCEL
| wxICON_STOP
) )
551 //case wxNO: nothing to do
555 #endif // !wxUSE_MSGDLG/wxUSE_MSGDLG
558 #endif // __WXDEBUG__
560 bool wxGUIAppTraitsBase::HasStderr()
562 // we consider that under Unix stderr always goes somewhere, even if the
563 // user doesn't always see it under GUI desktops
571 void wxGUIAppTraitsBase::ScheduleForDestroy(wxObject
*object
)
573 if ( !wxPendingDelete
.Member(object
) )
574 wxPendingDelete
.Append(object
);
577 void wxGUIAppTraitsBase::RemoveFromPendingDelete(wxObject
*object
)
579 wxPendingDelete
.DeleteObject(object
);
584 #if defined(__UNIX__) || defined(__DARWIN__) || defined(__OS2__)
585 #include "wx/unix/gsockunx.h"
586 #elif defined(__WINDOWS__)
587 #include "wx/msw/gsockmsw.h"
588 #elif defined(__WXMAC__)
589 #include <MacHeaders.c>
590 #define OTUNIXERRORS 1
591 #include <OpenTransport.h>
592 #include <OpenTransportProviders.h>
593 #include <OpenTptInternet.h>
595 #include "wx/mac/gsockmac.h"
597 #error "Must include correct GSocket header here"
600 GSocketGUIFunctionsTable
* wxGUIAppTraitsBase::GetSocketGUIFunctionsTable()
603 // NB: wxMac does not have any GUI-specific functions in gsocket.c and
604 // so it doesn't need this table at all
607 static GSocketGUIFunctionsTable table
=
610 _GSocket_GUI_Cleanup
,
611 _GSocket_GUI_Init_Socket
,
612 _GSocket_GUI_Destroy_Socket
,
614 _GSocket_Install_Callback
,
615 _GSocket_Uninstall_Callback
,
617 _GSocket_Enable_Events
,
618 _GSocket_Disable_Events
621 #endif // __WXMAC__/!__WXMAC__