]> git.saurik.com Git - wxWidgets.git/blob - src/common/event.cpp
updates for OpenVMS compile support
[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 return m_cmdString;
411 else
412 {
413 #if wxUSE_TEXTCTRL
414 wxTextCtrl *txt = wxDynamicCast(m_eventObject, wxTextCtrl);
415 if(txt)
416 return txt->GetValue();
417 else
418 #endif // wxUSE_TEXTCTRL
419 return m_cmdString;
420 }
421 }
422
423 /*
424 * UI update events
425 */
426
427 #if wxUSE_LONGLONG
428 wxLongLong wxUpdateUIEvent::sm_lastUpdate = 0;
429 #endif
430
431 long wxUpdateUIEvent::sm_updateInterval = 0;
432
433 wxUpdateUIMode wxUpdateUIEvent::sm_updateMode = wxUPDATE_UI_PROCESS_ALL;
434
435 // Can we update?
436 bool wxUpdateUIEvent::CanUpdate(wxWindowBase *win)
437 {
438 // Don't update if we've switched global updating off
439 // and this window doesn't support updates.
440 if (win &&
441 (GetMode() == wxUPDATE_UI_PROCESS_SPECIFIED &&
442 ((win->GetExtraStyle() & wxWS_EX_PROCESS_UI_UPDATES) == 0)))
443 return false;
444
445 if (sm_updateInterval == -1)
446 return false;
447
448 if (sm_updateInterval == 0)
449 return true;
450
451 #if wxUSE_STOPWATCH && wxUSE_LONGLONG
452 wxLongLong now = wxGetLocalTimeMillis();
453 if (now > (sm_lastUpdate + sm_updateInterval))
454 {
455 return true;
456 }
457
458 return false;
459 #else
460 // If we don't have wxStopWatch or wxLongLong, we
461 // should err on the safe side and update now anyway.
462 return true;
463 #endif
464 }
465
466 // Reset the update time to provide a delay until the next
467 // time we should update
468 void wxUpdateUIEvent::ResetUpdateTime()
469 {
470 #if wxUSE_STOPWATCH && wxUSE_LONGLONG
471 if (sm_updateInterval > 0)
472 {
473 wxLongLong now = wxGetLocalTimeMillis();
474 if (now > (sm_lastUpdate + sm_updateInterval))
475 {
476 sm_lastUpdate = now;
477 }
478 }
479 #endif
480 }
481
482 /*
483 * Scroll events
484 */
485
486 wxScrollEvent::wxScrollEvent(wxEventType commandType,
487 int id,
488 int pos,
489 int orient)
490 : wxCommandEvent(commandType, id)
491 {
492 m_extraLong = orient;
493 m_commandInt = pos;
494 }
495
496 /*
497 * ScrollWin events
498 */
499
500 wxScrollWinEvent::wxScrollWinEvent(wxEventType commandType,
501 int pos,
502 int orient)
503 {
504 m_eventType = commandType;
505 m_extraLong = orient;
506 m_commandInt = pos;
507 }
508
509 /*
510 * Mouse events
511 *
512 */
513
514 wxMouseEvent::wxMouseEvent(wxEventType commandType)
515 {
516 m_eventType = commandType;
517
518 m_x = 0;
519 m_y = 0;
520
521 m_leftDown = false;
522 m_middleDown = false;
523 m_rightDown = false;
524 m_aux1Down = false;
525 m_aux2Down = false;
526
527 m_controlDown = false;
528 m_shiftDown = false;
529 m_altDown = false;
530 m_metaDown = false;
531
532 m_clickCount = -1;
533
534 m_wheelRotation = 0;
535 m_wheelDelta = 0;
536 m_linesPerAction = 0;
537 m_wheelAxis = 0;
538 }
539
540 void wxMouseEvent::Assign(const wxMouseEvent& event)
541 {
542 m_eventType = event.m_eventType;
543
544 m_x = event.m_x;
545 m_y = event.m_y;
546
547 m_leftDown = event.m_leftDown;
548 m_middleDown = event.m_middleDown;
549 m_rightDown = event.m_rightDown;
550 m_aux1Down = event.m_aux1Down;
551 m_aux2Down = event.m_aux2Down;
552
553 m_controlDown = event.m_controlDown;
554 m_shiftDown = event.m_shiftDown;
555 m_altDown = event.m_altDown;
556 m_metaDown = event.m_metaDown;
557
558 m_wheelRotation = event.m_wheelRotation;
559 m_wheelDelta = event.m_wheelDelta;
560 m_linesPerAction = event.m_linesPerAction;
561 m_wheelAxis = event.m_wheelAxis;
562 }
563
564 // return true if was a button dclick event
565 bool wxMouseEvent::ButtonDClick(int but) const
566 {
567 switch (but)
568 {
569 default:
570 wxFAIL_MSG(wxT("invalid parameter in wxMouseEvent::ButtonDClick"));
571 // fall through
572
573 case wxMOUSE_BTN_ANY:
574 return (LeftDClick() || MiddleDClick() || RightDClick() ||
575 Aux1DClick() || Aux2DClick());
576
577 case wxMOUSE_BTN_LEFT:
578 return LeftDClick();
579
580 case wxMOUSE_BTN_MIDDLE:
581 return MiddleDClick();
582
583 case wxMOUSE_BTN_RIGHT:
584 return RightDClick();
585
586 case wxMOUSE_BTN_AUX1:
587 return Aux1DClick();
588
589 case wxMOUSE_BTN_AUX2:
590 return Aux2DClick();
591 }
592 }
593
594 // return true if was a button down event
595 bool wxMouseEvent::ButtonDown(int but) const
596 {
597 switch (but)
598 {
599 default:
600 wxFAIL_MSG(wxT("invalid parameter in wxMouseEvent::ButtonDown"));
601 // fall through
602
603 case wxMOUSE_BTN_ANY:
604 return (LeftDown() || MiddleDown() || RightDown() ||
605 Aux1Down() || Aux2Down());
606
607 case wxMOUSE_BTN_LEFT:
608 return LeftDown();
609
610 case wxMOUSE_BTN_MIDDLE:
611 return MiddleDown();
612
613 case wxMOUSE_BTN_RIGHT:
614 return RightDown();
615
616 case wxMOUSE_BTN_AUX1:
617 return Aux1Down();
618
619 case wxMOUSE_BTN_AUX2:
620 return Aux2Down();
621 }
622 }
623
624 // return true if was a button up event
625 bool wxMouseEvent::ButtonUp(int but) const
626 {
627 switch (but)
628 {
629 default:
630 wxFAIL_MSG(wxT("invalid parameter in wxMouseEvent::ButtonUp"));
631 // fall through
632
633 case wxMOUSE_BTN_ANY:
634 return (LeftUp() || MiddleUp() || RightUp() ||
635 Aux1Up() || Aux2Up());
636
637 case wxMOUSE_BTN_LEFT:
638 return LeftUp();
639
640 case wxMOUSE_BTN_MIDDLE:
641 return MiddleUp();
642
643 case wxMOUSE_BTN_RIGHT:
644 return RightUp();
645
646 case wxMOUSE_BTN_AUX1:
647 return Aux1Up();
648
649 case wxMOUSE_BTN_AUX2:
650 return Aux2Up();
651 }
652 }
653
654 // return true if the given button is currently changing state
655 bool wxMouseEvent::Button(int but) const
656 {
657 switch (but)
658 {
659 default:
660 wxFAIL_MSG(wxT("invalid parameter in wxMouseEvent::Button"));
661 // fall through
662
663 case wxMOUSE_BTN_ANY:
664 return ButtonUp(wxMOUSE_BTN_ANY) ||
665 ButtonDown(wxMOUSE_BTN_ANY) ||
666 ButtonDClick(wxMOUSE_BTN_ANY);
667
668 case wxMOUSE_BTN_LEFT:
669 return LeftDown() || LeftUp() || LeftDClick();
670
671 case wxMOUSE_BTN_MIDDLE:
672 return MiddleDown() || MiddleUp() || MiddleDClick();
673
674 case wxMOUSE_BTN_RIGHT:
675 return RightDown() || RightUp() || RightDClick();
676
677 case wxMOUSE_BTN_AUX1:
678 return Aux1Down() || Aux1Up() || Aux1DClick();
679
680 case wxMOUSE_BTN_AUX2:
681 return Aux2Down() || Aux2Up() || Aux2DClick();
682 }
683 }
684
685 bool wxMouseEvent::ButtonIsDown(int but) const
686 {
687 switch (but)
688 {
689 default:
690 wxFAIL_MSG(wxT("invalid parameter in wxMouseEvent::ButtonIsDown"));
691 // fall through
692
693 case wxMOUSE_BTN_ANY:
694 return LeftIsDown() || MiddleIsDown() || RightIsDown() || Aux1Down() || Aux2Down();
695
696 case wxMOUSE_BTN_LEFT:
697 return LeftIsDown();
698
699 case wxMOUSE_BTN_MIDDLE:
700 return MiddleIsDown();
701
702 case wxMOUSE_BTN_RIGHT:
703 return RightIsDown();
704
705 case wxMOUSE_BTN_AUX1:
706 return Aux1IsDown();
707
708 case wxMOUSE_BTN_AUX2:
709 return Aux2IsDown();
710 }
711 }
712
713 int wxMouseEvent::GetButton() const
714 {
715 for ( int i = 1; i < wxMOUSE_BTN_MAX; i++ )
716 {
717 if ( Button(i) )
718 {
719 return i;
720 }
721 }
722
723 return wxMOUSE_BTN_NONE;
724 }
725
726 // Find the logical position of the event given the DC
727 wxPoint wxMouseEvent::GetLogicalPosition(const wxDC& dc) const
728 {
729 wxPoint pt(dc.DeviceToLogicalX(m_x), dc.DeviceToLogicalY(m_y));
730 return pt;
731 }
732
733
734 /*
735 * Keyboard event
736 *
737 */
738
739 wxKeyEvent::wxKeyEvent(wxEventType type)
740 {
741 m_eventType = type;
742 m_shiftDown = false;
743 m_controlDown = false;
744 m_metaDown = false;
745 m_altDown = false;
746 m_keyCode = 0;
747 m_scanCode = 0;
748 #if wxUSE_UNICODE
749 m_uniChar = 0;
750 #endif
751 }
752
753 wxKeyEvent::wxKeyEvent(const wxKeyEvent& evt)
754 : wxEvent(evt)
755 {
756 m_x = evt.m_x;
757 m_y = evt.m_y;
758
759 m_keyCode = evt.m_keyCode;
760
761 m_controlDown = evt.m_controlDown;
762 m_shiftDown = evt.m_shiftDown;
763 m_altDown = evt.m_altDown;
764 m_metaDown = evt.m_metaDown;
765 m_scanCode = evt.m_scanCode;
766 m_rawCode = evt.m_rawCode;
767 m_rawFlags = evt.m_rawFlags;
768
769 #if wxUSE_UNICODE
770 m_uniChar = evt.m_uniChar;
771 #endif
772 }
773
774 wxWindowCreateEvent::wxWindowCreateEvent(wxWindow *win)
775 {
776 SetEventType(wxEVT_CREATE);
777 SetEventObject(win);
778 }
779
780 wxWindowDestroyEvent::wxWindowDestroyEvent(wxWindow *win)
781 {
782 SetEventType(wxEVT_DESTROY);
783 SetEventObject(win);
784 }
785
786 wxChildFocusEvent::wxChildFocusEvent(wxWindow *win)
787 : wxCommandEvent(wxEVT_CHILD_FOCUS)
788 {
789 SetEventObject(win);
790 }
791
792 // ----------------------------------------------------------------------------
793 // wxHelpEvent
794 // ----------------------------------------------------------------------------
795
796 /* static */
797 wxHelpEvent::Origin wxHelpEvent::GuessOrigin(Origin origin)
798 {
799 if ( origin == Origin_Unknown )
800 {
801 // assume that the event comes from the help button if it's not from
802 // keyboard and that pressing F1 always results in the help event
803 origin = wxGetKeyState(WXK_F1) ? Origin_Keyboard : Origin_HelpButton;
804 }
805
806 return origin;
807 }
808
809 #endif // wxUSE_GUI
810
811
812 #if wxUSE_BASE
813
814 // ----------------------------------------------------------------------------
815 // wxEventHashTable
816 // ----------------------------------------------------------------------------
817
818 static const int EVENT_TYPE_TABLE_INIT_SIZE = 31; // Not too big not too small...
819
820 wxEventHashTable* wxEventHashTable::sm_first = NULL;
821
822 wxEventHashTable::wxEventHashTable(const wxEventTable &table)
823 : m_table(table),
824 m_rebuildHash(true)
825 {
826 AllocEventTypeTable(EVENT_TYPE_TABLE_INIT_SIZE);
827
828 m_next = sm_first;
829 if (m_next)
830 m_next->m_previous = this;
831 sm_first = this;
832 }
833
834 wxEventHashTable::~wxEventHashTable()
835 {
836 if (m_next)
837 m_next->m_previous = m_previous;
838 if (m_previous)
839 m_previous->m_next = m_next;
840 if (sm_first == this)
841 sm_first = m_next;
842
843 Clear();
844 }
845
846 void wxEventHashTable::Clear()
847 {
848 for ( size_t i = 0; i < m_size; i++ )
849 {
850 EventTypeTablePointer eTTnode = m_eventTypeTable[i];
851 delete eTTnode;
852 }
853
854 delete[] m_eventTypeTable;
855 m_eventTypeTable = NULL;
856
857 m_size = 0;
858 }
859
860 #if defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING
861
862 // Clear all tables
863 void wxEventHashTable::ClearAll()
864 {
865 wxEventHashTable* table = sm_first;
866 while (table)
867 {
868 table->Clear();
869 table = table->m_next;
870 }
871 }
872
873 #endif // __WXDEBUG__ && wxUSE_MEMORY_TRACING
874
875 bool wxEventHashTable::HandleEvent(wxEvent &event, wxEvtHandler *self)
876 {
877 if (m_rebuildHash)
878 {
879 InitHashTable();
880 m_rebuildHash = false;
881 }
882
883 if (!m_eventTypeTable)
884 return false;
885
886 // Find all entries for the given event type.
887 wxEventType eventType = event.GetEventType();
888 const EventTypeTablePointer eTTnode = m_eventTypeTable[eventType % m_size];
889 if (eTTnode && eTTnode->eventType == eventType)
890 {
891 // Now start the search for an event handler
892 // that can handle an event with the given ID.
893 const wxEventTableEntryPointerArray&
894 eventEntryTable = eTTnode->eventEntryTable;
895
896 const size_t count = eventEntryTable.GetCount();
897 for (size_t n = 0; n < count; n++)
898 {
899 if ( wxEvtHandler::
900 ProcessEventIfMatches(*eventEntryTable[n], self, event) )
901 {
902 return true;
903 }
904 }
905 }
906
907 return false;
908 }
909
910 void wxEventHashTable::InitHashTable()
911 {
912 // Loop over the event tables and all its base tables.
913 const wxEventTable *table = &m_table;
914 while (table)
915 {
916 // Retrieve all valid event handler entries
917 const wxEventTableEntry *entry = table->entries;
918 while (entry->m_fn != 0)
919 {
920 // Add the event entry in the Hash.
921 AddEntry(*entry);
922
923 entry++;
924 }
925
926 table = table->baseTable;
927 }
928
929 // Lets free some memory.
930 size_t i;
931 for(i = 0; i < m_size; i++)
932 {
933 EventTypeTablePointer eTTnode = m_eventTypeTable[i];
934 if (eTTnode)
935 {
936 eTTnode->eventEntryTable.Shrink();
937 }
938 }
939 }
940
941 void wxEventHashTable::AddEntry(const wxEventTableEntry &entry)
942 {
943 // This might happen 'accidentally' as the app is exiting
944 if (!m_eventTypeTable)
945 return;
946
947 EventTypeTablePointer *peTTnode = &m_eventTypeTable[entry.m_eventType % m_size];
948 EventTypeTablePointer eTTnode = *peTTnode;
949
950 if (eTTnode)
951 {
952 if (eTTnode->eventType != entry.m_eventType)
953 {
954 // Resize the table!
955 GrowEventTypeTable();
956 // Try again to add it.
957 AddEntry(entry);
958 return;
959 }
960 }
961 else
962 {
963 eTTnode = new EventTypeTable;
964 eTTnode->eventType = entry.m_eventType;
965 *peTTnode = eTTnode;
966 }
967
968 // Fill all hash entries between entry.m_id and entry.m_lastId...
969 eTTnode->eventEntryTable.Add(&entry);
970 }
971
972 void wxEventHashTable::AllocEventTypeTable(size_t size)
973 {
974 m_eventTypeTable = new EventTypeTablePointer[size];
975 memset((void *)m_eventTypeTable, 0, sizeof(EventTypeTablePointer)*size);
976 m_size = size;
977 }
978
979 void wxEventHashTable::GrowEventTypeTable()
980 {
981 size_t oldSize = m_size;
982 EventTypeTablePointer *oldEventTypeTable = m_eventTypeTable;
983
984 // TODO: Search the most optimal grow sequence
985 AllocEventTypeTable(/* GetNextPrime(oldSize) */oldSize*2+1);
986
987 for ( size_t i = 0; i < oldSize; /* */ )
988 {
989 EventTypeTablePointer eTToldNode = oldEventTypeTable[i];
990 if (eTToldNode)
991 {
992 EventTypeTablePointer *peTTnode = &m_eventTypeTable[eTToldNode->eventType % m_size];
993 EventTypeTablePointer eTTnode = *peTTnode;
994
995 // Check for collision, we don't want any.
996 if (eTTnode)
997 {
998 GrowEventTypeTable();
999 continue; // Don't increment the counter,
1000 // as we still need to add this element.
1001 }
1002 else
1003 {
1004 // Get the old value and put it in the new table.
1005 *peTTnode = oldEventTypeTable[i];
1006 }
1007 }
1008
1009 i++;
1010 }
1011
1012 delete[] oldEventTypeTable;
1013 }
1014
1015
1016 // ----------------------------------------------------------------------------
1017 // wxEventConnectionRef
1018 // ----------------------------------------------------------------------------
1019
1020 // Below functions are mostly short but kept in cpp file to simplify setting
1021 // breakpoints (GDB)
1022 wxEventConnectionRef::wxEventConnectionRef(wxEvtHandler *src, wxEvtHandler *sink)
1023 : m_src(src), m_sink(sink), m_refCount(1)
1024 {
1025 wxASSERT( m_sink );
1026 m_sink->AddNode( this );
1027 }
1028
1029 wxEventConnectionRef::~wxEventConnectionRef()
1030 {
1031 }
1032
1033 void wxEventConnectionRef::OnObjectDestroy( )
1034 {
1035 if( m_src )
1036 m_src->OnSinkDestroyed( m_sink );
1037 // It is safe to delete this tracker object here
1038 delete this;
1039 }
1040
1041 void wxEventConnectionRef::DecRef( )
1042 {
1043 if( !--m_refCount )
1044 {
1045 // The sink holds the only external pointer to this object
1046 if( m_sink )
1047 m_sink->RemoveNode(this);
1048 delete this;
1049 }
1050 }
1051
1052
1053 // ----------------------------------------------------------------------------
1054 // wxEvtHandler
1055 // ----------------------------------------------------------------------------
1056
1057 /*
1058 * Event handler
1059 */
1060
1061 wxEvtHandler::wxEvtHandler()
1062 {
1063 m_nextHandler = (wxEvtHandler *) NULL;
1064 m_previousHandler = (wxEvtHandler *) NULL;
1065 m_enabled = true;
1066 m_dynamicEvents = (wxList *) NULL;
1067 m_pendingEvents = (wxList *) NULL;
1068
1069 // no client data (yet)
1070 m_clientData = NULL;
1071 m_clientDataType = wxClientData_None;
1072 }
1073
1074 wxEvtHandler::~wxEvtHandler()
1075 {
1076 // Takes itself out of the list of handlers
1077 if (m_previousHandler)
1078 m_previousHandler->m_nextHandler = m_nextHandler;
1079
1080 if (m_nextHandler)
1081 m_nextHandler->m_previousHandler = m_previousHandler;
1082
1083 if (m_dynamicEvents)
1084 {
1085 for ( wxList::iterator it = m_dynamicEvents->begin(),
1086 end = m_dynamicEvents->end();
1087 it != end;
1088 ++it )
1089 {
1090 wxDynamicEventTableEntry *entry = (wxDynamicEventTableEntry*)*it;
1091
1092 // Remove ourselves from sink destructor notifications
1093 // (this has usually been been done, in wxTrackable destructor)
1094 wxEvtHandler *eventSink = entry->m_eventSink;
1095 if( eventSink )
1096 {
1097 wxEventConnectionRef *pecr = FindRefInTrackerList( eventSink );
1098 if( pecr )
1099 {
1100 eventSink->RemoveNode( pecr );
1101 delete pecr;
1102 }
1103 }
1104
1105 if (entry->m_callbackUserData)
1106 delete entry->m_callbackUserData;
1107 delete entry;
1108 }
1109 delete m_dynamicEvents;
1110 };
1111
1112 if (m_pendingEvents)
1113 m_pendingEvents->DeleteContents(true);
1114 delete m_pendingEvents;
1115
1116 // Remove us from wxPendingEvents if necessary.
1117 if ( wxPendingEvents )
1118 {
1119 #if wxUSE_THREADS
1120 if(wxPendingEventsLocker)
1121 wxENTER_CRIT_SECT(*wxPendingEventsLocker);
1122 #endif
1123
1124 if ( wxPendingEvents->DeleteObject(this) )
1125 {
1126 // check that we were present only once in the list
1127 wxASSERT_MSG( !wxPendingEvents->Find(this),
1128 "Handler occurs twice in wxPendingEvents list" );
1129 }
1130 //else: we weren't in this list at all, it's ok
1131
1132 #if wxUSE_THREADS
1133 if(wxPendingEventsLocker)
1134 wxLEAVE_CRIT_SECT(*wxPendingEventsLocker);
1135 #endif
1136 }
1137
1138 // we only delete object data, not untyped
1139 if ( m_clientDataType == wxClientData_Object )
1140 delete m_clientObject;
1141 }
1142
1143 #if wxUSE_THREADS
1144
1145 bool wxEvtHandler::ProcessThreadEvent(const wxEvent& event)
1146 {
1147 // check that we are really in a child thread
1148 wxASSERT_MSG( !wxThread::IsMain(),
1149 wxT("use ProcessEvent() in main thread") );
1150
1151 AddPendingEvent(event);
1152
1153 return true;
1154 }
1155
1156 #endif // wxUSE_THREADS
1157
1158 void wxEvtHandler::AddPendingEvent(const wxEvent& event)
1159 {
1160 // 1) Add event to list of pending events of this event handler
1161
1162 wxEvent *eventCopy = event.Clone();
1163
1164 // we must be able to copy the events here so the event class must
1165 // implement Clone() properly instead of just providing a NULL stab for it
1166 wxCHECK_RET( eventCopy,
1167 _T("events of this type aren't supposed to be posted") );
1168
1169 wxENTER_CRIT_SECT( m_pendingEventsLock );
1170
1171 if ( !m_pendingEvents )
1172 m_pendingEvents = new wxList;
1173
1174 m_pendingEvents->Append(eventCopy);
1175
1176 wxLEAVE_CRIT_SECT( m_pendingEventsLock );
1177
1178 // 2) Add this event handler to list of event handlers that
1179 // have pending events.
1180
1181 wxENTER_CRIT_SECT(*wxPendingEventsLocker);
1182
1183 if ( !wxPendingEvents )
1184 wxPendingEvents = new wxList;
1185 if ( !wxPendingEvents->Find(this) )
1186 wxPendingEvents->Append(this);
1187
1188 wxLEAVE_CRIT_SECT(*wxPendingEventsLocker);
1189
1190 // 3) Inform the system that new pending events are somewhere,
1191 // and that these should be processed in idle time.
1192 wxWakeUpIdle();
1193 }
1194
1195 void wxEvtHandler::ProcessPendingEvents()
1196 {
1197 wxENTER_CRIT_SECT( m_pendingEventsLock );
1198
1199 // this method is only called by wxApp if this handler does have
1200 // pending events
1201 wxCHECK_RET( m_pendingEvents && !m_pendingEvents->IsEmpty(),
1202 "should have pending events if called" );
1203
1204 wxList::compatibility_iterator node = m_pendingEvents->GetFirst();
1205 wxEvent * const event = wx_static_cast(wxEvent *, node->GetData());
1206
1207 // it's important we remove event from list before processing it, else a
1208 // nested event loop, for example from a modal dialog, might process the
1209 // same event again.
1210 m_pendingEvents->Erase(node);
1211
1212 // if there are no more pending events left, we don't need to stay in this
1213 // list
1214 if ( m_pendingEvents->IsEmpty() )
1215 wxPendingEvents->DeleteObject(this);
1216
1217 wxLEAVE_CRIT_SECT( m_pendingEventsLock );
1218
1219 ProcessEvent(*event);
1220
1221 // careful: this object could have been deleted by the event handler
1222 // executed by the above ProcessEvent() call, so we can't access any fields
1223 // of this object any more
1224
1225 delete event;
1226 }
1227
1228 /*
1229 * Event table stuff
1230 */
1231 /* static */ bool
1232 wxEvtHandler::ProcessEventIfMatches(const wxEventTableEntryBase& entry,
1233 wxEvtHandler *handler,
1234 wxEvent& event)
1235 {
1236 int tableId1 = entry.m_id,
1237 tableId2 = entry.m_lastId;
1238
1239 // match only if the event type is the same and the id is either -1 in
1240 // the event table (meaning "any") or the event id matches the id
1241 // specified in the event table either exactly or by falling into
1242 // range between first and last
1243 if ((tableId1 == wxID_ANY) ||
1244 (tableId2 == wxID_ANY && tableId1 == event.GetId()) ||
1245 (tableId2 != wxID_ANY &&
1246 (event.GetId() >= tableId1 && event.GetId() <= tableId2)))
1247 {
1248 event.Skip(false);
1249 event.m_callbackUserData = entry.m_callbackUserData;
1250
1251 #if wxUSE_EXCEPTIONS
1252 if ( wxTheApp )
1253 {
1254 // call the handler via wxApp method which allows the user to catch
1255 // any exceptions which may be thrown by any handler in the program
1256 // in one place
1257 wxTheApp->HandleEvent(handler, (wxEventFunction)entry.m_fn, event);
1258 }
1259 else
1260 #endif // wxUSE_EXCEPTIONS
1261 {
1262 // no need for an extra virtual function call
1263 (handler->*((wxEventFunction) (entry.m_fn)))(event);
1264 }
1265
1266 if (!event.GetSkipped())
1267 return true;
1268 }
1269
1270 return false;
1271 }
1272
1273 bool wxEvtHandler::TryParent(wxEvent& event)
1274 {
1275 if ( wxTheApp && (this != wxTheApp) )
1276 {
1277 // Special case: don't pass wxEVT_IDLE to wxApp, since it'll always
1278 // swallow it. wxEVT_IDLE is sent explicitly to wxApp so it will be
1279 // processed appropriately via SearchEventTable.
1280 if ( event.GetEventType() != wxEVT_IDLE )
1281 {
1282 if ( wxTheApp->ProcessEvent(event) )
1283 return true;
1284 }
1285 }
1286
1287 return false;
1288 }
1289
1290 bool wxEvtHandler::ProcessEvent(wxEvent& event)
1291 {
1292 // allow the application to hook into event processing
1293 if ( wxTheApp )
1294 {
1295 int rc = wxTheApp->FilterEvent(event);
1296 if ( rc != -1 )
1297 {
1298 wxASSERT_MSG( rc == 1 || rc == 0,
1299 _T("unexpected wxApp::FilterEvent return value") );
1300
1301 return rc != 0;
1302 }
1303 //else: proceed normally
1304 }
1305
1306 // An event handler can be enabled or disabled
1307 if ( GetEvtHandlerEnabled() )
1308 {
1309 // if we have a validator, it has higher priority than our own event
1310 // table
1311 if ( TryValidator(event) )
1312 return true;
1313
1314 // Handle per-instance dynamic event tables first
1315 if ( m_dynamicEvents && SearchDynamicEventTable(event) )
1316 return true;
1317
1318 // Then static per-class event tables
1319 if ( GetEventHashTable().HandleEvent(event, this) )
1320 return true;
1321 }
1322
1323 // Try going down the event handler chain
1324 if ( GetNextHandler() )
1325 {
1326 // notice that we shouldn't let the parent have the event even if the
1327 // next handler does not process it because it will have already passed
1328 // it to the parent in this case
1329 return GetNextHandler()->ProcessEvent(event);
1330 }
1331
1332 // Finally propagate the event upwards the window chain and/or to the
1333 // application object as necessary
1334 return TryParent(event);
1335 }
1336
1337 bool wxEvtHandler::SafelyProcessEvent(wxEvent& event)
1338 {
1339 #if wxUSE_EXCEPTIONS
1340 try
1341 {
1342 #endif
1343 return ProcessEvent(event);
1344 #if wxUSE_EXCEPTIONS
1345 }
1346 catch ( ... )
1347 {
1348 wxEventLoopBase *loop = wxEventLoopBase::GetActive();
1349 try
1350 {
1351 if ( !wxTheApp || !wxTheApp->OnExceptionInMainLoop() )
1352 {
1353 if ( loop )
1354 loop->Exit();
1355 }
1356 //else: continue running current event loop
1357
1358 return false;
1359 }
1360 catch ( ... )
1361 {
1362 // OnExceptionInMainLoop() threw, possibly rethrowing the same
1363 // exception again: very good, but we still need Exit() to
1364 // be called
1365 if ( loop )
1366 loop->Exit();
1367 throw;
1368 }
1369 }
1370 #endif // wxUSE_EXCEPTIONS
1371 }
1372
1373
1374 bool wxEvtHandler::SearchEventTable(wxEventTable& table, wxEvent& event)
1375 {
1376 const wxEventType eventType = event.GetEventType();
1377 for ( int i = 0; table.entries[i].m_fn != 0; i++ )
1378 {
1379 const wxEventTableEntry& entry = table.entries[i];
1380 if ( eventType == entry.m_eventType )
1381 {
1382 if ( ProcessEventIfMatches(entry, this, event) )
1383 return true;
1384 }
1385 }
1386
1387 return false;
1388 }
1389
1390 void wxEvtHandler::Connect( int id, int lastId,
1391 int eventType,
1392 wxObjectEventFunction func,
1393 wxObject *userData,
1394 wxEvtHandler* eventSink )
1395 {
1396 wxDynamicEventTableEntry *entry =
1397 new wxDynamicEventTableEntry(eventType, id, lastId, func, userData, eventSink);
1398
1399 if (!m_dynamicEvents)
1400 m_dynamicEvents = new wxList;
1401
1402 // Insert at the front of the list so most recent additions are found first
1403 m_dynamicEvents->Insert( (wxObject*) entry );
1404
1405 // Make sure we get to know when a sink is destroyed
1406 if( eventSink )
1407 {
1408 wxEventConnectionRef *pecr = FindRefInTrackerList( eventSink );
1409 if( pecr )
1410 pecr->IncRef( );
1411 else
1412 pecr = new wxEventConnectionRef(this,eventSink);
1413 }
1414 }
1415
1416 bool wxEvtHandler::Disconnect( int id, int lastId, wxEventType eventType,
1417 wxObjectEventFunction func,
1418 wxObject *userData,
1419 wxEvtHandler* eventSink )
1420 {
1421 if (!m_dynamicEvents)
1422 return false;
1423
1424 // Remove connection from tracker node (wxEventConnectionRef)
1425 if( eventSink )
1426 {
1427 wxEventConnectionRef *pecr = FindRefInTrackerList( eventSink );
1428 if( pecr )
1429 pecr->DecRef( );
1430 }
1431
1432 wxList::compatibility_iterator node = m_dynamicEvents->GetFirst();
1433 while (node)
1434 {
1435 wxDynamicEventTableEntry *entry = (wxDynamicEventTableEntry*)node->GetData();
1436
1437 if ((entry->m_id == id) &&
1438 ((entry->m_lastId == lastId) || (lastId == wxID_ANY)) &&
1439 ((entry->m_eventType == eventType) || (eventType == wxEVT_NULL)) &&
1440 ((entry->m_fn == func) || (func == (wxObjectEventFunction)NULL)) &&
1441 ((entry->m_eventSink == eventSink) || (eventSink == (wxEvtHandler*)NULL)) &&
1442 ((entry->m_callbackUserData == userData) || (userData == (wxObject*)NULL)))
1443 {
1444 if (entry->m_callbackUserData)
1445 delete entry->m_callbackUserData;
1446 m_dynamicEvents->Erase( node );
1447 delete entry;
1448 return true;
1449 }
1450 node = node->GetNext();
1451 }
1452 return false;
1453 }
1454
1455 bool wxEvtHandler::SearchDynamicEventTable( wxEvent& event )
1456 {
1457 wxCHECK_MSG( m_dynamicEvents, false,
1458 wxT("caller should check that we have dynamic events") );
1459
1460 wxList::compatibility_iterator node = m_dynamicEvents->GetFirst();
1461 while (node)
1462 {
1463 wxDynamicEventTableEntry *entry = (wxDynamicEventTableEntry*)node->GetData();
1464
1465 // get next node before (maybe) calling the event handler as it could
1466 // call Disconnect() invalidating the current node
1467 node = node->GetNext();
1468
1469 if ((event.GetEventType() == entry->m_eventType) && (entry->m_fn != 0))
1470 {
1471 wxEvtHandler *handler = entry->m_eventSink ? entry->m_eventSink
1472 : this;
1473 if ( ProcessEventIfMatches(*entry, handler, event) )
1474 return true;
1475 }
1476 }
1477
1478 return false;
1479 }
1480
1481 void wxEvtHandler::DoSetClientObject( wxClientData *data )
1482 {
1483 wxASSERT_MSG( m_clientDataType != wxClientData_Void,
1484 wxT("can't have both object and void client data") );
1485
1486 if ( m_clientObject )
1487 delete m_clientObject;
1488
1489 m_clientObject = data;
1490 m_clientDataType = wxClientData_Object;
1491 }
1492
1493 wxClientData *wxEvtHandler::DoGetClientObject() const
1494 {
1495 // it's not an error to call GetClientObject() on a window which doesn't
1496 // have client data at all - NULL will be returned
1497 wxASSERT_MSG( m_clientDataType != wxClientData_Void,
1498 wxT("this window doesn't have object client data") );
1499
1500 return m_clientObject;
1501 }
1502
1503 void wxEvtHandler::DoSetClientData( void *data )
1504 {
1505 wxASSERT_MSG( m_clientDataType != wxClientData_Object,
1506 wxT("can't have both object and void client data") );
1507
1508 m_clientData = data;
1509 m_clientDataType = wxClientData_Void;
1510 }
1511
1512 void *wxEvtHandler::DoGetClientData() const
1513 {
1514 // it's not an error to call GetClientData() on a window which doesn't have
1515 // client data at all - NULL will be returned
1516 wxASSERT_MSG( m_clientDataType != wxClientData_Object,
1517 wxT("this window doesn't have void client data") );
1518
1519 return m_clientData;
1520 }
1521
1522 // A helper func to find an wxEventConnectionRef object
1523 wxEventConnectionRef* wxEvtHandler::FindRefInTrackerList( wxEvtHandler *eventSink )
1524 {
1525 wxASSERT(eventSink);
1526 for( wxTrackerNode *ptn=eventSink->GetFirst(); ptn; ptn=ptn->m_nxt )
1527 {
1528 // Only want wxEventConnectionRef nodes here
1529 if( ptn->GetType()!=wxTrackerNode::EventConnectionRef )
1530 continue;
1531 wxEventConnectionRef *pecr = static_cast<wxEventConnectionRef*>(ptn);
1532 if( pecr && pecr->m_src==this )
1533 {
1534 wxASSERT( pecr->m_sink==eventSink );
1535 return pecr;
1536 }
1537 }
1538 return NULL;
1539 }
1540
1541 void wxEvtHandler::OnSinkDestroyed( wxEvtHandler *sink )
1542 {
1543 wxASSERT(m_dynamicEvents);
1544
1545 // remove all connections with this sink
1546 wxList::compatibility_iterator node = m_dynamicEvents->GetFirst(), node_nxt;
1547 while (node)
1548 {
1549 wxDynamicEventTableEntry *entry = (wxDynamicEventTableEntry*)node->GetData();
1550 node_nxt = node->GetNext();
1551
1552 if( entry->m_eventSink==sink )
1553 {
1554 if (entry->m_callbackUserData)
1555 delete entry->m_callbackUserData;
1556 m_dynamicEvents->Erase( node );
1557 delete entry;
1558 }
1559 node = node_nxt;
1560 }
1561 }
1562
1563 #endif // wxUSE_BASE
1564
1565 #if wxUSE_GUI
1566
1567 // Find a window with the focus, that is also a descendant of the given window.
1568 // This is used to determine the window to initially send commands to.
1569 wxWindow* wxFindFocusDescendant(wxWindow* ancestor)
1570 {
1571 // Process events starting with the window with the focus, if any.
1572 wxWindow* focusWin = wxWindow::FindFocus();
1573 wxWindow* win = focusWin;
1574
1575 // Check if this is a descendant of this frame.
1576 // If not, win will be set to NULL.
1577 while (win)
1578 {
1579 if (win == ancestor)
1580 break;
1581 else
1582 win = win->GetParent();
1583 }
1584 if (win == (wxWindow*) NULL)
1585 focusWin = (wxWindow*) NULL;
1586
1587 return focusWin;
1588 }
1589
1590 // ----------------------------------------------------------------------------
1591 // wxEventBlocker
1592 // ----------------------------------------------------------------------------
1593
1594 wxEventBlocker::wxEventBlocker(wxWindow *win, wxEventType type)
1595 {
1596 wxCHECK_RET(win, wxT("Null window given to wxEventBlocker"));
1597
1598 m_window = win;
1599
1600 Block(type);
1601 m_window->PushEventHandler(this);
1602 }
1603
1604 wxEventBlocker::~wxEventBlocker()
1605 {
1606 wxEvtHandler *popped = m_window->PopEventHandler(false);
1607 wxCHECK_RET(popped == this,
1608 wxT("Don't push other event handlers into a window managed by wxEventBlocker!"));
1609 }
1610
1611 bool wxEventBlocker::ProcessEvent(wxEvent& event)
1612 {
1613 // should this event be blocked?
1614 for ( size_t i = 0; i < m_eventsToBlock.size(); i++ )
1615 {
1616 wxEventType t = (wxEventType)m_eventsToBlock[i];
1617 if ( t == wxEVT_ANY || t == event.GetEventType() )
1618 return true; // yes, it should: mark this event as processed
1619 }
1620
1621 return false;
1622 }
1623
1624 #endif // wxUSE_GUI