Jack Jansen | 7571f30 | 1995-07-29 13:48:41 +0000 | [diff] [blame] | 1 | """Edit the Python Preferences file.""" |
Jack Jansen | 822a30b | 1996-04-10 14:52:18 +0000 | [diff] [blame] | 2 | # |
| 3 | # This program is getting more and more clunky. It should really |
| 4 | # be rewritten in a modeless way some time soon. |
Jack Jansen | 7571f30 | 1995-07-29 13:48:41 +0000 | [diff] [blame] | 5 | |
| 6 | from Dlg import * |
| 7 | from Events import * |
| 8 | from Res import * |
Jack Jansen | 6a6db07 | 1999-12-23 14:34:07 +0000 | [diff] [blame] | 9 | import Controls |
Jack Jansen | 7571f30 | 1995-07-29 13:48:41 +0000 | [diff] [blame] | 10 | import string |
| 11 | import struct |
| 12 | import macfs |
| 13 | import MacOS |
| 14 | import os |
| 15 | import sys |
| 16 | import Res # For Res.Error |
Jack Jansen | 3b3a287 | 1997-09-08 13:19:42 +0000 | [diff] [blame] | 17 | import pythonprefs |
| 18 | import EasyDialogs |
Jack Jansen | b95901e | 1997-09-09 13:58:19 +0000 | [diff] [blame] | 19 | import Help |
Jack Jansen | 8487229 | 1996-10-22 15:33:02 +0000 | [diff] [blame] | 20 | |
Jack Jansen | 7571f30 | 1995-07-29 13:48:41 +0000 | [diff] [blame] | 21 | # resource IDs in our own resources (dialogs, etc) |
| 22 | MESSAGE_ID = 256 |
| 23 | |
Jack Jansen | 145c92d | 1996-10-11 11:30:26 +0000 | [diff] [blame] | 24 | DIALOG_ID = 511 |
Jack Jansen | 7571f30 | 1995-07-29 13:48:41 +0000 | [diff] [blame] | 25 | TEXT_ITEM = 1 |
| 26 | OK_ITEM = 2 |
| 27 | CANCEL_ITEM = 3 |
Jack Jansen | db0bace | 1996-04-04 15:38:44 +0000 | [diff] [blame] | 28 | DIR_ITEM = 4 |
| 29 | TITLE_ITEM = 5 |
Jack Jansen | 822a30b | 1996-04-10 14:52:18 +0000 | [diff] [blame] | 30 | OPTIONS_ITEM = 7 |
Jack Jansen | b95901e | 1997-09-09 13:58:19 +0000 | [diff] [blame] | 31 | HELP_ITEM = 9 |
Jack Jansen | 822a30b | 1996-04-10 14:52:18 +0000 | [diff] [blame] | 32 | |
| 33 | # The options dialog. There is a correspondence between |
| 34 | # the dialog item numbers and the option. |
Jack Jansen | 145c92d | 1996-10-11 11:30:26 +0000 | [diff] [blame] | 35 | OPT_DIALOG_ID = 510 |
Jack Jansen | 3b3a287 | 1997-09-08 13:19:42 +0000 | [diff] [blame] | 36 | |
| 37 | # Map dialog item numbers to option names (and the reverse) |
| 38 | opt_dialog_map = [ |
| 39 | None, |
Jack Jansen | 4a5eb96 | 2000-09-22 21:50:11 +0000 | [diff] [blame] | 40 | None, |
Jack Jansen | 3e3eb3e | 2000-10-19 21:50:54 +0000 | [diff] [blame] | 41 | None, |
Jack Jansen | 3b3a287 | 1997-09-08 13:19:42 +0000 | [diff] [blame] | 42 | "inspect", |
| 43 | "verbose", |
| 44 | "optimize", |
| 45 | "unbuffered", |
| 46 | "debugging", |
Jack Jansen | 4a5eb96 | 2000-09-22 21:50:11 +0000 | [diff] [blame] | 47 | "tabwarn", |
| 48 | "nosite", |
| 49 | "nonavservice", |
Jack Jansen | 3b3a287 | 1997-09-08 13:19:42 +0000 | [diff] [blame] | 50 | "nointopt", |
| 51 | "noargs", |
Jack Jansen | b95901e | 1997-09-09 13:58:19 +0000 | [diff] [blame] | 52 | "delayconsole", |
Jack Jansen | 4a5eb96 | 2000-09-22 21:50:11 +0000 | [diff] [blame] | 53 | ] |
Jack Jansen | 3b3a287 | 1997-09-08 13:19:42 +0000 | [diff] [blame] | 54 | opt_dialog_dict = {} |
| 55 | for 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 Jansen | 6c4e987 | 1996-09-06 22:25:34 +0000 | [diff] [blame] | 59 | # The GUSI creator/type and delay-console |
Jack Jansen | 4a5eb96 | 2000-09-22 21:50:11 +0000 | [diff] [blame] | 60 | OD_CREATOR_ITEM = 18 |
| 61 | OD_TYPE_ITEM = 19 |
| 62 | OD_OK_ITEM = 1 |
| 63 | OD_CANCEL_ITEM = 2 |
| 64 | OD_HELP_ITEM = 20 |
| 65 | OD_KEEPALWAYS_ITEM = 14 |
| 66 | OD_KEEPOUTPUT_ITEM = 15 |
| 67 | OD_KEEPERROR_ITEM = 16 |
| 68 | OD_KEEPNEVER_ITEM = 17 |
Jack Jansen | 7571f30 | 1995-07-29 13:48:41 +0000 | [diff] [blame] | 69 | |
Jack Jansen | 3b3a287 | 1997-09-08 13:19:42 +0000 | [diff] [blame] | 70 | def optinteract(options): |
Jack Jansen | 822a30b | 1996-04-10 14:52:18 +0000 | [diff] [blame] | 71 | """Let the user interact with the options dialog""" |
Jack Jansen | 822a30b | 1996-04-10 14:52:18 +0000 | [diff] [blame] | 72 | d = GetNewDialog(OPT_DIALOG_ID, -1) |
Jack Jansen | 6a6db07 | 1999-12-23 14:34:07 +0000 | [diff] [blame] | 73 | htext = d.GetDialogItemAsControl(OD_CREATOR_ITEM) |
| 74 | SetDialogItemText(htext, options['creator']) |
| 75 | htext = d.GetDialogItemAsControl(OD_TYPE_ITEM) |
| 76 | SetDialogItemText(htext, options['type']) |
Jack Jansen | 822a30b | 1996-04-10 14:52:18 +0000 | [diff] [blame] | 77 | d.SetDialogDefaultItem(OD_OK_ITEM) |
| 78 | d.SetDialogCancelItem(OD_CANCEL_ITEM) |
| 79 | while 1: |
Jack Jansen | 3b3a287 | 1997-09-08 13:19:42 +0000 | [diff] [blame] | 80 | for name in opt_dialog_dict.keys(): |
| 81 | num = opt_dialog_dict[name] |
Jack Jansen | 6a6db07 | 1999-12-23 14:34:07 +0000 | [diff] [blame] | 82 | ctl = d.GetDialogItemAsControl(num) |
| 83 | ctl.SetControlValue(options[name]) |
Jack Jansen | 4a5eb96 | 2000-09-22 21:50:11 +0000 | [diff] [blame] | 84 | ctl = d.GetDialogItemAsControl(OD_KEEPALWAYS_ITEM) |
| 85 | ctl.SetControlValue(options['keep_console'] == 3) |
| 86 | ctl = d.GetDialogItemAsControl(OD_KEEPOUTPUT_ITEM) |
| 87 | ctl.SetControlValue(options['keep_console'] == 1) |
| 88 | ctl = d.GetDialogItemAsControl(OD_KEEPERROR_ITEM) |
| 89 | ctl.SetControlValue(options['keep_console'] == 2) |
| 90 | ctl = d.GetDialogItemAsControl(OD_KEEPNEVER_ITEM) |
| 91 | ctl.SetControlValue(options['keep_console'] == 0) |
Jack Jansen | 822a30b | 1996-04-10 14:52:18 +0000 | [diff] [blame] | 92 | n = ModalDialog(None) |
| 93 | if n == OD_OK_ITEM: |
Jack Jansen | 6a6db07 | 1999-12-23 14:34:07 +0000 | [diff] [blame] | 94 | htext = d.GetDialogItemAsControl(OD_CREATOR_ITEM) |
| 95 | ncreator = GetDialogItemText(htext) |
| 96 | htext = d.GetDialogItemAsControl(OD_TYPE_ITEM) |
| 97 | ntype = GetDialogItemText(htext) |
Jack Jansen | 6c4e987 | 1996-09-06 22:25:34 +0000 | [diff] [blame] | 98 | if len(ncreator) == 4 and len(ntype) == 4: |
Jack Jansen | 3b3a287 | 1997-09-08 13:19:42 +0000 | [diff] [blame] | 99 | options['creator'] = ncreator |
| 100 | options['type'] = ntype |
| 101 | return options |
Jack Jansen | 6c4e987 | 1996-09-06 22:25:34 +0000 | [diff] [blame] | 102 | else: |
Jack Jansen | 3b3a287 | 1997-09-08 13:19:42 +0000 | [diff] [blame] | 103 | MacOS.SysBeep() |
Jack Jansen | 822a30b | 1996-04-10 14:52:18 +0000 | [diff] [blame] | 104 | elif n == OD_CANCEL_ITEM: |
Jack Jansen | 3b3a287 | 1997-09-08 13:19:42 +0000 | [diff] [blame] | 105 | return |
Jack Jansen | 6c4e987 | 1996-09-06 22:25:34 +0000 | [diff] [blame] | 106 | elif n in (OD_CREATOR_ITEM, OD_TYPE_ITEM): |
| 107 | pass |
Jack Jansen | 4a5eb96 | 2000-09-22 21:50:11 +0000 | [diff] [blame] | 108 | elif n == OD_KEEPALWAYS_ITEM: |
| 109 | options['keep_console'] = 3; |
| 110 | elif n == OD_KEEPOUTPUT_ITEM: |
| 111 | options['keep_console'] = 1; |
| 112 | elif n == OD_KEEPERROR_ITEM: |
| 113 | options['keep_console'] = 2; |
| 114 | elif n == OD_KEEPNEVER_ITEM: |
| 115 | options['keep_console'] = 0; |
Jack Jansen | b95901e | 1997-09-09 13:58:19 +0000 | [diff] [blame] | 116 | elif n == OD_HELP_ITEM: |
| 117 | onoff = Help.HMGetBalloons() |
| 118 | Help.HMSetBalloons(not onoff) |
Jack Jansen | 3b3a287 | 1997-09-08 13:19:42 +0000 | [diff] [blame] | 119 | elif 1 <= n <= len(opt_dialog_map): |
| 120 | options[opt_dialog_map[n]] = (not options[opt_dialog_map[n]]) |
Jack Jansen | 822a30b | 1996-04-10 14:52:18 +0000 | [diff] [blame] | 121 | |
| 122 | |
Jack Jansen | 3b3a287 | 1997-09-08 13:19:42 +0000 | [diff] [blame] | 123 | def interact(options, title): |
Jack Jansen | 7571f30 | 1995-07-29 13:48:41 +0000 | [diff] [blame] | 124 | """Let the user interact with the dialog""" |
Jack Jansen | 7571f30 | 1995-07-29 13:48:41 +0000 | [diff] [blame] | 125 | try: |
| 126 | # Try to go to the "correct" dir for GetDirectory |
Jack Jansen | 3b3a287 | 1997-09-08 13:19:42 +0000 | [diff] [blame] | 127 | os.chdir(options['dir'].as_pathname()) |
Jack Jansen | 7571f30 | 1995-07-29 13:48:41 +0000 | [diff] [blame] | 128 | except os.error: |
| 129 | pass |
| 130 | d = GetNewDialog(DIALOG_ID, -1) |
Jack Jansen | 6a6db07 | 1999-12-23 14:34:07 +0000 | [diff] [blame] | 131 | htext = d.GetDialogItemAsControl(TITLE_ITEM) |
| 132 | SetDialogItemText(htext, title) |
Jack Jansen | 8242c9e | 2000-01-13 16:22:12 +0000 | [diff] [blame] | 133 | path_ctl = d.GetDialogItemAsControl(TEXT_ITEM) |
Jack Jansen | 6a6db07 | 1999-12-23 14:34:07 +0000 | [diff] [blame] | 134 | data = string.joinfields(options['path'], '\r') |
Jack Jansen | 8242c9e | 2000-01-13 16:22:12 +0000 | [diff] [blame] | 135 | path_ctl.SetControlData(Controls.kControlEditTextPart, Controls.kControlEditTextTextTag, data) |
Jack Jansen | 6a6db07 | 1999-12-23 14:34:07 +0000 | [diff] [blame] | 136 | |
Jack Jansen | a918b8c | 1996-11-20 14:55:26 +0000 | [diff] [blame] | 137 | d.SelectDialogItemText(TEXT_ITEM, 0, 32767) |
| 138 | d.SelectDialogItemText(TEXT_ITEM, 0, 0) |
Jack Jansen | db0bace | 1996-04-04 15:38:44 +0000 | [diff] [blame] | 139 | ## d.SetDialogDefaultItem(OK_ITEM) |
| 140 | d.SetDialogCancelItem(CANCEL_ITEM) |
Jack Jansen | a918b8c | 1996-11-20 14:55:26 +0000 | [diff] [blame] | 141 | d.GetDialogWindow().ShowWindow() |
| 142 | d.DrawDialog() |
Jack Jansen | 7571f30 | 1995-07-29 13:48:41 +0000 | [diff] [blame] | 143 | while 1: |
| 144 | n = ModalDialog(None) |
| 145 | if n == OK_ITEM: |
| 146 | break |
| 147 | if n == CANCEL_ITEM: |
| 148 | return None |
Jack Jansen | db0bace | 1996-04-04 15:38:44 +0000 | [diff] [blame] | 149 | ## if n == REVERT_ITEM: |
| 150 | ## return [], pythondir |
Jack Jansen | 7571f30 | 1995-07-29 13:48:41 +0000 | [diff] [blame] | 151 | if n == DIR_ITEM: |
Jack Jansen | 9062fa2 | 1995-08-14 12:21:12 +0000 | [diff] [blame] | 152 | fss, ok = macfs.GetDirectory('Select python home folder:') |
Jack Jansen | 7571f30 | 1995-07-29 13:48:41 +0000 | [diff] [blame] | 153 | if ok: |
Jack Jansen | 3b3a287 | 1997-09-08 13:19:42 +0000 | [diff] [blame] | 154 | options['dir'] = fss |
Jack Jansen | b95901e | 1997-09-09 13:58:19 +0000 | [diff] [blame] | 155 | elif n == HELP_ITEM: |
| 156 | onoff = Help.HMGetBalloons() |
| 157 | Help.HMSetBalloons(not onoff) |
Jack Jansen | 822a30b | 1996-04-10 14:52:18 +0000 | [diff] [blame] | 158 | if n == OPTIONS_ITEM: |
Jack Jansen | 3b3a287 | 1997-09-08 13:19:42 +0000 | [diff] [blame] | 159 | noptions = options |
| 160 | for k in options.keys(): |
| 161 | noptions[k] = options[k] |
| 162 | noptions = optinteract(noptions) |
| 163 | if noptions: |
| 164 | options = noptions |
Jack Jansen | 8242c9e | 2000-01-13 16:22:12 +0000 | [diff] [blame] | 165 | data = path_ctl.GetControlData(Controls.kControlEditTextPart, Controls.kControlEditTextTextTag) |
| 166 | tmp = string.splitfields(data, '\r') |
Jack Jansen | 3b3a287 | 1997-09-08 13:19:42 +0000 | [diff] [blame] | 167 | newpath = [] |
Jack Jansen | 7571f30 | 1995-07-29 13:48:41 +0000 | [diff] [blame] | 168 | for i in tmp: |
| 169 | if i: |
Jack Jansen | 3b3a287 | 1997-09-08 13:19:42 +0000 | [diff] [blame] | 170 | newpath.append(i) |
| 171 | options['path'] = newpath |
| 172 | return options |
Jack Jansen | 7571f30 | 1995-07-29 13:48:41 +0000 | [diff] [blame] | 173 | |
Jack Jansen | db0bace | 1996-04-04 15:38:44 +0000 | [diff] [blame] | 174 | |
| 175 | def edit_preferences(): |
Jack Jansen | 3b3a287 | 1997-09-08 13:19:42 +0000 | [diff] [blame] | 176 | handler = pythonprefs.PythonOptions() |
Jack Jansen | 43fd1f7 | 1999-12-02 22:52:12 +0000 | [diff] [blame] | 177 | options = handler.load() |
| 178 | if options['noargs']: |
| 179 | EasyDialogs.Message('Warning: system-wide sys.argv processing is off.\nIf you dropped an applet I have not seen it.') |
| 180 | result = interact(options, 'System-wide preferences') |
Jack Jansen | 3b3a287 | 1997-09-08 13:19:42 +0000 | [diff] [blame] | 181 | if result: |
| 182 | handler.save(result) |
Jack Jansen | db0bace | 1996-04-04 15:38:44 +0000 | [diff] [blame] | 183 | |
| 184 | def edit_applet(name): |
Jack Jansen | 3b3a287 | 1997-09-08 13:19:42 +0000 | [diff] [blame] | 185 | handler = pythonprefs.AppletOptions(name) |
| 186 | result = interact(handler.load(), os.path.split(name)[1]) |
| 187 | if result: |
| 188 | handler.save(result) |
Jack Jansen | db0bace | 1996-04-04 15:38:44 +0000 | [diff] [blame] | 189 | |
| 190 | def main(): |
| 191 | try: |
Jack Jansen | d13c385 | 2000-06-20 21:59:25 +0000 | [diff] [blame] | 192 | h = FSpOpenResFile('EditPythonPrefs.rsrc', 1) |
Jack Jansen | db0bace | 1996-04-04 15:38:44 +0000 | [diff] [blame] | 193 | except Res.Error: |
| 194 | pass # Assume we already have acces to our own resource |
| 195 | |
Jack Jansen | 8c94d5e | 2000-10-13 23:34:06 +0000 | [diff] [blame] | 196 | MacOS.SchedParams(1, 0) |
Jack Jansen | db0bace | 1996-04-04 15:38:44 +0000 | [diff] [blame] | 197 | if len(sys.argv) <= 1: |
| 198 | edit_preferences() |
| 199 | else: |
| 200 | for appl in sys.argv[1:]: |
| 201 | edit_applet(appl) |
| 202 | |
Jack Jansen | 7571f30 | 1995-07-29 13:48:41 +0000 | [diff] [blame] | 203 | |
| 204 | if __name__ == '__main__': |
Jack Jansen | 7571f30 | 1995-07-29 13:48:41 +0000 | [diff] [blame] | 205 | main() |