wxMenu - How to change menu options if menu shown is for a container or a child item in wxDataViewTreeCtrl. 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

wxMenu - How to change menu options if menu shown is for a container or a child item in wxDataViewTreeCtrl.

Post by apoorv569 »

I have wxDataViewTreeCtrl, for which I want make right click popup menu. I want the menu items to be different when the user clicks on a container vs user clicks on a child item. The problem I am facing right now is, I can check if the item being click on is a container and show only menu items for the container, but if I check for if item is not a container then it still shows all the menu items including the items for container. Here is a GIF to explain this,
Image

And here is the relevant code,

Code: Select all

    if (item.IsOk() && m_CollectionView->IsContainer(item))
    {
        switch (m_CollectionView->GetPopupMenuSelectionFromUser(menu, event.GetPosition()))
        {
            case MN_CreateFolder:
               break;
            case MN_RemoveFolder:
                break;
            case MN_FilterSampleView:
                break;
            default:
                return;
        }
    }
    else if (item.IsOk() && !m_CollectionView->IsContainer(item))
    {
        switch (m_CollectionView->GetPopupMenuSelectionFromUser(menu, event.GetPosition()))
        {
            case MN_RemoveSample:
                break;
            case MN_ShowInLibrary:
                break;
            default:
                return;
        }
    }
Is there something wrong in the code, or is this even doable like this? Or do I need to make separate wxMenu for both conditions.
User avatar
doublemax
Moderator
Moderator
Posts: 19115
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxMenu - How to change menu options if menu shown is for a container or a child item in wxDataViewTreeCtrl.

Post by doublemax »

Please show the code that builds the menu.
Use the source, Luke!
apoorv569
Super wx Problem Solver
Super wx Problem Solver
Posts: 426
Joined: Tue Oct 20, 2020 3:35 pm

Re: wxMenu - How to change menu options if menu shown is for a container or a child item in wxDataViewTreeCtrl.

Post by apoorv569 »

doublemax wrote: Wed Jun 16, 2021 12:12 pm Please show the code that builds the menu.
Here is the code for the menu,

Code: Select all

    wxMenu menu;

    // Container menu items
    menu.Append(MN_CreateFolder, "Create new folder");
    menu.Append(MN_RemoveFolder, "Remove folder");

    if (!bFiltered)
        menu.Append(MN_FilterSampleView, "Filter sample view");
    else
        menu.Append(MN_FilterSampleView, "Clear filter");

    // Child menu items
    if (item.IsOk() && !m_CollectionView->IsContainer(item))
    {
        menu.Append(MN_RemoveSample, "Remove sample");
        menu.Append(MN_ShowInLibrary, "Show sample in library");
    }
Last edited by apoorv569 on Wed Jun 16, 2021 1:57 pm, edited 1 time in total.
User avatar
doublemax
Moderator
Moderator
Posts: 19115
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxMenu - How to change menu options if menu shown is for a container or a child item in wxDataViewTreeCtrl.

Post by doublemax »

Is this code complete? For me it looks like there is only one menu and you don't check if the item is a container or not, which doesn't match the GIF you posted.
Use the source, Luke!
apoorv569
Super wx Problem Solver
Super wx Problem Solver
Posts: 426
Joined: Tue Oct 20, 2020 3:35 pm

Re: wxMenu - How to change menu options if menu shown is for a container or a child item in wxDataViewTreeCtrl.

Post by apoorv569 »

doublemax wrote: Wed Jun 16, 2021 1:41 pm Is this code complete? For me it looks like there is only one menu and you don't check if the item is a container or not, which doesn't match the GIF you posted.
Sorry, it was a copy paste error. I updated the post above.
User avatar
doublemax
Moderator
Moderator
Posts: 19115
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxMenu - How to change menu options if menu shown is for a container or a child item in wxDataViewTreeCtrl.

Post by doublemax »

I hope you're not trolling me here...

Code: Select all

    // Container menu items
    menu.Append(MN_CreateFolder, "Create new folder");
    menu.Append(MN_RemoveFolder, "Remove folder");

    if (!bFiltered)
        menu.Append(MN_FilterSampleView, "Filter sample view");
    else
        menu.Append(MN_FilterSampleView, "Clear filter");
This code is executed unconditionally. If you only want these items to appear on container items, you need to add another if-check.
Use the source, Luke!
apoorv569
Super wx Problem Solver
Super wx Problem Solver
Posts: 426
Joined: Tue Oct 20, 2020 3:35 pm

Re: wxMenu - How to change menu options if menu shown is for a container or a child item in wxDataViewTreeCtrl.

Post by apoorv569 »

doublemax wrote: Wed Jun 16, 2021 2:35 pm I hope you're not trolling me here...

Code: Select all

    // Container menu items
    menu.Append(MN_CreateFolder, "Create new folder");
    menu.Append(MN_RemoveFolder, "Remove folder");

    if (!bFiltered)
        menu.Append(MN_FilterSampleView, "Filter sample view");
    else
        menu.Append(MN_FilterSampleView, "Clear filter");
This code is executed unconditionally. If you only want these items to appear on container items, you need to add another if-check.
No trolling, I do apologize, for my stupid mistake. I have a lot of things going on right now. Probably missed this somehow.
Post Reply