annotate Lib/Para.py @ 4566:6403c94de50d legacy-trunk

Added safeguard against failure in __del__.
author Guido van Rossum <guido@python.org>
date Wed, 27 Nov 1996 19:39:58 +0000
parents 34182a6408e0
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2442
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 # Text formatting abstractions
2946
10c1030b70ad added note about obsolescence
Guido van Rossum <guido@python.org>
parents: 2442
diff changeset
2 # Note -- this module is obsolete, it's too slow anyway
2442
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
3
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
4
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
5 # Oft-used type object
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
6 Int = type(0)
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
7
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
8
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 # Represent a paragraph. This is a list of words with associated
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
10 # font and size information, plus indents and justification for the
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 # entire paragraph.
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 # Once the words have been added to a paragraph, it can be laid out
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
13 # for different line widths. Once laid out, it can be rendered at
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
14 # different screen locations. Once rendered, it can be queried
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
15 # for mouse hits, and parts of the text can be highlighted
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
16 class Para:
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
17 #
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
18 def __init__(self):
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
19 self.words = [] # The words
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
20 self.just = 'l' # Justification: 'l', 'r', 'lr' or 'c'
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
21 self.indent_left = self.indent_right = self.indent_hang = 0
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
22 # Final lay-out parameters, may change
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 self.left = self.top = self.right = self.bottom = \
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 self.width = self.height = self.lines = None
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 #
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
26 # Add a word, computing size information for it.
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
27 # Words may also be added manually by appending to self.words
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 # Each word should be a 7-tuple:
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 # (font, text, width, space, stretch, ascent, descent)
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 def addword(self, d, font, text, space, stretch):
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 if font <> None:
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 d.setfont(font)
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
33 width = d.textwidth(text)
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
34 ascent = d.baseline()
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
35 descent = d.lineheight() - ascent
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
36 spw = d.textwidth(' ')
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
37 space = space * spw
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
38 stretch = stretch * spw
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
39 tuple = (font, text, width, space, stretch, ascent, descent)
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
40 self.words.append(tuple)
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
41 #
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 # Hooks to begin and end anchors -- insert numbers in the word list!
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
43 def bgn_anchor(self, id):
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
44 self.words.append(id)
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
45 #
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
46 def end_anchor(self, id):
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
47 self.words.append(0)
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
48 #
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
49 # Return the total length (width) of the text added so far, in pixels
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
50 def getlength(self):
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 total = 0
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
52 for word in self.words:
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
53 if type(word) <> Int:
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
54 total = total + word[2] + word[3]
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
55 return total
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
56 #
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
57 # Tab to a given position (relative to the current left indent):
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
58 # remove all stretch, add fixed space up to the new indent.
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
59 # If the current position is already beying the tab stop,
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
60 # don't add any new space (but still remove the stretch)
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
61 def tabto(self, tab):
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
62 total = 0
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
63 as, de = 1, 0
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
64 for i in range(len(self.words)):
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
65 word = self.words[i]
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
66 if type(word) == Int: continue
4364
34182a6408e0 Changes to always call list.append with a single argument.
Guido van Rossum <guido@python.org>
parents: 2946
diff changeset
67 (fo, te, wi, sp, st, as, de) = word
34182a6408e0 Changes to always call list.append with a single argument.
Guido van Rossum <guido@python.org>
parents: 2946
diff changeset
68 self.words[i] = (fo, te, wi, sp, 0, as, de)
2442
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
69 total = total + wi + sp
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
70 if total < tab:
4364
34182a6408e0 Changes to always call list.append with a single argument.
Guido van Rossum <guido@python.org>
parents: 2946
diff changeset
71 self.words.append((None, '', 0, tab-total, 0, as, de))
2442
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
72 #
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
73 # Make a hanging tag: tab to hang, increment indent_left by hang,
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
74 # and reset indent_hang to -hang
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
75 def makehangingtag(self, hang):
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
76 self.tabto(hang)
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
77 self.indent_left = self.indent_left + hang
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
78 self.indent_hang = -hang
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
79 #
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
80 # Decide where the line breaks will be given some screen width
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
81 def layout(self, linewidth):
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
82 self.width = linewidth
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
83 height = 0
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
84 self.lines = lines = []
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
85 avail1 = self.width - self.indent_left - self.indent_right
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
86 avail = avail1 - self.indent_hang
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
87 words = self.words
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
88 i = 0
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
89 n = len(words)
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
90 lastfont = None
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
91 while i < n:
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
92 firstfont = lastfont
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
93 charcount = 0
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
94 width = 0
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
95 stretch = 0
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
96 ascent = 0
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
97 descent = 0
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
98 lsp = 0
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
99 j = i
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
100 while i < n:
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
101 word = words[i]
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
102 if type(word) == Int:
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
103 if word > 0 and width >= avail:
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
104 break
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
105 i = i+1
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
106 continue
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
107 fo, te, wi, sp, st, as, de = word
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
108 if width + wi > avail and width > 0 and wi > 0:
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
109 break
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
110 if fo <> None:
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
111 lastfont = fo
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
112 if width == 0:
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
113 firstfont = fo
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
114 charcount = charcount + len(te) + (sp > 0)
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
115 width = width + wi + sp
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
116 lsp = sp
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
117 stretch = stretch + st
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
118 lst = st
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
119 ascent = max(ascent, as)
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
120 descent = max(descent, de)
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
121 i = i+1
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
122 while i > j and type(words[i-1]) == Int and \
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
123 words[i-1] > 0: i = i-1
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
124 width = width - lsp
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
125 if i < n:
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
126 stretch = stretch - lst
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
127 else:
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
128 stretch = 0
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
129 tuple = i-j, firstfont, charcount, width, stretch, \
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
130 ascent, descent
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
131 lines.append(tuple)
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
132 height = height + ascent + descent
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
133 avail = avail1
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
134 self.height = height
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
135 #
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
136 # Call a function for all words in a line
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
137 def visit(self, wordfunc, anchorfunc):
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
138 avail1 = self.width - self.indent_left - self.indent_right
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
139 avail = avail1 - self.indent_hang
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
140 v = self.top
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
141 i = 0
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
142 for tuple in self.lines:
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
143 wordcount, firstfont, charcount, width, stretch, \
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
144 ascent, descent = tuple
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
145 h = self.left + self.indent_left
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
146 if i == 0: h = h + self.indent_hang
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
147 extra = 0
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
148 if self.just == 'r': h = h + avail - width
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
149 elif self.just == 'c': h = h + (avail - width) / 2
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
150 elif self.just == 'lr' and stretch > 0:
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
151 extra = avail - width
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
152 v2 = v + ascent + descent
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
153 for j in range(i, i+wordcount):
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
154 word = self.words[j]
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
155 if type(word) == Int:
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
156 ok = anchorfunc(self, tuple, word, \
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
157 h, v)
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
158 if ok <> None: return ok
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
159 continue
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
160 fo, te, wi, sp, st, as, de = word
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
161 if extra > 0 and stretch > 0:
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
162 ex = extra * st / stretch
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
163 extra = extra - ex
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
164 stretch = stretch - st
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
165 else:
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
166 ex = 0
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
167 h2 = h + wi + sp + ex
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
168 ok = wordfunc(self, tuple, word, h, v, \
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
169 h2, v2, (j==i), (j==i+wordcount-1))
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
170 if ok <> None: return ok
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
171 h = h2
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
172 v = v2
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
173 i = i + wordcount
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
174 avail = avail1
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
175 #
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
176 # Render a paragraph in "drawing object" d, using the rectangle
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
177 # given by (left, top, right) with an unspecified bottom.
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
178 # Return the computed bottom of the text.
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
179 def render(self, d, left, top, right):
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
180 if self.width <> right-left:
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
181 self.layout(right-left)
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
182 self.left = left
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
183 self.top = top
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
184 self.right = right
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
185 self.bottom = self.top + self.height
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
186 self.anchorid = 0
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
187 try:
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
188 self.d = d
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
189 self.visit(self.__class__._renderword, \
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
190 self.__class__._renderanchor)
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
191 finally:
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
192 self.d = None
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
193 return self.bottom
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
194 #
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
195 def _renderword(self, tuple, word, h, v, h2, v2, isfirst, islast):
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
196 if word[0] <> None: self.d.setfont(word[0])
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
197 baseline = v + tuple[5]
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
198 self.d.text((h, baseline - word[5]), word[1])
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
199 if self.anchorid > 0:
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
200 self.d.line((h, baseline+2), (h2, baseline+2))
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
201 #
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
202 def _renderanchor(self, tuple, word, h, v):
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
203 self.anchorid = word
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
204 #
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
205 # Return which anchor(s) was hit by the mouse
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
206 def hitcheck(self, mouseh, mousev):
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
207 self.mouseh = mouseh
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
208 self.mousev = mousev
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
209 self.anchorid = 0
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
210 self.hits = []
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
211 self.visit(self.__class__._hitcheckword, \
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
212 self.__class__._hitcheckanchor)
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
213 return self.hits
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
214 #
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
215 def _hitcheckword(self, tuple, word, h, v, h2, v2, isfirst, islast):
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
216 if self.anchorid > 0 and h <= self.mouseh <= h2 and \
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
217 v <= self.mousev <= v2:
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
218 self.hits.append(self.anchorid)
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
219 #
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
220 def _hitcheckanchor(self, tuple, word, h, v):
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
221 self.anchorid = word
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
222 #
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
223 # Return whether the given anchor id is present
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
224 def hasanchor(self, id):
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
225 return id in self.words or -id in self.words
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
226 #
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
227 # Extract the raw text from the word list, substituting one space
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
228 # for non-empty inter-word space, and terminating with '\n'
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
229 def extract(self):
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
230 text = ''
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
231 for w in self.words:
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
232 if type(w) <> Int:
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
233 word = w[1]
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
234 if w[3]: word = word + ' '
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
235 text = text + word
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
236 return text + '\n'
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
237 #
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
238 # Return which character position was hit by the mouse, as
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
239 # an offset in the entire text as returned by extract().
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
240 # Return None if the mouse was not in this paragraph
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
241 def whereis(self, d, mouseh, mousev):
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
242 if mousev < self.top or mousev > self.bottom:
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
243 return None
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
244 self.mouseh = mouseh
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
245 self.mousev = mousev
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
246 self.lastfont = None
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
247 self.charcount = 0
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
248 try:
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
249 self.d = d
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
250 return self.visit(self.__class__._whereisword, \
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
251 self.__class__._whereisanchor)
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
252 finally:
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
253 self.d = None
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
254 #
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
255 def _whereisword(self, tuple, word, h1, v1, h2, v2, isfirst, islast):
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
256 fo, te, wi, sp, st, as, de = word
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
257 if fo <> None: self.lastfont = fo
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
258 h = h1
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
259 if isfirst: h1 = 0
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
260 if islast: h2 = 999999
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
261 if not (v1 <= self.mousev <= v2 and h1 <= self.mouseh <= h2):
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
262 self.charcount = self.charcount + len(te) + (sp > 0)
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
263 return
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
264 if self.lastfont <> None:
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
265 self.d.setfont(self.lastfont)
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
266 cc = 0
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
267 for c in te:
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
268 cw = self.d.textwidth(c)
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
269 if self.mouseh <= h + cw/2:
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
270 return self.charcount + cc
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
271 cc = cc+1
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
272 h = h+cw
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
273 self.charcount = self.charcount + cc
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
274 if self.mouseh <= (h+h2) / 2:
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
275 return self.charcount
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
276 else:
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
277 return self.charcount + 1
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
278 #
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
279 def _whereisanchor(self, tuple, word, h, v):
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
280 pass
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
281 #
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
282 # Return screen position corresponding to position in paragraph.
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
283 # Return tuple (h, vtop, vbaseline, vbottom).
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
284 # This is more or less the inverse of whereis()
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
285 def screenpos(self, d, pos):
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
286 if pos < 0:
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
287 ascent, descent = self.lines[0][5:7]
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
288 return self.left, self.top, self.top + ascent, \
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
289 self.top + ascent + descent
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
290 self.pos = pos
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
291 self.lastfont = None
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
292 try:
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
293 self.d = d
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
294 ok = self.visit(self.__class__._screenposword, \
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
295 self.__class__._screenposanchor)
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
296 finally:
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
297 self.d = None
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
298 if ok == None:
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
299 ascent, descent = self.lines[-1][5:7]
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
300 ok = self.right, self.bottom - ascent - descent, \
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
301 self.bottom - descent, self.bottom
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
302 return ok
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
303 #
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
304 def _screenposword(self, tuple, word, h1, v1, h2, v2, isfirst, islast):
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
305 fo, te, wi, sp, st, as, de = word
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
306 if fo <> None: self.lastfont = fo
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
307 cc = len(te) + (sp > 0)
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
308 if self.pos > cc:
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
309 self.pos = self.pos - cc
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
310 return
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
311 if self.pos < cc:
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
312 self.d.setfont(self.lastfont)
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
313 h = h1 + self.d.textwidth(te[:self.pos])
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
314 else:
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
315 h = h2
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
316 ascent, descent = tuple[5:7]
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
317 return h, v1, v1+ascent, v2
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
318 #
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
319 def _screenposanchor(self, tuple, word, h, v):
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
320 pass
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
321 #
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
322 # Invert the stretch of text between pos1 and pos2.
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
323 # If pos1 is None, the beginning is implied;
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
324 # if pos2 is None, the end is implied.
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
325 # Undoes its own effect when called again with the same arguments
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
326 def invert(self, d, pos1, pos2):
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
327 if pos1 == None:
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
328 pos1 = self.left, self.top, self.top, self.top
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
329 else:
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
330 pos1 = self.screenpos(d, pos1)
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
331 if pos2 == None:
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
332 pos2 = self.right, self.bottom,self.bottom,self.bottom
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
333 else:
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
334 pos2 = self.screenpos(d, pos2)
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
335 h1, top1, baseline1, bottom1 = pos1
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
336 h2, top2, baseline2, bottom2 = pos2
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
337 if bottom1 <= top2:
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
338 d.invert((h1, top1), (self.right, bottom1))
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
339 h1 = self.left
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
340 if bottom1 < top2:
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
341 d.invert((h1, bottom1), (self.right, top2))
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
342 top1, bottom1 = top2, bottom2
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
343 d.invert((h1, top1), (h2, bottom2))
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
344
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
345
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
346 # Test class Para
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
347 # XXX This was last used on the Mac, hence the weird fonts...
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
348 def test():
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
349 import stdwin
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
350 from stdwinevents import *
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
351 words = 'The', 'quick', 'brown', 'fox', 'jumps', 'over', \
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
352 'the', 'lazy', 'dog.'
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
353 paralist = []
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
354 for just in 'l', 'r', 'lr', 'c':
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
355 p = Para()
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
356 p.just = just
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
357 p.addword(stdwin, ('New York', 'p', 12), words[0], 1, 1)
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
358 for word in words[1:-1]:
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
359 p.addword(stdwin, None, word, 1, 1)
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
360 p.addword(stdwin, None, words[-1], 2, 4)
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
361 p.addword(stdwin, ('New York', 'b', 18), 'Bye!', 0, 0)
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
362 p.addword(stdwin, ('New York', 'p', 10), 'Bye!', 0, 0)
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
363 paralist.append(p)
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
364 window = stdwin.open('Para.test()')
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
365 start = stop = selpara = None
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
366 while 1:
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
367 etype, win, detail = stdwin.getevent()
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
368 if etype == WE_CLOSE:
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
369 break
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
370 if etype == WE_SIZE:
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
371 window.change((0, 0), (1000, 1000))
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
372 if etype == WE_DRAW:
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
373 width, height = window.getwinsize()
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
374 d = None
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
375 try:
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
376 d = window.begindrawing()
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
377 d.cliprect(detail)
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
378 d.erase(detail)
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
379 v = 0
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
380 for p in paralist:
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
381 v = p.render(d, 0, v, width)
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
382 if p == selpara and \
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
383 start <> None and stop <> None:
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
384 p.invert(d, start, stop)
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
385 finally:
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
386 if d: d.close()
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
387 if etype == WE_MOUSE_DOWN:
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
388 if selpara and start <> None and stop <> None:
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
389 d = window.begindrawing()
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
390 selpara.invert(d, start, stop)
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
391 d.close()
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
392 start = stop = selpara = None
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
393 mouseh, mousev = detail[0]
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
394 for p in paralist:
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
395 start = p.whereis(stdwin, mouseh, mousev)
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
396 if start <> None:
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
397 selpara = p
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
398 break
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
399 if etype == WE_MOUSE_UP and start <> None and selpara:
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
400 mouseh, mousev = detail[0]
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
401 stop = selpara.whereis(stdwin, mouseh, mousev)
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
402 if stop == None: start = selpara = None
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
403 else:
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
404 if start > stop:
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
405 start, stop = stop, start
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
406 d = window.begindrawing()
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
407 selpara.invert(d, start, stop)
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
408 d.close()
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
409 window.close()