blob: d9f30f103ccb096e77152b4b72a59deb76a9b045 [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
Jack Jansen0c6d0372000-05-05 23:11:14 +000051 "tabwarn",
52 "nosite",
Jack Jansen6f8a6d22000-05-09 10:01:52 +000053 "nonavservice"]
Jack Jansen3b3a2871997-09-08 13:19:42 +000054opt_dialog_dict = {}
55for i in range(len(opt_dialog_map)):
56 if opt_dialog_map[i]:
57 opt_dialog_dict[opt_dialog_map[i]] = i
58# 1 thru 10 are the options
Jack Jansen6c4e9871996-09-06 22:25:34 +000059# The GUSI creator/type and delay-console
Jack Jansen3b3a2871997-09-08 13:19:42 +000060OD_CREATOR_ITEM = 11
61OD_TYPE_ITEM = 12
Jack Jansen6c4e9871996-09-06 22:25:34 +000062OD_OK_ITEM = 13
63OD_CANCEL_ITEM = 14
Jack Jansenb95901e1997-09-09 13:58:19 +000064OD_HELP_ITEM = 22
Jack Jansen7571f301995-07-29 13:48:41 +000065
Jack Jansen3b3a2871997-09-08 13:19:42 +000066def optinteract(options):
Jack Jansen822a30b1996-04-10 14:52:18 +000067 """Let the user interact with the options dialog"""
Jack Jansen822a30b1996-04-10 14:52:18 +000068 d = GetNewDialog(OPT_DIALOG_ID, -1)
Jack Jansen6a6db071999-12-23 14:34:07 +000069 htext = d.GetDialogItemAsControl(OD_CREATOR_ITEM)
70 SetDialogItemText(htext, options['creator'])
71 htext = d.GetDialogItemAsControl(OD_TYPE_ITEM)
72 SetDialogItemText(htext, options['type'])
Jack Jansen822a30b1996-04-10 14:52:18 +000073 d.SetDialogDefaultItem(OD_OK_ITEM)
74 d.SetDialogCancelItem(OD_CANCEL_ITEM)
Jack Jansen3b3a2871997-09-08 13:19:42 +000075
Jack Jansen822a30b1996-04-10 14:52:18 +000076 while 1:
Jack Jansen3b3a2871997-09-08 13:19:42 +000077 for name in opt_dialog_dict.keys():
78 num = opt_dialog_dict[name]
Jack Jansen6a6db071999-12-23 14:34:07 +000079 ctl = d.GetDialogItemAsControl(num)
80 ctl.SetControlValue(options[name])
Jack Jansen822a30b1996-04-10 14:52:18 +000081 n = ModalDialog(None)
82 if n == OD_OK_ITEM:
Jack Jansen6a6db071999-12-23 14:34:07 +000083 htext = d.GetDialogItemAsControl(OD_CREATOR_ITEM)
84 ncreator = GetDialogItemText(htext)
85 htext = d.GetDialogItemAsControl(OD_TYPE_ITEM)
86 ntype = GetDialogItemText(htext)
Jack Jansen6c4e9871996-09-06 22:25:34 +000087 if len(ncreator) == 4 and len(ntype) == 4:
Jack Jansen3b3a2871997-09-08 13:19:42 +000088 options['creator'] = ncreator
89 options['type'] = ntype
90 return options
Jack Jansen6c4e9871996-09-06 22:25:34 +000091 else:
Jack Jansen3b3a2871997-09-08 13:19:42 +000092 MacOS.SysBeep()
Jack Jansen822a30b1996-04-10 14:52:18 +000093 elif n == OD_CANCEL_ITEM:
Jack Jansen3b3a2871997-09-08 13:19:42 +000094 return
Jack Jansen6c4e9871996-09-06 22:25:34 +000095 elif n in (OD_CREATOR_ITEM, OD_TYPE_ITEM):
96 pass
Jack Jansenb95901e1997-09-09 13:58:19 +000097 elif n == OD_HELP_ITEM:
98 onoff = Help.HMGetBalloons()
99 Help.HMSetBalloons(not onoff)
Jack Jansen3b3a2871997-09-08 13:19:42 +0000100 elif 1 <= n <= len(opt_dialog_map):
101 options[opt_dialog_map[n]] = (not options[opt_dialog_map[n]])
Jack Jansen822a30b1996-04-10 14:52:18 +0000102
103
Jack Jansen3b3a2871997-09-08 13:19:42 +0000104def interact(options, title):
Jack Jansen7571f301995-07-29 13:48:41 +0000105 """Let the user interact with the dialog"""
Jack Jansen7571f301995-07-29 13:48:41 +0000106 try:
107 # Try to go to the "correct" dir for GetDirectory
Jack Jansen3b3a2871997-09-08 13:19:42 +0000108 os.chdir(options['dir'].as_pathname())
Jack Jansen7571f301995-07-29 13:48:41 +0000109 except os.error:
110 pass
111 d = GetNewDialog(DIALOG_ID, -1)
Jack Jansen6a6db071999-12-23 14:34:07 +0000112 htext = d.GetDialogItemAsControl(TITLE_ITEM)
113 SetDialogItemText(htext, title)
Jack Jansen8242c9e2000-01-13 16:22:12 +0000114 path_ctl = d.GetDialogItemAsControl(TEXT_ITEM)
Jack Jansen6a6db071999-12-23 14:34:07 +0000115 data = string.joinfields(options['path'], '\r')
Jack Jansen8242c9e2000-01-13 16:22:12 +0000116 path_ctl.SetControlData(Controls.kControlEditTextPart, Controls.kControlEditTextTextTag, data)
Jack Jansen6a6db071999-12-23 14:34:07 +0000117
Jack Jansena918b8c1996-11-20 14:55:26 +0000118 d.SelectDialogItemText(TEXT_ITEM, 0, 32767)
119 d.SelectDialogItemText(TEXT_ITEM, 0, 0)
Jack Jansendb0bace1996-04-04 15:38:44 +0000120## d.SetDialogDefaultItem(OK_ITEM)
121 d.SetDialogCancelItem(CANCEL_ITEM)
Jack Jansena918b8c1996-11-20 14:55:26 +0000122 d.GetDialogWindow().ShowWindow()
123 d.DrawDialog()
Jack Jansen7571f301995-07-29 13:48:41 +0000124 while 1:
125 n = ModalDialog(None)
126 if n == OK_ITEM:
127 break
128 if n == CANCEL_ITEM:
129 return None
Jack Jansendb0bace1996-04-04 15:38:44 +0000130## if n == REVERT_ITEM:
131## return [], pythondir
Jack Jansen7571f301995-07-29 13:48:41 +0000132 if n == DIR_ITEM:
Jack Jansen9062fa21995-08-14 12:21:12 +0000133 fss, ok = macfs.GetDirectory('Select python home folder:')
Jack Jansen7571f301995-07-29 13:48:41 +0000134 if ok:
Jack Jansen3b3a2871997-09-08 13:19:42 +0000135 options['dir'] = fss
Jack Jansenb95901e1997-09-09 13:58:19 +0000136 elif n == HELP_ITEM:
137 onoff = Help.HMGetBalloons()
138 Help.HMSetBalloons(not onoff)
Jack Jansen822a30b1996-04-10 14:52:18 +0000139 if n == OPTIONS_ITEM:
Jack Jansen3b3a2871997-09-08 13:19:42 +0000140 noptions = options
141 for k in options.keys():
142 noptions[k] = options[k]
143 noptions = optinteract(noptions)
144 if noptions:
145 options = noptions
Jack Jansen8242c9e2000-01-13 16:22:12 +0000146 data = path_ctl.GetControlData(Controls.kControlEditTextPart, Controls.kControlEditTextTextTag)
147 tmp = string.splitfields(data, '\r')
Jack Jansen3b3a2871997-09-08 13:19:42 +0000148 newpath = []
Jack Jansen7571f301995-07-29 13:48:41 +0000149 for i in tmp:
150 if i:
Jack Jansen3b3a2871997-09-08 13:19:42 +0000151 newpath.append(i)
152 options['path'] = newpath
153 return options
Jack Jansen7571f301995-07-29 13:48:41 +0000154
Jack Jansendb0bace1996-04-04 15:38:44 +0000155
156def edit_preferences():
Jack Jansen3b3a2871997-09-08 13:19:42 +0000157 handler = pythonprefs.PythonOptions()
Jack Jansen43fd1f71999-12-02 22:52:12 +0000158 options = handler.load()
159 if options['noargs']:
160 EasyDialogs.Message('Warning: system-wide sys.argv processing is off.\nIf you dropped an applet I have not seen it.')
161 result = interact(options, 'System-wide preferences')
Jack Jansen3b3a2871997-09-08 13:19:42 +0000162 if result:
163 handler.save(result)
Jack Jansendb0bace1996-04-04 15:38:44 +0000164
165def edit_applet(name):
Jack Jansen3b3a2871997-09-08 13:19:42 +0000166 handler = pythonprefs.AppletOptions(name)
167 result = interact(handler.load(), os.path.split(name)[1])
168 if result:
169 handler.save(result)
Jack Jansendb0bace1996-04-04 15:38:44 +0000170
171def main():
172 try:
Jack Jansend13c3852000-06-20 21:59:25 +0000173 h = FSpOpenResFile('EditPythonPrefs.rsrc', 1)
Jack Jansendb0bace1996-04-04 15:38:44 +0000174 except Res.Error:
175 pass # Assume we already have acces to our own resource
176
177 if len(sys.argv) <= 1:
178 edit_preferences()
179 else:
180 for appl in sys.argv[1:]:
181 edit_applet(appl)
182
Jack Jansen7571f301995-07-29 13:48:41 +0000183
184if __name__ == '__main__':
Jack Jansen7571f301995-07-29 13:48:41 +0000185 main()