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