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 // ---------------------------------------------------------------------------
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/msgout.h"
45 #include "wx/thread.h"
48 #if defined(__WXMSW__)
49 #include "wx/msw/private.h" // includes windows.h for LOGFONT
53 #include "wx/fontmap.h"
54 #endif // wxUSE_FONTMAP
56 // DLL options compatibility check:
58 WX_CHECK_BUILD_OPTIONS("wxCore")
60 // ============================================================================
61 // wxAppBase implementation
62 // ============================================================================
64 // ----------------------------------------------------------------------------
66 // ----------------------------------------------------------------------------
68 wxAppBase::wxAppBase()
70 m_topWindow
= (wxWindow
*)NULL
;
71 m_useBestVisual
= FALSE
;
74 // We don't want to exit the app if the user code shows a dialog from its
75 // OnInit() -- but this is what would happen if we set m_exitOnFrameDelete
76 // to Yes initially as this dialog would be the last top level window.
77 // OTOH, if we set it to No initially we'll have to overwrite it with Yes
78 // when we enter our OnRun() because we do want the default behaviour from
79 // then on. But this would be a problem if the user code calls
80 // SetExitOnFrameDelete(FALSE) from OnInit().
82 // So we use the special "Later" value which is such that
83 // GetExitOnFrameDelete() returns FALSE for it but which we know we can
84 // safely (i.e. without losing the effect of the users SetExitOnFrameDelete
85 // call) overwrite in OnRun()
86 m_exitOnFrameDelete
= Later
;
89 bool wxAppBase::Initialize(int& argc
, wxChar
**argv
)
91 if ( !wxAppConsole::Initialize(argc
, argv
) )
95 wxPendingEventsLocker
= new wxCriticalSection
;
98 wxInitializeStockLists();
99 wxInitializeStockObjects();
101 wxBitmap::InitStandardHandlers();
106 // ----------------------------------------------------------------------------
108 // ----------------------------------------------------------------------------
110 wxAppBase::~wxAppBase()
112 // this destructor is required for Darwin
115 void wxAppBase::CleanUp()
117 // one last chance for pending objects to be cleaned up
118 DeletePendingObjects();
120 wxBitmap::CleanUpHandlers();
122 wxDeleteStockObjects();
124 wxDeleteStockLists();
126 delete wxTheColourDatabase
;
127 wxTheColourDatabase
= NULL
;
130 delete wxPendingEvents
;
131 wxPendingEvents
= NULL
;
133 delete wxPendingEventsLocker
;
134 wxPendingEventsLocker
= NULL
;
137 // If we don't do the following, we get an apparent memory leak.
138 ((wxEvtHandler
&) wxDefaultValidator
).ClearEventLocker();
139 #endif // wxUSE_VALIDATORS
140 #endif // wxUSE_THREADS
143 #if wxUSE_CMDLINE_PARSER
145 // ----------------------------------------------------------------------------
146 // GUI-specific command line options handling
147 // ----------------------------------------------------------------------------
149 #define OPTION_THEME _T("theme")
150 #define OPTION_MODE _T("mode")
152 void wxAppBase::OnInitCmdLine(wxCmdLineParser
& parser
)
154 // first add the standard non GUI options
155 wxAppConsole::OnInitCmdLine(parser
);
157 // the standard command line options
158 static const wxCmdLineEntryDesc cmdLineGUIDesc
[] =
160 #ifdef __WXUNIVERSAL__
165 gettext_noop("specify the theme to use"),
166 wxCMD_LINE_VAL_STRING
,
169 #endif // __WXUNIVERSAL__
171 #if defined(__WXMGL__)
172 // VS: this is not specific to wxMGL, all fullscreen (framebuffer) ports
173 // should provide this option. That's why it is in common/appcmn.cpp
174 // and not mgl/app.cpp
179 gettext_noop("specify display mode to use (e.g. 640x480-16)"),
180 wxCMD_LINE_VAL_STRING
,
196 parser
.SetDesc(cmdLineGUIDesc
);
199 bool wxAppBase::OnCmdLineParsed(wxCmdLineParser
& parser
)
201 #ifdef __WXUNIVERSAL__
203 if ( parser
.Found(OPTION_THEME
, &themeName
) )
205 wxTheme
*theme
= wxTheme::Create(themeName
);
208 wxLogError(_("Unsupported theme '%s'."), themeName
.c_str());
212 // Delete the defaultly created theme and set the new theme.
213 delete wxTheme::Get();
216 #endif // __WXUNIVERSAL__
218 #if defined(__WXMGL__)
220 if ( parser
.Found(OPTION_MODE
, &modeDesc
) )
223 if ( wxSscanf(modeDesc
.c_str(), _T("%ux%u-%u"), &w
, &h
, &bpp
) != 3 )
225 wxLogError(_("Invalid display mode specification '%s'."), modeDesc
.c_str());
229 if ( !SetDisplayMode(wxDisplayModeInfo(w
, h
, bpp
)) )
234 return wxAppConsole::OnCmdLineParsed(parser
);
237 #endif // wxUSE_CMDLINE_PARSER
239 // ----------------------------------------------------------------------------
241 // ----------------------------------------------------------------------------
243 bool wxAppBase::OnInitGui()
245 #ifdef __WXUNIVERSAL__
246 if ( !wxTheme::Get() && !wxTheme::CreateDefault() )
248 #endif // __WXUNIVERSAL__
253 int wxAppBase::OnRun()
255 // see the comment in ctor: if the initial value hasn't been changed, use
256 // the default Yes from now on
257 if ( m_exitOnFrameDelete
== Later
)
259 m_exitOnFrameDelete
= Yes
;
261 //else: it has been changed, assume the user knows what he is doing
266 int wxAppBase::OnExit()
268 #ifdef __WXUNIVERSAL__
269 delete wxTheme::Set(NULL
);
270 #endif // __WXUNIVERSAL__
272 return wxAppConsole::OnExit();
275 void wxAppBase::Exit()
280 wxAppTraits
*wxAppBase::CreateTraits()
282 return new wxGUIAppTraits
;
285 // ----------------------------------------------------------------------------
287 // ----------------------------------------------------------------------------
289 void wxAppBase::SetActive(bool active
, wxWindow
* WXUNUSED(lastFocus
))
291 if ( active
== m_isActive
)
296 wxActivateEvent
event(wxEVT_ACTIVATE_APP
, active
);
297 event
.SetEventObject(this);
299 (void)ProcessEvent(event
);
302 void wxAppBase::DeletePendingObjects()
304 wxList::compatibility_iterator node
= wxPendingDelete
.GetFirst();
307 wxObject
*obj
= node
->GetData();
311 if (wxPendingDelete
.Member(obj
))
312 wxPendingDelete
.Erase(node
);
314 // Deleting one object may have deleted other pending
315 // objects, so start from beginning of list again.
316 node
= wxPendingDelete
.GetFirst();
320 // Returns TRUE if more time is needed.
321 bool wxAppBase::ProcessIdle()
324 bool needMore
= FALSE
;
325 wxWindowList::compatibility_iterator node
= wxTopLevelWindows
.GetFirst();
326 node
= wxTopLevelWindows
.GetFirst();
329 wxWindow
* win
= node
->GetData();
330 if (SendIdleEvents(win
, event
))
332 node
= node
->GetNext();
335 event
.SetEventObject(this);
336 (void) ProcessEvent(event
);
337 if (event
.MoreRequested())
340 wxUpdateUIEvent::ResetUpdateTime();
345 // Send idle event to window and all subwindows
346 bool wxAppBase::SendIdleEvents(wxWindow
* win
, wxIdleEvent
& event
)
348 bool needMore
= FALSE
;
350 win
->OnInternalIdle();
352 if (wxIdleEvent::CanSend(win
))
354 event
.SetEventObject(win
);
355 win
->GetEventHandler()->ProcessEvent(event
);
357 if (event
.MoreRequested())
360 wxWindowList::compatibility_iterator node
= win
->GetChildren().GetFirst();
363 wxWindow
*child
= node
->GetData();
364 if (SendIdleEvents(child
, event
))
367 node
= node
->GetNext();
373 void wxAppBase::OnIdle(wxIdleEvent
& WXUNUSED(event
))
375 // If there are pending events, we must process them: pending events
376 // are either events to the threads other than main or events posted
377 // with wxPostEvent() functions
378 // GRG: I have moved this here so that all pending events are processed
379 // before starting to delete any objects. This behaves better (in
380 // particular, wrt wxPostEvent) and is coherent with wxGTK's current
381 // behaviour. Changed Feb/2000 before 2.1.14
382 ProcessPendingEvents();
384 // 'Garbage' collection of windows deleted with Close().
385 DeletePendingObjects();
388 // flush the logged messages if any
389 wxLog::FlushActive();
394 // ----------------------------------------------------------------------------
395 // wxGUIAppTraitsBase
396 // ----------------------------------------------------------------------------
400 wxLog
*wxGUIAppTraitsBase::CreateLogTarget()
407 wxMessageOutput
*wxGUIAppTraitsBase::CreateMessageOutput()
409 // The standard way of printing help on command line arguments (app --help)
410 // is (according to common practice):
411 // - console apps: to stderr (on any platform)
412 // - GUI apps: stderr on Unix platforms (!)
413 // message box under Windows and others
415 return new wxMessageOutputStderr
;
417 // wxMessageOutputMessageBox doesn't work under Motif
419 return new wxMessageOutputLog
;
421 return new wxMessageOutputMessageBox
;
423 #endif // __UNIX__/!__UNIX__
428 wxFontMapper
*wxGUIAppTraitsBase::CreateFontMapper()
430 return new wxFontMapper
;
433 #endif // wxUSE_FONTMAP
435 wxRendererNative
*wxGUIAppTraitsBase::CreateRenderer()
437 // use the default native renderer by default
443 bool wxGUIAppTraitsBase::ShowAssertDialog(const wxString
& msg
)
445 // under MSW we prefer to use the base class version using ::MessageBox()
446 // even if wxMessageBox() is available because it has less chances to
447 // double fault our app than our wxMessageBox()
448 #if defined(__WXMSW__) || !wxUSE_MSGDLG
449 return wxAppTraitsBase::ShowAssertDialog(msg
);
450 #else // wxUSE_MSGDLG
451 // this message is intentionally not translated -- it is for
453 wxString
msgDlg(msg
);
454 msgDlg
+= wxT("\nDo you want to stop the program?\n")
455 wxT("You can also choose [Cancel] to suppress ")
456 wxT("further warnings.");
458 switch ( wxMessageBox(msgDlg
, wxT("wxWindows Debug Alert"),
459 wxYES_NO
| wxCANCEL
| wxICON_STOP
) )
469 //case wxNO: nothing to do
473 #endif // !wxUSE_MSGDLG/wxUSE_MSGDLG
476 #endif // __WXDEBUG__
478 bool wxGUIAppTraitsBase::HasStderr()
480 // we consider that under Unix stderr always goes somewhere, even if the
481 // user doesn't always see it under GUI desktops
489 void wxGUIAppTraitsBase::ScheduleForDestroy(wxObject
*object
)
491 if ( !wxPendingDelete
.Member(object
) )
492 wxPendingDelete
.Append(object
);
495 void wxGUIAppTraitsBase::RemoveFromPendingDelete(wxObject
*object
)
497 wxPendingDelete
.DeleteObject(object
);