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