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