wxDataViewListCtrl - Is it possible to hide certain columns? Topic is solved

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
apoorv569
Super wx Problem Solver
Super wx Problem Solver
Posts: 426
Joined: Tue Oct 20, 2020 3:35 pm

wxDataViewListCtrl - Is it possible to hide certain columns?

Post by apoorv569 »

Is it possible to hide/show certain column in wxDataViewListCtrl inside the application as needed, like for example this screenshot of my file manager,
Image

As you can see, when I right click on the column headers, I get option to show/hide certain columns, is it possible to do so? Also if it is possible, can I still get the value and all about that column when it is hidden?
User avatar
doublemax
Moderator
Moderator
Posts: 19160
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxDataViewListCtrl - Is it possible to hide certain columns?

Post by doublemax »

Catch wxEVT_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
Display popup
Use wxSettableHeaderColumn::SetHidden( bool flag ) to set visibility
Use the source, Luke!
apoorv569
Super wx Problem Solver
Super wx Problem Solver
Posts: 426
Joined: Tue Oct 20, 2020 3:35 pm

Re: wxDataViewListCtrl - Is it possible to hide certain columns?

Post by apoorv569 »

doublemax wrote: Sun Jun 27, 2021 10:21 pm Catch wxEVT_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
Display popup
Use wxSettableHeaderColumn::SetHidden( bool flag ) to set visibility
Okay, I tried what you told me, I was to able to make it work, but how do I make it so each menu entry is dedicated to the specific column that the menu item text corresponds to?

Here is what I wrote, I set the binding for it,

Code: Select all

    Bind(wxEVT_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK, &MainFrame::OnShowSampleListColumnHeaderContextMenu,
         this, BC_SampleListView);
and here is the handler, MN_Column* are enums I created for the menu items.

Code: Select all

void MainFrame::OnShowSampleListColumnHeaderContextMenu(wxDataViewEvent& event)
{
    wxMenu menu;

    wxSettableHeaderColumn* setHeaderCol = event.GetDataViewColumn();

    menu.AppendCheckItem(MN_ColumnFavorite, "Favorites");
    menu.AppendCheckItem(MN_ColumnFilename, "Filename");
    menu.AppendCheckItem(MN_ColumnSamplePack, "Sample Pack");
    menu.AppendCheckItem(MN_ColumnType, "Type");
    menu.AppendCheckItem(MN_ColumnChannels, "Channels");
    menu.AppendCheckItem(MN_ColumnLength, "Length");
    menu.AppendCheckItem(MN_ColumnSampleRate, "Sample Rate");
    menu.AppendCheckItem(MN_ColumnBitrate, "Bitrate");
    menu.AppendCheckItem(MN_ColumnPath, "Path");

    switch (m_SampleListView->GetPopupMenuSelectionFromUser(menu, event.GetPosition()))
    {
        case MN_ColumnFavorite:
            if (menu.IsChecked(MN_ColumnFavorite))
                setHeaderCol->SetHidden(true);
            else
                setHeaderCol->SetHidden(false);
            break;
        case MN_ColumnFilename:
            if (menu.IsChecked(MN_ColumnFilename))
                setHeaderCol->SetHidden(true);
            else
                setHeaderCol->SetHidden(false);
            break;
        case MN_ColumnSamplePack:
            if (menu.IsChecked(MN_ColumnSamplePack))
                setHeaderCol->SetHidden(true);
            else
                setHeaderCol->SetHidden(false);
            break;
        case MN_ColumnType:
            if (menu.IsChecked(MN_ColumnType))
                setHeaderCol->SetHidden(true);
            else
                setHeaderCol->SetHidden(false);
            break;
        case MN_ColumnChannels:
            if (menu.IsChecked(MN_ColumnChannels))
                setHeaderCol->SetHidden(true);
            else
                setHeaderCol->SetHidden(false);
            break;
        case MN_ColumnLength:
            if (menu.IsChecked(MN_ColumnLength))
                setHeaderCol->SetHidden(true);
            else
                setHeaderCol->SetHidden(false);
            break;
        case MN_ColumnSampleRate:
            if (menu.IsChecked(MN_ColumnSampleRate))
                setHeaderCol->SetHidden(true);
            else
                setHeaderCol->SetHidden(false);
            break;
        case MN_ColumnBitrate:
            if (menu.IsChecked(MN_ColumnBitrate))
                setHeaderCol->SetHidden(true);
            else
                setHeaderCol->SetHidden(false);
            break;
        case MN_ColumnPath:
            if (menu.IsChecked(MN_ColumnPath))
                setHeaderCol->SetHidden(true);
            else
                setHeaderCol->SetHidden(false);
            break;
        default:
            break;
    }
}
User avatar
doublemax
Moderator
Moderator
Posts: 19160
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxDataViewListCtrl - Is it possible to hide certain columns?

Post by doublemax »

Code: Select all

wxSettableHeaderColumn* setHeaderCol = event.GetDataViewColumn();
You don't need this, the column you clicked on doesn't matter.

You need to way to get the correct wxDataViewColumn for each ID like MN_ColumnFilename.
You can get a pointer to a wxDataViewColumn with wxDataViewCtrl::GetColumn(int pos)
When creating the menu checkitems you should also check all entries for the columns that are currently visible ( wxHeaderColumn::IsShown() )
Use the source, Luke!
apoorv569
Super wx Problem Solver
Super wx Problem Solver
Posts: 426
Joined: Tue Oct 20, 2020 3:35 pm

Re: wxDataViewListCtrl - Is it possible to hide certain columns?

Post by apoorv569 »

doublemax wrote: Mon Jun 28, 2021 5:54 am

Code: Select all

wxSettableHeaderColumn* setHeaderCol = event.GetDataViewColumn();
You don't need this, the column you clicked on doesn't matter.

You need to way to get the correct wxDataViewColumn for each ID like MN_ColumnFilename.
You can get a pointer to a wxDataViewColumn with wxDataViewCtrl::GetColumn(int pos)
When creating the menu checkitems you should also check all entries for the columns that are currently visible ( wxHeaderColumn::IsShown() )
I see, if I understand correctly, it should be like this, I have commented out the path column, as it does not exist right now.

Code: Select all

void MainFrame::OnShowSampleListColumnHeaderContextMenu(wxDataViewEvent& event)
{
    wxMenu menu;

    // wxSettableHeaderColumn* setHeaderCol = event.GetDataViewColumn();

    wxDataViewColumn* FavoriteColumn = m_SampleListView->GetColumn(0);
    wxDataViewColumn* FilenameColumn = m_SampleListView->GetColumn(1);
    wxDataViewColumn* SamplePackColumn = m_SampleListView->GetColumn(2);
    wxDataViewColumn* TypeColumn = m_SampleListView->GetColumn(3);
    wxDataViewColumn* ChannelsColumn = m_SampleListView->GetColumn(4);
    wxDataViewColumn* LengthColumn = m_SampleListView->GetColumn(5);
    wxDataViewColumn* SampleRateColumn = m_SampleListView->GetColumn(6);
    wxDataViewColumn* BitrateColumn = m_SampleListView->GetColumn(7);
    // wxDataViewColumn* PathColumn = m_SampleListView->GetColumn(8);

    if (FavoriteColumn->IsShown())
        menu.AppendCheckItem(MN_ColumnFavorite, "Favorites")->Check(true);
    else
        menu.AppendCheckItem(MN_ColumnFavorite, "Favorites")->Check(false);

    if (FilenameColumn->IsShown())
        menu.AppendCheckItem(MN_ColumnFilename, "Filename")->Check(true);
    else
        menu.AppendCheckItem(MN_ColumnFilename, "Filename")->Check(false);

    if (SamplePackColumn->IsShown())
        menu.AppendCheckItem(MN_ColumnSamplePack, "Sample Pack")->Check(true);
    else
        menu.AppendCheckItem(MN_ColumnSamplePack, "Sample Pack")->Check(false);

    if (TypeColumn->IsShown())
        menu.AppendCheckItem(MN_ColumnType, "Type")->Check(true);
    else
        menu.AppendCheckItem(MN_ColumnType, "Type")->Check(false);

    if (ChannelsColumn->IsShown())
        menu.AppendCheckItem(MN_ColumnChannels, "Channels")->Check(true);
    else
        menu.AppendCheckItem(MN_ColumnChannels, "Channels")->Check(false);

    if (LengthColumn->IsShown())
        menu.AppendCheckItem(MN_ColumnLength, "Length")->Check(true);
    else
        menu.AppendCheckItem(MN_ColumnLength, "Length")->Check(false);

    if (SampleRateColumn->IsShown())
        menu.AppendCheckItem(MN_ColumnSampleRate, "Sample Rate")->Check(true);
    else
        menu.AppendCheckItem(MN_ColumnSampleRate, "Sample Rate")->Check(false);

    if (BitrateColumn->IsShown())
        menu.AppendCheckItem(MN_ColumnBitrate, "Bitrate")->Check(true);
    else
        menu.AppendCheckItem(MN_ColumnBitrate, "Bitrate")->Check(false);

    // if (PathColumn->IsShown())
    //     menu.AppendCheckItem(MN_ColumnPath, "Path")->Check(true);
    // else
    //     menu.AppendCheckItem(MN_ColumnPath, "Path")->Check(false);

    switch (m_SampleListView->GetPopupMenuSelectionFromUser(menu, event.GetPosition()))
    {
        case MN_ColumnFavorite:
            if (menu.IsChecked(MN_ColumnFavorite))
                FavoriteColumn->SetHidden(false);
            else
                FavoriteColumn->SetHidden(true);
            break;
        case MN_ColumnFilename:
            if (menu.IsChecked(MN_ColumnFilename))
                FilenameColumn->SetHidden(false);
            else
                FilenameColumn->SetHidden(true);
            break;
        case MN_ColumnSamplePack:
            if (menu.IsChecked(MN_ColumnSamplePack))
                SamplePackColumn->SetHidden(false);
            else
                SamplePackColumn->SetHidden(true);
            break;
        case MN_ColumnType:
            if (menu.IsChecked(MN_ColumnType))
                TypeColumn->SetHidden(false);
            else
                TypeColumn->SetHidden(true);
            break;
        case MN_ColumnChannels:
            if (menu.IsChecked(MN_ColumnChannels))
                ChannelsColumn->SetHidden(false);
            else
                ChannelsColumn->SetHidden(true);
            break;
        case MN_ColumnLength:
            if (menu.IsChecked(MN_ColumnLength))
                LengthColumn->SetHidden(false);
            else
                LengthColumn->SetHidden(true);
            break;
        case MN_ColumnSampleRate:
            if (menu.IsChecked(MN_ColumnSampleRate))
                SampleRateColumn->SetHidden(false);
            else
                SampleRateColumn->SetHidden(true);
            break;
        case MN_ColumnBitrate:
            if (menu.IsChecked(MN_ColumnBitrate))
                BitrateColumn->SetHidden(false);
            else
                BitrateColumn->SetHidden(true);
            break;
        // case MN_ColumnPath:
        //     if (menu.IsChecked(MN_ColumnPath))
        //         PathColumn->SetHidden(false);
        //     else
        //         PathColumn->SetHidden(true);
        //     break;
        default:
            break;
    }
}
Is this correct? It does work though, but I'm having a little problem, I have a context menu for the rows as well, when I right click on a column header, and select a item, the column header menu closes, and the row menu opens immediately. Here is a GIF,
Image
User avatar
doublemax
Moderator
Moderator
Posts: 19160
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxDataViewListCtrl - Is it possible to hide certain columns?

Post by doublemax »

Code: Select all

    if (FavoriteColumn->IsShown())
        menu.AppendCheckItem(MN_ColumnFavorite, "Favorites")->Check(true);
    else
        menu.AppendCheckItem(MN_ColumnFavorite, "Favorites")->Check(false);
This could also be written as:

Code: Select all

menu.AppendCheckItem(MN_ColumnFavorite, "Favorites")->Check( FavoriteColumn->IsShown() );
Same goes for the SetHidden() calls later.
It does work though, but I'm having a little problem, I have a context menu for the rows as well, when I right click on a column header, and select a item, the column header menu closes, and the row menu opens immediately.
Shouldn't the item menu only open on right click?
Use the source, Luke!
PB
Part Of The Furniture
Part Of The Furniture
Posts: 4204
Joined: Sun Jan 03, 2010 5:45 pm

Re: wxDataViewListCtrl - Is it possible to hide certain columns?

Post by PB »

doublemax wrote: Mon Jun 28, 2021 7:18 am
It does work though, but I'm having a little problem, I have a context menu for the rows as well, when I right click on a column header, and select a item, the column header menu closes, and the row menu opens immediately.
Shouldn't the item menu only open on right click?
Sounds like this recently fixed bug:
https://github.com/wxWidgets/wxWidgets/ ... 925f569a39
apoorv569
Super wx Problem Solver
Super wx Problem Solver
Posts: 426
Joined: Tue Oct 20, 2020 3:35 pm

Re: wxDataViewListCtrl - Is it possible to hide certain columns?

Post by apoorv569 »

doublemax wrote: Mon Jun 28, 2021 7:18 am

Code: Select all

    if (FavoriteColumn->IsShown())
        menu.AppendCheckItem(MN_ColumnFavorite, "Favorites")->Check(true);
    else
        menu.AppendCheckItem(MN_ColumnFavorite, "Favorites")->Check(false);
This could also be written as:

Code: Select all

menu.AppendCheckItem(MN_ColumnFavorite, "Favorites")->Check( FavoriteColumn->IsShown() );
Same goes for the SetHidden() calls later.
I see, thanks for the suggestion. I'll switch my statements to this.
doublemax wrote: Mon Jun 28, 2021 7:18 am
It does work though, but I'm having a little problem, I have a context menu for the rows as well, when I right click on a column header, and select a item, the column header menu closes, and the row menu opens immediately.
Shouldn't the item menu only open on right click?
It only opens on right click, this only started happening after adding the menu for column header.
apoorv569
Super wx Problem Solver
Super wx Problem Solver
Posts: 426
Joined: Tue Oct 20, 2020 3:35 pm

Re: wxDataViewListCtrl - Is it possible to hide certain columns?

Post by apoorv569 »

PB wrote: Mon Jun 28, 2021 7:57 am
doublemax wrote: Mon Jun 28, 2021 7:18 am
It does work though, but I'm having a little problem, I have a context menu for the rows as well, when I right click on a column header, and select a item, the column header menu closes, and the row menu opens immediately.
Shouldn't the item menu only open on right click?
Sounds like this recently fixed bug:
https://github.com/wxWidgets/wxWidgets/ ... 925f569a39
I see, is this fix for wx3.0.x or wx3.1.x? I'm currently using wx3.0.5.
PB
Part Of The Furniture
Part Of The Furniture
Posts: 4204
Joined: Sun Jan 03, 2010 5:45 pm

Re: wxDataViewListCtrl - Is it possible to hide certain columns?

Post by PB »

apoorv569 wrote: Mon Jun 28, 2021 10:59 am I see, is this fix for wx3.0.x or wx3.1.x? I'm currently using wx3.0.5.
The commit says it is in the master. I did not check if it was backported, AFAIK the chance of 3.0.6 being released is not that high anyway.
apoorv569
Super wx Problem Solver
Super wx Problem Solver
Posts: 426
Joined: Tue Oct 20, 2020 3:35 pm

Re: wxDataViewListCtrl - Is it possible to hide certain columns?

Post by apoorv569 »

PB wrote: Mon Jun 28, 2021 11:02 am
apoorv569 wrote: Mon Jun 28, 2021 10:59 am I see, is this fix for wx3.0.x or wx3.1.x? I'm currently using wx3.0.5.
The commit says it is in the master. I did not check if it was backported, AFAIK the chance of 3.0.6 being released is not that high anyway.
So for me only way to get this issue fixed is switching to wx version 3.1.x?
User avatar
doublemax
Moderator
Moderator
Posts: 19160
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxDataViewListCtrl - Is it possible to hide certain columns?

Post by doublemax »

The patch is trivial and should also apply to wx 3.0.x. So you could make the few changes yourself and rebuild your wx libraries.

However, upgrading to the latest wx version would be a good idea anyway.
Use the source, Luke!
apoorv569
Super wx Problem Solver
Super wx Problem Solver
Posts: 426
Joined: Tue Oct 20, 2020 3:35 pm

Re: wxDataViewListCtrl - Is it possible to hide certain columns?

Post by apoorv569 »

doublemax wrote: Mon Jun 28, 2021 11:50 am The patch is trivial and should also apply to wx 3.0.x. So you could make the few changes yourself and rebuild your wx libraries.

However, upgrading to the latest wx version would be a good idea anyway.
I see, I'll think about what to do. Thanks for the help :D
apoorv569
Super wx Problem Solver
Super wx Problem Solver
Posts: 426
Joined: Tue Oct 20, 2020 3:35 pm

Re: wxDataViewListCtrl - Is it possible to hide certain columns?

Post by apoorv569 »

doublemax wrote: Mon Jun 28, 2021 11:50 am The patch is trivial and should also apply to wx 3.0.x. So you could make the few changes yourself and rebuild your wx libraries.

However, upgrading to the latest wx version would be a good idea anyway.
Just confirming, the master branch on https://github.com/wxWidgets/wxWidgets repository is the wx3.1.x stable version right? I'm thinking providing a link to the repository as a dependency for my project, because no Linux distribution I know currently ships wx3.1.x in their repositories. Or if there is a better/good way to handle this dependency please let me know.
User avatar
doublemax
Moderator
Moderator
Posts: 19160
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxDataViewListCtrl - Is it possible to hide certain columns?

Post by doublemax »

Technically all odd subversion numbers (2.7.x, 2.9.x, 3.1.x, etc) are not stable, these are development versions. The even numbers (2.8.x, 3.0.x, etc) are stable. However stable here does not mean that it doesn't crash, it means that the API is stable, e.g. there won't be any incompatible API changes between 3.0.6 and 3.0.7. In terms of reliability the development version is safe to use for production code (IMHO).
Use the source, Luke!
Post Reply