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