]> git.saurik.com Git - wxWidgets.git/blame - src/common/event.cpp
always properly check if SeekI() calls succeded; this makes CanRead() functions of...
[wxWidgets.git] / src / common / event.cpp
CommitLineData
c801d85f 1/////////////////////////////////////////////////////////////////////////////
c54de5ae 2// Name: src/common/event.cpp
c801d85f
KB
3// Purpose: Event classes
4// Author: Julian Smart
5// Modified by:
6// Created: 01/02/97
7// RCS-ID: $Id$
55d99c7a 8// Copyright: (c) Julian Smart
65571936 9// Licence: wxWindows licence
c801d85f
KB
10/////////////////////////////////////////////////////////////////////////////
11
8e193f38
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
c801d85f
KB
20// For compilers that support precompilation, includes "wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
0b746ba8 24 #pragma hdrstop
c801d85f
KB
25#endif
26
d5da0ce7 27#include "wx/event.h"
937013e0 28#include "wx/evtloop.h"
d5da0ce7 29
c801d85f 30#ifndef WX_PRECOMP
e90c1d2a 31 #include "wx/list.h"
8ecff181 32 #include "wx/app.h"
de6185e2 33 #include "wx/utils.h"
bb90a3e6 34 #include "wx/stopwatch.h"
02761f6c 35 #include "wx/module.h"
e90c1d2a
VZ
36
37 #if wxUSE_GUI
a8ff046b 38 #include "wx/window.h"
e90c1d2a 39 #include "wx/control.h"
e90c1d2a 40 #include "wx/dc.h"
75a29298 41 #include "wx/textctrl.h"
264cb7f5 42 #include "wx/validate.h"
e90c1d2a 43 #endif // wxUSE_GUI
c801d85f
KB
44#endif
45
204abcd4 46#include "wx/thread.h"
47b6fabc 47
d102b02a
VZ
48#if wxUSE_BASE
49 #include "wx/ptr_scpd.h"
50
51 wxDECLARE_SCOPED_PTR(wxEvent, wxEventPtr)
52 wxDEFINE_SCOPED_PTR(wxEvent, wxEventPtr)
53#endif // wxUSE_BASE
204abcd4 54
8e193f38
VZ
55// ----------------------------------------------------------------------------
56// wxWin macros
57// ----------------------------------------------------------------------------
58
3ed2104c
VZ
59#if wxUSE_BASE
60 IMPLEMENT_DYNAMIC_CLASS(wxEvtHandler, wxObject)
61 IMPLEMENT_ABSTRACT_CLASS(wxEvent, wxObject)
b46b1d59 62 IMPLEMENT_DYNAMIC_CLASS(wxIdleEvent, wxEvent)
3ed2104c 63#endif // wxUSE_BASE
e90c1d2a 64
23f681ec
VZ
65#if wxUSE_GUI
66 IMPLEMENT_DYNAMIC_CLASS(wxCommandEvent, wxEvent)
67 IMPLEMENT_DYNAMIC_CLASS(wxNotifyEvent, wxCommandEvent)
68 IMPLEMENT_DYNAMIC_CLASS(wxScrollEvent, wxCommandEvent)
69 IMPLEMENT_DYNAMIC_CLASS(wxScrollWinEvent, wxEvent)
70 IMPLEMENT_DYNAMIC_CLASS(wxMouseEvent, wxEvent)
71 IMPLEMENT_DYNAMIC_CLASS(wxKeyEvent, wxEvent)
72 IMPLEMENT_DYNAMIC_CLASS(wxSizeEvent, wxEvent)
73 IMPLEMENT_DYNAMIC_CLASS(wxPaintEvent, wxEvent)
1e6feb95 74 IMPLEMENT_DYNAMIC_CLASS(wxNcPaintEvent, wxEvent)
23f681ec
VZ
75 IMPLEMENT_DYNAMIC_CLASS(wxEraseEvent, wxEvent)
76 IMPLEMENT_DYNAMIC_CLASS(wxMoveEvent, wxEvent)
77 IMPLEMENT_DYNAMIC_CLASS(wxFocusEvent, wxEvent)
456bc6d9 78 IMPLEMENT_DYNAMIC_CLASS(wxChildFocusEvent, wxCommandEvent)
23f681ec
VZ
79 IMPLEMENT_DYNAMIC_CLASS(wxCloseEvent, wxEvent)
80 IMPLEMENT_DYNAMIC_CLASS(wxShowEvent, wxEvent)
81 IMPLEMENT_DYNAMIC_CLASS(wxMaximizeEvent, wxEvent)
82 IMPLEMENT_DYNAMIC_CLASS(wxIconizeEvent, wxEvent)
83 IMPLEMENT_DYNAMIC_CLASS(wxMenuEvent, wxEvent)
84 IMPLEMENT_DYNAMIC_CLASS(wxJoystickEvent, wxEvent)
85 IMPLEMENT_DYNAMIC_CLASS(wxDropFilesEvent, wxEvent)
86 IMPLEMENT_DYNAMIC_CLASS(wxActivateEvent, wxEvent)
87 IMPLEMENT_DYNAMIC_CLASS(wxInitDialogEvent, wxEvent)
8e72b8b5 88 IMPLEMENT_DYNAMIC_CLASS(wxSetCursorEvent, wxEvent)
23f681ec 89 IMPLEMENT_DYNAMIC_CLASS(wxSysColourChangedEvent, wxEvent)
574c939e 90 IMPLEMENT_DYNAMIC_CLASS(wxDisplayChangedEvent, wxEvent)
23f681ec 91 IMPLEMENT_DYNAMIC_CLASS(wxUpdateUIEvent, wxCommandEvent)
75299490 92 IMPLEMENT_DYNAMIC_CLASS(wxNavigationKeyEvent, wxEvent)
23f681ec
VZ
93 IMPLEMENT_DYNAMIC_CLASS(wxPaletteChangedEvent, wxEvent)
94 IMPLEMENT_DYNAMIC_CLASS(wxQueryNewPaletteEvent, wxEvent)
95 IMPLEMENT_DYNAMIC_CLASS(wxWindowCreateEvent, wxEvent)
96 IMPLEMENT_DYNAMIC_CLASS(wxWindowDestroyEvent, wxEvent)
b96340e6 97 IMPLEMENT_DYNAMIC_CLASS(wxHelpEvent, wxCommandEvent)
69231000 98 IMPLEMENT_DYNAMIC_CLASS(wxContextMenuEvent, wxCommandEvent)
a5e84126 99 IMPLEMENT_DYNAMIC_CLASS(wxMouseCaptureChangedEvent, wxEvent)
63e819f2 100 IMPLEMENT_DYNAMIC_CLASS(wxMouseCaptureLostEvent, wxEvent)
78c91815 101 IMPLEMENT_DYNAMIC_CLASS(wxClipboardTextEvent, wxCommandEvent)
23f681ec 102#endif // wxUSE_GUI
0b746ba8 103
3ed2104c
VZ
104#if wxUSE_BASE
105
23f681ec
VZ
106const wxEventTable *wxEvtHandler::GetEventTable() const
107 { return &wxEvtHandler::sm_eventTable; }
0b746ba8 108
23f681ec
VZ
109const wxEventTable wxEvtHandler::sm_eventTable =
110 { (const wxEventTable *)NULL, &wxEvtHandler::sm_eventTableEntries[0] };
0b746ba8 111
b5a98acd
VZ
112wxEventHashTable &wxEvtHandler::GetEventHashTable() const
113 { return wxEvtHandler::sm_eventHashTable; }
114
115wxEventHashTable wxEvtHandler::sm_eventHashTable(wxEvtHandler::sm_eventTable);
116
23f681ec 117const wxEventTableEntry wxEvtHandler::sm_eventTableEntries[] =
3c778901 118 { DECLARE_EVENT_TABLE_TERMINATOR() };
c801d85f 119
1a18887b 120
a0826b11
VZ
121// wxUSE_MEMORY_TRACING considers memory freed from the static objects dtors
122// leaked, so we need to manually clean up all event tables before checking for
123// the memory leaks when using it, however this breaks re-initializing the
124// library (i.e. repeated calls to wxInitialize/wxUninitialize) because the
125// event tables won't be rebuilt the next time, so disable this by default
aa767a45 126#if defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING
a0826b11 127
afa039f9
JS
128class wxEventTableEntryModule: public wxModule
129{
afa039f9 130public:
a0826b11
VZ
131 wxEventTableEntryModule() { }
132 virtual bool OnInit() { return true; }
133 virtual void OnExit() { wxEventHashTable::ClearAll(); }
134
135 DECLARE_DYNAMIC_CLASS(wxEventTableEntryModule)
afa039f9 136};
a0826b11 137
afa039f9 138IMPLEMENT_DYNAMIC_CLASS(wxEventTableEntryModule, wxModule)
a0826b11
VZ
139
140#endif // __WXDEBUG__ && wxUSE_MEMORY_TRACING
1a18887b 141
8e193f38
VZ
142// ----------------------------------------------------------------------------
143// global variables
144// ----------------------------------------------------------------------------
145
19c4d916
VZ
146// List containing event handlers with pending events (each handler can occur
147// at most once here)
079f4130 148wxList *wxHandlersWithPendingEvents = NULL;
8e193f38 149
7214297d 150#if wxUSE_THREADS
079f4130
FM
151 // protects wxHandlersWithPendingEvents list
152 wxCriticalSection *wxHandlersWithPendingEventsLocker = NULL;
7214297d
GL
153#endif
154
2e4df4bf
VZ
155// common event types are defined here, other event types are defined by the
156// components which use them
b5a98acd 157
c15d9c85
VS
158const wxEventType wxEVT_FIRST = 10000;
159const wxEventType wxEVT_USER_FIRST = wxEVT_FIRST + 2000;
2e4df4bf 160
461697c2 161DEFINE_EVENT_TYPE(wxEVT_NULL)
3c778901 162wxDEFINE_EVENT( wxEVT_IDLE, wxIdleEvent )
886dd7d2 163
886dd7d2
VZ
164#endif // wxUSE_BASE
165
166#if wxUSE_GUI
167
3c778901
VZ
168wxDEFINE_EVENT( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEvent )
169wxDEFINE_EVENT( wxEVT_COMMAND_CHECKBOX_CLICKED, wxCommandEvent )
170wxDEFINE_EVENT( wxEVT_COMMAND_CHOICE_SELECTED, wxCommandEvent )
171wxDEFINE_EVENT( wxEVT_COMMAND_LISTBOX_SELECTED, wxCommandEvent )
172wxDEFINE_EVENT( wxEVT_COMMAND_LISTBOX_DOUBLECLICKED, wxCommandEvent )
173wxDEFINE_EVENT( wxEVT_COMMAND_CHECKLISTBOX_TOGGLED, wxCommandEvent )
174wxDEFINE_EVENT( wxEVT_COMMAND_MENU_SELECTED, wxCommandEvent )
175wxDEFINE_EVENT( wxEVT_COMMAND_SLIDER_UPDATED, wxCommandEvent )
176wxDEFINE_EVENT( wxEVT_COMMAND_RADIOBOX_SELECTED, wxCommandEvent )
177wxDEFINE_EVENT( wxEVT_COMMAND_RADIOBUTTON_SELECTED, wxCommandEvent )
178wxDEFINE_EVENT( wxEVT_COMMAND_SCROLLBAR_UPDATED, wxCommandEvent )
179wxDEFINE_EVENT( wxEVT_COMMAND_VLBOX_SELECTED, wxCommandEvent )
180wxDEFINE_EVENT( wxEVT_COMMAND_COMBOBOX_SELECTED, wxCommandEvent )
181wxDEFINE_EVENT( wxEVT_COMMAND_TOOL_RCLICKED, wxCommandEvent )
182wxDEFINE_EVENT( wxEVT_COMMAND_TOOL_ENTER, wxCommandEvent )
183wxDEFINE_EVENT( wxEVT_COMMAND_SPINCTRL_UPDATED, wxCommandEvent )
184wxDEFINE_EVENT( wxEVT_COMMAND_SPINCTRLDOUBLE_UPDATED, wxCommandEvent )
185wxDEFINE_EVENT( wxEVT_COMMAND_TOOL_DROPDOWN_CLICKED, wxCommandEvent )
0b5eceb6 186
0b5eceb6 187// Mouse event types
3c778901
VZ
188wxDEFINE_EVENT( wxEVT_LEFT_DOWN, wxMouseEvent )
189wxDEFINE_EVENT( wxEVT_LEFT_UP, wxMouseEvent )
190wxDEFINE_EVENT( wxEVT_MIDDLE_DOWN, wxMouseEvent )
191wxDEFINE_EVENT( wxEVT_MIDDLE_UP, wxMouseEvent )
192wxDEFINE_EVENT( wxEVT_RIGHT_DOWN, wxMouseEvent )
193wxDEFINE_EVENT( wxEVT_RIGHT_UP, wxMouseEvent )
194wxDEFINE_EVENT( wxEVT_MOTION, wxMouseEvent )
195wxDEFINE_EVENT( wxEVT_ENTER_WINDOW, wxMouseEvent )
196wxDEFINE_EVENT( wxEVT_LEAVE_WINDOW, wxMouseEvent )
197wxDEFINE_EVENT( wxEVT_LEFT_DCLICK, wxMouseEvent )
198wxDEFINE_EVENT( wxEVT_MIDDLE_DCLICK, wxMouseEvent )
199wxDEFINE_EVENT( wxEVT_RIGHT_DCLICK, wxMouseEvent )
200wxDEFINE_EVENT( wxEVT_SET_FOCUS, wxFocusEvent )
201wxDEFINE_EVENT( wxEVT_KILL_FOCUS, wxFocusEvent )
202wxDEFINE_EVENT( wxEVT_CHILD_FOCUS, wxChildFocusEvent )
203wxDEFINE_EVENT( wxEVT_MOUSEWHEEL, wxMouseEvent )
204wxDEFINE_EVENT( wxEVT_AUX1_DOWN, wxMouseEvent )
205wxDEFINE_EVENT( wxEVT_AUX1_UP, wxMouseEvent )
206wxDEFINE_EVENT( wxEVT_AUX1_DCLICK, wxMouseEvent )
207wxDEFINE_EVENT( wxEVT_AUX2_DOWN, wxMouseEvent )
208wxDEFINE_EVENT( wxEVT_AUX2_UP, wxMouseEvent )
209wxDEFINE_EVENT( wxEVT_AUX2_DCLICK, wxMouseEvent )
0b5eceb6
VZ
210
211// Character input event type
3c778901
VZ
212wxDEFINE_EVENT( wxEVT_CHAR, wxKeyEvent )
213wxDEFINE_EVENT( wxEVT_CHAR_HOOK, wxKeyEvent )
214wxDEFINE_EVENT( wxEVT_NAVIGATION_KEY, wxNavigationKeyEvent )
215wxDEFINE_EVENT( wxEVT_KEY_DOWN, wxKeyEvent )
216wxDEFINE_EVENT( wxEVT_KEY_UP, wxKeyEvent )
5048c832 217#if wxUSE_HOTKEY
3c778901 218wxDEFINE_EVENT( wxEVT_HOTKEY, wxKeyEvent )
5048c832 219#endif
0b5eceb6
VZ
220
221// Set cursor event
3c778901 222wxDEFINE_EVENT( wxEVT_SET_CURSOR, wxSetCursorEvent )
0b5eceb6
VZ
223
224// wxScrollbar and wxSlider event identifiers
3c778901
VZ
225wxDEFINE_EVENT( wxEVT_SCROLL_TOP, wxScrollEvent )
226wxDEFINE_EVENT( wxEVT_SCROLL_BOTTOM, wxScrollEvent )
227wxDEFINE_EVENT( wxEVT_SCROLL_LINEUP, wxScrollEvent )
228wxDEFINE_EVENT( wxEVT_SCROLL_LINEDOWN, wxScrollEvent )
229wxDEFINE_EVENT( wxEVT_SCROLL_PAGEUP, wxScrollEvent )
230wxDEFINE_EVENT( wxEVT_SCROLL_PAGEDOWN, wxScrollEvent )
231wxDEFINE_EVENT( wxEVT_SCROLL_THUMBTRACK, wxScrollEvent )
232wxDEFINE_EVENT( wxEVT_SCROLL_THUMBRELEASE, wxScrollEvent )
233wxDEFINE_EVENT( wxEVT_SCROLL_CHANGED, wxScrollEvent )
0b5eceb6
VZ
234
235// Scroll events from wxWindow
3c778901
VZ
236wxDEFINE_EVENT( wxEVT_SCROLLWIN_TOP, wxScrollWinEvent )
237wxDEFINE_EVENT( wxEVT_SCROLLWIN_BOTTOM, wxScrollWinEvent )
238wxDEFINE_EVENT( wxEVT_SCROLLWIN_LINEUP, wxScrollWinEvent )
239wxDEFINE_EVENT( wxEVT_SCROLLWIN_LINEDOWN, wxScrollWinEvent )
240wxDEFINE_EVENT( wxEVT_SCROLLWIN_PAGEUP, wxScrollWinEvent )
241wxDEFINE_EVENT( wxEVT_SCROLLWIN_PAGEDOWN, wxScrollWinEvent )
242wxDEFINE_EVENT( wxEVT_SCROLLWIN_THUMBTRACK, wxScrollWinEvent )
243wxDEFINE_EVENT( wxEVT_SCROLLWIN_THUMBRELEASE, wxScrollWinEvent )
0b5eceb6
VZ
244
245// System events
3c778901
VZ
246wxDEFINE_EVENT( wxEVT_SIZE, wxSizeEvent )
247wxDEFINE_EVENT( wxEVT_SIZING, wxSizeEvent )
248wxDEFINE_EVENT( wxEVT_MOVE, wxMoveEvent )
249wxDEFINE_EVENT( wxEVT_MOVING, wxMoveEvent )
250wxDEFINE_EVENT( wxEVT_MOVE_START, wxMoveEvent )
251wxDEFINE_EVENT( wxEVT_MOVE_END, wxMoveEvent )
252wxDEFINE_EVENT( wxEVT_CLOSE_WINDOW, wxCloseEvent )
253wxDEFINE_EVENT( wxEVT_END_SESSION, wxCloseEvent )
254wxDEFINE_EVENT( wxEVT_QUERY_END_SESSION, wxCloseEvent )
255wxDEFINE_EVENT( wxEVT_HIBERNATE, wxActivateEvent )
256wxDEFINE_EVENT( wxEVT_ACTIVATE_APP, wxActivateEvent )
257wxDEFINE_EVENT( wxEVT_ACTIVATE, wxActivateEvent )
258wxDEFINE_EVENT( wxEVT_CREATE, wxWindowCreateEvent )
259wxDEFINE_EVENT( wxEVT_DESTROY, wxWindowDestroyEvent )
260wxDEFINE_EVENT( wxEVT_SHOW, wxShowEvent )
261wxDEFINE_EVENT( wxEVT_ICONIZE, wxIconizeEvent )
262wxDEFINE_EVENT( wxEVT_MAXIMIZE, wxMaximizeEvent )
263wxDEFINE_EVENT( wxEVT_MOUSE_CAPTURE_CHANGED, wxMouseCaptureChangedEvent )
264wxDEFINE_EVENT( wxEVT_MOUSE_CAPTURE_LOST, wxMouseCaptureLostEvent )
265wxDEFINE_EVENT( wxEVT_PAINT, wxPaintEvent )
266wxDEFINE_EVENT( wxEVT_ERASE_BACKGROUND, wxEraseEvent )
267wxDEFINE_EVENT( wxEVT_NC_PAINT, wxNcPaintEvent )
268wxDEFINE_EVENT( wxEVT_MENU_OPEN, wxMenuEvent )
269wxDEFINE_EVENT( wxEVT_MENU_CLOSE, wxMenuEvent )
270wxDEFINE_EVENT( wxEVT_MENU_HIGHLIGHT, wxMenuEvent )
271wxDEFINE_EVENT( wxEVT_CONTEXT_MENU, wxContextMenuEvent )
272wxDEFINE_EVENT( wxEVT_SYS_COLOUR_CHANGED, wxSysColourChangedEvent )
273wxDEFINE_EVENT( wxEVT_DISPLAY_CHANGED, wxDisplayChangedEvent )
274wxDEFINE_EVENT( wxEVT_QUERY_NEW_PALETTE, wxQueryNewPaletteEvent )
275wxDEFINE_EVENT( wxEVT_PALETTE_CHANGED, wxPaletteChangedEvent )
276wxDEFINE_EVENT( wxEVT_JOY_BUTTON_DOWN, wxJoystickEvent )
277wxDEFINE_EVENT( wxEVT_JOY_BUTTON_UP, wxJoystickEvent )
278wxDEFINE_EVENT( wxEVT_JOY_MOVE, wxJoystickEvent )
279wxDEFINE_EVENT( wxEVT_JOY_ZMOVE, wxJoystickEvent )
280wxDEFINE_EVENT( wxEVT_DROP_FILES, wxDropFilesEvent )
281wxDEFINE_EVENT( wxEVT_INIT_DIALOG, wxInitDialogEvent )
282wxDEFINE_EVENT( wxEVT_UPDATE_UI, wxUpdateUIEvent )
0b5eceb6 283
78c91815 284// Clipboard events
3c778901
VZ
285wxDEFINE_EVENT( wxEVT_COMMAND_TEXT_COPY, wxClipboardTextEvent )
286wxDEFINE_EVENT( wxEVT_COMMAND_TEXT_CUT, wxClipboardTextEvent )
287wxDEFINE_EVENT( wxEVT_COMMAND_TEXT_PASTE, wxClipboardTextEvent )
78c91815 288
0b5eceb6
VZ
289// Generic command events
290// Note: a click is a higher-level event than button down/up
3c778901
VZ
291wxDEFINE_EVENT( wxEVT_COMMAND_LEFT_CLICK, wxCommandEvent )
292wxDEFINE_EVENT( wxEVT_COMMAND_LEFT_DCLICK, wxCommandEvent )
293wxDEFINE_EVENT( wxEVT_COMMAND_RIGHT_CLICK, wxCommandEvent )
294wxDEFINE_EVENT( wxEVT_COMMAND_RIGHT_DCLICK, wxCommandEvent )
295wxDEFINE_EVENT( wxEVT_COMMAND_SET_FOCUS, wxCommandEvent )
296wxDEFINE_EVENT( wxEVT_COMMAND_KILL_FOCUS, wxCommandEvent )
297wxDEFINE_EVENT( wxEVT_COMMAND_ENTER, wxCommandEvent )
0b5eceb6
VZ
298
299// Help events
3c778901
VZ
300wxDEFINE_EVENT( wxEVT_HELP, wxHelpEvent )
301wxDEFINE_EVENT( wxEVT_DETAILED_HELP, wxHelpEvent )
6164f93c 302
886dd7d2
VZ
303#endif // wxUSE_GUI
304
305#if wxUSE_BASE
306
b46b1d59
VZ
307wxIdleMode wxIdleEvent::sm_idleMode = wxIDLE_PROCESS_ALL;
308
6164f93c
VZ
309// ============================================================================
310// implementation
311// ============================================================================
0b5eceb6 312
0b5eceb6
VZ
313// ----------------------------------------------------------------------------
314// event initialization
315// ----------------------------------------------------------------------------
316
317int wxNewEventType()
318{
319 // MT-FIXME
320 static int s_lastUsedEventType = wxEVT_FIRST;
321
322 return s_lastUsedEventType++;
323}
3c778901
VZ
324// ----------------------------------------------------------------------------
325// wxEventFunctor
326// ----------------------------------------------------------------------------
327
328wxEventFunctor::~wxEventFunctor()
329{
330}
0b5eceb6 331
8e193f38
VZ
332// ----------------------------------------------------------------------------
333// wxEvent
334// ----------------------------------------------------------------------------
335
c801d85f 336/*
77ffb593 337 * General wxWidgets events, covering
c801d85f
KB
338 * all interesting things that might happen (button clicking, resizing,
339 * setting text in widgets, etc.).
340 *
341 * For each completely new event type, derive a new event class.
342 *
343 */
344
8e72b8b5 345wxEvent::wxEvent(int theId, wxEventType commandType )
c801d85f 346{
8e72b8b5 347 m_eventType = commandType;
3c778901 348 m_eventObject = NULL;
0b746ba8
VZ
349 m_timeStamp = 0;
350 m_id = theId;
1c624631 351 m_skipped = false;
3c778901 352 m_callbackUserData = NULL;
1c624631 353 m_isCommandEvent = false;
1648d51b 354 m_propagationLevel = wxEVENT_PROPAGATE_NONE;
c801d85f
KB
355}
356
0e097789 357wxEvent::wxEvent(const wxEvent& src)
a34ef99b 358 : wxObject(src)
e6a6feba
GD
359 , m_eventObject(src.m_eventObject)
360 , m_eventType(src.m_eventType)
361 , m_timeStamp(src.m_timeStamp)
362 , m_id(src.m_id)
363 , m_callbackUserData(src.m_callbackUserData)
6fd5903a 364 , m_propagationLevel(src.m_propagationLevel)
e6a6feba
GD
365 , m_skipped(src.m_skipped)
366 , m_isCommandEvent(src.m_isCommandEvent)
a737331d 367{
a737331d
GL
368}
369
0e097789
VZ
370wxEvent& wxEvent::operator=(const wxEvent& src)
371{
372 wxObject::operator=(src);
373
374 m_eventObject = src.m_eventObject;
375 m_eventType = src.m_eventType;
376 m_timeStamp = src.m_timeStamp;
377 m_id = src.m_id;
378 m_callbackUserData = src.m_callbackUserData;
379 m_propagationLevel = src.m_propagationLevel;
380 m_skipped = src.m_skipped;
381 m_isCommandEvent = src.m_isCommandEvent;
382
383 return *this;
384}
385
3ed2104c
VZ
386#endif // wxUSE_BASE
387
e90c1d2a
VZ
388#if wxUSE_GUI
389
c801d85f
KB
390/*
391 * Command events
392 *
393 */
394
21eefb76
VZ
395#ifdef __VISUALC__
396 // 'this' : used in base member initializer list (for m_commandString)
397 #pragma warning(disable:4355)
398#endif
399
7798a18e 400wxCommandEvent::wxCommandEvent(wxEventType commandType, int theId)
b5a98acd 401 : wxEvent(theId, commandType)
c801d85f 402{
3c778901
VZ
403 m_clientData = NULL;
404 m_clientObject = NULL;
0b746ba8
VZ
405 m_extraLong = 0;
406 m_commandInt = 0;
1c624631 407 m_isCommandEvent = true;
1648d51b
VZ
408
409 // the command events are propagated upwards by default
410 m_propagationLevel = wxEVENT_PROPAGATE_MAX;
c801d85f
KB
411}
412
21eefb76
VZ
413#ifdef __VISUALC__
414 #pragma warning(default:4355)
415#endif
416
ab1931f9
KH
417wxString wxCommandEvent::GetString() const
418{
cc6ceca7
VZ
419 if (m_eventType != wxEVT_COMMAND_TEXT_UPDATED || !m_eventObject)
420 {
ab1931f9 421 return m_cmdString;
cc6ceca7 422 }
ab1931f9
KH
423 else
424 {
89438177 425#if wxUSE_TEXTCTRL
ab1931f9 426 wxTextCtrl *txt = wxDynamicCast(m_eventObject, wxTextCtrl);
cc6ceca7 427 if ( txt )
ab1931f9
KH
428 return txt->GetValue();
429 else
89438177 430#endif // wxUSE_TEXTCTRL
ab1931f9
KH
431 return m_cmdString;
432 }
433}
434
0b30bb0b
JS
435/*
436 * UI update events
437 */
438
439#if wxUSE_LONGLONG
e39af974 440wxLongLong wxUpdateUIEvent::sm_lastUpdate = 0;
0b30bb0b
JS
441#endif
442
e39af974
JS
443long wxUpdateUIEvent::sm_updateInterval = 0;
444
445wxUpdateUIMode wxUpdateUIEvent::sm_updateMode = wxUPDATE_UI_PROCESS_ALL;
0b30bb0b
JS
446
447// Can we update?
a7bc03c9 448bool wxUpdateUIEvent::CanUpdate(wxWindowBase *win)
0b30bb0b 449{
e39af974
JS
450 // Don't update if we've switched global updating off
451 // and this window doesn't support updates.
452 if (win &&
453 (GetMode() == wxUPDATE_UI_PROCESS_SPECIFIED &&
454 ((win->GetExtraStyle() & wxWS_EX_PROCESS_UI_UPDATES) == 0)))
1c624631 455 return false;
b5a98acd 456
e39af974 457 if (sm_updateInterval == -1)
1c624631 458 return false;
534b127c
WS
459
460 if (sm_updateInterval == 0)
1c624631 461 return true;
534b127c 462
0b30bb0b 463#if wxUSE_STOPWATCH && wxUSE_LONGLONG
534b127c
WS
464 wxLongLong now = wxGetLocalTimeMillis();
465 if (now > (sm_lastUpdate + sm_updateInterval))
466 {
1c624631 467 return true;
0b30bb0b 468 }
534b127c 469
1c624631 470 return false;
534b127c
WS
471#else
472 // If we don't have wxStopWatch or wxLongLong, we
473 // should err on the safe side and update now anyway.
474 return true;
475#endif
0b30bb0b
JS
476}
477
478// Reset the update time to provide a delay until the next
479// time we should update
480void wxUpdateUIEvent::ResetUpdateTime()
481{
482#if wxUSE_STOPWATCH && wxUSE_LONGLONG
e39af974 483 if (sm_updateInterval > 0)
0b30bb0b
JS
484 {
485 wxLongLong now = wxGetLocalTimeMillis();
e39af974 486 if (now > (sm_lastUpdate + sm_updateInterval))
0b30bb0b 487 {
e39af974 488 sm_lastUpdate = now;
0b30bb0b
JS
489 }
490 }
491#endif
492}
493
c801d85f
KB
494/*
495 * Scroll events
496 */
497
0b746ba8
VZ
498wxScrollEvent::wxScrollEvent(wxEventType commandType,
499 int id,
500 int pos,
501 int orient)
e6a6feba 502 : wxCommandEvent(commandType, id)
c801d85f 503{
0b746ba8
VZ
504 m_extraLong = orient;
505 m_commandInt = pos;
c801d85f
KB
506}
507
d1367c3d
RR
508/*
509 * ScrollWin events
510 */
511
512wxScrollWinEvent::wxScrollWinEvent(wxEventType commandType,
513 int pos,
514 int orient)
d1367c3d 515{
c5b42c87 516 m_eventType = commandType;
d1367c3d
RR
517 m_extraLong = orient;
518 m_commandInt = pos;
519}
520
c801d85f
KB
521/*
522 * Mouse events
523 *
524 */
525
7798a18e 526wxMouseEvent::wxMouseEvent(wxEventType commandType)
c801d85f 527{
0b746ba8 528 m_eventType = commandType;
8866abbb
VZ
529
530 m_x = 0;
531 m_y = 0;
532
1c624631 533 m_leftDown = false;
1c624631 534 m_middleDown = false;
8866abbb 535 m_rightDown = false;
01101e2d
VZ
536 m_aux1Down = false;
537 m_aux2Down = false;
8866abbb 538
8866abbb
VZ
539 m_clickCount = -1;
540
d2c52078
RD
541 m_wheelRotation = 0;
542 m_wheelDelta = 0;
543 m_linesPerAction = 0;
aea37008 544 m_wheelAxis = 0;
c801d85f
KB
545}
546
abcbaea7
VZ
547void wxMouseEvent::Assign(const wxMouseEvent& event)
548{
0e097789 549 wxEvent::operator=(event);
3232da9d
VZ
550
551 // Borland C++ 5.82 doesn't compile an explicit call to an implicitly
552 // defined operator=() so need to do it this way:
553 *static_cast<wxMouseState *>(this) = event;
92309201 554
abcbaea7
VZ
555 m_x = event.m_x;
556 m_y = event.m_y;
557
558 m_leftDown = event.m_leftDown;
559 m_middleDown = event.m_middleDown;
560 m_rightDown = event.m_rightDown;
01101e2d
VZ
561 m_aux1Down = event.m_aux1Down;
562 m_aux2Down = event.m_aux2Down;
abcbaea7 563
abcbaea7
VZ
564 m_wheelRotation = event.m_wheelRotation;
565 m_wheelDelta = event.m_wheelDelta;
566 m_linesPerAction = event.m_linesPerAction;
aea37008 567 m_wheelAxis = event.m_wheelAxis;
abcbaea7
VZ
568}
569
3e89999c 570// return true if was a button dclick event
c801d85f
KB
571bool wxMouseEvent::ButtonDClick(int but) const
572{
0b746ba8
VZ
573 switch (but)
574 {
3e89999c
VZ
575 default:
576 wxFAIL_MSG(wxT("invalid parameter in wxMouseEvent::ButtonDClick"));
577 // fall through
578
579 case wxMOUSE_BTN_ANY:
01101e2d
VZ
580 return (LeftDClick() || MiddleDClick() || RightDClick() ||
581 Aux1DClick() || Aux2DClick());
3e89999c
VZ
582
583 case wxMOUSE_BTN_LEFT:
0b746ba8 584 return LeftDClick();
3e89999c
VZ
585
586 case wxMOUSE_BTN_MIDDLE:
0b746ba8 587 return MiddleDClick();
3e89999c
VZ
588
589 case wxMOUSE_BTN_RIGHT:
0b746ba8 590 return RightDClick();
01101e2d
VZ
591
592 case wxMOUSE_BTN_AUX1:
593 return Aux1DClick();
594
595 case wxMOUSE_BTN_AUX2:
596 return Aux2DClick();
0b746ba8 597 }
c801d85f
KB
598}
599
3e89999c 600// return true if was a button down event
c801d85f
KB
601bool wxMouseEvent::ButtonDown(int but) const
602{
0b746ba8
VZ
603 switch (but)
604 {
3e89999c
VZ
605 default:
606 wxFAIL_MSG(wxT("invalid parameter in wxMouseEvent::ButtonDown"));
607 // fall through
608
609 case wxMOUSE_BTN_ANY:
01101e2d
VZ
610 return (LeftDown() || MiddleDown() || RightDown() ||
611 Aux1Down() || Aux2Down());
3e89999c
VZ
612
613 case wxMOUSE_BTN_LEFT:
0b746ba8 614 return LeftDown();
3e89999c
VZ
615
616 case wxMOUSE_BTN_MIDDLE:
0b746ba8 617 return MiddleDown();
3e89999c
VZ
618
619 case wxMOUSE_BTN_RIGHT:
0b746ba8 620 return RightDown();
01101e2d
VZ
621
622 case wxMOUSE_BTN_AUX1:
623 return Aux1Down();
624
625 case wxMOUSE_BTN_AUX2:
626 return Aux2Down();
0b746ba8 627 }
c801d85f
KB
628}
629
3e89999c 630// return true if was a button up event
c801d85f
KB
631bool wxMouseEvent::ButtonUp(int but) const
632{
1e6feb95
VZ
633 switch (but)
634 {
3e89999c
VZ
635 default:
636 wxFAIL_MSG(wxT("invalid parameter in wxMouseEvent::ButtonUp"));
637 // fall through
638
639 case wxMOUSE_BTN_ANY:
01101e2d
VZ
640 return (LeftUp() || MiddleUp() || RightUp() ||
641 Aux1Up() || Aux2Up());
3e89999c
VZ
642
643 case wxMOUSE_BTN_LEFT:
0b746ba8 644 return LeftUp();
3e89999c
VZ
645
646 case wxMOUSE_BTN_MIDDLE:
0b746ba8 647 return MiddleUp();
3e89999c
VZ
648
649 case wxMOUSE_BTN_RIGHT:
0b746ba8 650 return RightUp();
01101e2d
VZ
651
652 case wxMOUSE_BTN_AUX1:
653 return Aux1Up();
654
655 case wxMOUSE_BTN_AUX2:
656 return Aux2Up();
0b746ba8 657 }
c801d85f
KB
658}
659
3e89999c 660// return true if the given button is currently changing state
c801d85f
KB
661bool wxMouseEvent::Button(int but) const
662{
1e6feb95
VZ
663 switch (but)
664 {
0b746ba8 665 default:
223d09f6 666 wxFAIL_MSG(wxT("invalid parameter in wxMouseEvent::Button"));
3e89999c
VZ
667 // fall through
668
669 case wxMOUSE_BTN_ANY:
670 return ButtonUp(wxMOUSE_BTN_ANY) ||
671 ButtonDown(wxMOUSE_BTN_ANY) ||
672 ButtonDClick(wxMOUSE_BTN_ANY);
673
674 case wxMOUSE_BTN_LEFT:
675 return LeftDown() || LeftUp() || LeftDClick();
676
677 case wxMOUSE_BTN_MIDDLE:
678 return MiddleDown() || MiddleUp() || MiddleDClick();
679
680 case wxMOUSE_BTN_RIGHT:
681 return RightDown() || RightUp() || RightDClick();
01101e2d
VZ
682
683 case wxMOUSE_BTN_AUX1:
684 return Aux1Down() || Aux1Up() || Aux1DClick();
685
686 case wxMOUSE_BTN_AUX2:
687 return Aux2Down() || Aux2Up() || Aux2DClick();
0b746ba8 688 }
c801d85f
KB
689}
690
691bool wxMouseEvent::ButtonIsDown(int but) const
692{
1e6feb95
VZ
693 switch (but)
694 {
3e89999c
VZ
695 default:
696 wxFAIL_MSG(wxT("invalid parameter in wxMouseEvent::ButtonIsDown"));
697 // fall through
698
699 case wxMOUSE_BTN_ANY:
01101e2d 700 return LeftIsDown() || MiddleIsDown() || RightIsDown() || Aux1Down() || Aux2Down();
3e89999c
VZ
701
702 case wxMOUSE_BTN_LEFT:
0b746ba8 703 return LeftIsDown();
3e89999c
VZ
704
705 case wxMOUSE_BTN_MIDDLE:
0b746ba8 706 return MiddleIsDown();
3e89999c
VZ
707
708 case wxMOUSE_BTN_RIGHT:
0b746ba8 709 return RightIsDown();
01101e2d
VZ
710
711 case wxMOUSE_BTN_AUX1:
712 return Aux1IsDown();
713
714 case wxMOUSE_BTN_AUX2:
715 return Aux2IsDown();
0b746ba8 716 }
c801d85f
KB
717}
718
1e6feb95
VZ
719int wxMouseEvent::GetButton() const
720{
01101e2d 721 for ( int i = 1; i < wxMOUSE_BTN_MAX; i++ )
1e6feb95
VZ
722 {
723 if ( Button(i) )
724 {
725 return i;
726 }
727 }
728
3e89999c 729 return wxMOUSE_BTN_NONE;
1e6feb95
VZ
730}
731
c801d85f
KB
732// Find the logical position of the event given the DC
733wxPoint wxMouseEvent::GetLogicalPosition(const wxDC& dc) const
734{
0757d27c
JS
735 wxPoint pt(dc.DeviceToLogicalX(m_x), dc.DeviceToLogicalY(m_y));
736 return pt;
c801d85f
KB
737}
738
739
740/*
8e72b8b5 741 * Keyboard event
c801d85f
KB
742 *
743 */
744
7798a18e 745wxKeyEvent::wxKeyEvent(wxEventType type)
c801d85f 746{
0b746ba8 747 m_eventType = type;
0b746ba8 748 m_keyCode = 0;
b0e813a0 749 m_scanCode = 0;
2b5f62a0
VZ
750#if wxUSE_UNICODE
751 m_uniChar = 0;
752#endif
c801d85f
KB
753}
754
92309201 755wxKeyEvent::wxKeyEvent(const wxKeyEvent& evt)
0e097789
VZ
756 : wxEvent(evt),
757 wxKeyboardState(evt)
2b5f62a0
VZ
758{
759 m_x = evt.m_x;
760 m_y = evt.m_y;
761
762 m_keyCode = evt.m_keyCode;
763
2b5f62a0
VZ
764 m_scanCode = evt.m_scanCode;
765 m_rawCode = evt.m_rawCode;
766 m_rawFlags = evt.m_rawFlags;
92309201 767
2b5f62a0
VZ
768#if wxUSE_UNICODE
769 m_uniChar = evt.m_uniChar;
770#endif
771}
12a3f227 772
42e69d6b 773wxWindowCreateEvent::wxWindowCreateEvent(wxWindow *win)
42e69d6b 774{
cfe17b74 775 SetEventType(wxEVT_CREATE);
42e69d6b
VZ
776 SetEventObject(win);
777}
778
779wxWindowDestroyEvent::wxWindowDestroyEvent(wxWindow *win)
42e69d6b 780{
cfe17b74 781 SetEventType(wxEVT_DESTROY);
42e69d6b
VZ
782 SetEventObject(win);
783}
784
456bc6d9
VZ
785wxChildFocusEvent::wxChildFocusEvent(wxWindow *win)
786 : wxCommandEvent(wxEVT_CHILD_FOCUS)
787{
788 SetEventObject(win);
789}
790
b107e8d5
VZ
791// ----------------------------------------------------------------------------
792// wxHelpEvent
793// ----------------------------------------------------------------------------
794
795/* static */
796wxHelpEvent::Origin wxHelpEvent::GuessOrigin(Origin origin)
797{
798 if ( origin == Origin_Unknown )
799 {
800 // assume that the event comes from the help button if it's not from
801 // keyboard and that pressing F1 always results in the help event
802 origin = wxGetKeyState(WXK_F1) ? Origin_Keyboard : Origin_HelpButton;
803 }
804
805 return origin;
806}
807
dd070522
VZ
808#endif // wxUSE_GUI
809
9416681d
VS
810
811#if wxUSE_BASE
812
b5a98acd
VZ
813// ----------------------------------------------------------------------------
814// wxEventHashTable
815// ----------------------------------------------------------------------------
816
afa039f9
JS
817static const int EVENT_TYPE_TABLE_INIT_SIZE = 31; // Not too big not too small...
818
819wxEventHashTable* wxEventHashTable::sm_first = NULL;
b5a98acd
VZ
820
821wxEventHashTable::wxEventHashTable(const wxEventTable &table)
822 : m_table(table),
1c624631 823 m_rebuildHash(true)
b5a98acd
VZ
824{
825 AllocEventTypeTable(EVENT_TYPE_TABLE_INIT_SIZE);
1a18887b 826
afa039f9
JS
827 m_next = sm_first;
828 if (m_next)
829 m_next->m_previous = this;
830 sm_first = this;
b5a98acd
VZ
831}
832
833wxEventHashTable::~wxEventHashTable()
afa039f9
JS
834{
835 if (m_next)
836 m_next->m_previous = m_previous;
837 if (m_previous)
838 m_previous->m_next = m_next;
839 if (sm_first == this)
840 sm_first = m_next;
1a18887b 841
afa039f9
JS
842 Clear();
843}
844
845void wxEventHashTable::Clear()
b5a98acd 846{
a0826b11 847 for ( size_t i = 0; i < m_size; i++ )
b5a98acd
VZ
848 {
849 EventTypeTablePointer eTTnode = m_eventTypeTable[i];
a0826b11 850 delete eTTnode;
b5a98acd
VZ
851 }
852
a0826b11 853 delete[] m_eventTypeTable;
afa039f9 854 m_eventTypeTable = NULL;
a0826b11 855
afa039f9
JS
856 m_size = 0;
857}
858
aa767a45 859#if defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING
a0826b11 860
afa039f9
JS
861// Clear all tables
862void wxEventHashTable::ClearAll()
863{
864 wxEventHashTable* table = sm_first;
865 while (table)
866 {
867 table->Clear();
868 table = table->m_next;
869 }
b5a98acd
VZ
870}
871
a0826b11
VZ
872#endif // __WXDEBUG__ && wxUSE_MEMORY_TRACING
873
b5a98acd
VZ
874bool wxEventHashTable::HandleEvent(wxEvent &event, wxEvtHandler *self)
875{
876 if (m_rebuildHash)
877 {
878 InitHashTable();
1c624631 879 m_rebuildHash = false;
b5a98acd 880 }
1a18887b 881
afa039f9 882 if (!m_eventTypeTable)
1a18887b 883 return false;
b5a98acd
VZ
884
885 // Find all entries for the given event type.
886 wxEventType eventType = event.GetEventType();
887 const EventTypeTablePointer eTTnode = m_eventTypeTable[eventType % m_size];
888 if (eTTnode && eTTnode->eventType == eventType)
889 {
890 // Now start the search for an event handler
891 // that can handle an event with the given ID.
1c624631
VZ
892 const wxEventTableEntryPointerArray&
893 eventEntryTable = eTTnode->eventEntryTable;
b5a98acd 894
1c624631
VZ
895 const size_t count = eventEntryTable.GetCount();
896 for (size_t n = 0; n < count; n++)
b5a98acd 897 {
03f3617b
VZ
898 const wxEventTableEntry& entry = *eventEntryTable[n];
899 if ( wxEvtHandler::ProcessEventIfMatchesId(entry, self, event) )
1c624631 900 return true;
b5a98acd 901 }
b5a98acd
VZ
902 }
903
1c624631 904 return false;
b5a98acd
VZ
905}
906
907void wxEventHashTable::InitHashTable()
908{
909 // Loop over the event tables and all its base tables.
910 const wxEventTable *table = &m_table;
911 while (table)
912 {
913 // Retrieve all valid event handler entries
914 const wxEventTableEntry *entry = table->entries;
915 while (entry->m_fn != 0)
916 {
917 // Add the event entry in the Hash.
918 AddEntry(*entry);
919
920 entry++;
921 }
922
923 table = table->baseTable;
924 }
925
926 // Lets free some memory.
927 size_t i;
928 for(i = 0; i < m_size; i++)
929 {
930 EventTypeTablePointer eTTnode = m_eventTypeTable[i];
931 if (eTTnode)
932 {
933 eTTnode->eventEntryTable.Shrink();
934 }
935 }
936}
937
938void wxEventHashTable::AddEntry(const wxEventTableEntry &entry)
939{
afa039f9
JS
940 // This might happen 'accidentally' as the app is exiting
941 if (!m_eventTypeTable)
942 return;
1a18887b 943
b5a98acd
VZ
944 EventTypeTablePointer *peTTnode = &m_eventTypeTable[entry.m_eventType % m_size];
945 EventTypeTablePointer eTTnode = *peTTnode;
946
947 if (eTTnode)
948 {
949 if (eTTnode->eventType != entry.m_eventType)
950 {
951 // Resize the table!
952 GrowEventTypeTable();
953 // Try again to add it.
954 AddEntry(entry);
955 return;
956 }
957 }
958 else
959 {
960 eTTnode = new EventTypeTable;
961 eTTnode->eventType = entry.m_eventType;
962 *peTTnode = eTTnode;
963 }
964
965 // Fill all hash entries between entry.m_id and entry.m_lastId...
966 eTTnode->eventEntryTable.Add(&entry);
967}
968
969void wxEventHashTable::AllocEventTypeTable(size_t size)
970{
971 m_eventTypeTable = new EventTypeTablePointer[size];
972 memset((void *)m_eventTypeTable, 0, sizeof(EventTypeTablePointer)*size);
973 m_size = size;
974}
975
976void wxEventHashTable::GrowEventTypeTable()
977{
978 size_t oldSize = m_size;
979 EventTypeTablePointer *oldEventTypeTable = m_eventTypeTable;
980
981 // TODO: Search the most optimal grow sequence
982 AllocEventTypeTable(/* GetNextPrime(oldSize) */oldSize*2+1);
983
984 for ( size_t i = 0; i < oldSize; /* */ )
985 {
986 EventTypeTablePointer eTToldNode = oldEventTypeTable[i];
987 if (eTToldNode)
988 {
989 EventTypeTablePointer *peTTnode = &m_eventTypeTable[eTToldNode->eventType % m_size];
990 EventTypeTablePointer eTTnode = *peTTnode;
991
992 // Check for collision, we don't want any.
993 if (eTTnode)
994 {
995 GrowEventTypeTable();
996 continue; // Don't increment the counter,
997 // as we still need to add this element.
998 }
999 else
1000 {
1001 // Get the old value and put it in the new table.
1002 *peTTnode = oldEventTypeTable[i];
1003 }
1004 }
1005
1006 i++;
1007 }
1008
1009 delete[] oldEventTypeTable;
1010}
1011
456bc6d9
VZ
1012// ----------------------------------------------------------------------------
1013// wxEvtHandler
1014// ----------------------------------------------------------------------------
1015
c801d85f
KB
1016/*
1017 * Event handler
1018 */
1019
0b746ba8 1020wxEvtHandler::wxEvtHandler()
c801d85f 1021{
3c778901
VZ
1022 m_nextHandler = NULL;
1023 m_previousHandler = NULL;
1c624631 1024 m_enabled = true;
3c778901
VZ
1025 m_dynamicEvents = NULL;
1026 m_pendingEvents = NULL;
aa767a45 1027
b88c44e7
RD
1028 // no client data (yet)
1029 m_clientData = NULL;
1030 m_clientDataType = wxClientData_None;
c801d85f
KB
1031}
1032
0b746ba8 1033wxEvtHandler::~wxEvtHandler()
c801d85f 1034{
0b746ba8
VZ
1035 // Takes itself out of the list of handlers
1036 if (m_previousHandler)
1037 m_previousHandler->m_nextHandler = m_nextHandler;
1038
1039 if (m_nextHandler)
1040 m_nextHandler->m_previousHandler = m_previousHandler;
1041
1042 if (m_dynamicEvents)
fe71f65c 1043 {
3c448b2e
VZ
1044 for ( wxList::iterator it = m_dynamicEvents->begin(),
1045 end = m_dynamicEvents->end();
1046 it != end;
1047 ++it )
0b746ba8 1048 {
df5168c4 1049 wxDynamicEventTableEntry *entry = (wxDynamicEventTableEntry*)*it;
2e4df4bf 1050
cc6ceca7 1051 // Remove ourselves from sink destructor notifications
3c778901
VZ
1052 // (this has usually been done, in wxTrackable destructor)
1053 wxEvtHandler *eventSink = entry->m_fn->GetHandler();
cc6ceca7 1054 if ( eventSink )
2ef989c9 1055 {
cc6ceca7
VZ
1056 wxEventConnectionRef * const
1057 evtConnRef = FindRefInTrackerList(eventSink);
1058 if ( evtConnRef )
1059 {
1060 eventSink->RemoveNode(evtConnRef);
1061 delete evtConnRef;
1062 }
2ef989c9 1063 }
cc6ceca7 1064
3c778901 1065 delete entry->m_callbackUserData;
0b746ba8 1066 delete entry;
0b746ba8
VZ
1067 }
1068 delete m_dynamicEvents;
1069 };
7214297d 1070
5875d39c
VZ
1071 if (m_pendingEvents)
1072 m_pendingEvents->DeleteContents(true);
7ce2fb71 1073 delete m_pendingEvents;
7214297d 1074
079f4130
FM
1075 // Remove us from wxHandlersWithPendingEvents if necessary.
1076 if ( wxHandlersWithPendingEvents )
5d9d1b88 1077 {
364ab6cc 1078#if wxUSE_THREADS
079f4130
FM
1079 if (wxHandlersWithPendingEventsLocker)
1080 wxENTER_CRIT_SECT(*wxHandlersWithPendingEventsLocker);
364ab6cc 1081#endif
19c4d916 1082
079f4130 1083 if ( wxHandlersWithPendingEvents->DeleteObject(this) )
19c4d916
VZ
1084 {
1085 // check that we were present only once in the list
079f4130
FM
1086 wxASSERT_MSG( !wxHandlersWithPendingEvents->Find(this),
1087 "Handler occurs twice in wxHandlersWithPendingEvents list" );
19c4d916
VZ
1088 }
1089 //else: we weren't in this list at all, it's ok
1090
364ab6cc 1091#if wxUSE_THREADS
079f4130
FM
1092 if (wxHandlersWithPendingEventsLocker)
1093 wxLEAVE_CRIT_SECT(*wxHandlersWithPendingEventsLocker);
364ab6cc 1094#endif
083f7497 1095 }
b88c44e7
RD
1096
1097 // we only delete object data, not untyped
1098 if ( m_clientDataType == wxClientData_Object )
1099 delete m_clientObject;
c801d85f
KB
1100}
1101
7214297d 1102#if wxUSE_THREADS
aadbdf11 1103
76bfdc9a 1104bool wxEvtHandler::ProcessThreadEvent(const wxEvent& event)
7214297d 1105{
7214297d 1106 // check that we are really in a child thread
8e193f38
VZ
1107 wxASSERT_MSG( !wxThread::IsMain(),
1108 wxT("use ProcessEvent() in main thread") );
1109
1110 AddPendingEvent(event);
7214297d 1111
1c624631 1112 return true;
8e193f38
VZ
1113}
1114
1115#endif // wxUSE_THREADS
1116
c3f94162 1117void wxEvtHandler::QueueEvent(wxEvent *event)
8e193f38 1118{
c3f94162 1119 wxCHECK_RET( event, "NULL event can't be posted" );
7a5c8ac4 1120
c3f94162 1121 // 1) Add this event to our list of pending events
8ebec7dc 1122 wxENTER_CRIT_SECT( m_pendingEventsLock );
16c1f79c 1123
8e193f38
VZ
1124 if ( !m_pendingEvents )
1125 m_pendingEvents = new wxList;
7214297d 1126
c3f94162 1127 m_pendingEvents->Append(event);
7214297d 1128
16c1f79c
RR
1129 // 2) Add this event handler to list of event handlers that
1130 // have pending events.
cfe17b74 1131
079f4130 1132 wxENTER_CRIT_SECT(*wxHandlersWithPendingEventsLocker);
72cdf4c9 1133
079f4130
FM
1134 if ( !wxHandlersWithPendingEvents )
1135 wxHandlersWithPendingEvents = new wxList;
1136 if ( !wxHandlersWithPendingEvents->Find(this) )
1137 wxHandlersWithPendingEvents->Append(this);
72cdf4c9 1138
079f4130 1139 wxLEAVE_CRIT_SECT(*wxHandlersWithPendingEventsLocker);
6164f93c 1140
a715b990
VZ
1141 // only release m_pendingEventsLock now because otherwise there is a race
1142 // condition as described in the ticket #9093: we could process the event
1143 // just added to m_pendingEvents in our ProcessPendingEvents() below before
079f4130 1144 // we had time to append this pointer to wxHandlersWithPendingEvents list; thus
a715b990
VZ
1145 // breaking the invariant that a handler should be in the list iff it has
1146 // any pending events to process
1147 wxLEAVE_CRIT_SECT( m_pendingEventsLock );
1148
90e572f1 1149 // 3) Inform the system that new pending events are somewhere,
16c1f79c 1150 // and that these should be processed in idle time.
bf9e3e73 1151 wxWakeUpIdle();
7214297d
GL
1152}
1153
1154void wxEvtHandler::ProcessPendingEvents()
1155{
8ebec7dc 1156 wxENTER_CRIT_SECT( m_pendingEventsLock );
8e193f38 1157
7ce2fb71
VZ
1158 // this method is only called by wxApp if this handler does have
1159 // pending events
1160 wxCHECK_RET( m_pendingEvents && !m_pendingEvents->IsEmpty(),
1161 "should have pending events if called" );
5fa150e2 1162
19c4d916 1163 wxList::compatibility_iterator node = m_pendingEvents->GetFirst();
5c33522f 1164 wxEventPtr event(static_cast<wxEvent *>(node->GetData()));
5875d39c 1165
19c4d916
VZ
1166 // it's important we remove event from list before processing it, else a
1167 // nested event loop, for example from a modal dialog, might process the
1168 // same event again.
1169 m_pendingEvents->Erase(node);
7b0d5c59 1170
19c4d916
VZ
1171 // if there are no more pending events left, we don't need to stay in this
1172 // list
1173 if ( m_pendingEvents->IsEmpty() )
1512f0f7
VZ
1174 {
1175#if wxUSE_THREADS
079f4130
FM
1176 if (wxHandlersWithPendingEventsLocker)
1177 wxENTER_CRIT_SECT(*wxHandlersWithPendingEventsLocker);
1512f0f7 1178#endif
079f4130 1179 wxHandlersWithPendingEvents->DeleteObject(this);
1512f0f7 1180#if wxUSE_THREADS
079f4130
FM
1181 if (wxHandlersWithPendingEventsLocker)
1182 wxLEAVE_CRIT_SECT(*wxHandlersWithPendingEventsLocker);
1512f0f7
VZ
1183#endif
1184 }
5fa150e2 1185
8ebec7dc 1186 wxLEAVE_CRIT_SECT( m_pendingEventsLock );
7b0d5c59 1187
19c4d916 1188 ProcessEvent(*event);
cfe17b74 1189
19c4d916
VZ
1190 // careful: this object could have been deleted by the event handler
1191 // executed by the above ProcessEvent() call, so we can't access any fields
1192 // of this object any more
7214297d 1193}
7214297d 1194
c801d85f
KB
1195/*
1196 * Event table stuff
1197 */
1c624631 1198/* static */ bool
03f3617b
VZ
1199wxEvtHandler::ProcessEventIfMatchesId(const wxEventTableEntryBase& entry,
1200 wxEvtHandler *handler,
1201 wxEvent& event)
1c624631
VZ
1202{
1203 int tableId1 = entry.m_id,
1204 tableId2 = entry.m_lastId;
1205
1206 // match only if the event type is the same and the id is either -1 in
1207 // the event table (meaning "any") or the event id matches the id
1208 // specified in the event table either exactly or by falling into
1209 // range between first and last
1a18887b
WS
1210 if ((tableId1 == wxID_ANY) ||
1211 (tableId2 == wxID_ANY && tableId1 == event.GetId()) ||
1212 (tableId2 != wxID_ANY &&
1c624631
VZ
1213 (event.GetId() >= tableId1 && event.GetId() <= tableId2)))
1214 {
1215 event.Skip(false);
1216 event.m_callbackUserData = entry.m_callbackUserData;
1217
1218#if wxUSE_EXCEPTIONS
1219 if ( wxTheApp )
1220 {
1221 // call the handler via wxApp method which allows the user to catch
1222 // any exceptions which may be thrown by any handler in the program
1223 // in one place
3c778901 1224 wxTheApp->CallEventHandler(handler, *entry.m_fn, event);
1c624631
VZ
1225 }
1226 else
8bd2a804 1227#endif // wxUSE_EXCEPTIONS
1c624631 1228 {
3c778901 1229 (*entry.m_fn)(handler, event);
1c624631 1230 }
1c624631
VZ
1231
1232 if (!event.GetSkipped())
1233 return true;
1234 }
1235
1236 return false;
1237}
c801d85f 1238
4caf847c 1239bool wxEvtHandler::TryParent(wxEvent& event)
c801d85f 1240{
358e9f2f
VZ
1241 if ( GetNextHandler() )
1242 {
1243 // the next handler will pass it to wxTheApp if it doesn't process it,
1244 // so return from here to avoid doing it again
1245 return GetNextHandler()->TryParent(event);
1246 }
1247
4caf847c 1248 if ( wxTheApp && (this != wxTheApp) )
9154d8cf 1249 {
4caf847c
VZ
1250 // Special case: don't pass wxEVT_IDLE to wxApp, since it'll always
1251 // swallow it. wxEVT_IDLE is sent explicitly to wxApp so it will be
1252 // processed appropriately via SearchEventTable.
1253 if ( event.GetEventType() != wxEVT_IDLE )
1254 {
1255 if ( wxTheApp->ProcessEvent(event) )
1c624631 1256 return true;
4caf847c 1257 }
9154d8cf
VZ
1258 }
1259
1c624631 1260 return false;
4caf847c 1261}
0b746ba8 1262
4caf847c
VZ
1263bool wxEvtHandler::ProcessEvent(wxEvent& event)
1264{
9154d8cf
VZ
1265 // allow the application to hook into event processing
1266 if ( wxTheApp )
1267 {
1268 int rc = wxTheApp->FilterEvent(event);
1269 if ( rc != -1 )
1270 {
1271 wxASSERT_MSG( rc == 1 || rc == 0,
1272 _T("unexpected wxApp::FilterEvent return value") );
1273
1274 return rc != 0;
1275 }
1276 //else: proceed normally
1277 }
1278
25250d33
VZ
1279 if ( ProcessEventHere(event) )
1280 return true;
1281
1282 // propagate the event upwards the window chain and/or to the application
c9c1faa4 1283 // object if it wasn't processed at this level
25250d33
VZ
1284 return TryParent(event);
1285}
1286
1287bool wxEvtHandler::ProcessEventHere(wxEvent& event)
1288{
8e193f38 1289 // An event handler can be enabled or disabled
0b746ba8 1290 if ( GetEvtHandlerEnabled() )
c801d85f 1291 {
22c2307c
VZ
1292 // if we have a validator, it has higher priority than our own event
1293 // table
4caf847c 1294 if ( TryValidator(event) )
1c624631 1295 return true;
4caf847c 1296
22c2307c
VZ
1297 // Handle per-instance dynamic event tables first
1298 if ( m_dynamicEvents && SearchDynamicEventTable(event) )
1c624631 1299 return true;
0b746ba8 1300
b5a98acd
VZ
1301 // Then static per-class event tables
1302 if ( GetEventHashTable().HandleEvent(event, this) )
1c624631 1303 return true;
c801d85f
KB
1304 }
1305
0b746ba8 1306 // Try going down the event handler chain
25250d33
VZ
1307 if ( GetNextHandler() && GetNextHandler()->ProcessEventHere(event) )
1308 return true;
c801d85f 1309
25250d33
VZ
1310 // We don't have a handler for this event.
1311 return false;
c801d85f
KB
1312}
1313
937013e0
VS
1314bool wxEvtHandler::SafelyProcessEvent(wxEvent& event)
1315{
1316#if wxUSE_EXCEPTIONS
1317 try
1318 {
1319#endif
1320 return ProcessEvent(event);
1321#if wxUSE_EXCEPTIONS
1322 }
1323 catch ( ... )
1324 {
1325 wxEventLoopBase *loop = wxEventLoopBase::GetActive();
1326 try
1327 {
1328 if ( !wxTheApp || !wxTheApp->OnExceptionInMainLoop() )
1329 {
1330 if ( loop )
1331 loop->Exit();
1332 }
1333 //else: continue running current event loop
1334
1335 return false;
1336 }
1337 catch ( ... )
1338 {
1339 // OnExceptionInMainLoop() threw, possibly rethrowing the same
1340 // exception again: very good, but we still need Exit() to
1341 // be called
1342 if ( loop )
1343 loop->Exit();
1344 throw;
1345 }
1346 }
1347#endif // wxUSE_EXCEPTIONS
1348}
1349
b5a98acd 1350
c801d85f
KB
1351bool wxEvtHandler::SearchEventTable(wxEventTable& table, wxEvent& event)
1352{
1c624631 1353 const wxEventType eventType = event.GetEventType();
68662769 1354 for ( int i = 0; table.entries[i].m_fn != 0; i++ )
c801d85f 1355 {
68662769 1356 const wxEventTableEntry& entry = table.entries[i];
68662769 1357 if ( eventType == entry.m_eventType )
0b746ba8 1358 {
03f3617b 1359 if ( ProcessEventIfMatchesId(entry, this, event) )
1c624631 1360 return true;
0b746ba8 1361 }
c801d85f 1362 }
68662769 1363
1c624631 1364 return false;
c801d85f 1365}
97d7bfb8 1366
3c778901 1367void wxEvtHandler::Subscribe( int id, int lastId,
c9c1faa4 1368 wxEventType eventType,
3c778901
VZ
1369 wxEventFunctor *func,
1370 wxObject *userData )
fe71f65c 1371{
0b5eceb6 1372 wxDynamicEventTableEntry *entry =
3c778901 1373 new wxDynamicEventTableEntry(eventType, id, lastId, func, userData);
0b746ba8
VZ
1374
1375 if (!m_dynamicEvents)
1376 m_dynamicEvents = new wxList;
1377
92309201
RD
1378 // Insert at the front of the list so most recent additions are found first
1379 m_dynamicEvents->Insert( (wxObject*) entry );
cc6ceca7
VZ
1380
1381 // Make sure we get to know when a sink is destroyed
3c778901 1382 wxEvtHandler *eventSink = func->GetHandler();
886274c6 1383 if ( eventSink && eventSink != this )
2ef989c9 1384 {
cc6ceca7
VZ
1385 wxEventConnectionRef *evtConnRef = FindRefInTrackerList(eventSink);
1386 if ( evtConnRef )
1387 evtConnRef->IncRef( );
2ef989c9 1388 else
e822d1bd 1389 new wxEventConnectionRef(this, eventSink);
2ef989c9 1390 }
fe71f65c 1391}
97d7bfb8 1392
3c778901
VZ
1393bool
1394wxEvtHandler::Unsubscribe(int id,
1395 int lastId,
1396 wxEventType eventType,
1397 const wxEventFunctor& func,
1398 wxObject *userData)
97d7bfb8
RR
1399{
1400 if (!m_dynamicEvents)
1c624631 1401 return false;
cfe17b74 1402
2ef989c9 1403 // Remove connection from tracker node (wxEventConnectionRef)
3c778901 1404 wxEvtHandler *eventSink = func.GetHandler();
886274c6 1405 if ( eventSink && eventSink != this )
2ef989c9 1406 {
cc6ceca7
VZ
1407 wxEventConnectionRef *evtConnRef = FindRefInTrackerList(eventSink);
1408 if ( evtConnRef )
1409 evtConnRef->DecRef();
2ef989c9
RR
1410 }
1411
df5168c4 1412 wxList::compatibility_iterator node = m_dynamicEvents->GetFirst();
97d7bfb8
RR
1413 while (node)
1414 {
ad684023 1415 wxDynamicEventTableEntry *entry = (wxDynamicEventTableEntry*)node->GetData();
2e4df4bf 1416
97d7bfb8 1417 if ((entry->m_id == id) &&
1a18887b 1418 ((entry->m_lastId == lastId) || (lastId == wxID_ANY)) &&
97d7bfb8 1419 ((entry->m_eventType == eventType) || (eventType == wxEVT_NULL)) &&
03f3617b 1420 entry->m_fn->Matches(func) &&
3c778901 1421 ((entry->m_callbackUserData == userData) || !userData))
97d7bfb8 1422 {
3c778901 1423 delete entry->m_callbackUserData;
df5168c4 1424 m_dynamicEvents->Erase( node );
97d7bfb8 1425 delete entry;
1c624631 1426 return true;
97d7bfb8 1427 }
b1d4dd7a 1428 node = node->GetNext();
97d7bfb8 1429 }
1c624631 1430 return false;
97d7bfb8 1431}
fe71f65c
RR
1432
1433bool wxEvtHandler::SearchDynamicEventTable( wxEvent& event )
1434{
1c624631 1435 wxCHECK_MSG( m_dynamicEvents, false,
223d09f6 1436 wxT("caller should check that we have dynamic events") );
0b746ba8 1437
df5168c4 1438 wxList::compatibility_iterator node = m_dynamicEvents->GetFirst();
0b746ba8 1439 while (node)
fe71f65c 1440 {
1c624631 1441 wxDynamicEventTableEntry *entry = (wxDynamicEventTableEntry*)node->GetData();
0b746ba8 1442
7fce89b9
VZ
1443 // get next node before (maybe) calling the event handler as it could
1444 // call Disconnect() invalidating the current node
1445 node = node->GetNext();
1446
03f3617b 1447 if ( event.GetEventType() == entry->m_eventType )
0b746ba8 1448 {
03f3617b
VZ
1449 wxEvtHandler *handler = entry->m_fn->GetHandler();
1450 if ( !handler )
1451 handler = this;
1452 if ( ProcessEventIfMatchesId(*entry, handler, event) )
1c624631 1453 return true;
0b746ba8 1454 }
7b678698 1455 }
1c624631
VZ
1456
1457 return false;
4115960d 1458}
fe71f65c 1459
b88c44e7
RD
1460void wxEvtHandler::DoSetClientObject( wxClientData *data )
1461{
1462 wxASSERT_MSG( m_clientDataType != wxClientData_Void,
1463 wxT("can't have both object and void client data") );
1464
1465 if ( m_clientObject )
1466 delete m_clientObject;
1467
1468 m_clientObject = data;
1469 m_clientDataType = wxClientData_Object;
1470}
1471
1472wxClientData *wxEvtHandler::DoGetClientObject() const
1473{
1474 // it's not an error to call GetClientObject() on a window which doesn't
1475 // have client data at all - NULL will be returned
1476 wxASSERT_MSG( m_clientDataType != wxClientData_Void,
1477 wxT("this window doesn't have object client data") );
1478
1479 return m_clientObject;
1480}
1481
1482void wxEvtHandler::DoSetClientData( void *data )
1483{
1484 wxASSERT_MSG( m_clientDataType != wxClientData_Object,
1485 wxT("can't have both object and void client data") );
1486
1487 m_clientData = data;
1488 m_clientDataType = wxClientData_Void;
1489}
1490
1491void *wxEvtHandler::DoGetClientData() const
1492{
1493 // it's not an error to call GetClientData() on a window which doesn't have
1494 // client data at all - NULL will be returned
1495 wxASSERT_MSG( m_clientDataType != wxClientData_Object,
1496 wxT("this window doesn't have void client data") );
1497
1498 return m_clientData;
1499}
1500
cc6ceca7
VZ
1501// A helper to find an wxEventConnectionRef object
1502wxEventConnectionRef *
1503wxEvtHandler::FindRefInTrackerList(wxEvtHandler *eventSink)
2ef989c9 1504{
cc6ceca7 1505 for ( wxTrackerNode *node = eventSink->GetFirst(); node; node = node->m_nxt )
2ef989c9 1506 {
cc6ceca7
VZ
1507 // we only want wxEventConnectionRef nodes here
1508 wxEventConnectionRef *evtConnRef = node->ToEventConnection();
1509 if ( evtConnRef && evtConnRef->m_src == this )
2ef989c9 1510 {
cc6ceca7
VZ
1511 wxASSERT( evtConnRef->m_sink==eventSink );
1512 return evtConnRef;
2ef989c9
RR
1513 }
1514 }
cc6ceca7 1515
2ef989c9
RR
1516 return NULL;
1517}
1518
1519void wxEvtHandler::OnSinkDestroyed( wxEvtHandler *sink )
1520{
1521 wxASSERT(m_dynamicEvents);
cc6ceca7 1522
2ef989c9
RR
1523 // remove all connections with this sink
1524 wxList::compatibility_iterator node = m_dynamicEvents->GetFirst(), node_nxt;
1525 while (node)
1526 {
1527 wxDynamicEventTableEntry *entry = (wxDynamicEventTableEntry*)node->GetData();
1528 node_nxt = node->GetNext();
cc6ceca7 1529
3c778901 1530 if ( entry->m_fn->GetHandler() == sink )
2ef989c9 1531 {
3c778901 1532 delete entry->m_callbackUserData;
2ef989c9
RR
1533 m_dynamicEvents->Erase( node );
1534 delete entry;
1535 }
1536 node = node_nxt;
1537 }
1538}
1539
4caf847c
VZ
1540#endif // wxUSE_BASE
1541
e90c1d2a
VZ
1542#if wxUSE_GUI
1543
e702ff0f
JS
1544// Find a window with the focus, that is also a descendant of the given window.
1545// This is used to determine the window to initially send commands to.
1546wxWindow* wxFindFocusDescendant(wxWindow* ancestor)
1547{
1548 // Process events starting with the window with the focus, if any.
1549 wxWindow* focusWin = wxWindow::FindFocus();
1550 wxWindow* win = focusWin;
1551
1552 // Check if this is a descendant of this frame.
1553 // If not, win will be set to NULL.
1554 while (win)
1555 {
1556 if (win == ancestor)
1557 break;
1558 else
1559 win = win->GetParent();
1560 }
3c778901
VZ
1561 if (win == NULL)
1562 focusWin = NULL;
e702ff0f
JS
1563
1564 return focusWin;
1565}
1566
c4fa5aa7
VZ
1567// ----------------------------------------------------------------------------
1568// wxEventBlocker
1569// ----------------------------------------------------------------------------
1570
1571wxEventBlocker::wxEventBlocker(wxWindow *win, wxEventType type)
1572{
1573 wxCHECK_RET(win, wxT("Null window given to wxEventBlocker"));
1574
1575 m_window = win;
1576
1577 Block(type);
1578 m_window->PushEventHandler(this);
1579}
1580
1581wxEventBlocker::~wxEventBlocker()
1582{
1583 wxEvtHandler *popped = m_window->PopEventHandler(false);
aa767a45 1584 wxCHECK_RET(popped == this,
c4fa5aa7
VZ
1585 wxT("Don't push other event handlers into a window managed by wxEventBlocker!"));
1586}
1587
1588bool wxEventBlocker::ProcessEvent(wxEvent& event)
1589{
1590 // should this event be blocked?
1591 for ( size_t i = 0; i < m_eventsToBlock.size(); i++ )
1592 {
1593 wxEventType t = (wxEventType)m_eventsToBlock[i];
1594 if ( t == wxEVT_ANY || t == event.GetEventType() )
1595 return true; // yes, it should: mark this event as processed
1596 }
1597
1598 return false;
1599}
1600
e90c1d2a 1601#endif // wxUSE_GUI
3c778901 1602