\r
There are two principal ways to handle events in wxWidgets. One of them uses\r
<em>event table</em> macros and allows you to define the connection between events\r
-and their handlers only statically, i.e. during program compilation. The other\r
+and their handlers only statically, i.e., during program compilation. The other\r
one uses wxEvtHandler::Connect() call and can be used to connect, and\r
-disconnect, the handlers dynamically, i.e. during run-time depending on some\r
-conditions. It also allows directly connecting the events of one object to a\r
-handler method in another object while the static event tables can only handle\r
+disconnect, the handlers dynamically, i.e., during run-time depending on some\r
+conditions. It also allows the direct connection of the events of one object to a\r
+handler method in another object. The static event tables can only handle\r
events in the object where they are defined so using Connect() is more flexible\r
than using the event tables. On the other hand, event tables are more succinct\r
and centralize all event handlers connection in one place. You can either\r
-choose a single approach which you find preferable or freely combine both\r
+choose a single approach that you find preferable or freely combine both\r
methods in your program in different classes or even in one and the same class,\r
although this is probably sufficiently confusing to be a bad idea.\r
\r
-But before you make this choice, let us discuss these two ways in some more\r
-details: in the next section we provide a short introduction to handling the\r
-events using the event tables, please see @ref overview_eventhandling_connect\r
+But before you make this choice, let us discuss these two ways in more\r
+detail. In the next section we provide a short introduction to handling the\r
+events using the event tables. Please see @ref overview_eventhandling_connect\r
for the discussion of Connect().\r
\r
@section overview_eventhandling_eventtables Event Handling with Event Tables\r
containing the menu, so let's suppose that you need to handle some events in @c\r
MyFrame class deriving from wxFrame.\r
\r
-First thing to do is to define one or more <em>event handlers</em>. They\r
-are just simple (non-virtual) methods of the class which take as a parameter a\r
-reference to an object of wxEvent-derived class and have no return value (any\r
+First define one or more <em>event handlers</em>. They\r
+are just simple (non-virtual) methods of the class that take as a parameter a\r
+reference to an object of a wxEvent-derived class and have no return value (any\r
return information is passed via the argument, which is why it is non-const).\r
You also need to insert a macro\r
\r
DECLARE_EVENT_TABLE()\r
@endcode\r
\r
-somewhere in the class declaration. It doesn't matter where does it occur but\r
-it's customary to put it at the end of it because the macro changes the access\r
-type internally and so it's safest if there is nothing that follows it. So the\r
+somewhere in the class declaration. It doesn't matter where it appears but\r
+it's customary to put it at the end because the macro changes the access\r
+type internally so it's safest if nothing follows it. The\r
full class declaration might look like this:\r
\r
@code\r
int m_whatever;\r
\r
private:\r
- // notice that as the event handlers normally are not called from outside\r
- // the class, they normally be private, in particular they don't need at\r
- // all to be public\r
+ // Notice that as the event handlers normally are not called from outside\r
+ // the class, they normally are private. In particular they don't need\r
+ // to be public.\r
void OnExit(wxCommandEvent& event);\r
void OnButton1(wxCommandEvent& event);\r
void OnSize(wxSizeEvent& event);\r
\r
// it's common to call the event handlers OnSomething() but there is no\r
- // obligation to it, this one is an event handler too:\r
+ // obligation to do that; this one is an event handler too:\r
void DoTest(wxCommandEvent& event);\r
\r
DECLARE_EVENT_TABLE()\r
};\r
@endcode\r
\r
-Next the event table must be defined and, as any definition, it must be placed\r
-in an implementation file to tell. The event table tells wxWidgets how to map\r
+Next the event table must be defined and, as with any definition, it must be\r
+placed in an implementation file. The event table tells wxWidgets how to map\r
events to member functions and in our example it could look like this:\r
\r
@code\r
@endcode\r
\r
Notice that you must mention a method you want to use for the event handling in\r
-the event table definition, just defining it in MyFrame class is @e not enough.\r
+the event table definition; just defining it in MyFrame class is @e not enough.\r
\r
Let us now look at the details of this definition: the first line means that we\r
are defining the event table for MyFrame class and that its base class is\r
-wxFrame, so events not processed by MyFrame will, by default, be handled to\r
+wxFrame, so events not processed by MyFrame will, by default, be handled by\r
wxFrame. The next four lines define connections of individual events to their\r
handlers: the first two of them map menu commands from the items with the\r
identifiers specified as the first macro parameter to two different member\r
The EVT_BUTTON macro demonstrates that the originating event does not have to\r
come from the window class implementing the event table -- if the event source\r
is a button within a panel within a frame, this will still work, because event\r
-tables are searched up through the hierarchy of windows for the command events\r
-(but only command events, so you can't catch mouse move events in a child\r
+tables are searched up through the hierarchy of windows for the command events.\r
+(But only command events, so you can't catch mouse move events in a child\r
control in the parent window in the same way because wxMouseEvent doesn't\r
-derive from wxCommandEvent, see below for how you can do it). In this case, the\r
+derive from wxCommandEvent. See below for how you can do it.) In this case, the\r
button's event table will be searched, then the parent panel's, then the\r
frame's.\r
\r
event handlers take a wxEvent-derived argument whose exact class differs\r
according to the type of event and the class of the originating window. For\r
size events, wxSizeEvent is used. For menu commands and most control commands\r
-(such as button presses), wxCommandEvent is used. And when controls get more\r
+(such as button presses), wxCommandEvent is used. When controls get more\r
complicated, more specific wxCommandEvent-derived event classes providing\r
additional control-specific information can be used, such as wxTreeEvent for\r
events from wxTreeCtrl windows.\r
\r
In the simplest possible case an event handler may not use the @c event\r
-parameter at all, e.g.\r
+parameter at all. For example,\r
\r
@code\r
-void MyFrame::OnExit(wxCommandEvent&)\r
+void MyFrame::OnExit(wxCommandEvent& WXUNUSED(event))\r
{\r
// when the user selects "Exit" from the menu we should close\r
Close(true);\r
\r
@section overview_eventhandling_connect Dynamic Event Handling\r
\r
-As with the event tables, you need to decide in which class do you intend to\r
-handle the events first and, also as before, this class must still derive from\r
-wxEvtHandler (usually indirectly via wxWindow), see the declaration of MyFrame\r
+As with the event tables, decide in which class you intend to\r
+handle the events first and, as before, this class must derive from\r
+wxEvtHandler (usually indirectly via wxWindow). See the declaration of MyFrame\r
in the previous section. However the similarities end here and both the syntax\r
-and the possibilities of this way of handling events in this way are rather\r
-different.\r
-\r
+and the possibilities of handling events in this way are rather different.\r
+ \r
Let us start by looking at the syntax: the first obvious difference is that you\r
-don't need to use neither @c DECLARE_EVENT_TABLE() nor @c BEGIN_EVENT_TABLE and\r
-associated macros any more. Instead, in any place in your code, but usually in\r
-the code of the class defining the handlers itself (and definitely not in the\r
-global scope as with the event tables), you should call its Connect() method\r
-like this:\r
+need not use @c DECLARE_EVENT_TABLE() nor @c BEGIN_EVENT_TABLE and the\r
+associated macros. Instead, in any place in your code, but usually in\r
+the code of the class defining the handler itself (and definitely not in the\r
+global scope as with the event tables), call its Connect() method like this:\r
\r
@code\r
MyFrame::MyFrame(...)\r
@endcode\r
\r
This class should be self-explanatory except for wxCommandEventHandler part:\r
-this is a macro which ensures that the method is of correct type by using\r
-static_cast in the same way as event table macros do it inside them.\r
+this is a macro that ensures that the method is of the correct type by using\r
+static_cast in the same way as the event table macros.\r
\r
Now let us describe the semantic differences:\r
<ul>\r
<li>\r
- Event handlers can be connected at any moment, e.g. it's possible to do\r
- some initialization first and only connect the handlers if and when it\r
- succeeds. This can avoid the need to test that the object was properly\r
- initialized in the event handlers themselves: with Connect() they\r
- simply won't be called at all if it wasn't.\r
+ Event handlers can be connected at any moment. For example, it's possible\r
+ to do some initialization first and only connect the handlers if and when\r
+ it succeeds. This can avoid the need to test that the object was properly\r
+ initialized in the event handlers themselves. With Connect() they\r
+ simply won't be called if it wasn't correctly initialized.\r
</li>\r
\r
<li>\r
As a slight extension of the above, the handlers can also be\r
- Disconnect()-ed at any time. And maybe later reconnected again. Of\r
- course, it's also possible to emulate this behaviour with the classic\r
- static (i.e. connected via event tables) handlers by using an internal\r
+ Disconnect()-ed at any time and maybe later reconnected. Of course,\r
+ it's also possible to emulate this behaviour with the classic\r
+ static (i.e., connected via event tables) handlers by using an internal\r
flag indicating whether the handler is currently enabled and returning\r
from it if it isn't, but using dynamically connected handlers requires\r
less code and is also usually more clear.\r
<li>\r
Last but very, very far from least is the possibility to connect an\r
event of some object to a method of another object. This is impossible\r
- to do with event tables because there is no possibility to specify the\r
+ to do with event tables because it is not possible to specify the\r
object to dispatch the event to so it necessarily needs to be sent to\r
the same object which generated the event. Not so with Connect() which\r
- has an optional @c eventSink parameter which can be used to specify the\r
+ has an optional @c eventSink parameter that can be used to specify the\r
object which will handle the event. Of course, in this case the method\r
- being connected must belong to the class which is the type of the\r
+ being connected must belong to the class that is the type of the\r
@c eventSink object! To give a quick example, people often want to catch\r
mouse movement events happening when the mouse is in one of the frame\r
children in the frame itself. Doing it in a naive way doesn't work:\r
<li>\r
A @c EVT_LEAVE_WINDOW(MyFrame::OnMouseLeave) line in the frame\r
event table has no effect as mouse move (including entering and\r
- leaving) events are not propagated upwards to the parent window\r
+ leaving) events are not propagated up to the parent window\r
(at least not by default).\r
</li>\r
\r
Putting the same line in a child event table will crash during\r
run-time because the MyFrame method will be called on a wrong\r
object -- it's easy to convince oneself that the only object\r
- which can be used here is the pointer to the child, as\r
+ that can be used here is the pointer to the child, as\r
wxWidgets has nothing else. But calling a frame method with the\r
child window pointer instead of the pointer to the frame is, of\r
course, disastrous.\r
this); // this indicates the object to connect to\r
}\r
@endcode\r
- will work exactly as expected. Note that you can get the object which\r
- generated the event -- and which is not the same as the frame -- via\r
+ will work exactly as expected. Note that you can get the object that\r
+ generated the event -- and that is not the same as the frame -- via\r
wxEvent::GetEventObject() method of @c event argument passed to the\r
event handler.\r
</li>\r
@section overview_eventhandling_processing How Events are Processed\r
\r
The previous sections explain how to define event handlers but don't address\r
-the question of how exactly does wxWidgets find the handler to call for the\r
-given event. This section describes the algorithm used to do it in details.\r
+the question of how exactly wxWidgets finds the handler to call for the\r
+given event. This section describes the algorithm used in detail.\r
\r
When an event is received from the windowing system, wxWidgets calls\r
wxEvtHandler::ProcessEvent() on the first event handler object belonging to the\r
If this event handler is disabled via a call to\r
wxEvtHandler::SetEvtHandlerEnabled() the next three steps are skipped and\r
the event handler resumes at step (5).\r
- </li?\r
+ </li>\r
\r
<li value="2">\r
If the object is a wxWindow and has an associated validator, wxValidator\r
</li>\r
\r
<li value="3">\r
- The list of dynamically connected event handlers, i.e. those for which\r
+ The list of dynamically connected event handlers, i.e., those for which\r
Connect() was called, is consulted. Notice that this is done before\r
checking the static event table entries, so if both a dynamic and a static\r
event handler match the same event, the static one is never going to be\r
\r
<li value="5">\r
The event is passed to the next event handler, if any, in the event handler\r
- chain, i.e. the steps (1) to (4) are done for it. This chain can be formed\r
+ chain, i.e., the steps (1) to (4) are done for it. This chain can be formed\r
using wxEvtHandler::SetNextHandler() or wxWindow::PushEventHandler() but\r
usually there is no next event handler and chaining event handlers using\r
these functions is much less useful now that Connect() exists so this step\r
</li>\r
\r
<li value="7">\r
- Finally, i.e. if the event is still not processed, the wxApp object itself\r
+ Finally, i.e., if the event is still not processed, the wxApp object itself\r
gets a last chance to process it.\r
</li>\r
</ol>\r
\r
<em>Please pay close attention to step 6!</em> People often overlook or get\r
confused by this powerful feature of the wxWidgets event processing system. The\r
-details of event propagation upwards the window hierarchy are described in the\r
+details of event propagation up the window hierarchy are described in the\r
next section.\r
\r
Also please notice that there are additional steps in the event handling for\r
-the windows making part of wxWidgets document-view framework, i.e.\r
+the windows-making part of wxWidgets document-view framework, i.e.,\r
wxDocParentFrame, wxDocChildFrame and their MDI equivalents wxDocMDIParentFrame\r
-and wxDocMDIChildFrame. The parent frame classes modify the step (2) above to\r
+and wxDocMDIChildFrame. The parent frame classes modify step (2) above to\r
send the events received by them to wxDocManager object first. This object, in\r
turn, sends the event to the current view and the view itself lets its\r
-associated document to process the event first. The child frame classes send\r
+associated document process the event first. The child frame classes send\r
the event directly to the associated view which still forwards it to its\r
document object. Notice that to avoid remembering the exact order in which the\r
events are processed in the document-view frame, the simplest, and recommended,\r
-solution is to only handle the events at the view classes level, but not in the\r
+solution is to only handle the events at the view classes level, and not in the\r
document or document manager classes\r
\r
\r
wxCommandEvent are propagated by default to the parent window if they are not\r
processed in this window itself. But although by default only the command\r
events are propagated like this, other events can be propagated as well because\r
-the event handling code uses wxEvent::ShouldPropagate() to check for whether an\r
+the event handling code uses wxEvent::ShouldPropagate() to check whether an\r
event should be propagated. It is also possible to propagate the event only a\r
limited number of times and not until it is processed (or a top level parent\r
window is reached).\r
\r
Finally, there is another additional complication (which, in fact, simplifies\r
life of wxWidgets programmers significantly): when propagating the command\r
-events upwards to the parent window, the event propagation stops when it\r
-reaches the parent dialog, if any. This means that you don't risk to get\r
+events up to the parent window, the event propagation stops when it\r
+reaches the parent dialog, if any. This means that you don't risk getting\r
unexpected events from the dialog controls (which might be left unprocessed by\r
the dialog itself because it doesn't care about them) when a modal dialog is\r
popped up. The events do propagate beyond the frames, however. The rationale\r
for this choice is that there are only a few frames in a typical application\r
and their parent-child relation are well understood by the programmer while it\r
-may be very difficult, if not impossible, to track down all the dialogs which\r
+may be difficult, if not impossible, to track down all the dialogs that\r
may be popped up in a complex program (remember that some are created\r
automatically by wxWidgets). If you need to specify a different behaviour for\r
some reason, you can use wxWindow::SetExtraStyle(wxWS_EX_BLOCK_EVENTS)\r
explicitly to prevent the events from being propagated beyond the given window\r
-or unset this flag for the dialogs which have it on by default.\r
+or unset this flag for the dialogs that have it on by default.\r
\r
Typically events that deal with a window as a window (size, motion,\r
paint, mouse, keyboard, etc.) are sent only to the window. Events\r
-that have a higher level of meaning and/or are generated by the window\r
+that have a higher level of meaning or are generated by the window\r
itself, (button click, menu select, tree expand, etc.) are command\r
events and are sent up to the parent to see if it is interested in the event.\r
\r
As mentioned above, only command events are recursively applied to the parents\r
event handler in the library itself. As this quite often causes confusion for\r
-users, here is a list of system events which will @em not get sent to the\r
+users, here is a list of system events that will @em not get sent to the\r
parent's event handler:\r
\r
@li wxEvent: The event base class\r
\r
It may be noted that wxWidgets' event processing system implements something\r
close to virtual methods in normal C++ in spirit: both of these mechanisms\r
-allow to alter the behaviour of the base class by defining the event handling\r
+allow you to alter the behaviour of the base class by defining the event handling\r
functions in the derived classes.\r
\r
There is however an important difference between the two mechanisms when you\r
class function directly and you can do it either in the beginning of the\r
derived class handler function (to post-process the event) or at its end (to\r
pre-process the event). With the event handlers, you only have the option of\r
-pre-processing the events and in order to still let the default behaviour to\r
+pre-processing the events and in order to still let the default behaviour\r
happen you must call wxEvent::Skip() and @em not call the base class event\r
handler directly. In fact, the event handler probably doesn't even exist in the\r
base class as the default behaviour is often implemented in platform-specific\r
code by the underlying toolkit or OS itself. But even if it does exist at\r
-wxWidgets level, it should never be called directly as the event handler are\r
+wxWidgets level, it should never be called directly as the event handlers are\r
not part of wxWidgets API and should never be called directly.\r
\r
Finally, please notice that the event handlers themselves shouldn't be virtual.\r
@section overview_eventhandling_prog User Generated Events vs Programmatically Generated Events\r
\r
While generically wxEvents can be generated both by user\r
-actions (e.g. resize of a wxWindow) and by calls to functions\r
-(e.g. wxWindow::SetSize), wxWidgets controls normally send wxCommandEvent-derived\r
+actions (e.g., resize of a wxWindow) and by calls to functions\r
+(e.g., wxWindow::SetSize), wxWidgets controls normally send wxCommandEvent-derived\r
events only for the user-generated events. The only @b exceptions to this rule are:\r
\r
@li wxNotebook::AddPage: No event-free alternatives\r
Window identifiers are integers, and are used to\r
uniquely determine window identity in the event system (though you can use it\r
for other purposes). In fact, identifiers do not need to be unique\r
-across your entire application just so long as they are unique within a\r
+across your entire application as long they are unique within the\r
particular context you're interested in, such as a frame and its children. You\r
may use the @c wxID_OK identifier, for example, on any number of dialogs so\r
long as you don't have several within the same dialog.\r
\r
@subsection overview_eventhandling_custom_ownclass Defining Your Own Event Class\r
\r
-Under certain circumstances, it will be required to define your own event\r
-class e.g. for sending more complex data from one place to another. Apart\r
+Under certain circumstances, you must define your own event\r
+class e.g., for sending more complex data from one place to another. Apart\r
from defining your event class, you will also need to define your own\r
event table macro (which is quite long). Watch out to put in enough\r
casts to the inherited event function. Here is an example:\r