blob: 27ffdb2c75d6d14c899c06d976088543beec62b6 [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
Guido van Rossumf6a84db1997-10-20 23:22:07 +0000104#define getintarg(v,a) PyArg_Parse(v, "i", a)
105#define getlongarg(v,a) PyArg_Parse(v, "l", a)
106#define getstrarg(v,a) PyArg_Parse(v, "s", a)
Barry Warsawc2feec31996-12-12 22:48:20 +0000107#define getpointarg(v, a) PyArg_Parse(v, "(ii)", a, (a)+1)
108#define get3pointarg(v, a) PyArg_Parse(v, "((ii)(ii)(ii))", \
109 a, a+1, a+2, a+3, a+4, a+5)
110#define getrectarg(v, a) PyArg_Parse(v, "((ii)(ii))", a, a+1, a+2, a+3)
111#define getrectintarg(v, a) PyArg_Parse(v, "(((ii)(ii))i)", \
112 a, a+1, a+2, a+3, a+4)
113#define getpointintarg(v, a) PyArg_Parse(v, "((ii)i)", a, a+1, a+2)
114#define getrectpointarg(v, a) PyArg_Parse(v, "(((ii)(ii))(ii))", \
115 a, a+1, a+2, a+3, a+4, a+5)
Guido van Rossum234f9421993-06-17 12:35:49 +0000116
Barry Warsawc2feec31996-12-12 22:48:20 +0000117static PyObject *StdwinError; /* Exception stdwin.error */
Guido van Rossum87e7ea71991-12-10 14:00:03 +0000118
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000119/* Window and menu object types declared here because of forward references */
120
121typedef struct {
Barry Warsawc2feec31996-12-12 22:48:20 +0000122 PyObject_HEAD
123 PyObject *w_title;
124 WINDOW *w_win;
125 PyObject *w_attr; /* Attributes dictionary */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000126} windowobject;
127
Barry Warsawc2feec31996-12-12 22:48:20 +0000128staticforward PyTypeObject Windowtype;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000129
130#define is_windowobject(wp) ((wp)->ob_type == &Windowtype)
131
132typedef struct {
Barry Warsawc2feec31996-12-12 22:48:20 +0000133 PyObject_HEAD
134 MENU *m_menu;
135 int m_id;
136 PyObject *m_attr; /* Attributes dictionary */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000137} menuobject;
138
Barry Warsawc2feec31996-12-12 22:48:20 +0000139staticforward PyTypeObject Menutype;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000140
141#define is_menuobject(mp) ((mp)->ob_type == &Menutype)
142
Guido van Rossumbf80e541993-02-08 15:49:17 +0000143typedef struct {
Barry Warsawc2feec31996-12-12 22:48:20 +0000144 PyObject_HEAD
145 BITMAP *b_bitmap;
146 PyObject *b_attr; /* Attributes dictionary */
Guido van Rossumbf80e541993-02-08 15:49:17 +0000147} bitmapobject;
148
Barry Warsawc2feec31996-12-12 22:48:20 +0000149staticforward PyTypeObject Bitmaptype;
Guido van Rossumbf80e541993-02-08 15:49:17 +0000150
151#define is_bitmapobject(mp) ((mp)->ob_type == &Bitmaptype)
152
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000153
154/* Strongly stdwin-specific argument handlers */
155
156static int
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000157getmenudetail(v, ep)
Barry Warsawc2feec31996-12-12 22:48:20 +0000158 PyObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000159 EVENT *ep;
160{
Guido van Rossumfc58e581992-01-27 16:45:55 +0000161 menuobject *mp;
Barry Warsawc2feec31996-12-12 22:48:20 +0000162 if (!PyArg_Parse(v, "(Oi)", &mp, &ep->u.m.item))
Guido van Rossumfc58e581992-01-27 16:45:55 +0000163 return 0;
164 if (!is_menuobject(mp))
Barry Warsawc2feec31996-12-12 22:48:20 +0000165 return PyErr_BadArgument();
Guido van Rossumfc58e581992-01-27 16:45:55 +0000166 ep->u.m.id = mp->m_id;
167 return 1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000168}
169
170static int
171geteventarg(v, ep)
Barry Warsawc2feec31996-12-12 22:48:20 +0000172 PyObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000173 EVENT *ep;
174{
Barry Warsawc2feec31996-12-12 22:48:20 +0000175 PyObject *wp, *detail;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000176 int a[4];
Barry Warsawc2feec31996-12-12 22:48:20 +0000177 if (!PyArg_Parse(v, "(iOO)", &ep->type, &wp, &detail))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000178 return 0;
Guido van Rossumfc58e581992-01-27 16:45:55 +0000179 if (is_windowobject(wp))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000180 ep->window = ((windowobject *)wp) -> w_win;
Barry Warsawc2feec31996-12-12 22:48:20 +0000181 else if (wp == Py_None)
Guido van Rossumfc58e581992-01-27 16:45:55 +0000182 ep->window = NULL;
183 else
Barry Warsawc2feec31996-12-12 22:48:20 +0000184 return PyErr_BadArgument();
Guido van Rossumfc58e581992-01-27 16:45:55 +0000185 switch (ep->type) {
186 case WE_CHAR: {
187 char c;
Barry Warsawc2feec31996-12-12 22:48:20 +0000188 if (!PyArg_Parse(detail, "c", &c))
Guido van Rossumfc58e581992-01-27 16:45:55 +0000189 return 0;
190 ep->u.character = c;
191 return 1;
192 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000193 case WE_COMMAND:
194 return getintarg(detail, &ep->u.command);
195 case WE_DRAW:
196 if (!getrectarg(detail, a))
197 return 0;
198 ep->u.area.left = a[0];
199 ep->u.area.top = a[1];
200 ep->u.area.right = a[2];
201 ep->u.area.bottom = a[3];
202 return 1;
203 case WE_MOUSE_DOWN:
204 case WE_MOUSE_UP:
205 case WE_MOUSE_MOVE:
Barry Warsawc2feec31996-12-12 22:48:20 +0000206 return PyArg_Parse(detail, "((ii)iii)",
207 &ep->u.where.h, &ep->u.where.v,
208 &ep->u.where.clicks,
209 &ep->u.where.button,
210 &ep->u.where.mask);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000211 case WE_MENU:
212 return getmenudetail(detail, ep);
Guido van Rossum3ee199e1992-06-30 12:48:26 +0000213 case WE_KEY:
Barry Warsawc2feec31996-12-12 22:48:20 +0000214 return PyArg_Parse(detail, "(ii)",
215 &ep->u.key.code, &ep->u.key.mask);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000216 default:
217 return 1;
218 }
219}
220
221
222/* Return construction tools */
223
Barry Warsawc2feec31996-12-12 22:48:20 +0000224static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000225makepoint(a, b)
226 int a, b;
227{
Barry Warsawc2feec31996-12-12 22:48:20 +0000228 return Py_BuildValue("(ii)", a, b);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000229}
230
Barry Warsawc2feec31996-12-12 22:48:20 +0000231static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000232makerect(a, b, c, d)
233 int a, b, c, d;
234{
Barry Warsawc2feec31996-12-12 22:48:20 +0000235 return Py_BuildValue("((ii)(ii))", a, b, c, d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000236}
237
238
239/* Drawing objects */
240
241typedef struct {
Barry Warsawc2feec31996-12-12 22:48:20 +0000242 PyObject_HEAD
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000243 windowobject *d_ref;
244} drawingobject;
245
246static drawingobject *Drawing; /* Set to current drawing object, or NULL */
247
248/* Drawing methods */
249
Barry Warsawc2feec31996-12-12 22:48:20 +0000250static PyObject *
Guido van Rossum3c284741991-11-27 14:54:54 +0000251drawing_close(dp)
252 drawingobject *dp;
253{
254 if (dp->d_ref != NULL) {
255 wenddrawing(dp->d_ref->w_win);
256 Drawing = NULL;
Barry Warsawc2feec31996-12-12 22:48:20 +0000257 Py_DECREF(dp->d_ref);
Guido van Rossum3c284741991-11-27 14:54:54 +0000258 dp->d_ref = NULL;
259 }
Barry Warsawc2feec31996-12-12 22:48:20 +0000260 Py_INCREF(Py_None);
261 return Py_None;
Guido van Rossum3c284741991-11-27 14:54:54 +0000262}
Guido van Rossum77b46041992-01-14 18:41:24 +0000263
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000264static void
265drawing_dealloc(dp)
266 drawingobject *dp;
267{
Guido van Rossum3c284741991-11-27 14:54:54 +0000268 if (dp->d_ref != NULL) {
269 wenddrawing(dp->d_ref->w_win);
270 Drawing = NULL;
Barry Warsawc2feec31996-12-12 22:48:20 +0000271 Py_DECREF(dp->d_ref);
Guido van Rossum3c284741991-11-27 14:54:54 +0000272 dp->d_ref = NULL;
273 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000274 free((char *)dp);
275}
276
Barry Warsawc2feec31996-12-12 22:48:20 +0000277static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000278drawing_generic(dp, args, func)
279 drawingobject *dp;
Barry Warsawc2feec31996-12-12 22:48:20 +0000280 PyObject *args;
281 void (*func) Py_FPROTO((int, int, int, int));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000282{
283 int a[4];
284 if (!getrectarg(args, a))
285 return NULL;
286 (*func)(a[0], a[1], a[2], a[3]);
Barry Warsawc2feec31996-12-12 22:48:20 +0000287 Py_INCREF(Py_None);
288 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000289}
290
Barry Warsawc2feec31996-12-12 22:48:20 +0000291static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000292drawing_line(dp, args)
293 drawingobject *dp;
Barry Warsawc2feec31996-12-12 22:48:20 +0000294 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000295{
Guido van Rossumbf109731991-03-06 13:14:12 +0000296 return drawing_generic(dp, args, wdrawline);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000297}
298
Barry Warsawc2feec31996-12-12 22:48:20 +0000299static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000300drawing_xorline(dp, args)
301 drawingobject *dp;
Barry Warsawc2feec31996-12-12 22:48:20 +0000302 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000303{
Guido van Rossumbf109731991-03-06 13:14:12 +0000304 return drawing_generic(dp, args, wxorline);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000305}
306
Barry Warsawc2feec31996-12-12 22:48:20 +0000307static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000308drawing_circle(dp, args)
309 drawingobject *dp;
Barry Warsawc2feec31996-12-12 22:48:20 +0000310 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000311{
312 int a[3];
313 if (!getpointintarg(args, a))
314 return NULL;
315 wdrawcircle(a[0], a[1], a[2]);
Barry Warsawc2feec31996-12-12 22:48:20 +0000316 Py_INCREF(Py_None);
317 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000318}
Guido van Rossuma2a181a1991-05-14 12:09:25 +0000319
Barry Warsawc2feec31996-12-12 22:48:20 +0000320static PyObject *
Guido van Rossum27201061991-04-16 08:43:03 +0000321drawing_fillcircle(dp, args)
322 drawingobject *dp;
Barry Warsawc2feec31996-12-12 22:48:20 +0000323 PyObject *args;
Guido van Rossum27201061991-04-16 08:43:03 +0000324{
325 int a[3];
326 if (!getpointintarg(args, a))
327 return NULL;
328 wfillcircle(a[0], a[1], a[2]);
Barry Warsawc2feec31996-12-12 22:48:20 +0000329 Py_INCREF(Py_None);
330 return Py_None;
Guido van Rossum27201061991-04-16 08:43:03 +0000331}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000332
Barry Warsawc2feec31996-12-12 22:48:20 +0000333static PyObject *
Guido van Rossuma2a181a1991-05-14 12:09:25 +0000334drawing_xorcircle(dp, args)
335 drawingobject *dp;
Barry Warsawc2feec31996-12-12 22:48:20 +0000336 PyObject *args;
Guido van Rossuma2a181a1991-05-14 12:09:25 +0000337{
338 int a[3];
339 if (!getpointintarg(args, a))
340 return NULL;
341 wxorcircle(a[0], a[1], a[2]);
Barry Warsawc2feec31996-12-12 22:48:20 +0000342 Py_INCREF(Py_None);
343 return Py_None;
Guido van Rossuma2a181a1991-05-14 12:09:25 +0000344}
345
Barry Warsawc2feec31996-12-12 22:48:20 +0000346static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000347drawing_elarc(dp, args)
348 drawingobject *dp;
Barry Warsawc2feec31996-12-12 22:48:20 +0000349 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000350{
351 int a[6];
352 if (!get3pointarg(args, a))
353 return NULL;
354 wdrawelarc(a[0], a[1], a[2], a[3], a[4], a[5]);
Barry Warsawc2feec31996-12-12 22:48:20 +0000355 Py_INCREF(Py_None);
356 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000357}
358
Barry Warsawc2feec31996-12-12 22:48:20 +0000359static PyObject *
Guido van Rossum27201061991-04-16 08:43:03 +0000360drawing_fillelarc(dp, args)
361 drawingobject *dp;
Barry Warsawc2feec31996-12-12 22:48:20 +0000362 PyObject *args;
Guido van Rossum27201061991-04-16 08:43:03 +0000363{
364 int a[6];
365 if (!get3pointarg(args, a))
366 return NULL;
367 wfillelarc(a[0], a[1], a[2], a[3], a[4], a[5]);
Barry Warsawc2feec31996-12-12 22:48:20 +0000368 Py_INCREF(Py_None);
369 return Py_None;
Guido van Rossum27201061991-04-16 08:43:03 +0000370}
371
Barry Warsawc2feec31996-12-12 22:48:20 +0000372static PyObject *
Guido van Rossuma2a181a1991-05-14 12:09:25 +0000373drawing_xorelarc(dp, args)
374 drawingobject *dp;
Barry Warsawc2feec31996-12-12 22:48:20 +0000375 PyObject *args;
Guido van Rossuma2a181a1991-05-14 12:09:25 +0000376{
377 int a[6];
378 if (!get3pointarg(args, a))
379 return NULL;
380 wxorelarc(a[0], a[1], a[2], a[3], a[4], a[5]);
Barry Warsawc2feec31996-12-12 22:48:20 +0000381 Py_INCREF(Py_None);
382 return Py_None;
Guido van Rossuma2a181a1991-05-14 12:09:25 +0000383}
384
Barry Warsawc2feec31996-12-12 22:48:20 +0000385static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000386drawing_box(dp, args)
387 drawingobject *dp;
Barry Warsawc2feec31996-12-12 22:48:20 +0000388 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000389{
Guido van Rossumbf109731991-03-06 13:14:12 +0000390 return drawing_generic(dp, args, wdrawbox);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000391}
392
Barry Warsawc2feec31996-12-12 22:48:20 +0000393static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000394drawing_erase(dp, args)
395 drawingobject *dp;
Barry Warsawc2feec31996-12-12 22:48:20 +0000396 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000397{
Guido van Rossumbf109731991-03-06 13:14:12 +0000398 return drawing_generic(dp, args, werase);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000399}
400
Barry Warsawc2feec31996-12-12 22:48:20 +0000401static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000402drawing_paint(dp, args)
403 drawingobject *dp;
Barry Warsawc2feec31996-12-12 22:48:20 +0000404 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000405{
Guido van Rossumbf109731991-03-06 13:14:12 +0000406 return drawing_generic(dp, args, wpaint);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000407}
408
Barry Warsawc2feec31996-12-12 22:48:20 +0000409static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000410drawing_invert(dp, args)
411 drawingobject *dp;
Barry Warsawc2feec31996-12-12 22:48:20 +0000412 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000413{
Guido van Rossumbf109731991-03-06 13:14:12 +0000414 return drawing_generic(dp, args, winvert);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000415}
416
Guido van Rossum27201061991-04-16 08:43:03 +0000417static POINT *
418getpointsarray(v, psize)
Barry Warsawc2feec31996-12-12 22:48:20 +0000419 PyObject *v;
Guido van Rossum27201061991-04-16 08:43:03 +0000420 int *psize;
421{
422 int n = -1;
Barry Warsawc2feec31996-12-12 22:48:20 +0000423 PyObject * (*getitem) Py_PROTO((PyObject *, int));
Guido van Rossum27201061991-04-16 08:43:03 +0000424 int i;
425 POINT *points;
426
427 if (v == NULL)
428 ;
Barry Warsawc2feec31996-12-12 22:48:20 +0000429 else if (PyList_Check(v)) {
430 n = PyList_Size(v);
431 getitem = PyList_GetItem;
Guido van Rossum27201061991-04-16 08:43:03 +0000432 }
Barry Warsawc2feec31996-12-12 22:48:20 +0000433 else if (PyTuple_Check(v)) {
434 n = PyTuple_Size(v);
435 getitem = PyTuple_GetItem;
Guido van Rossum27201061991-04-16 08:43:03 +0000436 }
437
438 if (n <= 0) {
Barry Warsawc2feec31996-12-12 22:48:20 +0000439 (void) PyErr_BadArgument();
Guido van Rossum27201061991-04-16 08:43:03 +0000440 return NULL;
441 }
442
Barry Warsawc2feec31996-12-12 22:48:20 +0000443 points = PyMem_NEW(POINT, n);
Guido van Rossum27201061991-04-16 08:43:03 +0000444 if (points == NULL) {
Barry Warsawc2feec31996-12-12 22:48:20 +0000445 (void) PyErr_NoMemory();
Guido van Rossum27201061991-04-16 08:43:03 +0000446 return NULL;
447 }
448
449 for (i = 0; i < n; i++) {
Barry Warsawc2feec31996-12-12 22:48:20 +0000450 PyObject *w = (*getitem)(v, i);
Guido van Rossum27201061991-04-16 08:43:03 +0000451 int a[2];
452 if (!getpointarg(w, a)) {
Barry Warsawc2feec31996-12-12 22:48:20 +0000453 PyMem_DEL(points);
Guido van Rossum27201061991-04-16 08:43:03 +0000454 return NULL;
455 }
456 points[i].h = a[0];
457 points[i].v = a[1];
458 }
459
460 *psize = n;
461 return points;
462}
463
Barry Warsawc2feec31996-12-12 22:48:20 +0000464static PyObject *
Guido van Rossum27201061991-04-16 08:43:03 +0000465drawing_poly(dp, args)
466 drawingobject *dp;
Barry Warsawc2feec31996-12-12 22:48:20 +0000467 PyObject *args;
Guido van Rossum27201061991-04-16 08:43:03 +0000468{
469 int n;
470 POINT *points = getpointsarray(args, &n);
471 if (points == NULL)
472 return NULL;
473 wdrawpoly(n, points);
Barry Warsawc2feec31996-12-12 22:48:20 +0000474 PyMem_DEL(points);
475 Py_INCREF(Py_None);
476 return Py_None;
Guido van Rossum27201061991-04-16 08:43:03 +0000477}
478
Barry Warsawc2feec31996-12-12 22:48:20 +0000479static PyObject *
Guido van Rossum27201061991-04-16 08:43:03 +0000480drawing_fillpoly(dp, args)
481 drawingobject *dp;
Barry Warsawc2feec31996-12-12 22:48:20 +0000482 PyObject *args;
Guido van Rossum27201061991-04-16 08:43:03 +0000483{
484 int n;
485 POINT *points = getpointsarray(args, &n);
486 if (points == NULL)
487 return NULL;
488 wfillpoly(n, points);
Barry Warsawc2feec31996-12-12 22:48:20 +0000489 PyMem_DEL(points);
490 Py_INCREF(Py_None);
491 return Py_None;
Guido van Rossum27201061991-04-16 08:43:03 +0000492}
493
Barry Warsawc2feec31996-12-12 22:48:20 +0000494static PyObject *
Guido van Rossuma2a181a1991-05-14 12:09:25 +0000495drawing_xorpoly(dp, args)
496 drawingobject *dp;
Barry Warsawc2feec31996-12-12 22:48:20 +0000497 PyObject *args;
Guido van Rossuma2a181a1991-05-14 12:09:25 +0000498{
499 int n;
500 POINT *points = getpointsarray(args, &n);
501 if (points == NULL)
502 return NULL;
503 wxorpoly(n, points);
Barry Warsawc2feec31996-12-12 22:48:20 +0000504 PyMem_DEL(points);
505 Py_INCREF(Py_None);
506 return Py_None;
Guido van Rossuma2a181a1991-05-14 12:09:25 +0000507}
508
Barry Warsawc2feec31996-12-12 22:48:20 +0000509static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000510drawing_cliprect(dp, args)
511 drawingobject *dp;
Barry Warsawc2feec31996-12-12 22:48:20 +0000512 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000513{
Guido van Rossumbf109731991-03-06 13:14:12 +0000514 return drawing_generic(dp, args, wcliprect);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000515}
516
Barry Warsawc2feec31996-12-12 22:48:20 +0000517static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000518drawing_noclip(dp, args)
519 drawingobject *dp;
Barry Warsawc2feec31996-12-12 22:48:20 +0000520 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000521{
Barry Warsawc2feec31996-12-12 22:48:20 +0000522 if (!PyArg_NoArgs(args))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000523 return NULL;
524 wnoclip();
Barry Warsawc2feec31996-12-12 22:48:20 +0000525 Py_INCREF(Py_None);
526 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000527}
528
Barry Warsawc2feec31996-12-12 22:48:20 +0000529static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000530drawing_shade(dp, args)
531 drawingobject *dp;
Barry Warsawc2feec31996-12-12 22:48:20 +0000532 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000533{
534 int a[5];
535 if (!getrectintarg(args, a))
536 return NULL;
537 wshade(a[0], a[1], a[2], a[3], a[4]);
Barry Warsawc2feec31996-12-12 22:48:20 +0000538 Py_INCREF(Py_None);
539 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000540}
541
Barry Warsawc2feec31996-12-12 22:48:20 +0000542static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000543drawing_text(dp, args)
544 drawingobject *dp;
Barry Warsawc2feec31996-12-12 22:48:20 +0000545 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000546{
Guido van Rossumfc58e581992-01-27 16:45:55 +0000547 int h, v, size;
548 char *text;
Barry Warsawc2feec31996-12-12 22:48:20 +0000549 if (!PyArg_Parse(args, "((ii)s#)", &h, &v, &text, &size))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000550 return NULL;
Guido van Rossumfc58e581992-01-27 16:45:55 +0000551 wdrawtext(h, v, text, size);
Barry Warsawc2feec31996-12-12 22:48:20 +0000552 Py_INCREF(Py_None);
553 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000554}
555
556/* The following four are also used as stdwin functions */
557
Barry Warsawc2feec31996-12-12 22:48:20 +0000558static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000559drawing_lineheight(dp, args)
560 drawingobject *dp;
Barry Warsawc2feec31996-12-12 22:48:20 +0000561 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000562{
Barry Warsawc2feec31996-12-12 22:48:20 +0000563 if (!PyArg_NoArgs(args))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000564 return NULL;
Barry Warsawc2feec31996-12-12 22:48:20 +0000565 return PyInt_FromLong((long)wlineheight());
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000566}
567
Barry Warsawc2feec31996-12-12 22:48:20 +0000568static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000569drawing_baseline(dp, args)
570 drawingobject *dp;
Barry Warsawc2feec31996-12-12 22:48:20 +0000571 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000572{
Barry Warsawc2feec31996-12-12 22:48:20 +0000573 if (!PyArg_NoArgs(args))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000574 return NULL;
Barry Warsawc2feec31996-12-12 22:48:20 +0000575 return PyInt_FromLong((long)wbaseline());
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000576}
577
Barry Warsawc2feec31996-12-12 22:48:20 +0000578static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000579drawing_textwidth(dp, args)
580 drawingobject *dp;
Barry Warsawc2feec31996-12-12 22:48:20 +0000581 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000582{
Guido van Rossumfc58e581992-01-27 16:45:55 +0000583 char *text;
584 int size;
Barry Warsawc2feec31996-12-12 22:48:20 +0000585 if (!PyArg_Parse(args, "s#", &text, &size))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000586 return NULL;
Barry Warsawc2feec31996-12-12 22:48:20 +0000587 return PyInt_FromLong((long)wtextwidth(text, size));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000588}
589
Barry Warsawc2feec31996-12-12 22:48:20 +0000590static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000591drawing_textbreak(dp, args)
592 drawingobject *dp;
Barry Warsawc2feec31996-12-12 22:48:20 +0000593 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000594{
Guido van Rossumfc58e581992-01-27 16:45:55 +0000595 char *text;
596 int size, width;
Barry Warsawc2feec31996-12-12 22:48:20 +0000597 if (!PyArg_Parse(args, "(s#i)", &text, &size, &width))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000598 return NULL;
Barry Warsawc2feec31996-12-12 22:48:20 +0000599 return PyInt_FromLong((long)wtextbreak(text, size, width));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000600}
601
Barry Warsawc2feec31996-12-12 22:48:20 +0000602static PyObject *
Guido van Rossum0c2290b1991-04-03 19:12:14 +0000603drawing_setfont(self, args)
604 drawingobject *self;
Barry Warsawc2feec31996-12-12 22:48:20 +0000605 PyObject *args;
Guido van Rossum0c2290b1991-04-03 19:12:14 +0000606{
Guido van Rossumfc58e581992-01-27 16:45:55 +0000607 char *font;
608 char style = '\0';
609 int size = 0;
Barry Warsawc2feec31996-12-12 22:48:20 +0000610 if (args == NULL || !PyTuple_Check(args)) {
611 if (!PyArg_Parse(args, "z", &font))
Guido van Rossum50429a11991-04-04 15:24:07 +0000612 return NULL;
613 }
614 else {
Barry Warsawc2feec31996-12-12 22:48:20 +0000615 int n = PyTuple_Size(args);
Guido van Rossumfc58e581992-01-27 16:45:55 +0000616 if (n == 2) {
Barry Warsawc2feec31996-12-12 22:48:20 +0000617 if (!PyArg_Parse(args, "(zi)", &font, &size))
Guido van Rossumfc58e581992-01-27 16:45:55 +0000618 return NULL;
619 }
Barry Warsawc2feec31996-12-12 22:48:20 +0000620 else if (!PyArg_Parse(args, "(zic)", &font, &size, &style)) {
621 PyErr_Clear();
622 if (!PyArg_Parse(args, "(zci)", &font, &style, &size))
Guido van Rossumfc58e581992-01-27 16:45:55 +0000623 return NULL;
Guido van Rossum50429a11991-04-04 15:24:07 +0000624 }
625 }
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000626 if (font != NULL) {
627 if (!wsetfont(font)) {
Barry Warsawc2feec31996-12-12 22:48:20 +0000628 PyErr_SetString(StdwinError, "font not found");
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000629 return NULL;
630 }
631 }
Guido van Rossum50429a11991-04-04 15:24:07 +0000632 if (size != 0)
633 wsetsize(size);
Guido van Rossumfc58e581992-01-27 16:45:55 +0000634 switch (style) {
635 case 'b':
636 wsetbold();
637 break;
638 case 'i':
639 wsetitalic();
640 break;
641 case 'o':
642 wsetbolditalic();
643 break;
644 case 'u':
645 wsetunderline();
646 break;
647 case 'p':
648 wsetplain();
649 break;
650 }
Barry Warsawc2feec31996-12-12 22:48:20 +0000651 Py_INCREF(Py_None);
652 return Py_None;
Guido van Rossum0c2290b1991-04-03 19:12:14 +0000653}
654
Barry Warsawc2feec31996-12-12 22:48:20 +0000655static PyObject *
Guido van Rossum0c2290b1991-04-03 19:12:14 +0000656drawing_getbgcolor(self, args)
Barry Warsawc2feec31996-12-12 22:48:20 +0000657 PyObject *self;
658 PyObject *args;
Guido van Rossum0c2290b1991-04-03 19:12:14 +0000659{
Barry Warsawc2feec31996-12-12 22:48:20 +0000660 if (!PyArg_NoArgs(args))
Guido van Rossum0c2290b1991-04-03 19:12:14 +0000661 return NULL;
Barry Warsawc2feec31996-12-12 22:48:20 +0000662 return PyInt_FromLong((long)wgetbgcolor());
Guido van Rossum0c2290b1991-04-03 19:12:14 +0000663}
664
Barry Warsawc2feec31996-12-12 22:48:20 +0000665static PyObject *
Guido van Rossum0c2290b1991-04-03 19:12:14 +0000666drawing_getfgcolor(self, args)
Barry Warsawc2feec31996-12-12 22:48:20 +0000667 PyObject *self;
668 PyObject *args;
Guido van Rossum0c2290b1991-04-03 19:12:14 +0000669{
Barry Warsawc2feec31996-12-12 22:48:20 +0000670 if (!PyArg_NoArgs(args))
Guido van Rossum0c2290b1991-04-03 19:12:14 +0000671 return NULL;
Barry Warsawc2feec31996-12-12 22:48:20 +0000672 return PyInt_FromLong((long)wgetfgcolor());
Guido van Rossum0c2290b1991-04-03 19:12:14 +0000673}
674
Barry Warsawc2feec31996-12-12 22:48:20 +0000675static PyObject *
Guido van Rossum0c2290b1991-04-03 19:12:14 +0000676drawing_setbgcolor(self, args)
Barry Warsawc2feec31996-12-12 22:48:20 +0000677 PyObject *self;
678 PyObject *args;
Guido van Rossum0c2290b1991-04-03 19:12:14 +0000679{
680 long color;
681 if (!getlongarg(args, &color))
682 return NULL;
683 wsetbgcolor((COLOR)color);
Barry Warsawc2feec31996-12-12 22:48:20 +0000684 Py_INCREF(Py_None);
685 return Py_None;
Guido van Rossum0c2290b1991-04-03 19:12:14 +0000686}
687
Barry Warsawc2feec31996-12-12 22:48:20 +0000688static PyObject *
Guido van Rossum0c2290b1991-04-03 19:12:14 +0000689drawing_setfgcolor(self, args)
Barry Warsawc2feec31996-12-12 22:48:20 +0000690 PyObject *self;
691 PyObject *args;
Guido van Rossum0c2290b1991-04-03 19:12:14 +0000692{
693 long color;
694 if (!getlongarg(args, &color))
695 return NULL;
696 wsetfgcolor((COLOR)color);
Barry Warsawc2feec31996-12-12 22:48:20 +0000697 Py_INCREF(Py_None);
698 return Py_None;
Guido van Rossum0c2290b1991-04-03 19:12:14 +0000699}
700
Guido van Rossume9066061993-07-29 13:14:32 +0000701#ifdef HAVE_BITMAPS
702
Barry Warsawc2feec31996-12-12 22:48:20 +0000703static PyObject *
Guido van Rossumbf80e541993-02-08 15:49:17 +0000704drawing_bitmap(self, args)
Barry Warsawc2feec31996-12-12 22:48:20 +0000705 PyObject *self;
706 PyObject *args;
Guido van Rossumbf80e541993-02-08 15:49:17 +0000707{
708 int h, v;
Barry Warsawc2feec31996-12-12 22:48:20 +0000709 PyObject *bp;
710 PyObject *mask = NULL;
711 if (!PyArg_Parse(args, "((ii)O)", &h, &v, &bp)) {
712 PyErr_Clear();
713 if (!PyArg_Parse(args, "((ii)OO)", &h, &v, &bp, &mask))
Guido van Rossumbf80e541993-02-08 15:49:17 +0000714 return NULL;
Barry Warsawc2feec31996-12-12 22:48:20 +0000715 if (mask == Py_None)
Guido van Rossumbf80e541993-02-08 15:49:17 +0000716 mask = NULL;
717 else if (!is_bitmapobject(mask)) {
Barry Warsawc2feec31996-12-12 22:48:20 +0000718 PyErr_BadArgument();
Guido van Rossumbf80e541993-02-08 15:49:17 +0000719 return NULL;
720 }
721 }
722 if (!is_bitmapobject(bp)) {
Barry Warsawc2feec31996-12-12 22:48:20 +0000723 PyErr_BadArgument();
Guido van Rossumbf80e541993-02-08 15:49:17 +0000724 return NULL;
725 }
726 if (((bitmapobject *)bp)->b_bitmap == NULL ||
727 mask != NULL && ((bitmapobject *)mask)->b_bitmap == NULL) {
Barry Warsawc2feec31996-12-12 22:48:20 +0000728 PyErr_SetString(StdwinError, "bitmap object already close");
Guido van Rossumbf80e541993-02-08 15:49:17 +0000729 return NULL;
730 }
731 if (mask == NULL)
732 wdrawbitmap(h, v, ((bitmapobject *)bp)->b_bitmap, ALLBITS);
733 else
734 wdrawbitmap(h, v,
735 ((bitmapobject *)bp)->b_bitmap,
736 ((bitmapobject *)bp)->b_bitmap);
Barry Warsawc2feec31996-12-12 22:48:20 +0000737 Py_INCREF(Py_None);
738 return Py_None;
Guido van Rossumbf80e541993-02-08 15:49:17 +0000739}
740
Guido van Rossume9066061993-07-29 13:14:32 +0000741#endif /* HAVE_BITMAPS */
742
Barry Warsawc2feec31996-12-12 22:48:20 +0000743static PyMethodDef drawing_methods[] = {
Guido van Rossume9066061993-07-29 13:14:32 +0000744#ifdef HAVE_BITMAPS
Barry Warsawc2feec31996-12-12 22:48:20 +0000745 {"bitmap", (PyCFunction)drawing_bitmap},
Guido van Rossume9066061993-07-29 13:14:32 +0000746#endif
Barry Warsawc2feec31996-12-12 22:48:20 +0000747 {"box", (PyCFunction)drawing_box},
748 {"circle", (PyCFunction)drawing_circle},
749 {"cliprect", (PyCFunction)drawing_cliprect},
750 {"close", (PyCFunction)drawing_close},
751 {"elarc", (PyCFunction)drawing_elarc},
752 {"enddrawing", (PyCFunction)drawing_close},
753 {"erase", (PyCFunction)drawing_erase},
754 {"fillcircle", (PyCFunction)drawing_fillcircle},
755 {"fillelarc", (PyCFunction)drawing_fillelarc},
756 {"fillpoly", (PyCFunction)drawing_fillpoly},
757 {"invert", (PyCFunction)drawing_invert},
758 {"line", (PyCFunction)drawing_line},
759 {"noclip", (PyCFunction)drawing_noclip},
760 {"paint", (PyCFunction)drawing_paint},
761 {"poly", (PyCFunction)drawing_poly},
762 {"shade", (PyCFunction)drawing_shade},
763 {"text", (PyCFunction)drawing_text},
764 {"xorcircle", (PyCFunction)drawing_xorcircle},
765 {"xorelarc", (PyCFunction)drawing_xorelarc},
766 {"xorline", (PyCFunction)drawing_xorline},
767 {"xorpoly", (PyCFunction)drawing_xorpoly},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000768
769 /* Text measuring methods: */
Barry Warsawc2feec31996-12-12 22:48:20 +0000770 {"baseline", (PyCFunction)drawing_baseline},
771 {"lineheight", (PyCFunction)drawing_lineheight},
772 {"textbreak", (PyCFunction)drawing_textbreak},
773 {"textwidth", (PyCFunction)drawing_textwidth},
Guido van Rossum0c2290b1991-04-03 19:12:14 +0000774
775 /* Font setting methods: */
Barry Warsawc2feec31996-12-12 22:48:20 +0000776 {"setfont", (PyCFunction)drawing_setfont},
Guido van Rossum0c2290b1991-04-03 19:12:14 +0000777
778 /* Color methods: */
Barry Warsawc2feec31996-12-12 22:48:20 +0000779 {"getbgcolor", (PyCFunction)drawing_getbgcolor},
780 {"getfgcolor", (PyCFunction)drawing_getfgcolor},
781 {"setbgcolor", (PyCFunction)drawing_setbgcolor},
782 {"setfgcolor", (PyCFunction)drawing_setfgcolor},
Guido van Rossum0c2290b1991-04-03 19:12:14 +0000783
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000784 {NULL, NULL} /* sentinel */
785};
786
Barry Warsawc2feec31996-12-12 22:48:20 +0000787static PyObject *
Guido van Rossum77b46041992-01-14 18:41:24 +0000788drawing_getattr(dp, name)
789 drawingobject *dp;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000790 char *name;
791{
Guido van Rossum77b46041992-01-14 18:41:24 +0000792 if (dp->d_ref == NULL) {
Barry Warsawc2feec31996-12-12 22:48:20 +0000793 PyErr_SetString(StdwinError, "drawing object already closed");
Guido van Rossum77b46041992-01-14 18:41:24 +0000794 return NULL;
795 }
Barry Warsawc2feec31996-12-12 22:48:20 +0000796 return Py_FindMethod(drawing_methods, (PyObject *)dp, name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000797}
798
Barry Warsawc2feec31996-12-12 22:48:20 +0000799PyTypeObject Drawingtype = {
800 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000801 0, /*ob_size*/
802 "drawing", /*tp_name*/
803 sizeof(drawingobject), /*tp_size*/
804 0, /*tp_itemsize*/
805 /* methods */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000806 (destructor)drawing_dealloc, /*tp_dealloc*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000807 0, /*tp_print*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000808 (getattrfunc)drawing_getattr, /*tp_getattr*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000809 0, /*tp_setattr*/
810 0, /*tp_compare*/
811 0, /*tp_repr*/
812};
813
814
815/* Text(edit) objects */
816
817typedef struct {
Barry Warsawc2feec31996-12-12 22:48:20 +0000818 PyObject_HEAD
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000819 TEXTEDIT *t_text;
820 windowobject *t_ref;
Barry Warsawc2feec31996-12-12 22:48:20 +0000821 PyObject *t_attr; /* Attributes dictionary */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000822} textobject;
823
Barry Warsawc2feec31996-12-12 22:48:20 +0000824staticforward PyTypeObject Texttype;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000825
826static textobject *
827newtextobject(wp, left, top, right, bottom)
828 windowobject *wp;
829 int left, top, right, bottom;
830{
831 textobject *tp;
Barry Warsawc2feec31996-12-12 22:48:20 +0000832 tp = PyObject_NEW(textobject, &Texttype);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000833 if (tp == NULL)
834 return NULL;
835 tp->t_attr = NULL;
Barry Warsawc2feec31996-12-12 22:48:20 +0000836 Py_INCREF(wp);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000837 tp->t_ref = wp;
838 tp->t_text = tecreate(wp->w_win, left, top, right, bottom);
839 if (tp->t_text == NULL) {
Barry Warsawc2feec31996-12-12 22:48:20 +0000840 Py_DECREF(tp);
841 return (textobject *) PyErr_NoMemory();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000842 }
843 return tp;
844}
845
846/* Text(edit) methods */
847
848static void
849text_dealloc(tp)
850 textobject *tp;
851{
852 if (tp->t_text != NULL)
853 tefree(tp->t_text);
Barry Warsawc2feec31996-12-12 22:48:20 +0000854 Py_XDECREF(tp->t_attr);
855 Py_XDECREF(tp->t_ref);
856 PyMem_DEL(tp);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000857}
858
Barry Warsawc2feec31996-12-12 22:48:20 +0000859static PyObject *
Guido van Rossum3c284741991-11-27 14:54:54 +0000860text_close(tp, args)
861 textobject *tp;
Barry Warsawc2feec31996-12-12 22:48:20 +0000862 PyObject *args;
Guido van Rossum3c284741991-11-27 14:54:54 +0000863{
864 if (tp->t_text != NULL) {
865 tefree(tp->t_text);
866 tp->t_text = NULL;
867 }
868 if (tp->t_attr != NULL) {
Barry Warsawc2feec31996-12-12 22:48:20 +0000869 Py_DECREF(tp->t_attr);
Guido van Rossum3c284741991-11-27 14:54:54 +0000870 tp->t_attr = NULL;
871 }
872 if (tp->t_ref != NULL) {
Barry Warsawc2feec31996-12-12 22:48:20 +0000873 Py_DECREF(tp->t_ref);
Guido van Rossum3c284741991-11-27 14:54:54 +0000874 tp->t_ref = NULL;
875 }
Barry Warsawc2feec31996-12-12 22:48:20 +0000876 Py_INCREF(Py_None);
877 return Py_None;
Guido van Rossum3c284741991-11-27 14:54:54 +0000878}
879
Barry Warsawc2feec31996-12-12 22:48:20 +0000880static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000881text_arrow(self, args)
882 textobject *self;
Barry Warsawc2feec31996-12-12 22:48:20 +0000883 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000884{
885 int code;
886 if (!getintarg(args, &code))
887 return NULL;
888 tearrow(self->t_text, code);
Barry Warsawc2feec31996-12-12 22:48:20 +0000889 Py_INCREF(Py_None);
890 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000891}
892
Barry Warsawc2feec31996-12-12 22:48:20 +0000893static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000894text_draw(self, args)
895 textobject *self;
Barry Warsawc2feec31996-12-12 22:48:20 +0000896 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000897{
898 register TEXTEDIT *tp = self->t_text;
899 int a[4];
900 int left, top, right, bottom;
901 if (!getrectarg(args, a))
902 return NULL;
903 if (Drawing != NULL) {
Barry Warsawc2feec31996-12-12 22:48:20 +0000904 PyErr_SetString(StdwinError, "already drawing");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000905 return NULL;
906 }
907 /* Clip to text area and ignore if area is empty */
908 left = tegetleft(tp);
909 top = tegettop(tp);
910 right = tegetright(tp);
911 bottom = tegetbottom(tp);
912 if (a[0] < left) a[0] = left;
913 if (a[1] < top) a[1] = top;
914 if (a[2] > right) a[2] = right;
915 if (a[3] > bottom) a[3] = bottom;
916 if (a[0] < a[2] && a[1] < a[3]) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000917 wbegindrawing(self->t_ref->w_win);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000918 tedrawnew(tp, a[0], a[1], a[2], a[3]);
919 wenddrawing(self->t_ref->w_win);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000920 }
Barry Warsawc2feec31996-12-12 22:48:20 +0000921 Py_INCREF(Py_None);
922 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000923}
924
Barry Warsawc2feec31996-12-12 22:48:20 +0000925static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000926text_event(self, args)
927 textobject *self;
Barry Warsawc2feec31996-12-12 22:48:20 +0000928 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000929{
930 register TEXTEDIT *tp = self->t_text;
931 EVENT e;
932 if (!geteventarg(args, &e))
933 return NULL;
934 if (e.type == WE_MOUSE_DOWN) {
Guido van Rossum33f17701991-02-13 23:19:39 +0000935 /* Cheat at the margins */
936 int width, height;
937 wgetdocsize(e.window, &width, &height);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000938 if (e.u.where.h < 0 && tegetleft(tp) == 0)
939 e.u.where.h = 0;
Guido van Rossum33f17701991-02-13 23:19:39 +0000940 else if (e.u.where.h > width && tegetright(tp) == width)
941 e.u.where.h = width;
942 if (e.u.where.v < 0 && tegettop(tp) == 0)
943 e.u.where.v = 0;
944 else if (e.u.where.v > height && tegetright(tp) == height)
945 e.u.where.v = height;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000946 }
Barry Warsawc2feec31996-12-12 22:48:20 +0000947 return PyInt_FromLong((long) teevent(tp, &e));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000948}
949
Barry Warsawc2feec31996-12-12 22:48:20 +0000950static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000951text_getfocus(self, args)
952 textobject *self;
Barry Warsawc2feec31996-12-12 22:48:20 +0000953 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000954{
Barry Warsawc2feec31996-12-12 22:48:20 +0000955 if (!PyArg_NoArgs(args))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000956 return NULL;
957 return makepoint(tegetfoc1(self->t_text), tegetfoc2(self->t_text));
958}
959
Barry Warsawc2feec31996-12-12 22:48:20 +0000960static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000961text_getfocustext(self, args)
962 textobject *self;
Barry Warsawc2feec31996-12-12 22:48:20 +0000963 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000964{
965 int f1, f2;
966 char *text;
Barry Warsawc2feec31996-12-12 22:48:20 +0000967 if (!PyArg_NoArgs(args))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000968 return NULL;
969 f1 = tegetfoc1(self->t_text);
970 f2 = tegetfoc2(self->t_text);
971 text = tegettext(self->t_text);
Barry Warsawc2feec31996-12-12 22:48:20 +0000972 return PyString_FromStringAndSize(text + f1, f2-f1);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000973}
974
Barry Warsawc2feec31996-12-12 22:48:20 +0000975static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000976text_getrect(self, args)
977 textobject *self;
Barry Warsawc2feec31996-12-12 22:48:20 +0000978 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000979{
Barry Warsawc2feec31996-12-12 22:48:20 +0000980 if (!PyArg_NoArgs(args))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000981 return NULL;
982 return makerect(tegetleft(self->t_text),
983 tegettop(self->t_text),
984 tegetright(self->t_text),
985 tegetbottom(self->t_text));
986}
987
Barry Warsawc2feec31996-12-12 22:48:20 +0000988static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000989text_gettext(self, args)
990 textobject *self;
Barry Warsawc2feec31996-12-12 22:48:20 +0000991 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000992{
Barry Warsawc2feec31996-12-12 22:48:20 +0000993 if (!PyArg_NoArgs(args))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000994 return NULL;
Barry Warsawc2feec31996-12-12 22:48:20 +0000995 return PyString_FromStringAndSize(tegettext(self->t_text),
996 tegetlen(self->t_text));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000997}
998
Barry Warsawc2feec31996-12-12 22:48:20 +0000999static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001000text_move(self, args)
1001 textobject *self;
Barry Warsawc2feec31996-12-12 22:48:20 +00001002 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001003{
1004 int a[4];
1005 if (!getrectarg(args, a))
1006 return NULL;
1007 temovenew(self->t_text, a[0], a[1], a[2], a[3]);
Barry Warsawc2feec31996-12-12 22:48:20 +00001008 Py_INCREF(Py_None);
1009 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001010}
1011
Barry Warsawc2feec31996-12-12 22:48:20 +00001012static PyObject *
Guido van Rossum4b9cf8e1991-05-28 21:57:04 +00001013text_replace(self, args)
1014 textobject *self;
Barry Warsawc2feec31996-12-12 22:48:20 +00001015 PyObject *args;
Guido van Rossum4b9cf8e1991-05-28 21:57:04 +00001016{
Guido van Rossumfc58e581992-01-27 16:45:55 +00001017 char *text;
Guido van Rossum4b9cf8e1991-05-28 21:57:04 +00001018 if (!getstrarg(args, &text))
1019 return NULL;
Guido van Rossumfc58e581992-01-27 16:45:55 +00001020 tereplace(self->t_text, text);
Barry Warsawc2feec31996-12-12 22:48:20 +00001021 Py_INCREF(Py_None);
1022 return Py_None;
Guido van Rossum4b9cf8e1991-05-28 21:57:04 +00001023}
1024
Barry Warsawc2feec31996-12-12 22:48:20 +00001025static PyObject *
Guido van Rossum4b9cf8e1991-05-28 21:57:04 +00001026text_setactive(self, args)
1027 textobject *self;
Barry Warsawc2feec31996-12-12 22:48:20 +00001028 PyObject *args;
Guido van Rossum4b9cf8e1991-05-28 21:57:04 +00001029{
1030 int flag;
1031 if (!getintarg(args, &flag))
1032 return NULL;
1033 tesetactive(self->t_text, flag);
Barry Warsawc2feec31996-12-12 22:48:20 +00001034 Py_INCREF(Py_None);
1035 return Py_None;
Guido van Rossum4b9cf8e1991-05-28 21:57:04 +00001036}
1037
Barry Warsawc2feec31996-12-12 22:48:20 +00001038static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001039text_setfocus(self, args)
1040 textobject *self;
Barry Warsawc2feec31996-12-12 22:48:20 +00001041 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001042{
1043 int a[2];
1044 if (!getpointarg(args, a))
1045 return NULL;
1046 tesetfocus(self->t_text, a[0], a[1]);
Barry Warsawc2feec31996-12-12 22:48:20 +00001047 Py_INCREF(Py_None);
1048 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001049}
1050
Barry Warsawc2feec31996-12-12 22:48:20 +00001051static PyObject *
Guido van Rossum541c8c01991-05-05 20:13:41 +00001052text_settext(self, args)
1053 textobject *self;
Barry Warsawc2feec31996-12-12 22:48:20 +00001054 PyObject *args;
Guido van Rossum541c8c01991-05-05 20:13:41 +00001055{
Guido van Rossumfc58e581992-01-27 16:45:55 +00001056 char *text;
Guido van Rossum541c8c01991-05-05 20:13:41 +00001057 char *buf;
1058 int size;
Barry Warsawc2feec31996-12-12 22:48:20 +00001059 if (!PyArg_Parse(args, "s#", &text, &size))
Guido van Rossum541c8c01991-05-05 20:13:41 +00001060 return NULL;
Barry Warsawc2feec31996-12-12 22:48:20 +00001061 if ((buf = PyMem_NEW(char, size)) == NULL) {
1062 return PyErr_NoMemory();
Guido van Rossum541c8c01991-05-05 20:13:41 +00001063 }
Guido van Rossumfc58e581992-01-27 16:45:55 +00001064 memcpy(buf, text, size);
Guido van Rossum541c8c01991-05-05 20:13:41 +00001065 tesetbuf(self->t_text, buf, size); /* Becomes owner of buffer */
Barry Warsawc2feec31996-12-12 22:48:20 +00001066 Py_INCREF(Py_None);
1067 return Py_None;
Guido van Rossum541c8c01991-05-05 20:13:41 +00001068}
1069
Barry Warsawc2feec31996-12-12 22:48:20 +00001070static PyObject *
Guido van Rossum4b9cf8e1991-05-28 21:57:04 +00001071text_setview(self, args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001072 textobject *self;
Barry Warsawc2feec31996-12-12 22:48:20 +00001073 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001074{
Guido van Rossum4b9cf8e1991-05-28 21:57:04 +00001075 int a[4];
Barry Warsawc2feec31996-12-12 22:48:20 +00001076 if (args == Py_None)
Guido van Rossum4b9cf8e1991-05-28 21:57:04 +00001077 tenoview(self->t_text);
1078 else {
1079 if (!getrectarg(args, a))
1080 return NULL;
1081 tesetview(self->t_text, a[0], a[1], a[2], a[3]);
1082 }
Barry Warsawc2feec31996-12-12 22:48:20 +00001083 Py_INCREF(Py_None);
1084 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001085}
1086
Barry Warsawc2feec31996-12-12 22:48:20 +00001087static PyMethodDef text_methods[] = {
1088 {"arrow", (PyCFunction)text_arrow},
1089 {"close", (PyCFunction)text_close},
1090 {"draw", (PyCFunction)text_draw},
1091 {"event", (PyCFunction)text_event},
1092 {"getfocus", (PyCFunction)text_getfocus},
1093 {"getfocustext",(PyCFunction)text_getfocustext},
1094 {"getrect", (PyCFunction)text_getrect},
1095 {"gettext", (PyCFunction)text_gettext},
1096 {"move", (PyCFunction)text_move},
1097 {"replace", (PyCFunction)text_replace},
1098 {"setactive", (PyCFunction)text_setactive},
1099 {"setfocus", (PyCFunction)text_setfocus},
1100 {"settext", (PyCFunction)text_settext},
1101 {"setview", (PyCFunction)text_setview},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001102 {NULL, NULL} /* sentinel */
1103};
1104
Barry Warsawc2feec31996-12-12 22:48:20 +00001105static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001106text_getattr(tp, name)
1107 textobject *tp;
1108 char *name;
1109{
Barry Warsawc2feec31996-12-12 22:48:20 +00001110 PyObject *v = NULL;
Guido van Rossum77b46041992-01-14 18:41:24 +00001111 if (tp->t_ref == NULL) {
Barry Warsawc2feec31996-12-12 22:48:20 +00001112 PyErr_SetString(StdwinError, "text object already closed");
Guido van Rossum77b46041992-01-14 18:41:24 +00001113 return NULL;
1114 }
Guido van Rossum85f50761991-10-20 20:22:50 +00001115 if (strcmp(name, "__dict__") == 0) {
1116 v = tp->t_attr;
1117 if (v == NULL)
Barry Warsawc2feec31996-12-12 22:48:20 +00001118 v = Py_None;
Guido van Rossum85f50761991-10-20 20:22:50 +00001119 }
1120 else if (tp->t_attr != NULL) {
Barry Warsawc2feec31996-12-12 22:48:20 +00001121 v = PyDict_GetItemString(tp->t_attr, name);
Guido van Rossum85f50761991-10-20 20:22:50 +00001122 }
1123 if (v != NULL) {
Barry Warsawc2feec31996-12-12 22:48:20 +00001124 Py_INCREF(v);
Guido van Rossum85f50761991-10-20 20:22:50 +00001125 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001126 }
Barry Warsawc2feec31996-12-12 22:48:20 +00001127 return Py_FindMethod(text_methods, (PyObject *)tp, name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001128}
1129
1130static int
1131text_setattr(tp, name, v)
1132 textobject *tp;
1133 char *name;
Barry Warsawc2feec31996-12-12 22:48:20 +00001134 PyObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001135{
1136 if (tp->t_attr == NULL) {
Barry Warsawc2feec31996-12-12 22:48:20 +00001137 tp->t_attr = PyDict_New();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001138 if (tp->t_attr == NULL)
1139 return -1;
1140 }
Guido van Rossum94472a01992-09-04 09:45:18 +00001141 if (v == NULL) {
Barry Warsawc2feec31996-12-12 22:48:20 +00001142 int rv = PyDict_DelItemString(tp->t_attr, name);
Guido van Rossum94472a01992-09-04 09:45:18 +00001143 if (rv < 0)
Barry Warsawc2feec31996-12-12 22:48:20 +00001144 PyErr_SetString(PyExc_AttributeError,
1145 "delete non-existing text object attribute");
Guido van Rossum94472a01992-09-04 09:45:18 +00001146 return rv;
1147 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001148 else
Barry Warsawc2feec31996-12-12 22:48:20 +00001149 return PyDict_SetItemString(tp->t_attr, name, v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001150}
1151
Barry Warsawc2feec31996-12-12 22:48:20 +00001152statichere PyTypeObject Texttype = {
1153 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001154 0, /*ob_size*/
1155 "textedit", /*tp_name*/
1156 sizeof(textobject), /*tp_size*/
1157 0, /*tp_itemsize*/
1158 /* methods */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001159 (destructor)text_dealloc, /*tp_dealloc*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001160 0, /*tp_print*/
Guido van Rossumb6775db1994-08-01 11:34:53 +00001161 (getattrfunc)text_getattr, /*tp_getattr*/
1162 (setattrfunc)text_setattr, /*tp_setattr*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001163 0, /*tp_compare*/
1164 0, /*tp_repr*/
1165};
1166
1167
1168/* Menu objects */
1169
Guido van Rossum2d14e211991-02-19 12:26:49 +00001170#define IDOFFSET 10 /* Menu IDs we use start here */
Guido van Rossum27201061991-04-16 08:43:03 +00001171#define MAXNMENU 200 /* Max #menus we allow */
Guido van Rossum2d14e211991-02-19 12:26:49 +00001172static menuobject *menulist[MAXNMENU];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001173
Barry Warsawc2feec31996-12-12 22:48:20 +00001174static menuobject *newmenuobject Py_PROTO((char *));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001175static menuobject *
1176newmenuobject(title)
Guido van Rossumfc58e581992-01-27 16:45:55 +00001177 char *title;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001178{
1179 int id;
1180 MENU *menu;
1181 menuobject *mp;
Guido van Rossum2d14e211991-02-19 12:26:49 +00001182 for (id = 0; id < MAXNMENU; id++) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001183 if (menulist[id] == NULL)
1184 break;
1185 }
Guido van Rossum27201061991-04-16 08:43:03 +00001186 if (id >= MAXNMENU) {
Barry Warsawc2feec31996-12-12 22:48:20 +00001187 PyErr_SetString(StdwinError, "creating too many menus");
Guido van Rossum27201061991-04-16 08:43:03 +00001188 return NULL;
1189 }
Guido van Rossumfc58e581992-01-27 16:45:55 +00001190 menu = wmenucreate(id + IDOFFSET, title);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001191 if (menu == NULL)
Barry Warsawc2feec31996-12-12 22:48:20 +00001192 return (menuobject *) PyErr_NoMemory();
1193 mp = PyObject_NEW(menuobject, &Menutype);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001194 if (mp != NULL) {
1195 mp->m_menu = menu;
Guido van Rossum2d14e211991-02-19 12:26:49 +00001196 mp->m_id = id + IDOFFSET;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001197 mp->m_attr = NULL;
1198 menulist[id] = mp;
1199 }
1200 else
1201 wmenudelete(menu);
1202 return mp;
1203}
1204
1205/* Menu methods */
1206
1207static void
1208menu_dealloc(mp)
1209 menuobject *mp;
1210{
1211
Guido van Rossum2d14e211991-02-19 12:26:49 +00001212 int id = mp->m_id - IDOFFSET;
1213 if (id >= 0 && id < MAXNMENU && menulist[id] == mp) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001214 menulist[id] = NULL;
1215 }
Guido van Rossum77b46041992-01-14 18:41:24 +00001216 if (mp->m_menu != NULL)
1217 wmenudelete(mp->m_menu);
Barry Warsawc2feec31996-12-12 22:48:20 +00001218 Py_XDECREF(mp->m_attr);
1219 PyMem_DEL(mp);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001220}
1221
Barry Warsawc2feec31996-12-12 22:48:20 +00001222static PyObject *
Guido van Rossum77b46041992-01-14 18:41:24 +00001223menu_close(mp, args)
1224 menuobject *mp;
Barry Warsawc2feec31996-12-12 22:48:20 +00001225 PyObject *args;
Guido van Rossum77b46041992-01-14 18:41:24 +00001226{
1227 int id = mp->m_id - IDOFFSET;
1228 if (id >= 0 && id < MAXNMENU && menulist[id] == mp) {
1229 menulist[id] = NULL;
1230 }
1231 mp->m_id = -1;
1232 if (mp->m_menu != NULL)
1233 wmenudelete(mp->m_menu);
1234 mp->m_menu = NULL;
Barry Warsawc2feec31996-12-12 22:48:20 +00001235 Py_XDECREF(mp->m_attr);
Guido van Rossum77b46041992-01-14 18:41:24 +00001236 mp->m_attr = NULL;
Barry Warsawc2feec31996-12-12 22:48:20 +00001237 Py_INCREF(Py_None);
1238 return Py_None;
Guido van Rossum77b46041992-01-14 18:41:24 +00001239}
1240
Barry Warsawc2feec31996-12-12 22:48:20 +00001241static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001242menu_additem(self, args)
1243 menuobject *self;
Barry Warsawc2feec31996-12-12 22:48:20 +00001244 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001245{
Guido van Rossumfc58e581992-01-27 16:45:55 +00001246 char *text;
1247 int shortcut = -1;
Barry Warsawc2feec31996-12-12 22:48:20 +00001248 if (PyTuple_Check(args)) {
Guido van Rossumfc58e581992-01-27 16:45:55 +00001249 char c;
Barry Warsawc2feec31996-12-12 22:48:20 +00001250 if (!PyArg_Parse(args, "(sc)", &text, &c))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001251 return NULL;
Guido van Rossumfc58e581992-01-27 16:45:55 +00001252 shortcut = c;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001253 }
Guido van Rossumfc58e581992-01-27 16:45:55 +00001254 else if (!getstrarg(args, &text))
1255 return NULL;
1256 wmenuadditem(self->m_menu, text, shortcut);
Barry Warsawc2feec31996-12-12 22:48:20 +00001257 Py_INCREF(Py_None);
1258 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001259}
1260
Barry Warsawc2feec31996-12-12 22:48:20 +00001261static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001262menu_setitem(self, args)
1263 menuobject *self;
Barry Warsawc2feec31996-12-12 22:48:20 +00001264 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001265{
1266 int index;
Guido van Rossumfc58e581992-01-27 16:45:55 +00001267 char *text;
Barry Warsawc2feec31996-12-12 22:48:20 +00001268 if (!PyArg_Parse(args, "(is)", &index, &text))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001269 return NULL;
Guido van Rossumfc58e581992-01-27 16:45:55 +00001270 wmenusetitem(self->m_menu, index, text);
Barry Warsawc2feec31996-12-12 22:48:20 +00001271 Py_INCREF(Py_None);
1272 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001273}
1274
Barry Warsawc2feec31996-12-12 22:48:20 +00001275static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001276menu_enable(self, args)
1277 menuobject *self;
Barry Warsawc2feec31996-12-12 22:48:20 +00001278 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001279{
1280 int index;
1281 int flag;
Barry Warsawc2feec31996-12-12 22:48:20 +00001282 if (!PyArg_Parse(args, "(ii)", &index, &flag))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001283 return NULL;
1284 wmenuenable(self->m_menu, index, flag);
Barry Warsawc2feec31996-12-12 22:48:20 +00001285 Py_INCREF(Py_None);
1286 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001287}
1288
Barry Warsawc2feec31996-12-12 22:48:20 +00001289static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001290menu_check(self, args)
1291 menuobject *self;
Barry Warsawc2feec31996-12-12 22:48:20 +00001292 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001293{
1294 int index;
1295 int flag;
Barry Warsawc2feec31996-12-12 22:48:20 +00001296 if (!PyArg_Parse(args, "(ii)", &index, &flag))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001297 return NULL;
1298 wmenucheck(self->m_menu, index, flag);
Barry Warsawc2feec31996-12-12 22:48:20 +00001299 Py_INCREF(Py_None);
1300 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001301}
1302
Barry Warsawc2feec31996-12-12 22:48:20 +00001303static PyMethodDef menu_methods[] = {
1304 {"additem", (PyCFunction)menu_additem},
1305 {"setitem", (PyCFunction)menu_setitem},
1306 {"enable", (PyCFunction)menu_enable},
1307 {"check", (PyCFunction)menu_check},
1308 {"close", (PyCFunction)menu_close},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001309 {NULL, NULL} /* sentinel */
1310};
1311
Barry Warsawc2feec31996-12-12 22:48:20 +00001312static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001313menu_getattr(mp, name)
1314 menuobject *mp;
1315 char *name;
1316{
Barry Warsawc2feec31996-12-12 22:48:20 +00001317 PyObject *v = NULL;
Guido van Rossum77b46041992-01-14 18:41:24 +00001318 if (mp->m_menu == NULL) {
Barry Warsawc2feec31996-12-12 22:48:20 +00001319 PyErr_SetString(StdwinError, "menu object already closed");
Guido van Rossum77b46041992-01-14 18:41:24 +00001320 return NULL;
1321 }
Guido van Rossum85f50761991-10-20 20:22:50 +00001322 if (strcmp(name, "__dict__") == 0) {
1323 v = mp->m_attr;
1324 if (v == NULL)
Barry Warsawc2feec31996-12-12 22:48:20 +00001325 v = Py_None;
Guido van Rossum85f50761991-10-20 20:22:50 +00001326 }
1327 else if (mp->m_attr != NULL) {
Barry Warsawc2feec31996-12-12 22:48:20 +00001328 v = PyDict_GetItemString(mp->m_attr, name);
Guido van Rossum85f50761991-10-20 20:22:50 +00001329 }
1330 if (v != NULL) {
Barry Warsawc2feec31996-12-12 22:48:20 +00001331 Py_INCREF(v);
Guido van Rossum85f50761991-10-20 20:22:50 +00001332 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001333 }
Barry Warsawc2feec31996-12-12 22:48:20 +00001334 return Py_FindMethod(menu_methods, (PyObject *)mp, name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001335}
1336
1337static int
1338menu_setattr(mp, name, v)
1339 menuobject *mp;
1340 char *name;
Barry Warsawc2feec31996-12-12 22:48:20 +00001341 PyObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001342{
1343 if (mp->m_attr == NULL) {
Barry Warsawc2feec31996-12-12 22:48:20 +00001344 mp->m_attr = PyDict_New();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001345 if (mp->m_attr == NULL)
1346 return -1;
1347 }
Guido van Rossum94472a01992-09-04 09:45:18 +00001348 if (v == NULL) {
Barry Warsawc2feec31996-12-12 22:48:20 +00001349 int rv = PyDict_DelItemString(mp->m_attr, name);
Guido van Rossum94472a01992-09-04 09:45:18 +00001350 if (rv < 0)
Barry Warsawc2feec31996-12-12 22:48:20 +00001351 PyErr_SetString(PyExc_AttributeError,
1352 "delete non-existing menu object attribute");
Guido van Rossum94472a01992-09-04 09:45:18 +00001353 return rv;
1354 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001355 else
Barry Warsawc2feec31996-12-12 22:48:20 +00001356 return PyDict_SetItemString(mp->m_attr, name, v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001357}
1358
Barry Warsawc2feec31996-12-12 22:48:20 +00001359statichere PyTypeObject Menutype = {
1360 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001361 0, /*ob_size*/
1362 "menu", /*tp_name*/
1363 sizeof(menuobject), /*tp_size*/
1364 0, /*tp_itemsize*/
1365 /* methods */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001366 (destructor)menu_dealloc, /*tp_dealloc*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001367 0, /*tp_print*/
Guido van Rossumb6775db1994-08-01 11:34:53 +00001368 (getattrfunc)menu_getattr, /*tp_getattr*/
1369 (setattrfunc)menu_setattr, /*tp_setattr*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001370 0, /*tp_compare*/
1371 0, /*tp_repr*/
1372};
1373
1374
Guido van Rossume9066061993-07-29 13:14:32 +00001375#ifdef HAVE_BITMAPS
1376
Guido van Rossumbf80e541993-02-08 15:49:17 +00001377/* Bitmaps objects */
1378
Barry Warsawc2feec31996-12-12 22:48:20 +00001379static bitmapobject *newbitmapobject Py_PROTO((int, int));
Guido van Rossumbf80e541993-02-08 15:49:17 +00001380static bitmapobject *
1381newbitmapobject(width, height)
1382 int width, height;
1383{
1384 BITMAP *bitmap;
1385 bitmapobject *bp;
1386 bitmap = wnewbitmap(width, height);
1387 if (bitmap == NULL)
Barry Warsawc2feec31996-12-12 22:48:20 +00001388 return (bitmapobject *) PyErr_NoMemory();
1389 bp = PyObject_NEW(bitmapobject, &Bitmaptype);
Guido van Rossumbf80e541993-02-08 15:49:17 +00001390 if (bp != NULL) {
1391 bp->b_bitmap = bitmap;
1392 bp->b_attr = NULL;
1393 }
1394 else
1395 wfreebitmap(bitmap);
1396 return bp;
1397}
1398
1399/* Bitmap methods */
1400
1401static void
1402bitmap_dealloc(bp)
1403 bitmapobject *bp;
1404{
1405 if (bp->b_bitmap != NULL)
1406 wfreebitmap(bp->b_bitmap);
Barry Warsawc2feec31996-12-12 22:48:20 +00001407 Py_XDECREF(bp->b_attr);
1408 PyMem_DEL(bp);
Guido van Rossumbf80e541993-02-08 15:49:17 +00001409}
1410
Barry Warsawc2feec31996-12-12 22:48:20 +00001411static PyObject *
Guido van Rossumbf80e541993-02-08 15:49:17 +00001412bitmap_close(bp, args)
1413 bitmapobject *bp;
Barry Warsawc2feec31996-12-12 22:48:20 +00001414 PyObject *args;
Guido van Rossumbf80e541993-02-08 15:49:17 +00001415{
1416 if (bp->b_bitmap != NULL)
1417 wfreebitmap(bp->b_bitmap);
1418 bp->b_bitmap = NULL;
Barry Warsawc2feec31996-12-12 22:48:20 +00001419 Py_XDECREF(bp->b_attr);
Guido van Rossumbf80e541993-02-08 15:49:17 +00001420 bp->b_attr = NULL;
Barry Warsawc2feec31996-12-12 22:48:20 +00001421 Py_INCREF(Py_None);
1422 return Py_None;
Guido van Rossumbf80e541993-02-08 15:49:17 +00001423}
1424
Barry Warsawc2feec31996-12-12 22:48:20 +00001425static PyObject *
Guido van Rossumbf80e541993-02-08 15:49:17 +00001426bitmap_setbit(self, args)
1427 bitmapobject *self;
Barry Warsawc2feec31996-12-12 22:48:20 +00001428 PyObject *args;
Guido van Rossumbf80e541993-02-08 15:49:17 +00001429{
1430 int a[3];
1431 if (!getpointintarg(args, a))
1432 return NULL;
1433 wsetbit(self->b_bitmap, a[0], a[1], a[2]);
Barry Warsawc2feec31996-12-12 22:48:20 +00001434 Py_INCREF(Py_None);
1435 return Py_None;
Guido van Rossumbf80e541993-02-08 15:49:17 +00001436}
1437
Barry Warsawc2feec31996-12-12 22:48:20 +00001438static PyObject *
Guido van Rossumbf80e541993-02-08 15:49:17 +00001439bitmap_getbit(self, args)
1440 bitmapobject *self;
Barry Warsawc2feec31996-12-12 22:48:20 +00001441 PyObject *args;
Guido van Rossumbf80e541993-02-08 15:49:17 +00001442{
1443 int a[2];
1444 if (!getpointarg(args, a))
1445 return NULL;
Barry Warsawc2feec31996-12-12 22:48:20 +00001446 return PyInt_FromLong((long) wgetbit(self->b_bitmap, a[0], a[1]));
Guido van Rossumbf80e541993-02-08 15:49:17 +00001447}
1448
Barry Warsawc2feec31996-12-12 22:48:20 +00001449static PyObject *
Guido van Rossumbf80e541993-02-08 15:49:17 +00001450bitmap_getsize(self, args)
1451 bitmapobject *self;
Barry Warsawc2feec31996-12-12 22:48:20 +00001452 PyObject *args;
Guido van Rossumbf80e541993-02-08 15:49:17 +00001453{
1454 int width, height;
Barry Warsawc2feec31996-12-12 22:48:20 +00001455 if (!PyArg_NoArgs(args))
Guido van Rossumbf80e541993-02-08 15:49:17 +00001456 return NULL;
1457 wgetbitmapsize(self->b_bitmap, &width, &height);
Barry Warsawc2feec31996-12-12 22:48:20 +00001458 return Py_BuildValue("(ii)", width, height);
Guido van Rossumbf80e541993-02-08 15:49:17 +00001459}
1460
Barry Warsawc2feec31996-12-12 22:48:20 +00001461static PyMethodDef bitmap_methods[] = {
1462 {"close", (PyCFunction)bitmap_close},
1463 {"getsize", (PyCFunction)bitmap_getsize},
1464 {"getbit", (PyCFunction)bitmap_getbit},
1465 {"setbit", (PyCFunction)bitmap_setbit},
Guido van Rossumbf80e541993-02-08 15:49:17 +00001466 {NULL, NULL} /* sentinel */
1467};
1468
Barry Warsawc2feec31996-12-12 22:48:20 +00001469static PyObject *
Guido van Rossumbf80e541993-02-08 15:49:17 +00001470bitmap_getattr(bp, name)
1471 bitmapobject *bp;
1472 char *name;
1473{
Barry Warsawc2feec31996-12-12 22:48:20 +00001474 PyObject *v = NULL;
Guido van Rossumbf80e541993-02-08 15:49:17 +00001475 if (bp->b_bitmap == NULL) {
Barry Warsawc2feec31996-12-12 22:48:20 +00001476 PyErr_SetString(StdwinError, "bitmap object already closed");
Guido van Rossumbf80e541993-02-08 15:49:17 +00001477 return NULL;
1478 }
1479 if (strcmp(name, "__dict__") == 0) {
1480 v = bp->b_attr;
1481 if (v == NULL)
Barry Warsawc2feec31996-12-12 22:48:20 +00001482 v = Py_None;
Guido van Rossumbf80e541993-02-08 15:49:17 +00001483 }
1484 else if (bp->b_attr != NULL) {
Barry Warsawc2feec31996-12-12 22:48:20 +00001485 v = PyDict_GetItemString(bp->b_attr, name);
Guido van Rossumbf80e541993-02-08 15:49:17 +00001486 }
1487 if (v != NULL) {
Barry Warsawc2feec31996-12-12 22:48:20 +00001488 Py_INCREF(v);
Guido van Rossumbf80e541993-02-08 15:49:17 +00001489 return v;
1490 }
Barry Warsawc2feec31996-12-12 22:48:20 +00001491 return Py_FindMethod(bitmap_methods, (PyObject *)bp, name);
Guido van Rossumbf80e541993-02-08 15:49:17 +00001492}
1493
1494static int
1495bitmap_setattr(bp, name, v)
1496 bitmapobject *bp;
1497 char *name;
Barry Warsawc2feec31996-12-12 22:48:20 +00001498 PyObject *v;
Guido van Rossumbf80e541993-02-08 15:49:17 +00001499{
1500 if (bp->b_attr == NULL) {
Barry Warsawc2feec31996-12-12 22:48:20 +00001501 bp->b_attr = PyDict_New();
Guido van Rossumbf80e541993-02-08 15:49:17 +00001502 if (bp->b_attr == NULL)
1503 return -1;
1504 }
1505 if (v == NULL) {
Barry Warsawc2feec31996-12-12 22:48:20 +00001506 int rv = PyDict_DelItemString(bp->b_attr, name);
Guido van Rossumbf80e541993-02-08 15:49:17 +00001507 if (rv < 0)
Barry Warsawc2feec31996-12-12 22:48:20 +00001508 PyErr_SetString(PyExc_AttributeError,
Guido van Rossumbf80e541993-02-08 15:49:17 +00001509 "delete non-existing bitmap object attribute");
1510 return rv;
1511 }
1512 else
Barry Warsawc2feec31996-12-12 22:48:20 +00001513 return PyDict_SetItemString(bp->b_attr, name, v);
Guido van Rossumbf80e541993-02-08 15:49:17 +00001514}
1515
Barry Warsawc2feec31996-12-12 22:48:20 +00001516statichere PyTypeObject Bitmaptype = {
1517 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossumbf80e541993-02-08 15:49:17 +00001518 0, /*ob_size*/
1519 "bitmap", /*tp_name*/
1520 sizeof(bitmapobject), /*tp_size*/
1521 0, /*tp_itemsize*/
1522 /* methods */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001523 (destructor)bitmap_dealloc, /*tp_dealloc*/
Guido van Rossumbf80e541993-02-08 15:49:17 +00001524 0, /*tp_print*/
Guido van Rossumb6775db1994-08-01 11:34:53 +00001525 (getattrfunc)bitmap_getattr, /*tp_getattr*/
1526 (setattrfunc)bitmap_setattr, /*tp_setattr*/
Guido van Rossumbf80e541993-02-08 15:49:17 +00001527 0, /*tp_compare*/
1528 0, /*tp_repr*/
1529};
1530
Guido van Rossume9066061993-07-29 13:14:32 +00001531#endif /* HAVE_BITMAPS */
1532
Guido van Rossumbf80e541993-02-08 15:49:17 +00001533
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001534/* Windows */
1535
1536#define MAXNWIN 50
1537static windowobject *windowlist[MAXNWIN];
1538
1539/* Window methods */
1540
1541static void
1542window_dealloc(wp)
1543 windowobject *wp;
1544{
1545 if (wp->w_win != NULL) {
1546 int tag = wgettag(wp->w_win);
1547 if (tag >= 0 && tag < MAXNWIN)
1548 windowlist[tag] = NULL;
1549 else
1550 fprintf(stderr, "XXX help! tag %d in window_dealloc\n",
1551 tag);
1552 wclose(wp->w_win);
1553 }
Barry Warsawc2feec31996-12-12 22:48:20 +00001554 Py_DECREF(wp->w_title);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001555 if (wp->w_attr != NULL)
Barry Warsawc2feec31996-12-12 22:48:20 +00001556 Py_DECREF(wp->w_attr);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001557 free((char *)wp);
1558}
1559
Barry Warsawc2feec31996-12-12 22:48:20 +00001560static PyObject *
Guido van Rossum3c284741991-11-27 14:54:54 +00001561window_close(wp, args)
1562 windowobject *wp;
Barry Warsawc2feec31996-12-12 22:48:20 +00001563 PyObject *args;
Guido van Rossum3c284741991-11-27 14:54:54 +00001564{
1565 if (wp->w_win != NULL) {
1566 int tag = wgettag(wp->w_win);
1567 if (tag >= 0 && tag < MAXNWIN)
1568 windowlist[tag] = NULL;
1569 wclose(wp->w_win);
1570 wp->w_win = NULL;
1571 }
Barry Warsawc2feec31996-12-12 22:48:20 +00001572 Py_INCREF(Py_None);
1573 return Py_None;
Guido van Rossum3c284741991-11-27 14:54:54 +00001574}
1575
Barry Warsawc2feec31996-12-12 22:48:20 +00001576static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001577window_begindrawing(wp, args)
1578 windowobject *wp;
Barry Warsawc2feec31996-12-12 22:48:20 +00001579 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001580{
1581 drawingobject *dp;
Barry Warsawc2feec31996-12-12 22:48:20 +00001582 if (!PyArg_NoArgs(args))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001583 return NULL;
1584 if (Drawing != NULL) {
Barry Warsawc2feec31996-12-12 22:48:20 +00001585 PyErr_SetString(StdwinError, "already drawing");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001586 return NULL;
1587 }
Barry Warsawc2feec31996-12-12 22:48:20 +00001588 dp = PyObject_NEW(drawingobject, &Drawingtype);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001589 if (dp == NULL)
1590 return NULL;
1591 Drawing = dp;
Barry Warsawc2feec31996-12-12 22:48:20 +00001592 Py_INCREF(wp);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001593 dp->d_ref = wp;
1594 wbegindrawing(wp->w_win);
Barry Warsawc2feec31996-12-12 22:48:20 +00001595 return (PyObject *)dp;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001596}
1597
Barry Warsawc2feec31996-12-12 22:48:20 +00001598static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001599window_change(wp, args)
1600 windowobject *wp;
Barry Warsawc2feec31996-12-12 22:48:20 +00001601 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001602{
1603 int a[4];
1604 if (!getrectarg(args, a))
1605 return NULL;
1606 wchange(wp->w_win, a[0], a[1], a[2], a[3]);
Barry Warsawc2feec31996-12-12 22:48:20 +00001607 Py_INCREF(Py_None);
1608 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001609}
1610
Barry Warsawc2feec31996-12-12 22:48:20 +00001611static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001612window_gettitle(wp, args)
1613 windowobject *wp;
Barry Warsawc2feec31996-12-12 22:48:20 +00001614 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001615{
Barry Warsawc2feec31996-12-12 22:48:20 +00001616 if (!PyArg_NoArgs(args))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001617 return NULL;
Barry Warsawc2feec31996-12-12 22:48:20 +00001618 Py_INCREF(wp->w_title);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001619 return wp->w_title;
1620}
1621
Barry Warsawc2feec31996-12-12 22:48:20 +00001622static PyObject *
Guido van Rossum541c8c01991-05-05 20:13:41 +00001623window_getwinpos(wp, args)
1624 windowobject *wp;
Barry Warsawc2feec31996-12-12 22:48:20 +00001625 PyObject *args;
Guido van Rossum541c8c01991-05-05 20:13:41 +00001626{
1627 int h, v;
Barry Warsawc2feec31996-12-12 22:48:20 +00001628 if (!PyArg_NoArgs(args))
Guido van Rossum541c8c01991-05-05 20:13:41 +00001629 return NULL;
1630 wgetwinpos(wp->w_win, &h, &v);
1631 return makepoint(h, v);
1632}
1633
Barry Warsawc2feec31996-12-12 22:48:20 +00001634static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001635window_getwinsize(wp, args)
1636 windowobject *wp;
Barry Warsawc2feec31996-12-12 22:48:20 +00001637 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001638{
1639 int width, height;
Barry Warsawc2feec31996-12-12 22:48:20 +00001640 if (!PyArg_NoArgs(args))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001641 return NULL;
1642 wgetwinsize(wp->w_win, &width, &height);
1643 return makepoint(width, height);
1644}
1645
Barry Warsawc2feec31996-12-12 22:48:20 +00001646static PyObject *
Guido van Rossumbf80e541993-02-08 15:49:17 +00001647window_setwinpos(wp, args)
1648 windowobject *wp;
Barry Warsawc2feec31996-12-12 22:48:20 +00001649 PyObject *args;
Guido van Rossumbf80e541993-02-08 15:49:17 +00001650{
1651 int a[2];
1652 if (!getpointarg(args, a))
1653 return NULL;
1654 wsetwinpos(wp->w_win, a[0], a[1]);
Barry Warsawc2feec31996-12-12 22:48:20 +00001655 Py_INCREF(Py_None);
1656 return Py_None;
Guido van Rossumbf80e541993-02-08 15:49:17 +00001657}
1658
Barry Warsawc2feec31996-12-12 22:48:20 +00001659static PyObject *
Guido van Rossumbf80e541993-02-08 15:49:17 +00001660window_setwinsize(wp, args)
1661 windowobject *wp;
Barry Warsawc2feec31996-12-12 22:48:20 +00001662 PyObject *args;
Guido van Rossumbf80e541993-02-08 15:49:17 +00001663{
1664 int a[2];
1665 if (!getpointarg(args, a))
1666 return NULL;
1667 wsetwinsize(wp->w_win, a[0], a[1]);
Barry Warsawc2feec31996-12-12 22:48:20 +00001668 Py_INCREF(Py_None);
1669 return Py_None;
Guido van Rossumbf80e541993-02-08 15:49:17 +00001670}
1671
Barry Warsawc2feec31996-12-12 22:48:20 +00001672static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001673window_getdocsize(wp, args)
1674 windowobject *wp;
Barry Warsawc2feec31996-12-12 22:48:20 +00001675 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001676{
1677 int width, height;
Barry Warsawc2feec31996-12-12 22:48:20 +00001678 if (!PyArg_NoArgs(args))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001679 return NULL;
1680 wgetdocsize(wp->w_win, &width, &height);
1681 return makepoint(width, height);
1682}
1683
Barry Warsawc2feec31996-12-12 22:48:20 +00001684static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001685window_getorigin(wp, args)
1686 windowobject *wp;
Barry Warsawc2feec31996-12-12 22:48:20 +00001687 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001688{
1689 int width, height;
Barry Warsawc2feec31996-12-12 22:48:20 +00001690 if (!PyArg_NoArgs(args))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001691 return NULL;
1692 wgetorigin(wp->w_win, &width, &height);
1693 return makepoint(width, height);
1694}
1695
Barry Warsawc2feec31996-12-12 22:48:20 +00001696static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001697window_scroll(wp, args)
1698 windowobject *wp;
Barry Warsawc2feec31996-12-12 22:48:20 +00001699 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001700{
1701 int a[6];
1702 if (!getrectpointarg(args, a))
1703 return NULL;
1704 wscroll(wp->w_win, a[0], a[1], a[2], a[3], a[4], a[5]);
Barry Warsawc2feec31996-12-12 22:48:20 +00001705 Py_INCREF(Py_None);
1706 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001707}
1708
Barry Warsawc2feec31996-12-12 22:48:20 +00001709static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001710window_setdocsize(wp, args)
1711 windowobject *wp;
Barry Warsawc2feec31996-12-12 22:48:20 +00001712 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001713{
1714 int a[2];
1715 if (!getpointarg(args, a))
1716 return NULL;
1717 wsetdocsize(wp->w_win, a[0], a[1]);
Barry Warsawc2feec31996-12-12 22:48:20 +00001718 Py_INCREF(Py_None);
1719 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001720}
1721
Barry Warsawc2feec31996-12-12 22:48:20 +00001722static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001723window_setorigin(wp, args)
1724 windowobject *wp;
Barry Warsawc2feec31996-12-12 22:48:20 +00001725 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001726{
1727 int a[2];
1728 if (!getpointarg(args, a))
1729 return NULL;
1730 wsetorigin(wp->w_win, a[0], a[1]);
Barry Warsawc2feec31996-12-12 22:48:20 +00001731 Py_INCREF(Py_None);
1732 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001733}
1734
Barry Warsawc2feec31996-12-12 22:48:20 +00001735static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001736window_settitle(wp, args)
1737 windowobject *wp;
Barry Warsawc2feec31996-12-12 22:48:20 +00001738 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001739{
Barry Warsawc2feec31996-12-12 22:48:20 +00001740 PyObject *title;
1741 if (!PyArg_Parse(args, "S", &title))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001742 return NULL;
Barry Warsawc2feec31996-12-12 22:48:20 +00001743 Py_DECREF(wp->w_title);
1744 Py_INCREF(title);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001745 wp->w_title = title;
Barry Warsawc2feec31996-12-12 22:48:20 +00001746 wsettitle(wp->w_win, PyString_AsString(title));
1747 Py_INCREF(Py_None);
1748 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001749}
1750
Barry Warsawc2feec31996-12-12 22:48:20 +00001751static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001752window_show(wp, args)
1753 windowobject *wp;
Barry Warsawc2feec31996-12-12 22:48:20 +00001754 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001755{
1756 int a[4];
1757 if (!getrectarg(args, a))
1758 return NULL;
1759 wshow(wp->w_win, a[0], a[1], a[2], a[3]);
Barry Warsawc2feec31996-12-12 22:48:20 +00001760 Py_INCREF(Py_None);
1761 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001762}
1763
Barry Warsawc2feec31996-12-12 22:48:20 +00001764static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001765window_settimer(wp, args)
1766 windowobject *wp;
Barry Warsawc2feec31996-12-12 22:48:20 +00001767 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001768{
1769 int a;
1770 if (!getintarg(args, &a))
1771 return NULL;
1772 wsettimer(wp->w_win, a);
Barry Warsawc2feec31996-12-12 22:48:20 +00001773 Py_INCREF(Py_None);
1774 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001775}
1776
Barry Warsawc2feec31996-12-12 22:48:20 +00001777static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001778window_menucreate(self, args)
1779 windowobject *self;
Barry Warsawc2feec31996-12-12 22:48:20 +00001780 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001781{
1782 menuobject *mp;
Guido van Rossumfc58e581992-01-27 16:45:55 +00001783 char *title;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001784 if (!getstrarg(args, &title))
1785 return NULL;
1786 wmenusetdeflocal(1);
1787 mp = newmenuobject(title);
1788 if (mp == NULL)
1789 return NULL;
1790 wmenuattach(self->w_win, mp->m_menu);
Barry Warsawc2feec31996-12-12 22:48:20 +00001791 return (PyObject *)mp;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001792}
1793
Barry Warsawc2feec31996-12-12 22:48:20 +00001794static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001795window_textcreate(self, args)
1796 windowobject *self;
Barry Warsawc2feec31996-12-12 22:48:20 +00001797 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001798{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001799 int a[4];
1800 if (!getrectarg(args, a))
1801 return NULL;
Barry Warsawc2feec31996-12-12 22:48:20 +00001802 return (PyObject *)newtextobject(self, a[0], a[1], a[2], a[3]);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001803}
1804
Barry Warsawc2feec31996-12-12 22:48:20 +00001805static PyObject *
Guido van Rossum5b10f451990-10-30 16:01:48 +00001806window_setselection(self, args)
1807 windowobject *self;
Barry Warsawc2feec31996-12-12 22:48:20 +00001808 PyObject *args;
Guido van Rossum5b10f451990-10-30 16:01:48 +00001809{
Guido van Rossumfc58e581992-01-27 16:45:55 +00001810 int sel, size, ok;
1811 char *text;
Barry Warsawc2feec31996-12-12 22:48:20 +00001812 if (!PyArg_Parse(args, "(is#)", &sel, &text, &size))
Guido van Rossum5b10f451990-10-30 16:01:48 +00001813 return NULL;
Guido van Rossumfc58e581992-01-27 16:45:55 +00001814 ok = wsetselection(self->w_win, sel, text, size);
Barry Warsawc2feec31996-12-12 22:48:20 +00001815 return PyInt_FromLong(ok);
Guido van Rossum5b10f451990-10-30 16:01:48 +00001816}
1817
Barry Warsawc2feec31996-12-12 22:48:20 +00001818static PyObject *
Guido van Rossum5b10f451990-10-30 16:01:48 +00001819window_setwincursor(self, args)
1820 windowobject *self;
Barry Warsawc2feec31996-12-12 22:48:20 +00001821 PyObject *args;
Guido van Rossum5b10f451990-10-30 16:01:48 +00001822{
Guido van Rossumfc58e581992-01-27 16:45:55 +00001823 char *name;
Guido van Rossum5b10f451990-10-30 16:01:48 +00001824 CURSOR *c;
Barry Warsawc2feec31996-12-12 22:48:20 +00001825 if (!PyArg_Parse(args, "z", &name))
Guido van Rossum5b10f451990-10-30 16:01:48 +00001826 return NULL;
Guido van Rossum3c8ba7a1992-02-05 11:15:00 +00001827 if (name == NULL)
1828 c = NULL;
1829 else {
1830 c = wfetchcursor(name);
1831 if (c == NULL) {
Barry Warsawc2feec31996-12-12 22:48:20 +00001832 PyErr_SetString(StdwinError, "no such cursor");
Guido van Rossum3c8ba7a1992-02-05 11:15:00 +00001833 return NULL;
1834 }
Guido van Rossum5b10f451990-10-30 16:01:48 +00001835 }
1836 wsetwincursor(self->w_win, c);
Barry Warsawc2feec31996-12-12 22:48:20 +00001837 Py_INCREF(Py_None);
1838 return Py_None;
Guido van Rossum5b10f451990-10-30 16:01:48 +00001839}
1840
Barry Warsawc2feec31996-12-12 22:48:20 +00001841static PyObject *
Guido van Rossumfc58e581992-01-27 16:45:55 +00001842window_setactive(self, args)
1843 windowobject *self;
Barry Warsawc2feec31996-12-12 22:48:20 +00001844 PyObject *args;
Guido van Rossumfc58e581992-01-27 16:45:55 +00001845{
Barry Warsawc2feec31996-12-12 22:48:20 +00001846 if (!PyArg_NoArgs(args))
Guido van Rossumfc58e581992-01-27 16:45:55 +00001847 return NULL;
1848 wsetactive(self->w_win);
Barry Warsawc2feec31996-12-12 22:48:20 +00001849 Py_INCREF(Py_None);
1850 return Py_None;
Guido van Rossumfc58e581992-01-27 16:45:55 +00001851}
1852
Guido van Rossum8dcbbac1991-07-27 21:42:24 +00001853#ifdef CWI_HACKS
Barry Warsawc2feec31996-12-12 22:48:20 +00001854static PyObject *
Guido van Rossum8dcbbac1991-07-27 21:42:24 +00001855window_getxwindowid(self, args)
1856 windowobject *self;
Barry Warsawc2feec31996-12-12 22:48:20 +00001857 PyObject *args;
Guido van Rossum8dcbbac1991-07-27 21:42:24 +00001858{
1859 long wid = wgetxwindowid(self->w_win);
Barry Warsawc2feec31996-12-12 22:48:20 +00001860 return PyInt_FromLong(wid);
Guido van Rossum8dcbbac1991-07-27 21:42:24 +00001861}
1862#endif
1863
Barry Warsawc2feec31996-12-12 22:48:20 +00001864static PyMethodDef window_methods[] = {
1865 {"begindrawing",(PyCFunction)window_begindrawing},
1866 {"change", (PyCFunction)window_change},
1867 {"close", (PyCFunction)window_close},
1868 {"getdocsize", (PyCFunction)window_getdocsize},
1869 {"getorigin", (PyCFunction)window_getorigin},
1870 {"gettitle", (PyCFunction)window_gettitle},
1871 {"getwinpos", (PyCFunction)window_getwinpos},
1872 {"getwinsize", (PyCFunction)window_getwinsize},
1873 {"menucreate", (PyCFunction)window_menucreate},
1874 {"scroll", (PyCFunction)window_scroll},
1875 {"setactive", (PyCFunction)window_setactive},
1876 {"setdocsize", (PyCFunction)window_setdocsize},
1877 {"setorigin", (PyCFunction)window_setorigin},
1878 {"setselection",(PyCFunction)window_setselection},
1879 {"settimer", (PyCFunction)window_settimer},
1880 {"settitle", (PyCFunction)window_settitle},
1881 {"setwincursor",(PyCFunction)window_setwincursor},
1882 {"setwinpos", (PyCFunction)window_setwinpos},
1883 {"setwinsize", (PyCFunction)window_setwinsize},
1884 {"show", (PyCFunction)window_show},
1885 {"textcreate", (PyCFunction)window_textcreate},
Guido van Rossum8dcbbac1991-07-27 21:42:24 +00001886#ifdef CWI_HACKS
Barry Warsawc2feec31996-12-12 22:48:20 +00001887 {"getxwindowid",(PyCFunction)window_getxwindowid},
Guido van Rossum8dcbbac1991-07-27 21:42:24 +00001888#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001889 {NULL, NULL} /* sentinel */
1890};
1891
Barry Warsawc2feec31996-12-12 22:48:20 +00001892static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001893window_getattr(wp, name)
1894 windowobject *wp;
1895 char *name;
1896{
Barry Warsawc2feec31996-12-12 22:48:20 +00001897 PyObject *v = NULL;
Guido van Rossum77b46041992-01-14 18:41:24 +00001898 if (wp->w_win == NULL) {
Barry Warsawc2feec31996-12-12 22:48:20 +00001899 PyErr_SetString(StdwinError, "window already closed");
Guido van Rossum77b46041992-01-14 18:41:24 +00001900 return NULL;
1901 }
Guido van Rossum85f50761991-10-20 20:22:50 +00001902 if (strcmp(name, "__dict__") == 0) {
1903 v = wp->w_attr;
1904 if (v == NULL)
Barry Warsawc2feec31996-12-12 22:48:20 +00001905 v = Py_None;
Guido van Rossum85f50761991-10-20 20:22:50 +00001906 }
1907 else if (wp->w_attr != NULL) {
Barry Warsawc2feec31996-12-12 22:48:20 +00001908 v = PyDict_GetItemString(wp->w_attr, name);
Guido van Rossum85f50761991-10-20 20:22:50 +00001909 }
1910 if (v != NULL) {
Barry Warsawc2feec31996-12-12 22:48:20 +00001911 Py_INCREF(v);
Guido van Rossum85f50761991-10-20 20:22:50 +00001912 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001913 }
Barry Warsawc2feec31996-12-12 22:48:20 +00001914 return Py_FindMethod(window_methods, (PyObject *)wp, name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001915}
1916
1917static int
1918window_setattr(wp, name, v)
1919 windowobject *wp;
1920 char *name;
Barry Warsawc2feec31996-12-12 22:48:20 +00001921 PyObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001922{
1923 if (wp->w_attr == NULL) {
Barry Warsawc2feec31996-12-12 22:48:20 +00001924 wp->w_attr = PyDict_New();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001925 if (wp->w_attr == NULL)
1926 return -1;
1927 }
Guido van Rossum94472a01992-09-04 09:45:18 +00001928 if (v == NULL) {
Barry Warsawc2feec31996-12-12 22:48:20 +00001929 int rv = PyDict_DelItemString(wp->w_attr, name);
Guido van Rossum94472a01992-09-04 09:45:18 +00001930 if (rv < 0)
Barry Warsawc2feec31996-12-12 22:48:20 +00001931 PyErr_SetString(PyExc_AttributeError,
1932 "delete non-existing menu object attribute");
Guido van Rossum94472a01992-09-04 09:45:18 +00001933 return rv;
1934 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001935 else
Barry Warsawc2feec31996-12-12 22:48:20 +00001936 return PyDict_SetItemString(wp->w_attr, name, v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001937}
1938
Barry Warsawc2feec31996-12-12 22:48:20 +00001939statichere PyTypeObject Windowtype = {
1940 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001941 0, /*ob_size*/
1942 "window", /*tp_name*/
1943 sizeof(windowobject), /*tp_size*/
1944 0, /*tp_itemsize*/
1945 /* methods */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001946 (destructor)window_dealloc, /*tp_dealloc*/
Guido van Rossum7066dd71992-09-17 17:54:56 +00001947 0, /*tp_print*/
Guido van Rossumb6775db1994-08-01 11:34:53 +00001948 (getattrfunc)window_getattr, /*tp_getattr*/
1949 (setattrfunc)window_setattr, /*tp_setattr*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001950 0, /*tp_compare*/
1951 0, /*tp_repr*/
1952};
1953
1954/* Stdwin methods */
1955
Barry Warsawc2feec31996-12-12 22:48:20 +00001956static PyObject *
Guido van Rossumcacd9571993-10-18 11:44:47 +00001957stdwin_done(sw, args)
Barry Warsawc2feec31996-12-12 22:48:20 +00001958 PyObject *sw;
1959 PyObject *args;
Guido van Rossumcacd9571993-10-18 11:44:47 +00001960{
Barry Warsawc2feec31996-12-12 22:48:20 +00001961 if (!PyArg_NoArgs(args))
Guido van Rossumcacd9571993-10-18 11:44:47 +00001962 return NULL;
1963 wdone();
1964 /* XXX There is no protection against continued use of
1965 XXX stdwin functions or objects after this call is made.
1966 XXX Use at own risk */
Barry Warsawc2feec31996-12-12 22:48:20 +00001967 Py_INCREF(Py_None);
1968 return Py_None;
Guido van Rossumcacd9571993-10-18 11:44:47 +00001969}
1970
Barry Warsawc2feec31996-12-12 22:48:20 +00001971static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001972stdwin_open(sw, args)
Barry Warsawc2feec31996-12-12 22:48:20 +00001973 PyObject *sw;
1974 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001975{
1976 int tag;
Barry Warsawc2feec31996-12-12 22:48:20 +00001977 PyObject *title;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001978 windowobject *wp;
Barry Warsawc2feec31996-12-12 22:48:20 +00001979 if (!PyArg_Parse(args, "S", &title))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001980 return NULL;
1981 for (tag = 0; tag < MAXNWIN; tag++) {
1982 if (windowlist[tag] == NULL)
1983 break;
1984 }
Guido van Rossum27201061991-04-16 08:43:03 +00001985 if (tag >= MAXNWIN) {
Barry Warsawc2feec31996-12-12 22:48:20 +00001986 PyErr_SetString(StdwinError, "creating too many windows");
Guido van Rossum27201061991-04-16 08:43:03 +00001987 return NULL;
1988 }
Barry Warsawc2feec31996-12-12 22:48:20 +00001989 wp = PyObject_NEW(windowobject, &Windowtype);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001990 if (wp == NULL)
1991 return NULL;
Barry Warsawc2feec31996-12-12 22:48:20 +00001992 Py_INCREF(title);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001993 wp->w_title = title;
Barry Warsawc2feec31996-12-12 22:48:20 +00001994 wp->w_win = wopen(PyString_AsString(title), (void (*)()) NULL);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001995 wp->w_attr = NULL;
1996 if (wp->w_win == NULL) {
Barry Warsawc2feec31996-12-12 22:48:20 +00001997 Py_DECREF(wp);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001998 return NULL;
1999 }
2000 windowlist[tag] = wp;
2001 wsettag(wp->w_win, tag);
Barry Warsawc2feec31996-12-12 22:48:20 +00002002 return (PyObject *)wp;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002003}
2004
Barry Warsawc2feec31996-12-12 22:48:20 +00002005static PyObject *
Guido van Rossum246b9d81991-06-03 10:55:14 +00002006window2object(win)
2007 WINDOW *win;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002008{
Barry Warsawc2feec31996-12-12 22:48:20 +00002009 PyObject *w;
Guido van Rossum246b9d81991-06-03 10:55:14 +00002010 if (win == NULL)
Barry Warsawc2feec31996-12-12 22:48:20 +00002011 w = Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002012 else {
Guido van Rossum246b9d81991-06-03 10:55:14 +00002013 int tag = wgettag(win);
2014 if (tag < 0 || tag >= MAXNWIN || windowlist[tag] == NULL ||
2015 windowlist[tag]->w_win != win)
Barry Warsawc2feec31996-12-12 22:48:20 +00002016 w = Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002017 else
Barry Warsawc2feec31996-12-12 22:48:20 +00002018 w = (PyObject *)windowlist[tag];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002019 }
Barry Warsawc2feec31996-12-12 22:48:20 +00002020 Py_INCREF(w);
Guido van Rossum246b9d81991-06-03 10:55:14 +00002021 return w;
2022}
2023
Barry Warsawc2feec31996-12-12 22:48:20 +00002024static PyObject *
Guido van Rossum246b9d81991-06-03 10:55:14 +00002025stdwin_get_poll_event(poll, args)
2026 int poll;
Barry Warsawc2feec31996-12-12 22:48:20 +00002027 PyObject *args;
Guido van Rossum246b9d81991-06-03 10:55:14 +00002028{
2029 EVENT e;
Barry Warsawc2feec31996-12-12 22:48:20 +00002030 PyObject *u, *v, *w;
2031 if (!PyArg_NoArgs(args))
Guido van Rossum246b9d81991-06-03 10:55:14 +00002032 return NULL;
2033 if (Drawing != NULL) {
Barry Warsawc2feec31996-12-12 22:48:20 +00002034 PyErr_SetString(StdwinError,
2035 "cannot getevent() while drawing");
Guido van Rossum246b9d81991-06-03 10:55:14 +00002036 return NULL;
2037 }
2038 again:
Guido van Rossumff4949e1992-08-05 19:58:53 +00002039 BGN_STDWIN
Guido van Rossum246b9d81991-06-03 10:55:14 +00002040 if (poll) {
2041 if (!wpollevent(&e)) {
Guido van Rossumff4949e1992-08-05 19:58:53 +00002042 RET_STDWIN
Barry Warsawc2feec31996-12-12 22:48:20 +00002043 Py_INCREF(Py_None);
2044 return Py_None;
Guido van Rossum246b9d81991-06-03 10:55:14 +00002045 }
2046 }
2047 else
2048 wgetevent(&e);
Guido van Rossumff4949e1992-08-05 19:58:53 +00002049 END_STDWIN
Guido van Rossum246b9d81991-06-03 10:55:14 +00002050 if (e.type == WE_COMMAND && e.u.command == WC_CANCEL) {
2051 /* Turn keyboard interrupts into exceptions */
Barry Warsawc2feec31996-12-12 22:48:20 +00002052 PyErr_SetNone(PyExc_KeyboardInterrupt);
Guido van Rossum246b9d81991-06-03 10:55:14 +00002053 return NULL;
2054 }
2055 if (e.type == WE_COMMAND && e.u.command == WC_CLOSE) {
2056 /* Turn WC_CLOSE commands into WE_CLOSE events */
2057 e.type = WE_CLOSE;
2058 }
Guido van Rossum2ee12f41992-04-13 15:54:35 +00002059 v = window2object(e.window);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002060 switch (e.type) {
2061 case WE_CHAR:
2062 {
2063 char c[1];
2064 c[0] = e.u.character;
Barry Warsawc2feec31996-12-12 22:48:20 +00002065 w = PyString_FromStringAndSize(c, 1);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002066 }
2067 break;
2068 case WE_COMMAND:
Barry Warsawc2feec31996-12-12 22:48:20 +00002069 w = PyInt_FromLong((long)e.u.command);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002070 break;
2071 case WE_DRAW:
2072 w = makerect(e.u.area.left, e.u.area.top,
2073 e.u.area.right, e.u.area.bottom);
2074 break;
2075 case WE_MOUSE_DOWN:
2076 case WE_MOUSE_MOVE:
2077 case WE_MOUSE_UP:
Barry Warsawc2feec31996-12-12 22:48:20 +00002078 w = Py_BuildValue("((ii)iii)",
2079 e.u.where.h, e.u.where.v,
2080 e.u.where.clicks,
2081 e.u.where.button,
2082 e.u.where.mask);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002083 break;
2084 case WE_MENU:
Barry Warsawc2feec31996-12-12 22:48:20 +00002085 if (e.u.m.id >= IDOFFSET &&
2086 e.u.m.id < IDOFFSET+MAXNMENU &&
2087 menulist[e.u.m.id - IDOFFSET] != NULL)
2088 {
2089 w = Py_BuildValue("(Oi)",
2090 menulist[e.u.m.id - IDOFFSET],
2091 e.u.m.item);
2092 }
Guido van Rossum246b9d81991-06-03 10:55:14 +00002093 else {
2094 /* Ghost menu event.
2095 Can occur only on the Mac if another part
2096 of the aplication has installed a menu;
2097 like the THINK C console library. */
Barry Warsawc2feec31996-12-12 22:48:20 +00002098 Py_DECREF(v);
Guido van Rossum246b9d81991-06-03 10:55:14 +00002099 goto again;
2100 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002101 break;
Guido van Rossum3ee199e1992-06-30 12:48:26 +00002102 case WE_KEY:
Barry Warsawc2feec31996-12-12 22:48:20 +00002103 w = Py_BuildValue("(ii)", e.u.key.code, e.u.key.mask);
Guido van Rossum3ee199e1992-06-30 12:48:26 +00002104 break;
Guido van Rossum5b10f451990-10-30 16:01:48 +00002105 case WE_LOST_SEL:
Barry Warsawc2feec31996-12-12 22:48:20 +00002106 w = PyInt_FromLong((long)e.u.sel);
Guido van Rossum5b10f451990-10-30 16:01:48 +00002107 break;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002108 default:
Barry Warsawc2feec31996-12-12 22:48:20 +00002109 w = Py_None;
2110 Py_INCREF(w);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002111 break;
2112 }
2113 if (w == NULL) {
Barry Warsawc2feec31996-12-12 22:48:20 +00002114 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002115 return NULL;
2116 }
Barry Warsawc2feec31996-12-12 22:48:20 +00002117 u = Py_BuildValue("(iOO)", e.type, v, w);
2118 Py_XDECREF(v);
2119 Py_XDECREF(w);
Guido van Rossum2ee12f41992-04-13 15:54:35 +00002120 return u;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002121}
2122
Barry Warsawc2feec31996-12-12 22:48:20 +00002123static PyObject *
Guido van Rossume8e7cf41991-01-16 14:06:18 +00002124stdwin_getevent(sw, args)
Barry Warsawc2feec31996-12-12 22:48:20 +00002125 PyObject *sw;
2126 PyObject *args;
Guido van Rossume8e7cf41991-01-16 14:06:18 +00002127{
2128 return stdwin_get_poll_event(0, args);
2129}
2130
Barry Warsawc2feec31996-12-12 22:48:20 +00002131static PyObject *
Guido van Rossume8e7cf41991-01-16 14:06:18 +00002132stdwin_pollevent(sw, args)
Barry Warsawc2feec31996-12-12 22:48:20 +00002133 PyObject *sw;
2134 PyObject *args;
Guido van Rossume8e7cf41991-01-16 14:06:18 +00002135{
2136 return stdwin_get_poll_event(1, args);
2137}
2138
Barry Warsawc2feec31996-12-12 22:48:20 +00002139static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002140stdwin_setdefwinpos(sw, args)
Barry Warsawc2feec31996-12-12 22:48:20 +00002141 PyObject *sw;
2142 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002143{
2144 int a[2];
2145 if (!getpointarg(args, a))
2146 return NULL;
2147 wsetdefwinpos(a[0], a[1]);
Barry Warsawc2feec31996-12-12 22:48:20 +00002148 Py_INCREF(Py_None);
2149 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002150}
2151
Barry Warsawc2feec31996-12-12 22:48:20 +00002152static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002153stdwin_setdefwinsize(sw, args)
Barry Warsawc2feec31996-12-12 22:48:20 +00002154 PyObject *sw;
2155 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002156{
2157 int a[2];
2158 if (!getpointarg(args, a))
2159 return NULL;
2160 wsetdefwinsize(a[0], a[1]);
Barry Warsawc2feec31996-12-12 22:48:20 +00002161 Py_INCREF(Py_None);
2162 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002163}
2164
Barry Warsawc2feec31996-12-12 22:48:20 +00002165static PyObject *
Guido van Rossum0c2290b1991-04-03 19:12:14 +00002166stdwin_setdefscrollbars(sw, args)
Barry Warsawc2feec31996-12-12 22:48:20 +00002167 PyObject *sw;
2168 PyObject *args;
Guido van Rossum0c2290b1991-04-03 19:12:14 +00002169{
2170 int a[2];
2171 if (!getpointarg(args, a))
2172 return NULL;
2173 wsetdefscrollbars(a[0], a[1]);
Barry Warsawc2feec31996-12-12 22:48:20 +00002174 Py_INCREF(Py_None);
2175 return Py_None;
Guido van Rossum0c2290b1991-04-03 19:12:14 +00002176}
2177
Barry Warsawc2feec31996-12-12 22:48:20 +00002178static PyObject *
Guido van Rossum541c8c01991-05-05 20:13:41 +00002179stdwin_getdefwinpos(self, args)
Barry Warsawc2feec31996-12-12 22:48:20 +00002180 PyObject *self;
2181 PyObject *args;
Guido van Rossum33f17701991-02-13 23:19:39 +00002182{
2183 int h, v;
Barry Warsawc2feec31996-12-12 22:48:20 +00002184 if (!PyArg_NoArgs(args))
Guido van Rossum33f17701991-02-13 23:19:39 +00002185 return NULL;
2186 wgetdefwinpos(&h, &v);
2187 return makepoint(h, v);
2188}
2189
Barry Warsawc2feec31996-12-12 22:48:20 +00002190static PyObject *
Guido van Rossum541c8c01991-05-05 20:13:41 +00002191stdwin_getdefwinsize(self, args)
Barry Warsawc2feec31996-12-12 22:48:20 +00002192 PyObject *self;
2193 PyObject *args;
Guido van Rossum33f17701991-02-13 23:19:39 +00002194{
2195 int width, height;
Barry Warsawc2feec31996-12-12 22:48:20 +00002196 if (!PyArg_NoArgs(args))
Guido van Rossum33f17701991-02-13 23:19:39 +00002197 return NULL;
2198 wgetdefwinsize(&width, &height);
2199 return makepoint(width, height);
2200}
2201
Barry Warsawc2feec31996-12-12 22:48:20 +00002202static PyObject *
Guido van Rossum541c8c01991-05-05 20:13:41 +00002203stdwin_getdefscrollbars(self, args)
Barry Warsawc2feec31996-12-12 22:48:20 +00002204 PyObject *self;
2205 PyObject *args;
Guido van Rossum0c2290b1991-04-03 19:12:14 +00002206{
2207 int h, v;
Barry Warsawc2feec31996-12-12 22:48:20 +00002208 if (!PyArg_NoArgs(args))
Guido van Rossum0c2290b1991-04-03 19:12:14 +00002209 return NULL;
2210 wgetdefscrollbars(&h, &v);
2211 return makepoint(h, v);
2212}
2213
Barry Warsawc2feec31996-12-12 22:48:20 +00002214static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002215stdwin_menucreate(self, args)
Barry Warsawc2feec31996-12-12 22:48:20 +00002216 PyObject *self;
2217 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002218{
Guido van Rossumfc58e581992-01-27 16:45:55 +00002219 char *title;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002220 if (!getstrarg(args, &title))
2221 return NULL;
2222 wmenusetdeflocal(0);
Barry Warsawc2feec31996-12-12 22:48:20 +00002223 return (PyObject *)newmenuobject(title);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002224}
2225
Barry Warsawc2feec31996-12-12 22:48:20 +00002226static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002227stdwin_askfile(self, args)
Barry Warsawc2feec31996-12-12 22:48:20 +00002228 PyObject *self;
2229 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002230{
Guido van Rossumfc58e581992-01-27 16:45:55 +00002231 char *prompt, *dflt;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002232 int new, ret;
2233 char buf[256];
Barry Warsawc2feec31996-12-12 22:48:20 +00002234 if (!PyArg_Parse(args, "(ssi)", &prompt, &dflt, &new))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002235 return NULL;
Guido van Rossumfc58e581992-01-27 16:45:55 +00002236 strncpy(buf, dflt, sizeof buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002237 buf[sizeof buf - 1] = '\0';
Guido van Rossumff4949e1992-08-05 19:58:53 +00002238 BGN_STDWIN
Guido van Rossumfc58e581992-01-27 16:45:55 +00002239 ret = waskfile(prompt, buf, sizeof buf, new);
Guido van Rossumff4949e1992-08-05 19:58:53 +00002240 END_STDWIN
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002241 if (!ret) {
Barry Warsawc2feec31996-12-12 22:48:20 +00002242 PyErr_SetNone(PyExc_KeyboardInterrupt);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002243 return NULL;
2244 }
Barry Warsawc2feec31996-12-12 22:48:20 +00002245 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002246}
2247
Barry Warsawc2feec31996-12-12 22:48:20 +00002248static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002249stdwin_askync(self, args)
Barry Warsawc2feec31996-12-12 22:48:20 +00002250 PyObject *self;
2251 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002252{
Guido van Rossumfc58e581992-01-27 16:45:55 +00002253 char *prompt;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002254 int new, ret;
Barry Warsawc2feec31996-12-12 22:48:20 +00002255 if (!PyArg_Parse(args, "(si)", &prompt, &new))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002256 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002257 BGN_STDWIN
Guido van Rossumfc58e581992-01-27 16:45:55 +00002258 ret = waskync(prompt, new);
Guido van Rossumff4949e1992-08-05 19:58:53 +00002259 END_STDWIN
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002260 if (ret < 0) {
Barry Warsawc2feec31996-12-12 22:48:20 +00002261 PyErr_SetNone(PyExc_KeyboardInterrupt);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002262 return NULL;
2263 }
Barry Warsawc2feec31996-12-12 22:48:20 +00002264 return PyInt_FromLong((long)ret);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002265}
2266
Barry Warsawc2feec31996-12-12 22:48:20 +00002267static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002268stdwin_askstr(self, args)
Barry Warsawc2feec31996-12-12 22:48:20 +00002269 PyObject *self;
2270 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002271{
Guido van Rossumfc58e581992-01-27 16:45:55 +00002272 char *prompt, *dflt;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002273 int ret;
2274 char buf[256];
Barry Warsawc2feec31996-12-12 22:48:20 +00002275 if (!PyArg_Parse(args, "(ss)", &prompt, &dflt))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002276 return NULL;
Guido van Rossumfc58e581992-01-27 16:45:55 +00002277 strncpy(buf, dflt, sizeof buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002278 buf[sizeof buf - 1] = '\0';
Guido van Rossumff4949e1992-08-05 19:58:53 +00002279 BGN_STDWIN
Guido van Rossumfc58e581992-01-27 16:45:55 +00002280 ret = waskstr(prompt, buf, sizeof buf);
Guido van Rossumff4949e1992-08-05 19:58:53 +00002281 END_STDWIN
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002282 if (!ret) {
Barry Warsawc2feec31996-12-12 22:48:20 +00002283 PyErr_SetNone(PyExc_KeyboardInterrupt);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002284 return NULL;
2285 }
Barry Warsawc2feec31996-12-12 22:48:20 +00002286 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002287}
2288
Barry Warsawc2feec31996-12-12 22:48:20 +00002289static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002290stdwin_message(self, args)
Barry Warsawc2feec31996-12-12 22:48:20 +00002291 PyObject *self;
2292 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002293{
Guido van Rossumfc58e581992-01-27 16:45:55 +00002294 char *msg;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002295 if (!getstrarg(args, &msg))
2296 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002297 BGN_STDWIN
Guido van Rossumfc58e581992-01-27 16:45:55 +00002298 wmessage(msg);
Guido van Rossumff4949e1992-08-05 19:58:53 +00002299 END_STDWIN
Barry Warsawc2feec31996-12-12 22:48:20 +00002300 Py_INCREF(Py_None);
2301 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002302}
2303
Barry Warsawc2feec31996-12-12 22:48:20 +00002304static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002305stdwin_fleep(self, args)
Barry Warsawc2feec31996-12-12 22:48:20 +00002306 PyObject *self;
2307 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002308{
Barry Warsawc2feec31996-12-12 22:48:20 +00002309 if (!PyArg_NoArgs(args))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002310 return NULL;
2311 wfleep();
Barry Warsawc2feec31996-12-12 22:48:20 +00002312 Py_INCREF(Py_None);
2313 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002314}
2315
Barry Warsawc2feec31996-12-12 22:48:20 +00002316static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002317stdwin_setcutbuffer(self, args)
Barry Warsawc2feec31996-12-12 22:48:20 +00002318 PyObject *self;
2319 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002320{
Guido van Rossumfc58e581992-01-27 16:45:55 +00002321 int i, size;
2322 char *str;
Barry Warsawc2feec31996-12-12 22:48:20 +00002323 if (!PyArg_Parse(args, "(is#)", &i, &str, &size))
Guido van Rossum124967c1990-11-06 15:17:35 +00002324 return NULL;
Guido van Rossumfc58e581992-01-27 16:45:55 +00002325 wsetcutbuffer(i, str, size);
Barry Warsawc2feec31996-12-12 22:48:20 +00002326 Py_INCREF(Py_None);
2327 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002328}
2329
Barry Warsawc2feec31996-12-12 22:48:20 +00002330static PyObject *
Guido van Rossum246b9d81991-06-03 10:55:14 +00002331stdwin_getactive(self, args)
Barry Warsawc2feec31996-12-12 22:48:20 +00002332 PyObject *self;
2333 PyObject *args;
Guido van Rossum246b9d81991-06-03 10:55:14 +00002334{
2335 return window2object(wgetactive());
2336}
2337
Barry Warsawc2feec31996-12-12 22:48:20 +00002338static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002339stdwin_getcutbuffer(self, args)
Barry Warsawc2feec31996-12-12 22:48:20 +00002340 PyObject *self;
2341 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002342{
Guido van Rossum5b10f451990-10-30 16:01:48 +00002343 int i;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002344 char *str;
Guido van Rossum01769f01990-10-30 13:39:00 +00002345 int len;
Guido van Rossum124967c1990-11-06 15:17:35 +00002346 if (!getintarg(args, &i))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002347 return NULL;
Guido van Rossum5b10f451990-10-30 16:01:48 +00002348 str = wgetcutbuffer(i, &len);
Guido van Rossum01769f01990-10-30 13:39:00 +00002349 if (str == NULL) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002350 str = "";
Guido van Rossum01769f01990-10-30 13:39:00 +00002351 len = 0;
2352 }
Barry Warsawc2feec31996-12-12 22:48:20 +00002353 return PyString_FromStringAndSize(str, len);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002354}
2355
Barry Warsawc2feec31996-12-12 22:48:20 +00002356static PyObject *
Guido van Rossum5b10f451990-10-30 16:01:48 +00002357stdwin_rotatecutbuffers(self, args)
Barry Warsawc2feec31996-12-12 22:48:20 +00002358 PyObject *self;
2359 PyObject *args;
Guido van Rossum5b10f451990-10-30 16:01:48 +00002360{
2361 int i;
2362 if (!getintarg(args, &i))
2363 return NULL;
2364 wrotatecutbuffers(i);
Barry Warsawc2feec31996-12-12 22:48:20 +00002365 Py_INCREF(Py_None);
2366 return Py_None;
Guido van Rossum5b10f451990-10-30 16:01:48 +00002367}
2368
Barry Warsawc2feec31996-12-12 22:48:20 +00002369static PyObject *
Guido van Rossum5b10f451990-10-30 16:01:48 +00002370stdwin_getselection(self, args)
Barry Warsawc2feec31996-12-12 22:48:20 +00002371 PyObject *self;
2372 PyObject *args;
Guido van Rossum5b10f451990-10-30 16:01:48 +00002373{
2374 int sel;
2375 char *data;
2376 int len;
2377 if (!getintarg(args, &sel))
2378 return NULL;
2379 data = wgetselection(sel, &len);
2380 if (data == NULL) {
2381 data = "";
2382 len = 0;
2383 }
Barry Warsawc2feec31996-12-12 22:48:20 +00002384 return PyString_FromStringAndSize(data, len);
Guido van Rossum5b10f451990-10-30 16:01:48 +00002385}
2386
Barry Warsawc2feec31996-12-12 22:48:20 +00002387static PyObject *
Guido van Rossum5b10f451990-10-30 16:01:48 +00002388stdwin_resetselection(self, args)
Barry Warsawc2feec31996-12-12 22:48:20 +00002389 PyObject *self;
2390 PyObject *args;
Guido van Rossum5b10f451990-10-30 16:01:48 +00002391{
2392 int sel;
2393 if (!getintarg(args, &sel))
2394 return NULL;
2395 wresetselection(sel);
Barry Warsawc2feec31996-12-12 22:48:20 +00002396 Py_INCREF(Py_None);
2397 return Py_None;
Guido van Rossum5b10f451990-10-30 16:01:48 +00002398}
2399
Barry Warsawc2feec31996-12-12 22:48:20 +00002400static PyObject *
Guido van Rossum0c2290b1991-04-03 19:12:14 +00002401stdwin_fetchcolor(self, args)
Barry Warsawc2feec31996-12-12 22:48:20 +00002402 PyObject *self;
2403 PyObject *args;
Guido van Rossum0c2290b1991-04-03 19:12:14 +00002404{
Guido van Rossumfc58e581992-01-27 16:45:55 +00002405 char *colorname;
Guido van Rossum34679b71993-01-26 13:33:44 +00002406 COLOR color;
Guido van Rossum0c2290b1991-04-03 19:12:14 +00002407 if (!getstrarg(args, &colorname))
2408 return NULL;
Guido van Rossum34679b71993-01-26 13:33:44 +00002409 color = wfetchcolor(colorname);
2410#ifdef BADCOLOR
2411 if (color == BADCOLOR) {
Barry Warsawc2feec31996-12-12 22:48:20 +00002412 PyErr_SetString(StdwinError, "color name not found");
Guido van Rossum34679b71993-01-26 13:33:44 +00002413 return NULL;
2414 }
2415#endif
Barry Warsawc2feec31996-12-12 22:48:20 +00002416 return PyInt_FromLong((long)color);
Guido van Rossum0c2290b1991-04-03 19:12:14 +00002417}
2418
Barry Warsawc2feec31996-12-12 22:48:20 +00002419static PyObject *
Guido van Rossum541c8c01991-05-05 20:13:41 +00002420stdwin_getscrsize(self, args)
Barry Warsawc2feec31996-12-12 22:48:20 +00002421 PyObject *self;
2422 PyObject *args;
Guido van Rossum541c8c01991-05-05 20:13:41 +00002423{
2424 int width, height;
Barry Warsawc2feec31996-12-12 22:48:20 +00002425 if (!PyArg_NoArgs(args))
Guido van Rossum541c8c01991-05-05 20:13:41 +00002426 return NULL;
2427 wgetscrsize(&width, &height);
2428 return makepoint(width, height);
2429}
2430
Barry Warsawc2feec31996-12-12 22:48:20 +00002431static PyObject *
Guido van Rossum541c8c01991-05-05 20:13:41 +00002432stdwin_getscrmm(self, args)
Barry Warsawc2feec31996-12-12 22:48:20 +00002433 PyObject *self;
2434 PyObject *args;
Guido van Rossum541c8c01991-05-05 20:13:41 +00002435{
2436 int width, height;
Barry Warsawc2feec31996-12-12 22:48:20 +00002437 if (!PyArg_NoArgs(args))
Guido van Rossum541c8c01991-05-05 20:13:41 +00002438 return NULL;
2439 wgetscrmm(&width, &height);
2440 return makepoint(width, height);
2441}
2442
Guido van Rossumed233a51992-06-23 09:07:03 +00002443#ifdef unix
Barry Warsawc2feec31996-12-12 22:48:20 +00002444static PyObject *
Guido van Rossumed233a51992-06-23 09:07:03 +00002445stdwin_connectionnumber(self, args)
Barry Warsawc2feec31996-12-12 22:48:20 +00002446 PyObject *self;
2447 PyObject *args;
Guido van Rossumed233a51992-06-23 09:07:03 +00002448{
Barry Warsawc2feec31996-12-12 22:48:20 +00002449 if (!PyArg_NoArgs(args))
Guido van Rossumed233a51992-06-23 09:07:03 +00002450 return NULL;
Barry Warsawc2feec31996-12-12 22:48:20 +00002451 return PyInt_FromLong((long) wconnectionnumber());
Guido van Rossumed233a51992-06-23 09:07:03 +00002452}
2453#endif
2454
Barry Warsawc2feec31996-12-12 22:48:20 +00002455static PyObject *
Guido van Rossumbf80e541993-02-08 15:49:17 +00002456stdwin_listfontnames(self, args)
Barry Warsawc2feec31996-12-12 22:48:20 +00002457 PyObject *self;
2458 PyObject *args;
Guido van Rossumbf80e541993-02-08 15:49:17 +00002459{
2460 char *pattern;
2461 char **fontnames;
2462 int count;
Barry Warsawc2feec31996-12-12 22:48:20 +00002463 PyObject *list;
2464 if (!PyArg_Parse(args, "z", &pattern))
Guido van Rossumbf80e541993-02-08 15:49:17 +00002465 return NULL;
2466 fontnames = wlistfontnames(pattern, &count);
Barry Warsawc2feec31996-12-12 22:48:20 +00002467 list = PyList_New(count);
Guido van Rossumbf80e541993-02-08 15:49:17 +00002468 if (list != NULL) {
2469 int i;
2470 for (i = 0; i < count; i++) {
Barry Warsawc2feec31996-12-12 22:48:20 +00002471 PyObject *v = PyString_FromString(fontnames[i]);
Guido van Rossumbf80e541993-02-08 15:49:17 +00002472 if (v == NULL) {
Barry Warsawc2feec31996-12-12 22:48:20 +00002473 Py_DECREF(list);
Guido van Rossumbf80e541993-02-08 15:49:17 +00002474 list = NULL;
2475 break;
2476 }
Barry Warsawc2feec31996-12-12 22:48:20 +00002477 PyList_SetItem(list, i, v);
Guido van Rossumbf80e541993-02-08 15:49:17 +00002478 }
2479 }
2480 return list;
2481}
2482
Guido van Rossume9066061993-07-29 13:14:32 +00002483#ifdef HAVE_BITMAPS
Barry Warsawc2feec31996-12-12 22:48:20 +00002484static PyObject *
Guido van Rossumbf80e541993-02-08 15:49:17 +00002485stdwin_newbitmap(self, args)
Barry Warsawc2feec31996-12-12 22:48:20 +00002486 PyObject *self;
2487 PyObject *args;
Guido van Rossumbf80e541993-02-08 15:49:17 +00002488{
2489 int width, height;
2490 bitmapobject *bp;
Barry Warsawc2feec31996-12-12 22:48:20 +00002491 if (!PyArg_Parse(args, "(ii)", &width, &height))
Guido van Rossumbf80e541993-02-08 15:49:17 +00002492 return NULL;
Barry Warsawc2feec31996-12-12 22:48:20 +00002493 return (PyObject *)newbitmapobject(width, height);
Guido van Rossumbf80e541993-02-08 15:49:17 +00002494}
Guido van Rossume9066061993-07-29 13:14:32 +00002495#endif
Guido van Rossumbf80e541993-02-08 15:49:17 +00002496
Barry Warsawc2feec31996-12-12 22:48:20 +00002497static PyMethodDef stdwin_methods[] = {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002498 {"askfile", stdwin_askfile},
2499 {"askstr", stdwin_askstr},
2500 {"askync", stdwin_askync},
Guido van Rossumcacd9571993-10-18 11:44:47 +00002501 {"done", stdwin_done},
Guido van Rossum27201061991-04-16 08:43:03 +00002502 {"fetchcolor", stdwin_fetchcolor},
Guido van Rossumed233a51992-06-23 09:07:03 +00002503#ifdef unix
2504 {"fileno", stdwin_connectionnumber},
2505 {"connectionnumber", stdwin_connectionnumber},
2506#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002507 {"fleep", stdwin_fleep},
Guido van Rossum246b9d81991-06-03 10:55:14 +00002508 {"getactive", stdwin_getactive},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002509 {"getcutbuffer", stdwin_getcutbuffer},
Guido van Rossum0c2290b1991-04-03 19:12:14 +00002510 {"getdefscrollbars", stdwin_getdefscrollbars},
Guido van Rossum33f17701991-02-13 23:19:39 +00002511 {"getdefwinpos", stdwin_getdefwinpos},
2512 {"getdefwinsize", stdwin_getdefwinsize},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002513 {"getevent", stdwin_getevent},
Guido van Rossum541c8c01991-05-05 20:13:41 +00002514 {"getscrmm", stdwin_getscrmm},
2515 {"getscrsize", stdwin_getscrsize},
Guido van Rossum27201061991-04-16 08:43:03 +00002516 {"getselection", stdwin_getselection},
Guido van Rossumbf80e541993-02-08 15:49:17 +00002517 {"listfontnames", stdwin_listfontnames},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002518 {"menucreate", stdwin_menucreate},
2519 {"message", stdwin_message},
Guido van Rossume9066061993-07-29 13:14:32 +00002520#ifdef HAVE_BITMAPS
Guido van Rossumbf80e541993-02-08 15:49:17 +00002521 {"newbitmap", stdwin_newbitmap},
Guido van Rossume9066061993-07-29 13:14:32 +00002522#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002523 {"open", stdwin_open},
Guido van Rossume8e7cf41991-01-16 14:06:18 +00002524 {"pollevent", stdwin_pollevent},
Guido van Rossum5b10f451990-10-30 16:01:48 +00002525 {"resetselection", stdwin_resetselection},
2526 {"rotatecutbuffers", stdwin_rotatecutbuffers},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002527 {"setcutbuffer", stdwin_setcutbuffer},
Guido van Rossum0c2290b1991-04-03 19:12:14 +00002528 {"setdefscrollbars", stdwin_setdefscrollbars},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002529 {"setdefwinpos", stdwin_setdefwinpos},
2530 {"setdefwinsize", stdwin_setdefwinsize},
2531
2532 /* Text measuring methods borrow code from drawing objects: */
Barry Warsawc2feec31996-12-12 22:48:20 +00002533 {"baseline", (PyCFunction)drawing_baseline},
2534 {"lineheight", (PyCFunction)drawing_lineheight},
2535 {"textbreak", (PyCFunction)drawing_textbreak},
2536 {"textwidth", (PyCFunction)drawing_textwidth},
Guido van Rossum0c2290b1991-04-03 19:12:14 +00002537
2538 /* Same for font setting methods: */
Barry Warsawc2feec31996-12-12 22:48:20 +00002539 {"setfont", (PyCFunction)drawing_setfont},
Guido van Rossum0c2290b1991-04-03 19:12:14 +00002540
2541 /* Same for color setting/getting methods: */
Barry Warsawc2feec31996-12-12 22:48:20 +00002542 {"getbgcolor", (PyCFunction)drawing_getbgcolor},
2543 {"getfgcolor", (PyCFunction)drawing_getfgcolor},
2544 {"setbgcolor", (PyCFunction)drawing_setbgcolor},
2545 {"setfgcolor", (PyCFunction)drawing_setfgcolor},
Guido van Rossum0c2290b1991-04-03 19:12:14 +00002546
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002547 {NULL, NULL} /* sentinel */
2548};
2549
Guido van Rossumb6775db1994-08-01 11:34:53 +00002550#ifndef macintosh
Guido van Rossumcacd9571993-10-18 11:44:47 +00002551static int
2552checkstringlist(args, ps, pn)
Barry Warsawc2feec31996-12-12 22:48:20 +00002553 PyObject *args;
Guido van Rossumcacd9571993-10-18 11:44:47 +00002554 char ***ps;
2555 int *pn;
2556{
2557 int i, n;
2558 char **s;
Barry Warsawc2feec31996-12-12 22:48:20 +00002559 if (!PyList_Check(args)) {
2560 PyErr_SetString(PyExc_TypeError, "list of strings expected");
Guido van Rossumcacd9571993-10-18 11:44:47 +00002561 return 0;
2562 }
Barry Warsawc2feec31996-12-12 22:48:20 +00002563 n = PyList_Size(args);
2564 s = PyMem_NEW(char *, n+1);
Guido van Rossumcacd9571993-10-18 11:44:47 +00002565 if (s == NULL) {
Barry Warsawc2feec31996-12-12 22:48:20 +00002566 PyErr_NoMemory();
Guido van Rossumcacd9571993-10-18 11:44:47 +00002567 return 0;
2568 }
2569 for (i = 0; i < n; i++) {
Barry Warsawc2feec31996-12-12 22:48:20 +00002570 PyObject *item = PyList_GetItem(args, i);
2571 if (!PyString_Check(item)) {
2572 PyErr_SetString(PyExc_TypeError,
2573 "list of strings expected");
Guido van Rossumcacd9571993-10-18 11:44:47 +00002574 return 0;
2575 }
Barry Warsawc2feec31996-12-12 22:48:20 +00002576 s[i] = PyString_AsString(item);
Guido van Rossumcacd9571993-10-18 11:44:47 +00002577 }
2578 s[n] = NULL; /* In case caller wants a NULL-terminated list */
2579 *ps = s;
2580 *pn = n;
2581 return 1;
2582}
2583
2584static int
2585putbackstringlist(list, s, n)
Barry Warsawc2feec31996-12-12 22:48:20 +00002586 PyObject *list;
Guido van Rossumcacd9571993-10-18 11:44:47 +00002587 char **s;
2588 int n;
2589{
Barry Warsawc2feec31996-12-12 22:48:20 +00002590 int oldsize = PyList_Size(list);
2591 PyObject *newlist;
Guido van Rossumcacd9571993-10-18 11:44:47 +00002592 int i;
2593 if (n == oldsize)
2594 return 1;
Barry Warsawc2feec31996-12-12 22:48:20 +00002595 newlist = PyList_New(n);
Guido van Rossumcacd9571993-10-18 11:44:47 +00002596 for (i = 0; i < n && newlist != NULL; i++) {
Barry Warsawc2feec31996-12-12 22:48:20 +00002597 PyObject *item = PyString_FromString(s[i]);
Guido van Rossumcacd9571993-10-18 11:44:47 +00002598 if (item == NULL) {
Barry Warsawc2feec31996-12-12 22:48:20 +00002599 Py_DECREF(newlist);
Guido van Rossumcacd9571993-10-18 11:44:47 +00002600 newlist = NULL;
2601 }
2602 else
Barry Warsawc2feec31996-12-12 22:48:20 +00002603 PyList_SetItem(newlist, i, item);
Guido van Rossumcacd9571993-10-18 11:44:47 +00002604 }
2605 if (newlist == NULL)
2606 return 0;
2607 (*list->ob_type->tp_as_sequence->sq_ass_slice)
2608 (list, 0, oldsize, newlist);
Barry Warsawc2feec31996-12-12 22:48:20 +00002609 Py_DECREF(newlist);
Guido van Rossumcacd9571993-10-18 11:44:47 +00002610 return 1;
2611}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002612#endif /* macintosh */
Guido van Rossumcacd9571993-10-18 11:44:47 +00002613
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002614void
2615initstdwin()
2616{
Barry Warsawc2feec31996-12-12 22:48:20 +00002617 PyObject *m, *d;
Guido van Rossumbbf94341991-12-16 15:44:53 +00002618 static int inited = 0;
2619
Guido van Rossum2d14e211991-02-19 12:26:49 +00002620 if (!inited) {
Guido van Rossumb6775db1994-08-01 11:34:53 +00002621#ifdef macintosh
2622 winit();
Jack Jansend56c1091995-01-27 14:44:16 +00002623 PyMac_DoYieldEnabled = 0;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002624#else
Guido van Rossum39cb5ce1995-01-26 00:37:10 +00002625 char buf[1000];
Guido van Rossumcacd9571993-10-18 11:44:47 +00002626 int argc = 0;
2627 char **argv = NULL;
Barry Warsawc2feec31996-12-12 22:48:20 +00002628 PyObject *sys_argv = PySys_GetObject("argv");
Guido van Rossumcacd9571993-10-18 11:44:47 +00002629 if (sys_argv != NULL) {
2630 if (!checkstringlist(sys_argv, &argv, &argc))
Barry Warsawc2feec31996-12-12 22:48:20 +00002631 PyErr_Clear();
Guido van Rossumcacd9571993-10-18 11:44:47 +00002632 }
Guido van Rossumc45611d1993-11-17 22:58:56 +00002633 if (argc > 0) {
2634 /* If argv[0] has a ".py" suffix, remove the suffix */
2635 char *p = strrchr(argv[0], '.');
2636 if (p != NULL && strcmp(p, ".py") == 0) {
2637 int n = p - argv[0];
2638 if (n >= sizeof(buf))
2639 n = sizeof(buf)-1;
2640 strncpy(buf, argv[0], n);
2641 buf[n] = '\0';
2642 argv[0] = buf;
2643 }
2644 }
Guido van Rossumcacd9571993-10-18 11:44:47 +00002645 winitargs(&argc, &argv);
2646 if (argv != NULL) {
2647 if (!putbackstringlist(sys_argv, argv, argc))
Barry Warsawc2feec31996-12-12 22:48:20 +00002648 PyErr_Clear();
Guido van Rossumcacd9571993-10-18 11:44:47 +00002649 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002650#endif
Guido van Rossum2d14e211991-02-19 12:26:49 +00002651 inited = 1;
2652 }
Barry Warsawc2feec31996-12-12 22:48:20 +00002653 m = Py_InitModule("stdwin", stdwin_methods);
2654 d = PyModule_GetDict(m);
Guido van Rossumbbf94341991-12-16 15:44:53 +00002655
2656 /* Initialize stdwin.error exception */
Guido van Rossum0cb96de1997-10-01 04:29:29 +00002657 StdwinError = PyErr_NewException("stdwin.error", NULL, NULL);
Barry Warsawc2feec31996-12-12 22:48:20 +00002658 if (StdwinError == NULL ||
2659 PyDict_SetItemString(d, "error", StdwinError) != 0)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00002660 return;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002661#ifdef WITH_THREAD
Guido van Rossumff4949e1992-08-05 19:58:53 +00002662 StdwinLock = allocate_lock();
Guido van Rossumff4949e1992-08-05 19:58:53 +00002663#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002664}