]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/samples/doodle/doodle.py
4 This module contains the DoodleWindow class which is a window that you
5 can do simple drawings upon.
9 from wxPython
.wx
import *
11 #----------------------------------------------------------------------
13 class DoodleWindow(wxWindow
):
14 menuColours
= { 100 : 'Black',
34 def __init__(self
, parent
, ID
):
35 wxWindow
.__init
__(self
, parent
, ID
)
36 self
.SetBackgroundColour(wxWHITE
)
39 self
.SetColour("Black")
44 # hook some mouse events
45 EVT_LEFT_DOWN(self
, self
.OnLeftDown
)
46 EVT_LEFT_UP(self
, self
.OnLeftUp
)
47 EVT_RIGHT_UP(self
, self
.OnRightUp
)
48 EVT_MOTION(self
, self
.OnMotion
)
50 # and the refresh event
51 EVT_PAINT(self
, self
.OnPaint
)
58 def SetColour(self
, colour
):
59 """Set a new colour and make a matching pen"""
61 self
.pen
= wxPen(wxNamedColour(self
.colour
), self
.thickness
, wxSOLID
)
65 def SetThickness(self
, num
):
66 """Set a new line thickness and make a matching pen"""
68 self
.pen
= wxPen(wxNamedColour(self
.colour
), self
.thickness
, wxSOLID
)
72 def GetLinesData(self
):
76 def SetLinesData(self
, lines
):
82 """Make a menu that can be popped up later"""
84 keys
= self
.menuColours
.keys()
87 text
= self
.menuColours
[k
]
88 menu
.Append(k
, text
, checkable
=true
)
89 EVT_MENU_RANGE(self
, 100, 200, self
.OnMenuSetColour
)
90 EVT_UPDATE_UI_RANGE(self
, 100, 200, self
.OnCheckMenuColours
)
93 for x
in range(1, self
.maxThickness
+1):
94 menu
.Append(x
, str(x
), checkable
=true
)
95 EVT_MENU_RANGE(self
, 1, self
.maxThickness
, self
.OnMenuSetThickness
)
96 EVT_UPDATE_UI_RANGE(self
, 1, self
.maxThickness
, self
.OnCheckMenuThickness
)
100 # These two event handlers are called before the menu is displayed
101 # to determine which items should be checked.
102 def OnCheckMenuColours(self
, event
):
103 text
= self
.menuColours
[event
.GetId()]
104 if text
== self
.colour
:
108 def OnCheckMenuThickness(self
, event
):
109 if event
.GetId() == self
.thickness
:
115 def OnLeftDown(self
, event
):
116 """called when the left mouse button is pressed"""
118 self
.x
, self
.y
= event
.GetPositionTuple()
122 def OnLeftUp(self
, event
):
123 """called when the left mouse button is released"""
124 self
.lines
.append( (self
.colour
, self
.thickness
, self
.curLine
) )
129 def OnRightUp(self
, event
):
130 """called when the right mouse button is released, will popup the menu"""
131 pt
= event
.GetPosition()
132 self
.PopupMenu(self
.menu
, pt
)
136 def OnMotion(self
, event
):
138 Called when the mouse is in motion. If the left button is
139 dragging then draw a line from the last event position to the
140 current one. Save the coordinants for redraws.
142 if event
.Dragging() and event
.LeftIsDown():
143 dc
= wxClientDC(self
)
146 pos
= event
.GetPositionTuple()
147 coords
= (self
.x
, self
.y
) + pos
148 self
.curLine
.append(coords
)
149 dc
.DrawLine(self
.x
, self
.y
, pos
[0], pos
[1])
154 def OnPaint(self
, event
):
156 Called when the window is exposed. Redraws all the lines that have
161 for colour
, thickness
, line
in self
.lines
:
162 pen
= wxPen(wxNamedColour(colour
), thickness
, wxSOLID
)
165 apply(dc
.DrawLine
, coords
)
169 # Event handlers for the popup menu, uses the event ID to determine
170 # the colour or the thickness to set.
171 def OnMenuSetColour(self
, event
):
172 self
.SetColour(self
.menuColours
[event
.GetId()])
174 def OnMenuSetThickness(self
, event
):
175 self
.SetThickness(event
.GetId())
178 # Observer pattern. Listeners are registered and then notified
179 # whenever doodle settings change.
180 def AddListener(self
, listener
):
181 self
.listeners
.append(listener
)
184 for other
in self
.listeners
:
185 other
.Update(self
.colour
, self
.thickness
)
188 #----------------------------------------------------------------------
190 class DoodleFrame(wxFrame
):
191 def __init__(self
, parent
):
192 wxFrame
.__init
__(self
, parent
, -1, "Doodle Frame", size
=(800,600))
193 self
.doodle
= DoodleWindow(self
, -1)
196 #----------------------------------------------------------------------
198 if __name__
== '__main__':
199 app
= wxPySimpleApp()
200 frame
= DoodleFrame(None)