Custom Event Handlers - value of variable - Example
Custom Event Handlers - value of variable - Example
Hi,
I have a scenario wherein a set of data is being shared by different GUI objects.
If I change the data, I would like to invoke a custom event, that would be handled by the different GUI objects and they would take action appropriately( say, repaint themselves ..) .
How could I implement the same in wxWindows.
I came across the EVT_CUSTOM macro, and was wondering if this could fit my need as mentioned above.
If so, how could i do that ?
--
Karthik
I have a scenario wherein a set of data is being shared by different GUI objects.
If I change the data, I would like to invoke a custom event, that would be handled by the different GUI objects and they would take action appropriately( say, repaint themselves ..) .
How could I implement the same in wxWindows.
I came across the EVT_CUSTOM macro, and was wondering if this could fit my need as mentioned above.
If so, how could i do that ?
--
Karthik
- Ryan Norton
- Moderator
- Posts: 1319
- Joined: Mon Aug 30, 2004 6:01 pm
Re: Custom Event Handlers - value of variable - Example
I'm not sure an event is the best idea for what you are trying to achieve - a timer would probably work better.akkumar wrote: If I change the data, I would like to invoke a custom event, that would be handled by the different GUI objects and they would take action appropriately( say, repaint themselves ..) .
Exactly what do you mean by changing the data? You mean a member variable?
[Mostly retired moderator, still check in to clean up some stuff]
Re: Custom Event Handlers - value of variable - Example
I would be refreshing a canvas based on the event. So if I have a timer, I would be refreshing the canvas frequently ( it has tons of opengl calls). I would not want to refresh so frequently since that would be visually unpleasant.Ryan Norton wrote:I'm not sure an event is the best idea for what you are trying to achieve - a timer would probably work better.akkumar wrote: If I change the data, I would like to invoke a custom event, that would be handled by the different GUI objects and they would take action appropriately( say, repaint themselves ..) .
True, I have a member variable on which three different canvas operate on. When this particular member variable is changed ( a simple SetVal ) function, I would like to raise a custom event that would be handled by all the 3 canvas in picture and they would refresh accordingly.Ryan Norton wrote: Exactly what do you mean by changing the data? You mean a member variable?
- Ryan Norton
- Moderator
- Posts: 1319
- Joined: Mon Aug 30, 2004 6:01 pm
Re: Custom Event Handlers - value of variable - Example
In this case, it's probably better to have some kind of manager class that the canvas would call in SetVal then.True, I have a member variable on which three different canvas operate on. When this particular member variable is changed ( a simple SetVal ) function, I would like to raise a custom event that would be handled by all the 3 canvas in picture and they would refresh accordingly.
Events are best used when you don't know what kind of class the event is being sent to (just that it's a derivative of wxEvtHandler). In order to have the classes handle the event, you need to keep track of the classes and call wxPostEvent for each one - and when you do this it has to go through wxWidget's internal message loop at a low priority. Not only would this be more difficult to implement then having each of the canvases keep track of each other, it would be slower too

Another way if you don't like the canvases or manager class idea is to have a manager class that goes through the RTTI table of the canvases' parent and calls a refresh method on each of the children.
[Mostly retired moderator, still check in to clean up some stuff]
- Ryan Norton
- Moderator
- Posts: 1319
- Joined: Mon Aug 30, 2004 6:01 pm
For completeness - If you really want to use custom events - here's what you'd do -
Code: Select all
//in .h
class WXDLLEXPORT wxMyEvent : public wxNotifyEvent
{
//...
DECLARE_DYNAMIC_CLASS(wxMyEvent)
};
#define wxMY_EVENT_ID 11000
DECLARE_EVENT_TYPE(wxEVT_MY_NOTIFY, wxMY_EVENT_ID)
typedef bool (wxEvtHandler::*wxMyEventFunction)(wxMyEvent&);
#define EVT_MY_NOTIFY(winid, fn) DECLARE_EVENT_TABLE_ENTRY( wxEVT_MY_NOTIFY, winid, wxID_ANY, (wxObjectEventFunction) (wxEventFunction) (wxMyEventFunction) & fn, (wxObject *) NULL ),
//in .cpp
IMPLEMENT_DYNAMIC_CLASS(wxMyEvent, wxEvent);
DEFINE_EVENT_TYPE(wxEVT_MY_NOTIFY);
[Mostly retired moderator, still check in to clean up some stuff]
This sounds a bit like the observer/observable tactic in Java.
I do not understand why you should use a event, when a simple message would do.
If the dataset inherits a observable class (which job it is to keep track of the observers, and to notify them when the data change) and other classes inherits a observer class, with a virtual dataChange() function or something, which would be called for all observers when the observable class have changed.
I wish I could draw this
I do not understand why you should use a event, when a simple message would do.
If the dataset inherits a observable class (which job it is to keep track of the observers, and to notify them when the data change) and other classes inherits a observer class, with a virtual dataChange() function or something, which would be called for all observers when the observable class have changed.
I wish I could draw this

I can recall someone being busy implementing just that in wxWidgets.. Maybe searching for observer will help you. The concept sounded interesting, I would use it myself when I need it. Observer patterns are very easy for data driven models ..
Edit of this post: http://forums.wxwidgets.org/viewtopic.p ... t=observer
The observer pattern
- Jorgen
Edit of this post: http://forums.wxwidgets.org/viewtopic.p ... t=observer
The observer pattern

- Jorgen
Forensic Software Engineer
Netherlands Forensic Insitute
http://english.forensischinstituut.nl/
-------------------------------------
Jorg's WasteBucket
http://www.xs4all.nl/~jorgb/wb
Netherlands Forensic Insitute
http://english.forensischinstituut.nl/
-------------------------------------
Jorg's WasteBucket
http://www.xs4all.nl/~jorgb/wb