comparison Mac/scripts/EditPythonPrefs.py @ 4230:41a52ab263da legacy-trunk

Allow editing selected bits of the GUSI preferences (creator, type and delayed-sioux-window flag).
author Jack Jansen <jack.jansen@cwi.nl>
date Fri, 06 Sep 1996 22:25:34 +0000
parents e8d501c88a3e
children 8f291dc8fa9b
comparison
equal deleted inserted replaced
4229:1f692c3f1d32 4230:41a52ab263da
26 OPTIONS_ITEM = 7 26 OPTIONS_ITEM = 7
27 27
28 # The options dialog. There is a correspondence between 28 # The options dialog. There is a correspondence between
29 # the dialog item numbers and the option. 29 # the dialog item numbers and the option.
30 OPT_DIALOG_ID = 513 30 OPT_DIALOG_ID = 513
31 # 1 thru 7 are the options 31 # 1 thru 9 are the options
32 OD_OK_ITEM = 8 32 # The GUSI creator/type and delay-console
33 OD_CANCEL_ITEM = 9 33 OD_CREATOR_ITEM = 10
34 OD_TYPE_ITEM = 11
35 OD_DELAYCONSOLE_ITEM = 12
36 OD_OK_ITEM = 13
37 OD_CANCEL_ITEM = 14
34 38
35 # Resource IDs in the preferences file 39 # Resource IDs in the preferences file
36 PATH_STRINGS_ID = 128 40 PATH_STRINGS_ID = 128
37 DIRECTORY_ID = 128 41 DIRECTORY_ID = 128
38 OPTIONS_ID = 128 42 OPTIONS_ID = 128
43 GUSI_ID = 10240
39 44
40 # Override IDs (in the applet) 45 # Override IDs (in the applet)
41 OVERRIDE_PATH_STRINGS_ID = 129 46 OVERRIDE_PATH_STRINGS_ID = 129
42 OVERRIDE_DIRECTORY_ID = 129 47 OVERRIDE_DIRECTORY_ID = 129
43 OVERRIDE_OPTIONS_ID = 129 48 OVERRIDE_OPTIONS_ID = 129
49 OVERRIDE_GUSI_ID = 10240
50
51 # Things we know about the GUSI resource. Note the code knows these too.
52 GUSIPOS_TYPE=0
53 GUSIPOS_CREATOR=4
54 GUSIPOS_SKIP=8
55 GUSIPOS_FLAGS=9
56 GUSIFLAGS_DELAY=0x04 # Mask
44 57
45 READ = 1 58 READ = 1
46 WRITE = 2 59 WRITE = 2
47 smAllScripts = -3 60 smAllScripts = -3
48 kOnSystemDisk = 0x8000 61 kOnSystemDisk = 0x8000
78 SetDialogItemText(h, str) 91 SetDialogItemText(h, str)
79 while 1: 92 while 1:
80 n = ModalDialog(None) 93 n = ModalDialog(None)
81 if n == 1: break 94 if n == 1: break
82 95
83 def optinteract(options): 96 def optinteract((options, creator, type, delaycons)):
84 """Let the user interact with the options dialog""" 97 """Let the user interact with the options dialog"""
85 old_options = options[:] 98 old_options = (options[:], creator, type, delaycons)
86 d = GetNewDialog(OPT_DIALOG_ID, -1) 99 d = GetNewDialog(OPT_DIALOG_ID, -1)
100 tp, h, rect = d.GetDialogItem(OD_CREATOR_ITEM)
101 SetDialogItemText(h, creator)
102 tp, h, rect = d.GetDialogItem(OD_TYPE_ITEM)
103 SetDialogItemText(h, type)
87 d.SetDialogDefaultItem(OD_OK_ITEM) 104 d.SetDialogDefaultItem(OD_OK_ITEM)
88 d.SetDialogCancelItem(OD_CANCEL_ITEM) 105 d.SetDialogCancelItem(OD_CANCEL_ITEM)
89 while 1: 106 while 1:
90 for i in range(len(options)): 107 for i in range(len(options)):
91 tp, h, rect = d.GetDialogItem(i+1) 108 tp, h, rect = d.GetDialogItem(i+1)
92 h.as_Control().SetControlValue(options[i]) 109 h.as_Control().SetControlValue(options[i])
110 tp, h, rect = d.GetDialogItem(OD_DELAYCONSOLE_ITEM)
111 h.as_Control().SetControlValue(delaycons)
93 n = ModalDialog(None) 112 n = ModalDialog(None)
94 if n == OD_OK_ITEM: 113 if n == OD_OK_ITEM:
95 return options 114 tp, h, rect = d.GetDialogItem(OD_CREATOR_ITEM)
115 ncreator = GetDialogItemText(h)
116 tp, h, rect = d.GetDialogItem(OD_TYPE_ITEM)
117 ntype = GetDialogItemText(h)
118 if len(ncreator) == 4 and len(ntype) == 4:
119 return options, ncreator, ntype, delaycons
120 else:
121 sys.stderr.write('\007')
96 elif n == OD_CANCEL_ITEM: 122 elif n == OD_CANCEL_ITEM:
97 return old_options 123 return old_options
124 elif n in (OD_CREATOR_ITEM, OD_TYPE_ITEM):
125 pass
126 elif n == OD_DELAYCONSOLE_ITEM:
127 delaycons = (not delaycons)
98 elif 1 <= n <= len(options): 128 elif 1 <= n <= len(options):
99 options[n-1] = (not options[n-1]) 129 options[n-1] = (not options[n-1])
100 130
101 131
102 def interact(list, pythondir, options, title): 132 def interact(list, pythondir, options, title):
158 opr = GetResource('Popt', id) 188 opr = GetResource('Popt', id)
159 except (MacOS.Error, Res.Error): 189 except (MacOS.Error, Res.Error):
160 return [0]*7, None 190 return [0]*7, None
161 return map(lambda x: ord(x), opr.data), opr 191 return map(lambda x: ord(x), opr.data), opr
162 192
193 def getgusioptions(id):
194 try:
195 opr = GetResource('GU\267I', id)
196 except (MacOS.Error, Res.Error):
197 return '????', '????', 0, None
198 data = opr.data
199 type = data[GUSIPOS_TYPE:GUSIPOS_TYPE+4]
200 creator = data[GUSIPOS_CREATOR:GUSIPOS_CREATOR+4]
201 flags = ord(data[GUSIPOS_FLAGS])
202 delay = (not not (flags & GUSIFLAGS_DELAY))
203 return creator, type, delay, opr
204
205 def setgusioptions(opr, creator, type, delay):
206 data = opr.data
207 flags = ord(data[GUSIPOS_FLAGS])
208 if delay:
209 flags = flags | GUSIFLAGS_DELAY
210 else:
211 flags = flags & ~GUSIFLAGS_DELAY
212 data = type + creator + data[GUSIPOS_SKIP] + chr(flags) + data[GUSIPOS_FLAGS+1:]
213 return data
214
163 def openpreffile(rw): 215 def openpreffile(rw):
164 # Find the preferences folder and our prefs file, create if needed. 216 # Find the preferences folder and our prefs file, create if needed.
165 vrefnum, dirid = macfs.FindFolder(kOnSystemDisk, 'pref', 0) 217 vrefnum, dirid = macfs.FindFolder(kOnSystemDisk, 'pref', 0)
166 preff_fss = macfs.FSSpec((vrefnum, dirid, 'Python Preferences')) 218 preff_fss = macfs.FSSpec((vrefnum, dirid, 'Python Preferences'))
167 try: 219 try:
197 fss_changed = 1 249 fss_changed = 1
198 250
199 options, opr = getoptions(OPTIONS_ID) 251 options, opr = getoptions(OPTIONS_ID)
200 saved_options = options[:] 252 saved_options = options[:]
201 253
254 creator, type, delaycons, gusi_opr = getgusioptions(GUSI_ID)
255 saved_gusi_options = creator, type, delaycons
256
202 # Let the user play away 257 # Let the user play away
203 result = interact(l, fss, options, 'System-wide preferences') 258 result = interact(l, fss, (options, creator, type, delaycons),
259 'System-wide preferences')
204 260
205 # See what we have to update, and how 261 # See what we have to update, and how
206 if result == None: 262 if result == None:
207 sys.exit(0) 263 sys.exit(0)
208 264
209 pathlist, nfss, options = result 265 pathlist, nfss, (options, creator, type, delaycons) = result
210 if nfss != fss: 266 if nfss != fss:
211 fss_changed = 1 267 fss_changed = 1
212 268
213 if fss_changed: 269 if fss_changed:
214 alias = nfss.NewAlias() 270 alias = nfss.NewAlias()
236 opr.data = newdata 292 opr.data = newdata
237 opr.ChangedResource() 293 opr.ChangedResource()
238 else: 294 else:
239 opr = Resource(newdata) 295 opr = Resource(newdata)
240 opr.AddResource('Popt', OPTIONS_ID, '') 296 opr.AddResource('Popt', OPTIONS_ID, '')
297
298 if (creator, type, delaycons) != saved_gusi_options:
299 newdata = setgusioptions(gusi_opr, creator, type, delaycons)
300 if gusi_opr.HomeResFile() == preff_handle:
301 gusi_opr.data = newdata
302 gusi_opr.ChangedResource()
303 else:
304 print 'Created new GUSI option'
305 ngusi_opr = Resource(gusi_opr.data)
306 ngusi_opr.AddResource('GU\267I', GUSI_ID, '')
241 307
242 CloseResFile(preff_handle) 308 CloseResFile(preff_handle)
243 309
244 def edit_applet(name): 310 def edit_applet(name):
245 pref_handle = openpreffile(READ) 311 pref_handle = openpreffile(READ)
273 else: 339 else:
274 notfound = 'options' 340 notfound = 'options'
275 options, dummy = getoptions(OPTIONS_ID) 341 options, dummy = getoptions(OPTIONS_ID)
276 saved_options = options[:] 342 saved_options = options[:]
277 343
344 creator, type, delaycons, gusi_opr = getgusioptions(OVERRIDE_GUSI_ID)
345 if not opr:
346 if notfound:
347 notfound = notfound + ', GUSI options'
348 else:
349 notfound = 'GUSI options'
350 creator, type, delaycons, dummy = getgusioptions(GUSI_ID)
351 saved_gusi_options = creator, type, delaycons
352
278 dummy = dummy2 = None # Discard them. 353 dummy = dummy2 = None # Discard them.
279 354
280 if notfound: 355 if notfound:
281 message('Warning: initial %s taken from system-wide defaults'%notfound) 356 message('Warning: initial %s taken from system-wide defaults'%notfound)
282 # Let the user play away 357 # Let the user play away
283 result = interact(l, fss, options, name) 358 result = interact(l, fss, (options, creator, type, delaycons), name)
284 359
285 # See what we have to update, and how 360 # See what we have to update, and how
286 if result == None: 361 if result == None:
287 sys.exit(0) 362 sys.exit(0)
288 363
316 opr.data = newdata 391 opr.data = newdata
317 opr.ChangedResource() 392 opr.ChangedResource()
318 else: 393 else:
319 opr = Resource(newdata) 394 opr = Resource(newdata)
320 opr.AddResource('Popt', OVERRIDE_OPTIONS_ID, '') 395 opr.AddResource('Popt', OVERRIDE_OPTIONS_ID, '')
321 396
397 if (creator, type, delaycons) != saved_gusi_options:
398 newdata = setgusioptions(gusi_opr, creator, type, delaycons)
399 if gusi_opr.HomeResFile == app_handle:
400 gusi_opr.data = newdata
401 gusi_opr.ChangedResource()
402 else:
403 gusi_opr = Resource(gusi_opr.data)
404 gusi_opr.AddResource('GU\267I', OVERRIDE_GUSI_ID, '')
405
322 CloseResFile(app_handle) 406 CloseResFile(app_handle)
323 407
324 def main(): 408 def main():
325 try: 409 try:
326 h = OpenResFile('EditPythonPrefs.rsrc') 410 h = OpenResFile('EditPythonPrefs.rsrc')
333 for appl in sys.argv[1:]: 417 for appl in sys.argv[1:]:
334 edit_applet(appl) 418 edit_applet(appl)
335 419
336 420
337 if __name__ == '__main__': 421 if __name__ == '__main__':
338 print # Stupid, to init toolboxes...
339 main() 422 main()