]> git.saurik.com Git - wxWidgets.git/blame - src/common/appcmn.cpp
disable Borland C++ warnings
[wxWidgets.git] / src / common / appcmn.cpp
CommitLineData
72cdf4c9
VZ
1/////////////////////////////////////////////////////////////////////////////
2// Name: 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
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ---------------------------------------------------------------------------
17// headers
18// ---------------------------------------------------------------------------
19
20#ifdef __GNUG__
21 #pragma implementation "appbase.h"
22#endif
23
24// For compilers that support precompilation, includes "wx.h".
25#include "wx/wxprec.h"
26
27#if defined(__BORLANDC__)
28 #pragma hdrstop
29#endif
30
31#ifndef WX_PRECOMP
32 #include "wx/app.h"
b2e972ec 33 #include "wx/bitmap.h"
bf188f1a 34 #include "wx/intl.h"
e87271f3 35 #include "wx/list.h"
46446cc2 36 #include "wx/log.h"
e2478fde 37 #include "wx/msgdlg.h"
b3dfbbc9
MB
38 #include "wx/bitmap.h"
39 #include "wx/confbase.h"
72cdf4c9
VZ
40#endif
41
e2478fde 42#include "wx/apptrait.h"
b913d3ed 43#include "wx/cmdline.h"
e2478fde 44#include "wx/msgout.h"
72cdf4c9 45#include "wx/thread.h"
bebc39e3 46#include "wx/utils.h"
a5f1fd3e 47
1c193821
JS
48#if defined(__WXMSW__)
49 #include "wx/msw/private.h" // includes windows.h for LOGFONT
50#endif
51
52#if wxUSE_FONTMAP
53 #include "wx/fontmap.h"
54#endif // wxUSE_FONTMAP
55
34fdf762
VS
56// DLL options compatibility check:
57#include "wx/build.h"
58WX_CHECK_BUILD_OPTIONS("wxCore")
59
e2478fde
VZ
60// ============================================================================
61// wxAppBase implementation
62// ============================================================================
d54598dd 63
bf188f1a 64// ----------------------------------------------------------------------------
94826170 65// initialization
bf188f1a
VZ
66// ----------------------------------------------------------------------------
67
090a6d7a 68wxAppBase::wxAppBase()
697c5f51 69{
1e6feb95
VZ
70 m_topWindow = (wxWindow *)NULL;
71 m_useBestVisual = FALSE;
1e6feb95 72 m_isActive = TRUE;
1cbee0b4
VZ
73
74 // We don't want to exit the app if the user code shows a dialog from its
75 // OnInit() -- but this is what would happen if we set m_exitOnFrameDelete
76 // to Yes initially as this dialog would be the last top level window.
77 // OTOH, if we set it to No initially we'll have to overwrite it with Yes
78 // when we enter our OnRun() because we do want the default behaviour from
79 // then on. But this would be a problem if the user code calls
80 // SetExitOnFrameDelete(FALSE) from OnInit().
81 //
82 // So we use the special "Later" value which is such that
83 // GetExitOnFrameDelete() returns FALSE for it but which we know we can
84 // safely (i.e. without losing the effect of the users SetExitOnFrameDelete
85 // call) overwrite in OnRun()
86 m_exitOnFrameDelete = Later;
1e6feb95
VZ
87}
88
05e2b077 89bool wxAppBase::Initialize(int& argc, wxChar **argv)
94826170
VZ
90{
91 if ( !wxAppConsole::Initialize(argc, argv) )
92 return false;
93
94826170
VZ
94#if wxUSE_THREADS
95 wxPendingEventsLocker = new wxCriticalSection;
96#endif
97
94826170
VZ
98 wxInitializeStockLists();
99 wxInitializeStockObjects();
100
101 wxBitmap::InitStandardHandlers();
102
103 return true;
104}
105
106// ----------------------------------------------------------------------------
107// cleanup
108// ----------------------------------------------------------------------------
109
799ea011
GD
110wxAppBase::~wxAppBase()
111{
112 // this destructor is required for Darwin
113}
114
94826170
VZ
115void wxAppBase::CleanUp()
116{
117 // one last chance for pending objects to be cleaned up
118 DeletePendingObjects();
119
120 wxBitmap::CleanUpHandlers();
121
122 wxDeleteStockObjects();
123
124 wxDeleteStockLists();
125
126 delete wxTheColourDatabase;
127 wxTheColourDatabase = NULL;
128
129#if wxUSE_THREADS
130 delete wxPendingEvents;
131 wxPendingEvents = NULL;
132
133 delete wxPendingEventsLocker;
134 wxPendingEventsLocker = NULL;
135
b913d3ed
VZ
136 #if wxUSE_VALIDATORS
137 // If we don't do the following, we get an apparent memory leak.
138 ((wxEvtHandler&) wxDefaultValidator).ClearEventLocker();
139 #endif // wxUSE_VALIDATORS
94826170
VZ
140#endif // wxUSE_THREADS
141}
142
b913d3ed
VZ
143#if wxUSE_CMDLINE_PARSER
144
145// ----------------------------------------------------------------------------
146// GUI-specific command line options handling
147// ----------------------------------------------------------------------------
148
149#define OPTION_THEME _T("theme")
150#define OPTION_MODE _T("mode")
151
152void wxAppBase::OnInitCmdLine(wxCmdLineParser& parser)
153{
9c13e5ef
VZ
154 // first add the standard non GUI options
155 wxAppConsole::OnInitCmdLine(parser);
156
b913d3ed
VZ
157 // the standard command line options
158 static const wxCmdLineEntryDesc cmdLineGUIDesc[] =
159 {
160#ifdef __WXUNIVERSAL__
161 {
162 wxCMD_LINE_OPTION,
163 _T(""),
164 OPTION_THEME,
165 gettext_noop("specify the theme to use"),
166 wxCMD_LINE_VAL_STRING,
167 0x0
168 },
169#endif // __WXUNIVERSAL__
170
171#if defined(__WXMGL__)
172 // VS: this is not specific to wxMGL, all fullscreen (framebuffer) ports
173 // should provide this option. That's why it is in common/appcmn.cpp
174 // and not mgl/app.cpp
175 {
176 wxCMD_LINE_OPTION,
177 _T(""),
178 OPTION_MODE,
179 gettext_noop("specify display mode to use (e.g. 640x480-16)"),
180 wxCMD_LINE_VAL_STRING,
181 0x0
182 },
183#endif // __WXMGL__
184
185 // terminator
186 {
187 wxCMD_LINE_NONE,
188 _T(""),
189 _T(""),
190 _T(""),
191 wxCMD_LINE_VAL_NONE,
192 0x0
193 }
194 };
195
196 parser.SetDesc(cmdLineGUIDesc);
197}
198
199bool wxAppBase::OnCmdLineParsed(wxCmdLineParser& parser)
200{
201#ifdef __WXUNIVERSAL__
202 wxString themeName;
203 if ( parser.Found(OPTION_THEME, &themeName) )
204 {
205 wxTheme *theme = wxTheme::Create(themeName);
206 if ( !theme )
207 {
208 wxLogError(_("Unsupported theme '%s'."), themeName.c_str());
209 return FALSE;
210 }
211
212 // Delete the defaultly created theme and set the new theme.
213 delete wxTheme::Get();
214 wxTheme::Set(theme);
215 }
216#endif // __WXUNIVERSAL__
217
218#if defined(__WXMGL__)
219 wxString modeDesc;
220 if ( parser.Found(OPTION_MODE, &modeDesc) )
221 {
222 unsigned w, h, bpp;
223 if ( wxSscanf(modeDesc.c_str(), _T("%ux%u-%u"), &w, &h, &bpp) != 3 )
224 {
225 wxLogError(_("Invalid display mode specification '%s'."), modeDesc.c_str());
226 return FALSE;
227 }
228
229 if ( !SetDisplayMode(wxDisplayModeInfo(w, h, bpp)) )
230 return FALSE;
231 }
232#endif // __WXMGL__
233
234 return wxAppConsole::OnCmdLineParsed(parser);
235}
236
237#endif // wxUSE_CMDLINE_PARSER
238
94826170
VZ
239// ----------------------------------------------------------------------------
240// OnXXX() hooks
241// ----------------------------------------------------------------------------
242
1e6feb95
VZ
243bool wxAppBase::OnInitGui()
244{
245#ifdef __WXUNIVERSAL__
bf188f1a 246 if ( !wxTheme::Get() && !wxTheme::CreateDefault() )
1e6feb95
VZ
247 return FALSE;
248#endif // __WXUNIVERSAL__
249
250 return TRUE;
251}
1e6feb95 252
1cbee0b4
VZ
253int wxAppBase::OnRun()
254{
255 // see the comment in ctor: if the initial value hasn't been changed, use
256 // the default Yes from now on
257 if ( m_exitOnFrameDelete == Later )
258 {
259 m_exitOnFrameDelete = Yes;
260 }
261 //else: it has been changed, assume the user knows what he is doing
262
263 return MainLoop();
264}
265
b913d3ed
VZ
266int wxAppBase::OnExit()
267{
268#ifdef __WXUNIVERSAL__
269 delete wxTheme::Set(NULL);
270#endif // __WXUNIVERSAL__
271
272 return wxAppConsole::OnExit();
273}
274
e2478fde 275void wxAppBase::Exit()
1e6feb95 276{
e2478fde 277 ExitMainLoop();
1e6feb95
VZ
278}
279
e2478fde 280wxAppTraits *wxAppBase::CreateTraits()
a69be60b 281{
7843d11b 282 return new wxGUIAppTraits;
72cdf4c9
VZ
283}
284
1e6feb95
VZ
285// ----------------------------------------------------------------------------
286// misc
287// ----------------------------------------------------------------------------
288
6e169cf3 289void wxAppBase::SetActive(bool active, wxWindow * WXUNUSED(lastFocus))
7beba2fc 290{
66dfed9b
VZ
291 if ( active == m_isActive )
292 return;
293
1e6feb95 294 m_isActive = active;
66dfed9b
VZ
295
296 wxActivateEvent event(wxEVT_ACTIVATE_APP, active);
297 event.SetEventObject(this);
298
299 (void)ProcessEvent(event);
7beba2fc 300}
1e6feb95 301
94826170
VZ
302void wxAppBase::DeletePendingObjects()
303{
222ed1d6 304 wxList::compatibility_iterator node = wxPendingDelete.GetFirst();
94826170
VZ
305 while (node)
306 {
307 wxObject *obj = node->GetData();
308
309 delete obj;
310
311 if (wxPendingDelete.Member(obj))
222ed1d6 312 wxPendingDelete.Erase(node);
94826170
VZ
313
314 // Deleting one object may have deleted other pending
315 // objects, so start from beginning of list again.
316 node = wxPendingDelete.GetFirst();
317 }
318}
319
e39af974
JS
320// Returns TRUE if more time is needed.
321bool wxAppBase::ProcessIdle()
322{
5109ae5d
JS
323 wxIdleEvent event;
324 bool needMore = FALSE;
222ed1d6 325 wxWindowList::compatibility_iterator node = wxTopLevelWindows.GetFirst();
e39af974
JS
326 node = wxTopLevelWindows.GetFirst();
327 while (node)
328 {
329 wxWindow* win = node->GetData();
5109ae5d
JS
330 if (SendIdleEvents(win, event))
331 needMore = TRUE;
e39af974
JS
332 node = node->GetNext();
333 }
334
e39af974 335 event.SetEventObject(this);
5109ae5d
JS
336 (void) ProcessEvent(event);
337 if (event.MoreRequested())
338 needMore = TRUE;
e39af974
JS
339
340 wxUpdateUIEvent::ResetUpdateTime();
341
5109ae5d 342 return needMore;
e39af974
JS
343}
344
e39af974 345// Send idle event to window and all subwindows
5109ae5d 346bool wxAppBase::SendIdleEvents(wxWindow* win, wxIdleEvent& event)
e39af974
JS
347{
348 bool needMore = FALSE;
42d11c8e 349
5109ae5d 350 win->OnInternalIdle();
42d11c8e 351
e39af974
JS
352 if (wxIdleEvent::CanSend(win))
353 {
e39af974
JS
354 event.SetEventObject(win);
355 win->GetEventHandler()->ProcessEvent(event);
356
5109ae5d
JS
357 if (event.MoreRequested())
358 needMore = TRUE;
e39af974 359 }
222ed1d6 360 wxWindowList::compatibility_iterator node = win->GetChildren().GetFirst();
e39af974
JS
361 while ( node )
362 {
529b7f71
JS
363 wxWindow *child = node->GetData();
364 if (SendIdleEvents(child, event))
e39af974
JS
365 needMore = TRUE;
366
367 node = node->GetNext();
368 }
369
370 return needMore;
371}
372
fc7a2a60 373void wxAppBase::OnIdle(wxIdleEvent& WXUNUSED(event))
955a9197
JS
374{
375 // If there are pending events, we must process them: pending events
376 // are either events to the threads other than main or events posted
377 // with wxPostEvent() functions
378 // GRG: I have moved this here so that all pending events are processed
379 // before starting to delete any objects. This behaves better (in
380 // particular, wrt wxPostEvent) and is coherent with wxGTK's current
381 // behaviour. Changed Feb/2000 before 2.1.14
382 ProcessPendingEvents();
383
384 // 'Garbage' collection of windows deleted with Close().
385 DeletePendingObjects();
386
387#if wxUSE_LOG
388 // flush the logged messages if any
389 wxLog::FlushActive();
390#endif // wxUSE_LOG
391
392}
e39af974 393
bf188f1a 394// ----------------------------------------------------------------------------
e2478fde 395// wxGUIAppTraitsBase
bf188f1a
VZ
396// ----------------------------------------------------------------------------
397
bf188f1a 398#if wxUSE_LOG
bf188f1a 399
e2478fde
VZ
400wxLog *wxGUIAppTraitsBase::CreateLogTarget()
401{
402 return new wxLogGui;
bf188f1a
VZ
403}
404
bf188f1a
VZ
405#endif // wxUSE_LOG
406
e2478fde 407wxMessageOutput *wxGUIAppTraitsBase::CreateMessageOutput()
bf188f1a 408{
e2478fde
VZ
409 // The standard way of printing help on command line arguments (app --help)
410 // is (according to common practice):
411 // - console apps: to stderr (on any platform)
412 // - GUI apps: stderr on Unix platforms (!)
413 // message box under Windows and others
414#ifdef __UNIX__
415 return new wxMessageOutputStderr;
416#else // !__UNIX__
417 // wxMessageOutputMessageBox doesn't work under Motif
418 #ifdef __WXMOTIF__
419 return new wxMessageOutputLog;
420 #else
421 return new wxMessageOutputMessageBox;
422 #endif
423#endif // __UNIX__/!__UNIX__
bf188f1a
VZ
424}
425
e2478fde 426#if wxUSE_FONTMAP
bf188f1a 427
e2478fde
VZ
428wxFontMapper *wxGUIAppTraitsBase::CreateFontMapper()
429{
430 return new wxFontMapper;
bf188f1a
VZ
431}
432
e2478fde 433#endif // wxUSE_FONTMAP
bf188f1a 434
f0244295
VZ
435wxRendererNative *wxGUIAppTraitsBase::CreateRenderer()
436{
437 // use the default native renderer by default
438 return NULL;
439}
440
090a6d7a 441#ifdef __WXDEBUG__
e6e6fcc9 442
e2478fde
VZ
443bool wxGUIAppTraitsBase::ShowAssertDialog(const wxString& msg)
444{
445 // under MSW we prefer to use the base class version using ::MessageBox()
446 // even if wxMessageBox() is available because it has less chances to
447 // double fault our app than our wxMessageBox()
448#if defined(__WXMSW__) || !wxUSE_MSGDLG
449 return wxAppTraitsBase::ShowAssertDialog(msg);
450#else // wxUSE_MSGDLG
451 // this message is intentionally not translated -- it is for
452 // developpers only
453 wxString msgDlg(msg);
454 msgDlg += wxT("\nDo you want to stop the program?\n")
455 wxT("You can also choose [Cancel] to suppress ")
456 wxT("further warnings.");
457
458 switch ( wxMessageBox(msgDlg, wxT("wxWindows Debug Alert"),
459 wxYES_NO | wxCANCEL | wxICON_STOP ) )
460 {
461 case wxYES:
462 wxTrap();
463 break;
090a6d7a 464
e2478fde
VZ
465 case wxCANCEL:
466 // no more asserts
467 return true;
a5f1fd3e 468
e2478fde 469 //case wxNO: nothing to do
090a6d7a 470 }
090a6d7a 471
e2478fde
VZ
472 return false;
473#endif // !wxUSE_MSGDLG/wxUSE_MSGDLG
a5f1fd3e
VZ
474}
475
e2478fde
VZ
476#endif // __WXDEBUG__
477
478bool wxGUIAppTraitsBase::HasStderr()
a5f1fd3e 479{
e2478fde
VZ
480 // we consider that under Unix stderr always goes somewhere, even if the
481 // user doesn't always see it under GUI desktops
482#ifdef __UNIX__
483 return true;
a5f1fd3e 484#else
e2478fde 485 return false;
a5f1fd3e 486#endif
a5f1fd3e
VZ
487}
488
e2478fde 489void wxGUIAppTraitsBase::ScheduleForDestroy(wxObject *object)
a5f1fd3e 490{
e2478fde
VZ
491 if ( !wxPendingDelete.Member(object) )
492 wxPendingDelete.Append(object);
a5f1fd3e
VZ
493}
494
e2478fde 495void wxGUIAppTraitsBase::RemoveFromPendingDelete(wxObject *object)
a5f1fd3e 496{
e2478fde 497 wxPendingDelete.DeleteObject(object);
a5f1fd3e
VZ
498}
499