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 | fd71b9e | 2000-06-30 23:50:40 +0000 | [diff] [blame] | 8 | Copyright (c) 2000, BeOpen.com. |
| 9 | Copyright (c) 1995-2000, Corporation for National Research Initiatives. |
| 10 | Copyright (c) 1990-1995, Stichting Mathematisch Centrum. |
| 11 | All rights reserved. |
Guido van Rossum | 845547d | 1996-06-26 18:26:04 +0000 | [diff] [blame] | 12 | |
Guido van Rossum | fd71b9e | 2000-06-30 23:50:40 +0000 | [diff] [blame] | 13 | See the file "Misc/COPYRIGHT" for information on usage and |
| 14 | redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
Guido van Rossum | 845547d | 1996-06-26 18:26:04 +0000 | [diff] [blame] | 15 | |
| 16 | ******************************************************************/ |
| 17 | |
| 18 | /* _tkinter.c -- Interface to libtk.a and libtcl.a. */ |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 19 | |
Guido van Rossum | 7ffa761 | 1996-08-13 21:10:16 +0000 | [diff] [blame] | 20 | /* TCL/TK VERSION INFO: |
| 21 | |
Guido van Rossum | a80649b | 2000-03-28 20:07:05 +0000 | [diff] [blame] | 22 | Only Tcl/Tk 8.0 and later are supported. Older versions are not |
| 23 | supported. (Use Python 1.5.2 if you cannot upgrade your Tcl/Tk |
| 24 | libraries.) |
| 25 | */ |
Guido van Rossum | 7ffa761 | 1996-08-13 21:10:16 +0000 | [diff] [blame] | 26 | |
Guido van Rossum | a80649b | 2000-03-28 20:07:05 +0000 | [diff] [blame] | 27 | /* XXX Further speed-up ideas, involving Tcl 8.0 features: |
Guido van Rossum | 212643f | 1998-04-29 16:22:14 +0000 | [diff] [blame] | 28 | |
| 29 | - In Tcl_Call(), create Tcl objects from the arguments, possibly using |
| 30 | intelligent mappings between Python objects and Tcl objects (e.g. ints, |
| 31 | floats and Tcl window pointers could be handled specially). |
| 32 | |
| 33 | - Register a new Tcl type, "Python callable", which can be called more |
| 34 | efficiently and passed to Tcl_EvalObj() directly (if this is possible). |
| 35 | |
Guido van Rossum | 7ffa761 | 1996-08-13 21:10:16 +0000 | [diff] [blame] | 36 | */ |
| 37 | |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 38 | |
Guido van Rossum | 9722ad8 | 1995-09-22 23:49:28 +0000 | [diff] [blame] | 39 | #include "Python.h" |
Guido van Rossum | 97867b2 | 1996-08-08 19:09:53 +0000 | [diff] [blame] | 40 | #include <ctype.h> |
Guido van Rossum | 9722ad8 | 1995-09-22 23:49:28 +0000 | [diff] [blame] | 41 | |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 42 | #ifdef WITH_THREAD |
Guido van Rossum | 49b5606 | 1998-10-01 20:42:43 +0000 | [diff] [blame] | 43 | #include "pythread.h" |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 44 | #endif |
| 45 | |
Guido van Rossum | 2a5119b | 1998-05-29 01:28:40 +0000 | [diff] [blame] | 46 | #ifdef MS_WINDOWS |
| 47 | #include <windows.h> |
| 48 | #endif |
| 49 | |
Guido van Rossum | bf0dc9f | 1996-08-20 20:49:56 +0000 | [diff] [blame] | 50 | #ifdef macintosh |
| 51 | #define MAC_TCL |
Guido van Rossum | 49b5606 | 1998-10-01 20:42:43 +0000 | [diff] [blame] | 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 | 3e819a7 | 1997-08-01 19:29:02 +0000 | [diff] [blame] | 57 | #define TKMAJORMINOR (TK_MAJOR_VERSION*1000 + TK_MINOR_VERSION) |
| 58 | |
Guido van Rossum | a80649b | 2000-03-28 20:07:05 +0000 | [diff] [blame] | 59 | #if TKMAJORMINOR < 8000 |
| 60 | #error "Tk older than 8.0 not supported" |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 61 | #endif |
| 62 | |
Guido van Rossum | a80649b | 2000-03-28 20:07:05 +0000 | [diff] [blame] | 63 | #if defined(macintosh) |
Guido van Rossum | 0d2390c | 1997-08-14 19:57:07 +0000 | [diff] [blame] | 64 | /* Sigh, we have to include this to get at the tcl qd pointer */ |
| 65 | #include <tkMac.h> |
Guido van Rossum | 2ea1c94 | 1998-04-28 16:12:43 +0000 | [diff] [blame] | 66 | /* And this one we need to clear the menu bar */ |
| 67 | #include <Menus.h> |
Guido van Rossum | 0d2390c | 1997-08-14 19:57:07 +0000 | [diff] [blame] | 68 | #endif |
| 69 | |
Guido van Rossum | a80649b | 2000-03-28 20:07:05 +0000 | [diff] [blame] | 70 | #if !defined(MS_WINDOWS) |
Guido van Rossum | 0d2390c | 1997-08-14 19:57:07 +0000 | [diff] [blame] | 71 | #define HAVE_CREATEFILEHANDLER |
| 72 | #endif |
| 73 | |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 74 | #ifdef HAVE_CREATEFILEHANDLER |
| 75 | |
| 76 | /* Tcl_CreateFileHandler() changed several times; these macros deal with the |
| 77 | messiness. In Tcl 8.0 and later, it is not available on Windows (and on |
| 78 | Unix, only because Jack added it back); when available on Windows, it only |
| 79 | applies to sockets. */ |
| 80 | |
Guido van Rossum | 7bf1564 | 1998-05-22 18:28:17 +0000 | [diff] [blame] | 81 | #ifdef MS_WINDOWS |
| 82 | #define FHANDLETYPE TCL_WIN_SOCKET |
| 83 | #else |
| 84 | #define FHANDLETYPE TCL_UNIX_FD |
| 85 | #endif |
| 86 | |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 87 | /* If Tcl can wait for a Unix file descriptor, define the EventHook() routine |
| 88 | which uses this to handle Tcl events while the user is typing commands. */ |
| 89 | |
| 90 | #if FHANDLETYPE == TCL_UNIX_FD |
Guido van Rossum | 7bf1564 | 1998-05-22 18:28:17 +0000 | [diff] [blame] | 91 | #define WAIT_FOR_STDIN |
| 92 | #endif |
| 93 | |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 94 | #endif /* HAVE_CREATEFILEHANDLER */ |
| 95 | |
Guido van Rossum | ad4db17 | 1998-06-13 13:56:28 +0000 | [diff] [blame] | 96 | #ifdef MS_WINDOWS |
| 97 | #include <conio.h> |
| 98 | #define WAIT_FOR_STDIN |
| 99 | #endif |
| 100 | |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 101 | #ifdef WITH_THREAD |
| 102 | |
| 103 | /* The threading situation is complicated. Tcl is not thread-safe, except for |
| 104 | Tcl 8.1, which will probably remain in alpha status for another 6 months |
| 105 | (and the README says that Tk will probably remain thread-unsafe forever). |
| 106 | So we need to use a lock around all uses of Tcl. Previously, the Python |
| 107 | interpreter lock was used for this. However, this causes problems when |
| 108 | other Python threads need to run while Tcl is blocked waiting for events. |
| 109 | |
| 110 | To solve this problem, a separate lock for Tcl is introduced. Holding it |
| 111 | is incompatible with holding Python's interpreter lock. The following four |
| 112 | macros manipulate both locks together. |
| 113 | |
| 114 | ENTER_TCL and LEAVE_TCL are brackets, just like Py_BEGIN_ALLOW_THREADS and |
| 115 | Py_END_ALLOW_THREADS. They should be used whenever a call into Tcl is made |
| 116 | that could call an event handler, or otherwise affect the state of a Tcl |
| 117 | interpreter. These assume that the surrounding code has the Python |
| 118 | interpreter lock; inside the brackets, the Python interpreter lock has been |
| 119 | released and the lock for Tcl has been acquired. |
| 120 | |
Guido van Rossum | 5e97783 | 1998-06-15 14:03:52 +0000 | [diff] [blame] | 121 | Sometimes, it is necessary to have both the Python lock and the Tcl lock. |
| 122 | (For example, when transferring data from the Tcl interpreter result to a |
| 123 | Python string object.) This can be done by using different macros to close |
| 124 | the ENTER_TCL block: ENTER_OVERLAP reacquires the Python lock (and restores |
| 125 | the thread state) but doesn't release the Tcl lock; LEAVE_OVERLAP_TCL |
| 126 | releases the Tcl lock. |
| 127 | |
Guido van Rossum | dc1adab | 1998-10-09 20:51:18 +0000 | [diff] [blame] | 128 | By contrast, ENTER_PYTHON and LEAVE_PYTHON are used in Tcl event |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 129 | handlers when the handler needs to use Python. Such event handlers are |
| 130 | entered while the lock for Tcl is held; the event handler presumably needs |
Guido van Rossum | dc1adab | 1998-10-09 20:51:18 +0000 | [diff] [blame] | 131 | to use Python. ENTER_PYTHON releases the lock for Tcl and acquires |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 132 | the Python interpreter lock, restoring the appropriate thread state, and |
| 133 | LEAVE_PYTHON releases the Python interpreter lock and re-acquires the lock |
| 134 | for Tcl. It is okay for ENTER_TCL/LEAVE_TCL pairs to be contained inside |
Guido van Rossum | dc1adab | 1998-10-09 20:51:18 +0000 | [diff] [blame] | 135 | the code between ENTER_PYTHON and LEAVE_PYTHON. |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 136 | |
| 137 | These locks expand to several statements and brackets; they should not be |
| 138 | used in branches of if statements and the like. |
| 139 | |
| 140 | */ |
| 141 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 142 | static PyThread_type_lock tcl_lock = 0; |
Guido van Rossum | dc1adab | 1998-10-09 20:51:18 +0000 | [diff] [blame] | 143 | static PyThreadState *tcl_tstate = NULL; |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 144 | |
| 145 | #define ENTER_TCL \ |
Guido van Rossum | dc1adab | 1998-10-09 20:51:18 +0000 | [diff] [blame] | 146 | { PyThreadState *tstate = PyThreadState_Get(); Py_BEGIN_ALLOW_THREADS \ |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 147 | PyThread_acquire_lock(tcl_lock, 1); tcl_tstate = tstate; |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 148 | |
| 149 | #define LEAVE_TCL \ |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 150 | tcl_tstate = NULL; PyThread_release_lock(tcl_lock); Py_END_ALLOW_THREADS} |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 151 | |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 152 | #define ENTER_OVERLAP \ |
| 153 | Py_END_ALLOW_THREADS |
| 154 | |
| 155 | #define LEAVE_OVERLAP_TCL \ |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 156 | tcl_tstate = NULL; PyThread_release_lock(tcl_lock); } |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 157 | |
Guido van Rossum | dc1adab | 1998-10-09 20:51:18 +0000 | [diff] [blame] | 158 | #define ENTER_PYTHON \ |
| 159 | { PyThreadState *tstate = tcl_tstate; tcl_tstate = NULL; \ |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 160 | PyThread_release_lock(tcl_lock); PyEval_RestoreThread((tstate)); } |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 161 | |
| 162 | #define LEAVE_PYTHON \ |
Guido van Rossum | dc1adab | 1998-10-09 20:51:18 +0000 | [diff] [blame] | 163 | { PyThreadState *tstate = PyEval_SaveThread(); \ |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 164 | PyThread_acquire_lock(tcl_lock, 1); tcl_tstate = tstate; } |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 165 | |
| 166 | #else |
| 167 | |
| 168 | #define ENTER_TCL |
| 169 | #define LEAVE_TCL |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 170 | #define ENTER_OVERLAP |
| 171 | #define LEAVE_OVERLAP_TCL |
Guido van Rossum | dc1adab | 1998-10-09 20:51:18 +0000 | [diff] [blame] | 172 | #define ENTER_PYTHON |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 173 | #define LEAVE_PYTHON |
| 174 | |
| 175 | #endif |
| 176 | |
Guido van Rossum | ec22c92 | 1996-02-25 04:50:29 +0000 | [diff] [blame] | 177 | #ifdef macintosh |
| 178 | |
| 179 | /* |
| 180 | ** Additional cruft needed by Tcl/Tk on the Mac. |
Guido van Rossum | 97867b2 | 1996-08-08 19:09:53 +0000 | [diff] [blame] | 181 | ** 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] | 182 | */ |
| 183 | |
Guido van Rossum | 7ffa761 | 1996-08-13 21:10:16 +0000 | [diff] [blame] | 184 | /* ckfree() expects a char* */ |
Guido van Rossum | 97867b2 | 1996-08-08 19:09:53 +0000 | [diff] [blame] | 185 | #define FREECAST (char *) |
| 186 | |
Guido van Rossum | ec22c92 | 1996-02-25 04:50:29 +0000 | [diff] [blame] | 187 | #include <Events.h> /* For EventRecord */ |
| 188 | |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 189 | typedef int (*TclMacConvertEventPtr) (EventRecord *eventPtr); |
| 190 | void Tcl_MacSetEventProc (TclMacConvertEventPtr procPtr); |
| 191 | int TkMacConvertEvent (EventRecord *eventPtr); |
Guido van Rossum | ec22c92 | 1996-02-25 04:50:29 +0000 | [diff] [blame] | 192 | |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 193 | staticforward int PyMacConvertEvent (EventRecord *eventPtr); |
Guido van Rossum | ec22c92 | 1996-02-25 04:50:29 +0000 | [diff] [blame] | 194 | |
Guido van Rossum | 2ea1c94 | 1998-04-28 16:12:43 +0000 | [diff] [blame] | 195 | #if defined(__CFM68K__) && !defined(__USING_STATIC_LIBS__) |
| 196 | #pragma import on |
| 197 | #endif |
| 198 | |
| 199 | #include <SIOUX.h> |
| 200 | extern int SIOUXIsAppWindow(WindowPtr); |
| 201 | |
| 202 | #if defined(__CFM68K__) && !defined(__USING_STATIC_LIBS__) |
| 203 | #pragma import reset |
| 204 | #endif |
Guido van Rossum | ec22c92 | 1996-02-25 04:50:29 +0000 | [diff] [blame] | 205 | #endif /* macintosh */ |
| 206 | |
Guido van Rossum | 97867b2 | 1996-08-08 19:09:53 +0000 | [diff] [blame] | 207 | #ifndef FREECAST |
Guido van Rossum | 7ffa761 | 1996-08-13 21:10:16 +0000 | [diff] [blame] | 208 | #define FREECAST (char *) |
Guido van Rossum | 97867b2 | 1996-08-08 19:09:53 +0000 | [diff] [blame] | 209 | #endif |
| 210 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 211 | /**** Tkapp Object Declaration ****/ |
| 212 | |
| 213 | staticforward PyTypeObject Tkapp_Type; |
| 214 | |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 215 | typedef struct { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 216 | PyObject_HEAD |
| 217 | Tcl_Interp *interp; |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 218 | } TkappObject; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 219 | |
| 220 | #define Tkapp_Check(v) ((v)->ob_type == &Tkapp_Type) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 221 | #define Tkapp_Interp(v) (((TkappObject *) (v))->interp) |
| 222 | #define Tkapp_Result(v) (((TkappObject *) (v))->interp->result) |
| 223 | |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 224 | #define DEBUG_REFCNT(v) (printf("DEBUG: id=%p, refcnt=%i\n", \ |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 225 | (void *) v, ((PyObject *) v)->ob_refcnt)) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 226 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 227 | |
| 228 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 229 | /**** Error Handling ****/ |
| 230 | |
| 231 | static PyObject *Tkinter_TclError; |
Guido van Rossum | d308e2b | 1994-07-07 09:25:12 +0000 | [diff] [blame] | 232 | static int quitMainLoop = 0; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 233 | static int errorInCmd = 0; |
| 234 | static PyObject *excInCmd; |
| 235 | static PyObject *valInCmd; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 236 | static PyObject *trbInCmd; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 237 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 238 | |
| 239 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 240 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 241 | Tkinter_Error(PyObject *v) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 242 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 243 | PyErr_SetString(Tkinter_TclError, Tkapp_Result(v)); |
| 244 | return NULL; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 245 | } |
| 246 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 247 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 248 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 249 | /**** Utils ****/ |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 250 | |
| 251 | #ifdef WITH_THREAD |
Guido van Rossum | 2a5119b | 1998-05-29 01:28:40 +0000 | [diff] [blame] | 252 | #ifndef MS_WINDOWS |
Guido van Rossum | 541f241 | 1998-08-13 13:29:22 +0000 | [diff] [blame] | 253 | |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 254 | /* Millisecond sleep() for Unix platforms. */ |
| 255 | |
| 256 | static void |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 257 | Sleep(int milli) |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 258 | { |
| 259 | /* XXX Too bad if you don't have select(). */ |
| 260 | struct timeval t; |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 261 | t.tv_sec = milli/1000; |
| 262 | t.tv_usec = (milli%1000) * 1000; |
| 263 | select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &t); |
| 264 | } |
Guido van Rossum | 2a5119b | 1998-05-29 01:28:40 +0000 | [diff] [blame] | 265 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 266 | #endif /* WITH_THREAD */ |
| 267 | |
| 268 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 269 | static char * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 270 | AsString(PyObject *value, PyObject *tmp) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 271 | { |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 272 | if (PyString_Check(value)) |
| 273 | return PyString_AsString(value); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 274 | else { |
| 275 | PyObject *v = PyObject_Str(value); |
| 276 | PyList_Append(tmp, v); |
| 277 | Py_DECREF(v); |
| 278 | return PyString_AsString(v); |
| 279 | } |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 280 | } |
| 281 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 282 | |
| 283 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 284 | #define ARGSZ 64 |
| 285 | |
| 286 | static char * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 287 | Merge(PyObject *args) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 288 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 289 | PyObject *tmp = NULL; |
| 290 | char *argvStore[ARGSZ]; |
| 291 | char **argv = NULL; |
| 292 | int fvStore[ARGSZ]; |
| 293 | int *fv = NULL; |
| 294 | int argc = 0, i; |
| 295 | char *res = NULL; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 296 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 297 | if (!(tmp = PyList_New(0))) |
| 298 | return NULL; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 299 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 300 | argv = argvStore; |
| 301 | fv = fvStore; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 302 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 303 | if (args == NULL) |
| 304 | argc = 0; |
| 305 | |
| 306 | else if (!PyTuple_Check(args)) { |
| 307 | argc = 1; |
| 308 | fv[0] = 0; |
| 309 | argv[0] = AsString(args, tmp); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 310 | } |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 311 | else { |
| 312 | argc = PyTuple_Size(args); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 313 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 314 | if (argc > ARGSZ) { |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 315 | argv = (char **)ckalloc(argc * sizeof(char *)); |
| 316 | fv = (int *)ckalloc(argc * sizeof(int)); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 317 | if (argv == NULL || fv == NULL) { |
| 318 | PyErr_NoMemory(); |
| 319 | goto finally; |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | for (i = 0; i < argc; i++) { |
| 324 | PyObject *v = PyTuple_GetItem(args, i); |
| 325 | if (PyTuple_Check(v)) { |
| 326 | fv[i] = 1; |
| 327 | if (!(argv[i] = Merge(v))) |
| 328 | goto finally; |
| 329 | } |
| 330 | else if (v == Py_None) { |
| 331 | argc = i; |
| 332 | break; |
| 333 | } |
| 334 | else { |
| 335 | fv[i] = 0; |
| 336 | argv[i] = AsString(v, tmp); |
| 337 | } |
| 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 | res = Tcl_Merge(argc, argv); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 341 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 342 | finally: |
| 343 | for (i = 0; i < argc; i++) |
| 344 | if (fv[i]) { |
| 345 | ckfree(argv[i]); |
| 346 | } |
| 347 | if (argv != argvStore) |
| 348 | ckfree(FREECAST argv); |
| 349 | if (fv != fvStore) |
| 350 | ckfree(FREECAST fv); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 351 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 352 | Py_DECREF(tmp); |
| 353 | return res; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 354 | } |
| 355 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 356 | |
| 357 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 358 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 359 | Split(char *list) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 360 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 361 | int argc; |
| 362 | char **argv; |
| 363 | PyObject *v; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 364 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 365 | if (list == NULL) { |
| 366 | Py_INCREF(Py_None); |
| 367 | return Py_None; |
| 368 | } |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 369 | |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 370 | if (Tcl_SplitList((Tcl_Interp *)NULL, list, &argc, &argv) != TCL_OK) { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 371 | /* Not a list. |
| 372 | * Could be a quoted string containing funnies, e.g. {"}. |
| 373 | * Return the string itself. |
| 374 | */ |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 375 | return PyString_FromString(list); |
| 376 | } |
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 | if (argc == 0) |
| 379 | v = PyString_FromString(""); |
| 380 | else if (argc == 1) |
| 381 | v = PyString_FromString(argv[0]); |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 382 | else if ((v = PyTuple_New(argc)) != NULL) { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 383 | int i; |
| 384 | PyObject *w; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 385 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 386 | for (i = 0; i < argc; i++) { |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 387 | if ((w = Split(argv[i])) == NULL) { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 388 | Py_DECREF(v); |
| 389 | v = NULL; |
| 390 | break; |
| 391 | } |
| 392 | PyTuple_SetItem(v, i, w); |
| 393 | } |
| 394 | } |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 395 | Tcl_Free(FREECAST argv); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 396 | return v; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 397 | } |
| 398 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 399 | |
| 400 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 401 | /**** Tkapp Object ****/ |
| 402 | |
| 403 | #ifndef WITH_APPINIT |
| 404 | int |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 405 | Tcl_AppInit(Tcl_Interp *interp) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 406 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 407 | Tk_Window main; |
Guido van Rossum | ec22c92 | 1996-02-25 04:50:29 +0000 | [diff] [blame] | 408 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 409 | main = Tk_MainWindow(interp); |
| 410 | if (Tcl_Init(interp) == TCL_ERROR) { |
Guido van Rossum | b41addf | 1998-05-12 15:02:41 +0000 | [diff] [blame] | 411 | PySys_WriteStderr("Tcl_Init error: %s\n", interp->result); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 412 | return TCL_ERROR; |
| 413 | } |
| 414 | if (Tk_Init(interp) == TCL_ERROR) { |
Guido van Rossum | b41addf | 1998-05-12 15:02:41 +0000 | [diff] [blame] | 415 | PySys_WriteStderr("Tk_Init error: %s\n", interp->result); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 416 | return TCL_ERROR; |
| 417 | } |
| 418 | return TCL_OK; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 419 | } |
| 420 | #endif /* !WITH_APPINIT */ |
| 421 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 422 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 423 | |
| 424 | |
| 425 | /* Initialize the Tk application; see the `main' function in |
| 426 | * `tkMain.c'. |
| 427 | */ |
Guido van Rossum | 7bf1564 | 1998-05-22 18:28:17 +0000 | [diff] [blame] | 428 | |
Thomas Wouters | 58d0510 | 2000-07-24 14:43:35 +0000 | [diff] [blame] | 429 | static void EnableEventHook(void); /* Forward */ |
| 430 | static void DisableEventHook(void); /* Forward */ |
Guido van Rossum | 7bf1564 | 1998-05-22 18:28:17 +0000 | [diff] [blame] | 431 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 432 | static TkappObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 433 | Tkapp_New(char *screenName, char *baseName, char *className, int interactive) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 434 | { |
| 435 | TkappObject *v; |
| 436 | char *argv0; |
| 437 | |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 438 | v = PyObject_New(TkappObject, &Tkapp_Type); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 439 | if (v == NULL) |
| 440 | return NULL; |
| 441 | |
| 442 | v->interp = Tcl_CreateInterp(); |
Guido van Rossum | dfd428d | 1996-02-25 04:46:40 +0000 | [diff] [blame] | 443 | |
Guido van Rossum | a80649b | 2000-03-28 20:07:05 +0000 | [diff] [blame] | 444 | #if defined(macintosh) |
| 445 | /* This seems to be needed */ |
Guido van Rossum | 2ea1c94 | 1998-04-28 16:12:43 +0000 | [diff] [blame] | 446 | ClearMenuBar(); |
| 447 | TkMacInitMenus(v->interp); |
| 448 | #endif |
Guido van Rossum | 1c0d315 | 1998-02-19 21:28:49 +0000 | [diff] [blame] | 449 | /* Delete the 'exit' command, which can screw things up */ |
| 450 | Tcl_DeleteCommand(v->interp, "exit"); |
| 451 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 452 | if (screenName != NULL) |
| 453 | Tcl_SetVar2(v->interp, "env", "DISPLAY", |
| 454 | screenName, TCL_GLOBAL_ONLY); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 455 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 456 | if (interactive) |
| 457 | Tcl_SetVar(v->interp, "tcl_interactive", "1", TCL_GLOBAL_ONLY); |
| 458 | else |
| 459 | Tcl_SetVar(v->interp, "tcl_interactive", "0", TCL_GLOBAL_ONLY); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 460 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 461 | /* This is used to get the application class for Tk 4.1 and up */ |
| 462 | argv0 = (char*)ckalloc(strlen(className) + 1); |
| 463 | if (!argv0) { |
| 464 | PyErr_NoMemory(); |
| 465 | Py_DECREF(v); |
| 466 | return NULL; |
| 467 | } |
Guido van Rossum | 97867b2 | 1996-08-08 19:09:53 +0000 | [diff] [blame] | 468 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 469 | strcpy(argv0, className); |
Guido van Rossum | 730806d | 1998-04-10 22:27:42 +0000 | [diff] [blame] | 470 | if (isupper((int)(argv0[0]))) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 471 | argv0[0] = tolower(argv0[0]); |
| 472 | Tcl_SetVar(v->interp, "argv0", argv0, TCL_GLOBAL_ONLY); |
| 473 | ckfree(argv0); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 474 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 475 | if (Tcl_AppInit(v->interp) != TCL_OK) |
Guido van Rossum | c821d1e | 1998-07-07 22:25:47 +0000 | [diff] [blame] | 476 | return (TkappObject *)Tkinter_Error((PyObject *)v); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 477 | |
Guido van Rossum | 7bf1564 | 1998-05-22 18:28:17 +0000 | [diff] [blame] | 478 | EnableEventHook(); |
| 479 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 480 | return v; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 481 | } |
| 482 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 483 | |
| 484 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 485 | /** Tcl Eval **/ |
| 486 | |
Guido van Rossum | a1f0a8f | 2000-03-31 00:51:37 +0000 | [diff] [blame] | 487 | #if TKMAJORMINOR >= 8001 |
| 488 | #define USING_OBJECTS |
| 489 | #endif |
| 490 | |
| 491 | #ifdef USING_OBJECTS |
| 492 | |
| 493 | static Tcl_Obj* |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 494 | AsObj(PyObject *value) |
Guido van Rossum | a1f0a8f | 2000-03-31 00:51:37 +0000 | [diff] [blame] | 495 | { |
| 496 | Tcl_Obj *result; |
| 497 | |
| 498 | if (PyString_Check(value)) |
| 499 | return Tcl_NewStringObj(PyString_AS_STRING(value), |
| 500 | PyString_GET_SIZE(value)); |
| 501 | else if (PyInt_Check(value)) |
| 502 | return Tcl_NewLongObj(PyInt_AS_LONG(value)); |
| 503 | else if (PyFloat_Check(value)) |
| 504 | return Tcl_NewDoubleObj(PyFloat_AS_DOUBLE(value)); |
| 505 | else if (PyTuple_Check(value)) { |
| 506 | Tcl_Obj **argv = (Tcl_Obj**) |
| 507 | ckalloc(PyTuple_Size(value)*sizeof(Tcl_Obj*)); |
| 508 | int i; |
| 509 | if(!argv) |
| 510 | return 0; |
| 511 | for(i=0;i<PyTuple_Size(value);i++) |
| 512 | argv[i] = AsObj(PyTuple_GetItem(value,i)); |
| 513 | result = Tcl_NewListObj(PyTuple_Size(value), argv); |
| 514 | ckfree(FREECAST argv); |
| 515 | return result; |
| 516 | } |
| 517 | else if (PyUnicode_Check(value)) { |
Guido van Rossum | 990f5c6 | 2000-05-04 15:07:16 +0000 | [diff] [blame] | 518 | #if TKMAJORMINOR <= 8001 |
| 519 | /* In Tcl 8.1 we must use UTF-8 */ |
Guido van Rossum | a1f0a8f | 2000-03-31 00:51:37 +0000 | [diff] [blame] | 520 | PyObject* utf8 = PyUnicode_AsUTF8String (value); |
| 521 | if (!utf8) |
| 522 | return 0; |
Guido van Rossum | 8823acc | 2000-04-27 20:14:31 +0000 | [diff] [blame] | 523 | result = Tcl_NewStringObj (PyString_AS_STRING (utf8), |
Guido van Rossum | a1f0a8f | 2000-03-31 00:51:37 +0000 | [diff] [blame] | 524 | PyString_GET_SIZE (utf8)); |
Guido van Rossum | 8823acc | 2000-04-27 20:14:31 +0000 | [diff] [blame] | 525 | Py_DECREF(utf8); |
| 526 | return result; |
Guido van Rossum | 990f5c6 | 2000-05-04 15:07:16 +0000 | [diff] [blame] | 527 | #else /* TKMAJORMINOR > 8001 */ |
| 528 | /* In Tcl 8.2 and later, use Tcl_NewUnicodeObj() */ |
| 529 | if (sizeof(Py_UNICODE) != sizeof(Tcl_UniChar)) { |
| 530 | /* XXX Should really test this at compile time */ |
| 531 | PyErr_SetString(PyExc_SystemError, |
| 532 | "Py_UNICODE and Tcl_UniChar differ in size"); |
| 533 | return 0; |
| 534 | } |
| 535 | return Tcl_NewUnicodeObj(PyUnicode_AS_UNICODE(value), |
| 536 | PyUnicode_GET_SIZE(value)); |
| 537 | #endif /* TKMAJORMINOR > 8001 */ |
Guido van Rossum | a1f0a8f | 2000-03-31 00:51:37 +0000 | [diff] [blame] | 538 | } |
| 539 | else { |
| 540 | PyObject *v = PyObject_Str(value); |
| 541 | if (!v) |
| 542 | return 0; |
| 543 | result = AsObj(v); |
| 544 | Py_DECREF(v); |
| 545 | return result; |
| 546 | } |
| 547 | } |
| 548 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 549 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 550 | Tkapp_Call(PyObject *self, PyObject *args) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 551 | { |
Guido van Rossum | 632de27 | 2000-03-29 00:19:50 +0000 | [diff] [blame] | 552 | Tcl_Obj *objStore[ARGSZ]; |
| 553 | Tcl_Obj **objv = NULL; |
| 554 | int objc = 0, i; |
| 555 | PyObject *res = NULL; |
| 556 | Tcl_Interp *interp = Tkapp_Interp(self); |
| 557 | /* Could add TCL_EVAL_GLOBAL if wrapped by GlobalCall... */ |
| 558 | int flags = TCL_EVAL_DIRECT; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 559 | |
Guido van Rossum | 632de27 | 2000-03-29 00:19:50 +0000 | [diff] [blame] | 560 | objv = objStore; |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 561 | |
Guido van Rossum | 212643f | 1998-04-29 16:22:14 +0000 | [diff] [blame] | 562 | if (args == NULL) |
Guido van Rossum | 632de27 | 2000-03-29 00:19:50 +0000 | [diff] [blame] | 563 | objc = 0; |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 564 | |
Guido van Rossum | 212643f | 1998-04-29 16:22:14 +0000 | [diff] [blame] | 565 | else if (!PyTuple_Check(args)) { |
Guido van Rossum | 632de27 | 2000-03-29 00:19:50 +0000 | [diff] [blame] | 566 | objc = 1; |
| 567 | objv[0] = AsObj(args); |
| 568 | if (objv[0] == 0) |
| 569 | goto finally; |
| 570 | Tcl_IncrRefCount(objv[0]); |
Guido van Rossum | 212643f | 1998-04-29 16:22:14 +0000 | [diff] [blame] | 571 | } |
| 572 | else { |
Guido van Rossum | 632de27 | 2000-03-29 00:19:50 +0000 | [diff] [blame] | 573 | objc = PyTuple_Size(args); |
Guido van Rossum | 212643f | 1998-04-29 16:22:14 +0000 | [diff] [blame] | 574 | |
Guido van Rossum | 632de27 | 2000-03-29 00:19:50 +0000 | [diff] [blame] | 575 | if (objc > ARGSZ) { |
| 576 | objv = (Tcl_Obj **)ckalloc(objc * sizeof(char *)); |
| 577 | if (objv == NULL) { |
Guido van Rossum | 212643f | 1998-04-29 16:22:14 +0000 | [diff] [blame] | 578 | PyErr_NoMemory(); |
| 579 | goto finally; |
| 580 | } |
| 581 | } |
| 582 | |
Guido van Rossum | 632de27 | 2000-03-29 00:19:50 +0000 | [diff] [blame] | 583 | for (i = 0; i < objc; i++) { |
Guido van Rossum | 212643f | 1998-04-29 16:22:14 +0000 | [diff] [blame] | 584 | PyObject *v = PyTuple_GetItem(args, i); |
Guido van Rossum | 64231e5 | 2000-03-31 03:29:39 +0000 | [diff] [blame] | 585 | if (v == Py_None) { |
| 586 | objc = i; |
| 587 | break; |
| 588 | } |
Guido van Rossum | 632de27 | 2000-03-29 00:19:50 +0000 | [diff] [blame] | 589 | objv[i] = AsObj(v); |
| 590 | if (!objv[i]) |
| 591 | goto finally; |
| 592 | Tcl_IncrRefCount(objv[i]); |
Guido van Rossum | 212643f | 1998-04-29 16:22:14 +0000 | [diff] [blame] | 593 | } |
| 594 | } |
Guido van Rossum | 212643f | 1998-04-29 16:22:14 +0000 | [diff] [blame] | 595 | |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 596 | ENTER_TCL |
Guido van Rossum | 632de27 | 2000-03-29 00:19:50 +0000 | [diff] [blame] | 597 | |
| 598 | i = Tcl_EvalObjv(interp, objc, objv, flags); |
| 599 | |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 600 | ENTER_OVERLAP |
Guido van Rossum | 632de27 | 2000-03-29 00:19:50 +0000 | [diff] [blame] | 601 | if (i == TCL_ERROR) |
Guido van Rossum | 212643f | 1998-04-29 16:22:14 +0000 | [diff] [blame] | 602 | Tkinter_Error(self); |
Guido van Rossum | 990f5c6 | 2000-05-04 15:07:16 +0000 | [diff] [blame] | 603 | else { |
Guido van Rossum | 632de27 | 2000-03-29 00:19:50 +0000 | [diff] [blame] | 604 | /* We could request the object result here, but doing |
| 605 | so would confuse applications that expect a string. */ |
Guido van Rossum | 990f5c6 | 2000-05-04 15:07:16 +0000 | [diff] [blame] | 606 | char *s = Tcl_GetStringResult(interp); |
| 607 | char *p = s; |
| 608 | /* If the result contains any bytes with the top bit set, |
| 609 | it's UTF-8 and we should decode it to Unicode */ |
| 610 | while (*p != '\0') { |
| 611 | if (*p & 0x80) |
| 612 | break; |
| 613 | p++; |
| 614 | } |
| 615 | if (*p == '\0') |
| 616 | res = PyString_FromStringAndSize(s, (int)(p-s)); |
| 617 | else { |
| 618 | /* Convert UTF-8 to Unicode string */ |
| 619 | p = strchr(p, '\0'); |
Guido van Rossum | 69529ad | 2000-05-04 15:55:17 +0000 | [diff] [blame] | 620 | res = PyUnicode_DecodeUTF8(s, (int)(p-s), "strict"); |
| 621 | if (res == NULL) { |
| 622 | PyErr_Clear(); |
| 623 | res = PyString_FromStringAndSize(s, (int)(p-s)); |
| 624 | } |
Guido van Rossum | 990f5c6 | 2000-05-04 15:07:16 +0000 | [diff] [blame] | 625 | } |
| 626 | } |
Guido van Rossum | 632de27 | 2000-03-29 00:19:50 +0000 | [diff] [blame] | 627 | |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 628 | LEAVE_OVERLAP_TCL |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 629 | |
Guido van Rossum | 212643f | 1998-04-29 16:22:14 +0000 | [diff] [blame] | 630 | finally: |
Guido van Rossum | 632de27 | 2000-03-29 00:19:50 +0000 | [diff] [blame] | 631 | for (i = 0; i < objc; i++) |
| 632 | Tcl_DecrRefCount(objv[i]); |
| 633 | if (objv != objStore) |
| 634 | ckfree(FREECAST objv); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 635 | return res; |
| 636 | } |
| 637 | |
Guido van Rossum | a1f0a8f | 2000-03-31 00:51:37 +0000 | [diff] [blame] | 638 | #else /* !USING_OBJECTS */ |
| 639 | |
| 640 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 641 | Tkapp_Call(PyObject *self, PyObject *args) |
Guido van Rossum | a1f0a8f | 2000-03-31 00:51:37 +0000 | [diff] [blame] | 642 | { |
| 643 | /* This is copied from Merge() */ |
| 644 | PyObject *tmp = NULL; |
| 645 | char *argvStore[ARGSZ]; |
| 646 | char **argv = NULL; |
| 647 | int fvStore[ARGSZ]; |
| 648 | int *fv = NULL; |
| 649 | int argc = 0, i; |
| 650 | PyObject *res = NULL; /* except this has a different type */ |
| 651 | Tcl_CmdInfo info; /* and this is added */ |
| 652 | Tcl_Interp *interp = Tkapp_Interp(self); /* and this too */ |
| 653 | |
| 654 | if (!(tmp = PyList_New(0))) |
| 655 | return NULL; |
| 656 | |
| 657 | argv = argvStore; |
| 658 | fv = fvStore; |
| 659 | |
| 660 | if (args == NULL) |
| 661 | argc = 0; |
| 662 | |
| 663 | else if (!PyTuple_Check(args)) { |
| 664 | argc = 1; |
| 665 | fv[0] = 0; |
| 666 | argv[0] = AsString(args, tmp); |
| 667 | } |
| 668 | else { |
| 669 | argc = PyTuple_Size(args); |
| 670 | |
| 671 | if (argc > ARGSZ) { |
| 672 | argv = (char **)ckalloc(argc * sizeof(char *)); |
| 673 | fv = (int *)ckalloc(argc * sizeof(int)); |
| 674 | if (argv == NULL || fv == NULL) { |
| 675 | PyErr_NoMemory(); |
| 676 | goto finally; |
| 677 | } |
| 678 | } |
| 679 | |
| 680 | for (i = 0; i < argc; i++) { |
| 681 | PyObject *v = PyTuple_GetItem(args, i); |
| 682 | if (PyTuple_Check(v)) { |
| 683 | fv[i] = 1; |
| 684 | if (!(argv[i] = Merge(v))) |
| 685 | goto finally; |
| 686 | } |
| 687 | else if (v == Py_None) { |
| 688 | argc = i; |
| 689 | break; |
| 690 | } |
| 691 | else { |
| 692 | fv[i] = 0; |
| 693 | argv[i] = AsString(v, tmp); |
| 694 | } |
| 695 | } |
| 696 | } |
| 697 | /* End code copied from Merge() */ |
| 698 | |
| 699 | /* All this to avoid a call to Tcl_Merge() and the corresponding call |
| 700 | to Tcl_SplitList() inside Tcl_Eval()... It can save a bundle! */ |
| 701 | if (Py_VerboseFlag >= 2) { |
| 702 | for (i = 0; i < argc; i++) |
| 703 | PySys_WriteStderr("%s ", argv[i]); |
| 704 | } |
| 705 | ENTER_TCL |
| 706 | info.proc = NULL; |
| 707 | if (argc < 1 || |
| 708 | !Tcl_GetCommandInfo(interp, argv[0], &info) || |
| 709 | info.proc == NULL) |
| 710 | { |
| 711 | char *cmd; |
| 712 | cmd = Tcl_Merge(argc, argv); |
| 713 | i = Tcl_Eval(interp, cmd); |
| 714 | ckfree(cmd); |
| 715 | } |
| 716 | else { |
| 717 | Tcl_ResetResult(interp); |
| 718 | i = (*info.proc)(info.clientData, interp, argc, argv); |
| 719 | } |
| 720 | ENTER_OVERLAP |
| 721 | if (info.proc == NULL && Py_VerboseFlag >= 2) |
| 722 | PySys_WriteStderr("... use TclEval "); |
| 723 | if (i == TCL_ERROR) { |
| 724 | if (Py_VerboseFlag >= 2) |
| 725 | PySys_WriteStderr("... error: '%s'\n", |
| 726 | interp->result); |
| 727 | Tkinter_Error(self); |
| 728 | } |
| 729 | else { |
| 730 | if (Py_VerboseFlag >= 2) |
| 731 | PySys_WriteStderr("-> '%s'\n", interp->result); |
| 732 | res = PyString_FromString(interp->result); |
| 733 | } |
| 734 | LEAVE_OVERLAP_TCL |
| 735 | |
| 736 | /* Copied from Merge() again */ |
| 737 | finally: |
| 738 | for (i = 0; i < argc; i++) |
| 739 | if (fv[i]) { |
| 740 | ckfree(argv[i]); |
| 741 | } |
| 742 | if (argv != argvStore) |
| 743 | ckfree(FREECAST argv); |
| 744 | if (fv != fvStore) |
| 745 | ckfree(FREECAST fv); |
| 746 | |
| 747 | Py_DECREF(tmp); |
| 748 | return res; |
| 749 | } |
| 750 | |
| 751 | #endif /* !USING_OBJECTS */ |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 752 | |
| 753 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 754 | Tkapp_GlobalCall(PyObject *self, PyObject *args) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 755 | { |
Guido van Rossum | 212643f | 1998-04-29 16:22:14 +0000 | [diff] [blame] | 756 | /* Could do the same here as for Tkapp_Call(), but this is not used |
| 757 | much, so I can't be bothered. Unfortunately Tcl doesn't export a |
| 758 | way for the user to do what all its Global* variants do (save and |
| 759 | reset the scope pointer, call the local version, restore the saved |
| 760 | scope pointer). */ |
| 761 | |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 762 | char *cmd; |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 763 | PyObject *res = NULL; |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 764 | |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 765 | cmd = Merge(args); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 766 | if (!cmd) |
| 767 | PyErr_SetString(Tkinter_TclError, "merge failed"); |
| 768 | |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 769 | else { |
| 770 | int err; |
| 771 | ENTER_TCL |
| 772 | err = Tcl_GlobalEval(Tkapp_Interp(self), cmd); |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 773 | ENTER_OVERLAP |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 774 | if (err == TCL_ERROR) |
| 775 | res = Tkinter_Error(self); |
| 776 | else |
| 777 | res = PyString_FromString(Tkapp_Result(self)); |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 778 | LEAVE_OVERLAP_TCL |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 779 | } |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 780 | |
| 781 | if (cmd) |
| 782 | ckfree(cmd); |
| 783 | |
| 784 | return res; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 785 | } |
| 786 | |
| 787 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 788 | Tkapp_Eval(PyObject *self, PyObject *args) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 789 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 790 | char *script; |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 791 | PyObject *res = NULL; |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 792 | int err; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 793 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 794 | if (!PyArg_ParseTuple(args, "s:eval", &script)) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 795 | return NULL; |
| 796 | |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 797 | ENTER_TCL |
| 798 | err = Tcl_Eval(Tkapp_Interp(self), script); |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 799 | ENTER_OVERLAP |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 800 | if (err == TCL_ERROR) |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 801 | res = Tkinter_Error(self); |
| 802 | else |
| 803 | res = PyString_FromString(Tkapp_Result(self)); |
| 804 | LEAVE_OVERLAP_TCL |
| 805 | return res; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 806 | } |
| 807 | |
| 808 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 809 | Tkapp_GlobalEval(PyObject *self, PyObject *args) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 810 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 811 | char *script; |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 812 | PyObject *res = NULL; |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 813 | int err; |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 814 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 815 | if (!PyArg_ParseTuple(args, "s:globaleval", &script)) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 816 | return NULL; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 817 | |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 818 | ENTER_TCL |
| 819 | err = Tcl_GlobalEval(Tkapp_Interp(self), script); |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 820 | ENTER_OVERLAP |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 821 | if (err == TCL_ERROR) |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 822 | res = Tkinter_Error(self); |
| 823 | else |
| 824 | res = PyString_FromString(Tkapp_Result(self)); |
| 825 | LEAVE_OVERLAP_TCL |
| 826 | return res; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 827 | } |
| 828 | |
| 829 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 830 | Tkapp_EvalFile(PyObject *self, PyObject *args) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 831 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 832 | char *fileName; |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 833 | PyObject *res = NULL; |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 834 | int err; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 835 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 836 | if (!PyArg_ParseTuple(args, "s:evalfile", &fileName)) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 837 | return NULL; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 838 | |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 839 | ENTER_TCL |
| 840 | err = Tcl_EvalFile(Tkapp_Interp(self), fileName); |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 841 | ENTER_OVERLAP |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 842 | if (err == TCL_ERROR) |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 843 | res = Tkinter_Error(self); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 844 | |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 845 | else |
| 846 | res = PyString_FromString(Tkapp_Result(self)); |
| 847 | LEAVE_OVERLAP_TCL |
| 848 | return res; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 849 | } |
| 850 | |
| 851 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 852 | Tkapp_Record(PyObject *self, PyObject *args) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 853 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 854 | char *script; |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 855 | PyObject *res = NULL; |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 856 | int err; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 857 | |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 858 | if (!PyArg_ParseTuple(args, "s", &script)) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 859 | return NULL; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 860 | |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 861 | ENTER_TCL |
| 862 | err = Tcl_RecordAndEval(Tkapp_Interp(self), script, TCL_NO_EVAL); |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 863 | ENTER_OVERLAP |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 864 | if (err == TCL_ERROR) |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 865 | res = Tkinter_Error(self); |
| 866 | else |
| 867 | res = PyString_FromString(Tkapp_Result(self)); |
| 868 | LEAVE_OVERLAP_TCL |
| 869 | return res; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 870 | } |
| 871 | |
| 872 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 873 | Tkapp_AddErrorInfo(PyObject *self, PyObject *args) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 874 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 875 | char *msg; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 876 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 877 | if (!PyArg_ParseTuple(args, "s:adderrorinfo", &msg)) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 878 | return NULL; |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 879 | ENTER_TCL |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 880 | Tcl_AddErrorInfo(Tkapp_Interp(self), msg); |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 881 | LEAVE_TCL |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 882 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 883 | Py_INCREF(Py_None); |
| 884 | return Py_None; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 885 | } |
| 886 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 887 | |
| 888 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 889 | /** Tcl Variable **/ |
| 890 | |
| 891 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 892 | SetVar(PyObject *self, PyObject *args, int flags) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 893 | { |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 894 | char *name1, *name2, *ok, *s; |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 895 | PyObject *newValue; |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 896 | PyObject *tmp; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 897 | |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 898 | tmp = PyList_New(0); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 899 | if (!tmp) |
| 900 | return NULL; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 901 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 902 | if (PyArg_ParseTuple(args, "sO:setvar", &name1, &newValue)) { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 903 | /* XXX Merge? */ |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 904 | s = AsString(newValue, tmp); |
| 905 | ENTER_TCL |
| 906 | ok = Tcl_SetVar(Tkapp_Interp(self), name1, s, flags); |
| 907 | LEAVE_TCL |
| 908 | } |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 909 | else { |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 910 | PyErr_Clear(); |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 911 | if (PyArg_ParseTuple(args, "ssO:setvar", &name1, &name2, &newValue)) { |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 912 | s = AsString (newValue, tmp); |
| 913 | ENTER_TCL |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 914 | ok = Tcl_SetVar2(Tkapp_Interp(self), name1, name2, |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 915 | s, flags); |
| 916 | LEAVE_TCL |
| 917 | } |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 918 | else { |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 919 | Py_DECREF(tmp); |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 920 | return NULL; |
| 921 | } |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 922 | } |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 923 | Py_DECREF(tmp); |
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 | if (!ok) |
| 926 | return Tkinter_Error(self); |
| 927 | |
| 928 | Py_INCREF(Py_None); |
| 929 | return Py_None; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 930 | } |
| 931 | |
| 932 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 933 | Tkapp_SetVar(PyObject *self, PyObject *args) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 934 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 935 | return SetVar(self, args, TCL_LEAVE_ERR_MSG); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 936 | } |
| 937 | |
| 938 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 939 | Tkapp_GlobalSetVar(PyObject *self, PyObject *args) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 940 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 941 | return SetVar(self, args, TCL_LEAVE_ERR_MSG | TCL_GLOBAL_ONLY); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 942 | } |
| 943 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 944 | |
| 945 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 946 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 947 | GetVar(PyObject *self, PyObject *args, int flags) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 948 | { |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 949 | char *name1, *name2=NULL, *s; |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 950 | PyObject *res = NULL; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 951 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 952 | if (!PyArg_ParseTuple(args, "s|s:getvar", &name1, &name2)) |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 953 | return NULL; |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 954 | ENTER_TCL |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 955 | if (name2 == NULL) |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 956 | s = Tcl_GetVar(Tkapp_Interp(self), name1, flags); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 957 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 958 | else |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 959 | s = Tcl_GetVar2(Tkapp_Interp(self), name1, name2, flags); |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 960 | ENTER_OVERLAP |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 961 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 962 | if (s == NULL) |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 963 | res = Tkinter_Error(self); |
| 964 | else |
| 965 | res = PyString_FromString(s); |
| 966 | LEAVE_OVERLAP_TCL |
| 967 | return res; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 968 | } |
| 969 | |
| 970 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 971 | Tkapp_GetVar(PyObject *self, PyObject *args) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 972 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 973 | return GetVar(self, args, TCL_LEAVE_ERR_MSG); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 974 | } |
| 975 | |
| 976 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 977 | Tkapp_GlobalGetVar(PyObject *self, PyObject *args) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 978 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 979 | return GetVar(self, args, TCL_LEAVE_ERR_MSG | TCL_GLOBAL_ONLY); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 980 | } |
| 981 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 982 | |
| 983 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 984 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 985 | UnsetVar(PyObject *self, PyObject *args, int flags) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 986 | { |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 987 | char *name1, *name2=NULL; |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 988 | PyObject *res = NULL; |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 989 | int code; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 990 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 991 | if (!PyArg_ParseTuple(args, "s|s:unsetvar", &name1, &name2)) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 992 | return NULL; |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 993 | ENTER_TCL |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 994 | if (name2 == NULL) |
| 995 | code = Tcl_UnsetVar(Tkapp_Interp(self), name1, flags); |
| 996 | |
| 997 | else |
| 998 | code = Tcl_UnsetVar2(Tkapp_Interp(self), name1, name2, flags); |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 999 | ENTER_OVERLAP |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1000 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1001 | if (code == TCL_ERROR) |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 1002 | res = Tkinter_Error(self); |
| 1003 | else { |
| 1004 | Py_INCREF(Py_None); |
| 1005 | res = Py_None; |
| 1006 | } |
| 1007 | LEAVE_OVERLAP_TCL |
| 1008 | return res; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1009 | } |
| 1010 | |
| 1011 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 1012 | Tkapp_UnsetVar(PyObject *self, PyObject *args) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1013 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1014 | return UnsetVar(self, args, TCL_LEAVE_ERR_MSG); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1015 | } |
| 1016 | |
| 1017 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 1018 | Tkapp_GlobalUnsetVar(PyObject *self, PyObject *args) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1019 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1020 | return UnsetVar(self, args, TCL_LEAVE_ERR_MSG | TCL_GLOBAL_ONLY); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1021 | } |
| 1022 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1023 | |
| 1024 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1025 | /** Tcl to Python **/ |
| 1026 | |
| 1027 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 1028 | Tkapp_GetInt(PyObject *self, PyObject *args) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1029 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1030 | char *s; |
| 1031 | int v; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1032 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 1033 | if (!PyArg_ParseTuple(args, "s:getint", &s)) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1034 | return NULL; |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1035 | if (Tcl_GetInt(Tkapp_Interp(self), s, &v) == TCL_ERROR) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1036 | return Tkinter_Error(self); |
| 1037 | return Py_BuildValue("i", v); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1038 | } |
| 1039 | |
| 1040 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 1041 | Tkapp_GetDouble(PyObject *self, PyObject *args) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1042 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1043 | char *s; |
| 1044 | double v; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1045 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 1046 | if (!PyArg_ParseTuple(args, "s:getdouble", &s)) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1047 | return NULL; |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1048 | if (Tcl_GetDouble(Tkapp_Interp(self), s, &v) == TCL_ERROR) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1049 | return Tkinter_Error(self); |
| 1050 | return Py_BuildValue("d", v); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1051 | } |
| 1052 | |
| 1053 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 1054 | Tkapp_GetBoolean(PyObject *self, PyObject *args) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1055 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1056 | char *s; |
| 1057 | int v; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1058 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 1059 | if (!PyArg_ParseTuple(args, "s:getboolean", &s)) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1060 | return NULL; |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1061 | if (Tcl_GetBoolean(Tkapp_Interp(self), s, &v) == TCL_ERROR) |
| 1062 | return Tkinter_Error(self); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1063 | return Py_BuildValue("i", v); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1064 | } |
| 1065 | |
| 1066 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 1067 | Tkapp_ExprString(PyObject *self, PyObject *args) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1068 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1069 | char *s; |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 1070 | PyObject *res = NULL; |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1071 | int retval; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1072 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 1073 | if (!PyArg_ParseTuple(args, "s:exprstring", &s)) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1074 | return NULL; |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1075 | ENTER_TCL |
| 1076 | retval = Tcl_ExprString(Tkapp_Interp(self), s); |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 1077 | ENTER_OVERLAP |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1078 | if (retval == TCL_ERROR) |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 1079 | res = Tkinter_Error(self); |
| 1080 | else |
| 1081 | res = Py_BuildValue("s", Tkapp_Result(self)); |
| 1082 | LEAVE_OVERLAP_TCL |
| 1083 | return res; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1084 | } |
| 1085 | |
| 1086 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 1087 | Tkapp_ExprLong(PyObject *self, PyObject *args) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1088 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1089 | char *s; |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 1090 | PyObject *res = NULL; |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1091 | int retval; |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1092 | long v; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1093 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 1094 | if (!PyArg_ParseTuple(args, "s:exprlong", &s)) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1095 | return NULL; |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1096 | ENTER_TCL |
| 1097 | retval = Tcl_ExprLong(Tkapp_Interp(self), s, &v); |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 1098 | ENTER_OVERLAP |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1099 | if (retval == TCL_ERROR) |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 1100 | res = Tkinter_Error(self); |
| 1101 | else |
| 1102 | res = Py_BuildValue("l", v); |
| 1103 | LEAVE_OVERLAP_TCL |
| 1104 | return res; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1105 | } |
| 1106 | |
| 1107 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 1108 | Tkapp_ExprDouble(PyObject *self, PyObject *args) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1109 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1110 | char *s; |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 1111 | PyObject *res = NULL; |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1112 | double v; |
Guido van Rossum | 52fa3a6 | 1997-02-14 22:59:58 +0000 | [diff] [blame] | 1113 | int retval; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1114 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 1115 | if (!PyArg_ParseTuple(args, "s:exprdouble", &s)) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1116 | return NULL; |
Guido van Rossum | 52fa3a6 | 1997-02-14 22:59:58 +0000 | [diff] [blame] | 1117 | PyFPE_START_PROTECT("Tkapp_ExprDouble", return 0) |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1118 | ENTER_TCL |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1119 | retval = Tcl_ExprDouble(Tkapp_Interp(self), s, &v); |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 1120 | ENTER_OVERLAP |
Guido van Rossum | 45b8391 | 1997-03-14 04:32:50 +0000 | [diff] [blame] | 1121 | PyFPE_END_PROTECT(retval) |
Guido van Rossum | 52fa3a6 | 1997-02-14 22:59:58 +0000 | [diff] [blame] | 1122 | if (retval == TCL_ERROR) |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 1123 | res = Tkinter_Error(self); |
| 1124 | else |
| 1125 | res = Py_BuildValue("d", v); |
| 1126 | LEAVE_OVERLAP_TCL |
| 1127 | return res; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1128 | } |
| 1129 | |
| 1130 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 1131 | Tkapp_ExprBoolean(PyObject *self, PyObject *args) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1132 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1133 | char *s; |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 1134 | PyObject *res = NULL; |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1135 | int retval; |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1136 | int v; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1137 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 1138 | if (!PyArg_ParseTuple(args, "s:exprboolean", &s)) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1139 | return NULL; |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1140 | ENTER_TCL |
| 1141 | retval = Tcl_ExprBoolean(Tkapp_Interp(self), s, &v); |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 1142 | ENTER_OVERLAP |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1143 | if (retval == TCL_ERROR) |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 1144 | res = Tkinter_Error(self); |
| 1145 | else |
| 1146 | res = Py_BuildValue("i", v); |
| 1147 | LEAVE_OVERLAP_TCL |
| 1148 | return res; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1149 | } |
| 1150 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1151 | |
| 1152 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1153 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 1154 | Tkapp_SplitList(PyObject *self, PyObject *args) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1155 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1156 | char *list; |
| 1157 | int argc; |
| 1158 | char **argv; |
| 1159 | PyObject *v; |
| 1160 | int i; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1161 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 1162 | if (!PyArg_ParseTuple(args, "s:splitlist", &list)) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1163 | return NULL; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1164 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1165 | if (Tcl_SplitList(Tkapp_Interp(self), list, &argc, &argv) == TCL_ERROR) |
| 1166 | return Tkinter_Error(self); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1167 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1168 | if (!(v = PyTuple_New(argc))) |
| 1169 | return NULL; |
| 1170 | |
| 1171 | for (i = 0; i < argc; i++) { |
| 1172 | PyObject *s = PyString_FromString(argv[i]); |
| 1173 | if (!s || PyTuple_SetItem(v, i, s)) { |
| 1174 | Py_DECREF(v); |
| 1175 | v = NULL; |
| 1176 | goto finally; |
| 1177 | } |
| 1178 | } |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1179 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1180 | finally: |
| 1181 | ckfree(FREECAST argv); |
| 1182 | return v; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1183 | } |
| 1184 | |
| 1185 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 1186 | Tkapp_Split(PyObject *self, PyObject *args) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1187 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1188 | char *list; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1189 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 1190 | if (!PyArg_ParseTuple(args, "s:split", &list)) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1191 | return NULL; |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1192 | return Split(list); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1193 | } |
| 1194 | |
| 1195 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 1196 | Tkapp_Merge(PyObject *self, PyObject *args) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1197 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1198 | char *s = Merge(args); |
| 1199 | PyObject *res = NULL; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1200 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1201 | if (s) { |
| 1202 | res = PyString_FromString(s); |
| 1203 | ckfree(s); |
| 1204 | } |
| 1205 | else |
| 1206 | PyErr_SetString(Tkinter_TclError, "merge failed"); |
| 1207 | |
| 1208 | return res; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1209 | } |
| 1210 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1211 | |
| 1212 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1213 | /** Tcl Command **/ |
| 1214 | |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1215 | /* Client data struct */ |
| 1216 | typedef struct { |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1217 | PyObject *self; |
| 1218 | PyObject *func; |
| 1219 | } PythonCmd_ClientData; |
| 1220 | |
| 1221 | static int |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 1222 | PythonCmd_Error(Tcl_Interp *interp) |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1223 | { |
| 1224 | errorInCmd = 1; |
| 1225 | PyErr_Fetch(&excInCmd, &valInCmd, &trbInCmd); |
| 1226 | LEAVE_PYTHON |
| 1227 | return TCL_ERROR; |
| 1228 | } |
| 1229 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1230 | /* This is the Tcl command that acts as a wrapper for Python |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1231 | * function or method. |
| 1232 | */ |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1233 | static int |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 1234 | PythonCmd(ClientData clientData, Tcl_Interp *interp, int argc, char *argv[]) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1235 | { |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1236 | PythonCmd_ClientData *data = (PythonCmd_ClientData *)clientData; |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1237 | PyObject *self, *func, *arg, *res, *tmp; |
| 1238 | int i; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1239 | |
Guido van Rossum | dc1adab | 1998-10-09 20:51:18 +0000 | [diff] [blame] | 1240 | ENTER_PYTHON |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1241 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1242 | /* TBD: no error checking here since we know, via the |
| 1243 | * Tkapp_CreateCommand() that the client data is a two-tuple |
| 1244 | */ |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1245 | self = data->self; |
| 1246 | func = data->func; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1247 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1248 | /* Create argument list (argv1, ..., argvN) */ |
| 1249 | if (!(arg = PyTuple_New(argc - 1))) |
| 1250 | return PythonCmd_Error(interp); |
Guido van Rossum | d308e2b | 1994-07-07 09:25:12 +0000 | [diff] [blame] | 1251 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1252 | for (i = 0; i < (argc - 1); i++) { |
| 1253 | PyObject *s = PyString_FromString(argv[i + 1]); |
| 1254 | if (!s || PyTuple_SetItem(arg, i, s)) { |
| 1255 | Py_DECREF(arg); |
Guido van Rossum | f766e23 | 1998-06-19 04:28:10 +0000 | [diff] [blame] | 1256 | return PythonCmd_Error(interp); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1257 | } |
| 1258 | } |
| 1259 | res = PyEval_CallObject(func, arg); |
| 1260 | Py_DECREF(arg); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1261 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1262 | if (res == NULL) |
| 1263 | return PythonCmd_Error(interp); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1264 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1265 | if (!(tmp = PyList_New(0))) { |
| 1266 | Py_DECREF(res); |
| 1267 | return PythonCmd_Error(interp); |
| 1268 | } |
| 1269 | |
| 1270 | Tcl_SetResult(Tkapp_Interp(self), AsString(res, tmp), TCL_VOLATILE); |
| 1271 | Py_DECREF(res); |
| 1272 | Py_DECREF(tmp); |
| 1273 | |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1274 | LEAVE_PYTHON |
| 1275 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1276 | return TCL_OK; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1277 | } |
| 1278 | |
| 1279 | static void |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 1280 | PythonCmdDelete(ClientData clientData) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1281 | { |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1282 | PythonCmd_ClientData *data = (PythonCmd_ClientData *)clientData; |
| 1283 | |
Guido van Rossum | dc1adab | 1998-10-09 20:51:18 +0000 | [diff] [blame] | 1284 | ENTER_PYTHON |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1285 | Py_XDECREF(data->self); |
| 1286 | Py_XDECREF(data->func); |
| 1287 | PyMem_DEL(data); |
| 1288 | LEAVE_PYTHON |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1289 | } |
| 1290 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1291 | |
| 1292 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1293 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 1294 | Tkapp_CreateCommand(PyObject *self, PyObject *args) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1295 | { |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1296 | PythonCmd_ClientData *data; |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1297 | char *cmdName; |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1298 | PyObject *func; |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1299 | Tcl_Command err; |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1300 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 1301 | if (!PyArg_ParseTuple(args, "sO:createcommand", &cmdName, &func)) |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1302 | return NULL; |
| 1303 | if (!PyCallable_Check(func)) { |
| 1304 | PyErr_SetString(PyExc_TypeError, "command not callable"); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1305 | return NULL; |
| 1306 | } |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1307 | |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1308 | data = PyMem_NEW(PythonCmd_ClientData, 1); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1309 | if (!data) |
| 1310 | return NULL; |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1311 | Py_XINCREF(self); |
| 1312 | Py_XINCREF(func); |
| 1313 | data->self = self; |
| 1314 | data->func = func; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1315 | |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1316 | ENTER_TCL |
| 1317 | err = Tcl_CreateCommand(Tkapp_Interp(self), cmdName, PythonCmd, |
| 1318 | (ClientData)data, PythonCmdDelete); |
| 1319 | LEAVE_TCL |
| 1320 | if (err == NULL) { |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1321 | PyErr_SetString(Tkinter_TclError, "can't create Tcl command"); |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1322 | PyMem_DEL(data); |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1323 | return NULL; |
| 1324 | } |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1325 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1326 | Py_INCREF(Py_None); |
| 1327 | return Py_None; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1328 | } |
| 1329 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1330 | |
| 1331 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1332 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 1333 | Tkapp_DeleteCommand(PyObject *self, PyObject *args) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1334 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1335 | char *cmdName; |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1336 | int err; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1337 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 1338 | if (!PyArg_ParseTuple(args, "s:deletecommand", &cmdName)) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1339 | return NULL; |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1340 | ENTER_TCL |
| 1341 | err = Tcl_DeleteCommand(Tkapp_Interp(self), cmdName); |
| 1342 | LEAVE_TCL |
| 1343 | if (err == -1) { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1344 | PyErr_SetString(Tkinter_TclError, "can't delete Tcl command"); |
| 1345 | return NULL; |
| 1346 | } |
| 1347 | Py_INCREF(Py_None); |
| 1348 | return Py_None; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1349 | } |
| 1350 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1351 | |
| 1352 | |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1353 | #ifdef HAVE_CREATEFILEHANDLER |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1354 | /** File Handler **/ |
| 1355 | |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1356 | typedef struct _fhcdata { |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1357 | PyObject *func; |
| 1358 | PyObject *file; |
| 1359 | int id; |
| 1360 | struct _fhcdata *next; |
| 1361 | } FileHandler_ClientData; |
| 1362 | |
| 1363 | static FileHandler_ClientData *HeadFHCD; |
| 1364 | |
| 1365 | static FileHandler_ClientData * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 1366 | NewFHCD(PyObject *func, PyObject *file, int id) |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1367 | { |
| 1368 | FileHandler_ClientData *p; |
| 1369 | p = PyMem_NEW(FileHandler_ClientData, 1); |
| 1370 | if (p != NULL) { |
| 1371 | Py_XINCREF(func); |
| 1372 | Py_XINCREF(file); |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1373 | p->func = func; |
| 1374 | p->file = file; |
| 1375 | p->id = id; |
| 1376 | p->next = HeadFHCD; |
| 1377 | HeadFHCD = p; |
| 1378 | } |
| 1379 | return p; |
| 1380 | } |
| 1381 | |
| 1382 | static void |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 1383 | DeleteFHCD(int id) |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1384 | { |
| 1385 | FileHandler_ClientData *p, **pp; |
| 1386 | |
| 1387 | pp = &HeadFHCD; |
| 1388 | while ((p = *pp) != NULL) { |
| 1389 | if (p->id == id) { |
| 1390 | *pp = p->next; |
| 1391 | Py_XDECREF(p->func); |
| 1392 | Py_XDECREF(p->file); |
| 1393 | PyMem_DEL(p); |
| 1394 | } |
| 1395 | else |
| 1396 | pp = &p->next; |
| 1397 | } |
| 1398 | } |
| 1399 | |
Guido van Rossum | a597dde | 1995-01-10 20:56:29 +0000 | [diff] [blame] | 1400 | static void |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 1401 | FileHandler(ClientData clientData, int mask) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1402 | { |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1403 | FileHandler_ClientData *data = (FileHandler_ClientData *)clientData; |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1404 | PyObject *func, *file, *arg, *res; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1405 | |
Guido van Rossum | dc1adab | 1998-10-09 20:51:18 +0000 | [diff] [blame] | 1406 | ENTER_PYTHON |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1407 | func = data->func; |
| 1408 | file = data->file; |
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 | arg = Py_BuildValue("(Oi)", file, (long) mask); |
| 1411 | res = PyEval_CallObject(func, arg); |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1412 | Py_DECREF(arg); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1413 | |
| 1414 | if (res == NULL) { |
| 1415 | errorInCmd = 1; |
| 1416 | PyErr_Fetch(&excInCmd, &valInCmd, &trbInCmd); |
| 1417 | } |
| 1418 | Py_XDECREF(res); |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1419 | LEAVE_PYTHON |
Guido van Rossum | 9bb4fd6 | 1994-08-09 14:15:19 +0000 | [diff] [blame] | 1420 | } |
| 1421 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1422 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 1423 | Tkapp_CreateFileHandler(PyObject *self, PyObject *args) |
| 1424 | /* args is (file, mask, func) */ |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1425 | { |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1426 | FileHandler_ClientData *data; |
| 1427 | PyObject *file, *func; |
Guido van Rossum | a80649b | 2000-03-28 20:07:05 +0000 | [diff] [blame] | 1428 | int mask, tfile; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1429 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 1430 | if (!PyArg_ParseTuple(args, "OiO:createfilehandler", &file, &mask, &func)) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1431 | return NULL; |
Andrew M. Kuchling | 9f28a03 | 2000-07-13 23:59:35 +0000 | [diff] [blame] | 1432 | tfile = PyObject_AsFileDescriptor(file); |
Guido van Rossum | a80649b | 2000-03-28 20:07:05 +0000 | [diff] [blame] | 1433 | if (tfile < 0) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1434 | return NULL; |
| 1435 | if (!PyCallable_Check(func)) { |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1436 | PyErr_SetString(PyExc_TypeError, "bad argument list"); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1437 | return NULL; |
| 1438 | } |
| 1439 | |
Guido van Rossum | a80649b | 2000-03-28 20:07:05 +0000 | [diff] [blame] | 1440 | data = NewFHCD(func, file, tfile); |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1441 | if (data == NULL) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1442 | return NULL; |
| 1443 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1444 | /* Ought to check for null Tcl_File object... */ |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1445 | ENTER_TCL |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1446 | Tcl_CreateFileHandler(tfile, mask, FileHandler, (ClientData) data); |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1447 | LEAVE_TCL |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1448 | Py_INCREF(Py_None); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1449 | return Py_None; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1450 | } |
| 1451 | |
| 1452 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 1453 | Tkapp_DeleteFileHandler(PyObject *self, PyObject *args) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1454 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1455 | PyObject *file; |
Guido van Rossum | a80649b | 2000-03-28 20:07:05 +0000 | [diff] [blame] | 1456 | int tfile; |
Guido van Rossum | 6878436 | 1996-05-16 17:17:31 +0000 | [diff] [blame] | 1457 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 1458 | if (!PyArg_ParseTuple(args, "O:deletefilehandler", &file)) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1459 | return NULL; |
Andrew M. Kuchling | 9f28a03 | 2000-07-13 23:59:35 +0000 | [diff] [blame] | 1460 | tfile = PyObject_AsFileDescriptor(file); |
Guido van Rossum | a80649b | 2000-03-28 20:07:05 +0000 | [diff] [blame] | 1461 | if (tfile < 0) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1462 | return NULL; |
| 1463 | |
Guido van Rossum | a80649b | 2000-03-28 20:07:05 +0000 | [diff] [blame] | 1464 | DeleteFHCD(tfile); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1465 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1466 | /* Ought to check for null Tcl_File object... */ |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1467 | ENTER_TCL |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1468 | Tcl_DeleteFileHandler(tfile); |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1469 | LEAVE_TCL |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1470 | Py_INCREF(Py_None); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1471 | return Py_None; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1472 | } |
Guido van Rossum | 0d2390c | 1997-08-14 19:57:07 +0000 | [diff] [blame] | 1473 | #endif /* HAVE_CREATEFILEHANDLER */ |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1474 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1475 | |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 1476 | /**** Tktt Object (timer token) ****/ |
| 1477 | |
| 1478 | staticforward PyTypeObject Tktt_Type; |
| 1479 | |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1480 | typedef struct { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1481 | PyObject_HEAD |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1482 | Tcl_TimerToken token; |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1483 | PyObject *func; |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1484 | } TkttObject; |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 1485 | |
| 1486 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 1487 | Tktt_DeleteTimerHandler(PyObject *self, PyObject *args) |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 1488 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1489 | TkttObject *v = (TkttObject *)self; |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1490 | PyObject *func = v->func; |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 1491 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 1492 | if (!PyArg_ParseTuple(args, ":deletetimerhandler")) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1493 | return NULL; |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1494 | if (v->token != NULL) { |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1495 | Tcl_DeleteTimerHandler(v->token); |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1496 | v->token = NULL; |
| 1497 | } |
| 1498 | if (func != NULL) { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1499 | v->func = NULL; |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1500 | Py_DECREF(func); |
| 1501 | Py_DECREF(v); /* See Tktt_New() */ |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1502 | } |
| 1503 | Py_INCREF(Py_None); |
| 1504 | return Py_None; |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 1505 | } |
| 1506 | |
| 1507 | static PyMethodDef Tktt_methods[] = |
| 1508 | { |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1509 | {"deletetimerhandler", Tktt_DeleteTimerHandler, 1}, |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1510 | {NULL, NULL} |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 1511 | }; |
| 1512 | |
| 1513 | static TkttObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 1514 | Tktt_New(PyObject *func) |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 1515 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1516 | TkttObject *v; |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 1517 | |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 1518 | v = PyObject_New(TkttObject, &Tktt_Type); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1519 | if (v == NULL) |
| 1520 | return NULL; |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 1521 | |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1522 | Py_INCREF(func); |
| 1523 | v->token = NULL; |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1524 | v->func = func; |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1525 | |
| 1526 | /* Extra reference, deleted when called or when handler is deleted */ |
| 1527 | Py_INCREF(v); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1528 | return v; |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 1529 | } |
| 1530 | |
| 1531 | static void |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 1532 | Tktt_Dealloc(PyObject *self) |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 1533 | { |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1534 | TkttObject *v = (TkttObject *)self; |
| 1535 | PyObject *func = v->func; |
| 1536 | |
| 1537 | Py_XDECREF(func); |
| 1538 | |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 1539 | PyObject_Del(self); |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 1540 | } |
| 1541 | |
Guido van Rossum | 597ac20 | 1998-05-12 14:36:19 +0000 | [diff] [blame] | 1542 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 1543 | Tktt_Repr(PyObject *self) |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 1544 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1545 | TkttObject *v = (TkttObject *)self; |
Guido van Rossum | 597ac20 | 1998-05-12 14:36:19 +0000 | [diff] [blame] | 1546 | char buf[100]; |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 1547 | |
Fred Drake | a44d353 | 2000-06-30 15:01:00 +0000 | [diff] [blame] | 1548 | sprintf(buf, "<tktimertoken at %p%s>", v, |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1549 | v->func == NULL ? ", handler deleted" : ""); |
Guido van Rossum | 597ac20 | 1998-05-12 14:36:19 +0000 | [diff] [blame] | 1550 | return PyString_FromString(buf); |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 1551 | } |
| 1552 | |
| 1553 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 1554 | Tktt_GetAttr(PyObject *self, char *name) |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 1555 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1556 | return Py_FindMethod(Tktt_methods, self, name); |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 1557 | } |
| 1558 | |
| 1559 | static PyTypeObject Tktt_Type = |
| 1560 | { |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1561 | PyObject_HEAD_INIT(NULL) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1562 | 0, /*ob_size */ |
| 1563 | "tktimertoken", /*tp_name */ |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1564 | sizeof(TkttObject), /*tp_basicsize */ |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1565 | 0, /*tp_itemsize */ |
| 1566 | Tktt_Dealloc, /*tp_dealloc */ |
Guido van Rossum | 597ac20 | 1998-05-12 14:36:19 +0000 | [diff] [blame] | 1567 | 0, /*tp_print */ |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1568 | Tktt_GetAttr, /*tp_getattr */ |
| 1569 | 0, /*tp_setattr */ |
| 1570 | 0, /*tp_compare */ |
Guido van Rossum | 597ac20 | 1998-05-12 14:36:19 +0000 | [diff] [blame] | 1571 | Tktt_Repr, /*tp_repr */ |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1572 | 0, /*tp_as_number */ |
| 1573 | 0, /*tp_as_sequence */ |
| 1574 | 0, /*tp_as_mapping */ |
| 1575 | 0, /*tp_hash */ |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 1576 | }; |
| 1577 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1578 | |
| 1579 | |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 1580 | /** Timer Handler **/ |
| 1581 | |
| 1582 | static void |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 1583 | TimerHandler(ClientData clientData) |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 1584 | { |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1585 | TkttObject *v = (TkttObject *)clientData; |
| 1586 | PyObject *func = v->func; |
| 1587 | PyObject *res; |
| 1588 | |
| 1589 | if (func == NULL) |
| 1590 | return; |
| 1591 | |
| 1592 | v->func = NULL; |
| 1593 | |
Guido van Rossum | dc1adab | 1998-10-09 20:51:18 +0000 | [diff] [blame] | 1594 | ENTER_PYTHON |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1595 | |
| 1596 | res = PyEval_CallObject(func, NULL); |
| 1597 | Py_DECREF(func); |
| 1598 | Py_DECREF(v); /* See Tktt_New() */ |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 1599 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1600 | if (res == NULL) { |
| 1601 | errorInCmd = 1; |
| 1602 | PyErr_Fetch(&excInCmd, &valInCmd, &trbInCmd); |
| 1603 | } |
| 1604 | else |
| 1605 | Py_DECREF(res); |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1606 | |
| 1607 | LEAVE_PYTHON |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 1608 | } |
| 1609 | |
| 1610 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 1611 | Tkapp_CreateTimerHandler(PyObject *self, PyObject *args) |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 1612 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1613 | int milliseconds; |
| 1614 | PyObject *func; |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1615 | TkttObject *v; |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 1616 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 1617 | if (!PyArg_ParseTuple(args, "iO:createtimerhandler", &milliseconds, &func)) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1618 | return NULL; |
| 1619 | if (!PyCallable_Check(func)) { |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1620 | PyErr_SetString(PyExc_TypeError, "bad argument list"); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1621 | return NULL; |
| 1622 | } |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1623 | v = Tktt_New(func); |
| 1624 | v->token = Tcl_CreateTimerHandler(milliseconds, TimerHandler, |
| 1625 | (ClientData)v); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1626 | |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1627 | return (PyObject *) v; |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 1628 | } |
| 1629 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1630 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1631 | /** Event Loop **/ |
| 1632 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1633 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 1634 | Tkapp_MainLoop(PyObject *self, PyObject *args) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1635 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1636 | int threshold = 0; |
Guido van Rossum | dc1adab | 1998-10-09 20:51:18 +0000 | [diff] [blame] | 1637 | #ifdef WITH_THREAD |
| 1638 | PyThreadState *tstate = PyThreadState_Get(); |
| 1639 | #endif |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 1640 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 1641 | if (!PyArg_ParseTuple(args, "|i:mainloop", &threshold)) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1642 | return NULL; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1643 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1644 | quitMainLoop = 0; |
| 1645 | while (Tk_GetNumMainWindows() > threshold && |
| 1646 | !quitMainLoop && |
| 1647 | !errorInCmd) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1648 | { |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1649 | int result; |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1650 | |
| 1651 | #ifdef WITH_THREAD |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 1652 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 1653 | PyThread_acquire_lock(tcl_lock, 1); |
Guido van Rossum | dc1adab | 1998-10-09 20:51:18 +0000 | [diff] [blame] | 1654 | tcl_tstate = tstate; |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1655 | result = Tcl_DoOneEvent(TCL_DONT_WAIT); |
Guido van Rossum | dc1adab | 1998-10-09 20:51:18 +0000 | [diff] [blame] | 1656 | tcl_tstate = NULL; |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 1657 | PyThread_release_lock(tcl_lock); |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1658 | if (result == 0) |
| 1659 | Sleep(20); |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1660 | Py_END_ALLOW_THREADS |
Guido van Rossum | 5b02078 | 1997-08-19 01:00:50 +0000 | [diff] [blame] | 1661 | #else |
| 1662 | result = Tcl_DoOneEvent(0); |
| 1663 | #endif |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1664 | |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1665 | if (PyErr_CheckSignals() != 0) |
| 1666 | return NULL; |
| 1667 | if (result < 0) |
| 1668 | break; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1669 | } |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1670 | quitMainLoop = 0; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1671 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1672 | if (errorInCmd) { |
| 1673 | errorInCmd = 0; |
| 1674 | PyErr_Restore(excInCmd, valInCmd, trbInCmd); |
| 1675 | excInCmd = valInCmd = trbInCmd = NULL; |
| 1676 | return NULL; |
| 1677 | } |
| 1678 | Py_INCREF(Py_None); |
| 1679 | return Py_None; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1680 | } |
| 1681 | |
| 1682 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 1683 | Tkapp_DoOneEvent(PyObject *self, PyObject *args) |
Guido van Rossum | 062cfb0 | 1995-01-10 17:42:51 +0000 | [diff] [blame] | 1684 | { |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1685 | int flags = 0; |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1686 | int rv; |
Guido van Rossum | 062cfb0 | 1995-01-10 17:42:51 +0000 | [diff] [blame] | 1687 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 1688 | if (!PyArg_ParseTuple(args, "|i:dooneevent", &flags)) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1689 | return NULL; |
| 1690 | |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1691 | ENTER_TCL |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1692 | rv = Tcl_DoOneEvent(flags); |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1693 | LEAVE_TCL |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1694 | return Py_BuildValue("i", rv); |
Guido van Rossum | 062cfb0 | 1995-01-10 17:42:51 +0000 | [diff] [blame] | 1695 | } |
| 1696 | |
| 1697 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 1698 | Tkapp_Quit(PyObject *self, PyObject *args) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1699 | { |
| 1700 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 1701 | if (!PyArg_ParseTuple(args, ":quit")) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1702 | return NULL; |
| 1703 | |
| 1704 | quitMainLoop = 1; |
| 1705 | Py_INCREF(Py_None); |
| 1706 | return Py_None; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1707 | } |
| 1708 | |
Guido van Rossum | 9d1b7ae | 1998-04-29 16:17:01 +0000 | [diff] [blame] | 1709 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 1710 | Tkapp_InterpAddr(PyObject *self, PyObject *args) |
Guido van Rossum | 9d1b7ae | 1998-04-29 16:17:01 +0000 | [diff] [blame] | 1711 | { |
| 1712 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 1713 | if (!PyArg_ParseTuple(args, ":interpaddr")) |
Guido van Rossum | 9d1b7ae | 1998-04-29 16:17:01 +0000 | [diff] [blame] | 1714 | return NULL; |
| 1715 | |
| 1716 | return PyInt_FromLong((long)Tkapp_Interp(self)); |
| 1717 | } |
| 1718 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1719 | |
| 1720 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1721 | /**** Tkapp Method List ****/ |
| 1722 | |
| 1723 | static PyMethodDef Tkapp_methods[] = |
| 1724 | { |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1725 | {"call", Tkapp_Call, 0}, |
| 1726 | {"globalcall", Tkapp_GlobalCall, 0}, |
| 1727 | {"eval", Tkapp_Eval, 1}, |
| 1728 | {"globaleval", Tkapp_GlobalEval, 1}, |
| 1729 | {"evalfile", Tkapp_EvalFile, 1}, |
| 1730 | {"record", Tkapp_Record, 1}, |
| 1731 | {"adderrorinfo", Tkapp_AddErrorInfo, 1}, |
| 1732 | {"setvar", Tkapp_SetVar, 1}, |
| 1733 | {"globalsetvar", Tkapp_GlobalSetVar, 1}, |
| 1734 | {"getvar", Tkapp_GetVar, 1}, |
| 1735 | {"globalgetvar", Tkapp_GlobalGetVar, 1}, |
| 1736 | {"unsetvar", Tkapp_UnsetVar, 1}, |
| 1737 | {"globalunsetvar", Tkapp_GlobalUnsetVar, 1}, |
| 1738 | {"getint", Tkapp_GetInt, 1}, |
| 1739 | {"getdouble", Tkapp_GetDouble, 1}, |
| 1740 | {"getboolean", Tkapp_GetBoolean, 1}, |
| 1741 | {"exprstring", Tkapp_ExprString, 1}, |
| 1742 | {"exprlong", Tkapp_ExprLong, 1}, |
| 1743 | {"exprdouble", Tkapp_ExprDouble, 1}, |
| 1744 | {"exprboolean", Tkapp_ExprBoolean, 1}, |
| 1745 | {"splitlist", Tkapp_SplitList, 1}, |
| 1746 | {"split", Tkapp_Split, 1}, |
| 1747 | {"merge", Tkapp_Merge, 0}, |
| 1748 | {"createcommand", Tkapp_CreateCommand, 1}, |
| 1749 | {"deletecommand", Tkapp_DeleteCommand, 1}, |
Guido van Rossum | 0d2390c | 1997-08-14 19:57:07 +0000 | [diff] [blame] | 1750 | #ifdef HAVE_CREATEFILEHANDLER |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1751 | {"createfilehandler", Tkapp_CreateFileHandler, 1}, |
| 1752 | {"deletefilehandler", Tkapp_DeleteFileHandler, 1}, |
Guido van Rossum | 02c0467 | 1997-08-07 00:12:22 +0000 | [diff] [blame] | 1753 | #endif |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1754 | {"createtimerhandler", Tkapp_CreateTimerHandler, 1}, |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1755 | {"mainloop", Tkapp_MainLoop, 1}, |
| 1756 | {"dooneevent", Tkapp_DoOneEvent, 1}, |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1757 | {"quit", Tkapp_Quit, 1}, |
Guido van Rossum | 9d1b7ae | 1998-04-29 16:17:01 +0000 | [diff] [blame] | 1758 | {"interpaddr", Tkapp_InterpAddr, 1}, |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1759 | {NULL, NULL} |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1760 | }; |
| 1761 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1762 | |
| 1763 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1764 | /**** Tkapp Type Methods ****/ |
| 1765 | |
| 1766 | static void |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 1767 | Tkapp_Dealloc(PyObject *self) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1768 | { |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1769 | ENTER_TCL |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1770 | Tcl_DeleteInterp(Tkapp_Interp(self)); |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1771 | LEAVE_TCL |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 1772 | PyObject_Del(self); |
Guido van Rossum | 7bf1564 | 1998-05-22 18:28:17 +0000 | [diff] [blame] | 1773 | DisableEventHook(); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1774 | } |
| 1775 | |
| 1776 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 1777 | Tkapp_GetAttr(PyObject *self, char *name) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1778 | { |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1779 | return Py_FindMethod(Tkapp_methods, self, name); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1780 | } |
| 1781 | |
| 1782 | static PyTypeObject Tkapp_Type = |
| 1783 | { |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1784 | PyObject_HEAD_INIT(NULL) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1785 | 0, /*ob_size */ |
| 1786 | "tkapp", /*tp_name */ |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1787 | sizeof(TkappObject), /*tp_basicsize */ |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1788 | 0, /*tp_itemsize */ |
| 1789 | Tkapp_Dealloc, /*tp_dealloc */ |
| 1790 | 0, /*tp_print */ |
| 1791 | Tkapp_GetAttr, /*tp_getattr */ |
| 1792 | 0, /*tp_setattr */ |
| 1793 | 0, /*tp_compare */ |
| 1794 | 0, /*tp_repr */ |
| 1795 | 0, /*tp_as_number */ |
| 1796 | 0, /*tp_as_sequence */ |
| 1797 | 0, /*tp_as_mapping */ |
| 1798 | 0, /*tp_hash */ |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1799 | }; |
| 1800 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1801 | |
| 1802 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1803 | /**** Tkinter Module ****/ |
| 1804 | |
Andrew M. Kuchling | e475e70 | 2000-06-18 18:45:50 +0000 | [diff] [blame] | 1805 | typedef struct { |
| 1806 | PyObject* tuple; |
| 1807 | int size; /* current size */ |
| 1808 | int maxsize; /* allocated size */ |
| 1809 | } FlattenContext; |
| 1810 | |
| 1811 | static int |
| 1812 | _bump(FlattenContext* context, int size) |
| 1813 | { |
| 1814 | /* expand tuple to hold (at least) size new items. return true if |
| 1815 | successful, false if an exception was raised*/ |
| 1816 | |
| 1817 | int maxsize = context->maxsize * 2; |
| 1818 | |
| 1819 | if (maxsize < context->size + size) |
| 1820 | maxsize = context->size + size; |
| 1821 | |
| 1822 | context->maxsize = maxsize; |
| 1823 | |
| 1824 | return _PyTuple_Resize(&context->tuple, maxsize, 0) >= 0; |
| 1825 | } |
| 1826 | |
| 1827 | static int |
Andrew M. Kuchling | 288e97b | 2000-06-19 00:55:09 +0000 | [diff] [blame] | 1828 | _flatten1(FlattenContext* context, PyObject* item, int depth) |
Andrew M. Kuchling | e475e70 | 2000-06-18 18:45:50 +0000 | [diff] [blame] | 1829 | { |
| 1830 | /* add tuple or list to argument tuple (recursively) */ |
| 1831 | |
| 1832 | int i, size; |
| 1833 | |
Andrew M. Kuchling | 288e97b | 2000-06-19 00:55:09 +0000 | [diff] [blame] | 1834 | if (depth > 1000) { |
| 1835 | PyErr_SetString(PyExc_ValueError,"nesting too deep in _flatten"); |
| 1836 | return 0; |
| 1837 | } else if (PyList_Check(item)) { |
Andrew M. Kuchling | e475e70 | 2000-06-18 18:45:50 +0000 | [diff] [blame] | 1838 | size = PyList_GET_SIZE(item); |
| 1839 | /* preallocate (assume no nesting) */ |
| 1840 | if (context->size + size > context->maxsize && !_bump(context, size)) |
| 1841 | return 0; |
| 1842 | /* copy items to output tuple */ |
| 1843 | for (i = 0; i < size; i++) { |
| 1844 | PyObject *o = PyList_GET_ITEM(item, i); |
| 1845 | if (PyList_Check(o) || PyTuple_Check(o)) { |
Andrew M. Kuchling | 288e97b | 2000-06-19 00:55:09 +0000 | [diff] [blame] | 1846 | if (!_flatten1(context, o, depth + 1)) |
Andrew M. Kuchling | e475e70 | 2000-06-18 18:45:50 +0000 | [diff] [blame] | 1847 | return 0; |
| 1848 | } else if (o != Py_None) { |
| 1849 | if (context->size + 1 > context->maxsize && !_bump(context, 1)) |
| 1850 | return 0; |
| 1851 | Py_INCREF(o); |
| 1852 | PyTuple_SET_ITEM(context->tuple, context->size++, o); |
| 1853 | } |
| 1854 | } |
| 1855 | } else if (PyTuple_Check(item)) { |
| 1856 | /* same, for tuples */ |
| 1857 | size = PyTuple_GET_SIZE(item); |
| 1858 | if (context->size + size > context->maxsize && !_bump(context, size)) |
| 1859 | return 0; |
| 1860 | for (i = 0; i < size; i++) { |
| 1861 | PyObject *o = PyTuple_GET_ITEM(item, i); |
| 1862 | if (PyList_Check(o) || PyTuple_Check(o)) { |
Andrew M. Kuchling | 288e97b | 2000-06-19 00:55:09 +0000 | [diff] [blame] | 1863 | if (!_flatten1(context, o, depth + 1)) |
Andrew M. Kuchling | e475e70 | 2000-06-18 18:45:50 +0000 | [diff] [blame] | 1864 | return 0; |
| 1865 | } else if (o != Py_None) { |
| 1866 | if (context->size + 1 > context->maxsize && !_bump(context, 1)) |
| 1867 | return 0; |
| 1868 | Py_INCREF(o); |
| 1869 | PyTuple_SET_ITEM(context->tuple, context->size++, o); |
| 1870 | } |
| 1871 | } |
| 1872 | } else { |
| 1873 | PyErr_SetString(PyExc_TypeError, "argument must be sequence"); |
| 1874 | return 0; |
| 1875 | } |
| 1876 | return 1; |
| 1877 | } |
| 1878 | |
| 1879 | static PyObject * |
| 1880 | Tkinter_Flatten(PyObject* self, PyObject* args) |
| 1881 | { |
| 1882 | FlattenContext context; |
| 1883 | PyObject* item; |
| 1884 | |
| 1885 | if (!PyArg_ParseTuple(args, "O:_flatten", &item)) |
| 1886 | return NULL; |
| 1887 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 1888 | context.maxsize = PySequence_Size(item); |
Andrew M. Kuchling | e475e70 | 2000-06-18 18:45:50 +0000 | [diff] [blame] | 1889 | if (context.maxsize <= 0) |
| 1890 | return PyTuple_New(0); |
| 1891 | |
| 1892 | context.tuple = PyTuple_New(context.maxsize); |
| 1893 | if (!context.tuple) |
| 1894 | return NULL; |
| 1895 | |
| 1896 | context.size = 0; |
| 1897 | |
Andrew M. Kuchling | 288e97b | 2000-06-19 00:55:09 +0000 | [diff] [blame] | 1898 | if (!_flatten1(&context, item,0)) |
Andrew M. Kuchling | e475e70 | 2000-06-18 18:45:50 +0000 | [diff] [blame] | 1899 | return NULL; |
| 1900 | |
| 1901 | if (_PyTuple_Resize(&context.tuple, context.size, 0)) |
| 1902 | return NULL; |
| 1903 | |
| 1904 | return context.tuple; |
| 1905 | } |
| 1906 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1907 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 1908 | Tkinter_Create(PyObject *self, PyObject *args) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1909 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1910 | char *screenName = NULL; |
| 1911 | char *baseName = NULL; |
| 1912 | char *className = NULL; |
| 1913 | int interactive = 0; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1914 | |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1915 | baseName = strrchr(Py_GetProgramName(), '/'); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1916 | if (baseName != NULL) |
| 1917 | baseName++; |
| 1918 | else |
| 1919 | baseName = Py_GetProgramName(); |
| 1920 | className = "Tk"; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1921 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 1922 | if (!PyArg_ParseTuple(args, "|zssi:create", |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1923 | &screenName, &baseName, &className, |
| 1924 | &interactive)) |
| 1925 | return NULL; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1926 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1927 | return (PyObject *) Tkapp_New(screenName, baseName, className, |
| 1928 | interactive); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1929 | } |
| 1930 | |
| 1931 | static PyMethodDef moduleMethods[] = |
| 1932 | { |
Andrew M. Kuchling | e475e70 | 2000-06-18 18:45:50 +0000 | [diff] [blame] | 1933 | {"_flatten", Tkinter_Flatten, 1}, |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1934 | {"create", Tkinter_Create, 1}, |
Guido van Rossum | 0d2390c | 1997-08-14 19:57:07 +0000 | [diff] [blame] | 1935 | #ifdef HAVE_CREATEFILEHANDLER |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1936 | {"createfilehandler", Tkapp_CreateFileHandler, 1}, |
| 1937 | {"deletefilehandler", Tkapp_DeleteFileHandler, 1}, |
Guido van Rossum | 02c0467 | 1997-08-07 00:12:22 +0000 | [diff] [blame] | 1938 | #endif |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1939 | {"createtimerhandler", Tkapp_CreateTimerHandler, 1}, |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1940 | {"mainloop", Tkapp_MainLoop, 1}, |
| 1941 | {"dooneevent", Tkapp_DoOneEvent, 1}, |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1942 | {"quit", Tkapp_Quit, 1}, |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1943 | {NULL, NULL} |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1944 | }; |
| 1945 | |
Guido van Rossum | 7bf1564 | 1998-05-22 18:28:17 +0000 | [diff] [blame] | 1946 | #ifdef WAIT_FOR_STDIN |
Guido van Rossum | 7bf1564 | 1998-05-22 18:28:17 +0000 | [diff] [blame] | 1947 | |
| 1948 | static int stdin_ready = 0; |
| 1949 | |
Guido van Rossum | ad4db17 | 1998-06-13 13:56:28 +0000 | [diff] [blame] | 1950 | #ifndef MS_WINDOWS |
Guido van Rossum | 7bf1564 | 1998-05-22 18:28:17 +0000 | [diff] [blame] | 1951 | static void |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 1952 | MyFileProc(void *clientData, int mask) |
Guido van Rossum | 7bf1564 | 1998-05-22 18:28:17 +0000 | [diff] [blame] | 1953 | { |
| 1954 | stdin_ready = 1; |
| 1955 | } |
Guido van Rossum | ad4db17 | 1998-06-13 13:56:28 +0000 | [diff] [blame] | 1956 | #endif |
Guido van Rossum | 7bf1564 | 1998-05-22 18:28:17 +0000 | [diff] [blame] | 1957 | |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1958 | static PyThreadState *event_tstate = NULL; |
Guido van Rossum | 0e8457c | 1997-10-07 18:51:41 +0000 | [diff] [blame] | 1959 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1960 | static int |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 1961 | EventHook(void) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1962 | { |
Guido van Rossum | ad4db17 | 1998-06-13 13:56:28 +0000 | [diff] [blame] | 1963 | #ifndef MS_WINDOWS |
Guido van Rossum | a80649b | 2000-03-28 20:07:05 +0000 | [diff] [blame] | 1964 | int tfile; |
Guido van Rossum | ad4db17 | 1998-06-13 13:56:28 +0000 | [diff] [blame] | 1965 | #endif |
Guido van Rossum | e9bc62d | 1998-11-17 03:45:24 +0000 | [diff] [blame] | 1966 | #ifdef WITH_THREAD |
Guido van Rossum | ad4db17 | 1998-06-13 13:56:28 +0000 | [diff] [blame] | 1967 | PyEval_RestoreThread(event_tstate); |
Guido van Rossum | e9bc62d | 1998-11-17 03:45:24 +0000 | [diff] [blame] | 1968 | #endif |
Guido van Rossum | 7bf1564 | 1998-05-22 18:28:17 +0000 | [diff] [blame] | 1969 | stdin_ready = 0; |
Guido van Rossum | ad4db17 | 1998-06-13 13:56:28 +0000 | [diff] [blame] | 1970 | errorInCmd = 0; |
| 1971 | #ifndef MS_WINDOWS |
Guido van Rossum | a80649b | 2000-03-28 20:07:05 +0000 | [diff] [blame] | 1972 | tfile = fileno(stdin); |
Guido van Rossum | 7bf1564 | 1998-05-22 18:28:17 +0000 | [diff] [blame] | 1973 | Tcl_CreateFileHandler(tfile, TCL_READABLE, MyFileProc, NULL); |
Guido van Rossum | ad4db17 | 1998-06-13 13:56:28 +0000 | [diff] [blame] | 1974 | #endif |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1975 | while (!errorInCmd && !stdin_ready) { |
| 1976 | int result; |
Guido van Rossum | ad4db17 | 1998-06-13 13:56:28 +0000 | [diff] [blame] | 1977 | #ifdef MS_WINDOWS |
| 1978 | if (_kbhit()) { |
| 1979 | stdin_ready = 1; |
| 1980 | break; |
| 1981 | } |
| 1982 | #endif |
| 1983 | #if defined(WITH_THREAD) || defined(MS_WINDOWS) |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 1984 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 1985 | PyThread_acquire_lock(tcl_lock, 1); |
Guido van Rossum | 41f0a98 | 1998-10-12 16:26:22 +0000 | [diff] [blame] | 1986 | tcl_tstate = event_tstate; |
Guido van Rossum | dc1adab | 1998-10-09 20:51:18 +0000 | [diff] [blame] | 1987 | |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1988 | result = Tcl_DoOneEvent(TCL_DONT_WAIT); |
Guido van Rossum | dc1adab | 1998-10-09 20:51:18 +0000 | [diff] [blame] | 1989 | |
| 1990 | tcl_tstate = NULL; |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 1991 | PyThread_release_lock(tcl_lock); |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1992 | if (result == 0) |
| 1993 | Sleep(20); |
| 1994 | Py_END_ALLOW_THREADS |
| 1995 | #else |
| 1996 | result = Tcl_DoOneEvent(0); |
Guido van Rossum | 7bf1564 | 1998-05-22 18:28:17 +0000 | [diff] [blame] | 1997 | #endif |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1998 | |
| 1999 | if (result < 0) |
| 2000 | break; |
| 2001 | } |
Guido van Rossum | ad4db17 | 1998-06-13 13:56:28 +0000 | [diff] [blame] | 2002 | #ifndef MS_WINDOWS |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2003 | Tcl_DeleteFileHandler(tfile); |
Guido van Rossum | ad4db17 | 1998-06-13 13:56:28 +0000 | [diff] [blame] | 2004 | #endif |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2005 | if (errorInCmd) { |
| 2006 | errorInCmd = 0; |
| 2007 | PyErr_Restore(excInCmd, valInCmd, trbInCmd); |
| 2008 | excInCmd = valInCmd = trbInCmd = NULL; |
| 2009 | PyErr_Print(); |
| 2010 | } |
Guido van Rossum | e9bc62d | 1998-11-17 03:45:24 +0000 | [diff] [blame] | 2011 | #ifdef WITH_THREAD |
Guido van Rossum | ad4db17 | 1998-06-13 13:56:28 +0000 | [diff] [blame] | 2012 | PyEval_SaveThread(); |
Guido van Rossum | e9bc62d | 1998-11-17 03:45:24 +0000 | [diff] [blame] | 2013 | #endif |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2014 | return 0; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2015 | } |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2016 | |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2017 | #endif |
| 2018 | |
Guido van Rossum | 7bf1564 | 1998-05-22 18:28:17 +0000 | [diff] [blame] | 2019 | static void |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 2020 | EnableEventHook(void) |
Guido van Rossum | 7bf1564 | 1998-05-22 18:28:17 +0000 | [diff] [blame] | 2021 | { |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2022 | #ifdef WAIT_FOR_STDIN |
Guido van Rossum | 7bf1564 | 1998-05-22 18:28:17 +0000 | [diff] [blame] | 2023 | if (PyOS_InputHook == NULL) { |
Guido van Rossum | e9bc62d | 1998-11-17 03:45:24 +0000 | [diff] [blame] | 2024 | #ifdef WITH_THREAD |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2025 | event_tstate = PyThreadState_Get(); |
Guido van Rossum | e9bc62d | 1998-11-17 03:45:24 +0000 | [diff] [blame] | 2026 | #endif |
Guido van Rossum | 7bf1564 | 1998-05-22 18:28:17 +0000 | [diff] [blame] | 2027 | PyOS_InputHook = EventHook; |
| 2028 | } |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2029 | #endif |
Guido van Rossum | 7bf1564 | 1998-05-22 18:28:17 +0000 | [diff] [blame] | 2030 | } |
| 2031 | |
| 2032 | static void |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 2033 | DisableEventHook(void) |
Guido van Rossum | 7bf1564 | 1998-05-22 18:28:17 +0000 | [diff] [blame] | 2034 | { |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2035 | #ifdef WAIT_FOR_STDIN |
Guido van Rossum | 7bf1564 | 1998-05-22 18:28:17 +0000 | [diff] [blame] | 2036 | if (Tk_GetNumMainWindows() == 0 && PyOS_InputHook == EventHook) { |
| 2037 | PyOS_InputHook = NULL; |
| 2038 | } |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2039 | #endif |
Guido van Rossum | 7bf1564 | 1998-05-22 18:28:17 +0000 | [diff] [blame] | 2040 | } |
| 2041 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2042 | |
| 2043 | /* all errors will be checked in one fell swoop in init_tkinter() */ |
| 2044 | static void |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 2045 | ins_long(PyObject *d, char *name, long val) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2046 | { |
| 2047 | PyObject *v = PyInt_FromLong(val); |
| 2048 | if (v) { |
| 2049 | PyDict_SetItemString(d, name, v); |
| 2050 | Py_DECREF(v); |
| 2051 | } |
| 2052 | } |
| 2053 | static void |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 2054 | ins_string(PyObject *d, char *name, char *val) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2055 | { |
| 2056 | PyObject *v = PyString_FromString(val); |
| 2057 | if (v) { |
| 2058 | PyDict_SetItemString(d, name, v); |
| 2059 | Py_DECREF(v); |
| 2060 | } |
| 2061 | } |
| 2062 | |
| 2063 | |
Guido van Rossum | 3886bb6 | 1998-12-04 18:50:17 +0000 | [diff] [blame] | 2064 | DL_EXPORT(void) |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 2065 | init_tkinter(void) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2066 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2067 | PyObject *m, *d; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2068 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2069 | Tkapp_Type.ob_type = &PyType_Type; |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2070 | |
| 2071 | #ifdef WITH_THREAD |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 2072 | tcl_lock = PyThread_allocate_lock(); |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2073 | #endif |
Guido van Rossum | ae92f01 | 1996-08-21 19:03:36 +0000 | [diff] [blame] | 2074 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2075 | m = Py_InitModule("_tkinter", moduleMethods); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2076 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2077 | d = PyModule_GetDict(m); |
| 2078 | Tkinter_TclError = Py_BuildValue("s", "TclError"); |
| 2079 | PyDict_SetItemString(d, "TclError", Tkinter_TclError); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2080 | |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 2081 | ins_long(d, "READABLE", TCL_READABLE); |
| 2082 | ins_long(d, "WRITABLE", TCL_WRITABLE); |
| 2083 | ins_long(d, "EXCEPTION", TCL_EXCEPTION); |
| 2084 | ins_long(d, "WINDOW_EVENTS", TCL_WINDOW_EVENTS); |
| 2085 | ins_long(d, "FILE_EVENTS", TCL_FILE_EVENTS); |
| 2086 | ins_long(d, "TIMER_EVENTS", TCL_TIMER_EVENTS); |
| 2087 | ins_long(d, "IDLE_EVENTS", TCL_IDLE_EVENTS); |
| 2088 | ins_long(d, "ALL_EVENTS", TCL_ALL_EVENTS); |
| 2089 | ins_long(d, "DONT_WAIT", TCL_DONT_WAIT); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2090 | ins_string(d, "TK_VERSION", TK_VERSION); |
| 2091 | ins_string(d, "TCL_VERSION", TCL_VERSION); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2092 | |
Guido van Rossum | 83551bf | 1997-09-13 00:44:23 +0000 | [diff] [blame] | 2093 | PyDict_SetItemString(d, "TkappType", (PyObject *)&Tkapp_Type); |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2094 | |
| 2095 | Tktt_Type.ob_type = &PyType_Type; |
Guido van Rossum | 83551bf | 1997-09-13 00:44:23 +0000 | [diff] [blame] | 2096 | PyDict_SetItemString(d, "TkttType", (PyObject *)&Tktt_Type); |
| 2097 | |
Guido van Rossum | e187b0e | 2000-03-27 21:46:29 +0000 | [diff] [blame] | 2098 | /* This helps the dynamic loader; in Unicode aware Tcl versions |
| 2099 | it also helps Tcl find its encodings. */ |
| 2100 | Tcl_FindExecutable(Py_GetProgramName()); |
Guido van Rossum | e187b0e | 2000-03-27 21:46:29 +0000 | [diff] [blame] | 2101 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2102 | if (PyErr_Occurred()) |
Guido van Rossum | 07e9fbf | 1998-02-06 22:35:46 +0000 | [diff] [blame] | 2103 | return; |
| 2104 | |
Guido van Rossum | 43ff868 | 1998-07-14 18:02:13 +0000 | [diff] [blame] | 2105 | #if 0 |
| 2106 | /* This was not a good idea; through <Destroy> bindings, |
| 2107 | Tcl_Finalize() may invoke Python code but at that point the |
| 2108 | interpreter and thread state have already been destroyed! */ |
Guido van Rossum | 07e9fbf | 1998-02-06 22:35:46 +0000 | [diff] [blame] | 2109 | Py_AtExit(Tcl_Finalize); |
Guido van Rossum | 2621637 | 1998-04-20 18:47:52 +0000 | [diff] [blame] | 2110 | #endif |
Guido van Rossum | 07e9fbf | 1998-02-06 22:35:46 +0000 | [diff] [blame] | 2111 | |
Jack Jansen | 34cc5c3 | 1995-10-31 16:15:12 +0000 | [diff] [blame] | 2112 | #ifdef macintosh |
Guido van Rossum | 0d2390c | 1997-08-14 19:57:07 +0000 | [diff] [blame] | 2113 | /* |
| 2114 | ** Part of this code is stolen from MacintoshInit in tkMacAppInit. |
| 2115 | ** Most of the initializations in that routine (toolbox init calls and |
| 2116 | ** such) have already been done for us, so we only need these. |
| 2117 | */ |
Guido van Rossum | 0d2390c | 1997-08-14 19:57:07 +0000 | [diff] [blame] | 2118 | tcl_macQdPtr = &qd; |
Guido van Rossum | 0d2390c | 1997-08-14 19:57:07 +0000 | [diff] [blame] | 2119 | |
| 2120 | Tcl_MacSetEventProc(PyMacConvertEvent); |
Guido van Rossum | ec22c92 | 1996-02-25 04:50:29 +0000 | [diff] [blame] | 2121 | #if GENERATINGCFM |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2122 | mac_addlibresources(); |
Guido van Rossum | ec22c92 | 1996-02-25 04:50:29 +0000 | [diff] [blame] | 2123 | #endif /* GENERATINGCFM */ |
| 2124 | #endif /* macintosh */ |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2125 | } |
Guido van Rossum | 9722ad8 | 1995-09-22 23:49:28 +0000 | [diff] [blame] | 2126 | |
Guido van Rossum | ec22c92 | 1996-02-25 04:50:29 +0000 | [diff] [blame] | 2127 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2128 | |
Guido van Rossum | 9722ad8 | 1995-09-22 23:49:28 +0000 | [diff] [blame] | 2129 | #ifdef macintosh |
Guido van Rossum | dfd428d | 1996-02-25 04:46:40 +0000 | [diff] [blame] | 2130 | |
| 2131 | /* |
Guido van Rossum | ec22c92 | 1996-02-25 04:50:29 +0000 | [diff] [blame] | 2132 | ** Anyone who embeds Tcl/Tk on the Mac must define panic(). |
Guido van Rossum | dfd428d | 1996-02-25 04:46:40 +0000 | [diff] [blame] | 2133 | */ |
| 2134 | |
Guido van Rossum | 9722ad8 | 1995-09-22 23:49:28 +0000 | [diff] [blame] | 2135 | void |
| 2136 | panic(char * format, ...) |
| 2137 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2138 | va_list varg; |
Guido van Rossum | 9722ad8 | 1995-09-22 23:49:28 +0000 | [diff] [blame] | 2139 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2140 | va_start(varg, format); |
Guido van Rossum | 9722ad8 | 1995-09-22 23:49:28 +0000 | [diff] [blame] | 2141 | |
Guido van Rossum | 227cf76 | 1998-08-05 13:53:32 +0000 | [diff] [blame] | 2142 | vfprintf(stderr, format, varg); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2143 | (void) fflush(stderr); |
Guido van Rossum | 9722ad8 | 1995-09-22 23:49:28 +0000 | [diff] [blame] | 2144 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2145 | va_end(varg); |
Guido van Rossum | 9722ad8 | 1995-09-22 23:49:28 +0000 | [diff] [blame] | 2146 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2147 | Py_FatalError("Tcl/Tk panic"); |
Guido van Rossum | 9722ad8 | 1995-09-22 23:49:28 +0000 | [diff] [blame] | 2148 | } |
Jack Jansen | 40b546d | 1995-11-14 10:34:45 +0000 | [diff] [blame] | 2149 | |
Guido van Rossum | ec22c92 | 1996-02-25 04:50:29 +0000 | [diff] [blame] | 2150 | /* |
| 2151 | ** Pass events to SIOUX before passing them to Tk. |
| 2152 | */ |
Guido van Rossum | dfd428d | 1996-02-25 04:46:40 +0000 | [diff] [blame] | 2153 | |
Guido van Rossum | ec22c92 | 1996-02-25 04:50:29 +0000 | [diff] [blame] | 2154 | static int |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 2155 | PyMacConvertEvent(EventRecord *eventPtr) |
Guido van Rossum | dfd428d | 1996-02-25 04:46:40 +0000 | [diff] [blame] | 2156 | { |
Guido van Rossum | 2ea1c94 | 1998-04-28 16:12:43 +0000 | [diff] [blame] | 2157 | WindowPtr frontwin; |
| 2158 | /* |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2159 | ** Sioux eats too many events, so we don't pass it everything. We |
| 2160 | ** always pass update events to Sioux, and we only pass other events if |
Guido van Rossum | 2ea1c94 | 1998-04-28 16:12:43 +0000 | [diff] [blame] | 2161 | ** the Sioux window is frontmost. This means that Tk menus don't work |
| 2162 | ** in that case, but at least we can scroll the sioux window. |
| 2163 | ** Note that the SIOUXIsAppWindow() routine we use here is not really |
| 2164 | ** part of the external interface of Sioux... |
| 2165 | */ |
| 2166 | frontwin = FrontWindow(); |
| 2167 | if ( eventPtr->what == updateEvt || SIOUXIsAppWindow(frontwin) ) { |
| 2168 | if (SIOUXHandleOneEvent(eventPtr)) |
| 2169 | return 0; /* Nothing happened to the Tcl event queue */ |
| 2170 | } |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2171 | return TkMacConvertEvent(eventPtr); |
Guido van Rossum | dfd428d | 1996-02-25 04:46:40 +0000 | [diff] [blame] | 2172 | } |
| 2173 | |
Guido van Rossum | ec22c92 | 1996-02-25 04:50:29 +0000 | [diff] [blame] | 2174 | #if GENERATINGCFM |
Guido van Rossum | dfd428d | 1996-02-25 04:46:40 +0000 | [diff] [blame] | 2175 | |
| 2176 | /* |
| 2177 | ** Additional Mac specific code for dealing with shared libraries. |
| 2178 | */ |
| 2179 | |
| 2180 | #include <Resources.h> |
| 2181 | #include <CodeFragments.h> |
| 2182 | |
| 2183 | static int loaded_from_shlib = 0; |
| 2184 | static FSSpec library_fss; |
Guido van Rossum | 9722ad8 | 1995-09-22 23:49:28 +0000 | [diff] [blame] | 2185 | |
Jack Jansen | 34cc5c3 | 1995-10-31 16:15:12 +0000 | [diff] [blame] | 2186 | /* |
| 2187 | ** If this module is dynamically loaded the following routine should |
| 2188 | ** be the init routine. It takes care of adding the shared library to |
| 2189 | ** the resource-file chain, so that the tk routines can find their |
| 2190 | ** resources. |
| 2191 | */ |
| 2192 | OSErr pascal |
Guido van Rossum | 8ec9e63 | 1997-04-29 15:49:04 +0000 | [diff] [blame] | 2193 | init_tkinter_shlib(CFragInitBlockPtr data) |
Jack Jansen | 34cc5c3 | 1995-10-31 16:15:12 +0000 | [diff] [blame] | 2194 | { |
Guido van Rossum | c9970ee | 1996-08-26 14:37:15 +0000 | [diff] [blame] | 2195 | __initialize(); |
Jack Jansen | 34cc5c3 | 1995-10-31 16:15:12 +0000 | [diff] [blame] | 2196 | if ( data == nil ) return noErr; |
Guido van Rossum | 8ec9e63 | 1997-04-29 15:49:04 +0000 | [diff] [blame] | 2197 | if ( data->fragLocator.where == kDataForkCFragLocator ) { |
Jack Jansen | 34cc5c3 | 1995-10-31 16:15:12 +0000 | [diff] [blame] | 2198 | library_fss = *data->fragLocator.u.onDisk.fileSpec; |
| 2199 | loaded_from_shlib = 1; |
Guido van Rossum | 8ec9e63 | 1997-04-29 15:49:04 +0000 | [diff] [blame] | 2200 | } else if ( data->fragLocator.where == kResourceCFragLocator ) { |
Jack Jansen | 34cc5c3 | 1995-10-31 16:15:12 +0000 | [diff] [blame] | 2201 | library_fss = *data->fragLocator.u.inSegs.fileSpec; |
| 2202 | loaded_from_shlib = 1; |
| 2203 | } |
| 2204 | return noErr; |
| 2205 | } |
| 2206 | |
| 2207 | /* |
| 2208 | ** Insert the library resources into the search path. Put them after |
| 2209 | ** the resources from the application. Again, we ignore errors. |
| 2210 | */ |
Guido van Rossum | dfd428d | 1996-02-25 04:46:40 +0000 | [diff] [blame] | 2211 | static |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 2212 | mac_addlibresources(void) |
Jack Jansen | 34cc5c3 | 1995-10-31 16:15:12 +0000 | [diff] [blame] | 2213 | { |
| 2214 | if ( !loaded_from_shlib ) |
| 2215 | return; |
| 2216 | (void)FSpOpenResFile(&library_fss, fsRdPerm); |
| 2217 | } |
| 2218 | |
Guido van Rossum | ec22c92 | 1996-02-25 04:50:29 +0000 | [diff] [blame] | 2219 | #endif /* GENERATINGCFM */ |
| 2220 | #endif /* macintosh */ |