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