]> git.saurik.com Git - wxWidgets.git/blame - src/common/event.cpp
adding OnLaunched
[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"
58cc1587 28#include "wx/eventfilter.h"
937013e0 29#include "wx/evtloop.h"
d5da0ce7 30
c801d85f 31#ifndef WX_PRECOMP
e90c1d2a 32 #include "wx/list.h"
8bd9fa33 33 #include "wx/log.h"
8ecff181 34 #include "wx/app.h"
de6185e2 35 #include "wx/utils.h"
bb90a3e6 36 #include "wx/stopwatch.h"
02761f6c 37 #include "wx/module.h"
e90c1d2a
VZ
38
39 #if wxUSE_GUI
a8ff046b 40 #include "wx/window.h"
e90c1d2a 41 #include "wx/control.h"
e90c1d2a 42 #include "wx/dc.h"
dca94103 43 #include "wx/spinbutt.h"
75a29298 44 #include "wx/textctrl.h"
264cb7f5 45 #include "wx/validate.h"
e90c1d2a 46 #endif // wxUSE_GUI
c801d85f
KB
47#endif
48
204abcd4 49#include "wx/thread.h"
47b6fabc 50
d102b02a 51#if wxUSE_BASE
664e1314 52 #include "wx/scopedptr.h"
d102b02a
VZ
53
54 wxDECLARE_SCOPED_PTR(wxEvent, wxEventPtr)
55 wxDEFINE_SCOPED_PTR(wxEvent, wxEventPtr)
56#endif // wxUSE_BASE
204abcd4 57
8e193f38
VZ
58// ----------------------------------------------------------------------------
59// wxWin macros
60// ----------------------------------------------------------------------------
61
3ed2104c
VZ
62#if wxUSE_BASE
63 IMPLEMENT_DYNAMIC_CLASS(wxEvtHandler, wxObject)
64 IMPLEMENT_ABSTRACT_CLASS(wxEvent, wxObject)
b46b1d59 65 IMPLEMENT_DYNAMIC_CLASS(wxIdleEvent, wxEvent)
c1b293bb 66 IMPLEMENT_DYNAMIC_CLASS(wxThreadEvent, wxEvent)
3ed2104c 67#endif // wxUSE_BASE
e90c1d2a 68
23f681ec
VZ
69#if wxUSE_GUI
70 IMPLEMENT_DYNAMIC_CLASS(wxCommandEvent, wxEvent)
71 IMPLEMENT_DYNAMIC_CLASS(wxNotifyEvent, wxCommandEvent)
72 IMPLEMENT_DYNAMIC_CLASS(wxScrollEvent, wxCommandEvent)
73 IMPLEMENT_DYNAMIC_CLASS(wxScrollWinEvent, wxEvent)
74 IMPLEMENT_DYNAMIC_CLASS(wxMouseEvent, wxEvent)
75 IMPLEMENT_DYNAMIC_CLASS(wxKeyEvent, wxEvent)
76 IMPLEMENT_DYNAMIC_CLASS(wxSizeEvent, wxEvent)
77 IMPLEMENT_DYNAMIC_CLASS(wxPaintEvent, wxEvent)
1e6feb95 78 IMPLEMENT_DYNAMIC_CLASS(wxNcPaintEvent, wxEvent)
23f681ec
VZ
79 IMPLEMENT_DYNAMIC_CLASS(wxEraseEvent, wxEvent)
80 IMPLEMENT_DYNAMIC_CLASS(wxMoveEvent, wxEvent)
81 IMPLEMENT_DYNAMIC_CLASS(wxFocusEvent, wxEvent)
456bc6d9 82 IMPLEMENT_DYNAMIC_CLASS(wxChildFocusEvent, wxCommandEvent)
23f681ec
VZ
83 IMPLEMENT_DYNAMIC_CLASS(wxCloseEvent, wxEvent)
84 IMPLEMENT_DYNAMIC_CLASS(wxShowEvent, wxEvent)
85 IMPLEMENT_DYNAMIC_CLASS(wxMaximizeEvent, wxEvent)
86 IMPLEMENT_DYNAMIC_CLASS(wxIconizeEvent, wxEvent)
87 IMPLEMENT_DYNAMIC_CLASS(wxMenuEvent, wxEvent)
88 IMPLEMENT_DYNAMIC_CLASS(wxJoystickEvent, wxEvent)
89 IMPLEMENT_DYNAMIC_CLASS(wxDropFilesEvent, wxEvent)
90 IMPLEMENT_DYNAMIC_CLASS(wxActivateEvent, wxEvent)
91 IMPLEMENT_DYNAMIC_CLASS(wxInitDialogEvent, wxEvent)
8e72b8b5 92 IMPLEMENT_DYNAMIC_CLASS(wxSetCursorEvent, wxEvent)
23f681ec 93 IMPLEMENT_DYNAMIC_CLASS(wxSysColourChangedEvent, wxEvent)
574c939e 94 IMPLEMENT_DYNAMIC_CLASS(wxDisplayChangedEvent, wxEvent)
23f681ec 95 IMPLEMENT_DYNAMIC_CLASS(wxUpdateUIEvent, wxCommandEvent)
75299490 96 IMPLEMENT_DYNAMIC_CLASS(wxNavigationKeyEvent, wxEvent)
23f681ec
VZ
97 IMPLEMENT_DYNAMIC_CLASS(wxPaletteChangedEvent, wxEvent)
98 IMPLEMENT_DYNAMIC_CLASS(wxQueryNewPaletteEvent, wxEvent)
99 IMPLEMENT_DYNAMIC_CLASS(wxWindowCreateEvent, wxEvent)
100 IMPLEMENT_DYNAMIC_CLASS(wxWindowDestroyEvent, wxEvent)
b96340e6 101 IMPLEMENT_DYNAMIC_CLASS(wxHelpEvent, wxCommandEvent)
69231000 102 IMPLEMENT_DYNAMIC_CLASS(wxContextMenuEvent, wxCommandEvent)
a5e84126 103 IMPLEMENT_DYNAMIC_CLASS(wxMouseCaptureChangedEvent, wxEvent)
63e819f2 104 IMPLEMENT_DYNAMIC_CLASS(wxMouseCaptureLostEvent, wxEvent)
78c91815 105 IMPLEMENT_DYNAMIC_CLASS(wxClipboardTextEvent, wxCommandEvent)
23f681ec 106#endif // wxUSE_GUI
0b746ba8 107
3ed2104c
VZ
108#if wxUSE_BASE
109
23f681ec
VZ
110const wxEventTable *wxEvtHandler::GetEventTable() const
111 { return &wxEvtHandler::sm_eventTable; }
0b746ba8 112
23f681ec
VZ
113const wxEventTable wxEvtHandler::sm_eventTable =
114 { (const wxEventTable *)NULL, &wxEvtHandler::sm_eventTableEntries[0] };
0b746ba8 115
b5a98acd
VZ
116wxEventHashTable &wxEvtHandler::GetEventHashTable() const
117 { return wxEvtHandler::sm_eventHashTable; }
118
119wxEventHashTable wxEvtHandler::sm_eventHashTable(wxEvtHandler::sm_eventTable);
120
23f681ec 121const wxEventTableEntry wxEvtHandler::sm_eventTableEntries[] =
a0e9a5df 122 { wxDECLARE_EVENT_TABLE_TERMINATOR() };
c801d85f 123
1a18887b 124
a0826b11
VZ
125// wxUSE_MEMORY_TRACING considers memory freed from the static objects dtors
126// leaked, so we need to manually clean up all event tables before checking for
127// the memory leaks when using it, however this breaks re-initializing the
128// library (i.e. repeated calls to wxInitialize/wxUninitialize) because the
129// event tables won't be rebuilt the next time, so disable this by default
657a8a35 130#if wxUSE_MEMORY_TRACING
a0826b11 131
afa039f9
JS
132class wxEventTableEntryModule: public wxModule
133{
afa039f9 134public:
a0826b11
VZ
135 wxEventTableEntryModule() { }
136 virtual bool OnInit() { return true; }
137 virtual void OnExit() { wxEventHashTable::ClearAll(); }
138
139 DECLARE_DYNAMIC_CLASS(wxEventTableEntryModule)
afa039f9 140};
a0826b11 141
afa039f9 142IMPLEMENT_DYNAMIC_CLASS(wxEventTableEntryModule, wxModule)
a0826b11 143
657a8a35 144#endif // wxUSE_MEMORY_TRACING
1a18887b 145
8e193f38
VZ
146// ----------------------------------------------------------------------------
147// global variables
148// ----------------------------------------------------------------------------
149
2e4df4bf
VZ
150// common event types are defined here, other event types are defined by the
151// components which use them
b5a98acd 152
c15d9c85
VS
153const wxEventType wxEVT_FIRST = 10000;
154const wxEventType wxEVT_USER_FIRST = wxEVT_FIRST + 2000;
a0e9a5df 155const wxEventType wxEVT_NULL = wxNewEventType();
2e4df4bf 156
9b11752c 157wxDEFINE_EVENT( wxEVT_IDLE, wxIdleEvent );
886dd7d2 158
cf222762 159// Thread and asynchronous call events
c1b293bb 160wxDEFINE_EVENT( wxEVT_THREAD, wxThreadEvent );
cf222762 161wxDEFINE_EVENT( wxEVT_ASYNC_METHOD_CALL, wxAsyncMethodCallEvent );
c1b293bb 162
886dd7d2
VZ
163#endif // wxUSE_BASE
164
165#if wxUSE_GUI
166
ce7fe42e
VZ
167wxDEFINE_EVENT( wxEVT_BUTTON, wxCommandEvent );
168wxDEFINE_EVENT( wxEVT_CHECKBOX, wxCommandEvent );
169wxDEFINE_EVENT( wxEVT_CHOICE, wxCommandEvent );
170wxDEFINE_EVENT( wxEVT_LISTBOX, wxCommandEvent );
171wxDEFINE_EVENT( wxEVT_LISTBOX_DCLICK, wxCommandEvent );
172wxDEFINE_EVENT( wxEVT_CHECKLISTBOX, wxCommandEvent );
173wxDEFINE_EVENT( wxEVT_MENU, wxCommandEvent );
174wxDEFINE_EVENT( wxEVT_SLIDER, wxCommandEvent );
175wxDEFINE_EVENT( wxEVT_RADIOBOX, wxCommandEvent );
176wxDEFINE_EVENT( wxEVT_RADIOBUTTON, wxCommandEvent );
177wxDEFINE_EVENT( wxEVT_SCROLLBAR, wxCommandEvent );
178wxDEFINE_EVENT( wxEVT_VLBOX, wxCommandEvent );
179wxDEFINE_EVENT( wxEVT_COMBOBOX, wxCommandEvent );
180wxDEFINE_EVENT( wxEVT_TOOL_RCLICKED, wxCommandEvent );
181wxDEFINE_EVENT( wxEVT_TOOL_ENTER, wxCommandEvent );
182wxDEFINE_EVENT( wxEVT_SPINCTRL, wxCommandEvent );
183wxDEFINE_EVENT( wxEVT_SPINCTRLDOUBLE, wxCommandEvent );
184wxDEFINE_EVENT( wxEVT_TOOL_DROPDOWN, wxCommandEvent );
185wxDEFINE_EVENT( wxEVT_COMBOBOX_DROPDOWN, wxCommandEvent);
186wxDEFINE_EVENT( wxEVT_COMBOBOX_CLOSEUP, wxCommandEvent);
0b5eceb6 187
0b5eceb6 188// Mouse event types
9b11752c
VZ
189wxDEFINE_EVENT( wxEVT_LEFT_DOWN, wxMouseEvent );
190wxDEFINE_EVENT( wxEVT_LEFT_UP, wxMouseEvent );
191wxDEFINE_EVENT( wxEVT_MIDDLE_DOWN, wxMouseEvent );
192wxDEFINE_EVENT( wxEVT_MIDDLE_UP, wxMouseEvent );
193wxDEFINE_EVENT( wxEVT_RIGHT_DOWN, wxMouseEvent );
194wxDEFINE_EVENT( wxEVT_RIGHT_UP, wxMouseEvent );
195wxDEFINE_EVENT( wxEVT_MOTION, wxMouseEvent );
196wxDEFINE_EVENT( wxEVT_ENTER_WINDOW, wxMouseEvent );
197wxDEFINE_EVENT( wxEVT_LEAVE_WINDOW, wxMouseEvent );
198wxDEFINE_EVENT( wxEVT_LEFT_DCLICK, wxMouseEvent );
199wxDEFINE_EVENT( wxEVT_MIDDLE_DCLICK, wxMouseEvent );
200wxDEFINE_EVENT( wxEVT_RIGHT_DCLICK, wxMouseEvent );
201wxDEFINE_EVENT( wxEVT_SET_FOCUS, wxFocusEvent );
202wxDEFINE_EVENT( wxEVT_KILL_FOCUS, wxFocusEvent );
203wxDEFINE_EVENT( wxEVT_CHILD_FOCUS, wxChildFocusEvent );
204wxDEFINE_EVENT( wxEVT_MOUSEWHEEL, wxMouseEvent );
205wxDEFINE_EVENT( wxEVT_AUX1_DOWN, wxMouseEvent );
206wxDEFINE_EVENT( wxEVT_AUX1_UP, wxMouseEvent );
207wxDEFINE_EVENT( wxEVT_AUX1_DCLICK, wxMouseEvent );
208wxDEFINE_EVENT( wxEVT_AUX2_DOWN, wxMouseEvent );
209wxDEFINE_EVENT( wxEVT_AUX2_UP, wxMouseEvent );
210wxDEFINE_EVENT( wxEVT_AUX2_DCLICK, wxMouseEvent );
0b5eceb6
VZ
211
212// Character input event type
9b11752c 213wxDEFINE_EVENT( wxEVT_CHAR, wxKeyEvent );
1afe4f9b 214wxDEFINE_EVENT( wxEVT_AFTER_CHAR, wxKeyEvent );
9b11752c
VZ
215wxDEFINE_EVENT( wxEVT_CHAR_HOOK, wxKeyEvent );
216wxDEFINE_EVENT( wxEVT_NAVIGATION_KEY, wxNavigationKeyEvent );
217wxDEFINE_EVENT( wxEVT_KEY_DOWN, wxKeyEvent );
218wxDEFINE_EVENT( wxEVT_KEY_UP, wxKeyEvent );
5048c832 219#if wxUSE_HOTKEY
9b11752c 220wxDEFINE_EVENT( wxEVT_HOTKEY, wxKeyEvent );
5048c832 221#endif
0b5eceb6
VZ
222
223// Set cursor event
9b11752c 224wxDEFINE_EVENT( wxEVT_SET_CURSOR, wxSetCursorEvent );
0b5eceb6
VZ
225
226// wxScrollbar and wxSlider event identifiers
9b11752c
VZ
227wxDEFINE_EVENT( wxEVT_SCROLL_TOP, wxScrollEvent );
228wxDEFINE_EVENT( wxEVT_SCROLL_BOTTOM, wxScrollEvent );
229wxDEFINE_EVENT( wxEVT_SCROLL_LINEUP, wxScrollEvent );
230wxDEFINE_EVENT( wxEVT_SCROLL_LINEDOWN, wxScrollEvent );
231wxDEFINE_EVENT( wxEVT_SCROLL_PAGEUP, wxScrollEvent );
232wxDEFINE_EVENT( wxEVT_SCROLL_PAGEDOWN, wxScrollEvent );
233wxDEFINE_EVENT( wxEVT_SCROLL_THUMBTRACK, wxScrollEvent );
234wxDEFINE_EVENT( wxEVT_SCROLL_THUMBRELEASE, wxScrollEvent );
235wxDEFINE_EVENT( wxEVT_SCROLL_CHANGED, wxScrollEvent );
0b5eceb6 236
dca94103
VZ
237// Due to a bug in older wx versions, wxSpinEvents were being sent with type of
238// wxEVT_SCROLL_LINEUP, wxEVT_SCROLL_LINEDOWN and wxEVT_SCROLL_THUMBTRACK. But
239// with the type-safe events in place, these event types are associated with
240// wxScrollEvent. To allow handling of spin events, new event types have been
241// defined in spinbutt.h/spinnbuttcmn.cpp. To maintain backward compatibility
242// the spin event types are being initialized with the scroll event types.
243
244#if wxUSE_SPINBTN
245
9b11752c
VZ
246wxDEFINE_EVENT_ALIAS( wxEVT_SPIN_UP, wxSpinEvent, wxEVT_SCROLL_LINEUP );
247wxDEFINE_EVENT_ALIAS( wxEVT_SPIN_DOWN, wxSpinEvent, wxEVT_SCROLL_LINEDOWN );
248wxDEFINE_EVENT_ALIAS( wxEVT_SPIN, wxSpinEvent, wxEVT_SCROLL_THUMBTRACK );
dca94103
VZ
249
250#endif // wxUSE_SPINBTN
251
0b5eceb6 252// Scroll events from wxWindow
9b11752c
VZ
253wxDEFINE_EVENT( wxEVT_SCROLLWIN_TOP, wxScrollWinEvent );
254wxDEFINE_EVENT( wxEVT_SCROLLWIN_BOTTOM, wxScrollWinEvent );
255wxDEFINE_EVENT( wxEVT_SCROLLWIN_LINEUP, wxScrollWinEvent );
256wxDEFINE_EVENT( wxEVT_SCROLLWIN_LINEDOWN, wxScrollWinEvent );
257wxDEFINE_EVENT( wxEVT_SCROLLWIN_PAGEUP, wxScrollWinEvent );
258wxDEFINE_EVENT( wxEVT_SCROLLWIN_PAGEDOWN, wxScrollWinEvent );
259wxDEFINE_EVENT( wxEVT_SCROLLWIN_THUMBTRACK, wxScrollWinEvent );
260wxDEFINE_EVENT( wxEVT_SCROLLWIN_THUMBRELEASE, wxScrollWinEvent );
0b5eceb6
VZ
261
262// System events
9b11752c
VZ
263wxDEFINE_EVENT( wxEVT_SIZE, wxSizeEvent );
264wxDEFINE_EVENT( wxEVT_SIZING, wxSizeEvent );
265wxDEFINE_EVENT( wxEVT_MOVE, wxMoveEvent );
266wxDEFINE_EVENT( wxEVT_MOVING, wxMoveEvent );
267wxDEFINE_EVENT( wxEVT_MOVE_START, wxMoveEvent );
268wxDEFINE_EVENT( wxEVT_MOVE_END, wxMoveEvent );
269wxDEFINE_EVENT( wxEVT_CLOSE_WINDOW, wxCloseEvent );
270wxDEFINE_EVENT( wxEVT_END_SESSION, wxCloseEvent );
271wxDEFINE_EVENT( wxEVT_QUERY_END_SESSION, wxCloseEvent );
272wxDEFINE_EVENT( wxEVT_HIBERNATE, wxActivateEvent );
273wxDEFINE_EVENT( wxEVT_ACTIVATE_APP, wxActivateEvent );
274wxDEFINE_EVENT( wxEVT_ACTIVATE, wxActivateEvent );
275wxDEFINE_EVENT( wxEVT_CREATE, wxWindowCreateEvent );
276wxDEFINE_EVENT( wxEVT_DESTROY, wxWindowDestroyEvent );
277wxDEFINE_EVENT( wxEVT_SHOW, wxShowEvent );
278wxDEFINE_EVENT( wxEVT_ICONIZE, wxIconizeEvent );
279wxDEFINE_EVENT( wxEVT_MAXIMIZE, wxMaximizeEvent );
280wxDEFINE_EVENT( wxEVT_MOUSE_CAPTURE_CHANGED, wxMouseCaptureChangedEvent );
281wxDEFINE_EVENT( wxEVT_MOUSE_CAPTURE_LOST, wxMouseCaptureLostEvent );
282wxDEFINE_EVENT( wxEVT_PAINT, wxPaintEvent );
283wxDEFINE_EVENT( wxEVT_ERASE_BACKGROUND, wxEraseEvent );
284wxDEFINE_EVENT( wxEVT_NC_PAINT, wxNcPaintEvent );
285wxDEFINE_EVENT( wxEVT_MENU_OPEN, wxMenuEvent );
286wxDEFINE_EVENT( wxEVT_MENU_CLOSE, wxMenuEvent );
287wxDEFINE_EVENT( wxEVT_MENU_HIGHLIGHT, wxMenuEvent );
288wxDEFINE_EVENT( wxEVT_CONTEXT_MENU, wxContextMenuEvent );
289wxDEFINE_EVENT( wxEVT_SYS_COLOUR_CHANGED, wxSysColourChangedEvent );
290wxDEFINE_EVENT( wxEVT_DISPLAY_CHANGED, wxDisplayChangedEvent );
291wxDEFINE_EVENT( wxEVT_QUERY_NEW_PALETTE, wxQueryNewPaletteEvent );
292wxDEFINE_EVENT( wxEVT_PALETTE_CHANGED, wxPaletteChangedEvent );
293wxDEFINE_EVENT( wxEVT_JOY_BUTTON_DOWN, wxJoystickEvent );
294wxDEFINE_EVENT( wxEVT_JOY_BUTTON_UP, wxJoystickEvent );
295wxDEFINE_EVENT( wxEVT_JOY_MOVE, wxJoystickEvent );
296wxDEFINE_EVENT( wxEVT_JOY_ZMOVE, wxJoystickEvent );
297wxDEFINE_EVENT( wxEVT_DROP_FILES, wxDropFilesEvent );
298wxDEFINE_EVENT( wxEVT_INIT_DIALOG, wxInitDialogEvent );
299wxDEFINE_EVENT( wxEVT_UPDATE_UI, wxUpdateUIEvent );
0b5eceb6 300
78c91815 301// Clipboard events
ce7fe42e
VZ
302wxDEFINE_EVENT( wxEVT_TEXT_COPY, wxClipboardTextEvent );
303wxDEFINE_EVENT( wxEVT_TEXT_CUT, wxClipboardTextEvent );
304wxDEFINE_EVENT( wxEVT_TEXT_PASTE, wxClipboardTextEvent );
78c91815 305
0b5eceb6
VZ
306// Generic command events
307// Note: a click is a higher-level event than button down/up
9b11752c
VZ
308wxDEFINE_EVENT( wxEVT_COMMAND_LEFT_CLICK, wxCommandEvent );
309wxDEFINE_EVENT( wxEVT_COMMAND_LEFT_DCLICK, wxCommandEvent );
310wxDEFINE_EVENT( wxEVT_COMMAND_RIGHT_CLICK, wxCommandEvent );
311wxDEFINE_EVENT( wxEVT_COMMAND_RIGHT_DCLICK, wxCommandEvent );
312wxDEFINE_EVENT( wxEVT_COMMAND_SET_FOCUS, wxCommandEvent );
313wxDEFINE_EVENT( wxEVT_COMMAND_KILL_FOCUS, wxCommandEvent );
314wxDEFINE_EVENT( wxEVT_COMMAND_ENTER, wxCommandEvent );
0b5eceb6
VZ
315
316// Help events
9b11752c
VZ
317wxDEFINE_EVENT( wxEVT_HELP, wxHelpEvent );
318wxDEFINE_EVENT( wxEVT_DETAILED_HELP, wxHelpEvent );
6164f93c 319
886dd7d2
VZ
320#endif // wxUSE_GUI
321
322#if wxUSE_BASE
323
b46b1d59
VZ
324wxIdleMode wxIdleEvent::sm_idleMode = wxIDLE_PROCESS_ALL;
325
6164f93c
VZ
326// ============================================================================
327// implementation
328// ============================================================================
0b5eceb6 329
0b5eceb6
VZ
330// ----------------------------------------------------------------------------
331// event initialization
332// ----------------------------------------------------------------------------
333
334int wxNewEventType()
335{
336 // MT-FIXME
337 static int s_lastUsedEventType = wxEVT_FIRST;
338
339 return s_lastUsedEventType++;
340}
3c778901
VZ
341// ----------------------------------------------------------------------------
342// wxEventFunctor
343// ----------------------------------------------------------------------------
344
345wxEventFunctor::~wxEventFunctor()
346{
347}
0b5eceb6 348
8e193f38
VZ
349// ----------------------------------------------------------------------------
350// wxEvent
351// ----------------------------------------------------------------------------
352
c801d85f 353/*
d48b06bd
FM
354 * General wxWidgets events, covering all interesting things that might happen
355 * (button clicking, resizing, setting text in widgets, etc.).
c801d85f
KB
356 *
357 * For each completely new event type, derive a new event class.
358 *
359 */
360
d48b06bd 361wxEvent::wxEvent(int theId, wxEventType commandType)
c801d85f 362{
8e72b8b5 363 m_eventType = commandType;
3c778901 364 m_eventObject = NULL;
0b746ba8
VZ
365 m_timeStamp = 0;
366 m_id = theId;
1c624631 367 m_skipped = false;
3c778901 368 m_callbackUserData = NULL;
bbdee10d 369 m_handlerToProcessOnlyIn = NULL;
1c624631 370 m_isCommandEvent = false;
1648d51b 371 m_propagationLevel = wxEVENT_PROPAGATE_NONE;
5ac5e40e 372 m_propagatedFrom = NULL;
29de6f40 373 m_wasProcessed = false;
3bad8c39 374 m_willBeProcessedAgain = false;
c801d85f
KB
375}
376
0e097789 377wxEvent::wxEvent(const wxEvent& src)
a34ef99b 378 : wxObject(src)
e6a6feba
GD
379 , m_eventObject(src.m_eventObject)
380 , m_eventType(src.m_eventType)
381 , m_timeStamp(src.m_timeStamp)
382 , m_id(src.m_id)
383 , m_callbackUserData(src.m_callbackUserData)
bbdee10d 384 , m_handlerToProcessOnlyIn(NULL)
6fd5903a 385 , m_propagationLevel(src.m_propagationLevel)
5ac5e40e 386 , m_propagatedFrom(NULL)
e6a6feba
GD
387 , m_skipped(src.m_skipped)
388 , m_isCommandEvent(src.m_isCommandEvent)
29de6f40 389 , m_wasProcessed(false)
3bad8c39 390 , m_willBeProcessedAgain(false)
a737331d 391{
a737331d
GL
392}
393
0e097789
VZ
394wxEvent& wxEvent::operator=(const wxEvent& src)
395{
396 wxObject::operator=(src);
397
398 m_eventObject = src.m_eventObject;
399 m_eventType = src.m_eventType;
400 m_timeStamp = src.m_timeStamp;
401 m_id = src.m_id;
402 m_callbackUserData = src.m_callbackUserData;
bbdee10d 403 m_handlerToProcessOnlyIn = NULL;
0e097789 404 m_propagationLevel = src.m_propagationLevel;
5ac5e40e 405 m_propagatedFrom = NULL;
0e097789
VZ
406 m_skipped = src.m_skipped;
407 m_isCommandEvent = src.m_isCommandEvent;
408
bbdee10d 409 // don't change m_wasProcessed
29de6f40 410
3bad8c39
VZ
411 // While the original again could be passed to another handler, this one
412 // isn't going to be processed anywhere else by default.
413 m_willBeProcessedAgain = false;
414
0e097789
VZ
415 return *this;
416}
417
3ed2104c
VZ
418#endif // wxUSE_BASE
419
e90c1d2a
VZ
420#if wxUSE_GUI
421
8e40ed85
FM
422// ----------------------------------------------------------------------------
423// wxCommandEvent
424// ----------------------------------------------------------------------------
c801d85f 425
7798a18e 426wxCommandEvent::wxCommandEvent(wxEventType commandType, int theId)
b5a98acd 427 : wxEvent(theId, commandType)
c801d85f 428{
3c778901
VZ
429 m_clientData = NULL;
430 m_clientObject = NULL;
1c624631 431 m_isCommandEvent = true;
1648d51b
VZ
432
433 // the command events are propagated upwards by default
434 m_propagationLevel = wxEVENT_PROPAGATE_MAX;
c801d85f
KB
435}
436
ab1931f9
KH
437wxString wxCommandEvent::GetString() const
438{
ce7fe42e 439 if (m_eventType != wxEVT_TEXT || !m_eventObject)
cc6ceca7 440 {
ab1931f9 441 return m_cmdString;
cc6ceca7 442 }
ab1931f9
KH
443 else
444 {
89438177 445#if wxUSE_TEXTCTRL
ab1931f9 446 wxTextCtrl *txt = wxDynamicCast(m_eventObject, wxTextCtrl);
cc6ceca7 447 if ( txt )
ab1931f9
KH
448 return txt->GetValue();
449 else
89438177 450#endif // wxUSE_TEXTCTRL
ab1931f9
KH
451 return m_cmdString;
452 }
453}
454
8e40ed85
FM
455// ----------------------------------------------------------------------------
456// wxUpdateUIEvent
457// ----------------------------------------------------------------------------
0b30bb0b
JS
458
459#if wxUSE_LONGLONG
e39af974 460wxLongLong wxUpdateUIEvent::sm_lastUpdate = 0;
0b30bb0b
JS
461#endif
462
e39af974
JS
463long wxUpdateUIEvent::sm_updateInterval = 0;
464
465wxUpdateUIMode wxUpdateUIEvent::sm_updateMode = wxUPDATE_UI_PROCESS_ALL;
0b30bb0b
JS
466
467// Can we update?
a7bc03c9 468bool wxUpdateUIEvent::CanUpdate(wxWindowBase *win)
0b30bb0b 469{
e39af974
JS
470 // Don't update if we've switched global updating off
471 // and this window doesn't support updates.
472 if (win &&
473 (GetMode() == wxUPDATE_UI_PROCESS_SPECIFIED &&
474 ((win->GetExtraStyle() & wxWS_EX_PROCESS_UI_UPDATES) == 0)))
1c624631 475 return false;
6d075845
VZ
476
477 // Don't update children of the hidden windows: this is useless as any
478 // change to their state won't be seen by the user anyhow. Notice that this
479 // argument doesn't apply to the hidden windows (with visible parent)
480 // themselves as they could be shown by their EVT_UPDATE_UI handler.
481 if ( win->GetParent() && !win->GetParent()->IsShownOnScreen() )
482 return false;
b5a98acd 483
e39af974 484 if (sm_updateInterval == -1)
1c624631 485 return false;
534b127c
WS
486
487 if (sm_updateInterval == 0)
1c624631 488 return true;
534b127c 489
0b30bb0b 490#if wxUSE_STOPWATCH && wxUSE_LONGLONG
534b127c
WS
491 wxLongLong now = wxGetLocalTimeMillis();
492 if (now > (sm_lastUpdate + sm_updateInterval))
493 {
1c624631 494 return true;
0b30bb0b 495 }
534b127c 496
1c624631 497 return false;
534b127c
WS
498#else
499 // If we don't have wxStopWatch or wxLongLong, we
500 // should err on the safe side and update now anyway.
501 return true;
502#endif
0b30bb0b
JS
503}
504
505// Reset the update time to provide a delay until the next
506// time we should update
507void wxUpdateUIEvent::ResetUpdateTime()
508{
509#if wxUSE_STOPWATCH && wxUSE_LONGLONG
e39af974 510 if (sm_updateInterval > 0)
0b30bb0b
JS
511 {
512 wxLongLong now = wxGetLocalTimeMillis();
e39af974 513 if (now > (sm_lastUpdate + sm_updateInterval))
0b30bb0b 514 {
e39af974 515 sm_lastUpdate = now;
0b30bb0b
JS
516 }
517 }
518#endif
519}
520
8e40ed85
FM
521// ----------------------------------------------------------------------------
522// wxScrollEvent
523// ----------------------------------------------------------------------------
c801d85f 524
0b746ba8
VZ
525wxScrollEvent::wxScrollEvent(wxEventType commandType,
526 int id,
527 int pos,
528 int orient)
e6a6feba 529 : wxCommandEvent(commandType, id)
c801d85f 530{
0b746ba8
VZ
531 m_extraLong = orient;
532 m_commandInt = pos;
c801d85f
KB
533}
534
8e40ed85
FM
535// ----------------------------------------------------------------------------
536// wxScrollWinEvent
537// ----------------------------------------------------------------------------
d1367c3d
RR
538
539wxScrollWinEvent::wxScrollWinEvent(wxEventType commandType,
540 int pos,
541 int orient)
d1367c3d 542{
c5b42c87 543 m_eventType = commandType;
d1367c3d
RR
544 m_extraLong = orient;
545 m_commandInt = pos;
546}
547
8e40ed85
FM
548// ----------------------------------------------------------------------------
549// wxMouseEvent
550// ----------------------------------------------------------------------------
c801d85f 551
7798a18e 552wxMouseEvent::wxMouseEvent(wxEventType commandType)
c801d85f 553{
0b746ba8 554 m_eventType = commandType;
8866abbb
VZ
555
556 m_x = 0;
557 m_y = 0;
558
1c624631 559 m_leftDown = false;
1c624631 560 m_middleDown = false;
8866abbb 561 m_rightDown = false;
01101e2d
VZ
562 m_aux1Down = false;
563 m_aux2Down = false;
8866abbb 564
8866abbb
VZ
565 m_clickCount = -1;
566
41469c9e 567 m_wheelAxis = wxMOUSE_WHEEL_VERTICAL;
d2c52078
RD
568 m_wheelRotation = 0;
569 m_wheelDelta = 0;
570 m_linesPerAction = 0;
5833988c 571 m_columnsPerAction = 0;
c801d85f
KB
572}
573
abcbaea7
VZ
574void wxMouseEvent::Assign(const wxMouseEvent& event)
575{
0e097789 576 wxEvent::operator=(event);
3232da9d
VZ
577
578 // Borland C++ 5.82 doesn't compile an explicit call to an implicitly
579 // defined operator=() so need to do it this way:
580 *static_cast<wxMouseState *>(this) = event;
92309201 581
abcbaea7
VZ
582 m_x = event.m_x;
583 m_y = event.m_y;
584
585 m_leftDown = event.m_leftDown;
586 m_middleDown = event.m_middleDown;
587 m_rightDown = event.m_rightDown;
01101e2d
VZ
588 m_aux1Down = event.m_aux1Down;
589 m_aux2Down = event.m_aux2Down;
abcbaea7 590
abcbaea7
VZ
591 m_wheelRotation = event.m_wheelRotation;
592 m_wheelDelta = event.m_wheelDelta;
593 m_linesPerAction = event.m_linesPerAction;
5833988c 594 m_columnsPerAction = event.m_columnsPerAction;
aea37008 595 m_wheelAxis = event.m_wheelAxis;
abcbaea7
VZ
596}
597
3e89999c 598// return true if was a button dclick event
c801d85f
KB
599bool wxMouseEvent::ButtonDClick(int but) const
600{
0b746ba8
VZ
601 switch (but)
602 {
3e89999c
VZ
603 default:
604 wxFAIL_MSG(wxT("invalid parameter in wxMouseEvent::ButtonDClick"));
605 // fall through
606
607 case wxMOUSE_BTN_ANY:
01101e2d
VZ
608 return (LeftDClick() || MiddleDClick() || RightDClick() ||
609 Aux1DClick() || Aux2DClick());
3e89999c
VZ
610
611 case wxMOUSE_BTN_LEFT:
0b746ba8 612 return LeftDClick();
3e89999c
VZ
613
614 case wxMOUSE_BTN_MIDDLE:
0b746ba8 615 return MiddleDClick();
3e89999c
VZ
616
617 case wxMOUSE_BTN_RIGHT:
0b746ba8 618 return RightDClick();
01101e2d
VZ
619
620 case wxMOUSE_BTN_AUX1:
621 return Aux1DClick();
622
623 case wxMOUSE_BTN_AUX2:
624 return Aux2DClick();
0b746ba8 625 }
c801d85f
KB
626}
627
3e89999c 628// return true if was a button down event
c801d85f
KB
629bool wxMouseEvent::ButtonDown(int but) const
630{
0b746ba8
VZ
631 switch (but)
632 {
3e89999c
VZ
633 default:
634 wxFAIL_MSG(wxT("invalid parameter in wxMouseEvent::ButtonDown"));
635 // fall through
636
637 case wxMOUSE_BTN_ANY:
01101e2d
VZ
638 return (LeftDown() || MiddleDown() || RightDown() ||
639 Aux1Down() || Aux2Down());
3e89999c
VZ
640
641 case wxMOUSE_BTN_LEFT:
0b746ba8 642 return LeftDown();
3e89999c
VZ
643
644 case wxMOUSE_BTN_MIDDLE:
0b746ba8 645 return MiddleDown();
3e89999c
VZ
646
647 case wxMOUSE_BTN_RIGHT:
0b746ba8 648 return RightDown();
01101e2d
VZ
649
650 case wxMOUSE_BTN_AUX1:
651 return Aux1Down();
652
653 case wxMOUSE_BTN_AUX2:
654 return Aux2Down();
0b746ba8 655 }
c801d85f
KB
656}
657
3e89999c 658// return true if was a button up event
c801d85f
KB
659bool wxMouseEvent::ButtonUp(int but) const
660{
1e6feb95
VZ
661 switch (but)
662 {
3e89999c
VZ
663 default:
664 wxFAIL_MSG(wxT("invalid parameter in wxMouseEvent::ButtonUp"));
665 // fall through
666
667 case wxMOUSE_BTN_ANY:
01101e2d
VZ
668 return (LeftUp() || MiddleUp() || RightUp() ||
669 Aux1Up() || Aux2Up());
3e89999c
VZ
670
671 case wxMOUSE_BTN_LEFT:
0b746ba8 672 return LeftUp();
3e89999c
VZ
673
674 case wxMOUSE_BTN_MIDDLE:
0b746ba8 675 return MiddleUp();
3e89999c
VZ
676
677 case wxMOUSE_BTN_RIGHT:
0b746ba8 678 return RightUp();
01101e2d
VZ
679
680 case wxMOUSE_BTN_AUX1:
681 return Aux1Up();
682
683 case wxMOUSE_BTN_AUX2:
684 return Aux2Up();
0b746ba8 685 }
c801d85f
KB
686}
687
3e89999c 688// return true if the given button is currently changing state
c801d85f
KB
689bool wxMouseEvent::Button(int but) const
690{
1e6feb95
VZ
691 switch (but)
692 {
0b746ba8 693 default:
223d09f6 694 wxFAIL_MSG(wxT("invalid parameter in wxMouseEvent::Button"));
3e89999c
VZ
695 // fall through
696
697 case wxMOUSE_BTN_ANY:
698 return ButtonUp(wxMOUSE_BTN_ANY) ||
699 ButtonDown(wxMOUSE_BTN_ANY) ||
700 ButtonDClick(wxMOUSE_BTN_ANY);
701
702 case wxMOUSE_BTN_LEFT:
703 return LeftDown() || LeftUp() || LeftDClick();
704
705 case wxMOUSE_BTN_MIDDLE:
706 return MiddleDown() || MiddleUp() || MiddleDClick();
707
708 case wxMOUSE_BTN_RIGHT:
709 return RightDown() || RightUp() || RightDClick();
01101e2d
VZ
710
711 case wxMOUSE_BTN_AUX1:
712 return Aux1Down() || Aux1Up() || Aux1DClick();
713
714 case wxMOUSE_BTN_AUX2:
715 return Aux2Down() || Aux2Up() || Aux2DClick();
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
8e40ed85
FM
739// ----------------------------------------------------------------------------
740// wxKeyEvent
741// ----------------------------------------------------------------------------
c801d85f 742
7798a18e 743wxKeyEvent::wxKeyEvent(wxEventType type)
c801d85f 744{
0b746ba8 745 m_eventType = type;
86408a03 746 m_keyCode = WXK_NONE;
2b5f62a0 747#if wxUSE_UNICODE
86408a03 748 m_uniChar = WXK_NONE;
2b5f62a0 749#endif
3a95f73c 750
2f7baaec
VZ
751 m_x =
752 m_y = wxDefaultCoord;
753 m_hasPosition = false;
754
3a95f73c 755 InitPropagation();
c801d85f
KB
756}
757
92309201 758wxKeyEvent::wxKeyEvent(const wxKeyEvent& evt)
0e097789
VZ
759 : wxEvent(evt),
760 wxKeyboardState(evt)
2b5f62a0 761{
dfc7216d 762 DoAssignMembers(evt);
3a95f73c
VZ
763
764 InitPropagation();
2b5f62a0 765}
12a3f227 766
e45d9090
VZ
767wxKeyEvent::wxKeyEvent(wxEventType eventType, const wxKeyEvent& evt)
768 : wxEvent(evt),
769 wxKeyboardState(evt)
770{
771 DoAssignMembers(evt);
772
773 m_eventType = eventType;
3a95f73c
VZ
774
775 InitPropagation();
e45d9090
VZ
776}
777
2f7baaec
VZ
778void wxKeyEvent::InitPositionIfNecessary() const
779{
780 if ( m_hasPosition )
781 return;
782
783 // We're const because we're called from const Get[XY]() methods but we
784 // need to update the "cached" values.
785 wxKeyEvent& self = const_cast<wxKeyEvent&>(*this);
786 self.m_hasPosition = true;
787
788 // The only position we can possibly associate with the keyboard event on
789 // the platforms where it doesn't carry it already is the mouse position.
790 wxGetMousePosition(&self.m_x, &self.m_y);
791
792 // If this event is associated with a window, the position should be in its
793 // client coordinates, but otherwise leave it in screen coordinates as what
794 // else can we use?
795 wxWindow* const win = wxDynamicCast(GetEventObject(), wxWindow);
796 if ( win )
797 win->ScreenToClient(&self.m_x, &self.m_y);
798}
799
800wxCoord wxKeyEvent::GetX() const
801{
802 InitPositionIfNecessary();
803
804 return m_x;
805}
806
807wxCoord wxKeyEvent::GetY() const
808{
809 InitPositionIfNecessary();
810
811 return m_y;
812}
813
7a34307e
VZ
814bool wxKeyEvent::IsKeyInCategory(int category) const
815{
816 switch ( GetKeyCode() )
817 {
818 case WXK_LEFT:
819 case WXK_RIGHT:
820 case WXK_UP:
821 case WXK_DOWN:
822 case WXK_NUMPAD_LEFT:
823 case WXK_NUMPAD_RIGHT:
824 case WXK_NUMPAD_UP:
825 case WXK_NUMPAD_DOWN:
826 return (category & WXK_CATEGORY_ARROW) != 0;
827
828 case WXK_PAGEDOWN:
829 case WXK_END:
830 case WXK_NUMPAD_PAGEUP:
831 case WXK_NUMPAD_PAGEDOWN:
832 return (category & WXK_CATEGORY_PAGING) != 0;
833
834 case WXK_HOME:
835 case WXK_PAGEUP:
836 case WXK_NUMPAD_HOME:
837 case WXK_NUMPAD_END:
838 return (category & WXK_CATEGORY_JUMP) != 0;
839
840 case WXK_TAB:
4f742042 841 case WXK_NUMPAD_TAB:
7a34307e
VZ
842 return (category & WXK_CATEGORY_TAB) != 0;
843
844 case WXK_BACK:
845 case WXK_DELETE:
846 case WXK_NUMPAD_DELETE:
847 return (category & WXK_CATEGORY_CUT) != 0;
848
849 default:
850 return false;
851 }
852}
853
8e40ed85
FM
854// ----------------------------------------------------------------------------
855// wxWindowCreateEvent
856// ----------------------------------------------------------------------------
857
42e69d6b 858wxWindowCreateEvent::wxWindowCreateEvent(wxWindow *win)
42e69d6b 859{
cfe17b74 860 SetEventType(wxEVT_CREATE);
42e69d6b
VZ
861 SetEventObject(win);
862}
863
8e40ed85
FM
864// ----------------------------------------------------------------------------
865// wxWindowDestroyEvent
866// ----------------------------------------------------------------------------
867
42e69d6b 868wxWindowDestroyEvent::wxWindowDestroyEvent(wxWindow *win)
42e69d6b 869{
cfe17b74 870 SetEventType(wxEVT_DESTROY);
42e69d6b
VZ
871 SetEventObject(win);
872}
873
8e40ed85
FM
874// ----------------------------------------------------------------------------
875// wxChildFocusEvent
876// ----------------------------------------------------------------------------
877
456bc6d9
VZ
878wxChildFocusEvent::wxChildFocusEvent(wxWindow *win)
879 : wxCommandEvent(wxEVT_CHILD_FOCUS)
880{
881 SetEventObject(win);
882}
883
b107e8d5
VZ
884// ----------------------------------------------------------------------------
885// wxHelpEvent
886// ----------------------------------------------------------------------------
887
888/* static */
889wxHelpEvent::Origin wxHelpEvent::GuessOrigin(Origin origin)
890{
891 if ( origin == Origin_Unknown )
892 {
893 // assume that the event comes from the help button if it's not from
894 // keyboard and that pressing F1 always results in the help event
895 origin = wxGetKeyState(WXK_F1) ? Origin_Keyboard : Origin_HelpButton;
896 }
897
898 return origin;
899}
900
dd070522
VZ
901#endif // wxUSE_GUI
902
9416681d
VS
903
904#if wxUSE_BASE
905
b5a98acd
VZ
906// ----------------------------------------------------------------------------
907// wxEventHashTable
908// ----------------------------------------------------------------------------
909
afa039f9
JS
910static const int EVENT_TYPE_TABLE_INIT_SIZE = 31; // Not too big not too small...
911
912wxEventHashTable* wxEventHashTable::sm_first = NULL;
b5a98acd
VZ
913
914wxEventHashTable::wxEventHashTable(const wxEventTable &table)
915 : m_table(table),
1c624631 916 m_rebuildHash(true)
b5a98acd
VZ
917{
918 AllocEventTypeTable(EVENT_TYPE_TABLE_INIT_SIZE);
1a18887b 919
afa039f9
JS
920 m_next = sm_first;
921 if (m_next)
922 m_next->m_previous = this;
923 sm_first = this;
b5a98acd
VZ
924}
925
926wxEventHashTable::~wxEventHashTable()
afa039f9
JS
927{
928 if (m_next)
929 m_next->m_previous = m_previous;
930 if (m_previous)
931 m_previous->m_next = m_next;
932 if (sm_first == this)
933 sm_first = m_next;
1a18887b 934
afa039f9
JS
935 Clear();
936}
937
938void wxEventHashTable::Clear()
b5a98acd 939{
a0826b11 940 for ( size_t i = 0; i < m_size; i++ )
b5a98acd
VZ
941 {
942 EventTypeTablePointer eTTnode = m_eventTypeTable[i];
a0826b11 943 delete eTTnode;
b5a98acd
VZ
944 }
945
5276b0a5 946 wxDELETEA(m_eventTypeTable);
a0826b11 947
afa039f9
JS
948 m_size = 0;
949}
950
657a8a35 951#if wxUSE_MEMORY_TRACING
a0826b11 952
afa039f9
JS
953// Clear all tables
954void wxEventHashTable::ClearAll()
955{
956 wxEventHashTable* table = sm_first;
957 while (table)
958 {
959 table->Clear();
960 table = table->m_next;
961 }
b5a98acd
VZ
962}
963
657a8a35 964#endif // wxUSE_MEMORY_TRACING
a0826b11 965
b5a98acd
VZ
966bool wxEventHashTable::HandleEvent(wxEvent &event, wxEvtHandler *self)
967{
968 if (m_rebuildHash)
969 {
970 InitHashTable();
1c624631 971 m_rebuildHash = false;
b5a98acd 972 }
1a18887b 973
afa039f9 974 if (!m_eventTypeTable)
1a18887b 975 return false;
b5a98acd
VZ
976
977 // Find all entries for the given event type.
978 wxEventType eventType = event.GetEventType();
979 const EventTypeTablePointer eTTnode = m_eventTypeTable[eventType % m_size];
980 if (eTTnode && eTTnode->eventType == eventType)
981 {
982 // Now start the search for an event handler
983 // that can handle an event with the given ID.
1c624631
VZ
984 const wxEventTableEntryPointerArray&
985 eventEntryTable = eTTnode->eventEntryTable;
b5a98acd 986
1c624631
VZ
987 const size_t count = eventEntryTable.GetCount();
988 for (size_t n = 0; n < count; n++)
b5a98acd 989 {
03f3617b
VZ
990 const wxEventTableEntry& entry = *eventEntryTable[n];
991 if ( wxEvtHandler::ProcessEventIfMatchesId(entry, self, event) )
1c624631 992 return true;
b5a98acd 993 }
b5a98acd
VZ
994 }
995
1c624631 996 return false;
b5a98acd
VZ
997}
998
999void wxEventHashTable::InitHashTable()
1000{
1001 // Loop over the event tables and all its base tables.
1002 const wxEventTable *table = &m_table;
1003 while (table)
1004 {
1005 // Retrieve all valid event handler entries
1006 const wxEventTableEntry *entry = table->entries;
1007 while (entry->m_fn != 0)
1008 {
1009 // Add the event entry in the Hash.
1010 AddEntry(*entry);
1011
1012 entry++;
1013 }
1014
1015 table = table->baseTable;
1016 }
1017
4c51a665 1018 // Let's free some memory.
b5a98acd
VZ
1019 size_t i;
1020 for(i = 0; i < m_size; i++)
1021 {
1022 EventTypeTablePointer eTTnode = m_eventTypeTable[i];
1023 if (eTTnode)
1024 {
1025 eTTnode->eventEntryTable.Shrink();
1026 }
1027 }
1028}
1029
1030void wxEventHashTable::AddEntry(const wxEventTableEntry &entry)
1031{
afa039f9
JS
1032 // This might happen 'accidentally' as the app is exiting
1033 if (!m_eventTypeTable)
1034 return;
1a18887b 1035
b5a98acd
VZ
1036 EventTypeTablePointer *peTTnode = &m_eventTypeTable[entry.m_eventType % m_size];
1037 EventTypeTablePointer eTTnode = *peTTnode;
1038
1039 if (eTTnode)
1040 {
1041 if (eTTnode->eventType != entry.m_eventType)
1042 {
1043 // Resize the table!
1044 GrowEventTypeTable();
1045 // Try again to add it.
1046 AddEntry(entry);
1047 return;
1048 }
1049 }
1050 else
1051 {
1052 eTTnode = new EventTypeTable;
1053 eTTnode->eventType = entry.m_eventType;
1054 *peTTnode = eTTnode;
1055 }
1056
1057 // Fill all hash entries between entry.m_id and entry.m_lastId...
1058 eTTnode->eventEntryTable.Add(&entry);
1059}
1060
1061void wxEventHashTable::AllocEventTypeTable(size_t size)
1062{
1063 m_eventTypeTable = new EventTypeTablePointer[size];
1064 memset((void *)m_eventTypeTable, 0, sizeof(EventTypeTablePointer)*size);
1065 m_size = size;
1066}
1067
1068void wxEventHashTable::GrowEventTypeTable()
1069{
1070 size_t oldSize = m_size;
1071 EventTypeTablePointer *oldEventTypeTable = m_eventTypeTable;
1072
1073 // TODO: Search the most optimal grow sequence
1074 AllocEventTypeTable(/* GetNextPrime(oldSize) */oldSize*2+1);
1075
1076 for ( size_t i = 0; i < oldSize; /* */ )
1077 {
1078 EventTypeTablePointer eTToldNode = oldEventTypeTable[i];
1079 if (eTToldNode)
1080 {
1081 EventTypeTablePointer *peTTnode = &m_eventTypeTable[eTToldNode->eventType % m_size];
1082 EventTypeTablePointer eTTnode = *peTTnode;
1083
1084 // Check for collision, we don't want any.
1085 if (eTTnode)
1086 {
1087 GrowEventTypeTable();
1088 continue; // Don't increment the counter,
1089 // as we still need to add this element.
1090 }
1091 else
1092 {
1093 // Get the old value and put it in the new table.
1094 *peTTnode = oldEventTypeTable[i];
1095 }
1096 }
1097
1098 i++;
1099 }
1100
1101 delete[] oldEventTypeTable;
1102}
1103
456bc6d9
VZ
1104// ----------------------------------------------------------------------------
1105// wxEvtHandler
1106// ----------------------------------------------------------------------------
1107
0b746ba8 1108wxEvtHandler::wxEvtHandler()
c801d85f 1109{
3c778901
VZ
1110 m_nextHandler = NULL;
1111 m_previousHandler = NULL;
1c624631 1112 m_enabled = true;
3c778901
VZ
1113 m_dynamicEvents = NULL;
1114 m_pendingEvents = NULL;
aa767a45 1115
b88c44e7
RD
1116 // no client data (yet)
1117 m_clientData = NULL;
1118 m_clientDataType = wxClientData_None;
c801d85f
KB
1119}
1120
0b746ba8 1121wxEvtHandler::~wxEvtHandler()
c801d85f 1122{
7f853dd0 1123 Unlink();
0b746ba8
VZ
1124
1125 if (m_dynamicEvents)
fe71f65c 1126 {
3c448b2e
VZ
1127 for ( wxList::iterator it = m_dynamicEvents->begin(),
1128 end = m_dynamicEvents->end();
1129 it != end;
1130 ++it )
0b746ba8 1131 {
df5168c4 1132 wxDynamicEventTableEntry *entry = (wxDynamicEventTableEntry*)*it;
2e4df4bf 1133
cc6ceca7 1134 // Remove ourselves from sink destructor notifications
3c778901 1135 // (this has usually been done, in wxTrackable destructor)
f3ff831f 1136 wxEvtHandler *eventSink = entry->m_fn->GetEvtHandler();
cc6ceca7 1137 if ( eventSink )
2ef989c9 1138 {
cc6ceca7
VZ
1139 wxEventConnectionRef * const
1140 evtConnRef = FindRefInTrackerList(eventSink);
1141 if ( evtConnRef )
1142 {
1143 eventSink->RemoveNode(evtConnRef);
1144 delete evtConnRef;
1145 }
2ef989c9 1146 }
cc6ceca7 1147
3c778901 1148 delete entry->m_callbackUserData;
0b746ba8 1149 delete entry;
0b746ba8
VZ
1150 }
1151 delete m_dynamicEvents;
dde19c21 1152 }
7214297d 1153
dde19c21 1154 // Remove us from the list of the pending events if necessary.
8e40ed85
FM
1155 if (wxTheApp)
1156 wxTheApp->RemovePendingEventHandler(this);
b88c44e7 1157
cae9e7b1
FM
1158 DeletePendingEvents();
1159
b88c44e7
RD
1160 // we only delete object data, not untyped
1161 if ( m_clientDataType == wxClientData_Object )
1162 delete m_clientObject;
c801d85f
KB
1163}
1164
7f853dd0
FM
1165void wxEvtHandler::Unlink()
1166{
1167 // this event handler must take itself out of the chain of handlers:
1168
1169 if (m_previousHandler)
1170 m_previousHandler->SetNextHandler(m_nextHandler);
1171
1172 if (m_nextHandler)
1173 m_nextHandler->SetPreviousHandler(m_previousHandler);
1174
1175 m_nextHandler = NULL;
1176 m_previousHandler = NULL;
1177}
1178
1179bool wxEvtHandler::IsUnlinked() const
1180{
1181 return m_previousHandler == NULL &&
1182 m_nextHandler == NULL;
1183}
1184
58cc1587
VZ
1185wxEventFilter* wxEvtHandler::ms_filterList = NULL;
1186
1187/* static */ void wxEvtHandler::AddFilter(wxEventFilter* filter)
1188{
1189 wxCHECK_RET( filter, "NULL filter" );
1190
1191 filter->m_next = ms_filterList;
1192 ms_filterList = filter;
1193}
1194
1195/* static */ void wxEvtHandler::RemoveFilter(wxEventFilter* filter)
1196{
1197 wxEventFilter* prev = NULL;
1198 for ( wxEventFilter* f = ms_filterList; f; f = f->m_next )
1199 {
1200 if ( f == filter )
1201 {
1202 // Set the previous list element or the list head to the next
1203 // element.
1204 if ( prev )
1205 prev->m_next = f->m_next;
1206 else
1207 ms_filterList = f->m_next;
1208
1209 // Also reset the next pointer in the filter itself just to avoid
1210 // having possibly dangling pointers, even though it's not strictly
1211 // necessary.
1212 f->m_next = NULL;
1213
1214 // Skip the assert below.
1215 return;
1216 }
956b1c85
VZ
1217
1218 prev = f;
58cc1587
VZ
1219 }
1220
1221 wxFAIL_MSG( "Filter not found" );
1222}
1223
7214297d 1224#if wxUSE_THREADS
aadbdf11 1225
76bfdc9a 1226bool wxEvtHandler::ProcessThreadEvent(const wxEvent& event)
7214297d 1227{
7214297d 1228 // check that we are really in a child thread
8e193f38
VZ
1229 wxASSERT_MSG( !wxThread::IsMain(),
1230 wxT("use ProcessEvent() in main thread") );
1231
1232 AddPendingEvent(event);
7214297d 1233
1c624631 1234 return true;
8e193f38
VZ
1235}
1236
1237#endif // wxUSE_THREADS
1238
c3f94162 1239void wxEvtHandler::QueueEvent(wxEvent *event)
8e193f38 1240{
c3f94162 1241 wxCHECK_RET( event, "NULL event can't be posted" );
7a5c8ac4 1242
8e40ed85 1243 if (!wxTheApp)
dde19c21
FM
1244 {
1245 // we need an event loop which manages the list of event handlers with
1246 // pending events... cannot proceed without it!
8e40ed85 1247 wxLogDebug("No application object! Cannot queue this event!");
9f54c829
FM
1248
1249 // anyway delete the given event to avoid memory leaks
1250 delete event;
1251
dde19c21
FM
1252 return;
1253 }
1254
c3f94162 1255 // 1) Add this event to our list of pending events
8ebec7dc 1256 wxENTER_CRIT_SECT( m_pendingEventsLock );
16c1f79c 1257
8e193f38 1258 if ( !m_pendingEvents )
cae9e7b1 1259 m_pendingEvents = new wxList;
7214297d 1260
c3f94162 1261 m_pendingEvents->Append(event);
7214297d 1262
16c1f79c
RR
1263 // 2) Add this event handler to list of event handlers that
1264 // have pending events.
cfe17b74 1265
8e40ed85 1266 wxTheApp->AppendPendingEventHandler(this);
6164f93c 1267
a715b990
VZ
1268 // only release m_pendingEventsLock now because otherwise there is a race
1269 // condition as described in the ticket #9093: we could process the event
1270 // just added to m_pendingEvents in our ProcessPendingEvents() below before
079f4130 1271 // we had time to append this pointer to wxHandlersWithPendingEvents list; thus
a715b990
VZ
1272 // breaking the invariant that a handler should be in the list iff it has
1273 // any pending events to process
1274 wxLEAVE_CRIT_SECT( m_pendingEventsLock );
1275
90e572f1 1276 // 3) Inform the system that new pending events are somewhere,
16c1f79c 1277 // and that these should be processed in idle time.
bf9e3e73 1278 wxWakeUpIdle();
7214297d
GL
1279}
1280
cae9e7b1
FM
1281void wxEvtHandler::DeletePendingEvents()
1282{
1283 if (m_pendingEvents)
1284 m_pendingEvents->DeleteContents(true);
1285 wxDELETE(m_pendingEvents);
1286}
1287
7214297d
GL
1288void wxEvtHandler::ProcessPendingEvents()
1289{
8e40ed85 1290 if (!wxTheApp)
dde19c21
FM
1291 {
1292 // we need an event loop which manages the list of event handlers with
1293 // pending events... cannot proceed without it!
8e40ed85 1294 wxLogDebug("No application object! Cannot process pending events!");
dde19c21
FM
1295 return;
1296 }
1297
d48b06bd
FM
1298 // we need to process only a single pending event in this call because
1299 // each call to ProcessEvent() could result in the destruction of this
1300 // same event handler (see the comment at the end of this function)
1301
8ebec7dc 1302 wxENTER_CRIT_SECT( m_pendingEventsLock );
8e193f38 1303
7ce2fb71
VZ
1304 // this method is only called by wxApp if this handler does have
1305 // pending events
1306 wxCHECK_RET( m_pendingEvents && !m_pendingEvents->IsEmpty(),
1307 "should have pending events if called" );
5fa150e2 1308
19c4d916 1309 wxList::compatibility_iterator node = m_pendingEvents->GetFirst();
d48b06bd
FM
1310 wxEvent* pEvent = static_cast<wxEvent *>(node->GetData());
1311
1312 // find the first event which can be processed now:
dde19c21
FM
1313 wxEventLoopBase* evtLoop = wxEventLoopBase::GetActive();
1314 if (evtLoop && evtLoop->IsYielding())
d48b06bd 1315 {
dde19c21 1316 while (node && pEvent && !evtLoop->IsEventAllowedInsideYield(pEvent->GetEventCategory()))
d48b06bd
FM
1317 {
1318 node = node->GetNext();
1319 pEvent = node ? static_cast<wxEvent *>(node->GetData()) : NULL;
1320 }
1321
1322 if (!node)
1323 {
1324 // all our events are NOT processable now... signal this:
8e40ed85 1325 wxTheApp->DelayPendingEventHandler(this);
dde19c21
FM
1326
1327 // see the comment at the beginning of evtloop.h header for the
1328 // logic behind YieldFor() and behind DelayPendingEventHandler()
1329
d48b06bd
FM
1330 wxLEAVE_CRIT_SECT( m_pendingEventsLock );
1331
1332 return;
1333 }
1334 }
1335
1336 wxEventPtr event(pEvent);
5875d39c 1337
19c4d916
VZ
1338 // it's important we remove event from list before processing it, else a
1339 // nested event loop, for example from a modal dialog, might process the
1340 // same event again.
1341 m_pendingEvents->Erase(node);
7b0d5c59 1342
19c4d916 1343 if ( m_pendingEvents->IsEmpty() )
1512f0f7 1344 {
dde19c21
FM
1345 // if there are no more pending events left, we don't need to
1346 // stay in this list
8e40ed85 1347 wxTheApp->RemovePendingEventHandler(this);
1512f0f7 1348 }
5fa150e2 1349
8ebec7dc 1350 wxLEAVE_CRIT_SECT( m_pendingEventsLock );
7b0d5c59 1351
19c4d916 1352 ProcessEvent(*event);
cfe17b74 1353
19c4d916
VZ
1354 // careful: this object could have been deleted by the event handler
1355 // executed by the above ProcessEvent() call, so we can't access any fields
1356 // of this object any more
7214297d 1357}
7214297d 1358
8e40ed85
FM
1359/* static */
1360bool wxEvtHandler::ProcessEventIfMatchesId(const wxEventTableEntryBase& entry,
1361 wxEvtHandler *handler,
1362 wxEvent& event)
1c624631
VZ
1363{
1364 int tableId1 = entry.m_id,
1365 tableId2 = entry.m_lastId;
1366
1367 // match only if the event type is the same and the id is either -1 in
1368 // the event table (meaning "any") or the event id matches the id
1369 // specified in the event table either exactly or by falling into
1370 // range between first and last
1a18887b
WS
1371 if ((tableId1 == wxID_ANY) ||
1372 (tableId2 == wxID_ANY && tableId1 == event.GetId()) ||
1373 (tableId2 != wxID_ANY &&
1c624631
VZ
1374 (event.GetId() >= tableId1 && event.GetId() <= tableId2)))
1375 {
1376 event.Skip(false);
1377 event.m_callbackUserData = entry.m_callbackUserData;
1378
1379#if wxUSE_EXCEPTIONS
1380 if ( wxTheApp )
1381 {
1382 // call the handler via wxApp method which allows the user to catch
1383 // any exceptions which may be thrown by any handler in the program
1384 // in one place
3c778901 1385 wxTheApp->CallEventHandler(handler, *entry.m_fn, event);
1c624631
VZ
1386 }
1387 else
8bd2a804 1388#endif // wxUSE_EXCEPTIONS
1c624631 1389 {
3c778901 1390 (*entry.m_fn)(handler, event);
1c624631 1391 }
1c624631
VZ
1392
1393 if (!event.GetSkipped())
1394 return true;
1395 }
1396
1397 return false;
1398}
c801d85f 1399
8cc208e3 1400bool wxEvtHandler::DoTryApp(wxEvent& event)
c801d85f 1401{
4caf847c 1402 if ( wxTheApp && (this != wxTheApp) )
9154d8cf 1403 {
4caf847c
VZ
1404 // Special case: don't pass wxEVT_IDLE to wxApp, since it'll always
1405 // swallow it. wxEVT_IDLE is sent explicitly to wxApp so it will be
1406 // processed appropriately via SearchEventTable.
1407 if ( event.GetEventType() != wxEVT_IDLE )
1408 {
1409 if ( wxTheApp->ProcessEvent(event) )
1c624631 1410 return true;
4caf847c 1411 }
9154d8cf
VZ
1412 }
1413
1c624631 1414 return false;
4caf847c 1415}
0b746ba8 1416
2d8c0c2c
VZ
1417bool wxEvtHandler::TryBefore(wxEvent& event)
1418{
609483b8 1419#if WXWIN_COMPATIBILITY_2_8
2d8c0c2c
VZ
1420 // call the old virtual function to keep the code overriding it working
1421 return TryValidator(event);
1422#else
1423 wxUnusedVar(event);
1424 return false;
1425#endif
1426}
1427
1428bool wxEvtHandler::TryAfter(wxEvent& event)
1429{
3cbb9df8
VZ
1430 // We only want to pass the window to the application object once even if
1431 // there are several chained handlers. Ensure that this is what happens by
1432 // only calling DoTryApp() if there is no next handler (which would do it).
1433 //
1434 // Notice that, unlike simply calling TryAfter() on the last handler in the
1435 // chain only from ProcessEvent(), this also works with wxWindow object in
1436 // the middle of the chain: its overridden TryAfter() will still be called
1437 // and propagate the event upwards the window hierarchy even if it's not
1438 // the last one in the chain (which, admittedly, shouldn't happen often).
1439 if ( GetNextHandler() )
1440 return GetNextHandler()->TryAfter(event);
1441
3bad8c39
VZ
1442 // If this event is going to be processed in another handler next, don't
1443 // pass it to wxTheApp now, it will be done from TryAfter() of this other
1444 // handler.
1445 if ( event.WillBeProcessedAgain() )
1446 return false;
1447
609483b8 1448#if WXWIN_COMPATIBILITY_2_8
2d8c0c2c
VZ
1449 // as above, call the old virtual function for compatibility
1450 return TryParent(event);
1451#else
1452 return DoTryApp(event);
1453#endif
1454}
1455
4caf847c
VZ
1456bool wxEvtHandler::ProcessEvent(wxEvent& event)
1457{
58cc1587
VZ
1458 // The very first thing we do is to allow any registered filters to hook
1459 // into event processing in order to globally pre-process all events.
29de6f40 1460 //
3cbb9df8 1461 // Note that we should only do it if we're the first event handler called
29de6f40 1462 // to avoid calling FilterEvent() multiple times as the event goes through
3cbb9df8 1463 // the event handler chain and possibly upwards the window hierarchy.
29de6f40 1464 if ( !event.WasProcessed() )
9154d8cf 1465 {
58cc1587 1466 for ( wxEventFilter* f = ms_filterList; f; f = f->m_next )
9154d8cf 1467 {
58cc1587
VZ
1468 int rc = f->FilterEvent(event);
1469 if ( rc != wxEventFilter::Event_Skip )
29de6f40 1470 {
58cc1587
VZ
1471 wxASSERT_MSG( rc == wxEventFilter::Event_Ignore ||
1472 rc == wxEventFilter::Event_Processed,
1473 "unexpected FilterEvent() return value" );
9154d8cf 1474
58cc1587 1475 return rc != wxEventFilter::Event_Ignore;
29de6f40
VZ
1476 }
1477 //else: proceed normally
9154d8cf 1478 }
9154d8cf
VZ
1479 }
1480
3cbb9df8
VZ
1481 // Short circuit the event processing logic if we're requested to process
1482 // this event in this handler only, see DoTryChain() for more details.
bbdee10d 1483 if ( event.ShouldProcessOnlyIn(this) )
4b6d82c7 1484 return TryBeforeAndHere(event);
3cbb9df8
VZ
1485
1486
1487 // Try to process the event in this handler itself.
1488 if ( ProcessEventLocally(event) )
0ef5cba6
VZ
1489 {
1490 // It is possible that DoTryChain() called from ProcessEventLocally()
1491 // returned true but the event was not really processed: this happens
1492 // if a custom handler ignores the request to process the event in this
1493 // handler only and in this case we should skip the post processing
1494 // done in TryAfter() but still return the correct value ourselves to
1495 // indicate whether we did or did not find a handler for this event.
1496 return !event.GetSkipped();
1497 }
3cbb9df8
VZ
1498
1499 // If we still didn't find a handler, propagate the event upwards the
1500 // window chain and/or to the application object.
1501 if ( TryAfter(event) )
1502 return true;
1503
1504
1505 // No handler found anywhere, bail out.
1506 return false;
1507}
1508
1509bool wxEvtHandler::ProcessEventLocally(wxEvent& event)
1510{
4b6d82c7
VZ
1511 // Try the hooks which should be called before our own handlers and this
1512 // handler itself first. Notice that we should not call ProcessEvent() on
1513 // this one as we're already called from it, which explains why we do it
1514 // here and not in DoTryChain()
1515 return TryBeforeAndHere(event) || DoTryChain(event);
3cbb9df8 1516}
29de6f40 1517
3cbb9df8
VZ
1518bool wxEvtHandler::DoTryChain(wxEvent& event)
1519{
1520 for ( wxEvtHandler *h = GetNextHandler(); h; h = h->GetNextHandler() )
1521 {
1522 // We need to process this event at the level of this handler only
1523 // right now, the pre-/post-processing was either already done by
1524 // ProcessEvent() from which we were called or will be done by it when
1525 // we return.
1526 //
4b6d82c7 1527 // However we must call ProcessEvent() and not TryHereOnly() because the
512220b6
VZ
1528 // existing code (including some in wxWidgets itself) expects the
1529 // overridden ProcessEvent() in its custom event handlers pushed on a
1530 // window to be called.
3cbb9df8
VZ
1531 //
1532 // So we must call ProcessEvent() but it must not do what it usually
bbdee10d
VZ
1533 // does. To resolve this paradox we set up a special flag inside the
1534 // object itself to let ProcessEvent() know that it shouldn't do any
1535 // pre/post-processing for this event if it gets it. Note that this
1536 // only applies to this handler, if the event is passed to another one
1537 // by explicitly calling its ProcessEvent(), pre/post-processing should
1538 // be done as usual.
1539 //
1540 // Final complication is that if the implementation of ProcessEvent()
1541 // called wxEvent::DidntHonourProcessOnlyIn() (as the gross hack that
1542 // is wxScrollHelperEvtHandler::ProcessEvent() does) and ignored our
1543 // request to process event in this handler only, we have to compensate
1544 // for it by not processing the event further because this was already
1545 // done by that rogue event handler.
1546 wxEventProcessInHandlerOnly processInHandlerOnly(event, h);
e0520316 1547 if ( h->ProcessEvent(event) )
0ef5cba6
VZ
1548 {
1549 // Make sure "skipped" flag is not set as the event was really
1550 // processed in this case. Normally it shouldn't be set anyhow but
1551 // make sure just in case the user code does something strange.
1552 event.Skip(false);
1553
3cbb9df8 1554 return true;
0ef5cba6 1555 }
e0520316
VZ
1556
1557 if ( !event.ShouldProcessOnlyIn(h) )
0ef5cba6
VZ
1558 {
1559 // Still return true to indicate that no further processing should
1560 // be undertaken but ensure that "skipped" flag is set so that the
1561 // caller knows that the event was not really processed.
1562 event.Skip();
1563
1564 return true;
1565 }
3cbb9df8
VZ
1566 }
1567
1568 return false;
25250d33
VZ
1569}
1570
4b6d82c7 1571bool wxEvtHandler::TryHereOnly(wxEvent& event)
25250d33 1572{
29de6f40
VZ
1573 // If the event handler is disabled it doesn't process any events
1574 if ( !GetEvtHandlerEnabled() )
1575 return false;
4caf847c 1576
29de6f40
VZ
1577 // Handle per-instance dynamic event tables first
1578 if ( m_dynamicEvents && SearchDynamicEventTable(event) )
1579 return true;
c801d85f 1580
29de6f40
VZ
1581 // Then static per-class event tables
1582 if ( GetEventHashTable().HandleEvent(event, this) )
25250d33 1583 return true;
c801d85f 1584
b690a8f6 1585#ifdef wxHAS_CALL_AFTER
856cad9a 1586 // There is an implicit entry for async method calls processing in every
cf222762
VZ
1587 // event handler:
1588 if ( event.GetEventType() == wxEVT_ASYNC_METHOD_CALL &&
1589 event.GetEventObject() == this )
1590 {
1591 static_cast<wxAsyncMethodCallEvent&>(event).Execute();
1592 return true;
1593 }
b690a8f6 1594#endif // wxHAS_CALL_AFTER
cf222762 1595
25250d33
VZ
1596 // We don't have a handler for this event.
1597 return false;
c801d85f
KB
1598}
1599
937013e0
VS
1600bool wxEvtHandler::SafelyProcessEvent(wxEvent& event)
1601{
1602#if wxUSE_EXCEPTIONS
1603 try
1604 {
1605#endif
1606 return ProcessEvent(event);
1607#if wxUSE_EXCEPTIONS
1608 }
1609 catch ( ... )
1610 {
8793514e
VZ
1611 // notice that we do it in 2 steps to avoid warnings about possibly
1612 // uninitialized loop variable from some versions of g++ which are not
1613 // smart enough to figure out that GetActive() doesn't throw and so
1614 // that loop will always be initialized
1615 wxEventLoopBase *loop = NULL;
937013e0
VS
1616 try
1617 {
8793514e
VZ
1618 loop = wxEventLoopBase::GetActive();
1619
937013e0
VS
1620 if ( !wxTheApp || !wxTheApp->OnExceptionInMainLoop() )
1621 {
1622 if ( loop )
1623 loop->Exit();
1624 }
1625 //else: continue running current event loop
1626
1627 return false;
1628 }
1629 catch ( ... )
1630 {
1631 // OnExceptionInMainLoop() threw, possibly rethrowing the same
1632 // exception again: very good, but we still need Exit() to
1633 // be called
1634 if ( loop )
1635 loop->Exit();
1636 throw;
1637 }
1638 }
1639#endif // wxUSE_EXCEPTIONS
1640}
1641
c801d85f
KB
1642bool wxEvtHandler::SearchEventTable(wxEventTable& table, wxEvent& event)
1643{
1c624631 1644 const wxEventType eventType = event.GetEventType();
68662769 1645 for ( int i = 0; table.entries[i].m_fn != 0; i++ )
c801d85f 1646 {
68662769 1647 const wxEventTableEntry& entry = table.entries[i];
68662769 1648 if ( eventType == entry.m_eventType )
0b746ba8 1649 {
03f3617b 1650 if ( ProcessEventIfMatchesId(entry, this, event) )
1c624631 1651 return true;
0b746ba8 1652 }
c801d85f 1653 }
68662769 1654
1c624631 1655 return false;
c801d85f 1656}
97d7bfb8 1657
890d70eb
VZ
1658void wxEvtHandler::DoBind(int id,
1659 int lastId,
1660 wxEventType eventType,
1661 wxEventFunctor *func,
1662 wxObject *userData)
fe71f65c 1663{
0b5eceb6 1664 wxDynamicEventTableEntry *entry =
3c778901 1665 new wxDynamicEventTableEntry(eventType, id, lastId, func, userData);
0b746ba8
VZ
1666
1667 if (!m_dynamicEvents)
1668 m_dynamicEvents = new wxList;
1669
92309201
RD
1670 // Insert at the front of the list so most recent additions are found first
1671 m_dynamicEvents->Insert( (wxObject*) entry );
cc6ceca7
VZ
1672
1673 // Make sure we get to know when a sink is destroyed
f3ff831f 1674 wxEvtHandler *eventSink = func->GetEvtHandler();
886274c6 1675 if ( eventSink && eventSink != this )
2ef989c9 1676 {
cc6ceca7
VZ
1677 wxEventConnectionRef *evtConnRef = FindRefInTrackerList(eventSink);
1678 if ( evtConnRef )
1679 evtConnRef->IncRef( );
2ef989c9 1680 else
e822d1bd 1681 new wxEventConnectionRef(this, eventSink);
2ef989c9 1682 }
fe71f65c 1683}
97d7bfb8 1684
3c778901 1685bool
890d70eb
VZ
1686wxEvtHandler::DoUnbind(int id,
1687 int lastId,
1688 wxEventType eventType,
1689 const wxEventFunctor& func,
1690 wxObject *userData)
97d7bfb8
RR
1691{
1692 if (!m_dynamicEvents)
1c624631 1693 return false;
cfe17b74 1694
df5168c4 1695 wxList::compatibility_iterator node = m_dynamicEvents->GetFirst();
97d7bfb8
RR
1696 while (node)
1697 {
ad684023 1698 wxDynamicEventTableEntry *entry = (wxDynamicEventTableEntry*)node->GetData();
2e4df4bf 1699
97d7bfb8 1700 if ((entry->m_id == id) &&
1a18887b 1701 ((entry->m_lastId == lastId) || (lastId == wxID_ANY)) &&
97d7bfb8 1702 ((entry->m_eventType == eventType) || (eventType == wxEVT_NULL)) &&
890d70eb 1703 entry->m_fn->IsMatching(func) &&
3c778901 1704 ((entry->m_callbackUserData == userData) || !userData))
97d7bfb8 1705 {
75b2220e
VZ
1706 // Remove connection from tracker node (wxEventConnectionRef)
1707 wxEvtHandler *eventSink = entry->m_fn->GetEvtHandler();
1708 if ( eventSink && eventSink != this )
1709 {
1710 wxEventConnectionRef *evtConnRef = FindRefInTrackerList(eventSink);
1711 if ( evtConnRef )
1712 evtConnRef->DecRef();
1713 }
1714
3c778901 1715 delete entry->m_callbackUserData;
df5168c4 1716 m_dynamicEvents->Erase( node );
97d7bfb8 1717 delete entry;
1c624631 1718 return true;
97d7bfb8 1719 }
b1d4dd7a 1720 node = node->GetNext();
97d7bfb8 1721 }
1c624631 1722 return false;
97d7bfb8 1723}
fe71f65c
RR
1724
1725bool wxEvtHandler::SearchDynamicEventTable( wxEvent& event )
1726{
1c624631 1727 wxCHECK_MSG( m_dynamicEvents, false,
223d09f6 1728 wxT("caller should check that we have dynamic events") );
0b746ba8 1729
df5168c4 1730 wxList::compatibility_iterator node = m_dynamicEvents->GetFirst();
0b746ba8 1731 while (node)
fe71f65c 1732 {
1c624631 1733 wxDynamicEventTableEntry *entry = (wxDynamicEventTableEntry*)node->GetData();
0b746ba8 1734
7fce89b9
VZ
1735 // get next node before (maybe) calling the event handler as it could
1736 // call Disconnect() invalidating the current node
1737 node = node->GetNext();
1738
03f3617b 1739 if ( event.GetEventType() == entry->m_eventType )
0b746ba8 1740 {
f3ff831f 1741 wxEvtHandler *handler = entry->m_fn->GetEvtHandler();
03f3617b
VZ
1742 if ( !handler )
1743 handler = this;
1744 if ( ProcessEventIfMatchesId(*entry, handler, event) )
1c624631 1745 return true;
0b746ba8 1746 }
7b678698 1747 }
1c624631
VZ
1748
1749 return false;
4115960d 1750}
fe71f65c 1751
b88c44e7
RD
1752void wxEvtHandler::DoSetClientObject( wxClientData *data )
1753{
1754 wxASSERT_MSG( m_clientDataType != wxClientData_Void,
1755 wxT("can't have both object and void client data") );
1756
1757 if ( m_clientObject )
1758 delete m_clientObject;
1759
1760 m_clientObject = data;
1761 m_clientDataType = wxClientData_Object;
1762}
1763
1764wxClientData *wxEvtHandler::DoGetClientObject() const
1765{
1766 // it's not an error to call GetClientObject() on a window which doesn't
1767 // have client data at all - NULL will be returned
1768 wxASSERT_MSG( m_clientDataType != wxClientData_Void,
1769 wxT("this window doesn't have object client data") );
1770
1771 return m_clientObject;
1772}
1773
1774void wxEvtHandler::DoSetClientData( void *data )
1775{
1776 wxASSERT_MSG( m_clientDataType != wxClientData_Object,
1777 wxT("can't have both object and void client data") );
1778
1779 m_clientData = data;
1780 m_clientDataType = wxClientData_Void;
1781}
1782
1783void *wxEvtHandler::DoGetClientData() const
1784{
1785 // it's not an error to call GetClientData() on a window which doesn't have
1786 // client data at all - NULL will be returned
1787 wxASSERT_MSG( m_clientDataType != wxClientData_Object,
1788 wxT("this window doesn't have void client data") );
1789
1790 return m_clientData;
1791}
1792
cc6ceca7
VZ
1793// A helper to find an wxEventConnectionRef object
1794wxEventConnectionRef *
1795wxEvtHandler::FindRefInTrackerList(wxEvtHandler *eventSink)
2ef989c9 1796{
cc6ceca7 1797 for ( wxTrackerNode *node = eventSink->GetFirst(); node; node = node->m_nxt )
2ef989c9 1798 {
cc6ceca7
VZ
1799 // we only want wxEventConnectionRef nodes here
1800 wxEventConnectionRef *evtConnRef = node->ToEventConnection();
1801 if ( evtConnRef && evtConnRef->m_src == this )
2ef989c9 1802 {
cc6ceca7
VZ
1803 wxASSERT( evtConnRef->m_sink==eventSink );
1804 return evtConnRef;
2ef989c9
RR
1805 }
1806 }
cc6ceca7 1807
2ef989c9
RR
1808 return NULL;
1809}
1810
1811void wxEvtHandler::OnSinkDestroyed( wxEvtHandler *sink )
1812{
1813 wxASSERT(m_dynamicEvents);
cc6ceca7 1814
2ef989c9
RR
1815 // remove all connections with this sink
1816 wxList::compatibility_iterator node = m_dynamicEvents->GetFirst(), node_nxt;
1817 while (node)
1818 {
1819 wxDynamicEventTableEntry *entry = (wxDynamicEventTableEntry*)node->GetData();
1820 node_nxt = node->GetNext();
cc6ceca7 1821
f3ff831f 1822 if ( entry->m_fn->GetEvtHandler() == sink )
2ef989c9 1823 {
3c778901 1824 delete entry->m_callbackUserData;
2ef989c9
RR
1825 m_dynamicEvents->Erase( node );
1826 delete entry;
1827 }
1828 node = node_nxt;
1829 }
1830}
1831
4caf847c
VZ
1832#endif // wxUSE_BASE
1833
e90c1d2a
VZ
1834#if wxUSE_GUI
1835
e702ff0f
JS
1836// Find a window with the focus, that is also a descendant of the given window.
1837// This is used to determine the window to initially send commands to.
1838wxWindow* wxFindFocusDescendant(wxWindow* ancestor)
1839{
1840 // Process events starting with the window with the focus, if any.
1841 wxWindow* focusWin = wxWindow::FindFocus();
1842 wxWindow* win = focusWin;
1843
1844 // Check if this is a descendant of this frame.
1845 // If not, win will be set to NULL.
1846 while (win)
1847 {
1848 if (win == ancestor)
1849 break;
1850 else
1851 win = win->GetParent();
1852 }
3c778901
VZ
1853 if (win == NULL)
1854 focusWin = NULL;
e702ff0f
JS
1855
1856 return focusWin;
1857}
1858
c4fa5aa7
VZ
1859// ----------------------------------------------------------------------------
1860// wxEventBlocker
1861// ----------------------------------------------------------------------------
1862
1863wxEventBlocker::wxEventBlocker(wxWindow *win, wxEventType type)
1864{
1865 wxCHECK_RET(win, wxT("Null window given to wxEventBlocker"));
1866
1867 m_window = win;
1868
1869 Block(type);
1870 m_window->PushEventHandler(this);
1871}
1872
1873wxEventBlocker::~wxEventBlocker()
1874{
1875 wxEvtHandler *popped = m_window->PopEventHandler(false);
aa767a45 1876 wxCHECK_RET(popped == this,
c4fa5aa7
VZ
1877 wxT("Don't push other event handlers into a window managed by wxEventBlocker!"));
1878}
1879
1880bool wxEventBlocker::ProcessEvent(wxEvent& event)
1881{
1882 // should this event be blocked?
1883 for ( size_t i = 0; i < m_eventsToBlock.size(); i++ )
1884 {
1885 wxEventType t = (wxEventType)m_eventsToBlock[i];
1886 if ( t == wxEVT_ANY || t == event.GetEventType() )
1887 return true; // yes, it should: mark this event as processed
1888 }
1889
1890 return false;
1891}
1892
e90c1d2a 1893#endif // wxUSE_GUI
3c778901 1894