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
[] =
72 // stupid SGI compiler --- offer aug 98
79 #endif // !USE_SHARED_LIBRARY
82 /* To put pending event handlers */
83 extern wxList
*wxPendingEvents
;
84 extern wxCriticalSection
*wxPendingEventsLocker
;
88 * General wxWindows events, covering
89 * all interesting things that might happen (button clicking, resizing,
90 * setting text in widgets, etc.).
92 * For each completely new event type, derive a new event class.
96 wxEvent::wxEvent(int theId
)
98 m_eventType
= wxEVT_NULL
;
99 m_eventObject
= (wxObject
*) NULL
;
100 m_eventHandle
= (char *) NULL
;
104 m_callbackUserData
= (wxObject
*) NULL
;
105 m_isCommandEvent
= FALSE
;
113 wxCommandEvent::wxCommandEvent(wxEventType commandType
, int theId
)
115 m_eventType
= commandType
;
116 m_clientData
= (char *) NULL
;
117 m_clientObject
= (wxClientData
*) NULL
;
121 m_commandString
= (char *) NULL
;
122 m_isCommandEvent
= TRUE
;
129 wxScrollEvent::wxScrollEvent(wxEventType commandType
,
133 : wxCommandEvent(commandType
, id
)
135 m_extraLong
= orient
;
145 wxMouseEvent::wxMouseEvent(wxEventType commandType
)
147 m_eventType
= commandType
;
150 m_controlDown
= FALSE
;
154 m_middleDown
= FALSE
;
159 // True if was a button dclick event (1 = left, 2 = middle, 3 = right)
160 // or any button dclick event (but = -1)
161 bool wxMouseEvent::ButtonDClick(int but
) const
166 return (LeftDClick() || MiddleDClick() || RightDClick());
170 return MiddleDClick();
172 return RightDClick();
174 wxFAIL_MSG("invalid parameter in wxMouseEvent::ButtonDClick");
180 // True if was a button down event (1 = left, 2 = middle, 3 = right)
181 // or any button down event (but = -1)
182 bool wxMouseEvent::ButtonDown(int but
) const
187 return (LeftDown() || MiddleDown() || RightDown());
195 wxFAIL_MSG("invalid parameter in wxMouseEvent::ButtonDown");
201 // True if was a button up event (1 = left, 2 = middle, 3 = right)
202 // or any button up event (but = -1)
203 bool wxMouseEvent::ButtonUp(int but
) const
207 return (LeftUp() || MiddleUp() || RightUp());
215 wxFAIL_MSG("invalid parameter in wxMouseEvent::ButtonUp");
221 // True if the given button is currently changing state
222 bool wxMouseEvent::Button(int but
) const
226 return (ButtonUp(-1) || ButtonDown(-1) || ButtonDClick(-1)) ;
228 return (LeftDown() || LeftUp() || LeftDClick());
230 return (MiddleDown() || MiddleUp() || MiddleDClick());
232 return (RightDown() || RightUp() || RightDClick());
234 wxFAIL_MSG("invalid parameter in wxMouseEvent::Button");
240 bool wxMouseEvent::ButtonIsDown(int but
) const
244 return (LeftIsDown() || MiddleIsDown() || RightIsDown());
248 return MiddleIsDown();
250 return RightIsDown();
252 wxFAIL_MSG("invalid parameter in wxMouseEvent::ButtonIsDown");
258 // Find the logical position of the event given the DC
259 wxPoint
wxMouseEvent::GetLogicalPosition(const wxDC
& dc
) const
261 wxPoint
pt(dc
.DeviceToLogicalX(m_x
), dc
.DeviceToLogicalY(m_y
));
271 wxKeyEvent::wxKeyEvent(wxEventType type
)
275 m_controlDown
= FALSE
;
285 wxEvtHandler::wxEvtHandler()
287 m_nextHandler
= (wxEvtHandler
*) NULL
;
288 m_previousHandler
= (wxEvtHandler
*) NULL
;
290 m_dynamicEvents
= (wxList
*) NULL
;
293 m_eventsLocker
= new wxCriticalSection();
295 m_pendingEvents
= (wxList
*) NULL
;
298 wxEvtHandler::~wxEvtHandler()
300 // Takes itself out of the list of handlers
301 if (m_previousHandler
)
302 m_previousHandler
->m_nextHandler
= m_nextHandler
;
305 m_nextHandler
->m_previousHandler
= m_previousHandler
;
309 wxNode
*node
= m_dynamicEvents
->First();
312 wxEventTableEntry
*entry
= (wxEventTableEntry
*)node
->Data();
313 if (entry
->m_callbackUserData
) delete entry
->m_callbackUserData
;
317 delete m_dynamicEvents
;
321 delete m_pendingEvents
;
324 delete m_eventsLocker
;
329 bool wxEvtHandler::ProcessThreadEvent(wxEvent
& event
)
332 wxCriticalSectionLocker
locker(*m_eventsLocker
);
334 // check that we are really in a child thread
335 wxASSERT( !wxThread::IsMain() );
337 if (m_pendingEvents
== NULL
)
338 m_pendingEvents
= new wxList();
340 event_main
= (wxEvent
*)event
.GetClassInfo()->CreateObject();
343 m_pendingEvents
->Append(event_main
);
345 wxPendingEventsLocker
->Enter();
346 wxPendingEvents
->Append(this);
347 wxPendingEventsLocker
->Leave();
352 void wxEvtHandler::ProcessPendingEvents()
354 wxCriticalSectionLocker
locker(*m_eventsLocker
);
355 wxNode
*node
= m_pendingEvents
->First();
358 while (node
!= NULL
) {
359 event
= (wxEvent
*)node
->Data();
360 ProcessEvent(*event
);
362 node
= m_pendingEvents
->First();
371 bool wxEvtHandler::ProcessEvent(wxEvent
& event
)
373 // check that our flag corresponds to reality
374 wxASSERT( m_isWindow
== IsKindOf(CLASSINFO(wxWindow
)) );
376 // An event handler can be enabled or disabled
377 if ( GetEvtHandlerEnabled() )
380 // Check whether we are in a child thread.
381 if (!wxThread::IsMain())
382 return ProcessThreadEvent(event
);
384 // Handle per-instance dynamic event tables first
386 if ( m_dynamicEvents
&& SearchDynamicEventTable(event
) )
389 // Then static per-class event tables
391 const wxEventTable
*table
= GetEventTable();
393 // Try the associated validator first, if this is a window.
394 // Problem: if the event handler of the window has been replaced,
395 // this wxEvtHandler may no longer be a window.
396 // Therefore validators won't be processed if the handler
397 // has been replaced with SetEventHandler.
398 // THIS CAN BE CURED if PushEventHandler is used instead of
399 // SetEventHandler, and then processing will be passed down the
400 // chain of event handlers.
403 wxWindow
*win
= (wxWindow
*)this;
405 // Can only use the validator of the window which
406 // is receiving the event
407 if ( win
== event
.GetEventObject() )
409 wxValidator
*validator
= win
->GetValidator();
410 if ( validator
&& validator
->ProcessEvent(event
) )
417 // Search upwards through the inheritance hierarchy
420 if ( SearchEventTable((wxEventTable
&)*table
, event
) )
422 table
= table
->baseTable
;
426 // Try going down the event handler chain
427 if ( GetNextHandler() )
429 if ( GetNextHandler()->ProcessEvent(event
) )
433 // Carry on up the parent-child hierarchy,
434 // but only if event is a command event: it wouldn't
435 // make sense for a parent to receive a child's size event, for example
436 if ( m_isWindow
&& event
.IsCommandEvent() )
438 wxWindow
*win
= (wxWindow
*)this;
439 wxWindow
*parent
= win
->GetParent();
440 if (parent
&& !parent
->IsBeingDeleted())
441 return parent
->GetEventHandler()->ProcessEvent(event
);
444 // Last try - application object.
445 if ( wxTheApp
&& (this != wxTheApp
) )
447 // Special case: don't pass wxEVT_IDLE to wxApp, since it'll always
448 // swallow it. wxEVT_IDLE is sent explicitly to wxApp so it will be
449 // processed appropriately via SearchEventTable.
450 if ( event
.GetEventType() != wxEVT_IDLE
)
452 if ( wxTheApp
->ProcessEvent(event
) )
460 bool wxEvtHandler::SearchEventTable(wxEventTable
& table
, wxEvent
& event
)
463 int commandId
= event
.GetId();
465 // BC++ doesn't like while (table.entries[i].m_fn)
468 while (table
.entries
[i
].m_fn
!= 0)
470 while (table
.entries
[i
].m_fn
!= 0L)
473 if ((event
.GetEventType() == table
.entries
[i
].m_eventType
) &&
474 (table
.entries
[i
].m_id
== -1 || // Match, if event spec says any id will do (id == -1)
475 (table
.entries
[i
].m_lastId
== -1 && commandId
== table
.entries
[i
].m_id
) ||
476 (table
.entries
[i
].m_lastId
!= -1 &&
477 (commandId
>= table
.entries
[i
].m_id
&& commandId
<= table
.entries
[i
].m_lastId
))))
480 event
.m_callbackUserData
= table
.entries
[i
].m_callbackUserData
;
482 (this->*((wxEventFunction
) (table
.entries
[i
].m_fn
)))(event
);
484 if ( event
.GetSkipped() )
493 void wxEvtHandler::Connect( int id
, int lastId
,
494 wxEventType eventType
,
495 wxObjectEventFunction func
,
498 wxEventTableEntry
*entry
= new wxEventTableEntry
;
500 entry
->m_lastId
= lastId
;
501 entry
->m_eventType
= eventType
;
503 entry
->m_callbackUserData
= userData
;
505 if (!m_dynamicEvents
)
506 m_dynamicEvents
= new wxList
;
508 m_dynamicEvents
->Append( (wxObject
*) entry
);
511 bool wxEvtHandler::SearchDynamicEventTable( wxEvent
& event
)
513 wxCHECK_MSG( m_dynamicEvents
, FALSE
,
514 "caller should check that we have dynamic events" );
516 int commandId
= event
.GetId();
518 wxNode
*node
= m_dynamicEvents
->First();
521 wxEventTableEntry
*entry
= (wxEventTableEntry
*)node
->Data();
525 // Match, if event spec says any id will do (id == -1)
526 if ( (event
.GetEventType() == entry
->m_eventType
) &&
527 (entry
->m_id
== -1 ||
528 (entry
->m_lastId
== -1 && commandId
== entry
->m_id
) ||
529 (entry
->m_lastId
!= -1 &&
530 (commandId
>= entry
->m_id
&& commandId
<= entry
->m_lastId
))) )
533 event
.m_callbackUserData
= entry
->m_callbackUserData
;
535 (this->*((wxEventFunction
) (entry
->m_fn
)))(event
);
537 if (event
.GetSkipped())
548 #if WXWIN_COMPATIBILITY
549 bool wxEvtHandler::OnClose()
551 if (GetNextHandler())
552 return GetNextHandler()->OnClose();
556 #endif // WXWIN_COMPATIBILITY
558 // Find a window with the focus, that is also a descendant of the given window.
559 // This is used to determine the window to initially send commands to.
560 wxWindow
* wxFindFocusDescendant(wxWindow
* ancestor
)
562 // Process events starting with the window with the focus, if any.
563 wxWindow
* focusWin
= wxWindow::FindFocus();
564 wxWindow
* win
= focusWin
;
566 // Check if this is a descendant of this frame.
567 // If not, win will be set to NULL.
573 win
= win
->GetParent();
575 if (win
== (wxWindow
*) NULL
)
576 focusWin
= (wxWindow
*) NULL
;