1999-04-30 03:29:54 +00:00
|
|
|
|
|
|
|
from wxPython.wx import *
|
|
|
|
|
2001-04-09 19:36:36 +00:00
|
|
|
import images
|
|
|
|
|
1999-04-30 03:29:54 +00:00
|
|
|
#---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
class MyCanvas(wxScrolledWindow):
|
1999-09-17 05:55:00 +00:00
|
|
|
def __init__(self, parent, id = -1, size = wxDefaultSize):
|
|
|
|
wxScrolledWindow.__init__(self, parent, id, wxPoint(0, 0), size, wxSUNKEN_BORDER)
|
1999-04-30 03:29:54 +00:00
|
|
|
|
|
|
|
self.lines = []
|
1999-06-22 07:03:29 +00:00
|
|
|
self.maxWidth = 1000
|
|
|
|
self.maxHeight = 1000
|
2002-01-05 23:45:33 +00:00
|
|
|
self.count = 0
|
1999-04-30 03:29:54 +00:00
|
|
|
|
2002-02-02 01:10:06 +00:00
|
|
|
self.SetBackgroundColour("WHITE")
|
2000-07-15 19:51:35 +00:00
|
|
|
EVT_LEFT_DOWN(self, self.OnLeftButtonEvent)
|
|
|
|
EVT_LEFT_UP(self, self.OnLeftButtonEvent)
|
|
|
|
EVT_MOTION(self, self.OnLeftButtonEvent)
|
|
|
|
EVT_PAINT(self, self.OnPaint)
|
|
|
|
|
1999-04-30 03:29:54 +00:00
|
|
|
self.SetCursor(wxStockCursor(wxCURSOR_PENCIL))
|
2001-04-09 19:36:36 +00:00
|
|
|
bmp = images.getTest2Bitmap()
|
1999-12-07 18:20:01 +00:00
|
|
|
mask = wxMaskColour(bmp, wxBLUE)
|
|
|
|
bmp.SetMask(mask)
|
1999-06-22 07:03:29 +00:00
|
|
|
self.bmp = bmp
|
1999-04-30 03:29:54 +00:00
|
|
|
|
1999-06-22 07:03:29 +00:00
|
|
|
self.SetScrollbars(20, 20, self.maxWidth/20, self.maxHeight/20)
|
|
|
|
|
2001-11-20 02:31:27 +00:00
|
|
|
|
1999-06-22 07:03:29 +00:00
|
|
|
def getWidth(self):
|
|
|
|
return self.maxWidth
|
|
|
|
|
|
|
|
def getHeight(self):
|
|
|
|
return self.maxHeight
|
1999-04-30 03:29:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
def OnPaint(self, event):
|
2002-01-05 23:45:33 +00:00
|
|
|
#self.count += 1
|
|
|
|
#print self.count, "begin paint...",
|
1999-04-30 03:29:54 +00:00
|
|
|
dc = wxPaintDC(self)
|
|
|
|
self.PrepareDC(dc)
|
|
|
|
self.DoDrawing(dc)
|
2002-01-05 23:45:33 +00:00
|
|
|
#print "end paint"
|
1999-04-30 03:29:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
def DoDrawing(self, dc):
|
|
|
|
dc.BeginDrawing()
|
2002-02-02 01:10:06 +00:00
|
|
|
dc.SetPen(wxPen('RED'))
|
1999-04-30 03:29:54 +00:00
|
|
|
dc.DrawRectangle(5, 5, 50, 50)
|
|
|
|
|
|
|
|
dc.SetBrush(wxLIGHT_GREY_BRUSH)
|
2002-02-02 01:10:06 +00:00
|
|
|
dc.SetPen(wxPen('BLUE', 4))
|
1999-04-30 03:29:54 +00:00
|
|
|
dc.DrawRectangle(15, 15, 50, 50)
|
|
|
|
|
2001-03-14 08:05:03 +00:00
|
|
|
dc.SetFont(wxFont(14, wxSWISS, wxNORMAL, wxNORMAL))
|
1999-04-30 03:29:54 +00:00
|
|
|
dc.SetTextForeground(wxColour(0xFF, 0x20, 0xFF))
|
|
|
|
te = dc.GetTextExtent("Hello World")
|
|
|
|
dc.DrawText("Hello World", 60, 65)
|
|
|
|
|
2002-02-02 01:10:06 +00:00
|
|
|
dc.SetPen(wxPen('VIOLET', 4))
|
1999-04-30 03:29:54 +00:00
|
|
|
dc.DrawLine(5, 65+te[1], 60+te[0], 65+te[1])
|
|
|
|
|
|
|
|
lst = [(100,110), (150,110), (150,160), (100,160)]
|
|
|
|
dc.DrawLines(lst, -60)
|
|
|
|
dc.SetPen(wxGREY_PEN)
|
|
|
|
dc.DrawPolygon(lst, 75)
|
|
|
|
dc.SetPen(wxGREEN_PEN)
|
|
|
|
dc.DrawSpline(lst+[(100,100)])
|
|
|
|
|
1999-12-07 18:20:01 +00:00
|
|
|
dc.DrawBitmap(self.bmp, 200, 20, true)
|
1999-06-22 07:03:29 +00:00
|
|
|
dc.SetTextForeground(wxColour(0, 0xFF, 0x80))
|
|
|
|
dc.DrawText("a bitmap", 200, 85)
|
1999-04-30 03:29:54 +00:00
|
|
|
|
1999-12-11 07:59:23 +00:00
|
|
|
font = wxFont(20, wxSWISS, wxNORMAL, wxNORMAL)
|
|
|
|
dc.SetFont(font)
|
|
|
|
dc.SetTextForeground(wxBLACK)
|
|
|
|
for a in range(0, 360, 45):
|
|
|
|
dc.DrawRotatedText("Rotated text...", 300, 300, a)
|
|
|
|
|
2000-01-31 21:07:04 +00:00
|
|
|
dc.SetPen(wxTRANSPARENT_PEN)
|
|
|
|
dc.SetBrush(wxBLUE_BRUSH)
|
|
|
|
dc.DrawRectangle(50,500,50,50)
|
|
|
|
dc.DrawRectangle(100,500,50,50)
|
|
|
|
|
2002-02-02 01:10:06 +00:00
|
|
|
dc.SetPen(wxPen('RED'))
|
2000-10-30 21:08:42 +00:00
|
|
|
dc.DrawEllipticArc(200, 500, 50, 75, 0, 90)
|
|
|
|
|
2001-09-27 00:27:46 +00:00
|
|
|
y = 20
|
|
|
|
for style in [wxDOT, wxLONG_DASH, wxSHORT_DASH, wxDOT_DASH, wxUSER_DASH]:
|
|
|
|
pen = wxPen("DARK ORCHID", 1, style)
|
|
|
|
if style == wxUSER_DASH:
|
2001-11-19 20:43:04 +00:00
|
|
|
pen.SetDashes([1, 2, 3, 4, 5, 6, 7, 8])
|
2001-09-27 02:11:47 +00:00
|
|
|
pen.SetColour("RED")
|
2001-09-27 00:27:46 +00:00
|
|
|
dc.SetPen(pen)
|
|
|
|
dc.DrawLine(300, y, 400, y)
|
|
|
|
y = y + 10
|
|
|
|
|
2002-02-02 01:10:06 +00:00
|
|
|
dc.SetBrush(wxTRANSPARENT_BRUSH)
|
2001-11-29 00:51:59 +00:00
|
|
|
dc.SetPen(wxPen(wxColour(0xFF, 0x20, 0xFF), 1, wxSOLID))
|
|
|
|
dc.DrawRectangle(450, 50, 100, 100)
|
|
|
|
old_pen = dc.GetPen()
|
|
|
|
new_pen = wxPen("BLACK", 5)
|
|
|
|
dc.SetPen(new_pen)
|
|
|
|
dc.DrawRectangle(470, 70, 60, 60)
|
|
|
|
dc.SetPen(old_pen)
|
|
|
|
dc.DrawRectangle(490, 90, 20, 20)
|
|
|
|
|
|
|
|
|
1999-04-30 03:29:54 +00:00
|
|
|
self.DrawSavedLines(dc)
|
|
|
|
dc.EndDrawing()
|
|
|
|
|
|
|
|
|
|
|
|
def DrawSavedLines(self, dc):
|
2002-02-02 01:10:06 +00:00
|
|
|
dc.SetPen(wxPen('MEDIUM FOREST GREEN', 4))
|
1999-04-30 03:29:54 +00:00
|
|
|
for line in self.lines:
|
|
|
|
for coords in line:
|
|
|
|
apply(dc.DrawLine, coords)
|
|
|
|
|
|
|
|
|
|
|
|
def SetXY(self, event):
|
|
|
|
self.x, self.y = self.ConvertEventCoords(event)
|
|
|
|
|
|
|
|
def ConvertEventCoords(self, event):
|
2000-08-08 06:11:51 +00:00
|
|
|
xView, yView = self.GetViewStart()
|
1999-04-30 03:29:54 +00:00
|
|
|
xDelta, yDelta = self.GetScrollPixelsPerUnit()
|
|
|
|
return (event.GetX() + (xView * xDelta),
|
|
|
|
event.GetY() + (yView * yDelta))
|
|
|
|
|
|
|
|
def OnLeftButtonEvent(self, event):
|
|
|
|
if event.LeftDown():
|
|
|
|
self.SetXY(event)
|
|
|
|
self.curLine = []
|
2000-02-16 05:57:16 +00:00
|
|
|
self.CaptureMouse()
|
1999-04-30 03:29:54 +00:00
|
|
|
|
|
|
|
elif event.Dragging():
|
|
|
|
dc = wxClientDC(self)
|
|
|
|
self.PrepareDC(dc)
|
|
|
|
dc.BeginDrawing()
|
2002-02-02 01:10:06 +00:00
|
|
|
dc.SetPen(wxPen('MEDIUM FOREST GREEN', 4))
|
1999-04-30 03:29:54 +00:00
|
|
|
coords = (self.x, self.y) + self.ConvertEventCoords(event)
|
|
|
|
self.curLine.append(coords)
|
|
|
|
apply(dc.DrawLine, coords)
|
|
|
|
self.SetXY(event)
|
|
|
|
dc.EndDrawing()
|
|
|
|
|
|
|
|
elif event.LeftUp():
|
|
|
|
self.lines.append(self.curLine)
|
|
|
|
self.curLine = []
|
2000-02-16 05:57:16 +00:00
|
|
|
self.ReleaseMouse()
|
1999-04-30 03:29:54 +00:00
|
|
|
|
2001-05-06 05:03:24 +00:00
|
|
|
|
|
|
|
## This is an example of what to do for the EVT_MOUSEWHEEL event,
|
|
|
|
## but since wxScrolledWindow does this already it's not
|
|
|
|
## necessary to do it ourselves.
|
|
|
|
|
|
|
|
## wheelScroll = 0
|
|
|
|
## def OnWheel(self, evt):
|
|
|
|
## delta = evt.GetWheelDelta()
|
|
|
|
## rot = evt.GetWheelRotation()
|
|
|
|
## linesPer = evt.GetLinesPerAction()
|
|
|
|
## ws = self.wheelScroll
|
|
|
|
## ws = ws + rot
|
|
|
|
## lines = ws / delta
|
|
|
|
## ws = ws - lines * delta
|
|
|
|
## self.wheelScroll = ws
|
|
|
|
## if lines != 0:
|
|
|
|
## lines = lines * linesPer
|
|
|
|
## vsx, vsy = self.GetViewStart()
|
|
|
|
## scrollTo = vsy - lines
|
|
|
|
## self.Scroll(-1, scrollTo)
|
|
|
|
|
1999-04-30 03:29:54 +00:00
|
|
|
#---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
def runTest(frame, nb, log):
|
|
|
|
win = MyCanvas(nb)
|
|
|
|
return win
|
|
|
|
|
|
|
|
#---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
overview = """\
|
|
|
|
"""
|