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