 |
Who's Online |
 |
|
There are currently, 2 guest(s) and 0 member(s) that are online.
You are Anonymous user. You can register for free by clicking here
|
|
|
 |
| |
Tutorials: Custom wxWidgets Events
Posted by HopeSeekr on Tuesday, August 03 @ 05:45:00 CDT
Contributed by HopeSeekr
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
|
|
| |
 |
Login |
 |
|
|
Don't have an account yet? You can create one. As a registered user you have some advantages like theme manager, comments configuration and post comments with your name.
|
|
|
 |
 |
Article Rating |
 |
|
Average Score: 3.83 Votes: 18

|
|
|
 |
|
Associated Topics
 |
|
|
No Comments Allowed for Anonymous, please register |
|
|