1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/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 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
23 #if defined(__BORLANDC__)
29 #include "wx/window.h"
30 #include "wx/bitmap.h"
32 #include "wx/msgdlg.h"
33 #include "wx/confbase.h"
35 #include "wx/wxcrtvararg.h"
38 #include "wx/apptrait.h"
39 #include "wx/cmdline.h"
40 #include "wx/msgout.h"
41 #include "wx/thread.h"
42 #include "wx/vidmode.h"
46 #include "wx/stackwalk.h"
47 #endif // wxUSE_STACKWALKER
50 #if defined(__WXMSW__)
51 #include "wx/msw/private.h" // includes windows.h for LOGFONT
55 #include "wx/fontmap.h"
56 #endif // wxUSE_FONTMAP
58 // DLL options compatibility check:
60 WX_CHECK_BUILD_OPTIONS("wxCore")
62 WXDLLIMPEXP_DATA_CORE(wxList
) wxPendingDelete
;
64 // ============================================================================
65 // wxAppBase implementation
66 // ============================================================================
68 // ----------------------------------------------------------------------------
70 // ----------------------------------------------------------------------------
72 wxAppBase::wxAppBase()
74 m_topWindow
= (wxWindow
*)NULL
;
76 m_useBestVisual
= false;
77 m_forceTrueColour
= false;
81 // We don't want to exit the app if the user code shows a dialog from its
82 // OnInit() -- but this is what would happen if we set m_exitOnFrameDelete
83 // to Yes initially as this dialog would be the last top level window.
84 // OTOH, if we set it to No initially we'll have to overwrite it with Yes
85 // when we enter our OnRun() because we do want the default behaviour from
86 // then on. But this would be a problem if the user code calls
87 // SetExitOnFrameDelete(false) from OnInit().
89 // So we use the special "Later" value which is such that
90 // GetExitOnFrameDelete() returns false for it but which we know we can
91 // safely (i.e. without losing the effect of the users SetExitOnFrameDelete
92 // call) overwrite in OnRun()
93 m_exitOnFrameDelete
= Later
;
96 bool wxAppBase::Initialize(int& argcOrig
, wxChar
**argvOrig
)
98 if ( !wxAppConsole::Initialize(argcOrig
, argvOrig
) )
101 wxInitializeStockLists();
103 wxBitmap::InitStandardHandlers();
108 // ----------------------------------------------------------------------------
110 // ----------------------------------------------------------------------------
112 wxAppBase::~wxAppBase()
114 // this destructor is required for Darwin
117 void wxAppBase::CleanUp()
119 // clean up all the pending objects
120 DeletePendingObjects();
122 // and any remaining TLWs (they remove themselves from wxTopLevelWindows
123 // when destroyed, so iterate until none are left)
124 while ( !wxTopLevelWindows
.empty() )
126 // do not use Destroy() here as it only puts the TLW in pending list
127 // but we want to delete them now
128 delete wxTopLevelWindows
.GetFirst()->GetData();
131 // undo everything we did in Initialize() above
132 wxBitmap::CleanUpHandlers();
134 wxStockGDI::DeleteAll();
136 wxDeleteStockLists();
138 delete wxTheColourDatabase
;
139 wxTheColourDatabase
= NULL
;
143 // If we don't do the following, we get an apparent memory leak.
144 ((wxEvtHandler
&) wxDefaultValidator
).ClearEventLocker();
145 #endif // wxUSE_VALIDATORS
146 #endif // wxUSE_THREADS
148 wxAppConsole::CleanUp();
151 // ----------------------------------------------------------------------------
153 // ----------------------------------------------------------------------------
155 wxWindow
* wxAppBase::GetTopWindow() const
157 wxWindow
* window
= m_topWindow
;
158 if (window
== NULL
&& wxTopLevelWindows
.GetCount() > 0)
159 window
= wxTopLevelWindows
.GetFirst()->GetData();
163 wxVideoMode
wxAppBase::GetDisplayMode() const
165 return wxVideoMode();
168 wxLayoutDirection
wxAppBase::GetLayoutDirection() const
171 const wxLocale
*const locale
= wxGetLocale();
174 const wxLanguageInfo
*const
175 info
= wxLocale::GetLanguageInfo(locale
->GetLanguage());
178 return info
->LayoutDirection
;
183 return wxLayout_Default
;
186 #if wxUSE_CMDLINE_PARSER
188 // ----------------------------------------------------------------------------
189 // GUI-specific command line options handling
190 // ----------------------------------------------------------------------------
192 #define OPTION_THEME "theme"
193 #define OPTION_MODE "mode"
195 void wxAppBase::OnInitCmdLine(wxCmdLineParser
& parser
)
197 // first add the standard non GUI options
198 wxAppConsole::OnInitCmdLine(parser
);
200 // the standard command line options
201 static const wxCmdLineEntryDesc cmdLineGUIDesc
[] =
203 #ifdef __WXUNIVERSAL__
208 gettext_noop("specify the theme to use"),
209 wxCMD_LINE_VAL_STRING
,
212 #endif // __WXUNIVERSAL__
214 #if defined(__WXMGL__)
215 // VS: this is not specific to wxMGL, all fullscreen (framebuffer) ports
216 // should provide this option. That's why it is in common/appcmn.cpp
217 // and not mgl/app.cpp
222 gettext_noop("specify display mode to use (e.g. 640x480-16)"),
223 wxCMD_LINE_VAL_STRING
,
232 parser
.SetDesc(cmdLineGUIDesc
);
235 bool wxAppBase::OnCmdLineParsed(wxCmdLineParser
& parser
)
237 #ifdef __WXUNIVERSAL__
239 if ( parser
.Found(OPTION_THEME
, &themeName
) )
241 wxTheme
*theme
= wxTheme::Create(themeName
);
244 wxLogError(_("Unsupported theme '%s'."), themeName
.c_str());
248 // Delete the defaultly created theme and set the new theme.
249 delete wxTheme::Get();
252 #endif // __WXUNIVERSAL__
254 #if defined(__WXMGL__)
256 if ( parser
.Found(OPTION_MODE
, &modeDesc
) )
259 if ( wxSscanf(modeDesc
.c_str(), _T("%ux%u-%u"), &w
, &h
, &bpp
) != 3 )
261 wxLogError(_("Invalid display mode specification '%s'."), modeDesc
.c_str());
265 if ( !SetDisplayMode(wxVideoMode(w
, h
, bpp
)) )
270 return wxAppConsole::OnCmdLineParsed(parser
);
273 #endif // wxUSE_CMDLINE_PARSER
275 // ----------------------------------------------------------------------------
277 // ----------------------------------------------------------------------------
279 bool wxAppBase::OnInitGui()
281 #ifdef __WXUNIVERSAL__
282 if ( !wxTheme::Get() && !wxTheme::CreateDefault() )
284 #endif // __WXUNIVERSAL__
289 int wxAppBase::OnRun()
291 // see the comment in ctor: if the initial value hasn't been changed, use
292 // the default Yes from now on
293 if ( m_exitOnFrameDelete
== Later
)
295 m_exitOnFrameDelete
= Yes
;
297 //else: it has been changed, assume the user knows what he is doing
299 return wxAppConsole::OnRun();
302 int wxAppBase::OnExit()
304 #ifdef __WXUNIVERSAL__
305 delete wxTheme::Set(NULL
);
306 #endif // __WXUNIVERSAL__
308 return wxAppConsole::OnExit();
311 wxAppTraits
*wxAppBase::CreateTraits()
313 return new wxGUIAppTraits
;
316 // ----------------------------------------------------------------------------
318 // ----------------------------------------------------------------------------
320 void wxAppBase::SetActive(bool active
, wxWindow
* WXUNUSED(lastFocus
))
322 if ( active
== m_isActive
)
327 wxActivateEvent
event(wxEVT_ACTIVATE_APP
, active
);
328 event
.SetEventObject(this);
330 (void)ProcessEvent(event
);
333 // ----------------------------------------------------------------------------
335 // ----------------------------------------------------------------------------
337 void wxAppBase::DeletePendingObjects()
339 wxList::compatibility_iterator node
= wxPendingDelete
.GetFirst();
342 wxObject
*obj
= node
->GetData();
344 // remove it from the list first so that if we get back here somehow
345 // during the object deletion (e.g. wxYield called from its dtor) we
346 // wouldn't try to delete it the second time
347 if ( wxPendingDelete
.Member(obj
) )
348 wxPendingDelete
.Erase(node
);
352 // Deleting one object may have deleted other pending
353 // objects, so start from beginning of list again.
354 node
= wxPendingDelete
.GetFirst();
358 // Returns true if more time is needed.
359 bool wxAppBase::ProcessIdle()
361 // process pending wx events before sending idle events
362 ProcessPendingEvents();
365 bool needMore
= false;
366 wxWindowList::compatibility_iterator node
= wxTopLevelWindows
.GetFirst();
369 wxWindow
* win
= node
->GetData();
370 if (SendIdleEvents(win
, event
))
372 node
= node
->GetNext();
375 if (wxAppConsole::ProcessIdle())
378 // 'Garbage' collection of windows deleted with Close().
379 DeletePendingObjects();
382 // flush the logged messages if any
383 wxLog::FlushActive();
386 wxUpdateUIEvent::ResetUpdateTime();
391 // Send idle event to window and all subwindows
392 bool wxAppBase::SendIdleEvents(wxWindow
* win
, wxIdleEvent
& event
)
394 bool needMore
= false;
396 win
->OnInternalIdle();
398 // should we send idle event to this window?
399 if ( wxIdleEvent::GetMode() == wxIDLE_PROCESS_ALL
||
400 win
->HasExtraStyle(wxWS_EX_PROCESS_IDLE
) )
402 event
.SetEventObject(win
);
403 win
->GetEventHandler()->ProcessEvent(event
);
405 if (event
.MoreRequested())
408 wxWindowList::compatibility_iterator node
= win
->GetChildren().GetFirst();
411 wxWindow
*child
= node
->GetData();
412 if (SendIdleEvents(child
, event
))
415 node
= node
->GetNext();
421 // ----------------------------------------------------------------------------
422 // wxGUIAppTraitsBase
423 // ----------------------------------------------------------------------------
427 wxLog
*wxGUIAppTraitsBase::CreateLogTarget()
429 // DE: One day I'll remove this but right now the generic dialog used for this
430 // just doesn't work right at all on wxCocoa.
431 #if wxUSE_LOGGUI && !defined(__WXCOCOA__)
434 // we must have something!
435 return new wxLogStderr
;
441 wxMessageOutput
*wxGUIAppTraitsBase::CreateMessageOutput()
443 // The standard way of printing help on command line arguments (app --help)
444 // is (according to common practice):
445 // - console apps: to stderr (on any platform)
446 // - GUI apps: stderr on Unix platforms (!)
447 // message box under Windows and others
449 return new wxMessageOutputStderr
;
451 // wxMessageOutputMessageBox doesn't work under Motif
453 return new wxMessageOutputLog
;
455 return new wxMessageOutputMessageBox
;
457 return new wxMessageOutputStderr
;
459 #endif // __UNIX__/!__UNIX__
464 wxFontMapper
*wxGUIAppTraitsBase::CreateFontMapper()
466 return new wxFontMapper
;
469 #endif // wxUSE_FONTMAP
471 wxRendererNative
*wxGUIAppTraitsBase::CreateRenderer()
473 // use the default native renderer by default
479 bool wxGUIAppTraitsBase::ShowAssertDialog(const wxString
& msg
)
481 #if defined(__WXMSW__) || !wxUSE_MSGDLG
482 // under MSW we prefer to use the base class version using ::MessageBox()
483 // even if wxMessageBox() is available because it has less chances to
484 // double fault our app than our wxMessageBox()
485 return wxAppTraitsBase::ShowAssertDialog(msg
);
486 #else // wxUSE_MSGDLG
487 wxString msgDlg
= msg
;
489 #if wxUSE_STACKWALKER
490 // on Unix stack frame generation may take some time, depending on the
491 // size of the executable mainly... warn the user that we are working
492 wxFprintf(stderr
, wxT("[Debug] Generating a stack trace... please wait"));
495 const wxString stackTrace
= GetAssertStackTrace();
496 if ( !stackTrace
.empty() )
497 msgDlg
<< _T("\n\nCall stack:\n") << stackTrace
;
498 #endif // wxUSE_STACKWALKER
500 // this message is intentionally not translated -- it is for
502 msgDlg
+= wxT("\nDo you want to stop the program?\n")
503 wxT("You can also choose [Cancel] to suppress ")
504 wxT("further warnings.");
506 switch ( wxMessageBox(msgDlg
, wxT("wxWidgets Debug Alert"),
507 wxYES_NO
| wxCANCEL
| wxICON_STOP
) )
517 //case wxNO: nothing to do
521 #endif // !wxUSE_MSGDLG/wxUSE_MSGDLG
524 #endif // __WXDEBUG__
526 bool wxGUIAppTraitsBase::HasStderr()
528 // we consider that under Unix stderr always goes somewhere, even if the
529 // user doesn't always see it under GUI desktops
537 void wxGUIAppTraitsBase::ScheduleForDestroy(wxObject
*object
)
539 if ( !wxPendingDelete
.Member(object
) )
540 wxPendingDelete
.Append(object
);
543 void wxGUIAppTraitsBase::RemoveFromPendingDelete(wxObject
*object
)
545 wxPendingDelete
.DeleteObject(object
);
550 #if defined(__WINDOWS__)
551 #include "wx/msw/gsockmsw.h"
552 #elif defined(__UNIX__) || defined(__DARWIN__) || defined(__OS2__)
553 #include "wx/unix/gsockunx.h"
554 #elif defined(__WXMAC__)
555 #include <MacHeaders.c>
556 #define OTUNIXERRORS 1
557 #include <OpenTransport.h>
558 #include <OpenTransportProviders.h>
559 #include <OpenTptInternet.h>
561 #include "wx/mac/gsockmac.h"
563 #error "Must include correct GSocket header here"
566 GSocketGUIFunctionsTable
* wxGUIAppTraitsBase::GetSocketGUIFunctionsTable()
568 #if defined(__WXMAC__) && !defined(__DARWIN__)
569 // NB: wxMac CFM does not have any GUI-specific functions in gsocket.c and
570 // so it doesn't need this table at all
572 #else // !__WXMAC__ || __DARWIN__
573 static GSocketGUIFunctionsTableConcrete table
;
575 #endif // !__WXMAC__ || __DARWIN__