]> git.saurik.com Git - wxWidgets.git/blob - src/common/appcmn.cpp
on HP-UX 11.11, vsscanf first parameter is not const
[wxWidgets.git] / src / common / appcmn.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/appcmn.cpp
3 // Purpose: wxAppConsole and 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
44 #ifdef __WXDEBUG__
45 #if wxUSE_STACKWALKER
46 #include "wx/stackwalk.h"
47 #endif // wxUSE_STACKWALKER
48 #endif // __WXDEBUG__
49
50 #if defined(__WXMSW__)
51 #include "wx/msw/private.h" // includes windows.h for LOGFONT
52 #endif
53
54 #if wxUSE_FONTMAP
55 #include "wx/fontmap.h"
56 #endif // wxUSE_FONTMAP
57
58 // DLL options compatibility check:
59 #include "wx/build.h"
60 WX_CHECK_BUILD_OPTIONS("wxCore")
61
62 WXDLLIMPEXP_DATA_CORE(wxList) wxPendingDelete;
63
64 // ============================================================================
65 // wxAppBase implementation
66 // ============================================================================
67
68 // ----------------------------------------------------------------------------
69 // initialization
70 // ----------------------------------------------------------------------------
71
72 wxAppBase::wxAppBase()
73 {
74 m_topWindow = (wxWindow *)NULL;
75
76 m_useBestVisual = false;
77 m_forceTrueColour = false;
78
79 m_isActive = true;
80
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().
88 //
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;
94 }
95
96 bool wxAppBase::Initialize(int& argcOrig, wxChar **argvOrig)
97 {
98 if ( !wxAppConsole::Initialize(argcOrig, argvOrig) )
99 return false;
100
101 wxInitializeStockLists();
102
103 wxBitmap::InitStandardHandlers();
104
105 return true;
106 }
107
108 // ----------------------------------------------------------------------------
109 // cleanup
110 // ----------------------------------------------------------------------------
111
112 wxAppBase::~wxAppBase()
113 {
114 // this destructor is required for Darwin
115 }
116
117 void wxAppBase::CleanUp()
118 {
119 // clean up all the pending objects
120 DeletePendingObjects();
121
122 // and any remaining TLWs (they remove themselves from wxTopLevelWindows
123 // when destroyed, so iterate until none are left)
124 while ( !wxTopLevelWindows.empty() )
125 {
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();
129 }
130
131 // undo everything we did in Initialize() above
132 wxBitmap::CleanUpHandlers();
133
134 wxStockGDI::DeleteAll();
135
136 wxDeleteStockLists();
137
138 delete wxTheColourDatabase;
139 wxTheColourDatabase = NULL;
140
141 wxAppConsole::CleanUp();
142 }
143
144 // ----------------------------------------------------------------------------
145 // various accessors
146 // ----------------------------------------------------------------------------
147
148 wxWindow* wxAppBase::GetTopWindow() const
149 {
150 wxWindow* window = m_topWindow;
151 if (window == NULL && wxTopLevelWindows.GetCount() > 0)
152 window = wxTopLevelWindows.GetFirst()->GetData();
153 return window;
154 }
155
156 wxVideoMode wxAppBase::GetDisplayMode() const
157 {
158 return wxVideoMode();
159 }
160
161 wxLayoutDirection wxAppBase::GetLayoutDirection() const
162 {
163 #if wxUSE_INTL
164 const wxLocale *const locale = wxGetLocale();
165 if ( locale )
166 {
167 const wxLanguageInfo *const
168 info = wxLocale::GetLanguageInfo(locale->GetLanguage());
169
170 if ( info )
171 return info->LayoutDirection;
172 }
173 #endif // wxUSE_INTL
174
175 // we don't know
176 return wxLayout_Default;
177 }
178
179 #if wxUSE_CMDLINE_PARSER
180
181 // ----------------------------------------------------------------------------
182 // GUI-specific command line options handling
183 // ----------------------------------------------------------------------------
184
185 #define OPTION_THEME "theme"
186 #define OPTION_MODE "mode"
187
188 void wxAppBase::OnInitCmdLine(wxCmdLineParser& parser)
189 {
190 // first add the standard non GUI options
191 wxAppConsole::OnInitCmdLine(parser);
192
193 // the standard command line options
194 static const wxCmdLineEntryDesc cmdLineGUIDesc[] =
195 {
196 #ifdef __WXUNIVERSAL__
197 {
198 wxCMD_LINE_OPTION,
199 NULL,
200 OPTION_THEME,
201 gettext_noop("specify the theme to use"),
202 wxCMD_LINE_VAL_STRING,
203 0x0
204 },
205 #endif // __WXUNIVERSAL__
206
207 #if defined(__WXMGL__)
208 // VS: this is not specific to wxMGL, all fullscreen (framebuffer) ports
209 // should provide this option. That's why it is in common/appcmn.cpp
210 // and not mgl/app.cpp
211 {
212 wxCMD_LINE_OPTION,
213 NULL,
214 OPTION_MODE,
215 gettext_noop("specify display mode to use (e.g. 640x480-16)"),
216 wxCMD_LINE_VAL_STRING,
217 0x0
218 },
219 #endif // __WXMGL__
220
221 // terminator
222 wxCMD_LINE_DESC_END
223 };
224
225 parser.SetDesc(cmdLineGUIDesc);
226 }
227
228 bool wxAppBase::OnCmdLineParsed(wxCmdLineParser& parser)
229 {
230 #ifdef __WXUNIVERSAL__
231 wxString themeName;
232 if ( parser.Found(OPTION_THEME, &themeName) )
233 {
234 wxTheme *theme = wxTheme::Create(themeName);
235 if ( !theme )
236 {
237 wxLogError(_("Unsupported theme '%s'."), themeName.c_str());
238 return false;
239 }
240
241 // Delete the defaultly created theme and set the new theme.
242 delete wxTheme::Get();
243 wxTheme::Set(theme);
244 }
245 #endif // __WXUNIVERSAL__
246
247 #if defined(__WXMGL__)
248 wxString modeDesc;
249 if ( parser.Found(OPTION_MODE, &modeDesc) )
250 {
251 unsigned w, h, bpp;
252 if ( wxSscanf(modeDesc.c_str(), _T("%ux%u-%u"), &w, &h, &bpp) != 3 )
253 {
254 wxLogError(_("Invalid display mode specification '%s'."), modeDesc.c_str());
255 return false;
256 }
257
258 if ( !SetDisplayMode(wxVideoMode(w, h, bpp)) )
259 return false;
260 }
261 #endif // __WXMGL__
262
263 return wxAppConsole::OnCmdLineParsed(parser);
264 }
265
266 #endif // wxUSE_CMDLINE_PARSER
267
268 // ----------------------------------------------------------------------------
269 // OnXXX() hooks
270 // ----------------------------------------------------------------------------
271
272 bool wxAppBase::OnInitGui()
273 {
274 #ifdef __WXUNIVERSAL__
275 if ( !wxTheme::Get() && !wxTheme::CreateDefault() )
276 return false;
277 #endif // __WXUNIVERSAL__
278
279 return true;
280 }
281
282 int wxAppBase::OnRun()
283 {
284 // see the comment in ctor: if the initial value hasn't been changed, use
285 // the default Yes from now on
286 if ( m_exitOnFrameDelete == Later )
287 {
288 m_exitOnFrameDelete = Yes;
289 }
290 //else: it has been changed, assume the user knows what he is doing
291
292 return wxAppConsole::OnRun();
293 }
294
295 int wxAppBase::OnExit()
296 {
297 #ifdef __WXUNIVERSAL__
298 delete wxTheme::Set(NULL);
299 #endif // __WXUNIVERSAL__
300
301 return wxAppConsole::OnExit();
302 }
303
304 wxAppTraits *wxAppBase::CreateTraits()
305 {
306 return new wxGUIAppTraits;
307 }
308
309 // ----------------------------------------------------------------------------
310 // misc
311 // ----------------------------------------------------------------------------
312
313 void wxAppBase::SetActive(bool active, wxWindow * WXUNUSED(lastFocus))
314 {
315 if ( active == m_isActive )
316 return;
317
318 m_isActive = active;
319
320 wxActivateEvent event(wxEVT_ACTIVATE_APP, active);
321 event.SetEventObject(this);
322
323 (void)ProcessEvent(event);
324 }
325
326 // ----------------------------------------------------------------------------
327 // idle handling
328 // ----------------------------------------------------------------------------
329
330 void wxAppBase::DeletePendingObjects()
331 {
332 wxList::compatibility_iterator node = wxPendingDelete.GetFirst();
333 while (node)
334 {
335 wxObject *obj = node->GetData();
336
337 // remove it from the list first so that if we get back here somehow
338 // during the object deletion (e.g. wxYield called from its dtor) we
339 // wouldn't try to delete it the second time
340 if ( wxPendingDelete.Member(obj) )
341 wxPendingDelete.Erase(node);
342
343 delete obj;
344
345 // Deleting one object may have deleted other pending
346 // objects, so start from beginning of list again.
347 node = wxPendingDelete.GetFirst();
348 }
349 }
350
351 // Returns true if more time is needed.
352 bool wxAppBase::ProcessIdle()
353 {
354 // process pending wx events before sending idle events
355 ProcessPendingEvents();
356
357 wxIdleEvent event;
358 bool needMore = false;
359 wxWindowList::compatibility_iterator node = wxTopLevelWindows.GetFirst();
360 while (node)
361 {
362 wxWindow* win = node->GetData();
363 if (SendIdleEvents(win, event))
364 needMore = true;
365 node = node->GetNext();
366 }
367
368 if (wxAppConsole::ProcessIdle())
369 needMore = true;
370
371 // 'Garbage' collection of windows deleted with Close().
372 DeletePendingObjects();
373
374 #if wxUSE_LOG
375 // flush the logged messages if any
376 wxLog::FlushActive();
377 #endif
378
379 wxUpdateUIEvent::ResetUpdateTime();
380
381 return needMore;
382 }
383
384 // Send idle event to window and all subwindows
385 bool wxAppBase::SendIdleEvents(wxWindow* win, wxIdleEvent& event)
386 {
387 bool needMore = false;
388
389 win->OnInternalIdle();
390
391 // should we send idle event to this window?
392 if ( wxIdleEvent::GetMode() == wxIDLE_PROCESS_ALL ||
393 win->HasExtraStyle(wxWS_EX_PROCESS_IDLE) )
394 {
395 event.SetEventObject(win);
396 win->GetEventHandler()->ProcessEvent(event);
397
398 if (event.MoreRequested())
399 needMore = true;
400 }
401 wxWindowList::compatibility_iterator node = win->GetChildren().GetFirst();
402 while ( node )
403 {
404 wxWindow *child = node->GetData();
405 if (SendIdleEvents(child, event))
406 needMore = true;
407
408 node = node->GetNext();
409 }
410
411 return needMore;
412 }
413
414 // ----------------------------------------------------------------------------
415 // wxGUIAppTraitsBase
416 // ----------------------------------------------------------------------------
417
418 #if wxUSE_LOG
419
420 wxLog *wxGUIAppTraitsBase::CreateLogTarget()
421 {
422 // DE: One day I'll remove this but right now the generic dialog used for this
423 // just doesn't work right at all on wxCocoa.
424 #if wxUSE_LOGGUI && !defined(__WXCOCOA__)
425 return new wxLogGui;
426 #else
427 // we must have something!
428 return new wxLogStderr;
429 #endif
430 }
431
432 #endif // wxUSE_LOG
433
434 wxMessageOutput *wxGUIAppTraitsBase::CreateMessageOutput()
435 {
436 // The standard way of printing help on command line arguments (app --help)
437 // is (according to common practice):
438 // - console apps: to stderr (on any platform)
439 // - GUI apps: stderr on Unix platforms (!)
440 // message box under Windows and others
441 #ifdef __UNIX__
442 return new wxMessageOutputStderr;
443 #else // !__UNIX__
444 // wxMessageOutputMessageBox doesn't work under Motif
445 #ifdef __WXMOTIF__
446 return new wxMessageOutputLog;
447 #elif wxUSE_MSGDLG
448 return new wxMessageOutputMessageBox;
449 #else
450 return new wxMessageOutputStderr;
451 #endif
452 #endif // __UNIX__/!__UNIX__
453 }
454
455 #if wxUSE_FONTMAP
456
457 wxFontMapper *wxGUIAppTraitsBase::CreateFontMapper()
458 {
459 return new wxFontMapper;
460 }
461
462 #endif // wxUSE_FONTMAP
463
464 wxRendererNative *wxGUIAppTraitsBase::CreateRenderer()
465 {
466 // use the default native renderer by default
467 return NULL;
468 }
469
470 #ifdef __WXDEBUG__
471
472 bool wxGUIAppTraitsBase::ShowAssertDialog(const wxString& msg)
473 {
474 #if defined(__WXMSW__) || !wxUSE_MSGDLG
475 // under MSW we prefer to use the base class version using ::MessageBox()
476 // even if wxMessageBox() is available because it has less chances to
477 // double fault our app than our wxMessageBox()
478 return wxAppTraitsBase::ShowAssertDialog(msg);
479 #else // wxUSE_MSGDLG
480 wxString msgDlg = msg;
481
482 #if wxUSE_STACKWALKER
483 // on Unix stack frame generation may take some time, depending on the
484 // size of the executable mainly... warn the user that we are working
485 wxFprintf(stderr, wxT("[Debug] Generating a stack trace... please wait"));
486 fflush(stderr);
487
488 const wxString stackTrace = GetAssertStackTrace();
489 if ( !stackTrace.empty() )
490 msgDlg << _T("\n\nCall stack:\n") << stackTrace;
491 #endif // wxUSE_STACKWALKER
492
493 // this message is intentionally not translated -- it is for
494 // developpers only
495 msgDlg += wxT("\nDo you want to stop the program?\n")
496 wxT("You can also choose [Cancel] to suppress ")
497 wxT("further warnings.");
498
499 switch ( wxMessageBox(msgDlg, wxT("wxWidgets Debug Alert"),
500 wxYES_NO | wxCANCEL | wxICON_STOP ) )
501 {
502 case wxYES:
503 wxTrap();
504 break;
505
506 case wxCANCEL:
507 // no more asserts
508 return true;
509
510 //case wxNO: nothing to do
511 }
512
513 return false;
514 #endif // !wxUSE_MSGDLG/wxUSE_MSGDLG
515 }
516
517 #endif // __WXDEBUG__
518
519 bool wxGUIAppTraitsBase::HasStderr()
520 {
521 // we consider that under Unix stderr always goes somewhere, even if the
522 // user doesn't always see it under GUI desktops
523 #ifdef __UNIX__
524 return true;
525 #else
526 return false;
527 #endif
528 }
529
530 void wxGUIAppTraitsBase::ScheduleForDestroy(wxObject *object)
531 {
532 if ( !wxPendingDelete.Member(object) )
533 wxPendingDelete.Append(object);
534 }
535
536 void wxGUIAppTraitsBase::RemoveFromPendingDelete(wxObject *object)
537 {
538 wxPendingDelete.DeleteObject(object);
539 }
540