how to use wx.DragTreeItem?

howdy,

I have found wx.DragTreeItem to be almost completely undocumented.

I'm trying to implement DnD on a wx.TreeCtrl and I want the drag icon to be a
visual representation of the tree item. I believe this is what wx.DragTreeItem
is for. I have found the following snippet of code on this list a few years back:

class FolderTree(wx.TreeCtrl):
[snip]
  wx.EVT_TREE_BEGIN_DRAG(self, ID_TREE, self.OnBeginDrag)
[snip]
  def OnBeginDrag(self, event):
          self.dragging_item = event.GetItem()
    self.drag_image = wx.DragTreeItem(self, self.dragging_item)

In fact I am trying to hack TraitsUI using the wx backend so that the cursor is
more useful than the standard block cursor.

I tried implementing this in their TreeEditor and it didn't seem to work.
Can someone be kind enough to write a few sentences and explain how this
drag image hook is supposed to work?

thanks,
Danny

Danny Shevitz wrote:

howdy,

I have found wx.DragTreeItem to be almost completely undocumented.

DragTreeItem is simply a convenience factory that fetches the text of the item from the tree and makes a bitmap out of it to pass to the main wx.DragImage constructor.

I'm trying to implement DnD on a wx.TreeCtrl and I want the drag icon to be a
visual representation of the tree item. I believe this is what wx.DragTreeItem
is for. I have found the following snippet of code on this list a few years back:

class FolderTree(wx.TreeCtrl):
[snip]
  wx.EVT_TREE_BEGIN_DRAG(self, ID_TREE, self.OnBeginDrag)
[snip]
  def OnBeginDrag(self, event):
          self.dragging_item = event.GetItem()
    self.drag_image = wx.DragTreeItem(self, self.dragging_item)

In fact I am trying to hack TraitsUI using the wx backend so that the cursor is
more useful than the standard block cursor.

I tried implementing this in their TreeEditor and it didn't seem to work. Can someone be kind enough to write a few sentences and explain how this
drag image hook is supposed to work?

Take a look at the DragImage sample in the demo. Basically a wx.DragImage object just makes it a little easier to draw a transient image somewhere either within the bounds of some window, or on the whole screen, and update (move) it in response to some other events, like mouse motion. So the key point for using it for something like drag and drop is to create the obj like above, and then call its methods from some appropriate event handler or callback to move it to follow the mouse cursor. For the wx.TreeCtrl's internal DnD you can do it from the tree's EVT_MOTION event. For normal DnD then you can do it from the DropSource's GiveFeedback method.

···

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

Danny Shevitz wrote:

I tried implementing this in their TreeEditor and it didn't seem to work. Can someone be kind enough to write a few sentences and explain how this
drag image hook is supposed to work?

I can probably be more specific. How do I create a wx.Cursor (the type
required by a DropSource) from a DragImage (what DragTreeItem produces)?

You don't. If you want to use a cursor then just make an image from the text and convert it to a cursor. Making a wx.DragImage first is just a wast of effort. But I expect that you don't want to do that because there are several limitations on the cursor sizes and/or colors, depending on platform.

···

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

> In fact I am trying to hack TraitsUI using the wx backend so that the c

ursor is

> more useful than the standard block cursor.
>
> I tried implementing this in their TreeEditor and it didn't seem to work.
> Can someone be kind enough to write a few sentences and explain how this
> drag image hook is supposed to work?

Take a look at the DragImage sample in the demo. Basically a
wx.DragImage object just makes it a little easier to draw a transient
image somewhere either within the bounds of some window, or on the whole
screen, and update (move) it in response to some other events, like
mouse motion. So the key point for using it for something like drag and
drop is to create the obj like above, and then call its methods from
some appropriate event handler or callback to move it to follow the
mouse cursor. For the wx.TreeCtrl's internal DnD you can do it from the
tree's EVT_MOTION event. For normal DnD then you can do it from the
DropSource's GiveFeedback method.

The actual architecture is the tree has a DropTarget. In EVT_TREE_BEGIN_DRAG
Traits creates a DropSource with a custom data format. The arguments to
DropSource can take wx.Cursor's, but I don't know how to create a cursor from
the tree item (including the icon). You are suggesting a second method which
is to override the DropSource's GiveFeedback method probably using a DragImage.

What do you recommend as the best way to proceed?

Danny Shevitz wrote:

In fact I am trying to hack TraitsUI using the wx backend so that the c

ursor is

more useful than the standard block cursor.

I tried implementing this in their TreeEditor and it didn't seem to work. Can someone be kind enough to write a few sentences and explain how this
drag image hook is supposed to work?

Take a look at the DragImage sample in the demo. Basically a wx.DragImage object just makes it a little easier to draw a transient image somewhere either within the bounds of some window, or on the whole screen, and update (move) it in response to some other events, like mouse motion. So the key point for using it for something like drag and drop is to create the obj like above, and then call its methods from some appropriate event handler or callback to move it to follow the mouse cursor. For the wx.TreeCtrl's internal DnD you can do it from the tree's EVT_MOTION event. For normal DnD then you can do it from the DropSource's GiveFeedback method.

The actual architecture is the tree has a DropTarget. In EVT_TREE_BEGIN_DRAG
Traits creates a DropSource with a custom data format. The arguments to DropSource can take wx.Cursor's, but I don't know how to create a cursor from
the tree item (including the icon).

Create a bitmap, select it into a wx.MemoryDC, draw the text and whatever else you want into this dc, convert the bitmap to a wx.Image, use wx.CursorFromImage to get a cursor.

You are suggesting a second method which
is to override the DropSource's GiveFeedback method probably using a DragImage.

What do you recommend as the best way to proceed?

The GiveFeedback method would probably be better since it won't have the same platform limitations as the cursor. Basically you just override GiveFeedback in your derived wx.DropSource class, and when it is called you can use wx.GetMousePosition to get the current screen location of the mouse cursor, and then if you are using a wx.DragImage just Move() it to that spot.

···

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