How to propagate mouse events to parent Frame from wxGLCanvas?

502 views
Skip to first unread message

Divick Kishore

unread,
Jul 7, 2006, 8:26:50 AM7/7/06
to wx-u...@lists.wxwidgets.org
Hi all,
I have a class derived from wxFrame wherein I have several panels, and within
one of the panels I have a class derived from wxGLCanvas (call it MyGLCanvas).

Since mouse and keyboard events for MyGLCanvas are NOT delegated to the parent
class or to the parent Frame (specified in the first argument of the constructor
of GLCanvas). But I want that some events like Mouse events and key events
should be delegated to the top level Frame class, in case MyGLCanvas does not
want to handle them.

Can somebody help me figure out how can I send these events to the top level
Frame class?

Thanks,
Divick

Alex Bligh

unread,
Jul 7, 2006, 9:00:06 AM7/7/06
to wx-u...@lists.wxwidgets.org, Alex Bligh

--On 07 July 2006 17:56 +0530 Divick Kishore <div...@darshansolutions.com>
wrote:

On the event handler for MyGLCanvas, just do something like:
GetParent()->GetEventHandler()->ProcessEvent(event);

where event was what you were passed. Obviously if your MyGLCanvas is
not an immediate child of your wxFrame, you will have to do something
a bit cleverer to find it, such as:
wxWindow * pParent = this;
while (pParent && !pParent->IsKindOf(CLASSINFO(wxFrame)))
{
pParent=pParent->GetParent();
}
if (pParent)
pParent->GetEventHandler()->ProcessEvent(event);


Alex

Alex Bligh

unread,
Jul 7, 2006, 9:50:12 AM7/7/06
to wx-u...@lists.wxwidgets.org, Alex Bligh

--On 07 July 2006 18:55 +0530 Divick Kishore <divick....@gmail.com>
wrote:

> What if I I don't have an event handler set using PushEventHandler method
> ?

The GetEventHandler() will return the wxFrame itself. If you don't use
PushEventHandler or Connect then the GetEventHandler is redunandant but
it will be harmless. Beware, you might not /know/ you are using
PushEventHandler (for instance if you use wxAUI on the frame).

> I tried as you suggested above but the events are not being delegated
> to the function defined in the parent using the STATIC event tables. Is
> there something wrong with my approach ?

Well it works for me. I use this to propagate mouse events upwards from
controls to dialogs. Is your parent window handler expecting that
the event object may be different?

Alex

Divick Kishore

unread,
Jul 7, 2006, 9:25:24 AM7/7/06
to wx-u...@lists.wxwidgets.org
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: wx-users-u...@lists.wxwidgets.org
> For additional commands, e-mail: wx-use...@lists.wxwidgets.org

>
>
What if I I don't have an event handler set using PushEventHandler
method ? I tried as you suggested above but the events are not being
delegated to the function defined in the parent using the STATIC event
tables. Is there something wrong with my approach ?

Thanks,
Divick

Divick Kishore

unread,
Jul 7, 2006, 11:20:28 AM7/7/06
to wx-u...@lists.wxwidgets.org
Sorry but I don't understand what you are trying to say. Please consider
me a newbie :)
I guess my question itself was not very clear.

My MyGLCanvas class's constructor looks like this:

MyGLCanvas ::MyGLCanvas (wxWindow *
parentWindow):wxGLCanvas(parentWindow, wxID_ANY, wxDefaultPosition,
wxDefaultSize, wxSUNKEN_BORDER)

and above is being called from the a class derived from wxFrame and with
this (MyFrame) passed in the constructor of MyGLCanvas. Does this mean
MyFrame is parent of MyGLCanvas?
To be more precise My GLCanvas is embeded inside a sizer added to the
frame. ( Does adding a widget/control to a sizer/panel make the widget /
control a child of size in which it is added ?)

Here is what it looks like in short:

wxFrame <-- MyFrame
wxGLCanvas <-- MyGLCanvas
MyFrame contains a wxSizer. wxSizer contains MyGLCanvas created with
MyFrame as the 'parent' argument in the constructor.

So if I write code something like as shown below it does not work.

wxWindow * pParent = this;
while (pParent && !pParent->IsKindOf(CLASSINFO(wxFrame)))
{
pParent=pParent->GetParent();
}
if (pParent)
pParent->GetEventHandler()->ProcessEvent(event);

while if I write something like this (as shown below ), then it works:

wxWindow * pParent = this;

while (pParent && !pParent->IsKindOf(CLASSINFO(MyFrame)))
{
pParent=pParent->GetParent();
}

MyFrame * frame = dynamic_cast<MyFrame *>(pParent);
if (frame)
frame->OnKeyF1(keyEvent); //This function handles
the event for some key

Is there something wrong with the above code or is it the right way of
doing things?

Thanks,
Divick

Alex Bligh

unread,
Jul 8, 2006, 6:08:33 AM7/8/06
to wx-u...@lists.wxwidgets.org, Alex Bligh
Divick,

> So if I write code something like as shown below it does not work.
>
> wxWindow * pParent = this;
> while (pParent && !pParent->IsKindOf(CLASSINFO(wxFrame)))
> {
> pParent=pParent->GetParent();
> }
> if (pParent)
> pParent->GetEventHandler()->ProcessEvent(event);
>
> while if I write something like this (as shown below ), then it works:
>
> wxWindow * pParent = this;
> while (pParent && !pParent->IsKindOf(CLASSINFO(MyFrame)))
> {
> pParent=pParent->GetParent();
> }
> MyFrame * frame = dynamic_cast<MyFrame *>(pParent);
> if (frame)
> frame->OnKeyF1(keyEvent); //This function handles the
> event for some key
>
> Is there something wrong with the above code or is it the right way of
> doing things?

I would have thought the first code set should work too. It possibly
does not because the default event table expects the window of
the event concerned to be equal to the window handling it. I didn't
think it did that, and is more likely to check the window ID which
you can circumvent by using wxID_ANY. In my test case I'm actually
passing it to a PushEventHandler'd event handler rather than the
static table.

If the latter works for you, then I'd just carry on using it. What
I was trying to give you was a more generic way of passing the
event up to the parent.

Alex

Reply all
Reply to author
Forward
0 new messages