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(wxMouseEvent
, wxEvent
)
41 IMPLEMENT_DYNAMIC_CLASS(wxKeyEvent
, wxEvent
)
42 IMPLEMENT_DYNAMIC_CLASS(wxSizeEvent
, wxEvent
)
43 IMPLEMENT_DYNAMIC_CLASS(wxPaintEvent
, wxEvent
)
44 IMPLEMENT_DYNAMIC_CLASS(wxEraseEvent
, wxEvent
)
45 IMPLEMENT_DYNAMIC_CLASS(wxMoveEvent
, wxEvent
)
46 IMPLEMENT_DYNAMIC_CLASS(wxFocusEvent
, wxEvent
)
47 IMPLEMENT_DYNAMIC_CLASS(wxCloseEvent
, wxEvent
)
48 IMPLEMENT_DYNAMIC_CLASS(wxShowEvent
, wxEvent
)
49 IMPLEMENT_DYNAMIC_CLASS(wxMaximizeEvent
, wxEvent
)
50 IMPLEMENT_DYNAMIC_CLASS(wxIconizeEvent
, wxEvent
)
51 IMPLEMENT_DYNAMIC_CLASS(wxMenuEvent
, wxEvent
)
52 IMPLEMENT_DYNAMIC_CLASS(wxJoystickEvent
, wxEvent
)
53 IMPLEMENT_DYNAMIC_CLASS(wxDropFilesEvent
, wxEvent
)
54 IMPLEMENT_DYNAMIC_CLASS(wxActivateEvent
, wxEvent
)
55 IMPLEMENT_DYNAMIC_CLASS(wxInitDialogEvent
, wxEvent
)
56 IMPLEMENT_DYNAMIC_CLASS(wxSysColourChangedEvent
, wxEvent
)
57 IMPLEMENT_DYNAMIC_CLASS(wxIdleEvent
, wxEvent
)
58 IMPLEMENT_DYNAMIC_CLASS(wxUpdateUIEvent
, wxCommandEvent
)
59 IMPLEMENT_DYNAMIC_CLASS(wxNavigationKeyEvent
, wxCommandEvent
)
60 IMPLEMENT_DYNAMIC_CLASS(wxPaletteChangedEvent
, wxEvent
)
61 IMPLEMENT_DYNAMIC_CLASS(wxQueryNewPaletteEvent
, wxEvent
)
63 const wxEventTable
*wxEvtHandler::GetEventTable() const
64 { return &wxEvtHandler::sm_eventTable
; }
66 const wxEventTable
wxEvtHandler::sm_eventTable
=
67 { (const wxEventTable
*)NULL
, &wxEvtHandler::sm_eventTableEntries
[0] };
69 const wxEventTableEntry
wxEvtHandler::sm_eventTableEntries
[] =
70 { { 0, 0, 0, (wxObjectEventFunction
) NULL
, (wxObject
*) NULL
} };
72 #endif // !USE_SHARED_LIBRARY
75 /* To put pending event handlers */
76 extern wxList
*wxPendingEvents
;
77 extern wxCriticalSection
*wxPendingEventsLocker
;
81 * General wxWindows events, covering
82 * all interesting things that might happen (button clicking, resizing,
83 * setting text in widgets, etc.).
85 * For each completely new event type, derive a new event class.
89 wxEvent::wxEvent(int theId
)
91 m_eventType
= wxEVT_NULL
;
92 m_eventObject
= (wxObject
*) NULL
;
93 m_eventHandle
= (char *) NULL
;
97 m_callbackUserData
= (wxObject
*) NULL
;
98 m_isCommandEvent
= FALSE
;
101 void wxEvent::CopyObject(wxObject
& object_dest
) const
103 wxEvent
*obj
= (wxEvent
*)&object_dest
;
104 wxObject::CopyObject(object_dest
);
106 obj
->m_eventType
= m_eventType
;
107 obj
->m_eventObject
= m_eventObject
;
108 obj
->m_eventHandle
= m_eventHandle
;
109 obj
->m_timeStamp
= m_timeStamp
;
111 obj
->m_skipped
= m_skipped
;
112 obj
->m_callbackUserData
= m_callbackUserData
;
113 obj
->m_isCommandEvent
= m_isCommandEvent
;
121 wxCommandEvent::wxCommandEvent(wxEventType commandType
, int theId
)
123 m_eventType
= commandType
;
124 m_clientData
= (char *) NULL
;
125 m_clientObject
= (wxClientData
*) NULL
;
129 m_commandString
= (wxChar
*) NULL
;
130 m_isCommandEvent
= TRUE
;
133 void wxCommandEvent::CopyObject(wxObject
& obj_d
) const
135 wxCommandEvent
*obj
= (wxCommandEvent
*)&obj_d
;
137 wxEvent::CopyObject(obj_d
);
139 obj
->m_clientData
= m_clientData
;
140 obj
->m_clientObject
= m_clientObject
;
141 obj
->m_extraLong
= m_extraLong
;
142 obj
->m_commandInt
= m_commandInt
;
149 wxScrollEvent::wxScrollEvent(wxEventType commandType
,
153 : wxCommandEvent(commandType
, id
)
155 m_extraLong
= orient
;
164 wxMouseEvent::wxMouseEvent(wxEventType commandType
)
166 m_eventType
= commandType
;
169 m_controlDown
= FALSE
;
173 m_middleDown
= FALSE
;
178 void wxMouseEvent::CopyObject(wxObject
& obj_d
) const
180 wxMouseEvent
*obj
= (wxMouseEvent
*)&obj_d
;
182 wxEvent::CopyObject(obj_d
);
184 obj
->m_metaDown
= m_metaDown
;
185 obj
->m_altDown
= m_altDown
;
186 obj
->m_controlDown
= m_controlDown
;
187 obj
->m_shiftDown
= m_shiftDown
;
188 obj
->m_leftDown
= m_leftDown
;
189 obj
->m_rightDown
= m_rightDown
;
190 obj
->m_middleDown
= m_middleDown
;
195 // True if was a button dclick event (1 = left, 2 = middle, 3 = right)
196 // or any button dclick event (but = -1)
197 bool wxMouseEvent::ButtonDClick(int but
) const
202 return (LeftDClick() || MiddleDClick() || RightDClick());
206 return MiddleDClick();
208 return RightDClick();
210 wxFAIL_MSG(_T("invalid parameter in wxMouseEvent::ButtonDClick"));
216 // True if was a button down event (1 = left, 2 = middle, 3 = right)
217 // or any button down event (but = -1)
218 bool wxMouseEvent::ButtonDown(int but
) const
223 return (LeftDown() || MiddleDown() || RightDown());
231 wxFAIL_MSG(_T("invalid parameter in wxMouseEvent::ButtonDown"));
237 // True if was a button up event (1 = left, 2 = middle, 3 = right)
238 // or any button up event (but = -1)
239 bool wxMouseEvent::ButtonUp(int but
) const
243 return (LeftUp() || MiddleUp() || RightUp());
251 wxFAIL_MSG(_T("invalid parameter in wxMouseEvent::ButtonUp"));
257 // True if the given button is currently changing state
258 bool wxMouseEvent::Button(int but
) const
262 return (ButtonUp(-1) || ButtonDown(-1) || ButtonDClick(-1)) ;
264 return (LeftDown() || LeftUp() || LeftDClick());
266 return (MiddleDown() || MiddleUp() || MiddleDClick());
268 return (RightDown() || RightUp() || RightDClick());
270 wxFAIL_MSG(_T("invalid parameter in wxMouseEvent::Button"));
276 bool wxMouseEvent::ButtonIsDown(int but
) const
280 return (LeftIsDown() || MiddleIsDown() || RightIsDown());
284 return MiddleIsDown();
286 return RightIsDown();
288 wxFAIL_MSG(_T("invalid parameter in wxMouseEvent::ButtonIsDown"));
294 // Find the logical position of the event given the DC
295 wxPoint
wxMouseEvent::GetLogicalPosition(const wxDC
& dc
) const
297 wxPoint
pt(dc
.DeviceToLogicalX(m_x
), dc
.DeviceToLogicalY(m_y
));
307 wxKeyEvent::wxKeyEvent(wxEventType type
)
311 m_controlDown
= FALSE
;
317 void wxKeyEvent::CopyObject(wxObject
& obj_d
) const
319 wxKeyEvent
*obj
= (wxKeyEvent
*)&obj_d
;
320 wxEvent::CopyObject(obj_d
);
322 obj
->m_shiftDown
= m_shiftDown
;
323 obj
->m_controlDown
= m_controlDown
;
324 obj
->m_metaDown
= m_metaDown
;
325 obj
->m_altDown
= m_altDown
;
326 obj
->m_keyCode
= m_keyCode
;
334 void wxSizeEvent::CopyObject(wxObject
& obj_d
) const
336 wxSizeEvent
*obj
= (wxSizeEvent
*)&obj_d
;
337 wxEvent::CopyObject(obj_d
);
339 obj
->m_size
= m_size
;
342 void wxMoveEvent::CopyObject(wxObject
& obj_d
) const
344 wxMoveEvent
*obj
= (wxMoveEvent
*)&obj_d
;
345 wxEvent::CopyObject(obj_d
);
350 void wxEraseEvent::CopyObject(wxObject
& obj_d
) const
352 wxEraseEvent
*obj
= (wxEraseEvent
*)&obj_d
;
353 wxEvent::CopyObject(obj_d
);
358 void wxActivateEvent::CopyObject(wxObject
& obj_d
) const
360 wxActivateEvent
*obj
= (wxActivateEvent
*)&obj_d
;
361 wxEvent::CopyObject(obj_d
);
363 obj
->m_active
= m_active
;
366 void wxMenuEvent::CopyObject(wxObject
& obj_d
) const
368 wxMenuEvent
*obj
= (wxMenuEvent
*)&obj_d
;
369 wxEvent::CopyObject(obj_d
);
371 obj
->m_menuId
= m_menuId
;
374 void wxCloseEvent::CopyObject(wxObject
& obj_d
) const
376 wxCloseEvent
*obj
= (wxCloseEvent
*)&obj_d
;
377 wxEvent::CopyObject(obj_d
);
379 obj
->m_loggingOff
= m_loggingOff
;
380 obj
->m_veto
= m_veto
;
381 #if WXWIN_COMPATIBILITY
382 obj
->m_force
= m_force
;
384 obj
->m_canVeto
= m_canVeto
;
387 void wxShowEvent::CopyObject(wxObject
& obj_d
) const
389 wxShowEvent
*obj
= (wxShowEvent
*)&obj_d
;
390 wxEvent::CopyObject(obj_d
);
392 obj
->m_show
= m_show
;
400 wxEvtHandler::wxEvtHandler()
402 m_nextHandler
= (wxEvtHandler
*) NULL
;
403 m_previousHandler
= (wxEvtHandler
*) NULL
;
405 m_dynamicEvents
= (wxList
*) NULL
;
408 m_eventsLocker
= new wxCriticalSection();
410 m_pendingEvents
= (wxList
*) NULL
;
413 wxEvtHandler::~wxEvtHandler()
415 // Takes itself out of the list of handlers
416 if (m_previousHandler
)
417 m_previousHandler
->m_nextHandler
= m_nextHandler
;
420 m_nextHandler
->m_previousHandler
= m_previousHandler
;
424 wxNode
*node
= m_dynamicEvents
->First();
427 wxEventTableEntry
*entry
= (wxEventTableEntry
*)node
->Data();
428 if (entry
->m_callbackUserData
) delete entry
->m_callbackUserData
;
432 delete m_dynamicEvents
;
437 delete m_pendingEvents
;
439 delete m_eventsLocker
;
446 extern bool g_isIdle
;
448 extern void wxapp_install_idle_handler();
451 bool wxEvtHandler::ProcessThreadEvent(wxEvent
& event
)
454 wxCriticalSectionLocker
locker(*m_eventsLocker
);
457 // check that we are really in a child thread
458 wxASSERT( !wxThread::IsMain() );
460 if (m_pendingEvents
== NULL
)
461 m_pendingEvents
= new wxList();
463 event_main
= (wxEvent
*)event
.Clone();
465 m_pendingEvents
->Append(event_main
);
467 wxPendingEventsLocker
->Enter();
468 wxPendingEvents
->Append(this);
469 wxPendingEventsLocker
->Leave();
472 if (g_isIdle
) wxapp_install_idle_handler();
478 void wxEvtHandler::ProcessPendingEvents()
480 wxCriticalSectionLocker
locker(*m_eventsLocker
);
481 wxNode
*node
= m_pendingEvents
->First();
484 while (node
!= NULL
) {
485 event
= (wxEvent
*)node
->Data();
486 ProcessEvent(*event
);
488 node
= m_pendingEvents
->First();
497 bool wxEvtHandler::ProcessEvent(wxEvent
& event
)
499 // check that our flag corresponds to reality
500 wxASSERT( m_isWindow
== IsKindOf(CLASSINFO(wxWindow
)) );
502 // An event handler can be enabled or disabled
503 if ( GetEvtHandlerEnabled() )
506 // Check whether we are in a child thread.
507 if (!wxThread::IsMain())
508 return ProcessThreadEvent(event
);
510 // Handle per-instance dynamic event tables first
512 if ( m_dynamicEvents
&& SearchDynamicEventTable(event
) )
515 // Then static per-class event tables
517 const wxEventTable
*table
= GetEventTable();
519 // Try the associated validator first, if this is a window.
520 // Problem: if the event handler of the window has been replaced,
521 // this wxEvtHandler may no longer be a window.
522 // Therefore validators won't be processed if the handler
523 // has been replaced with SetEventHandler.
524 // THIS CAN BE CURED if PushEventHandler is used instead of
525 // SetEventHandler, and then processing will be passed down the
526 // chain of event handlers.
529 wxWindow
*win
= (wxWindow
*)this;
531 // Can only use the validator of the window which
532 // is receiving the event
533 if ( win
== event
.GetEventObject() )
535 wxValidator
*validator
= win
->GetValidator();
536 if ( validator
&& validator
->ProcessEvent(event
) )
543 // Search upwards through the inheritance hierarchy
546 if ( SearchEventTable((wxEventTable
&)*table
, event
) )
548 table
= table
->baseTable
;
552 // Try going down the event handler chain
553 if ( GetNextHandler() )
555 if ( GetNextHandler()->ProcessEvent(event
) )
559 // Carry on up the parent-child hierarchy,
560 // but only if event is a command event: it wouldn't
561 // make sense for a parent to receive a child's size event, for example
562 if ( m_isWindow
&& event
.IsCommandEvent() )
564 wxWindow
*win
= (wxWindow
*)this;
565 wxWindow
*parent
= win
->GetParent();
566 if (parent
&& !parent
->IsBeingDeleted())
567 return parent
->GetEventHandler()->ProcessEvent(event
);
570 // Last try - application object.
571 if ( wxTheApp
&& (this != wxTheApp
) )
573 // Special case: don't pass wxEVT_IDLE to wxApp, since it'll always
574 // swallow it. wxEVT_IDLE is sent explicitly to wxApp so it will be
575 // processed appropriately via SearchEventTable.
576 if ( event
.GetEventType() != wxEVT_IDLE
)
578 if ( wxTheApp
->ProcessEvent(event
) )
586 bool wxEvtHandler::SearchEventTable(wxEventTable
& table
, wxEvent
& event
)
589 int commandId
= event
.GetId();
591 // BC++ doesn't like while (table.entries[i].m_fn)
594 while (table
.entries
[i
].m_fn
!= 0)
596 while (table
.entries
[i
].m_fn
!= 0L)
599 if ((event
.GetEventType() == table
.entries
[i
].m_eventType
) &&
600 (table
.entries
[i
].m_id
== -1 || // Match, if event spec says any id will do (id == -1)
601 (table
.entries
[i
].m_lastId
== -1 && commandId
== table
.entries
[i
].m_id
) ||
602 (table
.entries
[i
].m_lastId
!= -1 &&
603 (commandId
>= table
.entries
[i
].m_id
&& commandId
<= table
.entries
[i
].m_lastId
))))
606 event
.m_callbackUserData
= table
.entries
[i
].m_callbackUserData
;
608 (this->*((wxEventFunction
) (table
.entries
[i
].m_fn
)))(event
);
610 if ( event
.GetSkipped() )
619 void wxEvtHandler::Connect( int id
, int lastId
,
620 wxEventType eventType
,
621 wxObjectEventFunction func
,
624 wxEventTableEntry
*entry
= new wxEventTableEntry
;
626 entry
->m_lastId
= lastId
;
627 entry
->m_eventType
= eventType
;
629 entry
->m_callbackUserData
= userData
;
631 if (!m_dynamicEvents
)
632 m_dynamicEvents
= new wxList
;
634 m_dynamicEvents
->Append( (wxObject
*) entry
);
637 bool wxEvtHandler::SearchDynamicEventTable( wxEvent
& event
)
639 wxCHECK_MSG( m_dynamicEvents
, FALSE
,
640 _T("caller should check that we have dynamic events") );
642 int commandId
= event
.GetId();
644 wxNode
*node
= m_dynamicEvents
->First();
647 wxEventTableEntry
*entry
= (wxEventTableEntry
*)node
->Data();
651 // Match, if event spec says any id will do (id == -1)
652 if ( (event
.GetEventType() == entry
->m_eventType
) &&
653 (entry
->m_id
== -1 ||
654 (entry
->m_lastId
== -1 && commandId
== entry
->m_id
) ||
655 (entry
->m_lastId
!= -1 &&
656 (commandId
>= entry
->m_id
&& commandId
<= entry
->m_lastId
))) )
659 event
.m_callbackUserData
= entry
->m_callbackUserData
;
661 (this->*((wxEventFunction
) (entry
->m_fn
)))(event
);
663 if (event
.GetSkipped())
674 #if WXWIN_COMPATIBILITY
675 bool wxEvtHandler::OnClose()
677 if (GetNextHandler())
678 return GetNextHandler()->OnClose();
682 #endif // WXWIN_COMPATIBILITY
684 // Find a window with the focus, that is also a descendant of the given window.
685 // This is used to determine the window to initially send commands to.
686 wxWindow
* wxFindFocusDescendant(wxWindow
* ancestor
)
688 // Process events starting with the window with the focus, if any.
689 wxWindow
* focusWin
= wxWindow::FindFocus();
690 wxWindow
* win
= focusWin
;
692 // Check if this is a descendant of this frame.
693 // If not, win will be set to NULL.
699 win
= win
->GetParent();
701 if (win
== (wxWindow
*) NULL
)
702 focusWin
= (wxWindow
*) NULL
;