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