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
;
141 delete wxPendingEvents
;
142 wxPendingEvents
= NULL
;
146 // If we don't do the following, we get an apparent memory leak.
147 ((wxEvtHandler
&) wxDefaultValidator
).ClearEventLocker();
148 #endif // wxUSE_VALIDATORS
149 #endif // wxUSE_THREADS
152 // ----------------------------------------------------------------------------
154 // ----------------------------------------------------------------------------
156 wxWindow
* wxAppBase::GetTopWindow() const
158 wxWindow
* window
= m_topWindow
;
159 if (window
== NULL
&& wxTopLevelWindows
.GetCount() > 0)
160 window
= wxTopLevelWindows
.GetFirst()->GetData();
164 wxVideoMode
wxAppBase::GetDisplayMode() const
166 return wxVideoMode();
169 wxLayoutDirection
wxAppBase::GetLayoutDirection() const
172 const wxLocale
*const locale
= wxGetLocale();
175 const wxLanguageInfo
*const
176 info
= wxLocale::GetLanguageInfo(locale
->GetLanguage());
179 return info
->LayoutDirection
;
184 return wxLayout_Default
;
187 #if wxUSE_CMDLINE_PARSER
189 // ----------------------------------------------------------------------------
190 // GUI-specific command line options handling
191 // ----------------------------------------------------------------------------
193 #define OPTION_THEME _T("theme")
194 #define OPTION_MODE _T("mode")
196 void wxAppBase::OnInitCmdLine(wxCmdLineParser
& parser
)
198 // first add the standard non GUI options
199 wxAppConsole::OnInitCmdLine(parser
);
201 // the standard command line options
202 static const wxCmdLineEntryDesc cmdLineGUIDesc
[] =
204 #ifdef __WXUNIVERSAL__
209 gettext_noop("specify the theme to use"),
210 wxCMD_LINE_VAL_STRING
,
213 #endif // __WXUNIVERSAL__
215 #if defined(__WXMGL__)
216 // VS: this is not specific to wxMGL, all fullscreen (framebuffer) ports
217 // should provide this option. That's why it is in common/appcmn.cpp
218 // and not mgl/app.cpp
223 gettext_noop("specify display mode to use (e.g. 640x480-16)"),
224 wxCMD_LINE_VAL_STRING
,
240 parser
.SetDesc(cmdLineGUIDesc
);
243 bool wxAppBase::OnCmdLineParsed(wxCmdLineParser
& parser
)
245 #ifdef __WXUNIVERSAL__
247 if ( parser
.Found(OPTION_THEME
, &themeName
) )
249 wxTheme
*theme
= wxTheme::Create(themeName
);
252 wxLogError(_("Unsupported theme '%s'."), themeName
.c_str());
256 // Delete the defaultly created theme and set the new theme.
257 delete wxTheme::Get();
260 #endif // __WXUNIVERSAL__
262 #if defined(__WXMGL__)
264 if ( parser
.Found(OPTION_MODE
, &modeDesc
) )
267 if ( wxSscanf(modeDesc
.c_str(), _T("%ux%u-%u"), &w
, &h
, &bpp
) != 3 )
269 wxLogError(_("Invalid display mode specification '%s'."), modeDesc
.c_str());
273 if ( !SetDisplayMode(wxVideoMode(w
, h
, bpp
)) )
278 return wxAppConsole::OnCmdLineParsed(parser
);
281 #endif // wxUSE_CMDLINE_PARSER
283 // ----------------------------------------------------------------------------
285 // ----------------------------------------------------------------------------
287 bool wxAppBase::OnInitGui()
289 #ifdef __WXUNIVERSAL__
290 if ( !wxTheme::Get() && !wxTheme::CreateDefault() )
292 #endif // __WXUNIVERSAL__
297 int wxAppBase::OnRun()
299 // see the comment in ctor: if the initial value hasn't been changed, use
300 // the default Yes from now on
301 if ( m_exitOnFrameDelete
== Later
)
303 m_exitOnFrameDelete
= Yes
;
305 //else: it has been changed, assume the user knows what he is doing
307 return wxAppConsole::OnRun();
310 int wxAppBase::OnExit()
312 #ifdef __WXUNIVERSAL__
313 delete wxTheme::Set(NULL
);
314 #endif // __WXUNIVERSAL__
316 return wxAppConsole::OnExit();
319 wxAppTraits
*wxAppBase::CreateTraits()
321 return new wxGUIAppTraits
;
324 // ----------------------------------------------------------------------------
326 // ----------------------------------------------------------------------------
328 void wxAppBase::SetActive(bool active
, wxWindow
* WXUNUSED(lastFocus
))
330 if ( active
== m_isActive
)
335 wxActivateEvent
event(wxEVT_ACTIVATE_APP
, active
);
336 event
.SetEventObject(this);
338 (void)ProcessEvent(event
);
341 // ----------------------------------------------------------------------------
343 // ----------------------------------------------------------------------------
345 void wxAppBase::DeletePendingObjects()
347 wxList::compatibility_iterator node
= wxPendingDelete
.GetFirst();
350 wxObject
*obj
= node
->GetData();
352 // remove it from the list first so that if we get back here somehow
353 // during the object deletion (e.g. wxYield called from its dtor) we
354 // wouldn't try to delete it the second time
355 if ( wxPendingDelete
.Member(obj
) )
356 wxPendingDelete
.Erase(node
);
360 // Deleting one object may have deleted other pending
361 // objects, so start from beginning of list again.
362 node
= wxPendingDelete
.GetFirst();
366 // Returns true if more time is needed.
367 bool wxAppBase::ProcessIdle()
370 bool needMore
= false;
371 wxWindowList::compatibility_iterator node
= wxTopLevelWindows
.GetFirst();
374 wxWindow
* win
= node
->GetData();
375 if (SendIdleEvents(win
, event
))
377 node
= node
->GetNext();
380 needMore
= wxAppConsole::ProcessIdle();
382 wxUpdateUIEvent::ResetUpdateTime();
387 // Send idle event to window and all subwindows
388 bool wxAppBase::SendIdleEvents(wxWindow
* win
, wxIdleEvent
& event
)
390 bool needMore
= false;
392 win
->OnInternalIdle();
394 // should we send idle event to this window?
395 if ( wxIdleEvent::GetMode() == wxIDLE_PROCESS_ALL
||
396 win
->HasExtraStyle(wxWS_EX_PROCESS_IDLE
) )
398 event
.SetEventObject(win
);
399 win
->GetEventHandler()->ProcessEvent(event
);
401 if (event
.MoreRequested())
404 wxWindowList::compatibility_iterator node
= win
->GetChildren().GetFirst();
407 wxWindow
*child
= node
->GetData();
408 if (SendIdleEvents(child
, event
))
411 node
= node
->GetNext();
417 void wxAppBase::OnIdle(wxIdleEvent
& WXUNUSED(event
))
419 // If there are pending events, we must process them: pending events
420 // are either events to the threads other than main or events posted
421 // with wxPostEvent() functions
422 // GRG: I have moved this here so that all pending events are processed
423 // before starting to delete any objects. This behaves better (in
424 // particular, wrt wxPostEvent) and is coherent with wxGTK's current
425 // behaviour. Changed Feb/2000 before 2.1.14
426 ProcessPendingEvents();
428 // 'Garbage' collection of windows deleted with Close().
429 DeletePendingObjects();
432 // flush the logged messages if any
433 wxLog::FlushActive();
438 // ----------------------------------------------------------------------------
439 // wxGUIAppTraitsBase
440 // ----------------------------------------------------------------------------
444 wxLog
*wxGUIAppTraitsBase::CreateLogTarget()
449 // we must have something!
450 return new wxLogStderr
;
456 wxMessageOutput
*wxGUIAppTraitsBase::CreateMessageOutput()
458 // The standard way of printing help on command line arguments (app --help)
459 // is (according to common practice):
460 // - console apps: to stderr (on any platform)
461 // - GUI apps: stderr on Unix platforms (!)
462 // message box under Windows and others
464 return new wxMessageOutputStderr
;
466 // wxMessageOutputMessageBox doesn't work under Motif
468 return new wxMessageOutputLog
;
470 return new wxMessageOutputMessageBox
;
472 #endif // __UNIX__/!__UNIX__
477 wxFontMapper
*wxGUIAppTraitsBase::CreateFontMapper()
479 return new wxFontMapper
;
482 #endif // wxUSE_FONTMAP
484 wxRendererNative
*wxGUIAppTraitsBase::CreateRenderer()
486 // use the default native renderer by default
492 bool wxGUIAppTraitsBase::ShowAssertDialog(const wxString
& msg
)
494 #if defined(__WXMSW__) || !wxUSE_MSGDLG
495 // under MSW we prefer to use the base class version using ::MessageBox()
496 // even if wxMessageBox() is available because it has less chances to
497 // double fault our app than our wxMessageBox()
498 return wxAppTraitsBase::ShowAssertDialog(msg
);
499 #else // wxUSE_MSGDLG
500 wxString msgDlg
= msg
;
502 #if wxUSE_STACKWALKER
503 // on Unix stack frame generation may take some time, depending on the
504 // size of the executable mainly... warn the user that we are working
505 wxFprintf(stderr
, wxT("[Debug] Generating a stack trace... please wait"));
508 const wxString stackTrace
= GetAssertStackTrace();
509 if ( !stackTrace
.empty() )
510 msgDlg
<< _T("\n\nCall stack:\n") << stackTrace
;
511 #endif // wxUSE_STACKWALKER
513 // this message is intentionally not translated -- it is for
515 msgDlg
+= wxT("\nDo you want to stop the program?\n")
516 wxT("You can also choose [Cancel] to suppress ")
517 wxT("further warnings.");
519 switch ( wxMessageBox(msgDlg
, wxT("wxWidgets Debug Alert"),
520 wxYES_NO
| wxCANCEL
| wxICON_STOP
) )
530 //case wxNO: nothing to do
534 #endif // !wxUSE_MSGDLG/wxUSE_MSGDLG
537 #endif // __WXDEBUG__
539 bool wxGUIAppTraitsBase::HasStderr()
541 // we consider that under Unix stderr always goes somewhere, even if the
542 // user doesn't always see it under GUI desktops
550 void wxGUIAppTraitsBase::ScheduleForDestroy(wxObject
*object
)
552 if ( !wxPendingDelete
.Member(object
) )
553 wxPendingDelete
.Append(object
);
556 void wxGUIAppTraitsBase::RemoveFromPendingDelete(wxObject
*object
)
558 wxPendingDelete
.DeleteObject(object
);
563 #if defined(__WINDOWS__)
564 #include "wx/msw/gsockmsw.h"
565 #elif defined(__UNIX__) || defined(__DARWIN__) || defined(__OS2__)
566 #include "wx/unix/gsockunx.h"
567 #elif defined(__WXMAC__)
568 #include <MacHeaders.c>
569 #define OTUNIXERRORS 1
570 #include <OpenTransport.h>
571 #include <OpenTransportProviders.h>
572 #include <OpenTptInternet.h>
574 #include "wx/mac/gsockmac.h"
576 #error "Must include correct GSocket header here"
579 GSocketGUIFunctionsTable
* wxGUIAppTraitsBase::GetSocketGUIFunctionsTable()
581 #if defined(__WXMAC__) && !defined(__DARWIN__)
582 // NB: wxMac CFM does not have any GUI-specific functions in gsocket.c and
583 // so it doesn't need this table at all
585 #else // !__WXMAC__ || __DARWIN__
586 static GSocketGUIFunctionsTableConcrete table
;
588 #endif // !__WXMAC__ || __DARWIN__