Jerome Breitenbach
2009-05-14 23:23:05 UTC
Subject: Event handling and wxMenuBar
I'm fairly new to wxWidgets, though I have worked through several
examples and have been reading the wxWidgets documentation. In
particular, I've come across the following paragraph at
http://docs.wxwidgets.org/trunk/overview_events.html#overview_events_eventhandling.
To use an event table you must first decide in which class you wish
to handle the events. The only requirement imposed by wxWidgets is
that this class must derive from wxEvtHandler and so, considering
that wxWindow derives from it, any classes representing windows can
handle events. Simple events such as menu commands are usually
processed at the level of a top-level window containing the menu, so
let's suppose that you need to handle some events in MyFrame class
deriving from wxFrame.
Despite the remark about what is *usually* done, I decided to try to
handle events generated by a wxMenuBar within my menu bar class itself.
This seems more natural to me, since the events would be handled nearest
to where they are generated. Moreover, irrespective of whether this is
the "preferred" method, wxMenuBar does derive from wxWindow, and thus
also from wxEvtHandler.
However, all my attempts at this approach failed, whether using an event
table or wxEvtHandler::Connect() within my wxMenuBar subclass. After
much searching, I found the reason for this failure stated at
http://wiki.wxwindows.org/Events#wxMenuBar_Events:
A wxMenuBar doesn't send events to itself, but to its parent
frame. This might seem inconsistent, but it's actually convenient in
all common situations.
OK, even though I don't yet understand the convenience of doing so, I'll
now use the usual method for placing handlers of wxCommandEvent's
generated by wxMenuBar. But, I'm still left with several questions:
* Given the paragraph quoted above, how should I have been able to
conclude from the main wxWidgets docs
http://docs.wxwidgets.org/trunk/, http://docs.wxwidgets.org/stable/,
and http://www.wxwidgets.org/docs/book/ that wxMenuBar does not
receive the events it generates?
* Since wxMenuBar does not receive the events it generates, I see no
purpose to doing either of the following:
- placing an event table in a wxMenuBar subclass object
- having a wxMenuBar subclass object call Connect()
Accordingly, shouldn't any wxMenuBar subclass that does one of these
things fail to compile (thereby alerting the programmer of a
problem)?
* What other subclasses of wxEvtHandler don't receive the events they
generate? For example, the messages
http://groups.google.com/group/comp.soft-sys.wxwindows/browse_thread/thread/95e8ffcd9e5faba2/19270633c74d0e1,
seems to imply that this is the case for wxGrid. Without being
critical, it seems to me that such important information should be
stated--indeed, emphasized--in a prominent place, such as
http://docs.wxwidgets.org/trunk/overview_events.html.
* The above messages also observe that wxGrid contains a function
GetGridWindow(), and suggest calling GetGridWindow()->Connect(...)
from a wxGrid subclass. Why, then, doesn't wxMenuBar contain a
similar function like GetWindow() whereby one might call
GetWindow()->Connect(...) from a wxMenuBar subclass?
Thanks for the help,
Jerome
I'm fairly new to wxWidgets, though I have worked through several
examples and have been reading the wxWidgets documentation. In
particular, I've come across the following paragraph at
http://docs.wxwidgets.org/trunk/overview_events.html#overview_events_eventhandling.
To use an event table you must first decide in which class you wish
to handle the events. The only requirement imposed by wxWidgets is
that this class must derive from wxEvtHandler and so, considering
that wxWindow derives from it, any classes representing windows can
handle events. Simple events such as menu commands are usually
processed at the level of a top-level window containing the menu, so
let's suppose that you need to handle some events in MyFrame class
deriving from wxFrame.
Despite the remark about what is *usually* done, I decided to try to
handle events generated by a wxMenuBar within my menu bar class itself.
This seems more natural to me, since the events would be handled nearest
to where they are generated. Moreover, irrespective of whether this is
the "preferred" method, wxMenuBar does derive from wxWindow, and thus
also from wxEvtHandler.
However, all my attempts at this approach failed, whether using an event
table or wxEvtHandler::Connect() within my wxMenuBar subclass. After
much searching, I found the reason for this failure stated at
http://wiki.wxwindows.org/Events#wxMenuBar_Events:
A wxMenuBar doesn't send events to itself, but to its parent
frame. This might seem inconsistent, but it's actually convenient in
all common situations.
OK, even though I don't yet understand the convenience of doing so, I'll
now use the usual method for placing handlers of wxCommandEvent's
generated by wxMenuBar. But, I'm still left with several questions:
* Given the paragraph quoted above, how should I have been able to
conclude from the main wxWidgets docs
http://docs.wxwidgets.org/trunk/, http://docs.wxwidgets.org/stable/,
and http://www.wxwidgets.org/docs/book/ that wxMenuBar does not
receive the events it generates?
* Since wxMenuBar does not receive the events it generates, I see no
purpose to doing either of the following:
- placing an event table in a wxMenuBar subclass object
- having a wxMenuBar subclass object call Connect()
Accordingly, shouldn't any wxMenuBar subclass that does one of these
things fail to compile (thereby alerting the programmer of a
problem)?
* What other subclasses of wxEvtHandler don't receive the events they
generate? For example, the messages
http://groups.google.com/group/comp.soft-sys.wxwindows/browse_thread/thread/95e8ffcd9e5faba2/19270633c74d0e1,
seems to imply that this is the case for wxGrid. Without being
critical, it seems to me that such important information should be
stated--indeed, emphasized--in a prominent place, such as
http://docs.wxwidgets.org/trunk/overview_events.html.
* The above messages also observe that wxGrid contains a function
GetGridWindow(), and suggest calling GetGridWindow()->Connect(...)
from a wxGrid subclass. Why, then, doesn't wxMenuBar contain a
similar function like GetWindow() whereby one might call
GetWindow()->Connect(...) from a wxMenuBar subclass?
Thanks for the help,
Jerome