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"
46 #if defined(__WXMSW__)
47 #include "wx/msw/private.h" // includes windows.h for LOGFONT
51 #include "wx/fontmap.h"
52 #endif // wxUSE_FONTMAP
54 // DLL options compatibility check:
56 WX_CHECK_BUILD_OPTIONS("wxCore")
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;
80 #if wxUSE_EVTLOOP_IN_APP
82 #endif // wxUSE_EVTLOOP_IN_APP
84 // We don't want to exit the app if the user code shows a dialog from its
85 // OnInit() -- but this is what would happen if we set m_exitOnFrameDelete
86 // to Yes initially as this dialog would be the last top level window.
87 // OTOH, if we set it to No initially we'll have to overwrite it with Yes
88 // when we enter our OnRun() because we do want the default behaviour from
89 // then on. But this would be a problem if the user code calls
90 // SetExitOnFrameDelete(false) from OnInit().
92 // So we use the special "Later" value which is such that
93 // GetExitOnFrameDelete() returns false for it but which we know we can
94 // safely (i.e. without losing the effect of the users SetExitOnFrameDelete
95 // call) overwrite in OnRun()
96 m_exitOnFrameDelete
= Later
;
99 bool wxAppBase::Initialize(int& argcOrig
, wxChar
**argvOrig
)
101 if ( !wxAppConsole::Initialize(argcOrig
, argvOrig
) )
105 wxPendingEventsLocker
= new wxCriticalSection
;
108 wxInitializeStockLists();
109 wxInitializeStockObjects();
111 wxBitmap::InitStandardHandlers();
116 // ----------------------------------------------------------------------------
118 // ----------------------------------------------------------------------------
120 wxAppBase::~wxAppBase()
122 // this destructor is required for Darwin
125 void wxAppBase::CleanUp()
127 // clean up all the pending objects
128 DeletePendingObjects();
130 // and any remaining TLWs (they remove themselves from wxTopLevelWindows
131 // when destroyed, so iterate until none are left)
132 while ( !wxTopLevelWindows
.empty() )
134 // do not use Destroy() here as it only puts the TLW in pending list
135 // but we want to delete them now
136 delete wxTopLevelWindows
.GetFirst()->GetData();
139 // undo everything we did in Initialize() above
140 wxBitmap::CleanUpHandlers();
142 wxDeleteStockObjects();
144 wxDeleteStockLists();
146 delete wxTheColourDatabase
;
147 wxTheColourDatabase
= NULL
;
149 delete wxPendingEvents
;
150 wxPendingEvents
= NULL
;
153 delete wxPendingEventsLocker
;
154 wxPendingEventsLocker
= NULL
;
157 // If we don't do the following, we get an apparent memory leak.
158 ((wxEvtHandler
&) wxDefaultValidator
).ClearEventLocker();
159 #endif // wxUSE_VALIDATORS
160 #endif // wxUSE_THREADS
163 #if wxUSE_CMDLINE_PARSER
165 // ----------------------------------------------------------------------------
166 // GUI-specific command line options handling
167 // ----------------------------------------------------------------------------
169 #define OPTION_THEME _T("theme")
170 #define OPTION_MODE _T("mode")
172 void wxAppBase::OnInitCmdLine(wxCmdLineParser
& parser
)
174 // first add the standard non GUI options
175 wxAppConsole::OnInitCmdLine(parser
);
177 // the standard command line options
178 static const wxCmdLineEntryDesc cmdLineGUIDesc
[] =
180 #ifdef __WXUNIVERSAL__
185 gettext_noop("specify the theme to use"),
186 wxCMD_LINE_VAL_STRING
,
189 #endif // __WXUNIVERSAL__
191 #if defined(__WXMGL__)
192 // VS: this is not specific to wxMGL, all fullscreen (framebuffer) ports
193 // should provide this option. That's why it is in common/appcmn.cpp
194 // and not mgl/app.cpp
199 gettext_noop("specify display mode to use (e.g. 640x480-16)"),
200 wxCMD_LINE_VAL_STRING
,
216 parser
.SetDesc(cmdLineGUIDesc
);
219 bool wxAppBase::OnCmdLineParsed(wxCmdLineParser
& parser
)
221 #ifdef __WXUNIVERSAL__
223 if ( parser
.Found(OPTION_THEME
, &themeName
) )
225 wxTheme
*theme
= wxTheme::Create(themeName
);
228 wxLogError(_("Unsupported theme '%s'."), themeName
.c_str());
232 // Delete the defaultly created theme and set the new theme.
233 delete wxTheme::Get();
236 #endif // __WXUNIVERSAL__
238 #if defined(__WXMGL__)
240 if ( parser
.Found(OPTION_MODE
, &modeDesc
) )
243 if ( wxSscanf(modeDesc
.c_str(), _T("%ux%u-%u"), &w
, &h
, &bpp
) != 3 )
245 wxLogError(_("Invalid display mode specification '%s'."), modeDesc
.c_str());
249 if ( !SetDisplayMode(wxVideoMode(w
, h
, bpp
)) )
254 return wxAppConsole::OnCmdLineParsed(parser
);
257 #endif // wxUSE_CMDLINE_PARSER
259 // ----------------------------------------------------------------------------
260 // main event loop implementation
261 // ----------------------------------------------------------------------------
263 int wxAppBase::MainLoop()
265 #if wxUSE_EVTLOOP_IN_APP
266 wxEventLoopTiedPtr
mainLoop(&m_mainLoop
, new wxEventLoop
);
268 return m_mainLoop
->Run();
269 #else // !wxUSE_EVTLOOP_IN_APP
271 #endif // wxUSE_EVTLOOP_IN_APP/!wxUSE_EVTLOOP_IN_APP
274 void wxAppBase::ExitMainLoop()
276 #if wxUSE_EVTLOOP_IN_APP
277 // we should exit from the main event loop, not just any currently active
278 // (e.g. modal dialog) event loop
279 if ( m_mainLoop
&& m_mainLoop
->IsRunning() )
283 #endif // wxUSE_EVTLOOP_IN_APP
286 bool wxAppBase::Pending()
288 #if wxUSE_EVTLOOP_IN_APP
289 // use the currently active message loop here, not m_mainLoop, because if
290 // we're showing a modal dialog (with its own event loop) currently the
291 // main event loop is not running anyhow
292 wxEventLoop
* const loop
= wxEventLoop::GetActive();
294 return loop
&& loop
->Pending();
295 #else // wxUSE_EVTLOOP_IN_APP
297 #endif // wxUSE_EVTLOOP_IN_APP/!wxUSE_EVTLOOP_IN_APP
300 bool wxAppBase::Dispatch()
302 #if wxUSE_EVTLOOP_IN_APP
303 // see comment in Pending()
304 wxEventLoop
* const loop
= wxEventLoop::GetActive();
306 return loop
&& loop
->Dispatch();
307 #else // wxUSE_EVTLOOP_IN_APP
309 #endif // wxUSE_EVTLOOP_IN_APP/!wxUSE_EVTLOOP_IN_APP
312 // ----------------------------------------------------------------------------
314 // ----------------------------------------------------------------------------
316 bool wxAppBase::OnInitGui()
318 #ifdef __WXUNIVERSAL__
319 if ( !wxTheme::Get() && !wxTheme::CreateDefault() )
321 #endif // __WXUNIVERSAL__
326 int wxAppBase::OnRun()
328 // see the comment in ctor: if the initial value hasn't been changed, use
329 // the default Yes from now on
330 if ( m_exitOnFrameDelete
== Later
)
332 m_exitOnFrameDelete
= Yes
;
334 //else: it has been changed, assume the user knows what he is doing
339 int wxAppBase::OnExit()
341 #ifdef __WXUNIVERSAL__
342 delete wxTheme::Set(NULL
);
343 #endif // __WXUNIVERSAL__
345 return wxAppConsole::OnExit();
348 void wxAppBase::Exit()
353 wxAppTraits
*wxAppBase::CreateTraits()
355 return new wxGUIAppTraits
;
358 // ----------------------------------------------------------------------------
360 // ----------------------------------------------------------------------------
362 void wxAppBase::SetActive(bool active
, wxWindow
* WXUNUSED(lastFocus
))
364 if ( active
== m_isActive
)
369 wxActivateEvent
event(wxEVT_ACTIVATE_APP
, active
);
370 event
.SetEventObject(this);
372 (void)ProcessEvent(event
);
375 void wxAppBase::DeletePendingObjects()
377 wxList::compatibility_iterator node
= wxPendingDelete
.GetFirst();
380 wxObject
*obj
= node
->GetData();
384 if (wxPendingDelete
.Member(obj
))
385 wxPendingDelete
.Erase(node
);
387 // Deleting one object may have deleted other pending
388 // objects, so start from beginning of list again.
389 node
= wxPendingDelete
.GetFirst();
393 // Returns true if more time is needed.
394 bool wxAppBase::ProcessIdle()
397 bool needMore
= false;
398 wxWindowList::compatibility_iterator node
= wxTopLevelWindows
.GetFirst();
401 wxWindow
* win
= node
->GetData();
402 if (SendIdleEvents(win
, event
))
404 node
= node
->GetNext();
407 event
.SetEventObject(this);
408 (void) ProcessEvent(event
);
409 if (event
.MoreRequested())
412 wxUpdateUIEvent::ResetUpdateTime();
417 // Send idle event to window and all subwindows
418 bool wxAppBase::SendIdleEvents(wxWindow
* win
, wxIdleEvent
& event
)
420 bool needMore
= false;
422 win
->OnInternalIdle();
424 if (wxIdleEvent::CanSend(win
))
426 event
.SetEventObject(win
);
427 win
->GetEventHandler()->ProcessEvent(event
);
429 if (event
.MoreRequested())
432 wxWindowList::compatibility_iterator node
= win
->GetChildren().GetFirst();
435 wxWindow
*child
= node
->GetData();
436 if (SendIdleEvents(child
, event
))
439 node
= node
->GetNext();
445 void wxAppBase::OnIdle(wxIdleEvent
& WXUNUSED(event
))
447 // If there are pending events, we must process them: pending events
448 // are either events to the threads other than main or events posted
449 // with wxPostEvent() functions
450 // GRG: I have moved this here so that all pending events are processed
451 // before starting to delete any objects. This behaves better (in
452 // particular, wrt wxPostEvent) and is coherent with wxGTK's current
453 // behaviour. Changed Feb/2000 before 2.1.14
454 ProcessPendingEvents();
456 // 'Garbage' collection of windows deleted with Close().
457 DeletePendingObjects();
460 // flush the logged messages if any
461 wxLog::FlushActive();
466 // ----------------------------------------------------------------------------
467 // wxGUIAppTraitsBase
468 // ----------------------------------------------------------------------------
472 wxLog
*wxGUIAppTraitsBase::CreateLogTarget()
477 // we must have something!
478 return new wxLogStderr
;
484 wxMessageOutput
*wxGUIAppTraitsBase::CreateMessageOutput()
486 // The standard way of printing help on command line arguments (app --help)
487 // is (according to common practice):
488 // - console apps: to stderr (on any platform)
489 // - GUI apps: stderr on Unix platforms (!)
490 // message box under Windows and others
492 return new wxMessageOutputStderr
;
494 // wxMessageOutputMessageBox doesn't work under Motif
496 return new wxMessageOutputLog
;
498 return new wxMessageOutputMessageBox
;
500 #endif // __UNIX__/!__UNIX__
505 wxFontMapper
*wxGUIAppTraitsBase::CreateFontMapper()
507 return new wxFontMapper
;
510 #endif // wxUSE_FONTMAP
512 wxRendererNative
*wxGUIAppTraitsBase::CreateRenderer()
514 // use the default native renderer by default
520 bool wxGUIAppTraitsBase::ShowAssertDialog(const wxString
& msg
)
522 // under MSW we prefer to use the base class version using ::MessageBox()
523 // even if wxMessageBox() is available because it has less chances to
524 // double fault our app than our wxMessageBox()
525 #if defined(__WXMSW__) || !wxUSE_MSGDLG
526 return wxAppTraitsBase::ShowAssertDialog(msg
);
527 #else // wxUSE_MSGDLG
528 // this message is intentionally not translated -- it is for
530 wxString
msgDlg(msg
);
531 msgDlg
+= wxT("\nDo you want to stop the program?\n")
532 wxT("You can also choose [Cancel] to suppress ")
533 wxT("further warnings.");
535 switch ( wxMessageBox(msgDlg
, wxT("wxWidgets Debug Alert"),
536 wxYES_NO
| wxCANCEL
| wxICON_STOP
) )
546 //case wxNO: nothing to do
550 #endif // !wxUSE_MSGDLG/wxUSE_MSGDLG
553 #endif // __WXDEBUG__
555 bool wxGUIAppTraitsBase::HasStderr()
557 // we consider that under Unix stderr always goes somewhere, even if the
558 // user doesn't always see it under GUI desktops
566 void wxGUIAppTraitsBase::ScheduleForDestroy(wxObject
*object
)
568 if ( !wxPendingDelete
.Member(object
) )
569 wxPendingDelete
.Append(object
);
572 void wxGUIAppTraitsBase::RemoveFromPendingDelete(wxObject
*object
)
574 wxPendingDelete
.DeleteObject(object
);
579 #if defined(__WINDOWS__)
580 #include "wx/msw/gsockmsw.h"
581 #elif defined(__UNIX__) || defined(__DARWIN__) || defined(__OS2__)
582 #include "wx/unix/gsockunx.h"
583 #elif defined(__WXMAC__)
584 #include <MacHeaders.c>
585 #define OTUNIXERRORS 1
586 #include <OpenTransport.h>
587 #include <OpenTransportProviders.h>
588 #include <OpenTptInternet.h>
590 #include "wx/mac/gsockmac.h"
592 #error "Must include correct GSocket header here"
595 GSocketGUIFunctionsTable
* wxGUIAppTraitsBase::GetSocketGUIFunctionsTable()
597 #if defined(__WXMAC__) && !defined(__DARWIN__)
598 // NB: wxMac CFM does not have any GUI-specific functions in gsocket.c and
599 // so it doesn't need this table at all
601 #else // !__WXMAC__ || __DARWIN__
602 static GSocketGUIFunctionsTableConcrete table
;
604 #endif // !__WXMAC__ || __DARWIN__