1999-04-29 23:29:54 -04:00
|
|
|
|
|
|
|
from wxPython.wx import *
|
|
|
|
|
2001-04-09 15:36:36 -04:00
|
|
|
import images
|
|
|
|
|
2002-04-08 21:25:47 -04:00
|
|
|
BUFFERED = 1
|
|
|
|
|
1999-04-29 23:29:54 -04:00
|
|
|
#---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
class MyCanvas(wxScrolledWindow):
|
1999-09-17 01:55:00 -04:00
|
|
|
def __init__(self, parent, id = -1, size = wxDefaultSize):
|
|
|
|
wxScrolledWindow.__init__(self, parent, id, wxPoint(0, 0), size, wxSUNKEN_BORDER)
|
1999-04-29 23:29:54 -04:00
|
|
|
|
|
|
|
self.lines = []
|
1999-06-22 03:03:29 -04:00
|
|
|
self.maxWidth = 1000
|
|
|
|
self.maxHeight = 1000
|
2002-04-08 21:25:47 -04:00
|
|
|
self.x = self.y = 0
|
|
|
|
self.curLine = []
|
2003-03-25 01:35:27 -05:00
|
|
|
self.drawing = False
|
1999-04-29 23:29:54 -04:00
|
|
|
|
2002-02-01 20:10:06 -05:00
|
|
|
self.SetBackgroundColour("WHITE")
|
1999-04-29 23:29:54 -04:00
|
|
|
self.SetCursor(wxStockCursor(wxCURSOR_PENCIL))
|
2001-04-09 15:36:36 -04:00
|
|
|
bmp = images.getTest2Bitmap()
|
1999-12-07 13:20:01 -05:00
|
|
|
mask = wxMaskColour(bmp, wxBLUE)
|
|
|
|
bmp.SetMask(mask)
|
1999-06-22 03:03:29 -04:00
|
|
|
self.bmp = bmp
|
1999-04-29 23:29:54 -04:00
|
|
|
|
1999-06-22 03:03:29 -04:00
|
|
|
self.SetScrollbars(20, 20, self.maxWidth/20, self.maxHeight/20)
|
|
|
|
|
2002-04-08 21:25:47 -04:00
|
|
|
if BUFFERED:
|
|
|
|
# Initialize the buffer bitmap. No real DC is needed at this point.
|
|
|
|
self.buffer = wxEmptyBitmap(self.maxWidth, self.maxHeight)
|
|
|
|
dc = wxBufferedDC(None, self.buffer)
|
|
|
|
dc.SetBackground(wxBrush(self.GetBackgroundColour()))
|
|
|
|
dc.Clear()
|
|
|
|
self.DoDrawing(dc)
|
|
|
|
|
2002-05-15 01:05:02 -04:00
|
|
|
EVT_LEFT_DOWN(self, self.OnLeftButtonEvent)
|
|
|
|
EVT_LEFT_UP(self, self.OnLeftButtonEvent)
|
|
|
|
EVT_MOTION(self, self.OnLeftButtonEvent)
|
|
|
|
EVT_PAINT(self, self.OnPaint)
|
2002-07-19 20:14:28 -04:00
|
|
|
##EVT_MOUSEWHEEL(self, self.OnWheel)
|
2002-05-15 01:05:02 -04:00
|
|
|
|
2001-11-19 21:31:27 -05:00
|
|
|
|
1999-06-22 03:03:29 -04:00
|
|
|
def getWidth(self):
|
|
|
|
return self.maxWidth
|
|
|
|
|
|
|
|
def getHeight(self):
|
|
|
|
return self.maxHeight
|
1999-04-29 23:29:54 -04:00
|
|
|
|
|
|
|
|
|
|
|
def OnPaint(self, event):
|
2002-04-08 21:25:47 -04:00
|
|
|
if BUFFERED:
|
|
|
|
# Create a buffered paint DC. It will create the real
|
|
|
|
# wxPaintDC and then blit the bitmap to it when dc is
|
|
|
|
# deleted. Since we don't need to draw anything else
|
|
|
|
# here that's all there is to it.
|
|
|
|
dc = wxBufferedPaintDC(self, self.buffer)
|
|
|
|
else:
|
|
|
|
dc = wxPaintDC(self)
|
|
|
|
self.PrepareDC(dc)
|
|
|
|
# since we're not buffering in this case, we have to
|
|
|
|
# paint the whole window, potentially very time consuming.
|
|
|
|
self.DoDrawing(dc)
|
1999-04-29 23:29:54 -04:00
|
|
|
|
|
|
|
|
2003-07-02 19:13:10 -04:00
|
|
|
def DoDrawing(self, dc, printing=False):
|
1999-04-29 23:29:54 -04:00
|
|
|
dc.BeginDrawing()
|
2002-02-01 20:10:06 -05:00
|
|
|
dc.SetPen(wxPen('RED'))
|
2003-11-12 16:34:20 -05:00
|
|
|
dc.DrawRectangle((5, 5), (50, 50))
|
1999-04-29 23:29:54 -04:00
|
|
|
|
|
|
|
dc.SetBrush(wxLIGHT_GREY_BRUSH)
|
2002-02-01 20:10:06 -05:00
|
|
|
dc.SetPen(wxPen('BLUE', 4))
|
2003-11-12 16:34:20 -05:00
|
|
|
dc.DrawRectangle((15, 15), (50, 50))
|
1999-04-29 23:29:54 -04:00
|
|
|
|
2001-03-14 03:05:03 -05:00
|
|
|
dc.SetFont(wxFont(14, wxSWISS, wxNORMAL, wxNORMAL))
|
1999-04-29 23:29:54 -04:00
|
|
|
dc.SetTextForeground(wxColour(0xFF, 0x20, 0xFF))
|
|
|
|
te = dc.GetTextExtent("Hello World")
|
2003-11-12 16:34:20 -05:00
|
|
|
dc.DrawText("Hello World", (60, 65))
|
1999-04-29 23:29:54 -04:00
|
|
|
|
2002-02-01 20:10:06 -05:00
|
|
|
dc.SetPen(wxPen('VIOLET', 4))
|
2003-11-12 16:34:20 -05:00
|
|
|
dc.DrawLine((5, 65+te[1]), (60+te[0], 65+te[1]))
|
1999-04-29 23:29:54 -04:00
|
|
|
|
|
|
|
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)])
|
|
|
|
|
2003-11-12 16:34:20 -05:00
|
|
|
dc.DrawBitmap(self.bmp, (200, 20), True)
|
1999-06-22 03:03:29 -04:00
|
|
|
dc.SetTextForeground(wxColour(0, 0xFF, 0x80))
|
2003-11-12 16:34:20 -05:00
|
|
|
dc.DrawText("a bitmap", (200, 85))
|
1999-04-29 23:29:54 -04:00
|
|
|
|
2003-07-02 19:13:10 -04:00
|
|
|
## dc.SetFont(wxFont(14, wxSWISS, wxNORMAL, wxNORMAL))
|
|
|
|
## dc.SetTextForeground("BLACK")
|
|
|
|
## dc.DrawText("TEST this STRING", 10, 200)
|
|
|
|
## print dc.GetFullTextExtent("TEST this STRING")
|
|
|
|
|
1999-12-11 02:59:23 -05:00
|
|
|
font = wxFont(20, wxSWISS, wxNORMAL, wxNORMAL)
|
|
|
|
dc.SetFont(font)
|
|
|
|
dc.SetTextForeground(wxBLACK)
|
|
|
|
for a in range(0, 360, 45):
|
2003-11-12 16:34:20 -05:00
|
|
|
dc.DrawRotatedText("Rotated text...", (300, 300), a)
|
1999-12-11 02:59:23 -05:00
|
|
|
|
2000-01-31 16:07:04 -05:00
|
|
|
dc.SetPen(wxTRANSPARENT_PEN)
|
|
|
|
dc.SetBrush(wxBLUE_BRUSH)
|
2003-11-12 16:34:20 -05:00
|
|
|
dc.DrawRectangle((50,500), (50,50))
|
|
|
|
dc.DrawRectangle((100,500), (50,50))
|
2000-01-31 16:07:04 -05:00
|
|
|
|
2002-02-01 20:10:06 -05:00
|
|
|
dc.SetPen(wxPen('RED'))
|
2003-11-12 16:34:20 -05:00
|
|
|
dc.DrawEllipticArc((200, 500), (50, 75), 0, 90)
|
2000-10-30 16:08:42 -05:00
|
|
|
|
2003-07-02 19:13:10 -04:00
|
|
|
if not printing:
|
|
|
|
# This has troubles when used on a print preview in wxGTK,
|
|
|
|
# probably something to do with the pen styles and the scaling
|
|
|
|
# it does...
|
|
|
|
y = 20
|
|
|
|
for style in [wxDOT, wxLONG_DASH, wxSHORT_DASH, wxDOT_DASH, wxUSER_DASH]:
|
|
|
|
pen = wxPen("DARK ORCHID", 1, style)
|
|
|
|
if style == wxUSER_DASH:
|
|
|
|
pen.SetCap(wxCAP_BUTT)
|
|
|
|
pen.SetDashes([1,2])
|
|
|
|
pen.SetColour("RED")
|
|
|
|
dc.SetPen(pen)
|
2003-11-12 16:34:20 -05:00
|
|
|
dc.DrawLine((300, y), (400, y))
|
2003-07-02 19:13:10 -04:00
|
|
|
y = y + 10
|
2001-09-26 20:27:46 -04:00
|
|
|
|
2002-02-01 20:10:06 -05:00
|
|
|
dc.SetBrush(wxTRANSPARENT_BRUSH)
|
2001-11-28 19:51:59 -05:00
|
|
|
dc.SetPen(wxPen(wxColour(0xFF, 0x20, 0xFF), 1, wxSOLID))
|
2003-11-12 16:34:20 -05:00
|
|
|
dc.DrawRectangle((450, 50), (100, 100))
|
2001-11-28 19:51:59 -05:00
|
|
|
old_pen = dc.GetPen()
|
|
|
|
new_pen = wxPen("BLACK", 5)
|
|
|
|
dc.SetPen(new_pen)
|
2003-11-12 16:34:20 -05:00
|
|
|
dc.DrawRectangle((470, 70), (60, 60))
|
2001-11-28 19:51:59 -05:00
|
|
|
dc.SetPen(old_pen)
|
2003-11-12 16:34:20 -05:00
|
|
|
dc.DrawRectangle((490, 90), (20, 20))
|
2001-11-28 19:51:59 -05:00
|
|
|
|
1999-04-29 23:29:54 -04:00
|
|
|
self.DrawSavedLines(dc)
|
|
|
|
dc.EndDrawing()
|
|
|
|
|
|
|
|
|
|
|
|
def DrawSavedLines(self, dc):
|
2002-02-01 20:10:06 -05:00
|
|
|
dc.SetPen(wxPen('MEDIUM FOREST GREEN', 4))
|
1999-04-29 23:29:54 -04:00
|
|
|
for line in self.lines:
|
|
|
|
for coords in line:
|
2003-11-12 16:34:20 -05:00
|
|
|
dc.DrawLine(*coords)
|
1999-04-29 23:29:54 -04:00
|
|
|
|
|
|
|
|
|
|
|
def SetXY(self, event):
|
|
|
|
self.x, self.y = self.ConvertEventCoords(event)
|
|
|
|
|
|
|
|
def ConvertEventCoords(self, event):
|
2000-08-08 02:11:51 -04:00
|
|
|
xView, yView = self.GetViewStart()
|
1999-04-29 23:29:54 -04:00
|
|
|
xDelta, yDelta = self.GetScrollPixelsPerUnit()
|
|
|
|
return (event.GetX() + (xView * xDelta),
|
|
|
|
event.GetY() + (yView * yDelta))
|
|
|
|
|
|
|
|
def OnLeftButtonEvent(self, event):
|
|
|
|
if event.LeftDown():
|
2003-03-25 01:35:27 -05:00
|
|
|
self.SetFocus()
|
1999-04-29 23:29:54 -04:00
|
|
|
self.SetXY(event)
|
|
|
|
self.curLine = []
|
2000-02-16 00:57:16 -05:00
|
|
|
self.CaptureMouse()
|
2003-03-25 01:35:27 -05:00
|
|
|
self.drawing = True
|
2002-04-08 21:25:47 -04:00
|
|
|
|
|
|
|
elif event.Dragging() and self.drawing:
|
|
|
|
if BUFFERED:
|
|
|
|
# If doing buffered drawing, create the buffered DC, giving it
|
|
|
|
# it a real DC to blit to when done.
|
|
|
|
cdc = wxClientDC(self)
|
|
|
|
self.PrepareDC(cdc)
|
|
|
|
dc = wxBufferedDC(cdc, self.buffer)
|
|
|
|
else:
|
|
|
|
dc = wxClientDC(self)
|
|
|
|
self.PrepareDC(dc)
|
1999-04-29 23:29:54 -04:00
|
|
|
|
|
|
|
dc.BeginDrawing()
|
2002-02-01 20:10:06 -05:00
|
|
|
dc.SetPen(wxPen('MEDIUM FOREST GREEN', 4))
|
2003-11-12 16:34:20 -05:00
|
|
|
coords = ((self.x, self.y), self.ConvertEventCoords(event))
|
1999-04-29 23:29:54 -04:00
|
|
|
self.curLine.append(coords)
|
2003-11-12 16:34:20 -05:00
|
|
|
dc.DrawLine( *coords)
|
1999-04-29 23:29:54 -04:00
|
|
|
self.SetXY(event)
|
|
|
|
dc.EndDrawing()
|
|
|
|
|
2002-04-08 21:25:47 -04:00
|
|
|
|
|
|
|
elif event.LeftUp() and self.drawing:
|
1999-04-29 23:29:54 -04:00
|
|
|
self.lines.append(self.curLine)
|
|
|
|
self.curLine = []
|
2000-02-16 00:57:16 -05:00
|
|
|
self.ReleaseMouse()
|
2003-03-25 01:35:27 -05:00
|
|
|
self.drawing = False
|
1999-04-29 23:29:54 -04:00
|
|
|
|
2001-05-06 01:03:24 -04: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()
|
2002-07-19 20:14:28 -04:00
|
|
|
## print delta, rot, linesPer
|
2001-05-06 01:03:24 -04:00
|
|
|
## 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-29 23:29:54 -04:00
|
|
|
#---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
def runTest(frame, nb, log):
|
|
|
|
win = MyCanvas(nb)
|
|
|
|
return win
|
|
|
|
|
|
|
|
#---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2002-06-26 16:08:30 -04:00
|
|
|
overview = """\
|
|
|
|
"""
|
1999-04-29 23:29:54 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2002-06-26 16:08:30 -04:00
|
|
|
if __name__ == '__main__':
|
|
|
|
import sys,os
|
|
|
|
import run
|
|
|
|
run.main(['', os.path.basename(sys.argv[0])])
|
1999-04-29 23:29:54 -04:00
|
|
|
|