blob: e33610d7d626a8745d8b1e6778b087d3c55142bc [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#include "modsupport.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +000067#include "ceval.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 Rossumff4949e1992-08-05 19:58:53 +000071#ifdef USE_THREAD
72
73#include "thread.h"
74
75static type_lock StdwinLock; /* Lock held when interpreter not locked */
76
77#define BGN_STDWIN BGN_SAVE acquire_lock(StdwinLock, 1);
78#define RET_STDWIN release_lock(StdwinLock); RET_SAVE
79#define END_STDWIN release_lock(StdwinLock); END_SAVE
80
81#else
82
83#define BGN_STDWIN BGN_SAVE
84#define RET_STDWIN RET_SAVE
85#define END_STDWIN END_SAVE
86
87#endif
88
Guido van Rossumbbf94341991-12-16 15:44:53 +000089static object *StdwinError; /* Exception stdwin.error */
Guido van Rossum87e7ea71991-12-10 14:00:03 +000090
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000091/* Window and menu object types declared here because of forward references */
92
93typedef struct {
94 OB_HEAD
95 object *w_title;
96 WINDOW *w_win;
97 object *w_attr; /* Attributes dictionary */
98} windowobject;
99
100extern typeobject Windowtype; /* Really static, forward */
101
102#define is_windowobject(wp) ((wp)->ob_type == &Windowtype)
103
104typedef struct {
105 OB_HEAD
106 MENU *m_menu;
107 int m_id;
108 object *m_attr; /* Attributes dictionary */
109} menuobject;
110
111extern typeobject Menutype; /* Really static, forward */
112
113#define is_menuobject(mp) ((mp)->ob_type == &Menutype)
114
115
116/* Strongly stdwin-specific argument handlers */
117
118static int
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000119getmenudetail(v, ep)
120 object *v;
121 EVENT *ep;
122{
Guido van Rossumfc58e581992-01-27 16:45:55 +0000123 menuobject *mp;
124 if (!getargs(v, "(Oi)", &mp, &ep->u.m.item))
125 return 0;
126 if (!is_menuobject(mp))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000127 return err_badarg();
Guido van Rossumfc58e581992-01-27 16:45:55 +0000128 ep->u.m.id = mp->m_id;
129 return 1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000130}
131
132static int
133geteventarg(v, ep)
134 object *v;
135 EVENT *ep;
136{
137 object *wp, *detail;
138 int a[4];
Guido van Rossumfc58e581992-01-27 16:45:55 +0000139 if (!getargs(v, "(iOO)", &ep->type, &wp, &detail))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000140 return 0;
Guido van Rossumfc58e581992-01-27 16:45:55 +0000141 if (is_windowobject(wp))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000142 ep->window = ((windowobject *)wp) -> w_win;
Guido van Rossumfc58e581992-01-27 16:45:55 +0000143 else if (wp == None)
144 ep->window = NULL;
145 else
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000146 return err_badarg();
Guido van Rossumfc58e581992-01-27 16:45:55 +0000147 switch (ep->type) {
148 case WE_CHAR: {
149 char c;
150 if (!getargs(detail, "c", &c))
151 return 0;
152 ep->u.character = c;
153 return 1;
154 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000155 case WE_COMMAND:
156 return getintarg(detail, &ep->u.command);
157 case WE_DRAW:
158 if (!getrectarg(detail, a))
159 return 0;
160 ep->u.area.left = a[0];
161 ep->u.area.top = a[1];
162 ep->u.area.right = a[2];
163 ep->u.area.bottom = a[3];
164 return 1;
165 case WE_MOUSE_DOWN:
166 case WE_MOUSE_UP:
167 case WE_MOUSE_MOVE:
Guido van Rossumfc58e581992-01-27 16:45:55 +0000168 return getargs(detail, "((ii)iii)",
169 &ep->u.where.h, &ep->u.where.v,
170 &ep->u.where.clicks,
171 &ep->u.where.button,
172 &ep->u.where.mask);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000173 case WE_MENU:
174 return getmenudetail(detail, ep);
Guido van Rossum3ee199e1992-06-30 12:48:26 +0000175 case WE_KEY:
176 return getargs(detail, "(ii)",
177 &ep->u.key.code, &ep->u.key.mask);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000178 default:
179 return 1;
180 }
181}
182
183
184/* Return construction tools */
185
186static object *
187makepoint(a, b)
188 int a, b;
189{
Guido van Rossum2ee12f41992-04-13 15:54:35 +0000190 return mkvalue("(ii)", a, b);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000191}
192
193static object *
194makerect(a, b, c, d)
195 int a, b, c, d;
196{
Guido van Rossum2ee12f41992-04-13 15:54:35 +0000197 return mkvalue("((ii)(ii))", a, b, c, d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000198}
199
200
201/* Drawing objects */
202
203typedef struct {
204 OB_HEAD
205 windowobject *d_ref;
206} drawingobject;
207
208static drawingobject *Drawing; /* Set to current drawing object, or NULL */
209
210/* Drawing methods */
211
Guido van Rossum3c284741991-11-27 14:54:54 +0000212static object *
213drawing_close(dp)
214 drawingobject *dp;
215{
216 if (dp->d_ref != NULL) {
217 wenddrawing(dp->d_ref->w_win);
218 Drawing = NULL;
219 DECREF(dp->d_ref);
220 dp->d_ref = NULL;
221 }
222 INCREF(None);
223 return None;
224}
Guido van Rossum77b46041992-01-14 18:41:24 +0000225
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000226static void
227drawing_dealloc(dp)
228 drawingobject *dp;
229{
Guido van Rossum3c284741991-11-27 14:54:54 +0000230 if (dp->d_ref != NULL) {
231 wenddrawing(dp->d_ref->w_win);
232 Drawing = NULL;
233 DECREF(dp->d_ref);
234 dp->d_ref = NULL;
235 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000236 free((char *)dp);
237}
238
239static object *
240drawing_generic(dp, args, func)
241 drawingobject *dp;
242 object *args;
243 void (*func) FPROTO((int, int, int, int));
244{
245 int a[4];
246 if (!getrectarg(args, a))
247 return NULL;
248 (*func)(a[0], a[1], a[2], a[3]);
249 INCREF(None);
250 return None;
251}
252
253static object *
254drawing_line(dp, args)
255 drawingobject *dp;
256 object *args;
257{
Guido van Rossumbf109731991-03-06 13:14:12 +0000258 return drawing_generic(dp, args, wdrawline);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000259}
260
261static object *
262drawing_xorline(dp, args)
263 drawingobject *dp;
264 object *args;
265{
Guido van Rossumbf109731991-03-06 13:14:12 +0000266 return drawing_generic(dp, args, wxorline);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000267}
268
269static object *
270drawing_circle(dp, args)
271 drawingobject *dp;
272 object *args;
273{
274 int a[3];
275 if (!getpointintarg(args, a))
276 return NULL;
277 wdrawcircle(a[0], a[1], a[2]);
278 INCREF(None);
279 return None;
280}
Guido van Rossuma2a181a1991-05-14 12:09:25 +0000281
Guido van Rossum27201061991-04-16 08:43:03 +0000282static object *
283drawing_fillcircle(dp, args)
284 drawingobject *dp;
285 object *args;
286{
287 int a[3];
288 if (!getpointintarg(args, a))
289 return NULL;
290 wfillcircle(a[0], a[1], a[2]);
291 INCREF(None);
292 return None;
293}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000294
295static object *
Guido van Rossuma2a181a1991-05-14 12:09:25 +0000296drawing_xorcircle(dp, args)
297 drawingobject *dp;
298 object *args;
299{
300 int a[3];
301 if (!getpointintarg(args, a))
302 return NULL;
303 wxorcircle(a[0], a[1], a[2]);
304 INCREF(None);
305 return None;
306}
307
308static object *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000309drawing_elarc(dp, args)
310 drawingobject *dp;
311 object *args;
312{
313 int a[6];
314 if (!get3pointarg(args, a))
315 return NULL;
316 wdrawelarc(a[0], a[1], a[2], a[3], a[4], a[5]);
317 INCREF(None);
318 return None;
319}
320
321static object *
Guido van Rossum27201061991-04-16 08:43:03 +0000322drawing_fillelarc(dp, args)
323 drawingobject *dp;
324 object *args;
325{
326 int a[6];
327 if (!get3pointarg(args, a))
328 return NULL;
329 wfillelarc(a[0], a[1], a[2], a[3], a[4], a[5]);
330 INCREF(None);
331 return None;
332}
333
334static object *
Guido van Rossuma2a181a1991-05-14 12:09:25 +0000335drawing_xorelarc(dp, args)
336 drawingobject *dp;
337 object *args;
338{
339 int a[6];
340 if (!get3pointarg(args, a))
341 return NULL;
342 wxorelarc(a[0], a[1], a[2], a[3], a[4], a[5]);
343 INCREF(None);
344 return None;
345}
346
347static object *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000348drawing_box(dp, args)
349 drawingobject *dp;
350 object *args;
351{
Guido van Rossumbf109731991-03-06 13:14:12 +0000352 return drawing_generic(dp, args, wdrawbox);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000353}
354
355static object *
356drawing_erase(dp, args)
357 drawingobject *dp;
358 object *args;
359{
Guido van Rossumbf109731991-03-06 13:14:12 +0000360 return drawing_generic(dp, args, werase);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000361}
362
363static object *
364drawing_paint(dp, args)
365 drawingobject *dp;
366 object *args;
367{
Guido van Rossumbf109731991-03-06 13:14:12 +0000368 return drawing_generic(dp, args, wpaint);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000369}
370
371static object *
372drawing_invert(dp, args)
373 drawingobject *dp;
374 object *args;
375{
Guido van Rossumbf109731991-03-06 13:14:12 +0000376 return drawing_generic(dp, args, winvert);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000377}
378
Guido van Rossum27201061991-04-16 08:43:03 +0000379static POINT *
380getpointsarray(v, psize)
381 object *v;
382 int *psize;
383{
384 int n = -1;
385 object * (*getitem) PROTO((object *, int));
386 int i;
387 POINT *points;
388
389 if (v == NULL)
390 ;
391 else if (is_listobject(v)) {
392 n = getlistsize(v);
393 getitem = getlistitem;
394 }
395 else if (is_tupleobject(v)) {
396 n = gettuplesize(v);
397 getitem = gettupleitem;
398 }
399
400 if (n <= 0) {
401 (void) err_badarg();
402 return NULL;
403 }
404
405 points = NEW(POINT, n);
406 if (points == NULL) {
407 (void) err_nomem();
408 return NULL;
409 }
410
411 for (i = 0; i < n; i++) {
412 object *w = (*getitem)(v, i);
413 int a[2];
414 if (!getpointarg(w, a)) {
415 DEL(points);
416 return NULL;
417 }
418 points[i].h = a[0];
419 points[i].v = a[1];
420 }
421
422 *psize = n;
423 return points;
424}
425
426static object *
427drawing_poly(dp, args)
428 drawingobject *dp;
429 object *args;
430{
431 int n;
432 POINT *points = getpointsarray(args, &n);
433 if (points == NULL)
434 return NULL;
435 wdrawpoly(n, points);
436 DEL(points);
437 INCREF(None);
438 return None;
439}
440
441static object *
442drawing_fillpoly(dp, args)
443 drawingobject *dp;
444 object *args;
445{
446 int n;
447 POINT *points = getpointsarray(args, &n);
448 if (points == NULL)
449 return NULL;
450 wfillpoly(n, points);
451 DEL(points);
452 INCREF(None);
453 return None;
454}
455
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000456static object *
Guido van Rossuma2a181a1991-05-14 12:09:25 +0000457drawing_xorpoly(dp, args)
458 drawingobject *dp;
459 object *args;
460{
461 int n;
462 POINT *points = getpointsarray(args, &n);
463 if (points == NULL)
464 return NULL;
465 wxorpoly(n, points);
466 DEL(points);
467 INCREF(None);
468 return None;
469}
470
471static object *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000472drawing_cliprect(dp, args)
473 drawingobject *dp;
474 object *args;
475{
Guido van Rossumbf109731991-03-06 13:14:12 +0000476 return drawing_generic(dp, args, wcliprect);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000477}
478
479static object *
480drawing_noclip(dp, args)
481 drawingobject *dp;
482 object *args;
483{
484 if (!getnoarg(args))
485 return NULL;
486 wnoclip();
487 INCREF(None);
488 return None;
489}
490
491static object *
492drawing_shade(dp, args)
493 drawingobject *dp;
494 object *args;
495{
496 int a[5];
497 if (!getrectintarg(args, a))
498 return NULL;
499 wshade(a[0], a[1], a[2], a[3], a[4]);
500 INCREF(None);
501 return None;
502}
503
504static object *
505drawing_text(dp, args)
506 drawingobject *dp;
507 object *args;
508{
Guido van Rossumfc58e581992-01-27 16:45:55 +0000509 int h, v, size;
510 char *text;
511 if (!getargs(args, "((ii)s#)", &h, &v, &text, &size))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000512 return NULL;
Guido van Rossumfc58e581992-01-27 16:45:55 +0000513 wdrawtext(h, v, text, size);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000514 INCREF(None);
515 return None;
516}
517
518/* The following four are also used as stdwin functions */
519
520static object *
521drawing_lineheight(dp, args)
522 drawingobject *dp;
523 object *args;
524{
525 if (!getnoarg(args))
526 return NULL;
527 return newintobject((long)wlineheight());
528}
529
530static object *
531drawing_baseline(dp, args)
532 drawingobject *dp;
533 object *args;
534{
535 if (!getnoarg(args))
536 return NULL;
537 return newintobject((long)wbaseline());
538}
539
540static object *
541drawing_textwidth(dp, args)
542 drawingobject *dp;
543 object *args;
544{
Guido van Rossumfc58e581992-01-27 16:45:55 +0000545 char *text;
546 int size;
547 if (!getargs(args, "s#", &text, &size))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000548 return NULL;
Guido van Rossumfc58e581992-01-27 16:45:55 +0000549 return newintobject((long)wtextwidth(text, size));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000550}
551
552static object *
553drawing_textbreak(dp, args)
554 drawingobject *dp;
555 object *args;
556{
Guido van Rossumfc58e581992-01-27 16:45:55 +0000557 char *text;
558 int size, width;
559 if (!getargs(args, "(s#i)", &text, &size, &width))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000560 return NULL;
Guido van Rossumfc58e581992-01-27 16:45:55 +0000561 return newintobject((long)wtextbreak(text, size, width));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000562}
563
Guido van Rossum0c2290b1991-04-03 19:12:14 +0000564static object *
565drawing_setfont(self, args)
566 drawingobject *self;
567 object *args;
568{
Guido van Rossumfc58e581992-01-27 16:45:55 +0000569 char *font;
570 char style = '\0';
571 int size = 0;
572 if (args == NULL || !is_tupleobject(args)) {
Guido van Rossum3c8ba7a1992-02-05 11:15:00 +0000573 if (!getargs(args, "z", &font))
Guido van Rossum50429a11991-04-04 15:24:07 +0000574 return NULL;
575 }
576 else {
Guido van Rossumfc58e581992-01-27 16:45:55 +0000577 int n = gettuplesize(args);
578 if (n == 2) {
579 if (!getargs(args, "(zi)", &font, &size))
580 return NULL;
581 }
582 else if (!getargs(args, "(zic)", &font, &size, &style)) {
583 err_clear();
584 if (!getargs(args, "(zci)", &font, &style, &size))
585 return NULL;
Guido van Rossum50429a11991-04-04 15:24:07 +0000586 }
587 }
Guido van Rossumfc58e581992-01-27 16:45:55 +0000588 if (font != NULL)
589 wsetfont(font);
Guido van Rossum50429a11991-04-04 15:24:07 +0000590 if (size != 0)
591 wsetsize(size);
Guido van Rossumfc58e581992-01-27 16:45:55 +0000592 switch (style) {
593 case 'b':
594 wsetbold();
595 break;
596 case 'i':
597 wsetitalic();
598 break;
599 case 'o':
600 wsetbolditalic();
601 break;
602 case 'u':
603 wsetunderline();
604 break;
605 case 'p':
606 wsetplain();
607 break;
608 }
Guido van Rossum0c2290b1991-04-03 19:12:14 +0000609 INCREF(None);
610 return None;
611}
612
613static object *
614drawing_getbgcolor(self, args)
615 object *self;
616 object *args;
617{
618 if (!getnoarg(args))
619 return NULL;
620 return newintobject((long)wgetbgcolor());
621}
622
623static object *
624drawing_getfgcolor(self, args)
625 object *self;
626 object *args;
627{
628 if (!getnoarg(args))
629 return NULL;
630 return newintobject((long)wgetfgcolor());
631}
632
633static object *
634drawing_setbgcolor(self, args)
635 object *self;
636 object *args;
637{
638 long color;
639 if (!getlongarg(args, &color))
640 return NULL;
641 wsetbgcolor((COLOR)color);
642 INCREF(None);
643 return None;
644}
645
646static object *
647drawing_setfgcolor(self, args)
648 object *self;
649 object *args;
650{
651 long color;
652 if (!getlongarg(args, &color))
653 return NULL;
654 wsetfgcolor((COLOR)color);
655 INCREF(None);
656 return None;
657}
658
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000659static struct methodlist drawing_methods[] = {
660 {"box", drawing_box},
661 {"circle", drawing_circle},
662 {"cliprect", drawing_cliprect},
Guido van Rossum3c284741991-11-27 14:54:54 +0000663 {"close", drawing_close},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000664 {"elarc", drawing_elarc},
Guido van Rossum3c284741991-11-27 14:54:54 +0000665 {"enddrawing", drawing_close},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000666 {"erase", drawing_erase},
Guido van Rossum27201061991-04-16 08:43:03 +0000667 {"fillcircle", drawing_fillcircle},
Guido van Rossuma2a181a1991-05-14 12:09:25 +0000668 {"fillelarc", drawing_fillelarc},
Guido van Rossum27201061991-04-16 08:43:03 +0000669 {"fillpoly", drawing_fillpoly},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000670 {"invert", drawing_invert},
671 {"line", drawing_line},
672 {"noclip", drawing_noclip},
673 {"paint", drawing_paint},
Guido van Rossum27201061991-04-16 08:43:03 +0000674 {"poly", drawing_poly},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000675 {"shade", drawing_shade},
676 {"text", drawing_text},
Guido van Rossuma2a181a1991-05-14 12:09:25 +0000677 {"xorcircle", drawing_xorcircle},
678 {"xorelarc", drawing_xorelarc},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000679 {"xorline", drawing_xorline},
Guido van Rossuma2a181a1991-05-14 12:09:25 +0000680 {"xorpoly", drawing_xorpoly},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000681
682 /* Text measuring methods: */
683 {"baseline", drawing_baseline},
684 {"lineheight", drawing_lineheight},
685 {"textbreak", drawing_textbreak},
686 {"textwidth", drawing_textwidth},
Guido van Rossum0c2290b1991-04-03 19:12:14 +0000687
688 /* Font setting methods: */
689 {"setfont", drawing_setfont},
690
691 /* Color methods: */
692 {"getbgcolor", drawing_getbgcolor},
693 {"getfgcolor", drawing_getfgcolor},
694 {"setbgcolor", drawing_setbgcolor},
695 {"setfgcolor", drawing_setfgcolor},
696
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000697 {NULL, NULL} /* sentinel */
698};
699
700static object *
Guido van Rossum77b46041992-01-14 18:41:24 +0000701drawing_getattr(dp, name)
702 drawingobject *dp;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000703 char *name;
704{
Guido van Rossum77b46041992-01-14 18:41:24 +0000705 if (dp->d_ref == NULL) {
706 err_setstr(StdwinError, "drawing object already closed");
707 return NULL;
708 }
709 return findmethod(drawing_methods, (object *)dp, name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000710}
711
Guido van Rossum541c8c01991-05-05 20:13:41 +0000712typeobject Drawingtype = {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000713 OB_HEAD_INIT(&Typetype)
714 0, /*ob_size*/
715 "drawing", /*tp_name*/
716 sizeof(drawingobject), /*tp_size*/
717 0, /*tp_itemsize*/
718 /* methods */
719 drawing_dealloc, /*tp_dealloc*/
720 0, /*tp_print*/
721 drawing_getattr, /*tp_getattr*/
722 0, /*tp_setattr*/
723 0, /*tp_compare*/
724 0, /*tp_repr*/
725};
726
727
728/* Text(edit) objects */
729
730typedef struct {
731 OB_HEAD
732 TEXTEDIT *t_text;
733 windowobject *t_ref;
734 object *t_attr; /* Attributes dictionary */
735} textobject;
736
737extern typeobject Texttype; /* Really static, forward */
738
739static textobject *
740newtextobject(wp, left, top, right, bottom)
741 windowobject *wp;
742 int left, top, right, bottom;
743{
744 textobject *tp;
745 tp = NEWOBJ(textobject, &Texttype);
746 if (tp == NULL)
747 return NULL;
748 tp->t_attr = NULL;
749 INCREF(wp);
750 tp->t_ref = wp;
751 tp->t_text = tecreate(wp->w_win, left, top, right, bottom);
752 if (tp->t_text == NULL) {
753 DECREF(tp);
754 return (textobject *) err_nomem();
755 }
756 return tp;
757}
758
759/* Text(edit) methods */
760
761static void
762text_dealloc(tp)
763 textobject *tp;
764{
765 if (tp->t_text != NULL)
766 tefree(tp->t_text);
Guido van Rossum3c284741991-11-27 14:54:54 +0000767 XDECREF(tp->t_attr);
768 XDECREF(tp->t_ref);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000769 DEL(tp);
770}
771
772static object *
Guido van Rossum3c284741991-11-27 14:54:54 +0000773text_close(tp, args)
774 textobject *tp;
775 object *args;
776{
777 if (tp->t_text != NULL) {
778 tefree(tp->t_text);
779 tp->t_text = NULL;
780 }
781 if (tp->t_attr != NULL) {
782 DECREF(tp->t_attr);
783 tp->t_attr = NULL;
784 }
785 if (tp->t_ref != NULL) {
786 DECREF(tp->t_ref);
787 tp->t_ref = NULL;
788 }
789 INCREF(None);
790 return None;
791}
792
793static object *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000794text_arrow(self, args)
795 textobject *self;
796 object *args;
797{
798 int code;
799 if (!getintarg(args, &code))
800 return NULL;
801 tearrow(self->t_text, code);
802 INCREF(None);
803 return None;
804}
805
806static object *
807text_draw(self, args)
808 textobject *self;
809 object *args;
810{
811 register TEXTEDIT *tp = self->t_text;
812 int a[4];
813 int left, top, right, bottom;
814 if (!getrectarg(args, a))
815 return NULL;
816 if (Drawing != NULL) {
Guido van Rossum87e7ea71991-12-10 14:00:03 +0000817 err_setstr(StdwinError, "already drawing");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000818 return NULL;
819 }
820 /* Clip to text area and ignore if area is empty */
821 left = tegetleft(tp);
822 top = tegettop(tp);
823 right = tegetright(tp);
824 bottom = tegetbottom(tp);
825 if (a[0] < left) a[0] = left;
826 if (a[1] < top) a[1] = top;
827 if (a[2] > right) a[2] = right;
828 if (a[3] > bottom) a[3] = bottom;
829 if (a[0] < a[2] && a[1] < a[3]) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000830 wbegindrawing(self->t_ref->w_win);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000831 tedrawnew(tp, a[0], a[1], a[2], a[3]);
832 wenddrawing(self->t_ref->w_win);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000833 }
834 INCREF(None);
835 return None;
836}
837
838static object *
839text_event(self, args)
840 textobject *self;
841 object *args;
842{
843 register TEXTEDIT *tp = self->t_text;
844 EVENT e;
845 if (!geteventarg(args, &e))
846 return NULL;
847 if (e.type == WE_MOUSE_DOWN) {
Guido van Rossum33f17701991-02-13 23:19:39 +0000848 /* Cheat at the margins */
849 int width, height;
850 wgetdocsize(e.window, &width, &height);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000851 if (e.u.where.h < 0 && tegetleft(tp) == 0)
852 e.u.where.h = 0;
Guido van Rossum33f17701991-02-13 23:19:39 +0000853 else if (e.u.where.h > width && tegetright(tp) == width)
854 e.u.where.h = width;
855 if (e.u.where.v < 0 && tegettop(tp) == 0)
856 e.u.where.v = 0;
857 else if (e.u.where.v > height && tegetright(tp) == height)
858 e.u.where.v = height;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000859 }
860 return newintobject((long) teevent(tp, &e));
861}
862
863static object *
864text_getfocus(self, args)
865 textobject *self;
866 object *args;
867{
868 if (!getnoarg(args))
869 return NULL;
870 return makepoint(tegetfoc1(self->t_text), tegetfoc2(self->t_text));
871}
872
873static object *
874text_getfocustext(self, args)
875 textobject *self;
876 object *args;
877{
878 int f1, f2;
879 char *text;
880 if (!getnoarg(args))
881 return NULL;
882 f1 = tegetfoc1(self->t_text);
883 f2 = tegetfoc2(self->t_text);
884 text = tegettext(self->t_text);
885 return newsizedstringobject(text + f1, f2-f1);
886}
887
888static object *
889text_getrect(self, args)
890 textobject *self;
891 object *args;
892{
893 if (!getnoarg(args))
894 return NULL;
895 return makerect(tegetleft(self->t_text),
896 tegettop(self->t_text),
897 tegetright(self->t_text),
898 tegetbottom(self->t_text));
899}
900
901static object *
902text_gettext(self, args)
903 textobject *self;
904 object *args;
905{
906 if (!getnoarg(args))
907 return NULL;
908 return newsizedstringobject(tegettext(self->t_text),
909 tegetlen(self->t_text));
910}
911
912static object *
913text_move(self, args)
914 textobject *self;
915 object *args;
916{
917 int a[4];
918 if (!getrectarg(args, a))
919 return NULL;
920 temovenew(self->t_text, a[0], a[1], a[2], a[3]);
921 INCREF(None);
922 return None;
923}
924
925static object *
Guido van Rossum4b9cf8e1991-05-28 21:57:04 +0000926text_replace(self, args)
927 textobject *self;
928 object *args;
929{
Guido van Rossumfc58e581992-01-27 16:45:55 +0000930 char *text;
Guido van Rossum4b9cf8e1991-05-28 21:57:04 +0000931 if (!getstrarg(args, &text))
932 return NULL;
Guido van Rossumfc58e581992-01-27 16:45:55 +0000933 tereplace(self->t_text, text);
Guido van Rossum4b9cf8e1991-05-28 21:57:04 +0000934 INCREF(None);
935 return None;
936}
937
938static object *
939text_setactive(self, args)
940 textobject *self;
941 object *args;
942{
943 int flag;
944 if (!getintarg(args, &flag))
945 return NULL;
946 tesetactive(self->t_text, flag);
947 INCREF(None);
948 return None;
949}
950
951static object *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000952text_setfocus(self, args)
953 textobject *self;
954 object *args;
955{
956 int a[2];
957 if (!getpointarg(args, a))
958 return NULL;
959 tesetfocus(self->t_text, a[0], a[1]);
960 INCREF(None);
961 return None;
962}
963
964static object *
Guido van Rossum541c8c01991-05-05 20:13:41 +0000965text_settext(self, args)
966 textobject *self;
967 object *args;
968{
Guido van Rossumfc58e581992-01-27 16:45:55 +0000969 char *text;
Guido van Rossum541c8c01991-05-05 20:13:41 +0000970 char *buf;
971 int size;
Guido van Rossumfc58e581992-01-27 16:45:55 +0000972 if (!getargs(args, "s#", &text, &size))
Guido van Rossum541c8c01991-05-05 20:13:41 +0000973 return NULL;
Guido van Rossum541c8c01991-05-05 20:13:41 +0000974 if ((buf = NEW(char, size)) == NULL) {
Guido van Rossum87e7ea71991-12-10 14:00:03 +0000975 return err_nomem();
Guido van Rossum541c8c01991-05-05 20:13:41 +0000976 }
Guido van Rossumfc58e581992-01-27 16:45:55 +0000977 memcpy(buf, text, size);
Guido van Rossum541c8c01991-05-05 20:13:41 +0000978 tesetbuf(self->t_text, buf, size); /* Becomes owner of buffer */
979 INCREF(None);
980 return None;
981}
982
983static object *
Guido van Rossum4b9cf8e1991-05-28 21:57:04 +0000984text_setview(self, args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000985 textobject *self;
986 object *args;
987{
Guido van Rossum4b9cf8e1991-05-28 21:57:04 +0000988 int a[4];
989 if (args == None)
990 tenoview(self->t_text);
991 else {
992 if (!getrectarg(args, a))
993 return NULL;
994 tesetview(self->t_text, a[0], a[1], a[2], a[3]);
995 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000996 INCREF(None);
997 return None;
998}
999
1000static struct methodlist text_methods[] = {
Guido van Rossum3c284741991-11-27 14:54:54 +00001001 {"arrow", text_arrow},
1002 {"close", text_close},
1003 {"draw", text_draw},
1004 {"event", text_event},
1005 {"getfocus", text_getfocus},
Guido van Rossum77b46041992-01-14 18:41:24 +00001006 {"getfocustext",text_getfocustext},
Guido van Rossum3c284741991-11-27 14:54:54 +00001007 {"getrect", text_getrect},
1008 {"gettext", text_gettext},
Guido van Rossum77b46041992-01-14 18:41:24 +00001009 {"move", text_move},
Guido van Rossum3c284741991-11-27 14:54:54 +00001010 {"replace", text_replace},
1011 {"setactive", text_setactive},
1012 {"setfocus", text_setfocus},
1013 {"settext", text_settext},
1014 {"setview", text_setview},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001015 {NULL, NULL} /* sentinel */
1016};
1017
1018static object *
1019text_getattr(tp, name)
1020 textobject *tp;
1021 char *name;
1022{
Guido van Rossum85f50761991-10-20 20:22:50 +00001023 object *v = NULL;
Guido van Rossum77b46041992-01-14 18:41:24 +00001024 if (tp->t_ref == NULL) {
1025 err_setstr(StdwinError, "text object already closed");
1026 return NULL;
1027 }
Guido van Rossum85f50761991-10-20 20:22:50 +00001028 if (strcmp(name, "__dict__") == 0) {
1029 v = tp->t_attr;
1030 if (v == NULL)
1031 v = None;
1032 }
1033 else if (tp->t_attr != NULL) {
1034 v = dictlookup(tp->t_attr, name);
1035 }
1036 if (v != NULL) {
1037 INCREF(v);
1038 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001039 }
1040 return findmethod(text_methods, (object *)tp, name);
1041}
1042
1043static int
1044text_setattr(tp, name, v)
1045 textobject *tp;
1046 char *name;
1047 object *v;
1048{
1049 if (tp->t_attr == NULL) {
1050 tp->t_attr = newdictobject();
1051 if (tp->t_attr == NULL)
1052 return -1;
1053 }
1054 if (v == NULL)
1055 return dictremove(tp->t_attr, name);
1056 else
1057 return dictinsert(tp->t_attr, name, v);
1058}
1059
Guido van Rossum541c8c01991-05-05 20:13:41 +00001060typeobject Texttype = {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001061 OB_HEAD_INIT(&Typetype)
1062 0, /*ob_size*/
1063 "textedit", /*tp_name*/
1064 sizeof(textobject), /*tp_size*/
1065 0, /*tp_itemsize*/
1066 /* methods */
1067 text_dealloc, /*tp_dealloc*/
1068 0, /*tp_print*/
1069 text_getattr, /*tp_getattr*/
1070 text_setattr, /*tp_setattr*/
1071 0, /*tp_compare*/
1072 0, /*tp_repr*/
1073};
1074
1075
1076/* Menu objects */
1077
Guido van Rossum2d14e211991-02-19 12:26:49 +00001078#define IDOFFSET 10 /* Menu IDs we use start here */
Guido van Rossum27201061991-04-16 08:43:03 +00001079#define MAXNMENU 200 /* Max #menus we allow */
Guido van Rossum2d14e211991-02-19 12:26:49 +00001080static menuobject *menulist[MAXNMENU];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001081
Guido van Rossumfc58e581992-01-27 16:45:55 +00001082static menuobject *newmenuobject PROTO((char *));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001083static menuobject *
1084newmenuobject(title)
Guido van Rossumfc58e581992-01-27 16:45:55 +00001085 char *title;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001086{
1087 int id;
1088 MENU *menu;
1089 menuobject *mp;
Guido van Rossum2d14e211991-02-19 12:26:49 +00001090 for (id = 0; id < MAXNMENU; id++) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001091 if (menulist[id] == NULL)
1092 break;
1093 }
Guido van Rossum27201061991-04-16 08:43:03 +00001094 if (id >= MAXNMENU) {
Guido van Rossum87e7ea71991-12-10 14:00:03 +00001095 err_setstr(StdwinError, "creating too many menus");
Guido van Rossum27201061991-04-16 08:43:03 +00001096 return NULL;
1097 }
Guido van Rossumfc58e581992-01-27 16:45:55 +00001098 menu = wmenucreate(id + IDOFFSET, title);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001099 if (menu == NULL)
1100 return (menuobject *) err_nomem();
1101 mp = NEWOBJ(menuobject, &Menutype);
1102 if (mp != NULL) {
1103 mp->m_menu = menu;
Guido van Rossum2d14e211991-02-19 12:26:49 +00001104 mp->m_id = id + IDOFFSET;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001105 mp->m_attr = NULL;
1106 menulist[id] = mp;
1107 }
1108 else
1109 wmenudelete(menu);
1110 return mp;
1111}
1112
1113/* Menu methods */
1114
1115static void
1116menu_dealloc(mp)
1117 menuobject *mp;
1118{
1119
Guido van Rossum2d14e211991-02-19 12:26:49 +00001120 int id = mp->m_id - IDOFFSET;
1121 if (id >= 0 && id < MAXNMENU && menulist[id] == mp) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001122 menulist[id] = NULL;
1123 }
Guido van Rossum77b46041992-01-14 18:41:24 +00001124 if (mp->m_menu != NULL)
1125 wmenudelete(mp->m_menu);
1126 XDECREF(mp->m_attr);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001127 DEL(mp);
1128}
1129
1130static object *
Guido van Rossum77b46041992-01-14 18:41:24 +00001131menu_close(mp, args)
1132 menuobject *mp;
1133 object *args;
1134{
1135 int id = mp->m_id - IDOFFSET;
1136 if (id >= 0 && id < MAXNMENU && menulist[id] == mp) {
1137 menulist[id] = NULL;
1138 }
1139 mp->m_id = -1;
1140 if (mp->m_menu != NULL)
1141 wmenudelete(mp->m_menu);
1142 mp->m_menu = NULL;
1143 XDECREF(mp->m_attr);
1144 mp->m_attr = NULL;
1145 INCREF(None);
1146 return None;
1147}
1148
1149static object *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001150menu_additem(self, args)
1151 menuobject *self;
1152 object *args;
1153{
Guido van Rossumfc58e581992-01-27 16:45:55 +00001154 char *text;
1155 int shortcut = -1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001156 if (is_tupleobject(args)) {
Guido van Rossumfc58e581992-01-27 16:45:55 +00001157 char c;
1158 if (!getargs(args, "(sc)", &text, &c))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001159 return NULL;
Guido van Rossumfc58e581992-01-27 16:45:55 +00001160 shortcut = c;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001161 }
Guido van Rossumfc58e581992-01-27 16:45:55 +00001162 else if (!getstrarg(args, &text))
1163 return NULL;
1164 wmenuadditem(self->m_menu, text, shortcut);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001165 INCREF(None);
1166 return None;
1167}
1168
1169static object *
1170menu_setitem(self, args)
1171 menuobject *self;
1172 object *args;
1173{
1174 int index;
Guido van Rossumfc58e581992-01-27 16:45:55 +00001175 char *text;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001176 if (!getintstrarg(args, &index, &text))
1177 return NULL;
Guido van Rossumfc58e581992-01-27 16:45:55 +00001178 wmenusetitem(self->m_menu, index, text);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001179 INCREF(None);
1180 return None;
1181}
1182
1183static object *
1184menu_enable(self, args)
1185 menuobject *self;
1186 object *args;
1187{
1188 int index;
1189 int flag;
1190 if (!getintintarg(args, &index, &flag))
1191 return NULL;
1192 wmenuenable(self->m_menu, index, flag);
1193 INCREF(None);
1194 return None;
1195}
1196
1197static object *
1198menu_check(self, args)
1199 menuobject *self;
1200 object *args;
1201{
1202 int index;
1203 int flag;
1204 if (!getintintarg(args, &index, &flag))
1205 return NULL;
1206 wmenucheck(self->m_menu, index, flag);
1207 INCREF(None);
1208 return None;
1209}
1210
1211static struct methodlist menu_methods[] = {
Guido van Rossum3c284741991-11-27 14:54:54 +00001212 {"additem", menu_additem},
1213 {"setitem", menu_setitem},
1214 {"enable", menu_enable},
1215 {"check", menu_check},
Guido van Rossum77b46041992-01-14 18:41:24 +00001216 {"close", menu_close},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001217 {NULL, NULL} /* sentinel */
1218};
1219
1220static object *
1221menu_getattr(mp, name)
1222 menuobject *mp;
1223 char *name;
1224{
Guido van Rossum85f50761991-10-20 20:22:50 +00001225 object *v = NULL;
Guido van Rossum77b46041992-01-14 18:41:24 +00001226 if (mp->m_menu == NULL) {
1227 err_setstr(StdwinError, "menu object already closed");
1228 return NULL;
1229 }
Guido van Rossum85f50761991-10-20 20:22:50 +00001230 if (strcmp(name, "__dict__") == 0) {
1231 v = mp->m_attr;
1232 if (v == NULL)
1233 v = None;
1234 }
1235 else if (mp->m_attr != NULL) {
1236 v = dictlookup(mp->m_attr, name);
1237 }
1238 if (v != NULL) {
1239 INCREF(v);
1240 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001241 }
1242 return findmethod(menu_methods, (object *)mp, name);
1243}
1244
1245static int
1246menu_setattr(mp, name, v)
1247 menuobject *mp;
1248 char *name;
1249 object *v;
1250{
1251 if (mp->m_attr == NULL) {
1252 mp->m_attr = newdictobject();
1253 if (mp->m_attr == NULL)
1254 return -1;
1255 }
1256 if (v == NULL)
1257 return dictremove(mp->m_attr, name);
1258 else
1259 return dictinsert(mp->m_attr, name, v);
1260}
1261
Guido van Rossum541c8c01991-05-05 20:13:41 +00001262typeobject Menutype = {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001263 OB_HEAD_INIT(&Typetype)
1264 0, /*ob_size*/
1265 "menu", /*tp_name*/
1266 sizeof(menuobject), /*tp_size*/
1267 0, /*tp_itemsize*/
1268 /* methods */
1269 menu_dealloc, /*tp_dealloc*/
1270 0, /*tp_print*/
1271 menu_getattr, /*tp_getattr*/
1272 menu_setattr, /*tp_setattr*/
1273 0, /*tp_compare*/
1274 0, /*tp_repr*/
1275};
1276
1277
1278/* Windows */
1279
1280#define MAXNWIN 50
1281static windowobject *windowlist[MAXNWIN];
1282
1283/* Window methods */
1284
1285static void
1286window_dealloc(wp)
1287 windowobject *wp;
1288{
1289 if (wp->w_win != NULL) {
1290 int tag = wgettag(wp->w_win);
1291 if (tag >= 0 && tag < MAXNWIN)
1292 windowlist[tag] = NULL;
1293 else
1294 fprintf(stderr, "XXX help! tag %d in window_dealloc\n",
1295 tag);
1296 wclose(wp->w_win);
1297 }
1298 DECREF(wp->w_title);
1299 if (wp->w_attr != NULL)
1300 DECREF(wp->w_attr);
1301 free((char *)wp);
1302}
1303
Guido van Rossumd783a461991-06-07 22:35:42 +00001304static int
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001305window_print(wp, fp, flags)
1306 windowobject *wp;
1307 FILE *fp;
1308 int flags;
1309{
Guido van Rossum3c284741991-11-27 14:54:54 +00001310 fprintf(fp, "<%s window titled '%s'>",
1311 wp->w_win == NULL ? "closed" : "open",
1312 getstringvalue(wp->w_title));
Guido van Rossumd783a461991-06-07 22:35:42 +00001313 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001314}
1315
1316static object *
Guido van Rossum3c284741991-11-27 14:54:54 +00001317window_close(wp, args)
1318 windowobject *wp;
1319 object *args;
1320{
1321 if (wp->w_win != NULL) {
1322 int tag = wgettag(wp->w_win);
1323 if (tag >= 0 && tag < MAXNWIN)
1324 windowlist[tag] = NULL;
1325 wclose(wp->w_win);
1326 wp->w_win = NULL;
1327 }
1328 INCREF(None);
1329 return None;
1330}
1331
1332static object *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001333window_begindrawing(wp, args)
1334 windowobject *wp;
1335 object *args;
1336{
1337 drawingobject *dp;
1338 if (!getnoarg(args))
1339 return NULL;
1340 if (Drawing != NULL) {
Guido van Rossum87e7ea71991-12-10 14:00:03 +00001341 err_setstr(StdwinError, "already drawing");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001342 return NULL;
1343 }
1344 dp = NEWOBJ(drawingobject, &Drawingtype);
1345 if (dp == NULL)
1346 return NULL;
1347 Drawing = dp;
1348 INCREF(wp);
1349 dp->d_ref = wp;
1350 wbegindrawing(wp->w_win);
1351 return (object *)dp;
1352}
1353
1354static object *
1355window_change(wp, args)
1356 windowobject *wp;
1357 object *args;
1358{
1359 int a[4];
1360 if (!getrectarg(args, a))
1361 return NULL;
1362 wchange(wp->w_win, a[0], a[1], a[2], a[3]);
1363 INCREF(None);
1364 return None;
1365}
1366
1367static object *
1368window_gettitle(wp, args)
1369 windowobject *wp;
1370 object *args;
1371{
1372 if (!getnoarg(args))
1373 return NULL;
1374 INCREF(wp->w_title);
1375 return wp->w_title;
1376}
1377
1378static object *
Guido van Rossum541c8c01991-05-05 20:13:41 +00001379window_getwinpos(wp, args)
1380 windowobject *wp;
1381 object *args;
1382{
1383 int h, v;
1384 if (!getnoarg(args))
1385 return NULL;
1386 wgetwinpos(wp->w_win, &h, &v);
1387 return makepoint(h, v);
1388}
1389
1390static object *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001391window_getwinsize(wp, args)
1392 windowobject *wp;
1393 object *args;
1394{
1395 int width, height;
1396 if (!getnoarg(args))
1397 return NULL;
1398 wgetwinsize(wp->w_win, &width, &height);
1399 return makepoint(width, height);
1400}
1401
1402static object *
1403window_getdocsize(wp, args)
1404 windowobject *wp;
1405 object *args;
1406{
1407 int width, height;
1408 if (!getnoarg(args))
1409 return NULL;
1410 wgetdocsize(wp->w_win, &width, &height);
1411 return makepoint(width, height);
1412}
1413
1414static object *
1415window_getorigin(wp, args)
1416 windowobject *wp;
1417 object *args;
1418{
1419 int width, height;
1420 if (!getnoarg(args))
1421 return NULL;
1422 wgetorigin(wp->w_win, &width, &height);
1423 return makepoint(width, height);
1424}
1425
1426static object *
1427window_scroll(wp, args)
1428 windowobject *wp;
1429 object *args;
1430{
1431 int a[6];
1432 if (!getrectpointarg(args, a))
1433 return NULL;
1434 wscroll(wp->w_win, a[0], a[1], a[2], a[3], a[4], a[5]);
1435 INCREF(None);
1436 return None;
1437}
1438
1439static object *
1440window_setdocsize(wp, args)
1441 windowobject *wp;
1442 object *args;
1443{
1444 int a[2];
1445 if (!getpointarg(args, a))
1446 return NULL;
1447 wsetdocsize(wp->w_win, a[0], a[1]);
1448 INCREF(None);
1449 return None;
1450}
1451
1452static object *
1453window_setorigin(wp, args)
1454 windowobject *wp;
1455 object *args;
1456{
1457 int a[2];
1458 if (!getpointarg(args, a))
1459 return NULL;
1460 wsetorigin(wp->w_win, a[0], a[1]);
1461 INCREF(None);
1462 return None;
1463}
1464
1465static object *
1466window_settitle(wp, args)
1467 windowobject *wp;
1468 object *args;
1469{
1470 object *title;
Guido van Rossumfc58e581992-01-27 16:45:55 +00001471 if (!getStrarg(args, &title))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001472 return NULL;
1473 DECREF(wp->w_title);
1474 INCREF(title);
1475 wp->w_title = title;
1476 wsettitle(wp->w_win, getstringvalue(title));
1477 INCREF(None);
1478 return None;
1479}
1480
1481static object *
1482window_show(wp, args)
1483 windowobject *wp;
1484 object *args;
1485{
1486 int a[4];
1487 if (!getrectarg(args, a))
1488 return NULL;
1489 wshow(wp->w_win, a[0], a[1], a[2], a[3]);
1490 INCREF(None);
1491 return None;
1492}
1493
1494static object *
1495window_settimer(wp, args)
1496 windowobject *wp;
1497 object *args;
1498{
1499 int a;
1500 if (!getintarg(args, &a))
1501 return NULL;
1502 wsettimer(wp->w_win, a);
1503 INCREF(None);
1504 return None;
1505}
1506
1507static object *
1508window_menucreate(self, args)
1509 windowobject *self;
1510 object *args;
1511{
1512 menuobject *mp;
Guido van Rossumfc58e581992-01-27 16:45:55 +00001513 char *title;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001514 if (!getstrarg(args, &title))
1515 return NULL;
1516 wmenusetdeflocal(1);
1517 mp = newmenuobject(title);
1518 if (mp == NULL)
1519 return NULL;
1520 wmenuattach(self->w_win, mp->m_menu);
1521 return (object *)mp;
1522}
1523
1524static object *
1525window_textcreate(self, args)
1526 windowobject *self;
1527 object *args;
1528{
1529 textobject *tp;
1530 int a[4];
1531 if (!getrectarg(args, a))
1532 return NULL;
1533 return (object *)
1534 newtextobject(self, a[0], a[1], a[2], a[3]);
1535}
1536
Guido van Rossum5b10f451990-10-30 16:01:48 +00001537static object *
1538window_setselection(self, args)
1539 windowobject *self;
1540 object *args;
1541{
Guido van Rossumfc58e581992-01-27 16:45:55 +00001542 int sel, size, ok;
1543 char *text;
1544 if (!getargs(args, "(is#)", &sel, &text, &size))
Guido van Rossum5b10f451990-10-30 16:01:48 +00001545 return NULL;
Guido van Rossumfc58e581992-01-27 16:45:55 +00001546 ok = wsetselection(self->w_win, sel, text, size);
Guido van Rossum5b10f451990-10-30 16:01:48 +00001547 return newintobject(ok);
1548}
1549
1550static object *
1551window_setwincursor(self, args)
1552 windowobject *self;
1553 object *args;
1554{
Guido van Rossumfc58e581992-01-27 16:45:55 +00001555 char *name;
Guido van Rossum5b10f451990-10-30 16:01:48 +00001556 CURSOR *c;
Guido van Rossum3c8ba7a1992-02-05 11:15:00 +00001557 if (!getargs(args, "z", &name))
Guido van Rossum5b10f451990-10-30 16:01:48 +00001558 return NULL;
Guido van Rossum3c8ba7a1992-02-05 11:15:00 +00001559 if (name == NULL)
1560 c = NULL;
1561 else {
1562 c = wfetchcursor(name);
1563 if (c == NULL) {
1564 err_setstr(StdwinError, "no such cursor");
1565 return NULL;
1566 }
Guido van Rossum5b10f451990-10-30 16:01:48 +00001567 }
1568 wsetwincursor(self->w_win, c);
1569 INCREF(None);
1570 return None;
1571}
1572
Guido van Rossumfc58e581992-01-27 16:45:55 +00001573static object *
1574window_setactive(self, args)
1575 windowobject *self;
1576 object *args;
1577{
1578 if (!getnoarg(args))
1579 return NULL;
1580 wsetactive(self->w_win);
1581 INCREF(None);
1582 return None;
1583}
1584
Guido van Rossum8dcbbac1991-07-27 21:42:24 +00001585#ifdef CWI_HACKS
1586static object *
1587window_getxwindowid(self, args)
1588 windowobject *self;
1589 object *args;
1590{
1591 long wid = wgetxwindowid(self->w_win);
1592 return newintobject(wid);
1593}
1594#endif
1595
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001596static struct methodlist window_methods[] = {
1597 {"begindrawing",window_begindrawing},
1598 {"change", window_change},
Guido van Rossum3c284741991-11-27 14:54:54 +00001599 {"close", window_close},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001600 {"getdocsize", window_getdocsize},
1601 {"getorigin", window_getorigin},
1602 {"gettitle", window_gettitle},
Guido van Rossum541c8c01991-05-05 20:13:41 +00001603 {"getwinpos", window_getwinpos},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001604 {"getwinsize", window_getwinsize},
1605 {"menucreate", window_menucreate},
1606 {"scroll", window_scroll},
Guido van Rossumb7e51601992-01-26 18:13:18 +00001607 {"setactive", window_setactive},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001608 {"setdocsize", window_setdocsize},
1609 {"setorigin", window_setorigin},
Guido van Rossum5b10f451990-10-30 16:01:48 +00001610 {"setselection",window_setselection},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001611 {"settimer", window_settimer},
1612 {"settitle", window_settitle},
Guido van Rossum27201061991-04-16 08:43:03 +00001613 {"setwincursor",window_setwincursor},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001614 {"show", window_show},
1615 {"textcreate", window_textcreate},
Guido van Rossum8dcbbac1991-07-27 21:42:24 +00001616#ifdef CWI_HACKS
1617 {"getxwindowid",window_getxwindowid},
1618#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001619 {NULL, NULL} /* sentinel */
1620};
1621
1622static object *
1623window_getattr(wp, name)
1624 windowobject *wp;
1625 char *name;
1626{
Guido van Rossum85f50761991-10-20 20:22:50 +00001627 object *v = NULL;
Guido van Rossum77b46041992-01-14 18:41:24 +00001628 if (wp->w_win == NULL) {
1629 err_setstr(StdwinError, "window already closed");
1630 return NULL;
1631 }
Guido van Rossum85f50761991-10-20 20:22:50 +00001632 if (strcmp(name, "__dict__") == 0) {
1633 v = wp->w_attr;
1634 if (v == NULL)
1635 v = None;
1636 }
1637 else if (wp->w_attr != NULL) {
1638 v = dictlookup(wp->w_attr, name);
1639 }
1640 if (v != NULL) {
1641 INCREF(v);
1642 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001643 }
1644 return findmethod(window_methods, (object *)wp, name);
1645}
1646
1647static int
1648window_setattr(wp, name, v)
1649 windowobject *wp;
1650 char *name;
1651 object *v;
1652{
1653 if (wp->w_attr == NULL) {
1654 wp->w_attr = newdictobject();
1655 if (wp->w_attr == NULL)
1656 return -1;
1657 }
1658 if (v == NULL)
1659 return dictremove(wp->w_attr, name);
1660 else
1661 return dictinsert(wp->w_attr, name, v);
1662}
1663
Guido van Rossum541c8c01991-05-05 20:13:41 +00001664typeobject Windowtype = {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001665 OB_HEAD_INIT(&Typetype)
1666 0, /*ob_size*/
1667 "window", /*tp_name*/
1668 sizeof(windowobject), /*tp_size*/
1669 0, /*tp_itemsize*/
1670 /* methods */
1671 window_dealloc, /*tp_dealloc*/
1672 window_print, /*tp_print*/
1673 window_getattr, /*tp_getattr*/
1674 window_setattr, /*tp_setattr*/
1675 0, /*tp_compare*/
1676 0, /*tp_repr*/
1677};
1678
1679/* Stdwin methods */
1680
1681static object *
1682stdwin_open(sw, args)
1683 object *sw;
1684 object *args;
1685{
1686 int tag;
1687 object *title;
1688 windowobject *wp;
Guido van Rossumfc58e581992-01-27 16:45:55 +00001689 if (!getStrarg(args, &title))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001690 return NULL;
1691 for (tag = 0; tag < MAXNWIN; tag++) {
1692 if (windowlist[tag] == NULL)
1693 break;
1694 }
Guido van Rossum27201061991-04-16 08:43:03 +00001695 if (tag >= MAXNWIN) {
Guido van Rossum87e7ea71991-12-10 14:00:03 +00001696 err_setstr(StdwinError, "creating too many windows");
Guido van Rossum27201061991-04-16 08:43:03 +00001697 return NULL;
1698 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001699 wp = NEWOBJ(windowobject, &Windowtype);
1700 if (wp == NULL)
1701 return NULL;
1702 INCREF(title);
1703 wp->w_title = title;
1704 wp->w_win = wopen(getstringvalue(title), (void (*)()) NULL);
1705 wp->w_attr = NULL;
1706 if (wp->w_win == NULL) {
1707 DECREF(wp);
1708 return NULL;
1709 }
1710 windowlist[tag] = wp;
1711 wsettag(wp->w_win, tag);
1712 return (object *)wp;
1713}
1714
1715static object *
Guido van Rossum246b9d81991-06-03 10:55:14 +00001716window2object(win)
1717 WINDOW *win;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001718{
Guido van Rossum246b9d81991-06-03 10:55:14 +00001719 object *w;
1720 if (win == NULL)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001721 w = None;
1722 else {
Guido van Rossum246b9d81991-06-03 10:55:14 +00001723 int tag = wgettag(win);
1724 if (tag < 0 || tag >= MAXNWIN || windowlist[tag] == NULL ||
1725 windowlist[tag]->w_win != win)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001726 w = None;
1727 else
1728 w = (object *)windowlist[tag];
1729 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001730 INCREF(w);
Guido van Rossum246b9d81991-06-03 10:55:14 +00001731 return w;
1732}
1733
1734static object *
1735stdwin_get_poll_event(poll, args)
1736 int poll;
1737 object *args;
1738{
1739 EVENT e;
Guido van Rossum2ee12f41992-04-13 15:54:35 +00001740 object *u, *v, *w;
Guido van Rossum246b9d81991-06-03 10:55:14 +00001741 if (!getnoarg(args))
1742 return NULL;
1743 if (Drawing != NULL) {
Guido van Rossum87e7ea71991-12-10 14:00:03 +00001744 err_setstr(StdwinError, "cannot getevent() while drawing");
Guido van Rossum246b9d81991-06-03 10:55:14 +00001745 return NULL;
1746 }
1747 again:
Guido van Rossumff4949e1992-08-05 19:58:53 +00001748 BGN_STDWIN
Guido van Rossum246b9d81991-06-03 10:55:14 +00001749 if (poll) {
1750 if (!wpollevent(&e)) {
Guido van Rossumff4949e1992-08-05 19:58:53 +00001751 RET_STDWIN
Guido van Rossum246b9d81991-06-03 10:55:14 +00001752 INCREF(None);
1753 return None;
1754 }
1755 }
1756 else
1757 wgetevent(&e);
Guido van Rossumff4949e1992-08-05 19:58:53 +00001758 END_STDWIN
Guido van Rossum246b9d81991-06-03 10:55:14 +00001759 if (e.type == WE_COMMAND && e.u.command == WC_CANCEL) {
1760 /* Turn keyboard interrupts into exceptions */
1761 err_set(KeyboardInterrupt);
1762 return NULL;
1763 }
1764 if (e.type == WE_COMMAND && e.u.command == WC_CLOSE) {
1765 /* Turn WC_CLOSE commands into WE_CLOSE events */
1766 e.type = WE_CLOSE;
1767 }
Guido van Rossum2ee12f41992-04-13 15:54:35 +00001768 v = window2object(e.window);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001769 switch (e.type) {
1770 case WE_CHAR:
1771 {
1772 char c[1];
1773 c[0] = e.u.character;
1774 w = newsizedstringobject(c, 1);
1775 }
1776 break;
1777 case WE_COMMAND:
1778 w = newintobject((long)e.u.command);
1779 break;
1780 case WE_DRAW:
1781 w = makerect(e.u.area.left, e.u.area.top,
1782 e.u.area.right, e.u.area.bottom);
1783 break;
1784 case WE_MOUSE_DOWN:
1785 case WE_MOUSE_MOVE:
1786 case WE_MOUSE_UP:
Guido van Rossum2ee12f41992-04-13 15:54:35 +00001787 w = mkvalue("((ii)iii)",
1788 e.u.where.h, e.u.where.v,
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001789 e.u.where.clicks,
1790 e.u.where.button,
1791 e.u.where.mask);
1792 break;
1793 case WE_MENU:
Guido van Rossum2d14e211991-02-19 12:26:49 +00001794 if (e.u.m.id >= IDOFFSET && e.u.m.id < IDOFFSET+MAXNMENU &&
1795 menulist[e.u.m.id - IDOFFSET] != NULL)
Guido van Rossum2ee12f41992-04-13 15:54:35 +00001796 w = mkvalue("(Oi)",
1797 menulist[e.u.m.id - IDOFFSET], e.u.m.item);
Guido van Rossum246b9d81991-06-03 10:55:14 +00001798 else {
1799 /* Ghost menu event.
1800 Can occur only on the Mac if another part
1801 of the aplication has installed a menu;
1802 like the THINK C console library. */
1803 DECREF(v);
1804 goto again;
1805 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001806 break;
Guido van Rossum3ee199e1992-06-30 12:48:26 +00001807 case WE_KEY:
1808 w = mkvalue("(ii)", e.u.key.code, e.u.key.mask);
1809 break;
Guido van Rossum5b10f451990-10-30 16:01:48 +00001810 case WE_LOST_SEL:
1811 w = newintobject((long)e.u.sel);
1812 break;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001813 default:
1814 w = None;
1815 INCREF(w);
1816 break;
1817 }
1818 if (w == NULL) {
1819 DECREF(v);
1820 return NULL;
1821 }
Guido van Rossum2ee12f41992-04-13 15:54:35 +00001822 u = mkvalue("(iOO)", e.type, v, w);
1823 XDECREF(v);
1824 XDECREF(w);
1825 return u;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001826}
1827
1828static object *
Guido van Rossume8e7cf41991-01-16 14:06:18 +00001829stdwin_getevent(sw, args)
1830 object *sw;
1831 object *args;
1832{
1833 return stdwin_get_poll_event(0, args);
1834}
1835
1836static object *
1837stdwin_pollevent(sw, args)
1838 object *sw;
1839 object *args;
1840{
1841 return stdwin_get_poll_event(1, args);
1842}
1843
1844static object *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001845stdwin_setdefwinpos(sw, args)
1846 object *sw;
1847 object *args;
1848{
1849 int a[2];
1850 if (!getpointarg(args, a))
1851 return NULL;
1852 wsetdefwinpos(a[0], a[1]);
1853 INCREF(None);
1854 return None;
1855}
1856
1857static object *
1858stdwin_setdefwinsize(sw, args)
1859 object *sw;
1860 object *args;
1861{
1862 int a[2];
1863 if (!getpointarg(args, a))
1864 return NULL;
1865 wsetdefwinsize(a[0], a[1]);
1866 INCREF(None);
1867 return None;
1868}
1869
1870static object *
Guido van Rossum0c2290b1991-04-03 19:12:14 +00001871stdwin_setdefscrollbars(sw, args)
1872 object *sw;
1873 object *args;
1874{
1875 int a[2];
1876 if (!getpointarg(args, a))
1877 return NULL;
1878 wsetdefscrollbars(a[0], a[1]);
1879 INCREF(None);
1880 return None;
1881}
1882
1883static object *
Guido van Rossum541c8c01991-05-05 20:13:41 +00001884stdwin_getdefwinpos(self, args)
1885 object *self;
Guido van Rossum33f17701991-02-13 23:19:39 +00001886 object *args;
1887{
1888 int h, v;
1889 if (!getnoarg(args))
1890 return NULL;
1891 wgetdefwinpos(&h, &v);
1892 return makepoint(h, v);
1893}
1894
1895static object *
Guido van Rossum541c8c01991-05-05 20:13:41 +00001896stdwin_getdefwinsize(self, args)
1897 object *self;
Guido van Rossum33f17701991-02-13 23:19:39 +00001898 object *args;
1899{
1900 int width, height;
1901 if (!getnoarg(args))
1902 return NULL;
1903 wgetdefwinsize(&width, &height);
1904 return makepoint(width, height);
1905}
1906
1907static object *
Guido van Rossum541c8c01991-05-05 20:13:41 +00001908stdwin_getdefscrollbars(self, args)
1909 object *self;
Guido van Rossum0c2290b1991-04-03 19:12:14 +00001910 object *args;
1911{
1912 int h, v;
1913 if (!getnoarg(args))
1914 return NULL;
1915 wgetdefscrollbars(&h, &v);
1916 return makepoint(h, v);
1917}
1918
1919static object *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001920stdwin_menucreate(self, args)
1921 object *self;
1922 object *args;
1923{
Guido van Rossumfc58e581992-01-27 16:45:55 +00001924 char *title;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001925 if (!getstrarg(args, &title))
1926 return NULL;
1927 wmenusetdeflocal(0);
1928 return (object *)newmenuobject(title);
1929}
1930
1931static object *
1932stdwin_askfile(self, args)
1933 object *self;
1934 object *args;
1935{
Guido van Rossumfc58e581992-01-27 16:45:55 +00001936 char *prompt, *dflt;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001937 int new, ret;
1938 char buf[256];
1939 if (!getstrstrintarg(args, &prompt, &dflt, &new))
1940 return NULL;
Guido van Rossumfc58e581992-01-27 16:45:55 +00001941 strncpy(buf, dflt, sizeof buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001942 buf[sizeof buf - 1] = '\0';
Guido van Rossumff4949e1992-08-05 19:58:53 +00001943 BGN_STDWIN
Guido van Rossumfc58e581992-01-27 16:45:55 +00001944 ret = waskfile(prompt, buf, sizeof buf, new);
Guido van Rossumff4949e1992-08-05 19:58:53 +00001945 END_STDWIN
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001946 if (!ret) {
1947 err_set(KeyboardInterrupt);
1948 return NULL;
1949 }
1950 return newstringobject(buf);
1951}
1952
1953static object *
1954stdwin_askync(self, args)
1955 object *self;
1956 object *args;
1957{
Guido van Rossumfc58e581992-01-27 16:45:55 +00001958 char *prompt;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001959 int new, ret;
1960 if (!getstrintarg(args, &prompt, &new))
1961 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001962 BGN_STDWIN
Guido van Rossumfc58e581992-01-27 16:45:55 +00001963 ret = waskync(prompt, new);
Guido van Rossumff4949e1992-08-05 19:58:53 +00001964 END_STDWIN
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001965 if (ret < 0) {
1966 err_set(KeyboardInterrupt);
1967 return NULL;
1968 }
1969 return newintobject((long)ret);
1970}
1971
1972static object *
1973stdwin_askstr(self, args)
1974 object *self;
1975 object *args;
1976{
Guido van Rossumfc58e581992-01-27 16:45:55 +00001977 char *prompt, *dflt;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001978 int ret;
1979 char buf[256];
1980 if (!getstrstrarg(args, &prompt, &dflt))
1981 return NULL;
Guido van Rossumfc58e581992-01-27 16:45:55 +00001982 strncpy(buf, dflt, sizeof buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001983 buf[sizeof buf - 1] = '\0';
Guido van Rossumff4949e1992-08-05 19:58:53 +00001984 BGN_STDWIN
Guido van Rossumfc58e581992-01-27 16:45:55 +00001985 ret = waskstr(prompt, buf, sizeof buf);
Guido van Rossumff4949e1992-08-05 19:58:53 +00001986 END_STDWIN
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001987 if (!ret) {
1988 err_set(KeyboardInterrupt);
1989 return NULL;
1990 }
1991 return newstringobject(buf);
1992}
1993
1994static object *
1995stdwin_message(self, args)
1996 object *self;
1997 object *args;
1998{
Guido van Rossumfc58e581992-01-27 16:45:55 +00001999 char *msg;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002000 if (!getstrarg(args, &msg))
2001 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002002 BGN_STDWIN
Guido van Rossumfc58e581992-01-27 16:45:55 +00002003 wmessage(msg);
Guido van Rossumff4949e1992-08-05 19:58:53 +00002004 END_STDWIN
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002005 INCREF(None);
2006 return None;
2007}
2008
2009static object *
2010stdwin_fleep(self, args)
2011 object *self;
2012 object *args;
2013{
2014 if (!getnoarg(args))
2015 return NULL;
2016 wfleep();
2017 INCREF(None);
2018 return None;
2019}
2020
2021static object *
2022stdwin_setcutbuffer(self, args)
2023 object *self;
2024 object *args;
2025{
Guido van Rossumfc58e581992-01-27 16:45:55 +00002026 int i, size;
2027 char *str;
2028 if (!getargs(args, "(is#)", &i, &str, &size))
Guido van Rossum124967c1990-11-06 15:17:35 +00002029 return NULL;
Guido van Rossumfc58e581992-01-27 16:45:55 +00002030 wsetcutbuffer(i, str, size);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002031 INCREF(None);
2032 return None;
2033}
2034
2035static object *
Guido van Rossum246b9d81991-06-03 10:55:14 +00002036stdwin_getactive(self, args)
2037 object *self;
2038 object *args;
2039{
2040 return window2object(wgetactive());
2041}
2042
2043static object *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002044stdwin_getcutbuffer(self, args)
2045 object *self;
2046 object *args;
2047{
Guido van Rossum5b10f451990-10-30 16:01:48 +00002048 int i;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002049 char *str;
Guido van Rossum01769f01990-10-30 13:39:00 +00002050 int len;
Guido van Rossum124967c1990-11-06 15:17:35 +00002051 if (!getintarg(args, &i))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002052 return NULL;
Guido van Rossum5b10f451990-10-30 16:01:48 +00002053 str = wgetcutbuffer(i, &len);
Guido van Rossum01769f01990-10-30 13:39:00 +00002054 if (str == NULL) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002055 str = "";
Guido van Rossum01769f01990-10-30 13:39:00 +00002056 len = 0;
2057 }
2058 return newsizedstringobject(str, len);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002059}
2060
Guido van Rossum5b10f451990-10-30 16:01:48 +00002061static object *
2062stdwin_rotatecutbuffers(self, args)
2063 object *self;
2064 object *args;
2065{
2066 int i;
2067 if (!getintarg(args, &i))
2068 return NULL;
2069 wrotatecutbuffers(i);
2070 INCREF(None);
2071 return None;
2072}
2073
2074static object *
2075stdwin_getselection(self, args)
2076 object *self;
2077 object *args;
2078{
2079 int sel;
2080 char *data;
2081 int len;
2082 if (!getintarg(args, &sel))
2083 return NULL;
2084 data = wgetselection(sel, &len);
2085 if (data == NULL) {
2086 data = "";
2087 len = 0;
2088 }
2089 return newsizedstringobject(data, len);
2090}
2091
2092static object *
2093stdwin_resetselection(self, args)
2094 object *self;
2095 object *args;
2096{
2097 int sel;
2098 if (!getintarg(args, &sel))
2099 return NULL;
2100 wresetselection(sel);
2101 INCREF(None);
2102 return None;
2103}
2104
Guido van Rossum0c2290b1991-04-03 19:12:14 +00002105static object *
2106stdwin_fetchcolor(self, args)
2107 object *self;
2108 object *args;
2109{
Guido van Rossumfc58e581992-01-27 16:45:55 +00002110 char *colorname;
Guido van Rossum0c2290b1991-04-03 19:12:14 +00002111 if (!getstrarg(args, &colorname))
2112 return NULL;
Guido van Rossumfc58e581992-01-27 16:45:55 +00002113 return newintobject((long)wfetchcolor(colorname));
Guido van Rossum0c2290b1991-04-03 19:12:14 +00002114}
2115
Guido van Rossum541c8c01991-05-05 20:13:41 +00002116static object *
2117stdwin_getscrsize(self, args)
2118 object *self;
2119 object *args;
2120{
2121 int width, height;
2122 if (!getnoarg(args))
2123 return NULL;
2124 wgetscrsize(&width, &height);
2125 return makepoint(width, height);
2126}
2127
2128static object *
2129stdwin_getscrmm(self, args)
2130 object *self;
2131 object *args;
2132{
2133 int width, height;
2134 if (!getnoarg(args))
2135 return NULL;
2136 wgetscrmm(&width, &height);
2137 return makepoint(width, height);
2138}
2139
Guido van Rossumed233a51992-06-23 09:07:03 +00002140#ifdef unix
2141static object *
2142stdwin_connectionnumber(self, args)
2143 object *self;
2144 object *args;
2145{
2146 if (!getnoarg(args))
2147 return NULL;
2148 return newintobject((long) wconnectionnumber());
2149}
2150#endif
2151
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002152static struct methodlist stdwin_methods[] = {
2153 {"askfile", stdwin_askfile},
2154 {"askstr", stdwin_askstr},
2155 {"askync", stdwin_askync},
Guido van Rossum27201061991-04-16 08:43:03 +00002156 {"fetchcolor", stdwin_fetchcolor},
Guido van Rossumed233a51992-06-23 09:07:03 +00002157#ifdef unix
2158 {"fileno", stdwin_connectionnumber},
2159 {"connectionnumber", stdwin_connectionnumber},
2160#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002161 {"fleep", stdwin_fleep},
Guido van Rossum246b9d81991-06-03 10:55:14 +00002162 {"getactive", stdwin_getactive},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002163 {"getcutbuffer", stdwin_getcutbuffer},
Guido van Rossum0c2290b1991-04-03 19:12:14 +00002164 {"getdefscrollbars", stdwin_getdefscrollbars},
Guido van Rossum33f17701991-02-13 23:19:39 +00002165 {"getdefwinpos", stdwin_getdefwinpos},
2166 {"getdefwinsize", stdwin_getdefwinsize},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002167 {"getevent", stdwin_getevent},
Guido van Rossum541c8c01991-05-05 20:13:41 +00002168 {"getscrmm", stdwin_getscrmm},
2169 {"getscrsize", stdwin_getscrsize},
Guido van Rossum27201061991-04-16 08:43:03 +00002170 {"getselection", stdwin_getselection},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002171 {"menucreate", stdwin_menucreate},
2172 {"message", stdwin_message},
2173 {"open", stdwin_open},
Guido van Rossume8e7cf41991-01-16 14:06:18 +00002174 {"pollevent", stdwin_pollevent},
Guido van Rossum5b10f451990-10-30 16:01:48 +00002175 {"resetselection", stdwin_resetselection},
2176 {"rotatecutbuffers", stdwin_rotatecutbuffers},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002177 {"setcutbuffer", stdwin_setcutbuffer},
Guido van Rossum0c2290b1991-04-03 19:12:14 +00002178 {"setdefscrollbars", stdwin_setdefscrollbars},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002179 {"setdefwinpos", stdwin_setdefwinpos},
2180 {"setdefwinsize", stdwin_setdefwinsize},
2181
2182 /* Text measuring methods borrow code from drawing objects: */
2183 {"baseline", drawing_baseline},
2184 {"lineheight", drawing_lineheight},
2185 {"textbreak", drawing_textbreak},
2186 {"textwidth", drawing_textwidth},
Guido van Rossum0c2290b1991-04-03 19:12:14 +00002187
2188 /* Same for font setting methods: */
2189 {"setfont", drawing_setfont},
2190
2191 /* Same for color setting/getting methods: */
2192 {"getbgcolor", drawing_getbgcolor},
2193 {"getfgcolor", drawing_getfgcolor},
2194 {"setbgcolor", drawing_setbgcolor},
2195 {"setfgcolor", drawing_setfgcolor},
2196
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002197 {NULL, NULL} /* sentinel */
2198};
2199
2200void
2201initstdwin()
2202{
Guido van Rossumbbf94341991-12-16 15:44:53 +00002203 object *m, *d;
2204 static int inited = 0;
2205
Guido van Rossum2d14e211991-02-19 12:26:49 +00002206 if (!inited) {
2207 winit();
2208 inited = 1;
2209 }
Guido van Rossumbbf94341991-12-16 15:44:53 +00002210 m = initmodule("stdwin", stdwin_methods);
2211 d = getmoduledict(m);
2212
2213 /* Initialize stdwin.error exception */
2214 StdwinError = newstringobject("stdwin.error");
2215 if (StdwinError == NULL || dictinsert(d, "error", StdwinError) != 0)
2216 fatal("can't define stdwin.error");
Guido van Rossumff4949e1992-08-05 19:58:53 +00002217#ifdef USE_THREAD
2218 StdwinLock = allocate_lock();
2219 if (StdwinLock == NULL)
2220 fatal("can't allocate stdwin lock");
2221#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002222}