wxTreeCtrl context menu
Posted: Sat Mar 16, 2019 7:15 am
Hello All,
I'm having some trouble getting a context menu event to fire with the wxTreeCtrl. I'm actually using a wxGenericDirCtrl and getting the tree from it. I've included the relevant code below. I'm new to wxWidgets, so perhaps I'm doing something stupid. Any advice would be much appreciated.
Thanks,
-S
I'm having some trouble getting a context menu event to fire with the wxTreeCtrl. I'm actually using a wxGenericDirCtrl and getting the tree from it. I've included the relevant code below. I'm new to wxWidgets, so perhaps I'm doing something stupid. Any advice would be much appreciated.
Code: Select all
fileTree = new wxGenericDirCtrl(this, ID_FileTree, wxDirDialogDefaultFolderStr,
wxDefaultPosition,
wxSize(200, 150),
wxDIRCTRL_3D_INTERNAL | wxSUNKEN_BORDER,
wxEmptyString,
0,
"test");
Connect(ID_FileTree, wxEVT_DIRCTRL_SELECTIONCHANGED, wxCommandEventHandler(MainFrame::OnFolderSelect));
Connect(ID_FileTree, wxEVT_DIRCTRL_FILEACTIVATED, wxCommandEventHandler(MainFrame::OnFileSelect));
wxTreeCtrl *tree = fileTree->GetTreeCtrl();
tree->Connect(ID_Tree, wxEVT_TREE_ITEM_RIGHT_CLICK, wxTreeEventHandler(MainFrame::OnTreeItemMenu));
tree->Connect(ID_Tree, wxEVT_TREE_ITEM_MENU, wxTreeEventHandler(MainFrame::OnTreeItemMenu));
tree->Connect(ID_Tree, wxEVT_CONTEXT_MENU, wxTreeEventHandler(MainFrame::OnTreeItemMenu));
...
void MainFrame::OnTreeItemMenu(wxTreeEvent& evt)
{
wxTreeCtrl *tree = fileTree->GetTreeCtrl();
messages->AppendText("OnTreeItemMenu\n");
// This event handler creates a new menu and displays
// it as a popup menu with the wxWindow::PopupMenu function
wxMenu menu;
menu.Append(ID_TreePermissions, "Show node text");
// We need to pass some data (the text of the node) to
// the menu event handler. We create a class that holds
// the needed data and use the Connect function to
// associate our event handler with the menu.
UserData* userData = new UserData();
userData->data = tree->GetItemText(evt.GetItem());
menu.Connect(ID_TreePermissions, wxEVT_MENU,
(wxObjectEventFunction)&MainFrame::OnMenuItem,
userData, this);
// Display the menu as a popup menu
PopupMenu(&menu);
evt.Skip();
}
-S