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