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