]> git.saurik.com Git - wxWidgets.git/blobdiff - wxPython/demo/TreeCtrl.py
Fix "warning: operation on 'y' may be undefined".
[wxWidgets.git] / wxPython / demo / TreeCtrl.py
index 547d05c87cd7601fa5b79476dce79a52c669a78e..397deb0a0a736bc5aad54a16ff8c5efcfee231c1 100644 (file)
@@ -1,7 +1,3 @@
-# 11/21/2003 - Jeff Grimmett (grimmtooth@softhome.net)
-#
-# o Updated for wx namespace
-# 
 
 import  string
 import  wx
 
 import  string
 import  wx
@@ -44,7 +40,7 @@ class TestTreeCtrlPanel(wx.Panel):
         il = wx.ImageList(isz[0], isz[1])
         fldridx     = il.Add(wx.ArtProvider_GetBitmap(wx.ART_FOLDER,      wx.ART_OTHER, isz))
         fldropenidx = il.Add(wx.ArtProvider_GetBitmap(wx.ART_FILE_OPEN,   wx.ART_OTHER, isz))
         il = wx.ImageList(isz[0], isz[1])
         fldridx     = il.Add(wx.ArtProvider_GetBitmap(wx.ART_FOLDER,      wx.ART_OTHER, isz))
         fldropenidx = il.Add(wx.ArtProvider_GetBitmap(wx.ART_FILE_OPEN,   wx.ART_OTHER, isz))
-        fileidx     = il.Add(wx.ArtProvider_GetBitmap(wx.ART_REPORT_VIEW, wx.ART_OTHER, isz))
+        fileidx     = il.Add(wx.ArtProvider_GetBitmap(wx.ART_NORMAL_FILE, wx.ART_OTHER, isz))
         smileidx    = il.Add(images.getSmilesBitmap())
 
         self.tree.SetImageList(il)
         smileidx    = il.Add(images.getSmilesBitmap())
 
         self.tree.SetImageList(il)
@@ -88,39 +84,41 @@ class TestTreeCtrlPanel(wx.Panel):
         self.Bind(wx.EVT_TREE_ITEM_ACTIVATED, self.OnActivate, self.tree)
 
         self.tree.Bind(wx.EVT_LEFT_DCLICK, self.OnLeftDClick)
         self.Bind(wx.EVT_TREE_ITEM_ACTIVATED, self.OnActivate, self.tree)
 
         self.tree.Bind(wx.EVT_LEFT_DCLICK, self.OnLeftDClick)
-        self.tree.Bind(wx.EVT_RIGHT_DOWN, self.OnRightClick)
+        self.tree.Bind(wx.EVT_RIGHT_DOWN, self.OnRightDown)
         self.tree.Bind(wx.EVT_RIGHT_UP, self.OnRightUp)
 
 
         self.tree.Bind(wx.EVT_RIGHT_UP, self.OnRightUp)
 
 
-
-    def OnRightClick(self, event):
+    def OnRightDown(self, event):
         pt = event.GetPosition();
         item, flags = self.tree.HitTest(pt)
         pt = event.GetPosition();
         item, flags = self.tree.HitTest(pt)
-        self.log.WriteText("OnRightClick: %s, %s, %s\n" %
-                           (self.tree.GetItemText(item), type(item), item.__class__))
-        self.tree.SelectItem(item)
+        if item:
+            self.log.WriteText("OnRightClick: %s, %s, %s\n" %
+                               (self.tree.GetItemText(item), type(item), item.__class__))
+            self.tree.SelectItem(item)
 
 
     def OnRightUp(self, event):
         pt = event.GetPosition();
         item, flags = self.tree.HitTest(pt)
 
 
     def OnRightUp(self, event):
         pt = event.GetPosition();
         item, flags = self.tree.HitTest(pt)
-        self.log.WriteText("OnRightUp: %s (manually starting label edit)\n"
-                           % self.tree.GetItemText(item))
-        self.tree.EditLabel(item)
+        if item:        
+            self.log.WriteText("OnRightUp: %s (manually starting label edit)\n"
+                               % self.tree.GetItemText(item))
+            self.tree.EditLabel(item)
 
 
 
     def OnBeginEdit(self, event):
         self.log.WriteText("OnBeginEdit\n")
         # show how to prevent edit...
 
 
 
     def OnBeginEdit(self, event):
         self.log.WriteText("OnBeginEdit\n")
         # show how to prevent edit...
-        if self.tree.GetItemText(event.GetItem()) == "The Root Item":
+        item = event.GetItem()
+        if item and self.tree.GetItemText(item) == "The Root Item":
             wx.Bell()
             self.log.WriteText("You can't edit this one...\n")
 
             # Lets just see what's visible of its children
             cookie = 0
             root = event.GetItem()
             wx.Bell()
             self.log.WriteText("You can't edit this one...\n")
 
             # Lets just see what's visible of its children
             cookie = 0
             root = event.GetItem()
-            (child, cookie) = self.tree.GetFirstChild(root, cookie)
+            (child, cookie) = self.tree.GetFirstChild(root)
 
             while child.IsOk():
                 self.log.WriteText("Child [%s] visible = %d" %
 
             while child.IsOk():
                 self.log.WriteText("Child [%s] visible = %d" %
@@ -132,7 +130,8 @@ class TestTreeCtrlPanel(wx.Panel):
 
 
     def OnEndEdit(self, event):
 
 
     def OnEndEdit(self, event):
-        self.log.WriteText("OnEndEdit\n")
+        self.log.WriteText("OnEndEdit: %s %s\n" %
+                           (event.IsEditCancelled(), event.GetLabel()) )
         # show how to reject edit, we'll not allow any digits
         for x in event.GetLabel():
             if x in string.digits:
         # show how to reject edit, we'll not allow any digits
         for x in event.GetLabel():
             if x in string.digits:
@@ -144,9 +143,11 @@ class TestTreeCtrlPanel(wx.Panel):
     def OnLeftDClick(self, event):
         pt = event.GetPosition();
         item, flags = self.tree.HitTest(pt)
     def OnLeftDClick(self, event):
         pt = event.GetPosition();
         item, flags = self.tree.HitTest(pt)
-        self.log.WriteText("OnLeftDClick: %s\n" % self.tree.GetItemText(item))
-        parent = self.tree.GetItemParent(item)
-        self.tree.SortChildren(parent)
+        if item:
+            self.log.WriteText("OnLeftDClick: %s\n" % self.tree.GetItemText(item))
+            parent = self.tree.GetItemParent(item)
+            if parent.IsOk():
+                self.tree.SortChildren(parent)
         event.Skip()
 
 
         event.Skip()
 
 
@@ -157,25 +158,29 @@ class TestTreeCtrlPanel(wx.Panel):
 
     def OnItemExpanded(self, event):
         item = event.GetItem()
 
     def OnItemExpanded(self, event):
         item = event.GetItem()
-        self.log.WriteText("OnItemExpanded: %s\n" % self.tree.GetItemText(item))
+        if item:
+            self.log.WriteText("OnItemExpanded: %s\n" % self.tree.GetItemText(item))
 
     def OnItemCollapsed(self, event):
         item = event.GetItem()
 
     def OnItemCollapsed(self, event):
         item = event.GetItem()
-        self.log.WriteText("OnItemCollapsed: %s\n" % self.tree.GetItemText(item))
+        if item:
+            self.log.WriteText("OnItemCollapsed: %s\n" % self.tree.GetItemText(item))
 
     def OnSelChanged(self, event):
         self.item = event.GetItem()
 
     def OnSelChanged(self, event):
         self.item = event.GetItem()
-        self.log.WriteText("OnSelChanged: %s\n" % self.tree.GetItemText(self.item))
-        if wx.Platform == '__WXMSW__':
-            self.log.WriteText("BoundingRect: %s\n" %
-                               self.tree.GetBoundingRect(self.item, True))
-        #items = self.tree.GetSelections()
-        #print map(self.tree.GetItemText, items)
+        if self.item:
+            self.log.WriteText("OnSelChanged: %s\n" % self.tree.GetItemText(self.item))
+            if wx.Platform == '__WXMSW__':
+                self.log.WriteText("BoundingRect: %s\n" %
+                                   self.tree.GetBoundingRect(self.item, True))
+            #items = self.tree.GetSelections()
+            #print map(self.tree.GetItemText, items)
         event.Skip()
 
 
     def OnActivate(self, event):
         event.Skip()
 
 
     def OnActivate(self, event):
-        self.log.WriteText("OnActivate: %s\n" % self.tree.GetItemText(self.item))
+        if self.item:
+            self.log.WriteText("OnActivate: %s\n" % self.tree.GetItemText(self.item))
 
 
 #---------------------------------------------------------------------------
 
 
 #---------------------------------------------------------------------------
@@ -191,9 +196,9 @@ def runTest(frame, nb, log):
 
 
 overview = """\
 
 
 overview = """\
-A tree control presents information as a hierarchy, with items that may be 
+A TreeCtrl presents information as a hierarchy, with items that may be 
 expanded to show further items. Items in a tree control are referenced by 
 expanded to show further items. Items in a tree control are referenced by 
-wxTreeItemId handles.
+wx.TreeItemId handles.
 
 """
 
 
 """
 
@@ -202,5 +207,5 @@ wxTreeItemId handles.
 if __name__ == '__main__':
     import sys,os
     import run
 if __name__ == '__main__':
     import sys,os
     import run
-    run.main(['', os.path.basename(sys.argv[0])])
+    run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:])