blob: 4d2b48f40e5f312192c523fe9791608b9cb1508d [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
Guido van Rossumbab9d031992-04-05 14:26:55 +00002Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
Guido van Rossumf70e43a1991-02-19 12:39:46 +00003Netherlands.
4
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
33*/
34
35/* Rules for translating C stdwin function calls into Python stwin:
36 - All names drop their initial letter 'w'
37 - Functions with a window as first parameter are methods of window objects
38 - There is no equivalent for wclose(); just delete the window object
39 (all references to it!) (XXX maybe this is a bad idea)
40 - w.begindrawing() returns a drawing object
41 - There is no equivalent for wenddrawing(win); just delete the drawing
42 object (all references to it!) (XXX maybe this is a bad idea)
43 - Functions that may only be used inside wbegindrawing / wendddrawing
44 are methods of the drawing object; this includes the text measurement
45 functions (which however have doubles as module functions).
46 - Methods of the drawing object drop an initial 'draw' from their name
47 if they have it, e.g., wdrawline() --> d.line()
48 - The obvious type conversions: int --> intobject; string --> stringobject
49 - A text parameter followed by a length parameter is only a text (string)
50 parameter in Python
51 - A point or other pair of horizontal and vertical coordinates is always
52 a pair of integers in Python
53 - Two points forming a rectangle or endpoints of a line segment are a
54 pair of points in Python
55 - The arguments to d.elarc() are three points.
56 - The functions wgetclip() and wsetclip() are translated into
57 stdwin.getcutbuffer() and stdwin.setcutbuffer(); 'clip' is really
58 a bad word for what these functions do (clipping has a different
59 meaning in the drawing world), while cutbuffer is standard X jargon.
Guido van Rossum01769f01990-10-30 13:39:00 +000060 XXX This must change again in the light of changes to stdwin!
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000061 - For textedit, similar rules hold, but they are less strict.
62 XXX more?
63*/
64
Guido van Rossum3f5da241990-12-20 15:06:42 +000065#include "allobjects.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000066
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000067#include "modsupport.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000068
Guido van Rossum3f5da241990-12-20 15:06:42 +000069#include "stdwin.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000070
Guido van Rossumbbf94341991-12-16 15:44:53 +000071static object *StdwinError; /* Exception stdwin.error */
Guido van Rossum87e7ea71991-12-10 14:00:03 +000072
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000073/* Window and menu object types declared here because of forward references */
74
75typedef struct {
76 OB_HEAD
77 object *w_title;
78 WINDOW *w_win;
79 object *w_attr; /* Attributes dictionary */
80} windowobject;
81
82extern typeobject Windowtype; /* Really static, forward */
83
84#define is_windowobject(wp) ((wp)->ob_type == &Windowtype)
85
86typedef struct {
87 OB_HEAD
88 MENU *m_menu;
89 int m_id;
90 object *m_attr; /* Attributes dictionary */
91} menuobject;
92
93extern typeobject Menutype; /* Really static, forward */
94
95#define is_menuobject(mp) ((mp)->ob_type == &Menutype)
96
97
98/* Strongly stdwin-specific argument handlers */
99
100static int
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000101getmenudetail(v, ep)
102 object *v;
103 EVENT *ep;
104{
Guido van Rossumfc58e581992-01-27 16:45:55 +0000105 menuobject *mp;
106 if (!getargs(v, "(Oi)", &mp, &ep->u.m.item))
107 return 0;
108 if (!is_menuobject(mp))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000109 return err_badarg();
Guido van Rossumfc58e581992-01-27 16:45:55 +0000110 ep->u.m.id = mp->m_id;
111 return 1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000112}
113
114static int
115geteventarg(v, ep)
116 object *v;
117 EVENT *ep;
118{
119 object *wp, *detail;
120 int a[4];
Guido van Rossumfc58e581992-01-27 16:45:55 +0000121 if (!getargs(v, "(iOO)", &ep->type, &wp, &detail))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000122 return 0;
Guido van Rossumfc58e581992-01-27 16:45:55 +0000123 if (is_windowobject(wp))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000124 ep->window = ((windowobject *)wp) -> w_win;
Guido van Rossumfc58e581992-01-27 16:45:55 +0000125 else if (wp == None)
126 ep->window = NULL;
127 else
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000128 return err_badarg();
Guido van Rossumfc58e581992-01-27 16:45:55 +0000129 switch (ep->type) {
130 case WE_CHAR: {
131 char c;
132 if (!getargs(detail, "c", &c))
133 return 0;
134 ep->u.character = c;
135 return 1;
136 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000137 case WE_COMMAND:
138 return getintarg(detail, &ep->u.command);
139 case WE_DRAW:
140 if (!getrectarg(detail, a))
141 return 0;
142 ep->u.area.left = a[0];
143 ep->u.area.top = a[1];
144 ep->u.area.right = a[2];
145 ep->u.area.bottom = a[3];
146 return 1;
147 case WE_MOUSE_DOWN:
148 case WE_MOUSE_UP:
149 case WE_MOUSE_MOVE:
Guido van Rossumfc58e581992-01-27 16:45:55 +0000150 return getargs(detail, "((ii)iii)",
151 &ep->u.where.h, &ep->u.where.v,
152 &ep->u.where.clicks,
153 &ep->u.where.button,
154 &ep->u.where.mask);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000155 case WE_MENU:
156 return getmenudetail(detail, ep);
157 default:
158 return 1;
159 }
160}
161
162
163/* Return construction tools */
164
165static object *
166makepoint(a, b)
167 int a, b;
168{
Guido van Rossum2ee12f41992-04-13 15:54:35 +0000169 return mkvalue("(ii)", a, b);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000170}
171
172static object *
173makerect(a, b, c, d)
174 int a, b, c, d;
175{
Guido van Rossum2ee12f41992-04-13 15:54:35 +0000176 return mkvalue("((ii)(ii))", a, b, c, d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000177}
178
179
180/* Drawing objects */
181
182typedef struct {
183 OB_HEAD
184 windowobject *d_ref;
185} drawingobject;
186
187static drawingobject *Drawing; /* Set to current drawing object, or NULL */
188
189/* Drawing methods */
190
Guido van Rossum3c284741991-11-27 14:54:54 +0000191static object *
192drawing_close(dp)
193 drawingobject *dp;
194{
195 if (dp->d_ref != NULL) {
196 wenddrawing(dp->d_ref->w_win);
197 Drawing = NULL;
198 DECREF(dp->d_ref);
199 dp->d_ref = NULL;
200 }
201 INCREF(None);
202 return None;
203}
Guido van Rossum77b46041992-01-14 18:41:24 +0000204
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000205static void
206drawing_dealloc(dp)
207 drawingobject *dp;
208{
Guido van Rossum3c284741991-11-27 14:54:54 +0000209 if (dp->d_ref != NULL) {
210 wenddrawing(dp->d_ref->w_win);
211 Drawing = NULL;
212 DECREF(dp->d_ref);
213 dp->d_ref = NULL;
214 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000215 free((char *)dp);
216}
217
218static object *
219drawing_generic(dp, args, func)
220 drawingobject *dp;
221 object *args;
222 void (*func) FPROTO((int, int, int, int));
223{
224 int a[4];
225 if (!getrectarg(args, a))
226 return NULL;
227 (*func)(a[0], a[1], a[2], a[3]);
228 INCREF(None);
229 return None;
230}
231
232static object *
233drawing_line(dp, args)
234 drawingobject *dp;
235 object *args;
236{
Guido van Rossumbf109731991-03-06 13:14:12 +0000237 return drawing_generic(dp, args, wdrawline);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000238}
239
240static object *
241drawing_xorline(dp, args)
242 drawingobject *dp;
243 object *args;
244{
Guido van Rossumbf109731991-03-06 13:14:12 +0000245 return drawing_generic(dp, args, wxorline);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000246}
247
248static object *
249drawing_circle(dp, args)
250 drawingobject *dp;
251 object *args;
252{
253 int a[3];
254 if (!getpointintarg(args, a))
255 return NULL;
256 wdrawcircle(a[0], a[1], a[2]);
257 INCREF(None);
258 return None;
259}
Guido van Rossuma2a181a1991-05-14 12:09:25 +0000260
Guido van Rossum27201061991-04-16 08:43:03 +0000261static object *
262drawing_fillcircle(dp, args)
263 drawingobject *dp;
264 object *args;
265{
266 int a[3];
267 if (!getpointintarg(args, a))
268 return NULL;
269 wfillcircle(a[0], a[1], a[2]);
270 INCREF(None);
271 return None;
272}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000273
274static object *
Guido van Rossuma2a181a1991-05-14 12:09:25 +0000275drawing_xorcircle(dp, args)
276 drawingobject *dp;
277 object *args;
278{
279 int a[3];
280 if (!getpointintarg(args, a))
281 return NULL;
282 wxorcircle(a[0], a[1], a[2]);
283 INCREF(None);
284 return None;
285}
286
287static object *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000288drawing_elarc(dp, args)
289 drawingobject *dp;
290 object *args;
291{
292 int a[6];
293 if (!get3pointarg(args, a))
294 return NULL;
295 wdrawelarc(a[0], a[1], a[2], a[3], a[4], a[5]);
296 INCREF(None);
297 return None;
298}
299
300static object *
Guido van Rossum27201061991-04-16 08:43:03 +0000301drawing_fillelarc(dp, args)
302 drawingobject *dp;
303 object *args;
304{
305 int a[6];
306 if (!get3pointarg(args, a))
307 return NULL;
308 wfillelarc(a[0], a[1], a[2], a[3], a[4], a[5]);
309 INCREF(None);
310 return None;
311}
312
313static object *
Guido van Rossuma2a181a1991-05-14 12:09:25 +0000314drawing_xorelarc(dp, args)
315 drawingobject *dp;
316 object *args;
317{
318 int a[6];
319 if (!get3pointarg(args, a))
320 return NULL;
321 wxorelarc(a[0], a[1], a[2], a[3], a[4], a[5]);
322 INCREF(None);
323 return None;
324}
325
326static object *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000327drawing_box(dp, args)
328 drawingobject *dp;
329 object *args;
330{
Guido van Rossumbf109731991-03-06 13:14:12 +0000331 return drawing_generic(dp, args, wdrawbox);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000332}
333
334static object *
335drawing_erase(dp, args)
336 drawingobject *dp;
337 object *args;
338{
Guido van Rossumbf109731991-03-06 13:14:12 +0000339 return drawing_generic(dp, args, werase);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000340}
341
342static object *
343drawing_paint(dp, args)
344 drawingobject *dp;
345 object *args;
346{
Guido van Rossumbf109731991-03-06 13:14:12 +0000347 return drawing_generic(dp, args, wpaint);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000348}
349
350static object *
351drawing_invert(dp, args)
352 drawingobject *dp;
353 object *args;
354{
Guido van Rossumbf109731991-03-06 13:14:12 +0000355 return drawing_generic(dp, args, winvert);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000356}
357
Guido van Rossum27201061991-04-16 08:43:03 +0000358static POINT *
359getpointsarray(v, psize)
360 object *v;
361 int *psize;
362{
363 int n = -1;
364 object * (*getitem) PROTO((object *, int));
365 int i;
366 POINT *points;
367
368 if (v == NULL)
369 ;
370 else if (is_listobject(v)) {
371 n = getlistsize(v);
372 getitem = getlistitem;
373 }
374 else if (is_tupleobject(v)) {
375 n = gettuplesize(v);
376 getitem = gettupleitem;
377 }
378
379 if (n <= 0) {
380 (void) err_badarg();
381 return NULL;
382 }
383
384 points = NEW(POINT, n);
385 if (points == NULL) {
386 (void) err_nomem();
387 return NULL;
388 }
389
390 for (i = 0; i < n; i++) {
391 object *w = (*getitem)(v, i);
392 int a[2];
393 if (!getpointarg(w, a)) {
394 DEL(points);
395 return NULL;
396 }
397 points[i].h = a[0];
398 points[i].v = a[1];
399 }
400
401 *psize = n;
402 return points;
403}
404
405static object *
406drawing_poly(dp, args)
407 drawingobject *dp;
408 object *args;
409{
410 int n;
411 POINT *points = getpointsarray(args, &n);
412 if (points == NULL)
413 return NULL;
414 wdrawpoly(n, points);
415 DEL(points);
416 INCREF(None);
417 return None;
418}
419
420static object *
421drawing_fillpoly(dp, args)
422 drawingobject *dp;
423 object *args;
424{
425 int n;
426 POINT *points = getpointsarray(args, &n);
427 if (points == NULL)
428 return NULL;
429 wfillpoly(n, points);
430 DEL(points);
431 INCREF(None);
432 return None;
433}
434
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000435static object *
Guido van Rossuma2a181a1991-05-14 12:09:25 +0000436drawing_xorpoly(dp, args)
437 drawingobject *dp;
438 object *args;
439{
440 int n;
441 POINT *points = getpointsarray(args, &n);
442 if (points == NULL)
443 return NULL;
444 wxorpoly(n, points);
445 DEL(points);
446 INCREF(None);
447 return None;
448}
449
450static object *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000451drawing_cliprect(dp, args)
452 drawingobject *dp;
453 object *args;
454{
Guido van Rossumbf109731991-03-06 13:14:12 +0000455 return drawing_generic(dp, args, wcliprect);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000456}
457
458static object *
459drawing_noclip(dp, args)
460 drawingobject *dp;
461 object *args;
462{
463 if (!getnoarg(args))
464 return NULL;
465 wnoclip();
466 INCREF(None);
467 return None;
468}
469
470static object *
471drawing_shade(dp, args)
472 drawingobject *dp;
473 object *args;
474{
475 int a[5];
476 if (!getrectintarg(args, a))
477 return NULL;
478 wshade(a[0], a[1], a[2], a[3], a[4]);
479 INCREF(None);
480 return None;
481}
482
483static object *
484drawing_text(dp, args)
485 drawingobject *dp;
486 object *args;
487{
Guido van Rossumfc58e581992-01-27 16:45:55 +0000488 int h, v, size;
489 char *text;
490 if (!getargs(args, "((ii)s#)", &h, &v, &text, &size))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000491 return NULL;
Guido van Rossumfc58e581992-01-27 16:45:55 +0000492 wdrawtext(h, v, text, size);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000493 INCREF(None);
494 return None;
495}
496
497/* The following four are also used as stdwin functions */
498
499static object *
500drawing_lineheight(dp, args)
501 drawingobject *dp;
502 object *args;
503{
504 if (!getnoarg(args))
505 return NULL;
506 return newintobject((long)wlineheight());
507}
508
509static object *
510drawing_baseline(dp, args)
511 drawingobject *dp;
512 object *args;
513{
514 if (!getnoarg(args))
515 return NULL;
516 return newintobject((long)wbaseline());
517}
518
519static object *
520drawing_textwidth(dp, args)
521 drawingobject *dp;
522 object *args;
523{
Guido van Rossumfc58e581992-01-27 16:45:55 +0000524 char *text;
525 int size;
526 if (!getargs(args, "s#", &text, &size))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000527 return NULL;
Guido van Rossumfc58e581992-01-27 16:45:55 +0000528 return newintobject((long)wtextwidth(text, size));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000529}
530
531static object *
532drawing_textbreak(dp, args)
533 drawingobject *dp;
534 object *args;
535{
Guido van Rossumfc58e581992-01-27 16:45:55 +0000536 char *text;
537 int size, width;
538 if (!getargs(args, "(s#i)", &text, &size, &width))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000539 return NULL;
Guido van Rossumfc58e581992-01-27 16:45:55 +0000540 return newintobject((long)wtextbreak(text, size, width));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000541}
542
Guido van Rossum0c2290b1991-04-03 19:12:14 +0000543static object *
544drawing_setfont(self, args)
545 drawingobject *self;
546 object *args;
547{
Guido van Rossumfc58e581992-01-27 16:45:55 +0000548 char *font;
549 char style = '\0';
550 int size = 0;
551 if (args == NULL || !is_tupleobject(args)) {
Guido van Rossum3c8ba7a1992-02-05 11:15:00 +0000552 if (!getargs(args, "z", &font))
Guido van Rossum50429a11991-04-04 15:24:07 +0000553 return NULL;
554 }
555 else {
Guido van Rossumfc58e581992-01-27 16:45:55 +0000556 int n = gettuplesize(args);
557 if (n == 2) {
558 if (!getargs(args, "(zi)", &font, &size))
559 return NULL;
560 }
561 else if (!getargs(args, "(zic)", &font, &size, &style)) {
562 err_clear();
563 if (!getargs(args, "(zci)", &font, &style, &size))
564 return NULL;
Guido van Rossum50429a11991-04-04 15:24:07 +0000565 }
566 }
Guido van Rossumfc58e581992-01-27 16:45:55 +0000567 if (font != NULL)
568 wsetfont(font);
Guido van Rossum50429a11991-04-04 15:24:07 +0000569 if (size != 0)
570 wsetsize(size);
Guido van Rossumfc58e581992-01-27 16:45:55 +0000571 switch (style) {
572 case 'b':
573 wsetbold();
574 break;
575 case 'i':
576 wsetitalic();
577 break;
578 case 'o':
579 wsetbolditalic();
580 break;
581 case 'u':
582 wsetunderline();
583 break;
584 case 'p':
585 wsetplain();
586 break;
587 }
Guido van Rossum0c2290b1991-04-03 19:12:14 +0000588 INCREF(None);
589 return None;
590}
591
592static object *
593drawing_getbgcolor(self, args)
594 object *self;
595 object *args;
596{
597 if (!getnoarg(args))
598 return NULL;
599 return newintobject((long)wgetbgcolor());
600}
601
602static object *
603drawing_getfgcolor(self, args)
604 object *self;
605 object *args;
606{
607 if (!getnoarg(args))
608 return NULL;
609 return newintobject((long)wgetfgcolor());
610}
611
612static object *
613drawing_setbgcolor(self, args)
614 object *self;
615 object *args;
616{
617 long color;
618 if (!getlongarg(args, &color))
619 return NULL;
620 wsetbgcolor((COLOR)color);
621 INCREF(None);
622 return None;
623}
624
625static object *
626drawing_setfgcolor(self, args)
627 object *self;
628 object *args;
629{
630 long color;
631 if (!getlongarg(args, &color))
632 return NULL;
633 wsetfgcolor((COLOR)color);
634 INCREF(None);
635 return None;
636}
637
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000638static struct methodlist drawing_methods[] = {
639 {"box", drawing_box},
640 {"circle", drawing_circle},
641 {"cliprect", drawing_cliprect},
Guido van Rossum3c284741991-11-27 14:54:54 +0000642 {"close", drawing_close},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000643 {"elarc", drawing_elarc},
Guido van Rossum3c284741991-11-27 14:54:54 +0000644 {"enddrawing", drawing_close},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000645 {"erase", drawing_erase},
Guido van Rossum27201061991-04-16 08:43:03 +0000646 {"fillcircle", drawing_fillcircle},
Guido van Rossuma2a181a1991-05-14 12:09:25 +0000647 {"fillelarc", drawing_fillelarc},
Guido van Rossum27201061991-04-16 08:43:03 +0000648 {"fillpoly", drawing_fillpoly},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000649 {"invert", drawing_invert},
650 {"line", drawing_line},
651 {"noclip", drawing_noclip},
652 {"paint", drawing_paint},
Guido van Rossum27201061991-04-16 08:43:03 +0000653 {"poly", drawing_poly},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000654 {"shade", drawing_shade},
655 {"text", drawing_text},
Guido van Rossuma2a181a1991-05-14 12:09:25 +0000656 {"xorcircle", drawing_xorcircle},
657 {"xorelarc", drawing_xorelarc},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000658 {"xorline", drawing_xorline},
Guido van Rossuma2a181a1991-05-14 12:09:25 +0000659 {"xorpoly", drawing_xorpoly},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000660
661 /* Text measuring methods: */
662 {"baseline", drawing_baseline},
663 {"lineheight", drawing_lineheight},
664 {"textbreak", drawing_textbreak},
665 {"textwidth", drawing_textwidth},
Guido van Rossum0c2290b1991-04-03 19:12:14 +0000666
667 /* Font setting methods: */
668 {"setfont", drawing_setfont},
669
670 /* Color methods: */
671 {"getbgcolor", drawing_getbgcolor},
672 {"getfgcolor", drawing_getfgcolor},
673 {"setbgcolor", drawing_setbgcolor},
674 {"setfgcolor", drawing_setfgcolor},
675
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000676 {NULL, NULL} /* sentinel */
677};
678
679static object *
Guido van Rossum77b46041992-01-14 18:41:24 +0000680drawing_getattr(dp, name)
681 drawingobject *dp;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000682 char *name;
683{
Guido van Rossum77b46041992-01-14 18:41:24 +0000684 if (dp->d_ref == NULL) {
685 err_setstr(StdwinError, "drawing object already closed");
686 return NULL;
687 }
688 return findmethod(drawing_methods, (object *)dp, name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000689}
690
Guido van Rossum541c8c01991-05-05 20:13:41 +0000691typeobject Drawingtype = {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000692 OB_HEAD_INIT(&Typetype)
693 0, /*ob_size*/
694 "drawing", /*tp_name*/
695 sizeof(drawingobject), /*tp_size*/
696 0, /*tp_itemsize*/
697 /* methods */
698 drawing_dealloc, /*tp_dealloc*/
699 0, /*tp_print*/
700 drawing_getattr, /*tp_getattr*/
701 0, /*tp_setattr*/
702 0, /*tp_compare*/
703 0, /*tp_repr*/
704};
705
706
707/* Text(edit) objects */
708
709typedef struct {
710 OB_HEAD
711 TEXTEDIT *t_text;
712 windowobject *t_ref;
713 object *t_attr; /* Attributes dictionary */
714} textobject;
715
716extern typeobject Texttype; /* Really static, forward */
717
718static textobject *
719newtextobject(wp, left, top, right, bottom)
720 windowobject *wp;
721 int left, top, right, bottom;
722{
723 textobject *tp;
724 tp = NEWOBJ(textobject, &Texttype);
725 if (tp == NULL)
726 return NULL;
727 tp->t_attr = NULL;
728 INCREF(wp);
729 tp->t_ref = wp;
730 tp->t_text = tecreate(wp->w_win, left, top, right, bottom);
731 if (tp->t_text == NULL) {
732 DECREF(tp);
733 return (textobject *) err_nomem();
734 }
735 return tp;
736}
737
738/* Text(edit) methods */
739
740static void
741text_dealloc(tp)
742 textobject *tp;
743{
744 if (tp->t_text != NULL)
745 tefree(tp->t_text);
Guido van Rossum3c284741991-11-27 14:54:54 +0000746 XDECREF(tp->t_attr);
747 XDECREF(tp->t_ref);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000748 DEL(tp);
749}
750
751static object *
Guido van Rossum3c284741991-11-27 14:54:54 +0000752text_close(tp, args)
753 textobject *tp;
754 object *args;
755{
756 if (tp->t_text != NULL) {
757 tefree(tp->t_text);
758 tp->t_text = NULL;
759 }
760 if (tp->t_attr != NULL) {
761 DECREF(tp->t_attr);
762 tp->t_attr = NULL;
763 }
764 if (tp->t_ref != NULL) {
765 DECREF(tp->t_ref);
766 tp->t_ref = NULL;
767 }
768 INCREF(None);
769 return None;
770}
771
772static object *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000773text_arrow(self, args)
774 textobject *self;
775 object *args;
776{
777 int code;
778 if (!getintarg(args, &code))
779 return NULL;
780 tearrow(self->t_text, code);
781 INCREF(None);
782 return None;
783}
784
785static object *
786text_draw(self, args)
787 textobject *self;
788 object *args;
789{
790 register TEXTEDIT *tp = self->t_text;
791 int a[4];
792 int left, top, right, bottom;
793 if (!getrectarg(args, a))
794 return NULL;
795 if (Drawing != NULL) {
Guido van Rossum87e7ea71991-12-10 14:00:03 +0000796 err_setstr(StdwinError, "already drawing");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000797 return NULL;
798 }
799 /* Clip to text area and ignore if area is empty */
800 left = tegetleft(tp);
801 top = tegettop(tp);
802 right = tegetright(tp);
803 bottom = tegetbottom(tp);
804 if (a[0] < left) a[0] = left;
805 if (a[1] < top) a[1] = top;
806 if (a[2] > right) a[2] = right;
807 if (a[3] > bottom) a[3] = bottom;
808 if (a[0] < a[2] && a[1] < a[3]) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000809 wbegindrawing(self->t_ref->w_win);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000810 tedrawnew(tp, a[0], a[1], a[2], a[3]);
811 wenddrawing(self->t_ref->w_win);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000812 }
813 INCREF(None);
814 return None;
815}
816
817static object *
818text_event(self, args)
819 textobject *self;
820 object *args;
821{
822 register TEXTEDIT *tp = self->t_text;
823 EVENT e;
824 if (!geteventarg(args, &e))
825 return NULL;
826 if (e.type == WE_MOUSE_DOWN) {
Guido van Rossum33f17701991-02-13 23:19:39 +0000827 /* Cheat at the margins */
828 int width, height;
829 wgetdocsize(e.window, &width, &height);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000830 if (e.u.where.h < 0 && tegetleft(tp) == 0)
831 e.u.where.h = 0;
Guido van Rossum33f17701991-02-13 23:19:39 +0000832 else if (e.u.where.h > width && tegetright(tp) == width)
833 e.u.where.h = width;
834 if (e.u.where.v < 0 && tegettop(tp) == 0)
835 e.u.where.v = 0;
836 else if (e.u.where.v > height && tegetright(tp) == height)
837 e.u.where.v = height;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000838 }
839 return newintobject((long) teevent(tp, &e));
840}
841
842static object *
843text_getfocus(self, args)
844 textobject *self;
845 object *args;
846{
847 if (!getnoarg(args))
848 return NULL;
849 return makepoint(tegetfoc1(self->t_text), tegetfoc2(self->t_text));
850}
851
852static object *
853text_getfocustext(self, args)
854 textobject *self;
855 object *args;
856{
857 int f1, f2;
858 char *text;
859 if (!getnoarg(args))
860 return NULL;
861 f1 = tegetfoc1(self->t_text);
862 f2 = tegetfoc2(self->t_text);
863 text = tegettext(self->t_text);
864 return newsizedstringobject(text + f1, f2-f1);
865}
866
867static object *
868text_getrect(self, args)
869 textobject *self;
870 object *args;
871{
872 if (!getnoarg(args))
873 return NULL;
874 return makerect(tegetleft(self->t_text),
875 tegettop(self->t_text),
876 tegetright(self->t_text),
877 tegetbottom(self->t_text));
878}
879
880static object *
881text_gettext(self, args)
882 textobject *self;
883 object *args;
884{
885 if (!getnoarg(args))
886 return NULL;
887 return newsizedstringobject(tegettext(self->t_text),
888 tegetlen(self->t_text));
889}
890
891static object *
892text_move(self, args)
893 textobject *self;
894 object *args;
895{
896 int a[4];
897 if (!getrectarg(args, a))
898 return NULL;
899 temovenew(self->t_text, a[0], a[1], a[2], a[3]);
900 INCREF(None);
901 return None;
902}
903
904static object *
Guido van Rossum4b9cf8e1991-05-28 21:57:04 +0000905text_replace(self, args)
906 textobject *self;
907 object *args;
908{
Guido van Rossumfc58e581992-01-27 16:45:55 +0000909 char *text;
Guido van Rossum4b9cf8e1991-05-28 21:57:04 +0000910 if (!getstrarg(args, &text))
911 return NULL;
Guido van Rossumfc58e581992-01-27 16:45:55 +0000912 tereplace(self->t_text, text);
Guido van Rossum4b9cf8e1991-05-28 21:57:04 +0000913 INCREF(None);
914 return None;
915}
916
917static object *
918text_setactive(self, args)
919 textobject *self;
920 object *args;
921{
922 int flag;
923 if (!getintarg(args, &flag))
924 return NULL;
925 tesetactive(self->t_text, flag);
926 INCREF(None);
927 return None;
928}
929
930static object *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000931text_setfocus(self, args)
932 textobject *self;
933 object *args;
934{
935 int a[2];
936 if (!getpointarg(args, a))
937 return NULL;
938 tesetfocus(self->t_text, a[0], a[1]);
939 INCREF(None);
940 return None;
941}
942
943static object *
Guido van Rossum541c8c01991-05-05 20:13:41 +0000944text_settext(self, args)
945 textobject *self;
946 object *args;
947{
Guido van Rossumfc58e581992-01-27 16:45:55 +0000948 char *text;
Guido van Rossum541c8c01991-05-05 20:13:41 +0000949 char *buf;
950 int size;
Guido van Rossumfc58e581992-01-27 16:45:55 +0000951 if (!getargs(args, "s#", &text, &size))
Guido van Rossum541c8c01991-05-05 20:13:41 +0000952 return NULL;
Guido van Rossum541c8c01991-05-05 20:13:41 +0000953 if ((buf = NEW(char, size)) == NULL) {
Guido van Rossum87e7ea71991-12-10 14:00:03 +0000954 return err_nomem();
Guido van Rossum541c8c01991-05-05 20:13:41 +0000955 }
Guido van Rossumfc58e581992-01-27 16:45:55 +0000956 memcpy(buf, text, size);
Guido van Rossum541c8c01991-05-05 20:13:41 +0000957 tesetbuf(self->t_text, buf, size); /* Becomes owner of buffer */
958 INCREF(None);
959 return None;
960}
961
962static object *
Guido van Rossum4b9cf8e1991-05-28 21:57:04 +0000963text_setview(self, args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000964 textobject *self;
965 object *args;
966{
Guido van Rossum4b9cf8e1991-05-28 21:57:04 +0000967 int a[4];
968 if (args == None)
969 tenoview(self->t_text);
970 else {
971 if (!getrectarg(args, a))
972 return NULL;
973 tesetview(self->t_text, a[0], a[1], a[2], a[3]);
974 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000975 INCREF(None);
976 return None;
977}
978
979static struct methodlist text_methods[] = {
Guido van Rossum3c284741991-11-27 14:54:54 +0000980 {"arrow", text_arrow},
981 {"close", text_close},
982 {"draw", text_draw},
983 {"event", text_event},
984 {"getfocus", text_getfocus},
Guido van Rossum77b46041992-01-14 18:41:24 +0000985 {"getfocustext",text_getfocustext},
Guido van Rossum3c284741991-11-27 14:54:54 +0000986 {"getrect", text_getrect},
987 {"gettext", text_gettext},
Guido van Rossum77b46041992-01-14 18:41:24 +0000988 {"move", text_move},
Guido van Rossum3c284741991-11-27 14:54:54 +0000989 {"replace", text_replace},
990 {"setactive", text_setactive},
991 {"setfocus", text_setfocus},
992 {"settext", text_settext},
993 {"setview", text_setview},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000994 {NULL, NULL} /* sentinel */
995};
996
997static object *
998text_getattr(tp, name)
999 textobject *tp;
1000 char *name;
1001{
Guido van Rossum85f50761991-10-20 20:22:50 +00001002 object *v = NULL;
Guido van Rossum77b46041992-01-14 18:41:24 +00001003 if (tp->t_ref == NULL) {
1004 err_setstr(StdwinError, "text object already closed");
1005 return NULL;
1006 }
Guido van Rossum85f50761991-10-20 20:22:50 +00001007 if (strcmp(name, "__dict__") == 0) {
1008 v = tp->t_attr;
1009 if (v == NULL)
1010 v = None;
1011 }
1012 else if (tp->t_attr != NULL) {
1013 v = dictlookup(tp->t_attr, name);
1014 }
1015 if (v != NULL) {
1016 INCREF(v);
1017 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001018 }
1019 return findmethod(text_methods, (object *)tp, name);
1020}
1021
1022static int
1023text_setattr(tp, name, v)
1024 textobject *tp;
1025 char *name;
1026 object *v;
1027{
1028 if (tp->t_attr == NULL) {
1029 tp->t_attr = newdictobject();
1030 if (tp->t_attr == NULL)
1031 return -1;
1032 }
1033 if (v == NULL)
1034 return dictremove(tp->t_attr, name);
1035 else
1036 return dictinsert(tp->t_attr, name, v);
1037}
1038
Guido van Rossum541c8c01991-05-05 20:13:41 +00001039typeobject Texttype = {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001040 OB_HEAD_INIT(&Typetype)
1041 0, /*ob_size*/
1042 "textedit", /*tp_name*/
1043 sizeof(textobject), /*tp_size*/
1044 0, /*tp_itemsize*/
1045 /* methods */
1046 text_dealloc, /*tp_dealloc*/
1047 0, /*tp_print*/
1048 text_getattr, /*tp_getattr*/
1049 text_setattr, /*tp_setattr*/
1050 0, /*tp_compare*/
1051 0, /*tp_repr*/
1052};
1053
1054
1055/* Menu objects */
1056
Guido van Rossum2d14e211991-02-19 12:26:49 +00001057#define IDOFFSET 10 /* Menu IDs we use start here */
Guido van Rossum27201061991-04-16 08:43:03 +00001058#define MAXNMENU 200 /* Max #menus we allow */
Guido van Rossum2d14e211991-02-19 12:26:49 +00001059static menuobject *menulist[MAXNMENU];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001060
Guido van Rossumfc58e581992-01-27 16:45:55 +00001061static menuobject *newmenuobject PROTO((char *));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001062static menuobject *
1063newmenuobject(title)
Guido van Rossumfc58e581992-01-27 16:45:55 +00001064 char *title;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001065{
1066 int id;
1067 MENU *menu;
1068 menuobject *mp;
Guido van Rossum2d14e211991-02-19 12:26:49 +00001069 for (id = 0; id < MAXNMENU; id++) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001070 if (menulist[id] == NULL)
1071 break;
1072 }
Guido van Rossum27201061991-04-16 08:43:03 +00001073 if (id >= MAXNMENU) {
Guido van Rossum87e7ea71991-12-10 14:00:03 +00001074 err_setstr(StdwinError, "creating too many menus");
Guido van Rossum27201061991-04-16 08:43:03 +00001075 return NULL;
1076 }
Guido van Rossumfc58e581992-01-27 16:45:55 +00001077 menu = wmenucreate(id + IDOFFSET, title);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001078 if (menu == NULL)
1079 return (menuobject *) err_nomem();
1080 mp = NEWOBJ(menuobject, &Menutype);
1081 if (mp != NULL) {
1082 mp->m_menu = menu;
Guido van Rossum2d14e211991-02-19 12:26:49 +00001083 mp->m_id = id + IDOFFSET;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001084 mp->m_attr = NULL;
1085 menulist[id] = mp;
1086 }
1087 else
1088 wmenudelete(menu);
1089 return mp;
1090}
1091
1092/* Menu methods */
1093
1094static void
1095menu_dealloc(mp)
1096 menuobject *mp;
1097{
1098
Guido van Rossum2d14e211991-02-19 12:26:49 +00001099 int id = mp->m_id - IDOFFSET;
1100 if (id >= 0 && id < MAXNMENU && menulist[id] == mp) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001101 menulist[id] = NULL;
1102 }
Guido van Rossum77b46041992-01-14 18:41:24 +00001103 if (mp->m_menu != NULL)
1104 wmenudelete(mp->m_menu);
1105 XDECREF(mp->m_attr);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001106 DEL(mp);
1107}
1108
1109static object *
Guido van Rossum77b46041992-01-14 18:41:24 +00001110menu_close(mp, args)
1111 menuobject *mp;
1112 object *args;
1113{
1114 int id = mp->m_id - IDOFFSET;
1115 if (id >= 0 && id < MAXNMENU && menulist[id] == mp) {
1116 menulist[id] = NULL;
1117 }
1118 mp->m_id = -1;
1119 if (mp->m_menu != NULL)
1120 wmenudelete(mp->m_menu);
1121 mp->m_menu = NULL;
1122 XDECREF(mp->m_attr);
1123 mp->m_attr = NULL;
1124 INCREF(None);
1125 return None;
1126}
1127
1128static object *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001129menu_additem(self, args)
1130 menuobject *self;
1131 object *args;
1132{
Guido van Rossumfc58e581992-01-27 16:45:55 +00001133 char *text;
1134 int shortcut = -1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001135 if (is_tupleobject(args)) {
Guido van Rossumfc58e581992-01-27 16:45:55 +00001136 char c;
1137 if (!getargs(args, "(sc)", &text, &c))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001138 return NULL;
Guido van Rossumfc58e581992-01-27 16:45:55 +00001139 shortcut = c;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001140 }
Guido van Rossumfc58e581992-01-27 16:45:55 +00001141 else if (!getstrarg(args, &text))
1142 return NULL;
1143 wmenuadditem(self->m_menu, text, shortcut);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001144 INCREF(None);
1145 return None;
1146}
1147
1148static object *
1149menu_setitem(self, args)
1150 menuobject *self;
1151 object *args;
1152{
1153 int index;
Guido van Rossumfc58e581992-01-27 16:45:55 +00001154 char *text;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001155 if (!getintstrarg(args, &index, &text))
1156 return NULL;
Guido van Rossumfc58e581992-01-27 16:45:55 +00001157 wmenusetitem(self->m_menu, index, text);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001158 INCREF(None);
1159 return None;
1160}
1161
1162static object *
1163menu_enable(self, args)
1164 menuobject *self;
1165 object *args;
1166{
1167 int index;
1168 int flag;
1169 if (!getintintarg(args, &index, &flag))
1170 return NULL;
1171 wmenuenable(self->m_menu, index, flag);
1172 INCREF(None);
1173 return None;
1174}
1175
1176static object *
1177menu_check(self, args)
1178 menuobject *self;
1179 object *args;
1180{
1181 int index;
1182 int flag;
1183 if (!getintintarg(args, &index, &flag))
1184 return NULL;
1185 wmenucheck(self->m_menu, index, flag);
1186 INCREF(None);
1187 return None;
1188}
1189
1190static struct methodlist menu_methods[] = {
Guido van Rossum3c284741991-11-27 14:54:54 +00001191 {"additem", menu_additem},
1192 {"setitem", menu_setitem},
1193 {"enable", menu_enable},
1194 {"check", menu_check},
Guido van Rossum77b46041992-01-14 18:41:24 +00001195 {"close", menu_close},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001196 {NULL, NULL} /* sentinel */
1197};
1198
1199static object *
1200menu_getattr(mp, name)
1201 menuobject *mp;
1202 char *name;
1203{
Guido van Rossum85f50761991-10-20 20:22:50 +00001204 object *v = NULL;
Guido van Rossum77b46041992-01-14 18:41:24 +00001205 if (mp->m_menu == NULL) {
1206 err_setstr(StdwinError, "menu object already closed");
1207 return NULL;
1208 }
Guido van Rossum85f50761991-10-20 20:22:50 +00001209 if (strcmp(name, "__dict__") == 0) {
1210 v = mp->m_attr;
1211 if (v == NULL)
1212 v = None;
1213 }
1214 else if (mp->m_attr != NULL) {
1215 v = dictlookup(mp->m_attr, name);
1216 }
1217 if (v != NULL) {
1218 INCREF(v);
1219 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001220 }
1221 return findmethod(menu_methods, (object *)mp, name);
1222}
1223
1224static int
1225menu_setattr(mp, name, v)
1226 menuobject *mp;
1227 char *name;
1228 object *v;
1229{
1230 if (mp->m_attr == NULL) {
1231 mp->m_attr = newdictobject();
1232 if (mp->m_attr == NULL)
1233 return -1;
1234 }
1235 if (v == NULL)
1236 return dictremove(mp->m_attr, name);
1237 else
1238 return dictinsert(mp->m_attr, name, v);
1239}
1240
Guido van Rossum541c8c01991-05-05 20:13:41 +00001241typeobject Menutype = {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001242 OB_HEAD_INIT(&Typetype)
1243 0, /*ob_size*/
1244 "menu", /*tp_name*/
1245 sizeof(menuobject), /*tp_size*/
1246 0, /*tp_itemsize*/
1247 /* methods */
1248 menu_dealloc, /*tp_dealloc*/
1249 0, /*tp_print*/
1250 menu_getattr, /*tp_getattr*/
1251 menu_setattr, /*tp_setattr*/
1252 0, /*tp_compare*/
1253 0, /*tp_repr*/
1254};
1255
1256
1257/* Windows */
1258
1259#define MAXNWIN 50
1260static windowobject *windowlist[MAXNWIN];
1261
1262/* Window methods */
1263
1264static void
1265window_dealloc(wp)
1266 windowobject *wp;
1267{
1268 if (wp->w_win != NULL) {
1269 int tag = wgettag(wp->w_win);
1270 if (tag >= 0 && tag < MAXNWIN)
1271 windowlist[tag] = NULL;
1272 else
1273 fprintf(stderr, "XXX help! tag %d in window_dealloc\n",
1274 tag);
1275 wclose(wp->w_win);
1276 }
1277 DECREF(wp->w_title);
1278 if (wp->w_attr != NULL)
1279 DECREF(wp->w_attr);
1280 free((char *)wp);
1281}
1282
Guido van Rossumd783a461991-06-07 22:35:42 +00001283static int
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001284window_print(wp, fp, flags)
1285 windowobject *wp;
1286 FILE *fp;
1287 int flags;
1288{
Guido van Rossum3c284741991-11-27 14:54:54 +00001289 fprintf(fp, "<%s window titled '%s'>",
1290 wp->w_win == NULL ? "closed" : "open",
1291 getstringvalue(wp->w_title));
Guido van Rossumd783a461991-06-07 22:35:42 +00001292 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001293}
1294
1295static object *
Guido van Rossum3c284741991-11-27 14:54:54 +00001296window_close(wp, args)
1297 windowobject *wp;
1298 object *args;
1299{
1300 if (wp->w_win != NULL) {
1301 int tag = wgettag(wp->w_win);
1302 if (tag >= 0 && tag < MAXNWIN)
1303 windowlist[tag] = NULL;
1304 wclose(wp->w_win);
1305 wp->w_win = NULL;
1306 }
1307 INCREF(None);
1308 return None;
1309}
1310
1311static object *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001312window_begindrawing(wp, args)
1313 windowobject *wp;
1314 object *args;
1315{
1316 drawingobject *dp;
1317 if (!getnoarg(args))
1318 return NULL;
1319 if (Drawing != NULL) {
Guido van Rossum87e7ea71991-12-10 14:00:03 +00001320 err_setstr(StdwinError, "already drawing");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001321 return NULL;
1322 }
1323 dp = NEWOBJ(drawingobject, &Drawingtype);
1324 if (dp == NULL)
1325 return NULL;
1326 Drawing = dp;
1327 INCREF(wp);
1328 dp->d_ref = wp;
1329 wbegindrawing(wp->w_win);
1330 return (object *)dp;
1331}
1332
1333static object *
1334window_change(wp, args)
1335 windowobject *wp;
1336 object *args;
1337{
1338 int a[4];
1339 if (!getrectarg(args, a))
1340 return NULL;
1341 wchange(wp->w_win, a[0], a[1], a[2], a[3]);
1342 INCREF(None);
1343 return None;
1344}
1345
1346static object *
1347window_gettitle(wp, args)
1348 windowobject *wp;
1349 object *args;
1350{
1351 if (!getnoarg(args))
1352 return NULL;
1353 INCREF(wp->w_title);
1354 return wp->w_title;
1355}
1356
1357static object *
Guido van Rossum541c8c01991-05-05 20:13:41 +00001358window_getwinpos(wp, args)
1359 windowobject *wp;
1360 object *args;
1361{
1362 int h, v;
1363 if (!getnoarg(args))
1364 return NULL;
1365 wgetwinpos(wp->w_win, &h, &v);
1366 return makepoint(h, v);
1367}
1368
1369static object *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001370window_getwinsize(wp, args)
1371 windowobject *wp;
1372 object *args;
1373{
1374 int width, height;
1375 if (!getnoarg(args))
1376 return NULL;
1377 wgetwinsize(wp->w_win, &width, &height);
1378 return makepoint(width, height);
1379}
1380
1381static object *
1382window_getdocsize(wp, args)
1383 windowobject *wp;
1384 object *args;
1385{
1386 int width, height;
1387 if (!getnoarg(args))
1388 return NULL;
1389 wgetdocsize(wp->w_win, &width, &height);
1390 return makepoint(width, height);
1391}
1392
1393static object *
1394window_getorigin(wp, args)
1395 windowobject *wp;
1396 object *args;
1397{
1398 int width, height;
1399 if (!getnoarg(args))
1400 return NULL;
1401 wgetorigin(wp->w_win, &width, &height);
1402 return makepoint(width, height);
1403}
1404
1405static object *
1406window_scroll(wp, args)
1407 windowobject *wp;
1408 object *args;
1409{
1410 int a[6];
1411 if (!getrectpointarg(args, a))
1412 return NULL;
1413 wscroll(wp->w_win, a[0], a[1], a[2], a[3], a[4], a[5]);
1414 INCREF(None);
1415 return None;
1416}
1417
1418static object *
1419window_setdocsize(wp, args)
1420 windowobject *wp;
1421 object *args;
1422{
1423 int a[2];
1424 if (!getpointarg(args, a))
1425 return NULL;
1426 wsetdocsize(wp->w_win, a[0], a[1]);
1427 INCREF(None);
1428 return None;
1429}
1430
1431static object *
1432window_setorigin(wp, args)
1433 windowobject *wp;
1434 object *args;
1435{
1436 int a[2];
1437 if (!getpointarg(args, a))
1438 return NULL;
1439 wsetorigin(wp->w_win, a[0], a[1]);
1440 INCREF(None);
1441 return None;
1442}
1443
1444static object *
1445window_settitle(wp, args)
1446 windowobject *wp;
1447 object *args;
1448{
1449 object *title;
Guido van Rossumfc58e581992-01-27 16:45:55 +00001450 if (!getStrarg(args, &title))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001451 return NULL;
1452 DECREF(wp->w_title);
1453 INCREF(title);
1454 wp->w_title = title;
1455 wsettitle(wp->w_win, getstringvalue(title));
1456 INCREF(None);
1457 return None;
1458}
1459
1460static object *
1461window_show(wp, args)
1462 windowobject *wp;
1463 object *args;
1464{
1465 int a[4];
1466 if (!getrectarg(args, a))
1467 return NULL;
1468 wshow(wp->w_win, a[0], a[1], a[2], a[3]);
1469 INCREF(None);
1470 return None;
1471}
1472
1473static object *
1474window_settimer(wp, args)
1475 windowobject *wp;
1476 object *args;
1477{
1478 int a;
1479 if (!getintarg(args, &a))
1480 return NULL;
1481 wsettimer(wp->w_win, a);
1482 INCREF(None);
1483 return None;
1484}
1485
1486static object *
1487window_menucreate(self, args)
1488 windowobject *self;
1489 object *args;
1490{
1491 menuobject *mp;
Guido van Rossumfc58e581992-01-27 16:45:55 +00001492 char *title;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001493 if (!getstrarg(args, &title))
1494 return NULL;
1495 wmenusetdeflocal(1);
1496 mp = newmenuobject(title);
1497 if (mp == NULL)
1498 return NULL;
1499 wmenuattach(self->w_win, mp->m_menu);
1500 return (object *)mp;
1501}
1502
1503static object *
1504window_textcreate(self, args)
1505 windowobject *self;
1506 object *args;
1507{
1508 textobject *tp;
1509 int a[4];
1510 if (!getrectarg(args, a))
1511 return NULL;
1512 return (object *)
1513 newtextobject(self, a[0], a[1], a[2], a[3]);
1514}
1515
Guido van Rossum5b10f451990-10-30 16:01:48 +00001516static object *
1517window_setselection(self, args)
1518 windowobject *self;
1519 object *args;
1520{
Guido van Rossumfc58e581992-01-27 16:45:55 +00001521 int sel, size, ok;
1522 char *text;
1523 if (!getargs(args, "(is#)", &sel, &text, &size))
Guido van Rossum5b10f451990-10-30 16:01:48 +00001524 return NULL;
Guido van Rossumfc58e581992-01-27 16:45:55 +00001525 ok = wsetselection(self->w_win, sel, text, size);
Guido van Rossum5b10f451990-10-30 16:01:48 +00001526 return newintobject(ok);
1527}
1528
1529static object *
1530window_setwincursor(self, args)
1531 windowobject *self;
1532 object *args;
1533{
Guido van Rossumfc58e581992-01-27 16:45:55 +00001534 char *name;
Guido van Rossum5b10f451990-10-30 16:01:48 +00001535 CURSOR *c;
Guido van Rossum3c8ba7a1992-02-05 11:15:00 +00001536 if (!getargs(args, "z", &name))
Guido van Rossum5b10f451990-10-30 16:01:48 +00001537 return NULL;
Guido van Rossum3c8ba7a1992-02-05 11:15:00 +00001538 if (name == NULL)
1539 c = NULL;
1540 else {
1541 c = wfetchcursor(name);
1542 if (c == NULL) {
1543 err_setstr(StdwinError, "no such cursor");
1544 return NULL;
1545 }
Guido van Rossum5b10f451990-10-30 16:01:48 +00001546 }
1547 wsetwincursor(self->w_win, c);
1548 INCREF(None);
1549 return None;
1550}
1551
Guido van Rossumfc58e581992-01-27 16:45:55 +00001552static object *
1553window_setactive(self, args)
1554 windowobject *self;
1555 object *args;
1556{
1557 if (!getnoarg(args))
1558 return NULL;
1559 wsetactive(self->w_win);
1560 INCREF(None);
1561 return None;
1562}
1563
Guido van Rossum8dcbbac1991-07-27 21:42:24 +00001564#ifdef CWI_HACKS
1565static object *
1566window_getxwindowid(self, args)
1567 windowobject *self;
1568 object *args;
1569{
1570 long wid = wgetxwindowid(self->w_win);
1571 return newintobject(wid);
1572}
1573#endif
1574
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001575static struct methodlist window_methods[] = {
1576 {"begindrawing",window_begindrawing},
1577 {"change", window_change},
Guido van Rossum3c284741991-11-27 14:54:54 +00001578 {"close", window_close},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001579 {"getdocsize", window_getdocsize},
1580 {"getorigin", window_getorigin},
1581 {"gettitle", window_gettitle},
Guido van Rossum541c8c01991-05-05 20:13:41 +00001582 {"getwinpos", window_getwinpos},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001583 {"getwinsize", window_getwinsize},
1584 {"menucreate", window_menucreate},
1585 {"scroll", window_scroll},
Guido van Rossumb7e51601992-01-26 18:13:18 +00001586 {"setactive", window_setactive},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001587 {"setdocsize", window_setdocsize},
1588 {"setorigin", window_setorigin},
Guido van Rossum5b10f451990-10-30 16:01:48 +00001589 {"setselection",window_setselection},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001590 {"settimer", window_settimer},
1591 {"settitle", window_settitle},
Guido van Rossum27201061991-04-16 08:43:03 +00001592 {"setwincursor",window_setwincursor},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001593 {"show", window_show},
1594 {"textcreate", window_textcreate},
Guido van Rossum8dcbbac1991-07-27 21:42:24 +00001595#ifdef CWI_HACKS
1596 {"getxwindowid",window_getxwindowid},
1597#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001598 {NULL, NULL} /* sentinel */
1599};
1600
1601static object *
1602window_getattr(wp, name)
1603 windowobject *wp;
1604 char *name;
1605{
Guido van Rossum85f50761991-10-20 20:22:50 +00001606 object *v = NULL;
Guido van Rossum77b46041992-01-14 18:41:24 +00001607 if (wp->w_win == NULL) {
1608 err_setstr(StdwinError, "window already closed");
1609 return NULL;
1610 }
Guido van Rossum85f50761991-10-20 20:22:50 +00001611 if (strcmp(name, "__dict__") == 0) {
1612 v = wp->w_attr;
1613 if (v == NULL)
1614 v = None;
1615 }
1616 else if (wp->w_attr != NULL) {
1617 v = dictlookup(wp->w_attr, name);
1618 }
1619 if (v != NULL) {
1620 INCREF(v);
1621 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001622 }
1623 return findmethod(window_methods, (object *)wp, name);
1624}
1625
1626static int
1627window_setattr(wp, name, v)
1628 windowobject *wp;
1629 char *name;
1630 object *v;
1631{
1632 if (wp->w_attr == NULL) {
1633 wp->w_attr = newdictobject();
1634 if (wp->w_attr == NULL)
1635 return -1;
1636 }
1637 if (v == NULL)
1638 return dictremove(wp->w_attr, name);
1639 else
1640 return dictinsert(wp->w_attr, name, v);
1641}
1642
Guido van Rossum541c8c01991-05-05 20:13:41 +00001643typeobject Windowtype = {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001644 OB_HEAD_INIT(&Typetype)
1645 0, /*ob_size*/
1646 "window", /*tp_name*/
1647 sizeof(windowobject), /*tp_size*/
1648 0, /*tp_itemsize*/
1649 /* methods */
1650 window_dealloc, /*tp_dealloc*/
1651 window_print, /*tp_print*/
1652 window_getattr, /*tp_getattr*/
1653 window_setattr, /*tp_setattr*/
1654 0, /*tp_compare*/
1655 0, /*tp_repr*/
1656};
1657
1658/* Stdwin methods */
1659
1660static object *
1661stdwin_open(sw, args)
1662 object *sw;
1663 object *args;
1664{
1665 int tag;
1666 object *title;
1667 windowobject *wp;
Guido van Rossumfc58e581992-01-27 16:45:55 +00001668 if (!getStrarg(args, &title))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001669 return NULL;
1670 for (tag = 0; tag < MAXNWIN; tag++) {
1671 if (windowlist[tag] == NULL)
1672 break;
1673 }
Guido van Rossum27201061991-04-16 08:43:03 +00001674 if (tag >= MAXNWIN) {
Guido van Rossum87e7ea71991-12-10 14:00:03 +00001675 err_setstr(StdwinError, "creating too many windows");
Guido van Rossum27201061991-04-16 08:43:03 +00001676 return NULL;
1677 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001678 wp = NEWOBJ(windowobject, &Windowtype);
1679 if (wp == NULL)
1680 return NULL;
1681 INCREF(title);
1682 wp->w_title = title;
1683 wp->w_win = wopen(getstringvalue(title), (void (*)()) NULL);
1684 wp->w_attr = NULL;
1685 if (wp->w_win == NULL) {
1686 DECREF(wp);
1687 return NULL;
1688 }
1689 windowlist[tag] = wp;
1690 wsettag(wp->w_win, tag);
1691 return (object *)wp;
1692}
1693
1694static object *
Guido van Rossum246b9d81991-06-03 10:55:14 +00001695window2object(win)
1696 WINDOW *win;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001697{
Guido van Rossum246b9d81991-06-03 10:55:14 +00001698 object *w;
1699 if (win == NULL)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001700 w = None;
1701 else {
Guido van Rossum246b9d81991-06-03 10:55:14 +00001702 int tag = wgettag(win);
1703 if (tag < 0 || tag >= MAXNWIN || windowlist[tag] == NULL ||
1704 windowlist[tag]->w_win != win)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001705 w = None;
1706 else
1707 w = (object *)windowlist[tag];
1708 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001709 INCREF(w);
Guido van Rossum246b9d81991-06-03 10:55:14 +00001710 return w;
1711}
1712
1713static object *
1714stdwin_get_poll_event(poll, args)
1715 int poll;
1716 object *args;
1717{
1718 EVENT e;
Guido van Rossum2ee12f41992-04-13 15:54:35 +00001719 object *u, *v, *w;
Guido van Rossum246b9d81991-06-03 10:55:14 +00001720 if (!getnoarg(args))
1721 return NULL;
1722 if (Drawing != NULL) {
Guido van Rossum87e7ea71991-12-10 14:00:03 +00001723 err_setstr(StdwinError, "cannot getevent() while drawing");
Guido van Rossum246b9d81991-06-03 10:55:14 +00001724 return NULL;
1725 }
1726 again:
1727 if (poll) {
1728 if (!wpollevent(&e)) {
1729 INCREF(None);
1730 return None;
1731 }
1732 }
1733 else
1734 wgetevent(&e);
1735 if (e.type == WE_COMMAND && e.u.command == WC_CANCEL) {
1736 /* Turn keyboard interrupts into exceptions */
1737 err_set(KeyboardInterrupt);
1738 return NULL;
1739 }
1740 if (e.type == WE_COMMAND && e.u.command == WC_CLOSE) {
1741 /* Turn WC_CLOSE commands into WE_CLOSE events */
1742 e.type = WE_CLOSE;
1743 }
Guido van Rossum2ee12f41992-04-13 15:54:35 +00001744 v = window2object(e.window);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001745 switch (e.type) {
1746 case WE_CHAR:
1747 {
1748 char c[1];
1749 c[0] = e.u.character;
1750 w = newsizedstringobject(c, 1);
1751 }
1752 break;
1753 case WE_COMMAND:
1754 w = newintobject((long)e.u.command);
1755 break;
1756 case WE_DRAW:
1757 w = makerect(e.u.area.left, e.u.area.top,
1758 e.u.area.right, e.u.area.bottom);
1759 break;
1760 case WE_MOUSE_DOWN:
1761 case WE_MOUSE_MOVE:
1762 case WE_MOUSE_UP:
Guido van Rossum2ee12f41992-04-13 15:54:35 +00001763 w = mkvalue("((ii)iii)",
1764 e.u.where.h, e.u.where.v,
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001765 e.u.where.clicks,
1766 e.u.where.button,
1767 e.u.where.mask);
1768 break;
1769 case WE_MENU:
Guido van Rossum2d14e211991-02-19 12:26:49 +00001770 if (e.u.m.id >= IDOFFSET && e.u.m.id < IDOFFSET+MAXNMENU &&
1771 menulist[e.u.m.id - IDOFFSET] != NULL)
Guido van Rossum2ee12f41992-04-13 15:54:35 +00001772 w = mkvalue("(Oi)",
1773 menulist[e.u.m.id - IDOFFSET], e.u.m.item);
Guido van Rossum246b9d81991-06-03 10:55:14 +00001774 else {
1775 /* Ghost menu event.
1776 Can occur only on the Mac if another part
1777 of the aplication has installed a menu;
1778 like the THINK C console library. */
1779 DECREF(v);
1780 goto again;
1781 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001782 break;
Guido van Rossum5b10f451990-10-30 16:01:48 +00001783 case WE_LOST_SEL:
1784 w = newintobject((long)e.u.sel);
1785 break;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001786 default:
1787 w = None;
1788 INCREF(w);
1789 break;
1790 }
1791 if (w == NULL) {
1792 DECREF(v);
1793 return NULL;
1794 }
Guido van Rossum2ee12f41992-04-13 15:54:35 +00001795 u = mkvalue("(iOO)", e.type, v, w);
1796 XDECREF(v);
1797 XDECREF(w);
1798 return u;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001799}
1800
1801static object *
Guido van Rossume8e7cf41991-01-16 14:06:18 +00001802stdwin_getevent(sw, args)
1803 object *sw;
1804 object *args;
1805{
1806 return stdwin_get_poll_event(0, args);
1807}
1808
1809static object *
1810stdwin_pollevent(sw, args)
1811 object *sw;
1812 object *args;
1813{
1814 return stdwin_get_poll_event(1, args);
1815}
1816
1817static object *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001818stdwin_setdefwinpos(sw, args)
1819 object *sw;
1820 object *args;
1821{
1822 int a[2];
1823 if (!getpointarg(args, a))
1824 return NULL;
1825 wsetdefwinpos(a[0], a[1]);
1826 INCREF(None);
1827 return None;
1828}
1829
1830static object *
1831stdwin_setdefwinsize(sw, args)
1832 object *sw;
1833 object *args;
1834{
1835 int a[2];
1836 if (!getpointarg(args, a))
1837 return NULL;
1838 wsetdefwinsize(a[0], a[1]);
1839 INCREF(None);
1840 return None;
1841}
1842
1843static object *
Guido van Rossum0c2290b1991-04-03 19:12:14 +00001844stdwin_setdefscrollbars(sw, args)
1845 object *sw;
1846 object *args;
1847{
1848 int a[2];
1849 if (!getpointarg(args, a))
1850 return NULL;
1851 wsetdefscrollbars(a[0], a[1]);
1852 INCREF(None);
1853 return None;
1854}
1855
1856static object *
Guido van Rossum541c8c01991-05-05 20:13:41 +00001857stdwin_getdefwinpos(self, args)
1858 object *self;
Guido van Rossum33f17701991-02-13 23:19:39 +00001859 object *args;
1860{
1861 int h, v;
1862 if (!getnoarg(args))
1863 return NULL;
1864 wgetdefwinpos(&h, &v);
1865 return makepoint(h, v);
1866}
1867
1868static object *
Guido van Rossum541c8c01991-05-05 20:13:41 +00001869stdwin_getdefwinsize(self, args)
1870 object *self;
Guido van Rossum33f17701991-02-13 23:19:39 +00001871 object *args;
1872{
1873 int width, height;
1874 if (!getnoarg(args))
1875 return NULL;
1876 wgetdefwinsize(&width, &height);
1877 return makepoint(width, height);
1878}
1879
1880static object *
Guido van Rossum541c8c01991-05-05 20:13:41 +00001881stdwin_getdefscrollbars(self, args)
1882 object *self;
Guido van Rossum0c2290b1991-04-03 19:12:14 +00001883 object *args;
1884{
1885 int h, v;
1886 if (!getnoarg(args))
1887 return NULL;
1888 wgetdefscrollbars(&h, &v);
1889 return makepoint(h, v);
1890}
1891
1892static object *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001893stdwin_menucreate(self, args)
1894 object *self;
1895 object *args;
1896{
Guido van Rossumfc58e581992-01-27 16:45:55 +00001897 char *title;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001898 if (!getstrarg(args, &title))
1899 return NULL;
1900 wmenusetdeflocal(0);
1901 return (object *)newmenuobject(title);
1902}
1903
1904static object *
1905stdwin_askfile(self, args)
1906 object *self;
1907 object *args;
1908{
Guido van Rossumfc58e581992-01-27 16:45:55 +00001909 char *prompt, *dflt;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001910 int new, ret;
1911 char buf[256];
1912 if (!getstrstrintarg(args, &prompt, &dflt, &new))
1913 return NULL;
Guido van Rossumfc58e581992-01-27 16:45:55 +00001914 strncpy(buf, dflt, sizeof buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001915 buf[sizeof buf - 1] = '\0';
Guido van Rossumfc58e581992-01-27 16:45:55 +00001916 ret = waskfile(prompt, buf, sizeof buf, new);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001917 if (!ret) {
1918 err_set(KeyboardInterrupt);
1919 return NULL;
1920 }
1921 return newstringobject(buf);
1922}
1923
1924static object *
1925stdwin_askync(self, args)
1926 object *self;
1927 object *args;
1928{
Guido van Rossumfc58e581992-01-27 16:45:55 +00001929 char *prompt;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001930 int new, ret;
1931 if (!getstrintarg(args, &prompt, &new))
1932 return NULL;
Guido van Rossumfc58e581992-01-27 16:45:55 +00001933 ret = waskync(prompt, new);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001934 if (ret < 0) {
1935 err_set(KeyboardInterrupt);
1936 return NULL;
1937 }
1938 return newintobject((long)ret);
1939}
1940
1941static object *
1942stdwin_askstr(self, args)
1943 object *self;
1944 object *args;
1945{
Guido van Rossumfc58e581992-01-27 16:45:55 +00001946 char *prompt, *dflt;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001947 int ret;
1948 char buf[256];
1949 if (!getstrstrarg(args, &prompt, &dflt))
1950 return NULL;
Guido van Rossumfc58e581992-01-27 16:45:55 +00001951 strncpy(buf, dflt, sizeof buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001952 buf[sizeof buf - 1] = '\0';
Guido van Rossumfc58e581992-01-27 16:45:55 +00001953 ret = waskstr(prompt, buf, sizeof buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001954 if (!ret) {
1955 err_set(KeyboardInterrupt);
1956 return NULL;
1957 }
1958 return newstringobject(buf);
1959}
1960
1961static object *
1962stdwin_message(self, args)
1963 object *self;
1964 object *args;
1965{
Guido van Rossumfc58e581992-01-27 16:45:55 +00001966 char *msg;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001967 if (!getstrarg(args, &msg))
1968 return NULL;
Guido van Rossumfc58e581992-01-27 16:45:55 +00001969 wmessage(msg);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001970 INCREF(None);
1971 return None;
1972}
1973
1974static object *
1975stdwin_fleep(self, args)
1976 object *self;
1977 object *args;
1978{
1979 if (!getnoarg(args))
1980 return NULL;
1981 wfleep();
1982 INCREF(None);
1983 return None;
1984}
1985
1986static object *
1987stdwin_setcutbuffer(self, args)
1988 object *self;
1989 object *args;
1990{
Guido van Rossumfc58e581992-01-27 16:45:55 +00001991 int i, size;
1992 char *str;
1993 if (!getargs(args, "(is#)", &i, &str, &size))
Guido van Rossum124967c1990-11-06 15:17:35 +00001994 return NULL;
Guido van Rossumfc58e581992-01-27 16:45:55 +00001995 wsetcutbuffer(i, str, size);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001996 INCREF(None);
1997 return None;
1998}
1999
2000static object *
Guido van Rossum246b9d81991-06-03 10:55:14 +00002001stdwin_getactive(self, args)
2002 object *self;
2003 object *args;
2004{
2005 return window2object(wgetactive());
2006}
2007
2008static object *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002009stdwin_getcutbuffer(self, args)
2010 object *self;
2011 object *args;
2012{
Guido van Rossum5b10f451990-10-30 16:01:48 +00002013 int i;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002014 char *str;
Guido van Rossum01769f01990-10-30 13:39:00 +00002015 int len;
Guido van Rossum124967c1990-11-06 15:17:35 +00002016 if (!getintarg(args, &i))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002017 return NULL;
Guido van Rossum5b10f451990-10-30 16:01:48 +00002018 str = wgetcutbuffer(i, &len);
Guido van Rossum01769f01990-10-30 13:39:00 +00002019 if (str == NULL) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002020 str = "";
Guido van Rossum01769f01990-10-30 13:39:00 +00002021 len = 0;
2022 }
2023 return newsizedstringobject(str, len);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002024}
2025
Guido van Rossum5b10f451990-10-30 16:01:48 +00002026static object *
2027stdwin_rotatecutbuffers(self, args)
2028 object *self;
2029 object *args;
2030{
2031 int i;
2032 if (!getintarg(args, &i))
2033 return NULL;
2034 wrotatecutbuffers(i);
2035 INCREF(None);
2036 return None;
2037}
2038
2039static object *
2040stdwin_getselection(self, args)
2041 object *self;
2042 object *args;
2043{
2044 int sel;
2045 char *data;
2046 int len;
2047 if (!getintarg(args, &sel))
2048 return NULL;
2049 data = wgetselection(sel, &len);
2050 if (data == NULL) {
2051 data = "";
2052 len = 0;
2053 }
2054 return newsizedstringobject(data, len);
2055}
2056
2057static object *
2058stdwin_resetselection(self, args)
2059 object *self;
2060 object *args;
2061{
2062 int sel;
2063 if (!getintarg(args, &sel))
2064 return NULL;
2065 wresetselection(sel);
2066 INCREF(None);
2067 return None;
2068}
2069
Guido van Rossum0c2290b1991-04-03 19:12:14 +00002070static object *
2071stdwin_fetchcolor(self, args)
2072 object *self;
2073 object *args;
2074{
Guido van Rossumfc58e581992-01-27 16:45:55 +00002075 char *colorname;
Guido van Rossum0c2290b1991-04-03 19:12:14 +00002076 if (!getstrarg(args, &colorname))
2077 return NULL;
Guido van Rossumfc58e581992-01-27 16:45:55 +00002078 return newintobject((long)wfetchcolor(colorname));
Guido van Rossum0c2290b1991-04-03 19:12:14 +00002079}
2080
Guido van Rossum541c8c01991-05-05 20:13:41 +00002081static object *
2082stdwin_getscrsize(self, args)
2083 object *self;
2084 object *args;
2085{
2086 int width, height;
2087 if (!getnoarg(args))
2088 return NULL;
2089 wgetscrsize(&width, &height);
2090 return makepoint(width, height);
2091}
2092
2093static object *
2094stdwin_getscrmm(self, args)
2095 object *self;
2096 object *args;
2097{
2098 int width, height;
2099 if (!getnoarg(args))
2100 return NULL;
2101 wgetscrmm(&width, &height);
2102 return makepoint(width, height);
2103}
2104
Guido van Rossumed233a51992-06-23 09:07:03 +00002105#ifdef unix
2106static object *
2107stdwin_connectionnumber(self, args)
2108 object *self;
2109 object *args;
2110{
2111 if (!getnoarg(args))
2112 return NULL;
2113 return newintobject((long) wconnectionnumber());
2114}
2115#endif
2116
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002117static struct methodlist stdwin_methods[] = {
2118 {"askfile", stdwin_askfile},
2119 {"askstr", stdwin_askstr},
2120 {"askync", stdwin_askync},
Guido van Rossum27201061991-04-16 08:43:03 +00002121 {"fetchcolor", stdwin_fetchcolor},
Guido van Rossumed233a51992-06-23 09:07:03 +00002122#ifdef unix
2123 {"fileno", stdwin_connectionnumber},
2124 {"connectionnumber", stdwin_connectionnumber},
2125#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002126 {"fleep", stdwin_fleep},
Guido van Rossum246b9d81991-06-03 10:55:14 +00002127 {"getactive", stdwin_getactive},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002128 {"getcutbuffer", stdwin_getcutbuffer},
Guido van Rossum0c2290b1991-04-03 19:12:14 +00002129 {"getdefscrollbars", stdwin_getdefscrollbars},
Guido van Rossum33f17701991-02-13 23:19:39 +00002130 {"getdefwinpos", stdwin_getdefwinpos},
2131 {"getdefwinsize", stdwin_getdefwinsize},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002132 {"getevent", stdwin_getevent},
Guido van Rossum541c8c01991-05-05 20:13:41 +00002133 {"getscrmm", stdwin_getscrmm},
2134 {"getscrsize", stdwin_getscrsize},
Guido van Rossum27201061991-04-16 08:43:03 +00002135 {"getselection", stdwin_getselection},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002136 {"menucreate", stdwin_menucreate},
2137 {"message", stdwin_message},
2138 {"open", stdwin_open},
Guido van Rossume8e7cf41991-01-16 14:06:18 +00002139 {"pollevent", stdwin_pollevent},
Guido van Rossum5b10f451990-10-30 16:01:48 +00002140 {"resetselection", stdwin_resetselection},
2141 {"rotatecutbuffers", stdwin_rotatecutbuffers},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002142 {"setcutbuffer", stdwin_setcutbuffer},
Guido van Rossum0c2290b1991-04-03 19:12:14 +00002143 {"setdefscrollbars", stdwin_setdefscrollbars},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002144 {"setdefwinpos", stdwin_setdefwinpos},
2145 {"setdefwinsize", stdwin_setdefwinsize},
2146
2147 /* Text measuring methods borrow code from drawing objects: */
2148 {"baseline", drawing_baseline},
2149 {"lineheight", drawing_lineheight},
2150 {"textbreak", drawing_textbreak},
2151 {"textwidth", drawing_textwidth},
Guido van Rossum0c2290b1991-04-03 19:12:14 +00002152
2153 /* Same for font setting methods: */
2154 {"setfont", drawing_setfont},
2155
2156 /* Same for color setting/getting methods: */
2157 {"getbgcolor", drawing_getbgcolor},
2158 {"getfgcolor", drawing_getfgcolor},
2159 {"setbgcolor", drawing_setbgcolor},
2160 {"setfgcolor", drawing_setfgcolor},
2161
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002162 {NULL, NULL} /* sentinel */
2163};
2164
2165void
2166initstdwin()
2167{
Guido van Rossumbbf94341991-12-16 15:44:53 +00002168 object *m, *d;
2169 static int inited = 0;
2170
Guido van Rossum2d14e211991-02-19 12:26:49 +00002171 if (!inited) {
2172 winit();
2173 inited = 1;
2174 }
Guido van Rossumbbf94341991-12-16 15:44:53 +00002175 m = initmodule("stdwin", stdwin_methods);
2176 d = getmoduledict(m);
2177
2178 /* Initialize stdwin.error exception */
2179 StdwinError = newstringobject("stdwin.error");
2180 if (StdwinError == NULL || dictinsert(d, "error", StdwinError) != 0)
2181 fatal("can't define stdwin.error");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002182}