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