Beginner-trouble understanding events Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
arunlfc
In need of some credit
In need of some credit
Posts: 7
Joined: Fri Jul 03, 2009 8:37 am

Beginner-trouble understanding events

Post by arunlfc » Fri Jul 03, 2009 8:53 am

Hi all, my first post here.
I'm a beginner with wxWidgets (started just yesterday) and i'm having some problem understanding event-handling.
I read the http://docs.wxwidgets.org/stable/wx_eve ... rview.html document, but i am still confused.
The greatest source of confusion, i think, is the number of different classes that come in play.
Please explain to me how the follwing classes are related to event handling, and also the various macros:
wxEvtHandler, wxEvent, wxCommandEvent

I know that when a button is pressed a wxCommandEvent object is created.

If anyone could take the time to explain in detail so i can grasp the logic behind event-processing (Only static event-handling for the time-being).

I really want to get going with wxWidgets (spent quite some time on command-line apps with c++).

Thanks,
Arun.[/url]

DavidHart
Site Admin
Site Admin
Posts: 3884
Joined: Thu Jan 12, 2006 6:23 pm
Location: IoW, UK

Post by DavidHart » Fri Jul 03, 2009 10:59 am

Hi Arun,

First, all wx events are instances of class wxEvent, or derived from it e.g. wxCommandEvent.

Second, wxEvtHandler. You don't really need to worry about this. From it is derived wxWindow, the base class of all controls; so whenever a control generates an event e.g. when a button is clicked, it has the ability to handle the resulting event.

So, a simple case: an unsubclassed wxButton will receive a commandevent when it's clicked. (It may use it internally to change to its "I've been clicked" appearance, I'm not sure.) If it's not handled, or if it's handled but wxEvent::Skip is called, the event is then passed to the various things from which wxButton is derived: wxControl, wxWindow, wxEvtHandler. None of these are likely to care, but they get notified anyway ;) .
Since a button-click event is a wxCommandEvent, if it hasn't been handled yet, or is handled and Skip()ed, it then gets passed to the parent control for more processing. e.g. if the button is on a wxPanel, which is on a wxFrame, then it's sent to the panel, then the frame (and then finally to wxApp).

If you subclass wxButton, the same sort of things will happen; but if you catch the event in the derived class's event table, you get the chance to process it first, before wxButton etc see it. This isn't likely to be very useful for a button, but it is for other controls.

I hope this makes things a bit clearer. If not, look at the samples that come with the wxWidgets source; start with the 'minimal' sample. Many of these show how events can be used in practice.

Regards,

David

arunlfc
In need of some credit
In need of some credit
Posts: 7
Joined: Fri Jul 03, 2009 8:37 am

Post by arunlfc » Fri Jul 03, 2009 12:54 pm

thanks David, been very helpful so far.

How are the macros used?

What is the difference between the macros:
wxEVT_BUTTON and wxEVT_COMMAND_BUTTON_CLICKED ?

DavidHart
Site Admin
Site Admin
Posts: 3884
Joined: Thu Jan 12, 2006 6:23 pm
Location: IoW, UK

Post by DavidHart » Fri Jul 03, 2009 3:05 pm

It can get a bit complicated :( .

wxButton is a C++ class.

EVT_BUTTON (not wxEVT_BUTTON) is a macro, defined in include/wx/event.h:
#define EVT_BUTTON(winid, func) wx__DECLARE_EVT1(wxEVT_COMMAND_BUTTON_CLICKED, winid, wxCommandEventHandler(func))
You'll use it in an event table.

wxEVT_COMMAND_BUTTON_CLICKED isn't a macro, it's an enum member that acts as a label for the type of event. This is also defined in event.h:
DECLARE_EVENT_TYPE(wxEVT_COMMAND_BUTTON_CLICKED, 1)
In other words, when the wx-devs designed wxButton, they wanted some way to label the wxCommandEvent that it sends as being a button-click, not some other commandevent; so they labelled it with the enum member wxEVT_COMMAND_BUTTON_CLICKED. It's the first parameter of the wxCommandEvent ctor (for a button event):
wxCommandEvent(WXTYPE commandEventType = 0, int id = 0)
The only times you'd need to use this is if you're using Connect() instead of an event table; or if you're creating a custom event.

arunlfc
In need of some credit
In need of some credit
Posts: 7
Joined: Fri Jul 03, 2009 8:37 am

Post by arunlfc » Sat Jul 04, 2009 5:16 am

Thanks a lot David.
It's much clearer now. I hope that with time it becomes much much clearer! :)

vdell
Moderator
Moderator
Posts: 536
Joined: Fri Jan 07, 2005 3:44 pm
Location: Finland
Contact:

Post by vdell » Sat Jul 04, 2009 10:42 am

arunlfc wrote:Thanks a lot David.
It's much clearer now. I hope that with time it becomes much much clearer! :)
Hey Arun,

Please remember to click "Accept" for the post that helped you to solve your problem. Thanks!
Visual C++ 9.0 / Windows XP Pro SP3 / wxWidgets 2.9.0 (SVN) | Colligere

Post Reply