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