]>
Commit | Line | Data |
---|---|---|
72cdf4c9 VZ |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: common/appcmn.cpp | |
e2478fde | 3 | // Purpose: wxAppConsole and wxAppBase methods common to all platforms |
72cdf4c9 VZ |
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" | |
b2e972ec | 33 | #include "wx/bitmap.h" |
bf188f1a | 34 | #include "wx/intl.h" |
e87271f3 | 35 | #include "wx/list.h" |
46446cc2 | 36 | #include "wx/log.h" |
e2478fde | 37 | #include "wx/msgdlg.h" |
b3dfbbc9 MB |
38 | #include "wx/bitmap.h" |
39 | #include "wx/confbase.h" | |
72cdf4c9 VZ |
40 | #endif |
41 | ||
e2478fde | 42 | #include "wx/apptrait.h" |
b913d3ed | 43 | #include "wx/cmdline.h" |
e2478fde | 44 | #include "wx/msgout.h" |
72cdf4c9 | 45 | #include "wx/thread.h" |
bebc39e3 | 46 | #include "wx/utils.h" |
a5f1fd3e | 47 | |
1c193821 JS |
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 | ||
e2478fde VZ |
56 | // ============================================================================ |
57 | // wxAppBase implementation | |
58 | // ============================================================================ | |
d54598dd | 59 | |
bf188f1a | 60 | // ---------------------------------------------------------------------------- |
94826170 | 61 | // initialization |
bf188f1a VZ |
62 | // ---------------------------------------------------------------------------- |
63 | ||
090a6d7a | 64 | wxAppBase::wxAppBase() |
697c5f51 | 65 | { |
1e6feb95 VZ |
66 | m_topWindow = (wxWindow *)NULL; |
67 | m_useBestVisual = FALSE; | |
1e6feb95 | 68 | m_isActive = TRUE; |
1cbee0b4 VZ |
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; | |
1e6feb95 VZ |
83 | } |
84 | ||
05e2b077 | 85 | bool wxAppBase::Initialize(int& argc, wxChar **argv) |
94826170 VZ |
86 | { |
87 | if ( !wxAppConsole::Initialize(argc, argv) ) | |
88 | return false; | |
89 | ||
94826170 VZ |
90 | #if wxUSE_THREADS |
91 | wxPendingEventsLocker = new wxCriticalSection; | |
92 | #endif | |
93 | ||
94826170 VZ |
94 | wxInitializeStockLists(); |
95 | wxInitializeStockObjects(); | |
96 | ||
97 | wxBitmap::InitStandardHandlers(); | |
98 | ||
99 | return true; | |
100 | } | |
101 | ||
102 | // ---------------------------------------------------------------------------- | |
103 | // cleanup | |
104 | // ---------------------------------------------------------------------------- | |
105 | ||
799ea011 GD |
106 | wxAppBase::~wxAppBase() |
107 | { | |
108 | // this destructor is required for Darwin | |
109 | } | |
110 | ||
94826170 VZ |
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 | ||
b913d3ed VZ |
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 | |
94826170 VZ |
136 | #endif // wxUSE_THREADS |
137 | } | |
138 | ||
b913d3ed VZ |
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 | ||
94826170 VZ |
232 | // ---------------------------------------------------------------------------- |
233 | // OnXXX() hooks | |
234 | // ---------------------------------------------------------------------------- | |
235 | ||
1e6feb95 VZ |
236 | bool wxAppBase::OnInitGui() |
237 | { | |
238 | #ifdef __WXUNIVERSAL__ | |
bf188f1a | 239 | if ( !wxTheme::Get() && !wxTheme::CreateDefault() ) |
1e6feb95 VZ |
240 | return FALSE; |
241 | #endif // __WXUNIVERSAL__ | |
242 | ||
243 | return TRUE; | |
244 | } | |
1e6feb95 | 245 | |
1cbee0b4 VZ |
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 | ||
b913d3ed VZ |
259 | int wxAppBase::OnExit() |
260 | { | |
261 | #ifdef __WXUNIVERSAL__ | |
262 | delete wxTheme::Set(NULL); | |
263 | #endif // __WXUNIVERSAL__ | |
264 | ||
265 | return wxAppConsole::OnExit(); | |
266 | } | |
267 | ||
e2478fde | 268 | void wxAppBase::Exit() |
1e6feb95 | 269 | { |
e2478fde | 270 | ExitMainLoop(); |
1e6feb95 VZ |
271 | } |
272 | ||
e2478fde | 273 | wxAppTraits *wxAppBase::CreateTraits() |
a69be60b | 274 | { |
7843d11b | 275 | return new wxGUIAppTraits; |
72cdf4c9 VZ |
276 | } |
277 | ||
1e6feb95 VZ |
278 | // ---------------------------------------------------------------------------- |
279 | // misc | |
280 | // ---------------------------------------------------------------------------- | |
281 | ||
6e169cf3 | 282 | void wxAppBase::SetActive(bool active, wxWindow * WXUNUSED(lastFocus)) |
7beba2fc | 283 | { |
66dfed9b VZ |
284 | if ( active == m_isActive ) |
285 | return; | |
286 | ||
1e6feb95 | 287 | m_isActive = active; |
66dfed9b VZ |
288 | |
289 | wxActivateEvent event(wxEVT_ACTIVATE_APP, active); | |
290 | event.SetEventObject(this); | |
291 | ||
292 | (void)ProcessEvent(event); | |
7beba2fc | 293 | } |
1e6feb95 | 294 | |
94826170 VZ |
295 | void wxAppBase::DeletePendingObjects() |
296 | { | |
222ed1d6 | 297 | wxList::compatibility_iterator node = wxPendingDelete.GetFirst(); |
94826170 VZ |
298 | while (node) |
299 | { | |
300 | wxObject *obj = node->GetData(); | |
301 | ||
302 | delete obj; | |
303 | ||
304 | if (wxPendingDelete.Member(obj)) | |
222ed1d6 | 305 | wxPendingDelete.Erase(node); |
94826170 VZ |
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 | ||
e39af974 JS |
313 | // Returns TRUE if more time is needed. |
314 | bool wxAppBase::ProcessIdle() | |
315 | { | |
5109ae5d JS |
316 | wxIdleEvent event; |
317 | bool needMore = FALSE; | |
222ed1d6 | 318 | wxWindowList::compatibility_iterator node = wxTopLevelWindows.GetFirst(); |
e39af974 JS |
319 | node = wxTopLevelWindows.GetFirst(); |
320 | while (node) | |
321 | { | |
322 | wxWindow* win = node->GetData(); | |
5109ae5d JS |
323 | if (SendIdleEvents(win, event)) |
324 | needMore = TRUE; | |
e39af974 JS |
325 | node = node->GetNext(); |
326 | } | |
327 | ||
e39af974 | 328 | event.SetEventObject(this); |
5109ae5d JS |
329 | (void) ProcessEvent(event); |
330 | if (event.MoreRequested()) | |
331 | needMore = TRUE; | |
e39af974 JS |
332 | |
333 | wxUpdateUIEvent::ResetUpdateTime(); | |
334 | ||
5109ae5d | 335 | return needMore; |
e39af974 JS |
336 | } |
337 | ||
e39af974 | 338 | // Send idle event to window and all subwindows |
5109ae5d | 339 | bool wxAppBase::SendIdleEvents(wxWindow* win, wxIdleEvent& event) |
e39af974 JS |
340 | { |
341 | bool needMore = FALSE; | |
42d11c8e | 342 | |
5109ae5d | 343 | win->OnInternalIdle(); |
42d11c8e | 344 | |
e39af974 JS |
345 | if (wxIdleEvent::CanSend(win)) |
346 | { | |
e39af974 JS |
347 | event.SetEventObject(win); |
348 | win->GetEventHandler()->ProcessEvent(event); | |
349 | ||
5109ae5d JS |
350 | if (event.MoreRequested()) |
351 | needMore = TRUE; | |
e39af974 | 352 | } |
222ed1d6 | 353 | wxWindowList::compatibility_iterator node = win->GetChildren().GetFirst(); |
e39af974 JS |
354 | while ( node ) |
355 | { | |
529b7f71 JS |
356 | wxWindow *child = node->GetData(); |
357 | if (SendIdleEvents(child, event)) | |
e39af974 JS |
358 | needMore = TRUE; |
359 | ||
360 | node = node->GetNext(); | |
361 | } | |
362 | ||
363 | return needMore; | |
364 | } | |
365 | ||
fc7a2a60 | 366 | void wxAppBase::OnIdle(wxIdleEvent& WXUNUSED(event)) |
955a9197 JS |
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 | } | |
e39af974 | 386 | |
bf188f1a | 387 | // ---------------------------------------------------------------------------- |
e2478fde | 388 | // wxGUIAppTraitsBase |
bf188f1a VZ |
389 | // ---------------------------------------------------------------------------- |
390 | ||
bf188f1a | 391 | #if wxUSE_LOG |
bf188f1a | 392 | |
e2478fde VZ |
393 | wxLog *wxGUIAppTraitsBase::CreateLogTarget() |
394 | { | |
395 | return new wxLogGui; | |
bf188f1a VZ |
396 | } |
397 | ||
bf188f1a VZ |
398 | #endif // wxUSE_LOG |
399 | ||
e2478fde | 400 | wxMessageOutput *wxGUIAppTraitsBase::CreateMessageOutput() |
bf188f1a | 401 | { |
e2478fde VZ |
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__ | |
bf188f1a VZ |
417 | } |
418 | ||
e2478fde | 419 | #if wxUSE_FONTMAP |
bf188f1a | 420 | |
e2478fde VZ |
421 | wxFontMapper *wxGUIAppTraitsBase::CreateFontMapper() |
422 | { | |
423 | return new wxFontMapper; | |
bf188f1a VZ |
424 | } |
425 | ||
e2478fde | 426 | #endif // wxUSE_FONTMAP |
bf188f1a | 427 | |
f0244295 VZ |
428 | wxRendererNative *wxGUIAppTraitsBase::CreateRenderer() |
429 | { | |
430 | // use the default native renderer by default | |
431 | return NULL; | |
432 | } | |
433 | ||
090a6d7a | 434 | #ifdef __WXDEBUG__ |
e6e6fcc9 | 435 | |
e2478fde VZ |
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; | |
090a6d7a | 457 | |
e2478fde VZ |
458 | case wxCANCEL: |
459 | // no more asserts | |
460 | return true; | |
a5f1fd3e | 461 | |
e2478fde | 462 | //case wxNO: nothing to do |
090a6d7a | 463 | } |
090a6d7a | 464 | |
e2478fde VZ |
465 | return false; |
466 | #endif // !wxUSE_MSGDLG/wxUSE_MSGDLG | |
a5f1fd3e VZ |
467 | } |
468 | ||
e2478fde VZ |
469 | #endif // __WXDEBUG__ |
470 | ||
471 | bool wxGUIAppTraitsBase::HasStderr() | |
a5f1fd3e | 472 | { |
e2478fde VZ |
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; | |
a5f1fd3e | 477 | #else |
e2478fde | 478 | return false; |
a5f1fd3e | 479 | #endif |
a5f1fd3e VZ |
480 | } |
481 | ||
e2478fde | 482 | void wxGUIAppTraitsBase::ScheduleForDestroy(wxObject *object) |
a5f1fd3e | 483 | { |
e2478fde VZ |
484 | if ( !wxPendingDelete.Member(object) ) |
485 | wxPendingDelete.Append(object); | |
a5f1fd3e VZ |
486 | } |
487 | ||
e2478fde | 488 | void wxGUIAppTraitsBase::RemoveFromPendingDelete(wxObject *object) |
a5f1fd3e | 489 | { |
e2478fde | 490 | wxPendingDelete.DeleteObject(object); |
a5f1fd3e VZ |
491 | } |
492 |