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