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