Another question on DnD

Robin Dunn:

  Yes, it works, but I have other two questions:

    (1)I write a test script listed as following, if I drag an item(wx.DragCopy), everything is OK; while I drag an item into M$ Explorer with "Shift" down(that should be wx.DragMove), the file is indeed moved , but dragResult is not wx.DragMove. I don't know what's wrong with this script:

···

#===================================================
# FileDnD.py
"""
author: Bruce Who
date: 2005-03-01
description: DnD,演示了把节点拖出为文件的功能
"""
import os
import wx

class MyDropSource(wx.DropSource):
   """ This is a custom DropSource object designed to provide feedback to the user during the drag """
   def __init__(self, tree):
      wx.DropSource.__init__(self,tree)
      self.tree_ = tree

class MyTreeCtrl(wx.TreeCtrl):
    def __init__(self, parent):
        wx.TreeCtrl.__init__(self, parent,-1)
        root=self.AddRoot('root')
        item1 = self.AppendItem(root,'item1')
        item11 = self.AppendItem(item1,'item11')
        item12 = self.AppendItem(item1,'item12')
        item13 = self.AppendItem(item1,'item13')
        item2 = self.AppendItem(root,'item2')
        item21 = self.AppendItem(item2,'item21')
        item22 = self.AppendItem(item2,'item22')
        item23 = self.AppendItem(item2,'item23')
        self.Expand(root)
        self.Expand(item1)
        self.Expand(item2)

        ## events
        self.Bind(wx.EVT_TREE_BEGIN_DRAG,self.OnBeginDrag)
    def OnBeginDrag(self,evt):
      sel_item, flags = self.HitTest(evt.GetPoint())
      self.SelectItem(sel_item)

      print "A node called %s is being dragged" % (self.GetItemText(sel_item))

      cdo = wx.FileDataObject()
      ## this is a temp file
      cdo.AddFile(os.path.realpath('./test.txt'))

      tds = MyDropSource(self)
      tds.SetData(cdo)

      dragResult = tds.DoDragDrop(True)

      if dragResult == wx.DragCopy:
          print "Result indicated successful copy"
      elif dragResult == wx.DragMove:
          print "Result indicated successful move"
      else:
          print "Result indicated failed drop"
      print

class myFrame(wx.Frame): # {{{
    def __init__(self, *args, **kwds):
        wx.Frame.__init__(self, *args, **kwds)
        #------ panel
        self.panel_=wx.Panel(self,-1)
        self.sizer_ = wx.BoxSizer( wx.VERTICAL )
        self.treeCtrl_ = MyTreeCtrl(self.panel_)
        #------ put stuff into sizer
        self.sizer_.Add(self.treeCtrl_,proportion = 1,flag=wx.EXPAND)

        # apply sizer
        self.panel_.SetSizer(self.sizer_)
        self.panel_.SetAutoLayout(True)
# }}}

def main(): # {{{
    app = wx.PySimpleApp(0)
    frame = myFrame(None,-1,title='')
    frame.Show(True)
    app.SetTopWindow(frame)
    app.MainLoop()
# }}}

if __name__ == "__main__":main()
#===================================================

  (2)I read the wxwidgets document distributed with wxpython, and find that :

wxFileDataObject::AddFile
virtual void AddFile(const wxString& file)

MSW only: adds a file to the file list represented by this data object.

     so, does this mean that this approach only works for MSW? Are there any approaches for both M$ and Linux?

======= 2005-03-10 04:08:16 Robin Dunn wrote: =======

Bruce Who wrote:

Hi,

I have another question on DnD. wxpy demo only show how to implement
DragTarget. But how to do this:

I have a treeCtrl, a user can drag tree item out of my app to some
directory, and a new file is created there whose content is the
item's data.

For example, I have a a treeCtrl with an item 'ABC', and the item's
data is 'EFG', I drag the item to directory 'f:\dir1' of Windows
Explorer, then a new file 'ABC.txt' is created and its content is
'EFG'.

Any help would be appreciated!

When you want to start the DnD then create a temporary file with the
name and content that you want. Then create an instance of
wx.FileDataObject and add the name of your temporary file to it. Then
call DoDragDrop with that data object. Exlporer will then know what to
do with it from there.

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

---------------------------------------------------------------------
To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwidgets.org
For additional commands, e-mail: wxPython-users-help@lists.wxwidgets.org

.

= = = = = = = = = = = = = = = = = = = =
      
Bruce Who
HuXuZhao@hotmail.com
2005-03-10