I could use some help with custom events...

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
ouch67
Earned some good credits
Earned some good credits
Posts: 134
Joined: Sun Mar 23, 2008 12:09 am

I could use some help with custom events...

Post by ouch67 » Thu Jul 28, 2011 5:33 pm

So here is my code so far:

Code: Select all

class wxTimerLoopEvent: public wxEvent
{
public:
    wxTimerLoopEvent( wxEventType commandType = wxEVT_NULL);

    // accessors
    //wxPlotCurve *GetCurve()
    //{ return m_curve; }

    // required for sending with wxPostEvent()
    //wxEvent* Clone();

//private:
    int test;
};

DECLARE_EVENT_TYPE( wxEVT_TIMERLOOP_TRIGGER, -1)

//typedef void(wxEvtHandler::*wxTimerLoopEventFunction)(wxEVT_TIMERLOOP_TRIGGER&);

#define EVT_TIMERLOOP(id, fn) \
    DECLARE_EVENT_TABLE_ENTRY( wxEVT_TIMERLOOP_TRIGGER, id, -1, \
    (wxObjectEventFunction) (wxEventFunction) (wxCommandEventFunction) (wxNotifyEventFunction) \
    wxStaticCastEvent( wxTimerLoopEventFunction, & fn ), (wxObject *) NULL ),

IMPLEMENT_DYNAMIC_CLASS( wxTimerLoopEvent, wxCommandEvent ) //error here
DEFINE_EVENT_TYPE( EVT_TIMERLOOP )

wxTimerLoopEvent::wxTimerLoopEvent( wxEventType event)
{
	SetEventType( EVT_TIMERLOOP );
	//SetEventObject( event );
}
it's then bound with:

Code: Select all

Bind(wxEVT_TIMERLOOP_TRIGGER, &FunctionWrap, event_id);
this is what the error says: 1400 error: ISO C++ does not permit 'wxEvent::ms_classInfo' to be defined as 'wxTimerLoopEvent::ms_classInfo'

I don't really understand the process of creating a custom event. I've seen a few wiki's on the matter but they just tell you to write this, write that, and your done. None seem to actually tell you what the code sections are for or why this section of code is needed. to make matters worse only one shows you an example using the bind function instead of event tables.

I guess my real problem is that I really don't understand why setting up an custom event requires around 2 pages of code... it just seems so wrong, and it's frustrating me to no end.

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

Re: I could use some help with custom events...

Post by DavidHart » Thu Jul 28, 2011 7:46 pm

Hi,
IMPLEMENT_DYNAMIC_CLASS( wxTimerLoopEvent, wxCommandEvent ) //error here
That's because you don't have a
DECLARE_DYNAMIC_CLASS(wxTimerLoopEvent)
in the class definition.

Regards,

David

ouch67
Earned some good credits
Earned some good credits
Posts: 134
Joined: Sun Mar 23, 2008 12:09 am

Re: I could use some help with custom events...

Post by ouch67 » Thu Jul 28, 2011 8:27 pm

ok, well I added that and now I get:

1401 error: cannot allocate an object of abstract type 'wxTimerLoopEvent'

which mean what? ;)

User avatar
doublemax
Moderator
Moderator
Posts: 14519
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: I could use some help with custom events...

Post by doublemax » Thu Jul 28, 2011 8:40 pm

wxEvent::Clone() is pure virtual, so you must implement it.
Use the source, Luke!

ouch67
Earned some good credits
Earned some good credits
Posts: 134
Joined: Sun Mar 23, 2008 12:09 am

Re: I could use some help with custom events...

Post by ouch67 » Fri Jul 29, 2011 4:29 pm

ok, thanks for the help guys, but I'm still having problems. Here is the code thus far:

Code: Select all

class wxTimerLoopEvent: public wxCommandEvent
{
public:
    wxTimerLoopEvent( wxEventType commandType = wxEVT_NULL);
    DECLARE_DYNAMIC_CLASS(wxTimerLoopEvent)

    wxTimerLoopEvent( const wxTimerLoopEvent &event ) :  wxCommandEvent(event) {test = 1;}

    wxEvent* Clone() const { return new wxTimerLoopEvent(*this); }
    // accessors
    //wxPlotCurve *GetCurve()
    //{ return m_curve; }

    // required for sending with wxPostEvent()
    //wxEvent* Clone();

//private:
    int test;
};

DECLARE_EVENT_TYPE( wxEVT_TIMERLOOP_TRIGGER, -1)

//typedef void(wxEvtHandler::*wxTimerLoopEventFunction)(wxEVT_TIMERLOOP_TRIGGER&);

#define EVT_TIMERLOOP(id, fn) \
    DECLARE_EVENT_TABLE_ENTRY( wxEVT_TIMERLOOP_TRIGGER, id, -1, \
    (wxObjectEventFunction) (wxEventFunction) (wxCommandEventFunction) (wxNotifyEventFunction) \
    wxStaticCastEvent( wxTimerLoopEventFunction, & fn ), (wxObject *) NULL ),

IMPLEMENT_DYNAMIC_CLASS( wxTimerLoopEvent, wxCommandEvent )
DEFINE_EVENT_TYPE( EVT_TIMERLOOP )

wxTimerLoopEvent::wxTimerLoopEvent( wxEventType event)
{
	SetEventType( EVT_TIMERLOOP );
	//SetEventObject( event );
}
and it's being called with:

Code: Select all

Bind(wxEVT_TIMERLOOP_TRIGGER, &FunctionWrap, event_id);
I'm getting this error now:

C:\wxwidgets\wxWidgets\include\wx\event.h|448|error: invalid conversion from 'wxEventFunctorFunction<int, wxTimerLoopEvent>::EventClass*' to 'wxTimerLoopEvent*'|

which tells me I'm not initializing wxEVT_TIMERLOOP_TRIGGER properly...

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

Re: I could use some help with custom events...

Post by DavidHart » Fri Jul 29, 2011 5:46 pm

There's a working example of how to do all this (except Bind(), which I must get round to adding) at http://wiki.wxwidgets.org/Custom_Events ... -_Method_4

Post Reply