1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Event classes
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "event.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
29 #include "wx/control.h"
38 #include "wx/validate.h"
41 #if !USE_SHARED_LIBRARY
42 IMPLEMENT_DYNAMIC_CLASS(wxEvtHandler
, wxObject
)
43 IMPLEMENT_ABSTRACT_CLASS(wxEvent
, wxObject
)
44 IMPLEMENT_DYNAMIC_CLASS(wxIdleEvent
, wxEvent
)
47 IMPLEMENT_DYNAMIC_CLASS(wxCommandEvent
, wxEvent
)
48 IMPLEMENT_DYNAMIC_CLASS(wxNotifyEvent
, wxCommandEvent
)
49 IMPLEMENT_DYNAMIC_CLASS(wxScrollEvent
, wxCommandEvent
)
50 IMPLEMENT_DYNAMIC_CLASS(wxScrollWinEvent
, wxEvent
)
51 IMPLEMENT_DYNAMIC_CLASS(wxMouseEvent
, wxEvent
)
52 IMPLEMENT_DYNAMIC_CLASS(wxKeyEvent
, wxEvent
)
53 IMPLEMENT_DYNAMIC_CLASS(wxSizeEvent
, wxEvent
)
54 IMPLEMENT_DYNAMIC_CLASS(wxPaintEvent
, wxEvent
)
55 IMPLEMENT_DYNAMIC_CLASS(wxEraseEvent
, wxEvent
)
56 IMPLEMENT_DYNAMIC_CLASS(wxMoveEvent
, wxEvent
)
57 IMPLEMENT_DYNAMIC_CLASS(wxFocusEvent
, wxEvent
)
58 IMPLEMENT_DYNAMIC_CLASS(wxCloseEvent
, wxEvent
)
59 IMPLEMENT_DYNAMIC_CLASS(wxShowEvent
, wxEvent
)
60 IMPLEMENT_DYNAMIC_CLASS(wxMaximizeEvent
, wxEvent
)
61 IMPLEMENT_DYNAMIC_CLASS(wxIconizeEvent
, wxEvent
)
62 IMPLEMENT_DYNAMIC_CLASS(wxMenuEvent
, wxEvent
)
63 IMPLEMENT_DYNAMIC_CLASS(wxJoystickEvent
, wxEvent
)
64 IMPLEMENT_DYNAMIC_CLASS(wxDropFilesEvent
, wxEvent
)
65 IMPLEMENT_DYNAMIC_CLASS(wxActivateEvent
, wxEvent
)
66 IMPLEMENT_DYNAMIC_CLASS(wxInitDialogEvent
, wxEvent
)
67 IMPLEMENT_DYNAMIC_CLASS(wxSysColourChangedEvent
, wxEvent
)
68 IMPLEMENT_DYNAMIC_CLASS(wxUpdateUIEvent
, wxCommandEvent
)
69 IMPLEMENT_DYNAMIC_CLASS(wxNavigationKeyEvent
, wxCommandEvent
)
70 IMPLEMENT_DYNAMIC_CLASS(wxPaletteChangedEvent
, wxEvent
)
71 IMPLEMENT_DYNAMIC_CLASS(wxQueryNewPaletteEvent
, wxEvent
)
72 IMPLEMENT_DYNAMIC_CLASS(wxWindowCreateEvent
, wxEvent
)
73 IMPLEMENT_DYNAMIC_CLASS(wxWindowDestroyEvent
, wxEvent
)
76 const wxEventTable
*wxEvtHandler::GetEventTable() const
77 { return &wxEvtHandler::sm_eventTable
; }
79 const wxEventTable
wxEvtHandler::sm_eventTable
=
80 { (const wxEventTable
*)NULL
, &wxEvtHandler::sm_eventTableEntries
[0] };
82 const wxEventTableEntry
wxEvtHandler::sm_eventTableEntries
[] =
83 { { 0, 0, 0, (wxObjectEventFunction
) NULL
, (wxObject
*) NULL
} };
85 #endif // !USE_SHARED_LIBRARY
88 /* To put pending event handlers */
89 extern wxList
*wxPendingEvents
;
90 extern wxCriticalSection
*wxPendingEventsLocker
;
94 * General wxWindows events, covering
95 * all interesting things that might happen (button clicking, resizing,
96 * setting text in widgets, etc.).
98 * For each completely new event type, derive a new event class.
102 wxEvent::wxEvent(int theId
)
104 m_eventType
= wxEVT_NULL
;
105 m_eventObject
= (wxObject
*) NULL
;
109 m_callbackUserData
= (wxObject
*) NULL
;
110 m_isCommandEvent
= FALSE
;
113 void wxEvent::CopyObject(wxObject
& object_dest
) const
115 wxEvent
*obj
= (wxEvent
*)&object_dest
;
116 wxObject::CopyObject(object_dest
);
118 obj
->m_eventType
= m_eventType
;
119 obj
->m_eventObject
= m_eventObject
;
120 obj
->m_timeStamp
= m_timeStamp
;
122 obj
->m_skipped
= m_skipped
;
123 obj
->m_callbackUserData
= m_callbackUserData
;
124 obj
->m_isCommandEvent
= m_isCommandEvent
;
134 wxCommandEvent::wxCommandEvent(wxEventType commandType
, int theId
)
136 m_eventType
= commandType
;
137 m_clientData
= (char *) NULL
;
138 m_clientObject
= (wxClientData
*) NULL
;
142 m_commandString
= wxEmptyString
;
143 m_isCommandEvent
= TRUE
;
146 void wxCommandEvent::CopyObject(wxObject
& obj_d
) const
148 wxCommandEvent
*obj
= (wxCommandEvent
*)&obj_d
;
150 wxEvent::CopyObject(obj_d
);
152 obj
->m_clientData
= m_clientData
;
153 obj
->m_clientObject
= m_clientObject
;
154 obj
->m_extraLong
= m_extraLong
;
155 obj
->m_commandInt
= m_commandInt
;
162 wxScrollEvent::wxScrollEvent(wxEventType commandType
,
166 : wxCommandEvent(commandType
, id
)
168 m_extraLong
= orient
;
176 wxScrollWinEvent::wxScrollWinEvent(wxEventType commandType
,
180 m_eventType
= commandType
;
181 m_extraLong
= orient
;
185 void wxScrollWinEvent::CopyObject(wxObject
& obj_d
) const
187 wxScrollWinEvent
*obj
= (wxScrollWinEvent
*)&obj_d
;
189 wxEvent::CopyObject(obj_d
);
191 obj
->m_extraLong
= m_extraLong
;
192 obj
->m_commandInt
= m_commandInt
;
200 wxMouseEvent::wxMouseEvent(wxEventType commandType
)
202 m_eventType
= commandType
;
205 m_controlDown
= FALSE
;
209 m_middleDown
= FALSE
;
214 void wxMouseEvent::CopyObject(wxObject
& obj_d
) const
216 wxMouseEvent
*obj
= (wxMouseEvent
*)&obj_d
;
218 wxEvent::CopyObject(obj_d
);
220 obj
->m_metaDown
= m_metaDown
;
221 obj
->m_altDown
= m_altDown
;
222 obj
->m_controlDown
= m_controlDown
;
223 obj
->m_shiftDown
= m_shiftDown
;
224 obj
->m_leftDown
= m_leftDown
;
225 obj
->m_rightDown
= m_rightDown
;
226 obj
->m_middleDown
= m_middleDown
;
231 // True if was a button dclick event (1 = left, 2 = middle, 3 = right)
232 // or any button dclick event (but = -1)
233 bool wxMouseEvent::ButtonDClick(int but
) const
238 return (LeftDClick() || MiddleDClick() || RightDClick());
242 return MiddleDClick();
244 return RightDClick();
246 wxFAIL_MSG(wxT("invalid parameter in wxMouseEvent::ButtonDClick"));
252 // True if was a button down event (1 = left, 2 = middle, 3 = right)
253 // or any button down event (but = -1)
254 bool wxMouseEvent::ButtonDown(int but
) const
259 return (LeftDown() || MiddleDown() || RightDown());
267 wxFAIL_MSG(wxT("invalid parameter in wxMouseEvent::ButtonDown"));
273 // True if was a button up event (1 = left, 2 = middle, 3 = right)
274 // or any button up event (but = -1)
275 bool wxMouseEvent::ButtonUp(int but
) const
279 return (LeftUp() || MiddleUp() || RightUp());
287 wxFAIL_MSG(wxT("invalid parameter in wxMouseEvent::ButtonUp"));
293 // True if the given button is currently changing state
294 bool wxMouseEvent::Button(int but
) const
298 return (ButtonUp(-1) || ButtonDown(-1) || ButtonDClick(-1)) ;
300 return (LeftDown() || LeftUp() || LeftDClick());
302 return (MiddleDown() || MiddleUp() || MiddleDClick());
304 return (RightDown() || RightUp() || RightDClick());
306 wxFAIL_MSG(wxT("invalid parameter in wxMouseEvent::Button"));
312 bool wxMouseEvent::ButtonIsDown(int but
) const
316 return (LeftIsDown() || MiddleIsDown() || RightIsDown());
320 return MiddleIsDown();
322 return RightIsDown();
324 wxFAIL_MSG(wxT("invalid parameter in wxMouseEvent::ButtonIsDown"));
330 // Find the logical position of the event given the DC
331 wxPoint
wxMouseEvent::GetLogicalPosition(const wxDC
& dc
) const
333 wxPoint
pt(dc
.DeviceToLogicalX(m_x
), dc
.DeviceToLogicalY(m_y
));
343 wxKeyEvent::wxKeyEvent(wxEventType type
)
347 m_controlDown
= FALSE
;
354 void wxKeyEvent::CopyObject(wxObject
& obj_d
) const
356 wxKeyEvent
*obj
= (wxKeyEvent
*)&obj_d
;
357 wxEvent::CopyObject(obj_d
);
359 obj
->m_shiftDown
= m_shiftDown
;
360 obj
->m_controlDown
= m_controlDown
;
361 obj
->m_metaDown
= m_metaDown
;
362 obj
->m_altDown
= m_altDown
;
363 obj
->m_keyCode
= m_keyCode
;
371 void wxSizeEvent::CopyObject(wxObject
& obj_d
) const
373 wxSizeEvent
*obj
= (wxSizeEvent
*)&obj_d
;
374 wxEvent::CopyObject(obj_d
);
376 obj
->m_size
= m_size
;
379 void wxMoveEvent::CopyObject(wxObject
& obj_d
) const
381 wxMoveEvent
*obj
= (wxMoveEvent
*)&obj_d
;
382 wxEvent::CopyObject(obj_d
);
387 void wxEraseEvent::CopyObject(wxObject
& obj_d
) const
389 wxEraseEvent
*obj
= (wxEraseEvent
*)&obj_d
;
390 wxEvent::CopyObject(obj_d
);
395 void wxActivateEvent::CopyObject(wxObject
& obj_d
) const
397 wxActivateEvent
*obj
= (wxActivateEvent
*)&obj_d
;
398 wxEvent::CopyObject(obj_d
);
400 obj
->m_active
= m_active
;
403 void wxMenuEvent::CopyObject(wxObject
& obj_d
) const
405 wxMenuEvent
*obj
= (wxMenuEvent
*)&obj_d
;
406 wxEvent::CopyObject(obj_d
);
408 obj
->m_menuId
= m_menuId
;
411 void wxCloseEvent::CopyObject(wxObject
& obj_d
) const
413 wxCloseEvent
*obj
= (wxCloseEvent
*)&obj_d
;
414 wxEvent::CopyObject(obj_d
);
416 obj
->m_loggingOff
= m_loggingOff
;
417 obj
->m_veto
= m_veto
;
418 #if WXWIN_COMPATIBILITY
419 obj
->m_force
= m_force
;
421 obj
->m_canVeto
= m_canVeto
;
424 void wxShowEvent::CopyObject(wxObject
& obj_d
) const
426 wxShowEvent
*obj
= (wxShowEvent
*)&obj_d
;
427 wxEvent::CopyObject(obj_d
);
429 obj
->m_show
= m_show
;
432 void wxJoystickEvent::CopyObject(wxObject
& obj_d
) const
434 wxJoystickEvent
*obj
= (wxJoystickEvent
*)&obj_d
;
435 wxEvent::CopyObject(obj_d
);
438 obj
->m_zPosition
= m_zPosition
;
439 obj
->m_buttonChange
= m_buttonChange
;
440 obj
->m_buttonState
= m_buttonState
;
441 obj
->m_joyStick
= m_joyStick
;
444 void wxDropFilesEvent::CopyObject(wxObject
& obj_d
) const
446 wxDropFilesEvent
*obj
= (wxDropFilesEvent
*)&obj_d
;
447 wxEvent::CopyObject(obj_d
);
449 obj
->m_noFiles
= m_noFiles
;
451 // TODO: Problem with obj->m_files. It should be deallocated by the
452 // destructor of the event.
455 void wxUpdateUIEvent::CopyObject(wxObject
&obj_d
) const
457 wxUpdateUIEvent
*obj
= (wxUpdateUIEvent
*)&obj_d
;
458 wxEvent::CopyObject(obj_d
);
460 obj
->m_checked
= m_checked
;
461 obj
->m_enabled
= m_enabled
;
462 obj
->m_text
= m_text
;
463 obj
->m_setText
= m_setText
;
464 obj
->m_setChecked
= m_setChecked
;
465 obj
->m_setEnabled
= m_setEnabled
;
468 void wxPaletteChangedEvent::CopyObject(wxObject
&obj_d
) const
470 wxPaletteChangedEvent
*obj
= (wxPaletteChangedEvent
*)&obj_d
;
471 wxEvent::CopyObject(obj_d
);
473 obj
->m_changedWindow
= m_changedWindow
;
476 void wxQueryNewPaletteEvent::CopyObject(wxObject
& obj_d
) const
478 wxQueryNewPaletteEvent
*obj
= (wxQueryNewPaletteEvent
*)&obj_d
;
479 wxEvent::CopyObject(obj_d
);
481 obj
->m_paletteRealized
= m_paletteRealized
;
484 wxWindowCreateEvent::wxWindowCreateEvent(wxWindow
*win
)
485 : wxEvent(wxEVT_CREATE
)
490 wxWindowDestroyEvent::wxWindowDestroyEvent(wxWindow
*win
)
491 : wxEvent(wxEVT_DESTROY
)
498 void wxIdleEvent::CopyObject(wxObject
& obj_d
) const
500 wxIdleEvent
*obj
= (wxIdleEvent
*)&obj_d
;
501 wxEvent::CopyObject(obj_d
);
503 obj
->m_requestMore
= m_requestMore
;
510 wxEvtHandler::wxEvtHandler()
512 m_nextHandler
= (wxEvtHandler
*) NULL
;
513 m_previousHandler
= (wxEvtHandler
*) NULL
;
515 m_dynamicEvents
= (wxList
*) NULL
;
518 m_eventsLocker
= new wxCriticalSection();
520 m_pendingEvents
= (wxList
*) NULL
;
523 wxEvtHandler::~wxEvtHandler()
525 // Takes itself out of the list of handlers
526 if (m_previousHandler
)
527 m_previousHandler
->m_nextHandler
= m_nextHandler
;
530 m_nextHandler
->m_previousHandler
= m_previousHandler
;
534 wxNode
*node
= m_dynamicEvents
->First();
537 wxEventTableEntry
*entry
= (wxEventTableEntry
*)node
->Data();
538 if (entry
->m_callbackUserData
) delete entry
->m_callbackUserData
;
542 delete m_dynamicEvents
;
547 delete m_pendingEvents
;
549 delete m_eventsLocker
;
556 extern bool g_isIdle
;
557 extern void wxapp_install_idle_handler();
560 bool wxEvtHandler::ProcessThreadEvent(wxEvent
& event
)
563 wxCriticalSectionLocker
locker(*m_eventsLocker
);
565 // check that we are really in a child thread
566 wxASSERT( !wxThread::IsMain() );
568 if (m_pendingEvents
== NULL
)
569 m_pendingEvents
= new wxList();
571 event_main
= (wxEvent
*)event
.Clone();
573 m_pendingEvents
->Append(event_main
);
575 wxPendingEventsLocker
->Enter();
576 wxPendingEvents
->Append(this);
577 wxPendingEventsLocker
->Leave();
579 // TODO: Wake up idle handler for the other platforms.
582 wxapp_install_idle_handler();
588 void wxEvtHandler::ProcessPendingEvents()
590 wxCriticalSectionLocker
locker(*m_eventsLocker
);
591 wxNode
*node
= m_pendingEvents
->First();
594 while (node
!= NULL
) {
595 event
= (wxEvent
*)node
->Data();
596 ProcessEvent(*event
);
598 node
= m_pendingEvents
->First();
607 bool wxEvtHandler::ProcessEvent(wxEvent
& event
)
610 /* check that our flag corresponds to reality */
611 wxASSERT( m_isWindow
== IsKindOf(CLASSINFO(wxWindow
)) );
614 /* An event handler can be enabled or disabled */
615 if ( GetEvtHandlerEnabled() )
618 /* Check whether we are in a child thread. */
619 if (!wxThread::IsMain())
620 return ProcessThreadEvent(event
);
622 /* Handle per-instance dynamic event tables first */
624 if ( m_dynamicEvents
&& SearchDynamicEventTable(event
) )
627 /* Then static per-class event tables */
629 const wxEventTable
*table
= GetEventTable();
631 #if wxUSE_GUI && wxUSE_VALIDATORS
632 // Try the associated validator first, if this is a window.
633 // Problem: if the event handler of the window has been replaced,
634 // this wxEvtHandler may no longer be a window.
635 // Therefore validators won't be processed if the handler
636 // has been replaced with SetEventHandler.
637 // THIS CAN BE CURED if PushEventHandler is used instead of
638 // SetEventHandler, and then processing will be passed down the
639 // chain of event handlers.
642 wxWindow
*win
= (wxWindow
*)this;
644 // Can only use the validator of the window which
645 // is receiving the event
646 if ( win
== event
.GetEventObject() )
648 wxValidator
*validator
= win
->GetValidator();
649 if ( validator
&& validator
->ProcessEvent(event
) )
657 // Search upwards through the inheritance hierarchy
660 if ( SearchEventTable((wxEventTable
&)*table
, event
) )
662 table
= table
->baseTable
;
666 // Try going down the event handler chain
667 if ( GetNextHandler() )
669 if ( GetNextHandler()->ProcessEvent(event
) )
674 // Carry on up the parent-child hierarchy,
675 // but only if event is a command event: it wouldn't
676 // make sense for a parent to receive a child's size event, for example
677 if ( m_isWindow
&& event
.IsCommandEvent() )
679 wxWindow
*win
= (wxWindow
*)this;
680 wxWindow
*parent
= win
->GetParent();
681 if (parent
&& !parent
->IsBeingDeleted())
682 return parent
->GetEventHandler()->ProcessEvent(event
);
686 // Last try - application object.
687 if ( wxTheApp
&& (this != wxTheApp
) )
689 // Special case: don't pass wxEVT_IDLE to wxApp, since it'll always
690 // swallow it. wxEVT_IDLE is sent explicitly to wxApp so it will be
691 // processed appropriately via SearchEventTable.
692 if ( event
.GetEventType() != wxEVT_IDLE
)
694 if ( wxTheApp
->ProcessEvent(event
) )
702 bool wxEvtHandler::SearchEventTable(wxEventTable
& table
, wxEvent
& event
)
705 int commandId
= event
.GetId();
707 // BC++ doesn't like while (table.entries[i].m_fn)
710 while (table
.entries
[i
].m_fn
!= 0)
712 while (table
.entries
[i
].m_fn
!= 0L)
715 if ((event
.GetEventType() == table
.entries
[i
].m_eventType
) &&
716 (table
.entries
[i
].m_id
== -1 || // Match, if event spec says any id will do (id == -1)
717 (table
.entries
[i
].m_lastId
== -1 && commandId
== table
.entries
[i
].m_id
) ||
718 (table
.entries
[i
].m_lastId
!= -1 &&
719 (commandId
>= table
.entries
[i
].m_id
&& commandId
<= table
.entries
[i
].m_lastId
))))
722 event
.m_callbackUserData
= table
.entries
[i
].m_callbackUserData
;
724 (this->*((wxEventFunction
) (table
.entries
[i
].m_fn
)))(event
);
726 if ( event
.GetSkipped() )
735 void wxEvtHandler::Connect( int id
, int lastId
,
736 wxEventType eventType
,
737 wxObjectEventFunction func
,
740 wxEventTableEntry
*entry
= new wxEventTableEntry
;
742 entry
->m_lastId
= lastId
;
743 entry
->m_eventType
= eventType
;
745 entry
->m_callbackUserData
= userData
;
747 if (!m_dynamicEvents
)
748 m_dynamicEvents
= new wxList
;
750 m_dynamicEvents
->Append( (wxObject
*) entry
);
753 bool wxEvtHandler::SearchDynamicEventTable( wxEvent
& event
)
755 wxCHECK_MSG( m_dynamicEvents
, FALSE
,
756 wxT("caller should check that we have dynamic events") );
758 int commandId
= event
.GetId();
760 wxNode
*node
= m_dynamicEvents
->First();
763 wxEventTableEntry
*entry
= (wxEventTableEntry
*)node
->Data();
767 // Match, if event spec says any id will do (id == -1)
768 if ( (event
.GetEventType() == entry
->m_eventType
) &&
769 (entry
->m_id
== -1 ||
770 (entry
->m_lastId
== -1 && commandId
== entry
->m_id
) ||
771 (entry
->m_lastId
!= -1 &&
772 (commandId
>= entry
->m_id
&& commandId
<= entry
->m_lastId
))) )
775 event
.m_callbackUserData
= entry
->m_callbackUserData
;
777 (this->*((wxEventFunction
) (entry
->m_fn
)))(event
);
779 if (event
.GetSkipped())
790 #if WXWIN_COMPATIBILITY
791 bool wxEvtHandler::OnClose()
793 if (GetNextHandler())
794 return GetNextHandler()->OnClose();
798 #endif // WXWIN_COMPATIBILITY
802 // Find a window with the focus, that is also a descendant of the given window.
803 // This is used to determine the window to initially send commands to.
804 wxWindow
* wxFindFocusDescendant(wxWindow
* ancestor
)
806 // Process events starting with the window with the focus, if any.
807 wxWindow
* focusWin
= wxWindow::FindFocus();
808 wxWindow
* win
= focusWin
;
810 // Check if this is a descendant of this frame.
811 // If not, win will be set to NULL.
817 win
= win
->GetParent();
819 if (win
== (wxWindow
*) NULL
)
820 focusWin
= (wxWindow
*) NULL
;