Question

I tried to implement to implement my own custom event. It does work if I post it from the class i connected it from (a wxFrame) but if it gets posted from a child wxFrame it doesn't get catched.

The Custom event:

**cFrameFocus.h**
#pragma once
#include <wx\event.h>
class cFrameFocusEvent;
const wxEventTypeTag<cFrameFocusEvent> evtFRAME_FOCUS( wxNewEventType() );
//eventhandler macro
#if _MSC_VER <= 1600
typedef void (wxEvtHandler::*MyFrameFocusEventFunction)(cFrameFocusEvent&);
#define FrameFocusEventHandler(func) wxEVENT_HANDLER_CAST(MyFrameFocusEventFunction,func)
#else
#define FrameFocusEventHandler(func) (&func)
#endif
class cFrameFocusEvent :
    public wxEvent
{
    wxWindow* m_frame;
public:
    cFrameFocusEvent(wxEventType pEventType,int pWinId = wxID_ANY,const wxWindow* pWin = 0);
    ~cFrameFocusEvent(void);
    //impement base class pure virtual
    virtual wxEvent* Clone(void)const;
    //accessor
    wxWindow* GetWindow(void)const;
};

**cFrameFocus.cpp**
#include "cFrameFocus.h"
cFrameFocusEvent::cFrameFocusEvent(wxEventType pEventType,int pWinId,const wxWindow* pWin):
wxEvent(pWinId,pEventType)
{
    m_frame = const_cast<wxWindow*>(pWin);
}
cFrameFocusEvent::~cFrameFocusEvent(void)
{
}
wxEvent* cFrameFocusEvent::Clone(void)const{
    return new cFrameFocusEvent(*this);
}
wxWindow* cFrameFocusEvent::GetWindow(void)const{
    return m_frame;
}

The child Frame trying to send an even :

cFrameFocusEvent* pevent = new cFrameFocusEvent(evtFRAME_FOCUS,GetId(),this);
wxQueueEvent(GetParent(),pevent);

Note: I did already stumble upon this question wxWidgets 2.9 custom events, but it didn't help me.

Was it helpful?

Solution

You need to read event propagation overview to understand how the events work in wxWidgets. In particular, it would have told you that only events deriving from wxCommandEvent are propagated upwards by default, for the other ones you need to call ResumePropagation(N) to make them propagate N levels up (and could be INT_MAX to make them propagate without limit, of course).

OTHER TIPS

After some intensive debugging and study of the wxWidgets code I figured it out. The problem is that with every include of the file "cFrameFocus.h" the wxEventTypeTag changed (the id of it), which meant that when it got included in a file to connect to this event type id had the id "x" and in a different file to post this event the event type had the id "y". The solution to this wasn't a matter of propagation (since it got anyway sent to the level 0, where it should be processed), rather a "programming mistake".

**cFrameFocus.h**
extern const wxEventTypeTag<cFrameFocusEvent> evtFRAME_FOCUS;

**cFrameFocus.cpp**
const wxEventTypeTag<cFrameFocusEvent> evtFRAME_FOCUS(wxNewEventType());

this code change fixed the error

EDIT

As VZ mentioned in the comments, it is more appropriate to use the macros and not directly accessing the functions.

The proper solution:

***cFrameFocus.h***

wxDECLARE_EVENT(evtFRAME_FOCUS,cFrameFocusEvent);

***cFrameFocus.cpp***
 wxDEFINE_EVENT(evtFRAME_FOCUS,cFrameFocusEvent);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top