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