Fix an amazing number of typos & malformed sentences reported by Detlef
[python/dscho.git] / Mac / Tools / IDE / FontSettings.py
blob09886ae7cdf4e393c1cf8428e852fde0c4ef4ee3
1 """usage:
2 newsettings = FontDialog(('Chicago', 0, 12, (0, 0, 0))) # font name or id, style flags, size, color (color is ignored)
3 if newsettings:
4 fontsettings, tabsettings = newsettings
5 font, style, size, color = fontsettings # 'font' is always the font name, not the id number
6 # do something
7 """
9 import W
10 import PyEdit
11 import TextEdit
12 import Qd
13 import string
14 import types
15 import sys
16 import MacOS
17 if hasattr(MacOS, "SysBeep"):
18 SysBeep = MacOS.SysBeep
19 else:
20 def SysBeep(*args):
21 pass
23 _stylenames = ["Plain", "Bold", "Italic", "Underline", "Outline", "Shadow", "Condensed", "Extended"]
26 class _FontDialog:
28 #def __del__(self):
29 # print "doei!"
31 def __init__(self, fontsettings, tabsettings):
32 leftmargin = 60
33 leftmargin2 = leftmargin - 16
34 self.w = W.ModalDialog((440, 180), 'Font settings')
35 self.w.fonttitle = W.TextBox((10, 12, leftmargin2, 14), "Font:", TextEdit.teJustRight)
36 self.w.pop = W.FontMenu((leftmargin, 10, 16, 16), self.setfont)
37 self.w.fontname = W.TextBox((leftmargin + 20, 12, 150, 14))
38 self.w.sizetitle = W.TextBox((10, 38, leftmargin2, 14), "Size:", TextEdit.teJustRight)
39 self.w.sizeedit = W.EditText((leftmargin, 35, 40, 20), "", self.checksize)
40 styletop = 64
41 self.w.styletitle = W.TextBox((10, styletop + 2, leftmargin2, 14), "Style:", TextEdit.teJustRight)
42 for i in range(len(_stylenames)):
43 top = styletop + (i % 4) * 20
44 left = leftmargin + 80 * (i > 3) - 2
45 if i:
46 self.w[i] = W.CheckBox((left, top, 76, 16), _stylenames[i], self.dostyle)
47 else:
48 self.w[i] = W.CheckBox((left, top, 70, 16), _stylenames[i], self.doplain)
50 if tabsettings:
51 self.lasttab, self.tabmode = tabsettings
52 self.w.tabsizetitle = W.TextBox((10, -26, leftmargin2, 14), "Tabsize:", TextEdit.teJustRight)
53 self.w.tabsizeedit = W.EditText((leftmargin, -29, 40, 20), "", self.checktab)
54 self.w.tabsizeedit.set(`self.lasttab`)
55 radiobuttons = []
56 self.w.tabsizechars = W.RadioButton((leftmargin + 48, -26, 55, 14), "Spaces",
57 radiobuttons, self.toggletabmode)
58 self.w.tabsizepixels = W.RadioButton((leftmargin + 110, -26, 55, 14), "Pixels",
59 radiobuttons, self.toggletabmode)
60 if self.tabmode:
61 self.w.tabsizechars.set(1)
62 else:
63 self.w.tabsizepixels.set(1)
64 else:
65 self.tabmode = None
67 self.w.cancelbutton = W.Button((-180, -26, 80, 16), "Cancel", self.cancel)
68 self.w.donebutton = W.Button((-90, -26, 80, 16), "Done", self.done)
70 sampletext = "Sample text."
71 self.w.sample = W.EditText((230, 10, -10, 130), sampletext,
72 fontsettings = fontsettings, tabsettings = tabsettings)
74 self.w.setdefaultbutton(self.w.donebutton)
75 self.w.bind('cmd.', self.w.cancelbutton.push)
76 self.w.bind('cmdw', self.w.donebutton.push)
77 self.lastsize = fontsettings[2]
78 self._rv = None
79 self.set(fontsettings)
80 self.w.open()
82 def toggletabmode(self, onoff):
83 if self.w.tabsizechars.get():
84 tabmode = 1
85 else:
86 tabmode = 0
87 if self.tabmode <> tabmode:
88 port = self.w.wid.GetWindowPort()
89 (font, style, size, color), (tabsize, dummy) = self.get()
90 savesettings = W.GetPortFontSettings(port)
91 W.SetPortFontSettings(port, (font, style, size))
92 spacewidth = Qd.StringWidth(' ')
93 W.SetPortFontSettings(port, savesettings)
94 if tabmode:
95 # convert pixels to spaces
96 self.lasttab = int(round(float(tabsize) / spacewidth))
97 else:
98 # convert spaces to pixels
99 self.lasttab = spacewidth * tabsize
100 self.w.tabsizeedit.set(`self.lasttab`)
101 self.tabmode = tabmode
102 self.doit()
104 def set(self, fontsettings):
105 font, style, size, color = fontsettings
106 if type(font) <> types.StringType:
107 import Res
108 res = Res.GetResource('FOND', font)
109 font = res.GetResInfo()[2]
110 self.w.fontname.set(font)
111 self.w.sizeedit.set(str(size))
112 if style:
113 for i in range(1, len(_stylenames)):
114 self.w[i].set(style & 0x01)
115 style = style >> 1
116 else:
117 self.w[0].set(1)
119 def get(self):
120 font = self.w.fontname.get()
121 style = 0
122 if not self.w[0].get():
123 flag = 0x01
124 for i in range(1, len(_stylenames)):
125 if self.w[i].get():
126 style = style | flag
127 flag = flag << 1
128 size = self.lastsize
129 if self.tabmode is None:
130 return (font, style, size, (0, 0, 0)), (32, 0)
131 else:
132 return (font, style, size, (0, 0, 0)), (self.lasttab, self.tabmode)
134 def doit(self):
135 if self.w[0].get():
136 style = 0
137 else:
138 style = 0
139 for i in range(1, len(_stylenames)):
140 if self.w[i].get():
141 style = style | 2 ** (i - 1)
142 #self.w.sample.set(`style`)
143 fontsettings, tabsettings = self.get()
144 self.w.sample.setfontsettings(fontsettings)
145 self.w.sample.settabsettings(tabsettings)
147 def checktab(self):
148 tabsize = self.w.tabsizeedit.get()
149 if not tabsize:
150 return
151 try:
152 tabsize = string.atoi(tabsize)
153 except (ValueError, OverflowError):
154 good = 0
155 sys.exc_traceback = None
156 else:
157 good = 1 <= tabsize <= 500
158 if good:
159 if self.lasttab <> tabsize:
160 self.lasttab = tabsize
161 self.doit()
162 else:
163 SysBeep(0)
164 self.w.tabsizeedit.set(`self.lasttab`)
165 self.w.tabsizeedit.selectall()
167 def checksize(self):
168 size = self.w.sizeedit.get()
169 if not size:
170 return
171 try:
172 size = string.atoi(size)
173 except (ValueError, OverflowError):
174 good = 0
175 sys.exc_traceback = None
176 else:
177 good = 1 <= size <= 500
178 if good:
179 if self.lastsize <> size:
180 self.lastsize = size
181 self.doit()
182 else:
183 SysBeep(0)
184 self.w.sizeedit.set(`self.lastsize`)
185 self.w.sizeedit.selectall()
187 def doplain(self):
188 for i in range(1, len(_stylenames)):
189 self.w[i].set(0)
190 self.w[0].set(1)
191 self.doit()
193 def dostyle(self):
194 for i in range(1, len(_stylenames)):
195 if self.w[i].get():
196 self.w[0].set(0)
197 break
198 else:
199 self.w[0].set(1)
200 self.doit()
202 def close(self):
203 self.w.close()
204 del self.w
206 def cancel(self):
207 self.close()
209 def done(self):
210 self._rv = self.get()
211 self.close()
213 def setfont(self, fontname):
214 self.w.fontname.set(fontname)
215 self.doit()
218 def FontDialog(fontsettings, tabsettings = (32, 0)):
219 fd = _FontDialog(fontsettings, tabsettings)
220 return fd._rv
222 def test():
223 print FontDialog(('Zapata-Light', 0, 25, (0, 0, 0)))