]> git.saurik.com Git - wxWidgets.git/blobdiff - samples/treectrl/treetest.cpp
offset version by 1 to avoid having compatibility_version of 0.0.0 under Darwin:...
[wxWidgets.git] / samples / treectrl / treetest.cpp
index d140808b4de4c1e159d7cd08bbc764fb9ad8599d..813cd3b75a38151b361dd4a0beb3cf0628a0cbd8 100644 (file)
@@ -9,11 +9,6 @@
 // Licence:     wxWindows license
 /////////////////////////////////////////////////////////////////////////////
 
-#ifdef __GNUG__
-  #pragma implementation
-  #pragma interface
-#endif
-
 // For compilers that support precompilation, includes "wx/wx.h".
 #include "wx/wxprec.h"
 
@@ -32,8 +27,7 @@
 #include "wx/image.h"
 #include "wx/imaglist.h"
 #include "wx/treectrl.h"
-
-#include "math.h"
+#include "wx/math.h"
 
 #ifdef __WIN32__
     // this is not supported by native control
@@ -137,13 +131,22 @@ 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_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)
     EVT_TREE_ITEM_ACTIVATED(TreeTest_Ctrl, MyTreeCtrl::OnItemActivated)
+
+    // so many differents ways to handle right mouse button clicks...
+    EVT_CONTEXT_MENU(MyTreeCtrl::OnContextMenu)
+    // EVT_TREE_ITEM_MENU is the preferred event for creating context menus
+    // on a tree control, because it includes the point of the click or item,
+    // meaning that no additional placement calculations are required.
+    EVT_TREE_ITEM_MENU(TreeTest_Ctrl, MyTreeCtrl::OnItemMenu)
+    EVT_TREE_ITEM_RIGHT_CLICK(TreeTest_Ctrl, MyTreeCtrl::OnItemRClick)
+
+    EVT_RIGHT_DOWN(MyTreeCtrl::OnRMouseDown)
+    EVT_RIGHT_UP(MyTreeCtrl::OnRMouseUp)
     EVT_RIGHT_DCLICK(MyTreeCtrl::OnRMouseDClick)
 END_EVENT_TABLE()
 
@@ -411,7 +414,7 @@ void MyFrame::OnRename(wxCommandEvent& WXUNUSED(event))
     static wxString s_text;
     s_text = wxGetTextFromUser(wxT("New name: "), wxT("Tree sample question"),
             s_text, this);
-    if ( !s_text.IsEmpty() )
+    if ( !s_text.empty() )
     {
         m_treeCtrl->SetItemText(item, s_text);
     }
@@ -1014,7 +1017,7 @@ void LogKeyEvent(const wxChar *name, const wxKeyEvent& event)
 
             default:
             {
-               if ( wxIsprint((int)keycode) )
+               if ( keycode < 128 && wxIsprint((int)keycode) )
                    key.Printf(wxT("'%c'"), (char)keycode);
                else if ( keycode > 0 && keycode < 27 )
                    key.Printf(_("Ctrl-%c"), wxT('A') + keycode - 1);
@@ -1105,6 +1108,11 @@ void MyTreeCtrl::OnBeginLabelEdit(wxTreeEvent& event)
 
         event.Veto();
     }
+    else if ( itemId == GetRootItem() )
+    {
+        // test that it is possible to change the text of the item being edited
+        SetItemText(itemId, _T("Editing root item"));
+    }
 }
 
 void MyTreeCtrl::OnEndLabelEdit(wxTreeEvent& event)
@@ -1148,26 +1156,35 @@ void MyTreeCtrl::OnItemActivated(wxTreeEvent& event)
     wxLogMessage(wxT("OnItemActivated"));
 }
 
-void MyTreeCtrl::OnItemRightClick(wxTreeEvent& event)
+void MyTreeCtrl::OnItemMenu(wxTreeEvent& event)
 {
-    ShowMenu(event.GetItem(), event.GetPoint());
+    wxTreeItemId itemId = event.GetItem();
+    MyTreeItemData *item = itemId.IsOk() ? (MyTreeItemData *)GetItemData(itemId)
+                                         : NULL;
+
+    wxLogMessage(wxT("OnItemMenu for item \"%s\""), item ? item->GetDesc()
+                                                         : _T(""));
+
+    event.Skip();
 }
 
 void MyTreeCtrl::OnContextMenu(wxContextMenuEvent& event)
 {
     wxPoint pt = event.GetPosition();
-    wxTreeItemId item = GetSelection();
+    wxTreeItemId item;
+    if ( !HasFlag(wxTR_MULTIPLE) )
+        item = GetSelection();
     wxLogMessage(wxT("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)
+    // 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())
+        // attempt to guess where to show the menu
+        if ( item.IsOk() )
         {
-            //if an item was clicked, show menu to the right of it
+            // if an item was clicked, show menu to the right of it
             wxRect rect;
-            GetBoundingRect(item, rect, true); //true = only the label
+            GetBoundingRect(item, rect, true /* only the label */);
             pt = wxPoint(rect.GetRight(), rect.GetTop());
         }
         else
@@ -1175,9 +1192,8 @@ void MyTreeCtrl::OnContextMenu(wxContextMenuEvent& event)
             pt = wxPoint(0, 0);
         }
     }
-    else
+    else // event was generated by mouse, use supplied coords
     {
-        //event was generated by mouse, use supplied coords
         pt = ScreenToClient(pt);
     }
 
@@ -1205,6 +1221,32 @@ void MyTreeCtrl::ShowMenu(wxTreeItemId id, const wxPoint& pt)
 #endif // wxUSE_MENUS
 }
 
+void MyTreeCtrl::OnItemRClick(wxTreeEvent& event)
+{
+    wxTreeItemId itemId = event.GetItem();
+    MyTreeItemData *item = itemId.IsOk() ? (MyTreeItemData *)GetItemData(itemId)
+                                         : NULL;
+
+    wxLogMessage(wxT("Item \"%s\" right clicked"), item ? item->GetDesc()
+                                                        : _T(""));
+
+    event.Skip();
+}
+
+void MyTreeCtrl::OnRMouseDown(wxMouseEvent& event)
+{
+    wxLogMessage(wxT("Right mouse button down"));
+
+    event.Skip();
+}
+
+void MyTreeCtrl::OnRMouseUp(wxMouseEvent& event)
+{
+    wxLogMessage(wxT("Right mouse button up"));
+
+    event.Skip();
+}
+
 void MyTreeCtrl::OnRMouseDClick(wxMouseEvent& event)
 {
     wxTreeItemId id = HitTest(event.GetPosition());
@@ -1216,6 +1258,8 @@ void MyTreeCtrl::OnRMouseDClick(wxMouseEvent& event)
         if ( item )
             wxLogMessage(wxT("Item '%s' under mouse"), item->GetDesc());
     }
+
+    event.Skip();
 }
 
 static inline const wxChar *Bool2String(bool b)