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"
49 #include "wx/evtloop.h"
51 #if defined(__WXMSW__)
52 #include "wx/msw/private.h" // includes windows.h for LOGFONT
56 #include "wx/fontmap.h"
57 #endif // wxUSE_FONTMAP
59 // DLL options compatibility check:
61 WX_CHECK_BUILD_OPTIONS("wxCore")
64 // ----------------------------------------------------------------------------
66 // ----------------------------------------------------------------------------
68 // this defines wxEventLoopPtr
69 wxDEFINE_TIED_SCOPED_PTR_TYPE(wxEventLoop
);
71 // ============================================================================
72 // wxAppBase implementation
73 // ============================================================================
75 // ----------------------------------------------------------------------------
77 // ----------------------------------------------------------------------------
79 wxAppBase
::wxAppBase()
81 m_topWindow
= (wxWindow
*)NULL
;
82 m_useBestVisual
= false;
85 #if wxUSE_EVTLOOP_IN_APP
87 #endif // wxUSE_EVTLOOP_IN_APP
89 // We don't want to exit the app if the user code shows a dialog from its
90 // OnInit() -- but this is what would happen if we set m_exitOnFrameDelete
91 // to Yes initially as this dialog would be the last top level window.
92 // OTOH, if we set it to No initially we'll have to overwrite it with Yes
93 // when we enter our OnRun() because we do want the default behaviour from
94 // then on. But this would be a problem if the user code calls
95 // SetExitOnFrameDelete(false) from OnInit().
97 // So we use the special "Later" value which is such that
98 // GetExitOnFrameDelete() returns false for it but which we know we can
99 // safely (i.e. without losing the effect of the users SetExitOnFrameDelete
100 // call) overwrite in OnRun()
101 m_exitOnFrameDelete
= Later
;
104 bool wxAppBase
::Initialize(int& argc
, wxChar
**argv
)
106 if ( !wxAppConsole
::Initialize(argc
, argv
) )
110 wxPendingEventsLocker
= new wxCriticalSection
;
113 wxInitializeStockLists();
114 wxInitializeStockObjects();
116 wxBitmap
::InitStandardHandlers();
121 // ----------------------------------------------------------------------------
123 // ----------------------------------------------------------------------------
125 wxAppBase
::~wxAppBase()
127 // this destructor is required for Darwin
130 void wxAppBase
::CleanUp()
132 // clean up all the pending objects
133 DeletePendingObjects();
135 // and any remaining TLWs (they remove themselves from wxTopLevelWindows
136 // when destroyed, so iterate until none are left)
137 while ( !wxTopLevelWindows
.empty() )
139 // do not use Destroy() here as it only puts the TLW in pending list
140 // but we want to delete them now
141 delete wxTopLevelWindows
.GetFirst()->GetData();
144 // undo everything we did in Initialize() above
145 wxBitmap
::CleanUpHandlers();
147 wxDeleteStockObjects();
149 wxDeleteStockLists();
151 delete wxTheColourDatabase
;
152 wxTheColourDatabase
= NULL
;
154 delete wxPendingEvents
;
155 wxPendingEvents
= NULL
;
158 delete wxPendingEventsLocker
;
159 wxPendingEventsLocker
= NULL
;
162 // If we don't do the following, we get an apparent memory leak.
163 ((wxEvtHandler
&) wxDefaultValidator
).ClearEventLocker();
164 #endif // wxUSE_VALIDATORS
165 #endif // wxUSE_THREADS
168 #if wxUSE_CMDLINE_PARSER
170 // ----------------------------------------------------------------------------
171 // GUI-specific command line options handling
172 // ----------------------------------------------------------------------------
174 #define OPTION_THEME _T("theme")
175 #define OPTION_MODE _T("mode")
177 void wxAppBase
::OnInitCmdLine(wxCmdLineParser
& parser
)
179 // first add the standard non GUI options
180 wxAppConsole
::OnInitCmdLine(parser
);
182 // the standard command line options
183 static const wxCmdLineEntryDesc cmdLineGUIDesc
[] =
185 #ifdef __WXUNIVERSAL__
190 gettext_noop("specify the theme to use"),
191 wxCMD_LINE_VAL_STRING
,
194 #endif // __WXUNIVERSAL__
196 #if defined(__WXMGL__)
197 // VS: this is not specific to wxMGL, all fullscreen (framebuffer) ports
198 // should provide this option. That's why it is in common/appcmn.cpp
199 // and not mgl/app.cpp
204 gettext_noop("specify display mode to use (e.g. 640x480-16)"),
205 wxCMD_LINE_VAL_STRING
,
221 parser
.SetDesc(cmdLineGUIDesc
);
224 bool wxAppBase
::OnCmdLineParsed(wxCmdLineParser
& parser
)
226 #ifdef __WXUNIVERSAL__
228 if ( parser
.Found(OPTION_THEME
, &themeName
) )
230 wxTheme
*theme
= wxTheme
::Create(themeName
);
233 wxLogError(_("Unsupported theme '%s'."), themeName
.c_str());
237 // Delete the defaultly created theme and set the new theme.
238 delete wxTheme
::Get();
241 #endif // __WXUNIVERSAL__
243 #if defined(__WXMGL__)
245 if ( parser
.Found(OPTION_MODE
, &modeDesc
) )
248 if ( wxSscanf(modeDesc
.c_str(), _T("%ux%u-%u"), &w
, &h
, &bpp
) != 3 )
250 wxLogError(_("Invalid display mode specification '%s'."), modeDesc
.c_str());
254 if ( !SetDisplayMode(wxVideoMode(w
, h
, bpp
)) )
259 return wxAppConsole
::OnCmdLineParsed(parser
);
262 #endif // wxUSE_CMDLINE_PARSER
264 // ----------------------------------------------------------------------------
265 // main event loop implementation
266 // ----------------------------------------------------------------------------
268 int wxAppBase
::MainLoop()
270 #if wxUSE_EVTLOOP_IN_APP
271 wxEventLoopTiedPtr
mainLoop(&m_mainLoop
, new wxEventLoop
);
273 return m_mainLoop
->Run();
274 #else // !wxUSE_EVTLOOP_IN_APP
276 #endif // wxUSE_EVTLOOP_IN_APP/!wxUSE_EVTLOOP_IN_APP
279 void wxAppBase
::ExitMainLoop()
281 #if wxUSE_EVTLOOP_IN_APP
282 // we should exit from the main event loop, not just any currently active
283 // (e.g. modal dialog) event loop
284 if ( m_mainLoop
&& m_mainLoop
->IsRunning() )
288 #endif // wxUSE_EVTLOOP_IN_APP
291 bool wxAppBase
::Pending()
293 #if wxUSE_EVTLOOP_IN_APP
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();
300 #else // wxUSE_EVTLOOP_IN_APP
302 #endif // wxUSE_EVTLOOP_IN_APP/!wxUSE_EVTLOOP_IN_APP
305 bool wxAppBase
::Dispatch()
307 #if wxUSE_EVTLOOP_IN_APP
308 // see comment in Pending()
309 wxEventLoop
* const loop
= wxEventLoop
::GetActive();
311 return loop
&& loop
->Dispatch();
312 #else // wxUSE_EVTLOOP_IN_APP
314 #endif // wxUSE_EVTLOOP_IN_APP/!wxUSE_EVTLOOP_IN_APP
317 // ----------------------------------------------------------------------------
319 // ----------------------------------------------------------------------------
321 bool wxAppBase
::OnInitGui()
323 #ifdef __WXUNIVERSAL__
324 if ( !wxTheme
::Get() && !wxTheme
::CreateDefault() )
326 #endif // __WXUNIVERSAL__
331 int wxAppBase
::OnRun()
333 // see the comment in ctor: if the initial value hasn't been changed, use
334 // the default Yes from now on
335 if ( m_exitOnFrameDelete
== Later
)
337 m_exitOnFrameDelete
= Yes
;
339 //else: it has been changed, assume the user knows what he is doing
344 int wxAppBase
::OnExit()
346 #ifdef __WXUNIVERSAL__
347 delete wxTheme
::Set(NULL
);
348 #endif // __WXUNIVERSAL__
350 return wxAppConsole
::OnExit();
353 void wxAppBase
::Exit()
358 wxAppTraits
*wxAppBase
::CreateTraits()
360 return new wxGUIAppTraits
;
363 // ----------------------------------------------------------------------------
365 // ----------------------------------------------------------------------------
367 void wxAppBase
::SetActive(bool active
, wxWindow
* WXUNUSED(lastFocus
))
369 if ( active
== m_isActive
)
374 wxActivateEvent
event(wxEVT_ACTIVATE_APP
, active
);
375 event
.SetEventObject(this);
377 (void)ProcessEvent(event
);
380 void wxAppBase
::DeletePendingObjects()
382 wxList
::compatibility_iterator node
= wxPendingDelete
.GetFirst();
385 wxObject
*obj
= node
->GetData();
389 if (wxPendingDelete
.Member(obj
))
390 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 // exception handling
473 // ----------------------------------------------------------------------------
477 void wxAppBase
::HandleEvent(wxEvtHandler
*handler
,
478 wxEventFunction func
,
479 wxEvent
& event
) const
481 // by default, call wxApp::OnExceptionInMainLoop if an exception occurs
484 handler
->DoHandleEvent(func
, event
);
488 if ( !wxConstCast(this, wxAppBase
)->OnExceptionInMainLoop() )
490 wxEventLoop
*loop
= wxEventLoop
::GetActive();
494 //else: continue running the event loop
498 #endif // wxUSE_EXCEPTIONS
500 // ----------------------------------------------------------------------------
501 // wxGUIAppTraitsBase
502 // ----------------------------------------------------------------------------
506 wxLog
*wxGUIAppTraitsBase
::CreateLogTarget()
511 // we must have something!
512 return new wxLogStderr
;
518 wxMessageOutput
*wxGUIAppTraitsBase
::CreateMessageOutput()
520 // The standard way of printing help on command line arguments (app --help)
521 // is (according to common practice):
522 // - console apps: to stderr (on any platform)
523 // - GUI apps: stderr on Unix platforms (!)
524 // message box under Windows and others
526 return new wxMessageOutputStderr
;
528 // wxMessageOutputMessageBox doesn't work under Motif
530 return new wxMessageOutputLog
;
532 return new wxMessageOutputMessageBox
;
534 #endif // __UNIX__/!__UNIX__
539 wxFontMapper
*wxGUIAppTraitsBase
::CreateFontMapper()
541 return new wxFontMapper
;
544 #endif // wxUSE_FONTMAP
546 wxRendererNative
*wxGUIAppTraitsBase
::CreateRenderer()
548 // use the default native renderer by default
554 bool wxGUIAppTraitsBase
::ShowAssertDialog(const wxString
& msg
)
556 // under MSW we prefer to use the base class version using ::MessageBox()
557 // even if wxMessageBox() is available because it has less chances to
558 // double fault our app than our wxMessageBox()
559 #if defined(__WXMSW__) || !wxUSE_MSGDLG
560 return wxAppTraitsBase
::ShowAssertDialog(msg
);
561 #else // wxUSE_MSGDLG
562 // this message is intentionally not translated -- it is for
564 wxString
msgDlg(msg
);
565 msgDlg
+= wxT("\nDo you want to stop the program?\n")
566 wxT("You can also choose [Cancel] to suppress ")
567 wxT("further warnings.");
569 switch ( wxMessageBox(msgDlg
, wxT("wxWidgets Debug Alert"),
570 wxYES_NO
| wxCANCEL
| wxICON_STOP
) )
580 //case wxNO: nothing to do
584 #endif // !wxUSE_MSGDLG/wxUSE_MSGDLG
587 #endif // __WXDEBUG__
589 bool wxGUIAppTraitsBase
::HasStderr()
591 // we consider that under Unix stderr always goes somewhere, even if the
592 // user doesn't always see it under GUI desktops
600 void wxGUIAppTraitsBase
::ScheduleForDestroy(wxObject
*object
)
602 if ( !wxPendingDelete
.Member(object
) )
603 wxPendingDelete
.Append(object
);
606 void wxGUIAppTraitsBase
::RemoveFromPendingDelete(wxObject
*object
)
608 wxPendingDelete
.DeleteObject(object
);
613 #if defined(__WINDOWS__)
614 #include "wx/msw/gsockmsw.h"
615 #elif defined(__UNIX__) || defined(__DARWIN__) || defined(__OS2__)
616 #include "wx/unix/gsockunx.h"
617 #elif defined(__WXMAC__)
618 #include <MacHeaders.c>
619 #define OTUNIXERRORS 1
620 #include <OpenTransport.h>
621 #include <OpenTransportProviders.h>
622 #include <OpenTptInternet.h>
624 #include "wx/mac/gsockmac.h"
626 #error "Must include correct GSocket header here"
629 GSocketGUIFunctionsTable
* wxGUIAppTraitsBase
::GetSocketGUIFunctionsTable()
631 #if defined(__WXMAC__) && !defined(__DARWIN__)
632 // NB: wxMac CFM does not have any GUI-specific functions in gsocket.c and
633 // so it doesn't need this table at all
635 #else // !__WXMAC__ || __DARWIN__
636 static GSocketGUIFunctionsTableConcrete table
;
638 #endif // !__WXMAC__ || __DARWIN__