]> git.saurik.com Git - wxWidgets.git/blame - src/common/event.cpp
Fix tracking rectangles in 64-bit build by remembering the tag as the 64-bit integer...
[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
cc6ceca7 1063 // Remove ourselves from sink destructor notifications
2ef989c9
RR
1064 // (this has usually been been done, in wxTrackable destructor)
1065 wxEvtHandler *eventSink = entry->m_eventSink;
cc6ceca7 1066 if ( eventSink )
2ef989c9 1067 {
cc6ceca7
VZ
1068 wxEventConnectionRef * const
1069 evtConnRef = FindRefInTrackerList(eventSink);
1070 if ( evtConnRef )
1071 {
1072 eventSink->RemoveNode(evtConnRef);
1073 delete evtConnRef;
1074 }
2ef989c9 1075 }
cc6ceca7 1076
2e4df4bf
VZ
1077 if (entry->m_callbackUserData)
1078 delete entry->m_callbackUserData;
0b746ba8 1079 delete entry;
0b746ba8
VZ
1080 }
1081 delete m_dynamicEvents;
1082 };
7214297d 1083
5875d39c
VZ
1084 if (m_pendingEvents)
1085 m_pendingEvents->DeleteContents(true);
8e193f38 1086 delete m_pendingEvents;
7214297d 1087
083f7497 1088 // Remove us from wxPendingEvents if necessary.
5d9d1b88
RR
1089 if ( wxPendingEvents )
1090 {
364ab6cc 1091#if wxUSE_THREADS
cc6ceca7 1092 if (wxPendingEventsLocker)
19c4d916 1093 wxENTER_CRIT_SECT(*wxPendingEventsLocker);
364ab6cc 1094#endif
19c4d916
VZ
1095
1096 if ( wxPendingEvents->DeleteObject(this) )
1097 {
1098 // check that we were present only once in the list
1099 wxASSERT_MSG( !wxPendingEvents->Find(this),
1100 "Handler occurs twice in wxPendingEvents list" );
1101 }
1102 //else: we weren't in this list at all, it's ok
1103
364ab6cc 1104#if wxUSE_THREADS
cc6ceca7 1105 if (wxPendingEventsLocker)
19c4d916 1106 wxLEAVE_CRIT_SECT(*wxPendingEventsLocker);
364ab6cc 1107#endif
083f7497 1108 }
b88c44e7
RD
1109
1110 // we only delete object data, not untyped
1111 if ( m_clientDataType == wxClientData_Object )
1112 delete m_clientObject;
c801d85f
KB
1113}
1114
7214297d 1115#if wxUSE_THREADS
aadbdf11 1116
76bfdc9a 1117bool wxEvtHandler::ProcessThreadEvent(const wxEvent& event)
7214297d 1118{
7214297d 1119 // check that we are really in a child thread
8e193f38
VZ
1120 wxASSERT_MSG( !wxThread::IsMain(),
1121 wxT("use ProcessEvent() in main thread") );
1122
1123 AddPendingEvent(event);
7214297d 1124
1c624631 1125 return true;
8e193f38
VZ
1126}
1127
1128#endif // wxUSE_THREADS
1129
76bfdc9a 1130void wxEvtHandler::AddPendingEvent(const wxEvent& event)
8e193f38 1131{
16c1f79c
RR
1132 // 1) Add event to list of pending events of this event handler
1133
7a5c8ac4
VZ
1134 wxEvent *eventCopy = event.Clone();
1135
1136 // we must be able to copy the events here so the event class must
1137 // implement Clone() properly instead of just providing a NULL stab for it
1138 wxCHECK_RET( eventCopy,
1139 _T("events of this type aren't supposed to be posted") );
1140
8ebec7dc 1141 wxENTER_CRIT_SECT( m_pendingEventsLock );
16c1f79c 1142
8e193f38
VZ
1143 if ( !m_pendingEvents )
1144 m_pendingEvents = new wxList;
7214297d 1145
7a5c8ac4 1146 m_pendingEvents->Append(eventCopy);
7214297d 1147
8ebec7dc 1148 wxLEAVE_CRIT_SECT( m_pendingEventsLock );
16c1f79c
RR
1149
1150 // 2) Add this event handler to list of event handlers that
1151 // have pending events.
cfe17b74 1152
b568d04f 1153 wxENTER_CRIT_SECT(*wxPendingEventsLocker);
72cdf4c9 1154
8e193f38
VZ
1155 if ( !wxPendingEvents )
1156 wxPendingEvents = new wxList;
19c4d916
VZ
1157 if ( !wxPendingEvents->Find(this) )
1158 wxPendingEvents->Append(this);
72cdf4c9 1159
ce6d2511 1160 wxLEAVE_CRIT_SECT(*wxPendingEventsLocker);
6164f93c 1161
90e572f1 1162 // 3) Inform the system that new pending events are somewhere,
16c1f79c 1163 // and that these should be processed in idle time.
bf9e3e73 1164 wxWakeUpIdle();
7214297d
GL
1165}
1166
1167void wxEvtHandler::ProcessPendingEvents()
1168{
8ebec7dc 1169 wxENTER_CRIT_SECT( m_pendingEventsLock );
8e193f38 1170
19c4d916
VZ
1171 // this method is only called by wxApp if this handler does have
1172 // pending events
1173 wxCHECK_RET( m_pendingEvents && !m_pendingEvents->IsEmpty(),
1174 "should have pending events if called" );
5fa150e2 1175
19c4d916 1176 wxList::compatibility_iterator node = m_pendingEvents->GetFirst();
47b6fabc 1177 wxEventPtr event(wx_static_cast(wxEvent *, node->GetData()));
5875d39c 1178
19c4d916
VZ
1179 // it's important we remove event from list before processing it, else a
1180 // nested event loop, for example from a modal dialog, might process the
1181 // same event again.
1182 m_pendingEvents->Erase(node);
7b0d5c59 1183
19c4d916
VZ
1184 // if there are no more pending events left, we don't need to stay in this
1185 // list
1186 if ( m_pendingEvents->IsEmpty() )
1187 wxPendingEvents->DeleteObject(this);
5fa150e2 1188
8ebec7dc 1189 wxLEAVE_CRIT_SECT( m_pendingEventsLock );
7b0d5c59 1190
19c4d916 1191 ProcessEvent(*event);
cfe17b74 1192
19c4d916
VZ
1193 // careful: this object could have been deleted by the event handler
1194 // executed by the above ProcessEvent() call, so we can't access any fields
1195 // of this object any more
7214297d 1196}
7214297d 1197
c801d85f
KB
1198/*
1199 * Event table stuff
1200 */
1c624631
VZ
1201/* static */ bool
1202wxEvtHandler::ProcessEventIfMatches(const wxEventTableEntryBase& entry,
1203 wxEvtHandler *handler,
1204 wxEvent& event)
1205{
1206 int tableId1 = entry.m_id,
1207 tableId2 = entry.m_lastId;
1208
1209 // match only if the event type is the same and the id is either -1 in
1210 // the event table (meaning "any") or the event id matches the id
1211 // specified in the event table either exactly or by falling into
1212 // range between first and last
1a18887b
WS
1213 if ((tableId1 == wxID_ANY) ||
1214 (tableId2 == wxID_ANY && tableId1 == event.GetId()) ||
1215 (tableId2 != wxID_ANY &&
1c624631
VZ
1216 (event.GetId() >= tableId1 && event.GetId() <= tableId2)))
1217 {
1218 event.Skip(false);
1219 event.m_callbackUserData = entry.m_callbackUserData;
1220
1221#if wxUSE_EXCEPTIONS
1222 if ( wxTheApp )
1223 {
1224 // call the handler via wxApp method which allows the user to catch
1225 // any exceptions which may be thrown by any handler in the program
1226 // in one place
1227 wxTheApp->HandleEvent(handler, (wxEventFunction)entry.m_fn, event);
1228 }
1229 else
8bd2a804 1230#endif // wxUSE_EXCEPTIONS
1c624631
VZ
1231 {
1232 // no need for an extra virtual function call
1233 (handler->*((wxEventFunction) (entry.m_fn)))(event);
1234 }
1c624631
VZ
1235
1236 if (!event.GetSkipped())
1237 return true;
1238 }
1239
1240 return false;
1241}
c801d85f 1242
4caf847c 1243bool wxEvtHandler::TryParent(wxEvent& event)
c801d85f 1244{
4caf847c 1245 if ( wxTheApp && (this != wxTheApp) )
9154d8cf 1246 {
4caf847c
VZ
1247 // Special case: don't pass wxEVT_IDLE to wxApp, since it'll always
1248 // swallow it. wxEVT_IDLE is sent explicitly to wxApp so it will be
1249 // processed appropriately via SearchEventTable.
1250 if ( event.GetEventType() != wxEVT_IDLE )
1251 {
1252 if ( wxTheApp->ProcessEvent(event) )
1c624631 1253 return true;
4caf847c 1254 }
9154d8cf
VZ
1255 }
1256
1c624631 1257 return false;
4caf847c 1258}
0b746ba8 1259
4caf847c
VZ
1260bool wxEvtHandler::ProcessEvent(wxEvent& event)
1261{
9154d8cf
VZ
1262 // allow the application to hook into event processing
1263 if ( wxTheApp )
1264 {
1265 int rc = wxTheApp->FilterEvent(event);
1266 if ( rc != -1 )
1267 {
1268 wxASSERT_MSG( rc == 1 || rc == 0,
1269 _T("unexpected wxApp::FilterEvent return value") );
1270
1271 return rc != 0;
1272 }
1273 //else: proceed normally
1274 }
1275
8e193f38 1276 // An event handler can be enabled or disabled
0b746ba8 1277 if ( GetEvtHandlerEnabled() )
c801d85f 1278 {
22c2307c
VZ
1279 // if we have a validator, it has higher priority than our own event
1280 // table
4caf847c 1281 if ( TryValidator(event) )
1c624631 1282 return true;
4caf847c 1283
22c2307c
VZ
1284 // Handle per-instance dynamic event tables first
1285 if ( m_dynamicEvents && SearchDynamicEventTable(event) )
1c624631 1286 return true;
0b746ba8 1287
b5a98acd
VZ
1288 // Then static per-class event tables
1289 if ( GetEventHashTable().HandleEvent(event, this) )
1c624631 1290 return true;
c801d85f
KB
1291 }
1292
0b746ba8
VZ
1293 // Try going down the event handler chain
1294 if ( GetNextHandler() )
c801d85f 1295 {
21dffed2
VZ
1296 // notice that we shouldn't let the parent have the event even if the
1297 // next handler does not process it because it will have already passed
1298 // it to the parent in this case
1299 return GetNextHandler()->ProcessEvent(event);
c801d85f 1300 }
c801d85f 1301
22c2307c
VZ
1302 // Finally propagate the event upwards the window chain and/or to the
1303 // application object as necessary
4caf847c 1304 return TryParent(event);
c801d85f
KB
1305}
1306
937013e0
VS
1307bool wxEvtHandler::SafelyProcessEvent(wxEvent& event)
1308{
1309#if wxUSE_EXCEPTIONS
1310 try
1311 {
1312#endif
1313 return ProcessEvent(event);
1314#if wxUSE_EXCEPTIONS
1315 }
1316 catch ( ... )
1317 {
1318 wxEventLoopBase *loop = wxEventLoopBase::GetActive();
1319 try
1320 {
1321 if ( !wxTheApp || !wxTheApp->OnExceptionInMainLoop() )
1322 {
1323 if ( loop )
1324 loop->Exit();
1325 }
1326 //else: continue running current event loop
1327
1328 return false;
1329 }
1330 catch ( ... )
1331 {
1332 // OnExceptionInMainLoop() threw, possibly rethrowing the same
1333 // exception again: very good, but we still need Exit() to
1334 // be called
1335 if ( loop )
1336 loop->Exit();
1337 throw;
1338 }
1339 }
1340#endif // wxUSE_EXCEPTIONS
1341}
1342
b5a98acd 1343
c801d85f
KB
1344bool wxEvtHandler::SearchEventTable(wxEventTable& table, wxEvent& event)
1345{
1c624631 1346 const wxEventType eventType = event.GetEventType();
68662769 1347 for ( int i = 0; table.entries[i].m_fn != 0; i++ )
c801d85f 1348 {
68662769 1349 const wxEventTableEntry& entry = table.entries[i];
68662769 1350 if ( eventType == entry.m_eventType )
0b746ba8 1351 {
1c624631
VZ
1352 if ( ProcessEventIfMatches(entry, this, event) )
1353 return true;
0b746ba8 1354 }
c801d85f 1355 }
68662769 1356
1c624631 1357 return false;
c801d85f 1358}
97d7bfb8 1359
debe6624 1360void wxEvtHandler::Connect( int id, int lastId,
77d4384e 1361 int eventType,
0b746ba8 1362 wxObjectEventFunction func,
65b17727
JS
1363 wxObject *userData,
1364 wxEvtHandler* eventSink )
fe71f65c 1365{
0b5eceb6 1366 wxDynamicEventTableEntry *entry =
65b17727 1367 new wxDynamicEventTableEntry(eventType, id, lastId, func, userData, eventSink);
0b746ba8
VZ
1368
1369 if (!m_dynamicEvents)
1370 m_dynamicEvents = new wxList;
1371
92309201
RD
1372 // Insert at the front of the list so most recent additions are found first
1373 m_dynamicEvents->Insert( (wxObject*) entry );
cc6ceca7
VZ
1374
1375 // Make sure we get to know when a sink is destroyed
1376 if ( eventSink )
2ef989c9 1377 {
cc6ceca7
VZ
1378 wxEventConnectionRef *evtConnRef = FindRefInTrackerList(eventSink);
1379 if ( evtConnRef )
1380 evtConnRef->IncRef( );
2ef989c9 1381 else
cc6ceca7 1382 evtConnRef = new wxEventConnectionRef(this, eventSink);
2ef989c9 1383 }
fe71f65c 1384}
97d7bfb8
RR
1385
1386bool wxEvtHandler::Disconnect( int id, int lastId, wxEventType eventType,
1387 wxObjectEventFunction func,
65b17727
JS
1388 wxObject *userData,
1389 wxEvtHandler* eventSink )
97d7bfb8
RR
1390{
1391 if (!m_dynamicEvents)
1c624631 1392 return false;
cfe17b74 1393
2ef989c9 1394 // Remove connection from tracker node (wxEventConnectionRef)
cc6ceca7 1395 if ( eventSink )
2ef989c9 1396 {
cc6ceca7
VZ
1397 wxEventConnectionRef *evtConnRef = FindRefInTrackerList(eventSink);
1398 if ( evtConnRef )
1399 evtConnRef->DecRef();
2ef989c9
RR
1400 }
1401
df5168c4 1402 wxList::compatibility_iterator node = m_dynamicEvents->GetFirst();
97d7bfb8
RR
1403 while (node)
1404 {
ad684023 1405 wxDynamicEventTableEntry *entry = (wxDynamicEventTableEntry*)node->GetData();
2e4df4bf 1406
97d7bfb8 1407 if ((entry->m_id == id) &&
1a18887b 1408 ((entry->m_lastId == lastId) || (lastId == wxID_ANY)) &&
97d7bfb8
RR
1409 ((entry->m_eventType == eventType) || (eventType == wxEVT_NULL)) &&
1410 ((entry->m_fn == func) || (func == (wxObjectEventFunction)NULL)) &&
65b17727 1411 ((entry->m_eventSink == eventSink) || (eventSink == (wxEvtHandler*)NULL)) &&
97d7bfb8
RR
1412 ((entry->m_callbackUserData == userData) || (userData == (wxObject*)NULL)))
1413 {
2e4df4bf
VZ
1414 if (entry->m_callbackUserData)
1415 delete entry->m_callbackUserData;
df5168c4 1416 m_dynamicEvents->Erase( node );
97d7bfb8 1417 delete entry;
1c624631 1418 return true;
97d7bfb8 1419 }
b1d4dd7a 1420 node = node->GetNext();
97d7bfb8 1421 }
1c624631 1422 return false;
97d7bfb8 1423}
fe71f65c
RR
1424
1425bool wxEvtHandler::SearchDynamicEventTable( wxEvent& event )
1426{
1c624631 1427 wxCHECK_MSG( m_dynamicEvents, false,
223d09f6 1428 wxT("caller should check that we have dynamic events") );
0b746ba8 1429
df5168c4 1430 wxList::compatibility_iterator node = m_dynamicEvents->GetFirst();
0b746ba8 1431 while (node)
fe71f65c 1432 {
1c624631 1433 wxDynamicEventTableEntry *entry = (wxDynamicEventTableEntry*)node->GetData();
0b746ba8 1434
7fce89b9
VZ
1435 // get next node before (maybe) calling the event handler as it could
1436 // call Disconnect() invalidating the current node
1437 node = node->GetNext();
1438
ab1931f9 1439 if ((event.GetEventType() == entry->m_eventType) && (entry->m_fn != 0))
0b746ba8 1440 {
ad684023
RR
1441 wxEvtHandler *handler = entry->m_eventSink ? entry->m_eventSink
1442 : this;
1c624631 1443 if ( ProcessEventIfMatches(*entry, handler, event) )
1c624631 1444 return true;
0b746ba8 1445 }
7b678698 1446 }
1c624631
VZ
1447
1448 return false;
4115960d 1449}
fe71f65c 1450
b88c44e7
RD
1451void wxEvtHandler::DoSetClientObject( wxClientData *data )
1452{
1453 wxASSERT_MSG( m_clientDataType != wxClientData_Void,
1454 wxT("can't have both object and void client data") );
1455
1456 if ( m_clientObject )
1457 delete m_clientObject;
1458
1459 m_clientObject = data;
1460 m_clientDataType = wxClientData_Object;
1461}
1462
1463wxClientData *wxEvtHandler::DoGetClientObject() const
1464{
1465 // it's not an error to call GetClientObject() on a window which doesn't
1466 // have client data at all - NULL will be returned
1467 wxASSERT_MSG( m_clientDataType != wxClientData_Void,
1468 wxT("this window doesn't have object client data") );
1469
1470 return m_clientObject;
1471}
1472
1473void wxEvtHandler::DoSetClientData( void *data )
1474{
1475 wxASSERT_MSG( m_clientDataType != wxClientData_Object,
1476 wxT("can't have both object and void client data") );
1477
1478 m_clientData = data;
1479 m_clientDataType = wxClientData_Void;
1480}
1481
1482void *wxEvtHandler::DoGetClientData() const
1483{
1484 // it's not an error to call GetClientData() on a window which doesn't have
1485 // client data at all - NULL will be returned
1486 wxASSERT_MSG( m_clientDataType != wxClientData_Object,
1487 wxT("this window doesn't have void client data") );
1488
1489 return m_clientData;
1490}
1491
cc6ceca7
VZ
1492// A helper to find an wxEventConnectionRef object
1493wxEventConnectionRef *
1494wxEvtHandler::FindRefInTrackerList(wxEvtHandler *eventSink)
2ef989c9 1495{
cc6ceca7 1496 for ( wxTrackerNode *node = eventSink->GetFirst(); node; node = node->m_nxt )
2ef989c9 1497 {
cc6ceca7
VZ
1498 // we only want wxEventConnectionRef nodes here
1499 wxEventConnectionRef *evtConnRef = node->ToEventConnection();
1500 if ( evtConnRef && evtConnRef->m_src == this )
2ef989c9 1501 {
cc6ceca7
VZ
1502 wxASSERT( evtConnRef->m_sink==eventSink );
1503 return evtConnRef;
2ef989c9
RR
1504 }
1505 }
cc6ceca7 1506
2ef989c9
RR
1507 return NULL;
1508}
1509
1510void wxEvtHandler::OnSinkDestroyed( wxEvtHandler *sink )
1511{
1512 wxASSERT(m_dynamicEvents);
cc6ceca7 1513
2ef989c9
RR
1514 // remove all connections with this sink
1515 wxList::compatibility_iterator node = m_dynamicEvents->GetFirst(), node_nxt;
1516 while (node)
1517 {
1518 wxDynamicEventTableEntry *entry = (wxDynamicEventTableEntry*)node->GetData();
1519 node_nxt = node->GetNext();
cc6ceca7
VZ
1520
1521 if ( entry->m_eventSink==sink )
2ef989c9
RR
1522 {
1523 if (entry->m_callbackUserData)
1524 delete entry->m_callbackUserData;
1525 m_dynamicEvents->Erase( node );
1526 delete entry;
1527 }
1528 node = node_nxt;
1529 }
1530}
1531
4caf847c
VZ
1532#endif // wxUSE_BASE
1533
e90c1d2a
VZ
1534#if wxUSE_GUI
1535
e702ff0f
JS
1536// Find a window with the focus, that is also a descendant of the given window.
1537// This is used to determine the window to initially send commands to.
1538wxWindow* wxFindFocusDescendant(wxWindow* ancestor)
1539{
1540 // Process events starting with the window with the focus, if any.
1541 wxWindow* focusWin = wxWindow::FindFocus();
1542 wxWindow* win = focusWin;
1543
1544 // Check if this is a descendant of this frame.
1545 // If not, win will be set to NULL.
1546 while (win)
1547 {
1548 if (win == ancestor)
1549 break;
1550 else
1551 win = win->GetParent();
1552 }
1553 if (win == (wxWindow*) NULL)
1554 focusWin = (wxWindow*) NULL;
1555
1556 return focusWin;
1557}
1558
c4fa5aa7
VZ
1559// ----------------------------------------------------------------------------
1560// wxEventBlocker
1561// ----------------------------------------------------------------------------
1562
1563wxEventBlocker::wxEventBlocker(wxWindow *win, wxEventType type)
1564{
1565 wxCHECK_RET(win, wxT("Null window given to wxEventBlocker"));
1566
1567 m_window = win;
1568
1569 Block(type);
1570 m_window->PushEventHandler(this);
1571}
1572
1573wxEventBlocker::~wxEventBlocker()
1574{
1575 wxEvtHandler *popped = m_window->PopEventHandler(false);
aa767a45 1576 wxCHECK_RET(popped == this,
c4fa5aa7
VZ
1577 wxT("Don't push other event handlers into a window managed by wxEventBlocker!"));
1578}
1579
1580bool wxEventBlocker::ProcessEvent(wxEvent& event)
1581{
1582 // should this event be blocked?
1583 for ( size_t i = 0; i < m_eventsToBlock.size(); i++ )
1584 {
1585 wxEventType t = (wxEventType)m_eventsToBlock[i];
1586 if ( t == wxEVT_ANY || t == event.GetEventType() )
1587 return true; // yes, it should: mark this event as processed
1588 }
1589
1590 return false;
1591}
1592
e90c1d2a 1593#endif // wxUSE_GUI