File: demo11.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 (283 lines) | stat: -rwxr-xr-x 8,810 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#! /usr/local/bin/python

import wpy, wpycon
import htmllib, formatter, string
from types import *

class CParser(htmllib.HTMLParser):
  def anchor_bgn(self, href, name, type):
    htmllib.HTMLParser.anchor_bgn(self, href, name, type)
    self.formatter.writer.anchor_bgn(href, name, type)
  def anchor_end(self):
    htmllib.HTMLParser.anchor_end(self)
    self.formatter.writer.anchor_end()

class CWriter:
  FontSizeDict = {"h1":48, "h2":36, "h3":24, "h4":20, "h5":18, "h6":16}
  def __init__(self, view, DC):
    self.view = view
    self.DC = DC
    self.anchor = None
    self.lineHeight = 0
    self.atbreak = 0
    f = self.font = wpy.CFont("roman").Create()
    self.fontDict = {None:f}
    DC.SelectObject(self.font)
    x, y = DC.GetTextExtent("W" * 20)
    x = (x + 19) / 20	# Largest character size
    self.font.sizex = x
    self.font.sizey = self.oldLineHeight = y
    self.indentSize = x * 3
    self.x = self.indent = self.baseIndent = x / 3
    self.y = 0
    self.width = DC.wpySizeX - x
  def anchor_bgn(self, href, name, type):
    if href:
      self.oldcolor = self.DC.SetTextColor((0, 0, 200))
      self.anchor = (href, name, type)
  def anchor_end(self):
    if self.anchor:
      self.DC.SetTextColor(self.oldcolor)
      self.anchor = None
  # Start of methods required by the formatter
  def new_font(self, font):
    # font is None, or the tuple (size, i, b, tt)
    if self.font == font:
      return
    try:
      self.font = self.fontDict[font]
      self.DC.SelectObject(self.font)
    except KeyError:
      size = font[0]
      try:
        height = self.FontSizeDict[size]
      except KeyError:
        height = 0
      if font[3]:
        family = "modern"
      elif type(size) is StringType and size[0] == "h":
        family = "swiss"
      else:
        family = "roman"
      if font[1]:	# Italic indicator
        family = family + "-italic"
      if font[2]:	# Bold indicator
        weight = wpycon.FW_BOLD
      else:
        weight = wpycon.FW_NORMAL
      f = self.font = wpy.CFont(family, height, weight).Create()
      self.DC.SelectObject(f)
      x, f.sizey = self.DC.GetTextExtent("W" * 20)
      f.sizex = (x + 19) / 20	# Largest character size
      self.fontDict[font] = f
  def new_margin(self, margin, level):
    self.send_line_break()
    self.indent = self.x = self.baseIndent + self.indentSize * level
  def new_spacing(self, spacing):
    self.send_line_break()
    t = "new_spacing(%s)" % `spacing`
    self.OutputLine(t, 1)
  def new_styles(self, styles):
    self.send_line_break()
    t = "new_styles(%s)" % `styles`
    self.OutputLine(t, 1)
  def send_label_data(self, data):
    if data == "*":
      w = self.font.sizex * 7 / 10
      x = self.indent - w - w * 4 / 10
      y = self.y - w + self.font.sizex
      self.DC.Rectangle(x, y, w, w)
    else:
      w, h = self.DC.GetTextExtent(data)
      x = self.indent - w - self.font.sizex / 3
      if x < 0:
        x = 0
      self.DC.DrawText(data, x, self.y)
  def send_paragraph(self, blankline):
    self.send_line_break()
    self.y = self.y + self.oldLineHeight * blankline
    self.view.wpyVScrollSize = self.y + 50
  def send_line_break(self):
    if self.lineHeight:
      self.y = self.y + self.lineHeight
      self.oldLineHeight = self.lineHeight
      self.lineHeight = 0
      self.view.wpyVScrollSize = self.y + 50
    self.x = self.indent
    self.atbreak = 0
  def send_hor_rule(self):
    self.send_line_break()
    self.y = self.y + self.oldLineHeight
    border = self.font.sizex
    pen = wpy.CPen(3, (0, 0, 0)).Create()
    oldpen = self.DC.SelectObject(pen)
    self.DC.MoveTo(border, self.y)
    self.DC.LineTo(self.width - border, self.y)
    self.y = self.y + self.oldLineHeight
    self.DC.SelectObject(oldpen)
    self.view.wpyVScrollSize = self.y + 50
  def send_literal_data(self, data):
    if not data:
      return
    lines = string.splitfields(data, '\n')
    text = string.expandtabs(lines[0])
    for l in lines[1:]:
      self.OutputLine(text, 1)
      text = string.expandtabs(l)
    self.OutputLine(text, 0)
    self.atbreak = 0
  def send_flowing_data(self, data):
    if not data:
      return
    atbreak = self.atbreak or data[0] in string.whitespace
    text = ""
    pixels = chars = 0
    for word in string.split(data):
      bword = " " + word	# blank + word
      length = len(bword)
      # The current line is "text" and its size is
      #    "pixels" pixels plus "chars" characters.
      if not atbreak:
        text = word
        chars = chars + length - 1
      elif self.x + pixels + (chars + length) * self.font.sizex < self.width:
        # Word fits easily on current line.
        text = text + bword
        chars = chars + length
      else:
        w, h = self.DC.GetTextExtent(text + bword)
        if self.x + w < self.width:
          # Word fits.
          text = text + bword
          pixels = w
          chars = 0
        else:
          # Word does not fit.  Output current line.
          self.OutputLine(text, 1)
          text = word
          chars = length - 1
          pixels = 0
      atbreak = 1
    self.OutputLine(text, 0)
    self.atbreak = data[-1] in string.whitespace
  # Start of methods used to support the above methods
  def OutputLine(self, text, linebreak = 0):
    if text:
      o = self.DC.DrawText(text, self.x, self.y)
      if self.anchor:
        o.anchor = self.anchor
      self.lineHeight = max(self.lineHeight, o.wpySizeY)
      self.x = self.x + o.wpySizeX
    if linebreak:
      self.send_line_break()

class MyDocument(wpy.CDocument):
  def __init__(self):
    self.filelines = []
    wpy.CDocument.__init__(self)
  def DeleteContents(self):
    del self.filelines[:]
  def SerializeIn(self, file):
    filelines = self.filelines
    del filelines[:]
    while 1:
      text = file.readline()
      if not text:
        break
      filelines.append(text)

class MyFrame(wpy.CFrameWnd):
  def __init__(self):
    wpy.CFrameWnd.__init__(self)
    self.wpySizeX = self.wpyScreenSizeX  * 8 / 10
    self.wpySizeY = self.wpyScreenSizeY  * 8 / 10
  def OnCreateClient(self, context):
    s1 = wpy.CSplitterWnd(self, 2, 2)	# Two rows, 1 col
    s1.CreateStatic()
    s1.CreateView(0, 0, MyView)
    s1.CreateView(1, 0, SourceView)
    s1.CreateView(0, 1, SourceView)
    s1.CreateView(1, 1, MyView)
    s1.SetRowInfo(0, self.wpySizeY / 2, 0)
    s1.SetRowInfo(1, 0, 0)
    s1.SetColumnInfo(0, self.wpySizeX / 2, 0)
    s1.SetColumnInfo(1, 0, 0)

class SourceView(wpy.CScrollView):	# View of source text
  def OnDraw(self, DC):
    doc = self.wpyDocument
    filelines = doc.filelines
    xmax = 0
    if not filelines:
      self.wpyVScrollSize = 0
      self.wpyHScrollSize = 0
      self.SetScrollSizes()
      return
    x, height = DC.GetTextExtent("00000000")
    y = margin = height / 3
    for line in filelines:
      o = DC.DrawText(line, margin, y)
      xmax = max(xmax, o.wpySizeX)
      y = y + height
    self.wpyVScrollSize = max(y + height, self.wpyScreenSizeY)
    self.wpyHScrollSize = xmax + height
    self.SetScrollSizes()

class MyView(wpy.CScrollView):	# View of formatted document
  def OnDraw(self, DC):
    doc = self.wpyDocument
    filelines = doc.filelines
    if not filelines:
      self.wpyVScrollSize = 0
      self.wpyHScrollSize = 0
      self.SetScrollSizes()
      return
    self.BeginWaitCursor()
    writer = CWriter(self, DC)
    w = self.wpyScreenSizeX * 6 / 10		# Write fixed width
    writer.width = w
    self.wpyHScrollSize = w
    fmt = formatter.AbstractFormatter(writer)
    parser = CParser(fmt)
    for line in filelines:
      parser.feed(line)
    parser.close()
    self.EndWaitCursor()
    self.wpyVScrollSize = max(self.wpyVScrollSize, self.wpyScreenSizeY)
    self.SetScrollSizes()
  def OnLButtonDown(self, x, y, flags):
    (x0, y0) = self.GetDeviceScrollPosition()
    x = x + x0
    y = y + y0
    drawn = self.GetDrawnObject(x, y)
    if drawn and hasattr(drawn, "anchor"):
      print "anchor: ", drawn.anchor

class MyMenu(wpy.CMenu):
  def __init__(self):
    wpy.CMenu.__init__(self)
    file = wpy.MenuFile(self)
    help = wpy.MenuHelp(self)

    wpy.MenuFileNew(file)
    wpy.MenuFileOpen(file)
    wpy.MenuLine(file)
    wpy.MenuFileExit(file)

    wpy.CMenuButton(help, "Help")
    wpy.MenuLine(help)
    wpy.MenuHelpAbout(help)
      
class MyApp(wpy.CWinApp):
  def InitInstance(self):
    templ = wpy.CSingleDocTemplate(MyDocument, MyFrame, MyView, MyMenu)
    templ.wpyText = "Python HTML Viewer"
    templ.wpyFilter = ("HTML docs [*.htm*]", ".htm*")
    self.AddDocTemplate(templ)
    self.FileNew()
  def OnMenuHelpHelp(self, control):
    wpy.AfxMessageBox("This is a very simple html viewer.",
      wpycon.MB_OK | wpycon.MB_ICONINFORMATION)

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