]> git.saurik.com Git - wxWidgets.git/blame - src/common/appcmn.cpp
Resolve '&&' ambiguity (VC++ 6)
[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
c2e45372
VZ
192#define OPTION_THEME "theme"
193#define OPTION_MODE "mode"
b913d3ed
VZ
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,
0d5ab92f 206 NULL,
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,
0d5ab92f 220 NULL,
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
0d5ab92f 229 wxCMD_LINE_DESC_END
b913d3ed
VZ
230 };
231
232 parser.SetDesc(cmdLineGUIDesc);
233}
234
235bool wxAppBase::OnCmdLineParsed(wxCmdLineParser& parser)
236{
237#ifdef __WXUNIVERSAL__
238 wxString themeName;
239 if ( parser.Found(OPTION_THEME, &themeName) )
240 {
241 wxTheme *theme = wxTheme::Create(themeName);
242 if ( !theme )
243 {
244 wxLogError(_("Unsupported theme '%s'."), themeName.c_str());
4629016d 245 return false;
b913d3ed
VZ
246 }
247
248 // Delete the defaultly created theme and set the new theme.
249 delete wxTheme::Get();
250 wxTheme::Set(theme);
251 }
252#endif // __WXUNIVERSAL__
253
254#if defined(__WXMGL__)
255 wxString modeDesc;
256 if ( parser.Found(OPTION_MODE, &modeDesc) )
257 {
258 unsigned w, h, bpp;
259 if ( wxSscanf(modeDesc.c_str(), _T("%ux%u-%u"), &w, &h, &bpp) != 3 )
260 {
261 wxLogError(_("Invalid display mode specification '%s'."), modeDesc.c_str());
4629016d 262 return false;
b913d3ed
VZ
263 }
264
1c53456f 265 if ( !SetDisplayMode(wxVideoMode(w, h, bpp)) )
4629016d 266 return false;
b913d3ed
VZ
267 }
268#endif // __WXMGL__
269
270 return wxAppConsole::OnCmdLineParsed(parser);
271}
272
273#endif // wxUSE_CMDLINE_PARSER
274
94826170
VZ
275// ----------------------------------------------------------------------------
276// OnXXX() hooks
277// ----------------------------------------------------------------------------
278
1e6feb95
VZ
279bool wxAppBase::OnInitGui()
280{
281#ifdef __WXUNIVERSAL__
bf188f1a 282 if ( !wxTheme::Get() && !wxTheme::CreateDefault() )
4629016d 283 return false;
1e6feb95
VZ
284#endif // __WXUNIVERSAL__
285
4629016d 286 return true;
1e6feb95 287}
1e6feb95 288
1cbee0b4
VZ
289int wxAppBase::OnRun()
290{
291 // see the comment in ctor: if the initial value hasn't been changed, use
292 // the default Yes from now on
293 if ( m_exitOnFrameDelete == Later )
294 {
295 m_exitOnFrameDelete = Yes;
296 }
297 //else: it has been changed, assume the user knows what he is doing
298
b46b1d59 299 return wxAppConsole::OnRun();
1cbee0b4
VZ
300}
301
b913d3ed
VZ
302int wxAppBase::OnExit()
303{
304#ifdef __WXUNIVERSAL__
305 delete wxTheme::Set(NULL);
306#endif // __WXUNIVERSAL__
307
308 return wxAppConsole::OnExit();
309}
310
e2478fde 311wxAppTraits *wxAppBase::CreateTraits()
a69be60b 312{
7843d11b 313 return new wxGUIAppTraits;
72cdf4c9
VZ
314}
315
1e6feb95
VZ
316// ----------------------------------------------------------------------------
317// misc
318// ----------------------------------------------------------------------------
319
6e169cf3 320void wxAppBase::SetActive(bool active, wxWindow * WXUNUSED(lastFocus))
7beba2fc 321{
66dfed9b
VZ
322 if ( active == m_isActive )
323 return;
324
1e6feb95 325 m_isActive = active;
66dfed9b
VZ
326
327 wxActivateEvent event(wxEVT_ACTIVATE_APP, active);
328 event.SetEventObject(this);
329
330 (void)ProcessEvent(event);
7beba2fc 331}
1e6feb95 332
2dc62891
VZ
333// ----------------------------------------------------------------------------
334// idle handling
335// ----------------------------------------------------------------------------
336
94826170
VZ
337void wxAppBase::DeletePendingObjects()
338{
222ed1d6 339 wxList::compatibility_iterator node = wxPendingDelete.GetFirst();
94826170
VZ
340 while (node)
341 {
342 wxObject *obj = node->GetData();
343
73865dad
VZ
344 // remove it from the list first so that if we get back here somehow
345 // during the object deletion (e.g. wxYield called from its dtor) we
346 // wouldn't try to delete it the second time
347 if ( wxPendingDelete.Member(obj) )
222ed1d6 348 wxPendingDelete.Erase(node);
94826170 349
73865dad
VZ
350 delete obj;
351
94826170
VZ
352 // Deleting one object may have deleted other pending
353 // objects, so start from beginning of list again.
354 node = wxPendingDelete.GetFirst();
355 }
356}
357
4629016d 358// Returns true if more time is needed.
e39af974
JS
359bool wxAppBase::ProcessIdle()
360{
0728199b
PC
361 // process pending wx events before sending idle events
362 ProcessPendingEvents();
363
5109ae5d 364 wxIdleEvent event;
4629016d 365 bool needMore = false;
222ed1d6 366 wxWindowList::compatibility_iterator node = wxTopLevelWindows.GetFirst();
e39af974
JS
367 while (node)
368 {
369 wxWindow* win = node->GetData();
5109ae5d 370 if (SendIdleEvents(win, event))
4629016d 371 needMore = true;
e39af974
JS
372 node = node->GetNext();
373 }
374
0728199b
PC
375 if (wxAppConsole::ProcessIdle())
376 needMore = true;
377
378 // 'Garbage' collection of windows deleted with Close().
379 DeletePendingObjects();
380
381#if wxUSE_LOG
382 // flush the logged messages if any
383 wxLog::FlushActive();
384#endif
e39af974
JS
385
386 wxUpdateUIEvent::ResetUpdateTime();
4629016d 387
5109ae5d 388 return needMore;
e39af974
JS
389}
390
e39af974 391// Send idle event to window and all subwindows
5109ae5d 392bool wxAppBase::SendIdleEvents(wxWindow* win, wxIdleEvent& event)
e39af974 393{
4629016d 394 bool needMore = false;
42d11c8e 395
5109ae5d 396 win->OnInternalIdle();
42d11c8e 397
b46b1d59
VZ
398 // should we send idle event to this window?
399 if ( wxIdleEvent::GetMode() == wxIDLE_PROCESS_ALL ||
400 win->HasExtraStyle(wxWS_EX_PROCESS_IDLE) )
e39af974 401 {
e39af974
JS
402 event.SetEventObject(win);
403 win->GetEventHandler()->ProcessEvent(event);
404
5109ae5d 405 if (event.MoreRequested())
4629016d 406 needMore = true;
e39af974 407 }
222ed1d6 408 wxWindowList::compatibility_iterator node = win->GetChildren().GetFirst();
e39af974
JS
409 while ( node )
410 {
529b7f71
JS
411 wxWindow *child = node->GetData();
412 if (SendIdleEvents(child, event))
4629016d 413 needMore = true;
e39af974
JS
414
415 node = node->GetNext();
416 }
417
418 return needMore;
419}
420
bf188f1a 421// ----------------------------------------------------------------------------
e2478fde 422// wxGUIAppTraitsBase
bf188f1a
VZ
423// ----------------------------------------------------------------------------
424
bf188f1a 425#if wxUSE_LOG
bf188f1a 426
e2478fde
VZ
427wxLog *wxGUIAppTraitsBase::CreateLogTarget()
428{
4a7ebfc1
DE
429// DE: One day I'll remove this but right now the generic dialog used for this
430// just doesn't work right at all on wxCocoa.
431#if wxUSE_LOGGUI && !defined(__WXCOCOA__)
e2478fde 432 return new wxLogGui;
461dae94 433#else
fa6416df 434 // we must have something!
461dae94
VZ
435 return new wxLogStderr;
436#endif
bf188f1a
VZ
437}
438
bf188f1a
VZ
439#endif // wxUSE_LOG
440
e2478fde 441wxMessageOutput *wxGUIAppTraitsBase::CreateMessageOutput()
bf188f1a 442{
e2478fde
VZ
443 // The standard way of printing help on command line arguments (app --help)
444 // is (according to common practice):
445 // - console apps: to stderr (on any platform)
446 // - GUI apps: stderr on Unix platforms (!)
447 // message box under Windows and others
448#ifdef __UNIX__
449 return new wxMessageOutputStderr;
450#else // !__UNIX__
451 // wxMessageOutputMessageBox doesn't work under Motif
452 #ifdef __WXMOTIF__
453 return new wxMessageOutputLog;
a8ff046b 454 #elif wxUSE_MSGDLG
e2478fde 455 return new wxMessageOutputMessageBox;
a8ff046b
VZ
456 #else
457 return new wxMessageOutputStderr;
e2478fde
VZ
458 #endif
459#endif // __UNIX__/!__UNIX__
bf188f1a
VZ
460}
461
e2478fde 462#if wxUSE_FONTMAP
bf188f1a 463
e2478fde
VZ
464wxFontMapper *wxGUIAppTraitsBase::CreateFontMapper()
465{
466 return new wxFontMapper;
bf188f1a
VZ
467}
468
e2478fde 469#endif // wxUSE_FONTMAP
bf188f1a 470
f0244295
VZ
471wxRendererNative *wxGUIAppTraitsBase::CreateRenderer()
472{
473 // use the default native renderer by default
474 return NULL;
475}
476
090a6d7a 477#ifdef __WXDEBUG__
e6e6fcc9 478
e2478fde
VZ
479bool wxGUIAppTraitsBase::ShowAssertDialog(const wxString& msg)
480{
db9febdf 481#if defined(__WXMSW__) || !wxUSE_MSGDLG
e2478fde
VZ
482 // under MSW we prefer to use the base class version using ::MessageBox()
483 // even if wxMessageBox() is available because it has less chances to
484 // double fault our app than our wxMessageBox()
e2478fde
VZ
485 return wxAppTraitsBase::ShowAssertDialog(msg);
486#else // wxUSE_MSGDLG
db9febdf
RR
487 wxString msgDlg = msg;
488
489#if wxUSE_STACKWALKER
490 // on Unix stack frame generation may take some time, depending on the
491 // size of the executable mainly... warn the user that we are working
492 wxFprintf(stderr, wxT("[Debug] Generating a stack trace... please wait"));
493 fflush(stderr);
494
495 const wxString stackTrace = GetAssertStackTrace();
496 if ( !stackTrace.empty() )
497 msgDlg << _T("\n\nCall stack:\n") << stackTrace;
498#endif // wxUSE_STACKWALKER
499
e2478fde
VZ
500 // this message is intentionally not translated -- it is for
501 // developpers only
e2478fde
VZ
502 msgDlg += wxT("\nDo you want to stop the program?\n")
503 wxT("You can also choose [Cancel] to suppress ")
504 wxT("further warnings.");
505
77ffb593 506 switch ( wxMessageBox(msgDlg, wxT("wxWidgets Debug Alert"),
e2478fde
VZ
507 wxYES_NO | wxCANCEL | wxICON_STOP ) )
508 {
509 case wxYES:
510 wxTrap();
511 break;
090a6d7a 512
e2478fde
VZ
513 case wxCANCEL:
514 // no more asserts
515 return true;
a5f1fd3e 516
e2478fde 517 //case wxNO: nothing to do
090a6d7a 518 }
090a6d7a 519
e2478fde
VZ
520 return false;
521#endif // !wxUSE_MSGDLG/wxUSE_MSGDLG
a5f1fd3e
VZ
522}
523
e2478fde
VZ
524#endif // __WXDEBUG__
525
526bool wxGUIAppTraitsBase::HasStderr()
a5f1fd3e 527{
e2478fde
VZ
528 // we consider that under Unix stderr always goes somewhere, even if the
529 // user doesn't always see it under GUI desktops
530#ifdef __UNIX__
531 return true;
a5f1fd3e 532#else
e2478fde 533 return false;
a5f1fd3e 534#endif
a5f1fd3e
VZ
535}
536
e2478fde 537void wxGUIAppTraitsBase::ScheduleForDestroy(wxObject *object)
a5f1fd3e 538{
e2478fde
VZ
539 if ( !wxPendingDelete.Member(object) )
540 wxPendingDelete.Append(object);
a5f1fd3e
VZ
541}
542
e2478fde 543void wxGUIAppTraitsBase::RemoveFromPendingDelete(wxObject *object)
a5f1fd3e 544{
e2478fde 545 wxPendingDelete.DeleteObject(object);
a5f1fd3e
VZ
546}
547
38bb138f
VS
548#if wxUSE_SOCKETS
549
ae3036a2 550#if defined(__WINDOWS__)
38bb138f 551 #include "wx/msw/gsockmsw.h"
ae3036a2
RN
552#elif defined(__UNIX__) || defined(__DARWIN__) || defined(__OS2__)
553 #include "wx/unix/gsockunx.h"
69aa21ac 554#elif defined(__WXMAC__)
127eab18
WS
555 #include <MacHeaders.c>
556 #define OTUNIXERRORS 1
557 #include <OpenTransport.h>
558 #include <OpenTransportProviders.h>
559 #include <OpenTptInternet.h>
69aa21ac 560
127eab18 561 #include "wx/mac/gsockmac.h"
38bb138f
VS
562#else
563 #error "Must include correct GSocket header here"
564#endif
565
566GSocketGUIFunctionsTable* wxGUIAppTraitsBase::GetSocketGUIFunctionsTable()
567{
27a52ff9
GD
568#if defined(__WXMAC__) && !defined(__DARWIN__)
569 // NB: wxMac CFM does not have any GUI-specific functions in gsocket.c and
14a39351
VS
570 // so it doesn't need this table at all
571 return NULL;
27a52ff9 572#else // !__WXMAC__ || __DARWIN__
1d2ec8e5
DE
573 static GSocketGUIFunctionsTableConcrete table;
574 return &table;
1d2ec8e5 575#endif // !__WXMAC__ || __DARWIN__
38bb138f
VS
576}
577
578#endif