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