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