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 // the standard command line options
151 static const wxCmdLineEntryDesc cmdLineGUIDesc
[] =
153 #ifdef __WXUNIVERSAL__
158 gettext_noop("specify the theme to use"),
159 wxCMD_LINE_VAL_STRING
,
162 #endif // __WXUNIVERSAL__
164 #if defined(__WXMGL__)
165 // VS: this is not specific to wxMGL, all fullscreen (framebuffer) ports
166 // should provide this option. That's why it is in common/appcmn.cpp
167 // and not mgl/app.cpp
172 gettext_noop("specify display mode to use (e.g. 640x480-16)"),
173 wxCMD_LINE_VAL_STRING
,
189 parser
.SetDesc(cmdLineGUIDesc
);
192 bool wxAppBase::OnCmdLineParsed(wxCmdLineParser
& parser
)
194 #ifdef __WXUNIVERSAL__
196 if ( parser
.Found(OPTION_THEME
, &themeName
) )
198 wxTheme
*theme
= wxTheme::Create(themeName
);
201 wxLogError(_("Unsupported theme '%s'."), themeName
.c_str());
205 // Delete the defaultly created theme and set the new theme.
206 delete wxTheme::Get();
209 #endif // __WXUNIVERSAL__
211 #if defined(__WXMGL__)
213 if ( parser
.Found(OPTION_MODE
, &modeDesc
) )
216 if ( wxSscanf(modeDesc
.c_str(), _T("%ux%u-%u"), &w
, &h
, &bpp
) != 3 )
218 wxLogError(_("Invalid display mode specification '%s'."), modeDesc
.c_str());
222 if ( !SetDisplayMode(wxDisplayModeInfo(w
, h
, bpp
)) )
227 return wxAppConsole::OnCmdLineParsed(parser
);
230 #endif // wxUSE_CMDLINE_PARSER
232 // ----------------------------------------------------------------------------
234 // ----------------------------------------------------------------------------
236 bool wxAppBase::OnInitGui()
238 #ifdef __WXUNIVERSAL__
239 if ( !wxTheme::Get() && !wxTheme::CreateDefault() )
241 #endif // __WXUNIVERSAL__
246 int wxAppBase::OnRun()
248 // see the comment in ctor: if the initial value hasn't been changed, use
249 // the default Yes from now on
250 if ( m_exitOnFrameDelete
== Later
)
252 m_exitOnFrameDelete
= Yes
;
254 //else: it has been changed, assume the user knows what he is doing
259 int wxAppBase::OnExit()
261 #ifdef __WXUNIVERSAL__
262 delete wxTheme::Set(NULL
);
263 #endif // __WXUNIVERSAL__
265 return wxAppConsole::OnExit();
268 void wxAppBase::Exit()
273 wxAppTraits
*wxAppBase::CreateTraits()
275 return new wxGUIAppTraits
;
278 // ----------------------------------------------------------------------------
280 // ----------------------------------------------------------------------------
282 void wxAppBase::SetActive(bool active
, wxWindow
* WXUNUSED(lastFocus
))
284 if ( active
== m_isActive
)
289 wxActivateEvent
event(wxEVT_ACTIVATE_APP
, active
);
290 event
.SetEventObject(this);
292 (void)ProcessEvent(event
);
295 void wxAppBase::DeletePendingObjects()
297 wxList::compatibility_iterator node
= wxPendingDelete
.GetFirst();
300 wxObject
*obj
= node
->GetData();
304 if (wxPendingDelete
.Member(obj
))
305 wxPendingDelete
.Erase(node
);
307 // Deleting one object may have deleted other pending
308 // objects, so start from beginning of list again.
309 node
= wxPendingDelete
.GetFirst();
313 // Returns TRUE if more time is needed.
314 bool wxAppBase::ProcessIdle()
317 bool needMore
= FALSE
;
318 wxWindowList::compatibility_iterator node
= wxTopLevelWindows
.GetFirst();
319 node
= wxTopLevelWindows
.GetFirst();
322 wxWindow
* win
= node
->GetData();
323 if (SendIdleEvents(win
, event
))
325 node
= node
->GetNext();
328 event
.SetEventObject(this);
329 (void) ProcessEvent(event
);
330 if (event
.MoreRequested())
333 wxUpdateUIEvent::ResetUpdateTime();
338 // Send idle event to window and all subwindows
339 bool wxAppBase::SendIdleEvents(wxWindow
* win
, wxIdleEvent
& event
)
341 bool needMore
= FALSE
;
343 win
->OnInternalIdle();
345 if (wxIdleEvent::CanSend(win
))
347 event
.SetEventObject(win
);
348 win
->GetEventHandler()->ProcessEvent(event
);
350 if (event
.MoreRequested())
353 wxWindowList::compatibility_iterator node
= win
->GetChildren().GetFirst();
356 wxWindow
*child
= node
->GetData();
357 if (SendIdleEvents(child
, event
))
360 node
= node
->GetNext();
366 void wxAppBase::OnIdle(wxIdleEvent
& WXUNUSED(event
))
368 // If there are pending events, we must process them: pending events
369 // are either events to the threads other than main or events posted
370 // with wxPostEvent() functions
371 // GRG: I have moved this here so that all pending events are processed
372 // before starting to delete any objects. This behaves better (in
373 // particular, wrt wxPostEvent) and is coherent with wxGTK's current
374 // behaviour. Changed Feb/2000 before 2.1.14
375 ProcessPendingEvents();
377 // 'Garbage' collection of windows deleted with Close().
378 DeletePendingObjects();
381 // flush the logged messages if any
382 wxLog::FlushActive();
387 // ----------------------------------------------------------------------------
388 // wxGUIAppTraitsBase
389 // ----------------------------------------------------------------------------
393 wxLog
*wxGUIAppTraitsBase::CreateLogTarget()
400 wxMessageOutput
*wxGUIAppTraitsBase::CreateMessageOutput()
402 // The standard way of printing help on command line arguments (app --help)
403 // is (according to common practice):
404 // - console apps: to stderr (on any platform)
405 // - GUI apps: stderr on Unix platforms (!)
406 // message box under Windows and others
408 return new wxMessageOutputStderr
;
410 // wxMessageOutputMessageBox doesn't work under Motif
412 return new wxMessageOutputLog
;
414 return new wxMessageOutputMessageBox
;
416 #endif // __UNIX__/!__UNIX__
421 wxFontMapper
*wxGUIAppTraitsBase::CreateFontMapper()
423 return new wxFontMapper
;
426 #endif // wxUSE_FONTMAP
428 wxRendererNative
*wxGUIAppTraitsBase::CreateRenderer()
430 // use the default native renderer by default
436 bool wxGUIAppTraitsBase::ShowAssertDialog(const wxString
& msg
)
438 // under MSW we prefer to use the base class version using ::MessageBox()
439 // even if wxMessageBox() is available because it has less chances to
440 // double fault our app than our wxMessageBox()
441 #if defined(__WXMSW__) || !wxUSE_MSGDLG
442 return wxAppTraitsBase::ShowAssertDialog(msg
);
443 #else // wxUSE_MSGDLG
444 // this message is intentionally not translated -- it is for
446 wxString
msgDlg(msg
);
447 msgDlg
+= wxT("\nDo you want to stop the program?\n")
448 wxT("You can also choose [Cancel] to suppress ")
449 wxT("further warnings.");
451 switch ( wxMessageBox(msgDlg
, wxT("wxWindows Debug Alert"),
452 wxYES_NO
| wxCANCEL
| wxICON_STOP
) )
462 //case wxNO: nothing to do
466 #endif // !wxUSE_MSGDLG/wxUSE_MSGDLG
469 #endif // __WXDEBUG__
471 bool wxGUIAppTraitsBase::HasStderr()
473 // we consider that under Unix stderr always goes somewhere, even if the
474 // user doesn't always see it under GUI desktops
482 void wxGUIAppTraitsBase::ScheduleForDestroy(wxObject
*object
)
484 if ( !wxPendingDelete
.Member(object
) )
485 wxPendingDelete
.Append(object
);
488 void wxGUIAppTraitsBase::RemoveFromPendingDelete(wxObject
*object
)
490 wxPendingDelete
.DeleteObject(object
);