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