]>
Commit | Line | Data |
---|---|---|
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 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) | |
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 | // DLL options compatibility check: | |
57 | #include "wx/build.h" | |
58 | WX_CHECK_BUILD_OPTIONS("wxCore") | |
59 | ||
60 | // ============================================================================ | |
61 | // wxAppBase implementation | |
62 | // ============================================================================ | |
63 | ||
64 | // ---------------------------------------------------------------------------- | |
65 | // initialization | |
66 | // ---------------------------------------------------------------------------- | |
67 | ||
68 | wxAppBase::wxAppBase() | |
69 | { | |
70 | m_topWindow = (wxWindow *)NULL; | |
71 | m_useBestVisual = FALSE; | |
72 | m_isActive = TRUE; | |
73 | ||
74 | // We don't want to exit the app if the user code shows a dialog from its | |
75 | // OnInit() -- but this is what would happen if we set m_exitOnFrameDelete | |
76 | // to Yes initially as this dialog would be the last top level window. | |
77 | // OTOH, if we set it to No initially we'll have to overwrite it with Yes | |
78 | // when we enter our OnRun() because we do want the default behaviour from | |
79 | // then on. But this would be a problem if the user code calls | |
80 | // SetExitOnFrameDelete(FALSE) from OnInit(). | |
81 | // | |
82 | // So we use the special "Later" value which is such that | |
83 | // GetExitOnFrameDelete() returns FALSE for it but which we know we can | |
84 | // safely (i.e. without losing the effect of the users SetExitOnFrameDelete | |
85 | // call) overwrite in OnRun() | |
86 | m_exitOnFrameDelete = Later; | |
87 | } | |
88 | ||
89 | bool wxAppBase::Initialize(int& argc, wxChar **argv) | |
90 | { | |
91 | if ( !wxAppConsole::Initialize(argc, argv) ) | |
92 | return false; | |
93 | ||
94 | #if wxUSE_THREADS | |
95 | wxPendingEventsLocker = new wxCriticalSection; | |
96 | #endif | |
97 | ||
98 | wxInitializeStockLists(); | |
99 | wxInitializeStockObjects(); | |
100 | ||
101 | wxBitmap::InitStandardHandlers(); | |
102 | ||
103 | return true; | |
104 | } | |
105 | ||
106 | // ---------------------------------------------------------------------------- | |
107 | // cleanup | |
108 | // ---------------------------------------------------------------------------- | |
109 | ||
110 | wxAppBase::~wxAppBase() | |
111 | { | |
112 | // this destructor is required for Darwin | |
113 | } | |
114 | ||
115 | void wxAppBase::CleanUp() | |
116 | { | |
117 | // one last chance for pending objects to be cleaned up | |
118 | DeletePendingObjects(); | |
119 | ||
120 | wxBitmap::CleanUpHandlers(); | |
121 | ||
122 | wxDeleteStockObjects(); | |
123 | ||
124 | wxDeleteStockLists(); | |
125 | ||
126 | delete wxTheColourDatabase; | |
127 | wxTheColourDatabase = NULL; | |
128 | ||
129 | #if wxUSE_THREADS | |
130 | delete wxPendingEvents; | |
131 | wxPendingEvents = NULL; | |
132 | ||
133 | delete wxPendingEventsLocker; | |
134 | wxPendingEventsLocker = NULL; | |
135 | ||
136 | #if wxUSE_VALIDATORS | |
137 | // If we don't do the following, we get an apparent memory leak. | |
138 | ((wxEvtHandler&) wxDefaultValidator).ClearEventLocker(); | |
139 | #endif // wxUSE_VALIDATORS | |
140 | #endif // wxUSE_THREADS | |
141 | } | |
142 | ||
143 | #if wxUSE_CMDLINE_PARSER | |
144 | ||
145 | // ---------------------------------------------------------------------------- | |
146 | // GUI-specific command line options handling | |
147 | // ---------------------------------------------------------------------------- | |
148 | ||
149 | #define OPTION_THEME _T("theme") | |
150 | #define OPTION_MODE _T("mode") | |
151 | ||
152 | void wxAppBase::OnInitCmdLine(wxCmdLineParser& parser) | |
153 | { | |
154 | // first add the standard non GUI options | |
155 | wxAppConsole::OnInitCmdLine(parser); | |
156 | ||
157 | // the standard command line options | |
158 | static const wxCmdLineEntryDesc cmdLineGUIDesc[] = | |
159 | { | |
160 | #ifdef __WXUNIVERSAL__ | |
161 | { | |
162 | wxCMD_LINE_OPTION, | |
163 | _T(""), | |
164 | OPTION_THEME, | |
165 | gettext_noop("specify the theme to use"), | |
166 | wxCMD_LINE_VAL_STRING, | |
167 | 0x0 | |
168 | }, | |
169 | #endif // __WXUNIVERSAL__ | |
170 | ||
171 | #if defined(__WXMGL__) | |
172 | // VS: this is not specific to wxMGL, all fullscreen (framebuffer) ports | |
173 | // should provide this option. That's why it is in common/appcmn.cpp | |
174 | // and not mgl/app.cpp | |
175 | { | |
176 | wxCMD_LINE_OPTION, | |
177 | _T(""), | |
178 | OPTION_MODE, | |
179 | gettext_noop("specify display mode to use (e.g. 640x480-16)"), | |
180 | wxCMD_LINE_VAL_STRING, | |
181 | 0x0 | |
182 | }, | |
183 | #endif // __WXMGL__ | |
184 | ||
185 | // terminator | |
186 | { | |
187 | wxCMD_LINE_NONE, | |
188 | _T(""), | |
189 | _T(""), | |
190 | _T(""), | |
191 | wxCMD_LINE_VAL_NONE, | |
192 | 0x0 | |
193 | } | |
194 | }; | |
195 | ||
196 | parser.SetDesc(cmdLineGUIDesc); | |
197 | } | |
198 | ||
199 | bool wxAppBase::OnCmdLineParsed(wxCmdLineParser& parser) | |
200 | { | |
201 | #ifdef __WXUNIVERSAL__ | |
202 | wxString themeName; | |
203 | if ( parser.Found(OPTION_THEME, &themeName) ) | |
204 | { | |
205 | wxTheme *theme = wxTheme::Create(themeName); | |
206 | if ( !theme ) | |
207 | { | |
208 | wxLogError(_("Unsupported theme '%s'."), themeName.c_str()); | |
209 | return FALSE; | |
210 | } | |
211 | ||
212 | // Delete the defaultly created theme and set the new theme. | |
213 | delete wxTheme::Get(); | |
214 | wxTheme::Set(theme); | |
215 | } | |
216 | #endif // __WXUNIVERSAL__ | |
217 | ||
218 | #if defined(__WXMGL__) | |
219 | wxString modeDesc; | |
220 | if ( parser.Found(OPTION_MODE, &modeDesc) ) | |
221 | { | |
222 | unsigned w, h, bpp; | |
223 | if ( wxSscanf(modeDesc.c_str(), _T("%ux%u-%u"), &w, &h, &bpp) != 3 ) | |
224 | { | |
225 | wxLogError(_("Invalid display mode specification '%s'."), modeDesc.c_str()); | |
226 | return FALSE; | |
227 | } | |
228 | ||
229 | if ( !SetDisplayMode(wxDisplayModeInfo(w, h, bpp)) ) | |
230 | return FALSE; | |
231 | } | |
232 | #endif // __WXMGL__ | |
233 | ||
234 | return wxAppConsole::OnCmdLineParsed(parser); | |
235 | } | |
236 | ||
237 | #endif // wxUSE_CMDLINE_PARSER | |
238 | ||
239 | // ---------------------------------------------------------------------------- | |
240 | // OnXXX() hooks | |
241 | // ---------------------------------------------------------------------------- | |
242 | ||
243 | bool wxAppBase::OnInitGui() | |
244 | { | |
245 | #ifdef __WXUNIVERSAL__ | |
246 | if ( !wxTheme::Get() && !wxTheme::CreateDefault() ) | |
247 | return FALSE; | |
248 | #endif // __WXUNIVERSAL__ | |
249 | ||
250 | return TRUE; | |
251 | } | |
252 | ||
253 | int wxAppBase::OnRun() | |
254 | { | |
255 | // see the comment in ctor: if the initial value hasn't been changed, use | |
256 | // the default Yes from now on | |
257 | if ( m_exitOnFrameDelete == Later ) | |
258 | { | |
259 | m_exitOnFrameDelete = Yes; | |
260 | } | |
261 | //else: it has been changed, assume the user knows what he is doing | |
262 | ||
263 | return MainLoop(); | |
264 | } | |
265 | ||
266 | int wxAppBase::OnExit() | |
267 | { | |
268 | #ifdef __WXUNIVERSAL__ | |
269 | delete wxTheme::Set(NULL); | |
270 | #endif // __WXUNIVERSAL__ | |
271 | ||
272 | return wxAppConsole::OnExit(); | |
273 | } | |
274 | ||
275 | void wxAppBase::Exit() | |
276 | { | |
277 | ExitMainLoop(); | |
278 | } | |
279 | ||
280 | wxAppTraits *wxAppBase::CreateTraits() | |
281 | { | |
282 | return new wxGUIAppTraits; | |
283 | } | |
284 | ||
285 | // ---------------------------------------------------------------------------- | |
286 | // misc | |
287 | // ---------------------------------------------------------------------------- | |
288 | ||
289 | void wxAppBase::SetActive(bool active, wxWindow * WXUNUSED(lastFocus)) | |
290 | { | |
291 | if ( active == m_isActive ) | |
292 | return; | |
293 | ||
294 | m_isActive = active; | |
295 | ||
296 | wxActivateEvent event(wxEVT_ACTIVATE_APP, active); | |
297 | event.SetEventObject(this); | |
298 | ||
299 | (void)ProcessEvent(event); | |
300 | } | |
301 | ||
302 | void wxAppBase::DeletePendingObjects() | |
303 | { | |
304 | wxList::compatibility_iterator node = wxPendingDelete.GetFirst(); | |
305 | while (node) | |
306 | { | |
307 | wxObject *obj = node->GetData(); | |
308 | ||
309 | delete obj; | |
310 | ||
311 | if (wxPendingDelete.Member(obj)) | |
312 | wxPendingDelete.Erase(node); | |
313 | ||
314 | // Deleting one object may have deleted other pending | |
315 | // objects, so start from beginning of list again. | |
316 | node = wxPendingDelete.GetFirst(); | |
317 | } | |
318 | } | |
319 | ||
320 | // Returns TRUE if more time is needed. | |
321 | bool wxAppBase::ProcessIdle() | |
322 | { | |
323 | wxIdleEvent event; | |
324 | bool needMore = FALSE; | |
325 | wxWindowList::compatibility_iterator node = wxTopLevelWindows.GetFirst(); | |
326 | while (node) | |
327 | { | |
328 | wxWindow* win = node->GetData(); | |
329 | if (SendIdleEvents(win, event)) | |
330 | needMore = TRUE; | |
331 | node = node->GetNext(); | |
332 | } | |
333 | ||
334 | event.SetEventObject(this); | |
335 | (void) ProcessEvent(event); | |
336 | if (event.MoreRequested()) | |
337 | needMore = TRUE; | |
338 | ||
339 | wxUpdateUIEvent::ResetUpdateTime(); | |
340 | ||
341 | return needMore; | |
342 | } | |
343 | ||
344 | // Send idle event to window and all subwindows | |
345 | bool wxAppBase::SendIdleEvents(wxWindow* win, wxIdleEvent& event) | |
346 | { | |
347 | bool needMore = FALSE; | |
348 | ||
349 | win->OnInternalIdle(); | |
350 | ||
351 | if (wxIdleEvent::CanSend(win)) | |
352 | { | |
353 | event.SetEventObject(win); | |
354 | win->GetEventHandler()->ProcessEvent(event); | |
355 | ||
356 | if (event.MoreRequested()) | |
357 | needMore = TRUE; | |
358 | } | |
359 | wxWindowList::compatibility_iterator node = win->GetChildren().GetFirst(); | |
360 | while ( node ) | |
361 | { | |
362 | wxWindow *child = node->GetData(); | |
363 | if (SendIdleEvents(child, event)) | |
364 | needMore = TRUE; | |
365 | ||
366 | node = node->GetNext(); | |
367 | } | |
368 | ||
369 | return needMore; | |
370 | } | |
371 | ||
372 | void wxAppBase::OnIdle(wxIdleEvent& WXUNUSED(event)) | |
373 | { | |
374 | // If there are pending events, we must process them: pending events | |
375 | // are either events to the threads other than main or events posted | |
376 | // with wxPostEvent() functions | |
377 | // GRG: I have moved this here so that all pending events are processed | |
378 | // before starting to delete any objects. This behaves better (in | |
379 | // particular, wrt wxPostEvent) and is coherent with wxGTK's current | |
380 | // behaviour. Changed Feb/2000 before 2.1.14 | |
381 | ProcessPendingEvents(); | |
382 | ||
383 | // 'Garbage' collection of windows deleted with Close(). | |
384 | DeletePendingObjects(); | |
385 | ||
386 | #if wxUSE_LOG | |
387 | // flush the logged messages if any | |
388 | wxLog::FlushActive(); | |
389 | #endif // wxUSE_LOG | |
390 | ||
391 | } | |
392 | ||
393 | // ---------------------------------------------------------------------------- | |
394 | // wxGUIAppTraitsBase | |
395 | // ---------------------------------------------------------------------------- | |
396 | ||
397 | #if wxUSE_LOG | |
398 | ||
399 | wxLog *wxGUIAppTraitsBase::CreateLogTarget() | |
400 | { | |
401 | return new wxLogGui; | |
402 | } | |
403 | ||
404 | #endif // wxUSE_LOG | |
405 | ||
406 | wxMessageOutput *wxGUIAppTraitsBase::CreateMessageOutput() | |
407 | { | |
408 | // The standard way of printing help on command line arguments (app --help) | |
409 | // is (according to common practice): | |
410 | // - console apps: to stderr (on any platform) | |
411 | // - GUI apps: stderr on Unix platforms (!) | |
412 | // message box under Windows and others | |
413 | #ifdef __UNIX__ | |
414 | return new wxMessageOutputStderr; | |
415 | #else // !__UNIX__ | |
416 | // wxMessageOutputMessageBox doesn't work under Motif | |
417 | #ifdef __WXMOTIF__ | |
418 | return new wxMessageOutputLog; | |
419 | #else | |
420 | return new wxMessageOutputMessageBox; | |
421 | #endif | |
422 | #endif // __UNIX__/!__UNIX__ | |
423 | } | |
424 | ||
425 | #if wxUSE_FONTMAP | |
426 | ||
427 | wxFontMapper *wxGUIAppTraitsBase::CreateFontMapper() | |
428 | { | |
429 | return new wxFontMapper; | |
430 | } | |
431 | ||
432 | #endif // wxUSE_FONTMAP | |
433 | ||
434 | wxRendererNative *wxGUIAppTraitsBase::CreateRenderer() | |
435 | { | |
436 | // use the default native renderer by default | |
437 | return NULL; | |
438 | } | |
439 | ||
440 | #ifdef __WXDEBUG__ | |
441 | ||
442 | bool wxGUIAppTraitsBase::ShowAssertDialog(const wxString& msg) | |
443 | { | |
444 | // under MSW we prefer to use the base class version using ::MessageBox() | |
445 | // even if wxMessageBox() is available because it has less chances to | |
446 | // double fault our app than our wxMessageBox() | |
447 | #if defined(__WXMSW__) || !wxUSE_MSGDLG | |
448 | return wxAppTraitsBase::ShowAssertDialog(msg); | |
449 | #else // wxUSE_MSGDLG | |
450 | // this message is intentionally not translated -- it is for | |
451 | // developpers only | |
452 | wxString msgDlg(msg); | |
453 | msgDlg += wxT("\nDo you want to stop the program?\n") | |
454 | wxT("You can also choose [Cancel] to suppress ") | |
455 | wxT("further warnings."); | |
456 | ||
457 | switch ( wxMessageBox(msgDlg, wxT("wxWindows Debug Alert"), | |
458 | wxYES_NO | wxCANCEL | wxICON_STOP ) ) | |
459 | { | |
460 | case wxYES: | |
461 | wxTrap(); | |
462 | break; | |
463 | ||
464 | case wxCANCEL: | |
465 | // no more asserts | |
466 | return true; | |
467 | ||
468 | //case wxNO: nothing to do | |
469 | } | |
470 | ||
471 | return false; | |
472 | #endif // !wxUSE_MSGDLG/wxUSE_MSGDLG | |
473 | } | |
474 | ||
475 | #endif // __WXDEBUG__ | |
476 | ||
477 | bool wxGUIAppTraitsBase::HasStderr() | |
478 | { | |
479 | // we consider that under Unix stderr always goes somewhere, even if the | |
480 | // user doesn't always see it under GUI desktops | |
481 | #ifdef __UNIX__ | |
482 | return true; | |
483 | #else | |
484 | return false; | |
485 | #endif | |
486 | } | |
487 | ||
488 | void wxGUIAppTraitsBase::ScheduleForDestroy(wxObject *object) | |
489 | { | |
490 | if ( !wxPendingDelete.Member(object) ) | |
491 | wxPendingDelete.Append(object); | |
492 | } | |
493 | ||
494 | void wxGUIAppTraitsBase::RemoveFromPendingDelete(wxObject *object) | |
495 | { | |
496 | wxPendingDelete.DeleteObject(object); | |
497 | } | |
498 | ||
499 | #if wxUSE_SOCKETS | |
500 | ||
501 | #if defined(__UNIX__) || defined(__DARWIN__) || defined(__OS2__) | |
502 | #include "wx/unix/gsockunx.h" | |
503 | #elif defined(__WINDOWS__) | |
504 | #include "wx/msw/gsockmsw.h" | |
505 | #elif defined(__WXMAC__) | |
506 | #include <MacHeaders.c> | |
507 | #define OTUNIXERRORS 1 | |
508 | #include <OpenTransport.h> | |
509 | #include <OpenTransportProviders.h> | |
510 | #include <OpenTptInternet.h> | |
511 | ||
512 | #include "wx/mac/gsockmac.h" | |
513 | #else | |
514 | #error "Must include correct GSocket header here" | |
515 | #endif | |
516 | ||
517 | GSocketGUIFunctionsTable* wxGUIAppTraitsBase::GetSocketGUIFunctionsTable() | |
518 | { | |
519 | #ifdef __WXMAC__ | |
520 | // NB: wxMac does not have any GUI-specific functions in gsocket.c and | |
521 | // so it doesn't need this table at all | |
522 | return NULL; | |
523 | #else // !__WXMAC__ | |
524 | static GSocketGUIFunctionsTable table = | |
525 | { | |
526 | _GSocket_GUI_Init, | |
527 | _GSocket_GUI_Cleanup, | |
528 | _GSocket_GUI_Init_Socket, | |
529 | _GSocket_GUI_Destroy_Socket, | |
530 | #ifndef __WINDOWS__ | |
531 | _GSocket_Install_Callback, | |
532 | _GSocket_Uninstall_Callback, | |
533 | #endif | |
534 | _GSocket_Enable_Events, | |
535 | _GSocket_Disable_Events | |
536 | }; | |
537 | return &table; | |
538 | #endif // __WXMAC__/!__WXMAC__ | |
539 | } | |
540 | ||
541 | #endif | |
542 |