File: drop_source.py

package info (click to toggle)
wxpython3.0 3.0.2.0%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 482,760 kB
  • ctags: 518,293
  • sloc: cpp: 2,127,226; python: 294,045; makefile: 51,942; ansic: 19,033; sh: 3,013; xml: 1,629; perl: 17
file content (70 lines) | stat: -rw-r--r-- 2,359 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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.
    """
    def __init__(self, parent, source, size=(25,25)):
        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)
        dc.SetBackground(wx.Brush(self.GetBackgroundColour()))
        dc.Clear()
        w, h = dc.GetSize()
        y = h/2
        dc.SetPen(wx.Pen("dark blue", 2))
        dc.DrawLine(w/8,   y,  w-w/8, y)
        dc.DrawLine(w-w/8, y,  w/2,   h/4)
        dc.DrawLine(w-w/8, y,  w/2,   3*h/4)

    def OnLeftDown(self, evt):
        text = self.source.GetValue()
        data = wx.TextDataObject(text)
        dropSource = wx.DropSource(self)
        dropSource.SetData(data)
        result = dropSource.DoDragDrop(wx.Drag_AllowMove)

        # if the user wants to move the data then we should delete it
        # from the source
        if result == wx.DragMove:
            self.source.SetValue("")
        
class MyFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, title="Drop Source")
        p = wx.Panel(self)

        # create the controls
        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)

        # setup the layout with sizers
        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.App()
frm = MyFrame()
frm.Show()
app.MainLoop()