Introduction

In the hopes that someone might find this usefull... I had to write my own directory chooser because wx.DirDialog didn't work out well for me under linux.

What Objects are Involved

wx.GenericDirCtrl

Process Overview

The first thing to do is to create a custom dialog:

class CustomDirDialog(wx.Dialog):
    def __init__(self, parent, id, title):
        wx.Dialog.__init__(self, parent, id, title, size=(400,400))
        ...

Feel free to add a toolbar with some icons on the top: (ID_OUTPUT_CUSTOM_FOLDERINS is the ID associated with the 'click' event on this button - it's set to an integer elsewhere)

        newfolder_img = wx.Image('GUI/resources/Folder-new.png', wx.BITMAP_TYPE_PNG).ConvertToBitmap()
        self.toolbar = wx.ToolBar(self, -1, style=wx.TB_HORIZONTAL)
        self.toolbar.AddSimpleTool(ID_OUTPUT_CUSTOM_FOLDERINS, newfolder_img, 'New Folder', '')
        self.toolbar.Realize()

Now add the generic dir control:

        self.dcCustom = wx.GenericDirCtrl(self, size=wx.Size(350,250),
                  style=wx.DIRCTRL_DIR_ONLY|wx.DIRCTRL_EDIT_LABELS|     wx.BORDER_SUNKEN)

We'll need an event for handling directory additions: (Notice that you need to bind the ID of the tree control, not the generic directory control, to the event)

        wx.EVT_TOOL(self, ID_OUTPUT_CUSTOM_FOLDERINS, self.OnFolderInsert)

Here's the handlers:

    def OnFolderInsert(self, event):
        tree = self.dcCustom.GetTreeCtrl()
        treeitem = tree.GetSelection()
        parentpath = self.dcCustom.GetPath()
        path =  parentpath + os.sep + 'NewFolder'
        os.mkdir(path)
        self.dcCustom.ReCreateTree()
        self.dcCustom.ExpandPath(parentpath)
        self.dcCustom.SetPath(path)
        newitem = tree.GetSelection()
        tree.EditLabel(newitem)

I also had a custom popup menu on the tree control for doing renaming and deletion events. First bind the right click event to the tree control:

      wx.EVT_RIGHT_DOWN(self.dcCustom.GetTreeCtrl(), self.OnRightDown)

Here's the event handler:

    def OnRightDown(self,event):
        """Popup Menu handler
        """
        #fix parent offset
        x, y = event.GetPosition()
        x +=25
        y +=55
       
        #popup menu
        popup = wx.Menu()
        item1 = wx.MenuItem(popup, ID_OUTPUT_POP_REMOVE, "Rename")
        item2 = wx.MenuItem(popup, ID_OUTPUT_POP_DELETE, "Delete")
        popup.AppendItem(item1)
        popup.AppendItem(item2)
        
        #events
        wx.EVT_MENU(popup, ID_OUTPUT_POP_REMOVE, self.OnPopupRename)
        wx.EVT_MENU(popup, ID_OUTPUT_POP_DELETE, self.OnPopupDelete)
        self.PopupMenu(popup, (x,y))

And now the rename and delete handler:

    def OnPopupRename(self, event):
        tree = self.dcCustom.GetTreeCtrl()
        treeitem = tree.GetSelection()
        tree.EditLabel(treeitem)
    
    def OnPopupDelete(self, event):
        path = self.dcCustom.GetPath()
        if path:
            os.rmdir(path)
            self.dcCustom.ReCreateTree()
            self.dcCustom.ExpandPath(os.path.dirname(path))

Special Concerns

The event position seems quirky, so i did some simple adjustments. I'm sure theres a more elegant way.

Comments

This should allow you to add, remove, and rename directories inside the wx.GenericDirCtrl

GenericDirCtrl w/Add/Remove Directory support (last edited 2008-03-11 10:50:32 by localhost)

NOTE: To edit pages in this wiki you must be a member of the TrustedEditorsGroup.