blob: d45a6125b802c9871b22a12aae4cb36a81994541 [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 Jansen84872291996-10-22 15:33:02 +000018
Jack Jansen7571f301995-07-29 13:48:41 +000019# resource IDs in our own resources (dialogs, etc)
20MESSAGE_ID = 256
21
Jack Jansen145c92d1996-10-11 11:30:26 +000022DIALOG_ID = 511
Jack Jansen7571f301995-07-29 13:48:41 +000023TEXT_ITEM = 1
24OK_ITEM = 2
25CANCEL_ITEM = 3
Jack Jansendb0bace1996-04-04 15:38:44 +000026DIR_ITEM = 4
27TITLE_ITEM = 5
Jack Jansen822a30b1996-04-10 14:52:18 +000028OPTIONS_ITEM = 7
29
30# The options dialog. There is a correspondence between
31# the dialog item numbers and the option.
Jack Jansen145c92d1996-10-11 11:30:26 +000032OPT_DIALOG_ID = 510
Jack Jansen3b3a2871997-09-08 13:19:42 +000033
34# Map dialog item numbers to option names (and the reverse)
35opt_dialog_map = [
36 None,
37 "inspect",
38 "verbose",
39 "optimize",
40 "unbuffered",
41 "debugging",
42 "keepopen",
43 "keeperror",
44 "nointopt",
45 "noargs",
46 "delayconsole"]
47opt_dialog_dict = {}
48for i in range(len(opt_dialog_map)):
49 if opt_dialog_map[i]:
50 opt_dialog_dict[opt_dialog_map[i]] = i
51# 1 thru 10 are the options
Jack Jansen6c4e9871996-09-06 22:25:34 +000052# The GUSI creator/type and delay-console
Jack Jansen3b3a2871997-09-08 13:19:42 +000053OD_CREATOR_ITEM = 11
54OD_TYPE_ITEM = 12
Jack Jansen6c4e9871996-09-06 22:25:34 +000055OD_OK_ITEM = 13
56OD_CANCEL_ITEM = 14
Jack Jansen7571f301995-07-29 13:48:41 +000057
Jack Jansen3b3a2871997-09-08 13:19:42 +000058def optinteract(options):
Jack Jansen822a30b1996-04-10 14:52:18 +000059 """Let the user interact with the options dialog"""
Jack Jansen822a30b1996-04-10 14:52:18 +000060 d = GetNewDialog(OPT_DIALOG_ID, -1)
Jack Jansen6c4e9871996-09-06 22:25:34 +000061 tp, h, rect = d.GetDialogItem(OD_CREATOR_ITEM)
Jack Jansen3b3a2871997-09-08 13:19:42 +000062 SetDialogItemText(h, options['creator'])
Jack Jansen6c4e9871996-09-06 22:25:34 +000063 tp, h, rect = d.GetDialogItem(OD_TYPE_ITEM)
Jack Jansen3b3a2871997-09-08 13:19:42 +000064 SetDialogItemText(h, options['type'])
Jack Jansen822a30b1996-04-10 14:52:18 +000065 d.SetDialogDefaultItem(OD_OK_ITEM)
66 d.SetDialogCancelItem(OD_CANCEL_ITEM)
Jack Jansen3b3a2871997-09-08 13:19:42 +000067
Jack Jansen822a30b1996-04-10 14:52:18 +000068 while 1:
Jack Jansen3b3a2871997-09-08 13:19:42 +000069 for name in opt_dialog_dict.keys():
70 num = opt_dialog_dict[name]
71 tp, h, rect = d.GetDialogItem(num)
72 h.as_Control().SetControlValue(options[name])
Jack Jansen822a30b1996-04-10 14:52:18 +000073 n = ModalDialog(None)
74 if n == OD_OK_ITEM:
Jack Jansen6c4e9871996-09-06 22:25:34 +000075 tp, h, rect = d.GetDialogItem(OD_CREATOR_ITEM)
76 ncreator = GetDialogItemText(h)
77 tp, h, rect = d.GetDialogItem(OD_TYPE_ITEM)
78 ntype = GetDialogItemText(h)
79 if len(ncreator) == 4 and len(ntype) == 4:
Jack Jansen3b3a2871997-09-08 13:19:42 +000080 options['creator'] = ncreator
81 options['type'] = ntype
82 return options
Jack Jansen6c4e9871996-09-06 22:25:34 +000083 else:
Jack Jansen3b3a2871997-09-08 13:19:42 +000084 MacOS.SysBeep()
Jack Jansen822a30b1996-04-10 14:52:18 +000085 elif n == OD_CANCEL_ITEM:
Jack Jansen3b3a2871997-09-08 13:19:42 +000086 return
Jack Jansen6c4e9871996-09-06 22:25:34 +000087 elif n in (OD_CREATOR_ITEM, OD_TYPE_ITEM):
88 pass
Jack Jansen3b3a2871997-09-08 13:19:42 +000089 elif 1 <= n <= len(opt_dialog_map):
90 options[opt_dialog_map[n]] = (not options[opt_dialog_map[n]])
Jack Jansen822a30b1996-04-10 14:52:18 +000091
92
Jack Jansen3b3a2871997-09-08 13:19:42 +000093def interact(options, title):
Jack Jansen7571f301995-07-29 13:48:41 +000094 """Let the user interact with the dialog"""
Jack Jansen7571f301995-07-29 13:48:41 +000095 try:
96 # Try to go to the "correct" dir for GetDirectory
Jack Jansen3b3a2871997-09-08 13:19:42 +000097 os.chdir(options['dir'].as_pathname())
Jack Jansen7571f301995-07-29 13:48:41 +000098 except os.error:
99 pass
100 d = GetNewDialog(DIALOG_ID, -1)
Jack Jansendb0bace1996-04-04 15:38:44 +0000101 tp, h, rect = d.GetDialogItem(TITLE_ITEM)
102 SetDialogItemText(h, title)
103 tp, h, rect = d.GetDialogItem(TEXT_ITEM)
Jack Jansena918b8c1996-11-20 14:55:26 +0000104## SetDialogItemText(h, string.joinfields(list, '\r'))
Jack Jansen3b3a2871997-09-08 13:19:42 +0000105 h.data = string.joinfields(options['path'], '\r')
Jack Jansena918b8c1996-11-20 14:55:26 +0000106 d.SelectDialogItemText(TEXT_ITEM, 0, 32767)
107 d.SelectDialogItemText(TEXT_ITEM, 0, 0)
Jack Jansendb0bace1996-04-04 15:38:44 +0000108## d.SetDialogDefaultItem(OK_ITEM)
109 d.SetDialogCancelItem(CANCEL_ITEM)
Jack Jansena918b8c1996-11-20 14:55:26 +0000110 d.GetDialogWindow().ShowWindow()
111 d.DrawDialog()
Jack Jansen7571f301995-07-29 13:48:41 +0000112 while 1:
113 n = ModalDialog(None)
114 if n == OK_ITEM:
115 break
116 if n == CANCEL_ITEM:
117 return None
Jack Jansendb0bace1996-04-04 15:38:44 +0000118## if n == REVERT_ITEM:
119## return [], pythondir
Jack Jansen7571f301995-07-29 13:48:41 +0000120 if n == DIR_ITEM:
Jack Jansen9062fa21995-08-14 12:21:12 +0000121 fss, ok = macfs.GetDirectory('Select python home folder:')
Jack Jansen7571f301995-07-29 13:48:41 +0000122 if ok:
Jack Jansen3b3a2871997-09-08 13:19:42 +0000123 options['dir'] = fss
Jack Jansen822a30b1996-04-10 14:52:18 +0000124 if n == OPTIONS_ITEM:
Jack Jansen3b3a2871997-09-08 13:19:42 +0000125 noptions = options
126 for k in options.keys():
127 noptions[k] = options[k]
128 noptions = optinteract(noptions)
129 if noptions:
130 options = noptions
Jack Jansena918b8c1996-11-20 14:55:26 +0000131 tmp = string.splitfields(h.data, '\r')
Jack Jansen3b3a2871997-09-08 13:19:42 +0000132 newpath = []
Jack Jansen7571f301995-07-29 13:48:41 +0000133 for i in tmp:
134 if i:
Jack Jansen3b3a2871997-09-08 13:19:42 +0000135 newpath.append(i)
136 options['path'] = newpath
137 return options
Jack Jansen7571f301995-07-29 13:48:41 +0000138
Jack Jansendb0bace1996-04-04 15:38:44 +0000139
140def edit_preferences():
Jack Jansen3b3a2871997-09-08 13:19:42 +0000141 handler = pythonprefs.PythonOptions()
142 result = interact(handler.load(), 'System-wide preferences')
143 if result:
144 handler.save(result)
Jack Jansendb0bace1996-04-04 15:38:44 +0000145
146def edit_applet(name):
Jack Jansen3b3a2871997-09-08 13:19:42 +0000147 handler = pythonprefs.AppletOptions(name)
148 result = interact(handler.load(), os.path.split(name)[1])
149 if result:
150 handler.save(result)
Jack Jansendb0bace1996-04-04 15:38:44 +0000151
152def main():
153 try:
154 h = OpenResFile('EditPythonPrefs.rsrc')
155 except Res.Error:
156 pass # Assume we already have acces to our own resource
157
158 if len(sys.argv) <= 1:
159 edit_preferences()
160 else:
161 for appl in sys.argv[1:]:
162 edit_applet(appl)
163
Jack Jansen7571f301995-07-29 13:48:41 +0000164
165if __name__ == '__main__':
Jack Jansen7571f301995-07-29 13:48:41 +0000166 main()