]> git.saurik.com Git - wxWidgets.git/blob - src/common/appcmn.cpp
5321885ed4aac077ecbc9fd20829b01d671ae554
[wxWidgets.git] / src / common / appcmn.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/appcmn.cpp
3 // Purpose: wxAppBase methods common to all platforms
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 18.10.99
7 // Copyright: (c) Vadim Zeitlin
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 // ============================================================================
12 // declarations
13 // ============================================================================
14
15 // ---------------------------------------------------------------------------
16 // headers
17 // ---------------------------------------------------------------------------
18
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
21
22 #if defined(__BORLANDC__)
23 #pragma hdrstop
24 #endif
25
26 #ifndef WX_PRECOMP
27 #include "wx/app.h"
28 #include "wx/window.h"
29 #include "wx/bitmap.h"
30 #include "wx/log.h"
31 #include "wx/msgdlg.h"
32 #include "wx/confbase.h"
33 #include "wx/utils.h"
34 #include "wx/wxcrtvararg.h"
35 #endif
36
37 #include "wx/apptrait.h"
38 #include "wx/cmdline.h"
39 #include "wx/msgout.h"
40 #include "wx/thread.h"
41 #include "wx/vidmode.h"
42 #include "wx/evtloop.h"
43
44 #if wxUSE_FONTMAP
45 #include "wx/fontmap.h"
46 #endif // wxUSE_FONTMAP
47
48 // DLL options compatibility check:
49 #include "wx/build.h"
50 WX_CHECK_BUILD_OPTIONS("wxCore")
51
52 // ============================================================================
53 // wxAppBase implementation
54 // ============================================================================
55
56 // ----------------------------------------------------------------------------
57 // initialization
58 // ----------------------------------------------------------------------------
59
60 wxAppBase::wxAppBase()
61 {
62 m_topWindow = NULL;
63
64 m_useBestVisual = false;
65 m_forceTrueColour = false;
66
67 m_isActive = true;
68
69 // We don't want to exit the app if the user code shows a dialog from its
70 // OnInit() -- but this is what would happen if we set m_exitOnFrameDelete
71 // to Yes initially as this dialog would be the last top level window.
72 // OTOH, if we set it to No initially we'll have to overwrite it with Yes
73 // when we enter our OnRun() because we do want the default behaviour from
74 // then on. But this would be a problem if the user code calls
75 // SetExitOnFrameDelete(false) from OnInit().
76 //
77 // So we use the special "Later" value which is such that
78 // GetExitOnFrameDelete() returns false for it but which we know we can
79 // safely (i.e. without losing the effect of the users SetExitOnFrameDelete
80 // call) overwrite in OnRun()
81 m_exitOnFrameDelete = Later;
82 }
83
84 bool wxAppBase::Initialize(int& argcOrig, wxChar **argvOrig)
85 {
86 if ( !wxAppConsole::Initialize(argcOrig, argvOrig) )
87 return false;
88
89 wxInitializeStockLists();
90
91 wxBitmap::InitStandardHandlers();
92
93 // for compatibility call the old initialization function too
94 if ( !OnInitGui() )
95 return false;
96
97 return true;
98 }
99
100 // ----------------------------------------------------------------------------
101 // cleanup
102 // ----------------------------------------------------------------------------
103
104 wxAppBase::~wxAppBase()
105 {
106 // this destructor is required for Darwin
107 }
108
109 void wxAppBase::CleanUp()
110 {
111 // clean up all the pending objects
112 DeletePendingObjects();
113
114 // and any remaining TLWs (they remove themselves from wxTopLevelWindows
115 // when destroyed, so iterate until none are left)
116 while ( !wxTopLevelWindows.empty() )
117 {
118 // do not use Destroy() here as it only puts the TLW in pending list
119 // but we want to delete them now
120 delete wxTopLevelWindows.GetFirst()->GetData();
121 }
122
123 // undo everything we did in Initialize() above
124 wxBitmap::CleanUpHandlers();
125
126 wxStockGDI::DeleteAll();
127
128 wxDeleteStockLists();
129
130 wxDELETE(wxTheColourDatabase);
131
132 wxAppConsole::CleanUp();
133 }
134
135 // ----------------------------------------------------------------------------
136 // various accessors
137 // ----------------------------------------------------------------------------
138
139 wxWindow* wxAppBase::GetTopWindow() const
140 {
141 wxWindow* window = m_topWindow;
142 if (window == NULL && wxTopLevelWindows.GetCount() > 0)
143 window = wxTopLevelWindows.GetFirst()->GetData();
144 return window;
145 }
146
147 wxVideoMode wxAppBase::GetDisplayMode() const
148 {
149 return wxVideoMode();
150 }
151
152 wxLayoutDirection wxAppBase::GetLayoutDirection() const
153 {
154 #if wxUSE_INTL
155 const wxLocale *const locale = wxGetLocale();
156 if ( locale )
157 {
158 const wxLanguageInfo *const
159 info = wxLocale::GetLanguageInfo(locale->GetLanguage());
160
161 if ( info )
162 return info->LayoutDirection;
163 }
164 #endif // wxUSE_INTL
165
166 // we don't know
167 return wxLayout_Default;
168 }
169
170 #if wxUSE_CMDLINE_PARSER
171
172 // ----------------------------------------------------------------------------
173 // GUI-specific command line options handling
174 // ----------------------------------------------------------------------------
175
176 #define OPTION_THEME "theme"
177 #define OPTION_MODE "mode"
178
179 void wxAppBase::OnInitCmdLine(wxCmdLineParser& parser)
180 {
181 // first add the standard non GUI options
182 wxAppConsole::OnInitCmdLine(parser);
183
184 // the standard command line options
185 static const wxCmdLineEntryDesc cmdLineGUIDesc[] =
186 {
187 #ifdef __WXUNIVERSAL__
188 {
189 wxCMD_LINE_OPTION,
190 NULL,
191 OPTION_THEME,
192 gettext_noop("specify the theme to use"),
193 wxCMD_LINE_VAL_STRING,
194 0x0
195 },
196 #endif // __WXUNIVERSAL__
197
198 #if defined(__WXDFB__)
199 // VS: this is not specific to wxDFB, all fullscreen (framebuffer) ports
200 // should provide this option. That's why it is in common/appcmn.cpp
201 // and not dfb/app.cpp
202 {
203 wxCMD_LINE_OPTION,
204 NULL,
205 OPTION_MODE,
206 gettext_noop("specify display mode to use (e.g. 640x480-16)"),
207 wxCMD_LINE_VAL_STRING,
208 0x0
209 },
210 #endif // __WXDFB__
211
212 // terminator
213 wxCMD_LINE_DESC_END
214 };
215
216 parser.SetDesc(cmdLineGUIDesc);
217 }
218
219 bool wxAppBase::OnCmdLineParsed(wxCmdLineParser& parser)
220 {
221 #ifdef __WXUNIVERSAL__
222 wxString themeName;
223 if ( parser.Found(OPTION_THEME, &themeName) )
224 {
225 wxTheme *theme = wxTheme::Create(themeName);
226 if ( !theme )
227 {
228 wxLogError(_("Unsupported theme '%s'."), themeName.c_str());
229 return false;
230 }
231
232 // Delete the defaultly created theme and set the new theme.
233 delete wxTheme::Get();
234 wxTheme::Set(theme);
235 }
236 #endif // __WXUNIVERSAL__
237
238 #if defined(__WXDFB__)
239 wxString modeDesc;
240 if ( parser.Found(OPTION_MODE, &modeDesc) )
241 {
242 unsigned w, h, bpp;
243 if ( wxSscanf(modeDesc.c_str(), wxT("%ux%u-%u"), &w, &h, &bpp) != 3 )
244 {
245 wxLogError(_("Invalid display mode specification '%s'."), modeDesc.c_str());
246 return false;
247 }
248
249 if ( !SetDisplayMode(wxVideoMode(w, h, bpp)) )
250 return false;
251 }
252 #endif // __WXDFB__
253
254 return wxAppConsole::OnCmdLineParsed(parser);
255 }
256
257 #endif // wxUSE_CMDLINE_PARSER
258
259 // ----------------------------------------------------------------------------
260 // OnXXX() hooks
261 // ----------------------------------------------------------------------------
262
263 bool wxAppBase::OnInitGui()
264 {
265 #ifdef __WXUNIVERSAL__
266 if ( !wxTheme::Get() && !wxTheme::CreateDefault() )
267 return false;
268 #endif // __WXUNIVERSAL__
269
270 return true;
271 }
272
273 int wxAppBase::OnRun()
274 {
275 // see the comment in ctor: if the initial value hasn't been changed, use
276 // the default Yes from now on
277 if ( m_exitOnFrameDelete == Later )
278 {
279 m_exitOnFrameDelete = Yes;
280 }
281 //else: it has been changed, assume the user knows what he is doing
282
283 return wxAppConsole::OnRun();
284 }
285
286 int wxAppBase::OnExit()
287 {
288 #ifdef __WXUNIVERSAL__
289 delete wxTheme::Set(NULL);
290 #endif // __WXUNIVERSAL__
291
292 return wxAppConsole::OnExit();
293 }
294
295 wxAppTraits *wxAppBase::CreateTraits()
296 {
297 return new wxGUIAppTraits;
298 }
299
300 // ----------------------------------------------------------------------------
301 // misc
302 // ----------------------------------------------------------------------------
303
304 void wxAppBase::SetActive(bool active, wxWindow * WXUNUSED(lastFocus))
305 {
306 if ( active == m_isActive )
307 return;
308
309 m_isActive = active;
310
311 wxActivateEvent event(wxEVT_ACTIVATE_APP, active);
312 event.SetEventObject(this);
313
314 (void)ProcessEvent(event);
315 }
316
317 bool wxAppBase::SafeYield(wxWindow *win, bool onlyIfNeeded)
318 {
319 wxWindowDisabler wd(win);
320
321 wxEventLoopBase * const loop = wxEventLoopBase::GetActive();
322
323 return loop && loop->Yield(onlyIfNeeded);
324 }
325
326 bool wxAppBase::SafeYieldFor(wxWindow *win, long eventsToProcess)
327 {
328 wxWindowDisabler wd(win);
329
330 wxEventLoopBase * const loop = wxEventLoopBase::GetActive();
331
332 return loop && loop->YieldFor(eventsToProcess);
333 }
334
335
336 // ----------------------------------------------------------------------------
337 // idle handling
338 // ----------------------------------------------------------------------------
339
340 // Returns true if more time is needed.
341 bool wxAppBase::ProcessIdle()
342 {
343 // call the base class version first to send the idle event to wxTheApp
344 // itself
345 bool needMore = wxAppConsoleBase::ProcessIdle();
346 wxIdleEvent event;
347 wxWindowList::compatibility_iterator node = wxTopLevelWindows.GetFirst();
348 while (node)
349 {
350 wxWindow* win = node->GetData();
351
352 // Don't send idle events to the windows that are about to be destroyed
353 // anyhow, this is wasteful and unexpected.
354 if ( !wxPendingDelete.Member(win) && win->SendIdleEvents(event) )
355 needMore = true;
356 node = node->GetNext();
357 }
358
359 wxUpdateUIEvent::ResetUpdateTime();
360
361 return needMore;
362 }
363
364 // ----------------------------------------------------------------------------
365 // wxGUIAppTraitsBase
366 // ----------------------------------------------------------------------------
367
368 #if wxUSE_LOG
369
370 wxLog *wxGUIAppTraitsBase::CreateLogTarget()
371 {
372 #if wxUSE_LOGGUI
373 #ifndef __WXOSX_IPHONE__
374 return new wxLogGui;
375 #else
376 return new wxLogStderr;
377 #endif
378 #else
379 // we must have something!
380 return new wxLogStderr;
381 #endif
382 }
383
384 #endif // wxUSE_LOG
385
386 wxMessageOutput *wxGUIAppTraitsBase::CreateMessageOutput()
387 {
388 // The standard way of printing help on command line arguments (app --help)
389 // is (according to common practice):
390 // - console apps: to stderr (on any platform)
391 // - GUI apps: stderr on Unix platforms (!)
392 // stderr if available and message box otherwise on others
393 // (currently stderr only Windows if app running from console)
394 #ifdef __UNIX__
395 return new wxMessageOutputStderr;
396 #else // !__UNIX__
397 // wxMessageOutputMessageBox doesn't work under Motif
398 #ifdef __WXMOTIF__
399 return new wxMessageOutputLog;
400 #elif wxUSE_MSGDLG
401 return new wxMessageOutputBest(wxMSGOUT_PREFER_STDERR);
402 #else
403 return new wxMessageOutputStderr;
404 #endif
405 #endif // __UNIX__/!__UNIX__
406 }
407
408 #if wxUSE_FONTMAP
409
410 wxFontMapper *wxGUIAppTraitsBase::CreateFontMapper()
411 {
412 return new wxFontMapper;
413 }
414
415 #endif // wxUSE_FONTMAP
416
417 wxRendererNative *wxGUIAppTraitsBase::CreateRenderer()
418 {
419 // use the default native renderer by default
420 return NULL;
421 }
422
423 bool wxGUIAppTraitsBase::ShowAssertDialog(const wxString& msg)
424 {
425 #if wxDEBUG_LEVEL
426 // under MSW we prefer to use the base class version using ::MessageBox()
427 // even if wxMessageBox() is available because it has less chances to
428 // double fault our app than our wxMessageBox()
429 //
430 // under DFB the message dialog is not always functional right now
431 //
432 // and finally we can't use wxMessageBox() if it wasn't compiled in, of
433 // course
434 #if !defined(__WXMSW__) && !defined(__WXDFB__) && wxUSE_MSGDLG
435
436 // we can't (safely) show the GUI dialog from another thread, only do it
437 // for the asserts in the main thread
438 if ( wxIsMainThread() )
439 {
440 wxString msgDlg = msg;
441
442 #if wxUSE_STACKWALKER
443 const wxString stackTrace = GetAssertStackTrace();
444 if ( !stackTrace.empty() )
445 msgDlg << wxT("\n\nCall stack:\n") << stackTrace;
446 #endif // wxUSE_STACKWALKER
447
448 // this message is intentionally not translated -- it is for
449 // developpers only
450 msgDlg += wxT("\nDo you want to stop the program?\n")
451 wxT("You can also choose [Cancel] to suppress ")
452 wxT("further warnings.");
453
454 switch ( wxMessageBox(msgDlg, wxT("wxWidgets Debug Alert"),
455 wxYES_NO | wxCANCEL | wxICON_STOP ) )
456 {
457 case wxYES:
458 wxTrap();
459 break;
460
461 case wxCANCEL:
462 // no more asserts
463 return true;
464
465 //case wxNO: nothing to do
466 }
467
468 return false;
469 }
470 #endif // wxUSE_MSGDLG
471 #endif // wxDEBUG_LEVEL
472
473 return wxAppTraitsBase::ShowAssertDialog(msg);
474 }
475
476 bool wxGUIAppTraitsBase::HasStderr()
477 {
478 // we consider that under Unix stderr always goes somewhere, even if the
479 // user doesn't always see it under GUI desktops
480 #ifdef __UNIX__
481 return true;
482 #else
483 return false;
484 #endif
485 }
486