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