Guido van Rossum | 845547d | 1996-06-26 18:26:04 +0000 | [diff] [blame] | 1 | /*********************************************************** |
| 2 | Copyright (C) 1994 Steen Lumholt. |
| 3 | Copyright 1994-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 4 | The Netherlands. |
| 5 | |
| 6 | All Rights Reserved |
| 7 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 8 | Permission to use, copy, modify, and distribute this software and its |
| 9 | documentation for any purpose and without fee is hereby granted, |
Guido van Rossum | 845547d | 1996-06-26 18:26:04 +0000 | [diff] [blame] | 10 | provided that the above copyright notice appear in all copies and that |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 11 | both that copyright notice and this permission notice appear in |
Guido van Rossum | 845547d | 1996-06-26 18:26:04 +0000 | [diff] [blame] | 12 | supporting documentation, and that the names of Stichting Mathematisch |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 13 | Centrum or CWI or Corporation for National Research Initiatives or |
| 14 | CNRI not be used in advertising or publicity pertaining to |
| 15 | distribution of the software without specific, written prior |
| 16 | permission. |
Guido van Rossum | 845547d | 1996-06-26 18:26:04 +0000 | [diff] [blame] | 17 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 18 | While CWI is the initial source for this software, a modified version |
| 19 | is made available by the Corporation for National Research Initiatives |
| 20 | (CNRI) at the Internet address ftp://ftp.python.org. |
| 21 | |
| 22 | STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH |
| 23 | REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF |
| 24 | MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH |
| 25 | CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL |
| 26 | DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
| 27 | PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 28 | TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 29 | PERFORMANCE OF THIS SOFTWARE. |
Guido van Rossum | 845547d | 1996-06-26 18:26:04 +0000 | [diff] [blame] | 30 | |
| 31 | ******************************************************************/ |
| 32 | |
| 33 | /* _tkinter.c -- Interface to libtk.a and libtcl.a. */ |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 34 | |
Guido van Rossum | 7ffa761 | 1996-08-13 21:10:16 +0000 | [diff] [blame] | 35 | /* TCL/TK VERSION INFO: |
| 36 | |
| 37 | Unix: |
Guido van Rossum | 496f8f6 | 1997-07-19 19:57:42 +0000 | [diff] [blame] | 38 | Tcl/Tk 8.0 (even alpha or beta) or 7.6/4.2 are recommended. |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 39 | Versions 7.5/4.1 are the earliest versions still supported. |
| 40 | Versions 7.4/4.0 or Tk 3.x are no longer supported. |
Guido van Rossum | 7ffa761 | 1996-08-13 21:10:16 +0000 | [diff] [blame] | 41 | |
| 42 | Mac and Windows: |
Guido van Rossum | 496f8f6 | 1997-07-19 19:57:42 +0000 | [diff] [blame] | 43 | Use Tcl 8.0 if available (even alpha or beta). |
| 44 | The oldest usable version is 4.1p1/7.5p1. |
| 45 | |
Guido van Rossum | 212643f | 1998-04-29 16:22:14 +0000 | [diff] [blame] | 46 | XXX Further speed-up ideas, involving Tcl 8.0 features: |
| 47 | |
| 48 | - In Tcl_Call(), create Tcl objects from the arguments, possibly using |
| 49 | intelligent mappings between Python objects and Tcl objects (e.g. ints, |
| 50 | floats and Tcl window pointers could be handled specially). |
| 51 | |
| 52 | - Register a new Tcl type, "Python callable", which can be called more |
| 53 | efficiently and passed to Tcl_EvalObj() directly (if this is possible). |
| 54 | |
Guido van Rossum | 7ffa761 | 1996-08-13 21:10:16 +0000 | [diff] [blame] | 55 | */ |
| 56 | |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 57 | |
Guido van Rossum | 9722ad8 | 1995-09-22 23:49:28 +0000 | [diff] [blame] | 58 | #include "Python.h" |
Guido van Rossum | 97867b2 | 1996-08-08 19:09:53 +0000 | [diff] [blame] | 59 | #include <ctype.h> |
Guido van Rossum | 9722ad8 | 1995-09-22 23:49:28 +0000 | [diff] [blame] | 60 | |
Guido van Rossum | bf0dc9f | 1996-08-20 20:49:56 +0000 | [diff] [blame] | 61 | #ifdef macintosh |
| 62 | #define MAC_TCL |
Guido van Rossum | 290283b | 1997-06-02 22:16:43 +0000 | [diff] [blame] | 63 | #include "myselect.h" |
Guido van Rossum | bf0dc9f | 1996-08-20 20:49:56 +0000 | [diff] [blame] | 64 | #endif |
| 65 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 66 | #include <tcl.h> |
| 67 | #include <tk.h> |
| 68 | |
Guido van Rossum | 3e819a7 | 1997-08-01 19:29:02 +0000 | [diff] [blame] | 69 | #define TKMAJORMINOR (TK_MAJOR_VERSION*1000 + TK_MINOR_VERSION) |
| 70 | |
Guido van Rossum | 0d2390c | 1997-08-14 19:57:07 +0000 | [diff] [blame] | 71 | #if TKMAJORMINOR >= 8000 && defined(macintosh) |
| 72 | /* Sigh, we have to include this to get at the tcl qd pointer */ |
| 73 | #include <tkMac.h> |
Guido van Rossum | 2ea1c94 | 1998-04-28 16:12:43 +0000 | [diff] [blame] | 74 | /* And this one we need to clear the menu bar */ |
| 75 | #include <Menus.h> |
Guido van Rossum | 0d2390c | 1997-08-14 19:57:07 +0000 | [diff] [blame] | 76 | #endif |
| 77 | |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 78 | #if TKMAJORMINOR < 4001 |
Guido van Rossum | 75626a3 | 1997-09-08 02:04:00 +0000 | [diff] [blame] | 79 | #error "Tk 4.0 or 3.x are not supported -- use 4.1 or higher" |
Guido van Rossum | 9722ad8 | 1995-09-22 23:49:28 +0000 | [diff] [blame] | 80 | #endif |
| 81 | |
Guido van Rossum | b010544 | 1997-10-08 15:25:37 +0000 | [diff] [blame] | 82 | #if TKMAJORMINOR < 8000 || !defined(MS_WINDOWS) |
Guido van Rossum | 0d2390c | 1997-08-14 19:57:07 +0000 | [diff] [blame] | 83 | #define HAVE_CREATEFILEHANDLER |
| 84 | #endif |
| 85 | |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 86 | extern int Tk_GetNumMainWindows(); |
| 87 | |
Guido van Rossum | ec22c92 | 1996-02-25 04:50:29 +0000 | [diff] [blame] | 88 | #ifdef macintosh |
| 89 | |
| 90 | /* |
| 91 | ** Additional cruft needed by Tcl/Tk on the Mac. |
Guido van Rossum | 97867b2 | 1996-08-08 19:09:53 +0000 | [diff] [blame] | 92 | ** This is for Tcl 7.5 and Tk 4.1 (patch release 1). |
Guido van Rossum | ec22c92 | 1996-02-25 04:50:29 +0000 | [diff] [blame] | 93 | */ |
| 94 | |
Guido van Rossum | 7ffa761 | 1996-08-13 21:10:16 +0000 | [diff] [blame] | 95 | /* ckfree() expects a char* */ |
Guido van Rossum | 97867b2 | 1996-08-08 19:09:53 +0000 | [diff] [blame] | 96 | #define FREECAST (char *) |
| 97 | |
Guido van Rossum | ec22c92 | 1996-02-25 04:50:29 +0000 | [diff] [blame] | 98 | #include <Events.h> /* For EventRecord */ |
| 99 | |
| 100 | typedef int (*TclMacConvertEventPtr) Py_PROTO((EventRecord *eventPtr)); |
Guido van Rossum | 0d2390c | 1997-08-14 19:57:07 +0000 | [diff] [blame] | 101 | /* They changed the name... */ |
| 102 | #if TKMAJORMINOR < 8000 |
| 103 | #define Tcl_MacSetEventProc TclMacSetEventProc |
| 104 | #endif |
| 105 | void Tcl_MacSetEventProc Py_PROTO((TclMacConvertEventPtr procPtr)); |
Guido van Rossum | ec22c92 | 1996-02-25 04:50:29 +0000 | [diff] [blame] | 106 | int TkMacConvertEvent Py_PROTO((EventRecord *eventPtr)); |
| 107 | |
| 108 | staticforward int PyMacConvertEvent Py_PROTO((EventRecord *eventPtr)); |
| 109 | |
Guido van Rossum | 2ea1c94 | 1998-04-28 16:12:43 +0000 | [diff] [blame] | 110 | #if defined(__CFM68K__) && !defined(__USING_STATIC_LIBS__) |
| 111 | #pragma import on |
| 112 | #endif |
| 113 | |
| 114 | #include <SIOUX.h> |
| 115 | extern int SIOUXIsAppWindow(WindowPtr); |
| 116 | |
| 117 | #if defined(__CFM68K__) && !defined(__USING_STATIC_LIBS__) |
| 118 | #pragma import reset |
| 119 | #endif |
Guido van Rossum | ec22c92 | 1996-02-25 04:50:29 +0000 | [diff] [blame] | 120 | #endif /* macintosh */ |
| 121 | |
Guido van Rossum | 97867b2 | 1996-08-08 19:09:53 +0000 | [diff] [blame] | 122 | #ifndef FREECAST |
Guido van Rossum | 7ffa761 | 1996-08-13 21:10:16 +0000 | [diff] [blame] | 123 | #define FREECAST (char *) |
Guido van Rossum | 97867b2 | 1996-08-08 19:09:53 +0000 | [diff] [blame] | 124 | #endif |
| 125 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 126 | /**** Tkapp Object Declaration ****/ |
| 127 | |
| 128 | staticforward PyTypeObject Tkapp_Type; |
| 129 | |
| 130 | typedef struct |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 131 | { |
| 132 | PyObject_HEAD |
| 133 | Tcl_Interp *interp; |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 134 | } |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 135 | TkappObject; |
| 136 | |
| 137 | #define Tkapp_Check(v) ((v)->ob_type == &Tkapp_Type) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 138 | #define Tkapp_Interp(v) (((TkappObject *) (v))->interp) |
| 139 | #define Tkapp_Result(v) (((TkappObject *) (v))->interp->result) |
| 140 | |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 141 | #define DEBUG_REFCNT(v) (printf("DEBUG: id=%p, refcnt=%i\n", \ |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 142 | (void *) v, ((PyObject *) v)->ob_refcnt)) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 143 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 144 | |
| 145 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 146 | /**** Error Handling ****/ |
| 147 | |
| 148 | static PyObject *Tkinter_TclError; |
Guido van Rossum | d308e2b | 1994-07-07 09:25:12 +0000 | [diff] [blame] | 149 | static int quitMainLoop = 0; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 150 | static int errorInCmd = 0; |
| 151 | static PyObject *excInCmd; |
| 152 | static PyObject *valInCmd; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 153 | static PyObject *trbInCmd; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 154 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 155 | |
| 156 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 157 | static PyObject * |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 158 | Tkinter_Error(v) |
| 159 | PyObject *v; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 160 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 161 | PyErr_SetString(Tkinter_TclError, Tkapp_Result(v)); |
| 162 | return NULL; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 163 | } |
| 164 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 165 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 166 | int |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 167 | PythonCmd_Error(interp) |
| 168 | Tcl_Interp *interp; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 169 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 170 | errorInCmd = 1; |
| 171 | PyErr_Fetch(&excInCmd, &valInCmd, &trbInCmd); |
| 172 | return TCL_ERROR; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 173 | } |
| 174 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 175 | |
| 176 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 177 | /**** Utils ****/ |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 178 | static char * |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 179 | AsString(value, tmp) |
| 180 | PyObject *value; |
| 181 | PyObject *tmp; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 182 | { |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 183 | if (PyString_Check(value)) |
| 184 | return PyString_AsString(value); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 185 | else { |
| 186 | PyObject *v = PyObject_Str(value); |
| 187 | PyList_Append(tmp, v); |
| 188 | Py_DECREF(v); |
| 189 | return PyString_AsString(v); |
| 190 | } |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 191 | } |
| 192 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 193 | |
| 194 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 195 | #define ARGSZ 64 |
| 196 | |
| 197 | static char * |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 198 | Merge(args) |
| 199 | PyObject *args; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 200 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 201 | PyObject *tmp = NULL; |
| 202 | char *argvStore[ARGSZ]; |
| 203 | char **argv = NULL; |
| 204 | int fvStore[ARGSZ]; |
| 205 | int *fv = NULL; |
| 206 | int argc = 0, i; |
| 207 | char *res = NULL; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 208 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 209 | if (!(tmp = PyList_New(0))) |
| 210 | return NULL; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 211 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 212 | argv = argvStore; |
| 213 | fv = fvStore; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 214 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 215 | if (args == NULL) |
| 216 | argc = 0; |
| 217 | |
| 218 | else if (!PyTuple_Check(args)) { |
| 219 | argc = 1; |
| 220 | fv[0] = 0; |
| 221 | argv[0] = AsString(args, tmp); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 222 | } |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 223 | else { |
| 224 | argc = PyTuple_Size(args); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 225 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 226 | if (argc > ARGSZ) { |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 227 | argv = (char **)ckalloc(argc * sizeof(char *)); |
| 228 | fv = (int *)ckalloc(argc * sizeof(int)); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 229 | if (argv == NULL || fv == NULL) { |
| 230 | PyErr_NoMemory(); |
| 231 | goto finally; |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | for (i = 0; i < argc; i++) { |
| 236 | PyObject *v = PyTuple_GetItem(args, i); |
| 237 | if (PyTuple_Check(v)) { |
| 238 | fv[i] = 1; |
| 239 | if (!(argv[i] = Merge(v))) |
| 240 | goto finally; |
| 241 | } |
| 242 | else if (v == Py_None) { |
| 243 | argc = i; |
| 244 | break; |
| 245 | } |
| 246 | else { |
| 247 | fv[i] = 0; |
| 248 | argv[i] = AsString(v, tmp); |
| 249 | } |
| 250 | } |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 251 | } |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 252 | res = Tcl_Merge(argc, argv); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 253 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 254 | finally: |
| 255 | for (i = 0; i < argc; i++) |
| 256 | if (fv[i]) { |
| 257 | ckfree(argv[i]); |
| 258 | } |
| 259 | if (argv != argvStore) |
| 260 | ckfree(FREECAST argv); |
| 261 | if (fv != fvStore) |
| 262 | ckfree(FREECAST fv); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 263 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 264 | Py_DECREF(tmp); |
| 265 | return res; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 266 | } |
| 267 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 268 | |
| 269 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 270 | static PyObject * |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 271 | Split(self, list) |
| 272 | PyObject *self; |
| 273 | char *list; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 274 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 275 | int argc; |
| 276 | char **argv; |
| 277 | PyObject *v; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 278 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 279 | if (list == NULL) { |
| 280 | Py_INCREF(Py_None); |
| 281 | return Py_None; |
| 282 | } |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 283 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 284 | if (Tcl_SplitList(Tkapp_Interp(self), list, &argc, &argv) == TCL_ERROR) |
| 285 | { |
| 286 | /* Not a list. |
| 287 | * Could be a quoted string containing funnies, e.g. {"}. |
| 288 | * Return the string itself. |
| 289 | */ |
| 290 | PyErr_Clear(); |
| 291 | return PyString_FromString(list); |
| 292 | } |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 293 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 294 | if (argc == 0) |
| 295 | v = PyString_FromString(""); |
| 296 | else if (argc == 1) |
| 297 | v = PyString_FromString(argv[0]); |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 298 | else if ((v = PyTuple_New(argc)) != NULL) { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 299 | int i; |
| 300 | PyObject *w; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 301 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 302 | for (i = 0; i < argc; i++) { |
| 303 | if ((w = Split(self, argv[i])) == NULL) { |
| 304 | Py_DECREF(v); |
| 305 | v = NULL; |
| 306 | break; |
| 307 | } |
| 308 | PyTuple_SetItem(v, i, w); |
| 309 | } |
| 310 | } |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 311 | ckfree(FREECAST argv); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 312 | return v; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 313 | } |
| 314 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 315 | |
| 316 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 317 | /**** Tkapp Object ****/ |
| 318 | |
| 319 | #ifndef WITH_APPINIT |
| 320 | int |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 321 | Tcl_AppInit(interp) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 322 | Tcl_Interp *interp; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 323 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 324 | Tk_Window main; |
Guido van Rossum | ec22c92 | 1996-02-25 04:50:29 +0000 | [diff] [blame] | 325 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 326 | main = Tk_MainWindow(interp); |
| 327 | if (Tcl_Init(interp) == TCL_ERROR) { |
Guido van Rossum | b41addf | 1998-05-12 15:02:41 +0000 | [diff] [blame^] | 328 | PySys_WriteStderr("Tcl_Init error: %s\n", interp->result); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 329 | return TCL_ERROR; |
| 330 | } |
| 331 | if (Tk_Init(interp) == TCL_ERROR) { |
Guido van Rossum | b41addf | 1998-05-12 15:02:41 +0000 | [diff] [blame^] | 332 | PySys_WriteStderr("Tk_Init error: %s\n", interp->result); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 333 | return TCL_ERROR; |
| 334 | } |
| 335 | return TCL_OK; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 336 | } |
| 337 | #endif /* !WITH_APPINIT */ |
| 338 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 339 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 340 | |
| 341 | |
| 342 | /* Initialize the Tk application; see the `main' function in |
| 343 | * `tkMain.c'. |
| 344 | */ |
| 345 | static TkappObject * |
| 346 | Tkapp_New(screenName, baseName, className, interactive) |
| 347 | char *screenName; |
| 348 | char *baseName; |
| 349 | char *className; |
| 350 | int interactive; |
| 351 | { |
| 352 | TkappObject *v; |
| 353 | char *argv0; |
| 354 | |
| 355 | v = PyObject_NEW(TkappObject, &Tkapp_Type); |
| 356 | if (v == NULL) |
| 357 | return NULL; |
| 358 | |
| 359 | v->interp = Tcl_CreateInterp(); |
Guido van Rossum | dfd428d | 1996-02-25 04:46:40 +0000 | [diff] [blame] | 360 | |
Guido van Rossum | 2ea1c94 | 1998-04-28 16:12:43 +0000 | [diff] [blame] | 361 | #if defined(macintosh) && TKMAJORMINOR >= 8000 |
| 362 | /* This seems to be needed since Tk 8.0 */ |
| 363 | ClearMenuBar(); |
| 364 | TkMacInitMenus(v->interp); |
| 365 | #endif |
Guido van Rossum | 1c0d315 | 1998-02-19 21:28:49 +0000 | [diff] [blame] | 366 | /* Delete the 'exit' command, which can screw things up */ |
| 367 | Tcl_DeleteCommand(v->interp, "exit"); |
| 368 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 369 | if (screenName != NULL) |
| 370 | Tcl_SetVar2(v->interp, "env", "DISPLAY", |
| 371 | screenName, TCL_GLOBAL_ONLY); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 372 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 373 | if (interactive) |
| 374 | Tcl_SetVar(v->interp, "tcl_interactive", "1", TCL_GLOBAL_ONLY); |
| 375 | else |
| 376 | Tcl_SetVar(v->interp, "tcl_interactive", "0", TCL_GLOBAL_ONLY); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 377 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 378 | /* This is used to get the application class for Tk 4.1 and up */ |
| 379 | argv0 = (char*)ckalloc(strlen(className) + 1); |
| 380 | if (!argv0) { |
| 381 | PyErr_NoMemory(); |
| 382 | Py_DECREF(v); |
| 383 | return NULL; |
| 384 | } |
Guido van Rossum | 97867b2 | 1996-08-08 19:09:53 +0000 | [diff] [blame] | 385 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 386 | strcpy(argv0, className); |
Guido van Rossum | 730806d | 1998-04-10 22:27:42 +0000 | [diff] [blame] | 387 | if (isupper((int)(argv0[0]))) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 388 | argv0[0] = tolower(argv0[0]); |
| 389 | Tcl_SetVar(v->interp, "argv0", argv0, TCL_GLOBAL_ONLY); |
| 390 | ckfree(argv0); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 391 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 392 | if (Tcl_AppInit(v->interp) != TCL_OK) |
| 393 | return (TkappObject *)Tkinter_Error(v); |
| 394 | |
| 395 | return v; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 396 | } |
| 397 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 398 | |
| 399 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 400 | /** Tcl Eval **/ |
| 401 | |
| 402 | static PyObject * |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 403 | Tkapp_Call(self, args) |
| 404 | PyObject *self; |
| 405 | PyObject *args; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 406 | { |
Guido van Rossum | 212643f | 1998-04-29 16:22:14 +0000 | [diff] [blame] | 407 | /* This is copied from Merge() */ |
| 408 | PyObject *tmp = NULL; |
| 409 | char *argvStore[ARGSZ]; |
| 410 | char **argv = NULL; |
| 411 | int fvStore[ARGSZ]; |
| 412 | int *fv = NULL; |
| 413 | int argc = 0, i; |
| 414 | PyObject *res = NULL; /* except this has a different type */ |
| 415 | Tcl_CmdInfo info; /* and this is added */ |
| 416 | Tcl_Interp *interp = Tkapp_Interp(self); /* and this too */ |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 417 | |
Guido van Rossum | 212643f | 1998-04-29 16:22:14 +0000 | [diff] [blame] | 418 | if (!(tmp = PyList_New(0))) |
| 419 | return NULL; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 420 | |
Guido van Rossum | 212643f | 1998-04-29 16:22:14 +0000 | [diff] [blame] | 421 | argv = argvStore; |
| 422 | fv = fvStore; |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 423 | |
Guido van Rossum | 212643f | 1998-04-29 16:22:14 +0000 | [diff] [blame] | 424 | if (args == NULL) |
| 425 | argc = 0; |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 426 | |
Guido van Rossum | 212643f | 1998-04-29 16:22:14 +0000 | [diff] [blame] | 427 | else if (!PyTuple_Check(args)) { |
| 428 | argc = 1; |
| 429 | fv[0] = 0; |
| 430 | argv[0] = AsString(args, tmp); |
| 431 | } |
| 432 | else { |
| 433 | argc = PyTuple_Size(args); |
| 434 | |
| 435 | if (argc > ARGSZ) { |
| 436 | argv = (char **)ckalloc(argc * sizeof(char *)); |
| 437 | fv = (int *)ckalloc(argc * sizeof(int)); |
| 438 | if (argv == NULL || fv == NULL) { |
| 439 | PyErr_NoMemory(); |
| 440 | goto finally; |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | for (i = 0; i < argc; i++) { |
| 445 | PyObject *v = PyTuple_GetItem(args, i); |
| 446 | if (PyTuple_Check(v)) { |
| 447 | fv[i] = 1; |
| 448 | if (!(argv[i] = Merge(v))) |
| 449 | goto finally; |
| 450 | } |
| 451 | else if (v == Py_None) { |
| 452 | argc = i; |
| 453 | break; |
| 454 | } |
| 455 | else { |
| 456 | fv[i] = 0; |
| 457 | argv[i] = AsString(v, tmp); |
| 458 | } |
| 459 | } |
| 460 | } |
| 461 | /* End code copied from Merge() */ |
| 462 | |
| 463 | /* All this to avoid a call to Tcl_Merge() and the corresponding call |
| 464 | to Tcl_SplitList() inside Tcl_Eval()... It can save a bundle! */ |
| 465 | if (Py_VerboseFlag >= 2) { |
| 466 | for (i = 0; i < argc; i++) |
Guido van Rossum | b41addf | 1998-05-12 15:02:41 +0000 | [diff] [blame^] | 467 | PySys_WriteStderr("%s ", argv[i]); |
Guido van Rossum | 212643f | 1998-04-29 16:22:14 +0000 | [diff] [blame] | 468 | } |
| 469 | if (argc < 1 || |
| 470 | !Tcl_GetCommandInfo(interp, argv[0], &info) || |
| 471 | info.proc == NULL) |
| 472 | { |
| 473 | char *cmd; |
| 474 | if (Py_VerboseFlag >= 2) |
Guido van Rossum | b41addf | 1998-05-12 15:02:41 +0000 | [diff] [blame^] | 475 | PySys_WriteStderr("... use TclEval "); |
Guido van Rossum | 212643f | 1998-04-29 16:22:14 +0000 | [diff] [blame] | 476 | cmd = Tcl_Merge(argc, argv); |
| 477 | i = Tcl_Eval(interp, cmd); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 478 | ckfree(cmd); |
Guido van Rossum | 212643f | 1998-04-29 16:22:14 +0000 | [diff] [blame] | 479 | } |
| 480 | else { |
| 481 | Tcl_ResetResult(interp); |
| 482 | i = (*info.proc)(info.clientData, interp, argc, argv); |
| 483 | } |
| 484 | if (i == TCL_ERROR) { |
| 485 | if (Py_VerboseFlag >= 2) |
Guido van Rossum | b41addf | 1998-05-12 15:02:41 +0000 | [diff] [blame^] | 486 | PySys_WriteStderr("... error: '%s'\n", |
Guido van Rossum | 212643f | 1998-04-29 16:22:14 +0000 | [diff] [blame] | 487 | interp->result); |
| 488 | Tkinter_Error(self); |
| 489 | } |
| 490 | else { |
| 491 | if (Py_VerboseFlag >= 2) |
Guido van Rossum | b41addf | 1998-05-12 15:02:41 +0000 | [diff] [blame^] | 492 | PySys_WriteStderr("-> '%s'\n", interp->result); |
Guido van Rossum | 212643f | 1998-04-29 16:22:14 +0000 | [diff] [blame] | 493 | res = PyString_FromString(interp->result); |
| 494 | } |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 495 | |
Guido van Rossum | 212643f | 1998-04-29 16:22:14 +0000 | [diff] [blame] | 496 | /* Copied from Merge() again */ |
| 497 | finally: |
| 498 | for (i = 0; i < argc; i++) |
| 499 | if (fv[i]) { |
| 500 | ckfree(argv[i]); |
| 501 | } |
| 502 | if (argv != argvStore) |
| 503 | ckfree(FREECAST argv); |
| 504 | if (fv != fvStore) |
| 505 | ckfree(FREECAST fv); |
| 506 | |
| 507 | Py_DECREF(tmp); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 508 | return res; |
| 509 | } |
| 510 | |
| 511 | |
| 512 | static PyObject * |
| 513 | Tkapp_GlobalCall(self, args) |
| 514 | PyObject *self; |
| 515 | PyObject *args; |
| 516 | { |
Guido van Rossum | 212643f | 1998-04-29 16:22:14 +0000 | [diff] [blame] | 517 | /* Could do the same here as for Tkapp_Call(), but this is not used |
| 518 | much, so I can't be bothered. Unfortunately Tcl doesn't export a |
| 519 | way for the user to do what all its Global* variants do (save and |
| 520 | reset the scope pointer, call the local version, restore the saved |
| 521 | scope pointer). */ |
| 522 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 523 | char *cmd = Merge(args); |
| 524 | PyObject *res = NULL; |
| 525 | |
| 526 | |
| 527 | if (!cmd) |
| 528 | PyErr_SetString(Tkinter_TclError, "merge failed"); |
| 529 | |
| 530 | else if (Tcl_GlobalEval(Tkapp_Interp(self), cmd) == TCL_ERROR) |
| 531 | res = Tkinter_Error(self); |
| 532 | else |
| 533 | res = PyString_FromString(Tkapp_Result(self)); |
| 534 | |
| 535 | if (cmd) |
| 536 | ckfree(cmd); |
| 537 | |
| 538 | return res; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 539 | } |
| 540 | |
| 541 | static PyObject * |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 542 | Tkapp_Eval(self, args) |
| 543 | PyObject *self; |
| 544 | PyObject *args; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 545 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 546 | char *script; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 547 | |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 548 | if (!PyArg_ParseTuple(args, "s", &script)) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 549 | return NULL; |
| 550 | |
| 551 | if (Tcl_Eval(Tkapp_Interp(self), script) == TCL_ERROR) |
| 552 | return Tkinter_Error(self); |
| 553 | |
| 554 | return PyString_FromString(Tkapp_Result(self)); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | static PyObject * |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 558 | Tkapp_GlobalEval(self, args) |
| 559 | PyObject *self; |
| 560 | PyObject *args; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 561 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 562 | char *script; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 563 | |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 564 | if (!PyArg_ParseTuple(args, "s", &script)) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 565 | return NULL; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 566 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 567 | if (Tcl_GlobalEval(Tkapp_Interp(self), script) == TCL_ERROR) |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 568 | return Tkinter_Error(self); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 569 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 570 | return PyString_FromString(Tkapp_Result(self)); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | static PyObject * |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 574 | Tkapp_EvalFile(self, args) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 575 | PyObject *self; |
| 576 | PyObject *args; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 577 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 578 | char *fileName; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 579 | |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 580 | if (!PyArg_ParseTuple(args, "s", &fileName)) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 581 | return NULL; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 582 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 583 | if (Tcl_EvalFile(Tkapp_Interp(self), fileName) == TCL_ERROR) |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 584 | return Tkinter_Error(self); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 585 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 586 | return PyString_FromString(Tkapp_Result(self)); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | static PyObject * |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 590 | Tkapp_Record(self, args) |
| 591 | PyObject *self; |
| 592 | PyObject *args; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 593 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 594 | char *script; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 595 | |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 596 | if (!PyArg_ParseTuple(args, "s", &script)) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 597 | return NULL; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 598 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 599 | if (TCL_ERROR == Tcl_RecordAndEval(Tkapp_Interp(self), |
| 600 | script, TCL_NO_EVAL)) |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 601 | return Tkinter_Error(self); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 602 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 603 | return PyString_FromString(Tkapp_Result(self)); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 604 | } |
| 605 | |
| 606 | static PyObject * |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 607 | Tkapp_AddErrorInfo(self, args) |
| 608 | PyObject *self; |
| 609 | PyObject *args; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 610 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 611 | char *msg; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 612 | |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 613 | if (!PyArg_ParseTuple(args, "s", &msg)) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 614 | return NULL; |
| 615 | Tcl_AddErrorInfo(Tkapp_Interp(self), msg); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 616 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 617 | Py_INCREF(Py_None); |
| 618 | return Py_None; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 619 | } |
| 620 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 621 | |
| 622 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 623 | /** Tcl Variable **/ |
| 624 | |
| 625 | static PyObject * |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 626 | SetVar(self, args, flags) |
| 627 | PyObject *self; |
| 628 | PyObject *args; |
| 629 | int flags; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 630 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 631 | char *name1, *name2, *ok; |
| 632 | PyObject *newValue; |
| 633 | PyObject *tmp = PyList_New(0); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 634 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 635 | if (!tmp) |
| 636 | return NULL; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 637 | |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 638 | if (PyArg_ParseTuple(args, "sO", &name1, &newValue)) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 639 | /* XXX Merge? */ |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 640 | ok = Tcl_SetVar(Tkapp_Interp(self), name1, |
| 641 | AsString(newValue, tmp), flags); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 642 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 643 | else { |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 644 | PyErr_Clear(); |
| 645 | if (PyArg_ParseTuple(args, "ssO", &name1, &name2, &newValue)) |
| 646 | ok = Tcl_SetVar2(Tkapp_Interp(self), name1, name2, |
| 647 | AsString (newValue, tmp), flags); |
| 648 | else { |
| 649 | Py_DECREF (tmp); |
| 650 | return NULL; |
| 651 | } |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 652 | } |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 653 | Py_DECREF(tmp); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 654 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 655 | if (!ok) |
| 656 | return Tkinter_Error(self); |
| 657 | |
| 658 | Py_INCREF(Py_None); |
| 659 | return Py_None; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | static PyObject * |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 663 | Tkapp_SetVar(self, args) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 664 | PyObject *self; |
| 665 | PyObject *args; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 666 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 667 | return SetVar(self, args, TCL_LEAVE_ERR_MSG); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 668 | } |
| 669 | |
| 670 | static PyObject * |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 671 | Tkapp_GlobalSetVar(self, args) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 672 | PyObject *self; |
| 673 | PyObject *args; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 674 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 675 | return SetVar(self, args, TCL_LEAVE_ERR_MSG | TCL_GLOBAL_ONLY); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 676 | } |
| 677 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 678 | |
| 679 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 680 | static PyObject * |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 681 | GetVar(self, args, flags) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 682 | PyObject *self; |
| 683 | PyObject *args; |
| 684 | int flags; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 685 | { |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 686 | char *name1, *name2=NULL, *s; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 687 | |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 688 | if (!PyArg_ParseTuple(args, "s|s", &name1, &name2)) |
| 689 | return NULL; |
| 690 | if (name2 == NULL) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 691 | s = Tcl_GetVar(Tkapp_Interp (self), name1, flags); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 692 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 693 | else |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 694 | s = Tcl_GetVar2(Tkapp_Interp(self), name1, name2, flags); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 695 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 696 | if (s == NULL) |
| 697 | return Tkinter_Error(self); |
| 698 | |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 699 | return PyString_FromString(s); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 700 | } |
| 701 | |
| 702 | static PyObject * |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 703 | Tkapp_GetVar(self, args) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 704 | PyObject *self; |
| 705 | PyObject *args; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 706 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 707 | return GetVar(self, args, TCL_LEAVE_ERR_MSG); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 708 | } |
| 709 | |
| 710 | static PyObject * |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 711 | Tkapp_GlobalGetVar(self, args) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 712 | PyObject *self; |
| 713 | PyObject *args; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 714 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 715 | return GetVar(self, args, TCL_LEAVE_ERR_MSG | TCL_GLOBAL_ONLY); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 716 | } |
| 717 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 718 | |
| 719 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 720 | static PyObject * |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 721 | UnsetVar(self, args, flags) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 722 | PyObject *self; |
| 723 | PyObject *args; |
| 724 | int flags; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 725 | { |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 726 | char *name1, *name2=NULL; |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 727 | int code; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 728 | |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 729 | if (!PyArg_ParseTuple(args, "s|s", &name1, &name2)) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 730 | return NULL; |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 731 | if (name2 == NULL) |
| 732 | code = Tcl_UnsetVar(Tkapp_Interp(self), name1, flags); |
| 733 | |
| 734 | else |
| 735 | code = Tcl_UnsetVar2(Tkapp_Interp(self), name1, name2, flags); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 736 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 737 | if (code == TCL_ERROR) |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 738 | return Tkinter_Error(self); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 739 | |
| 740 | Py_INCREF(Py_None); |
| 741 | return Py_None; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 742 | } |
| 743 | |
| 744 | static PyObject * |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 745 | Tkapp_UnsetVar(self, args) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 746 | PyObject *self; |
| 747 | PyObject *args; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 748 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 749 | return UnsetVar(self, args, TCL_LEAVE_ERR_MSG); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 750 | } |
| 751 | |
| 752 | static PyObject * |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 753 | Tkapp_GlobalUnsetVar(self, args) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 754 | PyObject *self; |
| 755 | PyObject *args; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 756 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 757 | return UnsetVar(self, args, TCL_LEAVE_ERR_MSG | TCL_GLOBAL_ONLY); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 758 | } |
| 759 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 760 | |
| 761 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 762 | /** Tcl to Python **/ |
| 763 | |
| 764 | static PyObject * |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 765 | Tkapp_GetInt(self, args) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 766 | PyObject *self; |
| 767 | PyObject *args; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 768 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 769 | char *s; |
| 770 | int v; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 771 | |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 772 | if (!PyArg_ParseTuple(args, "s", &s)) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 773 | return NULL; |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 774 | if (Tcl_GetInt(Tkapp_Interp(self), s, &v) == TCL_ERROR) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 775 | return Tkinter_Error(self); |
| 776 | return Py_BuildValue("i", v); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 777 | } |
| 778 | |
| 779 | static PyObject * |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 780 | Tkapp_GetDouble(self, args) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 781 | PyObject *self; |
| 782 | PyObject *args; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 783 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 784 | char *s; |
| 785 | double v; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 786 | |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 787 | if (!PyArg_ParseTuple(args, "s", &s)) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 788 | return NULL; |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 789 | if (Tcl_GetDouble(Tkapp_Interp(self), s, &v) == TCL_ERROR) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 790 | return Tkinter_Error(self); |
| 791 | return Py_BuildValue("d", v); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 792 | } |
| 793 | |
| 794 | static PyObject * |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 795 | Tkapp_GetBoolean(self, args) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 796 | PyObject *self; |
| 797 | PyObject *args; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 798 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 799 | char *s; |
| 800 | int v; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 801 | |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 802 | if (!PyArg_ParseTuple(args, "s", &s)) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 803 | return NULL; |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 804 | if (Tcl_GetBoolean(Tkapp_Interp(self), s, &v) == TCL_ERROR) |
| 805 | return Tkinter_Error(self); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 806 | return Py_BuildValue("i", v); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 807 | } |
| 808 | |
| 809 | static PyObject * |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 810 | Tkapp_ExprString(self, args) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 811 | PyObject *self; |
| 812 | PyObject *args; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 813 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 814 | char *s; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 815 | |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 816 | if (!PyArg_ParseTuple(args, "s", &s)) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 817 | return NULL; |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 818 | if (Tcl_ExprString(Tkapp_Interp(self), s) == TCL_ERROR) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 819 | return Tkinter_Error(self); |
| 820 | return Py_BuildValue("s", Tkapp_Result(self)); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 821 | } |
| 822 | |
| 823 | static PyObject * |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 824 | Tkapp_ExprLong(self, args) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 825 | PyObject *self; |
| 826 | PyObject *args; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 827 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 828 | char *s; |
| 829 | long v; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 830 | |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 831 | if (!PyArg_ParseTuple(args, "s", &s)) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 832 | return NULL; |
| 833 | if (Tcl_ExprLong(Tkapp_Interp(self), s, &v) == TCL_ERROR) |
| 834 | return Tkinter_Error(self); |
| 835 | return Py_BuildValue("l", v); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 836 | } |
| 837 | |
| 838 | static PyObject * |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 839 | Tkapp_ExprDouble(self, args) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 840 | PyObject *self; |
| 841 | PyObject *args; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 842 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 843 | char *s; |
| 844 | double v; |
Guido van Rossum | 52fa3a6 | 1997-02-14 22:59:58 +0000 | [diff] [blame] | 845 | int retval; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 846 | |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 847 | if (!PyArg_ParseTuple(args, "s", &s)) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 848 | return NULL; |
Guido van Rossum | 52fa3a6 | 1997-02-14 22:59:58 +0000 | [diff] [blame] | 849 | PyFPE_START_PROTECT("Tkapp_ExprDouble", return 0) |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 850 | retval = Tcl_ExprDouble(Tkapp_Interp(self), s, &v); |
Guido van Rossum | 45b8391 | 1997-03-14 04:32:50 +0000 | [diff] [blame] | 851 | PyFPE_END_PROTECT(retval) |
Guido van Rossum | 52fa3a6 | 1997-02-14 22:59:58 +0000 | [diff] [blame] | 852 | if (retval == TCL_ERROR) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 853 | return Tkinter_Error(self); |
| 854 | return Py_BuildValue("d", v); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 855 | } |
| 856 | |
| 857 | static PyObject * |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 858 | Tkapp_ExprBoolean(self, args) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 859 | PyObject *self; |
| 860 | PyObject *args; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 861 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 862 | char *s; |
| 863 | int v; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 864 | |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 865 | if (!PyArg_ParseTuple(args, "s", &s)) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 866 | return NULL; |
| 867 | if (Tcl_ExprBoolean(Tkapp_Interp(self), s, &v) == TCL_ERROR) |
| 868 | return Tkinter_Error(self); |
| 869 | return Py_BuildValue("i", v); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 870 | } |
| 871 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 872 | |
| 873 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 874 | static PyObject * |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 875 | Tkapp_SplitList(self, args) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 876 | PyObject *self; |
| 877 | PyObject *args; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 878 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 879 | char *list; |
| 880 | int argc; |
| 881 | char **argv; |
| 882 | PyObject *v; |
| 883 | int i; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 884 | |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 885 | if (!PyArg_ParseTuple(args, "s", &list)) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 886 | return NULL; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 887 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 888 | if (Tcl_SplitList(Tkapp_Interp(self), list, &argc, &argv) == TCL_ERROR) |
| 889 | return Tkinter_Error(self); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 890 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 891 | if (!(v = PyTuple_New(argc))) |
| 892 | return NULL; |
| 893 | |
| 894 | for (i = 0; i < argc; i++) { |
| 895 | PyObject *s = PyString_FromString(argv[i]); |
| 896 | if (!s || PyTuple_SetItem(v, i, s)) { |
| 897 | Py_DECREF(v); |
| 898 | v = NULL; |
| 899 | goto finally; |
| 900 | } |
| 901 | } |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 902 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 903 | finally: |
| 904 | ckfree(FREECAST argv); |
| 905 | return v; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 906 | } |
| 907 | |
| 908 | static PyObject * |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 909 | Tkapp_Split(self, args) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 910 | PyObject *self; |
| 911 | PyObject *args; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 912 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 913 | char *list; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 914 | |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 915 | if (!PyArg_ParseTuple(args, "s", &list)) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 916 | return NULL; |
| 917 | return Split(self, list); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 918 | } |
| 919 | |
| 920 | static PyObject * |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 921 | Tkapp_Merge(self, args) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 922 | PyObject *self; |
| 923 | PyObject *args; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 924 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 925 | char *s = Merge(args); |
| 926 | PyObject *res = NULL; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 927 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 928 | if (s) { |
| 929 | res = PyString_FromString(s); |
| 930 | ckfree(s); |
| 931 | } |
| 932 | else |
| 933 | PyErr_SetString(Tkinter_TclError, "merge failed"); |
| 934 | |
| 935 | return res; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 936 | } |
| 937 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 938 | |
| 939 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 940 | /** Tcl Command **/ |
| 941 | |
| 942 | /* This is the Tcl command that acts as a wrapper for Python |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 943 | * function or method. |
| 944 | */ |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 945 | static int |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 946 | PythonCmd(clientData, interp, argc, argv) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 947 | ClientData clientData; /* Is (self, func) */ |
| 948 | Tcl_Interp *interp; |
| 949 | int argc; |
| 950 | char *argv[]; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 951 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 952 | PyObject *self, *func, *arg, *res, *tmp; |
| 953 | int i; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 954 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 955 | /* TBD: no error checking here since we know, via the |
| 956 | * Tkapp_CreateCommand() that the client data is a two-tuple |
| 957 | */ |
| 958 | self = PyTuple_GetItem((PyObject *) clientData, 0); |
| 959 | func = PyTuple_GetItem((PyObject *) clientData, 1); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 960 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 961 | /* Create argument list (argv1, ..., argvN) */ |
| 962 | if (!(arg = PyTuple_New(argc - 1))) |
| 963 | return PythonCmd_Error(interp); |
Guido van Rossum | d308e2b | 1994-07-07 09:25:12 +0000 | [diff] [blame] | 964 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 965 | for (i = 0; i < (argc - 1); i++) { |
| 966 | PyObject *s = PyString_FromString(argv[i + 1]); |
| 967 | if (!s || PyTuple_SetItem(arg, i, s)) { |
| 968 | Py_DECREF(arg); |
| 969 | PythonCmd_Error(interp); |
| 970 | } |
| 971 | } |
| 972 | res = PyEval_CallObject(func, arg); |
| 973 | Py_DECREF(arg); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 974 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 975 | if (res == NULL) |
| 976 | return PythonCmd_Error(interp); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 977 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 978 | if (!(tmp = PyList_New(0))) { |
| 979 | Py_DECREF(res); |
| 980 | return PythonCmd_Error(interp); |
| 981 | } |
| 982 | |
| 983 | Tcl_SetResult(Tkapp_Interp(self), AsString(res, tmp), TCL_VOLATILE); |
| 984 | Py_DECREF(res); |
| 985 | Py_DECREF(tmp); |
| 986 | |
| 987 | return TCL_OK; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 988 | } |
| 989 | |
| 990 | static void |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 991 | PythonCmdDelete(clientData) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 992 | ClientData clientData; /* Is (self, func) */ |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 993 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 994 | Py_DECREF((PyObject *) clientData); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 995 | } |
| 996 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 997 | |
| 998 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 999 | static PyObject * |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1000 | Tkapp_CreateCommand(self, args) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1001 | PyObject *self; |
| 1002 | PyObject *args; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1003 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1004 | char *cmdName; |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1005 | PyObject *func; |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1006 | PyObject *data; |
| 1007 | |
| 1008 | if (!PyArg_ParseTuple(args, "sO", &cmdName, &func)) |
| 1009 | return NULL; |
| 1010 | if (!PyCallable_Check(func)) { |
| 1011 | PyErr_SetString(PyExc_TypeError, "command not callable"); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1012 | return NULL; |
| 1013 | } |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1014 | |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1015 | data = Py_BuildValue("OO", self, func); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1016 | if (!data) |
| 1017 | return NULL; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1018 | |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1019 | if (Tcl_CreateCommand(Tkapp_Interp(self), cmdName, PythonCmd, |
| 1020 | (ClientData) data, PythonCmdDelete) == NULL) |
| 1021 | { |
| 1022 | PyErr_SetString(Tkinter_TclError, "can't create Tcl command"); |
| 1023 | Py_DECREF(data); |
| 1024 | return NULL; |
| 1025 | } |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1026 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1027 | Py_INCREF(Py_None); |
| 1028 | return Py_None; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1029 | } |
| 1030 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1031 | |
| 1032 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1033 | static PyObject * |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1034 | Tkapp_DeleteCommand(self, args) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1035 | PyObject *self; |
| 1036 | PyObject *args; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1037 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1038 | char *cmdName; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1039 | |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1040 | if (!PyArg_ParseTuple(args, "s", &cmdName)) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1041 | return NULL; |
| 1042 | if (Tcl_DeleteCommand(Tkapp_Interp(self), cmdName) == -1) |
| 1043 | { |
| 1044 | PyErr_SetString(Tkinter_TclError, "can't delete Tcl command"); |
| 1045 | return NULL; |
| 1046 | } |
| 1047 | Py_INCREF(Py_None); |
| 1048 | return Py_None; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1049 | } |
| 1050 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1051 | |
| 1052 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1053 | /** File Handler **/ |
| 1054 | |
Guido van Rossum | a597dde | 1995-01-10 20:56:29 +0000 | [diff] [blame] | 1055 | static void |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1056 | FileHandler(clientData, mask) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1057 | ClientData clientData; /* Is: (func, file) */ |
| 1058 | int mask; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1059 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1060 | PyObject *func, *file, *arg, *res; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1061 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1062 | func = PyTuple_GetItem((PyObject *) clientData, 0); |
| 1063 | file = PyTuple_GetItem((PyObject *) clientData, 1); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1064 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1065 | arg = Py_BuildValue("(Oi)", file, (long) mask); |
| 1066 | res = PyEval_CallObject(func, arg); |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1067 | Py_DECREF(arg); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1068 | |
| 1069 | if (res == NULL) { |
| 1070 | errorInCmd = 1; |
| 1071 | PyErr_Fetch(&excInCmd, &valInCmd, &trbInCmd); |
| 1072 | } |
| 1073 | Py_XDECREF(res); |
Guido van Rossum | 9bb4fd6 | 1994-08-09 14:15:19 +0000 | [diff] [blame] | 1074 | } |
| 1075 | |
| 1076 | static int |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1077 | GetFileNo(file) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1078 | /* Either an int >= 0 or an object with a |
| 1079 | *.fileno() method that returns an int >= 0 |
| 1080 | */ |
| 1081 | PyObject *file; |
Guido van Rossum | 9bb4fd6 | 1994-08-09 14:15:19 +0000 | [diff] [blame] | 1082 | { |
Guido van Rossum | a597dde | 1995-01-10 20:56:29 +0000 | [diff] [blame] | 1083 | PyObject *meth, *args, *res; |
Guido van Rossum | 9bb4fd6 | 1994-08-09 14:15:19 +0000 | [diff] [blame] | 1084 | int id; |
| 1085 | if (PyInt_Check(file)) { |
| 1086 | id = PyInt_AsLong(file); |
| 1087 | if (id < 0) |
| 1088 | PyErr_SetString(PyExc_ValueError, "invalid file id"); |
| 1089 | return id; |
| 1090 | } |
Guido van Rossum | 9bb4fd6 | 1994-08-09 14:15:19 +0000 | [diff] [blame] | 1091 | args = PyTuple_New(0); |
| 1092 | if (args == NULL) |
| 1093 | return -1; |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1094 | |
| 1095 | meth = PyObject_GetAttrString(file, "fileno"); |
| 1096 | if (meth == NULL) { |
| 1097 | Py_DECREF(args); |
| 1098 | return -1; |
| 1099 | } |
| 1100 | |
Guido van Rossum | 9bb4fd6 | 1994-08-09 14:15:19 +0000 | [diff] [blame] | 1101 | res = PyEval_CallObject(meth, args); |
| 1102 | Py_DECREF(args); |
| 1103 | Py_DECREF(meth); |
| 1104 | if (res == NULL) |
| 1105 | return -1; |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1106 | |
Guido van Rossum | 9bb4fd6 | 1994-08-09 14:15:19 +0000 | [diff] [blame] | 1107 | if (PyInt_Check(res)) |
| 1108 | id = PyInt_AsLong(res); |
| 1109 | else |
| 1110 | id = -1; |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1111 | |
Guido van Rossum | 9bb4fd6 | 1994-08-09 14:15:19 +0000 | [diff] [blame] | 1112 | if (id < 0) |
| 1113 | PyErr_SetString(PyExc_ValueError, |
| 1114 | "invalid fileno() return value"); |
| 1115 | Py_DECREF(res); |
| 1116 | return id; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1117 | } |
| 1118 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1119 | |
| 1120 | static PyObject* Tkapp_ClientDataDict = NULL; |
| 1121 | |
Guido van Rossum | 0d2390c | 1997-08-14 19:57:07 +0000 | [diff] [blame] | 1122 | #ifdef HAVE_CREATEFILEHANDLER |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1123 | static PyObject * |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1124 | Tkapp_CreateFileHandler(self, args) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1125 | PyObject *self; |
| 1126 | PyObject *args; /* Is (file, mask, func) */ |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1127 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1128 | PyObject *file, *func, *data; |
| 1129 | PyObject *idkey; |
| 1130 | int mask, id; |
Guido van Rossum | 3e819a7 | 1997-08-01 19:29:02 +0000 | [diff] [blame] | 1131 | #if TKMAJORMINOR < 8000 |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1132 | Tcl_File tfile; |
Guido van Rossum | 6878436 | 1996-05-16 17:17:31 +0000 | [diff] [blame] | 1133 | #endif |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1134 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1135 | if (!Tkapp_ClientDataDict) { |
| 1136 | if (!(Tkapp_ClientDataDict = PyDict_New())) |
| 1137 | return NULL; |
| 1138 | } |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1139 | |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1140 | if (!PyArg_ParseTuple(args, "OiO", &file, &mask, &func)) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1141 | return NULL; |
| 1142 | id = GetFileNo(file); |
| 1143 | if (id < 0) |
| 1144 | return NULL; |
| 1145 | if (!PyCallable_Check(func)) { |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1146 | PyErr_SetString(PyExc_TypeError, "bad argument list"); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1147 | return NULL; |
| 1148 | } |
| 1149 | |
| 1150 | if (!(idkey = PyInt_FromLong(id))) |
| 1151 | return NULL; |
| 1152 | |
| 1153 | /* ClientData is: (func, file) */ |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1154 | data = Py_BuildValue("(OO)", func, file); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1155 | if (!data || PyDict_SetItem(Tkapp_ClientDataDict, idkey, data)) { |
| 1156 | Py_DECREF(idkey); |
| 1157 | Py_XDECREF(data); |
| 1158 | return NULL; |
| 1159 | } |
| 1160 | Py_DECREF(idkey); |
Guido van Rossum | 7687522 | 1994-06-27 07:59:42 +0000 | [diff] [blame] | 1161 | |
Guido van Rossum | 3e819a7 | 1997-08-01 19:29:02 +0000 | [diff] [blame] | 1162 | #if TKMAJORMINOR < 8000 |
Guido van Rossum | 07886d0 | 1996-09-11 23:31:42 +0000 | [diff] [blame] | 1163 | #ifdef MS_WINDOWS |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1164 | /* We assume this is a socket... */ |
| 1165 | tfile = Tcl_GetFile((ClientData)id, TCL_WIN_SOCKET); |
Guido van Rossum | 3e819a7 | 1997-08-01 19:29:02 +0000 | [diff] [blame] | 1166 | #else /* !MS_WINDOWS */ |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1167 | tfile = Tcl_GetFile((ClientData)id, TCL_UNIX_FD); |
Guido van Rossum | 3e819a7 | 1997-08-01 19:29:02 +0000 | [diff] [blame] | 1168 | #endif /* !MS_WINDOWS */ |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1169 | /* Ought to check for null Tcl_File object... */ |
| 1170 | Tcl_CreateFileHandler(tfile, mask, FileHandler, (ClientData) data); |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1171 | #else /* >= 8000 */ |
Guido van Rossum | 3e819a7 | 1997-08-01 19:29:02 +0000 | [diff] [blame] | 1172 | Tcl_CreateFileHandler(id, mask, FileHandler, (ClientData) data); |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1173 | #endif /* >= 8000 */ |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1174 | /* XXX fileHandlerDict */ |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1175 | Py_INCREF(Py_None); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1176 | return Py_None; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1177 | } |
| 1178 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1179 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1180 | static PyObject * |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1181 | Tkapp_DeleteFileHandler(self, args) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1182 | PyObject *self; |
| 1183 | PyObject *args; /* Args: file */ |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1184 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1185 | PyObject *file; |
| 1186 | PyObject *idkey; |
| 1187 | PyObject *data; |
| 1188 | int id; |
Guido van Rossum | 3e819a7 | 1997-08-01 19:29:02 +0000 | [diff] [blame] | 1189 | #if TKMAJORMINOR < 8000 |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1190 | Tcl_File tfile; |
Guido van Rossum | 6878436 | 1996-05-16 17:17:31 +0000 | [diff] [blame] | 1191 | #endif |
| 1192 | |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1193 | if (!PyArg_ParseTuple(args, "O", &file)) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1194 | return NULL; |
| 1195 | id = GetFileNo(file); |
| 1196 | if (id < 0) |
| 1197 | return NULL; |
| 1198 | |
| 1199 | if (!(idkey = PyInt_FromLong(id))) |
| 1200 | return NULL; |
| 1201 | |
| 1202 | /* find and free the object created in the |
| 1203 | * Tkapp_CreateFileHandler() call |
| 1204 | */ |
| 1205 | data = PyDict_GetItem(Tkapp_ClientDataDict, idkey); |
| 1206 | Py_XDECREF(data); |
| 1207 | PyDict_DelItem(Tkapp_ClientDataDict, idkey); |
| 1208 | Py_DECREF(idkey); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1209 | |
Guido van Rossum | 3e819a7 | 1997-08-01 19:29:02 +0000 | [diff] [blame] | 1210 | #if TKMAJORMINOR < 8000 |
Guido van Rossum | 07886d0 | 1996-09-11 23:31:42 +0000 | [diff] [blame] | 1211 | #ifdef MS_WINDOWS |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1212 | /* We assume this is a socket... */ |
| 1213 | tfile = Tcl_GetFile((ClientData)id, TCL_WIN_SOCKET); |
Guido van Rossum | 845547d | 1996-06-26 18:26:04 +0000 | [diff] [blame] | 1214 | #else |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1215 | tfile = Tcl_GetFile((ClientData)id, TCL_UNIX_FD); |
Guido van Rossum | 845547d | 1996-06-26 18:26:04 +0000 | [diff] [blame] | 1216 | #endif |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1217 | /* Ought to check for null Tcl_File object... */ |
| 1218 | Tcl_DeleteFileHandler(tfile); |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1219 | #else /* >= 8000 */ |
Guido van Rossum | 3e819a7 | 1997-08-01 19:29:02 +0000 | [diff] [blame] | 1220 | Tcl_DeleteFileHandler(id); |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1221 | #endif /* >= 8000 */ |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1222 | /* XXX fileHandlerDict */ |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1223 | Py_INCREF(Py_None); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1224 | return Py_None; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1225 | } |
Guido van Rossum | 0d2390c | 1997-08-14 19:57:07 +0000 | [diff] [blame] | 1226 | #endif /* HAVE_CREATEFILEHANDLER */ |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1227 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1228 | |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 1229 | /**** Tktt Object (timer token) ****/ |
| 1230 | |
| 1231 | staticforward PyTypeObject Tktt_Type; |
| 1232 | |
| 1233 | typedef struct |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1234 | { |
| 1235 | PyObject_HEAD |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1236 | Tcl_TimerToken token; |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1237 | PyObject *func; |
| 1238 | } |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 1239 | TkttObject; |
| 1240 | |
| 1241 | static PyObject * |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1242 | Tktt_DeleteTimerHandler(self, args) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1243 | PyObject *self; |
| 1244 | PyObject *args; |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 1245 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1246 | TkttObject *v = (TkttObject *)self; |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 1247 | |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1248 | if (!PyArg_ParseTuple(args, "")) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1249 | return NULL; |
| 1250 | if (v->func != NULL) { |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1251 | Tcl_DeleteTimerHandler(v->token); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1252 | PyMem_DEL(v->func); |
| 1253 | v->func = NULL; |
| 1254 | } |
| 1255 | Py_INCREF(Py_None); |
| 1256 | return Py_None; |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 1257 | } |
| 1258 | |
| 1259 | static PyMethodDef Tktt_methods[] = |
| 1260 | { |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1261 | {"deletetimerhandler", Tktt_DeleteTimerHandler, 1}, |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1262 | {NULL, NULL} |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 1263 | }; |
| 1264 | |
| 1265 | static TkttObject * |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1266 | Tktt_New(token, func) |
| 1267 | Tcl_TimerToken token; |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1268 | PyObject *func; |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 1269 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1270 | TkttObject *v; |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 1271 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1272 | v = PyObject_NEW(TkttObject, &Tktt_Type); |
| 1273 | if (v == NULL) |
| 1274 | return NULL; |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 1275 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1276 | v->token = token; |
| 1277 | v->func = func; |
| 1278 | Py_INCREF(v->func); |
| 1279 | return v; |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 1280 | } |
| 1281 | |
| 1282 | static void |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1283 | Tktt_Dealloc(self) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1284 | PyObject *self; |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 1285 | { |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1286 | PyMem_DEL(self); |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 1287 | } |
| 1288 | |
Guido van Rossum | 597ac20 | 1998-05-12 14:36:19 +0000 | [diff] [blame] | 1289 | static PyObject * |
| 1290 | Tktt_Repr(self) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1291 | PyObject *self; |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 1292 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1293 | TkttObject *v = (TkttObject *)self; |
Guido van Rossum | 597ac20 | 1998-05-12 14:36:19 +0000 | [diff] [blame] | 1294 | char buf[100]; |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 1295 | |
Guido van Rossum | 597ac20 | 1998-05-12 14:36:19 +0000 | [diff] [blame] | 1296 | sprintf(buf, "<tktimertoken at 0x%lx%s>", (long)v, |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1297 | v->func == NULL ? ", handler deleted" : ""); |
Guido van Rossum | 597ac20 | 1998-05-12 14:36:19 +0000 | [diff] [blame] | 1298 | return PyString_FromString(buf); |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 1299 | } |
| 1300 | |
| 1301 | static PyObject * |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1302 | Tktt_GetAttr(self, name) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1303 | PyObject *self; |
| 1304 | char *name; |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 1305 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1306 | return Py_FindMethod(Tktt_methods, self, name); |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 1307 | } |
| 1308 | |
| 1309 | static PyTypeObject Tktt_Type = |
| 1310 | { |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1311 | PyObject_HEAD_INIT(NULL) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1312 | 0, /*ob_size */ |
| 1313 | "tktimertoken", /*tp_name */ |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1314 | sizeof(TkttObject), /*tp_basicsize */ |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1315 | 0, /*tp_itemsize */ |
| 1316 | Tktt_Dealloc, /*tp_dealloc */ |
Guido van Rossum | 597ac20 | 1998-05-12 14:36:19 +0000 | [diff] [blame] | 1317 | 0, /*tp_print */ |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1318 | Tktt_GetAttr, /*tp_getattr */ |
| 1319 | 0, /*tp_setattr */ |
| 1320 | 0, /*tp_compare */ |
Guido van Rossum | 597ac20 | 1998-05-12 14:36:19 +0000 | [diff] [blame] | 1321 | Tktt_Repr, /*tp_repr */ |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1322 | 0, /*tp_as_number */ |
| 1323 | 0, /*tp_as_sequence */ |
| 1324 | 0, /*tp_as_mapping */ |
| 1325 | 0, /*tp_hash */ |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 1326 | }; |
| 1327 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1328 | |
| 1329 | |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 1330 | /** Timer Handler **/ |
| 1331 | |
| 1332 | static void |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1333 | TimerHandler(clientData) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1334 | ClientData clientData; |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 1335 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1336 | PyObject *func = (PyObject *)clientData; |
| 1337 | PyObject *res = PyEval_CallObject(func, NULL); |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 1338 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1339 | if (res == NULL) { |
| 1340 | errorInCmd = 1; |
| 1341 | PyErr_Fetch(&excInCmd, &valInCmd, &trbInCmd); |
| 1342 | } |
| 1343 | else |
| 1344 | Py_DECREF(res); |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 1345 | } |
| 1346 | |
| 1347 | static PyObject * |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1348 | Tkapp_CreateTimerHandler(self, args) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1349 | PyObject *self; |
| 1350 | PyObject *args; /* Is (milliseconds, func) */ |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 1351 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1352 | int milliseconds; |
| 1353 | PyObject *func; |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1354 | Tcl_TimerToken token; |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 1355 | |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1356 | if (!PyArg_ParseTuple(args, "iO", &milliseconds, &func)) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1357 | return NULL; |
| 1358 | if (!PyCallable_Check(func)) { |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1359 | PyErr_SetString(PyExc_TypeError, "bad argument list"); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1360 | return NULL; |
| 1361 | } |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1362 | token = Tcl_CreateTimerHandler(milliseconds, TimerHandler, |
| 1363 | (ClientData)func); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1364 | |
| 1365 | return (PyObject *) Tktt_New(token, func); |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 1366 | } |
| 1367 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1368 | |
| 1369 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1370 | /** Event Loop **/ |
| 1371 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1372 | static PyObject * |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1373 | Tkapp_MainLoop(self, args) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1374 | PyObject *self; |
| 1375 | PyObject *args; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1376 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1377 | int threshold = 0; |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 1378 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1379 | if (!PyArg_ParseTuple(args, "|i", &threshold)) |
| 1380 | return NULL; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1381 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1382 | quitMainLoop = 0; |
| 1383 | while (Tk_GetNumMainWindows() > threshold && |
| 1384 | !quitMainLoop && |
| 1385 | !errorInCmd) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1386 | { |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1387 | int result; |
Guido van Rossum | 54e2091 | 1997-09-28 05:52:41 +0000 | [diff] [blame] | 1388 | #ifdef HAVE_PYTCL_WAITUNTILEVENT |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1389 | result = Tcl_DoOneEvent(TCL_DONT_WAIT); |
| 1390 | if (PyErr_CheckSignals() != 0) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1391 | return NULL; |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1392 | if (result) |
| 1393 | continue; |
| 1394 | /* XXX It's not *quite* certain that this is |
| 1395 | thread-safe, but it seems *rather* safe as long as |
| 1396 | no two threads call mainloop() simultaneously. */ |
| 1397 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 54e2091 | 1997-09-28 05:52:41 +0000 | [diff] [blame] | 1398 | result = PyTcl_WaitUntilEvent(); |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1399 | Py_END_ALLOW_THREADS |
Guido van Rossum | 5b02078 | 1997-08-19 01:00:50 +0000 | [diff] [blame] | 1400 | #else |
| 1401 | result = Tcl_DoOneEvent(0); |
| 1402 | #endif |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1403 | if (PyErr_CheckSignals() != 0) |
| 1404 | return NULL; |
| 1405 | if (result < 0) |
| 1406 | break; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1407 | } |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1408 | quitMainLoop = 0; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1409 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1410 | if (errorInCmd) { |
| 1411 | errorInCmd = 0; |
| 1412 | PyErr_Restore(excInCmd, valInCmd, trbInCmd); |
| 1413 | excInCmd = valInCmd = trbInCmd = NULL; |
| 1414 | return NULL; |
| 1415 | } |
| 1416 | Py_INCREF(Py_None); |
| 1417 | return Py_None; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1418 | } |
| 1419 | |
| 1420 | static PyObject * |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1421 | Tkapp_DoOneEvent(self, args) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1422 | PyObject *self; |
| 1423 | PyObject *args; |
Guido van Rossum | 062cfb0 | 1995-01-10 17:42:51 +0000 | [diff] [blame] | 1424 | { |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1425 | int flags = 0; |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1426 | int rv; |
Guido van Rossum | 062cfb0 | 1995-01-10 17:42:51 +0000 | [diff] [blame] | 1427 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1428 | if (!PyArg_ParseTuple(args, "|i", &flags)) |
| 1429 | return NULL; |
| 1430 | |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1431 | rv = Tcl_DoOneEvent(flags); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1432 | return Py_BuildValue("i", rv); |
Guido van Rossum | 062cfb0 | 1995-01-10 17:42:51 +0000 | [diff] [blame] | 1433 | } |
| 1434 | |
| 1435 | static PyObject * |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1436 | Tkapp_Quit(self, args) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1437 | PyObject *self; |
| 1438 | PyObject *args; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1439 | { |
| 1440 | |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1441 | if (!PyArg_ParseTuple(args, "")) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1442 | return NULL; |
| 1443 | |
| 1444 | quitMainLoop = 1; |
| 1445 | Py_INCREF(Py_None); |
| 1446 | return Py_None; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1447 | } |
| 1448 | |
Guido van Rossum | 9d1b7ae | 1998-04-29 16:17:01 +0000 | [diff] [blame] | 1449 | static PyObject * |
| 1450 | Tkapp_InterpAddr(self, args) |
| 1451 | PyObject *self; |
| 1452 | PyObject *args; |
| 1453 | { |
| 1454 | |
| 1455 | if (!PyArg_ParseTuple(args, "")) |
| 1456 | return NULL; |
| 1457 | |
| 1458 | return PyInt_FromLong((long)Tkapp_Interp(self)); |
| 1459 | } |
| 1460 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1461 | |
| 1462 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1463 | /**** Tkapp Method List ****/ |
| 1464 | |
| 1465 | static PyMethodDef Tkapp_methods[] = |
| 1466 | { |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1467 | {"call", Tkapp_Call, 0}, |
| 1468 | {"globalcall", Tkapp_GlobalCall, 0}, |
| 1469 | {"eval", Tkapp_Eval, 1}, |
| 1470 | {"globaleval", Tkapp_GlobalEval, 1}, |
| 1471 | {"evalfile", Tkapp_EvalFile, 1}, |
| 1472 | {"record", Tkapp_Record, 1}, |
| 1473 | {"adderrorinfo", Tkapp_AddErrorInfo, 1}, |
| 1474 | {"setvar", Tkapp_SetVar, 1}, |
| 1475 | {"globalsetvar", Tkapp_GlobalSetVar, 1}, |
| 1476 | {"getvar", Tkapp_GetVar, 1}, |
| 1477 | {"globalgetvar", Tkapp_GlobalGetVar, 1}, |
| 1478 | {"unsetvar", Tkapp_UnsetVar, 1}, |
| 1479 | {"globalunsetvar", Tkapp_GlobalUnsetVar, 1}, |
| 1480 | {"getint", Tkapp_GetInt, 1}, |
| 1481 | {"getdouble", Tkapp_GetDouble, 1}, |
| 1482 | {"getboolean", Tkapp_GetBoolean, 1}, |
| 1483 | {"exprstring", Tkapp_ExprString, 1}, |
| 1484 | {"exprlong", Tkapp_ExprLong, 1}, |
| 1485 | {"exprdouble", Tkapp_ExprDouble, 1}, |
| 1486 | {"exprboolean", Tkapp_ExprBoolean, 1}, |
| 1487 | {"splitlist", Tkapp_SplitList, 1}, |
| 1488 | {"split", Tkapp_Split, 1}, |
| 1489 | {"merge", Tkapp_Merge, 0}, |
| 1490 | {"createcommand", Tkapp_CreateCommand, 1}, |
| 1491 | {"deletecommand", Tkapp_DeleteCommand, 1}, |
Guido van Rossum | 0d2390c | 1997-08-14 19:57:07 +0000 | [diff] [blame] | 1492 | #ifdef HAVE_CREATEFILEHANDLER |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1493 | {"createfilehandler", Tkapp_CreateFileHandler, 1}, |
| 1494 | {"deletefilehandler", Tkapp_DeleteFileHandler, 1}, |
Guido van Rossum | 02c0467 | 1997-08-07 00:12:22 +0000 | [diff] [blame] | 1495 | #endif |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1496 | {"createtimerhandler", Tkapp_CreateTimerHandler, 1}, |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1497 | {"mainloop", Tkapp_MainLoop, 1}, |
| 1498 | {"dooneevent", Tkapp_DoOneEvent, 1}, |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1499 | {"quit", Tkapp_Quit, 1}, |
Guido van Rossum | 9d1b7ae | 1998-04-29 16:17:01 +0000 | [diff] [blame] | 1500 | {"interpaddr", Tkapp_InterpAddr, 1}, |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1501 | {NULL, NULL} |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1502 | }; |
| 1503 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1504 | |
| 1505 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1506 | /**** Tkapp Type Methods ****/ |
| 1507 | |
| 1508 | static void |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1509 | Tkapp_Dealloc(self) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1510 | PyObject *self; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1511 | { |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1512 | Tcl_DeleteInterp(Tkapp_Interp(self)); |
| 1513 | PyMem_DEL(self); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1514 | } |
| 1515 | |
| 1516 | static PyObject * |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1517 | Tkapp_GetAttr(self, name) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1518 | PyObject *self; |
| 1519 | char *name; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1520 | { |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1521 | return Py_FindMethod(Tkapp_methods, self, name); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1522 | } |
| 1523 | |
| 1524 | static PyTypeObject Tkapp_Type = |
| 1525 | { |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1526 | PyObject_HEAD_INIT(NULL) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1527 | 0, /*ob_size */ |
| 1528 | "tkapp", /*tp_name */ |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1529 | sizeof(TkappObject), /*tp_basicsize */ |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1530 | 0, /*tp_itemsize */ |
| 1531 | Tkapp_Dealloc, /*tp_dealloc */ |
| 1532 | 0, /*tp_print */ |
| 1533 | Tkapp_GetAttr, /*tp_getattr */ |
| 1534 | 0, /*tp_setattr */ |
| 1535 | 0, /*tp_compare */ |
| 1536 | 0, /*tp_repr */ |
| 1537 | 0, /*tp_as_number */ |
| 1538 | 0, /*tp_as_sequence */ |
| 1539 | 0, /*tp_as_mapping */ |
| 1540 | 0, /*tp_hash */ |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1541 | }; |
| 1542 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1543 | |
| 1544 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1545 | /**** Tkinter Module ****/ |
| 1546 | |
| 1547 | static PyObject * |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1548 | Tkinter_Create(self, args) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1549 | PyObject *self; |
| 1550 | PyObject *args; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1551 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1552 | char *screenName = NULL; |
| 1553 | char *baseName = NULL; |
| 1554 | char *className = NULL; |
| 1555 | int interactive = 0; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1556 | |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1557 | baseName = strrchr(Py_GetProgramName(), '/'); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1558 | if (baseName != NULL) |
| 1559 | baseName++; |
| 1560 | else |
| 1561 | baseName = Py_GetProgramName(); |
| 1562 | className = "Tk"; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1563 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1564 | if (!PyArg_ParseTuple(args, "|zssi", |
| 1565 | &screenName, &baseName, &className, |
| 1566 | &interactive)) |
| 1567 | return NULL; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1568 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1569 | return (PyObject *) Tkapp_New(screenName, baseName, className, |
| 1570 | interactive); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1571 | } |
| 1572 | |
| 1573 | static PyMethodDef moduleMethods[] = |
| 1574 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1575 | {"create", Tkinter_Create, 1}, |
Guido van Rossum | 0d2390c | 1997-08-14 19:57:07 +0000 | [diff] [blame] | 1576 | #ifdef HAVE_CREATEFILEHANDLER |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1577 | {"createfilehandler", Tkapp_CreateFileHandler, 1}, |
| 1578 | {"deletefilehandler", Tkapp_DeleteFileHandler, 1}, |
Guido van Rossum | 02c0467 | 1997-08-07 00:12:22 +0000 | [diff] [blame] | 1579 | #endif |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1580 | {"createtimerhandler", Tkapp_CreateTimerHandler, 1}, |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1581 | {"mainloop", Tkapp_MainLoop, 1}, |
| 1582 | {"dooneevent", Tkapp_DoOneEvent, 1}, |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1583 | {"quit", Tkapp_Quit, 1}, |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1584 | {NULL, NULL} |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1585 | }; |
| 1586 | |
Guido van Rossum | 0e8457c | 1997-10-07 18:51:41 +0000 | [diff] [blame] | 1587 | static PyInterpreterState *event_interp = NULL; |
| 1588 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1589 | static int |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1590 | EventHook() |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1591 | { |
Guido van Rossum | a59406a | 1997-10-10 17:39:19 +0000 | [diff] [blame] | 1592 | PyThreadState *tstate, *save_tstate; |
Guido van Rossum | 0e8457c | 1997-10-07 18:51:41 +0000 | [diff] [blame] | 1593 | |
| 1594 | if (Tk_GetNumMainWindows() == 0) |
| 1595 | return 0; |
| 1596 | if (event_interp == NULL) |
| 1597 | return 0; |
| 1598 | tstate = PyThreadState_New(event_interp); |
Guido van Rossum | a59406a | 1997-10-10 17:39:19 +0000 | [diff] [blame] | 1599 | save_tstate = PyThreadState_Swap(NULL); |
| 1600 | PyEval_RestoreThread(tstate); |
Guido van Rossum | 0e8457c | 1997-10-07 18:51:41 +0000 | [diff] [blame] | 1601 | if (!errorInCmd) |
| 1602 | Tcl_DoOneEvent(TCL_DONT_WAIT); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1603 | if (errorInCmd) { |
| 1604 | errorInCmd = 0; |
| 1605 | PyErr_Restore(excInCmd, valInCmd, trbInCmd); |
| 1606 | excInCmd = valInCmd = trbInCmd = NULL; |
| 1607 | PyErr_Print(); |
| 1608 | } |
Guido van Rossum | 0e8457c | 1997-10-07 18:51:41 +0000 | [diff] [blame] | 1609 | PyThreadState_Clear(tstate); |
Guido van Rossum | a59406a | 1997-10-10 17:39:19 +0000 | [diff] [blame] | 1610 | PyEval_SaveThread(); |
| 1611 | PyThreadState_Swap(save_tstate); |
Guido van Rossum | 0e8457c | 1997-10-07 18:51:41 +0000 | [diff] [blame] | 1612 | PyThreadState_Delete(tstate); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1613 | return 0; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1614 | } |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1615 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1616 | |
| 1617 | /* all errors will be checked in one fell swoop in init_tkinter() */ |
| 1618 | static void |
| 1619 | ins_long(d, name, val) |
| 1620 | PyObject *d; |
| 1621 | char *name; |
| 1622 | long val; |
| 1623 | { |
| 1624 | PyObject *v = PyInt_FromLong(val); |
| 1625 | if (v) { |
| 1626 | PyDict_SetItemString(d, name, v); |
| 1627 | Py_DECREF(v); |
| 1628 | } |
| 1629 | } |
| 1630 | static void |
| 1631 | ins_string(d, name, val) |
| 1632 | PyObject *d; |
| 1633 | char *name; |
| 1634 | char *val; |
| 1635 | { |
| 1636 | PyObject *v = PyString_FromString(val); |
| 1637 | if (v) { |
| 1638 | PyDict_SetItemString(d, name, v); |
| 1639 | Py_DECREF(v); |
| 1640 | } |
| 1641 | } |
| 1642 | |
| 1643 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1644 | void |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1645 | init_tkinter() |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1646 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1647 | PyObject *m, *d; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1648 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1649 | Tkapp_Type.ob_type = &PyType_Type; |
| 1650 | Tktt_Type.ob_type = &PyType_Type; |
Guido van Rossum | ae92f01 | 1996-08-21 19:03:36 +0000 | [diff] [blame] | 1651 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1652 | m = Py_InitModule("_tkinter", moduleMethods); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1653 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1654 | d = PyModule_GetDict(m); |
| 1655 | Tkinter_TclError = Py_BuildValue("s", "TclError"); |
| 1656 | PyDict_SetItemString(d, "TclError", Tkinter_TclError); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1657 | |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1658 | ins_long(d, "READABLE", TCL_READABLE); |
| 1659 | ins_long(d, "WRITABLE", TCL_WRITABLE); |
| 1660 | ins_long(d, "EXCEPTION", TCL_EXCEPTION); |
| 1661 | ins_long(d, "WINDOW_EVENTS", TCL_WINDOW_EVENTS); |
| 1662 | ins_long(d, "FILE_EVENTS", TCL_FILE_EVENTS); |
| 1663 | ins_long(d, "TIMER_EVENTS", TCL_TIMER_EVENTS); |
| 1664 | ins_long(d, "IDLE_EVENTS", TCL_IDLE_EVENTS); |
| 1665 | ins_long(d, "ALL_EVENTS", TCL_ALL_EVENTS); |
| 1666 | ins_long(d, "DONT_WAIT", TCL_DONT_WAIT); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1667 | ins_string(d, "TK_VERSION", TK_VERSION); |
| 1668 | ins_string(d, "TCL_VERSION", TCL_VERSION); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1669 | |
Guido van Rossum | 83551bf | 1997-09-13 00:44:23 +0000 | [diff] [blame] | 1670 | PyDict_SetItemString(d, "TkappType", (PyObject *)&Tkapp_Type); |
| 1671 | PyDict_SetItemString(d, "TkttType", (PyObject *)&Tktt_Type); |
| 1672 | |
Guido van Rossum | 0e8457c | 1997-10-07 18:51:41 +0000 | [diff] [blame] | 1673 | if (PyOS_InputHook == NULL) { |
Guido van Rossum | 0e8457c | 1997-10-07 18:51:41 +0000 | [diff] [blame] | 1674 | event_interp = PyThreadState_Get()->interp; |
Guido van Rossum | 4462064 | 1997-08-11 18:57:29 +0000 | [diff] [blame] | 1675 | PyOS_InputHook = EventHook; |
Guido van Rossum | 0e8457c | 1997-10-07 18:51:41 +0000 | [diff] [blame] | 1676 | } |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1677 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1678 | if (PyErr_Occurred()) |
Guido van Rossum | 07e9fbf | 1998-02-06 22:35:46 +0000 | [diff] [blame] | 1679 | return; |
| 1680 | |
Guido van Rossum | 2621637 | 1998-04-20 18:47:52 +0000 | [diff] [blame] | 1681 | #if TKMAJORMINOR >= 8000 |
Guido van Rossum | 07e9fbf | 1998-02-06 22:35:46 +0000 | [diff] [blame] | 1682 | Py_AtExit(Tcl_Finalize); |
Guido van Rossum | 2621637 | 1998-04-20 18:47:52 +0000 | [diff] [blame] | 1683 | #endif |
Guido van Rossum | 07e9fbf | 1998-02-06 22:35:46 +0000 | [diff] [blame] | 1684 | |
Jack Jansen | 34cc5c3 | 1995-10-31 16:15:12 +0000 | [diff] [blame] | 1685 | #ifdef macintosh |
Guido van Rossum | 0d2390c | 1997-08-14 19:57:07 +0000 | [diff] [blame] | 1686 | /* |
| 1687 | ** Part of this code is stolen from MacintoshInit in tkMacAppInit. |
| 1688 | ** Most of the initializations in that routine (toolbox init calls and |
| 1689 | ** such) have already been done for us, so we only need these. |
| 1690 | */ |
| 1691 | #if TKMAJORMINOR >= 8000 |
| 1692 | tcl_macQdPtr = &qd; |
| 1693 | #endif |
| 1694 | |
| 1695 | Tcl_MacSetEventProc(PyMacConvertEvent); |
Guido van Rossum | ec22c92 | 1996-02-25 04:50:29 +0000 | [diff] [blame] | 1696 | #if GENERATINGCFM |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1697 | mac_addlibresources(); |
Guido van Rossum | ec22c92 | 1996-02-25 04:50:29 +0000 | [diff] [blame] | 1698 | #endif /* GENERATINGCFM */ |
| 1699 | #endif /* macintosh */ |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1700 | } |
Guido van Rossum | 9722ad8 | 1995-09-22 23:49:28 +0000 | [diff] [blame] | 1701 | |
Guido van Rossum | ec22c92 | 1996-02-25 04:50:29 +0000 | [diff] [blame] | 1702 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1703 | |
Guido van Rossum | 9722ad8 | 1995-09-22 23:49:28 +0000 | [diff] [blame] | 1704 | #ifdef macintosh |
Guido van Rossum | dfd428d | 1996-02-25 04:46:40 +0000 | [diff] [blame] | 1705 | |
| 1706 | /* |
Guido van Rossum | ec22c92 | 1996-02-25 04:50:29 +0000 | [diff] [blame] | 1707 | ** Anyone who embeds Tcl/Tk on the Mac must define panic(). |
Guido van Rossum | dfd428d | 1996-02-25 04:46:40 +0000 | [diff] [blame] | 1708 | */ |
| 1709 | |
Guido van Rossum | 9722ad8 | 1995-09-22 23:49:28 +0000 | [diff] [blame] | 1710 | void |
| 1711 | panic(char * format, ...) |
| 1712 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1713 | va_list varg; |
Guido van Rossum | 9722ad8 | 1995-09-22 23:49:28 +0000 | [diff] [blame] | 1714 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1715 | va_start(varg, format); |
Guido van Rossum | 9722ad8 | 1995-09-22 23:49:28 +0000 | [diff] [blame] | 1716 | |
Guido van Rossum | b41addf | 1998-05-12 15:02:41 +0000 | [diff] [blame^] | 1717 | vPySys_WriteStderr(format, varg); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1718 | (void) fflush(stderr); |
Guido van Rossum | 9722ad8 | 1995-09-22 23:49:28 +0000 | [diff] [blame] | 1719 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1720 | va_end(varg); |
Guido van Rossum | 9722ad8 | 1995-09-22 23:49:28 +0000 | [diff] [blame] | 1721 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1722 | Py_FatalError("Tcl/Tk panic"); |
Guido van Rossum | 9722ad8 | 1995-09-22 23:49:28 +0000 | [diff] [blame] | 1723 | } |
Jack Jansen | 40b546d | 1995-11-14 10:34:45 +0000 | [diff] [blame] | 1724 | |
Guido van Rossum | ec22c92 | 1996-02-25 04:50:29 +0000 | [diff] [blame] | 1725 | /* |
| 1726 | ** Pass events to SIOUX before passing them to Tk. |
| 1727 | */ |
Guido van Rossum | dfd428d | 1996-02-25 04:46:40 +0000 | [diff] [blame] | 1728 | |
Guido van Rossum | ec22c92 | 1996-02-25 04:50:29 +0000 | [diff] [blame] | 1729 | static int |
| 1730 | PyMacConvertEvent(eventPtr) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1731 | EventRecord *eventPtr; |
Guido van Rossum | dfd428d | 1996-02-25 04:46:40 +0000 | [diff] [blame] | 1732 | { |
Guido van Rossum | 2ea1c94 | 1998-04-28 16:12:43 +0000 | [diff] [blame] | 1733 | WindowPtr frontwin; |
| 1734 | /* |
| 1735 | ** Sioux eats too many events, so we don't pass it everything. |
| 1736 | ** We always pass update events to Sioux, and we only pass other events if |
| 1737 | ** the Sioux window is frontmost. This means that Tk menus don't work |
| 1738 | ** in that case, but at least we can scroll the sioux window. |
| 1739 | ** Note that the SIOUXIsAppWindow() routine we use here is not really |
| 1740 | ** part of the external interface of Sioux... |
| 1741 | */ |
| 1742 | frontwin = FrontWindow(); |
| 1743 | if ( eventPtr->what == updateEvt || SIOUXIsAppWindow(frontwin) ) { |
| 1744 | if (SIOUXHandleOneEvent(eventPtr)) |
| 1745 | return 0; /* Nothing happened to the Tcl event queue */ |
| 1746 | } |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1747 | return TkMacConvertEvent(eventPtr); |
Guido van Rossum | dfd428d | 1996-02-25 04:46:40 +0000 | [diff] [blame] | 1748 | } |
| 1749 | |
Guido van Rossum | 0d2390c | 1997-08-14 19:57:07 +0000 | [diff] [blame] | 1750 | #if defined(USE_GUSI) && TKMAJORMINOR < 8000 |
Guido van Rossum | 290283b | 1997-06-02 22:16:43 +0000 | [diff] [blame] | 1751 | /* |
| 1752 | * For Python we have to override this routine (from TclMacNotify), |
| 1753 | * since we use GUSI for our sockets, not Tcl streams. Hence, we have |
| 1754 | * to use GUSI select to see whether our socket is ready. Note that |
| 1755 | * createfilehandler (above) sets the type to TCL_UNIX_FD for our |
| 1756 | * files and sockets. |
| 1757 | * |
| 1758 | * NOTE: this code was lifted from Tcl 7.6, it may need to be modified |
| 1759 | * for other versions. */ |
| 1760 | |
| 1761 | int |
| 1762 | Tcl_FileReady(file, mask) |
| 1763 | Tcl_File file; /* File handle for a stream. */ |
| 1764 | int mask; /* OR'ed combination of TCL_READABLE, |
| 1765 | * TCL_WRITABLE, and TCL_EXCEPTION: |
| 1766 | * indicates conditions caller cares about. */ |
| 1767 | { |
| 1768 | int type; |
| 1769 | int fd; |
| 1770 | |
| 1771 | fd = (int) Tcl_GetFileInfo(file, &type); |
| 1772 | |
| 1773 | if (type == TCL_MAC_SOCKET) { |
| 1774 | return TclMacSocketReady(file, mask); |
| 1775 | } else if (type == TCL_MAC_FILE) { |
| 1776 | /* |
| 1777 | * Under the Macintosh, files are always ready, so we just |
| 1778 | * return the mask that was passed in. |
| 1779 | */ |
| 1780 | |
| 1781 | return mask; |
| 1782 | } else if (type == TCL_UNIX_FD) { |
| 1783 | fd_set readset, writeset, excset; |
| 1784 | struct timeval tv; |
| 1785 | |
| 1786 | FD_ZERO(&readset); |
| 1787 | FD_ZERO(&writeset); |
| 1788 | FD_ZERO(&excset); |
| 1789 | |
| 1790 | if ( mask & TCL_READABLE ) FD_SET(fd, &readset); |
| 1791 | if ( mask & TCL_WRITABLE ) FD_SET(fd, &writeset); |
| 1792 | if ( mask & TCL_EXCEPTION ) FD_SET(fd, &excset); |
| 1793 | |
| 1794 | tv.tv_sec = tv.tv_usec = 0; |
| 1795 | if ( select(fd+1, &readset, &writeset, &excset, &tv) <= 0 ) |
| 1796 | return 0; |
| 1797 | |
| 1798 | mask = 0; |
| 1799 | if ( FD_ISSET(fd, &readset) ) mask |= TCL_READABLE; |
| 1800 | if ( FD_ISSET(fd, &writeset) ) mask |= TCL_WRITABLE; |
| 1801 | if ( FD_ISSET(fd, &excset) ) mask |= TCL_EXCEPTION; |
| 1802 | |
| 1803 | return mask; |
| 1804 | } |
| 1805 | |
| 1806 | return 0; |
| 1807 | } |
| 1808 | #endif /* USE_GUSI */ |
| 1809 | |
Guido van Rossum | ec22c92 | 1996-02-25 04:50:29 +0000 | [diff] [blame] | 1810 | #if GENERATINGCFM |
Guido van Rossum | dfd428d | 1996-02-25 04:46:40 +0000 | [diff] [blame] | 1811 | |
| 1812 | /* |
| 1813 | ** Additional Mac specific code for dealing with shared libraries. |
| 1814 | */ |
| 1815 | |
| 1816 | #include <Resources.h> |
| 1817 | #include <CodeFragments.h> |
| 1818 | |
| 1819 | static int loaded_from_shlib = 0; |
| 1820 | static FSSpec library_fss; |
Guido van Rossum | 9722ad8 | 1995-09-22 23:49:28 +0000 | [diff] [blame] | 1821 | |
Jack Jansen | 34cc5c3 | 1995-10-31 16:15:12 +0000 | [diff] [blame] | 1822 | /* |
| 1823 | ** If this module is dynamically loaded the following routine should |
| 1824 | ** be the init routine. It takes care of adding the shared library to |
| 1825 | ** the resource-file chain, so that the tk routines can find their |
| 1826 | ** resources. |
| 1827 | */ |
| 1828 | OSErr pascal |
Guido van Rossum | 8ec9e63 | 1997-04-29 15:49:04 +0000 | [diff] [blame] | 1829 | init_tkinter_shlib(CFragInitBlockPtr data) |
Jack Jansen | 34cc5c3 | 1995-10-31 16:15:12 +0000 | [diff] [blame] | 1830 | { |
Guido van Rossum | c9970ee | 1996-08-26 14:37:15 +0000 | [diff] [blame] | 1831 | __initialize(); |
Jack Jansen | 34cc5c3 | 1995-10-31 16:15:12 +0000 | [diff] [blame] | 1832 | if ( data == nil ) return noErr; |
Guido van Rossum | 8ec9e63 | 1997-04-29 15:49:04 +0000 | [diff] [blame] | 1833 | if ( data->fragLocator.where == kDataForkCFragLocator ) { |
Jack Jansen | 34cc5c3 | 1995-10-31 16:15:12 +0000 | [diff] [blame] | 1834 | library_fss = *data->fragLocator.u.onDisk.fileSpec; |
| 1835 | loaded_from_shlib = 1; |
Guido van Rossum | 8ec9e63 | 1997-04-29 15:49:04 +0000 | [diff] [blame] | 1836 | } else if ( data->fragLocator.where == kResourceCFragLocator ) { |
Jack Jansen | 34cc5c3 | 1995-10-31 16:15:12 +0000 | [diff] [blame] | 1837 | library_fss = *data->fragLocator.u.inSegs.fileSpec; |
| 1838 | loaded_from_shlib = 1; |
| 1839 | } |
| 1840 | return noErr; |
| 1841 | } |
| 1842 | |
| 1843 | /* |
| 1844 | ** Insert the library resources into the search path. Put them after |
| 1845 | ** the resources from the application. Again, we ignore errors. |
| 1846 | */ |
Guido van Rossum | dfd428d | 1996-02-25 04:46:40 +0000 | [diff] [blame] | 1847 | static |
Jack Jansen | 34cc5c3 | 1995-10-31 16:15:12 +0000 | [diff] [blame] | 1848 | mac_addlibresources() |
| 1849 | { |
| 1850 | if ( !loaded_from_shlib ) |
| 1851 | return; |
| 1852 | (void)FSpOpenResFile(&library_fss, fsRdPerm); |
| 1853 | } |
| 1854 | |
Guido van Rossum | ec22c92 | 1996-02-25 04:50:29 +0000 | [diff] [blame] | 1855 | #endif /* GENERATINGCFM */ |
| 1856 | #endif /* macintosh */ |