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