]> git.saurik.com Git - wxWidgets.git/blame - src/common/event.cpp
renamed wxComboControl to wxComboCtrl and related wxUSE_XXX and configure switches...
[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
dd070522
VZ
771#endif // wxUSE_GUI
772
9416681d
VS
773
774#if wxUSE_BASE
775
b5a98acd
VZ
776// ----------------------------------------------------------------------------
777// wxEventHashTable
778// ----------------------------------------------------------------------------
779
afa039f9
JS
780static const int EVENT_TYPE_TABLE_INIT_SIZE = 31; // Not too big not too small...
781
782wxEventHashTable* wxEventHashTable::sm_first = NULL;
b5a98acd
VZ
783
784wxEventHashTable::wxEventHashTable(const wxEventTable &table)
785 : m_table(table),
1c624631 786 m_rebuildHash(true)
b5a98acd
VZ
787{
788 AllocEventTypeTable(EVENT_TYPE_TABLE_INIT_SIZE);
1a18887b 789
afa039f9
JS
790 m_next = sm_first;
791 if (m_next)
792 m_next->m_previous = this;
793 sm_first = this;
b5a98acd
VZ
794}
795
796wxEventHashTable::~wxEventHashTable()
afa039f9
JS
797{
798 if (m_next)
799 m_next->m_previous = m_previous;
800 if (m_previous)
801 m_previous->m_next = m_next;
802 if (sm_first == this)
803 sm_first = m_next;
1a18887b 804
afa039f9
JS
805 Clear();
806}
807
808void wxEventHashTable::Clear()
b5a98acd
VZ
809{
810 size_t i;
811 for(i = 0; i < m_size; i++)
812 {
813 EventTypeTablePointer eTTnode = m_eventTypeTable[i];
814 if (eTTnode)
815 {
816 delete eTTnode;
817 }
818 }
819
afa039f9
JS
820 // Necessary in order to not invoke the
821 // overloaded delete operator when statics are cleaned up
822 if (m_eventTypeTable)
823 delete[] m_eventTypeTable;
824
825 m_eventTypeTable = NULL;
826 m_size = 0;
827}
828
829// Clear all tables
830void wxEventHashTable::ClearAll()
831{
832 wxEventHashTable* table = sm_first;
833 while (table)
834 {
835 table->Clear();
836 table = table->m_next;
837 }
b5a98acd
VZ
838}
839
840bool wxEventHashTable::HandleEvent(wxEvent &event, wxEvtHandler *self)
841{
842 if (m_rebuildHash)
843 {
844 InitHashTable();
1c624631 845 m_rebuildHash = false;
b5a98acd 846 }
1a18887b 847
afa039f9 848 if (!m_eventTypeTable)
1a18887b 849 return false;
b5a98acd
VZ
850
851 // Find all entries for the given event type.
852 wxEventType eventType = event.GetEventType();
853 const EventTypeTablePointer eTTnode = m_eventTypeTable[eventType % m_size];
854 if (eTTnode && eTTnode->eventType == eventType)
855 {
856 // Now start the search for an event handler
857 // that can handle an event with the given ID.
1c624631
VZ
858 const wxEventTableEntryPointerArray&
859 eventEntryTable = eTTnode->eventEntryTable;
b5a98acd 860
1c624631
VZ
861 const size_t count = eventEntryTable.GetCount();
862 for (size_t n = 0; n < count; n++)
b5a98acd 863 {
1c624631
VZ
864 if ( wxEvtHandler::
865 ProcessEventIfMatches(*eventEntryTable[n], self, event) )
b5a98acd 866 {
1c624631 867 return true;
b5a98acd
VZ
868 }
869 }
b5a98acd
VZ
870 }
871
1c624631 872 return false;
b5a98acd
VZ
873}
874
875void wxEventHashTable::InitHashTable()
876{
877 // Loop over the event tables and all its base tables.
878 const wxEventTable *table = &m_table;
879 while (table)
880 {
881 // Retrieve all valid event handler entries
882 const wxEventTableEntry *entry = table->entries;
883 while (entry->m_fn != 0)
884 {
885 // Add the event entry in the Hash.
886 AddEntry(*entry);
887
888 entry++;
889 }
890
891 table = table->baseTable;
892 }
893
894 // Lets free some memory.
895 size_t i;
896 for(i = 0; i < m_size; i++)
897 {
898 EventTypeTablePointer eTTnode = m_eventTypeTable[i];
899 if (eTTnode)
900 {
901 eTTnode->eventEntryTable.Shrink();
902 }
903 }
904}
905
906void wxEventHashTable::AddEntry(const wxEventTableEntry &entry)
907{
afa039f9
JS
908 // This might happen 'accidentally' as the app is exiting
909 if (!m_eventTypeTable)
910 return;
1a18887b 911
b5a98acd
VZ
912 EventTypeTablePointer *peTTnode = &m_eventTypeTable[entry.m_eventType % m_size];
913 EventTypeTablePointer eTTnode = *peTTnode;
914
915 if (eTTnode)
916 {
917 if (eTTnode->eventType != entry.m_eventType)
918 {
919 // Resize the table!
920 GrowEventTypeTable();
921 // Try again to add it.
922 AddEntry(entry);
923 return;
924 }
925 }
926 else
927 {
928 eTTnode = new EventTypeTable;
929 eTTnode->eventType = entry.m_eventType;
930 *peTTnode = eTTnode;
931 }
932
933 // Fill all hash entries between entry.m_id and entry.m_lastId...
934 eTTnode->eventEntryTable.Add(&entry);
935}
936
937void wxEventHashTable::AllocEventTypeTable(size_t size)
938{
939 m_eventTypeTable = new EventTypeTablePointer[size];
940 memset((void *)m_eventTypeTable, 0, sizeof(EventTypeTablePointer)*size);
941 m_size = size;
942}
943
944void wxEventHashTable::GrowEventTypeTable()
945{
946 size_t oldSize = m_size;
947 EventTypeTablePointer *oldEventTypeTable = m_eventTypeTable;
948
949 // TODO: Search the most optimal grow sequence
950 AllocEventTypeTable(/* GetNextPrime(oldSize) */oldSize*2+1);
951
952 for ( size_t i = 0; i < oldSize; /* */ )
953 {
954 EventTypeTablePointer eTToldNode = oldEventTypeTable[i];
955 if (eTToldNode)
956 {
957 EventTypeTablePointer *peTTnode = &m_eventTypeTable[eTToldNode->eventType % m_size];
958 EventTypeTablePointer eTTnode = *peTTnode;
959
960 // Check for collision, we don't want any.
961 if (eTTnode)
962 {
963 GrowEventTypeTable();
964 continue; // Don't increment the counter,
965 // as we still need to add this element.
966 }
967 else
968 {
969 // Get the old value and put it in the new table.
970 *peTTnode = oldEventTypeTable[i];
971 }
972 }
973
974 i++;
975 }
976
977 delete[] oldEventTypeTable;
978}
979
afa039f9 980
456bc6d9
VZ
981// ----------------------------------------------------------------------------
982// wxEvtHandler
983// ----------------------------------------------------------------------------
984
c801d85f
KB
985/*
986 * Event handler
987 */
988
0b746ba8 989wxEvtHandler::wxEvtHandler()
c801d85f 990{
0b746ba8
VZ
991 m_nextHandler = (wxEvtHandler *) NULL;
992 m_previousHandler = (wxEvtHandler *) NULL;
1c624631 993 m_enabled = true;
0b746ba8 994 m_dynamicEvents = (wxList *) NULL;
8e193f38 995 m_pendingEvents = (wxList *) NULL;
7214297d 996#if wxUSE_THREADS
41404da7 997# if !defined(__VISAGECPP__)
8e193f38 998 m_eventsLocker = new wxCriticalSection;
41404da7 999# endif
7214297d 1000#endif
b88c44e7
RD
1001 // no client data (yet)
1002 m_clientData = NULL;
1003 m_clientDataType = wxClientData_None;
c801d85f
KB
1004}
1005
0b746ba8 1006wxEvtHandler::~wxEvtHandler()
c801d85f 1007{
0b746ba8
VZ
1008 // Takes itself out of the list of handlers
1009 if (m_previousHandler)
1010 m_previousHandler->m_nextHandler = m_nextHandler;
1011
1012 if (m_nextHandler)
1013 m_nextHandler->m_previousHandler = m_previousHandler;
1014
1015 if (m_dynamicEvents)
fe71f65c 1016 {
df5168c4
MB
1017 wxList::iterator it = m_dynamicEvents->begin(),
1018 en = m_dynamicEvents->end();
1019 for (;it != en; ++it)
0b746ba8 1020 {
2e4df4bf 1021#if WXWIN_COMPATIBILITY_EVENT_TYPES
df5168c4 1022 wxEventTableEntry *entry = (wxEventTableEntry*)*it;
2e4df4bf 1023#else // !WXWIN_COMPATIBILITY_EVENT_TYPES
df5168c4 1024 wxDynamicEventTableEntry *entry = (wxDynamicEventTableEntry*)*it;
2e4df4bf
VZ
1025#endif // WXWIN_COMPATIBILITY_EVENT_TYPES/!WXWIN_COMPATIBILITY_EVENT_TYPES
1026
1027 if (entry->m_callbackUserData)
1028 delete entry->m_callbackUserData;
0b746ba8 1029 delete entry;
0b746ba8
VZ
1030 }
1031 delete m_dynamicEvents;
1032 };
7214297d 1033
5875d39c
VZ
1034 if (m_pendingEvents)
1035 m_pendingEvents->DeleteContents(true);
8e193f38 1036 delete m_pendingEvents;
7214297d 1037
8e193f38 1038#if wxUSE_THREADS
41404da7 1039# if !defined(__VISAGECPP__)
7214297d 1040 delete m_eventsLocker;
41404da7 1041# endif
b5a98acd 1042
083f7497
JS
1043 // Remove us from wxPendingEvents if necessary.
1044 if(wxPendingEventsLocker)
1045 wxENTER_CRIT_SECT(*wxPendingEventsLocker);
5d9d1b88
RR
1046 if ( wxPendingEvents )
1047 {
1048 // Delete all occurences of this from the list of pending events
1049 while (wxPendingEvents->DeleteObject(this)) { } // Do nothing
083f7497
JS
1050 }
1051 if(wxPendingEventsLocker)
1052 wxLEAVE_CRIT_SECT(*wxPendingEventsLocker);
7214297d 1053#endif
b88c44e7
RD
1054
1055 // we only delete object data, not untyped
1056 if ( m_clientDataType == wxClientData_Object )
1057 delete m_clientObject;
c801d85f
KB
1058}
1059
7214297d 1060#if wxUSE_THREADS
aadbdf11 1061
7214297d
GL
1062bool wxEvtHandler::ProcessThreadEvent(wxEvent& event)
1063{
7214297d 1064 // check that we are really in a child thread
8e193f38
VZ
1065 wxASSERT_MSG( !wxThread::IsMain(),
1066 wxT("use ProcessEvent() in main thread") );
1067
1068 AddPendingEvent(event);
7214297d 1069
1c624631 1070 return true;
8e193f38
VZ
1071}
1072
1c624631
VZ
1073void wxEvtHandler::ClearEventLocker()
1074{
1075#if !defined(__VISAGECPP__)
1076 delete m_eventsLocker;
1077 m_eventsLocker = NULL;
1078#endif
4115960d 1079}
1c624631 1080
8e193f38
VZ
1081#endif // wxUSE_THREADS
1082
1083void wxEvtHandler::AddPendingEvent(wxEvent& event)
1084{
16c1f79c
RR
1085 // 1) Add event to list of pending events of this event handler
1086
7a5c8ac4
VZ
1087 wxEvent *eventCopy = event.Clone();
1088
1089 // we must be able to copy the events here so the event class must
1090 // implement Clone() properly instead of just providing a NULL stab for it
1091 wxCHECK_RET( eventCopy,
1092 _T("events of this type aren't supposed to be posted") );
1093
cd30330f 1094 wxENTER_CRIT_SECT( Lock() );
16c1f79c 1095
8e193f38
VZ
1096 if ( !m_pendingEvents )
1097 m_pendingEvents = new wxList;
7214297d 1098
7a5c8ac4 1099 m_pendingEvents->Append(eventCopy);
7214297d 1100
cd30330f 1101 wxLEAVE_CRIT_SECT( Lock() );
16c1f79c
RR
1102
1103 // 2) Add this event handler to list of event handlers that
1104 // have pending events.
cfe17b74 1105
b568d04f 1106 wxENTER_CRIT_SECT(*wxPendingEventsLocker);
72cdf4c9 1107
8e193f38
VZ
1108 if ( !wxPendingEvents )
1109 wxPendingEvents = new wxList;
4d3a259a 1110 wxPendingEvents->Append(this);
72cdf4c9 1111
ce6d2511 1112 wxLEAVE_CRIT_SECT(*wxPendingEventsLocker);
6164f93c 1113
90e572f1 1114 // 3) Inform the system that new pending events are somewhere,
16c1f79c 1115 // and that these should be processed in idle time.
bf9e3e73 1116 wxWakeUpIdle();
7214297d
GL
1117}
1118
1119void wxEvtHandler::ProcessPendingEvents()
1120{
77e7c556
VZ
1121 // this method is only called by wxApp if this handler does have pending
1122 // events
1123 wxCHECK_RET( m_pendingEvents,
1124 wxT("Please call wxApp::ProcessPendingEvents() instead") );
1125
cd30330f 1126 wxENTER_CRIT_SECT( Lock() );
8e193f38 1127
7b0d5c59
VZ
1128 // we leave the loop once we have processed all events that were present at
1129 // the start of ProcessPendingEvents because otherwise we could get into
1130 // infinite loop if the pending event handler execution resulted in another
1131 // event being posted
1132 size_t n = m_pendingEvents->size();
1133 for ( wxList::compatibility_iterator node = m_pendingEvents->GetFirst();
1134 node;
1135 node = m_pendingEvents->GetFirst() )
8e193f38 1136 {
b1d4dd7a 1137 wxEvent *event = (wxEvent *)node->GetData();
7b0d5c59 1138
5875d39c
VZ
1139 // It's importan we remove event from list before processing it.
1140 // Else a nested event loop, for example from a modal dialog, might
1141 // process the same event again.
1142 m_pendingEvents->Erase(node);
1143
cd30330f 1144 wxLEAVE_CRIT_SECT( Lock() );
7b0d5c59 1145
8e193f38 1146 ProcessEvent(*event);
5875d39c 1147 delete event;
7b0d5c59 1148
cd30330f 1149 wxENTER_CRIT_SECT( Lock() );
cfe17b74 1150
8d7eaf91 1151 if ( --n == 0 )
18dbea4d 1152 break;
7214297d 1153 }
cfe17b74 1154
cd30330f 1155 wxLEAVE_CRIT_SECT( Lock() );
7214297d 1156}
7214297d 1157
c801d85f
KB
1158/*
1159 * Event table stuff
1160 */
1c624631
VZ
1161/* static */ bool
1162wxEvtHandler::ProcessEventIfMatches(const wxEventTableEntryBase& entry,
1163 wxEvtHandler *handler,
1164 wxEvent& event)
1165{
1166 int tableId1 = entry.m_id,
1167 tableId2 = entry.m_lastId;
1168
1169 // match only if the event type is the same and the id is either -1 in
1170 // the event table (meaning "any") or the event id matches the id
1171 // specified in the event table either exactly or by falling into
1172 // range between first and last
1a18887b
WS
1173 if ((tableId1 == wxID_ANY) ||
1174 (tableId2 == wxID_ANY && tableId1 == event.GetId()) ||
1175 (tableId2 != wxID_ANY &&
1c624631
VZ
1176 (event.GetId() >= tableId1 && event.GetId() <= tableId2)))
1177 {
1178 event.Skip(false);
1179 event.m_callbackUserData = entry.m_callbackUserData;
1180
1181#if wxUSE_EXCEPTIONS
1182 if ( wxTheApp )
1183 {
1184 // call the handler via wxApp method which allows the user to catch
1185 // any exceptions which may be thrown by any handler in the program
1186 // in one place
1187 wxTheApp->HandleEvent(handler, (wxEventFunction)entry.m_fn, event);
1188 }
1189 else
8bd2a804 1190#endif // wxUSE_EXCEPTIONS
1c624631
VZ
1191 {
1192 // no need for an extra virtual function call
1193 (handler->*((wxEventFunction) (entry.m_fn)))(event);
1194 }
1c624631
VZ
1195
1196 if (!event.GetSkipped())
1197 return true;
1198 }
1199
1200 return false;
1201}
c801d85f 1202
4caf847c 1203bool wxEvtHandler::TryParent(wxEvent& event)
c801d85f 1204{
4caf847c 1205 if ( wxTheApp && (this != wxTheApp) )
9154d8cf 1206 {
4caf847c
VZ
1207 // Special case: don't pass wxEVT_IDLE to wxApp, since it'll always
1208 // swallow it. wxEVT_IDLE is sent explicitly to wxApp so it will be
1209 // processed appropriately via SearchEventTable.
1210 if ( event.GetEventType() != wxEVT_IDLE )
1211 {
1212 if ( wxTheApp->ProcessEvent(event) )
1c624631 1213 return true;
4caf847c 1214 }
9154d8cf
VZ
1215 }
1216
1c624631 1217 return false;
4caf847c 1218}
0b746ba8 1219
4caf847c
VZ
1220bool wxEvtHandler::ProcessEvent(wxEvent& event)
1221{
9154d8cf
VZ
1222 // allow the application to hook into event processing
1223 if ( wxTheApp )
1224 {
1225 int rc = wxTheApp->FilterEvent(event);
1226 if ( rc != -1 )
1227 {
1228 wxASSERT_MSG( rc == 1 || rc == 0,
1229 _T("unexpected wxApp::FilterEvent return value") );
1230
1231 return rc != 0;
1232 }
1233 //else: proceed normally
1234 }
1235
8e193f38 1236 // An event handler can be enabled or disabled
0b746ba8 1237 if ( GetEvtHandlerEnabled() )
c801d85f 1238 {
22c2307c
VZ
1239 // if we have a validator, it has higher priority than our own event
1240 // table
4caf847c 1241 if ( TryValidator(event) )
1c624631 1242 return true;
4caf847c 1243
22c2307c
VZ
1244 // Handle per-instance dynamic event tables first
1245 if ( m_dynamicEvents && SearchDynamicEventTable(event) )
1c624631 1246 return true;
0b746ba8 1247
b5a98acd
VZ
1248 // Then static per-class event tables
1249 if ( GetEventHashTable().HandleEvent(event, this) )
1c624631 1250 return true;
c801d85f
KB
1251 }
1252
0b746ba8
VZ
1253 // Try going down the event handler chain
1254 if ( GetNextHandler() )
c801d85f 1255 {
0b746ba8 1256 if ( GetNextHandler()->ProcessEvent(event) )
1c624631 1257 return true;
c801d85f 1258 }
c801d85f 1259
22c2307c
VZ
1260 // Finally propagate the event upwards the window chain and/or to the
1261 // application object as necessary
4caf847c 1262 return TryParent(event);
c801d85f
KB
1263}
1264
b5a98acd 1265
c801d85f
KB
1266bool wxEvtHandler::SearchEventTable(wxEventTable& table, wxEvent& event)
1267{
1c624631 1268 const wxEventType eventType = event.GetEventType();
68662769 1269 for ( int i = 0; table.entries[i].m_fn != 0; i++ )
c801d85f 1270 {
68662769 1271 const wxEventTableEntry& entry = table.entries[i];
68662769 1272 if ( eventType == entry.m_eventType )
0b746ba8 1273 {
1c624631
VZ
1274 if ( ProcessEventIfMatches(entry, this, event) )
1275 return true;
0b746ba8 1276 }
c801d85f 1277 }
68662769 1278
1c624631 1279 return false;
c801d85f 1280}
97d7bfb8 1281
debe6624 1282void wxEvtHandler::Connect( int id, int lastId,
77d4384e 1283 int eventType,
0b746ba8 1284 wxObjectEventFunction func,
65b17727
JS
1285 wxObject *userData,
1286 wxEvtHandler* eventSink )
fe71f65c 1287{
cbee8f8d 1288#if WXWIN_COMPATIBILITY_EVENT_TYPES
2e4df4bf 1289 wxEventTableEntry *entry = new wxEventTableEntry;
cbee8f8d
VZ
1290 entry->m_eventType = eventType;
1291 entry->m_id = id;
1292 entry->m_lastId = lastId;
1293 entry->m_fn = func;
1294 entry->m_callbackUserData = userData;
1295#else // !WXWIN_COMPATIBILITY_EVENT_TYPES
0b5eceb6 1296 wxDynamicEventTableEntry *entry =
65b17727 1297 new wxDynamicEventTableEntry(eventType, id, lastId, func, userData, eventSink);
cbee8f8d 1298#endif // WXWIN_COMPATIBILITY_EVENT_TYPES/!WXWIN_COMPATIBILITY_EVENT_TYPES
0b746ba8
VZ
1299
1300 if (!m_dynamicEvents)
1301 m_dynamicEvents = new wxList;
1302
92309201
RD
1303 // Insert at the front of the list so most recent additions are found first
1304 m_dynamicEvents->Insert( (wxObject*) entry );
fe71f65c 1305}
97d7bfb8
RR
1306
1307bool wxEvtHandler::Disconnect( int id, int lastId, wxEventType eventType,
1308 wxObjectEventFunction func,
65b17727
JS
1309 wxObject *userData,
1310 wxEvtHandler* eventSink )
97d7bfb8
RR
1311{
1312 if (!m_dynamicEvents)
1c624631 1313 return false;
cfe17b74 1314
df5168c4 1315 wxList::compatibility_iterator node = m_dynamicEvents->GetFirst();
97d7bfb8
RR
1316 while (node)
1317 {
2e4df4bf 1318#if WXWIN_COMPATIBILITY_EVENT_TYPES
b1d4dd7a 1319 wxEventTableEntry *entry = (wxEventTableEntry*)node->GetData();
2e4df4bf 1320#else // !WXWIN_COMPATIBILITY_EVENT_TYPES
b1d4dd7a 1321 wxDynamicEventTableEntry *entry = (wxDynamicEventTableEntry*)node->GetData();
2e4df4bf
VZ
1322#endif // WXWIN_COMPATIBILITY_EVENT_TYPES/!WXWIN_COMPATIBILITY_EVENT_TYPES
1323
97d7bfb8 1324 if ((entry->m_id == id) &&
1a18887b 1325 ((entry->m_lastId == lastId) || (lastId == wxID_ANY)) &&
97d7bfb8
RR
1326 ((entry->m_eventType == eventType) || (eventType == wxEVT_NULL)) &&
1327 ((entry->m_fn == func) || (func == (wxObjectEventFunction)NULL)) &&
65b17727 1328 ((entry->m_eventSink == eventSink) || (eventSink == (wxEvtHandler*)NULL)) &&
97d7bfb8
RR
1329 ((entry->m_callbackUserData == userData) || (userData == (wxObject*)NULL)))
1330 {
2e4df4bf
VZ
1331 if (entry->m_callbackUserData)
1332 delete entry->m_callbackUserData;
df5168c4 1333 m_dynamicEvents->Erase( node );
97d7bfb8 1334 delete entry;
1c624631 1335 return true;
97d7bfb8 1336 }
b1d4dd7a 1337 node = node->GetNext();
97d7bfb8 1338 }
1c624631 1339 return false;
97d7bfb8 1340}
fe71f65c
RR
1341
1342bool wxEvtHandler::SearchDynamicEventTable( wxEvent& event )
1343{
1c624631 1344 wxCHECK_MSG( m_dynamicEvents, false,
223d09f6 1345 wxT("caller should check that we have dynamic events") );
0b746ba8 1346
df5168c4 1347 wxList::compatibility_iterator node = m_dynamicEvents->GetFirst();
0b746ba8 1348 while (node)
fe71f65c 1349 {
2e4df4bf 1350#if WXWIN_COMPATIBILITY_EVENT_TYPES
1c624631 1351 wxEventTableEntry *entry = (wxEventTableEntry*)node->GetData();
2e4df4bf 1352#else // !WXWIN_COMPATIBILITY_EVENT_TYPES
1c624631 1353 wxDynamicEventTableEntry *entry = (wxDynamicEventTableEntry*)node->GetData();
2e4df4bf 1354#endif // WXWIN_COMPATIBILITY_EVENT_TYPES/!WXWIN_COMPATIBILITY_EVENT_TYPES
0b746ba8 1355
7fce89b9
VZ
1356 // get next node before (maybe) calling the event handler as it could
1357 // call Disconnect() invalidating the current node
1358 node = node->GetNext();
1359
ab1931f9 1360 if ((event.GetEventType() == entry->m_eventType) && (entry->m_fn != 0))
0b746ba8 1361 {
1c624631 1362 wxEvtHandler *handler =
65b17727 1363#if !WXWIN_COMPATIBILITY_EVENT_TYPES
1c624631
VZ
1364 entry->m_eventSink ? entry->m_eventSink
1365 :
b5a98acd 1366#endif
1c624631 1367 this;
0b746ba8 1368
1c624631
VZ
1369 if ( ProcessEventIfMatches(*entry, handler, event) )
1370 {
1371 return true;
0b746ba8
VZ
1372 }
1373 }
7b678698 1374 }
1c624631
VZ
1375
1376 return false;
4115960d 1377}
fe71f65c 1378
b88c44e7
RD
1379void wxEvtHandler::DoSetClientObject( wxClientData *data )
1380{
1381 wxASSERT_MSG( m_clientDataType != wxClientData_Void,
1382 wxT("can't have both object and void client data") );
1383
1384 if ( m_clientObject )
1385 delete m_clientObject;
1386
1387 m_clientObject = data;
1388 m_clientDataType = wxClientData_Object;
1389}
1390
1391wxClientData *wxEvtHandler::DoGetClientObject() const
1392{
1393 // it's not an error to call GetClientObject() on a window which doesn't
1394 // have client data at all - NULL will be returned
1395 wxASSERT_MSG( m_clientDataType != wxClientData_Void,
1396 wxT("this window doesn't have object client data") );
1397
1398 return m_clientObject;
1399}
1400
1401void wxEvtHandler::DoSetClientData( void *data )
1402{
1403 wxASSERT_MSG( m_clientDataType != wxClientData_Object,
1404 wxT("can't have both object and void client data") );
1405
1406 m_clientData = data;
1407 m_clientDataType = wxClientData_Void;
1408}
1409
1410void *wxEvtHandler::DoGetClientData() const
1411{
1412 // it's not an error to call GetClientData() on a window which doesn't have
1413 // client data at all - NULL will be returned
1414 wxASSERT_MSG( m_clientDataType != wxClientData_Object,
1415 wxT("this window doesn't have void client data") );
1416
1417 return m_clientData;
1418}
1419
4caf847c
VZ
1420#endif // wxUSE_BASE
1421
e90c1d2a
VZ
1422#if wxUSE_GUI
1423
e702ff0f
JS
1424// Find a window with the focus, that is also a descendant of the given window.
1425// This is used to determine the window to initially send commands to.
1426wxWindow* wxFindFocusDescendant(wxWindow* ancestor)
1427{
1428 // Process events starting with the window with the focus, if any.
1429 wxWindow* focusWin = wxWindow::FindFocus();
1430 wxWindow* win = focusWin;
1431
1432 // Check if this is a descendant of this frame.
1433 // If not, win will be set to NULL.
1434 while (win)
1435 {
1436 if (win == ancestor)
1437 break;
1438 else
1439 win = win->GetParent();
1440 }
1441 if (win == (wxWindow*) NULL)
1442 focusWin = (wxWindow*) NULL;
1443
1444 return focusWin;
1445}
1446
e90c1d2a 1447#endif // wxUSE_GUI