]> git.saurik.com Git - wxWidgets.git/blob - wxPython/samples/wxPIA_book/Chapter-18/drop_source.py
don't use invalid wxIconBundles, it results in asserts after recent changes
[wxWidgets.git] / wxPython / samples / wxPIA_book / Chapter-18 / drop_source.py
1 import wx
2
3 class DragController(wx.Control):
4 """
5 Just a little control to handle dragging the text from a text
6 control. We use a separate control so as to not interfere with
7 the native drag-select functionality of the native text control.
8 """
9 def __init__(self, parent, source, size=(25,25)):
10 wx.Control.__init__(self, parent, -1, size=size,
11 style=wx.SIMPLE_BORDER)
12 self.source = source
13 self.SetMinSize(size)
14 self.Bind(wx.EVT_PAINT, self.OnPaint)
15 self.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown)
16
17 def OnPaint(self, evt):
18 # draw a simple arrow
19 dc = wx.BufferedPaintDC(self)
20 dc.SetBackground(wx.Brush(self.GetBackgroundColour()))
21 dc.Clear()
22 w, h = dc.GetSize()
23 y = h/2
24 dc.SetPen(wx.Pen("dark blue", 2))
25 dc.DrawLine(w/8, y, w-w/8, y)
26 dc.DrawLine(w-w/8, y, w/2, h/4)
27 dc.DrawLine(w-w/8, y, w/2, 3*h/4)
28
29 def OnLeftDown(self, evt):
30 text = self.source.GetValue()
31 data = wx.TextDataObject(text)
32 dropSource = wx.DropSource(self)
33 dropSource.SetData(data)
34 result = dropSource.DoDragDrop(wx.Drag_AllowMove)
35
36 # if the user wants to move the data then we should delete it
37 # from the source
38 if result == wx.DragMove:
39 self.source.SetValue("")
40
41 class MyFrame(wx.Frame):
42 def __init__(self):
43 wx.Frame.__init__(self, None, title="Drop Source")
44 p = wx.Panel(self)
45
46 # create the controls
47 label1 = wx.StaticText(p, -1, "Put some text in this control:")
48 label2 = wx.StaticText(p, -1,
49 "Then drag from the neighboring bitmap and\n"
50 "drop in an application that accepts dropped\n"
51 "text, such as MS Word.")
52 text = wx.TextCtrl(p, -1, "Some text")
53 dragctl = DragController(p, text)
54
55 # setup the layout with sizers
56 sizer = wx.BoxSizer(wx.VERTICAL)
57 sizer.Add(label1, 0, wx.ALL, 5)
58 hrow = wx.BoxSizer(wx.HORIZONTAL)
59 hrow.Add(text, 1, wx.RIGHT, 5)
60 hrow.Add(dragctl, 0)
61 sizer.Add(hrow, 0, wx.EXPAND|wx.ALL, 5)
62 sizer.Add(label2, 0, wx.ALL, 5)
63 p.SetSizer(sizer)
64 sizer.Fit(self)
65
66
67 app = wx.PySimpleApp()
68 frm = MyFrame()
69 frm.Show()
70 app.MainLoop()