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