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"
25 #include "wx/control.h"
32 #include "wx/validate.h"
34 #if !USE_SHARED_LIBRARY
35 IMPLEMENT_DYNAMIC_CLASS(wxEvtHandler
, wxObject
)
36 IMPLEMENT_ABSTRACT_CLASS(wxEvent
, wxObject
)
37 IMPLEMENT_DYNAMIC_CLASS(wxCommandEvent
, wxEvent
)
38 IMPLEMENT_DYNAMIC_CLASS(wxNotifyEvent
, wxCommandEvent
)
39 IMPLEMENT_DYNAMIC_CLASS(wxScrollEvent
, wxCommandEvent
)
40 IMPLEMENT_DYNAMIC_CLASS(wxScrollWinEvent
, wxEvent
)
41 IMPLEMENT_DYNAMIC_CLASS(wxMouseEvent
, wxEvent
)
42 IMPLEMENT_DYNAMIC_CLASS(wxKeyEvent
, wxEvent
)
43 IMPLEMENT_DYNAMIC_CLASS(wxSizeEvent
, wxEvent
)
44 IMPLEMENT_DYNAMIC_CLASS(wxPaintEvent
, wxEvent
)
45 IMPLEMENT_DYNAMIC_CLASS(wxEraseEvent
, wxEvent
)
46 IMPLEMENT_DYNAMIC_CLASS(wxMoveEvent
, wxEvent
)
47 IMPLEMENT_DYNAMIC_CLASS(wxFocusEvent
, wxEvent
)
48 IMPLEMENT_DYNAMIC_CLASS(wxCloseEvent
, wxEvent
)
49 IMPLEMENT_DYNAMIC_CLASS(wxShowEvent
, wxEvent
)
50 IMPLEMENT_DYNAMIC_CLASS(wxMaximizeEvent
, wxEvent
)
51 IMPLEMENT_DYNAMIC_CLASS(wxIconizeEvent
, wxEvent
)
52 IMPLEMENT_DYNAMIC_CLASS(wxMenuEvent
, wxEvent
)
53 IMPLEMENT_DYNAMIC_CLASS(wxJoystickEvent
, wxEvent
)
54 IMPLEMENT_DYNAMIC_CLASS(wxDropFilesEvent
, wxEvent
)
55 IMPLEMENT_DYNAMIC_CLASS(wxActivateEvent
, wxEvent
)
56 IMPLEMENT_DYNAMIC_CLASS(wxInitDialogEvent
, wxEvent
)
57 IMPLEMENT_DYNAMIC_CLASS(wxSysColourChangedEvent
, wxEvent
)
58 IMPLEMENT_DYNAMIC_CLASS(wxIdleEvent
, wxEvent
)
59 IMPLEMENT_DYNAMIC_CLASS(wxUpdateUIEvent
, wxCommandEvent
)
60 IMPLEMENT_DYNAMIC_CLASS(wxNavigationKeyEvent
, wxCommandEvent
)
61 IMPLEMENT_DYNAMIC_CLASS(wxPaletteChangedEvent
, wxEvent
)
62 IMPLEMENT_DYNAMIC_CLASS(wxQueryNewPaletteEvent
, wxEvent
)
63 IMPLEMENT_DYNAMIC_CLASS(wxWindowCreateEvent
, wxEvent
)
64 IMPLEMENT_DYNAMIC_CLASS(wxWindowDestroyEvent
, wxEvent
)
66 const wxEventTable
*wxEvtHandler::GetEventTable() const
67 { return &wxEvtHandler::sm_eventTable
; }
69 const wxEventTable
wxEvtHandler::sm_eventTable
=
70 { (const wxEventTable
*)NULL
, &wxEvtHandler::sm_eventTableEntries
[0] };
72 const wxEventTableEntry
wxEvtHandler::sm_eventTableEntries
[] =
73 { { 0, 0, 0, (wxObjectEventFunction
) NULL
, (wxObject
*) NULL
} };
75 #endif // !USE_SHARED_LIBRARY
78 /* To put pending event handlers */
79 extern wxList
*wxPendingEvents
;
80 extern wxCriticalSection
*wxPendingEventsLocker
;
84 * General wxWindows events, covering
85 * all interesting things that might happen (button clicking, resizing,
86 * setting text in widgets, etc.).
88 * For each completely new event type, derive a new event class.
92 wxEvent::wxEvent(int theId
)
94 m_eventType
= wxEVT_NULL
;
95 m_eventObject
= (wxObject
*) NULL
;
99 m_callbackUserData
= (wxObject
*) NULL
;
100 m_isCommandEvent
= FALSE
;
103 void wxEvent::CopyObject(wxObject
& object_dest
) const
105 wxEvent
*obj
= (wxEvent
*)&object_dest
;
106 wxObject::CopyObject(object_dest
);
108 obj
->m_eventType
= m_eventType
;
109 obj
->m_eventObject
= m_eventObject
;
110 obj
->m_timeStamp
= m_timeStamp
;
112 obj
->m_skipped
= m_skipped
;
113 obj
->m_callbackUserData
= m_callbackUserData
;
114 obj
->m_isCommandEvent
= m_isCommandEvent
;
122 wxCommandEvent::wxCommandEvent(wxEventType commandType
, int theId
)
124 m_eventType
= commandType
;
125 m_clientData
= (char *) NULL
;
126 m_clientObject
= (wxClientData
*) NULL
;
130 m_commandString
= wxEmptyString
;
131 m_isCommandEvent
= TRUE
;
134 void wxCommandEvent::CopyObject(wxObject
& obj_d
) const
136 wxCommandEvent
*obj
= (wxCommandEvent
*)&obj_d
;
138 wxEvent::CopyObject(obj_d
);
140 obj
->m_clientData
= m_clientData
;
141 obj
->m_clientObject
= m_clientObject
;
142 obj
->m_extraLong
= m_extraLong
;
143 obj
->m_commandInt
= m_commandInt
;
150 wxScrollEvent::wxScrollEvent(wxEventType commandType
,
154 : wxCommandEvent(commandType
, id
)
156 m_extraLong
= orient
;
164 wxScrollWinEvent::wxScrollWinEvent(wxEventType commandType
,
168 m_eventType
= commandType
;
169 m_extraLong
= orient
;
173 void wxScrollWinEvent::CopyObject(wxObject
& obj_d
) const
175 wxScrollWinEvent
*obj
= (wxScrollWinEvent
*)&obj_d
;
177 wxEvent::CopyObject(obj_d
);
179 obj
->m_extraLong
= m_extraLong
;
180 obj
->m_commandInt
= m_commandInt
;
188 wxMouseEvent::wxMouseEvent(wxEventType commandType
)
190 m_eventType
= commandType
;
193 m_controlDown
= FALSE
;
197 m_middleDown
= FALSE
;
202 void wxMouseEvent::CopyObject(wxObject
& obj_d
) const
204 wxMouseEvent
*obj
= (wxMouseEvent
*)&obj_d
;
206 wxEvent::CopyObject(obj_d
);
208 obj
->m_metaDown
= m_metaDown
;
209 obj
->m_altDown
= m_altDown
;
210 obj
->m_controlDown
= m_controlDown
;
211 obj
->m_shiftDown
= m_shiftDown
;
212 obj
->m_leftDown
= m_leftDown
;
213 obj
->m_rightDown
= m_rightDown
;
214 obj
->m_middleDown
= m_middleDown
;
219 // True if was a button dclick event (1 = left, 2 = middle, 3 = right)
220 // or any button dclick event (but = -1)
221 bool wxMouseEvent::ButtonDClick(int but
) const
226 return (LeftDClick() || MiddleDClick() || RightDClick());
230 return MiddleDClick();
232 return RightDClick();
234 wxFAIL_MSG(_T("invalid parameter in wxMouseEvent::ButtonDClick"));
240 // True if was a button down event (1 = left, 2 = middle, 3 = right)
241 // or any button down event (but = -1)
242 bool wxMouseEvent::ButtonDown(int but
) const
247 return (LeftDown() || MiddleDown() || RightDown());
255 wxFAIL_MSG(_T("invalid parameter in wxMouseEvent::ButtonDown"));
261 // True if was a button up event (1 = left, 2 = middle, 3 = right)
262 // or any button up event (but = -1)
263 bool wxMouseEvent::ButtonUp(int but
) const
267 return (LeftUp() || MiddleUp() || RightUp());
275 wxFAIL_MSG(_T("invalid parameter in wxMouseEvent::ButtonUp"));
281 // True if the given button is currently changing state
282 bool wxMouseEvent::Button(int but
) const
286 return (ButtonUp(-1) || ButtonDown(-1) || ButtonDClick(-1)) ;
288 return (LeftDown() || LeftUp() || LeftDClick());
290 return (MiddleDown() || MiddleUp() || MiddleDClick());
292 return (RightDown() || RightUp() || RightDClick());
294 wxFAIL_MSG(_T("invalid parameter in wxMouseEvent::Button"));
300 bool wxMouseEvent::ButtonIsDown(int but
) const
304 return (LeftIsDown() || MiddleIsDown() || RightIsDown());
308 return MiddleIsDown();
310 return RightIsDown();
312 wxFAIL_MSG(_T("invalid parameter in wxMouseEvent::ButtonIsDown"));
318 // Find the logical position of the event given the DC
319 wxPoint
wxMouseEvent::GetLogicalPosition(const wxDC
& dc
) const
321 wxPoint
pt(dc
.DeviceToLogicalX(m_x
), dc
.DeviceToLogicalY(m_y
));
331 wxKeyEvent::wxKeyEvent(wxEventType type
)
335 m_controlDown
= FALSE
;
342 void wxKeyEvent::CopyObject(wxObject
& obj_d
) const
344 wxKeyEvent
*obj
= (wxKeyEvent
*)&obj_d
;
345 wxEvent::CopyObject(obj_d
);
347 obj
->m_shiftDown
= m_shiftDown
;
348 obj
->m_controlDown
= m_controlDown
;
349 obj
->m_metaDown
= m_metaDown
;
350 obj
->m_altDown
= m_altDown
;
351 obj
->m_keyCode
= m_keyCode
;
359 void wxSizeEvent::CopyObject(wxObject
& obj_d
) const
361 wxSizeEvent
*obj
= (wxSizeEvent
*)&obj_d
;
362 wxEvent::CopyObject(obj_d
);
364 obj
->m_size
= m_size
;
367 void wxMoveEvent::CopyObject(wxObject
& obj_d
) const
369 wxMoveEvent
*obj
= (wxMoveEvent
*)&obj_d
;
370 wxEvent::CopyObject(obj_d
);
375 void wxEraseEvent::CopyObject(wxObject
& obj_d
) const
377 wxEraseEvent
*obj
= (wxEraseEvent
*)&obj_d
;
378 wxEvent::CopyObject(obj_d
);
383 void wxActivateEvent::CopyObject(wxObject
& obj_d
) const
385 wxActivateEvent
*obj
= (wxActivateEvent
*)&obj_d
;
386 wxEvent::CopyObject(obj_d
);
388 obj
->m_active
= m_active
;
391 void wxMenuEvent::CopyObject(wxObject
& obj_d
) const
393 wxMenuEvent
*obj
= (wxMenuEvent
*)&obj_d
;
394 wxEvent::CopyObject(obj_d
);
396 obj
->m_menuId
= m_menuId
;
399 void wxCloseEvent::CopyObject(wxObject
& obj_d
) const
401 wxCloseEvent
*obj
= (wxCloseEvent
*)&obj_d
;
402 wxEvent::CopyObject(obj_d
);
404 obj
->m_loggingOff
= m_loggingOff
;
405 obj
->m_veto
= m_veto
;
406 #if WXWIN_COMPATIBILITY
407 obj
->m_force
= m_force
;
409 obj
->m_canVeto
= m_canVeto
;
412 void wxShowEvent::CopyObject(wxObject
& obj_d
) const
414 wxShowEvent
*obj
= (wxShowEvent
*)&obj_d
;
415 wxEvent::CopyObject(obj_d
);
417 obj
->m_show
= m_show
;
420 void wxJoystickEvent::CopyObject(wxObject
& obj_d
) const
422 wxJoystickEvent
*obj
= (wxJoystickEvent
*)&obj_d
;
423 wxEvent::CopyObject(obj_d
);
426 obj
->m_zPosition
= m_zPosition
;
427 obj
->m_buttonChange
= m_buttonChange
;
428 obj
->m_buttonState
= m_buttonState
;
429 obj
->m_joyStick
= m_joyStick
;
432 void wxDropFilesEvent::CopyObject(wxObject
& obj_d
) const
434 wxDropFilesEvent
*obj
= (wxDropFilesEvent
*)&obj_d
;
435 wxEvent::CopyObject(obj_d
);
437 obj
->m_noFiles
= m_noFiles
;
439 // TODO: Problem with obj->m_files. It should be deallocated by the
440 // destructor of the event.
443 void wxIdleEvent::CopyObject(wxObject
& obj_d
) const
445 wxIdleEvent
*obj
= (wxIdleEvent
*)&obj_d
;
446 wxEvent::CopyObject(obj_d
);
448 obj
->m_requestMore
= m_requestMore
;
451 void wxUpdateUIEvent::CopyObject(wxObject
&obj_d
) const
453 wxUpdateUIEvent
*obj
= (wxUpdateUIEvent
*)&obj_d
;
454 wxEvent::CopyObject(obj_d
);
456 obj
->m_checked
= m_checked
;
457 obj
->m_enabled
= m_enabled
;
458 obj
->m_text
= m_text
;
459 obj
->m_setText
= m_setText
;
460 obj
->m_setChecked
= m_setChecked
;
461 obj
->m_setEnabled
= m_setEnabled
;
464 void wxPaletteChangedEvent::CopyObject(wxObject
&obj_d
) const
466 wxPaletteChangedEvent
*obj
= (wxPaletteChangedEvent
*)&obj_d
;
467 wxEvent::CopyObject(obj_d
);
469 obj
->m_changedWindow
= m_changedWindow
;
472 void wxQueryNewPaletteEvent::CopyObject(wxObject
& obj_d
) const
474 wxQueryNewPaletteEvent
*obj
= (wxQueryNewPaletteEvent
*)&obj_d
;
475 wxEvent::CopyObject(obj_d
);
477 obj
->m_paletteRealized
= m_paletteRealized
;
480 wxWindowCreateEvent::wxWindowCreateEvent(wxWindow
*win
)
481 : wxEvent(wxEVT_CREATE
)
486 wxWindowDestroyEvent::wxWindowDestroyEvent(wxWindow
*win
)
487 : wxEvent(wxEVT_DESTROY
)
496 wxEvtHandler::wxEvtHandler()
498 m_nextHandler
= (wxEvtHandler
*) NULL
;
499 m_previousHandler
= (wxEvtHandler
*) NULL
;
501 m_dynamicEvents
= (wxList
*) NULL
;
504 m_eventsLocker
= new wxCriticalSection();
506 m_pendingEvents
= (wxList
*) NULL
;
509 wxEvtHandler::~wxEvtHandler()
511 // Takes itself out of the list of handlers
512 if (m_previousHandler
)
513 m_previousHandler
->m_nextHandler
= m_nextHandler
;
516 m_nextHandler
->m_previousHandler
= m_previousHandler
;
520 wxNode
*node
= m_dynamicEvents
->First();
523 wxEventTableEntry
*entry
= (wxEventTableEntry
*)node
->Data();
524 if (entry
->m_callbackUserData
) delete entry
->m_callbackUserData
;
528 delete m_dynamicEvents
;
533 delete m_pendingEvents
;
535 delete m_eventsLocker
;
542 extern bool g_isIdle
;
543 extern void wxapp_install_idle_handler();
546 bool wxEvtHandler::ProcessThreadEvent(wxEvent
& event
)
549 wxCriticalSectionLocker
locker(*m_eventsLocker
);
551 // check that we are really in a child thread
552 wxASSERT( !wxThread::IsMain() );
554 if (m_pendingEvents
== NULL
)
555 m_pendingEvents
= new wxList();
557 event_main
= (wxEvent
*)event
.Clone();
559 m_pendingEvents
->Append(event_main
);
561 wxPendingEventsLocker
->Enter();
562 wxPendingEvents
->Append(this);
563 wxPendingEventsLocker
->Leave();
565 // TODO: Wake up idle handler for the other platforms.
568 wxapp_install_idle_handler();
574 void wxEvtHandler::ProcessPendingEvents()
576 wxCriticalSectionLocker
locker(*m_eventsLocker
);
577 wxNode
*node
= m_pendingEvents
->First();
580 while (node
!= NULL
) {
581 event
= (wxEvent
*)node
->Data();
582 ProcessEvent(*event
);
584 node
= m_pendingEvents
->First();
593 bool wxEvtHandler::ProcessEvent(wxEvent
& event
)
595 /* check that our flag corresponds to reality */
596 wxASSERT( m_isWindow
== IsKindOf(CLASSINFO(wxWindow
)) );
598 /* An event handler can be enabled or disabled */
599 if ( GetEvtHandlerEnabled() )
602 /* Check whether we are in a child thread. */
603 if (!wxThread::IsMain())
604 return ProcessThreadEvent(event
);
606 /* Handle per-instance dynamic event tables first */
608 if ( m_dynamicEvents
&& SearchDynamicEventTable(event
) )
611 /* Then static per-class event tables */
613 const wxEventTable
*table
= GetEventTable();
616 // Try the associated validator first, if this is a window.
617 // Problem: if the event handler of the window has been replaced,
618 // this wxEvtHandler may no longer be a window.
619 // Therefore validators won't be processed if the handler
620 // has been replaced with SetEventHandler.
621 // THIS CAN BE CURED if PushEventHandler is used instead of
622 // SetEventHandler, and then processing will be passed down the
623 // chain of event handlers.
626 wxWindow
*win
= (wxWindow
*)this;
628 // Can only use the validator of the window which
629 // is receiving the event
630 if ( win
== event
.GetEventObject() )
632 wxValidator
*validator
= win
->GetValidator();
633 if ( validator
&& validator
->ProcessEvent(event
) )
641 // Search upwards through the inheritance hierarchy
644 if ( SearchEventTable((wxEventTable
&)*table
, event
) )
646 table
= table
->baseTable
;
650 // Try going down the event handler chain
651 if ( GetNextHandler() )
653 if ( GetNextHandler()->ProcessEvent(event
) )
657 // Carry on up the parent-child hierarchy,
658 // but only if event is a command event: it wouldn't
659 // make sense for a parent to receive a child's size event, for example
660 if ( m_isWindow
&& event
.IsCommandEvent() )
662 wxWindow
*win
= (wxWindow
*)this;
663 wxWindow
*parent
= win
->GetParent();
664 if (parent
&& !parent
->IsBeingDeleted())
665 return parent
->GetEventHandler()->ProcessEvent(event
);
668 // Last try - application object.
669 if ( wxTheApp
&& (this != wxTheApp
) )
671 // Special case: don't pass wxEVT_IDLE to wxApp, since it'll always
672 // swallow it. wxEVT_IDLE is sent explicitly to wxApp so it will be
673 // processed appropriately via SearchEventTable.
674 if ( event
.GetEventType() != wxEVT_IDLE
)
676 if ( wxTheApp
->ProcessEvent(event
) )
684 bool wxEvtHandler::SearchEventTable(wxEventTable
& table
, wxEvent
& event
)
687 int commandId
= event
.GetId();
689 // BC++ doesn't like while (table.entries[i].m_fn)
692 while (table
.entries
[i
].m_fn
!= 0)
694 while (table
.entries
[i
].m_fn
!= 0L)
697 if ((event
.GetEventType() == table
.entries
[i
].m_eventType
) &&
698 (table
.entries
[i
].m_id
== -1 || // Match, if event spec says any id will do (id == -1)
699 (table
.entries
[i
].m_lastId
== -1 && commandId
== table
.entries
[i
].m_id
) ||
700 (table
.entries
[i
].m_lastId
!= -1 &&
701 (commandId
>= table
.entries
[i
].m_id
&& commandId
<= table
.entries
[i
].m_lastId
))))
704 event
.m_callbackUserData
= table
.entries
[i
].m_callbackUserData
;
706 (this->*((wxEventFunction
) (table
.entries
[i
].m_fn
)))(event
);
708 if ( event
.GetSkipped() )
717 void wxEvtHandler::Connect( int id
, int lastId
,
718 wxEventType eventType
,
719 wxObjectEventFunction func
,
722 wxEventTableEntry
*entry
= new wxEventTableEntry
;
724 entry
->m_lastId
= lastId
;
725 entry
->m_eventType
= eventType
;
727 entry
->m_callbackUserData
= userData
;
729 if (!m_dynamicEvents
)
730 m_dynamicEvents
= new wxList
;
732 m_dynamicEvents
->Append( (wxObject
*) entry
);
735 bool wxEvtHandler::SearchDynamicEventTable( wxEvent
& event
)
737 wxCHECK_MSG( m_dynamicEvents
, FALSE
,
738 _T("caller should check that we have dynamic events") );
740 int commandId
= event
.GetId();
742 wxNode
*node
= m_dynamicEvents
->First();
745 wxEventTableEntry
*entry
= (wxEventTableEntry
*)node
->Data();
749 // Match, if event spec says any id will do (id == -1)
750 if ( (event
.GetEventType() == entry
->m_eventType
) &&
751 (entry
->m_id
== -1 ||
752 (entry
->m_lastId
== -1 && commandId
== entry
->m_id
) ||
753 (entry
->m_lastId
!= -1 &&
754 (commandId
>= entry
->m_id
&& commandId
<= entry
->m_lastId
))) )
757 event
.m_callbackUserData
= entry
->m_callbackUserData
;
759 (this->*((wxEventFunction
) (entry
->m_fn
)))(event
);
761 if (event
.GetSkipped())
772 #if WXWIN_COMPATIBILITY
773 bool wxEvtHandler::OnClose()
775 if (GetNextHandler())
776 return GetNextHandler()->OnClose();
780 #endif // WXWIN_COMPATIBILITY
782 // Find a window with the focus, that is also a descendant of the given window.
783 // This is used to determine the window to initially send commands to.
784 wxWindow
* wxFindFocusDescendant(wxWindow
* ancestor
)
786 // Process events starting with the window with the focus, if any.
787 wxWindow
* focusWin
= wxWindow::FindFocus();
788 wxWindow
* win
= focusWin
;
790 // Check if this is a descendant of this frame.
791 // If not, win will be set to NULL.
797 win
= win
->GetParent();
799 if (win
== (wxWindow
*) NULL
)
800 focusWin
= (wxWindow
*) NULL
;