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