wxTreeListCtrl : Add an event

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
oscar1919
Earned a small fee
Earned a small fee
Posts: 17
Joined: Tue Dec 11, 2018 10:40 am
Location: Belgium

wxTreeListCtrl : Add an event

Post by oscar1919 »

Hi,

For my code, I need a wxTreeListCtrl, but I want to add an event, e.g. wxOnItemCollapsed. One way to do this is by copying the complete code of treelist.cpp into a new control, and modify this code to cope with the extra event. This works, but this is not so elegant. I wonder if there is a way by introducing a new control, derived from wxTreeListCtrl, and if there is any sample code for this?
I made an attempt to do this, by creating a derived control and adding an OnItemCollapsed(wxDataViewEvent& event); to it, but I see two problems.
- As soon as I add wxDECLARE_EVENT_TABLE(); to the derived control, the wxSizeEvent events seem to no longer reach the base control wxTreeListCtrl, and I do not know how to forward them.
- I do not know how I should obtain the wxTreeListCtrl item from the wxDataView item coupled to the wxDataViewEvent.

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

Re: wxTreeListCtrl : Add an event

Post by doublemax »

You're talking about wxTreeListCtrl, but also wxDataViewCtrl which are two different things.

As the needed methods of wxTreeListCtrl are not virtual, subclassing it won't work. But you could modify wxTreeListCtrl itself, and build the wxWidgets libraries yourself.

If it's only the wxOnItemCollapsed event you need, you could use wxDataViewTreeCtrl which does support wxEVT_DATAVIEW_ITEM_COLLAPSED.
Use the source, Luke!
oscar1919
Earned a small fee
Earned a small fee
Posts: 17
Joined: Tue Dec 11, 2018 10:40 am
Location: Belgium

Re: wxTreeListCtrl : Add an event

Post by oscar1919 »

Thanks for the quick reply, doublemax.

As the event handlers of wxTreeListCtrl take a wxDataViewEvent& as argument, I thought I had to do the same thing for the extra event in a subclass of wxTreeListCtrl. And use the wxDataViewCtrl* returned by wxTreeListCtrl::GetDataView.
But as you pointed out that subclassing won't work, I will simply copy the wxTreeListCtrl code into a new class and add the extra event code directly in that class.
User avatar
doublemax
Moderator
Moderator
Posts: 19115
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxTreeListCtrl : Add an event

Post by doublemax »

oscar1919 wrote: Mon Mar 20, 2023 8:41 pm As the event handlers of wxTreeListCtrl take a wxDataViewEvent& as argument, I thought I had to do the same thing for the extra event in a subclass of wxTreeListCtrl. And use the wxDataViewCtrl* returned by wxTreeListCtrl::GetDataView.
Ok, i missed that wxTreeListCtrl is implemented using wxDataViewCtrl internally. But in that case it could be possible that you're able to receive the wxEVT_DATAVIEW_ITEM_COLLAPSED event. Give it a try.
Use the source, Luke!
oscar1919
Earned a small fee
Earned a small fee
Posts: 17
Joined: Tue Dec 11, 2018 10:40 am
Location: Belgium

Re: wxTreeListCtrl : Add an event

Post by oscar1919 »

Ok, but in that case let me point to my original question. I can subclass wxTreeListCtrl. But as soon as I add the necessary code to add event handlers to this new class, the wxSizeEvent does no longer reach the base class. I can write a handler for this event in the subclass, and would like to propagate the wxSizeEvent to the base class, but I did not find a way to do that. Can it be done? Is there an example? (QueueEvent maybe ? ...)
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7459
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: wxTreeListCtrl : Add an event

Post by ONEEYEMAN »

Hi,
Did you read doublemax last reply?
There is an event already for what you want in wxDVC. Try to bind to it.

Thank you.
oscar1919
Earned a small fee
Earned a small fee
Posts: 17
Joined: Tue Dec 11, 2018 10:40 am
Location: Belgium

Re: wxTreeListCtrl : Add an event

Post by oscar1919 »

Dear ONEEYEMAN,

I am able to handle the ItemCollapsed event in the subclassed control, that is not the problem. The problem is with sizing the control. The control starts at a very small size and does not respond to resizing. I think the reason is that the wx "SIZE" events do not reach the base class any longer. That is also what debugging shows. I assume I need to add a handler for the size event to the derived control and there I want to delegate the actual sizing to the wxTreeListCtrl size event handler. I do not know how to do that last step, and do not even know if it is possible. How do I propagate the size event from the derived class to the base class? Or am I missing something else? If I do not add event handling to the derived class, sizing works perfect.

Thanks.
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7459
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: wxTreeListCtrl : Add an event

Post by ONEEYEMAN »

Hi
But why do you need subclasses?
Just wxTLC and handle the event doublemax mentioned.

Also you could try to look wxTLC implementation from wxCode.

Thank you.
oscar1919
Earned a small fee
Earned a small fee
Posts: 17
Joined: Tue Dec 11, 2018 10:40 am
Location: Belgium

Re: wxTreeListCtrl : Add an event

Post by oscar1919 »

Hi,
As you can read in my original post, I did that. Just copy the whole wxTLC and add what I need. But this does not feel as a very elegant solution. I would rather take an existing object and add functionality to it. You could call it an exercise. But from the replies, maybe I should conclude that wxTLC was not designed for this purpose.

Thx.
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7459
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: wxTreeListCtrl : Add an event

Post by ONEEYEMAN »

Hi,
You misunderstood our replies. ;-)
What me and doublemax says is that you don't need to copy anything.

Use the wxTLC as is from the library and see if you can catch the wxEVT_DATAVIEW_ITEM_COLLAPSED event.

So remove the sources, add the appropriate object and bind it to the event mentioned.

Let us know if it works.

Thank you.
oscar1919
Earned a small fee
Earned a small fee
Posts: 17
Joined: Tue Dec 11, 2018 10:40 am
Location: Belgium

Re: wxTreeListCtrl : Add an event

Post by oscar1919 »

Hi,

Yes, this works! That was quite easy and far less complicated as I thought. My next struggle is to translate the wxDataViewItem, that comes with the wxDataViewEvent, into the corresponding wxTreeListItem. I see that there are conversion functions in a class wxTreeListModel, but this class is part of treelist.cpp and not available in the header file. But maybe there is also a simple solution to that problem?

Again thanks for the support.
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7459
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: wxTreeListCtrl : Add an event

Post by ONEEYEMAN »

Hi,
What you could also try is to use EVT_TREELIST_ITEM_EXPAND{ING,ED} and check for IsExpanded() in the handler.

Check the documentation at https://docs.wxwidgets.org/latest/class ... _ctrl.html.

So you probably don't need to do any conversion at all.

Thank you.
oscar1919
Earned a small fee
Earned a small fee
Posts: 17
Joined: Tue Dec 11, 2018 10:40 am
Location: Belgium

Re: wxTreeListCtrl : Add an event

Post by oscar1919 »

Hi,

With respect to the conversion of wxDataViewItem to wxTreeListItem, it seems it can be done by casting. I do not understand it 100%, I think the reason is that both wxDataViewItem and wxTreeListItem are based on wxItemId<some_pointer>, where corresponding elements contain the same pointer.

So, my problem is solved, thanks.

This is what I have added to sample treelist.cpp to test it.

Code: Select all

//added to the include files
#include "wx/dataview.h"

...
// added to class MyFrame : 
    void OnItemCollapsed(wxDataViewEvent & event) ;

// added to the event table
    EVT_DATAVIEW_ITEM_COLLAPSED(wxID_ANY, MyFrame::OnItemCollapsed)

// function definition :

void MyFrame::OnItemCollapsed(wxDataViewEvent & event)  {

    wxDataViewItem dvi = event.GetItem() ;
    wxTreeListItem *wxip = reinterpret_cast<wxTreeListItem *>( &dvi) ;
    wxLogMessage("Item \"%s\" collapsed", DumpItem(*wxip));
}
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7459
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: wxTreeListCtrl : Add an event

Post by ONEEYEMAN »

Hi,
Did you try to catch the event I suggested?

Thank you.
oscar1919
Earned a small fee
Earned a small fee
Posts: 17
Joined: Tue Dec 11, 2018 10:40 am
Location: Belgium

Re: wxTreeListCtrl : Add an event

Post by oscar1919 »

Yes, EVT_TREELIST_ITEM_EXPAND{ING,ED} are not generated when an item collapses.
Post Reply