blob: 29f4fdb782168829c9676fff1f257db5e334e547 [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 *
9import string
10import struct
11import macfs
12import MacOS
13import os
14import sys
15import Res # For Res.Error
Jack Jansen3b3a2871997-09-08 13:19:42 +000016import pythonprefs
17import EasyDialogs
Jack Jansenb95901e1997-09-09 13:58:19 +000018import Help
Jack Jansen84872291996-10-22 15:33:02 +000019
Jack Jansen7571f301995-07-29 13:48:41 +000020# resource IDs in our own resources (dialogs, etc)
21MESSAGE_ID = 256
22
Jack Jansen145c92d1996-10-11 11:30:26 +000023DIALOG_ID = 511
Jack Jansen7571f301995-07-29 13:48:41 +000024TEXT_ITEM = 1
25OK_ITEM = 2
26CANCEL_ITEM = 3
Jack Jansendb0bace1996-04-04 15:38:44 +000027DIR_ITEM = 4
28TITLE_ITEM = 5
Jack Jansen822a30b1996-04-10 14:52:18 +000029OPTIONS_ITEM = 7
Jack Jansenb95901e1997-09-09 13:58:19 +000030HELP_ITEM = 9
Jack Jansen822a30b1996-04-10 14:52:18 +000031
32# The options dialog. There is a correspondence between
33# the dialog item numbers and the option.
Jack Jansen145c92d1996-10-11 11:30:26 +000034OPT_DIALOG_ID = 510
Jack Jansen3b3a2871997-09-08 13:19:42 +000035
36# Map dialog item numbers to option names (and the reverse)
37opt_dialog_map = [
38 None,
39 "inspect",
40 "verbose",
41 "optimize",
42 "unbuffered",
43 "debugging",
44 "keepopen",
45 "keeperror",
46 "nointopt",
47 "noargs",
Jack Jansenb95901e1997-09-09 13:58:19 +000048 "delayconsole",
49 None, None, None, None, None, None, None, None, # 11-18 are different
50 "oldexc",
51 "nosite"]
Jack Jansen3b3a2871997-09-08 13:19:42 +000052opt_dialog_dict = {}
53for i in range(len(opt_dialog_map)):
54 if opt_dialog_map[i]:
55 opt_dialog_dict[opt_dialog_map[i]] = i
56# 1 thru 10 are the options
Jack Jansen6c4e9871996-09-06 22:25:34 +000057# The GUSI creator/type and delay-console
Jack Jansen3b3a2871997-09-08 13:19:42 +000058OD_CREATOR_ITEM = 11
59OD_TYPE_ITEM = 12
Jack Jansen6c4e9871996-09-06 22:25:34 +000060OD_OK_ITEM = 13
61OD_CANCEL_ITEM = 14
Jack Jansenb95901e1997-09-09 13:58:19 +000062OD_HELP_ITEM = 22
Jack Jansen7571f301995-07-29 13:48:41 +000063
Jack Jansen3b3a2871997-09-08 13:19:42 +000064def optinteract(options):
Jack Jansen822a30b1996-04-10 14:52:18 +000065 """Let the user interact with the options dialog"""
Jack Jansen822a30b1996-04-10 14:52:18 +000066 d = GetNewDialog(OPT_DIALOG_ID, -1)
Jack Jansen6c4e9871996-09-06 22:25:34 +000067 tp, h, rect = d.GetDialogItem(OD_CREATOR_ITEM)
Jack Jansen3b3a2871997-09-08 13:19:42 +000068 SetDialogItemText(h, options['creator'])
Jack Jansen6c4e9871996-09-06 22:25:34 +000069 tp, h, rect = d.GetDialogItem(OD_TYPE_ITEM)
Jack Jansen3b3a2871997-09-08 13:19:42 +000070 SetDialogItemText(h, options['type'])
Jack Jansen822a30b1996-04-10 14:52:18 +000071 d.SetDialogDefaultItem(OD_OK_ITEM)
72 d.SetDialogCancelItem(OD_CANCEL_ITEM)
Jack Jansen3b3a2871997-09-08 13:19:42 +000073
Jack Jansen822a30b1996-04-10 14:52:18 +000074 while 1:
Jack Jansen3b3a2871997-09-08 13:19:42 +000075 for name in opt_dialog_dict.keys():
76 num = opt_dialog_dict[name]
77 tp, h, rect = d.GetDialogItem(num)
78 h.as_Control().SetControlValue(options[name])
Jack Jansen822a30b1996-04-10 14:52:18 +000079 n = ModalDialog(None)
80 if n == OD_OK_ITEM:
Jack Jansen6c4e9871996-09-06 22:25:34 +000081 tp, h, rect = d.GetDialogItem(OD_CREATOR_ITEM)
82 ncreator = GetDialogItemText(h)
83 tp, h, rect = d.GetDialogItem(OD_TYPE_ITEM)
84 ntype = GetDialogItemText(h)
85 if len(ncreator) == 4 and len(ntype) == 4:
Jack Jansen3b3a2871997-09-08 13:19:42 +000086 options['creator'] = ncreator
87 options['type'] = ntype
88 return options
Jack Jansen6c4e9871996-09-06 22:25:34 +000089 else:
Jack Jansen3b3a2871997-09-08 13:19:42 +000090 MacOS.SysBeep()
Jack Jansen822a30b1996-04-10 14:52:18 +000091 elif n == OD_CANCEL_ITEM:
Jack Jansen3b3a2871997-09-08 13:19:42 +000092 return
Jack Jansen6c4e9871996-09-06 22:25:34 +000093 elif n in (OD_CREATOR_ITEM, OD_TYPE_ITEM):
94 pass
Jack Jansenb95901e1997-09-09 13:58:19 +000095 elif n == OD_HELP_ITEM:
96 onoff = Help.HMGetBalloons()
97 Help.HMSetBalloons(not onoff)
Jack Jansen3b3a2871997-09-08 13:19:42 +000098 elif 1 <= n <= len(opt_dialog_map):
99 options[opt_dialog_map[n]] = (not options[opt_dialog_map[n]])
Jack Jansen822a30b1996-04-10 14:52:18 +0000100
101
Jack Jansen3b3a2871997-09-08 13:19:42 +0000102def interact(options, title):
Jack Jansen7571f301995-07-29 13:48:41 +0000103 """Let the user interact with the dialog"""
Jack Jansen7571f301995-07-29 13:48:41 +0000104 try:
105 # Try to go to the "correct" dir for GetDirectory
Jack Jansen3b3a2871997-09-08 13:19:42 +0000106 os.chdir(options['dir'].as_pathname())
Jack Jansen7571f301995-07-29 13:48:41 +0000107 except os.error:
108 pass
109 d = GetNewDialog(DIALOG_ID, -1)
Jack Jansendb0bace1996-04-04 15:38:44 +0000110 tp, h, rect = d.GetDialogItem(TITLE_ITEM)
111 SetDialogItemText(h, title)
112 tp, h, rect = d.GetDialogItem(TEXT_ITEM)
Jack Jansena918b8c1996-11-20 14:55:26 +0000113## SetDialogItemText(h, string.joinfields(list, '\r'))
Jack Jansen3b3a2871997-09-08 13:19:42 +0000114 h.data = string.joinfields(options['path'], '\r')
Jack Jansena918b8c1996-11-20 14:55:26 +0000115 d.SelectDialogItemText(TEXT_ITEM, 0, 32767)
116 d.SelectDialogItemText(TEXT_ITEM, 0, 0)
Jack Jansendb0bace1996-04-04 15:38:44 +0000117## d.SetDialogDefaultItem(OK_ITEM)
118 d.SetDialogCancelItem(CANCEL_ITEM)
Jack Jansena918b8c1996-11-20 14:55:26 +0000119 d.GetDialogWindow().ShowWindow()
120 d.DrawDialog()
Jack Jansen7571f301995-07-29 13:48:41 +0000121 while 1:
122 n = ModalDialog(None)
123 if n == OK_ITEM:
124 break
125 if n == CANCEL_ITEM:
126 return None
Jack Jansendb0bace1996-04-04 15:38:44 +0000127## if n == REVERT_ITEM:
128## return [], pythondir
Jack Jansen7571f301995-07-29 13:48:41 +0000129 if n == DIR_ITEM:
Jack Jansen9062fa21995-08-14 12:21:12 +0000130 fss, ok = macfs.GetDirectory('Select python home folder:')
Jack Jansen7571f301995-07-29 13:48:41 +0000131 if ok:
Jack Jansen3b3a2871997-09-08 13:19:42 +0000132 options['dir'] = fss
Jack Jansenb95901e1997-09-09 13:58:19 +0000133 elif n == HELP_ITEM:
134 onoff = Help.HMGetBalloons()
135 Help.HMSetBalloons(not onoff)
Jack Jansen822a30b1996-04-10 14:52:18 +0000136 if n == OPTIONS_ITEM:
Jack Jansen3b3a2871997-09-08 13:19:42 +0000137 noptions = options
138 for k in options.keys():
139 noptions[k] = options[k]
140 noptions = optinteract(noptions)
141 if noptions:
142 options = noptions
Jack Jansena918b8c1996-11-20 14:55:26 +0000143 tmp = string.splitfields(h.data, '\r')
Jack Jansen3b3a2871997-09-08 13:19:42 +0000144 newpath = []
Jack Jansen7571f301995-07-29 13:48:41 +0000145 for i in tmp:
146 if i:
Jack Jansen3b3a2871997-09-08 13:19:42 +0000147 newpath.append(i)
148 options['path'] = newpath
149 return options
Jack Jansen7571f301995-07-29 13:48:41 +0000150
Jack Jansendb0bace1996-04-04 15:38:44 +0000151
152def edit_preferences():
Jack Jansen3b3a2871997-09-08 13:19:42 +0000153 handler = pythonprefs.PythonOptions()
154 result = interact(handler.load(), 'System-wide preferences')
155 if result:
156 handler.save(result)
Jack Jansendb0bace1996-04-04 15:38:44 +0000157
158def edit_applet(name):
Jack Jansen3b3a2871997-09-08 13:19:42 +0000159 handler = pythonprefs.AppletOptions(name)
160 result = interact(handler.load(), os.path.split(name)[1])
161 if result:
162 handler.save(result)
Jack Jansendb0bace1996-04-04 15:38:44 +0000163
164def main():
165 try:
166 h = OpenResFile('EditPythonPrefs.rsrc')
167 except Res.Error:
168 pass # Assume we already have acces to our own resource
169
170 if len(sys.argv) <= 1:
171 edit_preferences()
172 else:
173 for appl in sys.argv[1:]:
174 edit_applet(appl)
175
Jack Jansen7571f301995-07-29 13:48:41 +0000176
177if __name__ == '__main__':
Jack Jansen7571f301995-07-29 13:48:41 +0000178 main()