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