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