blob: e8177c69440393fa505b8ea4c97010e6b084aa81 [file] [log] [blame]
Jack Jansen7571f301995-07-29 13:48:41 +00001"""Edit the Python Preferences file."""
Jack Jansen822a30b1996-04-10 14:52:18 +00002#
3# This program is getting more and more clunky. It should really
4# be rewritten in a modeless way some time soon.
Jack Jansen7571f301995-07-29 13:48:41 +00005
6from Dlg import *
7from Events import *
8from Res import *
Jack Jansen6a6db071999-12-23 14:34:07 +00009import Controls
Jack Jansen7571f301995-07-29 13:48:41 +000010import string
11import struct
12import macfs
13import MacOS
14import os
15import sys
16import Res # For Res.Error
Jack Jansen3b3a2871997-09-08 13:19:42 +000017import pythonprefs
18import EasyDialogs
Jack Jansenb95901e1997-09-09 13:58:19 +000019import Help
Jack Jansen84872291996-10-22 15:33:02 +000020
Jack Jansen7571f301995-07-29 13:48:41 +000021# resource IDs in our own resources (dialogs, etc)
22MESSAGE_ID = 256
23
Jack Jansen145c92d1996-10-11 11:30:26 +000024DIALOG_ID = 511
Jack Jansen7571f301995-07-29 13:48:41 +000025TEXT_ITEM = 1
26OK_ITEM = 2
27CANCEL_ITEM = 3
Jack Jansendb0bace1996-04-04 15:38:44 +000028DIR_ITEM = 4
29TITLE_ITEM = 5
Jack Jansen822a30b1996-04-10 14:52:18 +000030OPTIONS_ITEM = 7
Jack Jansenb95901e1997-09-09 13:58:19 +000031HELP_ITEM = 9
Jack Jansen822a30b1996-04-10 14:52:18 +000032
33# The options dialog. There is a correspondence between
34# the dialog item numbers and the option.
Jack Jansen145c92d1996-10-11 11:30:26 +000035OPT_DIALOG_ID = 510
Jack Jansen3b3a2871997-09-08 13:19:42 +000036
37# Map dialog item numbers to option names (and the reverse)
38opt_dialog_map = [
39 None,
40 "inspect",
41 "verbose",
42 "optimize",
43 "unbuffered",
44 "debugging",
45 "keepopen",
46 "keeperror",
47 "nointopt",
48 "noargs",
Jack Jansenb95901e1997-09-09 13:58:19 +000049 "delayconsole",
50 None, None, None, None, None, None, None, None, # 11-18 are different
51 "oldexc",
52 "nosite"]
Jack Jansen3b3a2871997-09-08 13:19:42 +000053opt_dialog_dict = {}
54for i in range(len(opt_dialog_map)):
55 if opt_dialog_map[i]:
56 opt_dialog_dict[opt_dialog_map[i]] = i
57# 1 thru 10 are the options
Jack Jansen6c4e9871996-09-06 22:25:34 +000058# The GUSI creator/type and delay-console
Jack Jansen3b3a2871997-09-08 13:19:42 +000059OD_CREATOR_ITEM = 11
60OD_TYPE_ITEM = 12
Jack Jansen6c4e9871996-09-06 22:25:34 +000061OD_OK_ITEM = 13
62OD_CANCEL_ITEM = 14
Jack Jansenb95901e1997-09-09 13:58:19 +000063OD_HELP_ITEM = 22
Jack Jansen7571f301995-07-29 13:48:41 +000064
Jack Jansen3b3a2871997-09-08 13:19:42 +000065def optinteract(options):
Jack Jansen822a30b1996-04-10 14:52:18 +000066 """Let the user interact with the options dialog"""
Jack Jansen822a30b1996-04-10 14:52:18 +000067 d = GetNewDialog(OPT_DIALOG_ID, -1)
Jack Jansen6a6db071999-12-23 14:34:07 +000068 htext = d.GetDialogItemAsControl(OD_CREATOR_ITEM)
69 SetDialogItemText(htext, options['creator'])
70 htext = d.GetDialogItemAsControl(OD_TYPE_ITEM)
71 SetDialogItemText(htext, options['type'])
Jack Jansen822a30b1996-04-10 14:52:18 +000072 d.SetDialogDefaultItem(OD_OK_ITEM)
73 d.SetDialogCancelItem(OD_CANCEL_ITEM)
Jack Jansen3b3a2871997-09-08 13:19:42 +000074
Jack Jansen822a30b1996-04-10 14:52:18 +000075 while 1:
Jack Jansen3b3a2871997-09-08 13:19:42 +000076 for name in opt_dialog_dict.keys():
77 num = opt_dialog_dict[name]
Jack Jansen6a6db071999-12-23 14:34:07 +000078 ctl = d.GetDialogItemAsControl(num)
79 ctl.SetControlValue(options[name])
Jack Jansen822a30b1996-04-10 14:52:18 +000080 n = ModalDialog(None)
81 if n == OD_OK_ITEM:
Jack Jansen6a6db071999-12-23 14:34:07 +000082 htext = d.GetDialogItemAsControl(OD_CREATOR_ITEM)
83 ncreator = GetDialogItemText(htext)
84 htext = d.GetDialogItemAsControl(OD_TYPE_ITEM)
85 ntype = GetDialogItemText(htext)
Jack Jansen6c4e9871996-09-06 22:25:34 +000086 if len(ncreator) == 4 and len(ntype) == 4:
Jack Jansen3b3a2871997-09-08 13:19:42 +000087 options['creator'] = ncreator
88 options['type'] = ntype
89 return options
Jack Jansen6c4e9871996-09-06 22:25:34 +000090 else:
Jack Jansen3b3a2871997-09-08 13:19:42 +000091 MacOS.SysBeep()
Jack Jansen822a30b1996-04-10 14:52:18 +000092 elif n == OD_CANCEL_ITEM:
Jack Jansen3b3a2871997-09-08 13:19:42 +000093 return
Jack Jansen6c4e9871996-09-06 22:25:34 +000094 elif n in (OD_CREATOR_ITEM, OD_TYPE_ITEM):
95 pass
Jack Jansenb95901e1997-09-09 13:58:19 +000096 elif n == OD_HELP_ITEM:
97 onoff = Help.HMGetBalloons()
98 Help.HMSetBalloons(not onoff)
Jack Jansen3b3a2871997-09-08 13:19:42 +000099 elif 1 <= n <= len(opt_dialog_map):
100 options[opt_dialog_map[n]] = (not options[opt_dialog_map[n]])
Jack Jansen822a30b1996-04-10 14:52:18 +0000101
102
Jack Jansen3b3a2871997-09-08 13:19:42 +0000103def interact(options, title):
Jack Jansen7571f301995-07-29 13:48:41 +0000104 """Let the user interact with the dialog"""
Jack Jansen7571f301995-07-29 13:48:41 +0000105 try:
106 # Try to go to the "correct" dir for GetDirectory
Jack Jansen3b3a2871997-09-08 13:19:42 +0000107 os.chdir(options['dir'].as_pathname())
Jack Jansen7571f301995-07-29 13:48:41 +0000108 except os.error:
109 pass
110 d = GetNewDialog(DIALOG_ID, -1)
Jack Jansen6a6db071999-12-23 14:34:07 +0000111 htext = d.GetDialogItemAsControl(TITLE_ITEM)
112 SetDialogItemText(htext, title)
113 ctl = d.GetDialogItemAsControl(TEXT_ITEM)
114 data = string.joinfields(options['path'], '\r')
115 ctl.SetControlData(Controls.kControlEditTextPart, Controls.kControlEditTextTextTag, data)
116
Jack Jansena918b8c1996-11-20 14:55:26 +0000117 d.SelectDialogItemText(TEXT_ITEM, 0, 32767)
118 d.SelectDialogItemText(TEXT_ITEM, 0, 0)
Jack Jansendb0bace1996-04-04 15:38:44 +0000119## d.SetDialogDefaultItem(OK_ITEM)
120 d.SetDialogCancelItem(CANCEL_ITEM)
Jack Jansena918b8c1996-11-20 14:55:26 +0000121 d.GetDialogWindow().ShowWindow()
122 d.DrawDialog()
Jack Jansen7571f301995-07-29 13:48:41 +0000123 while 1:
124 n = ModalDialog(None)
125 if n == OK_ITEM:
126 break
127 if n == CANCEL_ITEM:
128 return None
Jack Jansendb0bace1996-04-04 15:38:44 +0000129## if n == REVERT_ITEM:
130## return [], pythondir
Jack Jansen7571f301995-07-29 13:48:41 +0000131 if n == DIR_ITEM:
Jack Jansen9062fa21995-08-14 12:21:12 +0000132 fss, ok = macfs.GetDirectory('Select python home folder:')
Jack Jansen7571f301995-07-29 13:48:41 +0000133 if ok:
Jack Jansen3b3a2871997-09-08 13:19:42 +0000134 options['dir'] = fss
Jack Jansenb95901e1997-09-09 13:58:19 +0000135 elif n == HELP_ITEM:
136 onoff = Help.HMGetBalloons()
137 Help.HMSetBalloons(not onoff)
Jack Jansen822a30b1996-04-10 14:52:18 +0000138 if n == OPTIONS_ITEM:
Jack Jansen3b3a2871997-09-08 13:19:42 +0000139 noptions = options
140 for k in options.keys():
141 noptions[k] = options[k]
142 noptions = optinteract(noptions)
143 if noptions:
144 options = noptions
Jack Jansena918b8c1996-11-20 14:55:26 +0000145 tmp = string.splitfields(h.data, '\r')
Jack Jansen3b3a2871997-09-08 13:19:42 +0000146 newpath = []
Jack Jansen7571f301995-07-29 13:48:41 +0000147 for i in tmp:
148 if i:
Jack Jansen3b3a2871997-09-08 13:19:42 +0000149 newpath.append(i)
150 options['path'] = newpath
151 return options
Jack Jansen7571f301995-07-29 13:48:41 +0000152
Jack Jansendb0bace1996-04-04 15:38:44 +0000153
154def edit_preferences():
Jack Jansen3b3a2871997-09-08 13:19:42 +0000155 handler = pythonprefs.PythonOptions()
Jack Jansen43fd1f71999-12-02 22:52:12 +0000156 options = handler.load()
157 if options['noargs']:
158 EasyDialogs.Message('Warning: system-wide sys.argv processing is off.\nIf you dropped an applet I have not seen it.')
159 result = interact(options, 'System-wide preferences')
Jack Jansen3b3a2871997-09-08 13:19:42 +0000160 if result:
161 handler.save(result)
Jack Jansendb0bace1996-04-04 15:38:44 +0000162
163def edit_applet(name):
Jack Jansen3b3a2871997-09-08 13:19:42 +0000164 handler = pythonprefs.AppletOptions(name)
165 result = interact(handler.load(), os.path.split(name)[1])
166 if result:
167 handler.save(result)
Jack Jansendb0bace1996-04-04 15:38:44 +0000168
169def main():
170 try:
171 h = OpenResFile('EditPythonPrefs.rsrc')
172 except Res.Error:
173 pass # Assume we already have acces to our own resource
174
175 if len(sys.argv) <= 1:
176 edit_preferences()
177 else:
178 for appl in sys.argv[1:]:
179 edit_applet(appl)
180
Jack Jansen7571f301995-07-29 13:48:41 +0000181
182if __name__ == '__main__':
Jack Jansen7571f301995-07-29 13:48:41 +0000183 main()