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 // ============================================================================
57 // wxAppBase implementation
58 // ============================================================================
60 // ----------------------------------------------------------------------------
62 // ----------------------------------------------------------------------------
64 wxAppBase::wxAppBase()
66 m_topWindow
= (wxWindow
*)NULL
;
67 m_useBestVisual
= FALSE
;
70 // We don't want to exit the app if the user code shows a dialog from its
71 // OnInit() -- but this is what would happen if we set m_exitOnFrameDelete
72 // to Yes initially as this dialog would be the last top level window.
73 // OTOH, if we set it to No initially we'll have to overwrite it with Yes
74 // when we enter our OnRun() because we do want the default behaviour from
75 // then on. But this would be a problem if the user code calls
76 // SetExitOnFrameDelete(FALSE) from OnInit().
78 // So we use the special "Later" value which is such that
79 // GetExitOnFrameDelete() returns FALSE for it but which we know we can
80 // safely (i.e. without losing the effect of the users SetExitOnFrameDelete
81 // call) overwrite in OnRun()
82 m_exitOnFrameDelete
= Later
;
85 bool wxAppBase::Initialize(int& argc
, wxChar
**argv
)
87 if ( !wxAppConsole::Initialize(argc
, argv
) )
91 wxPendingEventsLocker
= new wxCriticalSection
;
94 wxInitializeStockLists();
95 wxInitializeStockObjects();
97 wxBitmap::InitStandardHandlers();
102 // ----------------------------------------------------------------------------
104 // ----------------------------------------------------------------------------
106 wxAppBase::~wxAppBase()
108 // this destructor is required for Darwin
111 void wxAppBase::CleanUp()
113 // one last chance for pending objects to be cleaned up
114 DeletePendingObjects();
116 wxBitmap::CleanUpHandlers();
118 wxDeleteStockObjects();
120 wxDeleteStockLists();
122 delete wxTheColourDatabase
;
123 wxTheColourDatabase
= NULL
;
126 delete wxPendingEvents
;
127 wxPendingEvents
= NULL
;
129 delete wxPendingEventsLocker
;
130 wxPendingEventsLocker
= NULL
;
133 // If we don't do the following, we get an apparent memory leak.
134 ((wxEvtHandler
&) wxDefaultValidator
).ClearEventLocker();
135 #endif // wxUSE_VALIDATORS
136 #endif // wxUSE_THREADS
139 #if wxUSE_CMDLINE_PARSER
141 // ----------------------------------------------------------------------------
142 // GUI-specific command line options handling
143 // ----------------------------------------------------------------------------
145 #define OPTION_THEME _T("theme")
146 #define OPTION_MODE _T("mode")
148 void wxAppBase::OnInitCmdLine(wxCmdLineParser
& parser
)
150 // first add the standard non GUI options
151 wxAppConsole::OnInitCmdLine(parser
);
153 // the standard command line options
154 static const wxCmdLineEntryDesc cmdLineGUIDesc
[] =
156 #ifdef __WXUNIVERSAL__
161 gettext_noop("specify the theme to use"),
162 wxCMD_LINE_VAL_STRING
,
165 #endif // __WXUNIVERSAL__
167 #if defined(__WXMGL__)
168 // VS: this is not specific to wxMGL, all fullscreen (framebuffer) ports
169 // should provide this option. That's why it is in common/appcmn.cpp
170 // and not mgl/app.cpp
175 gettext_noop("specify display mode to use (e.g. 640x480-16)"),
176 wxCMD_LINE_VAL_STRING
,
192 parser
.SetDesc(cmdLineGUIDesc
);
195 bool wxAppBase::OnCmdLineParsed(wxCmdLineParser
& parser
)
197 #ifdef __WXUNIVERSAL__
199 if ( parser
.Found(OPTION_THEME
, &themeName
) )
201 wxTheme
*theme
= wxTheme::Create(themeName
);
204 wxLogError(_("Unsupported theme '%s'."), themeName
.c_str());
208 // Delete the defaultly created theme and set the new theme.
209 delete wxTheme::Get();
212 #endif // __WXUNIVERSAL__
214 #if defined(__WXMGL__)
216 if ( parser
.Found(OPTION_MODE
, &modeDesc
) )
219 if ( wxSscanf(modeDesc
.c_str(), _T("%ux%u-%u"), &w
, &h
, &bpp
) != 3 )
221 wxLogError(_("Invalid display mode specification '%s'."), modeDesc
.c_str());
225 if ( !SetDisplayMode(wxDisplayModeInfo(w
, h
, bpp
)) )
230 return wxAppConsole::OnCmdLineParsed(parser
);
233 #endif // wxUSE_CMDLINE_PARSER
235 // ----------------------------------------------------------------------------
237 // ----------------------------------------------------------------------------
239 bool wxAppBase::OnInitGui()
241 #ifdef __WXUNIVERSAL__
242 if ( !wxTheme::Get() && !wxTheme::CreateDefault() )
244 #endif // __WXUNIVERSAL__
249 int wxAppBase::OnRun()
251 // see the comment in ctor: if the initial value hasn't been changed, use
252 // the default Yes from now on
253 if ( m_exitOnFrameDelete
== Later
)
255 m_exitOnFrameDelete
= Yes
;
257 //else: it has been changed, assume the user knows what he is doing
262 int wxAppBase::OnExit()
264 #ifdef __WXUNIVERSAL__
265 delete wxTheme::Set(NULL
);
266 #endif // __WXUNIVERSAL__
268 return wxAppConsole::OnExit();
271 void wxAppBase::Exit()
276 wxAppTraits
*wxAppBase::CreateTraits()
278 return new wxGUIAppTraits
;
281 // ----------------------------------------------------------------------------
283 // ----------------------------------------------------------------------------
285 void wxAppBase::SetActive(bool active
, wxWindow
* WXUNUSED(lastFocus
))
287 if ( active
== m_isActive
)
292 wxActivateEvent
event(wxEVT_ACTIVATE_APP
, active
);
293 event
.SetEventObject(this);
295 (void)ProcessEvent(event
);
298 void wxAppBase::DeletePendingObjects()
300 wxList::compatibility_iterator node
= wxPendingDelete
.GetFirst();
303 wxObject
*obj
= node
->GetData();
307 if (wxPendingDelete
.Member(obj
))
308 wxPendingDelete
.Erase(node
);
310 // Deleting one object may have deleted other pending
311 // objects, so start from beginning of list again.
312 node
= wxPendingDelete
.GetFirst();
316 // Returns TRUE if more time is needed.
317 bool wxAppBase::ProcessIdle()
320 bool needMore
= FALSE
;
321 wxWindowList::compatibility_iterator node
= wxTopLevelWindows
.GetFirst();
322 node
= wxTopLevelWindows
.GetFirst();
325 wxWindow
* win
= node
->GetData();
326 if (SendIdleEvents(win
, event
))
328 node
= node
->GetNext();
331 event
.SetEventObject(this);
332 (void) ProcessEvent(event
);
333 if (event
.MoreRequested())
336 wxUpdateUIEvent::ResetUpdateTime();
341 // Send idle event to window and all subwindows
342 bool wxAppBase::SendIdleEvents(wxWindow
* win
, wxIdleEvent
& event
)
344 bool needMore
= FALSE
;
346 win
->OnInternalIdle();
348 if (wxIdleEvent::CanSend(win
))
350 event
.SetEventObject(win
);
351 win
->GetEventHandler()->ProcessEvent(event
);
353 if (event
.MoreRequested())
356 wxWindowList::compatibility_iterator node
= win
->GetChildren().GetFirst();
359 wxWindow
*child
= node
->GetData();
360 if (SendIdleEvents(child
, event
))
363 node
= node
->GetNext();
369 void wxAppBase::OnIdle(wxIdleEvent
& WXUNUSED(event
))
371 // If there are pending events, we must process them: pending events
372 // are either events to the threads other than main or events posted
373 // with wxPostEvent() functions
374 // GRG: I have moved this here so that all pending events are processed
375 // before starting to delete any objects. This behaves better (in
376 // particular, wrt wxPostEvent) and is coherent with wxGTK's current
377 // behaviour. Changed Feb/2000 before 2.1.14
378 ProcessPendingEvents();
380 // 'Garbage' collection of windows deleted with Close().
381 DeletePendingObjects();
384 // flush the logged messages if any
385 wxLog::FlushActive();
390 // ----------------------------------------------------------------------------
391 // wxGUIAppTraitsBase
392 // ----------------------------------------------------------------------------
396 wxLog
*wxGUIAppTraitsBase::CreateLogTarget()
403 wxMessageOutput
*wxGUIAppTraitsBase::CreateMessageOutput()
405 // The standard way of printing help on command line arguments (app --help)
406 // is (according to common practice):
407 // - console apps: to stderr (on any platform)
408 // - GUI apps: stderr on Unix platforms (!)
409 // message box under Windows and others
411 return new wxMessageOutputStderr
;
413 // wxMessageOutputMessageBox doesn't work under Motif
415 return new wxMessageOutputLog
;
417 return new wxMessageOutputMessageBox
;
419 #endif // __UNIX__/!__UNIX__
424 wxFontMapper
*wxGUIAppTraitsBase::CreateFontMapper()
426 return new wxFontMapper
;
429 #endif // wxUSE_FONTMAP
431 wxRendererNative
*wxGUIAppTraitsBase::CreateRenderer()
433 // use the default native renderer by default
439 bool wxGUIAppTraitsBase::ShowAssertDialog(const wxString
& msg
)
441 // under MSW we prefer to use the base class version using ::MessageBox()
442 // even if wxMessageBox() is available because it has less chances to
443 // double fault our app than our wxMessageBox()
444 #if defined(__WXMSW__) || !wxUSE_MSGDLG
445 return wxAppTraitsBase::ShowAssertDialog(msg
);
446 #else // wxUSE_MSGDLG
447 // this message is intentionally not translated -- it is for
449 wxString
msgDlg(msg
);
450 msgDlg
+= wxT("\nDo you want to stop the program?\n")
451 wxT("You can also choose [Cancel] to suppress ")
452 wxT("further warnings.");
454 switch ( wxMessageBox(msgDlg
, wxT("wxWindows Debug Alert"),
455 wxYES_NO
| wxCANCEL
| wxICON_STOP
) )
465 //case wxNO: nothing to do
469 #endif // !wxUSE_MSGDLG/wxUSE_MSGDLG
472 #endif // __WXDEBUG__
474 bool wxGUIAppTraitsBase::HasStderr()
476 // we consider that under Unix stderr always goes somewhere, even if the
477 // user doesn't always see it under GUI desktops
485 void wxGUIAppTraitsBase::ScheduleForDestroy(wxObject
*object
)
487 if ( !wxPendingDelete
.Member(object
) )
488 wxPendingDelete
.Append(object
);
491 void wxGUIAppTraitsBase::RemoveFromPendingDelete(wxObject
*object
)
493 wxPendingDelete
.DeleteObject(object
);