2001-03-10 00:07:25 -05:00
|
|
|
# doodle.py
|
|
|
|
|
|
|
|
"""
|
|
|
|
This module contains the DoodleWindow class which is a window that you
|
|
|
|
can do simple drawings upon.
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
2003-07-02 19:13:10 -04:00
|
|
|
import wx # This module uses the new wx namespace
|
2001-03-10 00:07:25 -05:00
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
2003-07-02 19:13:10 -04:00
|
|
|
class DoodleWindow(wx.Window):
|
2001-03-10 00:07:25 -05:00
|
|
|
menuColours = { 100 : 'Black',
|
|
|
|
101 : 'Yellow',
|
|
|
|
102 : 'Red',
|
|
|
|
103 : 'Green',
|
|
|
|
104 : 'Blue',
|
|
|
|
105 : 'Purple',
|
|
|
|
106 : 'Brown',
|
|
|
|
107 : 'Aquamarine',
|
|
|
|
108 : 'Forest Green',
|
|
|
|
109 : 'Light Blue',
|
|
|
|
110 : 'Goldenrod',
|
|
|
|
111 : 'Cyan',
|
|
|
|
112 : 'Orange',
|
|
|
|
113 : 'Navy',
|
|
|
|
114 : 'Dark Grey',
|
|
|
|
115 : 'Light Grey',
|
|
|
|
}
|
|
|
|
maxThickness = 16
|
|
|
|
|
|
|
|
|
|
|
|
def __init__(self, parent, ID):
|
2003-07-02 19:13:10 -04:00
|
|
|
wx.Window.__init__(self, parent, ID, style=wx.NO_FULL_REPAINT_ON_RESIZE)
|
2002-04-12 20:39:12 -04:00
|
|
|
self.SetBackgroundColour("WHITE")
|
2001-03-10 00:07:25 -05:00
|
|
|
self.listeners = []
|
|
|
|
self.thickness = 1
|
|
|
|
self.SetColour("Black")
|
|
|
|
self.lines = []
|
2004-01-12 22:28:11 -05:00
|
|
|
self.pos = wx.Point(0,0)
|
2001-03-10 00:07:25 -05:00
|
|
|
self.MakeMenu()
|
|
|
|
|
2002-04-12 20:39:12 -04:00
|
|
|
self.InitBuffer()
|
|
|
|
|
2004-01-12 22:28:11 -05:00
|
|
|
self.SetCursor(wx.StockCursor(wx.CURSOR_PENCIL))
|
|
|
|
|
2001-03-10 00:07:25 -05:00
|
|
|
# hook some mouse events
|
2004-01-12 22:28:11 -05:00
|
|
|
self.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown)
|
|
|
|
self.Bind(wx.EVT_LEFT_UP, self.OnLeftUp)
|
|
|
|
self.Bind(wx.EVT_RIGHT_UP, self.OnRightUp)
|
|
|
|
self.Bind(wx.EVT_MOTION, self.OnMotion)
|
2001-03-10 00:07:25 -05:00
|
|
|
|
2002-04-12 20:39:12 -04:00
|
|
|
# the window resize event and idle events for managing the buffer
|
2004-01-12 22:28:11 -05:00
|
|
|
self.Bind(wx.EVT_SIZE, self.OnSize)
|
|
|
|
self.Bind(wx.EVT_IDLE, self.OnIdle)
|
2002-04-12 20:39:12 -04:00
|
|
|
|
2001-03-10 00:07:25 -05:00
|
|
|
# and the refresh event
|
2004-01-12 22:28:11 -05:00
|
|
|
self.Bind(wx.EVT_PAINT, self.OnPaint)
|
2001-03-10 00:07:25 -05:00
|
|
|
|
2002-04-18 01:35:20 -04:00
|
|
|
# When the window is destroyed, clean up resources.
|
2004-01-12 22:28:11 -05:00
|
|
|
self.Bind(wx.EVT_WINDOW_DESTROY, self.Cleanup)
|
2001-03-10 00:07:25 -05:00
|
|
|
|
2002-04-12 20:39:12 -04:00
|
|
|
|
2002-04-18 01:35:20 -04:00
|
|
|
def Cleanup(self, evt):
|
2002-04-12 20:39:12 -04:00
|
|
|
if hasattr(self, "menu"):
|
|
|
|
self.menu.Destroy()
|
|
|
|
del self.menu
|
|
|
|
|
|
|
|
|
|
|
|
def InitBuffer(self):
|
|
|
|
"""Initialize the bitmap used for buffering the display."""
|
|
|
|
size = self.GetClientSize()
|
2004-08-16 11:11:18 -04:00
|
|
|
self.buffer = wx.EmptyBitmap(max(1,size.width), max(1,size.height))
|
2003-07-02 19:13:10 -04:00
|
|
|
dc = wx.BufferedDC(None, self.buffer)
|
|
|
|
dc.SetBackground(wx.Brush(self.GetBackgroundColour()))
|
2002-04-12 20:39:12 -04:00
|
|
|
dc.Clear()
|
|
|
|
self.DrawLines(dc)
|
2003-03-25 01:35:27 -05:00
|
|
|
self.reInitBuffer = False
|
2001-03-10 00:07:25 -05:00
|
|
|
|
|
|
|
|
|
|
|
def SetColour(self, colour):
|
|
|
|
"""Set a new colour and make a matching pen"""
|
|
|
|
self.colour = colour
|
2003-07-02 19:13:10 -04:00
|
|
|
self.pen = wx.Pen(self.colour, self.thickness, wx.SOLID)
|
2001-03-10 00:07:25 -05:00
|
|
|
self.Notify()
|
|
|
|
|
|
|
|
|
|
|
|
def SetThickness(self, num):
|
|
|
|
"""Set a new line thickness and make a matching pen"""
|
|
|
|
self.thickness = num
|
2003-07-02 19:13:10 -04:00
|
|
|
self.pen = wx.Pen(self.colour, self.thickness, wx.SOLID)
|
2001-03-10 00:07:25 -05:00
|
|
|
self.Notify()
|
|
|
|
|
|
|
|
|
|
|
|
def GetLinesData(self):
|
|
|
|
return self.lines[:]
|
|
|
|
|
|
|
|
|
|
|
|
def SetLinesData(self, lines):
|
|
|
|
self.lines = lines[:]
|
2002-04-12 20:39:12 -04:00
|
|
|
self.InitBuffer()
|
2001-03-10 00:07:25 -05:00
|
|
|
self.Refresh()
|
|
|
|
|
|
|
|
|
|
|
|
def MakeMenu(self):
|
|
|
|
"""Make a menu that can be popped up later"""
|
2003-07-02 19:13:10 -04:00
|
|
|
menu = wx.Menu()
|
2001-03-10 00:07:25 -05:00
|
|
|
keys = self.menuColours.keys()
|
|
|
|
keys.sort()
|
|
|
|
for k in keys:
|
|
|
|
text = self.menuColours[k]
|
2003-07-02 19:13:10 -04:00
|
|
|
menu.Append(k, text, kind=wx.ITEM_CHECK)
|
2004-01-12 22:28:11 -05:00
|
|
|
self.Bind(wx.EVT_MENU_RANGE, self.OnMenuSetColour, id=100, id2=200)
|
|
|
|
self.Bind(wx.EVT_UPDATE_UI_RANGE, self.OnCheckMenuColours, id=100, id2=200)
|
2001-03-10 00:07:25 -05:00
|
|
|
menu.Break()
|
|
|
|
|
|
|
|
for x in range(1, self.maxThickness+1):
|
2003-07-02 19:13:10 -04:00
|
|
|
menu.Append(x, str(x), kind=wx.ITEM_CHECK)
|
2004-01-12 22:28:11 -05:00
|
|
|
|
|
|
|
self.Bind(wx.EVT_MENU_RANGE, self.OnMenuSetThickness, id=1, id2=self.maxThickness)
|
|
|
|
self.Bind(wx.EVT_UPDATE_UI_RANGE, self.OnCheckMenuThickness, id=1, id2=self.maxThickness)
|
2001-03-10 00:07:25 -05:00
|
|
|
self.menu = menu
|
|
|
|
|
|
|
|
|
|
|
|
# These two event handlers are called before the menu is displayed
|
|
|
|
# to determine which items should be checked.
|
|
|
|
def OnCheckMenuColours(self, event):
|
|
|
|
text = self.menuColours[event.GetId()]
|
|
|
|
if text == self.colour:
|
2003-03-25 01:35:27 -05:00
|
|
|
event.Check(True)
|
2003-07-02 19:13:10 -04:00
|
|
|
event.SetText(text.upper())
|
2001-03-10 00:07:25 -05:00
|
|
|
else:
|
2003-03-25 01:35:27 -05:00
|
|
|
event.Check(False)
|
2003-07-02 19:13:10 -04:00
|
|
|
event.SetText(text)
|
|
|
|
|
2001-03-10 00:07:25 -05:00
|
|
|
def OnCheckMenuThickness(self, event):
|
|
|
|
if event.GetId() == self.thickness:
|
2003-03-25 01:35:27 -05:00
|
|
|
event.Check(True)
|
2001-03-10 00:07:25 -05:00
|
|
|
else:
|
2003-03-25 01:35:27 -05:00
|
|
|
event.Check(False)
|
2001-03-10 00:07:25 -05:00
|
|
|
|
|
|
|
|
|
|
|
def OnLeftDown(self, event):
|
|
|
|
"""called when the left mouse button is pressed"""
|
|
|
|
self.curLine = []
|
2004-01-12 22:28:11 -05:00
|
|
|
self.pos = event.GetPosition()
|
2001-03-10 00:07:25 -05:00
|
|
|
self.CaptureMouse()
|
|
|
|
|
|
|
|
|
|
|
|
def OnLeftUp(self, event):
|
|
|
|
"""called when the left mouse button is released"""
|
2002-04-12 20:39:12 -04:00
|
|
|
if self.HasCapture():
|
|
|
|
self.lines.append( (self.colour, self.thickness, self.curLine) )
|
|
|
|
self.curLine = []
|
|
|
|
self.ReleaseMouse()
|
2001-03-10 00:07:25 -05:00
|
|
|
|
|
|
|
|
|
|
|
def OnRightUp(self, event):
|
|
|
|
"""called when the right mouse button is released, will popup the menu"""
|
|
|
|
pt = event.GetPosition()
|
|
|
|
self.PopupMenu(self.menu, pt)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def OnMotion(self, event):
|
|
|
|
"""
|
|
|
|
Called when the mouse is in motion. If the left button is
|
|
|
|
dragging then draw a line from the last event position to the
|
|
|
|
current one. Save the coordinants for redraws.
|
|
|
|
"""
|
|
|
|
if event.Dragging() and event.LeftIsDown():
|
2003-07-02 19:13:10 -04:00
|
|
|
dc = wx.BufferedDC(wx.ClientDC(self), self.buffer)
|
2001-03-10 00:07:25 -05:00
|
|
|
dc.BeginDrawing()
|
|
|
|
dc.SetPen(self.pen)
|
2004-01-12 22:28:11 -05:00
|
|
|
pos = event.GetPosition()
|
2004-05-28 00:51:08 -04:00
|
|
|
coords = (self.pos.x, self.pos.y, pos.x, pos.y)
|
2001-03-10 00:07:25 -05:00
|
|
|
self.curLine.append(coords)
|
2004-01-12 22:28:11 -05:00
|
|
|
dc.DrawLine(*coords)
|
|
|
|
self.pos = pos
|
2001-03-10 00:07:25 -05:00
|
|
|
dc.EndDrawing()
|
|
|
|
|
|
|
|
|
2002-04-12 20:39:12 -04:00
|
|
|
def OnSize(self, event):
|
|
|
|
"""
|
|
|
|
Called when the window is resized. We set a flag so the idle
|
|
|
|
handler will resize the buffer.
|
|
|
|
"""
|
2003-03-25 01:35:27 -05:00
|
|
|
self.reInitBuffer = True
|
2002-04-12 20:39:12 -04:00
|
|
|
|
|
|
|
|
|
|
|
def OnIdle(self, event):
|
|
|
|
"""
|
|
|
|
If the size was changed then resize the bitmap used for double
|
|
|
|
buffering to match the window size. We do it in Idle time so
|
|
|
|
there is only one refresh after resizing is done, not lots while
|
|
|
|
it is happening.
|
|
|
|
"""
|
|
|
|
if self.reInitBuffer:
|
|
|
|
self.InitBuffer()
|
2003-03-25 01:35:27 -05:00
|
|
|
self.Refresh(False)
|
2002-04-12 20:39:12 -04:00
|
|
|
|
|
|
|
|
2001-03-10 00:07:25 -05:00
|
|
|
def OnPaint(self, event):
|
|
|
|
"""
|
2002-04-12 20:39:12 -04:00
|
|
|
Called when the window is exposed.
|
|
|
|
"""
|
|
|
|
# Create a buffered paint DC. It will create the real
|
2003-07-02 19:13:10 -04:00
|
|
|
# wx.PaintDC and then blit the bitmap to it when dc is
|
2002-04-12 20:39:12 -04:00
|
|
|
# deleted. Since we don't need to draw anything else
|
|
|
|
# here that's all there is to it.
|
2003-07-02 19:13:10 -04:00
|
|
|
dc = wx.BufferedPaintDC(self, self.buffer)
|
2002-04-12 20:39:12 -04:00
|
|
|
|
|
|
|
|
|
|
|
def DrawLines(self, dc):
|
|
|
|
"""
|
|
|
|
Redraws all the lines that have been drawn already.
|
2001-03-10 00:07:25 -05:00
|
|
|
"""
|
|
|
|
dc.BeginDrawing()
|
|
|
|
for colour, thickness, line in self.lines:
|
2003-07-02 19:13:10 -04:00
|
|
|
pen = wx.Pen(colour, thickness, wx.SOLID)
|
2001-03-10 00:07:25 -05:00
|
|
|
dc.SetPen(pen)
|
|
|
|
for coords in line:
|
2004-01-12 22:28:11 -05:00
|
|
|
dc.DrawLine(*coords)
|
2001-03-10 00:07:25 -05:00
|
|
|
dc.EndDrawing()
|
|
|
|
|
|
|
|
|
|
|
|
# Event handlers for the popup menu, uses the event ID to determine
|
|
|
|
# the colour or the thickness to set.
|
|
|
|
def OnMenuSetColour(self, event):
|
|
|
|
self.SetColour(self.menuColours[event.GetId()])
|
|
|
|
|
|
|
|
def OnMenuSetThickness(self, event):
|
|
|
|
self.SetThickness(event.GetId())
|
|
|
|
|
|
|
|
|
|
|
|
# Observer pattern. Listeners are registered and then notified
|
|
|
|
# whenever doodle settings change.
|
|
|
|
def AddListener(self, listener):
|
|
|
|
self.listeners.append(listener)
|
|
|
|
|
|
|
|
def Notify(self):
|
|
|
|
for other in self.listeners:
|
|
|
|
other.Update(self.colour, self.thickness)
|
|
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
2003-07-02 19:13:10 -04:00
|
|
|
class DoodleFrame(wx.Frame):
|
2001-03-10 00:07:25 -05:00
|
|
|
def __init__(self, parent):
|
2003-07-02 19:13:10 -04:00
|
|
|
wx.Frame.__init__(self, parent, -1, "Doodle Frame", size=(800,600),
|
|
|
|
style=wx.DEFAULT_FRAME_STYLE | wx.NO_FULL_REPAINT_ON_RESIZE)
|
2002-04-12 20:39:12 -04:00
|
|
|
doodle = DoodleWindow(self, -1)
|
2001-03-10 00:07:25 -05:00
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2003-07-02 19:13:10 -04:00
|
|
|
app = wx.PySimpleApp()
|
2001-04-30 14:34:21 -04:00
|
|
|
frame = DoodleFrame(None)
|
2003-03-25 01:35:27 -05:00
|
|
|
frame.Show(True)
|
2001-03-10 00:07:25 -05:00
|
|
|
app.MainLoop()
|
|
|
|
|