Custom Event Handlers - value of variable - Example

Are you writing your own components and need help with how to set them up or have questions about the components you are deriving from ? Ask them here.
Post Reply
akkumar
Earned a small fee
Earned a small fee
Posts: 15
Joined: Sat Sep 11, 2004 8:35 pm
Location: CA

Custom Event Handlers - value of variable - Example

Post by akkumar »

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
User avatar
Ryan Norton
wxWorld Domination!
wxWorld Domination!
Posts: 1319
Joined: Mon Aug 30, 2004 6:01 pm

Re: Custom Event Handlers - value of variable - Example

Post by Ryan Norton »

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 ..) .
I'm not sure an event is the best idea for what you are trying to achieve - a timer would probably work better.

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]
akkumar
Earned a small fee
Earned a small fee
Posts: 15
Joined: Sat Sep 11, 2004 8:35 pm
Location: CA

Re: Custom Event Handlers - value of variable - Example

Post by akkumar »

Ryan Norton wrote:
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 ..) .
I'm not sure an event is the best idea for what you are trying to achieve - a timer would probably work better.
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: Exactly what do you mean by changing the data? You mean a member variable?
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.
User avatar
Ryan Norton
wxWorld Domination!
wxWorld Domination!
Posts: 1319
Joined: Mon Aug 30, 2004 6:01 pm

Re: Custom Event Handlers - value of variable - Example

Post by Ryan Norton »

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.
In this case, it's probably better to have some kind of manager class that the canvas would call in SetVal then.

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]
User avatar
Ryan Norton
wxWorld Domination!
wxWorld Domination!
Posts: 1319
Joined: Mon Aug 30, 2004 6:01 pm

Post by Ryan Norton »

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]
akkumar
Earned a small fee
Earned a small fee
Posts: 15
Joined: Sat Sep 11, 2004 8:35 pm
Location: CA

Post by akkumar »

Thanks Ryan for the detailed help and design suggestions
Karthik.
Daramarak
In need of some credit
In need of some credit
Posts: 6
Joined: Sun Nov 21, 2004 7:51 pm
Location: Norway

Post by Daramarak »

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 #-o
Jorg
Moderator
Moderator
Posts: 3971
Joined: Fri Aug 27, 2004 9:38 pm
Location: Delft, Netherlands
Contact:

Post by Jorg »

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
Forensic Software Engineer
Netherlands Forensic Insitute
http://english.forensischinstituut.nl/
-------------------------------------
Jorg's WasteBucket
http://www.xs4all.nl/~jorgb/wb
Post Reply