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