]> git.saurik.com Git - wxWidgets.git/blob - src/common/event.cpp
Committing in .
[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 and Markus Holzem
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #ifdef __GNUG__
21 #pragma implementation "event.h"
22 #endif
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 #ifndef WX_PRECOMP
32 #include "wx/defs.h"
33 #include "wx/app.h"
34 #include "wx/list.h"
35
36 #if wxUSE_GUI
37 #include "wx/control.h"
38 #include "wx/utils.h"
39 #include "wx/dc.h"
40 #endif // wxUSE_GUI
41 #endif
42
43 #include "wx/event.h"
44
45 #if wxUSE_GUI
46 #include "wx/validate.h"
47 #endif // wxUSE_GUI
48
49 // ----------------------------------------------------------------------------
50 // wxWin macros
51 // ----------------------------------------------------------------------------
52
53 IMPLEMENT_DYNAMIC_CLASS(wxEvtHandler, wxObject)
54 IMPLEMENT_ABSTRACT_CLASS(wxEvent, wxObject)
55 IMPLEMENT_DYNAMIC_CLASS(wxIdleEvent, wxEvent)
56
57 #if wxUSE_GUI
58 IMPLEMENT_DYNAMIC_CLASS(wxCommandEvent, wxEvent)
59 IMPLEMENT_DYNAMIC_CLASS(wxNotifyEvent, wxCommandEvent)
60 IMPLEMENT_DYNAMIC_CLASS(wxScrollEvent, wxCommandEvent)
61 IMPLEMENT_DYNAMIC_CLASS(wxScrollWinEvent, wxEvent)
62 IMPLEMENT_DYNAMIC_CLASS(wxMouseEvent, wxEvent)
63 IMPLEMENT_DYNAMIC_CLASS(wxKeyEvent, wxEvent)
64 IMPLEMENT_DYNAMIC_CLASS(wxSizeEvent, wxEvent)
65 IMPLEMENT_DYNAMIC_CLASS(wxPaintEvent, wxEvent)
66 IMPLEMENT_DYNAMIC_CLASS(wxEraseEvent, wxEvent)
67 IMPLEMENT_DYNAMIC_CLASS(wxMoveEvent, wxEvent)
68 IMPLEMENT_DYNAMIC_CLASS(wxFocusEvent, wxEvent)
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(wxSysColourChangedEvent, wxEvent)
79 IMPLEMENT_DYNAMIC_CLASS(wxUpdateUIEvent, wxCommandEvent)
80 IMPLEMENT_DYNAMIC_CLASS(wxNavigationKeyEvent, wxCommandEvent)
81 IMPLEMENT_DYNAMIC_CLASS(wxPaletteChangedEvent, wxEvent)
82 IMPLEMENT_DYNAMIC_CLASS(wxQueryNewPaletteEvent, wxEvent)
83 IMPLEMENT_DYNAMIC_CLASS(wxWindowCreateEvent, wxEvent)
84 IMPLEMENT_DYNAMIC_CLASS(wxWindowDestroyEvent, wxEvent)
85 #endif // wxUSE_GUI
86
87 const wxEventTable *wxEvtHandler::GetEventTable() const
88 { return &wxEvtHandler::sm_eventTable; }
89
90 const wxEventTable wxEvtHandler::sm_eventTable =
91 { (const wxEventTable *)NULL, &wxEvtHandler::sm_eventTableEntries[0] };
92
93 const wxEventTableEntry wxEvtHandler::sm_eventTableEntries[] =
94 { { 0, 0, 0, (wxObjectEventFunction) NULL, (wxObject*) NULL } };
95
96
97 // ----------------------------------------------------------------------------
98 // global variables
99 // ----------------------------------------------------------------------------
100
101 // To put pending event handlers
102 wxList *wxPendingEvents = (wxList *)NULL;
103
104 #if wxUSE_THREADS
105 // protects wxPendingEvents list
106 wxCriticalSection *wxPendingEventsLocker = (wxCriticalSection *)NULL;
107 #endif
108
109 // ============================================================================
110 // implementation
111 // ============================================================================
112
113 // ----------------------------------------------------------------------------
114 // wxEvent
115 // ----------------------------------------------------------------------------
116
117 /*
118 * General wxWindows events, covering
119 * all interesting things that might happen (button clicking, resizing,
120 * setting text in widgets, etc.).
121 *
122 * For each completely new event type, derive a new event class.
123 *
124 */
125
126 wxEvent::wxEvent(int theId)
127 {
128 m_eventType = wxEVT_NULL;
129 m_eventObject = (wxObject *) NULL;
130 m_timeStamp = 0;
131 m_id = theId;
132 m_skipped = FALSE;
133 m_callbackUserData = (wxObject *) NULL;
134 m_isCommandEvent = FALSE;
135 }
136
137 void wxEvent::CopyObject(wxObject& object_dest) const
138 {
139 wxEvent *obj = (wxEvent *)&object_dest;
140 wxObject::CopyObject(object_dest);
141
142 obj->m_eventType = m_eventType;
143 obj->m_eventObject = m_eventObject;
144 obj->m_timeStamp = m_timeStamp;
145 obj->m_id = m_id;
146 obj->m_skipped = m_skipped;
147 obj->m_callbackUserData = m_callbackUserData;
148 obj->m_isCommandEvent = m_isCommandEvent;
149 }
150
151 #if wxUSE_GUI
152
153 /*
154 * Command events
155 *
156 */
157
158 wxCommandEvent::wxCommandEvent(wxEventType commandType, int theId)
159 {
160 m_eventType = commandType;
161 m_clientData = (char *) NULL;
162 m_clientObject = (wxClientData *) NULL;
163 m_extraLong = 0;
164 m_commandInt = 0;
165 m_id = theId;
166 m_commandString = wxEmptyString;
167 m_isCommandEvent = TRUE;
168 }
169
170 void wxCommandEvent::CopyObject(wxObject& obj_d) const
171 {
172 wxCommandEvent *obj = (wxCommandEvent *)&obj_d;
173
174 wxEvent::CopyObject(obj_d);
175
176 obj->m_clientData = m_clientData;
177 obj->m_clientObject = m_clientObject;
178 obj->m_extraLong = m_extraLong;
179 obj->m_commandInt = m_commandInt;
180 obj->m_commandString = m_commandString;
181 }
182
183 /*
184 * Notify events
185 */
186
187 void wxNotifyEvent::CopyObject(wxObject& obj_d) const
188 {
189 wxNotifyEvent *obj = (wxNotifyEvent *)&obj_d;
190
191 wxEvent::CopyObject(obj_d);
192
193 if (!m_bAllow) obj->Veto();
194 }
195
196 /*
197 * Scroll events
198 */
199
200 wxScrollEvent::wxScrollEvent(wxEventType commandType,
201 int id,
202 int pos,
203 int orient)
204 : wxCommandEvent(commandType, id)
205 {
206 m_extraLong = orient;
207 m_commandInt = pos;
208 }
209
210 /*
211 * ScrollWin events
212 */
213
214 wxScrollWinEvent::wxScrollWinEvent(wxEventType commandType,
215 int pos,
216 int orient)
217 {
218 m_eventType = commandType;
219 m_extraLong = orient;
220 m_commandInt = pos;
221 }
222
223 void wxScrollWinEvent::CopyObject(wxObject& obj_d) const
224 {
225 wxScrollWinEvent *obj = (wxScrollWinEvent*)&obj_d;
226
227 wxEvent::CopyObject(obj_d);
228
229 obj->m_extraLong = m_extraLong;
230 obj->m_commandInt = m_commandInt;
231 }
232
233 /*
234 * Mouse events
235 *
236 */
237
238 wxMouseEvent::wxMouseEvent(wxEventType commandType)
239 {
240 m_eventType = commandType;
241 m_metaDown = FALSE;
242 m_altDown = FALSE;
243 m_controlDown = FALSE;
244 m_shiftDown = FALSE;
245 m_leftDown = FALSE;
246 m_rightDown = FALSE;
247 m_middleDown = FALSE;
248 m_x = 0;
249 m_y = 0;
250 }
251
252 void wxMouseEvent::CopyObject(wxObject& obj_d) const
253 {
254 wxMouseEvent *obj = (wxMouseEvent *)&obj_d;
255
256 wxEvent::CopyObject(obj_d);
257
258 obj->m_metaDown = m_metaDown;
259 obj->m_altDown = m_altDown;
260 obj->m_controlDown = m_controlDown;
261 obj->m_shiftDown = m_shiftDown;
262 obj->m_leftDown = m_leftDown;
263 obj->m_rightDown = m_rightDown;
264 obj->m_middleDown = m_middleDown;
265 obj->m_x = m_x;
266 obj->m_y = m_y;
267 }
268
269 // True if was a button dclick event (1 = left, 2 = middle, 3 = right)
270 // or any button dclick event (but = -1)
271 bool wxMouseEvent::ButtonDClick(int but) const
272 {
273 switch (but)
274 {
275 case -1:
276 return (LeftDClick() || MiddleDClick() || RightDClick());
277 case 1:
278 return LeftDClick();
279 case 2:
280 return MiddleDClick();
281 case 3:
282 return RightDClick();
283 default:
284 wxFAIL_MSG(wxT("invalid parameter in wxMouseEvent::ButtonDClick"));
285 }
286
287 return FALSE;
288 }
289
290 // True if was a button down event (1 = left, 2 = middle, 3 = right)
291 // or any button down event (but = -1)
292 bool wxMouseEvent::ButtonDown(int but) const
293 {
294 switch (but)
295 {
296 case -1:
297 return (LeftDown() || MiddleDown() || RightDown());
298 case 1:
299 return LeftDown();
300 case 2:
301 return MiddleDown();
302 case 3:
303 return RightDown();
304 default:
305 wxFAIL_MSG(wxT("invalid parameter in wxMouseEvent::ButtonDown"));
306 }
307
308 return FALSE;
309 }
310
311 // True if was a button up event (1 = left, 2 = middle, 3 = right)
312 // or any button up event (but = -1)
313 bool wxMouseEvent::ButtonUp(int but) const
314 {
315 switch (but) {
316 case -1:
317 return (LeftUp() || MiddleUp() || RightUp());
318 case 1:
319 return LeftUp();
320 case 2:
321 return MiddleUp();
322 case 3:
323 return RightUp();
324 default:
325 wxFAIL_MSG(wxT("invalid parameter in wxMouseEvent::ButtonUp"));
326 }
327
328 return FALSE;
329 }
330
331 // True if the given button is currently changing state
332 bool wxMouseEvent::Button(int but) const
333 {
334 switch (but) {
335 case -1:
336 return (ButtonUp(-1) || ButtonDown(-1) || ButtonDClick(-1));
337 case 1:
338 return (LeftDown() || LeftUp() || LeftDClick());
339 case 2:
340 return (MiddleDown() || MiddleUp() || MiddleDClick());
341 case 3:
342 return (RightDown() || RightUp() || RightDClick());
343 default:
344 wxFAIL_MSG(wxT("invalid parameter in wxMouseEvent::Button"));
345 }
346
347 return FALSE;
348 }
349
350 bool wxMouseEvent::ButtonIsDown(int but) const
351 {
352 switch (but) {
353 case -1:
354 return (LeftIsDown() || MiddleIsDown() || RightIsDown());
355 case 1:
356 return LeftIsDown();
357 case 2:
358 return MiddleIsDown();
359 case 3:
360 return RightIsDown();
361 default:
362 wxFAIL_MSG(wxT("invalid parameter in wxMouseEvent::ButtonIsDown"));
363 }
364
365 return FALSE;
366 }
367
368 // Find the logical position of the event given the DC
369 wxPoint wxMouseEvent::GetLogicalPosition(const wxDC& dc) const
370 {
371 wxPoint pt(dc.DeviceToLogicalX(m_x), dc.DeviceToLogicalY(m_y));
372 return pt;
373 }
374
375
376 /*
377 * Keyboard events
378 *
379 */
380
381 wxKeyEvent::wxKeyEvent(wxEventType type)
382 {
383 m_eventType = type;
384 m_shiftDown = FALSE;
385 m_controlDown = FALSE;
386 m_metaDown = FALSE;
387 m_altDown = FALSE;
388 m_keyCode = 0;
389 m_scanCode = 0;
390 }
391
392 void wxKeyEvent::CopyObject(wxObject& obj_d) const
393 {
394 wxKeyEvent *obj = (wxKeyEvent *)&obj_d;
395 wxEvent::CopyObject(obj_d);
396
397 obj->m_x = m_x;
398 obj->m_y = m_y;
399 obj->m_keyCode = m_keyCode;
400
401 obj->m_shiftDown = m_shiftDown;
402 obj->m_controlDown = m_controlDown;
403 obj->m_metaDown = m_metaDown;
404 obj->m_altDown = m_altDown;
405 obj->m_keyCode = m_keyCode;
406 }
407
408
409 /*
410 * Misc events
411 */
412
413 void wxSizeEvent::CopyObject(wxObject& obj_d) const
414 {
415 wxSizeEvent *obj = (wxSizeEvent *)&obj_d;
416 wxEvent::CopyObject(obj_d);
417
418 obj->m_size = m_size;
419 }
420
421 void wxMoveEvent::CopyObject(wxObject& obj_d) const
422 {
423 wxMoveEvent *obj = (wxMoveEvent *)&obj_d;
424 wxEvent::CopyObject(obj_d);
425
426 obj->m_pos = m_pos;
427 }
428
429 void wxEraseEvent::CopyObject(wxObject& obj_d) const
430 {
431 wxEraseEvent *obj = (wxEraseEvent *)&obj_d;
432 wxEvent::CopyObject(obj_d);
433
434 obj->m_dc = m_dc;
435 }
436
437 void wxActivateEvent::CopyObject(wxObject& obj_d) const
438 {
439 wxActivateEvent *obj = (wxActivateEvent *)&obj_d;
440 wxEvent::CopyObject(obj_d);
441
442 obj->m_active = m_active;
443 }
444
445 void wxMenuEvent::CopyObject(wxObject& obj_d) const
446 {
447 wxMenuEvent *obj = (wxMenuEvent *)&obj_d;
448 wxEvent::CopyObject(obj_d);
449
450 obj->m_menuId = m_menuId;
451 }
452
453 void wxCloseEvent::CopyObject(wxObject& obj_d) const
454 {
455 wxCloseEvent *obj = (wxCloseEvent *)&obj_d;
456 wxEvent::CopyObject(obj_d);
457
458 obj->m_loggingOff = m_loggingOff;
459 obj->m_veto = m_veto;
460 #if WXWIN_COMPATIBILITY
461 obj->m_force = m_force;
462 #endif
463 obj->m_canVeto = m_canVeto;
464 }
465
466 void wxShowEvent::CopyObject(wxObject& obj_d) const
467 {
468 wxShowEvent *obj = (wxShowEvent *)&obj_d;
469 wxEvent::CopyObject(obj_d);
470
471 obj->m_show = m_show;
472 }
473
474 void wxJoystickEvent::CopyObject(wxObject& obj_d) const
475 {
476 wxJoystickEvent *obj = (wxJoystickEvent *)&obj_d;
477 wxEvent::CopyObject(obj_d);
478
479 obj->m_pos = m_pos;
480 obj->m_zPosition = m_zPosition;
481 obj->m_buttonChange = m_buttonChange;
482 obj->m_buttonState = m_buttonState;
483 obj->m_joyStick = m_joyStick;
484 }
485
486 void wxDropFilesEvent::CopyObject(wxObject& obj_d) const
487 {
488 wxDropFilesEvent *obj = (wxDropFilesEvent *)&obj_d;
489 wxEvent::CopyObject(obj_d);
490
491 obj->m_noFiles = m_noFiles;
492 obj->m_pos = m_pos;
493 // TODO: Problem with obj->m_files. It should be deallocated by the
494 // destructor of the event.
495 }
496
497 void wxUpdateUIEvent::CopyObject(wxObject &obj_d) const
498 {
499 wxUpdateUIEvent *obj = (wxUpdateUIEvent *)&obj_d;
500 wxEvent::CopyObject(obj_d);
501
502 obj->m_checked = m_checked;
503 obj->m_enabled = m_enabled;
504 obj->m_text = m_text;
505 obj->m_setText = m_setText;
506 obj->m_setChecked = m_setChecked;
507 obj->m_setEnabled = m_setEnabled;
508 }
509
510 void wxPaletteChangedEvent::CopyObject(wxObject &obj_d) const
511 {
512 wxPaletteChangedEvent *obj = (wxPaletteChangedEvent *)&obj_d;
513 wxEvent::CopyObject(obj_d);
514
515 obj->m_changedWindow = m_changedWindow;
516 }
517
518 void wxQueryNewPaletteEvent::CopyObject(wxObject& obj_d) const
519 {
520 wxQueryNewPaletteEvent *obj = (wxQueryNewPaletteEvent *)&obj_d;
521 wxEvent::CopyObject(obj_d);
522
523 obj->m_paletteRealized = m_paletteRealized;
524 }
525
526 wxWindowCreateEvent::wxWindowCreateEvent(wxWindow *win)
527 {
528 SetEventType(wxEVT_CREATE);
529 SetEventObject(win);
530 }
531
532 wxWindowDestroyEvent::wxWindowDestroyEvent(wxWindow *win)
533 {
534 SetEventType(wxEVT_DESTROY);
535 SetEventObject(win);
536 }
537
538 #endif // wxUSE_GUI
539
540 void wxIdleEvent::CopyObject(wxObject& obj_d) const
541 {
542 wxIdleEvent *obj = (wxIdleEvent *)&obj_d;
543 wxEvent::CopyObject(obj_d);
544
545 obj->m_requestMore = m_requestMore;
546 }
547
548 /*
549 * Event handler
550 */
551
552 wxEvtHandler::wxEvtHandler()
553 {
554 m_nextHandler = (wxEvtHandler *) NULL;
555 m_previousHandler = (wxEvtHandler *) NULL;
556 m_enabled = TRUE;
557 m_dynamicEvents = (wxList *) NULL;
558 m_isWindow = FALSE;
559 m_pendingEvents = (wxList *) NULL;
560 #if wxUSE_THREADS
561 # if !defined(__VISAGECPP__)
562 m_eventsLocker = new wxCriticalSection;
563 # endif
564 #endif
565 }
566
567 wxEvtHandler::~wxEvtHandler()
568 {
569 // Takes itself out of the list of handlers
570 if (m_previousHandler)
571 m_previousHandler->m_nextHandler = m_nextHandler;
572
573 if (m_nextHandler)
574 m_nextHandler->m_previousHandler = m_previousHandler;
575
576 if (m_dynamicEvents)
577 {
578 wxNode *node = m_dynamicEvents->First();
579 while (node)
580 {
581 wxEventTableEntry *entry = (wxEventTableEntry*)node->Data();
582 if (entry->m_callbackUserData) delete entry->m_callbackUserData;
583 delete entry;
584 node = node->Next();
585 }
586 delete m_dynamicEvents;
587 };
588
589 delete m_pendingEvents;
590
591 #if wxUSE_THREADS
592 # if !defined(__VISAGECPP__)
593 delete m_eventsLocker;
594 # endif
595 #endif
596 }
597
598 #if wxUSE_THREADS
599
600 bool wxEvtHandler::ProcessThreadEvent(wxEvent& event)
601 {
602 // check that we are really in a child thread
603 wxASSERT_MSG( !wxThread::IsMain(),
604 wxT("use ProcessEvent() in main thread") );
605
606 AddPendingEvent(event);
607
608 return TRUE;
609 }
610
611 #endif // wxUSE_THREADS
612
613 void wxEvtHandler::AddPendingEvent(wxEvent& event)
614 {
615 // 1) Add event to list of pending events of this event handler
616
617 #if defined(__VISAGECPP__)
618 wxENTER_CRIT_SECT( m_eventsLocker);
619 #else
620 wxENTER_CRIT_SECT( *m_eventsLocker);
621 #endif
622
623 if ( !m_pendingEvents )
624 m_pendingEvents = new wxList;
625
626 wxEvent *event2 = (wxEvent *)event.Clone();
627
628 m_pendingEvents->Append(event2);
629
630 #if defined(__VISAGECPP__)
631 wxLEAVE_CRIT_SECT( m_eventsLocker);
632 #else
633 wxLEAVE_CRIT_SECT( *m_eventsLocker);
634 #endif
635
636 // 2) Add this event handler to list of event handlers that
637 // have pending events.
638
639 wxENTER_CRIT_SECT(*wxPendingEventsLocker);
640
641 if ( !wxPendingEvents )
642 wxPendingEvents = new wxList;
643 wxPendingEvents->Append(this);
644
645 wxLEAVE_CRIT_SECT(*wxPendingEventsLocker);
646
647 // 3) Inform the system that new pending events are somwehere,
648 // and that these should be processed in idle time.
649 wxWakeUpIdle();
650 }
651
652 void wxEvtHandler::ProcessPendingEvents()
653 {
654 #if defined(__VISAGECPP__)
655 wxENTER_CRIT_SECT( m_eventsLocker);
656 #else
657 wxENTER_CRIT_SECT( *m_eventsLocker);
658 #endif
659
660 wxNode *node = m_pendingEvents->First();
661 while ( node )
662 {
663 wxEvent *event = (wxEvent *)node->Data();
664 delete node;
665
666 // In ProcessEvent, new events might get added and
667 // we can safely leave the crtical section here.
668 #if defined(__VISAGECPP__)
669 wxLEAVE_CRIT_SECT( m_eventsLocker);
670 #else
671 wxLEAVE_CRIT_SECT( *m_eventsLocker);
672 #endif
673 ProcessEvent(*event);
674 delete event;
675 #if defined(__VISAGECPP__)
676 wxENTER_CRIT_SECT( m_eventsLocker);
677 #else
678 wxENTER_CRIT_SECT( *m_eventsLocker);
679 #endif
680
681 node = m_pendingEvents->First();
682 }
683
684 #if defined(__VISAGECPP__)
685 wxLEAVE_CRIT_SECT( m_eventsLocker);
686 #else
687 wxLEAVE_CRIT_SECT( *m_eventsLocker);
688 #endif
689 }
690
691 /*
692 * Event table stuff
693 */
694
695 bool wxEvtHandler::ProcessEvent(wxEvent& event)
696 {
697 #if wxUSE_GUI
698 // check that our flag corresponds to reality
699 wxASSERT( m_isWindow == IsKindOf(CLASSINFO(wxWindow)) );
700 #endif // wxUSE_GUI
701
702 // An event handler can be enabled or disabled
703 if ( GetEvtHandlerEnabled() )
704 {
705
706 #if 0
707 /*
708 What is this? When using GUI threads, a non main
709 threads can send an event and process it itself.
710 This breaks GTK's GUI threads, so please explain.
711 */
712
713 // Check whether we are in a child thread.
714 if ( !wxThread::IsMain() )
715 return ProcessThreadEvent(event);
716 #endif
717
718 // Handle per-instance dynamic event tables first
719 if ( m_dynamicEvents && SearchDynamicEventTable(event) )
720 return TRUE;
721
722 // Then static per-class event tables
723 const wxEventTable *table = GetEventTable();
724
725 #if wxUSE_GUI && wxUSE_VALIDATORS
726 // Try the associated validator first, if this is a window.
727 // Problem: if the event handler of the window has been replaced,
728 // this wxEvtHandler may no longer be a window.
729 // Therefore validators won't be processed if the handler
730 // has been replaced with SetEventHandler.
731 // THIS CAN BE CURED if PushEventHandler is used instead of
732 // SetEventHandler, and then processing will be passed down the
733 // chain of event handlers.
734 if (m_isWindow)
735 {
736 wxWindow *win = (wxWindow *)this;
737
738 // Can only use the validator of the window which
739 // is receiving the event
740 if ( win == event.GetEventObject() )
741 {
742 wxValidator *validator = win->GetValidator();
743 if ( validator && validator->ProcessEvent(event) )
744 {
745 return TRUE;
746 }
747 }
748 }
749 #endif
750
751 // Search upwards through the inheritance hierarchy
752 while (table)
753 {
754 if ( SearchEventTable((wxEventTable&)*table, event) )
755 return TRUE;
756 table = table->baseTable;
757 }
758 }
759
760 // Try going down the event handler chain
761 if ( GetNextHandler() )
762 {
763 if ( GetNextHandler()->ProcessEvent(event) )
764 return TRUE;
765 }
766
767 #if wxUSE_GUI
768 // Carry on up the parent-child hierarchy,
769 // but only if event is a command event: it wouldn't
770 // make sense for a parent to receive a child's size event, for example
771 if ( m_isWindow && event.IsCommandEvent() )
772 {
773 wxWindow *win = (wxWindow *)this;
774 wxWindow *parent = win->GetParent();
775 if (parent && !parent->IsBeingDeleted())
776 return parent->GetEventHandler()->ProcessEvent(event);
777 }
778 #endif // wxUSE_GUI
779
780 // Last try - application object.
781 if ( wxTheApp && (this != wxTheApp) )
782 {
783 // Special case: don't pass wxEVT_IDLE to wxApp, since it'll always
784 // swallow it. wxEVT_IDLE is sent explicitly to wxApp so it will be
785 // processed appropriately via SearchEventTable.
786 if ( event.GetEventType() != wxEVT_IDLE )
787 {
788 if ( wxTheApp->ProcessEvent(event) )
789 return TRUE;
790 }
791 }
792
793 return FALSE;
794 }
795
796 bool wxEvtHandler::SearchEventTable(wxEventTable& table, wxEvent& event)
797 {
798 wxEventType eventType = event.GetEventType();
799 int eventId = event.GetId();
800
801 // BC++ doesn't like testing for m_fn without != 0
802 for ( int i = 0; table.entries[i].m_fn != 0; i++ )
803 {
804 // the line using reference exposes a bug in gcc: although it _seems_
805 // to work, it leads to weird crashes later on during program
806 // execution
807 #ifdef __GNUG__
808 wxEventTableEntry entry = table.entries[i];
809 #else
810 const wxEventTableEntry& entry = table.entries[i];
811 #endif
812
813 // match only if the event type is the same and the id is either -1 in
814 // the event table (meaning "any") or the event id matches the id
815 // specified in the event table either exactly or by falling into
816 // range between first and last
817 if ( eventType == entry.m_eventType )
818 {
819 int tableId1 = entry.m_id,
820 tableId2 = entry.m_lastId;
821
822 if ( (tableId1 == -1) ||
823 (tableId2 == -1 && eventId == tableId1) ||
824 (tableId2 != -1 &&
825 (eventId >= tableId1 && eventId <= tableId2)) )
826 {
827 event.Skip(FALSE);
828 event.m_callbackUserData = entry.m_callbackUserData;
829
830 (this->*((wxEventFunction) (entry.m_fn)))(event);
831
832 return !event.GetSkipped();
833 }
834 }
835 }
836
837 return FALSE;
838 }
839
840 void wxEvtHandler::Connect( int id, int lastId,
841 wxEventType eventType,
842 wxObjectEventFunction func,
843 wxObject *userData )
844 {
845 wxEventTableEntry *entry = new wxEventTableEntry;
846 entry->m_id = id;
847 entry->m_lastId = lastId;
848 entry->m_eventType = eventType;
849 entry->m_fn = func;
850 entry->m_callbackUserData = userData;
851
852 if (!m_dynamicEvents)
853 m_dynamicEvents = new wxList;
854
855 m_dynamicEvents->Append( (wxObject*) entry );
856 }
857
858 bool wxEvtHandler::Disconnect( int id, int lastId, wxEventType eventType,
859 wxObjectEventFunction func,
860 wxObject *userData )
861 {
862 if (!m_dynamicEvents)
863 return FALSE;
864
865 wxNode *node = m_dynamicEvents->First();
866 while (node)
867 {
868 wxEventTableEntry *entry = (wxEventTableEntry*)node->Data();
869 if ((entry->m_id == id) &&
870 ((entry->m_lastId == lastId) || (lastId == -1)) &&
871 ((entry->m_eventType == eventType) || (eventType == wxEVT_NULL)) &&
872 ((entry->m_fn == func) || (func == (wxObjectEventFunction)NULL)) &&
873 ((entry->m_callbackUserData == userData) || (userData == (wxObject*)NULL)))
874 {
875 if (entry->m_callbackUserData) delete entry->m_callbackUserData;
876 m_dynamicEvents->DeleteNode( node );
877 delete entry;
878 return TRUE;
879 }
880 node = node->Next();
881 }
882 return FALSE;
883 }
884
885 bool wxEvtHandler::SearchDynamicEventTable( wxEvent& event )
886 {
887 wxCHECK_MSG( m_dynamicEvents, FALSE,
888 wxT("caller should check that we have dynamic events") );
889
890 int commandId = event.GetId();
891
892 wxNode *node = m_dynamicEvents->First();
893 while (node)
894 {
895 wxEventTableEntry *entry = (wxEventTableEntry*)node->Data();
896
897 if (entry->m_fn)
898 {
899 // Match, if event spec says any id will do (id == -1)
900 if ( (event.GetEventType() == entry->m_eventType) &&
901 (entry->m_id == -1 ||
902 (entry->m_lastId == -1 && commandId == entry->m_id) ||
903 (entry->m_lastId != -1 &&
904 (commandId >= entry->m_id && commandId <= entry->m_lastId))) )
905 {
906 event.Skip(FALSE);
907 event.m_callbackUserData = entry->m_callbackUserData;
908
909 (this->*((wxEventFunction) (entry->m_fn)))(event);
910
911 if (event.GetSkipped())
912 return FALSE;
913 else
914 return TRUE;
915 }
916 }
917 node = node->Next();
918 }
919 return FALSE;
920 };
921
922 #if WXWIN_COMPATIBILITY
923 bool wxEvtHandler::OnClose()
924 {
925 if (GetNextHandler())
926 return GetNextHandler()->OnClose();
927 else
928 return FALSE;
929 }
930 #endif // WXWIN_COMPATIBILITY
931
932 #if wxUSE_GUI
933
934 // Find a window with the focus, that is also a descendant of the given window.
935 // This is used to determine the window to initially send commands to.
936 wxWindow* wxFindFocusDescendant(wxWindow* ancestor)
937 {
938 // Process events starting with the window with the focus, if any.
939 wxWindow* focusWin = wxWindow::FindFocus();
940 wxWindow* win = focusWin;
941
942 // Check if this is a descendant of this frame.
943 // If not, win will be set to NULL.
944 while (win)
945 {
946 if (win == ancestor)
947 break;
948 else
949 win = win->GetParent();
950 }
951 if (win == (wxWindow*) NULL)
952 focusWin = (wxWindow*) NULL;
953
954 return focusWin;
955 }
956
957 #endif // wxUSE_GUI