blob: a67bc33df57ebb06f087529d52d2995a97aa27cf [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
Guido van Rossum524b5881995-01-04 19:10:35 +00002Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
Guido van Rossumf70e43a1991-02-19 12:39:46 +00004
5 All Rights Reserved
6
Guido van Rossumd266eb41996-10-25 14:44:06 +00007Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
Guido van Rossumf70e43a1991-02-19 12:39:46 +00009provided that the above copyright notice appear in all copies and that
Guido van Rossumd266eb41996-10-25 14:44:06 +000010both that copyright notice and this permission notice appear in
Guido van Rossumf70e43a1991-02-19 12:39:46 +000011supporting documentation, and that the names of Stichting Mathematisch
Guido van Rossumd266eb41996-10-25 14:44:06 +000012Centrum or CWI or Corporation for National Research Initiatives or
13CNRI not be used in advertising or publicity pertaining to
14distribution of the software without specific, written prior
15permission.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000016
Guido van Rossumd266eb41996-10-25 14:44:06 +000017While CWI is the initial source for this software, a modified version
18is made available by the Corporation for National Research Initiatives
19(CNRI) at the Internet address ftp://ftp.python.org.
20
21STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28PERFORMANCE OF THIS SOFTWARE.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000029
30******************************************************************/
31
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000032/* Stdwin module */
33
34/* Stdwin itself is a module, not a separate object type.
35 Object types defined here:
36 wp: a window
37 dp: a drawing structure (only one can exist at a time)
38 mp: a menu
39 tp: a textedit block
Guido van Rossumbf80e541993-02-08 15:49:17 +000040 bp: a bitmap
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000041*/
42
43/* Rules for translating C stdwin function calls into Python stwin:
44 - All names drop their initial letter 'w'
45 - Functions with a window as first parameter are methods of window objects
46 - There is no equivalent for wclose(); just delete the window object
47 (all references to it!) (XXX maybe this is a bad idea)
48 - w.begindrawing() returns a drawing object
49 - There is no equivalent for wenddrawing(win); just delete the drawing
50 object (all references to it!) (XXX maybe this is a bad idea)
51 - Functions that may only be used inside wbegindrawing / wendddrawing
52 are methods of the drawing object; this includes the text measurement
53 functions (which however have doubles as module functions).
54 - Methods of the drawing object drop an initial 'draw' from their name
55 if they have it, e.g., wdrawline() --> d.line()
56 - The obvious type conversions: int --> intobject; string --> stringobject
57 - A text parameter followed by a length parameter is only a text (string)
58 parameter in Python
59 - A point or other pair of horizontal and vertical coordinates is always
60 a pair of integers in Python
61 - Two points forming a rectangle or endpoints of a line segment are a
62 pair of points in Python
63 - The arguments to d.elarc() are three points.
64 - The functions wgetclip() and wsetclip() are translated into
65 stdwin.getcutbuffer() and stdwin.setcutbuffer(); 'clip' is really
66 a bad word for what these functions do (clipping has a different
67 meaning in the drawing world), while cutbuffer is standard X jargon.
Guido van Rossum01769f01990-10-30 13:39:00 +000068 XXX This must change again in the light of changes to stdwin!
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000069 - For textedit, similar rules hold, but they are less strict.
70 XXX more?
71*/
72
Barry Warsawc2feec31996-12-12 22:48:20 +000073#include "Python.h"
74
Jack Jansendc977a91995-08-14 12:17:18 +000075#ifdef macintosh
76#include "macglue.h"
77#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000078
Guido van Rossum0b0db8e1993-01-21 16:07:51 +000079#ifdef macintosh
Guido van Rossume9066061993-07-29 13:14:32 +000080#include ":::stdwin:H:stdwin.h"
Guido van Rossum0b0db8e1993-01-21 16:07:51 +000081#else /* !macintosh */
Guido van Rossum3f5da241990-12-20 15:06:42 +000082#include "stdwin.h"
Guido van Rossume9066061993-07-29 13:14:32 +000083#define HAVE_BITMAPS
Guido van Rossum0b0db8e1993-01-21 16:07:51 +000084#endif /* !macintosh */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000085
Guido van Rossumb6775db1994-08-01 11:34:53 +000086#ifdef WITH_THREAD
Guido van Rossumff4949e1992-08-05 19:58:53 +000087
88#include "thread.h"
89
90static type_lock StdwinLock; /* Lock held when interpreter not locked */
91
Barry Warsawc2feec31996-12-12 22:48:20 +000092#define BGN_STDWIN Py_BEGIN_ALLOW_THREADS acquire_lock(StdwinLock, 1);
93#define RET_STDWIN release_lock(StdwinLock); Py_BLOCK_THREADS
94#define END_STDWIN release_lock(StdwinLock); Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +000095
96#else
97
Barry Warsawc2feec31996-12-12 22:48:20 +000098#define BGN_STDWIN Py_BEGIN_ALLOW_THREADS
99#define RET_STDWIN Py_BLOCK_THREADS
100#define END_STDWIN Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000101
102#endif
103
Barry Warsawc2feec31996-12-12 22:48:20 +0000104#define getpointarg(v, a) PyArg_Parse(v, "(ii)", a, (a)+1)
105#define get3pointarg(v, a) PyArg_Parse(v, "((ii)(ii)(ii))", \
106 a, a+1, a+2, a+3, a+4, a+5)
107#define getrectarg(v, a) PyArg_Parse(v, "((ii)(ii))", a, a+1, a+2, a+3)
108#define getrectintarg(v, a) PyArg_Parse(v, "(((ii)(ii))i)", \
109 a, a+1, a+2, a+3, a+4)
110#define getpointintarg(v, a) PyArg_Parse(v, "((ii)i)", a, a+1, a+2)
111#define getrectpointarg(v, a) PyArg_Parse(v, "(((ii)(ii))(ii))", \
112 a, a+1, a+2, a+3, a+4, a+5)
Guido van Rossum234f9421993-06-17 12:35:49 +0000113
Barry Warsawc2feec31996-12-12 22:48:20 +0000114static PyObject *StdwinError; /* Exception stdwin.error */
Guido van Rossum87e7ea71991-12-10 14:00:03 +0000115
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000116/* Window and menu object types declared here because of forward references */
117
118typedef struct {
Barry Warsawc2feec31996-12-12 22:48:20 +0000119 PyObject_HEAD
120 PyObject *w_title;
121 WINDOW *w_win;
122 PyObject *w_attr; /* Attributes dictionary */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000123} windowobject;
124
Barry Warsawc2feec31996-12-12 22:48:20 +0000125staticforward PyTypeObject Windowtype;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000126
127#define is_windowobject(wp) ((wp)->ob_type == &Windowtype)
128
129typedef struct {
Barry Warsawc2feec31996-12-12 22:48:20 +0000130 PyObject_HEAD
131 MENU *m_menu;
132 int m_id;
133 PyObject *m_attr; /* Attributes dictionary */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000134} menuobject;
135
Barry Warsawc2feec31996-12-12 22:48:20 +0000136staticforward PyTypeObject Menutype;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000137
138#define is_menuobject(mp) ((mp)->ob_type == &Menutype)
139
Guido van Rossumbf80e541993-02-08 15:49:17 +0000140typedef struct {
Barry Warsawc2feec31996-12-12 22:48:20 +0000141 PyObject_HEAD
142 BITMAP *b_bitmap;
143 PyObject *b_attr; /* Attributes dictionary */
Guido van Rossumbf80e541993-02-08 15:49:17 +0000144} bitmapobject;
145
Barry Warsawc2feec31996-12-12 22:48:20 +0000146staticforward PyTypeObject Bitmaptype;
Guido van Rossumbf80e541993-02-08 15:49:17 +0000147
148#define is_bitmapobject(mp) ((mp)->ob_type == &Bitmaptype)
149
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000150
151/* Strongly stdwin-specific argument handlers */
152
153static int
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000154getmenudetail(v, ep)
Barry Warsawc2feec31996-12-12 22:48:20 +0000155 PyObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000156 EVENT *ep;
157{
Guido van Rossumfc58e581992-01-27 16:45:55 +0000158 menuobject *mp;
Barry Warsawc2feec31996-12-12 22:48:20 +0000159 if (!PyArg_Parse(v, "(Oi)", &mp, &ep->u.m.item))
Guido van Rossumfc58e581992-01-27 16:45:55 +0000160 return 0;
161 if (!is_menuobject(mp))
Barry Warsawc2feec31996-12-12 22:48:20 +0000162 return PyErr_BadArgument();
Guido van Rossumfc58e581992-01-27 16:45:55 +0000163 ep->u.m.id = mp->m_id;
164 return 1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000165}
166
167static int
168geteventarg(v, ep)
Barry Warsawc2feec31996-12-12 22:48:20 +0000169 PyObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000170 EVENT *ep;
171{
Barry Warsawc2feec31996-12-12 22:48:20 +0000172 PyObject *wp, *detail;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000173 int a[4];
Barry Warsawc2feec31996-12-12 22:48:20 +0000174 if (!PyArg_Parse(v, "(iOO)", &ep->type, &wp, &detail))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000175 return 0;
Guido van Rossumfc58e581992-01-27 16:45:55 +0000176 if (is_windowobject(wp))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000177 ep->window = ((windowobject *)wp) -> w_win;
Barry Warsawc2feec31996-12-12 22:48:20 +0000178 else if (wp == Py_None)
Guido van Rossumfc58e581992-01-27 16:45:55 +0000179 ep->window = NULL;
180 else
Barry Warsawc2feec31996-12-12 22:48:20 +0000181 return PyErr_BadArgument();
Guido van Rossumfc58e581992-01-27 16:45:55 +0000182 switch (ep->type) {
183 case WE_CHAR: {
184 char c;
Barry Warsawc2feec31996-12-12 22:48:20 +0000185 if (!PyArg_Parse(detail, "c", &c))
Guido van Rossumfc58e581992-01-27 16:45:55 +0000186 return 0;
187 ep->u.character = c;
188 return 1;
189 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000190 case WE_COMMAND:
191 return getintarg(detail, &ep->u.command);
192 case WE_DRAW:
193 if (!getrectarg(detail, a))
194 return 0;
195 ep->u.area.left = a[0];
196 ep->u.area.top = a[1];
197 ep->u.area.right = a[2];
198 ep->u.area.bottom = a[3];
199 return 1;
200 case WE_MOUSE_DOWN:
201 case WE_MOUSE_UP:
202 case WE_MOUSE_MOVE:
Barry Warsawc2feec31996-12-12 22:48:20 +0000203 return PyArg_Parse(detail, "((ii)iii)",
204 &ep->u.where.h, &ep->u.where.v,
205 &ep->u.where.clicks,
206 &ep->u.where.button,
207 &ep->u.where.mask);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000208 case WE_MENU:
209 return getmenudetail(detail, ep);
Guido van Rossum3ee199e1992-06-30 12:48:26 +0000210 case WE_KEY:
Barry Warsawc2feec31996-12-12 22:48:20 +0000211 return PyArg_Parse(detail, "(ii)",
212 &ep->u.key.code, &ep->u.key.mask);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000213 default:
214 return 1;
215 }
216}
217
218
219/* Return construction tools */
220
Barry Warsawc2feec31996-12-12 22:48:20 +0000221static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000222makepoint(a, b)
223 int a, b;
224{
Barry Warsawc2feec31996-12-12 22:48:20 +0000225 return Py_BuildValue("(ii)", a, b);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000226}
227
Barry Warsawc2feec31996-12-12 22:48:20 +0000228static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000229makerect(a, b, c, d)
230 int a, b, c, d;
231{
Barry Warsawc2feec31996-12-12 22:48:20 +0000232 return Py_BuildValue("((ii)(ii))", a, b, c, d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000233}
234
235
236/* Drawing objects */
237
238typedef struct {
Barry Warsawc2feec31996-12-12 22:48:20 +0000239 PyObject_HEAD
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000240 windowobject *d_ref;
241} drawingobject;
242
243static drawingobject *Drawing; /* Set to current drawing object, or NULL */
244
245/* Drawing methods */
246
Barry Warsawc2feec31996-12-12 22:48:20 +0000247static PyObject *
Guido van Rossum3c284741991-11-27 14:54:54 +0000248drawing_close(dp)
249 drawingobject *dp;
250{
251 if (dp->d_ref != NULL) {
252 wenddrawing(dp->d_ref->w_win);
253 Drawing = NULL;
Barry Warsawc2feec31996-12-12 22:48:20 +0000254 Py_DECREF(dp->d_ref);
Guido van Rossum3c284741991-11-27 14:54:54 +0000255 dp->d_ref = NULL;
256 }
Barry Warsawc2feec31996-12-12 22:48:20 +0000257 Py_INCREF(Py_None);
258 return Py_None;
Guido van Rossum3c284741991-11-27 14:54:54 +0000259}
Guido van Rossum77b46041992-01-14 18:41:24 +0000260
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000261static void
262drawing_dealloc(dp)
263 drawingobject *dp;
264{
Guido van Rossum3c284741991-11-27 14:54:54 +0000265 if (dp->d_ref != NULL) {
266 wenddrawing(dp->d_ref->w_win);
267 Drawing = NULL;
Barry Warsawc2feec31996-12-12 22:48:20 +0000268 Py_DECREF(dp->d_ref);
Guido van Rossum3c284741991-11-27 14:54:54 +0000269 dp->d_ref = NULL;
270 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000271 free((char *)dp);
272}
273
Barry Warsawc2feec31996-12-12 22:48:20 +0000274static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000275drawing_generic(dp, args, func)
276 drawingobject *dp;
Barry Warsawc2feec31996-12-12 22:48:20 +0000277 PyObject *args;
278 void (*func) Py_FPROTO((int, int, int, int));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000279{
280 int a[4];
281 if (!getrectarg(args, a))
282 return NULL;
283 (*func)(a[0], a[1], a[2], a[3]);
Barry Warsawc2feec31996-12-12 22:48:20 +0000284 Py_INCREF(Py_None);
285 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000286}
287
Barry Warsawc2feec31996-12-12 22:48:20 +0000288static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000289drawing_line(dp, args)
290 drawingobject *dp;
Barry Warsawc2feec31996-12-12 22:48:20 +0000291 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000292{
Guido van Rossumbf109731991-03-06 13:14:12 +0000293 return drawing_generic(dp, args, wdrawline);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000294}
295
Barry Warsawc2feec31996-12-12 22:48:20 +0000296static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000297drawing_xorline(dp, args)
298 drawingobject *dp;
Barry Warsawc2feec31996-12-12 22:48:20 +0000299 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000300{
Guido van Rossumbf109731991-03-06 13:14:12 +0000301 return drawing_generic(dp, args, wxorline);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000302}
303
Barry Warsawc2feec31996-12-12 22:48:20 +0000304static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000305drawing_circle(dp, args)
306 drawingobject *dp;
Barry Warsawc2feec31996-12-12 22:48:20 +0000307 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000308{
309 int a[3];
310 if (!getpointintarg(args, a))
311 return NULL;
312 wdrawcircle(a[0], a[1], a[2]);
Barry Warsawc2feec31996-12-12 22:48:20 +0000313 Py_INCREF(Py_None);
314 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000315}
Guido van Rossuma2a181a1991-05-14 12:09:25 +0000316
Barry Warsawc2feec31996-12-12 22:48:20 +0000317static PyObject *
Guido van Rossum27201061991-04-16 08:43:03 +0000318drawing_fillcircle(dp, args)
319 drawingobject *dp;
Barry Warsawc2feec31996-12-12 22:48:20 +0000320 PyObject *args;
Guido van Rossum27201061991-04-16 08:43:03 +0000321{
322 int a[3];
323 if (!getpointintarg(args, a))
324 return NULL;
325 wfillcircle(a[0], a[1], a[2]);
Barry Warsawc2feec31996-12-12 22:48:20 +0000326 Py_INCREF(Py_None);
327 return Py_None;
Guido van Rossum27201061991-04-16 08:43:03 +0000328}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000329
Barry Warsawc2feec31996-12-12 22:48:20 +0000330static PyObject *
Guido van Rossuma2a181a1991-05-14 12:09:25 +0000331drawing_xorcircle(dp, args)
332 drawingobject *dp;
Barry Warsawc2feec31996-12-12 22:48:20 +0000333 PyObject *args;
Guido van Rossuma2a181a1991-05-14 12:09:25 +0000334{
335 int a[3];
336 if (!getpointintarg(args, a))
337 return NULL;
338 wxorcircle(a[0], a[1], a[2]);
Barry Warsawc2feec31996-12-12 22:48:20 +0000339 Py_INCREF(Py_None);
340 return Py_None;
Guido van Rossuma2a181a1991-05-14 12:09:25 +0000341}
342
Barry Warsawc2feec31996-12-12 22:48:20 +0000343static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000344drawing_elarc(dp, args)
345 drawingobject *dp;
Barry Warsawc2feec31996-12-12 22:48:20 +0000346 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000347{
348 int a[6];
349 if (!get3pointarg(args, a))
350 return NULL;
351 wdrawelarc(a[0], a[1], a[2], a[3], a[4], a[5]);
Barry Warsawc2feec31996-12-12 22:48:20 +0000352 Py_INCREF(Py_None);
353 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000354}
355
Barry Warsawc2feec31996-12-12 22:48:20 +0000356static PyObject *
Guido van Rossum27201061991-04-16 08:43:03 +0000357drawing_fillelarc(dp, args)
358 drawingobject *dp;
Barry Warsawc2feec31996-12-12 22:48:20 +0000359 PyObject *args;
Guido van Rossum27201061991-04-16 08:43:03 +0000360{
361 int a[6];
362 if (!get3pointarg(args, a))
363 return NULL;
364 wfillelarc(a[0], a[1], a[2], a[3], a[4], a[5]);
Barry Warsawc2feec31996-12-12 22:48:20 +0000365 Py_INCREF(Py_None);
366 return Py_None;
Guido van Rossum27201061991-04-16 08:43:03 +0000367}
368
Barry Warsawc2feec31996-12-12 22:48:20 +0000369static PyObject *
Guido van Rossuma2a181a1991-05-14 12:09:25 +0000370drawing_xorelarc(dp, args)
371 drawingobject *dp;
Barry Warsawc2feec31996-12-12 22:48:20 +0000372 PyObject *args;
Guido van Rossuma2a181a1991-05-14 12:09:25 +0000373{
374 int a[6];
375 if (!get3pointarg(args, a))
376 return NULL;
377 wxorelarc(a[0], a[1], a[2], a[3], a[4], a[5]);
Barry Warsawc2feec31996-12-12 22:48:20 +0000378 Py_INCREF(Py_None);
379 return Py_None;
Guido van Rossuma2a181a1991-05-14 12:09:25 +0000380}
381
Barry Warsawc2feec31996-12-12 22:48:20 +0000382static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000383drawing_box(dp, args)
384 drawingobject *dp;
Barry Warsawc2feec31996-12-12 22:48:20 +0000385 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000386{
Guido van Rossumbf109731991-03-06 13:14:12 +0000387 return drawing_generic(dp, args, wdrawbox);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000388}
389
Barry Warsawc2feec31996-12-12 22:48:20 +0000390static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000391drawing_erase(dp, args)
392 drawingobject *dp;
Barry Warsawc2feec31996-12-12 22:48:20 +0000393 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000394{
Guido van Rossumbf109731991-03-06 13:14:12 +0000395 return drawing_generic(dp, args, werase);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000396}
397
Barry Warsawc2feec31996-12-12 22:48:20 +0000398static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000399drawing_paint(dp, args)
400 drawingobject *dp;
Barry Warsawc2feec31996-12-12 22:48:20 +0000401 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000402{
Guido van Rossumbf109731991-03-06 13:14:12 +0000403 return drawing_generic(dp, args, wpaint);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000404}
405
Barry Warsawc2feec31996-12-12 22:48:20 +0000406static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000407drawing_invert(dp, args)
408 drawingobject *dp;
Barry Warsawc2feec31996-12-12 22:48:20 +0000409 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000410{
Guido van Rossumbf109731991-03-06 13:14:12 +0000411 return drawing_generic(dp, args, winvert);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000412}
413
Guido van Rossum27201061991-04-16 08:43:03 +0000414static POINT *
415getpointsarray(v, psize)
Barry Warsawc2feec31996-12-12 22:48:20 +0000416 PyObject *v;
Guido van Rossum27201061991-04-16 08:43:03 +0000417 int *psize;
418{
419 int n = -1;
Barry Warsawc2feec31996-12-12 22:48:20 +0000420 PyObject * (*getitem) Py_PROTO((PyObject *, int));
Guido van Rossum27201061991-04-16 08:43:03 +0000421 int i;
422 POINT *points;
423
424 if (v == NULL)
425 ;
Barry Warsawc2feec31996-12-12 22:48:20 +0000426 else if (PyList_Check(v)) {
427 n = PyList_Size(v);
428 getitem = PyList_GetItem;
Guido van Rossum27201061991-04-16 08:43:03 +0000429 }
Barry Warsawc2feec31996-12-12 22:48:20 +0000430 else if (PyTuple_Check(v)) {
431 n = PyTuple_Size(v);
432 getitem = PyTuple_GetItem;
Guido van Rossum27201061991-04-16 08:43:03 +0000433 }
434
435 if (n <= 0) {
Barry Warsawc2feec31996-12-12 22:48:20 +0000436 (void) PyErr_BadArgument();
Guido van Rossum27201061991-04-16 08:43:03 +0000437 return NULL;
438 }
439
Barry Warsawc2feec31996-12-12 22:48:20 +0000440 points = PyMem_NEW(POINT, n);
Guido van Rossum27201061991-04-16 08:43:03 +0000441 if (points == NULL) {
Barry Warsawc2feec31996-12-12 22:48:20 +0000442 (void) PyErr_NoMemory();
Guido van Rossum27201061991-04-16 08:43:03 +0000443 return NULL;
444 }
445
446 for (i = 0; i < n; i++) {
Barry Warsawc2feec31996-12-12 22:48:20 +0000447 PyObject *w = (*getitem)(v, i);
Guido van Rossum27201061991-04-16 08:43:03 +0000448 int a[2];
449 if (!getpointarg(w, a)) {
Barry Warsawc2feec31996-12-12 22:48:20 +0000450 PyMem_DEL(points);
Guido van Rossum27201061991-04-16 08:43:03 +0000451 return NULL;
452 }
453 points[i].h = a[0];
454 points[i].v = a[1];
455 }
456
457 *psize = n;
458 return points;
459}
460
Barry Warsawc2feec31996-12-12 22:48:20 +0000461static PyObject *
Guido van Rossum27201061991-04-16 08:43:03 +0000462drawing_poly(dp, args)
463 drawingobject *dp;
Barry Warsawc2feec31996-12-12 22:48:20 +0000464 PyObject *args;
Guido van Rossum27201061991-04-16 08:43:03 +0000465{
466 int n;
467 POINT *points = getpointsarray(args, &n);
468 if (points == NULL)
469 return NULL;
470 wdrawpoly(n, points);
Barry Warsawc2feec31996-12-12 22:48:20 +0000471 PyMem_DEL(points);
472 Py_INCREF(Py_None);
473 return Py_None;
Guido van Rossum27201061991-04-16 08:43:03 +0000474}
475
Barry Warsawc2feec31996-12-12 22:48:20 +0000476static PyObject *
Guido van Rossum27201061991-04-16 08:43:03 +0000477drawing_fillpoly(dp, args)
478 drawingobject *dp;
Barry Warsawc2feec31996-12-12 22:48:20 +0000479 PyObject *args;
Guido van Rossum27201061991-04-16 08:43:03 +0000480{
481 int n;
482 POINT *points = getpointsarray(args, &n);
483 if (points == NULL)
484 return NULL;
485 wfillpoly(n, points);
Barry Warsawc2feec31996-12-12 22:48:20 +0000486 PyMem_DEL(points);
487 Py_INCREF(Py_None);
488 return Py_None;
Guido van Rossum27201061991-04-16 08:43:03 +0000489}
490
Barry Warsawc2feec31996-12-12 22:48:20 +0000491static PyObject *
Guido van Rossuma2a181a1991-05-14 12:09:25 +0000492drawing_xorpoly(dp, args)
493 drawingobject *dp;
Barry Warsawc2feec31996-12-12 22:48:20 +0000494 PyObject *args;
Guido van Rossuma2a181a1991-05-14 12:09:25 +0000495{
496 int n;
497 POINT *points = getpointsarray(args, &n);
498 if (points == NULL)
499 return NULL;
500 wxorpoly(n, points);
Barry Warsawc2feec31996-12-12 22:48:20 +0000501 PyMem_DEL(points);
502 Py_INCREF(Py_None);
503 return Py_None;
Guido van Rossuma2a181a1991-05-14 12:09:25 +0000504}
505
Barry Warsawc2feec31996-12-12 22:48:20 +0000506static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000507drawing_cliprect(dp, args)
508 drawingobject *dp;
Barry Warsawc2feec31996-12-12 22:48:20 +0000509 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000510{
Guido van Rossumbf109731991-03-06 13:14:12 +0000511 return drawing_generic(dp, args, wcliprect);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000512}
513
Barry Warsawc2feec31996-12-12 22:48:20 +0000514static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000515drawing_noclip(dp, args)
516 drawingobject *dp;
Barry Warsawc2feec31996-12-12 22:48:20 +0000517 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000518{
Barry Warsawc2feec31996-12-12 22:48:20 +0000519 if (!PyArg_NoArgs(args))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000520 return NULL;
521 wnoclip();
Barry Warsawc2feec31996-12-12 22:48:20 +0000522 Py_INCREF(Py_None);
523 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000524}
525
Barry Warsawc2feec31996-12-12 22:48:20 +0000526static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000527drawing_shade(dp, args)
528 drawingobject *dp;
Barry Warsawc2feec31996-12-12 22:48:20 +0000529 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000530{
531 int a[5];
532 if (!getrectintarg(args, a))
533 return NULL;
534 wshade(a[0], a[1], a[2], a[3], a[4]);
Barry Warsawc2feec31996-12-12 22:48:20 +0000535 Py_INCREF(Py_None);
536 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000537}
538
Barry Warsawc2feec31996-12-12 22:48:20 +0000539static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000540drawing_text(dp, args)
541 drawingobject *dp;
Barry Warsawc2feec31996-12-12 22:48:20 +0000542 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000543{
Guido van Rossumfc58e581992-01-27 16:45:55 +0000544 int h, v, size;
545 char *text;
Barry Warsawc2feec31996-12-12 22:48:20 +0000546 if (!PyArg_Parse(args, "((ii)s#)", &h, &v, &text, &size))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000547 return NULL;
Guido van Rossumfc58e581992-01-27 16:45:55 +0000548 wdrawtext(h, v, text, size);
Barry Warsawc2feec31996-12-12 22:48:20 +0000549 Py_INCREF(Py_None);
550 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000551}
552
553/* The following four are also used as stdwin functions */
554
Barry Warsawc2feec31996-12-12 22:48:20 +0000555static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000556drawing_lineheight(dp, args)
557 drawingobject *dp;
Barry Warsawc2feec31996-12-12 22:48:20 +0000558 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000559{
Barry Warsawc2feec31996-12-12 22:48:20 +0000560 if (!PyArg_NoArgs(args))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000561 return NULL;
Barry Warsawc2feec31996-12-12 22:48:20 +0000562 return PyInt_FromLong((long)wlineheight());
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000563}
564
Barry Warsawc2feec31996-12-12 22:48:20 +0000565static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000566drawing_baseline(dp, args)
567 drawingobject *dp;
Barry Warsawc2feec31996-12-12 22:48:20 +0000568 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000569{
Barry Warsawc2feec31996-12-12 22:48:20 +0000570 if (!PyArg_NoArgs(args))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000571 return NULL;
Barry Warsawc2feec31996-12-12 22:48:20 +0000572 return PyInt_FromLong((long)wbaseline());
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000573}
574
Barry Warsawc2feec31996-12-12 22:48:20 +0000575static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000576drawing_textwidth(dp, args)
577 drawingobject *dp;
Barry Warsawc2feec31996-12-12 22:48:20 +0000578 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000579{
Guido van Rossumfc58e581992-01-27 16:45:55 +0000580 char *text;
581 int size;
Barry Warsawc2feec31996-12-12 22:48:20 +0000582 if (!PyArg_Parse(args, "s#", &text, &size))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000583 return NULL;
Barry Warsawc2feec31996-12-12 22:48:20 +0000584 return PyInt_FromLong((long)wtextwidth(text, size));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000585}
586
Barry Warsawc2feec31996-12-12 22:48:20 +0000587static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000588drawing_textbreak(dp, args)
589 drawingobject *dp;
Barry Warsawc2feec31996-12-12 22:48:20 +0000590 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000591{
Guido van Rossumfc58e581992-01-27 16:45:55 +0000592 char *text;
593 int size, width;
Barry Warsawc2feec31996-12-12 22:48:20 +0000594 if (!PyArg_Parse(args, "(s#i)", &text, &size, &width))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000595 return NULL;
Barry Warsawc2feec31996-12-12 22:48:20 +0000596 return PyInt_FromLong((long)wtextbreak(text, size, width));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000597}
598
Barry Warsawc2feec31996-12-12 22:48:20 +0000599static PyObject *
Guido van Rossum0c2290b1991-04-03 19:12:14 +0000600drawing_setfont(self, args)
601 drawingobject *self;
Barry Warsawc2feec31996-12-12 22:48:20 +0000602 PyObject *args;
Guido van Rossum0c2290b1991-04-03 19:12:14 +0000603{
Guido van Rossumfc58e581992-01-27 16:45:55 +0000604 char *font;
605 char style = '\0';
606 int size = 0;
Barry Warsawc2feec31996-12-12 22:48:20 +0000607 if (args == NULL || !PyTuple_Check(args)) {
608 if (!PyArg_Parse(args, "z", &font))
Guido van Rossum50429a11991-04-04 15:24:07 +0000609 return NULL;
610 }
611 else {
Barry Warsawc2feec31996-12-12 22:48:20 +0000612 int n = PyTuple_Size(args);
Guido van Rossumfc58e581992-01-27 16:45:55 +0000613 if (n == 2) {
Barry Warsawc2feec31996-12-12 22:48:20 +0000614 if (!PyArg_Parse(args, "(zi)", &font, &size))
Guido van Rossumfc58e581992-01-27 16:45:55 +0000615 return NULL;
616 }
Barry Warsawc2feec31996-12-12 22:48:20 +0000617 else if (!PyArg_Parse(args, "(zic)", &font, &size, &style)) {
618 PyErr_Clear();
619 if (!PyArg_Parse(args, "(zci)", &font, &style, &size))
Guido van Rossumfc58e581992-01-27 16:45:55 +0000620 return NULL;
Guido van Rossum50429a11991-04-04 15:24:07 +0000621 }
622 }
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000623 if (font != NULL) {
624 if (!wsetfont(font)) {
Barry Warsawc2feec31996-12-12 22:48:20 +0000625 PyErr_SetString(StdwinError, "font not found");
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000626 return NULL;
627 }
628 }
Guido van Rossum50429a11991-04-04 15:24:07 +0000629 if (size != 0)
630 wsetsize(size);
Guido van Rossumfc58e581992-01-27 16:45:55 +0000631 switch (style) {
632 case 'b':
633 wsetbold();
634 break;
635 case 'i':
636 wsetitalic();
637 break;
638 case 'o':
639 wsetbolditalic();
640 break;
641 case 'u':
642 wsetunderline();
643 break;
644 case 'p':
645 wsetplain();
646 break;
647 }
Barry Warsawc2feec31996-12-12 22:48:20 +0000648 Py_INCREF(Py_None);
649 return Py_None;
Guido van Rossum0c2290b1991-04-03 19:12:14 +0000650}
651
Barry Warsawc2feec31996-12-12 22:48:20 +0000652static PyObject *
Guido van Rossum0c2290b1991-04-03 19:12:14 +0000653drawing_getbgcolor(self, args)
Barry Warsawc2feec31996-12-12 22:48:20 +0000654 PyObject *self;
655 PyObject *args;
Guido van Rossum0c2290b1991-04-03 19:12:14 +0000656{
Barry Warsawc2feec31996-12-12 22:48:20 +0000657 if (!PyArg_NoArgs(args))
Guido van Rossum0c2290b1991-04-03 19:12:14 +0000658 return NULL;
Barry Warsawc2feec31996-12-12 22:48:20 +0000659 return PyInt_FromLong((long)wgetbgcolor());
Guido van Rossum0c2290b1991-04-03 19:12:14 +0000660}
661
Barry Warsawc2feec31996-12-12 22:48:20 +0000662static PyObject *
Guido van Rossum0c2290b1991-04-03 19:12:14 +0000663drawing_getfgcolor(self, args)
Barry Warsawc2feec31996-12-12 22:48:20 +0000664 PyObject *self;
665 PyObject *args;
Guido van Rossum0c2290b1991-04-03 19:12:14 +0000666{
Barry Warsawc2feec31996-12-12 22:48:20 +0000667 if (!PyArg_NoArgs(args))
Guido van Rossum0c2290b1991-04-03 19:12:14 +0000668 return NULL;
Barry Warsawc2feec31996-12-12 22:48:20 +0000669 return PyInt_FromLong((long)wgetfgcolor());
Guido van Rossum0c2290b1991-04-03 19:12:14 +0000670}
671
Barry Warsawc2feec31996-12-12 22:48:20 +0000672static PyObject *
Guido van Rossum0c2290b1991-04-03 19:12:14 +0000673drawing_setbgcolor(self, args)
Barry Warsawc2feec31996-12-12 22:48:20 +0000674 PyObject *self;
675 PyObject *args;
Guido van Rossum0c2290b1991-04-03 19:12:14 +0000676{
677 long color;
678 if (!getlongarg(args, &color))
679 return NULL;
680 wsetbgcolor((COLOR)color);
Barry Warsawc2feec31996-12-12 22:48:20 +0000681 Py_INCREF(Py_None);
682 return Py_None;
Guido van Rossum0c2290b1991-04-03 19:12:14 +0000683}
684
Barry Warsawc2feec31996-12-12 22:48:20 +0000685static PyObject *
Guido van Rossum0c2290b1991-04-03 19:12:14 +0000686drawing_setfgcolor(self, args)
Barry Warsawc2feec31996-12-12 22:48:20 +0000687 PyObject *self;
688 PyObject *args;
Guido van Rossum0c2290b1991-04-03 19:12:14 +0000689{
690 long color;
691 if (!getlongarg(args, &color))
692 return NULL;
693 wsetfgcolor((COLOR)color);
Barry Warsawc2feec31996-12-12 22:48:20 +0000694 Py_INCREF(Py_None);
695 return Py_None;
Guido van Rossum0c2290b1991-04-03 19:12:14 +0000696}
697
Guido van Rossume9066061993-07-29 13:14:32 +0000698#ifdef HAVE_BITMAPS
699
Barry Warsawc2feec31996-12-12 22:48:20 +0000700static PyObject *
Guido van Rossumbf80e541993-02-08 15:49:17 +0000701drawing_bitmap(self, args)
Barry Warsawc2feec31996-12-12 22:48:20 +0000702 PyObject *self;
703 PyObject *args;
Guido van Rossumbf80e541993-02-08 15:49:17 +0000704{
705 int h, v;
Barry Warsawc2feec31996-12-12 22:48:20 +0000706 PyObject *bp;
707 PyObject *mask = NULL;
708 if (!PyArg_Parse(args, "((ii)O)", &h, &v, &bp)) {
709 PyErr_Clear();
710 if (!PyArg_Parse(args, "((ii)OO)", &h, &v, &bp, &mask))
Guido van Rossumbf80e541993-02-08 15:49:17 +0000711 return NULL;
Barry Warsawc2feec31996-12-12 22:48:20 +0000712 if (mask == Py_None)
Guido van Rossumbf80e541993-02-08 15:49:17 +0000713 mask = NULL;
714 else if (!is_bitmapobject(mask)) {
Barry Warsawc2feec31996-12-12 22:48:20 +0000715 PyErr_BadArgument();
Guido van Rossumbf80e541993-02-08 15:49:17 +0000716 return NULL;
717 }
718 }
719 if (!is_bitmapobject(bp)) {
Barry Warsawc2feec31996-12-12 22:48:20 +0000720 PyErr_BadArgument();
Guido van Rossumbf80e541993-02-08 15:49:17 +0000721 return NULL;
722 }
723 if (((bitmapobject *)bp)->b_bitmap == NULL ||
724 mask != NULL && ((bitmapobject *)mask)->b_bitmap == NULL) {
Barry Warsawc2feec31996-12-12 22:48:20 +0000725 PyErr_SetString(StdwinError, "bitmap object already close");
Guido van Rossumbf80e541993-02-08 15:49:17 +0000726 return NULL;
727 }
728 if (mask == NULL)
729 wdrawbitmap(h, v, ((bitmapobject *)bp)->b_bitmap, ALLBITS);
730 else
731 wdrawbitmap(h, v,
732 ((bitmapobject *)bp)->b_bitmap,
733 ((bitmapobject *)bp)->b_bitmap);
Barry Warsawc2feec31996-12-12 22:48:20 +0000734 Py_INCREF(Py_None);
735 return Py_None;
Guido van Rossumbf80e541993-02-08 15:49:17 +0000736}
737
Guido van Rossume9066061993-07-29 13:14:32 +0000738#endif /* HAVE_BITMAPS */
739
Barry Warsawc2feec31996-12-12 22:48:20 +0000740static PyMethodDef drawing_methods[] = {
Guido van Rossume9066061993-07-29 13:14:32 +0000741#ifdef HAVE_BITMAPS
Barry Warsawc2feec31996-12-12 22:48:20 +0000742 {"bitmap", (PyCFunction)drawing_bitmap},
Guido van Rossume9066061993-07-29 13:14:32 +0000743#endif
Barry Warsawc2feec31996-12-12 22:48:20 +0000744 {"box", (PyCFunction)drawing_box},
745 {"circle", (PyCFunction)drawing_circle},
746 {"cliprect", (PyCFunction)drawing_cliprect},
747 {"close", (PyCFunction)drawing_close},
748 {"elarc", (PyCFunction)drawing_elarc},
749 {"enddrawing", (PyCFunction)drawing_close},
750 {"erase", (PyCFunction)drawing_erase},
751 {"fillcircle", (PyCFunction)drawing_fillcircle},
752 {"fillelarc", (PyCFunction)drawing_fillelarc},
753 {"fillpoly", (PyCFunction)drawing_fillpoly},
754 {"invert", (PyCFunction)drawing_invert},
755 {"line", (PyCFunction)drawing_line},
756 {"noclip", (PyCFunction)drawing_noclip},
757 {"paint", (PyCFunction)drawing_paint},
758 {"poly", (PyCFunction)drawing_poly},
759 {"shade", (PyCFunction)drawing_shade},
760 {"text", (PyCFunction)drawing_text},
761 {"xorcircle", (PyCFunction)drawing_xorcircle},
762 {"xorelarc", (PyCFunction)drawing_xorelarc},
763 {"xorline", (PyCFunction)drawing_xorline},
764 {"xorpoly", (PyCFunction)drawing_xorpoly},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000765
766 /* Text measuring methods: */
Barry Warsawc2feec31996-12-12 22:48:20 +0000767 {"baseline", (PyCFunction)drawing_baseline},
768 {"lineheight", (PyCFunction)drawing_lineheight},
769 {"textbreak", (PyCFunction)drawing_textbreak},
770 {"textwidth", (PyCFunction)drawing_textwidth},
Guido van Rossum0c2290b1991-04-03 19:12:14 +0000771
772 /* Font setting methods: */
Barry Warsawc2feec31996-12-12 22:48:20 +0000773 {"setfont", (PyCFunction)drawing_setfont},
Guido van Rossum0c2290b1991-04-03 19:12:14 +0000774
775 /* Color methods: */
Barry Warsawc2feec31996-12-12 22:48:20 +0000776 {"getbgcolor", (PyCFunction)drawing_getbgcolor},
777 {"getfgcolor", (PyCFunction)drawing_getfgcolor},
778 {"setbgcolor", (PyCFunction)drawing_setbgcolor},
779 {"setfgcolor", (PyCFunction)drawing_setfgcolor},
Guido van Rossum0c2290b1991-04-03 19:12:14 +0000780
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000781 {NULL, NULL} /* sentinel */
782};
783
Barry Warsawc2feec31996-12-12 22:48:20 +0000784static PyObject *
Guido van Rossum77b46041992-01-14 18:41:24 +0000785drawing_getattr(dp, name)
786 drawingobject *dp;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000787 char *name;
788{
Guido van Rossum77b46041992-01-14 18:41:24 +0000789 if (dp->d_ref == NULL) {
Barry Warsawc2feec31996-12-12 22:48:20 +0000790 PyErr_SetString(StdwinError, "drawing object already closed");
Guido van Rossum77b46041992-01-14 18:41:24 +0000791 return NULL;
792 }
Barry Warsawc2feec31996-12-12 22:48:20 +0000793 return Py_FindMethod(drawing_methods, (PyObject *)dp, name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000794}
795
Barry Warsawc2feec31996-12-12 22:48:20 +0000796PyTypeObject Drawingtype = {
797 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000798 0, /*ob_size*/
799 "drawing", /*tp_name*/
800 sizeof(drawingobject), /*tp_size*/
801 0, /*tp_itemsize*/
802 /* methods */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000803 (destructor)drawing_dealloc, /*tp_dealloc*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000804 0, /*tp_print*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000805 (getattrfunc)drawing_getattr, /*tp_getattr*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000806 0, /*tp_setattr*/
807 0, /*tp_compare*/
808 0, /*tp_repr*/
809};
810
811
812/* Text(edit) objects */
813
814typedef struct {
Barry Warsawc2feec31996-12-12 22:48:20 +0000815 PyObject_HEAD
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000816 TEXTEDIT *t_text;
817 windowobject *t_ref;
Barry Warsawc2feec31996-12-12 22:48:20 +0000818 PyObject *t_attr; /* Attributes dictionary */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000819} textobject;
820
Barry Warsawc2feec31996-12-12 22:48:20 +0000821staticforward PyTypeObject Texttype;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000822
823static textobject *
824newtextobject(wp, left, top, right, bottom)
825 windowobject *wp;
826 int left, top, right, bottom;
827{
828 textobject *tp;
Barry Warsawc2feec31996-12-12 22:48:20 +0000829 tp = PyObject_NEW(textobject, &Texttype);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000830 if (tp == NULL)
831 return NULL;
832 tp->t_attr = NULL;
Barry Warsawc2feec31996-12-12 22:48:20 +0000833 Py_INCREF(wp);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000834 tp->t_ref = wp;
835 tp->t_text = tecreate(wp->w_win, left, top, right, bottom);
836 if (tp->t_text == NULL) {
Barry Warsawc2feec31996-12-12 22:48:20 +0000837 Py_DECREF(tp);
838 return (textobject *) PyErr_NoMemory();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000839 }
840 return tp;
841}
842
843/* Text(edit) methods */
844
845static void
846text_dealloc(tp)
847 textobject *tp;
848{
849 if (tp->t_text != NULL)
850 tefree(tp->t_text);
Barry Warsawc2feec31996-12-12 22:48:20 +0000851 Py_XDECREF(tp->t_attr);
852 Py_XDECREF(tp->t_ref);
853 PyMem_DEL(tp);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000854}
855
Barry Warsawc2feec31996-12-12 22:48:20 +0000856static PyObject *
Guido van Rossum3c284741991-11-27 14:54:54 +0000857text_close(tp, args)
858 textobject *tp;
Barry Warsawc2feec31996-12-12 22:48:20 +0000859 PyObject *args;
Guido van Rossum3c284741991-11-27 14:54:54 +0000860{
861 if (tp->t_text != NULL) {
862 tefree(tp->t_text);
863 tp->t_text = NULL;
864 }
865 if (tp->t_attr != NULL) {
Barry Warsawc2feec31996-12-12 22:48:20 +0000866 Py_DECREF(tp->t_attr);
Guido van Rossum3c284741991-11-27 14:54:54 +0000867 tp->t_attr = NULL;
868 }
869 if (tp->t_ref != NULL) {
Barry Warsawc2feec31996-12-12 22:48:20 +0000870 Py_DECREF(tp->t_ref);
Guido van Rossum3c284741991-11-27 14:54:54 +0000871 tp->t_ref = NULL;
872 }
Barry Warsawc2feec31996-12-12 22:48:20 +0000873 Py_INCREF(Py_None);
874 return Py_None;
Guido van Rossum3c284741991-11-27 14:54:54 +0000875}
876
Barry Warsawc2feec31996-12-12 22:48:20 +0000877static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000878text_arrow(self, args)
879 textobject *self;
Barry Warsawc2feec31996-12-12 22:48:20 +0000880 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000881{
882 int code;
883 if (!getintarg(args, &code))
884 return NULL;
885 tearrow(self->t_text, code);
Barry Warsawc2feec31996-12-12 22:48:20 +0000886 Py_INCREF(Py_None);
887 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000888}
889
Barry Warsawc2feec31996-12-12 22:48:20 +0000890static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000891text_draw(self, args)
892 textobject *self;
Barry Warsawc2feec31996-12-12 22:48:20 +0000893 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000894{
895 register TEXTEDIT *tp = self->t_text;
896 int a[4];
897 int left, top, right, bottom;
898 if (!getrectarg(args, a))
899 return NULL;
900 if (Drawing != NULL) {
Barry Warsawc2feec31996-12-12 22:48:20 +0000901 PyErr_SetString(StdwinError, "already drawing");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000902 return NULL;
903 }
904 /* Clip to text area and ignore if area is empty */
905 left = tegetleft(tp);
906 top = tegettop(tp);
907 right = tegetright(tp);
908 bottom = tegetbottom(tp);
909 if (a[0] < left) a[0] = left;
910 if (a[1] < top) a[1] = top;
911 if (a[2] > right) a[2] = right;
912 if (a[3] > bottom) a[3] = bottom;
913 if (a[0] < a[2] && a[1] < a[3]) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000914 wbegindrawing(self->t_ref->w_win);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000915 tedrawnew(tp, a[0], a[1], a[2], a[3]);
916 wenddrawing(self->t_ref->w_win);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000917 }
Barry Warsawc2feec31996-12-12 22:48:20 +0000918 Py_INCREF(Py_None);
919 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000920}
921
Barry Warsawc2feec31996-12-12 22:48:20 +0000922static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000923text_event(self, args)
924 textobject *self;
Barry Warsawc2feec31996-12-12 22:48:20 +0000925 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000926{
927 register TEXTEDIT *tp = self->t_text;
928 EVENT e;
929 if (!geteventarg(args, &e))
930 return NULL;
931 if (e.type == WE_MOUSE_DOWN) {
Guido van Rossum33f17701991-02-13 23:19:39 +0000932 /* Cheat at the margins */
933 int width, height;
934 wgetdocsize(e.window, &width, &height);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000935 if (e.u.where.h < 0 && tegetleft(tp) == 0)
936 e.u.where.h = 0;
Guido van Rossum33f17701991-02-13 23:19:39 +0000937 else if (e.u.where.h > width && tegetright(tp) == width)
938 e.u.where.h = width;
939 if (e.u.where.v < 0 && tegettop(tp) == 0)
940 e.u.where.v = 0;
941 else if (e.u.where.v > height && tegetright(tp) == height)
942 e.u.where.v = height;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000943 }
Barry Warsawc2feec31996-12-12 22:48:20 +0000944 return PyInt_FromLong((long) teevent(tp, &e));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000945}
946
Barry Warsawc2feec31996-12-12 22:48:20 +0000947static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000948text_getfocus(self, args)
949 textobject *self;
Barry Warsawc2feec31996-12-12 22:48:20 +0000950 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000951{
Barry Warsawc2feec31996-12-12 22:48:20 +0000952 if (!PyArg_NoArgs(args))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000953 return NULL;
954 return makepoint(tegetfoc1(self->t_text), tegetfoc2(self->t_text));
955}
956
Barry Warsawc2feec31996-12-12 22:48:20 +0000957static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000958text_getfocustext(self, args)
959 textobject *self;
Barry Warsawc2feec31996-12-12 22:48:20 +0000960 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000961{
962 int f1, f2;
963 char *text;
Barry Warsawc2feec31996-12-12 22:48:20 +0000964 if (!PyArg_NoArgs(args))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000965 return NULL;
966 f1 = tegetfoc1(self->t_text);
967 f2 = tegetfoc2(self->t_text);
968 text = tegettext(self->t_text);
Barry Warsawc2feec31996-12-12 22:48:20 +0000969 return PyString_FromStringAndSize(text + f1, f2-f1);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000970}
971
Barry Warsawc2feec31996-12-12 22:48:20 +0000972static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000973text_getrect(self, args)
974 textobject *self;
Barry Warsawc2feec31996-12-12 22:48:20 +0000975 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000976{
Barry Warsawc2feec31996-12-12 22:48:20 +0000977 if (!PyArg_NoArgs(args))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000978 return NULL;
979 return makerect(tegetleft(self->t_text),
980 tegettop(self->t_text),
981 tegetright(self->t_text),
982 tegetbottom(self->t_text));
983}
984
Barry Warsawc2feec31996-12-12 22:48:20 +0000985static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000986text_gettext(self, args)
987 textobject *self;
Barry Warsawc2feec31996-12-12 22:48:20 +0000988 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000989{
Barry Warsawc2feec31996-12-12 22:48:20 +0000990 if (!PyArg_NoArgs(args))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000991 return NULL;
Barry Warsawc2feec31996-12-12 22:48:20 +0000992 return PyString_FromStringAndSize(tegettext(self->t_text),
993 tegetlen(self->t_text));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000994}
995
Barry Warsawc2feec31996-12-12 22:48:20 +0000996static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000997text_move(self, args)
998 textobject *self;
Barry Warsawc2feec31996-12-12 22:48:20 +0000999 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001000{
1001 int a[4];
1002 if (!getrectarg(args, a))
1003 return NULL;
1004 temovenew(self->t_text, a[0], a[1], a[2], a[3]);
Barry Warsawc2feec31996-12-12 22:48:20 +00001005 Py_INCREF(Py_None);
1006 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001007}
1008
Barry Warsawc2feec31996-12-12 22:48:20 +00001009static PyObject *
Guido van Rossum4b9cf8e1991-05-28 21:57:04 +00001010text_replace(self, args)
1011 textobject *self;
Barry Warsawc2feec31996-12-12 22:48:20 +00001012 PyObject *args;
Guido van Rossum4b9cf8e1991-05-28 21:57:04 +00001013{
Guido van Rossumfc58e581992-01-27 16:45:55 +00001014 char *text;
Guido van Rossum4b9cf8e1991-05-28 21:57:04 +00001015 if (!getstrarg(args, &text))
1016 return NULL;
Guido van Rossumfc58e581992-01-27 16:45:55 +00001017 tereplace(self->t_text, text);
Barry Warsawc2feec31996-12-12 22:48:20 +00001018 Py_INCREF(Py_None);
1019 return Py_None;
Guido van Rossum4b9cf8e1991-05-28 21:57:04 +00001020}
1021
Barry Warsawc2feec31996-12-12 22:48:20 +00001022static PyObject *
Guido van Rossum4b9cf8e1991-05-28 21:57:04 +00001023text_setactive(self, args)
1024 textobject *self;
Barry Warsawc2feec31996-12-12 22:48:20 +00001025 PyObject *args;
Guido van Rossum4b9cf8e1991-05-28 21:57:04 +00001026{
1027 int flag;
1028 if (!getintarg(args, &flag))
1029 return NULL;
1030 tesetactive(self->t_text, flag);
Barry Warsawc2feec31996-12-12 22:48:20 +00001031 Py_INCREF(Py_None);
1032 return Py_None;
Guido van Rossum4b9cf8e1991-05-28 21:57:04 +00001033}
1034
Barry Warsawc2feec31996-12-12 22:48:20 +00001035static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001036text_setfocus(self, args)
1037 textobject *self;
Barry Warsawc2feec31996-12-12 22:48:20 +00001038 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001039{
1040 int a[2];
1041 if (!getpointarg(args, a))
1042 return NULL;
1043 tesetfocus(self->t_text, a[0], a[1]);
Barry Warsawc2feec31996-12-12 22:48:20 +00001044 Py_INCREF(Py_None);
1045 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001046}
1047
Barry Warsawc2feec31996-12-12 22:48:20 +00001048static PyObject *
Guido van Rossum541c8c01991-05-05 20:13:41 +00001049text_settext(self, args)
1050 textobject *self;
Barry Warsawc2feec31996-12-12 22:48:20 +00001051 PyObject *args;
Guido van Rossum541c8c01991-05-05 20:13:41 +00001052{
Guido van Rossumfc58e581992-01-27 16:45:55 +00001053 char *text;
Guido van Rossum541c8c01991-05-05 20:13:41 +00001054 char *buf;
1055 int size;
Barry Warsawc2feec31996-12-12 22:48:20 +00001056 if (!PyArg_Parse(args, "s#", &text, &size))
Guido van Rossum541c8c01991-05-05 20:13:41 +00001057 return NULL;
Barry Warsawc2feec31996-12-12 22:48:20 +00001058 if ((buf = PyMem_NEW(char, size)) == NULL) {
1059 return PyErr_NoMemory();
Guido van Rossum541c8c01991-05-05 20:13:41 +00001060 }
Guido van Rossumfc58e581992-01-27 16:45:55 +00001061 memcpy(buf, text, size);
Guido van Rossum541c8c01991-05-05 20:13:41 +00001062 tesetbuf(self->t_text, buf, size); /* Becomes owner of buffer */
Barry Warsawc2feec31996-12-12 22:48:20 +00001063 Py_INCREF(Py_None);
1064 return Py_None;
Guido van Rossum541c8c01991-05-05 20:13:41 +00001065}
1066
Barry Warsawc2feec31996-12-12 22:48:20 +00001067static PyObject *
Guido van Rossum4b9cf8e1991-05-28 21:57:04 +00001068text_setview(self, args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001069 textobject *self;
Barry Warsawc2feec31996-12-12 22:48:20 +00001070 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001071{
Guido van Rossum4b9cf8e1991-05-28 21:57:04 +00001072 int a[4];
Barry Warsawc2feec31996-12-12 22:48:20 +00001073 if (args == Py_None)
Guido van Rossum4b9cf8e1991-05-28 21:57:04 +00001074 tenoview(self->t_text);
1075 else {
1076 if (!getrectarg(args, a))
1077 return NULL;
1078 tesetview(self->t_text, a[0], a[1], a[2], a[3]);
1079 }
Barry Warsawc2feec31996-12-12 22:48:20 +00001080 Py_INCREF(Py_None);
1081 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001082}
1083
Barry Warsawc2feec31996-12-12 22:48:20 +00001084static PyMethodDef text_methods[] = {
1085 {"arrow", (PyCFunction)text_arrow},
1086 {"close", (PyCFunction)text_close},
1087 {"draw", (PyCFunction)text_draw},
1088 {"event", (PyCFunction)text_event},
1089 {"getfocus", (PyCFunction)text_getfocus},
1090 {"getfocustext",(PyCFunction)text_getfocustext},
1091 {"getrect", (PyCFunction)text_getrect},
1092 {"gettext", (PyCFunction)text_gettext},
1093 {"move", (PyCFunction)text_move},
1094 {"replace", (PyCFunction)text_replace},
1095 {"setactive", (PyCFunction)text_setactive},
1096 {"setfocus", (PyCFunction)text_setfocus},
1097 {"settext", (PyCFunction)text_settext},
1098 {"setview", (PyCFunction)text_setview},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001099 {NULL, NULL} /* sentinel */
1100};
1101
Barry Warsawc2feec31996-12-12 22:48:20 +00001102static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001103text_getattr(tp, name)
1104 textobject *tp;
1105 char *name;
1106{
Barry Warsawc2feec31996-12-12 22:48:20 +00001107 PyObject *v = NULL;
Guido van Rossum77b46041992-01-14 18:41:24 +00001108 if (tp->t_ref == NULL) {
Barry Warsawc2feec31996-12-12 22:48:20 +00001109 PyErr_SetString(StdwinError, "text object already closed");
Guido van Rossum77b46041992-01-14 18:41:24 +00001110 return NULL;
1111 }
Guido van Rossum85f50761991-10-20 20:22:50 +00001112 if (strcmp(name, "__dict__") == 0) {
1113 v = tp->t_attr;
1114 if (v == NULL)
Barry Warsawc2feec31996-12-12 22:48:20 +00001115 v = Py_None;
Guido van Rossum85f50761991-10-20 20:22:50 +00001116 }
1117 else if (tp->t_attr != NULL) {
Barry Warsawc2feec31996-12-12 22:48:20 +00001118 v = PyDict_GetItemString(tp->t_attr, name);
Guido van Rossum85f50761991-10-20 20:22:50 +00001119 }
1120 if (v != NULL) {
Barry Warsawc2feec31996-12-12 22:48:20 +00001121 Py_INCREF(v);
Guido van Rossum85f50761991-10-20 20:22:50 +00001122 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001123 }
Barry Warsawc2feec31996-12-12 22:48:20 +00001124 return Py_FindMethod(text_methods, (PyObject *)tp, name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001125}
1126
1127static int
1128text_setattr(tp, name, v)
1129 textobject *tp;
1130 char *name;
Barry Warsawc2feec31996-12-12 22:48:20 +00001131 PyObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001132{
1133 if (tp->t_attr == NULL) {
Barry Warsawc2feec31996-12-12 22:48:20 +00001134 tp->t_attr = PyDict_New();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001135 if (tp->t_attr == NULL)
1136 return -1;
1137 }
Guido van Rossum94472a01992-09-04 09:45:18 +00001138 if (v == NULL) {
Barry Warsawc2feec31996-12-12 22:48:20 +00001139 int rv = PyDict_DelItemString(tp->t_attr, name);
Guido van Rossum94472a01992-09-04 09:45:18 +00001140 if (rv < 0)
Barry Warsawc2feec31996-12-12 22:48:20 +00001141 PyErr_SetString(PyExc_AttributeError,
1142 "delete non-existing text object attribute");
Guido van Rossum94472a01992-09-04 09:45:18 +00001143 return rv;
1144 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001145 else
Barry Warsawc2feec31996-12-12 22:48:20 +00001146 return PyDict_SetItemString(tp->t_attr, name, v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001147}
1148
Barry Warsawc2feec31996-12-12 22:48:20 +00001149statichere PyTypeObject Texttype = {
1150 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001151 0, /*ob_size*/
1152 "textedit", /*tp_name*/
1153 sizeof(textobject), /*tp_size*/
1154 0, /*tp_itemsize*/
1155 /* methods */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001156 (destructor)text_dealloc, /*tp_dealloc*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001157 0, /*tp_print*/
Guido van Rossumb6775db1994-08-01 11:34:53 +00001158 (getattrfunc)text_getattr, /*tp_getattr*/
1159 (setattrfunc)text_setattr, /*tp_setattr*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001160 0, /*tp_compare*/
1161 0, /*tp_repr*/
1162};
1163
1164
1165/* Menu objects */
1166
Guido van Rossum2d14e211991-02-19 12:26:49 +00001167#define IDOFFSET 10 /* Menu IDs we use start here */
Guido van Rossum27201061991-04-16 08:43:03 +00001168#define MAXNMENU 200 /* Max #menus we allow */
Guido van Rossum2d14e211991-02-19 12:26:49 +00001169static menuobject *menulist[MAXNMENU];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001170
Barry Warsawc2feec31996-12-12 22:48:20 +00001171static menuobject *newmenuobject Py_PROTO((char *));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001172static menuobject *
1173newmenuobject(title)
Guido van Rossumfc58e581992-01-27 16:45:55 +00001174 char *title;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001175{
1176 int id;
1177 MENU *menu;
1178 menuobject *mp;
Guido van Rossum2d14e211991-02-19 12:26:49 +00001179 for (id = 0; id < MAXNMENU; id++) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001180 if (menulist[id] == NULL)
1181 break;
1182 }
Guido van Rossum27201061991-04-16 08:43:03 +00001183 if (id >= MAXNMENU) {
Barry Warsawc2feec31996-12-12 22:48:20 +00001184 PyErr_SetString(StdwinError, "creating too many menus");
Guido van Rossum27201061991-04-16 08:43:03 +00001185 return NULL;
1186 }
Guido van Rossumfc58e581992-01-27 16:45:55 +00001187 menu = wmenucreate(id + IDOFFSET, title);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001188 if (menu == NULL)
Barry Warsawc2feec31996-12-12 22:48:20 +00001189 return (menuobject *) PyErr_NoMemory();
1190 mp = PyObject_NEW(menuobject, &Menutype);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001191 if (mp != NULL) {
1192 mp->m_menu = menu;
Guido van Rossum2d14e211991-02-19 12:26:49 +00001193 mp->m_id = id + IDOFFSET;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001194 mp->m_attr = NULL;
1195 menulist[id] = mp;
1196 }
1197 else
1198 wmenudelete(menu);
1199 return mp;
1200}
1201
1202/* Menu methods */
1203
1204static void
1205menu_dealloc(mp)
1206 menuobject *mp;
1207{
1208
Guido van Rossum2d14e211991-02-19 12:26:49 +00001209 int id = mp->m_id - IDOFFSET;
1210 if (id >= 0 && id < MAXNMENU && menulist[id] == mp) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001211 menulist[id] = NULL;
1212 }
Guido van Rossum77b46041992-01-14 18:41:24 +00001213 if (mp->m_menu != NULL)
1214 wmenudelete(mp->m_menu);
Barry Warsawc2feec31996-12-12 22:48:20 +00001215 Py_XDECREF(mp->m_attr);
1216 PyMem_DEL(mp);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001217}
1218
Barry Warsawc2feec31996-12-12 22:48:20 +00001219static PyObject *
Guido van Rossum77b46041992-01-14 18:41:24 +00001220menu_close(mp, args)
1221 menuobject *mp;
Barry Warsawc2feec31996-12-12 22:48:20 +00001222 PyObject *args;
Guido van Rossum77b46041992-01-14 18:41:24 +00001223{
1224 int id = mp->m_id - IDOFFSET;
1225 if (id >= 0 && id < MAXNMENU && menulist[id] == mp) {
1226 menulist[id] = NULL;
1227 }
1228 mp->m_id = -1;
1229 if (mp->m_menu != NULL)
1230 wmenudelete(mp->m_menu);
1231 mp->m_menu = NULL;
Barry Warsawc2feec31996-12-12 22:48:20 +00001232 Py_XDECREF(mp->m_attr);
Guido van Rossum77b46041992-01-14 18:41:24 +00001233 mp->m_attr = NULL;
Barry Warsawc2feec31996-12-12 22:48:20 +00001234 Py_INCREF(Py_None);
1235 return Py_None;
Guido van Rossum77b46041992-01-14 18:41:24 +00001236}
1237
Barry Warsawc2feec31996-12-12 22:48:20 +00001238static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001239menu_additem(self, args)
1240 menuobject *self;
Barry Warsawc2feec31996-12-12 22:48:20 +00001241 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001242{
Guido van Rossumfc58e581992-01-27 16:45:55 +00001243 char *text;
1244 int shortcut = -1;
Barry Warsawc2feec31996-12-12 22:48:20 +00001245 if (PyTuple_Check(args)) {
Guido van Rossumfc58e581992-01-27 16:45:55 +00001246 char c;
Barry Warsawc2feec31996-12-12 22:48:20 +00001247 if (!PyArg_Parse(args, "(sc)", &text, &c))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001248 return NULL;
Guido van Rossumfc58e581992-01-27 16:45:55 +00001249 shortcut = c;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001250 }
Guido van Rossumfc58e581992-01-27 16:45:55 +00001251 else if (!getstrarg(args, &text))
1252 return NULL;
1253 wmenuadditem(self->m_menu, text, shortcut);
Barry Warsawc2feec31996-12-12 22:48:20 +00001254 Py_INCREF(Py_None);
1255 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001256}
1257
Barry Warsawc2feec31996-12-12 22:48:20 +00001258static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001259menu_setitem(self, args)
1260 menuobject *self;
Barry Warsawc2feec31996-12-12 22:48:20 +00001261 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001262{
1263 int index;
Guido van Rossumfc58e581992-01-27 16:45:55 +00001264 char *text;
Barry Warsawc2feec31996-12-12 22:48:20 +00001265 if (!PyArg_Parse(args, "(is)", &index, &text))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001266 return NULL;
Guido van Rossumfc58e581992-01-27 16:45:55 +00001267 wmenusetitem(self->m_menu, index, text);
Barry Warsawc2feec31996-12-12 22:48:20 +00001268 Py_INCREF(Py_None);
1269 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001270}
1271
Barry Warsawc2feec31996-12-12 22:48:20 +00001272static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001273menu_enable(self, args)
1274 menuobject *self;
Barry Warsawc2feec31996-12-12 22:48:20 +00001275 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001276{
1277 int index;
1278 int flag;
Barry Warsawc2feec31996-12-12 22:48:20 +00001279 if (!PyArg_Parse(args, "(ii)", &index, &flag))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001280 return NULL;
1281 wmenuenable(self->m_menu, index, flag);
Barry Warsawc2feec31996-12-12 22:48:20 +00001282 Py_INCREF(Py_None);
1283 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001284}
1285
Barry Warsawc2feec31996-12-12 22:48:20 +00001286static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001287menu_check(self, args)
1288 menuobject *self;
Barry Warsawc2feec31996-12-12 22:48:20 +00001289 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001290{
1291 int index;
1292 int flag;
Barry Warsawc2feec31996-12-12 22:48:20 +00001293 if (!PyArg_Parse(args, "(ii)", &index, &flag))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001294 return NULL;
1295 wmenucheck(self->m_menu, index, flag);
Barry Warsawc2feec31996-12-12 22:48:20 +00001296 Py_INCREF(Py_None);
1297 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001298}
1299
Barry Warsawc2feec31996-12-12 22:48:20 +00001300static PyMethodDef menu_methods[] = {
1301 {"additem", (PyCFunction)menu_additem},
1302 {"setitem", (PyCFunction)menu_setitem},
1303 {"enable", (PyCFunction)menu_enable},
1304 {"check", (PyCFunction)menu_check},
1305 {"close", (PyCFunction)menu_close},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001306 {NULL, NULL} /* sentinel */
1307};
1308
Barry Warsawc2feec31996-12-12 22:48:20 +00001309static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001310menu_getattr(mp, name)
1311 menuobject *mp;
1312 char *name;
1313{
Barry Warsawc2feec31996-12-12 22:48:20 +00001314 PyObject *v = NULL;
Guido van Rossum77b46041992-01-14 18:41:24 +00001315 if (mp->m_menu == NULL) {
Barry Warsawc2feec31996-12-12 22:48:20 +00001316 PyErr_SetString(StdwinError, "menu object already closed");
Guido van Rossum77b46041992-01-14 18:41:24 +00001317 return NULL;
1318 }
Guido van Rossum85f50761991-10-20 20:22:50 +00001319 if (strcmp(name, "__dict__") == 0) {
1320 v = mp->m_attr;
1321 if (v == NULL)
Barry Warsawc2feec31996-12-12 22:48:20 +00001322 v = Py_None;
Guido van Rossum85f50761991-10-20 20:22:50 +00001323 }
1324 else if (mp->m_attr != NULL) {
Barry Warsawc2feec31996-12-12 22:48:20 +00001325 v = PyDict_GetItemString(mp->m_attr, name);
Guido van Rossum85f50761991-10-20 20:22:50 +00001326 }
1327 if (v != NULL) {
Barry Warsawc2feec31996-12-12 22:48:20 +00001328 Py_INCREF(v);
Guido van Rossum85f50761991-10-20 20:22:50 +00001329 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001330 }
Barry Warsawc2feec31996-12-12 22:48:20 +00001331 return Py_FindMethod(menu_methods, (PyObject *)mp, name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001332}
1333
1334static int
1335menu_setattr(mp, name, v)
1336 menuobject *mp;
1337 char *name;
Barry Warsawc2feec31996-12-12 22:48:20 +00001338 PyObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001339{
1340 if (mp->m_attr == NULL) {
Barry Warsawc2feec31996-12-12 22:48:20 +00001341 mp->m_attr = PyDict_New();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001342 if (mp->m_attr == NULL)
1343 return -1;
1344 }
Guido van Rossum94472a01992-09-04 09:45:18 +00001345 if (v == NULL) {
Barry Warsawc2feec31996-12-12 22:48:20 +00001346 int rv = PyDict_DelItemString(mp->m_attr, name);
Guido van Rossum94472a01992-09-04 09:45:18 +00001347 if (rv < 0)
Barry Warsawc2feec31996-12-12 22:48:20 +00001348 PyErr_SetString(PyExc_AttributeError,
1349 "delete non-existing menu object attribute");
Guido van Rossum94472a01992-09-04 09:45:18 +00001350 return rv;
1351 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001352 else
Barry Warsawc2feec31996-12-12 22:48:20 +00001353 return PyDict_SetItemString(mp->m_attr, name, v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001354}
1355
Barry Warsawc2feec31996-12-12 22:48:20 +00001356statichere PyTypeObject Menutype = {
1357 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001358 0, /*ob_size*/
1359 "menu", /*tp_name*/
1360 sizeof(menuobject), /*tp_size*/
1361 0, /*tp_itemsize*/
1362 /* methods */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001363 (destructor)menu_dealloc, /*tp_dealloc*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001364 0, /*tp_print*/
Guido van Rossumb6775db1994-08-01 11:34:53 +00001365 (getattrfunc)menu_getattr, /*tp_getattr*/
1366 (setattrfunc)menu_setattr, /*tp_setattr*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001367 0, /*tp_compare*/
1368 0, /*tp_repr*/
1369};
1370
1371
Guido van Rossume9066061993-07-29 13:14:32 +00001372#ifdef HAVE_BITMAPS
1373
Guido van Rossumbf80e541993-02-08 15:49:17 +00001374/* Bitmaps objects */
1375
Barry Warsawc2feec31996-12-12 22:48:20 +00001376static bitmapobject *newbitmapobject Py_PROTO((int, int));
Guido van Rossumbf80e541993-02-08 15:49:17 +00001377static bitmapobject *
1378newbitmapobject(width, height)
1379 int width, height;
1380{
1381 BITMAP *bitmap;
1382 bitmapobject *bp;
1383 bitmap = wnewbitmap(width, height);
1384 if (bitmap == NULL)
Barry Warsawc2feec31996-12-12 22:48:20 +00001385 return (bitmapobject *) PyErr_NoMemory();
1386 bp = PyObject_NEW(bitmapobject, &Bitmaptype);
Guido van Rossumbf80e541993-02-08 15:49:17 +00001387 if (bp != NULL) {
1388 bp->b_bitmap = bitmap;
1389 bp->b_attr = NULL;
1390 }
1391 else
1392 wfreebitmap(bitmap);
1393 return bp;
1394}
1395
1396/* Bitmap methods */
1397
1398static void
1399bitmap_dealloc(bp)
1400 bitmapobject *bp;
1401{
1402 if (bp->b_bitmap != NULL)
1403 wfreebitmap(bp->b_bitmap);
Barry Warsawc2feec31996-12-12 22:48:20 +00001404 Py_XDECREF(bp->b_attr);
1405 PyMem_DEL(bp);
Guido van Rossumbf80e541993-02-08 15:49:17 +00001406}
1407
Barry Warsawc2feec31996-12-12 22:48:20 +00001408static PyObject *
Guido van Rossumbf80e541993-02-08 15:49:17 +00001409bitmap_close(bp, args)
1410 bitmapobject *bp;
Barry Warsawc2feec31996-12-12 22:48:20 +00001411 PyObject *args;
Guido van Rossumbf80e541993-02-08 15:49:17 +00001412{
1413 if (bp->b_bitmap != NULL)
1414 wfreebitmap(bp->b_bitmap);
1415 bp->b_bitmap = NULL;
Barry Warsawc2feec31996-12-12 22:48:20 +00001416 Py_XDECREF(bp->b_attr);
Guido van Rossumbf80e541993-02-08 15:49:17 +00001417 bp->b_attr = NULL;
Barry Warsawc2feec31996-12-12 22:48:20 +00001418 Py_INCREF(Py_None);
1419 return Py_None;
Guido van Rossumbf80e541993-02-08 15:49:17 +00001420}
1421
Barry Warsawc2feec31996-12-12 22:48:20 +00001422static PyObject *
Guido van Rossumbf80e541993-02-08 15:49:17 +00001423bitmap_setbit(self, args)
1424 bitmapobject *self;
Barry Warsawc2feec31996-12-12 22:48:20 +00001425 PyObject *args;
Guido van Rossumbf80e541993-02-08 15:49:17 +00001426{
1427 int a[3];
1428 if (!getpointintarg(args, a))
1429 return NULL;
1430 wsetbit(self->b_bitmap, a[0], a[1], a[2]);
Barry Warsawc2feec31996-12-12 22:48:20 +00001431 Py_INCREF(Py_None);
1432 return Py_None;
Guido van Rossumbf80e541993-02-08 15:49:17 +00001433}
1434
Barry Warsawc2feec31996-12-12 22:48:20 +00001435static PyObject *
Guido van Rossumbf80e541993-02-08 15:49:17 +00001436bitmap_getbit(self, args)
1437 bitmapobject *self;
Barry Warsawc2feec31996-12-12 22:48:20 +00001438 PyObject *args;
Guido van Rossumbf80e541993-02-08 15:49:17 +00001439{
1440 int a[2];
1441 if (!getpointarg(args, a))
1442 return NULL;
Barry Warsawc2feec31996-12-12 22:48:20 +00001443 return PyInt_FromLong((long) wgetbit(self->b_bitmap, a[0], a[1]));
Guido van Rossumbf80e541993-02-08 15:49:17 +00001444}
1445
Barry Warsawc2feec31996-12-12 22:48:20 +00001446static PyObject *
Guido van Rossumbf80e541993-02-08 15:49:17 +00001447bitmap_getsize(self, args)
1448 bitmapobject *self;
Barry Warsawc2feec31996-12-12 22:48:20 +00001449 PyObject *args;
Guido van Rossumbf80e541993-02-08 15:49:17 +00001450{
1451 int width, height;
Barry Warsawc2feec31996-12-12 22:48:20 +00001452 if (!PyArg_NoArgs(args))
Guido van Rossumbf80e541993-02-08 15:49:17 +00001453 return NULL;
1454 wgetbitmapsize(self->b_bitmap, &width, &height);
Barry Warsawc2feec31996-12-12 22:48:20 +00001455 return Py_BuildValue("(ii)", width, height);
Guido van Rossumbf80e541993-02-08 15:49:17 +00001456}
1457
Barry Warsawc2feec31996-12-12 22:48:20 +00001458static PyMethodDef bitmap_methods[] = {
1459 {"close", (PyCFunction)bitmap_close},
1460 {"getsize", (PyCFunction)bitmap_getsize},
1461 {"getbit", (PyCFunction)bitmap_getbit},
1462 {"setbit", (PyCFunction)bitmap_setbit},
Guido van Rossumbf80e541993-02-08 15:49:17 +00001463 {NULL, NULL} /* sentinel */
1464};
1465
Barry Warsawc2feec31996-12-12 22:48:20 +00001466static PyObject *
Guido van Rossumbf80e541993-02-08 15:49:17 +00001467bitmap_getattr(bp, name)
1468 bitmapobject *bp;
1469 char *name;
1470{
Barry Warsawc2feec31996-12-12 22:48:20 +00001471 PyObject *v = NULL;
Guido van Rossumbf80e541993-02-08 15:49:17 +00001472 if (bp->b_bitmap == NULL) {
Barry Warsawc2feec31996-12-12 22:48:20 +00001473 PyErr_SetString(StdwinError, "bitmap object already closed");
Guido van Rossumbf80e541993-02-08 15:49:17 +00001474 return NULL;
1475 }
1476 if (strcmp(name, "__dict__") == 0) {
1477 v = bp->b_attr;
1478 if (v == NULL)
Barry Warsawc2feec31996-12-12 22:48:20 +00001479 v = Py_None;
Guido van Rossumbf80e541993-02-08 15:49:17 +00001480 }
1481 else if (bp->b_attr != NULL) {
Barry Warsawc2feec31996-12-12 22:48:20 +00001482 v = PyDict_GetItemString(bp->b_attr, name);
Guido van Rossumbf80e541993-02-08 15:49:17 +00001483 }
1484 if (v != NULL) {
Barry Warsawc2feec31996-12-12 22:48:20 +00001485 Py_INCREF(v);
Guido van Rossumbf80e541993-02-08 15:49:17 +00001486 return v;
1487 }
Barry Warsawc2feec31996-12-12 22:48:20 +00001488 return Py_FindMethod(bitmap_methods, (PyObject *)bp, name);
Guido van Rossumbf80e541993-02-08 15:49:17 +00001489}
1490
1491static int
1492bitmap_setattr(bp, name, v)
1493 bitmapobject *bp;
1494 char *name;
Barry Warsawc2feec31996-12-12 22:48:20 +00001495 PyObject *v;
Guido van Rossumbf80e541993-02-08 15:49:17 +00001496{
1497 if (bp->b_attr == NULL) {
Barry Warsawc2feec31996-12-12 22:48:20 +00001498 bp->b_attr = PyDict_New();
Guido van Rossumbf80e541993-02-08 15:49:17 +00001499 if (bp->b_attr == NULL)
1500 return -1;
1501 }
1502 if (v == NULL) {
Barry Warsawc2feec31996-12-12 22:48:20 +00001503 int rv = PyDict_DelItemString(bp->b_attr, name);
Guido van Rossumbf80e541993-02-08 15:49:17 +00001504 if (rv < 0)
Barry Warsawc2feec31996-12-12 22:48:20 +00001505 PyErr_SetString(PyExc_AttributeError,
Guido van Rossumbf80e541993-02-08 15:49:17 +00001506 "delete non-existing bitmap object attribute");
1507 return rv;
1508 }
1509 else
Barry Warsawc2feec31996-12-12 22:48:20 +00001510 return PyDict_SetItemString(bp->b_attr, name, v);
Guido van Rossumbf80e541993-02-08 15:49:17 +00001511}
1512
Barry Warsawc2feec31996-12-12 22:48:20 +00001513statichere PyTypeObject Bitmaptype = {
1514 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossumbf80e541993-02-08 15:49:17 +00001515 0, /*ob_size*/
1516 "bitmap", /*tp_name*/
1517 sizeof(bitmapobject), /*tp_size*/
1518 0, /*tp_itemsize*/
1519 /* methods */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001520 (destructor)bitmap_dealloc, /*tp_dealloc*/
Guido van Rossumbf80e541993-02-08 15:49:17 +00001521 0, /*tp_print*/
Guido van Rossumb6775db1994-08-01 11:34:53 +00001522 (getattrfunc)bitmap_getattr, /*tp_getattr*/
1523 (setattrfunc)bitmap_setattr, /*tp_setattr*/
Guido van Rossumbf80e541993-02-08 15:49:17 +00001524 0, /*tp_compare*/
1525 0, /*tp_repr*/
1526};
1527
Guido van Rossume9066061993-07-29 13:14:32 +00001528#endif /* HAVE_BITMAPS */
1529
Guido van Rossumbf80e541993-02-08 15:49:17 +00001530
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001531/* Windows */
1532
1533#define MAXNWIN 50
1534static windowobject *windowlist[MAXNWIN];
1535
1536/* Window methods */
1537
1538static void
1539window_dealloc(wp)
1540 windowobject *wp;
1541{
1542 if (wp->w_win != NULL) {
1543 int tag = wgettag(wp->w_win);
1544 if (tag >= 0 && tag < MAXNWIN)
1545 windowlist[tag] = NULL;
1546 else
1547 fprintf(stderr, "XXX help! tag %d in window_dealloc\n",
1548 tag);
1549 wclose(wp->w_win);
1550 }
Barry Warsawc2feec31996-12-12 22:48:20 +00001551 Py_DECREF(wp->w_title);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001552 if (wp->w_attr != NULL)
Barry Warsawc2feec31996-12-12 22:48:20 +00001553 Py_DECREF(wp->w_attr);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001554 free((char *)wp);
1555}
1556
Barry Warsawc2feec31996-12-12 22:48:20 +00001557static PyObject *
Guido van Rossum3c284741991-11-27 14:54:54 +00001558window_close(wp, args)
1559 windowobject *wp;
Barry Warsawc2feec31996-12-12 22:48:20 +00001560 PyObject *args;
Guido van Rossum3c284741991-11-27 14:54:54 +00001561{
1562 if (wp->w_win != NULL) {
1563 int tag = wgettag(wp->w_win);
1564 if (tag >= 0 && tag < MAXNWIN)
1565 windowlist[tag] = NULL;
1566 wclose(wp->w_win);
1567 wp->w_win = NULL;
1568 }
Barry Warsawc2feec31996-12-12 22:48:20 +00001569 Py_INCREF(Py_None);
1570 return Py_None;
Guido van Rossum3c284741991-11-27 14:54:54 +00001571}
1572
Barry Warsawc2feec31996-12-12 22:48:20 +00001573static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001574window_begindrawing(wp, args)
1575 windowobject *wp;
Barry Warsawc2feec31996-12-12 22:48:20 +00001576 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001577{
1578 drawingobject *dp;
Barry Warsawc2feec31996-12-12 22:48:20 +00001579 if (!PyArg_NoArgs(args))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001580 return NULL;
1581 if (Drawing != NULL) {
Barry Warsawc2feec31996-12-12 22:48:20 +00001582 PyErr_SetString(StdwinError, "already drawing");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001583 return NULL;
1584 }
Barry Warsawc2feec31996-12-12 22:48:20 +00001585 dp = PyObject_NEW(drawingobject, &Drawingtype);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001586 if (dp == NULL)
1587 return NULL;
1588 Drawing = dp;
Barry Warsawc2feec31996-12-12 22:48:20 +00001589 Py_INCREF(wp);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001590 dp->d_ref = wp;
1591 wbegindrawing(wp->w_win);
Barry Warsawc2feec31996-12-12 22:48:20 +00001592 return (PyObject *)dp;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001593}
1594
Barry Warsawc2feec31996-12-12 22:48:20 +00001595static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001596window_change(wp, args)
1597 windowobject *wp;
Barry Warsawc2feec31996-12-12 22:48:20 +00001598 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001599{
1600 int a[4];
1601 if (!getrectarg(args, a))
1602 return NULL;
1603 wchange(wp->w_win, a[0], a[1], a[2], a[3]);
Barry Warsawc2feec31996-12-12 22:48:20 +00001604 Py_INCREF(Py_None);
1605 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001606}
1607
Barry Warsawc2feec31996-12-12 22:48:20 +00001608static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001609window_gettitle(wp, args)
1610 windowobject *wp;
Barry Warsawc2feec31996-12-12 22:48:20 +00001611 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001612{
Barry Warsawc2feec31996-12-12 22:48:20 +00001613 if (!PyArg_NoArgs(args))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001614 return NULL;
Barry Warsawc2feec31996-12-12 22:48:20 +00001615 Py_INCREF(wp->w_title);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001616 return wp->w_title;
1617}
1618
Barry Warsawc2feec31996-12-12 22:48:20 +00001619static PyObject *
Guido van Rossum541c8c01991-05-05 20:13:41 +00001620window_getwinpos(wp, args)
1621 windowobject *wp;
Barry Warsawc2feec31996-12-12 22:48:20 +00001622 PyObject *args;
Guido van Rossum541c8c01991-05-05 20:13:41 +00001623{
1624 int h, v;
Barry Warsawc2feec31996-12-12 22:48:20 +00001625 if (!PyArg_NoArgs(args))
Guido van Rossum541c8c01991-05-05 20:13:41 +00001626 return NULL;
1627 wgetwinpos(wp->w_win, &h, &v);
1628 return makepoint(h, v);
1629}
1630
Barry Warsawc2feec31996-12-12 22:48:20 +00001631static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001632window_getwinsize(wp, args)
1633 windowobject *wp;
Barry Warsawc2feec31996-12-12 22:48:20 +00001634 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001635{
1636 int width, height;
Barry Warsawc2feec31996-12-12 22:48:20 +00001637 if (!PyArg_NoArgs(args))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001638 return NULL;
1639 wgetwinsize(wp->w_win, &width, &height);
1640 return makepoint(width, height);
1641}
1642
Barry Warsawc2feec31996-12-12 22:48:20 +00001643static PyObject *
Guido van Rossumbf80e541993-02-08 15:49:17 +00001644window_setwinpos(wp, args)
1645 windowobject *wp;
Barry Warsawc2feec31996-12-12 22:48:20 +00001646 PyObject *args;
Guido van Rossumbf80e541993-02-08 15:49:17 +00001647{
1648 int a[2];
1649 if (!getpointarg(args, a))
1650 return NULL;
1651 wsetwinpos(wp->w_win, a[0], a[1]);
Barry Warsawc2feec31996-12-12 22:48:20 +00001652 Py_INCREF(Py_None);
1653 return Py_None;
Guido van Rossumbf80e541993-02-08 15:49:17 +00001654}
1655
Barry Warsawc2feec31996-12-12 22:48:20 +00001656static PyObject *
Guido van Rossumbf80e541993-02-08 15:49:17 +00001657window_setwinsize(wp, args)
1658 windowobject *wp;
Barry Warsawc2feec31996-12-12 22:48:20 +00001659 PyObject *args;
Guido van Rossumbf80e541993-02-08 15:49:17 +00001660{
1661 int a[2];
1662 if (!getpointarg(args, a))
1663 return NULL;
1664 wsetwinsize(wp->w_win, a[0], a[1]);
Barry Warsawc2feec31996-12-12 22:48:20 +00001665 Py_INCREF(Py_None);
1666 return Py_None;
Guido van Rossumbf80e541993-02-08 15:49:17 +00001667}
1668
Barry Warsawc2feec31996-12-12 22:48:20 +00001669static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001670window_getdocsize(wp, args)
1671 windowobject *wp;
Barry Warsawc2feec31996-12-12 22:48:20 +00001672 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001673{
1674 int width, height;
Barry Warsawc2feec31996-12-12 22:48:20 +00001675 if (!PyArg_NoArgs(args))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001676 return NULL;
1677 wgetdocsize(wp->w_win, &width, &height);
1678 return makepoint(width, height);
1679}
1680
Barry Warsawc2feec31996-12-12 22:48:20 +00001681static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001682window_getorigin(wp, args)
1683 windowobject *wp;
Barry Warsawc2feec31996-12-12 22:48:20 +00001684 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001685{
1686 int width, height;
Barry Warsawc2feec31996-12-12 22:48:20 +00001687 if (!PyArg_NoArgs(args))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001688 return NULL;
1689 wgetorigin(wp->w_win, &width, &height);
1690 return makepoint(width, height);
1691}
1692
Barry Warsawc2feec31996-12-12 22:48:20 +00001693static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001694window_scroll(wp, args)
1695 windowobject *wp;
Barry Warsawc2feec31996-12-12 22:48:20 +00001696 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001697{
1698 int a[6];
1699 if (!getrectpointarg(args, a))
1700 return NULL;
1701 wscroll(wp->w_win, a[0], a[1], a[2], a[3], a[4], a[5]);
Barry Warsawc2feec31996-12-12 22:48:20 +00001702 Py_INCREF(Py_None);
1703 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001704}
1705
Barry Warsawc2feec31996-12-12 22:48:20 +00001706static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001707window_setdocsize(wp, args)
1708 windowobject *wp;
Barry Warsawc2feec31996-12-12 22:48:20 +00001709 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001710{
1711 int a[2];
1712 if (!getpointarg(args, a))
1713 return NULL;
1714 wsetdocsize(wp->w_win, a[0], a[1]);
Barry Warsawc2feec31996-12-12 22:48:20 +00001715 Py_INCREF(Py_None);
1716 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001717}
1718
Barry Warsawc2feec31996-12-12 22:48:20 +00001719static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001720window_setorigin(wp, args)
1721 windowobject *wp;
Barry Warsawc2feec31996-12-12 22:48:20 +00001722 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001723{
1724 int a[2];
1725 if (!getpointarg(args, a))
1726 return NULL;
1727 wsetorigin(wp->w_win, a[0], a[1]);
Barry Warsawc2feec31996-12-12 22:48:20 +00001728 Py_INCREF(Py_None);
1729 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001730}
1731
Barry Warsawc2feec31996-12-12 22:48:20 +00001732static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001733window_settitle(wp, args)
1734 windowobject *wp;
Barry Warsawc2feec31996-12-12 22:48:20 +00001735 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001736{
Barry Warsawc2feec31996-12-12 22:48:20 +00001737 PyObject *title;
1738 if (!PyArg_Parse(args, "S", &title))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001739 return NULL;
Barry Warsawc2feec31996-12-12 22:48:20 +00001740 Py_DECREF(wp->w_title);
1741 Py_INCREF(title);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001742 wp->w_title = title;
Barry Warsawc2feec31996-12-12 22:48:20 +00001743 wsettitle(wp->w_win, PyString_AsString(title));
1744 Py_INCREF(Py_None);
1745 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001746}
1747
Barry Warsawc2feec31996-12-12 22:48:20 +00001748static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001749window_show(wp, args)
1750 windowobject *wp;
Barry Warsawc2feec31996-12-12 22:48:20 +00001751 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001752{
1753 int a[4];
1754 if (!getrectarg(args, a))
1755 return NULL;
1756 wshow(wp->w_win, a[0], a[1], a[2], a[3]);
Barry Warsawc2feec31996-12-12 22:48:20 +00001757 Py_INCREF(Py_None);
1758 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001759}
1760
Barry Warsawc2feec31996-12-12 22:48:20 +00001761static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001762window_settimer(wp, args)
1763 windowobject *wp;
Barry Warsawc2feec31996-12-12 22:48:20 +00001764 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001765{
1766 int a;
1767 if (!getintarg(args, &a))
1768 return NULL;
1769 wsettimer(wp->w_win, a);
Barry Warsawc2feec31996-12-12 22:48:20 +00001770 Py_INCREF(Py_None);
1771 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001772}
1773
Barry Warsawc2feec31996-12-12 22:48:20 +00001774static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001775window_menucreate(self, args)
1776 windowobject *self;
Barry Warsawc2feec31996-12-12 22:48:20 +00001777 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001778{
1779 menuobject *mp;
Guido van Rossumfc58e581992-01-27 16:45:55 +00001780 char *title;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001781 if (!getstrarg(args, &title))
1782 return NULL;
1783 wmenusetdeflocal(1);
1784 mp = newmenuobject(title);
1785 if (mp == NULL)
1786 return NULL;
1787 wmenuattach(self->w_win, mp->m_menu);
Barry Warsawc2feec31996-12-12 22:48:20 +00001788 return (PyObject *)mp;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001789}
1790
Barry Warsawc2feec31996-12-12 22:48:20 +00001791static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001792window_textcreate(self, args)
1793 windowobject *self;
Barry Warsawc2feec31996-12-12 22:48:20 +00001794 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001795{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001796 int a[4];
1797 if (!getrectarg(args, a))
1798 return NULL;
Barry Warsawc2feec31996-12-12 22:48:20 +00001799 return (PyObject *)newtextobject(self, a[0], a[1], a[2], a[3]);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001800}
1801
Barry Warsawc2feec31996-12-12 22:48:20 +00001802static PyObject *
Guido van Rossum5b10f451990-10-30 16:01:48 +00001803window_setselection(self, args)
1804 windowobject *self;
Barry Warsawc2feec31996-12-12 22:48:20 +00001805 PyObject *args;
Guido van Rossum5b10f451990-10-30 16:01:48 +00001806{
Guido van Rossumfc58e581992-01-27 16:45:55 +00001807 int sel, size, ok;
1808 char *text;
Barry Warsawc2feec31996-12-12 22:48:20 +00001809 if (!PyArg_Parse(args, "(is#)", &sel, &text, &size))
Guido van Rossum5b10f451990-10-30 16:01:48 +00001810 return NULL;
Guido van Rossumfc58e581992-01-27 16:45:55 +00001811 ok = wsetselection(self->w_win, sel, text, size);
Barry Warsawc2feec31996-12-12 22:48:20 +00001812 return PyInt_FromLong(ok);
Guido van Rossum5b10f451990-10-30 16:01:48 +00001813}
1814
Barry Warsawc2feec31996-12-12 22:48:20 +00001815static PyObject *
Guido van Rossum5b10f451990-10-30 16:01:48 +00001816window_setwincursor(self, args)
1817 windowobject *self;
Barry Warsawc2feec31996-12-12 22:48:20 +00001818 PyObject *args;
Guido van Rossum5b10f451990-10-30 16:01:48 +00001819{
Guido van Rossumfc58e581992-01-27 16:45:55 +00001820 char *name;
Guido van Rossum5b10f451990-10-30 16:01:48 +00001821 CURSOR *c;
Barry Warsawc2feec31996-12-12 22:48:20 +00001822 if (!PyArg_Parse(args, "z", &name))
Guido van Rossum5b10f451990-10-30 16:01:48 +00001823 return NULL;
Guido van Rossum3c8ba7a1992-02-05 11:15:00 +00001824 if (name == NULL)
1825 c = NULL;
1826 else {
1827 c = wfetchcursor(name);
1828 if (c == NULL) {
Barry Warsawc2feec31996-12-12 22:48:20 +00001829 PyErr_SetString(StdwinError, "no such cursor");
Guido van Rossum3c8ba7a1992-02-05 11:15:00 +00001830 return NULL;
1831 }
Guido van Rossum5b10f451990-10-30 16:01:48 +00001832 }
1833 wsetwincursor(self->w_win, c);
Barry Warsawc2feec31996-12-12 22:48:20 +00001834 Py_INCREF(Py_None);
1835 return Py_None;
Guido van Rossum5b10f451990-10-30 16:01:48 +00001836}
1837
Barry Warsawc2feec31996-12-12 22:48:20 +00001838static PyObject *
Guido van Rossumfc58e581992-01-27 16:45:55 +00001839window_setactive(self, args)
1840 windowobject *self;
Barry Warsawc2feec31996-12-12 22:48:20 +00001841 PyObject *args;
Guido van Rossumfc58e581992-01-27 16:45:55 +00001842{
Barry Warsawc2feec31996-12-12 22:48:20 +00001843 if (!PyArg_NoArgs(args))
Guido van Rossumfc58e581992-01-27 16:45:55 +00001844 return NULL;
1845 wsetactive(self->w_win);
Barry Warsawc2feec31996-12-12 22:48:20 +00001846 Py_INCREF(Py_None);
1847 return Py_None;
Guido van Rossumfc58e581992-01-27 16:45:55 +00001848}
1849
Guido van Rossum8dcbbac1991-07-27 21:42:24 +00001850#ifdef CWI_HACKS
Barry Warsawc2feec31996-12-12 22:48:20 +00001851static PyObject *
Guido van Rossum8dcbbac1991-07-27 21:42:24 +00001852window_getxwindowid(self, args)
1853 windowobject *self;
Barry Warsawc2feec31996-12-12 22:48:20 +00001854 PyObject *args;
Guido van Rossum8dcbbac1991-07-27 21:42:24 +00001855{
1856 long wid = wgetxwindowid(self->w_win);
Barry Warsawc2feec31996-12-12 22:48:20 +00001857 return PyInt_FromLong(wid);
Guido van Rossum8dcbbac1991-07-27 21:42:24 +00001858}
1859#endif
1860
Barry Warsawc2feec31996-12-12 22:48:20 +00001861static PyMethodDef window_methods[] = {
1862 {"begindrawing",(PyCFunction)window_begindrawing},
1863 {"change", (PyCFunction)window_change},
1864 {"close", (PyCFunction)window_close},
1865 {"getdocsize", (PyCFunction)window_getdocsize},
1866 {"getorigin", (PyCFunction)window_getorigin},
1867 {"gettitle", (PyCFunction)window_gettitle},
1868 {"getwinpos", (PyCFunction)window_getwinpos},
1869 {"getwinsize", (PyCFunction)window_getwinsize},
1870 {"menucreate", (PyCFunction)window_menucreate},
1871 {"scroll", (PyCFunction)window_scroll},
1872 {"setactive", (PyCFunction)window_setactive},
1873 {"setdocsize", (PyCFunction)window_setdocsize},
1874 {"setorigin", (PyCFunction)window_setorigin},
1875 {"setselection",(PyCFunction)window_setselection},
1876 {"settimer", (PyCFunction)window_settimer},
1877 {"settitle", (PyCFunction)window_settitle},
1878 {"setwincursor",(PyCFunction)window_setwincursor},
1879 {"setwinpos", (PyCFunction)window_setwinpos},
1880 {"setwinsize", (PyCFunction)window_setwinsize},
1881 {"show", (PyCFunction)window_show},
1882 {"textcreate", (PyCFunction)window_textcreate},
Guido van Rossum8dcbbac1991-07-27 21:42:24 +00001883#ifdef CWI_HACKS
Barry Warsawc2feec31996-12-12 22:48:20 +00001884 {"getxwindowid",(PyCFunction)window_getxwindowid},
Guido van Rossum8dcbbac1991-07-27 21:42:24 +00001885#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001886 {NULL, NULL} /* sentinel */
1887};
1888
Barry Warsawc2feec31996-12-12 22:48:20 +00001889static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001890window_getattr(wp, name)
1891 windowobject *wp;
1892 char *name;
1893{
Barry Warsawc2feec31996-12-12 22:48:20 +00001894 PyObject *v = NULL;
Guido van Rossum77b46041992-01-14 18:41:24 +00001895 if (wp->w_win == NULL) {
Barry Warsawc2feec31996-12-12 22:48:20 +00001896 PyErr_SetString(StdwinError, "window already closed");
Guido van Rossum77b46041992-01-14 18:41:24 +00001897 return NULL;
1898 }
Guido van Rossum85f50761991-10-20 20:22:50 +00001899 if (strcmp(name, "__dict__") == 0) {
1900 v = wp->w_attr;
1901 if (v == NULL)
Barry Warsawc2feec31996-12-12 22:48:20 +00001902 v = Py_None;
Guido van Rossum85f50761991-10-20 20:22:50 +00001903 }
1904 else if (wp->w_attr != NULL) {
Barry Warsawc2feec31996-12-12 22:48:20 +00001905 v = PyDict_GetItemString(wp->w_attr, name);
Guido van Rossum85f50761991-10-20 20:22:50 +00001906 }
1907 if (v != NULL) {
Barry Warsawc2feec31996-12-12 22:48:20 +00001908 Py_INCREF(v);
Guido van Rossum85f50761991-10-20 20:22:50 +00001909 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001910 }
Barry Warsawc2feec31996-12-12 22:48:20 +00001911 return Py_FindMethod(window_methods, (PyObject *)wp, name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001912}
1913
1914static int
1915window_setattr(wp, name, v)
1916 windowobject *wp;
1917 char *name;
Barry Warsawc2feec31996-12-12 22:48:20 +00001918 PyObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001919{
1920 if (wp->w_attr == NULL) {
Barry Warsawc2feec31996-12-12 22:48:20 +00001921 wp->w_attr = PyDict_New();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001922 if (wp->w_attr == NULL)
1923 return -1;
1924 }
Guido van Rossum94472a01992-09-04 09:45:18 +00001925 if (v == NULL) {
Barry Warsawc2feec31996-12-12 22:48:20 +00001926 int rv = PyDict_DelItemString(wp->w_attr, name);
Guido van Rossum94472a01992-09-04 09:45:18 +00001927 if (rv < 0)
Barry Warsawc2feec31996-12-12 22:48:20 +00001928 PyErr_SetString(PyExc_AttributeError,
1929 "delete non-existing menu object attribute");
Guido van Rossum94472a01992-09-04 09:45:18 +00001930 return rv;
1931 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001932 else
Barry Warsawc2feec31996-12-12 22:48:20 +00001933 return PyDict_SetItemString(wp->w_attr, name, v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001934}
1935
Barry Warsawc2feec31996-12-12 22:48:20 +00001936statichere PyTypeObject Windowtype = {
1937 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001938 0, /*ob_size*/
1939 "window", /*tp_name*/
1940 sizeof(windowobject), /*tp_size*/
1941 0, /*tp_itemsize*/
1942 /* methods */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001943 (destructor)window_dealloc, /*tp_dealloc*/
Guido van Rossum7066dd71992-09-17 17:54:56 +00001944 0, /*tp_print*/
Guido van Rossumb6775db1994-08-01 11:34:53 +00001945 (getattrfunc)window_getattr, /*tp_getattr*/
1946 (setattrfunc)window_setattr, /*tp_setattr*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001947 0, /*tp_compare*/
1948 0, /*tp_repr*/
1949};
1950
1951/* Stdwin methods */
1952
Barry Warsawc2feec31996-12-12 22:48:20 +00001953static PyObject *
Guido van Rossumcacd9571993-10-18 11:44:47 +00001954stdwin_done(sw, args)
Barry Warsawc2feec31996-12-12 22:48:20 +00001955 PyObject *sw;
1956 PyObject *args;
Guido van Rossumcacd9571993-10-18 11:44:47 +00001957{
Barry Warsawc2feec31996-12-12 22:48:20 +00001958 if (!PyArg_NoArgs(args))
Guido van Rossumcacd9571993-10-18 11:44:47 +00001959 return NULL;
1960 wdone();
1961 /* XXX There is no protection against continued use of
1962 XXX stdwin functions or objects after this call is made.
1963 XXX Use at own risk */
Barry Warsawc2feec31996-12-12 22:48:20 +00001964 Py_INCREF(Py_None);
1965 return Py_None;
Guido van Rossumcacd9571993-10-18 11:44:47 +00001966}
1967
Barry Warsawc2feec31996-12-12 22:48:20 +00001968static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001969stdwin_open(sw, args)
Barry Warsawc2feec31996-12-12 22:48:20 +00001970 PyObject *sw;
1971 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001972{
1973 int tag;
Barry Warsawc2feec31996-12-12 22:48:20 +00001974 PyObject *title;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001975 windowobject *wp;
Barry Warsawc2feec31996-12-12 22:48:20 +00001976 if (!PyArg_Parse(args, "S", &title))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001977 return NULL;
1978 for (tag = 0; tag < MAXNWIN; tag++) {
1979 if (windowlist[tag] == NULL)
1980 break;
1981 }
Guido van Rossum27201061991-04-16 08:43:03 +00001982 if (tag >= MAXNWIN) {
Barry Warsawc2feec31996-12-12 22:48:20 +00001983 PyErr_SetString(StdwinError, "creating too many windows");
Guido van Rossum27201061991-04-16 08:43:03 +00001984 return NULL;
1985 }
Barry Warsawc2feec31996-12-12 22:48:20 +00001986 wp = PyObject_NEW(windowobject, &Windowtype);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001987 if (wp == NULL)
1988 return NULL;
Barry Warsawc2feec31996-12-12 22:48:20 +00001989 Py_INCREF(title);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001990 wp->w_title = title;
Barry Warsawc2feec31996-12-12 22:48:20 +00001991 wp->w_win = wopen(PyString_AsString(title), (void (*)()) NULL);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001992 wp->w_attr = NULL;
1993 if (wp->w_win == NULL) {
Barry Warsawc2feec31996-12-12 22:48:20 +00001994 Py_DECREF(wp);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001995 return NULL;
1996 }
1997 windowlist[tag] = wp;
1998 wsettag(wp->w_win, tag);
Barry Warsawc2feec31996-12-12 22:48:20 +00001999 return (PyObject *)wp;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002000}
2001
Barry Warsawc2feec31996-12-12 22:48:20 +00002002static PyObject *
Guido van Rossum246b9d81991-06-03 10:55:14 +00002003window2object(win)
2004 WINDOW *win;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002005{
Barry Warsawc2feec31996-12-12 22:48:20 +00002006 PyObject *w;
Guido van Rossum246b9d81991-06-03 10:55:14 +00002007 if (win == NULL)
Barry Warsawc2feec31996-12-12 22:48:20 +00002008 w = Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002009 else {
Guido van Rossum246b9d81991-06-03 10:55:14 +00002010 int tag = wgettag(win);
2011 if (tag < 0 || tag >= MAXNWIN || windowlist[tag] == NULL ||
2012 windowlist[tag]->w_win != win)
Barry Warsawc2feec31996-12-12 22:48:20 +00002013 w = Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002014 else
Barry Warsawc2feec31996-12-12 22:48:20 +00002015 w = (PyObject *)windowlist[tag];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002016 }
Barry Warsawc2feec31996-12-12 22:48:20 +00002017 Py_INCREF(w);
Guido van Rossum246b9d81991-06-03 10:55:14 +00002018 return w;
2019}
2020
Barry Warsawc2feec31996-12-12 22:48:20 +00002021static PyObject *
Guido van Rossum246b9d81991-06-03 10:55:14 +00002022stdwin_get_poll_event(poll, args)
2023 int poll;
Barry Warsawc2feec31996-12-12 22:48:20 +00002024 PyObject *args;
Guido van Rossum246b9d81991-06-03 10:55:14 +00002025{
2026 EVENT e;
Barry Warsawc2feec31996-12-12 22:48:20 +00002027 PyObject *u, *v, *w;
2028 if (!PyArg_NoArgs(args))
Guido van Rossum246b9d81991-06-03 10:55:14 +00002029 return NULL;
2030 if (Drawing != NULL) {
Barry Warsawc2feec31996-12-12 22:48:20 +00002031 PyErr_SetString(StdwinError,
2032 "cannot getevent() while drawing");
Guido van Rossum246b9d81991-06-03 10:55:14 +00002033 return NULL;
2034 }
2035 again:
Guido van Rossumff4949e1992-08-05 19:58:53 +00002036 BGN_STDWIN
Guido van Rossum246b9d81991-06-03 10:55:14 +00002037 if (poll) {
2038 if (!wpollevent(&e)) {
Guido van Rossumff4949e1992-08-05 19:58:53 +00002039 RET_STDWIN
Barry Warsawc2feec31996-12-12 22:48:20 +00002040 Py_INCREF(Py_None);
2041 return Py_None;
Guido van Rossum246b9d81991-06-03 10:55:14 +00002042 }
2043 }
2044 else
2045 wgetevent(&e);
Guido van Rossumff4949e1992-08-05 19:58:53 +00002046 END_STDWIN
Guido van Rossum246b9d81991-06-03 10:55:14 +00002047 if (e.type == WE_COMMAND && e.u.command == WC_CANCEL) {
2048 /* Turn keyboard interrupts into exceptions */
Barry Warsawc2feec31996-12-12 22:48:20 +00002049 PyErr_SetNone(PyExc_KeyboardInterrupt);
Guido van Rossum246b9d81991-06-03 10:55:14 +00002050 return NULL;
2051 }
2052 if (e.type == WE_COMMAND && e.u.command == WC_CLOSE) {
2053 /* Turn WC_CLOSE commands into WE_CLOSE events */
2054 e.type = WE_CLOSE;
2055 }
Guido van Rossum2ee12f41992-04-13 15:54:35 +00002056 v = window2object(e.window);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002057 switch (e.type) {
2058 case WE_CHAR:
2059 {
2060 char c[1];
2061 c[0] = e.u.character;
Barry Warsawc2feec31996-12-12 22:48:20 +00002062 w = PyString_FromStringAndSize(c, 1);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002063 }
2064 break;
2065 case WE_COMMAND:
Barry Warsawc2feec31996-12-12 22:48:20 +00002066 w = PyInt_FromLong((long)e.u.command);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002067 break;
2068 case WE_DRAW:
2069 w = makerect(e.u.area.left, e.u.area.top,
2070 e.u.area.right, e.u.area.bottom);
2071 break;
2072 case WE_MOUSE_DOWN:
2073 case WE_MOUSE_MOVE:
2074 case WE_MOUSE_UP:
Barry Warsawc2feec31996-12-12 22:48:20 +00002075 w = Py_BuildValue("((ii)iii)",
2076 e.u.where.h, e.u.where.v,
2077 e.u.where.clicks,
2078 e.u.where.button,
2079 e.u.where.mask);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002080 break;
2081 case WE_MENU:
Barry Warsawc2feec31996-12-12 22:48:20 +00002082 if (e.u.m.id >= IDOFFSET &&
2083 e.u.m.id < IDOFFSET+MAXNMENU &&
2084 menulist[e.u.m.id - IDOFFSET] != NULL)
2085 {
2086 w = Py_BuildValue("(Oi)",
2087 menulist[e.u.m.id - IDOFFSET],
2088 e.u.m.item);
2089 }
Guido van Rossum246b9d81991-06-03 10:55:14 +00002090 else {
2091 /* Ghost menu event.
2092 Can occur only on the Mac if another part
2093 of the aplication has installed a menu;
2094 like the THINK C console library. */
Barry Warsawc2feec31996-12-12 22:48:20 +00002095 Py_DECREF(v);
Guido van Rossum246b9d81991-06-03 10:55:14 +00002096 goto again;
2097 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002098 break;
Guido van Rossum3ee199e1992-06-30 12:48:26 +00002099 case WE_KEY:
Barry Warsawc2feec31996-12-12 22:48:20 +00002100 w = Py_BuildValue("(ii)", e.u.key.code, e.u.key.mask);
Guido van Rossum3ee199e1992-06-30 12:48:26 +00002101 break;
Guido van Rossum5b10f451990-10-30 16:01:48 +00002102 case WE_LOST_SEL:
Barry Warsawc2feec31996-12-12 22:48:20 +00002103 w = PyInt_FromLong((long)e.u.sel);
Guido van Rossum5b10f451990-10-30 16:01:48 +00002104 break;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002105 default:
Barry Warsawc2feec31996-12-12 22:48:20 +00002106 w = Py_None;
2107 Py_INCREF(w);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002108 break;
2109 }
2110 if (w == NULL) {
Barry Warsawc2feec31996-12-12 22:48:20 +00002111 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002112 return NULL;
2113 }
Barry Warsawc2feec31996-12-12 22:48:20 +00002114 u = Py_BuildValue("(iOO)", e.type, v, w);
2115 Py_XDECREF(v);
2116 Py_XDECREF(w);
Guido van Rossum2ee12f41992-04-13 15:54:35 +00002117 return u;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002118}
2119
Barry Warsawc2feec31996-12-12 22:48:20 +00002120static PyObject *
Guido van Rossume8e7cf41991-01-16 14:06:18 +00002121stdwin_getevent(sw, args)
Barry Warsawc2feec31996-12-12 22:48:20 +00002122 PyObject *sw;
2123 PyObject *args;
Guido van Rossume8e7cf41991-01-16 14:06:18 +00002124{
2125 return stdwin_get_poll_event(0, args);
2126}
2127
Barry Warsawc2feec31996-12-12 22:48:20 +00002128static PyObject *
Guido van Rossume8e7cf41991-01-16 14:06:18 +00002129stdwin_pollevent(sw, args)
Barry Warsawc2feec31996-12-12 22:48:20 +00002130 PyObject *sw;
2131 PyObject *args;
Guido van Rossume8e7cf41991-01-16 14:06:18 +00002132{
2133 return stdwin_get_poll_event(1, args);
2134}
2135
Barry Warsawc2feec31996-12-12 22:48:20 +00002136static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002137stdwin_setdefwinpos(sw, args)
Barry Warsawc2feec31996-12-12 22:48:20 +00002138 PyObject *sw;
2139 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002140{
2141 int a[2];
2142 if (!getpointarg(args, a))
2143 return NULL;
2144 wsetdefwinpos(a[0], a[1]);
Barry Warsawc2feec31996-12-12 22:48:20 +00002145 Py_INCREF(Py_None);
2146 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002147}
2148
Barry Warsawc2feec31996-12-12 22:48:20 +00002149static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002150stdwin_setdefwinsize(sw, args)
Barry Warsawc2feec31996-12-12 22:48:20 +00002151 PyObject *sw;
2152 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002153{
2154 int a[2];
2155 if (!getpointarg(args, a))
2156 return NULL;
2157 wsetdefwinsize(a[0], a[1]);
Barry Warsawc2feec31996-12-12 22:48:20 +00002158 Py_INCREF(Py_None);
2159 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002160}
2161
Barry Warsawc2feec31996-12-12 22:48:20 +00002162static PyObject *
Guido van Rossum0c2290b1991-04-03 19:12:14 +00002163stdwin_setdefscrollbars(sw, args)
Barry Warsawc2feec31996-12-12 22:48:20 +00002164 PyObject *sw;
2165 PyObject *args;
Guido van Rossum0c2290b1991-04-03 19:12:14 +00002166{
2167 int a[2];
2168 if (!getpointarg(args, a))
2169 return NULL;
2170 wsetdefscrollbars(a[0], a[1]);
Barry Warsawc2feec31996-12-12 22:48:20 +00002171 Py_INCREF(Py_None);
2172 return Py_None;
Guido van Rossum0c2290b1991-04-03 19:12:14 +00002173}
2174
Barry Warsawc2feec31996-12-12 22:48:20 +00002175static PyObject *
Guido van Rossum541c8c01991-05-05 20:13:41 +00002176stdwin_getdefwinpos(self, args)
Barry Warsawc2feec31996-12-12 22:48:20 +00002177 PyObject *self;
2178 PyObject *args;
Guido van Rossum33f17701991-02-13 23:19:39 +00002179{
2180 int h, v;
Barry Warsawc2feec31996-12-12 22:48:20 +00002181 if (!PyArg_NoArgs(args))
Guido van Rossum33f17701991-02-13 23:19:39 +00002182 return NULL;
2183 wgetdefwinpos(&h, &v);
2184 return makepoint(h, v);
2185}
2186
Barry Warsawc2feec31996-12-12 22:48:20 +00002187static PyObject *
Guido van Rossum541c8c01991-05-05 20:13:41 +00002188stdwin_getdefwinsize(self, args)
Barry Warsawc2feec31996-12-12 22:48:20 +00002189 PyObject *self;
2190 PyObject *args;
Guido van Rossum33f17701991-02-13 23:19:39 +00002191{
2192 int width, height;
Barry Warsawc2feec31996-12-12 22:48:20 +00002193 if (!PyArg_NoArgs(args))
Guido van Rossum33f17701991-02-13 23:19:39 +00002194 return NULL;
2195 wgetdefwinsize(&width, &height);
2196 return makepoint(width, height);
2197}
2198
Barry Warsawc2feec31996-12-12 22:48:20 +00002199static PyObject *
Guido van Rossum541c8c01991-05-05 20:13:41 +00002200stdwin_getdefscrollbars(self, args)
Barry Warsawc2feec31996-12-12 22:48:20 +00002201 PyObject *self;
2202 PyObject *args;
Guido van Rossum0c2290b1991-04-03 19:12:14 +00002203{
2204 int h, v;
Barry Warsawc2feec31996-12-12 22:48:20 +00002205 if (!PyArg_NoArgs(args))
Guido van Rossum0c2290b1991-04-03 19:12:14 +00002206 return NULL;
2207 wgetdefscrollbars(&h, &v);
2208 return makepoint(h, v);
2209}
2210
Barry Warsawc2feec31996-12-12 22:48:20 +00002211static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002212stdwin_menucreate(self, args)
Barry Warsawc2feec31996-12-12 22:48:20 +00002213 PyObject *self;
2214 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002215{
Guido van Rossumfc58e581992-01-27 16:45:55 +00002216 char *title;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002217 if (!getstrarg(args, &title))
2218 return NULL;
2219 wmenusetdeflocal(0);
Barry Warsawc2feec31996-12-12 22:48:20 +00002220 return (PyObject *)newmenuobject(title);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002221}
2222
Barry Warsawc2feec31996-12-12 22:48:20 +00002223static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002224stdwin_askfile(self, args)
Barry Warsawc2feec31996-12-12 22:48:20 +00002225 PyObject *self;
2226 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002227{
Guido van Rossumfc58e581992-01-27 16:45:55 +00002228 char *prompt, *dflt;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002229 int new, ret;
2230 char buf[256];
Barry Warsawc2feec31996-12-12 22:48:20 +00002231 if (!PyArg_Parse(args, "(ssi)", &prompt, &dflt, &new))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002232 return NULL;
Guido van Rossumfc58e581992-01-27 16:45:55 +00002233 strncpy(buf, dflt, sizeof buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002234 buf[sizeof buf - 1] = '\0';
Guido van Rossumff4949e1992-08-05 19:58:53 +00002235 BGN_STDWIN
Guido van Rossumfc58e581992-01-27 16:45:55 +00002236 ret = waskfile(prompt, buf, sizeof buf, new);
Guido van Rossumff4949e1992-08-05 19:58:53 +00002237 END_STDWIN
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002238 if (!ret) {
Barry Warsawc2feec31996-12-12 22:48:20 +00002239 PyErr_SetNone(PyExc_KeyboardInterrupt);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002240 return NULL;
2241 }
Barry Warsawc2feec31996-12-12 22:48:20 +00002242 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002243}
2244
Barry Warsawc2feec31996-12-12 22:48:20 +00002245static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002246stdwin_askync(self, args)
Barry Warsawc2feec31996-12-12 22:48:20 +00002247 PyObject *self;
2248 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002249{
Guido van Rossumfc58e581992-01-27 16:45:55 +00002250 char *prompt;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002251 int new, ret;
Barry Warsawc2feec31996-12-12 22:48:20 +00002252 if (!PyArg_Parse(args, "(si)", &prompt, &new))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002253 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002254 BGN_STDWIN
Guido van Rossumfc58e581992-01-27 16:45:55 +00002255 ret = waskync(prompt, new);
Guido van Rossumff4949e1992-08-05 19:58:53 +00002256 END_STDWIN
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002257 if (ret < 0) {
Barry Warsawc2feec31996-12-12 22:48:20 +00002258 PyErr_SetNone(PyExc_KeyboardInterrupt);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002259 return NULL;
2260 }
Barry Warsawc2feec31996-12-12 22:48:20 +00002261 return PyInt_FromLong((long)ret);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002262}
2263
Barry Warsawc2feec31996-12-12 22:48:20 +00002264static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002265stdwin_askstr(self, args)
Barry Warsawc2feec31996-12-12 22:48:20 +00002266 PyObject *self;
2267 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002268{
Guido van Rossumfc58e581992-01-27 16:45:55 +00002269 char *prompt, *dflt;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002270 int ret;
2271 char buf[256];
Barry Warsawc2feec31996-12-12 22:48:20 +00002272 if (!PyArg_Parse(args, "(ss)", &prompt, &dflt))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002273 return NULL;
Guido van Rossumfc58e581992-01-27 16:45:55 +00002274 strncpy(buf, dflt, sizeof buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002275 buf[sizeof buf - 1] = '\0';
Guido van Rossumff4949e1992-08-05 19:58:53 +00002276 BGN_STDWIN
Guido van Rossumfc58e581992-01-27 16:45:55 +00002277 ret = waskstr(prompt, buf, sizeof buf);
Guido van Rossumff4949e1992-08-05 19:58:53 +00002278 END_STDWIN
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002279 if (!ret) {
Barry Warsawc2feec31996-12-12 22:48:20 +00002280 PyErr_SetNone(PyExc_KeyboardInterrupt);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002281 return NULL;
2282 }
Barry Warsawc2feec31996-12-12 22:48:20 +00002283 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002284}
2285
Barry Warsawc2feec31996-12-12 22:48:20 +00002286static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002287stdwin_message(self, args)
Barry Warsawc2feec31996-12-12 22:48:20 +00002288 PyObject *self;
2289 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002290{
Guido van Rossumfc58e581992-01-27 16:45:55 +00002291 char *msg;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002292 if (!getstrarg(args, &msg))
2293 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002294 BGN_STDWIN
Guido van Rossumfc58e581992-01-27 16:45:55 +00002295 wmessage(msg);
Guido van Rossumff4949e1992-08-05 19:58:53 +00002296 END_STDWIN
Barry Warsawc2feec31996-12-12 22:48:20 +00002297 Py_INCREF(Py_None);
2298 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002299}
2300
Barry Warsawc2feec31996-12-12 22:48:20 +00002301static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002302stdwin_fleep(self, args)
Barry Warsawc2feec31996-12-12 22:48:20 +00002303 PyObject *self;
2304 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002305{
Barry Warsawc2feec31996-12-12 22:48:20 +00002306 if (!PyArg_NoArgs(args))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002307 return NULL;
2308 wfleep();
Barry Warsawc2feec31996-12-12 22:48:20 +00002309 Py_INCREF(Py_None);
2310 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002311}
2312
Barry Warsawc2feec31996-12-12 22:48:20 +00002313static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002314stdwin_setcutbuffer(self, args)
Barry Warsawc2feec31996-12-12 22:48:20 +00002315 PyObject *self;
2316 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002317{
Guido van Rossumfc58e581992-01-27 16:45:55 +00002318 int i, size;
2319 char *str;
Barry Warsawc2feec31996-12-12 22:48:20 +00002320 if (!PyArg_Parse(args, "(is#)", &i, &str, &size))
Guido van Rossum124967c1990-11-06 15:17:35 +00002321 return NULL;
Guido van Rossumfc58e581992-01-27 16:45:55 +00002322 wsetcutbuffer(i, str, size);
Barry Warsawc2feec31996-12-12 22:48:20 +00002323 Py_INCREF(Py_None);
2324 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002325}
2326
Barry Warsawc2feec31996-12-12 22:48:20 +00002327static PyObject *
Guido van Rossum246b9d81991-06-03 10:55:14 +00002328stdwin_getactive(self, args)
Barry Warsawc2feec31996-12-12 22:48:20 +00002329 PyObject *self;
2330 PyObject *args;
Guido van Rossum246b9d81991-06-03 10:55:14 +00002331{
2332 return window2object(wgetactive());
2333}
2334
Barry Warsawc2feec31996-12-12 22:48:20 +00002335static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002336stdwin_getcutbuffer(self, args)
Barry Warsawc2feec31996-12-12 22:48:20 +00002337 PyObject *self;
2338 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002339{
Guido van Rossum5b10f451990-10-30 16:01:48 +00002340 int i;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002341 char *str;
Guido van Rossum01769f01990-10-30 13:39:00 +00002342 int len;
Guido van Rossum124967c1990-11-06 15:17:35 +00002343 if (!getintarg(args, &i))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002344 return NULL;
Guido van Rossum5b10f451990-10-30 16:01:48 +00002345 str = wgetcutbuffer(i, &len);
Guido van Rossum01769f01990-10-30 13:39:00 +00002346 if (str == NULL) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002347 str = "";
Guido van Rossum01769f01990-10-30 13:39:00 +00002348 len = 0;
2349 }
Barry Warsawc2feec31996-12-12 22:48:20 +00002350 return PyString_FromStringAndSize(str, len);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002351}
2352
Barry Warsawc2feec31996-12-12 22:48:20 +00002353static PyObject *
Guido van Rossum5b10f451990-10-30 16:01:48 +00002354stdwin_rotatecutbuffers(self, args)
Barry Warsawc2feec31996-12-12 22:48:20 +00002355 PyObject *self;
2356 PyObject *args;
Guido van Rossum5b10f451990-10-30 16:01:48 +00002357{
2358 int i;
2359 if (!getintarg(args, &i))
2360 return NULL;
2361 wrotatecutbuffers(i);
Barry Warsawc2feec31996-12-12 22:48:20 +00002362 Py_INCREF(Py_None);
2363 return Py_None;
Guido van Rossum5b10f451990-10-30 16:01:48 +00002364}
2365
Barry Warsawc2feec31996-12-12 22:48:20 +00002366static PyObject *
Guido van Rossum5b10f451990-10-30 16:01:48 +00002367stdwin_getselection(self, args)
Barry Warsawc2feec31996-12-12 22:48:20 +00002368 PyObject *self;
2369 PyObject *args;
Guido van Rossum5b10f451990-10-30 16:01:48 +00002370{
2371 int sel;
2372 char *data;
2373 int len;
2374 if (!getintarg(args, &sel))
2375 return NULL;
2376 data = wgetselection(sel, &len);
2377 if (data == NULL) {
2378 data = "";
2379 len = 0;
2380 }
Barry Warsawc2feec31996-12-12 22:48:20 +00002381 return PyString_FromStringAndSize(data, len);
Guido van Rossum5b10f451990-10-30 16:01:48 +00002382}
2383
Barry Warsawc2feec31996-12-12 22:48:20 +00002384static PyObject *
Guido van Rossum5b10f451990-10-30 16:01:48 +00002385stdwin_resetselection(self, args)
Barry Warsawc2feec31996-12-12 22:48:20 +00002386 PyObject *self;
2387 PyObject *args;
Guido van Rossum5b10f451990-10-30 16:01:48 +00002388{
2389 int sel;
2390 if (!getintarg(args, &sel))
2391 return NULL;
2392 wresetselection(sel);
Barry Warsawc2feec31996-12-12 22:48:20 +00002393 Py_INCREF(Py_None);
2394 return Py_None;
Guido van Rossum5b10f451990-10-30 16:01:48 +00002395}
2396
Barry Warsawc2feec31996-12-12 22:48:20 +00002397static PyObject *
Guido van Rossum0c2290b1991-04-03 19:12:14 +00002398stdwin_fetchcolor(self, args)
Barry Warsawc2feec31996-12-12 22:48:20 +00002399 PyObject *self;
2400 PyObject *args;
Guido van Rossum0c2290b1991-04-03 19:12:14 +00002401{
Guido van Rossumfc58e581992-01-27 16:45:55 +00002402 char *colorname;
Guido van Rossum34679b71993-01-26 13:33:44 +00002403 COLOR color;
Guido van Rossum0c2290b1991-04-03 19:12:14 +00002404 if (!getstrarg(args, &colorname))
2405 return NULL;
Guido van Rossum34679b71993-01-26 13:33:44 +00002406 color = wfetchcolor(colorname);
2407#ifdef BADCOLOR
2408 if (color == BADCOLOR) {
Barry Warsawc2feec31996-12-12 22:48:20 +00002409 PyErr_SetString(StdwinError, "color name not found");
Guido van Rossum34679b71993-01-26 13:33:44 +00002410 return NULL;
2411 }
2412#endif
Barry Warsawc2feec31996-12-12 22:48:20 +00002413 return PyInt_FromLong((long)color);
Guido van Rossum0c2290b1991-04-03 19:12:14 +00002414}
2415
Barry Warsawc2feec31996-12-12 22:48:20 +00002416static PyObject *
Guido van Rossum541c8c01991-05-05 20:13:41 +00002417stdwin_getscrsize(self, args)
Barry Warsawc2feec31996-12-12 22:48:20 +00002418 PyObject *self;
2419 PyObject *args;
Guido van Rossum541c8c01991-05-05 20:13:41 +00002420{
2421 int width, height;
Barry Warsawc2feec31996-12-12 22:48:20 +00002422 if (!PyArg_NoArgs(args))
Guido van Rossum541c8c01991-05-05 20:13:41 +00002423 return NULL;
2424 wgetscrsize(&width, &height);
2425 return makepoint(width, height);
2426}
2427
Barry Warsawc2feec31996-12-12 22:48:20 +00002428static PyObject *
Guido van Rossum541c8c01991-05-05 20:13:41 +00002429stdwin_getscrmm(self, args)
Barry Warsawc2feec31996-12-12 22:48:20 +00002430 PyObject *self;
2431 PyObject *args;
Guido van Rossum541c8c01991-05-05 20:13:41 +00002432{
2433 int width, height;
Barry Warsawc2feec31996-12-12 22:48:20 +00002434 if (!PyArg_NoArgs(args))
Guido van Rossum541c8c01991-05-05 20:13:41 +00002435 return NULL;
2436 wgetscrmm(&width, &height);
2437 return makepoint(width, height);
2438}
2439
Guido van Rossumed233a51992-06-23 09:07:03 +00002440#ifdef unix
Barry Warsawc2feec31996-12-12 22:48:20 +00002441static PyObject *
Guido van Rossumed233a51992-06-23 09:07:03 +00002442stdwin_connectionnumber(self, args)
Barry Warsawc2feec31996-12-12 22:48:20 +00002443 PyObject *self;
2444 PyObject *args;
Guido van Rossumed233a51992-06-23 09:07:03 +00002445{
Barry Warsawc2feec31996-12-12 22:48:20 +00002446 if (!PyArg_NoArgs(args))
Guido van Rossumed233a51992-06-23 09:07:03 +00002447 return NULL;
Barry Warsawc2feec31996-12-12 22:48:20 +00002448 return PyInt_FromLong((long) wconnectionnumber());
Guido van Rossumed233a51992-06-23 09:07:03 +00002449}
2450#endif
2451
Barry Warsawc2feec31996-12-12 22:48:20 +00002452static PyObject *
Guido van Rossumbf80e541993-02-08 15:49:17 +00002453stdwin_listfontnames(self, args)
Barry Warsawc2feec31996-12-12 22:48:20 +00002454 PyObject *self;
2455 PyObject *args;
Guido van Rossumbf80e541993-02-08 15:49:17 +00002456{
2457 char *pattern;
2458 char **fontnames;
2459 int count;
Barry Warsawc2feec31996-12-12 22:48:20 +00002460 PyObject *list;
2461 if (!PyArg_Parse(args, "z", &pattern))
Guido van Rossumbf80e541993-02-08 15:49:17 +00002462 return NULL;
2463 fontnames = wlistfontnames(pattern, &count);
Barry Warsawc2feec31996-12-12 22:48:20 +00002464 list = PyList_New(count);
Guido van Rossumbf80e541993-02-08 15:49:17 +00002465 if (list != NULL) {
2466 int i;
2467 for (i = 0; i < count; i++) {
Barry Warsawc2feec31996-12-12 22:48:20 +00002468 PyObject *v = PyString_FromString(fontnames[i]);
Guido van Rossumbf80e541993-02-08 15:49:17 +00002469 if (v == NULL) {
Barry Warsawc2feec31996-12-12 22:48:20 +00002470 Py_DECREF(list);
Guido van Rossumbf80e541993-02-08 15:49:17 +00002471 list = NULL;
2472 break;
2473 }
Barry Warsawc2feec31996-12-12 22:48:20 +00002474 PyList_SetItem(list, i, v);
Guido van Rossumbf80e541993-02-08 15:49:17 +00002475 }
2476 }
2477 return list;
2478}
2479
Guido van Rossume9066061993-07-29 13:14:32 +00002480#ifdef HAVE_BITMAPS
Barry Warsawc2feec31996-12-12 22:48:20 +00002481static PyObject *
Guido van Rossumbf80e541993-02-08 15:49:17 +00002482stdwin_newbitmap(self, args)
Barry Warsawc2feec31996-12-12 22:48:20 +00002483 PyObject *self;
2484 PyObject *args;
Guido van Rossumbf80e541993-02-08 15:49:17 +00002485{
2486 int width, height;
2487 bitmapobject *bp;
Barry Warsawc2feec31996-12-12 22:48:20 +00002488 if (!PyArg_Parse(args, "(ii)", &width, &height))
Guido van Rossumbf80e541993-02-08 15:49:17 +00002489 return NULL;
Barry Warsawc2feec31996-12-12 22:48:20 +00002490 return (PyObject *)newbitmapobject(width, height);
Guido van Rossumbf80e541993-02-08 15:49:17 +00002491}
Guido van Rossume9066061993-07-29 13:14:32 +00002492#endif
Guido van Rossumbf80e541993-02-08 15:49:17 +00002493
Barry Warsawc2feec31996-12-12 22:48:20 +00002494static PyMethodDef stdwin_methods[] = {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002495 {"askfile", stdwin_askfile},
2496 {"askstr", stdwin_askstr},
2497 {"askync", stdwin_askync},
Guido van Rossumcacd9571993-10-18 11:44:47 +00002498 {"done", stdwin_done},
Guido van Rossum27201061991-04-16 08:43:03 +00002499 {"fetchcolor", stdwin_fetchcolor},
Guido van Rossumed233a51992-06-23 09:07:03 +00002500#ifdef unix
2501 {"fileno", stdwin_connectionnumber},
2502 {"connectionnumber", stdwin_connectionnumber},
2503#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002504 {"fleep", stdwin_fleep},
Guido van Rossum246b9d81991-06-03 10:55:14 +00002505 {"getactive", stdwin_getactive},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002506 {"getcutbuffer", stdwin_getcutbuffer},
Guido van Rossum0c2290b1991-04-03 19:12:14 +00002507 {"getdefscrollbars", stdwin_getdefscrollbars},
Guido van Rossum33f17701991-02-13 23:19:39 +00002508 {"getdefwinpos", stdwin_getdefwinpos},
2509 {"getdefwinsize", stdwin_getdefwinsize},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002510 {"getevent", stdwin_getevent},
Guido van Rossum541c8c01991-05-05 20:13:41 +00002511 {"getscrmm", stdwin_getscrmm},
2512 {"getscrsize", stdwin_getscrsize},
Guido van Rossum27201061991-04-16 08:43:03 +00002513 {"getselection", stdwin_getselection},
Guido van Rossumbf80e541993-02-08 15:49:17 +00002514 {"listfontnames", stdwin_listfontnames},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002515 {"menucreate", stdwin_menucreate},
2516 {"message", stdwin_message},
Guido van Rossume9066061993-07-29 13:14:32 +00002517#ifdef HAVE_BITMAPS
Guido van Rossumbf80e541993-02-08 15:49:17 +00002518 {"newbitmap", stdwin_newbitmap},
Guido van Rossume9066061993-07-29 13:14:32 +00002519#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002520 {"open", stdwin_open},
Guido van Rossume8e7cf41991-01-16 14:06:18 +00002521 {"pollevent", stdwin_pollevent},
Guido van Rossum5b10f451990-10-30 16:01:48 +00002522 {"resetselection", stdwin_resetselection},
2523 {"rotatecutbuffers", stdwin_rotatecutbuffers},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002524 {"setcutbuffer", stdwin_setcutbuffer},
Guido van Rossum0c2290b1991-04-03 19:12:14 +00002525 {"setdefscrollbars", stdwin_setdefscrollbars},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002526 {"setdefwinpos", stdwin_setdefwinpos},
2527 {"setdefwinsize", stdwin_setdefwinsize},
2528
2529 /* Text measuring methods borrow code from drawing objects: */
Barry Warsawc2feec31996-12-12 22:48:20 +00002530 {"baseline", (PyCFunction)drawing_baseline},
2531 {"lineheight", (PyCFunction)drawing_lineheight},
2532 {"textbreak", (PyCFunction)drawing_textbreak},
2533 {"textwidth", (PyCFunction)drawing_textwidth},
Guido van Rossum0c2290b1991-04-03 19:12:14 +00002534
2535 /* Same for font setting methods: */
Barry Warsawc2feec31996-12-12 22:48:20 +00002536 {"setfont", (PyCFunction)drawing_setfont},
Guido van Rossum0c2290b1991-04-03 19:12:14 +00002537
2538 /* Same for color setting/getting methods: */
Barry Warsawc2feec31996-12-12 22:48:20 +00002539 {"getbgcolor", (PyCFunction)drawing_getbgcolor},
2540 {"getfgcolor", (PyCFunction)drawing_getfgcolor},
2541 {"setbgcolor", (PyCFunction)drawing_setbgcolor},
2542 {"setfgcolor", (PyCFunction)drawing_setfgcolor},
Guido van Rossum0c2290b1991-04-03 19:12:14 +00002543
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002544 {NULL, NULL} /* sentinel */
2545};
2546
Guido van Rossumb6775db1994-08-01 11:34:53 +00002547#ifndef macintosh
Guido van Rossumcacd9571993-10-18 11:44:47 +00002548static int
2549checkstringlist(args, ps, pn)
Barry Warsawc2feec31996-12-12 22:48:20 +00002550 PyObject *args;
Guido van Rossumcacd9571993-10-18 11:44:47 +00002551 char ***ps;
2552 int *pn;
2553{
2554 int i, n;
2555 char **s;
Barry Warsawc2feec31996-12-12 22:48:20 +00002556 if (!PyList_Check(args)) {
2557 PyErr_SetString(PyExc_TypeError, "list of strings expected");
Guido van Rossumcacd9571993-10-18 11:44:47 +00002558 return 0;
2559 }
Barry Warsawc2feec31996-12-12 22:48:20 +00002560 n = PyList_Size(args);
2561 s = PyMem_NEW(char *, n+1);
Guido van Rossumcacd9571993-10-18 11:44:47 +00002562 if (s == NULL) {
Barry Warsawc2feec31996-12-12 22:48:20 +00002563 PyErr_NoMemory();
Guido van Rossumcacd9571993-10-18 11:44:47 +00002564 return 0;
2565 }
2566 for (i = 0; i < n; i++) {
Barry Warsawc2feec31996-12-12 22:48:20 +00002567 PyObject *item = PyList_GetItem(args, i);
2568 if (!PyString_Check(item)) {
2569 PyErr_SetString(PyExc_TypeError,
2570 "list of strings expected");
Guido van Rossumcacd9571993-10-18 11:44:47 +00002571 return 0;
2572 }
Barry Warsawc2feec31996-12-12 22:48:20 +00002573 s[i] = PyString_AsString(item);
Guido van Rossumcacd9571993-10-18 11:44:47 +00002574 }
2575 s[n] = NULL; /* In case caller wants a NULL-terminated list */
2576 *ps = s;
2577 *pn = n;
2578 return 1;
2579}
2580
2581static int
2582putbackstringlist(list, s, n)
Barry Warsawc2feec31996-12-12 22:48:20 +00002583 PyObject *list;
Guido van Rossumcacd9571993-10-18 11:44:47 +00002584 char **s;
2585 int n;
2586{
Barry Warsawc2feec31996-12-12 22:48:20 +00002587 int oldsize = PyList_Size(list);
2588 PyObject *newlist;
Guido van Rossumcacd9571993-10-18 11:44:47 +00002589 int i;
2590 if (n == oldsize)
2591 return 1;
Barry Warsawc2feec31996-12-12 22:48:20 +00002592 newlist = PyList_New(n);
Guido van Rossumcacd9571993-10-18 11:44:47 +00002593 for (i = 0; i < n && newlist != NULL; i++) {
Barry Warsawc2feec31996-12-12 22:48:20 +00002594 PyObject *item = PyString_FromString(s[i]);
Guido van Rossumcacd9571993-10-18 11:44:47 +00002595 if (item == NULL) {
Barry Warsawc2feec31996-12-12 22:48:20 +00002596 Py_DECREF(newlist);
Guido van Rossumcacd9571993-10-18 11:44:47 +00002597 newlist = NULL;
2598 }
2599 else
Barry Warsawc2feec31996-12-12 22:48:20 +00002600 PyList_SetItem(newlist, i, item);
Guido van Rossumcacd9571993-10-18 11:44:47 +00002601 }
2602 if (newlist == NULL)
2603 return 0;
2604 (*list->ob_type->tp_as_sequence->sq_ass_slice)
2605 (list, 0, oldsize, newlist);
Barry Warsawc2feec31996-12-12 22:48:20 +00002606 Py_DECREF(newlist);
Guido van Rossumcacd9571993-10-18 11:44:47 +00002607 return 1;
2608}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002609#endif /* macintosh */
Guido van Rossumcacd9571993-10-18 11:44:47 +00002610
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002611void
2612initstdwin()
2613{
Barry Warsawc2feec31996-12-12 22:48:20 +00002614 PyObject *m, *d;
Guido van Rossumbbf94341991-12-16 15:44:53 +00002615 static int inited = 0;
2616
Guido van Rossum2d14e211991-02-19 12:26:49 +00002617 if (!inited) {
Guido van Rossumb6775db1994-08-01 11:34:53 +00002618#ifdef macintosh
2619 winit();
Jack Jansend56c1091995-01-27 14:44:16 +00002620 PyMac_DoYieldEnabled = 0;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002621#else
Guido van Rossum39cb5ce1995-01-26 00:37:10 +00002622 char buf[1000];
Guido van Rossumcacd9571993-10-18 11:44:47 +00002623 int argc = 0;
2624 char **argv = NULL;
Barry Warsawc2feec31996-12-12 22:48:20 +00002625 PyObject *sys_argv = PySys_GetObject("argv");
Guido van Rossumcacd9571993-10-18 11:44:47 +00002626 if (sys_argv != NULL) {
2627 if (!checkstringlist(sys_argv, &argv, &argc))
Barry Warsawc2feec31996-12-12 22:48:20 +00002628 PyErr_Clear();
Guido van Rossumcacd9571993-10-18 11:44:47 +00002629 }
Guido van Rossumc45611d1993-11-17 22:58:56 +00002630 if (argc > 0) {
2631 /* If argv[0] has a ".py" suffix, remove the suffix */
2632 char *p = strrchr(argv[0], '.');
2633 if (p != NULL && strcmp(p, ".py") == 0) {
2634 int n = p - argv[0];
2635 if (n >= sizeof(buf))
2636 n = sizeof(buf)-1;
2637 strncpy(buf, argv[0], n);
2638 buf[n] = '\0';
2639 argv[0] = buf;
2640 }
2641 }
Guido van Rossumcacd9571993-10-18 11:44:47 +00002642 winitargs(&argc, &argv);
2643 if (argv != NULL) {
2644 if (!putbackstringlist(sys_argv, argv, argc))
Barry Warsawc2feec31996-12-12 22:48:20 +00002645 PyErr_Clear();
Guido van Rossumcacd9571993-10-18 11:44:47 +00002646 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002647#endif
Guido van Rossum2d14e211991-02-19 12:26:49 +00002648 inited = 1;
2649 }
Barry Warsawc2feec31996-12-12 22:48:20 +00002650 m = Py_InitModule("stdwin", stdwin_methods);
2651 d = PyModule_GetDict(m);
Guido van Rossumbbf94341991-12-16 15:44:53 +00002652
2653 /* Initialize stdwin.error exception */
Barry Warsawc2feec31996-12-12 22:48:20 +00002654 StdwinError = PyString_FromString("stdwin.error");
2655 if (StdwinError == NULL ||
2656 PyDict_SetItemString(d, "error", StdwinError) != 0)
2657 Py_FatalError("can't define stdwin.error");
Guido van Rossumb6775db1994-08-01 11:34:53 +00002658#ifdef WITH_THREAD
Guido van Rossumff4949e1992-08-05 19:58:53 +00002659 StdwinLock = allocate_lock();
2660 if (StdwinLock == NULL)
Barry Warsawc2feec31996-12-12 22:48:20 +00002661 Py_FatalError("can't allocate stdwin lock");
Guido van Rossumff4949e1992-08-05 19:58:53 +00002662#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002663}