fixed non GUI cmd line options
[wxWidgets.git] / src / common / appcmn.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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 #ifdef __GNUG__
21 #pragma implementation "appbase.h"
22 #endif
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #if defined(__BORLANDC__)
28 #pragma hdrstop
29 #endif
30
31 #ifndef WX_PRECOMP
32 #include "wx/app.h"
33 #include "wx/bitmap.h"
34 #include "wx/intl.h"
35 #include "wx/list.h"
36 #include "wx/log.h"
37 #include "wx/msgdlg.h"
38 #include "wx/bitmap.h"
39 #include "wx/confbase.h"
40 #endif
41
42 #include "wx/apptrait.h"
43 #include "wx/cmdline.h"
44 #include "wx/msgout.h"
45 #include "wx/thread.h"
46 #include "wx/utils.h"
47
48 #if defined(__WXMSW__)
49 #include "wx/msw/private.h" // includes windows.h for LOGFONT
50 #endif
51
52 #if wxUSE_FONTMAP
53 #include "wx/fontmap.h"
54 #endif // wxUSE_FONTMAP
55
56 // ============================================================================
57 // wxAppBase implementation
58 // ============================================================================
59
60 // ----------------------------------------------------------------------------
61 // initialization
62 // ----------------------------------------------------------------------------
63
64 wxAppBase::wxAppBase()
65 {
66 m_topWindow = (wxWindow *)NULL;
67 m_useBestVisual = FALSE;
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& argc, wxChar **argv)
86 {
87 if ( !wxAppConsole::Initialize(argc, argv) )
88 return false;
89
90 #if wxUSE_THREADS
91 wxPendingEventsLocker = new wxCriticalSection;
92 #endif
93
94 wxInitializeStockLists();
95 wxInitializeStockObjects();
96
97 wxBitmap::InitStandardHandlers();
98
99 return true;
100 }
101
102 // ----------------------------------------------------------------------------
103 // cleanup
104 // ----------------------------------------------------------------------------
105
106 wxAppBase::~wxAppBase()
107 {
108 // this destructor is required for Darwin
109 }
110
111 void wxAppBase::CleanUp()
112 {
113 // one last chance for pending objects to be cleaned up
114 DeletePendingObjects();
115
116 wxBitmap::CleanUpHandlers();
117
118 wxDeleteStockObjects();
119
120 wxDeleteStockLists();
121
122 delete wxTheColourDatabase;
123 wxTheColourDatabase = NULL;
124
125 #if wxUSE_THREADS
126 delete wxPendingEvents;
127 wxPendingEvents = NULL;
128
129 delete wxPendingEventsLocker;
130 wxPendingEventsLocker = NULL;
131
132 #if wxUSE_VALIDATORS
133 // If we don't do the following, we get an apparent memory leak.
134 ((wxEvtHandler&) wxDefaultValidator).ClearEventLocker();
135 #endif // wxUSE_VALIDATORS
136 #endif // wxUSE_THREADS
137 }
138
139 #if wxUSE_CMDLINE_PARSER
140
141 // ----------------------------------------------------------------------------
142 // GUI-specific command line options handling
143 // ----------------------------------------------------------------------------
144
145 #define OPTION_THEME _T("theme")
146 #define OPTION_MODE _T("mode")
147
148 void wxAppBase::OnInitCmdLine(wxCmdLineParser& parser)
149 {
150 // first add the standard non GUI options
151 wxAppConsole::OnInitCmdLine(parser);
152
153 // the standard command line options
154 static const wxCmdLineEntryDesc cmdLineGUIDesc[] =
155 {
156 #ifdef __WXUNIVERSAL__
157 {
158 wxCMD_LINE_OPTION,
159 _T(""),
160 OPTION_THEME,
161 gettext_noop("specify the theme to use"),
162 wxCMD_LINE_VAL_STRING,
163 0x0
164 },
165 #endif // __WXUNIVERSAL__
166
167 #if defined(__WXMGL__)
168 // VS: this is not specific to wxMGL, all fullscreen (framebuffer) ports
169 // should provide this option. That's why it is in common/appcmn.cpp
170 // and not mgl/app.cpp
171 {
172 wxCMD_LINE_OPTION,
173 _T(""),
174 OPTION_MODE,
175 gettext_noop("specify display mode to use (e.g. 640x480-16)"),
176 wxCMD_LINE_VAL_STRING,
177 0x0
178 },
179 #endif // __WXMGL__
180
181 // terminator
182 {
183 wxCMD_LINE_NONE,
184 _T(""),
185 _T(""),
186 _T(""),
187 wxCMD_LINE_VAL_NONE,
188 0x0
189 }
190 };
191
192 parser.SetDesc(cmdLineGUIDesc);
193 }
194
195 bool wxAppBase::OnCmdLineParsed(wxCmdLineParser& parser)
196 {
197 #ifdef __WXUNIVERSAL__
198 wxString themeName;
199 if ( parser.Found(OPTION_THEME, &themeName) )
200 {
201 wxTheme *theme = wxTheme::Create(themeName);
202 if ( !theme )
203 {
204 wxLogError(_("Unsupported theme '%s'."), themeName.c_str());
205 return FALSE;
206 }
207
208 // Delete the defaultly created theme and set the new theme.
209 delete wxTheme::Get();
210 wxTheme::Set(theme);
211 }
212 #endif // __WXUNIVERSAL__
213
214 #if defined(__WXMGL__)
215 wxString modeDesc;
216 if ( parser.Found(OPTION_MODE, &modeDesc) )
217 {
218 unsigned w, h, bpp;
219 if ( wxSscanf(modeDesc.c_str(), _T("%ux%u-%u"), &w, &h, &bpp) != 3 )
220 {
221 wxLogError(_("Invalid display mode specification '%s'."), modeDesc.c_str());
222 return FALSE;
223 }
224
225 if ( !SetDisplayMode(wxDisplayModeInfo(w, h, bpp)) )
226 return FALSE;
227 }
228 #endif // __WXMGL__
229
230 return wxAppConsole::OnCmdLineParsed(parser);
231 }
232
233 #endif // wxUSE_CMDLINE_PARSER
234
235 // ----------------------------------------------------------------------------
236 // OnXXX() hooks
237 // ----------------------------------------------------------------------------
238
239 bool wxAppBase::OnInitGui()
240 {
241 #ifdef __WXUNIVERSAL__
242 if ( !wxTheme::Get() && !wxTheme::CreateDefault() )
243 return FALSE;
244 #endif // __WXUNIVERSAL__
245
246 return TRUE;
247 }
248
249 int wxAppBase::OnRun()
250 {
251 // see the comment in ctor: if the initial value hasn't been changed, use
252 // the default Yes from now on
253 if ( m_exitOnFrameDelete == Later )
254 {
255 m_exitOnFrameDelete = Yes;
256 }
257 //else: it has been changed, assume the user knows what he is doing
258
259 return MainLoop();
260 }
261
262 int wxAppBase::OnExit()
263 {
264 #ifdef __WXUNIVERSAL__
265 delete wxTheme::Set(NULL);
266 #endif // __WXUNIVERSAL__
267
268 return wxAppConsole::OnExit();
269 }
270
271 void wxAppBase::Exit()
272 {
273 ExitMainLoop();
274 }
275
276 wxAppTraits *wxAppBase::CreateTraits()
277 {
278 return new wxGUIAppTraits;
279 }
280
281 // ----------------------------------------------------------------------------
282 // misc
283 // ----------------------------------------------------------------------------
284
285 void wxAppBase::SetActive(bool active, wxWindow * WXUNUSED(lastFocus))
286 {
287 if ( active == m_isActive )
288 return;
289
290 m_isActive = active;
291
292 wxActivateEvent event(wxEVT_ACTIVATE_APP, active);
293 event.SetEventObject(this);
294
295 (void)ProcessEvent(event);
296 }
297
298 void wxAppBase::DeletePendingObjects()
299 {
300 wxList::compatibility_iterator node = wxPendingDelete.GetFirst();
301 while (node)
302 {
303 wxObject *obj = node->GetData();
304
305 delete obj;
306
307 if (wxPendingDelete.Member(obj))
308 wxPendingDelete.Erase(node);
309
310 // Deleting one object may have deleted other pending
311 // objects, so start from beginning of list again.
312 node = wxPendingDelete.GetFirst();
313 }
314 }
315
316 // Returns TRUE if more time is needed.
317 bool wxAppBase::ProcessIdle()
318 {
319 wxIdleEvent event;
320 bool needMore = FALSE;
321 wxWindowList::compatibility_iterator node = wxTopLevelWindows.GetFirst();
322 node = wxTopLevelWindows.GetFirst();
323 while (node)
324 {
325 wxWindow* win = node->GetData();
326 if (SendIdleEvents(win, event))
327 needMore = TRUE;
328 node = node->GetNext();
329 }
330
331 event.SetEventObject(this);
332 (void) ProcessEvent(event);
333 if (event.MoreRequested())
334 needMore = TRUE;
335
336 wxUpdateUIEvent::ResetUpdateTime();
337
338 return needMore;
339 }
340
341 // Send idle event to window and all subwindows
342 bool wxAppBase::SendIdleEvents(wxWindow* win, wxIdleEvent& event)
343 {
344 bool needMore = FALSE;
345
346 win->OnInternalIdle();
347
348 if (wxIdleEvent::CanSend(win))
349 {
350 event.SetEventObject(win);
351 win->GetEventHandler()->ProcessEvent(event);
352
353 if (event.MoreRequested())
354 needMore = TRUE;
355 }
356 wxWindowList::compatibility_iterator node = win->GetChildren().GetFirst();
357 while ( node )
358 {
359 wxWindow *child = node->GetData();
360 if (SendIdleEvents(child, event))
361 needMore = TRUE;
362
363 node = node->GetNext();
364 }
365
366 return needMore;
367 }
368
369 void wxAppBase::OnIdle(wxIdleEvent& WXUNUSED(event))
370 {
371 // If there are pending events, we must process them: pending events
372 // are either events to the threads other than main or events posted
373 // with wxPostEvent() functions
374 // GRG: I have moved this here so that all pending events are processed
375 // before starting to delete any objects. This behaves better (in
376 // particular, wrt wxPostEvent) and is coherent with wxGTK's current
377 // behaviour. Changed Feb/2000 before 2.1.14
378 ProcessPendingEvents();
379
380 // 'Garbage' collection of windows deleted with Close().
381 DeletePendingObjects();
382
383 #if wxUSE_LOG
384 // flush the logged messages if any
385 wxLog::FlushActive();
386 #endif // wxUSE_LOG
387
388 }
389
390 // ----------------------------------------------------------------------------
391 // wxGUIAppTraitsBase
392 // ----------------------------------------------------------------------------
393
394 #if wxUSE_LOG
395
396 wxLog *wxGUIAppTraitsBase::CreateLogTarget()
397 {
398 return new wxLogGui;
399 }
400
401 #endif // wxUSE_LOG
402
403 wxMessageOutput *wxGUIAppTraitsBase::CreateMessageOutput()
404 {
405 // The standard way of printing help on command line arguments (app --help)
406 // is (according to common practice):
407 // - console apps: to stderr (on any platform)
408 // - GUI apps: stderr on Unix platforms (!)
409 // message box under Windows and others
410 #ifdef __UNIX__
411 return new wxMessageOutputStderr;
412 #else // !__UNIX__
413 // wxMessageOutputMessageBox doesn't work under Motif
414 #ifdef __WXMOTIF__
415 return new wxMessageOutputLog;
416 #else
417 return new wxMessageOutputMessageBox;
418 #endif
419 #endif // __UNIX__/!__UNIX__
420 }
421
422 #if wxUSE_FONTMAP
423
424 wxFontMapper *wxGUIAppTraitsBase::CreateFontMapper()
425 {
426 return new wxFontMapper;
427 }
428
429 #endif // wxUSE_FONTMAP
430
431 wxRendererNative *wxGUIAppTraitsBase::CreateRenderer()
432 {
433 // use the default native renderer by default
434 return NULL;
435 }
436
437 #ifdef __WXDEBUG__
438
439 bool wxGUIAppTraitsBase::ShowAssertDialog(const wxString& msg)
440 {
441 // under MSW we prefer to use the base class version using ::MessageBox()
442 // even if wxMessageBox() is available because it has less chances to
443 // double fault our app than our wxMessageBox()
444 #if defined(__WXMSW__) || !wxUSE_MSGDLG
445 return wxAppTraitsBase::ShowAssertDialog(msg);
446 #else // wxUSE_MSGDLG
447 // this message is intentionally not translated -- it is for
448 // developpers only
449 wxString msgDlg(msg);
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("wxWindows 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 #endif // !wxUSE_MSGDLG/wxUSE_MSGDLG
470 }
471
472 #endif // __WXDEBUG__
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
485 void wxGUIAppTraitsBase::ScheduleForDestroy(wxObject *object)
486 {
487 if ( !wxPendingDelete.Member(object) )
488 wxPendingDelete.Append(object);
489 }
490
491 void wxGUIAppTraitsBase::RemoveFromPendingDelete(wxObject *object)
492 {
493 wxPendingDelete.DeleteObject(object);
494 }
495