Custom wxWidgets Events
Date: Tuesday, August 03 @ 05:45:00 CDT
Topic: Tutorials


One of the scarier parts of wx seems to be custom events. After all, what's friendly about macro functions?! Fortunately, creating brand-new events isn't that difficult at all. This tutorial will show you steps I used to create EVT_ROOT_EXPANDING in wxTreeMultiCtrl, hope(TM)fully in a way that will help you understand how to create custom events in general.

Step 1: n your header you need to declare your custom event type. It needs to be outside the class {}; declaration and probably near the top of the file. extern const wxEventType wxEVT_NODE_EXPANDED Similarly, in the file's cpp file, you need: const wxEventType wxEVT_NODE_EXPANDED = wxNewEventType(); Step 2: You need to declare EVT_ROOT_EXPANDED for use in the stock BEGIN_EVENT_TABLE() / END_EVENT_TABLE() macros. In the header, stick an edited version of this in there, below the code for Step 1: #define EVT_NODE_EXPANDED(id, fn) DECLARE_EVENT_TABLE_ENTRY( wxEVT_NODE_EXPANDED, id, -1, (wxObjectEventFunction)(wxEventFunction)(wxNotifyEventFunction)&fn, (wxObject *) NULL ), Step 3: Create and post your custom event in your source code. For instance, I wanted to post an event whenever the control's root was expanded, so i used the following code: wxNotifyEvent event(wxEVT_ROOT_EXPANDED); wxPostEvent(this, event); Step 4 (optional): For really unique events, the stock event class, wxNotifyEvent and wxCommandEvent just won't provide you with what you want. Say you want a custom value that is associated with your custom control, for instance. In my case, I wanted the wxTreeMultiItem of the root node that was being expanded. For situations such as these, you must create a custom event class. class wxTreeMultiEvent: public wxNotifyEvent { private: wxTreeMultiItem m_item; public: wxTreeMultiEvent(const wxEventType& event): wxNotifyEvent(event) { } virtual wxEvent *Clone() const { return new wxTreeMultiEvent(*this); } wxTreeMultiItem GetItem() const { return m_item; } wxString GetLabel() const { return m_item.GetLabel(); } void SetItem(wxTreeMultiItem item) { m_item = item; } }; Right before your work in Step 2, you should place: typedef void (wxEvtHandler::*wxTreeMultiEventFunction)(wxTreeMultiEvent&); Then, you must change line 4 of Step 2 to (wxObjectEventFunction)(wxEventFunction)(wxTreeMultiEventFunction)&fn, You must also pass the event in Step 3 any additional information, such as in the following code: wxTreeMultiEvent event(wxEVT_ROOT_EXPANDED); event.SetItem(item); wxPostEvent(this, event); That's pretty much it. Leave me a note if this is helpful :-)
-hope

I have sworn upon the altar of God, eternal hostility against every form of tyranny over the mind of man.
-Thomas Jefferson





This article comes from xMule: A P2P Client derived from eMule
http://www.xmule.ws/phpnuke

The URL for this story is:
http://www.xmule.ws/phpnuke/modules.php?name=News&file=article&sid=76