1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
|
#! /usr/local/bin/python
from wpy import *
import string
class CScribDoc(CDocument):
def __init__(self):
CDocument.__init__(self)
self.pen = self.thin_pen = CPen(2, (200, 0, 0)).Create()
self.thick_pen = CPen(5, (0, 200, 0)).Create()
self.m_strokeList = []
def NewStroke(self):
s = CStroke(self.pen)
self.m_strokeList.append(s)
self.SetModifiedFlag()
return s
def OnCloseDocument(self):
CDocument.OnCloseDocument(self)
self.pen = None
def DeleteContents(self):
self.m_strokeList = []
def Serialize(self, is_storing):
print "Nothing happens"
def OnMenuPenThinpen(self, control):
if string.find(control.wpyText, "Thick") >= 0:
self.pen = self.thick_pen
else:
self.pen = self.thin_pen
class ScribFrameMenu(CMenu):
def __init__(self):
CMenu.__init__(self)
file = MenuFile(self)
edit = MenuEdit(self)
pen = CMenuButton(self, "Pen")
view = MenuView(self)
window = MenuWindow(self)
help = MenuHelp(self)
# file items:
new = MenuFileNew(file)
close = MenuFileClose(file)
CMenuLine(file)
exit = MenuFileExit(file)
# edit items
# pen items:
thin = CMenuRadio(pen, "Thin pen", None)
thin.wpyMessage = "Toggles the line thickness between thin and thick"
thick = CMenuRadio(pen, "Thick pen", thin)
thick.wpyMessage = thin.wpyMessage
thick.wpyHandler = thin.wpyHandler
# view items
status = MenuViewStatusbar(view)
status.wpyCheckValue = 0
status.EnableMenuItem(0)
# window items
new = MenuWindowNewwindow(window)
cascade = MenuWindowCascade(window)
tileh = MenuWindowTilehorz(window)
tilev = MenuWindowTilevert(window)
arrange = MenuWindowArrangeicons(window)
# help items
about = MenuHelpAbout(help)
class ScribMainFrameMenu(CMenu):
def __init__(self):
CMenu.__init__(self)
file = MenuFile(self)
view = MenuView(self)
help = MenuHelp(self)
# file items:
new = MenuFileNew(file)
CMenuLine(file)
exit = MenuFileExit(file)
# view items
status = MenuViewStatusbar(view)
# help items
about = MenuHelpAbout(help)
class CStroke:
def __init__(self, pen):
self.pen = pen
self.m_pointArray = []
def DrawStroke(self, DC):
DC.SelectObject(self.pen)
i = self.m_pointArray[0]
DC.MoveTo(i[0], i[1])
for i in self.m_pointArray[1:]:
DC.LineTo(i[0], i[1])
class CScribView(CScrollView):
def OnCreate(self, event):
self.m_pStrokeCur = None
self.m_ptPrev = None
def OnDraw(self, DC):
for stroke in self.wpyDocument.m_strokeList:
stroke.DrawStroke(DC)
def OnLButtonDown(self, x, y, flags):
point = (x, y)
self.m_pStrokeCur = self.wpyDocument.NewStroke()
self.m_pStrokeCur.m_pointArray.append(point)
self.SetCapture()
self.m_ptPrev = point
DC = self.GetDC()
DC.SelectObject(self.wpyDocument.pen)
self.ReleaseDC(DC)
return 0
def OnLButtonUp(self, x, y, flags):
if GetCapture() != self:
return
point = (x, y)
DC = self.GetDC()
DC.MoveTo(self.m_ptPrev[0], self.m_ptPrev[1])
DC.LineTo(x, y)
self.ReleaseDC(DC)
self.m_pStrokeCur.m_pointArray.append(point)
ReleaseCapture()
self.wpyDocument.UpdateAllViews(self, None)
self.wpyDocument.SetModifiedFlag(0)
return 0
def OnMouseMove(self, x, y, flags):
if GetCapture() != self:
return
point = (x, y)
self.m_pStrokeCur.m_pointArray.append(point)
DC = self.GetDC()
DC.MoveTo(self.m_ptPrev[0], self.m_ptPrev[1])
DC.LineTo(x, y)
self.ReleaseDC(DC)
self.m_ptPrev = point
return 0
class MyApp(CWinApp):
def InitInstance(self):
templ = CMultiDocTemplate(CScribDoc, CMDIChildWnd, CScribView, ScribFrameMenu)
templ.wpyText = "Scribble" # Title for document windows
self.AddDocTemplate(templ)
main_frame = CMDIFrameWnd()
main_frame.wpyStatusLine = CStatusBar(self, "")
main_frame.wpyText = "Python Scribble Demo" # Title for main frame
# Menu when there are no frames
main_frame.wpyMenu = ScribMainFrameMenu()
main_frame.Create()
self.FileNew()
# Start the application, respond to events.
app = MyApp()
|