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