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