1 /////////////////////////////////////////////////////////////////////////////
2 // Name: interface/wx/event_base.h
3 // Purpose: Documentation of wxEvtHandler, wxEvent, wxIdleEvent and other
4 // non-GUI event-related classes declared in include/wx/event.h
5 // (see interface/wx/event.h for the GUI event classes)
6 // Author: wxWidgets team
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
12 The predefined constants for the number of times we propagate event
13 upwards window child-parent chain.
15 enum wxEventPropagation
17 /// don't propagate it at all
18 wxEVENT_PROPAGATE_NONE
= 0,
20 /// propagate it until it is processed
21 wxEVENT_PROPAGATE_MAX
= INT_MAX
25 The different categories for a wxEvent; see wxEvent::GetEventCategory.
27 @note They are used as OR-combinable flags by wxEventLoopBase::YieldFor.
32 This is the category for those events which are generated to update
33 the appearance of the GUI but which (usually) do not comport data
34 processing, i.e. which do not provide input or output data
35 (e.g. size events, scroll events, etc).
36 They are events NOT directly generated by the user's input devices.
38 wxEVT_CATEGORY_UI
= 1,
41 This category groups those events which are generated directly from the
42 user through input devices like mouse and keyboard and usually result in
43 data to be processed from the application
44 (e.g. mouse clicks, key presses, etc).
46 wxEVT_CATEGORY_USER_INPUT
= 2,
48 /// This category is for wxSocketEvent
49 wxEVT_CATEGORY_SOCKET
= 4,
51 /// This category is for wxTimerEvent
52 wxEVT_CATEGORY_TIMER
= 8,
55 This category is for any event used to send notifications from the
56 secondary threads to the main one or in general for notifications among
57 different threads (which may or may not be user-generated).
58 See e.g. wxThreadEvent.
60 wxEVT_CATEGORY_THREAD
= 16,
63 This mask is used in wxEventLoopBase::YieldFor to specify that all event
64 categories should be processed.
67 wxEVT_CATEGORY_UI
|wxEVT_CATEGORY_USER_INPUT
|wxEVT_CATEGORY_SOCKET
| \
68 wxEVT_CATEGORY_TIMER
|wxEVT_CATEGORY_THREAD
74 An event is a structure holding information about an event passed to a
75 callback or member function.
77 wxEvent used to be a multipurpose event object, and is an abstract base class
78 for other event classes (see below).
80 For more information about events, see the @ref overview_events overview.
83 In wxPerl custom event classes should be derived from
84 @c Wx::PlEvent and @c Wx::PlCommandEvent.
91 @see wxCommandEvent, wxMouseEvent
93 class wxEvent
: public wxObject
99 Notice that events are usually created by wxWidgets itself and creating
100 e.g. a wxPaintEvent in your code and sending it to e.g. a wxTextCtrl
101 will not usually affect it at all as native controls have no specific
102 knowledge about wxWidgets events. However you may construct objects of
103 specific types and pass them to wxEvtHandler::ProcessEvent() if you
104 want to create your own custom control and want to process its events
105 in the same manner as the standard ones.
107 Also please notice that the order of parameters in this constructor is
108 different from almost all the derived classes which specify the event
109 type as the first argument.
112 The identifier of the object (window, timer, ...) which generated
115 The unique type of event, e.g. @c wxEVT_PAINT, @c wxEVT_SIZE or
116 @c wxEVT_COMMAND_BUTTON_CLICKED.
118 wxEvent(int id
= 0, wxEventType eventType
= wxEVT_NULL
);
121 Returns a copy of the event.
123 Any event that is posted to the wxWidgets event system for later action
124 (via wxEvtHandler::AddPendingEvent, wxEvtHandler::QueueEvent or wxPostEvent())
125 must implement this method.
127 All wxWidgets events fully implement this method, but any derived events
128 implemented by the user should also implement this method just in case they
129 (or some event derived from them) are ever posted.
131 All wxWidgets events implement a copy constructor, so the easiest way of
132 implementing the Clone function is to implement a copy constructor for
133 a new event (call it MyEvent) and then define the Clone function like this:
136 wxEvent *Clone() const { return new MyEvent(*this); }
139 virtual wxEvent
* Clone() const = 0;
142 Returns the object (usually a window) associated with the event, if any.
144 wxObject
* GetEventObject() const;
147 Returns the identifier of the given event type, such as @c wxEVT_COMMAND_BUTTON_CLICKED.
149 wxEventType
GetEventType() const;
152 Returns a generic category for this event.
153 wxEvent implementation returns @c wxEVT_CATEGORY_UI by default.
155 This function is used to selectively process events in wxEventLoopBase::YieldFor.
157 virtual wxEventCategory
GetEventCategory() const;
160 Returns the identifier associated with this event, such as a button command id.
165 Return the user data associated with a dynamically connected event handler.
167 wxEvtHandler::Connect() and wxEvtHandler::Bind() allow associating
168 optional @c userData pointer with the handler and this method returns
169 the value of this pointer.
171 The returned pointer is owned by wxWidgets and must not be deleted.
175 wxObject
*GetEventUserData() const;
178 Returns @true if the event handler should be skipped, @false otherwise.
180 bool GetSkipped() const;
183 Gets the timestamp for the event. The timestamp is the time in milliseconds
184 since some fixed moment (not necessarily the standard Unix Epoch, so only
185 differences between the timestamps and not their absolute values usually make sense).
188 wxWidgets returns a non-NULL timestamp only for mouse and key events
189 (see wxMouseEvent and wxKeyEvent).
191 long GetTimestamp() const;
194 Returns @true if the event is or is derived from wxCommandEvent else it returns @false.
196 @note exists only for optimization purposes.
198 bool IsCommandEvent() const;
201 Sets the propagation level to the given value (for example returned from an
202 earlier call to wxEvent::StopPropagation).
204 void ResumePropagation(int propagationLevel
);
207 Sets the originating object.
209 void SetEventObject(wxObject
* object
);
214 void SetEventType(wxEventType type
);
217 Sets the identifier associated with this event, such as a button command id.
222 Sets the timestamp for the event.
224 void SetTimestamp(long timeStamp
= 0);
227 Test if this event should be propagated or not, i.e. if the propagation level
228 is currently greater than 0.
230 bool ShouldPropagate() const;
233 This method can be used inside an event handler to control whether further
234 event handlers bound to this event will be called after the current one returns.
236 Without Skip() (or equivalently if Skip(@false) is used), the event will not
237 be processed any more. If Skip(@true) is called, the event processing system
238 continues searching for a further handler function for this event, even though
239 it has been processed already in the current handler.
241 In general, it is recommended to skip all non-command events to allow the
242 default handling to take place. The command events are, however, normally not
243 skipped as usually a single command such as a button click or menu item
244 selection must only be processed by one handler.
246 void Skip(bool skip
= true);
249 Stop the event from propagating to its parent window.
251 Returns the old propagation level value which may be later passed to
252 ResumePropagation() to allow propagating the event again.
254 int StopPropagation();
258 Indicates how many levels the event can propagate.
260 This member is protected and should typically only be set in the constructors
261 of the derived classes. It may be temporarily changed by StopPropagation()
262 and ResumePropagation() and tested with ShouldPropagate().
264 The initial value is set to either @c wxEVENT_PROPAGATE_NONE (by default)
265 meaning that the event shouldn't be propagated at all or to
266 @c wxEVENT_PROPAGATE_MAX (for command events) meaning that it should be
267 propagated as much as necessary.
269 Any positive number means that the event should be propagated but no more than
270 the given number of times. E.g. the propagation level may be set to 1 to
271 propagate the event to its parent only, but not to its grandparent.
273 int m_propagationLevel
;
282 A class that can handle events from the windowing system.
283 wxWindow is (and therefore all window classes are) derived from this class.
285 When events are received, wxEvtHandler invokes the method listed in the
286 event table using itself as the object. When using multiple inheritance
287 <b>it is imperative that the wxEvtHandler(-derived) class is the first
288 class inherited</b> such that the @c this pointer for the overall object
289 will be identical to the @c this pointer of the wxEvtHandler portion.
295 @see @ref overview_events_processing, wxEventBlocker, wxEventLoopBase
297 class wxEvtHandler
: public wxObject
, public wxTrackable
308 If the handler is part of a chain, the destructor will unlink itself
311 virtual ~wxEvtHandler();
315 @name Event queuing and processing
320 Queue event for a later processing.
322 This method is similar to ProcessEvent() but while the latter is
323 synchronous, i.e. the event is processed immediately, before the
324 function returns, this one is asynchronous and returns immediately
325 while the event will be processed at some later time (usually during
326 the next event loop iteration).
328 Another important difference is that this method takes ownership of the
329 @a event parameter, i.e. it will delete it itself. This implies that
330 the event should be allocated on the heap and that the pointer can't be
331 used any more after the function returns (as it can be deleted at any
334 QueueEvent() can be used for inter-thread communication from the worker
335 threads to the main thread, it is safe in the sense that it uses
336 locking internally and avoids the problem mentioned in AddPendingEvent()
337 documentation by ensuring that the @a event object is not used by the
338 calling thread any more. Care should still be taken to avoid that some
339 fields of this object are used by it, notably any wxString members of
340 the event object must not be shallow copies of another wxString object
341 as this would result in them still using the same string buffer behind
342 the scenes. For example:
344 void FunctionInAWorkerThread(const wxString& str)
346 wxCommandEvent* evt = new wxCommandEvent;
348 // NOT evt->SetString(str) as this would be a shallow copy
349 evt->SetString(str.c_str()); // make a deep copy
351 wxTheApp->QueueEvent( evt );
355 Note that you can use wxThreadEvent instead of wxCommandEvent
356 to avoid this problem:
358 void FunctionInAWorkerThread(const wxString& str)
363 // wxThreadEvent::Clone() makes sure that the internal wxString
364 // member is not shared by other wxString instances:
365 wxTheApp->QueueEvent( evt.Clone() );
369 Finally notice that this method automatically wakes up the event loop
370 if it is currently idle by calling ::wxWakeUpIdle() so there is no need
371 to do it manually when using it.
376 A heap-allocated event to be queued, QueueEvent() takes ownership
377 of it. This parameter shouldn't be @c NULL.
379 virtual void QueueEvent(wxEvent
*event
);
382 Post an event to be processed later.
384 This function is similar to QueueEvent() but can't be used to post
385 events from worker threads for the event objects with wxString fields
386 (i.e. in practice most of them) because of an unsafe use of the same
387 wxString object which happens because the wxString field in the
388 original @a event object and its copy made internally by this function
389 share the same string buffer internally. Use QueueEvent() to avoid
392 A copy of @a event is made by the function, so the original can be deleted
393 as soon as function returns (it is common that the original is created
394 on the stack). This requires that the wxEvent::Clone() method be
395 implemented by event so that it can be duplicated and stored until it
399 Event to add to the pending events queue.
401 virtual void AddPendingEvent(const wxEvent
& event
);
404 Asynchronously call the given method.
406 Calling this function on an object schedules an asynchronous call to
407 the method specified as CallAfter() argument at a (slightly) later
408 time. This is useful when processing some events as certain actions
409 typically can't be performed inside their handlers, e.g. you shouldn't
410 show a modal dialog from a mouse click event handler as this would
411 break the mouse capture state -- but you can call a method showing
412 this message dialog after the current event handler completes.
414 The method being called must be the method of the object on which
415 CallAfter() itself is called.
417 Notice that it is safe to use CallAfter() from other, non-GUI,
418 threads, but that the method will be always called in the main, GUI,
423 class MyFrame : public wxFrame {
424 void OnClick(wxMouseEvent& event) {
425 CallAfter(&MyFrame::ShowPosition, event.GetPosition());
428 void ShowPosition(const wxPoint& pos) {
430 wxString::Format("Perform click at (%d, %d)?",
431 pos.x, pos.y), "", wxYES_NO) == wxYES )
433 ... do take this click into account ...
439 @param method The method to call.
440 @param x1 The (optional) first parameter to pass to the method.
441 @param x2 The (optional) second parameter to pass to the method.
443 Note that currently only up to 2 arguments can be passed.
445 @note This method is not available with Visual C++ 6 which doesn't
446 have the required support for C++ templates to implement it.
450 template<typename T
, typename T1
, ...>
451 void CallAfter(void (T::*method
)(T1
, ...), T1 x1
, ...);
454 Processes an event, searching event tables and calling zero or more suitable
455 event handler function(s).
457 Normally, your application would not call this function: it is called in the
458 wxWidgets implementation to dispatch incoming user interface events to the
459 framework (and application).
461 However, you might need to call it if implementing new functionality
462 (such as a new control) where you define new event types, as opposed to
463 allowing the user to override virtual functions.
465 Notice that you don't usually need to override ProcessEvent() to
466 customize the event handling, overriding the specially provided
467 TryBefore() and TryAfter() functions is usually enough. For example,
468 wxMDIParentFrame may override TryBefore() to ensure that the menu
469 events are processed in the active child frame before being processed
470 in the parent frame itself.
472 The normal order of event table searching is as follows:
473 -# wxApp::FilterEvent() is called. If it returns anything but @c -1
474 (default) the processing stops here.
475 -# TryBefore() is called (this is where wxValidator are taken into
476 account for wxWindow objects). If this returns @true, the function exits.
477 -# If the object is disabled (via a call to wxEvtHandler::SetEvtHandlerEnabled)
478 the function skips to step (7).
479 -# Dynamic event table of the handlers bound using Bind<>() is
480 searched. If a handler is found, it is executed and the function
481 returns @true unless the handler used wxEvent::Skip() to indicate
482 that it didn't handle the event in which case the search continues.
483 -# Static events table of the handlers bound using event table
484 macros is searched for this event handler. If this fails, the base
485 class event table is tried, and so on until no more tables
486 exist or an appropriate function was found. If a handler is found,
487 the same logic as in the previous step applies.
488 -# The search is applied down the entire chain of event handlers (usually the
489 chain has a length of one). This chain can be formed using wxEvtHandler::SetNextHandler():
490 @image html overview_events_chain.png
491 (referring to the image, if @c A->ProcessEvent is called and it doesn't handle
492 the event, @c B->ProcessEvent will be called and so on...).
493 Note that in the case of wxWindow you can build a stack of event handlers
494 (see wxWindow::PushEventHandler() for more info).
495 If any of the handlers of the chain return @true, the function exits.
496 -# TryAfter() is called: for the wxWindow object this may propagate the
497 event to the window parent (recursively). If the event is still not
498 processed, ProcessEvent() on wxTheApp object is called as the last
501 Notice that steps (2)-(6) are performed in ProcessEventLocally()
502 which is called by this function.
507 @true if a suitable event handler function was found and executed,
508 and the function did not call wxEvent::Skip.
510 @see SearchEventTable()
512 virtual bool ProcessEvent(wxEvent
& event
);
515 Try to process the event in this handler and all those chained to it.
517 As explained in ProcessEvent() documentation, the event handlers may be
518 chained in a doubly-linked list. This function tries to process the
519 event in this handler (including performing any pre-processing done in
520 TryBefore(), e.g. applying validators) and all those following it in
521 the chain until the event is processed or the chain is exhausted.
523 This function is called from ProcessEvent() and, in turn, calls
524 TryBefore() and TryAfter(). It is not virtual and so cannot be
525 overridden but can, and should, be called to forward an event to
526 another handler instead of ProcessEvent() which would result in a
527 duplicate call to TryAfter(), e.g. resulting in all unprocessed events
528 being sent to the application object multiple times.
535 @true if this handler of one of those chained to it processed the
538 bool ProcessEventLocally(wxEvent
& event
);
541 Processes an event by calling ProcessEvent() and handles any exceptions
542 that occur in the process.
543 If an exception is thrown in event handler, wxApp::OnExceptionInMainLoop is called.
548 @return @true if the event was processed, @false if no handler was found
549 or an exception was thrown.
551 @see wxWindow::HandleWindowEvent
553 bool SafelyProcessEvent(wxEvent
& event
);
556 Processes the pending events previously queued using QueueEvent() or
557 AddPendingEvent(); you must call this function only if you are sure
558 there are pending events for this handler, otherwise a @c wxCHECK
561 The real processing still happens in ProcessEvent() which is called by this
564 Note that this function needs a valid application object (see
565 wxAppConsole::GetInstance()) because wxApp holds the list of the event
566 handlers with pending events and this function manipulates that list.
568 void ProcessPendingEvents();
571 Deletes all events queued on this event handler using QueueEvent() or
574 Use with care because the events which are deleted are (obviously) not
575 processed and this may have unwanted consequences (e.g. user actions events
578 void DeletePendingEvents();
581 Searches the event table, executing an event handler function if an appropriate
585 Event table to be searched.
587 Event to be matched against an event table entry.
589 @return @true if a suitable event handler function was found and
590 executed, and the function did not call wxEvent::Skip.
592 @remarks This function looks through the object's event table and tries
593 to find an entry that will match the event.
594 An entry will match if:
595 @li The event type matches, and
596 @li the identifier or identifier range matches, or the event table
597 entry's identifier is zero.
599 If a suitable function is called but calls wxEvent::Skip, this
600 function will fail, and searching will continue.
602 @todo this function in the header is listed as an "implementation only" function;
603 are we sure we want to document it?
607 virtual bool SearchEventTable(wxEventTable
& table
,
614 @name Connecting and disconnecting
619 Connects the given function dynamically with the event handler, id and
622 Notice that Bind() provides a more flexible and safer way to do the
623 same thing as Connect(), please use it in any new code -- while
624 Connect() is not formally deprecated due to its existing widespread
625 usage, it has no advantages compared to Bind().
627 This is an alternative to the use of static event tables. It is more
628 flexible as it allows to connect events generated by some object to an
629 event handler defined in a different object of a different class (which
630 is impossible to do directly with the event tables -- the events can be
631 only handled in another object if they are propagated upwards to it).
632 Do make sure to specify the correct @a eventSink when connecting to an
633 event of a different object.
635 See @ref overview_events_bind for more detailed explanation
636 of this function and the @ref page_samples_event sample for usage
639 This specific overload allows you to connect an event handler to a @e range
641 Do not confuse @e source IDs with event @e types: source IDs identify the
642 event generator objects (typically wxMenuItem or wxWindow objects) while the
643 event @e type identify which type of events should be handled by the
644 given @e function (an event generator object may generate many different
648 The first ID of the identifier range to be associated with the event
651 The last ID of the identifier range to be associated with the event
654 The event type to be associated with this event handler.
656 The event handler function. Note that this function should
657 be explicitly converted to the correct type which can be done using a macro
658 called @c wxFooEventHandler for the handler for any @c wxFooEvent.
660 Optional data to be associated with the event table entry.
661 wxWidgets will take ownership of this pointer, i.e. it will be
662 destroyed when the event handler is disconnected or at the program
663 termination. This pointer can be retrieved using
664 wxEvent::GetEventUserData() later.
666 Object whose member function should be called. It must be specified
667 when connecting an event generated by one object to a member
668 function of a different object. If it is omitted, @c this is used.
671 In wxPerl this function takes 4 arguments: @a id, @a lastid,
672 @a type, @a method; if @a method is undef, the handler is
678 void Connect(int id
, int lastId
, wxEventType eventType
,
679 wxObjectEventFunction function
,
680 wxObject
* userData
= NULL
,
681 wxEvtHandler
* eventSink
= NULL
);
684 See the Connect(int, int, wxEventType, wxObjectEventFunction, wxObject*, wxEvtHandler*)
685 overload for more info.
687 This overload can be used to attach an event handler to a single source ID:
691 frame->Connect( wxID_EXIT,
692 wxEVT_COMMAND_MENU_SELECTED,
693 wxCommandEventHandler(MyFrame::OnQuit) );
697 Not supported by wxPerl.
700 void Connect(int id
, wxEventType eventType
,
701 wxObjectEventFunction function
,
702 wxObject
* userData
= NULL
,
703 wxEvtHandler
* eventSink
= NULL
);
706 See the Connect(int, int, wxEventType, wxObjectEventFunction, wxObject*, wxEvtHandler*)
707 overload for more info.
709 This overload will connect the given event handler so that regardless of the
710 ID of the event source, the handler will be called.
713 Not supported by wxPerl.
716 void Connect(wxEventType eventType
,
717 wxObjectEventFunction function
,
718 wxObject
* userData
= NULL
,
719 wxEvtHandler
* eventSink
= NULL
);
722 Disconnects the given function dynamically from the event handler, using the
723 specified parameters as search criteria and returning @true if a matching
724 function has been found and removed.
726 This method can only disconnect functions which have been added using the
727 Connect() method. There is no way to disconnect functions connected using
728 the (static) event tables.
731 The event type associated with this event handler.
733 The event handler function.
735 Data associated with the event table entry.
737 Object whose member function should be called.
740 Not supported by wxPerl.
743 bool Disconnect(wxEventType eventType
,
744 wxObjectEventFunction function
,
745 wxObject
* userData
= NULL
,
746 wxEvtHandler
* eventSink
= NULL
);
749 See the Disconnect(wxEventType, wxObjectEventFunction, wxObject*, wxEvtHandler*)
750 overload for more info.
752 This overload takes the additional @a id parameter.
755 Not supported by wxPerl.
758 bool Disconnect(int id
= wxID_ANY
,
759 wxEventType eventType
= wxEVT_NULL
,
760 wxObjectEventFunction function
= NULL
,
761 wxObject
* userData
= NULL
,
762 wxEvtHandler
* eventSink
= NULL
);
765 See the Disconnect(wxEventType, wxObjectEventFunction, wxObject*, wxEvtHandler*)
766 overload for more info.
768 This overload takes an additional range of source IDs.
771 In wxPerl this function takes 3 arguments: @a id,
775 bool Disconnect(int id
, int lastId
,
776 wxEventType eventType
,
777 wxObjectEventFunction function
= NULL
,
778 wxObject
* userData
= NULL
,
779 wxEvtHandler
* eventSink
= NULL
);
784 @name Binding and Unbinding
789 Binds the given function, functor or method dynamically with the event.
791 This offers basically the same functionality as Connect(), but it is
792 more flexible as it also allows you to use ordinary functions and
793 arbitrary functors as event handlers. It is also less restrictive then
794 Connect() because you can use an arbitrary method as an event handler,
795 whereas Connect() requires a wxEvtHandler derived handler.
797 See @ref overview_events_bind for more detailed explanation
798 of this function and the @ref page_samples_event sample for usage
802 The event type to be associated with this event handler.
804 The event handler functor. This can be an ordinary function but also
805 an arbitrary functor like boost::function<>.
807 The first ID of the identifier range to be associated with the event
810 The last ID of the identifier range to be associated with the event
813 Optional data to be associated with the event table entry.
814 wxWidgets will take ownership of this pointer, i.e. it will be
815 destroyed when the event handler is disconnected or at the program
816 termination. This pointer can be retrieved using
817 wxEvent::GetEventUserData() later.
819 @see @ref overview_cpp_rtti_disabled
823 template <typename EventTag
, typename Functor
>
824 void Bind(const EventTag
& eventType
,
827 int lastId
= wxID_ANY
,
828 wxObject
*userData
= NULL
);
831 See the Bind<>(const EventTag&, Functor, int, int, wxObject*) overload for
834 This overload will bind the given method as the event handler.
837 The event type to be associated with this event handler.
839 The event handler method. This can be an arbitrary method (doesn't need
840 to be from a wxEvtHandler derived class).
842 Object whose method should be called. It must always be specified
843 so it can be checked at compile time whether the given method is an
844 actual member of the given handler.
846 The first ID of the identifier range to be associated with the event
849 The last ID of the identifier range to be associated with the event
852 Optional data to be associated with the event table entry.
853 wxWidgets will take ownership of this pointer, i.e. it will be
854 destroyed when the event handler is disconnected or at the program
855 termination. This pointer can be retrieved using
856 wxEvent::GetEventUserData() later.
858 @see @ref overview_cpp_rtti_disabled
862 template <typename EventTag
, typename Class
, typename EventArg
, typename EventHandler
>
863 void Bind(const EventTag
&eventType
,
864 void (Class::*method
)(EventArg
&),
865 EventHandler
*handler
,
867 int lastId
= wxID_ANY
,
868 wxObject
*userData
= NULL
);
870 Unbinds the given function, functor or method dynamically from the
871 event handler, using the specified parameters as search criteria and
872 returning @true if a matching function has been found and removed.
874 This method can only unbind functions, functors or methods which have
875 been added using the Bind<>() method. There is no way to unbind
876 functions bound using the (static) event tables.
879 The event type associated with this event handler.
881 The event handler functor. This can be an ordinary function but also
882 an arbitrary functor like boost::function<>.
884 The first ID of the identifier range associated with the event
887 The last ID of the identifier range associated with the event
890 Data associated with the event table entry.
892 @see @ref overview_cpp_rtti_disabled
896 template <typename EventTag
, typename Functor
>
897 bool Unbind(const EventTag
& eventType
,
900 int lastId
= wxID_ANY
,
901 wxObject
*userData
= NULL
);
904 See the Unbind<>(const EventTag&, Functor, int, int, wxObject*)
905 overload for more info.
907 This overload unbinds the given method from the event..
910 The event type associated with this event handler.
912 The event handler method associated with this event.
914 Object whose method was called.
916 The first ID of the identifier range associated with the event
919 The last ID of the identifier range associated with the event
922 Data associated with the event table entry.
924 @see @ref overview_cpp_rtti_disabled
928 template <typename EventTag
, typename Class
, typename EventArg
, typename EventHandler
>
929 bool Unbind(const EventTag
&eventType
,
930 void (Class::*method
)(EventArg
&),
931 EventHandler
*handler
,
933 int lastId
= wxID_ANY
,
934 wxObject
*userData
= NULL
);
937 @name User-supplied data
942 Returns user-supplied client data.
944 @remarks Normally, any extra data the programmer wishes to associate with
945 the object should be made available by deriving a new class with
950 void* GetClientData() const;
953 Returns a pointer to the user-supplied client data object.
955 @see SetClientObject(), wxClientData
957 wxClientData
* GetClientObject() const;
960 Sets user-supplied client data.
963 Data to be associated with the event handler.
965 @remarks Normally, any extra data the programmer wishes to associate
966 with the object should be made available by deriving a new
967 class with new data members. You must not call this method
968 and SetClientObject on the same class - only one of them.
972 void SetClientData(void* data
);
975 Set the client data object. Any previous object will be deleted.
977 @see GetClientObject(), wxClientData
979 void SetClientObject(wxClientData
* data
);
985 @name Event handler chaining
987 wxEvtHandler can be arranged in a double-linked list of handlers
988 which is automatically iterated by ProcessEvent() if needed.
993 Returns @true if the event handler is enabled, @false otherwise.
995 @see SetEvtHandlerEnabled()
997 bool GetEvtHandlerEnabled() const;
1000 Returns the pointer to the next handler in the chain.
1002 @see SetNextHandler(), GetPreviousHandler(), SetPreviousHandler(),
1003 wxWindow::PushEventHandler, wxWindow::PopEventHandler
1005 wxEvtHandler
* GetNextHandler() const;
1008 Returns the pointer to the previous handler in the chain.
1010 @see SetPreviousHandler(), GetNextHandler(), SetNextHandler(),
1011 wxWindow::PushEventHandler, wxWindow::PopEventHandler
1013 wxEvtHandler
* GetPreviousHandler() const;
1016 Enables or disables the event handler.
1019 @true if the event handler is to be enabled, @false if it is to be disabled.
1021 @remarks You can use this function to avoid having to remove the event
1022 handler from the chain, for example when implementing a
1023 dialog editor and changing from edit to test mode.
1025 @see GetEvtHandlerEnabled()
1027 void SetEvtHandlerEnabled(bool enabled
);
1030 Sets the pointer to the next handler.
1033 See ProcessEvent() for more info about how the chains of event handlers
1034 are internally used.
1035 Also remember that wxEvtHandler uses double-linked lists and thus if you
1036 use this function, you should also call SetPreviousHandler() on the
1037 argument passed to this function:
1039 handlerA->SetNextHandler(handlerB);
1040 handlerB->SetPreviousHandler(handlerA);
1044 The event handler to be set as the next handler.
1047 @see @ref overview_events_processing
1049 virtual void SetNextHandler(wxEvtHandler
* handler
);
1052 Sets the pointer to the previous handler.
1053 All remarks about SetNextHandler() apply to this function as well.
1056 The event handler to be set as the previous handler.
1059 @see @ref overview_events_processing
1061 virtual void SetPreviousHandler(wxEvtHandler
* handler
);
1064 Unlinks this event handler from the chain it's part of (if any);
1065 then links the "previous" event handler to the "next" one
1066 (so that the chain won't be interrupted).
1068 E.g. if before calling Unlink() you have the following chain:
1069 @image html evthandler_unlink_before.png
1070 then after calling @c B->Unlink() you'll have:
1071 @image html evthandler_unlink_after.png
1078 Returns @true if the next and the previous handler pointers of this
1079 event handler instance are @NULL.
1083 @see SetPreviousHandler(), SetNextHandler()
1085 bool IsUnlinked() const;
1090 @name Global event filters.
1092 Methods for working with the global list of event filters.
1094 Event filters can be defined to pre-process all the events that happen
1095 in an application, see wxEventFilter documentation for more information.
1100 Add an event filter whose FilterEvent() method will be called for each
1101 and every event processed by wxWidgets.
1103 The filters are called in LIFO order and wxApp is registered as an
1104 event filter by default. The pointer must remain valid until it's
1105 removed with RemoveFilter() and is not deleted by wxEvtHandler.
1109 static void AddFilter(wxEventFilter
* filter
);
1112 Remove a filter previously installed with AddFilter().
1114 It's an error to remove a filter that hadn't been previously added or
1115 was already removed.
1119 static void RemoveFilter(wxEventFilter
* filter
);
1125 Method called by ProcessEvent() before examining this object event
1128 This method can be overridden to hook into the event processing logic
1129 as early as possible. You should usually call the base class version
1130 when overriding this method, even if wxEvtHandler itself does nothing
1131 here, some derived classes do use this method, e.g. wxWindow implements
1132 support for wxValidator in it.
1136 class MyClass : public BaseClass // inheriting from wxEvtHandler
1140 virtual bool TryBefore(wxEvent& event)
1142 if ( MyPreProcess(event) )
1145 return BaseClass::TryBefore(event);
1152 virtual bool TryBefore(wxEvent
& event
);
1155 Method called by ProcessEvent() as last resort.
1157 This method can be overridden to implement post-processing for the
1158 events which were not processed anywhere else.
1160 The base class version handles forwarding the unprocessed events to
1161 wxApp at wxEvtHandler level and propagating them upwards the window
1162 child-parent chain at wxWindow level and so should usually be called
1163 when overriding this method:
1165 class MyClass : public BaseClass // inheriting from wxEvtHandler
1169 virtual bool TryAfter(wxEvent& event)
1171 if ( BaseClass::TryAfter(event) )
1174 return MyPostProcess(event);
1181 virtual bool TryAfter(wxEvent
& event
);
1187 See wxIdleEvent::SetMode() for more info.
1191 /** Send idle events to all windows */
1194 /** Send idle events to windows that have the wxWS_EX_PROCESS_IDLE flag specified */
1195 wxIDLE_PROCESS_SPECIFIED
1202 This class is used for idle events, which are generated when the system becomes
1203 idle. Note that, unless you do something specifically, the idle events are not
1204 sent if the system remains idle once it has become it, e.g. only a single idle
1205 event will be generated until something else resulting in more normal events
1206 happens and only then is the next idle event sent again.
1208 If you need to ensure a continuous stream of idle events, you can either use
1209 wxIdleEvent::RequestMore method in your handler or call wxWakeUpIdle() periodically
1210 (for example from a timer event handler), but note that both of these approaches
1211 (and especially the first one) increase the system load and so should be avoided
1214 By default, idle events are sent to all windows, including even the hidden
1215 ones because they may be shown if some condition is met from their @c
1216 wxEVT_IDLE (or related @c wxEVT_UPDATE_UI) handler. The children of hidden
1217 windows do not receive idle events however as they can't change their state
1218 in any way noticeable by the user. Finally, the global wxApp object also
1219 receives these events, as usual, so it can be used for any global idle time
1222 If sending idle events to all windows is causing a significant overhead in
1223 your application, you can call wxIdleEvent::SetMode with the value
1224 wxIDLE_PROCESS_SPECIFIED, and set the wxWS_EX_PROCESS_IDLE extra window
1225 style for every window which should receive idle events, all the other ones
1226 will not receive them in this case.
1228 @beginEventTable{wxIdleEvent}
1229 @event{EVT_IDLE(func)}
1230 Process a @c wxEVT_IDLE event.
1237 @section sec_delayed_action Delayed Action Mechanism
1239 wxIdleEvent can be used to perform some action "at slightly later time".
1240 This can be necessary in several circumstances when, for whatever reason,
1241 something can't be done in the current event handler. For example, if a
1242 mouse event handler is called with the mouse button pressed, the mouse can
1243 be currently captured and some operations with it -- notably capturing it
1244 again -- might be impossible or lead to undesirable results. If you still
1245 want to capture it, you can do it from @c wxEVT_IDLE handler when it is
1246 called the next time instead of doing it immediately.
1248 This can be achieved in two different ways: when using static event tables,
1249 you will need a flag indicating to the (always connected) idle event
1250 handler whether the desired action should be performed. The originally
1251 called handler would then set it to indicate that it should indeed be done
1252 and the idle handler itself would reset it to prevent it from doing the
1255 Using dynamically connected event handlers things are even simpler as the
1256 original event handler can simply wxEvtHandler::Connect() or
1257 wxEvtHandler::Bind() the idle event handler which would only be executed
1258 then and could wxEvtHandler::Disconnect() or wxEvtHandler::Unbind() itself.
1261 @see @ref overview_events, wxUpdateUIEvent, wxWindow::OnInternalIdle
1263 class wxIdleEvent
: public wxEvent
1272 Static function returning a value specifying how wxWidgets will send idle
1273 events: to all windows, or only to those which specify that they
1274 will process the events.
1278 static wxIdleMode
GetMode();
1281 Returns @true if the OnIdle function processing this event requested more
1286 bool MoreRequested() const;
1289 Tells wxWidgets that more processing is required.
1291 This function can be called by an OnIdle handler for a window or window event
1292 handler to indicate that wxApp::OnIdle should forward the OnIdle event once
1293 more to the application windows.
1295 If no window calls this function during OnIdle, then the application will
1296 remain in a passive event loop (not calling OnIdle) until a new event is
1297 posted to the application by the windowing system.
1299 @see MoreRequested()
1301 void RequestMore(bool needMore
= true);
1304 Static function for specifying how wxWidgets will send idle events: to
1305 all windows, or only to those which specify that they will process the events.
1308 Can be one of the ::wxIdleMode values.
1309 The default is wxIDLE_PROCESS_ALL.
1311 static void SetMode(wxIdleMode mode
);
1316 // ============================================================================
1317 // Global functions/macros
1318 // ============================================================================
1320 /** @addtogroup group_funcmacro_events */
1324 A value uniquely identifying the type of the event.
1326 The values of this type should only be created using wxNewEventType().
1328 See the macro DEFINE_EVENT_TYPE() for more info.
1330 @see @ref overview_events_introduction
1332 typedef int wxEventType
;
1335 A special event type usually used to indicate that some wxEvent has yet
1338 wxEventType wxEVT_NULL
;
1340 wxEventType wxEVT_ANY
;
1343 Generates a new unique event type.
1345 Usually this function is only used by wxDEFINE_EVENT() and not called
1348 wxEventType
wxNewEventType();
1351 Define a new event type associated with the specified event class.
1353 This macro defines a new unique event type @a name associated with the
1358 wxDEFINE_EVENT(MY_COMMAND_EVENT, wxCommandEvent);
1360 class MyCustomEvent : public wxEvent { ... };
1361 wxDEFINE_EVENT(MY_CUSTOM_EVENT, MyCustomEvent);
1364 @see wxDECLARE_EVENT(), @ref overview_events_custom
1366 #define wxDEFINE_EVENT(name, cls) \
1367 const wxEventTypeTag< cls > name(wxNewEventType())
1370 Declares a custom event type.
1372 This macro declares a variable called @a name which must be defined
1373 elsewhere using wxDEFINE_EVENT().
1375 The class @a cls must be the wxEvent-derived class associated with the
1376 events of this type and its full declaration must be visible from the point
1377 of use of this macro.
1381 wxDECLARE_EVENT(MY_COMMAND_EVENT, wxCommandEvent);
1383 class MyCustomEvent : public wxEvent { ... };
1384 wxDECLARE_EVENT(MY_CUSTOM_EVENT, MyCustomEvent);
1387 #define wxDECLARE_EVENT(name, cls) \
1388 wxDECLARE_EXPORTED_EVENT(wxEMPTY_PARAMETER_VALUE, name, cls)
1391 Variant of wxDECLARE_EVENT() used for event types defined inside a shared
1394 This is mostly used by wxWidgets internally, e.g.
1396 wxDECLARE_EXPORTED_EVENT(WXDLLIMPEXP_CORE, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEvent)
1399 #define wxDECLARE_EXPORTED_EVENT( expdecl, name, cls ) \
1400 extern const expdecl wxEventTypeTag< cls > name;
1403 Helper macro for definition of custom event table macros.
1405 This macro must only be used if wxEVENTS_COMPATIBILITY_2_8 is 1, otherwise
1406 it is better and more clear to just use the address of the function
1407 directly as this is all this macro does in this case. However it needs to
1408 explicitly cast @a func to @a functype, which is the type of wxEvtHandler
1409 member function taking the custom event argument when
1410 wxEVENTS_COMPATIBILITY_2_8 is 0.
1412 See wx__DECLARE_EVT0 for an example of use.
1414 @see @ref overview_events_custom_ownclass
1416 #define wxEVENT_HANDLER_CAST(functype, func) (&func)
1419 This macro is used to define event table macros for handling custom
1424 class MyEvent : public wxEvent { ... };
1426 // note that this is not necessary unless using old compilers: for the
1427 // reasonably new ones just use &func instead of MyEventHandler(func)
1428 typedef void (wxEvtHandler::*MyEventFunction)(MyEvent&);
1429 #define MyEventHandler(func) wxEVENT_HANDLER_CAST(MyEventFunction, func)
1431 wxDEFINE_EVENT(MY_EVENT_TYPE, MyEvent);
1433 #define EVT_MY(id, func) \
1434 wx__DECLARE_EVT1(MY_EVENT_TYPE, id, MyEventHandler(func))
1438 wxBEGIN_EVENT_TABLE(MyFrame, wxFrame)
1439 EVT_MY(wxID_ANY, MyFrame::OnMyEvent)
1444 The event type to handle.
1446 The identifier of events to handle.
1448 The event handler method.
1450 #define wx__DECLARE_EVT1(evt, id, fn) \
1451 wx__DECLARE_EVT2(evt, id, wxID_ANY, fn)
1454 Generalized version of the wx__DECLARE_EVT1() macro taking a range of
1455 IDs instead of a single one.
1456 Argument @a id1 is the first identifier of the range, @a id2 is the
1457 second identifier of the range.
1459 #define wx__DECLARE_EVT2(evt, id1, id2, fn) \
1460 DECLARE_EVENT_TABLE_ENTRY(evt, id1, id2, fn, NULL),
1463 Simplified version of the wx__DECLARE_EVT1() macro, to be used when the
1464 event type must be handled regardless of the ID associated with the
1465 specific event instances.
1467 #define wx__DECLARE_EVT0(evt, fn) \
1468 wx__DECLARE_EVT1(evt, wxID_ANY, fn)
1471 Use this macro inside a class declaration to declare a @e static event table
1474 In the implementation file you'll need to use the wxBEGIN_EVENT_TABLE()
1475 and the wxEND_EVENT_TABLE() macros, plus some additional @c EVT_xxx macro
1478 Note that this macro requires a final semicolon.
1480 @see @ref overview_events_eventtables
1482 #define wxDECLARE_EVENT_TABLE()
1485 Use this macro in a source file to start listing @e static event handlers
1486 for a specific class.
1488 Use wxEND_EVENT_TABLE() to terminate the event-declaration block.
1490 @see @ref overview_events_eventtables
1492 #define wxBEGIN_EVENT_TABLE(theClass, baseClass)
1495 Use this macro in a source file to end listing @e static event handlers
1496 for a specific class.
1498 Use wxBEGIN_EVENT_TABLE() to start the event-declaration block.
1500 @see @ref overview_events_eventtables
1502 #define wxEND_EVENT_TABLE()
1505 In a GUI application, this function posts @a event to the specified @e dest
1506 object using wxEvtHandler::AddPendingEvent().
1508 Otherwise, it dispatches @a event immediately using
1509 wxEvtHandler::ProcessEvent(). See the respective documentation for details
1510 (and caveats). Because of limitation of wxEvtHandler::AddPendingEvent()
1511 this function is not thread-safe for event objects having wxString fields,
1512 use wxQueueEvent() instead.
1516 void wxPostEvent(wxEvtHandler
* dest
, const wxEvent
& event
);
1519 Queue an event for processing on the given object.
1521 This is a wrapper around wxEvtHandler::QueueEvent(), see its documentation
1527 The object to queue the event on, can't be @c NULL.
1529 The heap-allocated and non-@c NULL event to queue, the function takes
1532 void wxQueueEvent(wxEvtHandler
* dest
, wxEvent
*event
);