1 /////////////////////////////////////////////////////////////////////////////
2 // Name: common/appcmn.cpp
3 // Purpose: 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__)
36 #include "wx/msgdlg.h"
40 #include "wx/cmdline.h"
41 #include "wx/thread.h"
42 #include "wx/confbase.h"
43 #include "wx/tokenzr.h"
45 #include "wx/artprov.h"
47 #if !defined(__WXMSW__) || defined(__WXMICROWIN__)
48 #include <signal.h> // for SIGTRAP used by wxTrap()
51 #if defined(__WXMSW__)
52 #include "wx/msw/private.h" // includes windows.h for MessageBox()
55 #if defined(__WXMAC__)
56 #include "wx/mac/private.h" // includes mac headers
59 // ===========================================================================
61 // ===========================================================================
63 // ----------------------------------------------------------------------------
64 // initialization and termination
65 // ----------------------------------------------------------------------------
68 static void LINKAGEMODE
SetTraceMasks()
71 if ( wxGetEnv(wxT("WXTRACE"), &mask
) )
73 wxStringTokenizer
tkn(mask
, wxT(","));
74 while ( tkn
.HasMoreTokens() )
75 wxLog::AddTraceMask(tkn
.GetNextToken());
80 wxAppBase::wxAppBase()
82 wxTheApp
= (wxApp
*)this;
84 #if WXWIN_COMPATIBILITY_2_2
85 m_wantDebugOutput
= FALSE
;
86 #endif // WXWIN_COMPATIBILITY_2_2
89 m_topWindow
= (wxWindow
*)NULL
;
90 m_useBestVisual
= FALSE
;
91 m_exitOnFrameDelete
= TRUE
;
100 wxAppBase::~wxAppBase()
102 // this destructor is required for Darwin
106 bool wxAppBase::OnInitGui()
108 #ifdef __WXUNIVERSAL__
109 if ( !wxTheme::Get() && !wxTheme::CreateDefault() )
111 wxArtProvider
*art
= wxTheme::Get()->GetArtProvider();
113 wxArtProvider::PushProvider(art
);
114 #endif // __WXUNIVERSAL__
120 int wxAppBase::OnExit()
123 // delete the config object if any (don't use Get() here, but Set()
124 // because Get() could create a new config object)
125 delete wxConfigBase::Set((wxConfigBase
*) NULL
);
126 #endif // wxUSE_CONFIG
128 #ifdef __WXUNIVERSAL__
129 delete wxTheme::Set(NULL
);
130 #endif // __WXUNIVERSAL__
135 // ---------------------------------------------------------------------------
137 // ----------------------------------------------------------------------------
139 void wxAppBase::ProcessPendingEvents()
141 // ensure that we're the only thread to modify the pending events list
142 wxENTER_CRIT_SECT( *wxPendingEventsLocker
);
144 if ( !wxPendingEvents
)
146 wxLEAVE_CRIT_SECT( *wxPendingEventsLocker
);
150 // iterate until the list becomes empty
151 wxNode
*node
= wxPendingEvents
->First();
154 wxEvtHandler
*handler
= (wxEvtHandler
*)node
->Data();
157 // In ProcessPendingEvents(), new handlers might be add
158 // and we can safely leave the critical section here.
159 wxLEAVE_CRIT_SECT( *wxPendingEventsLocker
);
160 handler
->ProcessPendingEvents();
161 wxENTER_CRIT_SECT( *wxPendingEventsLocker
);
163 node
= wxPendingEvents
->First();
166 wxLEAVE_CRIT_SECT( *wxPendingEventsLocker
);
169 // ----------------------------------------------------------------------------
171 // ----------------------------------------------------------------------------
175 void wxAppBase::SetActive(bool active
, wxWindow
* WXUNUSED(lastFocus
))
177 if ( active
== m_isActive
)
182 wxActivateEvent
event(wxEVT_ACTIVATE_APP
, active
);
183 event
.SetEventObject(this);
185 (void)ProcessEvent(event
);
190 // ----------------------------------------------------------------------------
192 // ----------------------------------------------------------------------------
194 bool wxAppBase::OnInit()
196 #if wxUSE_CMDLINE_PARSER
197 wxCmdLineParser
parser(argc
, argv
);
199 OnInitCmdLine(parser
);
202 switch ( parser
.Parse(FALSE
/* don't show usage */) )
205 cont
= OnCmdLineHelp(parser
);
209 cont
= OnCmdLineParsed(parser
);
213 cont
= OnCmdLineError(parser
);
219 #endif // wxUSE_CMDLINE_PARSER
224 #if wxUSE_CMDLINE_PARSER
226 #define OPTION_VERBOSE _T("verbose")
227 #define OPTION_THEME _T("theme")
228 #define OPTION_MODE _T("mode")
230 void wxAppBase::OnInitCmdLine(wxCmdLineParser
& parser
)
232 // the standard command line options
233 static const wxCmdLineEntryDesc cmdLineDesc
[] =
239 gettext_noop("show this help message"),
241 wxCMD_LINE_OPTION_HELP
249 gettext_noop("generate verbose log messages")
253 #ifdef __WXUNIVERSAL__
258 gettext_noop("specify the theme to use"),
259 wxCMD_LINE_VAL_STRING
261 #endif // __WXUNIVERSAL__
263 #if defined(__WXMGL__)
264 // VS: this is not specific to wxMGL, all fullscreen (framebuffer) ports
265 // should provide this option. That's why it is in common/appcmn.cpp
266 // and not mgl/app.cpp
271 gettext_noop("specify display mode to use (e.g. 640x480-16)"),
272 wxCMD_LINE_VAL_STRING
280 parser
.SetDesc(cmdLineDesc
);
283 bool wxAppBase::OnCmdLineParsed(wxCmdLineParser
& parser
)
286 if ( parser
.Found(OPTION_VERBOSE
) )
288 wxLog::SetVerbose(TRUE
);
292 #ifdef __WXUNIVERSAL__
294 if ( parser
.Found(OPTION_THEME
, &themeName
) )
296 wxTheme
*theme
= wxTheme::Create(themeName
);
299 wxLogError(_("Unsupported theme '%s'."), themeName
.c_str());
306 #endif // __WXUNIVERSAL__
308 #if defined(__WXMGL__)
310 if ( parser
.Found(OPTION_MODE
, &modeDesc
) )
313 if ( wxSscanf(modeDesc
.c_str(), _T("%ux%u-%u"), &w
, &h
, &bpp
) != 3 )
315 wxLogError(_("Invalid display mode specification '%s'."), modeDesc
.c_str());
320 if ( !SetDisplayMode(wxDisplayModeInfo(w
, h
, bpp
)) )
328 bool wxAppBase::OnCmdLineHelp(wxCmdLineParser
& parser
)
335 bool wxAppBase::OnCmdLineError(wxCmdLineParser
& parser
)
342 #endif // wxUSE_CMDLINE_PARSER
344 // ----------------------------------------------------------------------------
346 // ----------------------------------------------------------------------------
351 bool wxAssertIsEqual(int x
, int y
)
356 // break into the debugger
359 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
361 #elif defined(__WXMAC__) && !defined(__DARWIN__)
367 #elif defined(__UNIX__)
374 // show the assert modal dialog
376 void ShowAssertDialog(const wxChar
*szFile
, int nLine
, const wxChar
*szMsg
)
378 // this variable can be set to true to suppress "assert failure" messages
379 static bool s_bNoAsserts
= FALSE
;
383 // make life easier for people using VC++ IDE: clicking on the message
384 // will take us immediately to the place of the failed assert
385 wxSnprintf(szBuf
, WXSIZEOF(szBuf
),
387 wxT("%s(%d): assert failed"),
389 // make the error message more clear for all the others
390 wxT("Assert failed in file %s at line %d"),
396 wxStrcat(szBuf
, wxT(": "));
397 wxStrcat(szBuf
, szMsg
);
399 else // no message given
401 wxStrcat(szBuf
, wxT("."));
406 // send it to the normal log destination
409 #if (wxUSE_GUI && wxUSE_MSGDLG) || defined(__WXMSW__)
410 // this message is intentionally not translated - it is for
412 wxStrcat(szBuf
, wxT("\nDo you want to stop the program?\nYou can also choose [Cancel] to suppress further warnings."));
414 // use the native message box if available: this is more robust than
416 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
417 switch ( ::MessageBox(NULL
, szBuf
, _T("Debug"),
418 MB_YESNOCANCEL
| MB_ICONSTOP
) )
428 //case IDNO: nothing to do
431 switch ( wxMessageBox(szBuf
, wxT("Debug"),
432 wxYES_NO
| wxCANCEL
| wxICON_STOP
) )
442 //case wxNO: nothing to do
452 // this function is called when an assert fails
453 void wxOnAssert(const wxChar
*szFile
, int nLine
, const wxChar
*szMsg
)
456 static bool s_bInAssert
= FALSE
;
460 // He-e-e-e-elp!! we're trapped in endless loop
472 // by default, show the assert dialog box - we can't customize this
474 ShowAssertDialog(szFile
, nLine
, szMsg
);
478 // let the app process it as it wants
479 wxTheApp
->OnAssert(szFile
, nLine
, szMsg
);
485 void wxAppBase::OnAssert(const wxChar
*file
, int line
, const wxChar
*msg
)
487 ShowAssertDialog(file
, line
, msg
);