A class that can handle events from the windowing system.
Window is (and therefore all window classes are) derived from this class.
When events are received, EvtHandler invokes the method listed in the event table using itself as the object. When using multiple inheritance it is imperative that the EvtHandler (-derived) class is the first class inherited such that the this pointer for the overall object will be identical to the this pointer of the EvtHandler portion.
See also
AppConsole, AuiManager, DocManager, Document, EventBlocker, FileSystemWatcher, Menu, MouseEventsManager, NotificationMessage, Process, PropertyGridPage, TaskBarIcon, Timer, Validator, View, Window
__init__ | Constructor. |
AddFilter | Add an event filter whose FilterEvent() method will be called for each and every event processed by wxWidgets. |
AddPendingEvent | Post an event to be processed later. |
Bind | Bind an event to an event handler. |
DeletePendingEvents | Deletes all events queued on this event handler using QueueEvent or AddPendingEvent . |
GetEvtHandlerEnabled | Returns True if the event handler is enabled, False otherwise. |
GetNextHandler | Returns the pointer to the next handler in the chain. |
GetPreviousHandler | Returns the pointer to the previous handler in the chain. |
IsUnlinked | Returns True if the next and the previous handler pointers of this event handler instance are None. |
ProcessEvent | Processes an event, searching event tables and calling zero or more suitable event handler function(s). |
ProcessEventLocally | Try to process the event in this handler and all those chained to it. |
ProcessPendingEvents | Processes the pending events previously queued using QueueEvent or AddPendingEvent ; you must call this function only if you are sure there are pending events for this handler, otherwise a CHECK will fail. |
QueueEvent | Queue event for a later processing. |
RemoveFilter | Remove a filter previously installed with AddFilter . |
SafelyProcessEvent | Processes an event by calling ProcessEvent and handles any exceptions that occur in the process. |
SetEvtHandlerEnabled | Enables or disables the event handler. |
SetNextHandler | Sets the pointer to the next handler. |
SetPreviousHandler | Sets the pointer to the previous handler. |
Unbind | Disconnects the event handler binding for event from self. |
Unlink | Unlinks this event handler from the chain it’s part of (if any); then links the “previous” event handler to the “next” one (so that the chain won’t be interrupted). |
EvtHandlerEnabled | See GetEvtHandlerEnabled and SetEvtHandlerEnabled |
NextHandler | See GetNextHandler and SetNextHandler |
PreviousHandler | See GetPreviousHandler and SetPreviousHandler |
A class that can handle events from the windowing system.
Possible constructors:
EvtHandler()
Constructor.
Add an event filter whose FilterEvent() method will be called for each and every event processed by wxWidgets.
The filters are called in LIFO order and App is registered as an event filter by default. The pointer must remain valid until it’s removed with RemoveFilter and is not deleted by EvtHandler.
Parameters: | filter (EventFilter) – |
---|
New in version 2.9.3.
Post an event to be processed later.
This function is similar to QueueEvent but can’t be used to post events from worker threads for the event objects with String fields (i.e. in practice most of them) because of an unsafe use of the same String object which happens because the String field in the original event object and its copy made internally by this function share the same string buffer internally. Use QueueEvent to avoid this.
A copy of event is made by the function, so the original can be deleted as soon as function returns (it is common that the original is created on the stack). This requires that the Event.Clone method be implemented by event so that it can be duplicated and stored until it gets processed.
Parameters: | event (Event) – Event to add to the pending events queue. |
---|
Bind an event to an event handler.
Parameters: |
|
---|
Deletes all events queued on this event handler using QueueEvent or AddPendingEvent .
Use with care because the events which are deleted are (obviously) not processed and this may have unwanted consequences (e.g. user actions events will be lost).
Returns True if the event handler is enabled, False otherwise.
Return type: | bool |
---|
See also
Returns the pointer to the next handler in the chain.
Return type: | EvtHandler |
---|
Returns the pointer to the previous handler in the chain.
Return type: | EvtHandler |
---|
Returns True if the next and the previous handler pointers of this event handler instance are None.
Return type: | bool |
---|
New in version 2.9.0.
See also
Processes an event, searching event tables and calling zero or more suitable event handler function(s).
Normally, your application would not call this function: it is called in the wxWidgets implementation to dispatch incoming user interface events to the framework (and application).
However, you might need to call it if implementing new functionality (such as a new control) where you define new event types, as opposed to allowing the user to override virtual functions.
Notice that you don’t usually need to override ProcessEvent to customize the event handling, overriding the specially provided TryBefore and TryAfter functions is usually enough. For example, MDIParentFrame may override TryBefore to ensure that the menu events are processed in the active child frame before being processed in the parent frame itself.
The normal order of event table searching is as follows:
(referring to the image, if A->ProcessEvent is called and it doesn’t handle the event, B->ProcessEvent will be called and so on...). Note that in the case of Window you can build a stack of event handlers (see Window.PushEventHandler for more info). If any of the handlers of the chain return True, the function exits.
Notice that steps (2)-(6) are performed in ProcessEventLocally which is called by this function.
Parameters: | event (Event) – Event to process. |
---|---|
Return type: | bool |
Returns: | True if a suitable event handler function was found and executed, and the function did not call Event.Skip . |
See also
SearchEventTable
Try to process the event in this handler and all those chained to it.
As explained in ProcessEvent documentation, the event handlers may be chained in a doubly-linked list. This function tries to process the event in this handler (including performing any pre-processing done in TryBefore , e.g. applying validators) and all those following it in the chain until the event is processed or the chain is exhausted.
This function is called from ProcessEvent and, in turn, calls TryBefore and TryAfter . It is not virtual and so cannot be overridden but can, and should, be called to forward an event to another handler instead of ProcessEvent which would result in a duplicate call to TryAfter , e.g. resulting in all unprocessed events being sent to the application object multiple times.
Parameters: | event (Event) – Event to process. |
---|---|
Return type: | bool |
Returns: | True if this handler of one of those chained to it processed the event. |
New in version 2.9.1.
Processes the pending events previously queued using QueueEvent or AddPendingEvent ; you must call this function only if you are sure there are pending events for this handler, otherwise a CHECK will fail.
The real processing still happens in ProcessEvent which is called by this function.
Note that this function needs a valid application object (see AppConsole.GetInstance ) because App holds the list of the event handlers with pending events and this function manipulates that list.
Queue event for a later processing.
This method is similar to ProcessEvent but while the latter is synchronous, i.e. the event is processed immediately, before the function returns, this one is asynchronous and returns immediately while the event will be processed at some later time (usually during the next event loop iteration).
Another important difference is that this method takes ownership of the event parameter, i.e. it will delete it itself. This implies that the event should be allocated on the heap and that the pointer can’t be used any more after the function returns (as it can be deleted at any moment).
QueueEvent can be used for inter-thread communication from the worker threads to the main thread, it is safe in the sense that it uses locking internally and avoids the problem mentioned in AddPendingEvent documentation by ensuring that the event object is not used by the calling thread any more. Care should still be taken to avoid that some fields of this object are used by it, notably any String members of the event object must not be shallow copies of another String object as this would result in them still using the same string buffer behind the scenes. For example:
def FunctionInAWorkerThread(strs):
evt = wx.CommandEvent()
# NOT evt.SetString(strs) as this would be a shallow copy
evt.SetString(strs[:]) # make a deep copy
wx.TheApp.QueueEvent(evt)
Note that you can use ThreadEvent instead of CommandEvent to avoid this problem:
def FunctionInAWorkerThread(strs):
evt = wx.ThreadEvent()
evt.SetString(strs)
# wx.ThreadEvent.Clone() makes sure that the internal wx.String
# member is not shared by other string instances:
wx.TheApp.QueueEvent(evt.Clone())
Finally notice that this method automatically wakes up the event loop if it is currently idle by calling WakeUpIdle so there is no need to do it manually when using it.
Parameters: | event (Event) – A heap-allocated event to be queued, QueueEvent takes ownership of it. This parameter shouldn’t be NULL . |
---|
New in version 2.9.0.
Remove a filter previously installed with AddFilter .
It’s an error to remove a filter that hadn’t been previously added or was already removed.
Parameters: | filter (EventFilter) – |
---|
New in version 2.9.3.
Processes an event by calling ProcessEvent and handles any exceptions that occur in the process.
If an exception is thrown in event handler, App.OnExceptionInMainLoop is called.
Parameters: | event (Event) – Event to process. |
---|---|
Return type: | bool |
Returns: | True if the event was processed, False if no handler was found or an exception was thrown. |
See also
Enables or disables the event handler.
Parameters: | enabled (bool) – True if the event handler is to be enabled, False if it is to be disabled. |
---|
Note
You can use this function to avoid having to remove the event handler from the chain, for example when implementing a dialog editor and changing from edit to test mode.
See also
Sets the pointer to the next handler.
Parameters: | handler (EvtHandler) – The event handler to be set as the next handler. Cannot be None. |
---|
Note
See ProcessEvent for more info about how the chains of event handlers are internally used. Also remember that EvtHandler uses double-linked lists and thus if you use this function, you should also call SetPreviousHandler on the argument passed to this function:
handlerA.SetNextHandler(handlerB)
handlerB.SetPreviousHandler(handlerA)
See also
Sets the pointer to the previous handler.
All remarks about SetNextHandler apply to this function as well.
Parameters: | handler (EvtHandler) – The event handler to be set as the previous handler. Cannot be None. |
---|
See also
Disconnects the event handler binding for event from self. Returns True if successful.
Unlinks this event handler from the chain it’s part of (if any); then links the “previous” event handler to the “next” one (so that the chain won’t be interrupted).
E.g. if before calling Unlink you have the following chain:
then after calling B-> Unlink you’ll have:
New in version 2.9.0.
See GetNextHandler and SetNextHandler
See GetPreviousHandler and SetPreviousHandler