File: wxDragImage.py

package info (click to toggle)
wxwindows2.4 2.4.5.1.1
  • links: PTS
  • area: main
  • in suites: etch-m68k
  • size: 59,920 kB
  • ctags: 97,489
  • sloc: cpp: 610,307; ansic: 111,957; python: 103,357; makefile: 3,676; sh: 3,391; lex: 192; yacc: 128; xml: 95; pascal: 74
file content (276 lines) | stat: -rw-r--r-- 8,023 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276

from wxPython.wx import *

import images

#----------------------------------------------------------------------

class DragShape:
    def __init__(self, bmp):
        self.bmp = bmp
        self.pos = wxPoint(0,0)
        self.shown = True
        self.text = None
        self.fullscreen = False


    def HitTest(self, pt):
        rect = self.GetRect()
        return rect.InsideXY(pt.x, pt.y)


    def GetRect(self):
        return wxRect(self.pos.x, self.pos.y,
                      self.bmp.GetWidth(), self.bmp.GetHeight())


    def Draw(self, dc, op = wxCOPY):
        if self.bmp.Ok():
            memDC = wxMemoryDC()
            memDC.SelectObject(self.bmp)

            dc.Blit(self.pos.x, self.pos.y,
                    self.bmp.GetWidth(), self.bmp.GetHeight(),
                    memDC, 0, 0, op, True)

            return True
        else:
            return False



#----------------------------------------------------------------------

class DragCanvas(wxScrolledWindow):
    def __init__(self, parent, ID):
        wxScrolledWindow.__init__(self, parent, ID)
        self.shapes = []
        self.dragImage = None
        self.dragShape = None
        self.hiliteShape = None

        self.SetCursor(wxStockCursor(wxCURSOR_ARROW))
        self.bg_bmp = images.getBackgroundBitmap()


        # Make a shape from an image and mask.  This one will demo
        # dragging outside the window
        bmp = images.getTestStarBitmap()
        shape = DragShape(bmp)
        shape.pos = wxPoint(5, 5)
        shape.fullscreen = True
        self.shapes.append(shape)


        # Make a shape from some text
        text = "Some Text"
        bg_colour = wxColour(57, 115, 57)  # matches the bg image
        font = wxFont(15, wxROMAN, wxNORMAL, wxBOLD)
        textExtent = self.GetFullTextExtent(text, font)
        bmp = wxEmptyBitmap(textExtent[0], textExtent[1])
        dc = wxMemoryDC()
        dc.SelectObject(bmp)
        dc.SetBackground(wxBrush(bg_colour, wxSOLID))
        dc.Clear()
        dc.SetTextForeground(wxRED)
        dc.SetFont(font)
        dc.DrawText(text, 0, 0)
        dc.SelectObject(wxNullBitmap)
        mask = wxMaskColour(bmp, bg_colour)
        bmp.SetMask(mask)
        shape = DragShape(bmp)
        shape.pos = wxPoint(5, 100)
        shape.text = "Some dragging text"
        self.shapes.append(shape)

        bmp = images.getTheKidBitmap()
        shape = DragShape(bmp)
        shape.pos = wxPoint(200, 5)
        self.shapes.append(shape)

        EVT_ERASE_BACKGROUND(self, self.OnEraseBackground)
        EVT_PAINT(self, self.OnPaint)
        EVT_LEFT_DOWN(self, self.OnLeftDown)
        EVT_LEFT_UP(self, self.OnLeftUp)
        EVT_MOTION(self, self.OnMotion)
        EVT_LEAVE_WINDOW(self, self.OnLeaveWindow)


    def OnLeaveWindow(self, evt):
        pass


    def TileBackground(self, dc):
        # tile the background bitmap
        sz = self.GetClientSize()
        w = self.bg_bmp.GetWidth()
        h = self.bg_bmp.GetHeight()

        x = 0
        while x < sz.width:
            y = 0
            while y < sz.height:
                dc.DrawBitmap(self.bg_bmp, x, y)
                y = y + h
            x = x + w


    def DrawShapes(self, dc):
        for shape in self.shapes:
            if shape.shown:
                shape.Draw(dc)


    def FindShape(self, pt):
        for shape in self.shapes:
            if shape.HitTest(pt):
                return shape
        return None


    def EraseShape(self, shape, dc):
        r = shape.GetRect()
        dc.SetClippingRegion(r.x, r.y, r.width, r.height)
        self.TileBackground(dc)
        self.DrawShapes(dc)
        dc.DestroyClippingRegion()


    def OnEraseBackground(self, evt):
        dc = evt.GetDC()
        if not dc:
            dc = wxClientDC(self)
            rect = self.GetUpdateRegion().GetBox()
            dc.SetClippingRegion(rect.x, rect.y, rect.width, rect.height)
        self.TileBackground(dc)


    def OnPaint(self, evt):
        dc = wxPaintDC(self)
        self.PrepareDC(dc)
        self.DrawShapes(dc)


    def OnLeftDown(self, evt):
        shape = self.FindShape(evt.GetPosition())
        if shape:
            # get ready to start dragging, but wait for the user to
            # move it a bit first
            self.dragShape = shape
            self.dragStartPos = evt.GetPosition()


    def OnLeftUp(self, evt):
        if not self.dragImage or not self.dragShape:
            self.dragImage = None
            self.dragShape = None
            return

        # end the dragging
        self.dragImage.Hide()
        self.dragImage.EndDrag()
        self.dragImage = None

        dc = wxClientDC(self)
        if self.hiliteShape:
            self.hiliteShape.Draw(dc)
            self.hiliteShape = None

        # reposition and draw the shape
        self.dragShape.pos = self.dragShape.pos + evt.GetPosition() - self.dragStartPos
        self.dragShape.shown = True
        self.dragShape.Draw(dc)
        self.dragShape = None


    def OnMotion(self, evt):
        if not self.dragShape or not evt.Dragging() or not evt.LeftIsDown():
            return

        # if we have a shape, but haven't started dragging yet
        if self.dragShape and not self.dragImage:

            # only start the drag after having moved a couple pixels
            tolerance = 2
            pt = evt.GetPosition()
            dx = abs(pt.x - self.dragStartPos.x)
            dy = abs(pt.y - self.dragStartPos.y)
            if dx <= tolerance and dy <= tolerance:
                return

            # erase the shape since it will be drawn independently now
            dc = wxClientDC(self)
            self.dragShape.shown = False
            self.EraseShape(self.dragShape, dc)


            if self.dragShape.text:
                self.dragImage = wxDragString(self.dragShape.text,
                                              wxStockCursor(wxCURSOR_HAND))
            else:
                self.dragImage = wxDragImage(self.dragShape.bmp,
                                             wxStockCursor(wxCURSOR_HAND))

            hotspot = self.dragStartPos - self.dragShape.pos
            self.dragImage.BeginDrag(hotspot, self, self.dragShape.fullscreen)

            self.dragImage.Move(pt)
            self.dragImage.Show()


        # if we have shape and image then move it, posibly highlighting another shape.
        elif self.dragShape and self.dragImage:
            onShape = self.FindShape(evt.GetPosition())
            unhiliteOld = False
            hiliteNew = False

            # figure out what to hilite and what to unhilite
            if self.hiliteShape:
                if onShape is None or self.hiliteShape is not onShape:
                    unhiliteOld = True

            if onShape and onShape is not self.hiliteShape and onShape.shown:
                hiliteNew = True

            # if needed, hide the drag image so we can update the window
            if unhiliteOld or hiliteNew:
                self.dragImage.Hide()

            if unhiliteOld:
                dc = wxClientDC(self)
                self.hiliteShape.Draw(dc)
                self.hiliteShape = None

            if hiliteNew:
                dc = wxClientDC(self)
                self.hiliteShape = onShape
                self.hiliteShape.Draw(dc, wxINVERT)

            # now move it and show it again if needed
            self.dragImage.Move(evt.GetPosition())
            if unhiliteOld or hiliteNew:
                self.dragImage.Show()


#----------------------------------------------------------------------

def runTest(frame, nb, log):
    win = wxPanel(nb, -1)
    canvas = DragCanvas(win, -1)
    def onSize(evt, panel=win, canvas=canvas): canvas.SetSize(panel.GetSize())
    EVT_SIZE(win, onSize)
    return win

#----------------------------------------------------------------------



overview = """\
"""


if __name__ == '__main__':
    import sys,os
    import run
    run.main(['', os.path.basename(sys.argv[0])])