blob: 7ada57ac038d012e78d55b289f03265b82dc7587 [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
7Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
9provided that the above copyright notice appear in all copies and that
10both that copyright notice and this permission notice appear in
11supporting documentation, and that the names of Stichting Mathematisch
12Centrum or CWI not be used in advertising or publicity pertaining to
13distribution of the software without specific, written prior permission.
14
15STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22
23******************************************************************/
24
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000025/* Stdwin module */
26
27/* Stdwin itself is a module, not a separate object type.
28 Object types defined here:
29 wp: a window
30 dp: a drawing structure (only one can exist at a time)
31 mp: a menu
32 tp: a textedit block
Guido van Rossumbf80e541993-02-08 15:49:17 +000033 bp: a bitmap
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000034*/
35
36/* Rules for translating C stdwin function calls into Python stwin:
37 - All names drop their initial letter 'w'
38 - Functions with a window as first parameter are methods of window objects
39 - There is no equivalent for wclose(); just delete the window object
40 (all references to it!) (XXX maybe this is a bad idea)
41 - w.begindrawing() returns a drawing object
42 - There is no equivalent for wenddrawing(win); just delete the drawing
43 object (all references to it!) (XXX maybe this is a bad idea)
44 - Functions that may only be used inside wbegindrawing / wendddrawing
45 are methods of the drawing object; this includes the text measurement
46 functions (which however have doubles as module functions).
47 - Methods of the drawing object drop an initial 'draw' from their name
48 if they have it, e.g., wdrawline() --> d.line()
49 - The obvious type conversions: int --> intobject; string --> stringobject
50 - A text parameter followed by a length parameter is only a text (string)
51 parameter in Python
52 - A point or other pair of horizontal and vertical coordinates is always
53 a pair of integers in Python
54 - Two points forming a rectangle or endpoints of a line segment are a
55 pair of points in Python
56 - The arguments to d.elarc() are three points.
57 - The functions wgetclip() and wsetclip() are translated into
58 stdwin.getcutbuffer() and stdwin.setcutbuffer(); 'clip' is really
59 a bad word for what these functions do (clipping has a different
60 meaning in the drawing world), while cutbuffer is standard X jargon.
Guido van Rossum01769f01990-10-30 13:39:00 +000061 XXX This must change again in the light of changes to stdwin!
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000062 - For textedit, similar rules hold, but they are less strict.
63 XXX more?
64*/
65
Guido van Rossum3f5da241990-12-20 15:06:42 +000066#include "allobjects.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000067#include "modsupport.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +000068#include "ceval.h"
Guido van Rossumcacd9571993-10-18 11:44:47 +000069#include "sysmodule.h"
Jack Jansendc977a91995-08-14 12:17:18 +000070#ifdef macintosh
71#include "macglue.h"
72#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000073
Guido van Rossum0b0db8e1993-01-21 16:07:51 +000074#ifdef macintosh
Guido van Rossume9066061993-07-29 13:14:32 +000075#include ":::stdwin:H:stdwin.h"
Guido van Rossum0b0db8e1993-01-21 16:07:51 +000076#else /* !macintosh */
Guido van Rossum3f5da241990-12-20 15:06:42 +000077#include "stdwin.h"
Guido van Rossume9066061993-07-29 13:14:32 +000078#define HAVE_BITMAPS
Guido van Rossum0b0db8e1993-01-21 16:07:51 +000079#endif /* !macintosh */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000080
Guido van Rossumb6775db1994-08-01 11:34:53 +000081#ifdef WITH_THREAD
Guido van Rossumff4949e1992-08-05 19:58:53 +000082
83#include "thread.h"
84
85static type_lock StdwinLock; /* Lock held when interpreter not locked */
86
87#define BGN_STDWIN BGN_SAVE acquire_lock(StdwinLock, 1);
88#define RET_STDWIN release_lock(StdwinLock); RET_SAVE
89#define END_STDWIN release_lock(StdwinLock); END_SAVE
90
91#else
92
93#define BGN_STDWIN BGN_SAVE
94#define RET_STDWIN RET_SAVE
95#define END_STDWIN END_SAVE
96
97#endif
98
Guido van Rossum234f9421993-06-17 12:35:49 +000099#define getpointarg(v, a) getargs(v, "(ii)", a, (a)+1)
100#define get3pointarg(v, a) getargs(v, "((ii)(ii)(ii))", \
101 a, a+1, a+2, a+3, a+4, a+5)
102#define getrectarg(v, a) getargs(v, "((ii)(ii))", a, a+1, a+2, a+3)
103#define getrectintarg(v, a) getargs(v, "(((ii)(ii))i)", a, a+1, a+2, a+3, a+4)
104#define getpointintarg(v, a) getargs(v, "((ii)i)", a, a+1, a+2)
105#define getrectpointarg(v, a) getargs(v, "(((ii)(ii))(ii))", \
106 a, a+1, a+2, a+3, a+4, a+5)
107
Guido van Rossumbbf94341991-12-16 15:44:53 +0000108static object *StdwinError; /* Exception stdwin.error */
Guido van Rossum87e7ea71991-12-10 14:00:03 +0000109
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000110/* Window and menu object types declared here because of forward references */
111
112typedef struct {
113 OB_HEAD
114 object *w_title;
115 WINDOW *w_win;
116 object *w_attr; /* Attributes dictionary */
117} windowobject;
118
Guido van Rossumb6775db1994-08-01 11:34:53 +0000119staticforward typeobject Windowtype;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000120
121#define is_windowobject(wp) ((wp)->ob_type == &Windowtype)
122
123typedef struct {
124 OB_HEAD
125 MENU *m_menu;
126 int m_id;
127 object *m_attr; /* Attributes dictionary */
128} menuobject;
129
Guido van Rossumb6775db1994-08-01 11:34:53 +0000130staticforward typeobject Menutype;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000131
132#define is_menuobject(mp) ((mp)->ob_type == &Menutype)
133
Guido van Rossumbf80e541993-02-08 15:49:17 +0000134typedef struct {
135 OB_HEAD
136 BITMAP *b_bitmap;
137 object *b_attr; /* Attributes dictionary */
138} bitmapobject;
139
Guido van Rossumb6775db1994-08-01 11:34:53 +0000140staticforward typeobject Bitmaptype;
Guido van Rossumbf80e541993-02-08 15:49:17 +0000141
142#define is_bitmapobject(mp) ((mp)->ob_type == &Bitmaptype)
143
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000144
145/* Strongly stdwin-specific argument handlers */
146
147static int
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000148getmenudetail(v, ep)
149 object *v;
150 EVENT *ep;
151{
Guido van Rossumfc58e581992-01-27 16:45:55 +0000152 menuobject *mp;
153 if (!getargs(v, "(Oi)", &mp, &ep->u.m.item))
154 return 0;
155 if (!is_menuobject(mp))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000156 return err_badarg();
Guido van Rossumfc58e581992-01-27 16:45:55 +0000157 ep->u.m.id = mp->m_id;
158 return 1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000159}
160
161static int
162geteventarg(v, ep)
163 object *v;
164 EVENT *ep;
165{
166 object *wp, *detail;
167 int a[4];
Guido van Rossumfc58e581992-01-27 16:45:55 +0000168 if (!getargs(v, "(iOO)", &ep->type, &wp, &detail))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000169 return 0;
Guido van Rossumfc58e581992-01-27 16:45:55 +0000170 if (is_windowobject(wp))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000171 ep->window = ((windowobject *)wp) -> w_win;
Guido van Rossumfc58e581992-01-27 16:45:55 +0000172 else if (wp == None)
173 ep->window = NULL;
174 else
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000175 return err_badarg();
Guido van Rossumfc58e581992-01-27 16:45:55 +0000176 switch (ep->type) {
177 case WE_CHAR: {
178 char c;
179 if (!getargs(detail, "c", &c))
180 return 0;
181 ep->u.character = c;
182 return 1;
183 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000184 case WE_COMMAND:
185 return getintarg(detail, &ep->u.command);
186 case WE_DRAW:
187 if (!getrectarg(detail, a))
188 return 0;
189 ep->u.area.left = a[0];
190 ep->u.area.top = a[1];
191 ep->u.area.right = a[2];
192 ep->u.area.bottom = a[3];
193 return 1;
194 case WE_MOUSE_DOWN:
195 case WE_MOUSE_UP:
196 case WE_MOUSE_MOVE:
Guido van Rossumfc58e581992-01-27 16:45:55 +0000197 return getargs(detail, "((ii)iii)",
198 &ep->u.where.h, &ep->u.where.v,
199 &ep->u.where.clicks,
200 &ep->u.where.button,
201 &ep->u.where.mask);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000202 case WE_MENU:
203 return getmenudetail(detail, ep);
Guido van Rossum3ee199e1992-06-30 12:48:26 +0000204 case WE_KEY:
205 return getargs(detail, "(ii)",
206 &ep->u.key.code, &ep->u.key.mask);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000207 default:
208 return 1;
209 }
210}
211
212
213/* Return construction tools */
214
215static object *
216makepoint(a, b)
217 int a, b;
218{
Guido van Rossum2ee12f41992-04-13 15:54:35 +0000219 return mkvalue("(ii)", a, b);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000220}
221
222static object *
223makerect(a, b, c, d)
224 int a, b, c, d;
225{
Guido van Rossum2ee12f41992-04-13 15:54:35 +0000226 return mkvalue("((ii)(ii))", a, b, c, d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000227}
228
229
230/* Drawing objects */
231
232typedef struct {
233 OB_HEAD
234 windowobject *d_ref;
235} drawingobject;
236
237static drawingobject *Drawing; /* Set to current drawing object, or NULL */
238
239/* Drawing methods */
240
Guido van Rossum3c284741991-11-27 14:54:54 +0000241static object *
242drawing_close(dp)
243 drawingobject *dp;
244{
245 if (dp->d_ref != NULL) {
246 wenddrawing(dp->d_ref->w_win);
247 Drawing = NULL;
248 DECREF(dp->d_ref);
249 dp->d_ref = NULL;
250 }
251 INCREF(None);
252 return None;
253}
Guido van Rossum77b46041992-01-14 18:41:24 +0000254
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000255static void
256drawing_dealloc(dp)
257 drawingobject *dp;
258{
Guido van Rossum3c284741991-11-27 14:54:54 +0000259 if (dp->d_ref != NULL) {
260 wenddrawing(dp->d_ref->w_win);
261 Drawing = NULL;
262 DECREF(dp->d_ref);
263 dp->d_ref = NULL;
264 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000265 free((char *)dp);
266}
267
268static object *
269drawing_generic(dp, args, func)
270 drawingobject *dp;
271 object *args;
272 void (*func) FPROTO((int, int, int, int));
273{
274 int a[4];
275 if (!getrectarg(args, a))
276 return NULL;
277 (*func)(a[0], a[1], a[2], a[3]);
278 INCREF(None);
279 return None;
280}
281
282static object *
283drawing_line(dp, args)
284 drawingobject *dp;
285 object *args;
286{
Guido van Rossumbf109731991-03-06 13:14:12 +0000287 return drawing_generic(dp, args, wdrawline);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000288}
289
290static object *
291drawing_xorline(dp, args)
292 drawingobject *dp;
293 object *args;
294{
Guido van Rossumbf109731991-03-06 13:14:12 +0000295 return drawing_generic(dp, args, wxorline);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000296}
297
298static object *
299drawing_circle(dp, args)
300 drawingobject *dp;
301 object *args;
302{
303 int a[3];
304 if (!getpointintarg(args, a))
305 return NULL;
306 wdrawcircle(a[0], a[1], a[2]);
307 INCREF(None);
308 return None;
309}
Guido van Rossuma2a181a1991-05-14 12:09:25 +0000310
Guido van Rossum27201061991-04-16 08:43:03 +0000311static object *
312drawing_fillcircle(dp, args)
313 drawingobject *dp;
314 object *args;
315{
316 int a[3];
317 if (!getpointintarg(args, a))
318 return NULL;
319 wfillcircle(a[0], a[1], a[2]);
320 INCREF(None);
321 return None;
322}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000323
324static object *
Guido van Rossuma2a181a1991-05-14 12:09:25 +0000325drawing_xorcircle(dp, args)
326 drawingobject *dp;
327 object *args;
328{
329 int a[3];
330 if (!getpointintarg(args, a))
331 return NULL;
332 wxorcircle(a[0], a[1], a[2]);
333 INCREF(None);
334 return None;
335}
336
337static object *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000338drawing_elarc(dp, args)
339 drawingobject *dp;
340 object *args;
341{
342 int a[6];
343 if (!get3pointarg(args, a))
344 return NULL;
345 wdrawelarc(a[0], a[1], a[2], a[3], a[4], a[5]);
346 INCREF(None);
347 return None;
348}
349
350static object *
Guido van Rossum27201061991-04-16 08:43:03 +0000351drawing_fillelarc(dp, args)
352 drawingobject *dp;
353 object *args;
354{
355 int a[6];
356 if (!get3pointarg(args, a))
357 return NULL;
358 wfillelarc(a[0], a[1], a[2], a[3], a[4], a[5]);
359 INCREF(None);
360 return None;
361}
362
363static object *
Guido van Rossuma2a181a1991-05-14 12:09:25 +0000364drawing_xorelarc(dp, args)
365 drawingobject *dp;
366 object *args;
367{
368 int a[6];
369 if (!get3pointarg(args, a))
370 return NULL;
371 wxorelarc(a[0], a[1], a[2], a[3], a[4], a[5]);
372 INCREF(None);
373 return None;
374}
375
376static object *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000377drawing_box(dp, args)
378 drawingobject *dp;
379 object *args;
380{
Guido van Rossumbf109731991-03-06 13:14:12 +0000381 return drawing_generic(dp, args, wdrawbox);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000382}
383
384static object *
385drawing_erase(dp, args)
386 drawingobject *dp;
387 object *args;
388{
Guido van Rossumbf109731991-03-06 13:14:12 +0000389 return drawing_generic(dp, args, werase);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000390}
391
392static object *
393drawing_paint(dp, args)
394 drawingobject *dp;
395 object *args;
396{
Guido van Rossumbf109731991-03-06 13:14:12 +0000397 return drawing_generic(dp, args, wpaint);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000398}
399
400static object *
401drawing_invert(dp, args)
402 drawingobject *dp;
403 object *args;
404{
Guido van Rossumbf109731991-03-06 13:14:12 +0000405 return drawing_generic(dp, args, winvert);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000406}
407
Guido van Rossum27201061991-04-16 08:43:03 +0000408static POINT *
409getpointsarray(v, psize)
410 object *v;
411 int *psize;
412{
413 int n = -1;
414 object * (*getitem) PROTO((object *, int));
415 int i;
416 POINT *points;
417
418 if (v == NULL)
419 ;
420 else if (is_listobject(v)) {
421 n = getlistsize(v);
422 getitem = getlistitem;
423 }
424 else if (is_tupleobject(v)) {
425 n = gettuplesize(v);
426 getitem = gettupleitem;
427 }
428
429 if (n <= 0) {
430 (void) err_badarg();
431 return NULL;
432 }
433
434 points = NEW(POINT, n);
435 if (points == NULL) {
436 (void) err_nomem();
437 return NULL;
438 }
439
440 for (i = 0; i < n; i++) {
441 object *w = (*getitem)(v, i);
442 int a[2];
443 if (!getpointarg(w, a)) {
444 DEL(points);
445 return NULL;
446 }
447 points[i].h = a[0];
448 points[i].v = a[1];
449 }
450
451 *psize = n;
452 return points;
453}
454
455static object *
456drawing_poly(dp, args)
457 drawingobject *dp;
458 object *args;
459{
460 int n;
461 POINT *points = getpointsarray(args, &n);
462 if (points == NULL)
463 return NULL;
464 wdrawpoly(n, points);
465 DEL(points);
466 INCREF(None);
467 return None;
468}
469
470static object *
471drawing_fillpoly(dp, args)
472 drawingobject *dp;
473 object *args;
474{
475 int n;
476 POINT *points = getpointsarray(args, &n);
477 if (points == NULL)
478 return NULL;
479 wfillpoly(n, points);
480 DEL(points);
481 INCREF(None);
482 return None;
483}
484
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000485static object *
Guido van Rossuma2a181a1991-05-14 12:09:25 +0000486drawing_xorpoly(dp, args)
487 drawingobject *dp;
488 object *args;
489{
490 int n;
491 POINT *points = getpointsarray(args, &n);
492 if (points == NULL)
493 return NULL;
494 wxorpoly(n, points);
495 DEL(points);
496 INCREF(None);
497 return None;
498}
499
500static object *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000501drawing_cliprect(dp, args)
502 drawingobject *dp;
503 object *args;
504{
Guido van Rossumbf109731991-03-06 13:14:12 +0000505 return drawing_generic(dp, args, wcliprect);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000506}
507
508static object *
509drawing_noclip(dp, args)
510 drawingobject *dp;
511 object *args;
512{
513 if (!getnoarg(args))
514 return NULL;
515 wnoclip();
516 INCREF(None);
517 return None;
518}
519
520static object *
521drawing_shade(dp, args)
522 drawingobject *dp;
523 object *args;
524{
525 int a[5];
526 if (!getrectintarg(args, a))
527 return NULL;
528 wshade(a[0], a[1], a[2], a[3], a[4]);
529 INCREF(None);
530 return None;
531}
532
533static object *
534drawing_text(dp, args)
535 drawingobject *dp;
536 object *args;
537{
Guido van Rossumfc58e581992-01-27 16:45:55 +0000538 int h, v, size;
539 char *text;
540 if (!getargs(args, "((ii)s#)", &h, &v, &text, &size))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000541 return NULL;
Guido van Rossumfc58e581992-01-27 16:45:55 +0000542 wdrawtext(h, v, text, size);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000543 INCREF(None);
544 return None;
545}
546
547/* The following four are also used as stdwin functions */
548
549static object *
550drawing_lineheight(dp, args)
551 drawingobject *dp;
552 object *args;
553{
554 if (!getnoarg(args))
555 return NULL;
556 return newintobject((long)wlineheight());
557}
558
559static object *
560drawing_baseline(dp, args)
561 drawingobject *dp;
562 object *args;
563{
564 if (!getnoarg(args))
565 return NULL;
566 return newintobject((long)wbaseline());
567}
568
569static object *
570drawing_textwidth(dp, args)
571 drawingobject *dp;
572 object *args;
573{
Guido van Rossumfc58e581992-01-27 16:45:55 +0000574 char *text;
575 int size;
576 if (!getargs(args, "s#", &text, &size))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000577 return NULL;
Guido van Rossumfc58e581992-01-27 16:45:55 +0000578 return newintobject((long)wtextwidth(text, size));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000579}
580
581static object *
582drawing_textbreak(dp, args)
583 drawingobject *dp;
584 object *args;
585{
Guido van Rossumfc58e581992-01-27 16:45:55 +0000586 char *text;
587 int size, width;
588 if (!getargs(args, "(s#i)", &text, &size, &width))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000589 return NULL;
Guido van Rossumfc58e581992-01-27 16:45:55 +0000590 return newintobject((long)wtextbreak(text, size, width));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000591}
592
Guido van Rossum0c2290b1991-04-03 19:12:14 +0000593static object *
594drawing_setfont(self, args)
595 drawingobject *self;
596 object *args;
597{
Guido van Rossumfc58e581992-01-27 16:45:55 +0000598 char *font;
599 char style = '\0';
600 int size = 0;
601 if (args == NULL || !is_tupleobject(args)) {
Guido van Rossum3c8ba7a1992-02-05 11:15:00 +0000602 if (!getargs(args, "z", &font))
Guido van Rossum50429a11991-04-04 15:24:07 +0000603 return NULL;
604 }
605 else {
Guido van Rossumfc58e581992-01-27 16:45:55 +0000606 int n = gettuplesize(args);
607 if (n == 2) {
608 if (!getargs(args, "(zi)", &font, &size))
609 return NULL;
610 }
611 else if (!getargs(args, "(zic)", &font, &size, &style)) {
612 err_clear();
613 if (!getargs(args, "(zci)", &font, &style, &size))
614 return NULL;
Guido van Rossum50429a11991-04-04 15:24:07 +0000615 }
616 }
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000617 if (font != NULL) {
618 if (!wsetfont(font)) {
619 err_setstr(StdwinError, "font not found");
620 return NULL;
621 }
622 }
Guido van Rossum50429a11991-04-04 15:24:07 +0000623 if (size != 0)
624 wsetsize(size);
Guido van Rossumfc58e581992-01-27 16:45:55 +0000625 switch (style) {
626 case 'b':
627 wsetbold();
628 break;
629 case 'i':
630 wsetitalic();
631 break;
632 case 'o':
633 wsetbolditalic();
634 break;
635 case 'u':
636 wsetunderline();
637 break;
638 case 'p':
639 wsetplain();
640 break;
641 }
Guido van Rossum0c2290b1991-04-03 19:12:14 +0000642 INCREF(None);
643 return None;
644}
645
646static object *
647drawing_getbgcolor(self, args)
648 object *self;
649 object *args;
650{
651 if (!getnoarg(args))
652 return NULL;
653 return newintobject((long)wgetbgcolor());
654}
655
656static object *
657drawing_getfgcolor(self, args)
658 object *self;
659 object *args;
660{
661 if (!getnoarg(args))
662 return NULL;
663 return newintobject((long)wgetfgcolor());
664}
665
666static object *
667drawing_setbgcolor(self, args)
668 object *self;
669 object *args;
670{
671 long color;
672 if (!getlongarg(args, &color))
673 return NULL;
674 wsetbgcolor((COLOR)color);
675 INCREF(None);
676 return None;
677}
678
679static object *
680drawing_setfgcolor(self, args)
681 object *self;
682 object *args;
683{
684 long color;
685 if (!getlongarg(args, &color))
686 return NULL;
687 wsetfgcolor((COLOR)color);
688 INCREF(None);
689 return None;
690}
691
Guido van Rossume9066061993-07-29 13:14:32 +0000692#ifdef HAVE_BITMAPS
693
Guido van Rossumbf80e541993-02-08 15:49:17 +0000694static object *
695drawing_bitmap(self, args)
696 object *self;
697 object *args;
698{
699 int h, v;
700 object *bp;
701 object *mask = NULL;
702 if (!getargs(args, "((ii)O)", &h, &v, &bp)) {
703 err_clear();
704 if (!getargs(args, "((ii)OO)", &h, &v, &bp, &mask))
705 return NULL;
706 if (mask == None)
707 mask = NULL;
708 else if (!is_bitmapobject(mask)) {
709 err_badarg();
710 return NULL;
711 }
712 }
713 if (!is_bitmapobject(bp)) {
714 err_badarg();
715 return NULL;
716 }
717 if (((bitmapobject *)bp)->b_bitmap == NULL ||
718 mask != NULL && ((bitmapobject *)mask)->b_bitmap == NULL) {
719 err_setstr(StdwinError, "bitmap object already close");
720 return NULL;
721 }
722 if (mask == NULL)
723 wdrawbitmap(h, v, ((bitmapobject *)bp)->b_bitmap, ALLBITS);
724 else
725 wdrawbitmap(h, v,
726 ((bitmapobject *)bp)->b_bitmap,
727 ((bitmapobject *)bp)->b_bitmap);
728 INCREF(None);
729 return None;
730}
731
Guido van Rossume9066061993-07-29 13:14:32 +0000732#endif /* HAVE_BITMAPS */
733
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000734static struct methodlist drawing_methods[] = {
Guido van Rossume9066061993-07-29 13:14:32 +0000735#ifdef HAVE_BITMAPS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000736 {"bitmap", (method)drawing_bitmap},
Guido van Rossume9066061993-07-29 13:14:32 +0000737#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000738 {"box", (method)drawing_box},
739 {"circle", (method)drawing_circle},
740 {"cliprect", (method)drawing_cliprect},
741 {"close", (method)drawing_close},
742 {"elarc", (method)drawing_elarc},
743 {"enddrawing", (method)drawing_close},
744 {"erase", (method)drawing_erase},
745 {"fillcircle", (method)drawing_fillcircle},
746 {"fillelarc", (method)drawing_fillelarc},
747 {"fillpoly", (method)drawing_fillpoly},
748 {"invert", (method)drawing_invert},
749 {"line", (method)drawing_line},
750 {"noclip", (method)drawing_noclip},
751 {"paint", (method)drawing_paint},
752 {"poly", (method)drawing_poly},
753 {"shade", (method)drawing_shade},
754 {"text", (method)drawing_text},
755 {"xorcircle", (method)drawing_xorcircle},
756 {"xorelarc", (method)drawing_xorelarc},
757 {"xorline", (method)drawing_xorline},
758 {"xorpoly", (method)drawing_xorpoly},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000759
760 /* Text measuring methods: */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000761 {"baseline", (method)drawing_baseline},
762 {"lineheight", (method)drawing_lineheight},
763 {"textbreak", (method)drawing_textbreak},
764 {"textwidth", (method)drawing_textwidth},
Guido van Rossum0c2290b1991-04-03 19:12:14 +0000765
766 /* Font setting methods: */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000767 {"setfont", (method)drawing_setfont},
Guido van Rossum0c2290b1991-04-03 19:12:14 +0000768
769 /* Color methods: */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000770 {"getbgcolor", (method)drawing_getbgcolor},
771 {"getfgcolor", (method)drawing_getfgcolor},
772 {"setbgcolor", (method)drawing_setbgcolor},
773 {"setfgcolor", (method)drawing_setfgcolor},
Guido van Rossum0c2290b1991-04-03 19:12:14 +0000774
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000775 {NULL, NULL} /* sentinel */
776};
777
778static object *
Guido van Rossum77b46041992-01-14 18:41:24 +0000779drawing_getattr(dp, name)
780 drawingobject *dp;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000781 char *name;
782{
Guido van Rossum77b46041992-01-14 18:41:24 +0000783 if (dp->d_ref == NULL) {
784 err_setstr(StdwinError, "drawing object already closed");
785 return NULL;
786 }
787 return findmethod(drawing_methods, (object *)dp, name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000788}
789
Guido van Rossum541c8c01991-05-05 20:13:41 +0000790typeobject Drawingtype = {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000791 OB_HEAD_INIT(&Typetype)
792 0, /*ob_size*/
793 "drawing", /*tp_name*/
794 sizeof(drawingobject), /*tp_size*/
795 0, /*tp_itemsize*/
796 /* methods */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000797 (destructor)drawing_dealloc, /*tp_dealloc*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000798 0, /*tp_print*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000799 (getattrfunc)drawing_getattr, /*tp_getattr*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000800 0, /*tp_setattr*/
801 0, /*tp_compare*/
802 0, /*tp_repr*/
803};
804
805
806/* Text(edit) objects */
807
808typedef struct {
809 OB_HEAD
810 TEXTEDIT *t_text;
811 windowobject *t_ref;
812 object *t_attr; /* Attributes dictionary */
813} textobject;
814
Guido van Rossumb6775db1994-08-01 11:34:53 +0000815staticforward typeobject Texttype;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000816
817static textobject *
818newtextobject(wp, left, top, right, bottom)
819 windowobject *wp;
820 int left, top, right, bottom;
821{
822 textobject *tp;
823 tp = NEWOBJ(textobject, &Texttype);
824 if (tp == NULL)
825 return NULL;
826 tp->t_attr = NULL;
827 INCREF(wp);
828 tp->t_ref = wp;
829 tp->t_text = tecreate(wp->w_win, left, top, right, bottom);
830 if (tp->t_text == NULL) {
831 DECREF(tp);
832 return (textobject *) err_nomem();
833 }
834 return tp;
835}
836
837/* Text(edit) methods */
838
839static void
840text_dealloc(tp)
841 textobject *tp;
842{
843 if (tp->t_text != NULL)
844 tefree(tp->t_text);
Guido van Rossum3c284741991-11-27 14:54:54 +0000845 XDECREF(tp->t_attr);
846 XDECREF(tp->t_ref);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000847 DEL(tp);
848}
849
850static object *
Guido van Rossum3c284741991-11-27 14:54:54 +0000851text_close(tp, args)
852 textobject *tp;
853 object *args;
854{
855 if (tp->t_text != NULL) {
856 tefree(tp->t_text);
857 tp->t_text = NULL;
858 }
859 if (tp->t_attr != NULL) {
860 DECREF(tp->t_attr);
861 tp->t_attr = NULL;
862 }
863 if (tp->t_ref != NULL) {
864 DECREF(tp->t_ref);
865 tp->t_ref = NULL;
866 }
867 INCREF(None);
868 return None;
869}
870
871static object *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000872text_arrow(self, args)
873 textobject *self;
874 object *args;
875{
876 int code;
877 if (!getintarg(args, &code))
878 return NULL;
879 tearrow(self->t_text, code);
880 INCREF(None);
881 return None;
882}
883
884static object *
885text_draw(self, args)
886 textobject *self;
887 object *args;
888{
889 register TEXTEDIT *tp = self->t_text;
890 int a[4];
891 int left, top, right, bottom;
892 if (!getrectarg(args, a))
893 return NULL;
894 if (Drawing != NULL) {
Guido van Rossum87e7ea71991-12-10 14:00:03 +0000895 err_setstr(StdwinError, "already drawing");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000896 return NULL;
897 }
898 /* Clip to text area and ignore if area is empty */
899 left = tegetleft(tp);
900 top = tegettop(tp);
901 right = tegetright(tp);
902 bottom = tegetbottom(tp);
903 if (a[0] < left) a[0] = left;
904 if (a[1] < top) a[1] = top;
905 if (a[2] > right) a[2] = right;
906 if (a[3] > bottom) a[3] = bottom;
907 if (a[0] < a[2] && a[1] < a[3]) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000908 wbegindrawing(self->t_ref->w_win);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000909 tedrawnew(tp, a[0], a[1], a[2], a[3]);
910 wenddrawing(self->t_ref->w_win);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000911 }
912 INCREF(None);
913 return None;
914}
915
916static object *
917text_event(self, args)
918 textobject *self;
919 object *args;
920{
921 register TEXTEDIT *tp = self->t_text;
922 EVENT e;
923 if (!geteventarg(args, &e))
924 return NULL;
925 if (e.type == WE_MOUSE_DOWN) {
Guido van Rossum33f17701991-02-13 23:19:39 +0000926 /* Cheat at the margins */
927 int width, height;
928 wgetdocsize(e.window, &width, &height);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000929 if (e.u.where.h < 0 && tegetleft(tp) == 0)
930 e.u.where.h = 0;
Guido van Rossum33f17701991-02-13 23:19:39 +0000931 else if (e.u.where.h > width && tegetright(tp) == width)
932 e.u.where.h = width;
933 if (e.u.where.v < 0 && tegettop(tp) == 0)
934 e.u.where.v = 0;
935 else if (e.u.where.v > height && tegetright(tp) == height)
936 e.u.where.v = height;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000937 }
938 return newintobject((long) teevent(tp, &e));
939}
940
941static object *
942text_getfocus(self, args)
943 textobject *self;
944 object *args;
945{
946 if (!getnoarg(args))
947 return NULL;
948 return makepoint(tegetfoc1(self->t_text), tegetfoc2(self->t_text));
949}
950
951static object *
952text_getfocustext(self, args)
953 textobject *self;
954 object *args;
955{
956 int f1, f2;
957 char *text;
958 if (!getnoarg(args))
959 return NULL;
960 f1 = tegetfoc1(self->t_text);
961 f2 = tegetfoc2(self->t_text);
962 text = tegettext(self->t_text);
963 return newsizedstringobject(text + f1, f2-f1);
964}
965
966static object *
967text_getrect(self, args)
968 textobject *self;
969 object *args;
970{
971 if (!getnoarg(args))
972 return NULL;
973 return makerect(tegetleft(self->t_text),
974 tegettop(self->t_text),
975 tegetright(self->t_text),
976 tegetbottom(self->t_text));
977}
978
979static object *
980text_gettext(self, args)
981 textobject *self;
982 object *args;
983{
984 if (!getnoarg(args))
985 return NULL;
986 return newsizedstringobject(tegettext(self->t_text),
987 tegetlen(self->t_text));
988}
989
990static object *
991text_move(self, args)
992 textobject *self;
993 object *args;
994{
995 int a[4];
996 if (!getrectarg(args, a))
997 return NULL;
998 temovenew(self->t_text, a[0], a[1], a[2], a[3]);
999 INCREF(None);
1000 return None;
1001}
1002
1003static object *
Guido van Rossum4b9cf8e1991-05-28 21:57:04 +00001004text_replace(self, args)
1005 textobject *self;
1006 object *args;
1007{
Guido van Rossumfc58e581992-01-27 16:45:55 +00001008 char *text;
Guido van Rossum4b9cf8e1991-05-28 21:57:04 +00001009 if (!getstrarg(args, &text))
1010 return NULL;
Guido van Rossumfc58e581992-01-27 16:45:55 +00001011 tereplace(self->t_text, text);
Guido van Rossum4b9cf8e1991-05-28 21:57:04 +00001012 INCREF(None);
1013 return None;
1014}
1015
1016static object *
1017text_setactive(self, args)
1018 textobject *self;
1019 object *args;
1020{
1021 int flag;
1022 if (!getintarg(args, &flag))
1023 return NULL;
1024 tesetactive(self->t_text, flag);
1025 INCREF(None);
1026 return None;
1027}
1028
1029static object *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001030text_setfocus(self, args)
1031 textobject *self;
1032 object *args;
1033{
1034 int a[2];
1035 if (!getpointarg(args, a))
1036 return NULL;
1037 tesetfocus(self->t_text, a[0], a[1]);
1038 INCREF(None);
1039 return None;
1040}
1041
1042static object *
Guido van Rossum541c8c01991-05-05 20:13:41 +00001043text_settext(self, args)
1044 textobject *self;
1045 object *args;
1046{
Guido van Rossumfc58e581992-01-27 16:45:55 +00001047 char *text;
Guido van Rossum541c8c01991-05-05 20:13:41 +00001048 char *buf;
1049 int size;
Guido van Rossumfc58e581992-01-27 16:45:55 +00001050 if (!getargs(args, "s#", &text, &size))
Guido van Rossum541c8c01991-05-05 20:13:41 +00001051 return NULL;
Guido van Rossum541c8c01991-05-05 20:13:41 +00001052 if ((buf = NEW(char, size)) == NULL) {
Guido van Rossum87e7ea71991-12-10 14:00:03 +00001053 return err_nomem();
Guido van Rossum541c8c01991-05-05 20:13:41 +00001054 }
Guido van Rossumfc58e581992-01-27 16:45:55 +00001055 memcpy(buf, text, size);
Guido van Rossum541c8c01991-05-05 20:13:41 +00001056 tesetbuf(self->t_text, buf, size); /* Becomes owner of buffer */
1057 INCREF(None);
1058 return None;
1059}
1060
1061static object *
Guido van Rossum4b9cf8e1991-05-28 21:57:04 +00001062text_setview(self, args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001063 textobject *self;
1064 object *args;
1065{
Guido van Rossum4b9cf8e1991-05-28 21:57:04 +00001066 int a[4];
1067 if (args == None)
1068 tenoview(self->t_text);
1069 else {
1070 if (!getrectarg(args, a))
1071 return NULL;
1072 tesetview(self->t_text, a[0], a[1], a[2], a[3]);
1073 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001074 INCREF(None);
1075 return None;
1076}
1077
1078static struct methodlist text_methods[] = {
Guido van Rossumb6775db1994-08-01 11:34:53 +00001079 {"arrow", (method)text_arrow},
1080 {"close", (method)text_close},
1081 {"draw", (method)text_draw},
1082 {"event", (method)text_event},
1083 {"getfocus", (method)text_getfocus},
1084 {"getfocustext",(method)text_getfocustext},
1085 {"getrect", (method)text_getrect},
1086 {"gettext", (method)text_gettext},
1087 {"move", (method)text_move},
1088 {"replace", (method)text_replace},
1089 {"setactive", (method)text_setactive},
1090 {"setfocus", (method)text_setfocus},
1091 {"settext", (method)text_settext},
1092 {"setview", (method)text_setview},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001093 {NULL, NULL} /* sentinel */
1094};
1095
1096static object *
1097text_getattr(tp, name)
1098 textobject *tp;
1099 char *name;
1100{
Guido van Rossum85f50761991-10-20 20:22:50 +00001101 object *v = NULL;
Guido van Rossum77b46041992-01-14 18:41:24 +00001102 if (tp->t_ref == NULL) {
1103 err_setstr(StdwinError, "text object already closed");
1104 return NULL;
1105 }
Guido van Rossum85f50761991-10-20 20:22:50 +00001106 if (strcmp(name, "__dict__") == 0) {
1107 v = tp->t_attr;
1108 if (v == NULL)
1109 v = None;
1110 }
1111 else if (tp->t_attr != NULL) {
1112 v = dictlookup(tp->t_attr, name);
1113 }
1114 if (v != NULL) {
1115 INCREF(v);
1116 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001117 }
1118 return findmethod(text_methods, (object *)tp, name);
1119}
1120
1121static int
1122text_setattr(tp, name, v)
1123 textobject *tp;
1124 char *name;
1125 object *v;
1126{
1127 if (tp->t_attr == NULL) {
1128 tp->t_attr = newdictobject();
1129 if (tp->t_attr == NULL)
1130 return -1;
1131 }
Guido van Rossum94472a01992-09-04 09:45:18 +00001132 if (v == NULL) {
1133 int rv = dictremove(tp->t_attr, name);
1134 if (rv < 0)
1135 err_setstr(AttributeError,
1136 "delete non-existing text object attribute");
1137 return rv;
1138 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001139 else
1140 return dictinsert(tp->t_attr, name, v);
1141}
1142
Guido van Rossuma320fd31995-03-09 12:14:15 +00001143statichere typeobject Texttype = {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001144 OB_HEAD_INIT(&Typetype)
1145 0, /*ob_size*/
1146 "textedit", /*tp_name*/
1147 sizeof(textobject), /*tp_size*/
1148 0, /*tp_itemsize*/
1149 /* methods */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001150 (destructor)text_dealloc, /*tp_dealloc*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001151 0, /*tp_print*/
Guido van Rossumb6775db1994-08-01 11:34:53 +00001152 (getattrfunc)text_getattr, /*tp_getattr*/
1153 (setattrfunc)text_setattr, /*tp_setattr*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001154 0, /*tp_compare*/
1155 0, /*tp_repr*/
1156};
1157
1158
1159/* Menu objects */
1160
Guido van Rossum2d14e211991-02-19 12:26:49 +00001161#define IDOFFSET 10 /* Menu IDs we use start here */
Guido van Rossum27201061991-04-16 08:43:03 +00001162#define MAXNMENU 200 /* Max #menus we allow */
Guido van Rossum2d14e211991-02-19 12:26:49 +00001163static menuobject *menulist[MAXNMENU];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001164
Guido van Rossumfc58e581992-01-27 16:45:55 +00001165static menuobject *newmenuobject PROTO((char *));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001166static menuobject *
1167newmenuobject(title)
Guido van Rossumfc58e581992-01-27 16:45:55 +00001168 char *title;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001169{
1170 int id;
1171 MENU *menu;
1172 menuobject *mp;
Guido van Rossum2d14e211991-02-19 12:26:49 +00001173 for (id = 0; id < MAXNMENU; id++) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001174 if (menulist[id] == NULL)
1175 break;
1176 }
Guido van Rossum27201061991-04-16 08:43:03 +00001177 if (id >= MAXNMENU) {
Guido van Rossum87e7ea71991-12-10 14:00:03 +00001178 err_setstr(StdwinError, "creating too many menus");
Guido van Rossum27201061991-04-16 08:43:03 +00001179 return NULL;
1180 }
Guido van Rossumfc58e581992-01-27 16:45:55 +00001181 menu = wmenucreate(id + IDOFFSET, title);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001182 if (menu == NULL)
1183 return (menuobject *) err_nomem();
1184 mp = NEWOBJ(menuobject, &Menutype);
1185 if (mp != NULL) {
1186 mp->m_menu = menu;
Guido van Rossum2d14e211991-02-19 12:26:49 +00001187 mp->m_id = id + IDOFFSET;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001188 mp->m_attr = NULL;
1189 menulist[id] = mp;
1190 }
1191 else
1192 wmenudelete(menu);
1193 return mp;
1194}
1195
1196/* Menu methods */
1197
1198static void
1199menu_dealloc(mp)
1200 menuobject *mp;
1201{
1202
Guido van Rossum2d14e211991-02-19 12:26:49 +00001203 int id = mp->m_id - IDOFFSET;
1204 if (id >= 0 && id < MAXNMENU && menulist[id] == mp) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001205 menulist[id] = NULL;
1206 }
Guido van Rossum77b46041992-01-14 18:41:24 +00001207 if (mp->m_menu != NULL)
1208 wmenudelete(mp->m_menu);
1209 XDECREF(mp->m_attr);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001210 DEL(mp);
1211}
1212
1213static object *
Guido van Rossum77b46041992-01-14 18:41:24 +00001214menu_close(mp, args)
1215 menuobject *mp;
1216 object *args;
1217{
1218 int id = mp->m_id - IDOFFSET;
1219 if (id >= 0 && id < MAXNMENU && menulist[id] == mp) {
1220 menulist[id] = NULL;
1221 }
1222 mp->m_id = -1;
1223 if (mp->m_menu != NULL)
1224 wmenudelete(mp->m_menu);
1225 mp->m_menu = NULL;
1226 XDECREF(mp->m_attr);
1227 mp->m_attr = NULL;
1228 INCREF(None);
1229 return None;
1230}
1231
1232static object *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001233menu_additem(self, args)
1234 menuobject *self;
1235 object *args;
1236{
Guido van Rossumfc58e581992-01-27 16:45:55 +00001237 char *text;
1238 int shortcut = -1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001239 if (is_tupleobject(args)) {
Guido van Rossumfc58e581992-01-27 16:45:55 +00001240 char c;
1241 if (!getargs(args, "(sc)", &text, &c))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001242 return NULL;
Guido van Rossumfc58e581992-01-27 16:45:55 +00001243 shortcut = c;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001244 }
Guido van Rossumfc58e581992-01-27 16:45:55 +00001245 else if (!getstrarg(args, &text))
1246 return NULL;
1247 wmenuadditem(self->m_menu, text, shortcut);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001248 INCREF(None);
1249 return None;
1250}
1251
1252static object *
1253menu_setitem(self, args)
1254 menuobject *self;
1255 object *args;
1256{
1257 int index;
Guido van Rossumfc58e581992-01-27 16:45:55 +00001258 char *text;
Guido van Rossum234f9421993-06-17 12:35:49 +00001259 if (!getargs(args, "(is)", &index, &text))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001260 return NULL;
Guido van Rossumfc58e581992-01-27 16:45:55 +00001261 wmenusetitem(self->m_menu, index, text);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001262 INCREF(None);
1263 return None;
1264}
1265
1266static object *
1267menu_enable(self, args)
1268 menuobject *self;
1269 object *args;
1270{
1271 int index;
1272 int flag;
Guido van Rossum234f9421993-06-17 12:35:49 +00001273 if (!getargs(args, "(ii)", &index, &flag))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001274 return NULL;
1275 wmenuenable(self->m_menu, index, flag);
1276 INCREF(None);
1277 return None;
1278}
1279
1280static object *
1281menu_check(self, args)
1282 menuobject *self;
1283 object *args;
1284{
1285 int index;
1286 int flag;
Guido van Rossum234f9421993-06-17 12:35:49 +00001287 if (!getargs(args, "(ii)", &index, &flag))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001288 return NULL;
1289 wmenucheck(self->m_menu, index, flag);
1290 INCREF(None);
1291 return None;
1292}
1293
1294static struct methodlist menu_methods[] = {
Guido van Rossumb6775db1994-08-01 11:34:53 +00001295 {"additem", (method)menu_additem},
1296 {"setitem", (method)menu_setitem},
1297 {"enable", (method)menu_enable},
1298 {"check", (method)menu_check},
1299 {"close", (method)menu_close},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001300 {NULL, NULL} /* sentinel */
1301};
1302
1303static object *
1304menu_getattr(mp, name)
1305 menuobject *mp;
1306 char *name;
1307{
Guido van Rossum85f50761991-10-20 20:22:50 +00001308 object *v = NULL;
Guido van Rossum77b46041992-01-14 18:41:24 +00001309 if (mp->m_menu == NULL) {
1310 err_setstr(StdwinError, "menu object already closed");
1311 return NULL;
1312 }
Guido van Rossum85f50761991-10-20 20:22:50 +00001313 if (strcmp(name, "__dict__") == 0) {
1314 v = mp->m_attr;
1315 if (v == NULL)
1316 v = None;
1317 }
1318 else if (mp->m_attr != NULL) {
1319 v = dictlookup(mp->m_attr, name);
1320 }
1321 if (v != NULL) {
1322 INCREF(v);
1323 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001324 }
1325 return findmethod(menu_methods, (object *)mp, name);
1326}
1327
1328static int
1329menu_setattr(mp, name, v)
1330 menuobject *mp;
1331 char *name;
1332 object *v;
1333{
1334 if (mp->m_attr == NULL) {
1335 mp->m_attr = newdictobject();
1336 if (mp->m_attr == NULL)
1337 return -1;
1338 }
Guido van Rossum94472a01992-09-04 09:45:18 +00001339 if (v == NULL) {
1340 int rv = dictremove(mp->m_attr, name);
1341 if (rv < 0)
1342 err_setstr(AttributeError,
1343 "delete non-existing menu object attribute");
1344 return rv;
1345 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001346 else
1347 return dictinsert(mp->m_attr, name, v);
1348}
1349
Guido van Rossuma320fd31995-03-09 12:14:15 +00001350statichere typeobject Menutype = {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001351 OB_HEAD_INIT(&Typetype)
1352 0, /*ob_size*/
1353 "menu", /*tp_name*/
1354 sizeof(menuobject), /*tp_size*/
1355 0, /*tp_itemsize*/
1356 /* methods */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001357 (destructor)menu_dealloc, /*tp_dealloc*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001358 0, /*tp_print*/
Guido van Rossumb6775db1994-08-01 11:34:53 +00001359 (getattrfunc)menu_getattr, /*tp_getattr*/
1360 (setattrfunc)menu_setattr, /*tp_setattr*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001361 0, /*tp_compare*/
1362 0, /*tp_repr*/
1363};
1364
1365
Guido van Rossume9066061993-07-29 13:14:32 +00001366#ifdef HAVE_BITMAPS
1367
Guido van Rossumbf80e541993-02-08 15:49:17 +00001368/* Bitmaps objects */
1369
1370static bitmapobject *newbitmapobject PROTO((int, int));
1371static bitmapobject *
1372newbitmapobject(width, height)
1373 int width, height;
1374{
1375 BITMAP *bitmap;
1376 bitmapobject *bp;
1377 bitmap = wnewbitmap(width, height);
1378 if (bitmap == NULL)
1379 return (bitmapobject *) err_nomem();
1380 bp = NEWOBJ(bitmapobject, &Bitmaptype);
1381 if (bp != NULL) {
1382 bp->b_bitmap = bitmap;
1383 bp->b_attr = NULL;
1384 }
1385 else
1386 wfreebitmap(bitmap);
1387 return bp;
1388}
1389
1390/* Bitmap methods */
1391
1392static void
1393bitmap_dealloc(bp)
1394 bitmapobject *bp;
1395{
1396 if (bp->b_bitmap != NULL)
1397 wfreebitmap(bp->b_bitmap);
1398 XDECREF(bp->b_attr);
1399 DEL(bp);
1400}
1401
1402static object *
1403bitmap_close(bp, args)
1404 bitmapobject *bp;
1405 object *args;
1406{
1407 if (bp->b_bitmap != NULL)
1408 wfreebitmap(bp->b_bitmap);
1409 bp->b_bitmap = NULL;
1410 XDECREF(bp->b_attr);
1411 bp->b_attr = NULL;
1412 INCREF(None);
1413 return None;
1414}
1415
1416static object *
1417bitmap_setbit(self, args)
1418 bitmapobject *self;
1419 object *args;
1420{
1421 int a[3];
1422 if (!getpointintarg(args, a))
1423 return NULL;
1424 wsetbit(self->b_bitmap, a[0], a[1], a[2]);
1425 INCREF(None);
1426 return None;
1427}
1428
1429static object *
1430bitmap_getbit(self, args)
1431 bitmapobject *self;
1432 object *args;
1433{
1434 int a[2];
1435 if (!getpointarg(args, a))
1436 return NULL;
1437 return newintobject((long) wgetbit(self->b_bitmap, a[0], a[1]));
1438}
1439
1440static object *
1441bitmap_getsize(self, args)
1442 bitmapobject *self;
1443 object *args;
1444{
1445 int width, height;
1446 if (!getnoarg(args))
1447 return NULL;
1448 wgetbitmapsize(self->b_bitmap, &width, &height);
1449 return mkvalue("(ii)", width, height);
1450}
1451
1452static struct methodlist bitmap_methods[] = {
Guido van Rossumb6775db1994-08-01 11:34:53 +00001453 {"close", (method)bitmap_close},
1454 {"getsize", (method)bitmap_getsize},
1455 {"getbit", (method)bitmap_getbit},
1456 {"setbit", (method)bitmap_setbit},
Guido van Rossumbf80e541993-02-08 15:49:17 +00001457 {NULL, NULL} /* sentinel */
1458};
1459
1460static object *
1461bitmap_getattr(bp, name)
1462 bitmapobject *bp;
1463 char *name;
1464{
1465 object *v = NULL;
1466 if (bp->b_bitmap == NULL) {
1467 err_setstr(StdwinError, "bitmap object already closed");
1468 return NULL;
1469 }
1470 if (strcmp(name, "__dict__") == 0) {
1471 v = bp->b_attr;
1472 if (v == NULL)
1473 v = None;
1474 }
1475 else if (bp->b_attr != NULL) {
1476 v = dictlookup(bp->b_attr, name);
1477 }
1478 if (v != NULL) {
1479 INCREF(v);
1480 return v;
1481 }
1482 return findmethod(bitmap_methods, (object *)bp, name);
1483}
1484
1485static int
1486bitmap_setattr(bp, name, v)
1487 bitmapobject *bp;
1488 char *name;
1489 object *v;
1490{
1491 if (bp->b_attr == NULL) {
1492 bp->b_attr = newdictobject();
1493 if (bp->b_attr == NULL)
1494 return -1;
1495 }
1496 if (v == NULL) {
1497 int rv = dictremove(bp->b_attr, name);
1498 if (rv < 0)
1499 err_setstr(AttributeError,
1500 "delete non-existing bitmap object attribute");
1501 return rv;
1502 }
1503 else
1504 return dictinsert(bp->b_attr, name, v);
1505}
1506
Guido van Rossuma320fd31995-03-09 12:14:15 +00001507statichere typeobject Bitmaptype = {
Guido van Rossumbf80e541993-02-08 15:49:17 +00001508 OB_HEAD_INIT(&Typetype)
1509 0, /*ob_size*/
1510 "bitmap", /*tp_name*/
1511 sizeof(bitmapobject), /*tp_size*/
1512 0, /*tp_itemsize*/
1513 /* methods */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001514 (destructor)bitmap_dealloc, /*tp_dealloc*/
Guido van Rossumbf80e541993-02-08 15:49:17 +00001515 0, /*tp_print*/
Guido van Rossumb6775db1994-08-01 11:34:53 +00001516 (getattrfunc)bitmap_getattr, /*tp_getattr*/
1517 (setattrfunc)bitmap_setattr, /*tp_setattr*/
Guido van Rossumbf80e541993-02-08 15:49:17 +00001518 0, /*tp_compare*/
1519 0, /*tp_repr*/
1520};
1521
Guido van Rossume9066061993-07-29 13:14:32 +00001522#endif /* HAVE_BITMAPS */
1523
Guido van Rossumbf80e541993-02-08 15:49:17 +00001524
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001525/* Windows */
1526
1527#define MAXNWIN 50
1528static windowobject *windowlist[MAXNWIN];
1529
1530/* Window methods */
1531
1532static void
1533window_dealloc(wp)
1534 windowobject *wp;
1535{
1536 if (wp->w_win != NULL) {
1537 int tag = wgettag(wp->w_win);
1538 if (tag >= 0 && tag < MAXNWIN)
1539 windowlist[tag] = NULL;
1540 else
1541 fprintf(stderr, "XXX help! tag %d in window_dealloc\n",
1542 tag);
1543 wclose(wp->w_win);
1544 }
1545 DECREF(wp->w_title);
1546 if (wp->w_attr != NULL)
1547 DECREF(wp->w_attr);
1548 free((char *)wp);
1549}
1550
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001551static object *
Guido van Rossum3c284741991-11-27 14:54:54 +00001552window_close(wp, args)
1553 windowobject *wp;
1554 object *args;
1555{
1556 if (wp->w_win != NULL) {
1557 int tag = wgettag(wp->w_win);
1558 if (tag >= 0 && tag < MAXNWIN)
1559 windowlist[tag] = NULL;
1560 wclose(wp->w_win);
1561 wp->w_win = NULL;
1562 }
1563 INCREF(None);
1564 return None;
1565}
1566
1567static object *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001568window_begindrawing(wp, args)
1569 windowobject *wp;
1570 object *args;
1571{
1572 drawingobject *dp;
1573 if (!getnoarg(args))
1574 return NULL;
1575 if (Drawing != NULL) {
Guido van Rossum87e7ea71991-12-10 14:00:03 +00001576 err_setstr(StdwinError, "already drawing");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001577 return NULL;
1578 }
1579 dp = NEWOBJ(drawingobject, &Drawingtype);
1580 if (dp == NULL)
1581 return NULL;
1582 Drawing = dp;
1583 INCREF(wp);
1584 dp->d_ref = wp;
1585 wbegindrawing(wp->w_win);
1586 return (object *)dp;
1587}
1588
1589static object *
1590window_change(wp, args)
1591 windowobject *wp;
1592 object *args;
1593{
1594 int a[4];
1595 if (!getrectarg(args, a))
1596 return NULL;
1597 wchange(wp->w_win, a[0], a[1], a[2], a[3]);
1598 INCREF(None);
1599 return None;
1600}
1601
1602static object *
1603window_gettitle(wp, args)
1604 windowobject *wp;
1605 object *args;
1606{
1607 if (!getnoarg(args))
1608 return NULL;
1609 INCREF(wp->w_title);
1610 return wp->w_title;
1611}
1612
1613static object *
Guido van Rossum541c8c01991-05-05 20:13:41 +00001614window_getwinpos(wp, args)
1615 windowobject *wp;
1616 object *args;
1617{
1618 int h, v;
1619 if (!getnoarg(args))
1620 return NULL;
1621 wgetwinpos(wp->w_win, &h, &v);
1622 return makepoint(h, v);
1623}
1624
1625static object *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001626window_getwinsize(wp, args)
1627 windowobject *wp;
1628 object *args;
1629{
1630 int width, height;
1631 if (!getnoarg(args))
1632 return NULL;
1633 wgetwinsize(wp->w_win, &width, &height);
1634 return makepoint(width, height);
1635}
1636
1637static object *
Guido van Rossumbf80e541993-02-08 15:49:17 +00001638window_setwinpos(wp, args)
1639 windowobject *wp;
1640 object *args;
1641{
1642 int a[2];
1643 if (!getpointarg(args, a))
1644 return NULL;
1645 wsetwinpos(wp->w_win, a[0], a[1]);
1646 INCREF(None);
1647 return None;
1648}
1649
1650static object *
1651window_setwinsize(wp, args)
1652 windowobject *wp;
1653 object *args;
1654{
1655 int a[2];
1656 if (!getpointarg(args, a))
1657 return NULL;
1658 wsetwinsize(wp->w_win, a[0], a[1]);
1659 INCREF(None);
1660 return None;
1661}
1662
1663static object *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001664window_getdocsize(wp, args)
1665 windowobject *wp;
1666 object *args;
1667{
1668 int width, height;
1669 if (!getnoarg(args))
1670 return NULL;
1671 wgetdocsize(wp->w_win, &width, &height);
1672 return makepoint(width, height);
1673}
1674
1675static object *
1676window_getorigin(wp, args)
1677 windowobject *wp;
1678 object *args;
1679{
1680 int width, height;
1681 if (!getnoarg(args))
1682 return NULL;
1683 wgetorigin(wp->w_win, &width, &height);
1684 return makepoint(width, height);
1685}
1686
1687static object *
1688window_scroll(wp, args)
1689 windowobject *wp;
1690 object *args;
1691{
1692 int a[6];
1693 if (!getrectpointarg(args, a))
1694 return NULL;
1695 wscroll(wp->w_win, a[0], a[1], a[2], a[3], a[4], a[5]);
1696 INCREF(None);
1697 return None;
1698}
1699
1700static object *
1701window_setdocsize(wp, args)
1702 windowobject *wp;
1703 object *args;
1704{
1705 int a[2];
1706 if (!getpointarg(args, a))
1707 return NULL;
1708 wsetdocsize(wp->w_win, a[0], a[1]);
1709 INCREF(None);
1710 return None;
1711}
1712
1713static object *
1714window_setorigin(wp, args)
1715 windowobject *wp;
1716 object *args;
1717{
1718 int a[2];
1719 if (!getpointarg(args, a))
1720 return NULL;
1721 wsetorigin(wp->w_win, a[0], a[1]);
1722 INCREF(None);
1723 return None;
1724}
1725
1726static object *
1727window_settitle(wp, args)
1728 windowobject *wp;
1729 object *args;
1730{
1731 object *title;
Guido van Rossum234f9421993-06-17 12:35:49 +00001732 if (!getargs(args, "S", &title))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001733 return NULL;
1734 DECREF(wp->w_title);
1735 INCREF(title);
1736 wp->w_title = title;
1737 wsettitle(wp->w_win, getstringvalue(title));
1738 INCREF(None);
1739 return None;
1740}
1741
1742static object *
1743window_show(wp, args)
1744 windowobject *wp;
1745 object *args;
1746{
1747 int a[4];
1748 if (!getrectarg(args, a))
1749 return NULL;
1750 wshow(wp->w_win, a[0], a[1], a[2], a[3]);
1751 INCREF(None);
1752 return None;
1753}
1754
1755static object *
1756window_settimer(wp, args)
1757 windowobject *wp;
1758 object *args;
1759{
1760 int a;
1761 if (!getintarg(args, &a))
1762 return NULL;
1763 wsettimer(wp->w_win, a);
1764 INCREF(None);
1765 return None;
1766}
1767
1768static object *
1769window_menucreate(self, args)
1770 windowobject *self;
1771 object *args;
1772{
1773 menuobject *mp;
Guido van Rossumfc58e581992-01-27 16:45:55 +00001774 char *title;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001775 if (!getstrarg(args, &title))
1776 return NULL;
1777 wmenusetdeflocal(1);
1778 mp = newmenuobject(title);
1779 if (mp == NULL)
1780 return NULL;
1781 wmenuattach(self->w_win, mp->m_menu);
1782 return (object *)mp;
1783}
1784
1785static object *
1786window_textcreate(self, args)
1787 windowobject *self;
1788 object *args;
1789{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001790 int a[4];
1791 if (!getrectarg(args, a))
1792 return NULL;
1793 return (object *)
1794 newtextobject(self, a[0], a[1], a[2], a[3]);
1795}
1796
Guido van Rossum5b10f451990-10-30 16:01:48 +00001797static object *
1798window_setselection(self, args)
1799 windowobject *self;
1800 object *args;
1801{
Guido van Rossumfc58e581992-01-27 16:45:55 +00001802 int sel, size, ok;
1803 char *text;
1804 if (!getargs(args, "(is#)", &sel, &text, &size))
Guido van Rossum5b10f451990-10-30 16:01:48 +00001805 return NULL;
Guido van Rossumfc58e581992-01-27 16:45:55 +00001806 ok = wsetselection(self->w_win, sel, text, size);
Guido van Rossum5b10f451990-10-30 16:01:48 +00001807 return newintobject(ok);
1808}
1809
1810static object *
1811window_setwincursor(self, args)
1812 windowobject *self;
1813 object *args;
1814{
Guido van Rossumfc58e581992-01-27 16:45:55 +00001815 char *name;
Guido van Rossum5b10f451990-10-30 16:01:48 +00001816 CURSOR *c;
Guido van Rossum3c8ba7a1992-02-05 11:15:00 +00001817 if (!getargs(args, "z", &name))
Guido van Rossum5b10f451990-10-30 16:01:48 +00001818 return NULL;
Guido van Rossum3c8ba7a1992-02-05 11:15:00 +00001819 if (name == NULL)
1820 c = NULL;
1821 else {
1822 c = wfetchcursor(name);
1823 if (c == NULL) {
1824 err_setstr(StdwinError, "no such cursor");
1825 return NULL;
1826 }
Guido van Rossum5b10f451990-10-30 16:01:48 +00001827 }
1828 wsetwincursor(self->w_win, c);
1829 INCREF(None);
1830 return None;
1831}
1832
Guido van Rossumfc58e581992-01-27 16:45:55 +00001833static object *
1834window_setactive(self, args)
1835 windowobject *self;
1836 object *args;
1837{
1838 if (!getnoarg(args))
1839 return NULL;
1840 wsetactive(self->w_win);
1841 INCREF(None);
1842 return None;
1843}
1844
Guido van Rossum8dcbbac1991-07-27 21:42:24 +00001845#ifdef CWI_HACKS
1846static object *
1847window_getxwindowid(self, args)
1848 windowobject *self;
1849 object *args;
1850{
1851 long wid = wgetxwindowid(self->w_win);
1852 return newintobject(wid);
1853}
1854#endif
1855
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001856static struct methodlist window_methods[] = {
Guido van Rossumb6775db1994-08-01 11:34:53 +00001857 {"begindrawing",(method)window_begindrawing},
1858 {"change", (method)window_change},
1859 {"close", (method)window_close},
1860 {"getdocsize", (method)window_getdocsize},
1861 {"getorigin", (method)window_getorigin},
1862 {"gettitle", (method)window_gettitle},
1863 {"getwinpos", (method)window_getwinpos},
1864 {"getwinsize", (method)window_getwinsize},
1865 {"menucreate", (method)window_menucreate},
1866 {"scroll", (method)window_scroll},
1867 {"setactive", (method)window_setactive},
1868 {"setdocsize", (method)window_setdocsize},
1869 {"setorigin", (method)window_setorigin},
1870 {"setselection",(method)window_setselection},
1871 {"settimer", (method)window_settimer},
1872 {"settitle", (method)window_settitle},
1873 {"setwincursor",(method)window_setwincursor},
1874 {"setwinpos", (method)window_setwinpos},
1875 {"setwinsize", (method)window_setwinsize},
1876 {"show", (method)window_show},
1877 {"textcreate", (method)window_textcreate},
Guido van Rossum8dcbbac1991-07-27 21:42:24 +00001878#ifdef CWI_HACKS
Guido van Rossumb6775db1994-08-01 11:34:53 +00001879 {"getxwindowid",(method)window_getxwindowid},
Guido van Rossum8dcbbac1991-07-27 21:42:24 +00001880#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001881 {NULL, NULL} /* sentinel */
1882};
1883
1884static object *
1885window_getattr(wp, name)
1886 windowobject *wp;
1887 char *name;
1888{
Guido van Rossum85f50761991-10-20 20:22:50 +00001889 object *v = NULL;
Guido van Rossum77b46041992-01-14 18:41:24 +00001890 if (wp->w_win == NULL) {
1891 err_setstr(StdwinError, "window already closed");
1892 return NULL;
1893 }
Guido van Rossum85f50761991-10-20 20:22:50 +00001894 if (strcmp(name, "__dict__") == 0) {
1895 v = wp->w_attr;
1896 if (v == NULL)
1897 v = None;
1898 }
1899 else if (wp->w_attr != NULL) {
1900 v = dictlookup(wp->w_attr, name);
1901 }
1902 if (v != NULL) {
1903 INCREF(v);
1904 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001905 }
1906 return findmethod(window_methods, (object *)wp, name);
1907}
1908
1909static int
1910window_setattr(wp, name, v)
1911 windowobject *wp;
1912 char *name;
1913 object *v;
1914{
1915 if (wp->w_attr == NULL) {
1916 wp->w_attr = newdictobject();
1917 if (wp->w_attr == NULL)
1918 return -1;
1919 }
Guido van Rossum94472a01992-09-04 09:45:18 +00001920 if (v == NULL) {
1921 int rv = dictremove(wp->w_attr, name);
1922 if (rv < 0)
1923 err_setstr(AttributeError,
1924 "delete non-existing menu object attribute");
1925 return rv;
1926 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001927 else
1928 return dictinsert(wp->w_attr, name, v);
1929}
1930
Guido van Rossuma320fd31995-03-09 12:14:15 +00001931statichere typeobject Windowtype = {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001932 OB_HEAD_INIT(&Typetype)
1933 0, /*ob_size*/
1934 "window", /*tp_name*/
1935 sizeof(windowobject), /*tp_size*/
1936 0, /*tp_itemsize*/
1937 /* methods */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001938 (destructor)window_dealloc, /*tp_dealloc*/
Guido van Rossum7066dd71992-09-17 17:54:56 +00001939 0, /*tp_print*/
Guido van Rossumb6775db1994-08-01 11:34:53 +00001940 (getattrfunc)window_getattr, /*tp_getattr*/
1941 (setattrfunc)window_setattr, /*tp_setattr*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001942 0, /*tp_compare*/
1943 0, /*tp_repr*/
1944};
1945
1946/* Stdwin methods */
1947
1948static object *
Guido van Rossumcacd9571993-10-18 11:44:47 +00001949stdwin_done(sw, args)
1950 object *sw;
1951 object *args;
1952{
1953 if (!getnoarg(args))
1954 return NULL;
1955 wdone();
1956 /* XXX There is no protection against continued use of
1957 XXX stdwin functions or objects after this call is made.
1958 XXX Use at own risk */
1959 INCREF(None);
1960 return None;
1961}
1962
1963static object *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001964stdwin_open(sw, args)
1965 object *sw;
1966 object *args;
1967{
1968 int tag;
1969 object *title;
1970 windowobject *wp;
Guido van Rossum234f9421993-06-17 12:35:49 +00001971 if (!getargs(args, "S", &title))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001972 return NULL;
1973 for (tag = 0; tag < MAXNWIN; tag++) {
1974 if (windowlist[tag] == NULL)
1975 break;
1976 }
Guido van Rossum27201061991-04-16 08:43:03 +00001977 if (tag >= MAXNWIN) {
Guido van Rossum87e7ea71991-12-10 14:00:03 +00001978 err_setstr(StdwinError, "creating too many windows");
Guido van Rossum27201061991-04-16 08:43:03 +00001979 return NULL;
1980 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001981 wp = NEWOBJ(windowobject, &Windowtype);
1982 if (wp == NULL)
1983 return NULL;
1984 INCREF(title);
1985 wp->w_title = title;
1986 wp->w_win = wopen(getstringvalue(title), (void (*)()) NULL);
1987 wp->w_attr = NULL;
1988 if (wp->w_win == NULL) {
1989 DECREF(wp);
1990 return NULL;
1991 }
1992 windowlist[tag] = wp;
1993 wsettag(wp->w_win, tag);
1994 return (object *)wp;
1995}
1996
1997static object *
Guido van Rossum246b9d81991-06-03 10:55:14 +00001998window2object(win)
1999 WINDOW *win;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002000{
Guido van Rossum246b9d81991-06-03 10:55:14 +00002001 object *w;
2002 if (win == NULL)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002003 w = None;
2004 else {
Guido van Rossum246b9d81991-06-03 10:55:14 +00002005 int tag = wgettag(win);
2006 if (tag < 0 || tag >= MAXNWIN || windowlist[tag] == NULL ||
2007 windowlist[tag]->w_win != win)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002008 w = None;
2009 else
2010 w = (object *)windowlist[tag];
2011 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002012 INCREF(w);
Guido van Rossum246b9d81991-06-03 10:55:14 +00002013 return w;
2014}
2015
2016static object *
2017stdwin_get_poll_event(poll, args)
2018 int poll;
2019 object *args;
2020{
2021 EVENT e;
Guido van Rossum2ee12f41992-04-13 15:54:35 +00002022 object *u, *v, *w;
Guido van Rossum246b9d81991-06-03 10:55:14 +00002023 if (!getnoarg(args))
2024 return NULL;
2025 if (Drawing != NULL) {
Guido van Rossum87e7ea71991-12-10 14:00:03 +00002026 err_setstr(StdwinError, "cannot getevent() while drawing");
Guido van Rossum246b9d81991-06-03 10:55:14 +00002027 return NULL;
2028 }
2029 again:
Guido van Rossumff4949e1992-08-05 19:58:53 +00002030 BGN_STDWIN
Guido van Rossum246b9d81991-06-03 10:55:14 +00002031 if (poll) {
2032 if (!wpollevent(&e)) {
Guido van Rossumff4949e1992-08-05 19:58:53 +00002033 RET_STDWIN
Guido van Rossum246b9d81991-06-03 10:55:14 +00002034 INCREF(None);
2035 return None;
2036 }
2037 }
2038 else
2039 wgetevent(&e);
Guido van Rossumff4949e1992-08-05 19:58:53 +00002040 END_STDWIN
Guido van Rossum246b9d81991-06-03 10:55:14 +00002041 if (e.type == WE_COMMAND && e.u.command == WC_CANCEL) {
2042 /* Turn keyboard interrupts into exceptions */
2043 err_set(KeyboardInterrupt);
2044 return NULL;
2045 }
2046 if (e.type == WE_COMMAND && e.u.command == WC_CLOSE) {
2047 /* Turn WC_CLOSE commands into WE_CLOSE events */
2048 e.type = WE_CLOSE;
2049 }
Guido van Rossum2ee12f41992-04-13 15:54:35 +00002050 v = window2object(e.window);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002051 switch (e.type) {
2052 case WE_CHAR:
2053 {
2054 char c[1];
2055 c[0] = e.u.character;
2056 w = newsizedstringobject(c, 1);
2057 }
2058 break;
2059 case WE_COMMAND:
2060 w = newintobject((long)e.u.command);
2061 break;
2062 case WE_DRAW:
2063 w = makerect(e.u.area.left, e.u.area.top,
2064 e.u.area.right, e.u.area.bottom);
2065 break;
2066 case WE_MOUSE_DOWN:
2067 case WE_MOUSE_MOVE:
2068 case WE_MOUSE_UP:
Guido van Rossum2ee12f41992-04-13 15:54:35 +00002069 w = mkvalue("((ii)iii)",
2070 e.u.where.h, e.u.where.v,
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002071 e.u.where.clicks,
2072 e.u.where.button,
2073 e.u.where.mask);
2074 break;
2075 case WE_MENU:
Guido van Rossum2d14e211991-02-19 12:26:49 +00002076 if (e.u.m.id >= IDOFFSET && e.u.m.id < IDOFFSET+MAXNMENU &&
2077 menulist[e.u.m.id - IDOFFSET] != NULL)
Guido van Rossum2ee12f41992-04-13 15:54:35 +00002078 w = mkvalue("(Oi)",
2079 menulist[e.u.m.id - IDOFFSET], e.u.m.item);
Guido van Rossum246b9d81991-06-03 10:55:14 +00002080 else {
2081 /* Ghost menu event.
2082 Can occur only on the Mac if another part
2083 of the aplication has installed a menu;
2084 like the THINK C console library. */
2085 DECREF(v);
2086 goto again;
2087 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002088 break;
Guido van Rossum3ee199e1992-06-30 12:48:26 +00002089 case WE_KEY:
2090 w = mkvalue("(ii)", e.u.key.code, e.u.key.mask);
2091 break;
Guido van Rossum5b10f451990-10-30 16:01:48 +00002092 case WE_LOST_SEL:
2093 w = newintobject((long)e.u.sel);
2094 break;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002095 default:
2096 w = None;
2097 INCREF(w);
2098 break;
2099 }
2100 if (w == NULL) {
2101 DECREF(v);
2102 return NULL;
2103 }
Guido van Rossum2ee12f41992-04-13 15:54:35 +00002104 u = mkvalue("(iOO)", e.type, v, w);
2105 XDECREF(v);
2106 XDECREF(w);
2107 return u;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002108}
2109
2110static object *
Guido van Rossume8e7cf41991-01-16 14:06:18 +00002111stdwin_getevent(sw, args)
2112 object *sw;
2113 object *args;
2114{
2115 return stdwin_get_poll_event(0, args);
2116}
2117
2118static object *
2119stdwin_pollevent(sw, args)
2120 object *sw;
2121 object *args;
2122{
2123 return stdwin_get_poll_event(1, args);
2124}
2125
2126static object *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002127stdwin_setdefwinpos(sw, args)
2128 object *sw;
2129 object *args;
2130{
2131 int a[2];
2132 if (!getpointarg(args, a))
2133 return NULL;
2134 wsetdefwinpos(a[0], a[1]);
2135 INCREF(None);
2136 return None;
2137}
2138
2139static object *
2140stdwin_setdefwinsize(sw, args)
2141 object *sw;
2142 object *args;
2143{
2144 int a[2];
2145 if (!getpointarg(args, a))
2146 return NULL;
2147 wsetdefwinsize(a[0], a[1]);
2148 INCREF(None);
2149 return None;
2150}
2151
2152static object *
Guido van Rossum0c2290b1991-04-03 19:12:14 +00002153stdwin_setdefscrollbars(sw, args)
2154 object *sw;
2155 object *args;
2156{
2157 int a[2];
2158 if (!getpointarg(args, a))
2159 return NULL;
2160 wsetdefscrollbars(a[0], a[1]);
2161 INCREF(None);
2162 return None;
2163}
2164
2165static object *
Guido van Rossum541c8c01991-05-05 20:13:41 +00002166stdwin_getdefwinpos(self, args)
2167 object *self;
Guido van Rossum33f17701991-02-13 23:19:39 +00002168 object *args;
2169{
2170 int h, v;
2171 if (!getnoarg(args))
2172 return NULL;
2173 wgetdefwinpos(&h, &v);
2174 return makepoint(h, v);
2175}
2176
2177static object *
Guido van Rossum541c8c01991-05-05 20:13:41 +00002178stdwin_getdefwinsize(self, args)
2179 object *self;
Guido van Rossum33f17701991-02-13 23:19:39 +00002180 object *args;
2181{
2182 int width, height;
2183 if (!getnoarg(args))
2184 return NULL;
2185 wgetdefwinsize(&width, &height);
2186 return makepoint(width, height);
2187}
2188
2189static object *
Guido van Rossum541c8c01991-05-05 20:13:41 +00002190stdwin_getdefscrollbars(self, args)
2191 object *self;
Guido van Rossum0c2290b1991-04-03 19:12:14 +00002192 object *args;
2193{
2194 int h, v;
2195 if (!getnoarg(args))
2196 return NULL;
2197 wgetdefscrollbars(&h, &v);
2198 return makepoint(h, v);
2199}
2200
2201static object *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002202stdwin_menucreate(self, args)
2203 object *self;
2204 object *args;
2205{
Guido van Rossumfc58e581992-01-27 16:45:55 +00002206 char *title;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002207 if (!getstrarg(args, &title))
2208 return NULL;
2209 wmenusetdeflocal(0);
2210 return (object *)newmenuobject(title);
2211}
2212
2213static object *
2214stdwin_askfile(self, args)
2215 object *self;
2216 object *args;
2217{
Guido van Rossumfc58e581992-01-27 16:45:55 +00002218 char *prompt, *dflt;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002219 int new, ret;
2220 char buf[256];
Guido van Rossum234f9421993-06-17 12:35:49 +00002221 if (!getargs(args, "(ssi)", &prompt, &dflt, &new))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002222 return NULL;
Guido van Rossumfc58e581992-01-27 16:45:55 +00002223 strncpy(buf, dflt, sizeof buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002224 buf[sizeof buf - 1] = '\0';
Guido van Rossumff4949e1992-08-05 19:58:53 +00002225 BGN_STDWIN
Guido van Rossumfc58e581992-01-27 16:45:55 +00002226 ret = waskfile(prompt, buf, sizeof buf, new);
Guido van Rossumff4949e1992-08-05 19:58:53 +00002227 END_STDWIN
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002228 if (!ret) {
2229 err_set(KeyboardInterrupt);
2230 return NULL;
2231 }
2232 return newstringobject(buf);
2233}
2234
2235static object *
2236stdwin_askync(self, args)
2237 object *self;
2238 object *args;
2239{
Guido van Rossumfc58e581992-01-27 16:45:55 +00002240 char *prompt;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002241 int new, ret;
Guido van Rossum234f9421993-06-17 12:35:49 +00002242 if (!getargs(args, "(si)", &prompt, &new))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002243 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002244 BGN_STDWIN
Guido van Rossumfc58e581992-01-27 16:45:55 +00002245 ret = waskync(prompt, new);
Guido van Rossumff4949e1992-08-05 19:58:53 +00002246 END_STDWIN
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002247 if (ret < 0) {
2248 err_set(KeyboardInterrupt);
2249 return NULL;
2250 }
2251 return newintobject((long)ret);
2252}
2253
2254static object *
2255stdwin_askstr(self, args)
2256 object *self;
2257 object *args;
2258{
Guido van Rossumfc58e581992-01-27 16:45:55 +00002259 char *prompt, *dflt;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002260 int ret;
2261 char buf[256];
Guido van Rossum234f9421993-06-17 12:35:49 +00002262 if (!getargs(args, "(ss)", &prompt, &dflt))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002263 return NULL;
Guido van Rossumfc58e581992-01-27 16:45:55 +00002264 strncpy(buf, dflt, sizeof buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002265 buf[sizeof buf - 1] = '\0';
Guido van Rossumff4949e1992-08-05 19:58:53 +00002266 BGN_STDWIN
Guido van Rossumfc58e581992-01-27 16:45:55 +00002267 ret = waskstr(prompt, buf, sizeof buf);
Guido van Rossumff4949e1992-08-05 19:58:53 +00002268 END_STDWIN
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002269 if (!ret) {
2270 err_set(KeyboardInterrupt);
2271 return NULL;
2272 }
2273 return newstringobject(buf);
2274}
2275
2276static object *
2277stdwin_message(self, args)
2278 object *self;
2279 object *args;
2280{
Guido van Rossumfc58e581992-01-27 16:45:55 +00002281 char *msg;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002282 if (!getstrarg(args, &msg))
2283 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002284 BGN_STDWIN
Guido van Rossumfc58e581992-01-27 16:45:55 +00002285 wmessage(msg);
Guido van Rossumff4949e1992-08-05 19:58:53 +00002286 END_STDWIN
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002287 INCREF(None);
2288 return None;
2289}
2290
2291static object *
2292stdwin_fleep(self, args)
2293 object *self;
2294 object *args;
2295{
2296 if (!getnoarg(args))
2297 return NULL;
2298 wfleep();
2299 INCREF(None);
2300 return None;
2301}
2302
2303static object *
2304stdwin_setcutbuffer(self, args)
2305 object *self;
2306 object *args;
2307{
Guido van Rossumfc58e581992-01-27 16:45:55 +00002308 int i, size;
2309 char *str;
2310 if (!getargs(args, "(is#)", &i, &str, &size))
Guido van Rossum124967c1990-11-06 15:17:35 +00002311 return NULL;
Guido van Rossumfc58e581992-01-27 16:45:55 +00002312 wsetcutbuffer(i, str, size);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002313 INCREF(None);
2314 return None;
2315}
2316
2317static object *
Guido van Rossum246b9d81991-06-03 10:55:14 +00002318stdwin_getactive(self, args)
2319 object *self;
2320 object *args;
2321{
2322 return window2object(wgetactive());
2323}
2324
2325static object *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002326stdwin_getcutbuffer(self, args)
2327 object *self;
2328 object *args;
2329{
Guido van Rossum5b10f451990-10-30 16:01:48 +00002330 int i;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002331 char *str;
Guido van Rossum01769f01990-10-30 13:39:00 +00002332 int len;
Guido van Rossum124967c1990-11-06 15:17:35 +00002333 if (!getintarg(args, &i))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002334 return NULL;
Guido van Rossum5b10f451990-10-30 16:01:48 +00002335 str = wgetcutbuffer(i, &len);
Guido van Rossum01769f01990-10-30 13:39:00 +00002336 if (str == NULL) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002337 str = "";
Guido van Rossum01769f01990-10-30 13:39:00 +00002338 len = 0;
2339 }
2340 return newsizedstringobject(str, len);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002341}
2342
Guido van Rossum5b10f451990-10-30 16:01:48 +00002343static object *
2344stdwin_rotatecutbuffers(self, args)
2345 object *self;
2346 object *args;
2347{
2348 int i;
2349 if (!getintarg(args, &i))
2350 return NULL;
2351 wrotatecutbuffers(i);
2352 INCREF(None);
2353 return None;
2354}
2355
2356static object *
2357stdwin_getselection(self, args)
2358 object *self;
2359 object *args;
2360{
2361 int sel;
2362 char *data;
2363 int len;
2364 if (!getintarg(args, &sel))
2365 return NULL;
2366 data = wgetselection(sel, &len);
2367 if (data == NULL) {
2368 data = "";
2369 len = 0;
2370 }
2371 return newsizedstringobject(data, len);
2372}
2373
2374static object *
2375stdwin_resetselection(self, args)
2376 object *self;
2377 object *args;
2378{
2379 int sel;
2380 if (!getintarg(args, &sel))
2381 return NULL;
2382 wresetselection(sel);
2383 INCREF(None);
2384 return None;
2385}
2386
Guido van Rossum0c2290b1991-04-03 19:12:14 +00002387static object *
2388stdwin_fetchcolor(self, args)
2389 object *self;
2390 object *args;
2391{
Guido van Rossumfc58e581992-01-27 16:45:55 +00002392 char *colorname;
Guido van Rossum34679b71993-01-26 13:33:44 +00002393 COLOR color;
Guido van Rossum0c2290b1991-04-03 19:12:14 +00002394 if (!getstrarg(args, &colorname))
2395 return NULL;
Guido van Rossum34679b71993-01-26 13:33:44 +00002396 color = wfetchcolor(colorname);
2397#ifdef BADCOLOR
2398 if (color == BADCOLOR) {
2399 err_setstr(StdwinError, "color name not found");
2400 return NULL;
2401 }
2402#endif
2403 return newintobject((long)color);
Guido van Rossum0c2290b1991-04-03 19:12:14 +00002404}
2405
Guido van Rossum541c8c01991-05-05 20:13:41 +00002406static object *
2407stdwin_getscrsize(self, args)
2408 object *self;
2409 object *args;
2410{
2411 int width, height;
2412 if (!getnoarg(args))
2413 return NULL;
2414 wgetscrsize(&width, &height);
2415 return makepoint(width, height);
2416}
2417
2418static object *
2419stdwin_getscrmm(self, args)
2420 object *self;
2421 object *args;
2422{
2423 int width, height;
2424 if (!getnoarg(args))
2425 return NULL;
2426 wgetscrmm(&width, &height);
2427 return makepoint(width, height);
2428}
2429
Guido van Rossumed233a51992-06-23 09:07:03 +00002430#ifdef unix
2431static object *
2432stdwin_connectionnumber(self, args)
2433 object *self;
2434 object *args;
2435{
2436 if (!getnoarg(args))
2437 return NULL;
2438 return newintobject((long) wconnectionnumber());
2439}
2440#endif
2441
Guido van Rossumbf80e541993-02-08 15:49:17 +00002442static object *
2443stdwin_listfontnames(self, args)
2444 object *self;
2445 object *args;
2446{
2447 char *pattern;
2448 char **fontnames;
2449 int count;
2450 object *list;
2451 if (!getargs(args, "z", &pattern))
2452 return NULL;
2453 fontnames = wlistfontnames(pattern, &count);
2454 list = newlistobject(count);
2455 if (list != NULL) {
2456 int i;
2457 for (i = 0; i < count; i++) {
2458 object *v = newstringobject(fontnames[i]);
2459 if (v == NULL) {
2460 DECREF(list);
2461 list = NULL;
2462 break;
2463 }
2464 setlistitem(list, i, v);
2465 }
2466 }
2467 return list;
2468}
2469
Guido van Rossume9066061993-07-29 13:14:32 +00002470#ifdef HAVE_BITMAPS
Guido van Rossumbf80e541993-02-08 15:49:17 +00002471static object *
2472stdwin_newbitmap(self, args)
2473 object *self;
2474 object *args;
2475{
2476 int width, height;
2477 bitmapobject *bp;
2478 if (!getargs(args, "(ii)", &width, &height))
2479 return NULL;
2480 return (object *)newbitmapobject(width, height);
2481}
Guido van Rossume9066061993-07-29 13:14:32 +00002482#endif
Guido van Rossumbf80e541993-02-08 15:49:17 +00002483
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002484static struct methodlist stdwin_methods[] = {
2485 {"askfile", stdwin_askfile},
2486 {"askstr", stdwin_askstr},
2487 {"askync", stdwin_askync},
Guido van Rossumcacd9571993-10-18 11:44:47 +00002488 {"done", stdwin_done},
Guido van Rossum27201061991-04-16 08:43:03 +00002489 {"fetchcolor", stdwin_fetchcolor},
Guido van Rossumed233a51992-06-23 09:07:03 +00002490#ifdef unix
2491 {"fileno", stdwin_connectionnumber},
2492 {"connectionnumber", stdwin_connectionnumber},
2493#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002494 {"fleep", stdwin_fleep},
Guido van Rossum246b9d81991-06-03 10:55:14 +00002495 {"getactive", stdwin_getactive},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002496 {"getcutbuffer", stdwin_getcutbuffer},
Guido van Rossum0c2290b1991-04-03 19:12:14 +00002497 {"getdefscrollbars", stdwin_getdefscrollbars},
Guido van Rossum33f17701991-02-13 23:19:39 +00002498 {"getdefwinpos", stdwin_getdefwinpos},
2499 {"getdefwinsize", stdwin_getdefwinsize},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002500 {"getevent", stdwin_getevent},
Guido van Rossum541c8c01991-05-05 20:13:41 +00002501 {"getscrmm", stdwin_getscrmm},
2502 {"getscrsize", stdwin_getscrsize},
Guido van Rossum27201061991-04-16 08:43:03 +00002503 {"getselection", stdwin_getselection},
Guido van Rossumbf80e541993-02-08 15:49:17 +00002504 {"listfontnames", stdwin_listfontnames},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002505 {"menucreate", stdwin_menucreate},
2506 {"message", stdwin_message},
Guido van Rossume9066061993-07-29 13:14:32 +00002507#ifdef HAVE_BITMAPS
Guido van Rossumbf80e541993-02-08 15:49:17 +00002508 {"newbitmap", stdwin_newbitmap},
Guido van Rossume9066061993-07-29 13:14:32 +00002509#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002510 {"open", stdwin_open},
Guido van Rossume8e7cf41991-01-16 14:06:18 +00002511 {"pollevent", stdwin_pollevent},
Guido van Rossum5b10f451990-10-30 16:01:48 +00002512 {"resetselection", stdwin_resetselection},
2513 {"rotatecutbuffers", stdwin_rotatecutbuffers},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002514 {"setcutbuffer", stdwin_setcutbuffer},
Guido van Rossum0c2290b1991-04-03 19:12:14 +00002515 {"setdefscrollbars", stdwin_setdefscrollbars},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002516 {"setdefwinpos", stdwin_setdefwinpos},
2517 {"setdefwinsize", stdwin_setdefwinsize},
2518
2519 /* Text measuring methods borrow code from drawing objects: */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002520 {"baseline", (method)drawing_baseline},
2521 {"lineheight", (method)drawing_lineheight},
2522 {"textbreak", (method)drawing_textbreak},
2523 {"textwidth", (method)drawing_textwidth},
Guido van Rossum0c2290b1991-04-03 19:12:14 +00002524
2525 /* Same for font setting methods: */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002526 {"setfont", (method)drawing_setfont},
Guido van Rossum0c2290b1991-04-03 19:12:14 +00002527
2528 /* Same for color setting/getting methods: */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002529 {"getbgcolor", (method)drawing_getbgcolor},
2530 {"getfgcolor", (method)drawing_getfgcolor},
2531 {"setbgcolor", (method)drawing_setbgcolor},
2532 {"setfgcolor", (method)drawing_setfgcolor},
Guido van Rossum0c2290b1991-04-03 19:12:14 +00002533
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002534 {NULL, NULL} /* sentinel */
2535};
2536
Guido van Rossumb6775db1994-08-01 11:34:53 +00002537#ifndef macintosh
Guido van Rossumcacd9571993-10-18 11:44:47 +00002538static int
2539checkstringlist(args, ps, pn)
2540 object *args;
2541 char ***ps;
2542 int *pn;
2543{
2544 int i, n;
2545 char **s;
2546 if (!is_listobject(args)) {
2547 err_setstr(TypeError, "list of strings expected");
2548 return 0;
2549 }
2550 n = getlistsize(args);
2551 s = NEW(char *, n+1);
2552 if (s == NULL) {
2553 err_nomem();
2554 return 0;
2555 }
2556 for (i = 0; i < n; i++) {
2557 object *item = getlistitem(args, i);
2558 if (!is_stringobject(item)) {
2559 err_setstr(TypeError, "list of strings expected");
2560 return 0;
2561 }
2562 s[i] = getstringvalue(item);
2563 }
2564 s[n] = NULL; /* In case caller wants a NULL-terminated list */
2565 *ps = s;
2566 *pn = n;
2567 return 1;
2568}
2569
2570static int
2571putbackstringlist(list, s, n)
2572 object *list;
2573 char **s;
2574 int n;
2575{
2576 int oldsize = getlistsize(list);
2577 object *newlist;
2578 int i;
2579 if (n == oldsize)
2580 return 1;
2581 newlist = newlistobject(n);
2582 for (i = 0; i < n && newlist != NULL; i++) {
2583 object *item = newstringobject(s[i]);
2584 if (item == NULL) {
2585 DECREF(newlist);
2586 newlist = NULL;
2587 }
2588 else
2589 setlistitem(newlist, i, item);
2590 }
2591 if (newlist == NULL)
2592 return 0;
2593 (*list->ob_type->tp_as_sequence->sq_ass_slice)
2594 (list, 0, oldsize, newlist);
2595 DECREF(newlist);
2596 return 1;
2597}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002598#endif /* macintosh */
Guido van Rossumcacd9571993-10-18 11:44:47 +00002599
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002600void
2601initstdwin()
2602{
Guido van Rossumbbf94341991-12-16 15:44:53 +00002603 object *m, *d;
2604 static int inited = 0;
2605
Guido van Rossum2d14e211991-02-19 12:26:49 +00002606 if (!inited) {
Guido van Rossumb6775db1994-08-01 11:34:53 +00002607#ifdef macintosh
2608 winit();
Jack Jansend56c1091995-01-27 14:44:16 +00002609 PyMac_DoYieldEnabled = 0;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002610#else
Guido van Rossum39cb5ce1995-01-26 00:37:10 +00002611 char buf[1000];
Guido van Rossumcacd9571993-10-18 11:44:47 +00002612 int argc = 0;
2613 char **argv = NULL;
2614 object *sys_argv = sysget("argv");
2615 if (sys_argv != NULL) {
2616 if (!checkstringlist(sys_argv, &argv, &argc))
2617 err_clear();
2618 }
Guido van Rossumc45611d1993-11-17 22:58:56 +00002619 if (argc > 0) {
2620 /* If argv[0] has a ".py" suffix, remove the suffix */
2621 char *p = strrchr(argv[0], '.');
2622 if (p != NULL && strcmp(p, ".py") == 0) {
2623 int n = p - argv[0];
2624 if (n >= sizeof(buf))
2625 n = sizeof(buf)-1;
2626 strncpy(buf, argv[0], n);
2627 buf[n] = '\0';
2628 argv[0] = buf;
2629 }
2630 }
Guido van Rossumcacd9571993-10-18 11:44:47 +00002631 winitargs(&argc, &argv);
2632 if (argv != NULL) {
2633 if (!putbackstringlist(sys_argv, argv, argc))
2634 err_clear();
2635 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002636#endif
Guido van Rossum2d14e211991-02-19 12:26:49 +00002637 inited = 1;
2638 }
Guido van Rossumbbf94341991-12-16 15:44:53 +00002639 m = initmodule("stdwin", stdwin_methods);
2640 d = getmoduledict(m);
2641
2642 /* Initialize stdwin.error exception */
2643 StdwinError = newstringobject("stdwin.error");
2644 if (StdwinError == NULL || dictinsert(d, "error", StdwinError) != 0)
2645 fatal("can't define stdwin.error");
Guido van Rossumb6775db1994-08-01 11:34:53 +00002646#ifdef WITH_THREAD
Guido van Rossumff4949e1992-08-05 19:58:53 +00002647 StdwinLock = allocate_lock();
2648 if (StdwinLock == NULL)
2649 fatal("can't allocate stdwin lock");
2650#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002651}