]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/common/event.cpp
No changes, just get rid of some wxT()s in wxString unit test.
[wxWidgets.git] / src / common / event.cpp
... / ...
CommitLineData
1/////////////////////////////////////////////////////////////////////////////
2// Name: src/common/event.cpp
3// Purpose: Event classes
4// Author: Julian Smart
5// Modified by:
6// Created: 01/02/97
7// RCS-ID: $Id$
8// Copyright: (c) Julian Smart
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
20// For compilers that support precompilation, includes "wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
24 #pragma hdrstop
25#endif
26
27#include "wx/event.h"
28#include "wx/eventfilter.h"
29#include "wx/evtloop.h"
30
31#ifndef WX_PRECOMP
32 #include "wx/list.h"
33 #include "wx/log.h"
34 #include "wx/app.h"
35 #include "wx/utils.h"
36 #include "wx/stopwatch.h"
37 #include "wx/module.h"
38
39 #if wxUSE_GUI
40 #include "wx/window.h"
41 #include "wx/control.h"
42 #include "wx/dc.h"
43 #include "wx/spinbutt.h"
44 #include "wx/textctrl.h"
45 #include "wx/validate.h"
46 #endif // wxUSE_GUI
47#endif
48
49#include "wx/thread.h"
50
51#if wxUSE_BASE
52 #include "wx/scopedptr.h"
53
54 wxDECLARE_SCOPED_PTR(wxEvent, wxEventPtr)
55 wxDEFINE_SCOPED_PTR(wxEvent, wxEventPtr)
56#endif // wxUSE_BASE
57
58// ----------------------------------------------------------------------------
59// wxWin macros
60// ----------------------------------------------------------------------------
61
62#if wxUSE_BASE
63 IMPLEMENT_DYNAMIC_CLASS(wxEvtHandler, wxObject)
64 IMPLEMENT_ABSTRACT_CLASS(wxEvent, wxObject)
65 IMPLEMENT_DYNAMIC_CLASS(wxIdleEvent, wxEvent)
66 IMPLEMENT_DYNAMIC_CLASS(wxThreadEvent, wxEvent)
67#endif // wxUSE_BASE
68
69#if wxUSE_GUI
70 IMPLEMENT_DYNAMIC_CLASS(wxCommandEvent, wxEvent)
71 IMPLEMENT_DYNAMIC_CLASS(wxNotifyEvent, wxCommandEvent)
72 IMPLEMENT_DYNAMIC_CLASS(wxScrollEvent, wxCommandEvent)
73 IMPLEMENT_DYNAMIC_CLASS(wxScrollWinEvent, wxEvent)
74 IMPLEMENT_DYNAMIC_CLASS(wxMouseEvent, wxEvent)
75 IMPLEMENT_DYNAMIC_CLASS(wxKeyEvent, wxEvent)
76 IMPLEMENT_DYNAMIC_CLASS(wxSizeEvent, wxEvent)
77 IMPLEMENT_DYNAMIC_CLASS(wxPaintEvent, wxEvent)
78 IMPLEMENT_DYNAMIC_CLASS(wxNcPaintEvent, wxEvent)
79 IMPLEMENT_DYNAMIC_CLASS(wxEraseEvent, wxEvent)
80 IMPLEMENT_DYNAMIC_CLASS(wxMoveEvent, wxEvent)
81 IMPLEMENT_DYNAMIC_CLASS(wxFocusEvent, wxEvent)
82 IMPLEMENT_DYNAMIC_CLASS(wxChildFocusEvent, wxCommandEvent)
83 IMPLEMENT_DYNAMIC_CLASS(wxCloseEvent, wxEvent)
84 IMPLEMENT_DYNAMIC_CLASS(wxShowEvent, wxEvent)
85 IMPLEMENT_DYNAMIC_CLASS(wxMaximizeEvent, wxEvent)
86 IMPLEMENT_DYNAMIC_CLASS(wxIconizeEvent, wxEvent)
87 IMPLEMENT_DYNAMIC_CLASS(wxMenuEvent, wxEvent)
88 IMPLEMENT_DYNAMIC_CLASS(wxJoystickEvent, wxEvent)
89 IMPLEMENT_DYNAMIC_CLASS(wxDropFilesEvent, wxEvent)
90 IMPLEMENT_DYNAMIC_CLASS(wxActivateEvent, wxEvent)
91 IMPLEMENT_DYNAMIC_CLASS(wxInitDialogEvent, wxEvent)
92 IMPLEMENT_DYNAMIC_CLASS(wxSetCursorEvent, wxEvent)
93 IMPLEMENT_DYNAMIC_CLASS(wxSysColourChangedEvent, wxEvent)
94 IMPLEMENT_DYNAMIC_CLASS(wxDisplayChangedEvent, wxEvent)
95 IMPLEMENT_DYNAMIC_CLASS(wxUpdateUIEvent, wxCommandEvent)
96 IMPLEMENT_DYNAMIC_CLASS(wxNavigationKeyEvent, wxEvent)
97 IMPLEMENT_DYNAMIC_CLASS(wxPaletteChangedEvent, wxEvent)
98 IMPLEMENT_DYNAMIC_CLASS(wxQueryNewPaletteEvent, wxEvent)
99 IMPLEMENT_DYNAMIC_CLASS(wxWindowCreateEvent, wxEvent)
100 IMPLEMENT_DYNAMIC_CLASS(wxWindowDestroyEvent, wxEvent)
101 IMPLEMENT_DYNAMIC_CLASS(wxHelpEvent, wxCommandEvent)
102 IMPLEMENT_DYNAMIC_CLASS(wxContextMenuEvent, wxCommandEvent)
103 IMPLEMENT_DYNAMIC_CLASS(wxMouseCaptureChangedEvent, wxEvent)
104 IMPLEMENT_DYNAMIC_CLASS(wxMouseCaptureLostEvent, wxEvent)
105 IMPLEMENT_DYNAMIC_CLASS(wxClipboardTextEvent, wxCommandEvent)
106#endif // wxUSE_GUI
107
108#if wxUSE_BASE
109
110const wxEventTable *wxEvtHandler::GetEventTable() const
111 { return &wxEvtHandler::sm_eventTable; }
112
113const wxEventTable wxEvtHandler::sm_eventTable =
114 { (const wxEventTable *)NULL, &wxEvtHandler::sm_eventTableEntries[0] };
115
116wxEventHashTable &wxEvtHandler::GetEventHashTable() const
117 { return wxEvtHandler::sm_eventHashTable; }
118
119wxEventHashTable wxEvtHandler::sm_eventHashTable(wxEvtHandler::sm_eventTable);
120
121const wxEventTableEntry wxEvtHandler::sm_eventTableEntries[] =
122 { wxDECLARE_EVENT_TABLE_TERMINATOR() };
123
124
125// wxUSE_MEMORY_TRACING considers memory freed from the static objects dtors
126// leaked, so we need to manually clean up all event tables before checking for
127// the memory leaks when using it, however this breaks re-initializing the
128// library (i.e. repeated calls to wxInitialize/wxUninitialize) because the
129// event tables won't be rebuilt the next time, so disable this by default
130#if wxUSE_MEMORY_TRACING
131
132class wxEventTableEntryModule: public wxModule
133{
134public:
135 wxEventTableEntryModule() { }
136 virtual bool OnInit() { return true; }
137 virtual void OnExit() { wxEventHashTable::ClearAll(); }
138
139 DECLARE_DYNAMIC_CLASS(wxEventTableEntryModule)
140};
141
142IMPLEMENT_DYNAMIC_CLASS(wxEventTableEntryModule, wxModule)
143
144#endif // wxUSE_MEMORY_TRACING
145
146// ----------------------------------------------------------------------------
147// global variables
148// ----------------------------------------------------------------------------
149
150// common event types are defined here, other event types are defined by the
151// components which use them
152
153const wxEventType wxEVT_FIRST = 10000;
154const wxEventType wxEVT_USER_FIRST = wxEVT_FIRST + 2000;
155const wxEventType wxEVT_NULL = wxNewEventType();
156
157wxDEFINE_EVENT( wxEVT_IDLE, wxIdleEvent );
158
159// Thread and asynchronous call events
160wxDEFINE_EVENT( wxEVT_THREAD, wxThreadEvent );
161wxDEFINE_EVENT( wxEVT_ASYNC_METHOD_CALL, wxAsyncMethodCallEvent );
162
163#endif // wxUSE_BASE
164
165#if wxUSE_GUI
166
167wxDEFINE_EVENT( wxEVT_BUTTON, wxCommandEvent );
168wxDEFINE_EVENT( wxEVT_CHECKBOX, wxCommandEvent );
169wxDEFINE_EVENT( wxEVT_CHOICE, wxCommandEvent );
170wxDEFINE_EVENT( wxEVT_LISTBOX, wxCommandEvent );
171wxDEFINE_EVENT( wxEVT_LISTBOX_DCLICK, wxCommandEvent );
172wxDEFINE_EVENT( wxEVT_CHECKLISTBOX, wxCommandEvent );
173wxDEFINE_EVENT( wxEVT_MENU, wxCommandEvent );
174wxDEFINE_EVENT( wxEVT_SLIDER, wxCommandEvent );
175wxDEFINE_EVENT( wxEVT_RADIOBOX, wxCommandEvent );
176wxDEFINE_EVENT( wxEVT_RADIOBUTTON, wxCommandEvent );
177wxDEFINE_EVENT( wxEVT_SCROLLBAR, wxCommandEvent );
178wxDEFINE_EVENT( wxEVT_VLBOX, wxCommandEvent );
179wxDEFINE_EVENT( wxEVT_COMBOBOX, wxCommandEvent );
180wxDEFINE_EVENT( wxEVT_TOOL_RCLICKED, wxCommandEvent );
181wxDEFINE_EVENT( wxEVT_TOOL_ENTER, wxCommandEvent );
182wxDEFINE_EVENT( wxEVT_SPINCTRL, wxCommandEvent );
183wxDEFINE_EVENT( wxEVT_SPINCTRLDOUBLE, wxCommandEvent );
184wxDEFINE_EVENT( wxEVT_TOOL_DROPDOWN, wxCommandEvent );
185wxDEFINE_EVENT( wxEVT_COMBOBOX_DROPDOWN, wxCommandEvent);
186wxDEFINE_EVENT( wxEVT_COMBOBOX_CLOSEUP, wxCommandEvent);
187
188// Mouse event types
189wxDEFINE_EVENT( wxEVT_LEFT_DOWN, wxMouseEvent );
190wxDEFINE_EVENT( wxEVT_LEFT_UP, wxMouseEvent );
191wxDEFINE_EVENT( wxEVT_MIDDLE_DOWN, wxMouseEvent );
192wxDEFINE_EVENT( wxEVT_MIDDLE_UP, wxMouseEvent );
193wxDEFINE_EVENT( wxEVT_RIGHT_DOWN, wxMouseEvent );
194wxDEFINE_EVENT( wxEVT_RIGHT_UP, wxMouseEvent );
195wxDEFINE_EVENT( wxEVT_MOTION, wxMouseEvent );
196wxDEFINE_EVENT( wxEVT_ENTER_WINDOW, wxMouseEvent );
197wxDEFINE_EVENT( wxEVT_LEAVE_WINDOW, wxMouseEvent );
198wxDEFINE_EVENT( wxEVT_LEFT_DCLICK, wxMouseEvent );
199wxDEFINE_EVENT( wxEVT_MIDDLE_DCLICK, wxMouseEvent );
200wxDEFINE_EVENT( wxEVT_RIGHT_DCLICK, wxMouseEvent );
201wxDEFINE_EVENT( wxEVT_SET_FOCUS, wxFocusEvent );
202wxDEFINE_EVENT( wxEVT_KILL_FOCUS, wxFocusEvent );
203wxDEFINE_EVENT( wxEVT_CHILD_FOCUS, wxChildFocusEvent );
204wxDEFINE_EVENT( wxEVT_MOUSEWHEEL, wxMouseEvent );
205wxDEFINE_EVENT( wxEVT_AUX1_DOWN, wxMouseEvent );
206wxDEFINE_EVENT( wxEVT_AUX1_UP, wxMouseEvent );
207wxDEFINE_EVENT( wxEVT_AUX1_DCLICK, wxMouseEvent );
208wxDEFINE_EVENT( wxEVT_AUX2_DOWN, wxMouseEvent );
209wxDEFINE_EVENT( wxEVT_AUX2_UP, wxMouseEvent );
210wxDEFINE_EVENT( wxEVT_AUX2_DCLICK, wxMouseEvent );
211
212// Character input event type
213wxDEFINE_EVENT( wxEVT_CHAR, wxKeyEvent );
214wxDEFINE_EVENT( wxEVT_AFTER_CHAR, wxKeyEvent );
215wxDEFINE_EVENT( wxEVT_CHAR_HOOK, wxKeyEvent );
216wxDEFINE_EVENT( wxEVT_NAVIGATION_KEY, wxNavigationKeyEvent );
217wxDEFINE_EVENT( wxEVT_KEY_DOWN, wxKeyEvent );
218wxDEFINE_EVENT( wxEVT_KEY_UP, wxKeyEvent );
219#if wxUSE_HOTKEY
220wxDEFINE_EVENT( wxEVT_HOTKEY, wxKeyEvent );
221#endif
222
223// Set cursor event
224wxDEFINE_EVENT( wxEVT_SET_CURSOR, wxSetCursorEvent );
225
226// wxScrollbar and wxSlider event identifiers
227wxDEFINE_EVENT( wxEVT_SCROLL_TOP, wxScrollEvent );
228wxDEFINE_EVENT( wxEVT_SCROLL_BOTTOM, wxScrollEvent );
229wxDEFINE_EVENT( wxEVT_SCROLL_LINEUP, wxScrollEvent );
230wxDEFINE_EVENT( wxEVT_SCROLL_LINEDOWN, wxScrollEvent );
231wxDEFINE_EVENT( wxEVT_SCROLL_PAGEUP, wxScrollEvent );
232wxDEFINE_EVENT( wxEVT_SCROLL_PAGEDOWN, wxScrollEvent );
233wxDEFINE_EVENT( wxEVT_SCROLL_THUMBTRACK, wxScrollEvent );
234wxDEFINE_EVENT( wxEVT_SCROLL_THUMBRELEASE, wxScrollEvent );
235wxDEFINE_EVENT( wxEVT_SCROLL_CHANGED, wxScrollEvent );
236
237// Due to a bug in older wx versions, wxSpinEvents were being sent with type of
238// wxEVT_SCROLL_LINEUP, wxEVT_SCROLL_LINEDOWN and wxEVT_SCROLL_THUMBTRACK. But
239// with the type-safe events in place, these event types are associated with
240// wxScrollEvent. To allow handling of spin events, new event types have been
241// defined in spinbutt.h/spinnbuttcmn.cpp. To maintain backward compatibility
242// the spin event types are being initialized with the scroll event types.
243
244#if wxUSE_SPINBTN
245
246wxDEFINE_EVENT_ALIAS( wxEVT_SPIN_UP, wxSpinEvent, wxEVT_SCROLL_LINEUP );
247wxDEFINE_EVENT_ALIAS( wxEVT_SPIN_DOWN, wxSpinEvent, wxEVT_SCROLL_LINEDOWN );
248wxDEFINE_EVENT_ALIAS( wxEVT_SPIN, wxSpinEvent, wxEVT_SCROLL_THUMBTRACK );
249
250#endif // wxUSE_SPINBTN
251
252// Scroll events from wxWindow
253wxDEFINE_EVENT( wxEVT_SCROLLWIN_TOP, wxScrollWinEvent );
254wxDEFINE_EVENT( wxEVT_SCROLLWIN_BOTTOM, wxScrollWinEvent );
255wxDEFINE_EVENT( wxEVT_SCROLLWIN_LINEUP, wxScrollWinEvent );
256wxDEFINE_EVENT( wxEVT_SCROLLWIN_LINEDOWN, wxScrollWinEvent );
257wxDEFINE_EVENT( wxEVT_SCROLLWIN_PAGEUP, wxScrollWinEvent );
258wxDEFINE_EVENT( wxEVT_SCROLLWIN_PAGEDOWN, wxScrollWinEvent );
259wxDEFINE_EVENT( wxEVT_SCROLLWIN_THUMBTRACK, wxScrollWinEvent );
260wxDEFINE_EVENT( wxEVT_SCROLLWIN_THUMBRELEASE, wxScrollWinEvent );
261
262// System events
263wxDEFINE_EVENT( wxEVT_SIZE, wxSizeEvent );
264wxDEFINE_EVENT( wxEVT_SIZING, wxSizeEvent );
265wxDEFINE_EVENT( wxEVT_MOVE, wxMoveEvent );
266wxDEFINE_EVENT( wxEVT_MOVING, wxMoveEvent );
267wxDEFINE_EVENT( wxEVT_MOVE_START, wxMoveEvent );
268wxDEFINE_EVENT( wxEVT_MOVE_END, wxMoveEvent );
269wxDEFINE_EVENT( wxEVT_CLOSE_WINDOW, wxCloseEvent );
270wxDEFINE_EVENT( wxEVT_END_SESSION, wxCloseEvent );
271wxDEFINE_EVENT( wxEVT_QUERY_END_SESSION, wxCloseEvent );
272wxDEFINE_EVENT( wxEVT_HIBERNATE, wxActivateEvent );
273wxDEFINE_EVENT( wxEVT_ACTIVATE_APP, wxActivateEvent );
274wxDEFINE_EVENT( wxEVT_ACTIVATE, wxActivateEvent );
275wxDEFINE_EVENT( wxEVT_CREATE, wxWindowCreateEvent );
276wxDEFINE_EVENT( wxEVT_DESTROY, wxWindowDestroyEvent );
277wxDEFINE_EVENT( wxEVT_SHOW, wxShowEvent );
278wxDEFINE_EVENT( wxEVT_ICONIZE, wxIconizeEvent );
279wxDEFINE_EVENT( wxEVT_MAXIMIZE, wxMaximizeEvent );
280wxDEFINE_EVENT( wxEVT_MOUSE_CAPTURE_CHANGED, wxMouseCaptureChangedEvent );
281wxDEFINE_EVENT( wxEVT_MOUSE_CAPTURE_LOST, wxMouseCaptureLostEvent );
282wxDEFINE_EVENT( wxEVT_PAINT, wxPaintEvent );
283wxDEFINE_EVENT( wxEVT_ERASE_BACKGROUND, wxEraseEvent );
284wxDEFINE_EVENT( wxEVT_NC_PAINT, wxNcPaintEvent );
285wxDEFINE_EVENT( wxEVT_MENU_OPEN, wxMenuEvent );
286wxDEFINE_EVENT( wxEVT_MENU_CLOSE, wxMenuEvent );
287wxDEFINE_EVENT( wxEVT_MENU_HIGHLIGHT, wxMenuEvent );
288wxDEFINE_EVENT( wxEVT_CONTEXT_MENU, wxContextMenuEvent );
289wxDEFINE_EVENT( wxEVT_SYS_COLOUR_CHANGED, wxSysColourChangedEvent );
290wxDEFINE_EVENT( wxEVT_DISPLAY_CHANGED, wxDisplayChangedEvent );
291wxDEFINE_EVENT( wxEVT_QUERY_NEW_PALETTE, wxQueryNewPaletteEvent );
292wxDEFINE_EVENT( wxEVT_PALETTE_CHANGED, wxPaletteChangedEvent );
293wxDEFINE_EVENT( wxEVT_JOY_BUTTON_DOWN, wxJoystickEvent );
294wxDEFINE_EVENT( wxEVT_JOY_BUTTON_UP, wxJoystickEvent );
295wxDEFINE_EVENT( wxEVT_JOY_MOVE, wxJoystickEvent );
296wxDEFINE_EVENT( wxEVT_JOY_ZMOVE, wxJoystickEvent );
297wxDEFINE_EVENT( wxEVT_DROP_FILES, wxDropFilesEvent );
298wxDEFINE_EVENT( wxEVT_INIT_DIALOG, wxInitDialogEvent );
299wxDEFINE_EVENT( wxEVT_UPDATE_UI, wxUpdateUIEvent );
300
301// Clipboard events
302wxDEFINE_EVENT( wxEVT_TEXT_COPY, wxClipboardTextEvent );
303wxDEFINE_EVENT( wxEVT_TEXT_CUT, wxClipboardTextEvent );
304wxDEFINE_EVENT( wxEVT_TEXT_PASTE, wxClipboardTextEvent );
305
306// Generic command events
307// Note: a click is a higher-level event than button down/up
308wxDEFINE_EVENT( wxEVT_COMMAND_LEFT_CLICK, wxCommandEvent );
309wxDEFINE_EVENT( wxEVT_COMMAND_LEFT_DCLICK, wxCommandEvent );
310wxDEFINE_EVENT( wxEVT_COMMAND_RIGHT_CLICK, wxCommandEvent );
311wxDEFINE_EVENT( wxEVT_COMMAND_RIGHT_DCLICK, wxCommandEvent );
312wxDEFINE_EVENT( wxEVT_COMMAND_SET_FOCUS, wxCommandEvent );
313wxDEFINE_EVENT( wxEVT_COMMAND_KILL_FOCUS, wxCommandEvent );
314wxDEFINE_EVENT( wxEVT_COMMAND_ENTER, wxCommandEvent );
315
316// Help events
317wxDEFINE_EVENT( wxEVT_HELP, wxHelpEvent );
318wxDEFINE_EVENT( wxEVT_DETAILED_HELP, wxHelpEvent );
319
320#endif // wxUSE_GUI
321
322#if wxUSE_BASE
323
324wxIdleMode wxIdleEvent::sm_idleMode = wxIDLE_PROCESS_ALL;
325
326// ============================================================================
327// implementation
328// ============================================================================
329
330// ----------------------------------------------------------------------------
331// event initialization
332// ----------------------------------------------------------------------------
333
334int wxNewEventType()
335{
336 // MT-FIXME
337 static int s_lastUsedEventType = wxEVT_FIRST;
338
339 return s_lastUsedEventType++;
340}
341// ----------------------------------------------------------------------------
342// wxEventFunctor
343// ----------------------------------------------------------------------------
344
345wxEventFunctor::~wxEventFunctor()
346{
347}
348
349// ----------------------------------------------------------------------------
350// wxEvent
351// ----------------------------------------------------------------------------
352
353/*
354 * General wxWidgets events, covering all interesting things that might happen
355 * (button clicking, resizing, setting text in widgets, etc.).
356 *
357 * For each completely new event type, derive a new event class.
358 *
359 */
360
361wxEvent::wxEvent(int theId, wxEventType commandType)
362{
363 m_eventType = commandType;
364 m_eventObject = NULL;
365 m_timeStamp = 0;
366 m_id = theId;
367 m_skipped = false;
368 m_callbackUserData = NULL;
369 m_handlerToProcessOnlyIn = NULL;
370 m_isCommandEvent = false;
371 m_propagationLevel = wxEVENT_PROPAGATE_NONE;
372 m_propagatedFrom = NULL;
373 m_wasProcessed = false;
374 m_willBeProcessedAgain = false;
375}
376
377wxEvent::wxEvent(const wxEvent& src)
378 : wxObject(src)
379 , m_eventObject(src.m_eventObject)
380 , m_eventType(src.m_eventType)
381 , m_timeStamp(src.m_timeStamp)
382 , m_id(src.m_id)
383 , m_callbackUserData(src.m_callbackUserData)
384 , m_handlerToProcessOnlyIn(NULL)
385 , m_propagationLevel(src.m_propagationLevel)
386 , m_propagatedFrom(NULL)
387 , m_skipped(src.m_skipped)
388 , m_isCommandEvent(src.m_isCommandEvent)
389 , m_wasProcessed(false)
390 , m_willBeProcessedAgain(false)
391{
392}
393
394wxEvent& wxEvent::operator=(const wxEvent& src)
395{
396 wxObject::operator=(src);
397
398 m_eventObject = src.m_eventObject;
399 m_eventType = src.m_eventType;
400 m_timeStamp = src.m_timeStamp;
401 m_id = src.m_id;
402 m_callbackUserData = src.m_callbackUserData;
403 m_handlerToProcessOnlyIn = NULL;
404 m_propagationLevel = src.m_propagationLevel;
405 m_propagatedFrom = NULL;
406 m_skipped = src.m_skipped;
407 m_isCommandEvent = src.m_isCommandEvent;
408
409 // don't change m_wasProcessed
410
411 // While the original again could be passed to another handler, this one
412 // isn't going to be processed anywhere else by default.
413 m_willBeProcessedAgain = false;
414
415 return *this;
416}
417
418#endif // wxUSE_BASE
419
420#if wxUSE_GUI
421
422// ----------------------------------------------------------------------------
423// wxCommandEvent
424// ----------------------------------------------------------------------------
425
426wxCommandEvent::wxCommandEvent(wxEventType commandType, int theId)
427 : wxEvent(theId, commandType)
428{
429 m_clientData = NULL;
430 m_clientObject = NULL;
431 m_isCommandEvent = true;
432
433 // the command events are propagated upwards by default
434 m_propagationLevel = wxEVENT_PROPAGATE_MAX;
435}
436
437wxString wxCommandEvent::GetString() const
438{
439 if (m_eventType != wxEVT_TEXT || !m_eventObject)
440 {
441 return m_cmdString;
442 }
443 else
444 {
445#if wxUSE_TEXTCTRL
446 wxTextCtrl *txt = wxDynamicCast(m_eventObject, wxTextCtrl);
447 if ( txt )
448 return txt->GetValue();
449 else
450#endif // wxUSE_TEXTCTRL
451 return m_cmdString;
452 }
453}
454
455// ----------------------------------------------------------------------------
456// wxUpdateUIEvent
457// ----------------------------------------------------------------------------
458
459#if wxUSE_LONGLONG
460wxLongLong wxUpdateUIEvent::sm_lastUpdate = 0;
461#endif
462
463long wxUpdateUIEvent::sm_updateInterval = 0;
464
465wxUpdateUIMode wxUpdateUIEvent::sm_updateMode = wxUPDATE_UI_PROCESS_ALL;
466
467// Can we update?
468bool wxUpdateUIEvent::CanUpdate(wxWindowBase *win)
469{
470 // Don't update if we've switched global updating off
471 // and this window doesn't support updates.
472 if (win &&
473 (GetMode() == wxUPDATE_UI_PROCESS_SPECIFIED &&
474 ((win->GetExtraStyle() & wxWS_EX_PROCESS_UI_UPDATES) == 0)))
475 return false;
476
477 // Don't update children of the hidden windows: this is useless as any
478 // change to their state won't be seen by the user anyhow. Notice that this
479 // argument doesn't apply to the hidden windows (with visible parent)
480 // themselves as they could be shown by their EVT_UPDATE_UI handler.
481 if ( win->GetParent() && !win->GetParent()->IsShownOnScreen() )
482 return false;
483
484 if (sm_updateInterval == -1)
485 return false;
486
487 if (sm_updateInterval == 0)
488 return true;
489
490#if wxUSE_STOPWATCH && wxUSE_LONGLONG
491 wxLongLong now = wxGetLocalTimeMillis();
492 if (now > (sm_lastUpdate + sm_updateInterval))
493 {
494 return true;
495 }
496
497 return false;
498#else
499 // If we don't have wxStopWatch or wxLongLong, we
500 // should err on the safe side and update now anyway.
501 return true;
502#endif
503}
504
505// Reset the update time to provide a delay until the next
506// time we should update
507void wxUpdateUIEvent::ResetUpdateTime()
508{
509#if wxUSE_STOPWATCH && wxUSE_LONGLONG
510 if (sm_updateInterval > 0)
511 {
512 wxLongLong now = wxGetLocalTimeMillis();
513 if (now > (sm_lastUpdate + sm_updateInterval))
514 {
515 sm_lastUpdate = now;
516 }
517 }
518#endif
519}
520
521// ----------------------------------------------------------------------------
522// wxScrollEvent
523// ----------------------------------------------------------------------------
524
525wxScrollEvent::wxScrollEvent(wxEventType commandType,
526 int id,
527 int pos,
528 int orient)
529 : wxCommandEvent(commandType, id)
530{
531 m_extraLong = orient;
532 m_commandInt = pos;
533}
534
535// ----------------------------------------------------------------------------
536// wxScrollWinEvent
537// ----------------------------------------------------------------------------
538
539wxScrollWinEvent::wxScrollWinEvent(wxEventType commandType,
540 int pos,
541 int orient)
542{
543 m_eventType = commandType;
544 m_extraLong = orient;
545 m_commandInt = pos;
546}
547
548// ----------------------------------------------------------------------------
549// wxMouseEvent
550// ----------------------------------------------------------------------------
551
552wxMouseEvent::wxMouseEvent(wxEventType commandType)
553{
554 m_eventType = commandType;
555
556 m_x = 0;
557 m_y = 0;
558
559 m_leftDown = false;
560 m_middleDown = false;
561 m_rightDown = false;
562 m_aux1Down = false;
563 m_aux2Down = false;
564
565 m_clickCount = -1;
566
567 m_wheelAxis = wxMOUSE_WHEEL_VERTICAL;
568 m_wheelRotation = 0;
569 m_wheelDelta = 0;
570 m_linesPerAction = 0;
571 m_columnsPerAction = 0;
572}
573
574void wxMouseEvent::Assign(const wxMouseEvent& event)
575{
576 wxEvent::operator=(event);
577
578 // Borland C++ 5.82 doesn't compile an explicit call to an implicitly
579 // defined operator=() so need to do it this way:
580 *static_cast<wxMouseState *>(this) = event;
581
582 m_x = event.m_x;
583 m_y = event.m_y;
584
585 m_leftDown = event.m_leftDown;
586 m_middleDown = event.m_middleDown;
587 m_rightDown = event.m_rightDown;
588 m_aux1Down = event.m_aux1Down;
589 m_aux2Down = event.m_aux2Down;
590
591 m_wheelRotation = event.m_wheelRotation;
592 m_wheelDelta = event.m_wheelDelta;
593 m_linesPerAction = event.m_linesPerAction;
594 m_columnsPerAction = event.m_columnsPerAction;
595 m_wheelAxis = event.m_wheelAxis;
596}
597
598// return true if was a button dclick event
599bool wxMouseEvent::ButtonDClick(int but) const
600{
601 switch (but)
602 {
603 default:
604 wxFAIL_MSG(wxT("invalid parameter in wxMouseEvent::ButtonDClick"));
605 // fall through
606
607 case wxMOUSE_BTN_ANY:
608 return (LeftDClick() || MiddleDClick() || RightDClick() ||
609 Aux1DClick() || Aux2DClick());
610
611 case wxMOUSE_BTN_LEFT:
612 return LeftDClick();
613
614 case wxMOUSE_BTN_MIDDLE:
615 return MiddleDClick();
616
617 case wxMOUSE_BTN_RIGHT:
618 return RightDClick();
619
620 case wxMOUSE_BTN_AUX1:
621 return Aux1DClick();
622
623 case wxMOUSE_BTN_AUX2:
624 return Aux2DClick();
625 }
626}
627
628// return true if was a button down event
629bool wxMouseEvent::ButtonDown(int but) const
630{
631 switch (but)
632 {
633 default:
634 wxFAIL_MSG(wxT("invalid parameter in wxMouseEvent::ButtonDown"));
635 // fall through
636
637 case wxMOUSE_BTN_ANY:
638 return (LeftDown() || MiddleDown() || RightDown() ||
639 Aux1Down() || Aux2Down());
640
641 case wxMOUSE_BTN_LEFT:
642 return LeftDown();
643
644 case wxMOUSE_BTN_MIDDLE:
645 return MiddleDown();
646
647 case wxMOUSE_BTN_RIGHT:
648 return RightDown();
649
650 case wxMOUSE_BTN_AUX1:
651 return Aux1Down();
652
653 case wxMOUSE_BTN_AUX2:
654 return Aux2Down();
655 }
656}
657
658// return true if was a button up event
659bool wxMouseEvent::ButtonUp(int but) const
660{
661 switch (but)
662 {
663 default:
664 wxFAIL_MSG(wxT("invalid parameter in wxMouseEvent::ButtonUp"));
665 // fall through
666
667 case wxMOUSE_BTN_ANY:
668 return (LeftUp() || MiddleUp() || RightUp() ||
669 Aux1Up() || Aux2Up());
670
671 case wxMOUSE_BTN_LEFT:
672 return LeftUp();
673
674 case wxMOUSE_BTN_MIDDLE:
675 return MiddleUp();
676
677 case wxMOUSE_BTN_RIGHT:
678 return RightUp();
679
680 case wxMOUSE_BTN_AUX1:
681 return Aux1Up();
682
683 case wxMOUSE_BTN_AUX2:
684 return Aux2Up();
685 }
686}
687
688// return true if the given button is currently changing state
689bool wxMouseEvent::Button(int but) const
690{
691 switch (but)
692 {
693 default:
694 wxFAIL_MSG(wxT("invalid parameter in wxMouseEvent::Button"));
695 // fall through
696
697 case wxMOUSE_BTN_ANY:
698 return ButtonUp(wxMOUSE_BTN_ANY) ||
699 ButtonDown(wxMOUSE_BTN_ANY) ||
700 ButtonDClick(wxMOUSE_BTN_ANY);
701
702 case wxMOUSE_BTN_LEFT:
703 return LeftDown() || LeftUp() || LeftDClick();
704
705 case wxMOUSE_BTN_MIDDLE:
706 return MiddleDown() || MiddleUp() || MiddleDClick();
707
708 case wxMOUSE_BTN_RIGHT:
709 return RightDown() || RightUp() || RightDClick();
710
711 case wxMOUSE_BTN_AUX1:
712 return Aux1Down() || Aux1Up() || Aux1DClick();
713
714 case wxMOUSE_BTN_AUX2:
715 return Aux2Down() || Aux2Up() || Aux2DClick();
716 }
717}
718
719int wxMouseEvent::GetButton() const
720{
721 for ( int i = 1; i < wxMOUSE_BTN_MAX; i++ )
722 {
723 if ( Button(i) )
724 {
725 return i;
726 }
727 }
728
729 return wxMOUSE_BTN_NONE;
730}
731
732// Find the logical position of the event given the DC
733wxPoint wxMouseEvent::GetLogicalPosition(const wxDC& dc) const
734{
735 wxPoint pt(dc.DeviceToLogicalX(m_x), dc.DeviceToLogicalY(m_y));
736 return pt;
737}
738
739// ----------------------------------------------------------------------------
740// wxKeyEvent
741// ----------------------------------------------------------------------------
742
743wxKeyEvent::wxKeyEvent(wxEventType type)
744{
745 m_eventType = type;
746 m_keyCode = WXK_NONE;
747#if wxUSE_UNICODE
748 m_uniChar = WXK_NONE;
749#endif
750
751 m_x =
752 m_y = wxDefaultCoord;
753 m_hasPosition = false;
754
755 InitPropagation();
756}
757
758wxKeyEvent::wxKeyEvent(const wxKeyEvent& evt)
759 : wxEvent(evt),
760 wxKeyboardState(evt)
761{
762 DoAssignMembers(evt);
763
764 InitPropagation();
765}
766
767wxKeyEvent::wxKeyEvent(wxEventType eventType, const wxKeyEvent& evt)
768 : wxEvent(evt),
769 wxKeyboardState(evt)
770{
771 DoAssignMembers(evt);
772
773 m_eventType = eventType;
774
775 InitPropagation();
776}
777
778void wxKeyEvent::InitPositionIfNecessary() const
779{
780 if ( m_hasPosition )
781 return;
782
783 // We're const because we're called from const Get[XY]() methods but we
784 // need to update the "cached" values.
785 wxKeyEvent& self = const_cast<wxKeyEvent&>(*this);
786 self.m_hasPosition = true;
787
788 // The only position we can possibly associate with the keyboard event on
789 // the platforms where it doesn't carry it already is the mouse position.
790 wxGetMousePosition(&self.m_x, &self.m_y);
791
792 // If this event is associated with a window, the position should be in its
793 // client coordinates, but otherwise leave it in screen coordinates as what
794 // else can we use?
795 wxWindow* const win = wxDynamicCast(GetEventObject(), wxWindow);
796 if ( win )
797 win->ScreenToClient(&self.m_x, &self.m_y);
798}
799
800wxCoord wxKeyEvent::GetX() const
801{
802 InitPositionIfNecessary();
803
804 return m_x;
805}
806
807wxCoord wxKeyEvent::GetY() const
808{
809 InitPositionIfNecessary();
810
811 return m_y;
812}
813
814bool wxKeyEvent::IsKeyInCategory(int category) const
815{
816 switch ( GetKeyCode() )
817 {
818 case WXK_LEFT:
819 case WXK_RIGHT:
820 case WXK_UP:
821 case WXK_DOWN:
822 case WXK_NUMPAD_LEFT:
823 case WXK_NUMPAD_RIGHT:
824 case WXK_NUMPAD_UP:
825 case WXK_NUMPAD_DOWN:
826 return (category & WXK_CATEGORY_ARROW) != 0;
827
828 case WXK_PAGEDOWN:
829 case WXK_END:
830 case WXK_NUMPAD_PAGEUP:
831 case WXK_NUMPAD_PAGEDOWN:
832 return (category & WXK_CATEGORY_PAGING) != 0;
833
834 case WXK_HOME:
835 case WXK_PAGEUP:
836 case WXK_NUMPAD_HOME:
837 case WXK_NUMPAD_END:
838 return (category & WXK_CATEGORY_JUMP) != 0;
839
840 case WXK_TAB:
841 case WXK_NUMPAD_TAB:
842 return (category & WXK_CATEGORY_TAB) != 0;
843
844 case WXK_BACK:
845 case WXK_DELETE:
846 case WXK_NUMPAD_DELETE:
847 return (category & WXK_CATEGORY_CUT) != 0;
848
849 default:
850 return false;
851 }
852}
853
854// ----------------------------------------------------------------------------
855// wxWindowCreateEvent
856// ----------------------------------------------------------------------------
857
858wxWindowCreateEvent::wxWindowCreateEvent(wxWindow *win)
859{
860 SetEventType(wxEVT_CREATE);
861 SetEventObject(win);
862}
863
864// ----------------------------------------------------------------------------
865// wxWindowDestroyEvent
866// ----------------------------------------------------------------------------
867
868wxWindowDestroyEvent::wxWindowDestroyEvent(wxWindow *win)
869{
870 SetEventType(wxEVT_DESTROY);
871 SetEventObject(win);
872}
873
874// ----------------------------------------------------------------------------
875// wxChildFocusEvent
876// ----------------------------------------------------------------------------
877
878wxChildFocusEvent::wxChildFocusEvent(wxWindow *win)
879 : wxCommandEvent(wxEVT_CHILD_FOCUS)
880{
881 SetEventObject(win);
882}
883
884// ----------------------------------------------------------------------------
885// wxHelpEvent
886// ----------------------------------------------------------------------------
887
888/* static */
889wxHelpEvent::Origin wxHelpEvent::GuessOrigin(Origin origin)
890{
891 if ( origin == Origin_Unknown )
892 {
893 // assume that the event comes from the help button if it's not from
894 // keyboard and that pressing F1 always results in the help event
895 origin = wxGetKeyState(WXK_F1) ? Origin_Keyboard : Origin_HelpButton;
896 }
897
898 return origin;
899}
900
901#endif // wxUSE_GUI
902
903
904#if wxUSE_BASE
905
906// ----------------------------------------------------------------------------
907// wxEventHashTable
908// ----------------------------------------------------------------------------
909
910static const int EVENT_TYPE_TABLE_INIT_SIZE = 31; // Not too big not too small...
911
912wxEventHashTable* wxEventHashTable::sm_first = NULL;
913
914wxEventHashTable::wxEventHashTable(const wxEventTable &table)
915 : m_table(table),
916 m_rebuildHash(true)
917{
918 AllocEventTypeTable(EVENT_TYPE_TABLE_INIT_SIZE);
919
920 m_next = sm_first;
921 if (m_next)
922 m_next->m_previous = this;
923 sm_first = this;
924}
925
926wxEventHashTable::~wxEventHashTable()
927{
928 if (m_next)
929 m_next->m_previous = m_previous;
930 if (m_previous)
931 m_previous->m_next = m_next;
932 if (sm_first == this)
933 sm_first = m_next;
934
935 Clear();
936}
937
938void wxEventHashTable::Clear()
939{
940 for ( size_t i = 0; i < m_size; i++ )
941 {
942 EventTypeTablePointer eTTnode = m_eventTypeTable[i];
943 delete eTTnode;
944 }
945
946 wxDELETEA(m_eventTypeTable);
947
948 m_size = 0;
949}
950
951#if wxUSE_MEMORY_TRACING
952
953// Clear all tables
954void wxEventHashTable::ClearAll()
955{
956 wxEventHashTable* table = sm_first;
957 while (table)
958 {
959 table->Clear();
960 table = table->m_next;
961 }
962}
963
964#endif // wxUSE_MEMORY_TRACING
965
966bool wxEventHashTable::HandleEvent(wxEvent &event, wxEvtHandler *self)
967{
968 if (m_rebuildHash)
969 {
970 InitHashTable();
971 m_rebuildHash = false;
972 }
973
974 if (!m_eventTypeTable)
975 return false;
976
977 // Find all entries for the given event type.
978 wxEventType eventType = event.GetEventType();
979 const EventTypeTablePointer eTTnode = m_eventTypeTable[eventType % m_size];
980 if (eTTnode && eTTnode->eventType == eventType)
981 {
982 // Now start the search for an event handler
983 // that can handle an event with the given ID.
984 const wxEventTableEntryPointerArray&
985 eventEntryTable = eTTnode->eventEntryTable;
986
987 const size_t count = eventEntryTable.GetCount();
988 for (size_t n = 0; n < count; n++)
989 {
990 const wxEventTableEntry& entry = *eventEntryTable[n];
991 if ( wxEvtHandler::ProcessEventIfMatchesId(entry, self, event) )
992 return true;
993 }
994 }
995
996 return false;
997}
998
999void wxEventHashTable::InitHashTable()
1000{
1001 // Loop over the event tables and all its base tables.
1002 const wxEventTable *table = &m_table;
1003 while (table)
1004 {
1005 // Retrieve all valid event handler entries
1006 const wxEventTableEntry *entry = table->entries;
1007 while (entry->m_fn != 0)
1008 {
1009 // Add the event entry in the Hash.
1010 AddEntry(*entry);
1011
1012 entry++;
1013 }
1014
1015 table = table->baseTable;
1016 }
1017
1018 // Let's free some memory.
1019 size_t i;
1020 for(i = 0; i < m_size; i++)
1021 {
1022 EventTypeTablePointer eTTnode = m_eventTypeTable[i];
1023 if (eTTnode)
1024 {
1025 eTTnode->eventEntryTable.Shrink();
1026 }
1027 }
1028}
1029
1030void wxEventHashTable::AddEntry(const wxEventTableEntry &entry)
1031{
1032 // This might happen 'accidentally' as the app is exiting
1033 if (!m_eventTypeTable)
1034 return;
1035
1036 EventTypeTablePointer *peTTnode = &m_eventTypeTable[entry.m_eventType % m_size];
1037 EventTypeTablePointer eTTnode = *peTTnode;
1038
1039 if (eTTnode)
1040 {
1041 if (eTTnode->eventType != entry.m_eventType)
1042 {
1043 // Resize the table!
1044 GrowEventTypeTable();
1045 // Try again to add it.
1046 AddEntry(entry);
1047 return;
1048 }
1049 }
1050 else
1051 {
1052 eTTnode = new EventTypeTable;
1053 eTTnode->eventType = entry.m_eventType;
1054 *peTTnode = eTTnode;
1055 }
1056
1057 // Fill all hash entries between entry.m_id and entry.m_lastId...
1058 eTTnode->eventEntryTable.Add(&entry);
1059}
1060
1061void wxEventHashTable::AllocEventTypeTable(size_t size)
1062{
1063 m_eventTypeTable = new EventTypeTablePointer[size];
1064 memset((void *)m_eventTypeTable, 0, sizeof(EventTypeTablePointer)*size);
1065 m_size = size;
1066}
1067
1068void wxEventHashTable::GrowEventTypeTable()
1069{
1070 size_t oldSize = m_size;
1071 EventTypeTablePointer *oldEventTypeTable = m_eventTypeTable;
1072
1073 // TODO: Search the most optimal grow sequence
1074 AllocEventTypeTable(/* GetNextPrime(oldSize) */oldSize*2+1);
1075
1076 for ( size_t i = 0; i < oldSize; /* */ )
1077 {
1078 EventTypeTablePointer eTToldNode = oldEventTypeTable[i];
1079 if (eTToldNode)
1080 {
1081 EventTypeTablePointer *peTTnode = &m_eventTypeTable[eTToldNode->eventType % m_size];
1082 EventTypeTablePointer eTTnode = *peTTnode;
1083
1084 // Check for collision, we don't want any.
1085 if (eTTnode)
1086 {
1087 GrowEventTypeTable();
1088 continue; // Don't increment the counter,
1089 // as we still need to add this element.
1090 }
1091 else
1092 {
1093 // Get the old value and put it in the new table.
1094 *peTTnode = oldEventTypeTable[i];
1095 }
1096 }
1097
1098 i++;
1099 }
1100
1101 delete[] oldEventTypeTable;
1102}
1103
1104// ----------------------------------------------------------------------------
1105// wxEvtHandler
1106// ----------------------------------------------------------------------------
1107
1108wxEvtHandler::wxEvtHandler()
1109{
1110 m_nextHandler = NULL;
1111 m_previousHandler = NULL;
1112 m_enabled = true;
1113 m_dynamicEvents = NULL;
1114 m_pendingEvents = NULL;
1115
1116 // no client data (yet)
1117 m_clientData = NULL;
1118 m_clientDataType = wxClientData_None;
1119}
1120
1121wxEvtHandler::~wxEvtHandler()
1122{
1123 Unlink();
1124
1125 if (m_dynamicEvents)
1126 {
1127 for ( wxList::iterator it = m_dynamicEvents->begin(),
1128 end = m_dynamicEvents->end();
1129 it != end;
1130 ++it )
1131 {
1132 wxDynamicEventTableEntry *entry = (wxDynamicEventTableEntry*)*it;
1133
1134 // Remove ourselves from sink destructor notifications
1135 // (this has usually been done, in wxTrackable destructor)
1136 wxEvtHandler *eventSink = entry->m_fn->GetEvtHandler();
1137 if ( eventSink )
1138 {
1139 wxEventConnectionRef * const
1140 evtConnRef = FindRefInTrackerList(eventSink);
1141 if ( evtConnRef )
1142 {
1143 eventSink->RemoveNode(evtConnRef);
1144 delete evtConnRef;
1145 }
1146 }
1147
1148 delete entry->m_callbackUserData;
1149 delete entry;
1150 }
1151 delete m_dynamicEvents;
1152 }
1153
1154 // Remove us from the list of the pending events if necessary.
1155 if (wxTheApp)
1156 wxTheApp->RemovePendingEventHandler(this);
1157
1158 DeletePendingEvents();
1159
1160 // we only delete object data, not untyped
1161 if ( m_clientDataType == wxClientData_Object )
1162 delete m_clientObject;
1163}
1164
1165void wxEvtHandler::Unlink()
1166{
1167 // this event handler must take itself out of the chain of handlers:
1168
1169 if (m_previousHandler)
1170 m_previousHandler->SetNextHandler(m_nextHandler);
1171
1172 if (m_nextHandler)
1173 m_nextHandler->SetPreviousHandler(m_previousHandler);
1174
1175 m_nextHandler = NULL;
1176 m_previousHandler = NULL;
1177}
1178
1179bool wxEvtHandler::IsUnlinked() const
1180{
1181 return m_previousHandler == NULL &&
1182 m_nextHandler == NULL;
1183}
1184
1185wxEventFilter* wxEvtHandler::ms_filterList = NULL;
1186
1187/* static */ void wxEvtHandler::AddFilter(wxEventFilter* filter)
1188{
1189 wxCHECK_RET( filter, "NULL filter" );
1190
1191 filter->m_next = ms_filterList;
1192 ms_filterList = filter;
1193}
1194
1195/* static */ void wxEvtHandler::RemoveFilter(wxEventFilter* filter)
1196{
1197 wxEventFilter* prev = NULL;
1198 for ( wxEventFilter* f = ms_filterList; f; f = f->m_next )
1199 {
1200 if ( f == filter )
1201 {
1202 // Set the previous list element or the list head to the next
1203 // element.
1204 if ( prev )
1205 prev->m_next = f->m_next;
1206 else
1207 ms_filterList = f->m_next;
1208
1209 // Also reset the next pointer in the filter itself just to avoid
1210 // having possibly dangling pointers, even though it's not strictly
1211 // necessary.
1212 f->m_next = NULL;
1213
1214 // Skip the assert below.
1215 return;
1216 }
1217
1218 prev = f;
1219 }
1220
1221 wxFAIL_MSG( "Filter not found" );
1222}
1223
1224#if wxUSE_THREADS
1225
1226bool wxEvtHandler::ProcessThreadEvent(const wxEvent& event)
1227{
1228 // check that we are really in a child thread
1229 wxASSERT_MSG( !wxThread::IsMain(),
1230 wxT("use ProcessEvent() in main thread") );
1231
1232 AddPendingEvent(event);
1233
1234 return true;
1235}
1236
1237#endif // wxUSE_THREADS
1238
1239void wxEvtHandler::QueueEvent(wxEvent *event)
1240{
1241 wxCHECK_RET( event, "NULL event can't be posted" );
1242
1243 if (!wxTheApp)
1244 {
1245 // we need an event loop which manages the list of event handlers with
1246 // pending events... cannot proceed without it!
1247 wxLogDebug("No application object! Cannot queue this event!");
1248
1249 // anyway delete the given event to avoid memory leaks
1250 delete event;
1251
1252 return;
1253 }
1254
1255 // 1) Add this event to our list of pending events
1256 wxENTER_CRIT_SECT( m_pendingEventsLock );
1257
1258 if ( !m_pendingEvents )
1259 m_pendingEvents = new wxList;
1260
1261 m_pendingEvents->Append(event);
1262
1263 // 2) Add this event handler to list of event handlers that
1264 // have pending events.
1265
1266 wxTheApp->AppendPendingEventHandler(this);
1267
1268 // only release m_pendingEventsLock now because otherwise there is a race
1269 // condition as described in the ticket #9093: we could process the event
1270 // just added to m_pendingEvents in our ProcessPendingEvents() below before
1271 // we had time to append this pointer to wxHandlersWithPendingEvents list; thus
1272 // breaking the invariant that a handler should be in the list iff it has
1273 // any pending events to process
1274 wxLEAVE_CRIT_SECT( m_pendingEventsLock );
1275
1276 // 3) Inform the system that new pending events are somewhere,
1277 // and that these should be processed in idle time.
1278 wxWakeUpIdle();
1279}
1280
1281void wxEvtHandler::DeletePendingEvents()
1282{
1283 if (m_pendingEvents)
1284 m_pendingEvents->DeleteContents(true);
1285 wxDELETE(m_pendingEvents);
1286}
1287
1288void wxEvtHandler::ProcessPendingEvents()
1289{
1290 if (!wxTheApp)
1291 {
1292 // we need an event loop which manages the list of event handlers with
1293 // pending events... cannot proceed without it!
1294 wxLogDebug("No application object! Cannot process pending events!");
1295 return;
1296 }
1297
1298 // we need to process only a single pending event in this call because
1299 // each call to ProcessEvent() could result in the destruction of this
1300 // same event handler (see the comment at the end of this function)
1301
1302 wxENTER_CRIT_SECT( m_pendingEventsLock );
1303
1304 // this method is only called by wxApp if this handler does have
1305 // pending events
1306 wxCHECK_RET( m_pendingEvents && !m_pendingEvents->IsEmpty(),
1307 "should have pending events if called" );
1308
1309 wxList::compatibility_iterator node = m_pendingEvents->GetFirst();
1310 wxEvent* pEvent = static_cast<wxEvent *>(node->GetData());
1311
1312 // find the first event which can be processed now:
1313 wxEventLoopBase* evtLoop = wxEventLoopBase::GetActive();
1314 if (evtLoop && evtLoop->IsYielding())
1315 {
1316 while (node && pEvent && !evtLoop->IsEventAllowedInsideYield(pEvent->GetEventCategory()))
1317 {
1318 node = node->GetNext();
1319 pEvent = node ? static_cast<wxEvent *>(node->GetData()) : NULL;
1320 }
1321
1322 if (!node)
1323 {
1324 // all our events are NOT processable now... signal this:
1325 wxTheApp->DelayPendingEventHandler(this);
1326
1327 // see the comment at the beginning of evtloop.h header for the
1328 // logic behind YieldFor() and behind DelayPendingEventHandler()
1329
1330 wxLEAVE_CRIT_SECT( m_pendingEventsLock );
1331
1332 return;
1333 }
1334 }
1335
1336 wxEventPtr event(pEvent);
1337
1338 // it's important we remove event from list before processing it, else a
1339 // nested event loop, for example from a modal dialog, might process the
1340 // same event again.
1341 m_pendingEvents->Erase(node);
1342
1343 if ( m_pendingEvents->IsEmpty() )
1344 {
1345 // if there are no more pending events left, we don't need to
1346 // stay in this list
1347 wxTheApp->RemovePendingEventHandler(this);
1348 }
1349
1350 wxLEAVE_CRIT_SECT( m_pendingEventsLock );
1351
1352 ProcessEvent(*event);
1353
1354 // careful: this object could have been deleted by the event handler
1355 // executed by the above ProcessEvent() call, so we can't access any fields
1356 // of this object any more
1357}
1358
1359/* static */
1360bool wxEvtHandler::ProcessEventIfMatchesId(const wxEventTableEntryBase& entry,
1361 wxEvtHandler *handler,
1362 wxEvent& event)
1363{
1364 int tableId1 = entry.m_id,
1365 tableId2 = entry.m_lastId;
1366
1367 // match only if the event type is the same and the id is either -1 in
1368 // the event table (meaning "any") or the event id matches the id
1369 // specified in the event table either exactly or by falling into
1370 // range between first and last
1371 if ((tableId1 == wxID_ANY) ||
1372 (tableId2 == wxID_ANY && tableId1 == event.GetId()) ||
1373 (tableId2 != wxID_ANY &&
1374 (event.GetId() >= tableId1 && event.GetId() <= tableId2)))
1375 {
1376 event.Skip(false);
1377 event.m_callbackUserData = entry.m_callbackUserData;
1378
1379#if wxUSE_EXCEPTIONS
1380 if ( wxTheApp )
1381 {
1382 // call the handler via wxApp method which allows the user to catch
1383 // any exceptions which may be thrown by any handler in the program
1384 // in one place
1385 wxTheApp->CallEventHandler(handler, *entry.m_fn, event);
1386 }
1387 else
1388#endif // wxUSE_EXCEPTIONS
1389 {
1390 (*entry.m_fn)(handler, event);
1391 }
1392
1393 if (!event.GetSkipped())
1394 return true;
1395 }
1396
1397 return false;
1398}
1399
1400bool wxEvtHandler::DoTryApp(wxEvent& event)
1401{
1402 if ( wxTheApp && (this != wxTheApp) )
1403 {
1404 // Special case: don't pass wxEVT_IDLE to wxApp, since it'll always
1405 // swallow it. wxEVT_IDLE is sent explicitly to wxApp so it will be
1406 // processed appropriately via SearchEventTable.
1407 if ( event.GetEventType() != wxEVT_IDLE )
1408 {
1409 if ( wxTheApp->ProcessEvent(event) )
1410 return true;
1411 }
1412 }
1413
1414 return false;
1415}
1416
1417bool wxEvtHandler::TryBefore(wxEvent& event)
1418{
1419#if WXWIN_COMPATIBILITY_2_8
1420 // call the old virtual function to keep the code overriding it working
1421 return TryValidator(event);
1422#else
1423 wxUnusedVar(event);
1424 return false;
1425#endif
1426}
1427
1428bool wxEvtHandler::TryAfter(wxEvent& event)
1429{
1430 // We only want to pass the window to the application object once even if
1431 // there are several chained handlers. Ensure that this is what happens by
1432 // only calling DoTryApp() if there is no next handler (which would do it).
1433 //
1434 // Notice that, unlike simply calling TryAfter() on the last handler in the
1435 // chain only from ProcessEvent(), this also works with wxWindow object in
1436 // the middle of the chain: its overridden TryAfter() will still be called
1437 // and propagate the event upwards the window hierarchy even if it's not
1438 // the last one in the chain (which, admittedly, shouldn't happen often).
1439 if ( GetNextHandler() )
1440 return GetNextHandler()->TryAfter(event);
1441
1442 // If this event is going to be processed in another handler next, don't
1443 // pass it to wxTheApp now, it will be done from TryAfter() of this other
1444 // handler.
1445 if ( event.WillBeProcessedAgain() )
1446 return false;
1447
1448#if WXWIN_COMPATIBILITY_2_8
1449 // as above, call the old virtual function for compatibility
1450 return TryParent(event);
1451#else
1452 return DoTryApp(event);
1453#endif
1454}
1455
1456bool wxEvtHandler::ProcessEvent(wxEvent& event)
1457{
1458 // The very first thing we do is to allow any registered filters to hook
1459 // into event processing in order to globally pre-process all events.
1460 //
1461 // Note that we should only do it if we're the first event handler called
1462 // to avoid calling FilterEvent() multiple times as the event goes through
1463 // the event handler chain and possibly upwards the window hierarchy.
1464 if ( !event.WasProcessed() )
1465 {
1466 for ( wxEventFilter* f = ms_filterList; f; f = f->m_next )
1467 {
1468 int rc = f->FilterEvent(event);
1469 if ( rc != wxEventFilter::Event_Skip )
1470 {
1471 wxASSERT_MSG( rc == wxEventFilter::Event_Ignore ||
1472 rc == wxEventFilter::Event_Processed,
1473 "unexpected FilterEvent() return value" );
1474
1475 return rc != wxEventFilter::Event_Ignore;
1476 }
1477 //else: proceed normally
1478 }
1479 }
1480
1481 // Short circuit the event processing logic if we're requested to process
1482 // this event in this handler only, see DoTryChain() for more details.
1483 if ( event.ShouldProcessOnlyIn(this) )
1484 return TryBeforeAndHere(event);
1485
1486
1487 // Try to process the event in this handler itself.
1488 if ( ProcessEventLocally(event) )
1489 {
1490 // It is possible that DoTryChain() called from ProcessEventLocally()
1491 // returned true but the event was not really processed: this happens
1492 // if a custom handler ignores the request to process the event in this
1493 // handler only and in this case we should skip the post processing
1494 // done in TryAfter() but still return the correct value ourselves to
1495 // indicate whether we did or did not find a handler for this event.
1496 return !event.GetSkipped();
1497 }
1498
1499 // If we still didn't find a handler, propagate the event upwards the
1500 // window chain and/or to the application object.
1501 if ( TryAfter(event) )
1502 return true;
1503
1504
1505 // No handler found anywhere, bail out.
1506 return false;
1507}
1508
1509bool wxEvtHandler::ProcessEventLocally(wxEvent& event)
1510{
1511 // Try the hooks which should be called before our own handlers and this
1512 // handler itself first. Notice that we should not call ProcessEvent() on
1513 // this one as we're already called from it, which explains why we do it
1514 // here and not in DoTryChain()
1515 return TryBeforeAndHere(event) || DoTryChain(event);
1516}
1517
1518bool wxEvtHandler::DoTryChain(wxEvent& event)
1519{
1520 for ( wxEvtHandler *h = GetNextHandler(); h; h = h->GetNextHandler() )
1521 {
1522 // We need to process this event at the level of this handler only
1523 // right now, the pre-/post-processing was either already done by
1524 // ProcessEvent() from which we were called or will be done by it when
1525 // we return.
1526 //
1527 // However we must call ProcessEvent() and not TryHereOnly() because the
1528 // existing code (including some in wxWidgets itself) expects the
1529 // overridden ProcessEvent() in its custom event handlers pushed on a
1530 // window to be called.
1531 //
1532 // So we must call ProcessEvent() but it must not do what it usually
1533 // does. To resolve this paradox we set up a special flag inside the
1534 // object itself to let ProcessEvent() know that it shouldn't do any
1535 // pre/post-processing for this event if it gets it. Note that this
1536 // only applies to this handler, if the event is passed to another one
1537 // by explicitly calling its ProcessEvent(), pre/post-processing should
1538 // be done as usual.
1539 //
1540 // Final complication is that if the implementation of ProcessEvent()
1541 // called wxEvent::DidntHonourProcessOnlyIn() (as the gross hack that
1542 // is wxScrollHelperEvtHandler::ProcessEvent() does) and ignored our
1543 // request to process event in this handler only, we have to compensate
1544 // for it by not processing the event further because this was already
1545 // done by that rogue event handler.
1546 wxEventProcessInHandlerOnly processInHandlerOnly(event, h);
1547 if ( h->ProcessEvent(event) )
1548 {
1549 // Make sure "skipped" flag is not set as the event was really
1550 // processed in this case. Normally it shouldn't be set anyhow but
1551 // make sure just in case the user code does something strange.
1552 event.Skip(false);
1553
1554 return true;
1555 }
1556
1557 if ( !event.ShouldProcessOnlyIn(h) )
1558 {
1559 // Still return true to indicate that no further processing should
1560 // be undertaken but ensure that "skipped" flag is set so that the
1561 // caller knows that the event was not really processed.
1562 event.Skip();
1563
1564 return true;
1565 }
1566 }
1567
1568 return false;
1569}
1570
1571bool wxEvtHandler::TryHereOnly(wxEvent& event)
1572{
1573 // If the event handler is disabled it doesn't process any events
1574 if ( !GetEvtHandlerEnabled() )
1575 return false;
1576
1577 // Handle per-instance dynamic event tables first
1578 if ( m_dynamicEvents && SearchDynamicEventTable(event) )
1579 return true;
1580
1581 // Then static per-class event tables
1582 if ( GetEventHashTable().HandleEvent(event, this) )
1583 return true;
1584
1585#ifdef wxHAS_CALL_AFTER
1586 // There is an implicit entry for async method calls processing in every
1587 // event handler:
1588 if ( event.GetEventType() == wxEVT_ASYNC_METHOD_CALL &&
1589 event.GetEventObject() == this )
1590 {
1591 static_cast<wxAsyncMethodCallEvent&>(event).Execute();
1592 return true;
1593 }
1594#endif // wxHAS_CALL_AFTER
1595
1596 // We don't have a handler for this event.
1597 return false;
1598}
1599
1600bool wxEvtHandler::SafelyProcessEvent(wxEvent& event)
1601{
1602#if wxUSE_EXCEPTIONS
1603 try
1604 {
1605#endif
1606 return ProcessEvent(event);
1607#if wxUSE_EXCEPTIONS
1608 }
1609 catch ( ... )
1610 {
1611 // notice that we do it in 2 steps to avoid warnings about possibly
1612 // uninitialized loop variable from some versions of g++ which are not
1613 // smart enough to figure out that GetActive() doesn't throw and so
1614 // that loop will always be initialized
1615 wxEventLoopBase *loop = NULL;
1616 try
1617 {
1618 loop = wxEventLoopBase::GetActive();
1619
1620 if ( !wxTheApp || !wxTheApp->OnExceptionInMainLoop() )
1621 {
1622 if ( loop )
1623 loop->Exit();
1624 }
1625 //else: continue running current event loop
1626
1627 return false;
1628 }
1629 catch ( ... )
1630 {
1631 // OnExceptionInMainLoop() threw, possibly rethrowing the same
1632 // exception again: very good, but we still need Exit() to
1633 // be called
1634 if ( loop )
1635 loop->Exit();
1636 throw;
1637 }
1638 }
1639#endif // wxUSE_EXCEPTIONS
1640}
1641
1642bool wxEvtHandler::SearchEventTable(wxEventTable& table, wxEvent& event)
1643{
1644 const wxEventType eventType = event.GetEventType();
1645 for ( int i = 0; table.entries[i].m_fn != 0; i++ )
1646 {
1647 const wxEventTableEntry& entry = table.entries[i];
1648 if ( eventType == entry.m_eventType )
1649 {
1650 if ( ProcessEventIfMatchesId(entry, this, event) )
1651 return true;
1652 }
1653 }
1654
1655 return false;
1656}
1657
1658void wxEvtHandler::DoBind(int id,
1659 int lastId,
1660 wxEventType eventType,
1661 wxEventFunctor *func,
1662 wxObject *userData)
1663{
1664 wxDynamicEventTableEntry *entry =
1665 new wxDynamicEventTableEntry(eventType, id, lastId, func, userData);
1666
1667 if (!m_dynamicEvents)
1668 m_dynamicEvents = new wxList;
1669
1670 // Insert at the front of the list so most recent additions are found first
1671 m_dynamicEvents->Insert( (wxObject*) entry );
1672
1673 // Make sure we get to know when a sink is destroyed
1674 wxEvtHandler *eventSink = func->GetEvtHandler();
1675 if ( eventSink && eventSink != this )
1676 {
1677 wxEventConnectionRef *evtConnRef = FindRefInTrackerList(eventSink);
1678 if ( evtConnRef )
1679 evtConnRef->IncRef( );
1680 else
1681 new wxEventConnectionRef(this, eventSink);
1682 }
1683}
1684
1685bool
1686wxEvtHandler::DoUnbind(int id,
1687 int lastId,
1688 wxEventType eventType,
1689 const wxEventFunctor& func,
1690 wxObject *userData)
1691{
1692 if (!m_dynamicEvents)
1693 return false;
1694
1695 wxList::compatibility_iterator node = m_dynamicEvents->GetFirst();
1696 while (node)
1697 {
1698 wxDynamicEventTableEntry *entry = (wxDynamicEventTableEntry*)node->GetData();
1699
1700 if ((entry->m_id == id) &&
1701 ((entry->m_lastId == lastId) || (lastId == wxID_ANY)) &&
1702 ((entry->m_eventType == eventType) || (eventType == wxEVT_NULL)) &&
1703 entry->m_fn->IsMatching(func) &&
1704 ((entry->m_callbackUserData == userData) || !userData))
1705 {
1706 // Remove connection from tracker node (wxEventConnectionRef)
1707 wxEvtHandler *eventSink = entry->m_fn->GetEvtHandler();
1708 if ( eventSink && eventSink != this )
1709 {
1710 wxEventConnectionRef *evtConnRef = FindRefInTrackerList(eventSink);
1711 if ( evtConnRef )
1712 evtConnRef->DecRef();
1713 }
1714
1715 delete entry->m_callbackUserData;
1716 m_dynamicEvents->Erase( node );
1717 delete entry;
1718 return true;
1719 }
1720 node = node->GetNext();
1721 }
1722 return false;
1723}
1724
1725bool wxEvtHandler::SearchDynamicEventTable( wxEvent& event )
1726{
1727 wxCHECK_MSG( m_dynamicEvents, false,
1728 wxT("caller should check that we have dynamic events") );
1729
1730 wxList::compatibility_iterator node = m_dynamicEvents->GetFirst();
1731 while (node)
1732 {
1733 wxDynamicEventTableEntry *entry = (wxDynamicEventTableEntry*)node->GetData();
1734
1735 // get next node before (maybe) calling the event handler as it could
1736 // call Disconnect() invalidating the current node
1737 node = node->GetNext();
1738
1739 if ( event.GetEventType() == entry->m_eventType )
1740 {
1741 wxEvtHandler *handler = entry->m_fn->GetEvtHandler();
1742 if ( !handler )
1743 handler = this;
1744 if ( ProcessEventIfMatchesId(*entry, handler, event) )
1745 return true;
1746 }
1747 }
1748
1749 return false;
1750}
1751
1752void wxEvtHandler::DoSetClientObject( wxClientData *data )
1753{
1754 wxASSERT_MSG( m_clientDataType != wxClientData_Void,
1755 wxT("can't have both object and void client data") );
1756
1757 if ( m_clientObject )
1758 delete m_clientObject;
1759
1760 m_clientObject = data;
1761 m_clientDataType = wxClientData_Object;
1762}
1763
1764wxClientData *wxEvtHandler::DoGetClientObject() const
1765{
1766 // it's not an error to call GetClientObject() on a window which doesn't
1767 // have client data at all - NULL will be returned
1768 wxASSERT_MSG( m_clientDataType != wxClientData_Void,
1769 wxT("this window doesn't have object client data") );
1770
1771 return m_clientObject;
1772}
1773
1774void wxEvtHandler::DoSetClientData( void *data )
1775{
1776 wxASSERT_MSG( m_clientDataType != wxClientData_Object,
1777 wxT("can't have both object and void client data") );
1778
1779 m_clientData = data;
1780 m_clientDataType = wxClientData_Void;
1781}
1782
1783void *wxEvtHandler::DoGetClientData() const
1784{
1785 // it's not an error to call GetClientData() on a window which doesn't have
1786 // client data at all - NULL will be returned
1787 wxASSERT_MSG( m_clientDataType != wxClientData_Object,
1788 wxT("this window doesn't have void client data") );
1789
1790 return m_clientData;
1791}
1792
1793// A helper to find an wxEventConnectionRef object
1794wxEventConnectionRef *
1795wxEvtHandler::FindRefInTrackerList(wxEvtHandler *eventSink)
1796{
1797 for ( wxTrackerNode *node = eventSink->GetFirst(); node; node = node->m_nxt )
1798 {
1799 // we only want wxEventConnectionRef nodes here
1800 wxEventConnectionRef *evtConnRef = node->ToEventConnection();
1801 if ( evtConnRef && evtConnRef->m_src == this )
1802 {
1803 wxASSERT( evtConnRef->m_sink==eventSink );
1804 return evtConnRef;
1805 }
1806 }
1807
1808 return NULL;
1809}
1810
1811void wxEvtHandler::OnSinkDestroyed( wxEvtHandler *sink )
1812{
1813 wxASSERT(m_dynamicEvents);
1814
1815 // remove all connections with this sink
1816 wxList::compatibility_iterator node = m_dynamicEvents->GetFirst(), node_nxt;
1817 while (node)
1818 {
1819 wxDynamicEventTableEntry *entry = (wxDynamicEventTableEntry*)node->GetData();
1820 node_nxt = node->GetNext();
1821
1822 if ( entry->m_fn->GetEvtHandler() == sink )
1823 {
1824 delete entry->m_callbackUserData;
1825 m_dynamicEvents->Erase( node );
1826 delete entry;
1827 }
1828 node = node_nxt;
1829 }
1830}
1831
1832#endif // wxUSE_BASE
1833
1834#if wxUSE_GUI
1835
1836// Find a window with the focus, that is also a descendant of the given window.
1837// This is used to determine the window to initially send commands to.
1838wxWindow* wxFindFocusDescendant(wxWindow* ancestor)
1839{
1840 // Process events starting with the window with the focus, if any.
1841 wxWindow* focusWin = wxWindow::FindFocus();
1842 wxWindow* win = focusWin;
1843
1844 // Check if this is a descendant of this frame.
1845 // If not, win will be set to NULL.
1846 while (win)
1847 {
1848 if (win == ancestor)
1849 break;
1850 else
1851 win = win->GetParent();
1852 }
1853 if (win == NULL)
1854 focusWin = NULL;
1855
1856 return focusWin;
1857}
1858
1859// ----------------------------------------------------------------------------
1860// wxEventBlocker
1861// ----------------------------------------------------------------------------
1862
1863wxEventBlocker::wxEventBlocker(wxWindow *win, wxEventType type)
1864{
1865 wxCHECK_RET(win, wxT("Null window given to wxEventBlocker"));
1866
1867 m_window = win;
1868
1869 Block(type);
1870 m_window->PushEventHandler(this);
1871}
1872
1873wxEventBlocker::~wxEventBlocker()
1874{
1875 wxEvtHandler *popped = m_window->PopEventHandler(false);
1876 wxCHECK_RET(popped == this,
1877 wxT("Don't push other event handlers into a window managed by wxEventBlocker!"));
1878}
1879
1880bool wxEventBlocker::ProcessEvent(wxEvent& event)
1881{
1882 // should this event be blocked?
1883 for ( size_t i = 0; i < m_eventsToBlock.size(); i++ )
1884 {
1885 wxEventType t = (wxEventType)m_eventsToBlock[i];
1886 if ( t == wxEVT_ANY || t == event.GetEventType() )
1887 return true; // yes, it should: mark this event as processed
1888 }
1889
1890 return false;
1891}
1892
1893#endif // wxUSE_GUI
1894