File: demo09.py

package info (click to toggle)
python-wpy 0.53-0.1
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 832 kB
  • ctags: 1,991
  • sloc: python: 8,624; makefile: 57; sh: 24
file content (184 lines) | stat: -rwxr-xr-x 5,173 bytes parent folder | download | duplicates (3)
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#! /usr/local/bin/python

# Create two different views from the same document class.
# Like demo05.py, but add a different view showing the data.
# Note: The title is always the document title in MFC.
# See also demo10.py.

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 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)
    CMenuLine(window)
    data	= CMenuButton(window, "New Data 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 CScribDataView(CScrollView):
  def OnDraw(self, DC):
    y = height = DC.wpyCharSizeY
    x = chars = 0
    DC.DrawText("Stroke List:", x, y)
    y = y + height
    for stroke in self.wpyDocument.m_strokeList:
      text = `stroke.m_pointArray`
      l = len(text)
      if l > chars:
        chars = l
      DC.DrawText(text, x, y)
      y = y + height
    self.wpyHScrollSize = chars * DC.wpyCharSizeX
    self.wpyVScrollSize = y + height * 5
    self.SetScrollSizes()
      

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"
    self.AddDocTemplate(templ)
    templ = CMultiDocTemplate(CScribDoc, CMDIChildWnd, CScribDataView, ScribFrameMenu)
    # templ.wpyText="Title":  Does not work because the document is the same
    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()
  def OnMenuWindowNewdatawindow(self, control):
    templ = self.wpyChildList[1]
    self.WindowNew(templ)

# Start the application, respond to events.
app = MyApp()