Dragging in action

Listing 18.2 displays a complete drag source control suitable for testing by dragging the arrow graphic to any application on your system that accepts text. Figure 18.2 displays that example.

Listing 18.2 A small drag source control import wx class DragController(wx.Control):

Just a little control to handle dragging the text from a text control. We use a separate control so as to not interfere with the native drag-select functionality of the native text control.

wx.Control._init_(self, parent, -1, size=size, style=wx.SIMPLE_BORDER)

self.source = source self.SetMinSize(size)

self.Bind(wx.EVT_PAINT, self.OnPaint) self.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown)

def OnPaint(self, evt):

# draw a simple arrow dc = wx.BufferedPaintDC(self)



Put some text in this control:

Then drag from the neighboring bitmap and drop in an application that accepts dropped text, such as MS Word.

Figure 18.2 The drop source control as it looks on screen def OnLeftDown(self, evt):

text = self.source.GetValue() data = wx.TextDataObject(text)

dropSource = wx.DropSource(self) <-l Creating the drop source dropSource.SetData(data) <— Setting the data result = dropSource.DoDragDrop(wx.Drag_AllowMove) •

if result == wx.DragMove:

self.source.SetValue("") <— n , .. ,

Deleting from source if desired

Performing the drop class MyFrame(wx.Frame):

wx.Frame.__init__(self, None, title="Drop Source") p = wx.Panel(self)

label1 = wx.StaticText(p, -1, "Put some text in this control:") label2 = wx.StaticText(p, -1,

"Then drag from the neighboring bitmap and\n" "drop in an application that accepts dropped\n" "text, such as MS Word.") text = wx.TextCtrl(p, -1, "Some text") dragctl = DragController(p, text) sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add(label1, 0, wx.ALL, 5) hrow = wx.BoxSizer(wx.HORIZONTAL) hrow.Add(text, 1, wx.RIGHT, 5) hrow.Add(dragctl, 0)

sizer.Add(hrow, 0, wx.EXPAND|wx.ALL, 5) sizer.Add(label2, 0, wx.ALL, 5) p.SetSizer(sizer) sizer.Fit(self)

app = wx.PySimpleApp() frm = MyFrame() frm.Show() app.MainLoop()

Next, we'll show you what drag and drop looks like from the target side.

Was this article helpful?

0 0

Post a comment