Guido van Rossum | b340466 | 1995-01-22 18:36:13 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Jack Jansen | 42218ce | 1997-01-31 16:15:11 +0000 | [diff] [blame] | 2 | Copyright 1991-1997 by Stichting Mathematisch Centrum, Amsterdam, |
Guido van Rossum | b340466 | 1995-01-22 18:36:13 +0000 | [diff] [blame] | 3 | The Netherlands. |
| 4 | |
| 5 | All Rights Reserved |
| 6 | |
| 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
| 9 | provided that the above copyright notice appear in all copies and that |
| 10 | both that copyright notice and this permission notice appear in |
| 11 | supporting documentation, and that the names of Stichting Mathematisch |
| 12 | Centrum or CWI not be used in advertising or publicity pertaining to |
| 13 | distribution of the software without specific, written prior permission. |
| 14 | |
| 15 | STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO |
| 16 | THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 17 | FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE |
| 18 | FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 19 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 20 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 21 | OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 22 | |
| 23 | ******************************************************************/ |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 24 | |
Jack Jansen | bf05d4c | 1996-08-19 15:11:45 +0000 | [diff] [blame] | 25 | #ifdef __CFM68K__ |
| 26 | /* cfm68k InterfaceLib exports GetEventQueue, but Events.h doesn't know this |
| 27 | ** and defines it as GetEvQHdr (which is correct for PPC). This fix is for |
| 28 | ** CW9, check that the workaround is still needed for the next release. |
| 29 | */ |
| 30 | #define GetEvQHdr GetEventQueue |
| 31 | #endif /* __CFM68K__ */ |
| 32 | |
| 33 | #include <Events.h> |
| 34 | |
| 35 | #ifdef __CFM68K__ |
| 36 | #undef GetEventQueue |
| 37 | #endif /* __CFM68K__ */ |
| 38 | |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 39 | #include "Python.h" |
Guido van Rossum | becdbec | 1995-02-14 01:27:24 +0000 | [diff] [blame] | 40 | |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 41 | #include "macglue.h" |
Jack Jansen | 74162f3 | 1995-02-15 22:58:33 +0000 | [diff] [blame] | 42 | #include "marshal.h" |
| 43 | #include "import.h" |
Jack Jansen | eda7863 | 1997-06-12 15:29:46 +0000 | [diff] [blame] | 44 | #include "importdl.h" |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 45 | |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 46 | #include "pythonresources.h" |
| 47 | |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 48 | #include <OSUtils.h> /* for Set(Current)A5 */ |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 49 | #include <Files.h> |
Jack Jansen | 8cd2b72 | 1995-02-13 11:33:28 +0000 | [diff] [blame] | 50 | #include <StandardFile.h> |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 51 | #include <Resources.h> |
| 52 | #include <Memory.h> |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 53 | #include <Windows.h> |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 54 | #include <Traps.h> |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 55 | #include <Processes.h> |
Guido van Rossum | c3d1c8e | 1995-02-18 15:01:31 +0000 | [diff] [blame] | 56 | #include <Fonts.h> |
| 57 | #include <Menus.h> |
Jack Jansen | 0830550 | 1995-06-18 20:03:40 +0000 | [diff] [blame] | 58 | #include <TextUtils.h> |
Guido van Rossum | cc0d879 | 1995-01-30 08:57:13 +0000 | [diff] [blame] | 59 | #ifdef THINK_C |
| 60 | #include <OSEvents.h> /* For EvQElPtr */ |
| 61 | #endif |
Jack Jansen | 16df2aa | 1995-02-27 16:17:28 +0000 | [diff] [blame] | 62 | #ifdef __MWERKS__ |
| 63 | #include <SIOUX.h> |
| 64 | #endif |
Jack Jansen | 911ad6b | 1996-03-05 16:56:24 +0000 | [diff] [blame] | 65 | #ifdef USE_GUSI |
| 66 | #include <TFileSpec.h> /* For Path2FSSpec */ |
Jack Jansen | 378815c | 1996-03-06 16:21:34 +0000 | [diff] [blame] | 67 | #include <LowMem.h> /* For SetSFCurDir, etc */ |
Jack Jansen | f6865f7 | 1996-09-04 15:24:59 +0000 | [diff] [blame] | 68 | #include <GUSI.h> |
Jack Jansen | 911ad6b | 1996-03-05 16:56:24 +0000 | [diff] [blame] | 69 | #endif |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 70 | |
Jack Jansen | 3469e99 | 1996-09-06 00:30:45 +0000 | [diff] [blame] | 71 | /* The ID of the Sioux apple menu */ |
| 72 | #define SIOUX_APPLEID 32000 |
| 73 | |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 74 | #include <signal.h> |
Jack Jansen | 74162f3 | 1995-02-15 22:58:33 +0000 | [diff] [blame] | 75 | #include <stdio.h> |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 76 | |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 77 | /* |
Jack Jansen | d1f0631 | 1996-08-01 15:23:54 +0000 | [diff] [blame] | 78 | ** When less than this amount of stackspace is left we |
| 79 | ** raise a MemoryError. |
| 80 | */ |
| 81 | #ifndef MINIMUM_STACK_SIZE |
| 82 | #ifdef __powerc |
| 83 | #define MINIMUM_STACK_SIZE 8192 |
| 84 | #else |
| 85 | #define MINIMUM_STACK_SIZE 4096 |
| 86 | #endif |
| 87 | #endif |
| 88 | |
| 89 | /* |
Jack Jansen | 16df2aa | 1995-02-27 16:17:28 +0000 | [diff] [blame] | 90 | ** We have to be careful, since we can't handle |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 91 | ** things like updates (and they'll keep coming back if we don't |
Jack Jansen | 16df2aa | 1995-02-27 16:17:28 +0000 | [diff] [blame] | 92 | ** handle them). Note that we don't know who has windows open, so |
| 93 | ** even handing updates off to SIOUX under MW isn't going to work. |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 94 | */ |
Jack Jansen | 0c96887 | 1997-08-26 13:20:34 +0000 | [diff] [blame] | 95 | #define MAINLOOP_EVENTMASK (mDownMask|keyDownMask|osMask|activMask) |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 96 | |
| 97 | #include <signal.h> |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 98 | |
Guido van Rossum | b340466 | 1995-01-22 18:36:13 +0000 | [diff] [blame] | 99 | /* XXX We should include Errors.h here, but it has a name conflict |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 100 | ** with the python errors.h. */ |
| 101 | #define fnfErr -43 |
| 102 | |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 103 | /* Declared in macfsmodule.c: */ |
| 104 | extern FSSpec *mfs_GetFSSpecFSSpec(); |
Jack Jansen | 8bb573e | 1999-12-12 21:37:14 +0000 | [diff] [blame] | 105 | extern PyObject *newmfssobject Py_PROTO((FSSpec *)); |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 106 | |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 107 | /* Interrupt code variables: */ |
| 108 | static int interrupted; /* Set to true when cmd-. seen */ |
| 109 | static RETSIGTYPE intcatcher Py_PROTO((int)); |
| 110 | |
Jack Jansen | 36ed506 | 1997-06-20 16:18:15 +0000 | [diff] [blame] | 111 | static int PyMac_DoYield Py_PROTO((int, int)); |
| 112 | static int PyMac_Yield Py_PROTO((void)); |
Jack Jansen | f6865f7 | 1996-09-04 15:24:59 +0000 | [diff] [blame] | 113 | |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 114 | /* |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 115 | ** These are the real scheduling parameters that control what we check |
| 116 | ** in the event loop, and how often we check. The values are initialized |
| 117 | ** from pyMac_SchedParamStruct. |
| 118 | */ |
| 119 | |
| 120 | struct real_sched_param_struct { |
| 121 | int check_interrupt; /* if true check for command-dot */ |
| 122 | int process_events; /* if nonzero enable evt processing, this mask */ |
| 123 | int besocial; /* if nonzero be a little social with CPU */ |
| 124 | unsigned long check_interval; /* how often to check, in ticks */ |
| 125 | unsigned long bg_yield; /* yield so long when in background */ |
| 126 | /* these are computed from previous and clock and such */ |
| 127 | int enabled; /* check_interrupt OR process_event OR yield */ |
| 128 | unsigned long next_check; /* when to check/yield next, in ticks */ |
| 129 | }; |
| 130 | |
| 131 | static struct real_sched_param_struct schedparams = |
| 132 | { 1, MAINLOOP_EVENTMASK, 1, 15, 15, 1, 0}; |
| 133 | |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 134 | /* |
Jack Jansen | f6865f7 | 1996-09-04 15:24:59 +0000 | [diff] [blame] | 135 | ** Workaround for sioux/gusi combo: set when we are exiting |
| 136 | */ |
| 137 | int PyMac_ConsoleIsDead; |
| 138 | |
| 139 | /* |
Jack Jansen | caa7c46 | 1997-06-12 10:49:13 +0000 | [diff] [blame] | 140 | ** Sioux menu bar, saved early so we can restore it |
| 141 | */ |
| 142 | static Handle sioux_mbar; |
| 143 | |
| 144 | /* |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 145 | ** Some stuff for our GetDirectory and PromptGetFile routines |
| 146 | */ |
| 147 | struct hook_args { |
| 148 | int selectcur_hit; /* Set to true when "select current" selected */ |
| 149 | char *prompt; /* The prompt */ |
| 150 | }; |
| 151 | static DlgHookYDUPP myhook_upp; |
| 152 | static int upp_inited = 0; |
| 153 | |
Jack Jansen | 36ed506 | 1997-06-20 16:18:15 +0000 | [diff] [blame] | 154 | /* |
| 155 | ** The python-code event handler |
| 156 | */ |
| 157 | static PyObject *python_event_handler; |
| 158 | |
Jack Jansen | 8f5725a | 1999-12-07 23:08:10 +0000 | [diff] [blame] | 159 | /* |
| 160 | ** Set to true if we're appearance-compliant |
| 161 | */ |
| 162 | int PyMac_AppearanceCompliant; |
| 163 | |
Jack Jansen | 378815c | 1996-03-06 16:21:34 +0000 | [diff] [blame] | 164 | #ifdef USE_GUSI |
| 165 | /* |
| 166 | ** GUSI (1.6.0 and earlier, at the least) do not set the MacOS idea of |
| 167 | ** the working directory. Hence, we call this routine after each call |
| 168 | ** to chdir() to rectify things. |
| 169 | */ |
| 170 | void |
| 171 | PyMac_FixGUSIcd() |
| 172 | { |
| 173 | WDPBRec pb; |
| 174 | FSSpec curdirfss; |
| 175 | |
| 176 | if ( Path2FSSpec(":x", &curdirfss) != noErr ) |
| 177 | return; |
| 178 | |
| 179 | /* Set MacOS "working directory" */ |
| 180 | pb.ioNamePtr= "\p"; |
| 181 | pb.ioVRefNum= curdirfss.vRefNum; |
| 182 | pb.ioWDDirID= curdirfss.parID; |
Jack Jansen | 08c3be3 | 1997-04-08 15:27:00 +0000 | [diff] [blame] | 183 | if (PBHSetVolSync(&pb) != noErr) |
Jack Jansen | 378815c | 1996-03-06 16:21:34 +0000 | [diff] [blame] | 184 | return; |
Jack Jansen | 378815c | 1996-03-06 16:21:34 +0000 | [diff] [blame] | 185 | } |
Jack Jansen | 7ac70af | 1996-08-19 11:01:05 +0000 | [diff] [blame] | 186 | |
Jack Jansen | 7d5f9e8 | 1996-09-07 17:09:31 +0000 | [diff] [blame] | 187 | /* |
| 188 | ** SpinCursor (needed by GUSI) drags in heaps of stuff, so we |
| 189 | ** provide a dummy here. |
| 190 | */ |
Jack Jansen | cfadbd4 | 1996-08-19 11:36:25 +0000 | [diff] [blame] | 191 | void SpinCursor(short x) { /* Dummy */ } |
Jack Jansen | efaada7 | 1998-02-20 16:03:15 +0000 | [diff] [blame] | 192 | void RotateCursor(short x) { /* Dummy */ } |
Jack Jansen | cfadbd4 | 1996-08-19 11:36:25 +0000 | [diff] [blame] | 193 | |
Jack Jansen | f6865f7 | 1996-09-04 15:24:59 +0000 | [diff] [blame] | 194 | /* |
| 195 | ** Replacement GUSI Spin function |
| 196 | */ |
| 197 | static int |
| 198 | PyMac_GUSISpin(spin_msg msg, long arg) |
| 199 | { |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 200 | static Boolean inForeground = true; |
| 201 | int maxsleep = 6; /* 6 ticks is "normal" sleeptime */ |
Jack Jansen | f6865f7 | 1996-09-04 15:24:59 +0000 | [diff] [blame] | 202 | |
| 203 | if (PyMac_ConsoleIsDead) return 0; |
| 204 | #if 0 |
| 205 | if (inForeground) |
| 206 | SpinCursor(msg == SP_AUTO_SPIN ? short(arg) : 1); |
| 207 | #endif |
| 208 | |
| 209 | if (interrupted) return -1; |
| 210 | |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 211 | if ( msg == SP_AUTO_SPIN ) |
| 212 | maxsleep = 0; |
| 213 | if ( msg==SP_SLEEP||msg==SP_SELECT ) |
| 214 | maxsleep = arg; |
Jack Jansen | f6865f7 | 1996-09-04 15:24:59 +0000 | [diff] [blame] | 215 | |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 216 | PyMac_DoYield(maxsleep, 0); /* XXXX or is it safe to call python here? */ |
Jack Jansen | f6865f7 | 1996-09-04 15:24:59 +0000 | [diff] [blame] | 217 | |
| 218 | return 0; |
| 219 | } |
| 220 | |
| 221 | void |
| 222 | PyMac_SetGUSISpin() { |
| 223 | GUSISetHook(GUSI_SpinHook, (GUSIHook)PyMac_GUSISpin); |
| 224 | } |
| 225 | |
Jack Jansen | a39f1b0 | 1997-05-23 15:35:14 +0000 | [diff] [blame] | 226 | /* Called at exit() time thru atexit(), to stop event processing */ |
| 227 | void |
| 228 | PyMac_StopGUSISpin() { |
| 229 | PyMac_ConsoleIsDead = 1; |
| 230 | } |
| 231 | |
| 232 | /* |
| 233 | ** Replacement routines for the PLstr... functions so we don't need |
| 234 | ** StdCLib. Moreover, that implementation is broken under cfm68k... |
| 235 | */ |
Jack Jansen | 8bb573e | 1999-12-12 21:37:14 +0000 | [diff] [blame] | 236 | pascal void |
Jack Jansen | a39f1b0 | 1997-05-23 15:35:14 +0000 | [diff] [blame] | 237 | PLstrcpy(to, fr) |
| 238 | unsigned char *to, *fr; |
| 239 | { |
| 240 | memcpy(to, fr, fr[0]+1); |
| 241 | } |
| 242 | |
Jack Jansen | 8bb573e | 1999-12-12 21:37:14 +0000 | [diff] [blame] | 243 | pascal int |
Jack Jansen | a39f1b0 | 1997-05-23 15:35:14 +0000 | [diff] [blame] | 244 | PLstrcmp(s1, s2) |
| 245 | unsigned char *s1, *s2; |
| 246 | { |
| 247 | int res; |
| 248 | int l = s1[0] < s2[0] ? s1[0] : s2[0]; |
| 249 | |
| 250 | res = memcmp(s1+1, s2+1, l); |
| 251 | if ( res != 0 ) |
| 252 | return res; |
| 253 | |
Jack Jansen | 7e1fb7c | 1998-07-31 09:36:30 +0000 | [diff] [blame] | 254 | if ( s1[0] < s2[0] ) |
Jack Jansen | a39f1b0 | 1997-05-23 15:35:14 +0000 | [diff] [blame] | 255 | return -1; |
Jack Jansen | 7e1fb7c | 1998-07-31 09:36:30 +0000 | [diff] [blame] | 256 | else if ( s1[0] > s2[0] ) |
Jack Jansen | a39f1b0 | 1997-05-23 15:35:14 +0000 | [diff] [blame] | 257 | return 1; |
| 258 | else |
| 259 | return 0; |
| 260 | } |
| 261 | |
Jack Jansen | 8bb573e | 1999-12-12 21:37:14 +0000 | [diff] [blame] | 262 | pascal unsigned char * |
Jack Jansen | a39f1b0 | 1997-05-23 15:35:14 +0000 | [diff] [blame] | 263 | PLstrrchr(str, chr) |
| 264 | unsigned char *str; |
| 265 | unsigned char chr; |
| 266 | { |
| 267 | unsigned char *ptr = 0; |
| 268 | unsigned char *p; |
| 269 | |
| 270 | for(p=str+1; p<str+str[0]; p++) |
| 271 | if ( *p == chr ) |
| 272 | ptr = p; |
| 273 | return ptr; |
| 274 | } |
| 275 | |
| 276 | #endif /* USE_GUSI */ |
Jack Jansen | 378815c | 1996-03-06 16:21:34 +0000 | [diff] [blame] | 277 | |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 278 | |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 279 | /* Convert C to Pascal string. Returns pointer to static buffer. */ |
| 280 | unsigned char * |
| 281 | Pstring(char *str) |
| 282 | { |
| 283 | static Str255 buf; |
| 284 | int len; |
| 285 | |
| 286 | len = strlen(str); |
Guido van Rossum | 9aa3d13 | 1995-01-21 13:46:04 +0000 | [diff] [blame] | 287 | if (len > 255) |
| 288 | len = 255; |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 289 | buf[0] = (unsigned char)len; |
| 290 | strncpy((char *)buf+1, str, len); |
| 291 | return buf; |
| 292 | } |
| 293 | |
Guido van Rossum | c3d1c8e | 1995-02-18 15:01:31 +0000 | [diff] [blame] | 294 | /* Like strerror() but for Mac OS error numbers */ |
| 295 | char *PyMac_StrError(int err) |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 296 | { |
| 297 | static char buf[256]; |
| 298 | Handle h; |
| 299 | char *str; |
| 300 | |
| 301 | h = GetResource('Estr', err); |
| 302 | if ( h ) { |
| 303 | HLock(h); |
| 304 | str = (char *)*h; |
| 305 | memcpy(buf, str+1, (unsigned char)str[0]); |
Guido van Rossum | cc9bc8f | 1995-02-13 16:17:03 +0000 | [diff] [blame] | 306 | buf[(unsigned char)str[0]] = '\0'; |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 307 | HUnlock(h); |
| 308 | ReleaseResource(h); |
| 309 | } else { |
| 310 | sprintf(buf, "Mac OS error code %d", err); |
| 311 | } |
| 312 | return buf; |
| 313 | } |
| 314 | |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 315 | /* Exception object shared by all Mac specific modules for Mac OS errors */ |
| 316 | PyObject *PyMac_OSErrException; |
| 317 | |
| 318 | /* Initialize and return PyMac_OSErrException */ |
| 319 | PyObject * |
| 320 | PyMac_GetOSErrException() |
| 321 | { |
| 322 | if (PyMac_OSErrException == NULL) |
| 323 | PyMac_OSErrException = PyString_FromString("Mac OS Error"); |
| 324 | return PyMac_OSErrException; |
| 325 | } |
| 326 | |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 327 | /* Set a MAC-specific error from errno, and return NULL; return None if no error */ |
| 328 | PyObject * |
Guido van Rossum | fffb8bb | 1995-01-12 12:37:24 +0000 | [diff] [blame] | 329 | PyErr_Mac(PyObject *eobj, int err) |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 330 | { |
| 331 | char *msg; |
| 332 | PyObject *v; |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 333 | |
Guido van Rossum | 8f69179 | 1995-01-18 23:57:26 +0000 | [diff] [blame] | 334 | if (err == 0 && !PyErr_Occurred()) { |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 335 | Py_INCREF(Py_None); |
| 336 | return Py_None; |
| 337 | } |
Guido van Rossum | 8f69179 | 1995-01-18 23:57:26 +0000 | [diff] [blame] | 338 | if (err == -1 && PyErr_Occurred()) |
| 339 | return NULL; |
Guido van Rossum | c3d1c8e | 1995-02-18 15:01:31 +0000 | [diff] [blame] | 340 | msg = PyMac_StrError(err); |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 341 | v = Py_BuildValue("(is)", err, msg); |
| 342 | PyErr_SetObject(eobj, v); |
| 343 | Py_DECREF(v); |
| 344 | return NULL; |
| 345 | } |
| 346 | |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 347 | /* Call PyErr_Mac with PyMac_OSErrException */ |
| 348 | PyObject * |
| 349 | PyMac_Error(OSErr err) |
| 350 | { |
| 351 | return PyErr_Mac(PyMac_GetOSErrException(), err); |
| 352 | } |
| 353 | |
Jack Jansen | 1ed9529 | 1996-07-22 15:25:10 +0000 | [diff] [blame] | 354 | #ifdef USE_STACKCHECK |
| 355 | /* Check for stack overflow */ |
| 356 | int |
| 357 | PyOS_CheckStack() |
| 358 | { |
| 359 | long left; |
| 360 | |
| 361 | left = StackSpace(); |
Jack Jansen | d1f0631 | 1996-08-01 15:23:54 +0000 | [diff] [blame] | 362 | if ( left < MINIMUM_STACK_SIZE ) |
Jack Jansen | 1ed9529 | 1996-07-22 15:25:10 +0000 | [diff] [blame] | 363 | return -1; |
| 364 | return 0; |
| 365 | } |
| 366 | #endif /* USE_STACKCHECK */ |
| 367 | |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 368 | /* The catcher routine (which may not be used for all compilers) */ |
| 369 | static RETSIGTYPE |
| 370 | intcatcher(sig) |
| 371 | int sig; |
| 372 | { |
| 373 | interrupted = 1; |
| 374 | signal(SIGINT, intcatcher); |
| 375 | } |
| 376 | |
| 377 | void |
| 378 | PyOS_InitInterrupts() |
| 379 | { |
| 380 | if (signal(SIGINT, SIG_IGN) != SIG_IGN) |
| 381 | signal(SIGINT, intcatcher); |
| 382 | } |
| 383 | |
Jack Jansen | a8441de | 1997-08-08 14:57:37 +0000 | [diff] [blame] | 384 | void |
| 385 | PyOS_FiniInterrupts() |
| 386 | { |
| 387 | } |
| 388 | |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 389 | /* |
| 390 | ** This routine scans the event queue looking for cmd-. |
| 391 | ** This is the only way to get an interrupt under THINK (since it |
| 392 | ** doesn't do SIGINT handling), but is also used under MW, when |
| 393 | ** the full-fledged event loop is disabled. This way, we can at least |
| 394 | ** interrupt a runaway python program. |
| 395 | */ |
| 396 | static void |
| 397 | scan_event_queue(flush) |
| 398 | int flush; |
| 399 | { |
| 400 | register EvQElPtr q; |
| 401 | |
Jack Jansen | efaada7 | 1998-02-20 16:03:15 +0000 | [diff] [blame] | 402 | q = (EvQElPtr) LMGetEventQueue()->qHead; |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 403 | |
| 404 | for (; q; q = (EvQElPtr)q->qLink) { |
| 405 | if (q->evtQWhat == keyDown && |
| 406 | (char)q->evtQMessage == '.' && |
| 407 | (q->evtQModifiers & cmdKey) != 0) { |
| 408 | if ( flush ) |
| 409 | FlushEvents(keyDownMask, 0); |
| 410 | interrupted = 1; |
| 411 | break; |
| 412 | } |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | int |
Jack Jansen | 36ed506 | 1997-06-20 16:18:15 +0000 | [diff] [blame] | 417 | PyErr_CheckSignals() |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 418 | { |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 419 | if (schedparams.enabled) { |
| 420 | if ( (unsigned long)LMGetTicks() > schedparams.next_check ) { |
Jack Jansen | 36ed506 | 1997-06-20 16:18:15 +0000 | [diff] [blame] | 421 | if ( PyMac_Yield() < 0) |
| 422 | return -1; |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 423 | schedparams.next_check = (unsigned long)LMGetTicks() |
| 424 | + schedparams.check_interval; |
| 425 | if (interrupted) { |
Jack Jansen | caa7c46 | 1997-06-12 10:49:13 +0000 | [diff] [blame] | 426 | scan_event_queue(1); /* Eat events up to cmd-. */ |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 427 | interrupted = 0; |
Jack Jansen | 36ed506 | 1997-06-20 16:18:15 +0000 | [diff] [blame] | 428 | PyErr_SetNone(PyExc_KeyboardInterrupt); |
| 429 | return -1; |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 430 | } |
| 431 | } |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 432 | } |
| 433 | return 0; |
| 434 | } |
| 435 | |
Jack Jansen | 36ed506 | 1997-06-20 16:18:15 +0000 | [diff] [blame] | 436 | int |
| 437 | PyOS_InterruptOccurred() |
| 438 | { |
| 439 | scan_event_queue(1); |
| 440 | return interrupted; |
| 441 | } |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 442 | /* Check whether we are in the foreground */ |
| 443 | int |
| 444 | PyMac_InForeground() |
| 445 | { |
| 446 | static ProcessSerialNumber ours; |
| 447 | static inited; |
| 448 | ProcessSerialNumber curfg; |
| 449 | Boolean eq; |
| 450 | |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 451 | if ( inited == 0 ) { |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 452 | (void)GetCurrentProcess(&ours); |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 453 | inited = 1; |
| 454 | } |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 455 | if ( GetFrontProcess(&curfg) < 0 ) |
| 456 | eq = 1; |
| 457 | else if ( SameProcess(&ours, &curfg, &eq) < 0 ) |
| 458 | eq = 1; |
| 459 | return (int)eq; |
| 460 | |
| 461 | } |
| 462 | |
Jack Jansen | 36ed506 | 1997-06-20 16:18:15 +0000 | [diff] [blame] | 463 | int |
| 464 | PyMac_SetEventHandler(PyObject *evh) |
| 465 | { |
| 466 | if ( evh && python_event_handler ) { |
| 467 | PyErr_SetString(PyExc_RuntimeError, "Python event handler already set"); |
| 468 | return 0; |
| 469 | } |
| 470 | if ( python_event_handler ) |
| 471 | Py_DECREF(python_event_handler); |
| 472 | if ( evh ) |
| 473 | Py_INCREF(evh); |
| 474 | python_event_handler = evh; |
| 475 | return 1; |
| 476 | } |
| 477 | |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 478 | /* |
Jack Jansen | a76382a | 1995-02-02 14:25:56 +0000 | [diff] [blame] | 479 | ** Handle an event, either one found in the mainloop eventhandler or |
| 480 | ** one passed back from the python program. |
| 481 | */ |
| 482 | void |
Jack Jansen | 36ed506 | 1997-06-20 16:18:15 +0000 | [diff] [blame] | 483 | PyMac_HandleEventIntern(evp) |
Jack Jansen | a76382a | 1995-02-02 14:25:56 +0000 | [diff] [blame] | 484 | EventRecord *evp; |
| 485 | { |
Jack Jansen | 0c96887 | 1997-08-26 13:20:34 +0000 | [diff] [blame] | 486 | if ( evp->what == mouseDown ) { |
| 487 | WindowPtr wp; |
| 488 | |
| 489 | if ( FindWindow(evp->where, &wp) == inSysWindow ) { |
| 490 | SystemClick(evp, wp); |
| 491 | return; |
| 492 | } |
| 493 | } |
Jack Jansen | a76382a | 1995-02-02 14:25:56 +0000 | [diff] [blame] | 494 | #ifdef __MWERKS__ |
Jack Jansen | 38e9766 | 1995-11-10 14:53:00 +0000 | [diff] [blame] | 495 | { |
| 496 | int siouxdidit; |
| 497 | |
| 498 | /* If SIOUX wants it we're done */ |
| 499 | siouxdidit = SIOUXHandleOneEvent(evp); |
| 500 | if ( siouxdidit ) |
| 501 | return; |
| 502 | } |
Jack Jansen | a76382a | 1995-02-02 14:25:56 +0000 | [diff] [blame] | 503 | #else |
Jack Jansen | 0c96887 | 1997-08-26 13:20:34 +0000 | [diff] [blame] | 504 | /* Other compilers are just unlucky... */ |
Jack Jansen | a76382a | 1995-02-02 14:25:56 +0000 | [diff] [blame] | 505 | #endif /* !__MWERKS__ */ |
Jack Jansen | 36ed506 | 1997-06-20 16:18:15 +0000 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | /* |
| 509 | ** Handle an event, either through HandleEvent or by passing it to the Python |
| 510 | ** event handler. |
| 511 | */ |
| 512 | int |
| 513 | PyMac_HandleEvent(evp) |
| 514 | EventRecord *evp; |
| 515 | { |
| 516 | PyObject *rv; |
| 517 | |
| 518 | if ( python_event_handler ) { |
| 519 | rv = PyObject_CallFunction(python_event_handler, "(O&)", |
| 520 | PyMac_BuildEventRecord, evp); |
| 521 | if ( rv ) |
| 522 | Py_DECREF(rv); |
| 523 | else |
| 524 | return -1; /* Propagate exception */ |
| 525 | } else { |
| 526 | PyMac_HandleEventIntern(evp); |
| 527 | } |
| 528 | return 0; |
Jack Jansen | a76382a | 1995-02-02 14:25:56 +0000 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | /* |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 532 | ** Yield the CPU to other tasks without processing events. |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 533 | */ |
Jack Jansen | 36ed506 | 1997-06-20 16:18:15 +0000 | [diff] [blame] | 534 | static int |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 535 | PyMac_DoYield(int maxsleep, int maycallpython) |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 536 | { |
| 537 | EventRecord ev; |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 538 | int gotone; |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 539 | long latest_time_ready; |
Jack Jansen | 36ed506 | 1997-06-20 16:18:15 +0000 | [diff] [blame] | 540 | static int in_here = 0; |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 541 | |
Jack Jansen | 36ed506 | 1997-06-20 16:18:15 +0000 | [diff] [blame] | 542 | in_here++; |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 543 | /* |
| 544 | ** First check for interrupts, if wanted. |
| 545 | ** This sets a flag that will be picked up at an appropriate |
| 546 | ** moment in the mainloop. |
| 547 | */ |
| 548 | if (schedparams.check_interrupt) |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 549 | scan_event_queue(0); |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 550 | |
| 551 | /* XXXX Implementing an idle routine goes here */ |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 552 | |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 553 | /* |
| 554 | ** Check which of the eventloop cases we have: |
| 555 | ** - process events |
| 556 | ** - don't process events but do yield |
| 557 | ** - do neither |
| 558 | */ |
Jack Jansen | 36ed506 | 1997-06-20 16:18:15 +0000 | [diff] [blame] | 559 | if( in_here > 1 || !schedparams.process_events || |
| 560 | (python_event_handler && !maycallpython) ) { |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 561 | if ( maxsleep >= 0 ) { |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 562 | SystemTask(); |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 563 | } |
| 564 | } else { |
| 565 | latest_time_ready = LMGetTicks() + maxsleep; |
| 566 | while ( maxsleep >= 0 ) { |
Jack Jansen | 0c96887 | 1997-08-26 13:20:34 +0000 | [diff] [blame] | 567 | /* XXXX Hack by Jack. |
| 568 | ** In time.sleep() you can click to another application |
| 569 | ** once only. If you come back to Python you cannot get away |
| 570 | ** again. |
| 571 | **/ |
Jack Jansen | 36ed506 | 1997-06-20 16:18:15 +0000 | [diff] [blame] | 572 | gotone = WaitNextEvent(schedparams.process_events, &ev, maxsleep, NULL); |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 573 | /* Get out quickly if nothing interesting is happening */ |
| 574 | if ( !gotone || ev.what == nullEvent ) |
| 575 | break; |
Jack Jansen | 36ed506 | 1997-06-20 16:18:15 +0000 | [diff] [blame] | 576 | if ( PyMac_HandleEvent(&ev) < 0 ) { |
| 577 | in_here--; |
| 578 | return -1; |
| 579 | } |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 580 | maxsleep = latest_time_ready - LMGetTicks(); |
| 581 | } |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 582 | } |
Jack Jansen | 36ed506 | 1997-06-20 16:18:15 +0000 | [diff] [blame] | 583 | in_here--; |
| 584 | return 0; |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | /* |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 588 | ** Process events and/or yield the CPU to other tasks if opportune |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 589 | */ |
Jack Jansen | 36ed506 | 1997-06-20 16:18:15 +0000 | [diff] [blame] | 590 | int |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 591 | PyMac_Yield() { |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 592 | unsigned long maxsleep; |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 593 | |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 594 | if( PyMac_InForeground() ) |
| 595 | maxsleep = 0; |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 596 | else |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 597 | maxsleep = schedparams.bg_yield; |
| 598 | |
Jack Jansen | 36ed506 | 1997-06-20 16:18:15 +0000 | [diff] [blame] | 599 | return PyMac_DoYield(maxsleep, 1); |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 600 | } |
| 601 | |
| 602 | /* |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 603 | ** Return current scheduler parameters |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 604 | */ |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 605 | void |
| 606 | PyMac_GetSchedParams(PyMacSchedParams *sp) |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 607 | { |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 608 | sp->check_interrupt = schedparams.check_interrupt; |
| 609 | sp->process_events = schedparams.process_events; |
| 610 | sp->besocial = schedparams.besocial; |
| 611 | sp->check_interval = schedparams.check_interval / 60.0; |
| 612 | sp->bg_yield = schedparams.bg_yield / 60.0; |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 613 | } |
Jack Jansen | f6865f7 | 1996-09-04 15:24:59 +0000 | [diff] [blame] | 614 | |
Jack Jansen | 74162f3 | 1995-02-15 22:58:33 +0000 | [diff] [blame] | 615 | /* |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 616 | ** Set current scheduler parameters |
| 617 | */ |
| 618 | void |
| 619 | PyMac_SetSchedParams(PyMacSchedParams *sp) |
| 620 | { |
| 621 | schedparams.check_interrupt = sp->check_interrupt; |
| 622 | schedparams.process_events = sp->process_events; |
| 623 | schedparams.besocial = sp->besocial; |
| 624 | schedparams.check_interval = (unsigned long)(sp->check_interval*60); |
| 625 | schedparams.bg_yield = (unsigned long)(sp->bg_yield*60); |
| 626 | if ( schedparams.check_interrupt || schedparams.process_events || |
| 627 | schedparams.besocial ) |
| 628 | schedparams.enabled = 1; |
| 629 | else |
| 630 | schedparams.enabled = 0; |
| 631 | schedparams.next_check = 0; /* Check immedeately */ |
| 632 | } |
Jack Jansen | caa7c46 | 1997-06-12 10:49:13 +0000 | [diff] [blame] | 633 | |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 634 | /* |
Jack Jansen | 3469e99 | 1996-09-06 00:30:45 +0000 | [diff] [blame] | 635 | ** Install our menu bar. |
| 636 | */ |
| 637 | void |
| 638 | PyMac_InitMenuBar() |
| 639 | { |
Jack Jansen | 3469e99 | 1996-09-06 00:30:45 +0000 | [diff] [blame] | 640 | MenuHandle applemenu; |
| 641 | |
Jack Jansen | caa7c46 | 1997-06-12 10:49:13 +0000 | [diff] [blame] | 642 | if ( (sioux_mbar=GetMenuBar()) == NULL ) { |
| 643 | /* Sioux menu not installed yet. Do so */ |
| 644 | SIOUXSetupMenus(); |
| 645 | if ( (sioux_mbar=GetMenuBar()) == NULL ) |
| 646 | return; |
| 647 | } |
Jack Jansen | 08c3be3 | 1997-04-08 15:27:00 +0000 | [diff] [blame] | 648 | if ( (applemenu=GetMenuHandle(SIOUX_APPLEID)) == NULL ) return; |
Jack Jansen | 3469e99 | 1996-09-06 00:30:45 +0000 | [diff] [blame] | 649 | SetMenuItemText(applemenu, 1, "\pAbout Python..."); |
| 650 | } |
| 651 | |
| 652 | /* |
Jack Jansen | caa7c46 | 1997-06-12 10:49:13 +0000 | [diff] [blame] | 653 | ** Restore sioux menu bar |
| 654 | */ |
| 655 | void |
| 656 | PyMac_RestoreMenuBar() |
| 657 | { |
Jack Jansen | eda7863 | 1997-06-12 15:29:46 +0000 | [diff] [blame] | 658 | if ( sioux_mbar ) { |
Jack Jansen | caa7c46 | 1997-06-12 10:49:13 +0000 | [diff] [blame] | 659 | SetMenuBar(sioux_mbar); |
Jack Jansen | eda7863 | 1997-06-12 15:29:46 +0000 | [diff] [blame] | 660 | DrawMenuBar(); |
Jack Jansen | efaada7 | 1998-02-20 16:03:15 +0000 | [diff] [blame] | 661 | } else |
| 662 | PyMac_InitMenuBar(); |
Jack Jansen | caa7c46 | 1997-06-12 10:49:13 +0000 | [diff] [blame] | 663 | } |
| 664 | |
| 665 | |
| 666 | /* |
Jack Jansen | 3469e99 | 1996-09-06 00:30:45 +0000 | [diff] [blame] | 667 | ** Our replacement about box |
| 668 | */ |
Jack Jansen | 7e1fb7c | 1998-07-31 09:36:30 +0000 | [diff] [blame] | 669 | |
| 670 | #include "patchlevel.h" |
| 671 | |
Jack Jansen | 3469e99 | 1996-09-06 00:30:45 +0000 | [diff] [blame] | 672 | void |
| 673 | SIOUXDoAboutBox(void) |
| 674 | { |
| 675 | DialogPtr theDialog; |
Jack Jansen | 7e1fb7c | 1998-07-31 09:36:30 +0000 | [diff] [blame] | 676 | WindowPtr theWindow; |
Jack Jansen | 3469e99 | 1996-09-06 00:30:45 +0000 | [diff] [blame] | 677 | short item; |
Jack Jansen | 7e1fb7c | 1998-07-31 09:36:30 +0000 | [diff] [blame] | 678 | short fontID; |
Jack Jansen | 3469e99 | 1996-09-06 00:30:45 +0000 | [diff] [blame] | 679 | |
| 680 | if( (theDialog = GetNewDialog(ABOUT_ID, NULL, (WindowPtr)-1)) == NULL ) |
| 681 | return; |
Jack Jansen | d617c57 | 1996-09-22 22:14:30 +0000 | [diff] [blame] | 682 | theWindow = GetDialogWindow(theDialog); |
Jack Jansen | 7e1fb7c | 1998-07-31 09:36:30 +0000 | [diff] [blame] | 683 | SetPortWindowPort(theWindow); |
| 684 | GetFNum("\pPython-Sans", &fontID); |
| 685 | if (fontID == 0) |
| 686 | fontID = kFontIDGeneva; |
| 687 | TextFont(fontID); |
| 688 | TextSize(9); |
| 689 | ParamText(Pstring(PATCHLEVEL), "\p", "\p", "\p"); |
Jack Jansen | d617c57 | 1996-09-22 22:14:30 +0000 | [diff] [blame] | 690 | ShowWindow(theWindow); |
Jack Jansen | 3469e99 | 1996-09-06 00:30:45 +0000 | [diff] [blame] | 691 | ModalDialog(NULL, &item); |
| 692 | DisposeDialog(theDialog); |
| 693 | } |
| 694 | |
Jack Jansen | eda7863 | 1997-06-12 15:29:46 +0000 | [diff] [blame] | 695 | #if 0 |
| 696 | int |
| 697 | PyMac_FileExists(char *name) |
| 698 | { |
| 699 | FSSpec fss; |
| 700 | |
| 701 | if ( FSMakeFSSpec(0, 0, Pstring(name), &fss) == noErr ) |
| 702 | return 1; |
| 703 | return 0; |
| 704 | } |
| 705 | #endif |
| 706 | |
| 707 | /* |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 708 | ** Helper routine for GetDirectory |
| 709 | */ |
Guido van Rossum | 24a45e3 | 1995-02-20 23:45:53 +0000 | [diff] [blame] | 710 | static pascal short |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 711 | myhook_proc(short item, DialogPtr theDialog, struct hook_args *dataptr) |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 712 | { |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 713 | if ( item == sfHookFirstCall && dataptr->prompt) { |
| 714 | Handle prompth; |
| 715 | short type; |
| 716 | Rect rect; |
| 717 | |
| 718 | GetDialogItem(theDialog, PROMPT_ITEM, &type, &prompth, &rect); |
| 719 | if ( prompth ) |
| 720 | SetDialogItemText(prompth, (unsigned char *)dataptr->prompt); |
| 721 | } else |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 722 | if ( item == SELECTCUR_ITEM ) { |
| 723 | item = sfItemCancelButton; |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 724 | dataptr->selectcur_hit = 1; |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 725 | } |
| 726 | return item; |
| 727 | } |
| 728 | |
| 729 | /* |
| 730 | ** Ask the user for a directory. I still can't understand |
| 731 | ** why Apple doesn't provide a standard solution for this... |
| 732 | */ |
| 733 | int |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 734 | PyMac_GetDirectory(dirfss, prompt) |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 735 | FSSpec *dirfss; |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 736 | char *prompt; |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 737 | { |
| 738 | static SFTypeList list = {'fldr', 0, 0, 0}; |
| 739 | static Point where = {-1, -1}; |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 740 | StandardFileReply reply; |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 741 | struct hook_args hook_args; |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 742 | |
| 743 | if ( !upp_inited ) { |
| 744 | myhook_upp = NewDlgHookYDProc(myhook_proc); |
| 745 | upp_inited = 1; |
| 746 | } |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 747 | if ( prompt && *prompt ) |
| 748 | hook_args.prompt = (char *)Pstring(prompt); |
| 749 | else |
| 750 | hook_args.prompt = NULL; |
| 751 | hook_args.selectcur_hit = 0; |
Guido van Rossum | 24a45e3 | 1995-02-20 23:45:53 +0000 | [diff] [blame] | 752 | CustomGetFile((FileFilterYDUPP)0, 1, list, &reply, GETDIR_ID, where, myhook_upp, |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 753 | NULL, NULL, NULL, (void *)&hook_args); |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 754 | |
| 755 | reply.sfFile.name[0] = 0; |
| 756 | if( FSMakeFSSpec(reply.sfFile.vRefNum, reply.sfFile.parID, reply.sfFile.name, dirfss) ) |
| 757 | return 0; |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 758 | return hook_args.selectcur_hit; |
| 759 | } |
| 760 | |
| 761 | /* |
| 762 | ** Slightly extended StandardGetFile: accepts a prompt */ |
| 763 | void PyMac_PromptGetFile(short numTypes, ConstSFTypeListPtr typeList, |
| 764 | StandardFileReply *reply, char *prompt) |
| 765 | { |
| 766 | static Point where = {-1, -1}; |
| 767 | struct hook_args hook_args; |
| 768 | |
| 769 | if ( !upp_inited ) { |
| 770 | myhook_upp = NewDlgHookYDProc(myhook_proc); |
| 771 | upp_inited = 1; |
| 772 | } |
| 773 | if ( prompt && *prompt ) |
| 774 | hook_args.prompt = (char *)Pstring(prompt); |
| 775 | else |
| 776 | hook_args.prompt = NULL; |
| 777 | hook_args.selectcur_hit = 0; |
| 778 | CustomGetFile((FileFilterYDUPP)0, numTypes, typeList, reply, GETFILEPROMPT_ID, where, |
| 779 | myhook_upp, NULL, NULL, NULL, (void *)&hook_args); |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 780 | } |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 781 | |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 782 | /* Convert a 4-char string object argument to an OSType value */ |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 783 | int |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 784 | PyMac_GetOSType(PyObject *v, OSType *pr) |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 785 | { |
| 786 | if (!PyString_Check(v) || PyString_Size(v) != 4) { |
| 787 | PyErr_SetString(PyExc_TypeError, |
| 788 | "OSType arg must be string of 4 chars"); |
| 789 | return 0; |
| 790 | } |
| 791 | memcpy((char *)pr, PyString_AsString(v), 4); |
| 792 | return 1; |
| 793 | } |
| 794 | |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 795 | /* Convert an OSType value to a 4-char string object */ |
| 796 | PyObject * |
| 797 | PyMac_BuildOSType(OSType t) |
| 798 | { |
| 799 | return PyString_FromStringAndSize((char *)&t, 4); |
| 800 | } |
| 801 | |
Jack Jansen | d1f0631 | 1996-08-01 15:23:54 +0000 | [diff] [blame] | 802 | /* Convert an NumVersion value to a 4-element tuple */ |
| 803 | PyObject * |
| 804 | PyMac_BuildNumVersion(NumVersion t) |
| 805 | { |
| 806 | return Py_BuildValue("(hhhh)", t.majorRev, t.minorAndBugRev, t.stage, t.nonRelRev); |
| 807 | } |
| 808 | |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 809 | |
| 810 | /* Convert a Python string object to a Str255 */ |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 811 | int |
Guido van Rossum | 8f69179 | 1995-01-18 23:57:26 +0000 | [diff] [blame] | 812 | PyMac_GetStr255(PyObject *v, Str255 pbuf) |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 813 | { |
| 814 | int len; |
| 815 | if (!PyString_Check(v) || (len = PyString_Size(v)) > 255) { |
| 816 | PyErr_SetString(PyExc_TypeError, |
| 817 | "Str255 arg must be string of at most 255 chars"); |
| 818 | return 0; |
| 819 | } |
| 820 | pbuf[0] = len; |
| 821 | memcpy((char *)(pbuf+1), PyString_AsString(v), len); |
| 822 | return 1; |
| 823 | } |
| 824 | |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 825 | /* Convert a Str255 to a Python string object */ |
| 826 | PyObject * |
| 827 | PyMac_BuildStr255(Str255 s) |
| 828 | { |
Jack Jansen | b734ade | 1999-12-17 17:15:50 +0000 | [diff] [blame] | 829 | if ( s == NULL ) { |
| 830 | PyErr_SetString(PyExc_SystemError, "Str255 pointer is NULL"); |
| 831 | return NULL; |
| 832 | } |
| 833 | return PyString_FromStringAndSize((char *)&s[1], (int)s[0]); |
| 834 | } |
| 835 | |
| 836 | PyObject * |
| 837 | PyMac_BuildOptStr255(Str255 s) |
| 838 | { |
| 839 | if ( s == NULL ) { |
| 840 | Py_INCREF(Py_None); |
| 841 | return Py_None; |
| 842 | } |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 843 | return PyString_FromStringAndSize((char *)&s[1], (int)s[0]); |
| 844 | } |
| 845 | |
| 846 | |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 847 | /* |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 848 | ** Convert a Python object to an FSSpec. |
| 849 | ** The object may either be a full pathname or a triple |
| 850 | ** (vrefnum, dirid, path). |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 851 | ** NOTE: This routine will fail on pre-sys7 machines. |
| 852 | ** The caller is responsible for not calling this routine |
| 853 | ** in those cases (which is fine, since everyone calling |
| 854 | ** this is probably sys7 dependent anyway). |
| 855 | */ |
| 856 | int |
Guido van Rossum | 8f69179 | 1995-01-18 23:57:26 +0000 | [diff] [blame] | 857 | PyMac_GetFSSpec(PyObject *v, FSSpec *fs) |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 858 | { |
| 859 | Str255 path; |
| 860 | short refnum; |
| 861 | long parid; |
| 862 | OSErr err; |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 863 | FSSpec *fs2; |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 864 | |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 865 | /* first check whether it already is an FSSpec */ |
| 866 | fs2 = mfs_GetFSSpecFSSpec(v); |
| 867 | if ( fs2 ) { |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 868 | (void)FSMakeFSSpec(fs2->vRefNum, fs2->parID, fs2->name, fs); |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 869 | return 1; |
| 870 | } |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 871 | if ( PyString_Check(v) ) { |
| 872 | /* It's a pathname */ |
Guido van Rossum | 9aa3d13 | 1995-01-21 13:46:04 +0000 | [diff] [blame] | 873 | if( !PyArg_Parse(v, "O&", PyMac_GetStr255, &path) ) |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 874 | return 0; |
Jack Jansen | 378815c | 1996-03-06 16:21:34 +0000 | [diff] [blame] | 875 | refnum = 0; /* XXXX Should get CurWD here?? */ |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 876 | parid = 0; |
| 877 | } else { |
Guido van Rossum | 9aa3d13 | 1995-01-21 13:46:04 +0000 | [diff] [blame] | 878 | if( !PyArg_Parse(v, "(hlO&); FSSpec should be fullpath or (vrefnum,dirid,path)", |
| 879 | &refnum, &parid, PyMac_GetStr255, &path)) { |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 880 | return 0; |
Guido van Rossum | 9aa3d13 | 1995-01-21 13:46:04 +0000 | [diff] [blame] | 881 | } |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 882 | } |
| 883 | err = FSMakeFSSpec(refnum, parid, path, fs); |
| 884 | if ( err && err != fnfErr ) { |
Guido van Rossum | 9aa3d13 | 1995-01-21 13:46:04 +0000 | [diff] [blame] | 885 | PyErr_Mac(PyExc_ValueError, err); |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 886 | return 0; |
| 887 | } |
| 888 | return 1; |
| 889 | } |
| 890 | |
Jack Jansen | 8bb573e | 1999-12-12 21:37:14 +0000 | [diff] [blame] | 891 | /* Convert FSSpec to PyObject */ |
| 892 | PyObject *PyMac_BuildFSSpec(FSSpec *v) |
| 893 | { |
| 894 | return newmfssobject(v); |
| 895 | } |
Guido van Rossum | 8f69179 | 1995-01-18 23:57:26 +0000 | [diff] [blame] | 896 | |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 897 | /* Convert a Python object to a Rect. |
Guido van Rossum | 5279ec6 | 1995-01-26 22:56:59 +0000 | [diff] [blame] | 898 | The object must be a (left, top, right, bottom) tuple. |
| 899 | (This differs from the order in the struct but is consistent with |
| 900 | the arguments to SetRect(), and also with STDWIN). */ |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 901 | int |
| 902 | PyMac_GetRect(PyObject *v, Rect *r) |
Guido van Rossum | 8f69179 | 1995-01-18 23:57:26 +0000 | [diff] [blame] | 903 | { |
Guido van Rossum | 5279ec6 | 1995-01-26 22:56:59 +0000 | [diff] [blame] | 904 | return PyArg_Parse(v, "(hhhh)", &r->left, &r->top, &r->right, &r->bottom); |
Guido van Rossum | 8f69179 | 1995-01-18 23:57:26 +0000 | [diff] [blame] | 905 | } |
Guido van Rossum | b340466 | 1995-01-22 18:36:13 +0000 | [diff] [blame] | 906 | |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 907 | /* Convert a Rect to a Python object */ |
Guido van Rossum | b340466 | 1995-01-22 18:36:13 +0000 | [diff] [blame] | 908 | PyObject * |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 909 | PyMac_BuildRect(Rect *r) |
Guido van Rossum | b340466 | 1995-01-22 18:36:13 +0000 | [diff] [blame] | 910 | { |
Guido van Rossum | 5279ec6 | 1995-01-26 22:56:59 +0000 | [diff] [blame] | 911 | return Py_BuildValue("(hhhh)", r->left, r->top, r->right, r->bottom); |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 912 | } |
| 913 | |
| 914 | |
| 915 | /* Convert a Python object to a Point. |
Guido van Rossum | 5279ec6 | 1995-01-26 22:56:59 +0000 | [diff] [blame] | 916 | The object must be a (h, v) tuple. |
| 917 | (This differs from the order in the struct but is consistent with |
| 918 | the arguments to SetPoint(), and also with STDWIN). */ |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 919 | int |
| 920 | PyMac_GetPoint(PyObject *v, Point *p) |
| 921 | { |
Guido van Rossum | 5279ec6 | 1995-01-26 22:56:59 +0000 | [diff] [blame] | 922 | return PyArg_Parse(v, "(hh)", &p->h, &p->v); |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 923 | } |
| 924 | |
| 925 | /* Convert a Point to a Python object */ |
| 926 | PyObject * |
| 927 | PyMac_BuildPoint(Point p) |
| 928 | { |
Guido van Rossum | 5279ec6 | 1995-01-26 22:56:59 +0000 | [diff] [blame] | 929 | return Py_BuildValue("(hh)", p.h, p.v); |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 930 | } |
| 931 | |
| 932 | |
| 933 | /* Convert a Python object to an EventRecord. |
| 934 | The object must be a (what, message, when, (v, h), modifiers) tuple. */ |
| 935 | int |
| 936 | PyMac_GetEventRecord(PyObject *v, EventRecord *e) |
| 937 | { |
| 938 | return PyArg_Parse(v, "(hll(hh)h)", |
| 939 | &e->what, |
| 940 | &e->message, |
| 941 | &e->when, |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 942 | &e->where.h, |
Guido van Rossum | 5279ec6 | 1995-01-26 22:56:59 +0000 | [diff] [blame] | 943 | &e->where.v, |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 944 | &e->modifiers); |
| 945 | } |
| 946 | |
| 947 | /* Convert a Rect to an EventRecord object */ |
| 948 | PyObject * |
| 949 | PyMac_BuildEventRecord(EventRecord *e) |
| 950 | { |
| 951 | return Py_BuildValue("(hll(hh)h)", |
| 952 | e->what, |
| 953 | e->message, |
| 954 | e->when, |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 955 | e->where.h, |
Guido van Rossum | 5279ec6 | 1995-01-26 22:56:59 +0000 | [diff] [blame] | 956 | e->where.v, |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 957 | e->modifiers); |
Guido van Rossum | b340466 | 1995-01-22 18:36:13 +0000 | [diff] [blame] | 958 | } |
Jack Jansen | fa4d5d0 | 1995-11-15 15:19:29 +0000 | [diff] [blame] | 959 | |
| 960 | /* Convert Python object to Fixed */ |
| 961 | int |
| 962 | PyMac_GetFixed(PyObject *v, Fixed *f) |
| 963 | { |
| 964 | double d; |
| 965 | |
| 966 | if( !PyArg_Parse(v, "d", &d)) |
| 967 | return 0; |
| 968 | *f = (Fixed)(d * 0x10000); |
Jack Jansen | 31dd5c0 | 1996-05-31 13:01:39 +0000 | [diff] [blame] | 969 | return 1; |
Jack Jansen | fa4d5d0 | 1995-11-15 15:19:29 +0000 | [diff] [blame] | 970 | } |
| 971 | |
| 972 | /* Convert a Point to a Python object */ |
| 973 | PyObject * |
| 974 | PyMac_BuildFixed(Fixed f) |
| 975 | { |
| 976 | double d; |
| 977 | |
| 978 | d = f; |
| 979 | d = d / 0x10000; |
| 980 | return Py_BuildValue("d", d); |
| 981 | } |
| 982 | |
Jack Jansen | d58cd63 | 1998-04-21 15:24:39 +0000 | [diff] [blame] | 983 | /* Convert wide to/from Python int or (hi, lo) tuple. XXXX Should use Python longs */ |
| 984 | int |
| 985 | PyMac_Getwide(PyObject *v, wide *rv) |
| 986 | { |
| 987 | if (PyInt_Check(v)) { |
| 988 | rv->hi = 0; |
| 989 | rv->lo = PyInt_AsLong(v); |
| 990 | if( rv->lo & 0x80000000 ) |
| 991 | rv->hi = -1; |
| 992 | return 1; |
| 993 | } |
| 994 | return PyArg_Parse(v, "(ll)", &rv->hi, &rv->lo); |
| 995 | } |
| 996 | |
| 997 | |
| 998 | PyObject * |
Jack Jansen | bbf4be2 | 1998-04-23 13:20:17 +0000 | [diff] [blame] | 999 | PyMac_Buildwide(wide *w) |
Jack Jansen | d58cd63 | 1998-04-21 15:24:39 +0000 | [diff] [blame] | 1000 | { |
Jack Jansen | bbf4be2 | 1998-04-23 13:20:17 +0000 | [diff] [blame] | 1001 | if ( (w->hi == 0 && (w->lo & 0x80000000) == 0) || |
| 1002 | (w->hi == -1 && (w->lo & 0x80000000) ) ) |
| 1003 | return PyInt_FromLong(w->lo); |
| 1004 | return Py_BuildValue("(ll)", w->hi, w->lo); |
Jack Jansen | d58cd63 | 1998-04-21 15:24:39 +0000 | [diff] [blame] | 1005 | } |