moved wxUniv-specific GUI stuff from wxBase to wxCore; this fixes wxUniv DLL build
[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 // the standard command line options
151 static const wxCmdLineEntryDesc cmdLineGUIDesc[] =
152 {
153 #ifdef __WXUNIVERSAL__
154 {
155 wxCMD_LINE_OPTION,
156 _T(""),
157 OPTION_THEME,
158 gettext_noop("specify the theme to use"),
159 wxCMD_LINE_VAL_STRING,
160 0x0
161 },
162 #endif // __WXUNIVERSAL__
163
164 #if defined(__WXMGL__)
165 // VS: this is not specific to wxMGL, all fullscreen (framebuffer) ports
166 // should provide this option. That's why it is in common/appcmn.cpp
167 // and not mgl/app.cpp
168 {
169 wxCMD_LINE_OPTION,
170 _T(""),
171 OPTION_MODE,
172 gettext_noop("specify display mode to use (e.g. 640x480-16)"),
173 wxCMD_LINE_VAL_STRING,
174 0x0
175 },
176 #endif // __WXMGL__
177
178 // terminator
179 {
180 wxCMD_LINE_NONE,
181 _T(""),
182 _T(""),
183 _T(""),
184 wxCMD_LINE_VAL_NONE,
185 0x0
186 }
187 };
188
189 parser.SetDesc(cmdLineGUIDesc);
190 }
191
192 bool wxAppBase::OnCmdLineParsed(wxCmdLineParser& parser)
193 {
194 #ifdef __WXUNIVERSAL__
195 wxString themeName;
196 if ( parser.Found(OPTION_THEME, &themeName) )
197 {
198 wxTheme *theme = wxTheme::Create(themeName);
199 if ( !theme )
200 {
201 wxLogError(_("Unsupported theme '%s'."), themeName.c_str());
202 return FALSE;
203 }
204
205 // Delete the defaultly created theme and set the new theme.
206 delete wxTheme::Get();
207 wxTheme::Set(theme);
208 }
209 #endif // __WXUNIVERSAL__
210
211 #if defined(__WXMGL__)
212 wxString modeDesc;
213 if ( parser.Found(OPTION_MODE, &modeDesc) )
214 {
215 unsigned w, h, bpp;
216 if ( wxSscanf(modeDesc.c_str(), _T("%ux%u-%u"), &w, &h, &bpp) != 3 )
217 {
218 wxLogError(_("Invalid display mode specification '%s'."), modeDesc.c_str());
219 return FALSE;
220 }
221
222 if ( !SetDisplayMode(wxDisplayModeInfo(w, h, bpp)) )
223 return FALSE;
224 }
225 #endif // __WXMGL__
226
227 return wxAppConsole::OnCmdLineParsed(parser);
228 }
229
230 #endif // wxUSE_CMDLINE_PARSER
231
232 // ----------------------------------------------------------------------------
233 // OnXXX() hooks
234 // ----------------------------------------------------------------------------
235
236 bool wxAppBase::OnInitGui()
237 {
238 #ifdef __WXUNIVERSAL__
239 if ( !wxTheme::Get() && !wxTheme::CreateDefault() )
240 return FALSE;
241 #endif // __WXUNIVERSAL__
242
243 return TRUE;
244 }
245
246 int wxAppBase::OnRun()
247 {
248 // see the comment in ctor: if the initial value hasn't been changed, use
249 // the default Yes from now on
250 if ( m_exitOnFrameDelete == Later )
251 {
252 m_exitOnFrameDelete = Yes;
253 }
254 //else: it has been changed, assume the user knows what he is doing
255
256 return MainLoop();
257 }
258
259 int wxAppBase::OnExit()
260 {
261 #ifdef __WXUNIVERSAL__
262 delete wxTheme::Set(NULL);
263 #endif // __WXUNIVERSAL__
264
265 return wxAppConsole::OnExit();
266 }
267
268 void wxAppBase::Exit()
269 {
270 ExitMainLoop();
271 }
272
273 wxAppTraits *wxAppBase::CreateTraits()
274 {
275 return new wxGUIAppTraits;
276 }
277
278 // ----------------------------------------------------------------------------
279 // misc
280 // ----------------------------------------------------------------------------
281
282 void wxAppBase::SetActive(bool active, wxWindow * WXUNUSED(lastFocus))
283 {
284 if ( active == m_isActive )
285 return;
286
287 m_isActive = active;
288
289 wxActivateEvent event(wxEVT_ACTIVATE_APP, active);
290 event.SetEventObject(this);
291
292 (void)ProcessEvent(event);
293 }
294
295 void wxAppBase::DeletePendingObjects()
296 {
297 wxList::compatibility_iterator node = wxPendingDelete.GetFirst();
298 while (node)
299 {
300 wxObject *obj = node->GetData();
301
302 delete obj;
303
304 if (wxPendingDelete.Member(obj))
305 wxPendingDelete.Erase(node);
306
307 // Deleting one object may have deleted other pending
308 // objects, so start from beginning of list again.
309 node = wxPendingDelete.GetFirst();
310 }
311 }
312
313 // Returns TRUE if more time is needed.
314 bool wxAppBase::ProcessIdle()
315 {
316 wxIdleEvent event;
317 bool needMore = FALSE;
318 wxWindowList::compatibility_iterator node = wxTopLevelWindows.GetFirst();
319 node = wxTopLevelWindows.GetFirst();
320 while (node)
321 {
322 wxWindow* win = node->GetData();
323 if (SendIdleEvents(win, event))
324 needMore = TRUE;
325 node = node->GetNext();
326 }
327
328 event.SetEventObject(this);
329 (void) ProcessEvent(event);
330 if (event.MoreRequested())
331 needMore = TRUE;
332
333 wxUpdateUIEvent::ResetUpdateTime();
334
335 return needMore;
336 }
337
338 // Send idle event to window and all subwindows
339 bool wxAppBase::SendIdleEvents(wxWindow* win, wxIdleEvent& event)
340 {
341 bool needMore = FALSE;
342
343 win->OnInternalIdle();
344
345 if (wxIdleEvent::CanSend(win))
346 {
347 event.SetEventObject(win);
348 win->GetEventHandler()->ProcessEvent(event);
349
350 if (event.MoreRequested())
351 needMore = TRUE;
352 }
353 wxWindowList::compatibility_iterator node = win->GetChildren().GetFirst();
354 while ( node )
355 {
356 wxWindow *child = node->GetData();
357 if (SendIdleEvents(child, event))
358 needMore = TRUE;
359
360 node = node->GetNext();
361 }
362
363 return needMore;
364 }
365
366 void wxAppBase::OnIdle(wxIdleEvent& WXUNUSED(event))
367 {
368 // If there are pending events, we must process them: pending events
369 // are either events to the threads other than main or events posted
370 // with wxPostEvent() functions
371 // GRG: I have moved this here so that all pending events are processed
372 // before starting to delete any objects. This behaves better (in
373 // particular, wrt wxPostEvent) and is coherent with wxGTK's current
374 // behaviour. Changed Feb/2000 before 2.1.14
375 ProcessPendingEvents();
376
377 // 'Garbage' collection of windows deleted with Close().
378 DeletePendingObjects();
379
380 #if wxUSE_LOG
381 // flush the logged messages if any
382 wxLog::FlushActive();
383 #endif // wxUSE_LOG
384
385 }
386
387 // ----------------------------------------------------------------------------
388 // wxGUIAppTraitsBase
389 // ----------------------------------------------------------------------------
390
391 #if wxUSE_LOG
392
393 wxLog *wxGUIAppTraitsBase::CreateLogTarget()
394 {
395 return new wxLogGui;
396 }
397
398 #endif // wxUSE_LOG
399
400 wxMessageOutput *wxGUIAppTraitsBase::CreateMessageOutput()
401 {
402 // The standard way of printing help on command line arguments (app --help)
403 // is (according to common practice):
404 // - console apps: to stderr (on any platform)
405 // - GUI apps: stderr on Unix platforms (!)
406 // message box under Windows and others
407 #ifdef __UNIX__
408 return new wxMessageOutputStderr;
409 #else // !__UNIX__
410 // wxMessageOutputMessageBox doesn't work under Motif
411 #ifdef __WXMOTIF__
412 return new wxMessageOutputLog;
413 #else
414 return new wxMessageOutputMessageBox;
415 #endif
416 #endif // __UNIX__/!__UNIX__
417 }
418
419 #if wxUSE_FONTMAP
420
421 wxFontMapper *wxGUIAppTraitsBase::CreateFontMapper()
422 {
423 return new wxFontMapper;
424 }
425
426 #endif // wxUSE_FONTMAP
427
428 wxRendererNative *wxGUIAppTraitsBase::CreateRenderer()
429 {
430 // use the default native renderer by default
431 return NULL;
432 }
433
434 #ifdef __WXDEBUG__
435
436 bool wxGUIAppTraitsBase::ShowAssertDialog(const wxString& msg)
437 {
438 // under MSW we prefer to use the base class version using ::MessageBox()
439 // even if wxMessageBox() is available because it has less chances to
440 // double fault our app than our wxMessageBox()
441 #if defined(__WXMSW__) || !wxUSE_MSGDLG
442 return wxAppTraitsBase::ShowAssertDialog(msg);
443 #else // wxUSE_MSGDLG
444 // this message is intentionally not translated -- it is for
445 // developpers only
446 wxString msgDlg(msg);
447 msgDlg += wxT("\nDo you want to stop the program?\n")
448 wxT("You can also choose [Cancel] to suppress ")
449 wxT("further warnings.");
450
451 switch ( wxMessageBox(msgDlg, wxT("wxWindows Debug Alert"),
452 wxYES_NO | wxCANCEL | wxICON_STOP ) )
453 {
454 case wxYES:
455 wxTrap();
456 break;
457
458 case wxCANCEL:
459 // no more asserts
460 return true;
461
462 //case wxNO: nothing to do
463 }
464
465 return false;
466 #endif // !wxUSE_MSGDLG/wxUSE_MSGDLG
467 }
468
469 #endif // __WXDEBUG__
470
471 bool wxGUIAppTraitsBase::HasStderr()
472 {
473 // we consider that under Unix stderr always goes somewhere, even if the
474 // user doesn't always see it under GUI desktops
475 #ifdef __UNIX__
476 return true;
477 #else
478 return false;
479 #endif
480 }
481
482 void wxGUIAppTraitsBase::ScheduleForDestroy(wxObject *object)
483 {
484 if ( !wxPendingDelete.Member(object) )
485 wxPendingDelete.Append(object);
486 }
487
488 void wxGUIAppTraitsBase::RemoveFromPendingDelete(wxObject *object)
489 {
490 wxPendingDelete.DeleteObject(object);
491 }
492