added clipboard events (wxEVT_COMMAND_TEXT_COPY/CUT/PASTE) and implemented them for...
[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 #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 if (m_pendingEvents)
1035 m_pendingEvents->DeleteContents(true);
1036 delete m_pendingEvents;
1037
1038 #if wxUSE_THREADS
1039 # if !defined(__VISAGECPP__)
1040 delete m_eventsLocker;
1041 # endif
1042
1043 // Remove us from wxPendingEvents if necessary.
1044 if(wxPendingEventsLocker)
1045 wxENTER_CRIT_SECT(*wxPendingEventsLocker);
1046 if ( wxPendingEvents )
1047 {
1048 // Delete all occurences of this from the list of pending events
1049 while (wxPendingEvents->DeleteObject(this)) { } // Do nothing
1050 }
1051 if(wxPendingEventsLocker)
1052 wxLEAVE_CRIT_SECT(*wxPendingEventsLocker);
1053 #endif
1054
1055 // we only delete object data, not untyped
1056 if ( m_clientDataType == wxClientData_Object )
1057 delete m_clientObject;
1058 }
1059
1060 #if wxUSE_THREADS
1061
1062 bool wxEvtHandler::ProcessThreadEvent(wxEvent& event)
1063 {
1064 // check that we are really in a child thread
1065 wxASSERT_MSG( !wxThread::IsMain(),
1066 wxT("use ProcessEvent() in main thread") );
1067
1068 AddPendingEvent(event);
1069
1070 return true;
1071 }
1072
1073 void wxEvtHandler::ClearEventLocker()
1074 {
1075 #if !defined(__VISAGECPP__)
1076 delete m_eventsLocker;
1077 m_eventsLocker = NULL;
1078 #endif
1079 }
1080
1081 #endif // wxUSE_THREADS
1082
1083 void wxEvtHandler::AddPendingEvent(wxEvent& event)
1084 {
1085 // 1) Add event to list of pending events of this event handler
1086
1087 wxEvent *eventCopy = event.Clone();
1088
1089 // we must be able to copy the events here so the event class must
1090 // implement Clone() properly instead of just providing a NULL stab for it
1091 wxCHECK_RET( eventCopy,
1092 _T("events of this type aren't supposed to be posted") );
1093
1094 wxENTER_CRIT_SECT( Lock() );
1095
1096 if ( !m_pendingEvents )
1097 m_pendingEvents = new wxList;
1098
1099 m_pendingEvents->Append(eventCopy);
1100
1101 wxLEAVE_CRIT_SECT( Lock() );
1102
1103 // 2) Add this event handler to list of event handlers that
1104 // have pending events.
1105
1106 wxENTER_CRIT_SECT(*wxPendingEventsLocker);
1107
1108 if ( !wxPendingEvents )
1109 wxPendingEvents = new wxList;
1110 wxPendingEvents->Append(this);
1111
1112 wxLEAVE_CRIT_SECT(*wxPendingEventsLocker);
1113
1114 // 3) Inform the system that new pending events are somewhere,
1115 // and that these should be processed in idle time.
1116 wxWakeUpIdle();
1117 }
1118
1119 void wxEvtHandler::ProcessPendingEvents()
1120 {
1121 // this method is only called by wxApp if this handler does have pending
1122 // events
1123 wxCHECK_RET( m_pendingEvents,
1124 wxT("Please call wxApp::ProcessPendingEvents() instead") );
1125
1126 wxENTER_CRIT_SECT( Lock() );
1127
1128 // we leave the loop once we have processed all events that were present at
1129 // the start of ProcessPendingEvents because otherwise we could get into
1130 // infinite loop if the pending event handler execution resulted in another
1131 // event being posted
1132 size_t n = m_pendingEvents->size();
1133 for ( wxList::compatibility_iterator node = m_pendingEvents->GetFirst();
1134 node;
1135 node = m_pendingEvents->GetFirst() )
1136 {
1137 wxEvent *event = (wxEvent *)node->GetData();
1138
1139 // It's importan we remove event from list before processing it.
1140 // Else a nested event loop, for example from a modal dialog, might
1141 // process the same event again.
1142 m_pendingEvents->Erase(node);
1143
1144 wxLEAVE_CRIT_SECT( Lock() );
1145
1146 ProcessEvent(*event);
1147 delete event;
1148
1149 wxENTER_CRIT_SECT( Lock() );
1150
1151 if ( --n == 0 )
1152 break;
1153 }
1154
1155 wxLEAVE_CRIT_SECT( Lock() );
1156 }
1157
1158 /*
1159 * Event table stuff
1160 */
1161 /* static */ bool
1162 wxEvtHandler::ProcessEventIfMatches(const wxEventTableEntryBase& entry,
1163 wxEvtHandler *handler,
1164 wxEvent& event)
1165 {
1166 int tableId1 = entry.m_id,
1167 tableId2 = entry.m_lastId;
1168
1169 // match only if the event type is the same and the id is either -1 in
1170 // the event table (meaning "any") or the event id matches the id
1171 // specified in the event table either exactly or by falling into
1172 // range between first and last
1173 if ((tableId1 == wxID_ANY) ||
1174 (tableId2 == wxID_ANY && tableId1 == event.GetId()) ||
1175 (tableId2 != wxID_ANY &&
1176 (event.GetId() >= tableId1 && event.GetId() <= tableId2)))
1177 {
1178 event.Skip(false);
1179 event.m_callbackUserData = entry.m_callbackUserData;
1180
1181 #if wxUSE_EXCEPTIONS
1182 if ( wxTheApp )
1183 {
1184 // call the handler via wxApp method which allows the user to catch
1185 // any exceptions which may be thrown by any handler in the program
1186 // in one place
1187 wxTheApp->HandleEvent(handler, (wxEventFunction)entry.m_fn, event);
1188 }
1189 else
1190 #endif // wxUSE_EXCEPTIONS
1191 {
1192 // no need for an extra virtual function call
1193 (handler->*((wxEventFunction) (entry.m_fn)))(event);
1194 }
1195
1196 if (!event.GetSkipped())
1197 return true;
1198 }
1199
1200 return false;
1201 }
1202
1203 bool wxEvtHandler::TryParent(wxEvent& event)
1204 {
1205 if ( wxTheApp && (this != wxTheApp) )
1206 {
1207 // Special case: don't pass wxEVT_IDLE to wxApp, since it'll always
1208 // swallow it. wxEVT_IDLE is sent explicitly to wxApp so it will be
1209 // processed appropriately via SearchEventTable.
1210 if ( event.GetEventType() != wxEVT_IDLE )
1211 {
1212 if ( wxTheApp->ProcessEvent(event) )
1213 return true;
1214 }
1215 }
1216
1217 return false;
1218 }
1219
1220 bool wxEvtHandler::ProcessEvent(wxEvent& event)
1221 {
1222 // allow the application to hook into event processing
1223 if ( wxTheApp )
1224 {
1225 int rc = wxTheApp->FilterEvent(event);
1226 if ( rc != -1 )
1227 {
1228 wxASSERT_MSG( rc == 1 || rc == 0,
1229 _T("unexpected wxApp::FilterEvent return value") );
1230
1231 return rc != 0;
1232 }
1233 //else: proceed normally
1234 }
1235
1236 // An event handler can be enabled or disabled
1237 if ( GetEvtHandlerEnabled() )
1238 {
1239 // if we have a validator, it has higher priority than our own event
1240 // table
1241 if ( TryValidator(event) )
1242 return true;
1243
1244 // Handle per-instance dynamic event tables first
1245 if ( m_dynamicEvents && SearchDynamicEventTable(event) )
1246 return true;
1247
1248 // Then static per-class event tables
1249 if ( GetEventHashTable().HandleEvent(event, this) )
1250 return true;
1251 }
1252
1253 // Try going down the event handler chain
1254 if ( GetNextHandler() )
1255 {
1256 if ( GetNextHandler()->ProcessEvent(event) )
1257 return true;
1258 }
1259
1260 // Finally propagate the event upwards the window chain and/or to the
1261 // application object as necessary
1262 return TryParent(event);
1263 }
1264
1265
1266 bool wxEvtHandler::SearchEventTable(wxEventTable& table, wxEvent& event)
1267 {
1268 const wxEventType eventType = event.GetEventType();
1269 for ( int i = 0; table.entries[i].m_fn != 0; i++ )
1270 {
1271 const wxEventTableEntry& entry = table.entries[i];
1272 if ( eventType == entry.m_eventType )
1273 {
1274 if ( ProcessEventIfMatches(entry, this, event) )
1275 return true;
1276 }
1277 }
1278
1279 return false;
1280 }
1281
1282 void wxEvtHandler::Connect( int id, int lastId,
1283 int eventType,
1284 wxObjectEventFunction func,
1285 wxObject *userData,
1286 wxEvtHandler* eventSink )
1287 {
1288 #if WXWIN_COMPATIBILITY_EVENT_TYPES
1289 wxEventTableEntry *entry = new wxEventTableEntry;
1290 entry->m_eventType = eventType;
1291 entry->m_id = id;
1292 entry->m_lastId = lastId;
1293 entry->m_fn = func;
1294 entry->m_callbackUserData = userData;
1295 #else // !WXWIN_COMPATIBILITY_EVENT_TYPES
1296 wxDynamicEventTableEntry *entry =
1297 new wxDynamicEventTableEntry(eventType, id, lastId, func, userData, eventSink);
1298 #endif // WXWIN_COMPATIBILITY_EVENT_TYPES/!WXWIN_COMPATIBILITY_EVENT_TYPES
1299
1300 if (!m_dynamicEvents)
1301 m_dynamicEvents = new wxList;
1302
1303 // Insert at the front of the list so most recent additions are found first
1304 m_dynamicEvents->Insert( (wxObject*) entry );
1305 }
1306
1307 bool wxEvtHandler::Disconnect( int id, int lastId, wxEventType eventType,
1308 wxObjectEventFunction func,
1309 wxObject *userData,
1310 wxEvtHandler* eventSink )
1311 {
1312 if (!m_dynamicEvents)
1313 return false;
1314
1315 wxList::compatibility_iterator node = m_dynamicEvents->GetFirst();
1316 while (node)
1317 {
1318 #if WXWIN_COMPATIBILITY_EVENT_TYPES
1319 wxEventTableEntry *entry = (wxEventTableEntry*)node->GetData();
1320 #else // !WXWIN_COMPATIBILITY_EVENT_TYPES
1321 wxDynamicEventTableEntry *entry = (wxDynamicEventTableEntry*)node->GetData();
1322 #endif // WXWIN_COMPATIBILITY_EVENT_TYPES/!WXWIN_COMPATIBILITY_EVENT_TYPES
1323
1324 if ((entry->m_id == id) &&
1325 ((entry->m_lastId == lastId) || (lastId == wxID_ANY)) &&
1326 ((entry->m_eventType == eventType) || (eventType == wxEVT_NULL)) &&
1327 ((entry->m_fn == func) || (func == (wxObjectEventFunction)NULL)) &&
1328 ((entry->m_eventSink == eventSink) || (eventSink == (wxEvtHandler*)NULL)) &&
1329 ((entry->m_callbackUserData == userData) || (userData == (wxObject*)NULL)))
1330 {
1331 if (entry->m_callbackUserData)
1332 delete entry->m_callbackUserData;
1333 m_dynamicEvents->Erase( node );
1334 delete entry;
1335 return true;
1336 }
1337 node = node->GetNext();
1338 }
1339 return false;
1340 }
1341
1342 bool wxEvtHandler::SearchDynamicEventTable( wxEvent& event )
1343 {
1344 wxCHECK_MSG( m_dynamicEvents, false,
1345 wxT("caller should check that we have dynamic events") );
1346
1347 wxList::compatibility_iterator node = m_dynamicEvents->GetFirst();
1348 while (node)
1349 {
1350 #if WXWIN_COMPATIBILITY_EVENT_TYPES
1351 wxEventTableEntry *entry = (wxEventTableEntry*)node->GetData();
1352 #else // !WXWIN_COMPATIBILITY_EVENT_TYPES
1353 wxDynamicEventTableEntry *entry = (wxDynamicEventTableEntry*)node->GetData();
1354 #endif // WXWIN_COMPATIBILITY_EVENT_TYPES/!WXWIN_COMPATIBILITY_EVENT_TYPES
1355
1356 // get next node before (maybe) calling the event handler as it could
1357 // call Disconnect() invalidating the current node
1358 node = node->GetNext();
1359
1360 if ((event.GetEventType() == entry->m_eventType) && (entry->m_fn != 0))
1361 {
1362 wxEvtHandler *handler =
1363 #if !WXWIN_COMPATIBILITY_EVENT_TYPES
1364 entry->m_eventSink ? entry->m_eventSink
1365 :
1366 #endif
1367 this;
1368
1369 if ( ProcessEventIfMatches(*entry, handler, event) )
1370 {
1371 return true;
1372 }
1373 }
1374 }
1375
1376 return false;
1377 }
1378
1379 void wxEvtHandler::DoSetClientObject( wxClientData *data )
1380 {
1381 wxASSERT_MSG( m_clientDataType != wxClientData_Void,
1382 wxT("can't have both object and void client data") );
1383
1384 if ( m_clientObject )
1385 delete m_clientObject;
1386
1387 m_clientObject = data;
1388 m_clientDataType = wxClientData_Object;
1389 }
1390
1391 wxClientData *wxEvtHandler::DoGetClientObject() const
1392 {
1393 // it's not an error to call GetClientObject() on a window which doesn't
1394 // have client data at all - NULL will be returned
1395 wxASSERT_MSG( m_clientDataType != wxClientData_Void,
1396 wxT("this window doesn't have object client data") );
1397
1398 return m_clientObject;
1399 }
1400
1401 void wxEvtHandler::DoSetClientData( void *data )
1402 {
1403 wxASSERT_MSG( m_clientDataType != wxClientData_Object,
1404 wxT("can't have both object and void client data") );
1405
1406 m_clientData = data;
1407 m_clientDataType = wxClientData_Void;
1408 }
1409
1410 void *wxEvtHandler::DoGetClientData() const
1411 {
1412 // it's not an error to call GetClientData() on a window which doesn't have
1413 // client data at all - NULL will be returned
1414 wxASSERT_MSG( m_clientDataType != wxClientData_Object,
1415 wxT("this window doesn't have void client data") );
1416
1417 return m_clientData;
1418 }
1419
1420 #endif // wxUSE_BASE
1421
1422 #if wxUSE_GUI
1423
1424 // Find a window with the focus, that is also a descendant of the given window.
1425 // This is used to determine the window to initially send commands to.
1426 wxWindow* wxFindFocusDescendant(wxWindow* ancestor)
1427 {
1428 // Process events starting with the window with the focus, if any.
1429 wxWindow* focusWin = wxWindow::FindFocus();
1430 wxWindow* win = focusWin;
1431
1432 // Check if this is a descendant of this frame.
1433 // If not, win will be set to NULL.
1434 while (win)
1435 {
1436 if (win == ancestor)
1437 break;
1438 else
1439 win = win->GetParent();
1440 }
1441 if (win == (wxWindow*) NULL)
1442 focusWin = (wxWindow*) NULL;
1443
1444 return focusWin;
1445 }
1446
1447 #endif // wxUSE_GUI