]> git.saurik.com Git - wxWidgets.git/blob - src/common/appcmn.cpp
Add various selection menu items to the sample.
[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 wxDELETE(wxTheColourDatabase);
132
133 wxAppConsole::CleanUp();
134 }
135
136 // ----------------------------------------------------------------------------
137 // various accessors
138 // ----------------------------------------------------------------------------
139
140 wxWindow* wxAppBase::GetTopWindow() const
141 {
142 wxWindow* window = m_topWindow;
143 if (window == NULL && wxTopLevelWindows.GetCount() > 0)
144 window = wxTopLevelWindows.GetFirst()->GetData();
145 return window;
146 }
147
148 wxVideoMode wxAppBase::GetDisplayMode() const
149 {
150 return wxVideoMode();
151 }
152
153 wxLayoutDirection wxAppBase::GetLayoutDirection() const
154 {
155 #if wxUSE_INTL
156 const wxLocale *const locale = wxGetLocale();
157 if ( locale )
158 {
159 const wxLanguageInfo *const
160 info = wxLocale::GetLanguageInfo(locale->GetLanguage());
161
162 if ( info )
163 return info->LayoutDirection;
164 }
165 #endif // wxUSE_INTL
166
167 // we don't know
168 return wxLayout_Default;
169 }
170
171 #if wxUSE_CMDLINE_PARSER
172
173 // ----------------------------------------------------------------------------
174 // GUI-specific command line options handling
175 // ----------------------------------------------------------------------------
176
177 #define OPTION_THEME "theme"
178 #define OPTION_MODE "mode"
179
180 void wxAppBase::OnInitCmdLine(wxCmdLineParser& parser)
181 {
182 // first add the standard non GUI options
183 wxAppConsole::OnInitCmdLine(parser);
184
185 // the standard command line options
186 static const wxCmdLineEntryDesc cmdLineGUIDesc[] =
187 {
188 #ifdef __WXUNIVERSAL__
189 {
190 wxCMD_LINE_OPTION,
191 NULL,
192 OPTION_THEME,
193 gettext_noop("specify the theme to use"),
194 wxCMD_LINE_VAL_STRING,
195 0x0
196 },
197 #endif // __WXUNIVERSAL__
198
199 #if defined(__WXMGL__)
200 // VS: this is not specific to wxMGL, all fullscreen (framebuffer) ports
201 // should provide this option. That's why it is in common/appcmn.cpp
202 // and not mgl/app.cpp
203 {
204 wxCMD_LINE_OPTION,
205 NULL,
206 OPTION_MODE,
207 gettext_noop("specify display mode to use (e.g. 640x480-16)"),
208 wxCMD_LINE_VAL_STRING,
209 0x0
210 },
211 #endif // __WXMGL__
212
213 // terminator
214 wxCMD_LINE_DESC_END
215 };
216
217 parser.SetDesc(cmdLineGUIDesc);
218 }
219
220 bool wxAppBase::OnCmdLineParsed(wxCmdLineParser& parser)
221 {
222 #ifdef __WXUNIVERSAL__
223 wxString themeName;
224 if ( parser.Found(OPTION_THEME, &themeName) )
225 {
226 wxTheme *theme = wxTheme::Create(themeName);
227 if ( !theme )
228 {
229 wxLogError(_("Unsupported theme '%s'."), themeName.c_str());
230 return false;
231 }
232
233 // Delete the defaultly created theme and set the new theme.
234 delete wxTheme::Get();
235 wxTheme::Set(theme);
236 }
237 #endif // __WXUNIVERSAL__
238
239 #if defined(__WXMGL__)
240 wxString modeDesc;
241 if ( parser.Found(OPTION_MODE, &modeDesc) )
242 {
243 unsigned w, h, bpp;
244 if ( wxSscanf(modeDesc.c_str(), wxT("%ux%u-%u"), &w, &h, &bpp) != 3 )
245 {
246 wxLogError(_("Invalid display mode specification '%s'."), modeDesc.c_str());
247 return false;
248 }
249
250 if ( !SetDisplayMode(wxVideoMode(w, h, bpp)) )
251 return false;
252 }
253 #endif // __WXMGL__
254
255 return wxAppConsole::OnCmdLineParsed(parser);
256 }
257
258 #endif // wxUSE_CMDLINE_PARSER
259
260 // ----------------------------------------------------------------------------
261 // OnXXX() hooks
262 // ----------------------------------------------------------------------------
263
264 bool wxAppBase::OnInitGui()
265 {
266 #ifdef __WXUNIVERSAL__
267 if ( !wxTheme::Get() && !wxTheme::CreateDefault() )
268 return false;
269 #endif // __WXUNIVERSAL__
270
271 return true;
272 }
273
274 int wxAppBase::OnRun()
275 {
276 // see the comment in ctor: if the initial value hasn't been changed, use
277 // the default Yes from now on
278 if ( m_exitOnFrameDelete == Later )
279 {
280 m_exitOnFrameDelete = Yes;
281 }
282 //else: it has been changed, assume the user knows what he is doing
283
284 return wxAppConsole::OnRun();
285 }
286
287 int wxAppBase::OnExit()
288 {
289 #ifdef __WXUNIVERSAL__
290 delete wxTheme::Set(NULL);
291 #endif // __WXUNIVERSAL__
292
293 return wxAppConsole::OnExit();
294 }
295
296 wxAppTraits *wxAppBase::CreateTraits()
297 {
298 return new wxGUIAppTraits;
299 }
300
301 // ----------------------------------------------------------------------------
302 // misc
303 // ----------------------------------------------------------------------------
304
305 void wxAppBase::SetActive(bool active, wxWindow * WXUNUSED(lastFocus))
306 {
307 if ( active == m_isActive )
308 return;
309
310 m_isActive = active;
311
312 wxActivateEvent event(wxEVT_ACTIVATE_APP, active);
313 event.SetEventObject(this);
314
315 (void)ProcessEvent(event);
316 }
317
318 bool wxAppBase::SafeYield(wxWindow *win, bool onlyIfNeeded)
319 {
320 wxWindowDisabler wd(win);
321
322 wxEventLoopBase * const loop = wxEventLoopBase::GetActive();
323
324 return loop && loop->Yield(onlyIfNeeded);
325 }
326
327 bool wxAppBase::SafeYieldFor(wxWindow *win, long eventsToProcess)
328 {
329 wxWindowDisabler wd(win);
330
331 wxEventLoopBase * const loop = wxEventLoopBase::GetActive();
332
333 return loop && loop->YieldFor(eventsToProcess);
334 }
335
336
337 // ----------------------------------------------------------------------------
338 // idle handling
339 // ----------------------------------------------------------------------------
340
341 // Returns true if more time is needed.
342 bool wxAppBase::ProcessIdle()
343 {
344 // call the base class version first to send the idle event to wxTheApp
345 // itself
346 bool needMore = wxAppConsoleBase::ProcessIdle();
347 wxIdleEvent event;
348 wxWindowList::compatibility_iterator node = wxTopLevelWindows.GetFirst();
349 while (node)
350 {
351 wxWindow* win = node->GetData();
352 if (win->SendIdleEvents(event))
353 needMore = true;
354 node = node->GetNext();
355 }
356
357 wxUpdateUIEvent::ResetUpdateTime();
358
359 return needMore;
360 }
361
362 // ----------------------------------------------------------------------------
363 // wxGUIAppTraitsBase
364 // ----------------------------------------------------------------------------
365
366 #if wxUSE_LOG
367
368 wxLog *wxGUIAppTraitsBase::CreateLogTarget()
369 {
370 #if wxUSE_LOGGUI
371 #ifndef __WXOSX_IPHONE__
372 return new wxLogGui;
373 #else
374 return new wxLogStderr;
375 #endif
376 #else
377 // we must have something!
378 return new wxLogStderr;
379 #endif
380 }
381
382 #endif // wxUSE_LOG
383
384 wxMessageOutput *wxGUIAppTraitsBase::CreateMessageOutput()
385 {
386 // The standard way of printing help on command line arguments (app --help)
387 // is (according to common practice):
388 // - console apps: to stderr (on any platform)
389 // - GUI apps: stderr on Unix platforms (!)
390 // stderr if available and message box otherwise on others
391 // (currently stderr only Windows if app running from console)
392 #ifdef __UNIX__
393 return new wxMessageOutputStderr;
394 #else // !__UNIX__
395 // wxMessageOutputMessageBox doesn't work under Motif
396 #ifdef __WXMOTIF__
397 return new wxMessageOutputLog;
398 #elif wxUSE_MSGDLG
399 return new wxMessageOutputBest(wxMSGOUT_PREFER_STDERR);
400 #else
401 return new wxMessageOutputStderr;
402 #endif
403 #endif // __UNIX__/!__UNIX__
404 }
405
406 #if wxUSE_FONTMAP
407
408 wxFontMapper *wxGUIAppTraitsBase::CreateFontMapper()
409 {
410 return new wxFontMapper;
411 }
412
413 #endif // wxUSE_FONTMAP
414
415 wxRendererNative *wxGUIAppTraitsBase::CreateRenderer()
416 {
417 // use the default native renderer by default
418 return NULL;
419 }
420
421 bool wxGUIAppTraitsBase::ShowAssertDialog(const wxString& msg)
422 {
423 #if wxDEBUG_LEVEL
424 // under MSW we prefer to use the base class version using ::MessageBox()
425 // even if wxMessageBox() is available because it has less chances to
426 // double fault our app than our wxMessageBox()
427 //
428 // under DFB the message dialog is not always functional right now
429 //
430 // and finally we can't use wxMessageBox() if it wasn't compiled in, of
431 // course
432 #if !defined(__WXMSW__) && !defined(__WXDFB__) && wxUSE_MSGDLG
433
434 // we can't (safely) show the GUI dialog from another thread, only do it
435 // for the asserts in the main thread
436 if ( wxIsMainThread() )
437 {
438 wxString msgDlg = msg;
439
440 #if wxUSE_STACKWALKER
441 const wxString stackTrace = GetAssertStackTrace();
442 if ( !stackTrace.empty() )
443 msgDlg << wxT("\n\nCall stack:\n") << stackTrace;
444 #endif // wxUSE_STACKWALKER
445
446 // this message is intentionally not translated -- it is for
447 // developpers only
448 msgDlg += wxT("\nDo you want to stop the program?\n")
449 wxT("You can also choose [Cancel] to suppress ")
450 wxT("further warnings.");
451
452 switch ( wxMessageBox(msgDlg, wxT("wxWidgets Debug Alert"),
453 wxYES_NO | wxCANCEL | wxICON_STOP ) )
454 {
455 case wxYES:
456 wxTrap();
457 break;
458
459 case wxCANCEL:
460 // no more asserts
461 return true;
462
463 //case wxNO: nothing to do
464 }
465
466 return false;
467 }
468 #endif // wxUSE_MSGDLG
469 #endif // wxDEBUG_LEVEL
470
471 return wxAppTraitsBase::ShowAssertDialog(msg);
472 }
473
474 bool wxGUIAppTraitsBase::HasStderr()
475 {
476 // we consider that under Unix stderr always goes somewhere, even if the
477 // user doesn't always see it under GUI desktops
478 #ifdef __UNIX__
479 return true;
480 #else
481 return false;
482 #endif
483 }
484