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