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