1999-04-29 23:29:54 -04:00
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
import wx
|
|
|
|
import images
|
2001-04-09 15:36:36 -04:00
|
|
|
|
2002-04-08 21:25:47 -04:00
|
|
|
BUFFERED = 1
|
|
|
|
|
1999-04-29 23:29:54 -04:00
|
|
|
#---------------------------------------------------------------------------
|
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
class MyCanvas(wx.ScrolledWindow):
|
2003-12-11 14:55:48 -05:00
|
|
|
def __init__(self, parent, id = -1, size = wx.DefaultSize):
|
2003-12-08 20:23:28 -05:00
|
|
|
wx.ScrolledWindow.__init__(self, parent, id, (0, 0), size=size, style=wx.SUNKEN_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")
|
2003-12-08 20:23:28 -05:00
|
|
|
self.SetCursor(wx.StockCursor(wx.CURSOR_PENCIL))
|
2001-04-09 15:36:36 -04:00
|
|
|
bmp = images.getTest2Bitmap()
|
2004-05-01 22:41:33 -04:00
|
|
|
mask = wx.Mask(bmp, wx.BLUE)
|
1999-12-07 13:20:01 -05:00
|
|
|
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.
|
2003-12-08 20:23:28 -05:00
|
|
|
self.buffer = wx.EmptyBitmap(self.maxWidth, self.maxHeight)
|
|
|
|
dc = wx.BufferedDC(None, self.buffer)
|
|
|
|
dc.SetBackground(wx.Brush(self.GetBackgroundColour()))
|
2002-04-08 21:25:47 -04:00
|
|
|
dc.Clear()
|
|
|
|
self.DoDrawing(dc)
|
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
self.Bind(wx.EVT_LEFT_DOWN, self.OnLeftButtonEvent)
|
|
|
|
self.Bind(wx.EVT_LEFT_UP, self.OnLeftButtonEvent)
|
|
|
|
self.Bind(wx.EVT_MOTION, self.OnLeftButtonEvent)
|
|
|
|
self.Bind(wx.EVT_PAINT, self.OnPaint)
|
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
|
2003-12-08 20:23:28 -05:00
|
|
|
# wx.PaintDC and then blit the bitmap to it when dc is
|
2002-04-08 21:25:47 -04:00
|
|
|
# deleted. Since we don't need to draw anything else
|
|
|
|
# here that's all there is to it.
|
2003-12-08 20:23:28 -05:00
|
|
|
dc = wx.BufferedPaintDC(self, self.buffer)
|
2002-04-08 21:25:47 -04:00
|
|
|
else:
|
2003-12-08 20:23:28 -05:00
|
|
|
dc = wx.PaintDC(self)
|
2002-04-08 21:25:47 -04:00
|
|
|
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()
|
2003-12-08 20:23:28 -05:00
|
|
|
dc.SetPen(wx.Pen('RED'))
|
2004-05-01 22:41:33 -04:00
|
|
|
dc.DrawRectangle(5, 5, 50, 50)
|
1999-04-29 23:29:54 -04:00
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
dc.SetBrush(wx.LIGHT_GREY_BRUSH)
|
|
|
|
dc.SetPen(wx.Pen('BLUE', 4))
|
2004-05-01 22:41:33 -04:00
|
|
|
dc.DrawRectangle(15, 15, 50, 50)
|
1999-04-29 23:29:54 -04:00
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
dc.SetFont(wx.Font(14, wx.SWISS, wx.NORMAL, wx.NORMAL))
|
|
|
|
dc.SetTextForeground(wx.Colour(0xFF, 0x20, 0xFF))
|
1999-04-29 23:29:54 -04:00
|
|
|
te = dc.GetTextExtent("Hello World")
|
2004-05-01 22:41:33 -04:00
|
|
|
dc.DrawText("Hello World", 60, 65)
|
1999-04-29 23:29:54 -04:00
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
dc.SetPen(wx.Pen('VIOLET', 4))
|
2004-05-01 22:41:33 -04: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)
|
2003-12-08 20:23:28 -05:00
|
|
|
dc.SetPen(wx.GREY_PEN)
|
1999-04-29 23:29:54 -04:00
|
|
|
dc.DrawPolygon(lst, 75)
|
2003-12-08 20:23:28 -05:00
|
|
|
dc.SetPen(wx.GREEN_PEN)
|
1999-04-29 23:29:54 -04:00
|
|
|
dc.DrawSpline(lst+[(100,100)])
|
|
|
|
|
2004-05-01 22:41:33 -04:00
|
|
|
dc.DrawBitmap(self.bmp, 200, 20, True)
|
2003-12-08 20:23:28 -05:00
|
|
|
dc.SetTextForeground(wx.Colour(0, 0xFF, 0x80))
|
2004-05-01 22:41:33 -04:00
|
|
|
dc.DrawText("a bitmap", 200, 85)
|
1999-04-29 23:29:54 -04:00
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
## dc.SetFont(wx.Font(14, wx.SWISS, wx.NORMAL, wx.NORMAL))
|
2003-07-02 19:13:10 -04:00
|
|
|
## dc.SetTextForeground("BLACK")
|
2004-05-01 22:41:33 -04:00
|
|
|
## dc.DrawText("TEST this STRING", 10, 200)
|
2003-07-02 19:13:10 -04:00
|
|
|
## print dc.GetFullTextExtent("TEST this STRING")
|
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
font = wx.Font(20, wx.SWISS, wx.NORMAL, wx.NORMAL)
|
1999-12-11 02:59:23 -05:00
|
|
|
dc.SetFont(font)
|
2003-12-08 20:23:28 -05:00
|
|
|
dc.SetTextForeground(wx.BLACK)
|
|
|
|
|
1999-12-11 02:59:23 -05:00
|
|
|
for a in range(0, 360, 45):
|
2004-05-01 22:41:33 -04:00
|
|
|
dc.DrawRotatedText("Rotated text...", 300, 300, a)
|
1999-12-11 02:59:23 -05:00
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
dc.SetPen(wx.TRANSPARENT_PEN)
|
|
|
|
dc.SetBrush(wx.BLUE_BRUSH)
|
2004-05-01 22:41:33 -04:00
|
|
|
dc.DrawRectangle(50,500, 50,50)
|
|
|
|
dc.DrawRectangle(100,500, 50,50)
|
2000-01-31 16:07:04 -05:00
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
dc.SetPen(wx.Pen('RED'))
|
2004-05-01 22:41:33 -04: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
|
2003-12-08 20:23:28 -05:00
|
|
|
|
|
|
|
for style in [wx.DOT, wx.LONG_DASH, wx.SHORT_DASH, wx.DOT_DASH, wx.USER_DASH]:
|
|
|
|
pen = wx.Pen("DARK ORCHID", 1, style)
|
|
|
|
if style == wx.USER_DASH:
|
|
|
|
pen.SetCap(wx.CAP_BUTT)
|
2003-07-02 19:13:10 -04:00
|
|
|
pen.SetDashes([1,2])
|
|
|
|
pen.SetColour("RED")
|
|
|
|
dc.SetPen(pen)
|
2004-05-01 22:41:33 -04: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
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
dc.SetBrush(wx.TRANSPARENT_BRUSH)
|
|
|
|
dc.SetPen(wx.Pen(wx.Colour(0xFF, 0x20, 0xFF), 1, wx.SOLID))
|
2004-05-01 22:41:33 -04:00
|
|
|
dc.DrawRectangle(450,50, 100,100)
|
2001-11-28 19:51:59 -05:00
|
|
|
old_pen = dc.GetPen()
|
2003-12-08 20:23:28 -05:00
|
|
|
new_pen = wx.Pen("BLACK", 5)
|
2001-11-28 19:51:59 -05:00
|
|
|
dc.SetPen(new_pen)
|
2004-05-01 22:41:33 -04:00
|
|
|
dc.DrawRectangle(470,70, 60,60)
|
2001-11-28 19:51:59 -05:00
|
|
|
dc.SetPen(old_pen)
|
2004-05-01 22:41:33 -04: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):
|
2003-12-08 20:23:28 -05:00
|
|
|
dc.SetPen(wx.Pen('MEDIUM FOREST GREEN', 4))
|
|
|
|
|
1999-04-29 23:29:54 -04:00
|
|
|
for line in self.lines:
|
|
|
|
for coords in line:
|
2003-12-08 20:23:28 -05:00
|
|
|
apply(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.
|
2003-12-08 20:23:28 -05:00
|
|
|
cdc = wx.ClientDC(self)
|
2002-04-08 21:25:47 -04:00
|
|
|
self.PrepareDC(cdc)
|
2003-12-08 20:23:28 -05:00
|
|
|
dc = wx.BufferedDC(cdc, self.buffer)
|
2002-04-08 21:25:47 -04:00
|
|
|
else:
|
2003-12-08 20:23:28 -05:00
|
|
|
dc = wx.ClientDC(self)
|
2002-04-08 21:25:47 -04:00
|
|
|
self.PrepareDC(dc)
|
1999-04-29 23:29:54 -04:00
|
|
|
|
|
|
|
dc.BeginDrawing()
|
2003-12-08 20:23:28 -05:00
|
|
|
dc.SetPen(wx.Pen('MEDIUM FOREST GREEN', 4))
|
2004-05-01 22:41:33 -04:00
|
|
|
coords = (self.x, self.y) + self.ConvertEventCoords(event)
|
1999-04-29 23:29:54 -04:00
|
|
|
self.curLine.append(coords)
|
2004-05-01 22:41:33 -04: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,
|
2004-01-02 20:14:17 -05:00
|
|
|
## but since wx.ScrolledWindow does this already it's not
|
2003-12-08 20:23:28 -05:00
|
|
|
## necessary to do it ourselves. You would need to add an event table
|
|
|
|
## entry to __init__() to direct wheelmouse events to this handler.
|
2001-05-06 01:03:24 -04:00
|
|
|
|
|
|
|
## 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
|
|
|
|
|
|
|
|
#---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
overview = """
|
|
|
|
<html>
|
|
|
|
<body>
|
|
|
|
The wx.ScrolledWindow class manages scrolling for its client area, transforming the
|
|
|
|
coordinates according to the scrollbar positions, and setting the scroll positions,
|
|
|
|
thumb sizes and ranges according to the area in view.
|
|
|
|
</body>
|
|
|
|
</html>
|
2002-06-26 16:08:30 -04:00
|
|
|
"""
|
1999-04-29 23:29:54 -04:00
|
|
|
|
|
|
|
|
2002-06-26 16:08:30 -04:00
|
|
|
if __name__ == '__main__':
|
|
|
|
import sys,os
|
|
|
|
import run
|
2004-03-04 19:06:33 -05:00
|
|
|
run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:])
|
1999-04-29 23:29:54 -04:00
|
|
|
|