]> git.saurik.com Git - wxWidgets.git/blobdiff - samples/treectrl/treetest.cpp
fixed completely broken CheckForTransparency()
[wxWidgets.git] / samples / treectrl / treetest.cpp
index 8731c603462ed07cc9f9aa7d3dadf7c086a18598..ab501de148a9b2a21f2d2cd09a2d8ee5e2af876f 100644 (file)
@@ -131,9 +131,9 @@ BEGIN_EVENT_TABLE(MyTreeCtrl, wxTreeCtrl)
     EVT_TREE_ITEM_EXPANDING(TreeTest_Ctrl, MyTreeCtrl::OnItemExpanding)
     EVT_TREE_ITEM_COLLAPSED(TreeTest_Ctrl, MyTreeCtrl::OnItemCollapsed)
     EVT_TREE_ITEM_COLLAPSING(TreeTest_Ctrl, MyTreeCtrl::OnItemCollapsing)
-    EVT_TREE_ITEM_RIGHT_CLICK(TreeTest_Ctrl, MyTreeCtrl::OnItemRightClick)
+    //EVT_TREE_ITEM_RIGHT_CLICK(TreeTest_Ctrl, MyTreeCtrl::OnItemRightClick)
 
-    EVT_RIGHT_UP(MyTreeCtrl::OnRMouseUp)
+    EVT_CONTEXT_MENU(MyTreeCtrl::OnContextMenu)
     EVT_TREE_SEL_CHANGED(TreeTest_Ctrl, MyTreeCtrl::OnSelChanged)
     EVT_TREE_SEL_CHANGING(TreeTest_Ctrl, MyTreeCtrl::OnSelChanging)
     EVT_TREE_KEY_DOWN(TreeTest_Ctrl, MyTreeCtrl::OnTreeKeyDown)
@@ -412,7 +412,7 @@ void MyFrame::OnDump(wxCommandEvent& WXUNUSED(event))
 
     CHECK_ITEM( root );
 
-    m_treeCtrl->GetItemsRecursively(root, -1);
+    m_treeCtrl->GetItemsRecursively(root);
 }
 
 #ifndef NO_MULTIPLE_SELECTION
@@ -791,7 +791,7 @@ void MyTreeCtrl::AddTestItemsToTree(size_t numChildren,
     // set some colours/fonts for testing
     SetItemFont(rootId, *wxITALIC_FONT);
 
-    long cookie;
+    wxTreeItemIdValue cookie;
     wxTreeItemId id = GetFirstChild(rootId, cookie);
     SetItemTextColour(id, *wxBLUE);
 
@@ -801,23 +801,24 @@ void MyTreeCtrl::AddTestItemsToTree(size_t numChildren,
     SetItemBackgroundColour(id, *wxLIGHT_GREY);
 }
 
-void MyTreeCtrl::GetItemsRecursively(const wxTreeItemId& idParent, long cookie)
+void MyTreeCtrl::GetItemsRecursively(const wxTreeItemId& idParent,
+                                     wxTreeItemIdValue cookie)
 {
     wxTreeItemId id;
 
-    if( cookie == -1 )
+    if ( !cookie )
         id = GetFirstChild(idParent, cookie);
     else
         id = GetNextChild(idParent, cookie);
 
-    if(id <= 0)
+    if ( !id.IsOk() )
         return;
 
     wxString text = GetItemText(id);
     wxLogMessage(text);
 
     if (ItemHasChildren(id))
-        GetItemsRecursively(id,-1);
+        GetItemsRecursively(id);
 
     GetItemsRecursively(idParent, cookie);
 }
@@ -836,6 +837,7 @@ void MyTreeCtrl::DoToggleIcon(const wxTreeItemId& item)
 void MyTreeCtrl::name(wxTreeEvent& event)                        \
 {                                                                \
     wxLogMessage(wxT(#name));                                    \
+    SetLastItem(wxTreeItemId());                                 \
     event.Skip();                                                \
 }
 
@@ -1101,10 +1103,35 @@ void MyTreeCtrl::OnItemRightClick(wxTreeEvent& event)
     ShowMenu(event.GetItem(), event.GetPoint());
 }
 
-void MyTreeCtrl::OnRMouseUp(wxMouseEvent& event)
+void MyTreeCtrl::OnContextMenu(wxContextMenuEvent& event)
 {
     wxPoint pt = event.GetPosition();
-    ShowMenu(HitTest(pt), pt);
+    wxTreeItemId item = GetSelection();
+    wxLogMessage("OnContextMenu at screen coords (%i, %i)", pt.x, pt.y);
+
+    //check if event was generated by keyboard (MSW-specific?)
+    if (pt.x==-1 && pt.y==-1) //(this is how MSW indicates it)
+    {
+        //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
+    {
+        //event was generated by mouse, use supplied coords
+        pt = ScreenToClient(pt);
+    }
+    
+    ShowMenu(item, pt);
 }
 
 void MyTreeCtrl::ShowMenu(wxTreeItemId id, const wxPoint& pt)