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