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