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