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