]> git.saurik.com Git - wxWidgets.git/blame - src/common/appcmn.cpp
Typos
[wxWidgets.git] / src / common / appcmn.cpp
CommitLineData
72cdf4c9 1/////////////////////////////////////////////////////////////////////////////
127eab18 2// Name: src/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
65571936 9// Licence: wxWindows licence
72cdf4c9
VZ
10/////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ---------------------------------------------------------------------------
17// headers
18// ---------------------------------------------------------------------------
19
72cdf4c9
VZ
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"
b2e972ec 29 #include "wx/bitmap.h"
bf188f1a 30 #include "wx/intl.h"
e87271f3 31 #include "wx/list.h"
46446cc2 32 #include "wx/log.h"
e2478fde 33 #include "wx/msgdlg.h"
b3dfbbc9
MB
34 #include "wx/bitmap.h"
35 #include "wx/confbase.h"
72cdf4c9
VZ
36#endif
37
e2478fde 38#include "wx/apptrait.h"
b913d3ed 39#include "wx/cmdline.h"
1bf77ee5 40#include "wx/evtloop.h"
e2478fde 41#include "wx/msgout.h"
72cdf4c9 42#include "wx/thread.h"
bebc39e3 43#include "wx/utils.h"
1bf77ee5 44#include "wx/ptr_scpd.h"
a5f1fd3e 45
82ef81ed
WS
46#if defined(__WXMSW__)
47 #include "wx/msw/private.h" // includes windows.h for LOGFONT
1c193821
JS
48#endif
49
50#if wxUSE_FONTMAP
51 #include "wx/fontmap.h"
52#endif // wxUSE_FONTMAP
53
34fdf762
VS
54// DLL options compatibility check:
55#include "wx/build.h"
56WX_CHECK_BUILD_OPTIONS("wxCore")
57
1bf77ee5
VZ
58
59// ----------------------------------------------------------------------------
60// wxEventLoopPtr
61// ----------------------------------------------------------------------------
62
63// this defines wxEventLoopPtr
259c43f6 64wxDEFINE_TIED_SCOPED_PTR_TYPE(wxEventLoop)
1bf77ee5 65
e2478fde
VZ
66// ============================================================================
67// wxAppBase implementation
68// ============================================================================
d54598dd 69
bf188f1a 70// ----------------------------------------------------------------------------
94826170 71// initialization
bf188f1a
VZ
72// ----------------------------------------------------------------------------
73
090a6d7a 74wxAppBase::wxAppBase()
697c5f51 75{
1e6feb95 76 m_topWindow = (wxWindow *)NULL;
4629016d
WS
77 m_useBestVisual = false;
78 m_isActive = true;
1cbee0b4 79
fb761cd5 80#if wxUSE_EVTLOOP_IN_APP
1bf77ee5 81 m_mainLoop = NULL;
fb761cd5 82#endif // wxUSE_EVTLOOP_IN_APP
1bf77ee5 83
1cbee0b4
VZ
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
4629016d 90 // SetExitOnFrameDelete(false) from OnInit().
1cbee0b4
VZ
91 //
92 // So we use the special "Later" value which is such that
4629016d 93 // GetExitOnFrameDelete() returns false for it but which we know we can
1cbee0b4
VZ
94 // safely (i.e. without losing the effect of the users SetExitOnFrameDelete
95 // call) overwrite in OnRun()
96 m_exitOnFrameDelete = Later;
1e6feb95
VZ
97}
98
a54930e0 99bool wxAppBase::Initialize(int& argcOrig, wxChar **argvOrig)
94826170 100{
a54930e0 101 if ( !wxAppConsole::Initialize(argcOrig, argvOrig) )
94826170
VZ
102 return false;
103
94826170
VZ
104#if wxUSE_THREADS
105 wxPendingEventsLocker = new wxCriticalSection;
106#endif
107
94826170
VZ
108 wxInitializeStockLists();
109 wxInitializeStockObjects();
110
111 wxBitmap::InitStandardHandlers();
112
113 return true;
114}
115
116// ----------------------------------------------------------------------------
117// cleanup
118// ----------------------------------------------------------------------------
119
799ea011
GD
120wxAppBase::~wxAppBase()
121{
122 // this destructor is required for Darwin
123}
124
94826170
VZ
125void wxAppBase::CleanUp()
126{
07460370 127 // clean up all the pending objects
94826170
VZ
128 DeletePendingObjects();
129
07460370
VZ
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 }
4055ed82 138
07460370 139 // undo everything we did in Initialize() above
94826170
VZ
140 wxBitmap::CleanUpHandlers();
141
142 wxDeleteStockObjects();
143
144 wxDeleteStockLists();
145
146 delete wxTheColourDatabase;
147 wxTheColourDatabase = NULL;
148
94826170
VZ
149 delete wxPendingEvents;
150 wxPendingEvents = NULL;
151
c8b1e804 152#if wxUSE_THREADS
94826170
VZ
153 delete wxPendingEventsLocker;
154 wxPendingEventsLocker = NULL;
155
b913d3ed
VZ
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
94826170
VZ
160#endif // wxUSE_THREADS
161}
162
b913d3ed
VZ
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{
9c13e5ef
VZ
174 // first add the standard non GUI options
175 wxAppConsole::OnInitCmdLine(parser);
176
b913d3ed
VZ
177 // the standard command line options
178 static const wxCmdLineEntryDesc cmdLineGUIDesc[] =
179 {
180#ifdef __WXUNIVERSAL__
181 {
182 wxCMD_LINE_OPTION,
b494c48b 183 wxEmptyString,
b913d3ed
VZ
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,
b494c48b 197 wxEmptyString,
b913d3ed
VZ
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,
b494c48b
WS
208 wxEmptyString,
209 wxEmptyString,
210 wxEmptyString,
b913d3ed
VZ
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());
4629016d 229 return false;
b913d3ed
VZ
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());
4629016d 246 return false;
b913d3ed
VZ
247 }
248
1c53456f 249 if ( !SetDisplayMode(wxVideoMode(w, h, bpp)) )
4629016d 250 return false;
b913d3ed
VZ
251 }
252#endif // __WXMGL__
253
254 return wxAppConsole::OnCmdLineParsed(parser);
255}
256
257#endif // wxUSE_CMDLINE_PARSER
258
1bf77ee5
VZ
259// ----------------------------------------------------------------------------
260// main event loop implementation
261// ----------------------------------------------------------------------------
262
263int wxAppBase::MainLoop()
264{
fb761cd5 265#if wxUSE_EVTLOOP_IN_APP
efbfda9d 266 wxEventLoopTiedPtr mainLoop(&m_mainLoop, new wxEventLoop);
1bf77ee5
VZ
267
268 return m_mainLoop->Run();
fb761cd5
VZ
269#else // !wxUSE_EVTLOOP_IN_APP
270 return 0;
271#endif // wxUSE_EVTLOOP_IN_APP/!wxUSE_EVTLOOP_IN_APP
1bf77ee5
VZ
272}
273
274void wxAppBase::ExitMainLoop()
275{
fb761cd5 276#if wxUSE_EVTLOOP_IN_APP
1bf77ee5
VZ
277 // we should exit from the main event loop, not just any currently active
278 // (e.g. modal dialog) event loop
dd435a79 279 if ( m_mainLoop && m_mainLoop->IsRunning() )
1bf77ee5
VZ
280 {
281 m_mainLoop->Exit(0);
282 }
fb761cd5 283#endif // wxUSE_EVTLOOP_IN_APP
1bf77ee5
VZ
284}
285
286bool wxAppBase::Pending()
287{
fb761cd5 288#if wxUSE_EVTLOOP_IN_APP
1bf77ee5
VZ
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();
fb761cd5
VZ
295#else // wxUSE_EVTLOOP_IN_APP
296 return false;
297#endif // wxUSE_EVTLOOP_IN_APP/!wxUSE_EVTLOOP_IN_APP
1bf77ee5
VZ
298}
299
300bool wxAppBase::Dispatch()
301{
fb761cd5 302#if wxUSE_EVTLOOP_IN_APP
1bf77ee5
VZ
303 // see comment in Pending()
304 wxEventLoop * const loop = wxEventLoop::GetActive();
305
d1fc6f06 306 return loop && loop->Dispatch();
fb761cd5
VZ
307#else // wxUSE_EVTLOOP_IN_APP
308 return true;
309#endif // wxUSE_EVTLOOP_IN_APP/!wxUSE_EVTLOOP_IN_APP
1bf77ee5
VZ
310}
311
94826170
VZ
312// ----------------------------------------------------------------------------
313// OnXXX() hooks
314// ----------------------------------------------------------------------------
315
1e6feb95
VZ
316bool wxAppBase::OnInitGui()
317{
318#ifdef __WXUNIVERSAL__
bf188f1a 319 if ( !wxTheme::Get() && !wxTheme::CreateDefault() )
4629016d 320 return false;
1e6feb95
VZ
321#endif // __WXUNIVERSAL__
322
4629016d 323 return true;
1e6feb95 324}
1e6feb95 325
1cbee0b4
VZ
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
b913d3ed
VZ
339int wxAppBase::OnExit()
340{
341#ifdef __WXUNIVERSAL__
342 delete wxTheme::Set(NULL);
343#endif // __WXUNIVERSAL__
344
345 return wxAppConsole::OnExit();
346}
347
e2478fde 348void wxAppBase::Exit()
1e6feb95 349{
e2478fde 350 ExitMainLoop();
1e6feb95
VZ
351}
352
e2478fde 353wxAppTraits *wxAppBase::CreateTraits()
a69be60b 354{
7843d11b 355 return new wxGUIAppTraits;
72cdf4c9
VZ
356}
357
1e6feb95
VZ
358// ----------------------------------------------------------------------------
359// misc
360// ----------------------------------------------------------------------------
361
6e169cf3 362void wxAppBase::SetActive(bool active, wxWindow * WXUNUSED(lastFocus))
7beba2fc 363{
66dfed9b
VZ
364 if ( active == m_isActive )
365 return;
366
1e6feb95 367 m_isActive = active;
66dfed9b
VZ
368
369 wxActivateEvent event(wxEVT_ACTIVATE_APP, active);
370 event.SetEventObject(this);
371
372 (void)ProcessEvent(event);
7beba2fc 373}
1e6feb95 374
2dc62891
VZ
375// ----------------------------------------------------------------------------
376// idle handling
377// ----------------------------------------------------------------------------
378
94826170
VZ
379void wxAppBase::DeletePendingObjects()
380{
222ed1d6 381 wxList::compatibility_iterator node = wxPendingDelete.GetFirst();
94826170
VZ
382 while (node)
383 {
384 wxObject *obj = node->GetData();
385
386 delete obj;
387
388 if (wxPendingDelete.Member(obj))
222ed1d6 389 wxPendingDelete.Erase(node);
94826170
VZ
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
4629016d 397// Returns true if more time is needed.
e39af974
JS
398bool wxAppBase::ProcessIdle()
399{
5109ae5d 400 wxIdleEvent event;
4629016d 401 bool needMore = false;
222ed1d6 402 wxWindowList::compatibility_iterator node = wxTopLevelWindows.GetFirst();
e39af974
JS
403 while (node)
404 {
405 wxWindow* win = node->GetData();
5109ae5d 406 if (SendIdleEvents(win, event))
4629016d 407 needMore = true;
e39af974
JS
408 node = node->GetNext();
409 }
410
e39af974 411 event.SetEventObject(this);
5109ae5d
JS
412 (void) ProcessEvent(event);
413 if (event.MoreRequested())
4629016d 414 needMore = true;
e39af974
JS
415
416 wxUpdateUIEvent::ResetUpdateTime();
4629016d 417
5109ae5d 418 return needMore;
e39af974
JS
419}
420
e39af974 421// Send idle event to window and all subwindows
5109ae5d 422bool wxAppBase::SendIdleEvents(wxWindow* win, wxIdleEvent& event)
e39af974 423{
4629016d 424 bool needMore = false;
42d11c8e 425
5109ae5d 426 win->OnInternalIdle();
42d11c8e 427
e39af974
JS
428 if (wxIdleEvent::CanSend(win))
429 {
e39af974
JS
430 event.SetEventObject(win);
431 win->GetEventHandler()->ProcessEvent(event);
432
5109ae5d 433 if (event.MoreRequested())
4629016d 434 needMore = true;
e39af974 435 }
222ed1d6 436 wxWindowList::compatibility_iterator node = win->GetChildren().GetFirst();
e39af974
JS
437 while ( node )
438 {
529b7f71
JS
439 wxWindow *child = node->GetData();
440 if (SendIdleEvents(child, event))
4629016d 441 needMore = true;
e39af974
JS
442
443 node = node->GetNext();
444 }
445
446 return needMore;
447}
448
fc7a2a60 449void wxAppBase::OnIdle(wxIdleEvent& WXUNUSED(event))
955a9197
JS
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}
e39af974 469
2dc62891
VZ
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
bf188f1a 488// ----------------------------------------------------------------------------
e2478fde 489// wxGUIAppTraitsBase
bf188f1a
VZ
490// ----------------------------------------------------------------------------
491
bf188f1a 492#if wxUSE_LOG
bf188f1a 493
e2478fde
VZ
494wxLog *wxGUIAppTraitsBase::CreateLogTarget()
495{
461dae94 496#if wxUSE_LOGGUI
e2478fde 497 return new wxLogGui;
461dae94 498#else
fa6416df 499 // we must have something!
461dae94
VZ
500 return new wxLogStderr;
501#endif
bf188f1a
VZ
502}
503
bf188f1a
VZ
504#endif // wxUSE_LOG
505
e2478fde 506wxMessageOutput *wxGUIAppTraitsBase::CreateMessageOutput()
bf188f1a 507{
e2478fde
VZ
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__
bf188f1a
VZ
523}
524
e2478fde 525#if wxUSE_FONTMAP
bf188f1a 526
e2478fde
VZ
527wxFontMapper *wxGUIAppTraitsBase::CreateFontMapper()
528{
529 return new wxFontMapper;
bf188f1a
VZ
530}
531
e2478fde 532#endif // wxUSE_FONTMAP
bf188f1a 533
f0244295
VZ
534wxRendererNative *wxGUIAppTraitsBase::CreateRenderer()
535{
536 // use the default native renderer by default
537 return NULL;
538}
539
090a6d7a 540#ifdef __WXDEBUG__
e6e6fcc9 541
e2478fde
VZ
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
77ffb593 557 switch ( wxMessageBox(msgDlg, wxT("wxWidgets Debug Alert"),
e2478fde
VZ
558 wxYES_NO | wxCANCEL | wxICON_STOP ) )
559 {
560 case wxYES:
561 wxTrap();
562 break;
090a6d7a 563
e2478fde
VZ
564 case wxCANCEL:
565 // no more asserts
566 return true;
a5f1fd3e 567
e2478fde 568 //case wxNO: nothing to do
090a6d7a 569 }
090a6d7a 570
e2478fde
VZ
571 return false;
572#endif // !wxUSE_MSGDLG/wxUSE_MSGDLG
a5f1fd3e
VZ
573}
574
e2478fde
VZ
575#endif // __WXDEBUG__
576
577bool wxGUIAppTraitsBase::HasStderr()
a5f1fd3e 578{
e2478fde
VZ
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;
a5f1fd3e 583#else
e2478fde 584 return false;
a5f1fd3e 585#endif
a5f1fd3e
VZ
586}
587
e2478fde 588void wxGUIAppTraitsBase::ScheduleForDestroy(wxObject *object)
a5f1fd3e 589{
e2478fde
VZ
590 if ( !wxPendingDelete.Member(object) )
591 wxPendingDelete.Append(object);
a5f1fd3e
VZ
592}
593
e2478fde 594void wxGUIAppTraitsBase::RemoveFromPendingDelete(wxObject *object)
a5f1fd3e 595{
e2478fde 596 wxPendingDelete.DeleteObject(object);
a5f1fd3e
VZ
597}
598
38bb138f
VS
599#if wxUSE_SOCKETS
600
ae3036a2 601#if defined(__WINDOWS__)
38bb138f 602 #include "wx/msw/gsockmsw.h"
ae3036a2
RN
603#elif defined(__UNIX__) || defined(__DARWIN__) || defined(__OS2__)
604 #include "wx/unix/gsockunx.h"
69aa21ac 605#elif defined(__WXMAC__)
127eab18
WS
606 #include <MacHeaders.c>
607 #define OTUNIXERRORS 1
608 #include <OpenTransport.h>
609 #include <OpenTransportProviders.h>
610 #include <OpenTptInternet.h>
69aa21ac 611
127eab18 612 #include "wx/mac/gsockmac.h"
38bb138f
VS
613#else
614 #error "Must include correct GSocket header here"
615#endif
616
617GSocketGUIFunctionsTable* wxGUIAppTraitsBase::GetSocketGUIFunctionsTable()
618{
27a52ff9
GD
619#if defined(__WXMAC__) && !defined(__DARWIN__)
620 // NB: wxMac CFM does not have any GUI-specific functions in gsocket.c and
14a39351
VS
621 // so it doesn't need this table at all
622 return NULL;
27a52ff9 623#else // !__WXMAC__ || __DARWIN__
1d2ec8e5
DE
624 static GSocketGUIFunctionsTableConcrete table;
625 return &table;
1d2ec8e5 626#endif // !__WXMAC__ || __DARWIN__
38bb138f
VS
627}
628
629#endif