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