Extending wxCommandEvent

52 views
Skip to first unread message

Eran Ifrah

unread,
May 20, 2013, 5:14:01 PM5/20/13
to wx-u...@googlegroups.com
Hi,

I often use wxCommandEvent to pass around commands from one handler to another
One of the pains I am having is to keep track of the wxClientData I am setting in the event (i.e. delete it when its no longer needed)

I ended up writing a small class like this:


class WXDLLIMPEXP_CL clCommandEvent : public wxCommandEvent
{
    wxSharedPtr<wxClientData> m_ptr;

public:
    clCommandEvent(wxEventType commandType = wxEVT_NULL, int winid = 0);
    clCommandEvent(const clCommandEvent& event);
    virtual ~clCommandEvent();

    // Hides wxCommandEvent::Set{Get}ClientObject
    void SetClientObject(wxClientData* clientObject) ;

    wxClientData *GetClientObject() const ;

    virtual wxEvent *Clone() const;
};

typedef void (wxEvtHandler::*clCommandEventFunction)(clCommandEvent&);
#define clCommandEventHandler(func) \
    wxEVENT_HANDLER_CAST(clCommandEventFunction, func)


Basically it hides wxCommandEvent 'Set{Get}ClientObject' by setting the wxClientData passed by the user to an internal wxSharedPtr<wxClientData> member
This allows my clCommandEvent class to take ownership on the user's clientObject and delete it when its no longer referenced

This seems a very useful to me and I wondered whether you consider extending wxCommandEvent with new API something like (using 'Assign' to be consistent with other APIs):

AssignClientObject / GetAssignedClientObject 

--
 
Eran Ifrah
Author of codelite, a cross platform open source C/C++ IDE: http://www.codelite.org
wxCrafter, a wxWidgets RAD: http://wxcrafter.codelite.org

Vadim Zeitlin

unread,
May 21, 2013, 8:00:11 AM5/21/13
to wx-u...@googlegroups.com
On Tue, 21 May 2013 00:14:01 +0300 Eran Ifrah wrote:

EI> I often use wxCommandEvent to pass around commands from one handler to
EI> another One of the pains I am having is to keep track of the
EI> wxClientData I am setting in the event (i.e. delete it when its no
EI> longer needed)

I think this is a rather unusual situation. The client data is supposed to
be owned by a control, not event.

EI> I ended up writing a small class like this:

Of course, if you do want to have client data in your events, your
approach works just fine.

EI> This seems a very useful to me and I wondered whether you consider
EI> extending wxCommandEvent with new API something like (using 'Assign' to be
EI> consistent with other APIs):
EI>
EI> AssignClientObject / GetAssignedClientObject

No, I don't think this is useful enough to be done in wxCommandEvent. I'm
also a bit suspicious of the whole idea, it doesn't seem right to me to
have objects owned by the events even if I can't give any concrete reason
as to why would it be so right now. So I'd prefer to not change anything in
wxCommandEvent.

Regards,
VZ

--
TT-Solutions: wxWidgets consultancy and technical support
http://www.tt-solutions.com/
Reply all
Reply to author
Forward
0 new messages