blob: 618d33e4ec184ab10ecdd72a24b1e6aa79a48f1c [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",
53 None,
54 "nonavservices"]
Jack Jansen3b3a2871997-09-08 13:19:42 +000055opt_dialog_dict = {}
56for i in range(len(opt_dialog_map)):
57 if opt_dialog_map[i]:
58 opt_dialog_dict[opt_dialog_map[i]] = i
59# 1 thru 10 are the options
Jack Jansen6c4e9871996-09-06 22:25:34 +000060# The GUSI creator/type and delay-console
Jack Jansen3b3a2871997-09-08 13:19:42 +000061OD_CREATOR_ITEM = 11
62OD_TYPE_ITEM = 12
Jack Jansen6c4e9871996-09-06 22:25:34 +000063OD_OK_ITEM = 13
64OD_CANCEL_ITEM = 14
Jack Jansenb95901e1997-09-09 13:58:19 +000065OD_HELP_ITEM = 22
Jack Jansen7571f301995-07-29 13:48:41 +000066
Jack Jansen3b3a2871997-09-08 13:19:42 +000067def optinteract(options):
Jack Jansen822a30b1996-04-10 14:52:18 +000068 """Let the user interact with the options dialog"""
Jack Jansen822a30b1996-04-10 14:52:18 +000069 d = GetNewDialog(OPT_DIALOG_ID, -1)
Jack Jansen6a6db071999-12-23 14:34:07 +000070 htext = d.GetDialogItemAsControl(OD_CREATOR_ITEM)
71 SetDialogItemText(htext, options['creator'])
72 htext = d.GetDialogItemAsControl(OD_TYPE_ITEM)
73 SetDialogItemText(htext, options['type'])
Jack Jansen822a30b1996-04-10 14:52:18 +000074 d.SetDialogDefaultItem(OD_OK_ITEM)
75 d.SetDialogCancelItem(OD_CANCEL_ITEM)
Jack Jansen3b3a2871997-09-08 13:19:42 +000076
Jack Jansen822a30b1996-04-10 14:52:18 +000077 while 1:
Jack Jansen3b3a2871997-09-08 13:19:42 +000078 for name in opt_dialog_dict.keys():
79 num = opt_dialog_dict[name]
Jack Jansen6a6db071999-12-23 14:34:07 +000080 ctl = d.GetDialogItemAsControl(num)
81 ctl.SetControlValue(options[name])
Jack Jansen822a30b1996-04-10 14:52:18 +000082 n = ModalDialog(None)
83 if n == OD_OK_ITEM:
Jack Jansen6a6db071999-12-23 14:34:07 +000084 htext = d.GetDialogItemAsControl(OD_CREATOR_ITEM)
85 ncreator = GetDialogItemText(htext)
86 htext = d.GetDialogItemAsControl(OD_TYPE_ITEM)
87 ntype = GetDialogItemText(htext)
Jack Jansen6c4e9871996-09-06 22:25:34 +000088 if len(ncreator) == 4 and len(ntype) == 4:
Jack Jansen3b3a2871997-09-08 13:19:42 +000089 options['creator'] = ncreator
90 options['type'] = ntype
91 return options
Jack Jansen6c4e9871996-09-06 22:25:34 +000092 else:
Jack Jansen3b3a2871997-09-08 13:19:42 +000093 MacOS.SysBeep()
Jack Jansen822a30b1996-04-10 14:52:18 +000094 elif n == OD_CANCEL_ITEM:
Jack Jansen3b3a2871997-09-08 13:19:42 +000095 return
Jack Jansen6c4e9871996-09-06 22:25:34 +000096 elif n in (OD_CREATOR_ITEM, OD_TYPE_ITEM):
97 pass
Jack Jansenb95901e1997-09-09 13:58:19 +000098 elif n == OD_HELP_ITEM:
99 onoff = Help.HMGetBalloons()
100 Help.HMSetBalloons(not onoff)
Jack Jansen3b3a2871997-09-08 13:19:42 +0000101 elif 1 <= n <= len(opt_dialog_map):
102 options[opt_dialog_map[n]] = (not options[opt_dialog_map[n]])
Jack Jansen822a30b1996-04-10 14:52:18 +0000103
104
Jack Jansen3b3a2871997-09-08 13:19:42 +0000105def interact(options, title):
Jack Jansen7571f301995-07-29 13:48:41 +0000106 """Let the user interact with the dialog"""
Jack Jansen7571f301995-07-29 13:48:41 +0000107 try:
108 # Try to go to the "correct" dir for GetDirectory
Jack Jansen3b3a2871997-09-08 13:19:42 +0000109 os.chdir(options['dir'].as_pathname())
Jack Jansen7571f301995-07-29 13:48:41 +0000110 except os.error:
111 pass
112 d = GetNewDialog(DIALOG_ID, -1)
Jack Jansen6a6db071999-12-23 14:34:07 +0000113 htext = d.GetDialogItemAsControl(TITLE_ITEM)
114 SetDialogItemText(htext, title)
Jack Jansen8242c9e2000-01-13 16:22:12 +0000115 path_ctl = d.GetDialogItemAsControl(TEXT_ITEM)
Jack Jansen6a6db071999-12-23 14:34:07 +0000116 data = string.joinfields(options['path'], '\r')
Jack Jansen8242c9e2000-01-13 16:22:12 +0000117 path_ctl.SetControlData(Controls.kControlEditTextPart, Controls.kControlEditTextTextTag, data)
Jack Jansen6a6db071999-12-23 14:34:07 +0000118
Jack Jansena918b8c1996-11-20 14:55:26 +0000119 d.SelectDialogItemText(TEXT_ITEM, 0, 32767)
120 d.SelectDialogItemText(TEXT_ITEM, 0, 0)
Jack Jansendb0bace1996-04-04 15:38:44 +0000121## d.SetDialogDefaultItem(OK_ITEM)
122 d.SetDialogCancelItem(CANCEL_ITEM)
Jack Jansena918b8c1996-11-20 14:55:26 +0000123 d.GetDialogWindow().ShowWindow()
124 d.DrawDialog()
Jack Jansen7571f301995-07-29 13:48:41 +0000125 while 1:
126 n = ModalDialog(None)
127 if n == OK_ITEM:
128 break
129 if n == CANCEL_ITEM:
130 return None
Jack Jansendb0bace1996-04-04 15:38:44 +0000131## if n == REVERT_ITEM:
132## return [], pythondir
Jack Jansen7571f301995-07-29 13:48:41 +0000133 if n == DIR_ITEM:
Jack Jansen9062fa21995-08-14 12:21:12 +0000134 fss, ok = macfs.GetDirectory('Select python home folder:')
Jack Jansen7571f301995-07-29 13:48:41 +0000135 if ok:
Jack Jansen3b3a2871997-09-08 13:19:42 +0000136 options['dir'] = fss
Jack Jansenb95901e1997-09-09 13:58:19 +0000137 elif n == HELP_ITEM:
138 onoff = Help.HMGetBalloons()
139 Help.HMSetBalloons(not onoff)
Jack Jansen822a30b1996-04-10 14:52:18 +0000140 if n == OPTIONS_ITEM:
Jack Jansen3b3a2871997-09-08 13:19:42 +0000141 noptions = options
142 for k in options.keys():
143 noptions[k] = options[k]
144 noptions = optinteract(noptions)
145 if noptions:
146 options = noptions
Jack Jansen8242c9e2000-01-13 16:22:12 +0000147 data = path_ctl.GetControlData(Controls.kControlEditTextPart, Controls.kControlEditTextTextTag)
148 tmp = string.splitfields(data, '\r')
Jack Jansen3b3a2871997-09-08 13:19:42 +0000149 newpath = []
Jack Jansen7571f301995-07-29 13:48:41 +0000150 for i in tmp:
151 if i:
Jack Jansen3b3a2871997-09-08 13:19:42 +0000152 newpath.append(i)
153 options['path'] = newpath
154 return options
Jack Jansen7571f301995-07-29 13:48:41 +0000155
Jack Jansendb0bace1996-04-04 15:38:44 +0000156
157def edit_preferences():
Jack Jansen3b3a2871997-09-08 13:19:42 +0000158 handler = pythonprefs.PythonOptions()
Jack Jansen43fd1f71999-12-02 22:52:12 +0000159 options = handler.load()
160 if options['noargs']:
161 EasyDialogs.Message('Warning: system-wide sys.argv processing is off.\nIf you dropped an applet I have not seen it.')
162 result = interact(options, 'System-wide preferences')
Jack Jansen3b3a2871997-09-08 13:19:42 +0000163 if result:
164 handler.save(result)
Jack Jansendb0bace1996-04-04 15:38:44 +0000165
166def edit_applet(name):
Jack Jansen3b3a2871997-09-08 13:19:42 +0000167 handler = pythonprefs.AppletOptions(name)
168 result = interact(handler.load(), os.path.split(name)[1])
169 if result:
170 handler.save(result)
Jack Jansendb0bace1996-04-04 15:38:44 +0000171
172def main():
173 try:
174 h = OpenResFile('EditPythonPrefs.rsrc')
175 except Res.Error:
176 pass # Assume we already have acces to our own resource
177
178 if len(sys.argv) <= 1:
179 edit_preferences()
180 else:
181 for appl in sys.argv[1:]:
182 edit_applet(appl)
183
Jack Jansen7571f301995-07-29 13:48:41 +0000184
185if __name__ == '__main__':
Jack Jansen7571f301995-07-29 13:48:41 +0000186 main()