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