Custom events revisited

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
mr1x
Knows some wx things
Knows some wx things
Posts: 34
Joined: Mon Jan 05, 2009 3:24 am

Custom events revisited

Post by mr1x » Thu Mar 12, 2009 1:00 am

I see that there is another question on custom events, but mine is even more basic, perhaps.

Incidentally, I also have a kind of communication event I'd like to define. I defined everything according to "cookbook" (and it looks like it matches the code in other post too). However, my custom event is not being delivered to the main frame. If I replace it with standard wxCommandEvent, everything works just fine of course.

Here is the definition in include file:

Code: Select all

BEGIN_DECLARE_EVENT_TYPES()
   DECLARE_EVENT_TYPE(wxEVT_COMM_EVENT, -1);
END_DECLARE_EVENT_TYPES()

class commEvent;
typedef void (wxEvtHandler::*CommEventFunction)(commEvent&);

#define EVT_COMM(id, fn)\
    DECLARE_EVENT_TABLE_ENTRY(wxEVT_COMM_EVENT, id, wxID_ANY, (wxObjectEventFunction) (wxEventFunction) (CommEventFunction) & fn, (wxObject *) NULL ), 

enum {
   COMM_Test
};


class commEvent : public wxEvent
{
public:
   commEvent(int id);
   commEvent *Clone() const { return new commEvent(*this); }
};
Here is the definition in CPP file:

Code: Select all

DEFINE_EVENT_TYPE(wxEVT_COMM_EVENT);

commEvent::commEvent(int id) : wxEvent(wxEVT_COMM_EVENT, id)
{
   printf("Event type %d\n", wxEVT_COMM_EVENT);
}


BEGIN_EVENT_TABLE(commEventFrame, wxFrame)
   EVT_COMM(COMM_Test, commEventFrame::OnTest)
END_EVENT_TABLE()
Here is the event handler attached to the main frame:

Code: Select all

void commEventFrame::OnTest(commEvent &event)
{
   printf("Received test event: %d\n", event.GetId());
}
And finally here is how it is being sent:

Code: Select all

   commEvent evt(COMM_Test);
   wxPostEvent(mainFrame, evt);
So, I must be missing something - but what?

Auria
Site Admin
Site Admin
Posts: 6695
Joined: Thu Sep 28, 2006 12:23 am
Contact:

Post by Auria » Thu Mar 12, 2009 1:31 am

DECLARE_EVENT_TYPE does not go inside the event table, it is a declaration so goes in a header. In your event table, put EVT_COMMAND(id, your event's name, callback)

BTW event tables usually don't go in headers.
Also check the wiki, there's examples

mr1x
Knows some wx things
Knows some wx things
Posts: 34
Joined: Mon Jan 05, 2009 3:24 am

Post by mr1x » Thu Mar 12, 2009 2:11 am

Auria wrote:DECLARE_EVENT_TYPE does not go inside the event table, it is a declaration so goes in a header.
It is in a header, it is not in the event table (it is surrounded by BEGIN_DECLARE_EVENT_TYPES()/END_DECLARE_EVENT_TYPES(), not by event table BEGIN_EVENT_TABLE/END_EVENT_TABLE
In your event table, put EVT_COMMAND(id, your event's name, callback)
but it is not a command event. I specifically used the example to define a specialized event table entry EVT_COMM. I don't want to use command events (I can do this now) but rather my own event type, custom defined - to be able to carry specific data (and to avoid the wxString copy silliness in wxCommandEvent).
BTW event tables usually don't go in headers.
as I said, it is not there - it is in the CPP file.
Also check the wiki, there's examples
That's where I copied the example from.

mr1x
Knows some wx things
Knows some wx things
Posts: 34
Joined: Mon Jan 05, 2009 3:24 am

Post by mr1x » Thu Mar 12, 2009 3:29 am

Found the problem, I'll leave it here in case anyone else hits it. Examples use wxCommandEvent(commandType, id) - note the order of arguments to a constructor.

I used wxEvent(id, commandType) :)

It would be nice to have a consistent calling convention to event constructors, or at least different types for commandType and id. Orthogonal APIs make for good software.

Auria
Site Admin
Site Admin
Posts: 6695
Joined: Thu Sep 28, 2006 12:23 am
Contact:

Post by Auria » Thu Mar 12, 2009 2:28 pm

mr1x wrote:
Auria wrote:DECLARE_EVENT_TYPE does not go inside the event table, it is a declaration so goes in a header.
It is in a header, it is not in the event table (it is surrounded by BEGIN_DECLARE_EVENT_TYPES()/END_DECLARE_EVENT_TYPES(), not by event table BEGIN_EVENT_TABLE/END_EVENT_TABLE
In your event table, put EVT_COMMAND(id, your event's name, callback)
but it is not a command event. I specifically used the example to define a specialized event table entry EVT_COMM. I don't want to use command events (I can do this now) but rather my own event type, custom defined - to be able to carry specific data (and to avoid the wxString copy silliness in wxCommandEvent).
BTW event tables usually don't go in headers.
as I said, it is not there - it is in the CPP file.
Also check the wiki, there's examples
That's where I copied the example from.
Sorry for my useless reply, guess i read too fast :oops:

glad it's solved

Post Reply