I have understood how the "context" menu and the "right click" work.
EVT_CONTEXT_MENU(MyFrame::OnContextMenu)
EVT_TREE_ITEM_RIGHT_CLICK(-1, MyFrame::OnItemRightClick)
both functions create and display the popup menu regarding the "selected" item if any. I also handle the context menu when the user clicked in an empty area.
My problem is the following:
1/ I right click on a given element in my tree (the popup is displayed correctly using EVT_TREE_ITEM_RIGHT_CLICK
2/ I LEFT click on another tree item (without cancelling explicitely the popup menu), I receive a "EVT_CONTEXT_MENU" event before the focus is set on the tree item... and I think I should not receive this event at all !
Here is a part of my code:
The same code works well if I use a wxTreeListCtrl.void MyFrame::OnItemRightClick(wxTreeEvent& event)
{
wxTreeItemId &cur_item = event.GetItem();
if (!IsSelected(cur_item))
{
UnselectAll();
SelectItem (cur_item);
}
// this is my function creating and displaying a dynamic popup menu
ShowMenu(cur_item, event.GetPoint());
event.StopPropagation ();
}
void MyFrame::OnContextMenu(wxContextMenuEvent& event)
{
wxTreeItemId item;
wxPoint pt = event.GetPosition();
// if event was generated by keyboard (MSW-specific?)
if (pt.x==-1 && pt.y==-1) //(this is how MSW indicates it)
{
// get the selection
item = GetSelection();
//attempt to guess where to show the menu
if (item.IsOk())
{
//if an item was clicked, show menu to the right of it
wxRect rect;
GetBoundingRect(item, rect, true); //true = only the label
pt = wxPoint(rect.GetRight(), rect.GetTop());
}
else
pt = wxPoint(0, 0);
}
else
// else, the user clicked in a blank area... so unselect all items
{
//event was generated by mouse, use supplied coords
pt = ScreenToClient(pt);
UnselectAll();
}
ShowMenu(item, pt);
}
void MyFrame::ShowMenu(wxTreeItemId id, const wxPoint& pt)
{
// creates a menu
...
wxMenu * menu = new wxMenu...
if (menu != NULL)
{
PopupMenu(menu, pt);
delete menu;
}
}
Does anyone know what may be wrong ? Is there a "bug fix" in 2.5.3 ?
wxWidgets 2.5.1
on Windows (a port will be done on Linux/Unix & MacOS)