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