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/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();
328 wxWindow
* win
= node
->GetData();
329 if (SendIdleEvents(win
, event
))
331 node
= node
->GetNext();
334 event
.SetEventObject(this);
335 (void) ProcessEvent(event
);
336 if (event
.MoreRequested())
339 wxUpdateUIEvent::ResetUpdateTime();
344 // Send idle event to window and all subwindows
345 bool wxAppBase::SendIdleEvents(wxWindow
* win
, wxIdleEvent
& event
)
347 bool needMore
= FALSE
;
349 win
->OnInternalIdle();
351 if (wxIdleEvent::CanSend(win
))
353 event
.SetEventObject(win
);
354 win
->GetEventHandler()->ProcessEvent(event
);
356 if (event
.MoreRequested())
359 wxWindowList::compatibility_iterator node
= win
->GetChildren().GetFirst();
362 wxWindow
*child
= node
->GetData();
363 if (SendIdleEvents(child
, event
))
366 node
= node
->GetNext();
372 void wxAppBase::OnIdle(wxIdleEvent
& WXUNUSED(event
))
374 // If there are pending events, we must process them: pending events
375 // are either events to the threads other than main or events posted
376 // with wxPostEvent() functions
377 // GRG: I have moved this here so that all pending events are processed
378 // before starting to delete any objects. This behaves better (in
379 // particular, wrt wxPostEvent) and is coherent with wxGTK's current
380 // behaviour. Changed Feb/2000 before 2.1.14
381 ProcessPendingEvents();
383 // 'Garbage' collection of windows deleted with Close().
384 DeletePendingObjects();
387 // flush the logged messages if any
388 wxLog::FlushActive();
393 // ----------------------------------------------------------------------------
394 // wxGUIAppTraitsBase
395 // ----------------------------------------------------------------------------
399 wxLog
*wxGUIAppTraitsBase::CreateLogTarget()
406 wxMessageOutput
*wxGUIAppTraitsBase::CreateMessageOutput()
408 // The standard way of printing help on command line arguments (app --help)
409 // is (according to common practice):
410 // - console apps: to stderr (on any platform)
411 // - GUI apps: stderr on Unix platforms (!)
412 // message box under Windows and others
414 return new wxMessageOutputStderr
;
416 // wxMessageOutputMessageBox doesn't work under Motif
418 return new wxMessageOutputLog
;
420 return new wxMessageOutputMessageBox
;
422 #endif // __UNIX__/!__UNIX__
427 wxFontMapper
*wxGUIAppTraitsBase::CreateFontMapper()
429 return new wxFontMapper
;
432 #endif // wxUSE_FONTMAP
434 wxRendererNative
*wxGUIAppTraitsBase::CreateRenderer()
436 // use the default native renderer by default
442 bool wxGUIAppTraitsBase::ShowAssertDialog(const wxString
& msg
)
444 // under MSW we prefer to use the base class version using ::MessageBox()
445 // even if wxMessageBox() is available because it has less chances to
446 // double fault our app than our wxMessageBox()
447 #if defined(__WXMSW__) || !wxUSE_MSGDLG
448 return wxAppTraitsBase::ShowAssertDialog(msg
);
449 #else // wxUSE_MSGDLG
450 // this message is intentionally not translated -- it is for
452 wxString
msgDlg(msg
);
453 msgDlg
+= wxT("\nDo you want to stop the program?\n")
454 wxT("You can also choose [Cancel] to suppress ")
455 wxT("further warnings.");
457 switch ( wxMessageBox(msgDlg
, wxT("wxWindows Debug Alert"),
458 wxYES_NO
| wxCANCEL
| wxICON_STOP
) )
468 //case wxNO: nothing to do
472 #endif // !wxUSE_MSGDLG/wxUSE_MSGDLG
475 #endif // __WXDEBUG__
477 bool wxGUIAppTraitsBase::HasStderr()
479 // we consider that under Unix stderr always goes somewhere, even if the
480 // user doesn't always see it under GUI desktops
488 void wxGUIAppTraitsBase::ScheduleForDestroy(wxObject
*object
)
490 if ( !wxPendingDelete
.Member(object
) )
491 wxPendingDelete
.Append(object
);
494 void wxGUIAppTraitsBase::RemoveFromPendingDelete(wxObject
*object
)
496 wxPendingDelete
.DeleteObject(object
);
501 #if defined(__UNIX__) || defined(__DARWIN__) || defined(__OS2__)
502 #include "wx/unix/gsockunx.h"
503 #elif defined(__WINDOWS__)
504 #include "wx/msw/gsockmsw.h"
505 #elif defined(__WXMAC__)
506 #include <MacHeaders.c>
507 #define OTUNIXERRORS 1
508 #include <OpenTransport.h>
509 #include <OpenTransportProviders.h>
510 #include <OpenTptInternet.h>
512 #include "wx/mac/gsockmac.h"
514 #error "Must include correct GSocket header here"
517 GSocketGUIFunctionsTable
* wxGUIAppTraitsBase::GetSocketGUIFunctionsTable()
520 // NB: wxMac does not have any GUI-specific functions in gsocket.c and
521 // so it doesn't need this table at all
524 static GSocketGUIFunctionsTable table
=
527 _GSocket_GUI_Cleanup
,
528 _GSocket_GUI_Init_Socket
,
529 _GSocket_GUI_Destroy_Socket
,
531 _GSocket_Install_Callback
,
532 _GSocket_Uninstall_Callback
,
534 _GSocket_Enable_Events
,
535 _GSocket_Disable_Events
538 #endif // __WXMAC__/!__WXMAC__