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