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