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: |
| 38 | This should work with any version from Tcl 4.0 / Tck 7.4. |
| 39 | Do not use older versions. |
| 40 | |
| 41 | Mac and Windows: |
| 42 | Use Tcl 4.1p1 / Tk 7.5p1 or possibly newer. |
| 43 | It does not seem to work reliably with the original 4.1/7.5 |
| 44 | release. (4.0/7.4 were never released for these platforms.) |
| 45 | */ |
| 46 | |
Guido van Rossum | 9722ad8 | 1995-09-22 23:49:28 +0000 | [diff] [blame] | 47 | #include "Python.h" |
Guido van Rossum | 97867b2 | 1996-08-08 19:09:53 +0000 | [diff] [blame] | 48 | #include <ctype.h> |
Guido van Rossum | 9722ad8 | 1995-09-22 23:49:28 +0000 | [diff] [blame] | 49 | |
Guido van Rossum | bf0dc9f | 1996-08-20 20:49:56 +0000 | [diff] [blame] | 50 | #ifdef macintosh |
| 51 | #define MAC_TCL |
| 52 | #endif |
| 53 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 54 | #include <tcl.h> |
| 55 | #include <tk.h> |
| 56 | |
Guido van Rossum | 32aa1a7 | 1996-07-31 19:51:15 +0000 | [diff] [blame] | 57 | extern char *Py_GetProgramName (); |
Guido van Rossum | d308e2b | 1994-07-07 09:25:12 +0000 | [diff] [blame] | 58 | |
Guido van Rossum | 9722ad8 | 1995-09-22 23:49:28 +0000 | [diff] [blame] | 59 | /* Internal declarations from tkInt.h. */ |
| 60 | #if (TK_MAJOR_VERSION*1000 + TK_MINOR_VERSION) >= 4001 |
| 61 | extern int Tk_GetNumMainWindows(); |
| 62 | #else |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 63 | extern int tk_NumMainWindows; |
Guido van Rossum | 9722ad8 | 1995-09-22 23:49:28 +0000 | [diff] [blame] | 64 | #define Tk_GetNumMainWindows() (tk_NumMainWindows) |
Guido van Rossum | dfd428d | 1996-02-25 04:46:40 +0000 | [diff] [blame] | 65 | #define NEED_TKCREATEMAINWINDOW 1 |
Guido van Rossum | 9722ad8 | 1995-09-22 23:49:28 +0000 | [diff] [blame] | 66 | #endif |
| 67 | |
| 68 | #if TK_MAJOR_VERSION < 4 |
Guido van Rossum | d308e2b | 1994-07-07 09:25:12 +0000 | [diff] [blame] | 69 | extern struct { Tk_Window win; } *tkMainWindowList; |
Guido van Rossum | 9722ad8 | 1995-09-22 23:49:28 +0000 | [diff] [blame] | 70 | #endif |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 71 | |
Guido van Rossum | ec22c92 | 1996-02-25 04:50:29 +0000 | [diff] [blame] | 72 | #ifdef macintosh |
| 73 | |
| 74 | /* |
| 75 | ** Additional cruft needed by Tcl/Tk on the Mac. |
Guido van Rossum | 97867b2 | 1996-08-08 19:09:53 +0000 | [diff] [blame] | 76 | ** 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] | 77 | */ |
| 78 | |
Guido van Rossum | 7ffa761 | 1996-08-13 21:10:16 +0000 | [diff] [blame] | 79 | /* ckfree() expects a char* */ |
Guido van Rossum | 97867b2 | 1996-08-08 19:09:53 +0000 | [diff] [blame] | 80 | #define FREECAST (char *) |
| 81 | |
Guido van Rossum | ec22c92 | 1996-02-25 04:50:29 +0000 | [diff] [blame] | 82 | #include <Events.h> /* For EventRecord */ |
| 83 | |
| 84 | typedef int (*TclMacConvertEventPtr) Py_PROTO((EventRecord *eventPtr)); |
| 85 | void TclMacSetEventProc Py_PROTO((TclMacConvertEventPtr procPtr)); |
| 86 | int TkMacConvertEvent Py_PROTO((EventRecord *eventPtr)); |
| 87 | |
| 88 | staticforward int PyMacConvertEvent Py_PROTO((EventRecord *eventPtr)); |
| 89 | |
| 90 | #endif /* macintosh */ |
| 91 | |
Guido van Rossum | 97867b2 | 1996-08-08 19:09:53 +0000 | [diff] [blame] | 92 | #ifndef FREECAST |
Guido van Rossum | 7ffa761 | 1996-08-13 21:10:16 +0000 | [diff] [blame] | 93 | #define FREECAST (char *) |
Guido van Rossum | 97867b2 | 1996-08-08 19:09:53 +0000 | [diff] [blame] | 94 | #endif |
| 95 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 96 | /**** Tkapp Object Declaration ****/ |
| 97 | |
| 98 | staticforward PyTypeObject Tkapp_Type; |
| 99 | |
| 100 | typedef struct |
| 101 | { |
| 102 | PyObject_HEAD |
| 103 | Tcl_Interp *interp; |
Guido van Rossum | dfd428d | 1996-02-25 04:46:40 +0000 | [diff] [blame] | 104 | #ifdef NEED_TKCREATEMAINWINDOW |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 105 | Tk_Window tkwin; |
Guido van Rossum | dfd428d | 1996-02-25 04:46:40 +0000 | [diff] [blame] | 106 | #endif |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 107 | } |
| 108 | TkappObject; |
| 109 | |
| 110 | #define Tkapp_Check(v) ((v)->ob_type == &Tkapp_Type) |
Guido van Rossum | dfd428d | 1996-02-25 04:46:40 +0000 | [diff] [blame] | 111 | #ifdef NEED_TKCREATEMAINWINDOW |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 112 | #define Tkapp_Tkwin(v) (((TkappObject *) (v))->tkwin) |
Guido van Rossum | dfd428d | 1996-02-25 04:46:40 +0000 | [diff] [blame] | 113 | #endif |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 114 | #define Tkapp_Interp(v) (((TkappObject *) (v))->interp) |
| 115 | #define Tkapp_Result(v) (((TkappObject *) (v))->interp->result) |
| 116 | |
| 117 | #define DEBUG_REFCNT(v) (printf ("DEBUG: id=%p, refcnt=%i\n", \ |
| 118 | (void *) v, ((PyObject *) v)->ob_refcnt)) |
| 119 | |
| 120 | /**** Error Handling ****/ |
| 121 | |
| 122 | static PyObject *Tkinter_TclError; |
Guido van Rossum | d308e2b | 1994-07-07 09:25:12 +0000 | [diff] [blame] | 123 | static int quitMainLoop = 0; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 124 | static int errorInCmd = 0; |
| 125 | static PyObject *excInCmd; |
| 126 | static PyObject *valInCmd; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 127 | static PyObject *trbInCmd; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 128 | |
| 129 | static PyObject * |
| 130 | Tkinter_Error (v) |
| 131 | PyObject *v; |
| 132 | { |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 133 | PyErr_SetString (Tkinter_TclError, Tkapp_Result (v)); |
| 134 | return NULL; |
| 135 | } |
| 136 | |
| 137 | int |
| 138 | PythonCmd_Error (interp) |
| 139 | Tcl_Interp *interp; |
| 140 | { |
| 141 | errorInCmd = 1; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 142 | PyErr_Fetch (&excInCmd, &valInCmd, &trbInCmd); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 143 | return TCL_ERROR; |
| 144 | } |
| 145 | |
| 146 | /**** Utils ****/ |
| 147 | |
| 148 | static char * |
| 149 | AsString (value, tmp) |
| 150 | PyObject *value; |
| 151 | PyObject *tmp; |
| 152 | { |
| 153 | if (PyString_Check (value)) |
| 154 | return PyString_AsString (value); |
| 155 | else |
| 156 | { |
| 157 | PyObject *v; |
| 158 | |
Guido van Rossum | cd938fc | 1995-01-17 16:13:48 +0000 | [diff] [blame] | 159 | v = PyObject_Str (value); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 160 | PyList_Append (tmp, v); |
| 161 | Py_DECREF (v); |
| 162 | return PyString_AsString (v); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | #define ARGSZ 64 |
| 167 | |
| 168 | static char * |
| 169 | Merge (args) |
| 170 | PyObject *args; |
| 171 | { |
| 172 | PyObject *tmp; |
| 173 | char *argvStore[ARGSZ]; |
| 174 | char **argv; |
| 175 | int fvStore[ARGSZ]; |
| 176 | int *fv; |
| 177 | int argc; |
| 178 | char *res; |
| 179 | int i; |
| 180 | |
| 181 | tmp = PyList_New (0); |
| 182 | argv = argvStore; |
| 183 | fv = fvStore; |
| 184 | |
Guido van Rossum | 845547d | 1996-06-26 18:26:04 +0000 | [diff] [blame] | 185 | if (args == NULL) |
| 186 | { |
| 187 | argc = 0; |
| 188 | } |
| 189 | else if (!PyTuple_Check (args)) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 190 | { |
| 191 | argc = 1; |
| 192 | fv[0] = 0; |
| 193 | argv[0] = AsString (args, tmp); |
| 194 | } |
| 195 | else |
| 196 | { |
| 197 | PyObject *v; |
| 198 | |
| 199 | if (PyTuple_Size (args) > ARGSZ) |
| 200 | { |
Guido van Rossum | 7ffa761 | 1996-08-13 21:10:16 +0000 | [diff] [blame] | 201 | argv = (char **) ckalloc (PyTuple_Size (args) * sizeof (char *)); |
| 202 | fv = (int *) ckalloc (PyTuple_Size (args) * sizeof (int)); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 203 | if (argv == NULL || fv == NULL) |
| 204 | PyErr_NoMemory (); |
| 205 | } |
| 206 | |
| 207 | argc = PyTuple_Size (args); |
| 208 | for (i = 0; i < argc; i++) |
| 209 | { |
| 210 | v = PyTuple_GetItem (args, i); |
| 211 | if (PyTuple_Check (v)) |
| 212 | { |
| 213 | fv[i] = 1; |
| 214 | argv[i] = Merge (v); |
| 215 | } |
| 216 | else if (v == Py_None) |
| 217 | { |
| 218 | argc = i; |
| 219 | break; |
| 220 | } |
| 221 | else |
| 222 | { |
| 223 | fv[i] = 0; |
| 224 | argv[i] = AsString (v, tmp); |
| 225 | } |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | res = Tcl_Merge (argc, argv); |
| 230 | |
| 231 | Py_DECREF (tmp); |
| 232 | for (i = 0; i < argc; i++) |
Guido van Rossum | 7ffa761 | 1996-08-13 21:10:16 +0000 | [diff] [blame] | 233 | if (fv[i]) ckfree (argv[i]); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 234 | if (argv != argvStore) |
Guido van Rossum | 7ffa761 | 1996-08-13 21:10:16 +0000 | [diff] [blame] | 235 | ckfree (FREECAST argv); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 236 | if (fv != fvStore) |
Guido van Rossum | 7ffa761 | 1996-08-13 21:10:16 +0000 | [diff] [blame] | 237 | ckfree (FREECAST fv); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 238 | |
| 239 | return res; |
| 240 | } |
| 241 | |
| 242 | static PyObject * |
| 243 | Split (self, list) |
| 244 | PyObject *self; |
| 245 | char *list; |
| 246 | { |
| 247 | int argc; |
| 248 | char **argv; |
| 249 | PyObject *v; |
| 250 | |
| 251 | if (list == NULL) |
| 252 | { |
| 253 | Py_INCREF (Py_None); |
| 254 | return Py_None; |
| 255 | } |
| 256 | |
| 257 | if (Tcl_SplitList (Tkapp_Interp (self), list, &argc, &argv) == TCL_ERROR) |
| 258 | return Tkinter_Error (self); |
| 259 | |
| 260 | if (argc == 0) |
| 261 | v = PyString_FromString (""); |
| 262 | else if (argc == 1) |
| 263 | v = PyString_FromString (argv[0]); |
Guido van Rossum | 0f86837 | 1996-12-17 01:02:18 +0000 | [diff] [blame^] | 264 | else if ((v = PyTuple_New (argc)) != NULL) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 265 | { |
| 266 | int i; |
Guido van Rossum | 0f86837 | 1996-12-17 01:02:18 +0000 | [diff] [blame^] | 267 | PyObject *w; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 268 | |
Guido van Rossum | 0f86837 | 1996-12-17 01:02:18 +0000 | [diff] [blame^] | 269 | for (i = 0; i < argc; i++) { |
| 270 | if ((w = Split (self, argv[i])) == NULL) |
| 271 | { |
| 272 | Py_DECREF(v); |
| 273 | v = NULL; |
| 274 | break; |
| 275 | } |
| 276 | PyTuple_SetItem (v, i, w); |
| 277 | } |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 278 | } |
| 279 | |
Guido van Rossum | 7ffa761 | 1996-08-13 21:10:16 +0000 | [diff] [blame] | 280 | ckfree (FREECAST argv); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 281 | return v; |
| 282 | } |
| 283 | |
| 284 | /**** Tkapp Object ****/ |
| 285 | |
| 286 | #ifndef WITH_APPINIT |
| 287 | int |
| 288 | Tcl_AppInit (interp) |
| 289 | Tcl_Interp *interp; |
| 290 | { |
Guido van Rossum | ec22c92 | 1996-02-25 04:50:29 +0000 | [diff] [blame] | 291 | Tk_Window main; |
| 292 | |
| 293 | main = Tk_MainWindow(interp); |
Guido van Rossum | a3c3f2c | 1995-02-07 15:41:02 +0000 | [diff] [blame] | 294 | if (Tcl_Init (interp) == TCL_ERROR) { |
| 295 | fprintf(stderr, "Tcl_Init error: %s\n", interp->result); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 296 | return TCL_ERROR; |
Guido van Rossum | a3c3f2c | 1995-02-07 15:41:02 +0000 | [diff] [blame] | 297 | } |
| 298 | if (Tk_Init (interp) == TCL_ERROR) { |
| 299 | fprintf(stderr, "Tk_Init error: %s\n", interp->result); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 300 | return TCL_ERROR; |
Guido van Rossum | a3c3f2c | 1995-02-07 15:41:02 +0000 | [diff] [blame] | 301 | } |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 302 | return TCL_OK; |
| 303 | } |
Guido van Rossum | dfd428d | 1996-02-25 04:46:40 +0000 | [diff] [blame] | 304 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 305 | #endif /* !WITH_APPINIT */ |
| 306 | |
| 307 | /* Initialize the Tk application; see the `main' function in |
| 308 | `tkMain.c'. */ |
| 309 | static TkappObject * |
| 310 | Tkapp_New (screenName, baseName, className, interactive) |
| 311 | char *screenName; |
| 312 | char *baseName; |
| 313 | char *className; |
| 314 | int interactive; |
| 315 | { |
| 316 | TkappObject *v; |
Guido van Rossum | 97867b2 | 1996-08-08 19:09:53 +0000 | [diff] [blame] | 317 | char *argv0; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 318 | |
| 319 | v = PyObject_NEW (TkappObject, &Tkapp_Type); |
| 320 | if (v == NULL) |
| 321 | return NULL; |
| 322 | |
| 323 | v->interp = Tcl_CreateInterp (); |
Guido van Rossum | dfd428d | 1996-02-25 04:46:40 +0000 | [diff] [blame] | 324 | |
| 325 | #ifdef NEED_TKCREATEMAINWINDOW |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 326 | v->tkwin = Tk_CreateMainWindow (v->interp, screenName, |
| 327 | baseName, className); |
| 328 | if (v->tkwin == NULL) |
| 329 | return (TkappObject *) Tkinter_Error ((PyObject *) v); |
| 330 | |
| 331 | Tk_GeometryRequest (v->tkwin, 200, 200); |
Guido van Rossum | dfd428d | 1996-02-25 04:46:40 +0000 | [diff] [blame] | 332 | #endif |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 333 | |
| 334 | if (screenName != NULL) |
| 335 | Tcl_SetVar2 (v->interp, "env", "DISPLAY", screenName, TCL_GLOBAL_ONLY); |
| 336 | |
| 337 | if (interactive) |
| 338 | Tcl_SetVar (v->interp, "tcl_interactive", "1", TCL_GLOBAL_ONLY); |
| 339 | else |
| 340 | Tcl_SetVar (v->interp, "tcl_interactive", "0", TCL_GLOBAL_ONLY); |
| 341 | |
Guido van Rossum | 97867b2 | 1996-08-08 19:09:53 +0000 | [diff] [blame] | 342 | /* This is used to get the application class for Tk 4.1 and up */ |
Guido van Rossum | 7ffa761 | 1996-08-13 21:10:16 +0000 | [diff] [blame] | 343 | argv0 = (char*) ckalloc (strlen (className) + 1); |
Guido van Rossum | 97867b2 | 1996-08-08 19:09:53 +0000 | [diff] [blame] | 344 | if (argv0 != NULL) { |
| 345 | strcpy (argv0, className); |
| 346 | if (isupper (argv0[0])) |
| 347 | argv0[0] = tolower (argv0[0]); |
| 348 | Tcl_SetVar (v->interp, "argv0", argv0, TCL_GLOBAL_ONLY); |
Guido van Rossum | 7ffa761 | 1996-08-13 21:10:16 +0000 | [diff] [blame] | 349 | ckfree (argv0); |
Guido van Rossum | 97867b2 | 1996-08-08 19:09:53 +0000 | [diff] [blame] | 350 | } |
| 351 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 352 | if (Tcl_AppInit (v->interp) != TCL_OK) |
Guido van Rossum | 845547d | 1996-06-26 18:26:04 +0000 | [diff] [blame] | 353 | return (TkappObject *) Tkinter_Error (v); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 354 | |
| 355 | return v; |
| 356 | } |
| 357 | |
| 358 | /** Tcl Eval **/ |
| 359 | |
| 360 | static PyObject * |
| 361 | Tkapp_Call (self, args) |
| 362 | PyObject *self; |
| 363 | PyObject *args; |
| 364 | { |
| 365 | char *cmd; |
| 366 | |
| 367 | cmd = Merge (args); |
| 368 | if (Tcl_Eval (Tkapp_Interp (self), cmd) == TCL_ERROR) |
| 369 | { |
Guido van Rossum | 7ffa761 | 1996-08-13 21:10:16 +0000 | [diff] [blame] | 370 | ckfree (cmd); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 371 | return Tkinter_Error (self); |
| 372 | } |
| 373 | |
Guido van Rossum | 7ffa761 | 1996-08-13 21:10:16 +0000 | [diff] [blame] | 374 | ckfree (cmd); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 375 | return PyString_FromString (Tkapp_Result (self)); |
| 376 | } |
| 377 | |
| 378 | static PyObject * |
| 379 | Tkapp_GlobalCall (self, args) |
| 380 | PyObject *self; |
| 381 | PyObject *args; |
| 382 | { |
| 383 | char *cmd; |
| 384 | |
| 385 | cmd = Merge (args); |
| 386 | if (Tcl_GlobalEval (Tkapp_Interp (self), cmd) == TCL_ERROR) |
| 387 | { |
Guido van Rossum | 7ffa761 | 1996-08-13 21:10:16 +0000 | [diff] [blame] | 388 | ckfree (cmd); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 389 | return Tkinter_Error (self); |
| 390 | } |
| 391 | |
Guido van Rossum | 7ffa761 | 1996-08-13 21:10:16 +0000 | [diff] [blame] | 392 | ckfree (cmd); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 393 | return PyString_FromString (Tkapp_Result (self)); |
| 394 | } |
| 395 | |
| 396 | static PyObject * |
| 397 | Tkapp_Eval (self, args) |
| 398 | PyObject *self; |
| 399 | PyObject *args; |
| 400 | { |
| 401 | char *script; |
| 402 | |
| 403 | if (!PyArg_Parse (args, "s", &script)) |
| 404 | return NULL; |
| 405 | |
| 406 | if (Tcl_Eval (Tkapp_Interp (self), script) == TCL_ERROR) |
| 407 | return Tkinter_Error (self); |
| 408 | |
| 409 | return PyString_FromString (Tkapp_Result (self)); |
| 410 | } |
| 411 | |
| 412 | static PyObject * |
| 413 | Tkapp_GlobalEval (self, args) |
| 414 | PyObject *self; |
| 415 | PyObject *args; |
| 416 | { |
| 417 | char *script; |
| 418 | |
| 419 | if (!PyArg_Parse (args, "s", &script)) |
| 420 | return NULL; |
| 421 | |
| 422 | if (Tcl_GlobalEval (Tkapp_Interp (self), script) == TCL_ERROR) |
| 423 | return Tkinter_Error (self); |
| 424 | |
| 425 | return PyString_FromString (Tkapp_Result (self)); |
| 426 | } |
| 427 | |
| 428 | static PyObject * |
| 429 | Tkapp_EvalFile (self, args) |
| 430 | PyObject *self; |
| 431 | PyObject *args; |
| 432 | { |
| 433 | char *fileName; |
| 434 | |
| 435 | if (!PyArg_Parse (args, "s", &fileName)) |
| 436 | return NULL; |
| 437 | |
| 438 | if (Tcl_EvalFile (Tkapp_Interp (self), fileName) == TCL_ERROR) |
| 439 | return Tkinter_Error (self); |
| 440 | |
| 441 | return PyString_FromString (Tkapp_Result (self)); |
| 442 | } |
| 443 | |
| 444 | static PyObject * |
| 445 | Tkapp_Record (self, args) |
| 446 | PyObject *self; |
| 447 | PyObject *args; |
| 448 | { |
| 449 | char *script; |
| 450 | |
| 451 | if (!PyArg_Parse (args, "s", &script)) |
| 452 | return NULL; |
| 453 | |
| 454 | if (Tcl_RecordAndEval (Tkapp_Interp (self), |
| 455 | script, TCL_NO_EVAL) == TCL_ERROR) |
| 456 | return Tkinter_Error (self); |
| 457 | |
| 458 | return PyString_FromString (Tkapp_Result (self)); |
| 459 | } |
| 460 | |
| 461 | static PyObject * |
| 462 | Tkapp_AddErrorInfo (self, args) |
| 463 | PyObject *self; |
| 464 | PyObject *args; |
| 465 | { |
| 466 | char *msg; |
| 467 | |
| 468 | if (!PyArg_Parse (args, "s", &msg)) |
| 469 | return NULL; |
| 470 | Tcl_AddErrorInfo (Tkapp_Interp (self), msg); |
| 471 | |
| 472 | Py_INCREF(Py_None); |
| 473 | return Py_None; |
| 474 | } |
| 475 | |
| 476 | /** Tcl Variable **/ |
| 477 | |
| 478 | static PyObject * |
| 479 | SetVar (self, args, flags) |
| 480 | PyObject *self; |
| 481 | PyObject *args; |
| 482 | int flags; |
| 483 | { |
| 484 | char *name1, *name2, *ok; |
| 485 | PyObject *newValue; |
| 486 | PyObject *tmp; |
| 487 | |
| 488 | tmp = PyList_New (0); |
| 489 | |
| 490 | if (PyArg_Parse (args, "(sO)", &name1, &newValue)) |
| 491 | ok = Tcl_SetVar (Tkapp_Interp (self), name1, |
| 492 | AsString (newValue, tmp), flags); /* XXX Merge? */ |
| 493 | else if (PyArg_Parse (args, "(ssO)", &name1, &name2, &newValue)) |
| 494 | ok = Tcl_SetVar2 (Tkapp_Interp (self), name1, name2, |
| 495 | AsString (newValue, tmp), flags); |
| 496 | else |
| 497 | { |
| 498 | Py_DECREF (tmp); |
| 499 | return NULL; |
| 500 | } |
| 501 | Py_DECREF (tmp); |
| 502 | |
| 503 | if (!ok) |
| 504 | return Tkinter_Error (self); |
| 505 | |
| 506 | Py_INCREF (Py_None); |
| 507 | return Py_None; |
| 508 | } |
| 509 | |
| 510 | static PyObject * |
| 511 | Tkapp_SetVar (self, args) |
| 512 | PyObject *self; |
| 513 | PyObject *args; |
| 514 | { |
| 515 | return SetVar (self, args, TCL_LEAVE_ERR_MSG); |
| 516 | } |
| 517 | |
| 518 | static PyObject * |
| 519 | Tkapp_GlobalSetVar (self, args) |
| 520 | PyObject *self; |
| 521 | PyObject *args; |
| 522 | { |
| 523 | return SetVar (self, args, TCL_LEAVE_ERR_MSG | TCL_GLOBAL_ONLY); |
| 524 | } |
| 525 | |
| 526 | static PyObject * |
| 527 | GetVar (self, args, flags) |
| 528 | PyObject *self; |
| 529 | PyObject *args; |
| 530 | int flags; |
| 531 | { |
| 532 | char *name1, *name2, *s; |
| 533 | |
| 534 | if (PyArg_Parse (args, "s", &name1)) |
| 535 | s = Tcl_GetVar (Tkapp_Interp (self), name1, flags); |
| 536 | else if (PyArg_Parse (args, "(ss)", &name1, &name2)) |
| 537 | s = Tcl_GetVar2 (Tkapp_Interp (self), name1, name2, flags); |
| 538 | else |
| 539 | return NULL; |
| 540 | |
| 541 | if (s == NULL) |
| 542 | return Tkinter_Error (self); |
| 543 | |
| 544 | return PyString_FromString (s); |
| 545 | } |
| 546 | |
| 547 | static PyObject * |
| 548 | Tkapp_GetVar (self, args) |
| 549 | PyObject *self; |
| 550 | PyObject *args; |
| 551 | { |
| 552 | return GetVar (self, args, TCL_LEAVE_ERR_MSG); |
| 553 | } |
| 554 | |
| 555 | static PyObject * |
| 556 | Tkapp_GlobalGetVar (self, args) |
| 557 | PyObject *self; |
| 558 | PyObject *args; |
| 559 | { |
| 560 | return GetVar (self, args, TCL_LEAVE_ERR_MSG | TCL_GLOBAL_ONLY); |
| 561 | } |
| 562 | |
| 563 | static PyObject * |
| 564 | UnsetVar (self, args, flags) |
| 565 | PyObject *self; |
| 566 | PyObject *args; |
| 567 | int flags; |
| 568 | { |
| 569 | char *name1, *name2; |
| 570 | int code; |
| 571 | |
| 572 | if (PyArg_Parse (args, "s", &name1)) |
| 573 | code = Tcl_UnsetVar (Tkapp_Interp (self), name1, flags); |
| 574 | else if (PyArg_Parse (args, "(ss)", &name1, &name2)) |
| 575 | code = Tcl_UnsetVar2 (Tkapp_Interp (self), name1, name2, flags); |
| 576 | else |
| 577 | return NULL; |
| 578 | |
| 579 | if (code == TCL_ERROR) |
| 580 | return Tkinter_Error (self); |
| 581 | |
| 582 | Py_INCREF (Py_None); |
| 583 | return Py_None; |
| 584 | } |
| 585 | |
| 586 | static PyObject * |
| 587 | Tkapp_UnsetVar (self, args) |
| 588 | PyObject *self; |
| 589 | PyObject *args; |
| 590 | { |
| 591 | return UnsetVar (self, args, TCL_LEAVE_ERR_MSG); |
| 592 | } |
| 593 | |
| 594 | static PyObject * |
| 595 | Tkapp_GlobalUnsetVar (self, args) |
| 596 | PyObject *self; |
| 597 | PyObject *args; |
| 598 | { |
| 599 | return UnsetVar (self, args, TCL_LEAVE_ERR_MSG | TCL_GLOBAL_ONLY); |
| 600 | } |
| 601 | |
| 602 | /** Tcl to Python **/ |
| 603 | |
| 604 | static PyObject * |
| 605 | Tkapp_GetInt (self, args) |
| 606 | PyObject *self; |
| 607 | PyObject *args; |
| 608 | { |
| 609 | char *s; |
| 610 | int v; |
| 611 | |
| 612 | if (!PyArg_Parse (args, "s", &s)) |
| 613 | return NULL; |
| 614 | if (Tcl_GetInt (Tkapp_Interp (self), s, &v) == TCL_ERROR) |
| 615 | return Tkinter_Error (self); |
| 616 | return Py_BuildValue ("i", v); |
| 617 | } |
| 618 | |
| 619 | static PyObject * |
| 620 | Tkapp_GetDouble (self, args) |
| 621 | PyObject *self; |
| 622 | PyObject *args; |
| 623 | { |
| 624 | char *s; |
| 625 | double v; |
| 626 | |
| 627 | if (!PyArg_Parse (args, "s", &s)) |
| 628 | return NULL; |
| 629 | if (Tcl_GetDouble (Tkapp_Interp (self), s, &v) == TCL_ERROR) |
| 630 | return Tkinter_Error (self); |
| 631 | return Py_BuildValue ("d", v); |
| 632 | } |
| 633 | |
| 634 | static PyObject * |
| 635 | Tkapp_GetBoolean (self, args) |
| 636 | PyObject *self; |
| 637 | PyObject *args; |
| 638 | { |
| 639 | char *s; |
| 640 | int v; |
| 641 | |
| 642 | if (!PyArg_Parse (args, "s", &s)) |
| 643 | return NULL; |
| 644 | if (Tcl_GetBoolean (Tkapp_Interp (self), s, &v) == TCL_ERROR) |
| 645 | return Tkinter_Error (self); |
| 646 | return Py_BuildValue ("i", v); |
| 647 | } |
| 648 | |
| 649 | static PyObject * |
| 650 | Tkapp_ExprString (self, args) |
| 651 | PyObject *self; |
| 652 | PyObject *args; |
| 653 | { |
| 654 | char *s; |
| 655 | |
| 656 | if (!PyArg_Parse (args, "s", &s)) |
| 657 | return NULL; |
| 658 | if (Tcl_ExprString (Tkapp_Interp (self), s) == TCL_ERROR) |
| 659 | return Tkinter_Error (self); |
| 660 | return Py_BuildValue ("s", Tkapp_Result (self)); |
| 661 | } |
| 662 | |
| 663 | static PyObject * |
| 664 | Tkapp_ExprLong (self, args) |
| 665 | PyObject *self; |
| 666 | PyObject *args; |
| 667 | { |
| 668 | char *s; |
| 669 | long v; |
| 670 | |
| 671 | if (!PyArg_Parse (args, "s", &s)) |
| 672 | return NULL; |
| 673 | if (Tcl_ExprLong (Tkapp_Interp (self), s, &v) == TCL_ERROR) |
| 674 | return Tkinter_Error (self); |
| 675 | return Py_BuildValue ("l", v); |
| 676 | } |
| 677 | |
| 678 | static PyObject * |
| 679 | Tkapp_ExprDouble (self, args) |
| 680 | PyObject *self; |
| 681 | PyObject *args; |
| 682 | { |
| 683 | char *s; |
| 684 | double v; |
| 685 | |
| 686 | if (!PyArg_Parse (args, "s", &s)) |
| 687 | return NULL; |
| 688 | if (Tcl_ExprDouble (Tkapp_Interp (self), s, &v) == TCL_ERROR) |
| 689 | return Tkinter_Error (self); |
| 690 | return Py_BuildValue ("d", v); |
| 691 | } |
| 692 | |
| 693 | static PyObject * |
| 694 | Tkapp_ExprBoolean (self, args) |
| 695 | PyObject *self; |
| 696 | PyObject *args; |
| 697 | { |
| 698 | char *s; |
| 699 | int v; |
| 700 | |
| 701 | if (!PyArg_Parse (args, "s", &s)) |
| 702 | return NULL; |
| 703 | if (Tcl_ExprBoolean (Tkapp_Interp (self), s, &v) == TCL_ERROR) |
| 704 | return Tkinter_Error (self); |
| 705 | return Py_BuildValue ("i", v); |
| 706 | } |
| 707 | |
| 708 | static PyObject * |
| 709 | Tkapp_SplitList (self, args) |
| 710 | PyObject *self; |
| 711 | PyObject *args; |
| 712 | { |
| 713 | char *list; |
| 714 | int argc; |
| 715 | char **argv; |
| 716 | PyObject *v; |
| 717 | int i; |
| 718 | |
| 719 | if (!PyArg_Parse (args, "s", &list)) |
| 720 | return NULL; |
| 721 | |
| 722 | if (Tcl_SplitList (Tkapp_Interp (self), list, &argc, &argv) == TCL_ERROR) |
| 723 | return Tkinter_Error (self); |
| 724 | |
| 725 | v = PyTuple_New (argc); |
| 726 | for (i = 0; i < argc; i++) |
| 727 | PyTuple_SetItem (v, i, PyString_FromString (argv[i])); |
| 728 | |
Guido van Rossum | 7ffa761 | 1996-08-13 21:10:16 +0000 | [diff] [blame] | 729 | ckfree (FREECAST argv); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 730 | return v; |
| 731 | } |
| 732 | |
| 733 | static PyObject * |
| 734 | Tkapp_Split (self, args) |
| 735 | PyObject *self; |
| 736 | PyObject *args; |
| 737 | { |
| 738 | char *list; |
| 739 | |
| 740 | if (!PyArg_Parse (args, "s", &list)) |
| 741 | return NULL; |
| 742 | return Split (self, list); |
| 743 | } |
| 744 | |
| 745 | static PyObject * |
| 746 | Tkapp_Merge (self, args) |
| 747 | PyObject *self; |
| 748 | PyObject *args; |
| 749 | { |
| 750 | char *s; |
| 751 | PyObject *v; |
| 752 | |
| 753 | s = Merge (args); |
| 754 | v = PyString_FromString (s); |
Guido van Rossum | 7ffa761 | 1996-08-13 21:10:16 +0000 | [diff] [blame] | 755 | ckfree (s); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 756 | return v; |
| 757 | } |
| 758 | |
| 759 | /** Tcl Command **/ |
| 760 | |
| 761 | /* This is the Tcl command that acts as a wrapper for Python |
| 762 | function or method. */ |
| 763 | static int |
| 764 | PythonCmd (clientData, interp, argc, argv) |
| 765 | ClientData clientData; /* Is (self, func) */ |
| 766 | Tcl_Interp *interp; |
| 767 | int argc; |
| 768 | char *argv[]; |
| 769 | { |
| 770 | PyObject *self, *func, *arg, *res, *tmp; |
| 771 | int i; |
| 772 | |
| 773 | self = PyTuple_GetItem ((PyObject *) clientData, 0); |
| 774 | func = PyTuple_GetItem ((PyObject *) clientData, 1); |
| 775 | |
| 776 | /* Create argument list (argv1, ..., argvN) */ |
| 777 | arg = PyTuple_New (argc - 1); |
| 778 | for (i = 0; i < (argc - 1); i++) |
| 779 | PyTuple_SetItem (arg, i, PyString_FromString (argv[i + 1])); |
| 780 | |
| 781 | res = PyEval_CallObject (func, arg); |
| 782 | Py_DECREF (arg); |
Guido van Rossum | d308e2b | 1994-07-07 09:25:12 +0000 | [diff] [blame] | 783 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 784 | if (res == NULL) |
| 785 | return PythonCmd_Error (interp); |
| 786 | |
| 787 | tmp = PyList_New (0); |
| 788 | Tcl_SetResult (Tkapp_Interp (self), AsString (res, tmp), TCL_VOLATILE); |
| 789 | Py_DECREF (res); |
| 790 | Py_DECREF (tmp); |
| 791 | |
| 792 | return TCL_OK; |
| 793 | } |
| 794 | |
| 795 | static void |
| 796 | PythonCmdDelete (clientData) |
| 797 | ClientData clientData; /* Is (self, func) */ |
| 798 | { |
| 799 | Py_DECREF ((PyObject *) clientData); |
| 800 | } |
| 801 | |
| 802 | static PyObject * |
| 803 | Tkapp_CreateCommand (self, args) |
| 804 | PyObject *self; |
| 805 | PyObject *args; |
| 806 | { |
| 807 | char *cmdName; |
| 808 | PyObject *data; |
| 809 | PyObject *func; |
| 810 | |
| 811 | /* Args is: (cmdName, func) */ |
| 812 | if (!PyTuple_Check (args) |
| 813 | || !(PyTuple_Size (args) == 2) |
| 814 | || !PyString_Check (PyTuple_GetItem (args, 0)) |
Guido van Rossum | a3c3f2c | 1995-02-07 15:41:02 +0000 | [diff] [blame] | 815 | || !PyCallable_Check (PyTuple_GetItem (args, 1))) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 816 | { |
| 817 | PyErr_SetString (PyExc_TypeError, "bad argument list"); |
| 818 | return NULL; |
| 819 | } |
| 820 | |
| 821 | cmdName = PyString_AsString (PyTuple_GetItem (args, 0)); |
| 822 | func = PyTuple_GetItem (args, 1); |
| 823 | |
| 824 | data = PyTuple_New (2); /* ClientData is: (self, func) */ |
| 825 | |
| 826 | Py_INCREF (self); |
| 827 | PyTuple_SetItem (data, 0, self); |
| 828 | |
| 829 | Py_INCREF (func); |
| 830 | PyTuple_SetItem (data, 1, func); |
| 831 | |
| 832 | Tcl_CreateCommand (Tkapp_Interp (self), cmdName, PythonCmd, |
| 833 | (ClientData) data, PythonCmdDelete); |
| 834 | |
| 835 | Py_INCREF (Py_None); |
| 836 | return Py_None; |
| 837 | } |
| 838 | |
| 839 | static PyObject * |
| 840 | Tkapp_DeleteCommand (self, args) |
| 841 | PyObject *self; |
| 842 | PyObject *args; |
| 843 | { |
| 844 | char *cmdName; |
| 845 | |
| 846 | if (!PyArg_Parse (args, "s", &cmdName)) |
| 847 | return NULL; |
| 848 | if (Tcl_DeleteCommand (Tkapp_Interp (self), cmdName) == -1) |
| 849 | { |
| 850 | PyErr_SetString (Tkinter_TclError, "can't delete Tcl command"); |
| 851 | return NULL; |
| 852 | } |
| 853 | Py_INCREF (Py_None); |
| 854 | return Py_None; |
| 855 | } |
| 856 | |
| 857 | /** File Handler **/ |
| 858 | |
Guido van Rossum | a597dde | 1995-01-10 20:56:29 +0000 | [diff] [blame] | 859 | static void |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 860 | FileHandler (clientData, mask) |
Guido van Rossum | 7687522 | 1994-06-27 07:59:42 +0000 | [diff] [blame] | 861 | ClientData clientData; /* Is: (func, file) */ |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 862 | int mask; |
| 863 | { |
Guido van Rossum | 7687522 | 1994-06-27 07:59:42 +0000 | [diff] [blame] | 864 | PyObject *func, *file, *arg, *res; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 865 | |
Guido van Rossum | 7687522 | 1994-06-27 07:59:42 +0000 | [diff] [blame] | 866 | func = PyTuple_GetItem ((PyObject *) clientData, 0); |
| 867 | file = PyTuple_GetItem ((PyObject *) clientData, 1); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 868 | |
Guido van Rossum | 7687522 | 1994-06-27 07:59:42 +0000 | [diff] [blame] | 869 | arg = Py_BuildValue ("(Oi)", file, (long) mask); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 870 | res = PyEval_CallObject (func, arg); |
| 871 | Py_DECREF (arg); |
| 872 | if (res == NULL) |
| 873 | { |
| 874 | errorInCmd = 1; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 875 | PyErr_Fetch (&excInCmd, &valInCmd, &trbInCmd); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 876 | } |
Guido van Rossum | 9bb4fd6 | 1994-08-09 14:15:19 +0000 | [diff] [blame] | 877 | Py_XDECREF (res); |
| 878 | } |
| 879 | |
| 880 | static int |
| 881 | GetFileNo (file) |
| 882 | PyObject *file; /* Either an int >= 0 or an object with a |
| 883 | .fileno() method that returns an int >= 0 */ |
| 884 | { |
Guido van Rossum | a597dde | 1995-01-10 20:56:29 +0000 | [diff] [blame] | 885 | PyObject *meth, *args, *res; |
Guido van Rossum | 9bb4fd6 | 1994-08-09 14:15:19 +0000 | [diff] [blame] | 886 | int id; |
| 887 | if (PyInt_Check(file)) { |
| 888 | id = PyInt_AsLong(file); |
| 889 | if (id < 0) |
| 890 | PyErr_SetString(PyExc_ValueError, "invalid file id"); |
| 891 | return id; |
| 892 | } |
| 893 | meth = PyObject_GetAttrString(file, "fileno"); |
| 894 | if (meth == NULL) |
| 895 | return -1; |
| 896 | args = PyTuple_New(0); |
| 897 | if (args == NULL) |
| 898 | return -1; |
| 899 | res = PyEval_CallObject(meth, args); |
| 900 | Py_DECREF(args); |
| 901 | Py_DECREF(meth); |
| 902 | if (res == NULL) |
| 903 | return -1; |
| 904 | if (PyInt_Check(res)) |
| 905 | id = PyInt_AsLong(res); |
| 906 | else |
| 907 | id = -1; |
| 908 | if (id < 0) |
| 909 | PyErr_SetString(PyExc_ValueError, |
| 910 | "invalid fileno() return value"); |
| 911 | Py_DECREF(res); |
| 912 | return id; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 913 | } |
| 914 | |
| 915 | static PyObject * |
| 916 | Tkapp_CreateFileHandler (self, args) |
| 917 | PyObject *self; |
| 918 | PyObject *args; /* Is (file, mask, func) */ |
| 919 | { |
Guido van Rossum | 7687522 | 1994-06-27 07:59:42 +0000 | [diff] [blame] | 920 | PyObject *file, *func, *data; |
| 921 | int mask, id; |
Guido van Rossum | 6878436 | 1996-05-16 17:17:31 +0000 | [diff] [blame] | 922 | #if (TK_MAJOR_VERSION*1000 + TK_MINOR_VERSION) >= 4001 |
| 923 | Tcl_File tfile; |
| 924 | #endif |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 925 | |
| 926 | if (!PyArg_Parse (args, "(OiO)", &file, &mask, &func)) |
| 927 | return NULL; |
Guido van Rossum | 9bb4fd6 | 1994-08-09 14:15:19 +0000 | [diff] [blame] | 928 | id = GetFileNo (file); |
| 929 | if (id < 0) |
| 930 | return NULL; |
Guido van Rossum | a3c3f2c | 1995-02-07 15:41:02 +0000 | [diff] [blame] | 931 | if (!PyCallable_Check(func)) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 932 | { |
| 933 | PyErr_SetString (PyExc_TypeError, "bad argument list"); |
| 934 | return NULL; |
| 935 | } |
| 936 | |
Guido van Rossum | 7687522 | 1994-06-27 07:59:42 +0000 | [diff] [blame] | 937 | /* ClientData is: (func, file) */ |
| 938 | data = Py_BuildValue ("(OO)", func, file); |
| 939 | |
Guido van Rossum | 6878436 | 1996-05-16 17:17:31 +0000 | [diff] [blame] | 940 | #if (TK_MAJOR_VERSION*1000 + TK_MINOR_VERSION) >= 4001 |
Guido van Rossum | 07886d0 | 1996-09-11 23:31:42 +0000 | [diff] [blame] | 941 | #ifdef MS_WINDOWS |
Guido van Rossum | 845547d | 1996-06-26 18:26:04 +0000 | [diff] [blame] | 942 | /* We assume this is a socket... */ |
| 943 | tfile = Tcl_GetFile((ClientData)id, TCL_WIN_SOCKET); |
| 944 | #else |
Guido van Rossum | 6878436 | 1996-05-16 17:17:31 +0000 | [diff] [blame] | 945 | tfile = Tcl_GetFile((ClientData)id, TCL_UNIX_FD); |
Guido van Rossum | 845547d | 1996-06-26 18:26:04 +0000 | [diff] [blame] | 946 | #endif |
Guido van Rossum | 6878436 | 1996-05-16 17:17:31 +0000 | [diff] [blame] | 947 | /* Oughtta check for null Tcl_File object... */ |
| 948 | Tcl_CreateFileHandler (tfile, mask, FileHandler, (ClientData) data); |
| 949 | #else |
Guido van Rossum | 845547d | 1996-06-26 18:26:04 +0000 | [diff] [blame] | 950 | Tk_CreateFileHandler (id, mask, FileHandler, (ClientData) data); |
Guido van Rossum | 6878436 | 1996-05-16 17:17:31 +0000 | [diff] [blame] | 951 | #endif |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 952 | /* XXX fileHandlerDict */ |
Guido van Rossum | 7687522 | 1994-06-27 07:59:42 +0000 | [diff] [blame] | 953 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 954 | Py_INCREF (Py_None); |
| 955 | return Py_None; |
| 956 | } |
| 957 | |
| 958 | static PyObject * |
| 959 | Tkapp_DeleteFileHandler (self, args) |
| 960 | PyObject *self; |
| 961 | PyObject *args; /* Args: file */ |
| 962 | { |
Guido van Rossum | 9bb4fd6 | 1994-08-09 14:15:19 +0000 | [diff] [blame] | 963 | PyObject *file; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 964 | int id; |
Guido van Rossum | 6878436 | 1996-05-16 17:17:31 +0000 | [diff] [blame] | 965 | #if (TK_MAJOR_VERSION*1000 + TK_MINOR_VERSION) >= 4001 |
| 966 | Tcl_File tfile; |
| 967 | #endif |
| 968 | |
Guido van Rossum | 9bb4fd6 | 1994-08-09 14:15:19 +0000 | [diff] [blame] | 969 | if (!PyArg_Parse (args, "O", &file)) |
| 970 | return NULL; |
| 971 | id = GetFileNo (file); |
| 972 | if (id < 0) |
| 973 | return NULL; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 974 | |
Guido van Rossum | 6878436 | 1996-05-16 17:17:31 +0000 | [diff] [blame] | 975 | #if (TK_MAJOR_VERSION*1000 + TK_MINOR_VERSION) >= 4001 |
Guido van Rossum | 07886d0 | 1996-09-11 23:31:42 +0000 | [diff] [blame] | 976 | #ifdef MS_WINDOWS |
Guido van Rossum | 845547d | 1996-06-26 18:26:04 +0000 | [diff] [blame] | 977 | /* We assume this is a socket... */ |
| 978 | tfile = Tcl_GetFile((ClientData)id, TCL_WIN_SOCKET); |
| 979 | #else |
| 980 | tfile = Tcl_GetFile((ClientData)id, TCL_UNIX_FD); |
| 981 | #endif |
Guido van Rossum | 6878436 | 1996-05-16 17:17:31 +0000 | [diff] [blame] | 982 | /* Oughtta check for null Tcl_File object... */ |
| 983 | Tcl_DeleteFileHandler(tfile); |
| 984 | #else |
Guido van Rossum | 845547d | 1996-06-26 18:26:04 +0000 | [diff] [blame] | 985 | Tk_DeleteFileHandler (id); |
Guido van Rossum | 6878436 | 1996-05-16 17:17:31 +0000 | [diff] [blame] | 986 | #endif |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 987 | /* XXX fileHandlerDict */ |
| 988 | Py_INCREF (Py_None); |
| 989 | return Py_None; |
| 990 | } |
| 991 | |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 992 | /**** Tktt Object (timer token) ****/ |
| 993 | |
| 994 | staticforward PyTypeObject Tktt_Type; |
| 995 | |
| 996 | typedef struct |
| 997 | { |
| 998 | PyObject_HEAD |
| 999 | Tk_TimerToken token; |
| 1000 | PyObject *func; |
| 1001 | } |
| 1002 | TkttObject; |
| 1003 | |
| 1004 | static PyObject * |
| 1005 | Tktt_DeleteTimerHandler (self, args) |
| 1006 | PyObject *self; |
| 1007 | PyObject *args; |
| 1008 | { |
| 1009 | TkttObject *v = (TkttObject *) self; |
| 1010 | |
| 1011 | if (!PyArg_Parse (args, "")) |
| 1012 | return NULL; |
| 1013 | if (v->func != NULL) |
| 1014 | { |
| 1015 | Tk_DeleteTimerHandler (v->token); |
| 1016 | PyMem_DEL (v->func); |
| 1017 | v->func = NULL; |
| 1018 | } |
| 1019 | Py_INCREF (Py_None); |
| 1020 | return Py_None; |
| 1021 | } |
| 1022 | |
| 1023 | static PyMethodDef Tktt_methods[] = |
| 1024 | { |
| 1025 | {"deletetimerhandler", Tktt_DeleteTimerHandler}, |
| 1026 | {NULL, NULL} |
| 1027 | }; |
| 1028 | |
| 1029 | static TkttObject * |
| 1030 | Tktt_New (token, func) |
| 1031 | Tk_TimerToken token; |
| 1032 | PyObject *func; |
| 1033 | { |
| 1034 | TkttObject *v; |
| 1035 | |
| 1036 | v = PyObject_NEW (TkttObject, &Tktt_Type); |
| 1037 | if (v == NULL) |
| 1038 | return NULL; |
| 1039 | |
| 1040 | v->token = token; |
| 1041 | v->func = func; |
| 1042 | Py_INCREF (v->func); |
| 1043 | return v; |
| 1044 | } |
| 1045 | |
| 1046 | static void |
| 1047 | Tktt_Dealloc (self) |
| 1048 | PyObject *self; |
| 1049 | { |
| 1050 | PyMem_DEL (self); |
| 1051 | } |
| 1052 | |
| 1053 | static int |
| 1054 | Tktt_Print (self, fp, flags) |
| 1055 | PyObject *self; |
| 1056 | FILE *fp; |
| 1057 | int flags; |
| 1058 | { |
| 1059 | TkttObject *v = (TkttObject *) self; |
| 1060 | |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 1061 | fprintf(fp, "<tktimertoken at 0x%lx%s>", (long)v, |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 1062 | v->func == NULL ? ", handler deleted" : ""); |
| 1063 | return 0; |
| 1064 | } |
| 1065 | |
| 1066 | static PyObject * |
| 1067 | Tktt_GetAttr (self, name) |
| 1068 | PyObject *self; |
| 1069 | char *name; |
| 1070 | { |
| 1071 | return Py_FindMethod (Tktt_methods, self, name); |
| 1072 | } |
| 1073 | |
| 1074 | static PyTypeObject Tktt_Type = |
| 1075 | { |
Guido van Rossum | ae92f01 | 1996-08-21 19:03:36 +0000 | [diff] [blame] | 1076 | PyObject_HEAD_INIT (NULL) |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 1077 | 0, /*ob_size */ |
| 1078 | "tktimertoken", /*tp_name */ |
| 1079 | sizeof (TkttObject), /*tp_basicsize */ |
| 1080 | 0, /*tp_itemsize */ |
| 1081 | Tktt_Dealloc, /*tp_dealloc */ |
| 1082 | Tktt_Print, /*tp_print */ |
| 1083 | Tktt_GetAttr, /*tp_getattr */ |
| 1084 | 0, /*tp_setattr */ |
| 1085 | 0, /*tp_compare */ |
| 1086 | 0, /*tp_repr */ |
| 1087 | 0, /*tp_as_number */ |
| 1088 | 0, /*tp_as_sequence */ |
| 1089 | 0, /*tp_as_mapping */ |
| 1090 | 0, /*tp_hash */ |
| 1091 | }; |
| 1092 | |
| 1093 | /** Timer Handler **/ |
| 1094 | |
| 1095 | static void |
| 1096 | TimerHandler (clientData) |
| 1097 | ClientData clientData; |
| 1098 | { |
| 1099 | PyObject *func = (PyObject *) clientData; |
| 1100 | PyObject *arg, *res; |
| 1101 | |
| 1102 | arg = PyTuple_New (0); |
| 1103 | res = PyEval_CallObject (func, arg); |
| 1104 | Py_DECREF (arg); |
| 1105 | if (res == NULL) |
| 1106 | { |
| 1107 | errorInCmd = 1; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 1108 | PyErr_Fetch (&excInCmd, &valInCmd, &trbInCmd); |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 1109 | } |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 1110 | else |
| 1111 | Py_DECREF (res); |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 1112 | } |
| 1113 | |
| 1114 | static PyObject * |
| 1115 | Tkapp_CreateTimerHandler (self, args) |
| 1116 | PyObject *self; |
| 1117 | PyObject *args; /* Is (milliseconds, func) */ |
| 1118 | { |
| 1119 | int milliseconds; |
| 1120 | PyObject *func; |
| 1121 | Tk_TimerToken token; |
| 1122 | |
| 1123 | if (!PyArg_Parse (args, "(iO)", &milliseconds, &func)) |
| 1124 | return NULL; |
Guido van Rossum | a3c3f2c | 1995-02-07 15:41:02 +0000 | [diff] [blame] | 1125 | if (!PyCallable_Check(func)) |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 1126 | { |
| 1127 | PyErr_SetString (PyExc_TypeError, "bad argument list"); |
| 1128 | return NULL; |
| 1129 | } |
| 1130 | token = Tk_CreateTimerHandler(milliseconds, TimerHandler, (ClientData) func); |
| 1131 | return (PyObject *) Tktt_New (token, func); |
| 1132 | } |
| 1133 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1134 | /** Event Loop **/ |
| 1135 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1136 | static PyObject * |
| 1137 | Tkapp_MainLoop (self, args) |
| 1138 | PyObject *self; |
| 1139 | PyObject *args; |
| 1140 | { |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 1141 | int threshold = 0; |
| 1142 | |
Guido van Rossum | e42fc2f | 1995-07-26 17:29:45 +0000 | [diff] [blame] | 1143 | if (!PyArg_ParseTuple (args, "|i", &threshold)) |
| 1144 | return NULL; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1145 | |
| 1146 | quitMainLoop = 0; |
Guido van Rossum | 9722ad8 | 1995-09-22 23:49:28 +0000 | [diff] [blame] | 1147 | while (Tk_GetNumMainWindows() > threshold && !quitMainLoop && !errorInCmd) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1148 | { |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 1149 | /* XXX Ought to check for other signals! */ |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1150 | if (PyOS_InterruptOccurred ()) |
| 1151 | { |
| 1152 | PyErr_SetNone (PyExc_KeyboardInterrupt); |
| 1153 | return NULL; |
| 1154 | } |
| 1155 | Tk_DoOneEvent (0); |
| 1156 | } |
Guido van Rossum | 64b24fb | 1995-09-30 17:00:24 +0000 | [diff] [blame] | 1157 | quitMainLoop = 0; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1158 | |
| 1159 | if (errorInCmd) |
| 1160 | { |
| 1161 | errorInCmd = 0; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 1162 | PyErr_Restore (excInCmd, valInCmd, trbInCmd); |
| 1163 | excInCmd = valInCmd = trbInCmd = NULL; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1164 | return NULL; |
| 1165 | } |
| 1166 | Py_INCREF (Py_None); |
| 1167 | return Py_None; |
| 1168 | } |
| 1169 | |
| 1170 | static PyObject * |
Guido van Rossum | 062cfb0 | 1995-01-10 17:42:51 +0000 | [diff] [blame] | 1171 | Tkapp_DoOneEvent (self, args) |
| 1172 | PyObject *self; |
| 1173 | PyObject *args; |
| 1174 | { |
Guido van Rossum | e42fc2f | 1995-07-26 17:29:45 +0000 | [diff] [blame] | 1175 | int flags = TK_ALL_EVENTS; |
Guido van Rossum | 062cfb0 | 1995-01-10 17:42:51 +0000 | [diff] [blame] | 1176 | int rv; |
| 1177 | |
Guido van Rossum | e42fc2f | 1995-07-26 17:29:45 +0000 | [diff] [blame] | 1178 | if (!PyArg_ParseTuple (args, "|i", &flags)) |
| 1179 | return NULL; |
Guido van Rossum | 062cfb0 | 1995-01-10 17:42:51 +0000 | [diff] [blame] | 1180 | rv = Tk_DoOneEvent(flags); |
| 1181 | return Py_BuildValue ("i", rv); |
| 1182 | } |
| 1183 | |
| 1184 | static PyObject * |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1185 | Tkapp_Quit (self, args) |
| 1186 | PyObject *self; |
| 1187 | PyObject *args; |
| 1188 | { |
| 1189 | |
| 1190 | if (!PyArg_Parse (args, "")) |
| 1191 | return NULL; |
| 1192 | quitMainLoop = 1; |
| 1193 | Py_INCREF (Py_None); |
| 1194 | return Py_None; |
| 1195 | } |
| 1196 | |
| 1197 | /**** Tkapp Method List ****/ |
| 1198 | |
| 1199 | static PyMethodDef Tkapp_methods[] = |
| 1200 | { |
| 1201 | {"call", Tkapp_Call}, |
| 1202 | {"globalcall", Tkapp_GlobalCall}, |
| 1203 | {"eval", Tkapp_Eval}, |
| 1204 | {"globaleval", Tkapp_GlobalEval}, |
| 1205 | {"evalfile", Tkapp_EvalFile}, |
| 1206 | {"record", Tkapp_Record}, |
| 1207 | {"adderrorinfo", Tkapp_AddErrorInfo}, |
| 1208 | {"setvar", Tkapp_SetVar}, |
| 1209 | {"globalsetvar", Tkapp_GlobalSetVar}, |
| 1210 | {"getvar", Tkapp_GetVar}, |
| 1211 | {"globalgetvar", Tkapp_GlobalGetVar}, |
| 1212 | {"unsetvar", Tkapp_UnsetVar}, |
| 1213 | {"globalunsetvar", Tkapp_GlobalUnsetVar}, |
| 1214 | {"getint", Tkapp_GetInt}, |
| 1215 | {"getdouble", Tkapp_GetDouble}, |
| 1216 | {"getboolean", Tkapp_GetBoolean}, |
| 1217 | {"exprstring", Tkapp_ExprString}, |
| 1218 | {"exprlong", Tkapp_ExprLong}, |
| 1219 | {"exprdouble", Tkapp_ExprDouble}, |
| 1220 | {"exprboolean", Tkapp_ExprBoolean}, |
| 1221 | {"splitlist", Tkapp_SplitList}, |
| 1222 | {"split", Tkapp_Split}, |
| 1223 | {"merge", Tkapp_Merge}, |
| 1224 | {"createcommand", Tkapp_CreateCommand}, |
| 1225 | {"deletecommand", Tkapp_DeleteCommand}, |
| 1226 | {"createfilehandler", Tkapp_CreateFileHandler}, |
| 1227 | {"deletefilehandler", Tkapp_DeleteFileHandler}, |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 1228 | {"createtimerhandler", Tkapp_CreateTimerHandler}, |
Guido van Rossum | e42fc2f | 1995-07-26 17:29:45 +0000 | [diff] [blame] | 1229 | {"mainloop", Tkapp_MainLoop, 1}, |
| 1230 | {"dooneevent", Tkapp_DoOneEvent, 1}, |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1231 | {"quit", Tkapp_Quit}, |
| 1232 | {NULL, NULL} |
| 1233 | }; |
| 1234 | |
| 1235 | /**** Tkapp Type Methods ****/ |
| 1236 | |
| 1237 | static void |
| 1238 | Tkapp_Dealloc (self) |
| 1239 | PyObject *self; |
| 1240 | { |
Guido van Rossum | dfd428d | 1996-02-25 04:46:40 +0000 | [diff] [blame] | 1241 | #ifdef NEED_TKCREATEMAINWINDOW |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1242 | Tk_DestroyWindow (Tkapp_Tkwin (self)); |
Guido van Rossum | dfd428d | 1996-02-25 04:46:40 +0000 | [diff] [blame] | 1243 | #endif |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1244 | Tcl_DeleteInterp (Tkapp_Interp (self)); |
| 1245 | PyMem_DEL (self); |
| 1246 | } |
| 1247 | |
| 1248 | static PyObject * |
| 1249 | Tkapp_GetAttr (self, name) |
| 1250 | PyObject *self; |
| 1251 | char *name; |
| 1252 | { |
| 1253 | return Py_FindMethod (Tkapp_methods, self, name); |
| 1254 | } |
| 1255 | |
| 1256 | static PyTypeObject Tkapp_Type = |
| 1257 | { |
Guido van Rossum | ae92f01 | 1996-08-21 19:03:36 +0000 | [diff] [blame] | 1258 | PyObject_HEAD_INIT (NULL) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1259 | 0, /*ob_size */ |
| 1260 | "tkapp", /*tp_name */ |
| 1261 | sizeof (TkappObject), /*tp_basicsize */ |
| 1262 | 0, /*tp_itemsize */ |
| 1263 | Tkapp_Dealloc, /*tp_dealloc */ |
| 1264 | 0, /*tp_print */ |
| 1265 | Tkapp_GetAttr, /*tp_getattr */ |
| 1266 | 0, /*tp_setattr */ |
| 1267 | 0, /*tp_compare */ |
| 1268 | 0, /*tp_repr */ |
| 1269 | 0, /*tp_as_number */ |
| 1270 | 0, /*tp_as_sequence */ |
| 1271 | 0, /*tp_as_mapping */ |
| 1272 | 0, /*tp_hash */ |
| 1273 | }; |
| 1274 | |
| 1275 | /**** Tkinter Module ****/ |
| 1276 | |
| 1277 | static PyObject * |
| 1278 | Tkinter_Create (self, args) |
| 1279 | PyObject *self; |
| 1280 | PyObject *args; |
| 1281 | { |
| 1282 | char *screenName = NULL; |
Guido van Rossum | e42fc2f | 1995-07-26 17:29:45 +0000 | [diff] [blame] | 1283 | char *baseName = NULL; |
| 1284 | char *className = NULL; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1285 | int interactive = 0; |
| 1286 | |
Guido van Rossum | 32aa1a7 | 1996-07-31 19:51:15 +0000 | [diff] [blame] | 1287 | baseName = strrchr (Py_GetProgramName (), '/'); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1288 | if (baseName != NULL) |
| 1289 | baseName++; |
| 1290 | else |
Guido van Rossum | 32aa1a7 | 1996-07-31 19:51:15 +0000 | [diff] [blame] | 1291 | baseName = Py_GetProgramName (); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1292 | className = "Tk"; |
| 1293 | |
Guido van Rossum | e42fc2f | 1995-07-26 17:29:45 +0000 | [diff] [blame] | 1294 | if (!PyArg_ParseTuple (args, "|zssi", |
| 1295 | &screenName, &baseName, &className, &interactive)) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1296 | return NULL; |
| 1297 | |
| 1298 | return (PyObject *) Tkapp_New (screenName, baseName, className, |
| 1299 | interactive); |
| 1300 | } |
| 1301 | |
| 1302 | static PyMethodDef moduleMethods[] = |
| 1303 | { |
Guido van Rossum | e42fc2f | 1995-07-26 17:29:45 +0000 | [diff] [blame] | 1304 | {"create", Tkinter_Create, 1}, |
| 1305 | {"createfilehandler", Tkapp_CreateFileHandler, 0}, |
| 1306 | {"deletefilehandler", Tkapp_DeleteFileHandler, 0}, |
| 1307 | {"createtimerhandler", Tkapp_CreateTimerHandler, 0}, |
| 1308 | {"mainloop", Tkapp_MainLoop, 1}, |
| 1309 | {"dooneevent", Tkapp_DoOneEvent, 1}, |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 1310 | {"quit", Tkapp_Quit}, |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1311 | {NULL, NULL} |
| 1312 | }; |
| 1313 | |
Guido van Rossum | a3c3f2c | 1995-02-07 15:41:02 +0000 | [diff] [blame] | 1314 | #undef WITH_READLINE /* XXX */ |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1315 | #ifdef WITH_READLINE |
| 1316 | static int |
| 1317 | EventHook () |
| 1318 | { |
| 1319 | if (errorInCmd) /* XXX Reset tty */ |
| 1320 | { |
| 1321 | errorInCmd = 0; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 1322 | PyErr_Restore (excInCmd, valInCmd, trbInCmd); |
| 1323 | excInCmd = valInCmd = trbInCmd = NULL; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1324 | PyErr_Print (); |
| 1325 | } |
Guido van Rossum | 9722ad8 | 1995-09-22 23:49:28 +0000 | [diff] [blame] | 1326 | if (Tk_GetNumMainWindows() > 0) |
Guido van Rossum | e2ca9bd | 1994-08-03 08:01:43 +0000 | [diff] [blame] | 1327 | Tk_DoOneEvent (TK_DONT_WAIT); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1328 | return 0; |
| 1329 | } |
| 1330 | #endif /* WITH_READLINE */ |
| 1331 | |
| 1332 | static void |
Guido van Rossum | d308e2b | 1994-07-07 09:25:12 +0000 | [diff] [blame] | 1333 | Tkinter_Cleanup () |
| 1334 | { |
Guido van Rossum | a3c3f2c | 1995-02-07 15:41:02 +0000 | [diff] [blame] | 1335 | /* This segfault with Tk 4.0 beta and seems unnecessary there as well */ |
| 1336 | #if TK_MAJOR_VERSION < 4 |
Guido van Rossum | d308e2b | 1994-07-07 09:25:12 +0000 | [diff] [blame] | 1337 | /* XXX rl_deprep_terminal is static, damned! */ |
| 1338 | while (tkMainWindowList != 0) |
| 1339 | Tk_DestroyWindow (tkMainWindowList->win); |
Guido van Rossum | a3c3f2c | 1995-02-07 15:41:02 +0000 | [diff] [blame] | 1340 | #endif |
Guido van Rossum | d308e2b | 1994-07-07 09:25:12 +0000 | [diff] [blame] | 1341 | } |
| 1342 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1343 | void |
Guido van Rossum | ad1f7ee | 1996-02-13 00:11:37 +0000 | [diff] [blame] | 1344 | init_tkinter () |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1345 | { |
Guido van Rossum | d308e2b | 1994-07-07 09:25:12 +0000 | [diff] [blame] | 1346 | static inited = 0; |
| 1347 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1348 | #ifdef WITH_READLINE |
| 1349 | extern int (*rl_event_hook) (); |
| 1350 | #endif /* WITH_READLINE */ |
| 1351 | PyObject *m, *d, *v; |
| 1352 | |
Guido van Rossum | ae92f01 | 1996-08-21 19:03:36 +0000 | [diff] [blame] | 1353 | Tkapp_Type.ob_type = &PyType_Type; |
| 1354 | Tktt_Type.ob_type = &PyType_Type; |
| 1355 | |
Jack Jansen | ba0311e | 1995-10-23 14:34:14 +0000 | [diff] [blame] | 1356 | m = Py_InitModule ("_tkinter", moduleMethods); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1357 | |
| 1358 | d = PyModule_GetDict (m); |
| 1359 | Tkinter_TclError = Py_BuildValue ("s", "TclError"); |
| 1360 | PyDict_SetItemString (d, "TclError", Tkinter_TclError); |
| 1361 | |
| 1362 | v = Py_BuildValue ("i", TK_READABLE); |
| 1363 | PyDict_SetItemString (d, "READABLE", v); |
| 1364 | v = Py_BuildValue ("i", TK_WRITABLE); |
| 1365 | PyDict_SetItemString (d, "WRITABLE", v); |
| 1366 | v = Py_BuildValue ("i", TK_EXCEPTION); |
| 1367 | PyDict_SetItemString (d, "EXCEPTION", v); |
Guido van Rossum | 062cfb0 | 1995-01-10 17:42:51 +0000 | [diff] [blame] | 1368 | v = Py_BuildValue ("i", TK_X_EVENTS); |
| 1369 | PyDict_SetItemString (d, "X_EVENTS", v); |
| 1370 | v = Py_BuildValue ("i", TK_FILE_EVENTS); |
| 1371 | PyDict_SetItemString (d, "FILE_EVENTS", v); |
| 1372 | v = Py_BuildValue ("i", TK_TIMER_EVENTS); |
| 1373 | PyDict_SetItemString (d, "TIMER_EVENTS", v); |
| 1374 | v = Py_BuildValue ("i", TK_IDLE_EVENTS); |
| 1375 | PyDict_SetItemString (d, "IDLE_EVENTS", v); |
| 1376 | v = Py_BuildValue ("i", TK_ALL_EVENTS); |
| 1377 | PyDict_SetItemString (d, "ALL_EVENTS", v); |
| 1378 | v = Py_BuildValue ("i", TK_DONT_WAIT); |
| 1379 | PyDict_SetItemString (d, "DONT_WAIT", v); |
Guido van Rossum | a3c3f2c | 1995-02-07 15:41:02 +0000 | [diff] [blame] | 1380 | v = Py_BuildValue ("s", TK_VERSION); |
| 1381 | PyDict_SetItemString (d, "TK_VERSION", v); |
| 1382 | v = Py_BuildValue ("s", TCL_VERSION); |
| 1383 | PyDict_SetItemString (d, "TCL_VERSION", v); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1384 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1385 | #ifdef WITH_READLINE |
| 1386 | rl_event_hook = EventHook; |
| 1387 | #endif /* WITH_READLINE */ |
| 1388 | |
Guido van Rossum | d308e2b | 1994-07-07 09:25:12 +0000 | [diff] [blame] | 1389 | if (!inited) |
| 1390 | { |
| 1391 | inited = 1; |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 1392 | if (Py_AtExit (Tkinter_Cleanup) != 0) |
| 1393 | fprintf(stderr, |
| 1394 | "Tkinter: warning: cleanup procedure not registered\n"); |
Guido van Rossum | d308e2b | 1994-07-07 09:25:12 +0000 | [diff] [blame] | 1395 | } |
| 1396 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1397 | if (PyErr_Occurred ()) |
Jack Jansen | ba0311e | 1995-10-23 14:34:14 +0000 | [diff] [blame] | 1398 | Py_FatalError ("can't initialize module _tkinter"); |
Jack Jansen | 34cc5c3 | 1995-10-31 16:15:12 +0000 | [diff] [blame] | 1399 | #ifdef macintosh |
Guido van Rossum | ec22c92 | 1996-02-25 04:50:29 +0000 | [diff] [blame] | 1400 | TclMacSetEventProc(PyMacConvertEvent); |
| 1401 | #if GENERATINGCFM |
Jack Jansen | 34cc5c3 | 1995-10-31 16:15:12 +0000 | [diff] [blame] | 1402 | mac_addlibresources(); |
Guido van Rossum | ec22c92 | 1996-02-25 04:50:29 +0000 | [diff] [blame] | 1403 | #endif /* GENERATINGCFM */ |
| 1404 | #endif /* macintosh */ |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1405 | } |
Guido van Rossum | 9722ad8 | 1995-09-22 23:49:28 +0000 | [diff] [blame] | 1406 | |
Guido van Rossum | ec22c92 | 1996-02-25 04:50:29 +0000 | [diff] [blame] | 1407 | |
Guido van Rossum | 9722ad8 | 1995-09-22 23:49:28 +0000 | [diff] [blame] | 1408 | #ifdef macintosh |
Guido van Rossum | dfd428d | 1996-02-25 04:46:40 +0000 | [diff] [blame] | 1409 | |
| 1410 | /* |
Guido van Rossum | ec22c92 | 1996-02-25 04:50:29 +0000 | [diff] [blame] | 1411 | ** Anyone who embeds Tcl/Tk on the Mac must define panic(). |
Guido van Rossum | dfd428d | 1996-02-25 04:46:40 +0000 | [diff] [blame] | 1412 | */ |
| 1413 | |
Guido van Rossum | 9722ad8 | 1995-09-22 23:49:28 +0000 | [diff] [blame] | 1414 | void |
| 1415 | panic(char * format, ...) |
| 1416 | { |
| 1417 | va_list varg; |
| 1418 | |
| 1419 | va_start(varg, format); |
| 1420 | |
| 1421 | vfprintf(stderr, format, varg); |
| 1422 | (void) fflush(stderr); |
| 1423 | |
| 1424 | va_end(varg); |
| 1425 | |
| 1426 | Py_FatalError("Tcl/Tk panic"); |
| 1427 | } |
Jack Jansen | 40b546d | 1995-11-14 10:34:45 +0000 | [diff] [blame] | 1428 | |
Guido van Rossum | ec22c92 | 1996-02-25 04:50:29 +0000 | [diff] [blame] | 1429 | /* |
| 1430 | ** Pass events to SIOUX before passing them to Tk. |
| 1431 | */ |
Guido van Rossum | dfd428d | 1996-02-25 04:46:40 +0000 | [diff] [blame] | 1432 | |
Guido van Rossum | ec22c92 | 1996-02-25 04:50:29 +0000 | [diff] [blame] | 1433 | static int |
| 1434 | PyMacConvertEvent(eventPtr) |
Guido van Rossum | dfd428d | 1996-02-25 04:46:40 +0000 | [diff] [blame] | 1435 | EventRecord *eventPtr; |
| 1436 | { |
Guido van Rossum | ec22c92 | 1996-02-25 04:50:29 +0000 | [diff] [blame] | 1437 | if (SIOUXHandleOneEvent(eventPtr)) |
| 1438 | return 0; /* Nothing happened to the Tcl event queue */ |
| 1439 | return TkMacConvertEvent(eventPtr); |
Guido van Rossum | dfd428d | 1996-02-25 04:46:40 +0000 | [diff] [blame] | 1440 | } |
| 1441 | |
Guido van Rossum | ec22c92 | 1996-02-25 04:50:29 +0000 | [diff] [blame] | 1442 | #if GENERATINGCFM |
Guido van Rossum | dfd428d | 1996-02-25 04:46:40 +0000 | [diff] [blame] | 1443 | |
| 1444 | /* |
| 1445 | ** Additional Mac specific code for dealing with shared libraries. |
| 1446 | */ |
| 1447 | |
| 1448 | #include <Resources.h> |
| 1449 | #include <CodeFragments.h> |
| 1450 | |
| 1451 | static int loaded_from_shlib = 0; |
| 1452 | static FSSpec library_fss; |
Guido van Rossum | 9722ad8 | 1995-09-22 23:49:28 +0000 | [diff] [blame] | 1453 | |
Jack Jansen | 34cc5c3 | 1995-10-31 16:15:12 +0000 | [diff] [blame] | 1454 | /* |
| 1455 | ** If this module is dynamically loaded the following routine should |
| 1456 | ** be the init routine. It takes care of adding the shared library to |
| 1457 | ** the resource-file chain, so that the tk routines can find their |
| 1458 | ** resources. |
| 1459 | */ |
| 1460 | OSErr pascal |
| 1461 | init_tkinter_shlib(InitBlockPtr data) |
| 1462 | { |
Guido van Rossum | c9970ee | 1996-08-26 14:37:15 +0000 | [diff] [blame] | 1463 | __initialize(); |
Jack Jansen | 34cc5c3 | 1995-10-31 16:15:12 +0000 | [diff] [blame] | 1464 | if ( data == nil ) return noErr; |
| 1465 | if ( data->fragLocator.where == kOnDiskFlat ) { |
| 1466 | library_fss = *data->fragLocator.u.onDisk.fileSpec; |
| 1467 | loaded_from_shlib = 1; |
| 1468 | } else if ( data->fragLocator.where == kOnDiskSegmented ) { |
| 1469 | library_fss = *data->fragLocator.u.inSegs.fileSpec; |
| 1470 | loaded_from_shlib = 1; |
| 1471 | } |
| 1472 | return noErr; |
| 1473 | } |
| 1474 | |
| 1475 | /* |
| 1476 | ** Insert the library resources into the search path. Put them after |
| 1477 | ** the resources from the application. Again, we ignore errors. |
| 1478 | */ |
Guido van Rossum | dfd428d | 1996-02-25 04:46:40 +0000 | [diff] [blame] | 1479 | static |
Jack Jansen | 34cc5c3 | 1995-10-31 16:15:12 +0000 | [diff] [blame] | 1480 | mac_addlibresources() |
| 1481 | { |
| 1482 | if ( !loaded_from_shlib ) |
| 1483 | return; |
| 1484 | (void)FSpOpenResFile(&library_fss, fsRdPerm); |
| 1485 | } |
| 1486 | |
Guido van Rossum | ec22c92 | 1996-02-25 04:50:29 +0000 | [diff] [blame] | 1487 | #endif /* GENERATINGCFM */ |
| 1488 | #endif /* macintosh */ |