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