blob: 77ba6ec81d52ce523eb9f96327a813e4630c69a3 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
Guido van Rossum0b0db8e1993-01-21 16:07:51 +00002Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
3Amsterdam, The Netherlands.
Guido van Rossumf70e43a1991-02-19 12:39:46 +00004
5 All Rights Reserved
6
7Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
9provided that the above copyright notice appear in all copies and that
10both that copyright notice and this permission notice appear in
11supporting documentation, and that the names of Stichting Mathematisch
12Centrum or CWI not be used in advertising or publicity pertaining to
13distribution of the software without specific, written prior permission.
14
15STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22
23******************************************************************/
24
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000025/* Stdwin module */
26
27/* Stdwin itself is a module, not a separate object type.
28 Object types defined here:
29 wp: a window
30 dp: a drawing structure (only one can exist at a time)
31 mp: a menu
32 tp: a textedit block
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 Rossum0b0db8e1993-01-21 16:07:51 +000069#ifdef macintosh
70#include ":::src:stdwin:H:stdwin.h"
71#else /* !macintosh */
Guido van Rossum3f5da241990-12-20 15:06:42 +000072#include "stdwin.h"
Guido van Rossum0b0db8e1993-01-21 16:07:51 +000073#endif /* !macintosh */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000074
Guido van Rossumff4949e1992-08-05 19:58:53 +000075#ifdef USE_THREAD
76
77#include "thread.h"
78
79static type_lock StdwinLock; /* Lock held when interpreter not locked */
80
81#define BGN_STDWIN BGN_SAVE acquire_lock(StdwinLock, 1);
82#define RET_STDWIN release_lock(StdwinLock); RET_SAVE
83#define END_STDWIN release_lock(StdwinLock); END_SAVE
84
85#else
86
87#define BGN_STDWIN BGN_SAVE
88#define RET_STDWIN RET_SAVE
89#define END_STDWIN END_SAVE
90
91#endif
92
Guido van Rossumbbf94341991-12-16 15:44:53 +000093static object *StdwinError; /* Exception stdwin.error */
Guido van Rossum87e7ea71991-12-10 14:00:03 +000094
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000095/* Window and menu object types declared here because of forward references */
96
97typedef struct {
98 OB_HEAD
99 object *w_title;
100 WINDOW *w_win;
101 object *w_attr; /* Attributes dictionary */
102} windowobject;
103
104extern typeobject Windowtype; /* Really static, forward */
105
106#define is_windowobject(wp) ((wp)->ob_type == &Windowtype)
107
108typedef struct {
109 OB_HEAD
110 MENU *m_menu;
111 int m_id;
112 object *m_attr; /* Attributes dictionary */
113} menuobject;
114
115extern typeobject Menutype; /* Really static, forward */
116
117#define is_menuobject(mp) ((mp)->ob_type == &Menutype)
118
119
120/* Strongly stdwin-specific argument handlers */
121
122static int
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000123getmenudetail(v, ep)
124 object *v;
125 EVENT *ep;
126{
Guido van Rossumfc58e581992-01-27 16:45:55 +0000127 menuobject *mp;
128 if (!getargs(v, "(Oi)", &mp, &ep->u.m.item))
129 return 0;
130 if (!is_menuobject(mp))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000131 return err_badarg();
Guido van Rossumfc58e581992-01-27 16:45:55 +0000132 ep->u.m.id = mp->m_id;
133 return 1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000134}
135
136static int
137geteventarg(v, ep)
138 object *v;
139 EVENT *ep;
140{
141 object *wp, *detail;
142 int a[4];
Guido van Rossumfc58e581992-01-27 16:45:55 +0000143 if (!getargs(v, "(iOO)", &ep->type, &wp, &detail))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000144 return 0;
Guido van Rossumfc58e581992-01-27 16:45:55 +0000145 if (is_windowobject(wp))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000146 ep->window = ((windowobject *)wp) -> w_win;
Guido van Rossumfc58e581992-01-27 16:45:55 +0000147 else if (wp == None)
148 ep->window = NULL;
149 else
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000150 return err_badarg();
Guido van Rossumfc58e581992-01-27 16:45:55 +0000151 switch (ep->type) {
152 case WE_CHAR: {
153 char c;
154 if (!getargs(detail, "c", &c))
155 return 0;
156 ep->u.character = c;
157 return 1;
158 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000159 case WE_COMMAND:
160 return getintarg(detail, &ep->u.command);
161 case WE_DRAW:
162 if (!getrectarg(detail, a))
163 return 0;
164 ep->u.area.left = a[0];
165 ep->u.area.top = a[1];
166 ep->u.area.right = a[2];
167 ep->u.area.bottom = a[3];
168 return 1;
169 case WE_MOUSE_DOWN:
170 case WE_MOUSE_UP:
171 case WE_MOUSE_MOVE:
Guido van Rossumfc58e581992-01-27 16:45:55 +0000172 return getargs(detail, "((ii)iii)",
173 &ep->u.where.h, &ep->u.where.v,
174 &ep->u.where.clicks,
175 &ep->u.where.button,
176 &ep->u.where.mask);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000177 case WE_MENU:
178 return getmenudetail(detail, ep);
Guido van Rossum3ee199e1992-06-30 12:48:26 +0000179 case WE_KEY:
180 return getargs(detail, "(ii)",
181 &ep->u.key.code, &ep->u.key.mask);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000182 default:
183 return 1;
184 }
185}
186
187
188/* Return construction tools */
189
190static object *
191makepoint(a, b)
192 int a, b;
193{
Guido van Rossum2ee12f41992-04-13 15:54:35 +0000194 return mkvalue("(ii)", a, b);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000195}
196
197static object *
198makerect(a, b, c, d)
199 int a, b, c, d;
200{
Guido van Rossum2ee12f41992-04-13 15:54:35 +0000201 return mkvalue("((ii)(ii))", a, b, c, d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000202}
203
204
205/* Drawing objects */
206
207typedef struct {
208 OB_HEAD
209 windowobject *d_ref;
210} drawingobject;
211
212static drawingobject *Drawing; /* Set to current drawing object, or NULL */
213
214/* Drawing methods */
215
Guido van Rossum3c284741991-11-27 14:54:54 +0000216static object *
217drawing_close(dp)
218 drawingobject *dp;
219{
220 if (dp->d_ref != NULL) {
221 wenddrawing(dp->d_ref->w_win);
222 Drawing = NULL;
223 DECREF(dp->d_ref);
224 dp->d_ref = NULL;
225 }
226 INCREF(None);
227 return None;
228}
Guido van Rossum77b46041992-01-14 18:41:24 +0000229
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000230static void
231drawing_dealloc(dp)
232 drawingobject *dp;
233{
Guido van Rossum3c284741991-11-27 14:54:54 +0000234 if (dp->d_ref != NULL) {
235 wenddrawing(dp->d_ref->w_win);
236 Drawing = NULL;
237 DECREF(dp->d_ref);
238 dp->d_ref = NULL;
239 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000240 free((char *)dp);
241}
242
243static object *
244drawing_generic(dp, args, func)
245 drawingobject *dp;
246 object *args;
247 void (*func) FPROTO((int, int, int, int));
248{
249 int a[4];
250 if (!getrectarg(args, a))
251 return NULL;
252 (*func)(a[0], a[1], a[2], a[3]);
253 INCREF(None);
254 return None;
255}
256
257static object *
258drawing_line(dp, args)
259 drawingobject *dp;
260 object *args;
261{
Guido van Rossumbf109731991-03-06 13:14:12 +0000262 return drawing_generic(dp, args, wdrawline);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000263}
264
265static object *
266drawing_xorline(dp, args)
267 drawingobject *dp;
268 object *args;
269{
Guido van Rossumbf109731991-03-06 13:14:12 +0000270 return drawing_generic(dp, args, wxorline);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000271}
272
273static object *
274drawing_circle(dp, args)
275 drawingobject *dp;
276 object *args;
277{
278 int a[3];
279 if (!getpointintarg(args, a))
280 return NULL;
281 wdrawcircle(a[0], a[1], a[2]);
282 INCREF(None);
283 return None;
284}
Guido van Rossuma2a181a1991-05-14 12:09:25 +0000285
Guido van Rossum27201061991-04-16 08:43:03 +0000286static object *
287drawing_fillcircle(dp, args)
288 drawingobject *dp;
289 object *args;
290{
291 int a[3];
292 if (!getpointintarg(args, a))
293 return NULL;
294 wfillcircle(a[0], a[1], a[2]);
295 INCREF(None);
296 return None;
297}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000298
299static object *
Guido van Rossuma2a181a1991-05-14 12:09:25 +0000300drawing_xorcircle(dp, args)
301 drawingobject *dp;
302 object *args;
303{
304 int a[3];
305 if (!getpointintarg(args, a))
306 return NULL;
307 wxorcircle(a[0], a[1], a[2]);
308 INCREF(None);
309 return None;
310}
311
312static object *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000313drawing_elarc(dp, args)
314 drawingobject *dp;
315 object *args;
316{
317 int a[6];
318 if (!get3pointarg(args, a))
319 return NULL;
320 wdrawelarc(a[0], a[1], a[2], a[3], a[4], a[5]);
321 INCREF(None);
322 return None;
323}
324
325static object *
Guido van Rossum27201061991-04-16 08:43:03 +0000326drawing_fillelarc(dp, args)
327 drawingobject *dp;
328 object *args;
329{
330 int a[6];
331 if (!get3pointarg(args, a))
332 return NULL;
333 wfillelarc(a[0], a[1], a[2], a[3], a[4], a[5]);
334 INCREF(None);
335 return None;
336}
337
338static object *
Guido van Rossuma2a181a1991-05-14 12:09:25 +0000339drawing_xorelarc(dp, args)
340 drawingobject *dp;
341 object *args;
342{
343 int a[6];
344 if (!get3pointarg(args, a))
345 return NULL;
346 wxorelarc(a[0], a[1], a[2], a[3], a[4], a[5]);
347 INCREF(None);
348 return None;
349}
350
351static object *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000352drawing_box(dp, args)
353 drawingobject *dp;
354 object *args;
355{
Guido van Rossumbf109731991-03-06 13:14:12 +0000356 return drawing_generic(dp, args, wdrawbox);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000357}
358
359static object *
360drawing_erase(dp, args)
361 drawingobject *dp;
362 object *args;
363{
Guido van Rossumbf109731991-03-06 13:14:12 +0000364 return drawing_generic(dp, args, werase);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000365}
366
367static object *
368drawing_paint(dp, args)
369 drawingobject *dp;
370 object *args;
371{
Guido van Rossumbf109731991-03-06 13:14:12 +0000372 return drawing_generic(dp, args, wpaint);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000373}
374
375static object *
376drawing_invert(dp, args)
377 drawingobject *dp;
378 object *args;
379{
Guido van Rossumbf109731991-03-06 13:14:12 +0000380 return drawing_generic(dp, args, winvert);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000381}
382
Guido van Rossum27201061991-04-16 08:43:03 +0000383static POINT *
384getpointsarray(v, psize)
385 object *v;
386 int *psize;
387{
388 int n = -1;
389 object * (*getitem) PROTO((object *, int));
390 int i;
391 POINT *points;
392
393 if (v == NULL)
394 ;
395 else if (is_listobject(v)) {
396 n = getlistsize(v);
397 getitem = getlistitem;
398 }
399 else if (is_tupleobject(v)) {
400 n = gettuplesize(v);
401 getitem = gettupleitem;
402 }
403
404 if (n <= 0) {
405 (void) err_badarg();
406 return NULL;
407 }
408
409 points = NEW(POINT, n);
410 if (points == NULL) {
411 (void) err_nomem();
412 return NULL;
413 }
414
415 for (i = 0; i < n; i++) {
416 object *w = (*getitem)(v, i);
417 int a[2];
418 if (!getpointarg(w, a)) {
419 DEL(points);
420 return NULL;
421 }
422 points[i].h = a[0];
423 points[i].v = a[1];
424 }
425
426 *psize = n;
427 return points;
428}
429
430static object *
431drawing_poly(dp, args)
432 drawingobject *dp;
433 object *args;
434{
435 int n;
436 POINT *points = getpointsarray(args, &n);
437 if (points == NULL)
438 return NULL;
439 wdrawpoly(n, points);
440 DEL(points);
441 INCREF(None);
442 return None;
443}
444
445static object *
446drawing_fillpoly(dp, args)
447 drawingobject *dp;
448 object *args;
449{
450 int n;
451 POINT *points = getpointsarray(args, &n);
452 if (points == NULL)
453 return NULL;
454 wfillpoly(n, points);
455 DEL(points);
456 INCREF(None);
457 return None;
458}
459
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000460static object *
Guido van Rossuma2a181a1991-05-14 12:09:25 +0000461drawing_xorpoly(dp, args)
462 drawingobject *dp;
463 object *args;
464{
465 int n;
466 POINT *points = getpointsarray(args, &n);
467 if (points == NULL)
468 return NULL;
469 wxorpoly(n, points);
470 DEL(points);
471 INCREF(None);
472 return None;
473}
474
475static object *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000476drawing_cliprect(dp, args)
477 drawingobject *dp;
478 object *args;
479{
Guido van Rossumbf109731991-03-06 13:14:12 +0000480 return drawing_generic(dp, args, wcliprect);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000481}
482
483static object *
484drawing_noclip(dp, args)
485 drawingobject *dp;
486 object *args;
487{
488 if (!getnoarg(args))
489 return NULL;
490 wnoclip();
491 INCREF(None);
492 return None;
493}
494
495static object *
496drawing_shade(dp, args)
497 drawingobject *dp;
498 object *args;
499{
500 int a[5];
501 if (!getrectintarg(args, a))
502 return NULL;
503 wshade(a[0], a[1], a[2], a[3], a[4]);
504 INCREF(None);
505 return None;
506}
507
508static object *
509drawing_text(dp, args)
510 drawingobject *dp;
511 object *args;
512{
Guido van Rossumfc58e581992-01-27 16:45:55 +0000513 int h, v, size;
514 char *text;
515 if (!getargs(args, "((ii)s#)", &h, &v, &text, &size))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000516 return NULL;
Guido van Rossumfc58e581992-01-27 16:45:55 +0000517 wdrawtext(h, v, text, size);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000518 INCREF(None);
519 return None;
520}
521
522/* The following four are also used as stdwin functions */
523
524static object *
525drawing_lineheight(dp, args)
526 drawingobject *dp;
527 object *args;
528{
529 if (!getnoarg(args))
530 return NULL;
531 return newintobject((long)wlineheight());
532}
533
534static object *
535drawing_baseline(dp, args)
536 drawingobject *dp;
537 object *args;
538{
539 if (!getnoarg(args))
540 return NULL;
541 return newintobject((long)wbaseline());
542}
543
544static object *
545drawing_textwidth(dp, args)
546 drawingobject *dp;
547 object *args;
548{
Guido van Rossumfc58e581992-01-27 16:45:55 +0000549 char *text;
550 int size;
551 if (!getargs(args, "s#", &text, &size))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000552 return NULL;
Guido van Rossumfc58e581992-01-27 16:45:55 +0000553 return newintobject((long)wtextwidth(text, size));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000554}
555
556static object *
557drawing_textbreak(dp, args)
558 drawingobject *dp;
559 object *args;
560{
Guido van Rossumfc58e581992-01-27 16:45:55 +0000561 char *text;
562 int size, width;
563 if (!getargs(args, "(s#i)", &text, &size, &width))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000564 return NULL;
Guido van Rossumfc58e581992-01-27 16:45:55 +0000565 return newintobject((long)wtextbreak(text, size, width));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000566}
567
Guido van Rossum0c2290b1991-04-03 19:12:14 +0000568static object *
569drawing_setfont(self, args)
570 drawingobject *self;
571 object *args;
572{
Guido van Rossumfc58e581992-01-27 16:45:55 +0000573 char *font;
574 char style = '\0';
575 int size = 0;
576 if (args == NULL || !is_tupleobject(args)) {
Guido van Rossum3c8ba7a1992-02-05 11:15:00 +0000577 if (!getargs(args, "z", &font))
Guido van Rossum50429a11991-04-04 15:24:07 +0000578 return NULL;
579 }
580 else {
Guido van Rossumfc58e581992-01-27 16:45:55 +0000581 int n = gettuplesize(args);
582 if (n == 2) {
583 if (!getargs(args, "(zi)", &font, &size))
584 return NULL;
585 }
586 else if (!getargs(args, "(zic)", &font, &size, &style)) {
587 err_clear();
588 if (!getargs(args, "(zci)", &font, &style, &size))
589 return NULL;
Guido van Rossum50429a11991-04-04 15:24:07 +0000590 }
591 }
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000592 if (font != NULL) {
593 if (!wsetfont(font)) {
594 err_setstr(StdwinError, "font not found");
595 return NULL;
596 }
597 }
Guido van Rossum50429a11991-04-04 15:24:07 +0000598 if (size != 0)
599 wsetsize(size);
Guido van Rossumfc58e581992-01-27 16:45:55 +0000600 switch (style) {
601 case 'b':
602 wsetbold();
603 break;
604 case 'i':
605 wsetitalic();
606 break;
607 case 'o':
608 wsetbolditalic();
609 break;
610 case 'u':
611 wsetunderline();
612 break;
613 case 'p':
614 wsetplain();
615 break;
616 }
Guido van Rossum0c2290b1991-04-03 19:12:14 +0000617 INCREF(None);
618 return None;
619}
620
621static object *
622drawing_getbgcolor(self, args)
623 object *self;
624 object *args;
625{
626 if (!getnoarg(args))
627 return NULL;
628 return newintobject((long)wgetbgcolor());
629}
630
631static object *
632drawing_getfgcolor(self, args)
633 object *self;
634 object *args;
635{
636 if (!getnoarg(args))
637 return NULL;
638 return newintobject((long)wgetfgcolor());
639}
640
641static object *
642drawing_setbgcolor(self, args)
643 object *self;
644 object *args;
645{
646 long color;
647 if (!getlongarg(args, &color))
648 return NULL;
649 wsetbgcolor((COLOR)color);
650 INCREF(None);
651 return None;
652}
653
654static object *
655drawing_setfgcolor(self, args)
656 object *self;
657 object *args;
658{
659 long color;
660 if (!getlongarg(args, &color))
661 return NULL;
662 wsetfgcolor((COLOR)color);
663 INCREF(None);
664 return None;
665}
666
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000667static struct methodlist drawing_methods[] = {
668 {"box", drawing_box},
669 {"circle", drawing_circle},
670 {"cliprect", drawing_cliprect},
Guido van Rossum3c284741991-11-27 14:54:54 +0000671 {"close", drawing_close},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000672 {"elarc", drawing_elarc},
Guido van Rossum3c284741991-11-27 14:54:54 +0000673 {"enddrawing", drawing_close},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000674 {"erase", drawing_erase},
Guido van Rossum27201061991-04-16 08:43:03 +0000675 {"fillcircle", drawing_fillcircle},
Guido van Rossuma2a181a1991-05-14 12:09:25 +0000676 {"fillelarc", drawing_fillelarc},
Guido van Rossum27201061991-04-16 08:43:03 +0000677 {"fillpoly", drawing_fillpoly},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000678 {"invert", drawing_invert},
679 {"line", drawing_line},
680 {"noclip", drawing_noclip},
681 {"paint", drawing_paint},
Guido van Rossum27201061991-04-16 08:43:03 +0000682 {"poly", drawing_poly},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000683 {"shade", drawing_shade},
684 {"text", drawing_text},
Guido van Rossuma2a181a1991-05-14 12:09:25 +0000685 {"xorcircle", drawing_xorcircle},
686 {"xorelarc", drawing_xorelarc},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000687 {"xorline", drawing_xorline},
Guido van Rossuma2a181a1991-05-14 12:09:25 +0000688 {"xorpoly", drawing_xorpoly},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000689
690 /* Text measuring methods: */
691 {"baseline", drawing_baseline},
692 {"lineheight", drawing_lineheight},
693 {"textbreak", drawing_textbreak},
694 {"textwidth", drawing_textwidth},
Guido van Rossum0c2290b1991-04-03 19:12:14 +0000695
696 /* Font setting methods: */
697 {"setfont", drawing_setfont},
698
699 /* Color methods: */
700 {"getbgcolor", drawing_getbgcolor},
701 {"getfgcolor", drawing_getfgcolor},
702 {"setbgcolor", drawing_setbgcolor},
703 {"setfgcolor", drawing_setfgcolor},
704
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000705 {NULL, NULL} /* sentinel */
706};
707
708static object *
Guido van Rossum77b46041992-01-14 18:41:24 +0000709drawing_getattr(dp, name)
710 drawingobject *dp;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000711 char *name;
712{
Guido van Rossum77b46041992-01-14 18:41:24 +0000713 if (dp->d_ref == NULL) {
714 err_setstr(StdwinError, "drawing object already closed");
715 return NULL;
716 }
717 return findmethod(drawing_methods, (object *)dp, name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000718}
719
Guido van Rossum541c8c01991-05-05 20:13:41 +0000720typeobject Drawingtype = {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000721 OB_HEAD_INIT(&Typetype)
722 0, /*ob_size*/
723 "drawing", /*tp_name*/
724 sizeof(drawingobject), /*tp_size*/
725 0, /*tp_itemsize*/
726 /* methods */
727 drawing_dealloc, /*tp_dealloc*/
728 0, /*tp_print*/
729 drawing_getattr, /*tp_getattr*/
730 0, /*tp_setattr*/
731 0, /*tp_compare*/
732 0, /*tp_repr*/
733};
734
735
736/* Text(edit) objects */
737
738typedef struct {
739 OB_HEAD
740 TEXTEDIT *t_text;
741 windowobject *t_ref;
742 object *t_attr; /* Attributes dictionary */
743} textobject;
744
745extern typeobject Texttype; /* Really static, forward */
746
747static textobject *
748newtextobject(wp, left, top, right, bottom)
749 windowobject *wp;
750 int left, top, right, bottom;
751{
752 textobject *tp;
753 tp = NEWOBJ(textobject, &Texttype);
754 if (tp == NULL)
755 return NULL;
756 tp->t_attr = NULL;
757 INCREF(wp);
758 tp->t_ref = wp;
759 tp->t_text = tecreate(wp->w_win, left, top, right, bottom);
760 if (tp->t_text == NULL) {
761 DECREF(tp);
762 return (textobject *) err_nomem();
763 }
764 return tp;
765}
766
767/* Text(edit) methods */
768
769static void
770text_dealloc(tp)
771 textobject *tp;
772{
773 if (tp->t_text != NULL)
774 tefree(tp->t_text);
Guido van Rossum3c284741991-11-27 14:54:54 +0000775 XDECREF(tp->t_attr);
776 XDECREF(tp->t_ref);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000777 DEL(tp);
778}
779
780static object *
Guido van Rossum3c284741991-11-27 14:54:54 +0000781text_close(tp, args)
782 textobject *tp;
783 object *args;
784{
785 if (tp->t_text != NULL) {
786 tefree(tp->t_text);
787 tp->t_text = NULL;
788 }
789 if (tp->t_attr != NULL) {
790 DECREF(tp->t_attr);
791 tp->t_attr = NULL;
792 }
793 if (tp->t_ref != NULL) {
794 DECREF(tp->t_ref);
795 tp->t_ref = NULL;
796 }
797 INCREF(None);
798 return None;
799}
800
801static object *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000802text_arrow(self, args)
803 textobject *self;
804 object *args;
805{
806 int code;
807 if (!getintarg(args, &code))
808 return NULL;
809 tearrow(self->t_text, code);
810 INCREF(None);
811 return None;
812}
813
814static object *
815text_draw(self, args)
816 textobject *self;
817 object *args;
818{
819 register TEXTEDIT *tp = self->t_text;
820 int a[4];
821 int left, top, right, bottom;
822 if (!getrectarg(args, a))
823 return NULL;
824 if (Drawing != NULL) {
Guido van Rossum87e7ea71991-12-10 14:00:03 +0000825 err_setstr(StdwinError, "already drawing");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000826 return NULL;
827 }
828 /* Clip to text area and ignore if area is empty */
829 left = tegetleft(tp);
830 top = tegettop(tp);
831 right = tegetright(tp);
832 bottom = tegetbottom(tp);
833 if (a[0] < left) a[0] = left;
834 if (a[1] < top) a[1] = top;
835 if (a[2] > right) a[2] = right;
836 if (a[3] > bottom) a[3] = bottom;
837 if (a[0] < a[2] && a[1] < a[3]) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000838 wbegindrawing(self->t_ref->w_win);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000839 tedrawnew(tp, a[0], a[1], a[2], a[3]);
840 wenddrawing(self->t_ref->w_win);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000841 }
842 INCREF(None);
843 return None;
844}
845
846static object *
847text_event(self, args)
848 textobject *self;
849 object *args;
850{
851 register TEXTEDIT *tp = self->t_text;
852 EVENT e;
853 if (!geteventarg(args, &e))
854 return NULL;
855 if (e.type == WE_MOUSE_DOWN) {
Guido van Rossum33f17701991-02-13 23:19:39 +0000856 /* Cheat at the margins */
857 int width, height;
858 wgetdocsize(e.window, &width, &height);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000859 if (e.u.where.h < 0 && tegetleft(tp) == 0)
860 e.u.where.h = 0;
Guido van Rossum33f17701991-02-13 23:19:39 +0000861 else if (e.u.where.h > width && tegetright(tp) == width)
862 e.u.where.h = width;
863 if (e.u.where.v < 0 && tegettop(tp) == 0)
864 e.u.where.v = 0;
865 else if (e.u.where.v > height && tegetright(tp) == height)
866 e.u.where.v = height;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000867 }
868 return newintobject((long) teevent(tp, &e));
869}
870
871static object *
872text_getfocus(self, args)
873 textobject *self;
874 object *args;
875{
876 if (!getnoarg(args))
877 return NULL;
878 return makepoint(tegetfoc1(self->t_text), tegetfoc2(self->t_text));
879}
880
881static object *
882text_getfocustext(self, args)
883 textobject *self;
884 object *args;
885{
886 int f1, f2;
887 char *text;
888 if (!getnoarg(args))
889 return NULL;
890 f1 = tegetfoc1(self->t_text);
891 f2 = tegetfoc2(self->t_text);
892 text = tegettext(self->t_text);
893 return newsizedstringobject(text + f1, f2-f1);
894}
895
896static object *
897text_getrect(self, args)
898 textobject *self;
899 object *args;
900{
901 if (!getnoarg(args))
902 return NULL;
903 return makerect(tegetleft(self->t_text),
904 tegettop(self->t_text),
905 tegetright(self->t_text),
906 tegetbottom(self->t_text));
907}
908
909static object *
910text_gettext(self, args)
911 textobject *self;
912 object *args;
913{
914 if (!getnoarg(args))
915 return NULL;
916 return newsizedstringobject(tegettext(self->t_text),
917 tegetlen(self->t_text));
918}
919
920static object *
921text_move(self, args)
922 textobject *self;
923 object *args;
924{
925 int a[4];
926 if (!getrectarg(args, a))
927 return NULL;
928 temovenew(self->t_text, a[0], a[1], a[2], a[3]);
929 INCREF(None);
930 return None;
931}
932
933static object *
Guido van Rossum4b9cf8e1991-05-28 21:57:04 +0000934text_replace(self, args)
935 textobject *self;
936 object *args;
937{
Guido van Rossumfc58e581992-01-27 16:45:55 +0000938 char *text;
Guido van Rossum4b9cf8e1991-05-28 21:57:04 +0000939 if (!getstrarg(args, &text))
940 return NULL;
Guido van Rossumfc58e581992-01-27 16:45:55 +0000941 tereplace(self->t_text, text);
Guido van Rossum4b9cf8e1991-05-28 21:57:04 +0000942 INCREF(None);
943 return None;
944}
945
946static object *
947text_setactive(self, args)
948 textobject *self;
949 object *args;
950{
951 int flag;
952 if (!getintarg(args, &flag))
953 return NULL;
954 tesetactive(self->t_text, flag);
955 INCREF(None);
956 return None;
957}
958
959static object *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000960text_setfocus(self, args)
961 textobject *self;
962 object *args;
963{
964 int a[2];
965 if (!getpointarg(args, a))
966 return NULL;
967 tesetfocus(self->t_text, a[0], a[1]);
968 INCREF(None);
969 return None;
970}
971
972static object *
Guido van Rossum541c8c01991-05-05 20:13:41 +0000973text_settext(self, args)
974 textobject *self;
975 object *args;
976{
Guido van Rossumfc58e581992-01-27 16:45:55 +0000977 char *text;
Guido van Rossum541c8c01991-05-05 20:13:41 +0000978 char *buf;
979 int size;
Guido van Rossumfc58e581992-01-27 16:45:55 +0000980 if (!getargs(args, "s#", &text, &size))
Guido van Rossum541c8c01991-05-05 20:13:41 +0000981 return NULL;
Guido van Rossum541c8c01991-05-05 20:13:41 +0000982 if ((buf = NEW(char, size)) == NULL) {
Guido van Rossum87e7ea71991-12-10 14:00:03 +0000983 return err_nomem();
Guido van Rossum541c8c01991-05-05 20:13:41 +0000984 }
Guido van Rossumfc58e581992-01-27 16:45:55 +0000985 memcpy(buf, text, size);
Guido van Rossum541c8c01991-05-05 20:13:41 +0000986 tesetbuf(self->t_text, buf, size); /* Becomes owner of buffer */
987 INCREF(None);
988 return None;
989}
990
991static object *
Guido van Rossum4b9cf8e1991-05-28 21:57:04 +0000992text_setview(self, args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000993 textobject *self;
994 object *args;
995{
Guido van Rossum4b9cf8e1991-05-28 21:57:04 +0000996 int a[4];
997 if (args == None)
998 tenoview(self->t_text);
999 else {
1000 if (!getrectarg(args, a))
1001 return NULL;
1002 tesetview(self->t_text, a[0], a[1], a[2], a[3]);
1003 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001004 INCREF(None);
1005 return None;
1006}
1007
1008static struct methodlist text_methods[] = {
Guido van Rossum3c284741991-11-27 14:54:54 +00001009 {"arrow", text_arrow},
1010 {"close", text_close},
1011 {"draw", text_draw},
1012 {"event", text_event},
1013 {"getfocus", text_getfocus},
Guido van Rossum77b46041992-01-14 18:41:24 +00001014 {"getfocustext",text_getfocustext},
Guido van Rossum3c284741991-11-27 14:54:54 +00001015 {"getrect", text_getrect},
1016 {"gettext", text_gettext},
Guido van Rossum77b46041992-01-14 18:41:24 +00001017 {"move", text_move},
Guido van Rossum3c284741991-11-27 14:54:54 +00001018 {"replace", text_replace},
1019 {"setactive", text_setactive},
1020 {"setfocus", text_setfocus},
1021 {"settext", text_settext},
1022 {"setview", text_setview},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001023 {NULL, NULL} /* sentinel */
1024};
1025
1026static object *
1027text_getattr(tp, name)
1028 textobject *tp;
1029 char *name;
1030{
Guido van Rossum85f50761991-10-20 20:22:50 +00001031 object *v = NULL;
Guido van Rossum77b46041992-01-14 18:41:24 +00001032 if (tp->t_ref == NULL) {
1033 err_setstr(StdwinError, "text object already closed");
1034 return NULL;
1035 }
Guido van Rossum85f50761991-10-20 20:22:50 +00001036 if (strcmp(name, "__dict__") == 0) {
1037 v = tp->t_attr;
1038 if (v == NULL)
1039 v = None;
1040 }
1041 else if (tp->t_attr != NULL) {
1042 v = dictlookup(tp->t_attr, name);
1043 }
1044 if (v != NULL) {
1045 INCREF(v);
1046 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001047 }
1048 return findmethod(text_methods, (object *)tp, name);
1049}
1050
1051static int
1052text_setattr(tp, name, v)
1053 textobject *tp;
1054 char *name;
1055 object *v;
1056{
1057 if (tp->t_attr == NULL) {
1058 tp->t_attr = newdictobject();
1059 if (tp->t_attr == NULL)
1060 return -1;
1061 }
Guido van Rossum94472a01992-09-04 09:45:18 +00001062 if (v == NULL) {
1063 int rv = dictremove(tp->t_attr, name);
1064 if (rv < 0)
1065 err_setstr(AttributeError,
1066 "delete non-existing text object attribute");
1067 return rv;
1068 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001069 else
1070 return dictinsert(tp->t_attr, name, v);
1071}
1072
Guido van Rossum541c8c01991-05-05 20:13:41 +00001073typeobject Texttype = {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001074 OB_HEAD_INIT(&Typetype)
1075 0, /*ob_size*/
1076 "textedit", /*tp_name*/
1077 sizeof(textobject), /*tp_size*/
1078 0, /*tp_itemsize*/
1079 /* methods */
1080 text_dealloc, /*tp_dealloc*/
1081 0, /*tp_print*/
1082 text_getattr, /*tp_getattr*/
1083 text_setattr, /*tp_setattr*/
1084 0, /*tp_compare*/
1085 0, /*tp_repr*/
1086};
1087
1088
1089/* Menu objects */
1090
Guido van Rossum2d14e211991-02-19 12:26:49 +00001091#define IDOFFSET 10 /* Menu IDs we use start here */
Guido van Rossum27201061991-04-16 08:43:03 +00001092#define MAXNMENU 200 /* Max #menus we allow */
Guido van Rossum2d14e211991-02-19 12:26:49 +00001093static menuobject *menulist[MAXNMENU];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001094
Guido van Rossumfc58e581992-01-27 16:45:55 +00001095static menuobject *newmenuobject PROTO((char *));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001096static menuobject *
1097newmenuobject(title)
Guido van Rossumfc58e581992-01-27 16:45:55 +00001098 char *title;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001099{
1100 int id;
1101 MENU *menu;
1102 menuobject *mp;
Guido van Rossum2d14e211991-02-19 12:26:49 +00001103 for (id = 0; id < MAXNMENU; id++) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001104 if (menulist[id] == NULL)
1105 break;
1106 }
Guido van Rossum27201061991-04-16 08:43:03 +00001107 if (id >= MAXNMENU) {
Guido van Rossum87e7ea71991-12-10 14:00:03 +00001108 err_setstr(StdwinError, "creating too many menus");
Guido van Rossum27201061991-04-16 08:43:03 +00001109 return NULL;
1110 }
Guido van Rossumfc58e581992-01-27 16:45:55 +00001111 menu = wmenucreate(id + IDOFFSET, title);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001112 if (menu == NULL)
1113 return (menuobject *) err_nomem();
1114 mp = NEWOBJ(menuobject, &Menutype);
1115 if (mp != NULL) {
1116 mp->m_menu = menu;
Guido van Rossum2d14e211991-02-19 12:26:49 +00001117 mp->m_id = id + IDOFFSET;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001118 mp->m_attr = NULL;
1119 menulist[id] = mp;
1120 }
1121 else
1122 wmenudelete(menu);
1123 return mp;
1124}
1125
1126/* Menu methods */
1127
1128static void
1129menu_dealloc(mp)
1130 menuobject *mp;
1131{
1132
Guido van Rossum2d14e211991-02-19 12:26:49 +00001133 int id = mp->m_id - IDOFFSET;
1134 if (id >= 0 && id < MAXNMENU && menulist[id] == mp) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001135 menulist[id] = NULL;
1136 }
Guido van Rossum77b46041992-01-14 18:41:24 +00001137 if (mp->m_menu != NULL)
1138 wmenudelete(mp->m_menu);
1139 XDECREF(mp->m_attr);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001140 DEL(mp);
1141}
1142
1143static object *
Guido van Rossum77b46041992-01-14 18:41:24 +00001144menu_close(mp, args)
1145 menuobject *mp;
1146 object *args;
1147{
1148 int id = mp->m_id - IDOFFSET;
1149 if (id >= 0 && id < MAXNMENU && menulist[id] == mp) {
1150 menulist[id] = NULL;
1151 }
1152 mp->m_id = -1;
1153 if (mp->m_menu != NULL)
1154 wmenudelete(mp->m_menu);
1155 mp->m_menu = NULL;
1156 XDECREF(mp->m_attr);
1157 mp->m_attr = NULL;
1158 INCREF(None);
1159 return None;
1160}
1161
1162static object *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001163menu_additem(self, args)
1164 menuobject *self;
1165 object *args;
1166{
Guido van Rossumfc58e581992-01-27 16:45:55 +00001167 char *text;
1168 int shortcut = -1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001169 if (is_tupleobject(args)) {
Guido van Rossumfc58e581992-01-27 16:45:55 +00001170 char c;
1171 if (!getargs(args, "(sc)", &text, &c))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001172 return NULL;
Guido van Rossumfc58e581992-01-27 16:45:55 +00001173 shortcut = c;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001174 }
Guido van Rossumfc58e581992-01-27 16:45:55 +00001175 else if (!getstrarg(args, &text))
1176 return NULL;
1177 wmenuadditem(self->m_menu, text, shortcut);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001178 INCREF(None);
1179 return None;
1180}
1181
1182static object *
1183menu_setitem(self, args)
1184 menuobject *self;
1185 object *args;
1186{
1187 int index;
Guido van Rossumfc58e581992-01-27 16:45:55 +00001188 char *text;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001189 if (!getintstrarg(args, &index, &text))
1190 return NULL;
Guido van Rossumfc58e581992-01-27 16:45:55 +00001191 wmenusetitem(self->m_menu, index, text);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001192 INCREF(None);
1193 return None;
1194}
1195
1196static object *
1197menu_enable(self, args)
1198 menuobject *self;
1199 object *args;
1200{
1201 int index;
1202 int flag;
1203 if (!getintintarg(args, &index, &flag))
1204 return NULL;
1205 wmenuenable(self->m_menu, index, flag);
1206 INCREF(None);
1207 return None;
1208}
1209
1210static object *
1211menu_check(self, args)
1212 menuobject *self;
1213 object *args;
1214{
1215 int index;
1216 int flag;
1217 if (!getintintarg(args, &index, &flag))
1218 return NULL;
1219 wmenucheck(self->m_menu, index, flag);
1220 INCREF(None);
1221 return None;
1222}
1223
1224static struct methodlist menu_methods[] = {
Guido van Rossum3c284741991-11-27 14:54:54 +00001225 {"additem", menu_additem},
1226 {"setitem", menu_setitem},
1227 {"enable", menu_enable},
1228 {"check", menu_check},
Guido van Rossum77b46041992-01-14 18:41:24 +00001229 {"close", menu_close},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001230 {NULL, NULL} /* sentinel */
1231};
1232
1233static object *
1234menu_getattr(mp, name)
1235 menuobject *mp;
1236 char *name;
1237{
Guido van Rossum85f50761991-10-20 20:22:50 +00001238 object *v = NULL;
Guido van Rossum77b46041992-01-14 18:41:24 +00001239 if (mp->m_menu == NULL) {
1240 err_setstr(StdwinError, "menu object already closed");
1241 return NULL;
1242 }
Guido van Rossum85f50761991-10-20 20:22:50 +00001243 if (strcmp(name, "__dict__") == 0) {
1244 v = mp->m_attr;
1245 if (v == NULL)
1246 v = None;
1247 }
1248 else if (mp->m_attr != NULL) {
1249 v = dictlookup(mp->m_attr, name);
1250 }
1251 if (v != NULL) {
1252 INCREF(v);
1253 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001254 }
1255 return findmethod(menu_methods, (object *)mp, name);
1256}
1257
1258static int
1259menu_setattr(mp, name, v)
1260 menuobject *mp;
1261 char *name;
1262 object *v;
1263{
1264 if (mp->m_attr == NULL) {
1265 mp->m_attr = newdictobject();
1266 if (mp->m_attr == NULL)
1267 return -1;
1268 }
Guido van Rossum94472a01992-09-04 09:45:18 +00001269 if (v == NULL) {
1270 int rv = dictremove(mp->m_attr, name);
1271 if (rv < 0)
1272 err_setstr(AttributeError,
1273 "delete non-existing menu object attribute");
1274 return rv;
1275 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001276 else
1277 return dictinsert(mp->m_attr, name, v);
1278}
1279
Guido van Rossum541c8c01991-05-05 20:13:41 +00001280typeobject Menutype = {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001281 OB_HEAD_INIT(&Typetype)
1282 0, /*ob_size*/
1283 "menu", /*tp_name*/
1284 sizeof(menuobject), /*tp_size*/
1285 0, /*tp_itemsize*/
1286 /* methods */
1287 menu_dealloc, /*tp_dealloc*/
1288 0, /*tp_print*/
1289 menu_getattr, /*tp_getattr*/
1290 menu_setattr, /*tp_setattr*/
1291 0, /*tp_compare*/
1292 0, /*tp_repr*/
1293};
1294
1295
1296/* Windows */
1297
1298#define MAXNWIN 50
1299static windowobject *windowlist[MAXNWIN];
1300
1301/* Window methods */
1302
1303static void
1304window_dealloc(wp)
1305 windowobject *wp;
1306{
1307 if (wp->w_win != NULL) {
1308 int tag = wgettag(wp->w_win);
1309 if (tag >= 0 && tag < MAXNWIN)
1310 windowlist[tag] = NULL;
1311 else
1312 fprintf(stderr, "XXX help! tag %d in window_dealloc\n",
1313 tag);
1314 wclose(wp->w_win);
1315 }
1316 DECREF(wp->w_title);
1317 if (wp->w_attr != NULL)
1318 DECREF(wp->w_attr);
1319 free((char *)wp);
1320}
1321
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001322static object *
Guido van Rossum3c284741991-11-27 14:54:54 +00001323window_close(wp, args)
1324 windowobject *wp;
1325 object *args;
1326{
1327 if (wp->w_win != NULL) {
1328 int tag = wgettag(wp->w_win);
1329 if (tag >= 0 && tag < MAXNWIN)
1330 windowlist[tag] = NULL;
1331 wclose(wp->w_win);
1332 wp->w_win = NULL;
1333 }
1334 INCREF(None);
1335 return None;
1336}
1337
1338static object *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001339window_begindrawing(wp, args)
1340 windowobject *wp;
1341 object *args;
1342{
1343 drawingobject *dp;
1344 if (!getnoarg(args))
1345 return NULL;
1346 if (Drawing != NULL) {
Guido van Rossum87e7ea71991-12-10 14:00:03 +00001347 err_setstr(StdwinError, "already drawing");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001348 return NULL;
1349 }
1350 dp = NEWOBJ(drawingobject, &Drawingtype);
1351 if (dp == NULL)
1352 return NULL;
1353 Drawing = dp;
1354 INCREF(wp);
1355 dp->d_ref = wp;
1356 wbegindrawing(wp->w_win);
1357 return (object *)dp;
1358}
1359
1360static object *
1361window_change(wp, args)
1362 windowobject *wp;
1363 object *args;
1364{
1365 int a[4];
1366 if (!getrectarg(args, a))
1367 return NULL;
1368 wchange(wp->w_win, a[0], a[1], a[2], a[3]);
1369 INCREF(None);
1370 return None;
1371}
1372
1373static object *
1374window_gettitle(wp, args)
1375 windowobject *wp;
1376 object *args;
1377{
1378 if (!getnoarg(args))
1379 return NULL;
1380 INCREF(wp->w_title);
1381 return wp->w_title;
1382}
1383
1384static object *
Guido van Rossum541c8c01991-05-05 20:13:41 +00001385window_getwinpos(wp, args)
1386 windowobject *wp;
1387 object *args;
1388{
1389 int h, v;
1390 if (!getnoarg(args))
1391 return NULL;
1392 wgetwinpos(wp->w_win, &h, &v);
1393 return makepoint(h, v);
1394}
1395
1396static object *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001397window_getwinsize(wp, args)
1398 windowobject *wp;
1399 object *args;
1400{
1401 int width, height;
1402 if (!getnoarg(args))
1403 return NULL;
1404 wgetwinsize(wp->w_win, &width, &height);
1405 return makepoint(width, height);
1406}
1407
1408static object *
1409window_getdocsize(wp, args)
1410 windowobject *wp;
1411 object *args;
1412{
1413 int width, height;
1414 if (!getnoarg(args))
1415 return NULL;
1416 wgetdocsize(wp->w_win, &width, &height);
1417 return makepoint(width, height);
1418}
1419
1420static object *
1421window_getorigin(wp, args)
1422 windowobject *wp;
1423 object *args;
1424{
1425 int width, height;
1426 if (!getnoarg(args))
1427 return NULL;
1428 wgetorigin(wp->w_win, &width, &height);
1429 return makepoint(width, height);
1430}
1431
1432static object *
1433window_scroll(wp, args)
1434 windowobject *wp;
1435 object *args;
1436{
1437 int a[6];
1438 if (!getrectpointarg(args, a))
1439 return NULL;
1440 wscroll(wp->w_win, a[0], a[1], a[2], a[3], a[4], a[5]);
1441 INCREF(None);
1442 return None;
1443}
1444
1445static object *
1446window_setdocsize(wp, args)
1447 windowobject *wp;
1448 object *args;
1449{
1450 int a[2];
1451 if (!getpointarg(args, a))
1452 return NULL;
1453 wsetdocsize(wp->w_win, a[0], a[1]);
1454 INCREF(None);
1455 return None;
1456}
1457
1458static object *
1459window_setorigin(wp, args)
1460 windowobject *wp;
1461 object *args;
1462{
1463 int a[2];
1464 if (!getpointarg(args, a))
1465 return NULL;
1466 wsetorigin(wp->w_win, a[0], a[1]);
1467 INCREF(None);
1468 return None;
1469}
1470
1471static object *
1472window_settitle(wp, args)
1473 windowobject *wp;
1474 object *args;
1475{
1476 object *title;
Guido van Rossumfc58e581992-01-27 16:45:55 +00001477 if (!getStrarg(args, &title))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001478 return NULL;
1479 DECREF(wp->w_title);
1480 INCREF(title);
1481 wp->w_title = title;
1482 wsettitle(wp->w_win, getstringvalue(title));
1483 INCREF(None);
1484 return None;
1485}
1486
1487static object *
1488window_show(wp, args)
1489 windowobject *wp;
1490 object *args;
1491{
1492 int a[4];
1493 if (!getrectarg(args, a))
1494 return NULL;
1495 wshow(wp->w_win, a[0], a[1], a[2], a[3]);
1496 INCREF(None);
1497 return None;
1498}
1499
1500static object *
1501window_settimer(wp, args)
1502 windowobject *wp;
1503 object *args;
1504{
1505 int a;
1506 if (!getintarg(args, &a))
1507 return NULL;
1508 wsettimer(wp->w_win, a);
1509 INCREF(None);
1510 return None;
1511}
1512
1513static object *
1514window_menucreate(self, args)
1515 windowobject *self;
1516 object *args;
1517{
1518 menuobject *mp;
Guido van Rossumfc58e581992-01-27 16:45:55 +00001519 char *title;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001520 if (!getstrarg(args, &title))
1521 return NULL;
1522 wmenusetdeflocal(1);
1523 mp = newmenuobject(title);
1524 if (mp == NULL)
1525 return NULL;
1526 wmenuattach(self->w_win, mp->m_menu);
1527 return (object *)mp;
1528}
1529
1530static object *
1531window_textcreate(self, args)
1532 windowobject *self;
1533 object *args;
1534{
1535 textobject *tp;
1536 int a[4];
1537 if (!getrectarg(args, a))
1538 return NULL;
1539 return (object *)
1540 newtextobject(self, a[0], a[1], a[2], a[3]);
1541}
1542
Guido van Rossum5b10f451990-10-30 16:01:48 +00001543static object *
1544window_setselection(self, args)
1545 windowobject *self;
1546 object *args;
1547{
Guido van Rossumfc58e581992-01-27 16:45:55 +00001548 int sel, size, ok;
1549 char *text;
1550 if (!getargs(args, "(is#)", &sel, &text, &size))
Guido van Rossum5b10f451990-10-30 16:01:48 +00001551 return NULL;
Guido van Rossumfc58e581992-01-27 16:45:55 +00001552 ok = wsetselection(self->w_win, sel, text, size);
Guido van Rossum5b10f451990-10-30 16:01:48 +00001553 return newintobject(ok);
1554}
1555
1556static object *
1557window_setwincursor(self, args)
1558 windowobject *self;
1559 object *args;
1560{
Guido van Rossumfc58e581992-01-27 16:45:55 +00001561 char *name;
Guido van Rossum5b10f451990-10-30 16:01:48 +00001562 CURSOR *c;
Guido van Rossum3c8ba7a1992-02-05 11:15:00 +00001563 if (!getargs(args, "z", &name))
Guido van Rossum5b10f451990-10-30 16:01:48 +00001564 return NULL;
Guido van Rossum3c8ba7a1992-02-05 11:15:00 +00001565 if (name == NULL)
1566 c = NULL;
1567 else {
1568 c = wfetchcursor(name);
1569 if (c == NULL) {
1570 err_setstr(StdwinError, "no such cursor");
1571 return NULL;
1572 }
Guido van Rossum5b10f451990-10-30 16:01:48 +00001573 }
1574 wsetwincursor(self->w_win, c);
1575 INCREF(None);
1576 return None;
1577}
1578
Guido van Rossumfc58e581992-01-27 16:45:55 +00001579static object *
1580window_setactive(self, args)
1581 windowobject *self;
1582 object *args;
1583{
1584 if (!getnoarg(args))
1585 return NULL;
1586 wsetactive(self->w_win);
1587 INCREF(None);
1588 return None;
1589}
1590
Guido van Rossum8dcbbac1991-07-27 21:42:24 +00001591#ifdef CWI_HACKS
1592static object *
1593window_getxwindowid(self, args)
1594 windowobject *self;
1595 object *args;
1596{
1597 long wid = wgetxwindowid(self->w_win);
1598 return newintobject(wid);
1599}
1600#endif
1601
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001602static struct methodlist window_methods[] = {
1603 {"begindrawing",window_begindrawing},
1604 {"change", window_change},
Guido van Rossum3c284741991-11-27 14:54:54 +00001605 {"close", window_close},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001606 {"getdocsize", window_getdocsize},
1607 {"getorigin", window_getorigin},
1608 {"gettitle", window_gettitle},
Guido van Rossum541c8c01991-05-05 20:13:41 +00001609 {"getwinpos", window_getwinpos},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001610 {"getwinsize", window_getwinsize},
1611 {"menucreate", window_menucreate},
1612 {"scroll", window_scroll},
Guido van Rossumb7e51601992-01-26 18:13:18 +00001613 {"setactive", window_setactive},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001614 {"setdocsize", window_setdocsize},
1615 {"setorigin", window_setorigin},
Guido van Rossum5b10f451990-10-30 16:01:48 +00001616 {"setselection",window_setselection},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001617 {"settimer", window_settimer},
1618 {"settitle", window_settitle},
Guido van Rossum27201061991-04-16 08:43:03 +00001619 {"setwincursor",window_setwincursor},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001620 {"show", window_show},
1621 {"textcreate", window_textcreate},
Guido van Rossum8dcbbac1991-07-27 21:42:24 +00001622#ifdef CWI_HACKS
1623 {"getxwindowid",window_getxwindowid},
1624#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001625 {NULL, NULL} /* sentinel */
1626};
1627
1628static object *
1629window_getattr(wp, name)
1630 windowobject *wp;
1631 char *name;
1632{
Guido van Rossum85f50761991-10-20 20:22:50 +00001633 object *v = NULL;
Guido van Rossum77b46041992-01-14 18:41:24 +00001634 if (wp->w_win == NULL) {
1635 err_setstr(StdwinError, "window already closed");
1636 return NULL;
1637 }
Guido van Rossum85f50761991-10-20 20:22:50 +00001638 if (strcmp(name, "__dict__") == 0) {
1639 v = wp->w_attr;
1640 if (v == NULL)
1641 v = None;
1642 }
1643 else if (wp->w_attr != NULL) {
1644 v = dictlookup(wp->w_attr, name);
1645 }
1646 if (v != NULL) {
1647 INCREF(v);
1648 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001649 }
1650 return findmethod(window_methods, (object *)wp, name);
1651}
1652
1653static int
1654window_setattr(wp, name, v)
1655 windowobject *wp;
1656 char *name;
1657 object *v;
1658{
1659 if (wp->w_attr == NULL) {
1660 wp->w_attr = newdictobject();
1661 if (wp->w_attr == NULL)
1662 return -1;
1663 }
Guido van Rossum94472a01992-09-04 09:45:18 +00001664 if (v == NULL) {
1665 int rv = dictremove(wp->w_attr, name);
1666 if (rv < 0)
1667 err_setstr(AttributeError,
1668 "delete non-existing menu object attribute");
1669 return rv;
1670 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001671 else
1672 return dictinsert(wp->w_attr, name, v);
1673}
1674
Guido van Rossum541c8c01991-05-05 20:13:41 +00001675typeobject Windowtype = {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001676 OB_HEAD_INIT(&Typetype)
1677 0, /*ob_size*/
1678 "window", /*tp_name*/
1679 sizeof(windowobject), /*tp_size*/
1680 0, /*tp_itemsize*/
1681 /* methods */
1682 window_dealloc, /*tp_dealloc*/
Guido van Rossum7066dd71992-09-17 17:54:56 +00001683 0, /*tp_print*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001684 window_getattr, /*tp_getattr*/
1685 window_setattr, /*tp_setattr*/
1686 0, /*tp_compare*/
1687 0, /*tp_repr*/
1688};
1689
1690/* Stdwin methods */
1691
1692static object *
1693stdwin_open(sw, args)
1694 object *sw;
1695 object *args;
1696{
1697 int tag;
1698 object *title;
1699 windowobject *wp;
Guido van Rossumfc58e581992-01-27 16:45:55 +00001700 if (!getStrarg(args, &title))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001701 return NULL;
1702 for (tag = 0; tag < MAXNWIN; tag++) {
1703 if (windowlist[tag] == NULL)
1704 break;
1705 }
Guido van Rossum27201061991-04-16 08:43:03 +00001706 if (tag >= MAXNWIN) {
Guido van Rossum87e7ea71991-12-10 14:00:03 +00001707 err_setstr(StdwinError, "creating too many windows");
Guido van Rossum27201061991-04-16 08:43:03 +00001708 return NULL;
1709 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001710 wp = NEWOBJ(windowobject, &Windowtype);
1711 if (wp == NULL)
1712 return NULL;
1713 INCREF(title);
1714 wp->w_title = title;
1715 wp->w_win = wopen(getstringvalue(title), (void (*)()) NULL);
1716 wp->w_attr = NULL;
1717 if (wp->w_win == NULL) {
1718 DECREF(wp);
1719 return NULL;
1720 }
1721 windowlist[tag] = wp;
1722 wsettag(wp->w_win, tag);
1723 return (object *)wp;
1724}
1725
1726static object *
Guido van Rossum246b9d81991-06-03 10:55:14 +00001727window2object(win)
1728 WINDOW *win;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001729{
Guido van Rossum246b9d81991-06-03 10:55:14 +00001730 object *w;
1731 if (win == NULL)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001732 w = None;
1733 else {
Guido van Rossum246b9d81991-06-03 10:55:14 +00001734 int tag = wgettag(win);
1735 if (tag < 0 || tag >= MAXNWIN || windowlist[tag] == NULL ||
1736 windowlist[tag]->w_win != win)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001737 w = None;
1738 else
1739 w = (object *)windowlist[tag];
1740 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001741 INCREF(w);
Guido van Rossum246b9d81991-06-03 10:55:14 +00001742 return w;
1743}
1744
1745static object *
1746stdwin_get_poll_event(poll, args)
1747 int poll;
1748 object *args;
1749{
1750 EVENT e;
Guido van Rossum2ee12f41992-04-13 15:54:35 +00001751 object *u, *v, *w;
Guido van Rossum246b9d81991-06-03 10:55:14 +00001752 if (!getnoarg(args))
1753 return NULL;
1754 if (Drawing != NULL) {
Guido van Rossum87e7ea71991-12-10 14:00:03 +00001755 err_setstr(StdwinError, "cannot getevent() while drawing");
Guido van Rossum246b9d81991-06-03 10:55:14 +00001756 return NULL;
1757 }
1758 again:
Guido van Rossumff4949e1992-08-05 19:58:53 +00001759 BGN_STDWIN
Guido van Rossum246b9d81991-06-03 10:55:14 +00001760 if (poll) {
1761 if (!wpollevent(&e)) {
Guido van Rossumff4949e1992-08-05 19:58:53 +00001762 RET_STDWIN
Guido van Rossum246b9d81991-06-03 10:55:14 +00001763 INCREF(None);
1764 return None;
1765 }
1766 }
1767 else
1768 wgetevent(&e);
Guido van Rossumff4949e1992-08-05 19:58:53 +00001769 END_STDWIN
Guido van Rossum246b9d81991-06-03 10:55:14 +00001770 if (e.type == WE_COMMAND && e.u.command == WC_CANCEL) {
1771 /* Turn keyboard interrupts into exceptions */
1772 err_set(KeyboardInterrupt);
1773 return NULL;
1774 }
1775 if (e.type == WE_COMMAND && e.u.command == WC_CLOSE) {
1776 /* Turn WC_CLOSE commands into WE_CLOSE events */
1777 e.type = WE_CLOSE;
1778 }
Guido van Rossum2ee12f41992-04-13 15:54:35 +00001779 v = window2object(e.window);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001780 switch (e.type) {
1781 case WE_CHAR:
1782 {
1783 char c[1];
1784 c[0] = e.u.character;
1785 w = newsizedstringobject(c, 1);
1786 }
1787 break;
1788 case WE_COMMAND:
1789 w = newintobject((long)e.u.command);
1790 break;
1791 case WE_DRAW:
1792 w = makerect(e.u.area.left, e.u.area.top,
1793 e.u.area.right, e.u.area.bottom);
1794 break;
1795 case WE_MOUSE_DOWN:
1796 case WE_MOUSE_MOVE:
1797 case WE_MOUSE_UP:
Guido van Rossum2ee12f41992-04-13 15:54:35 +00001798 w = mkvalue("((ii)iii)",
1799 e.u.where.h, e.u.where.v,
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001800 e.u.where.clicks,
1801 e.u.where.button,
1802 e.u.where.mask);
1803 break;
1804 case WE_MENU:
Guido van Rossum2d14e211991-02-19 12:26:49 +00001805 if (e.u.m.id >= IDOFFSET && e.u.m.id < IDOFFSET+MAXNMENU &&
1806 menulist[e.u.m.id - IDOFFSET] != NULL)
Guido van Rossum2ee12f41992-04-13 15:54:35 +00001807 w = mkvalue("(Oi)",
1808 menulist[e.u.m.id - IDOFFSET], e.u.m.item);
Guido van Rossum246b9d81991-06-03 10:55:14 +00001809 else {
1810 /* Ghost menu event.
1811 Can occur only on the Mac if another part
1812 of the aplication has installed a menu;
1813 like the THINK C console library. */
1814 DECREF(v);
1815 goto again;
1816 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001817 break;
Guido van Rossum3ee199e1992-06-30 12:48:26 +00001818 case WE_KEY:
1819 w = mkvalue("(ii)", e.u.key.code, e.u.key.mask);
1820 break;
Guido van Rossum5b10f451990-10-30 16:01:48 +00001821 case WE_LOST_SEL:
1822 w = newintobject((long)e.u.sel);
1823 break;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001824 default:
1825 w = None;
1826 INCREF(w);
1827 break;
1828 }
1829 if (w == NULL) {
1830 DECREF(v);
1831 return NULL;
1832 }
Guido van Rossum2ee12f41992-04-13 15:54:35 +00001833 u = mkvalue("(iOO)", e.type, v, w);
1834 XDECREF(v);
1835 XDECREF(w);
1836 return u;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001837}
1838
1839static object *
Guido van Rossume8e7cf41991-01-16 14:06:18 +00001840stdwin_getevent(sw, args)
1841 object *sw;
1842 object *args;
1843{
1844 return stdwin_get_poll_event(0, args);
1845}
1846
1847static object *
1848stdwin_pollevent(sw, args)
1849 object *sw;
1850 object *args;
1851{
1852 return stdwin_get_poll_event(1, args);
1853}
1854
1855static object *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001856stdwin_setdefwinpos(sw, args)
1857 object *sw;
1858 object *args;
1859{
1860 int a[2];
1861 if (!getpointarg(args, a))
1862 return NULL;
1863 wsetdefwinpos(a[0], a[1]);
1864 INCREF(None);
1865 return None;
1866}
1867
1868static object *
1869stdwin_setdefwinsize(sw, args)
1870 object *sw;
1871 object *args;
1872{
1873 int a[2];
1874 if (!getpointarg(args, a))
1875 return NULL;
1876 wsetdefwinsize(a[0], a[1]);
1877 INCREF(None);
1878 return None;
1879}
1880
1881static object *
Guido van Rossum0c2290b1991-04-03 19:12:14 +00001882stdwin_setdefscrollbars(sw, args)
1883 object *sw;
1884 object *args;
1885{
1886 int a[2];
1887 if (!getpointarg(args, a))
1888 return NULL;
1889 wsetdefscrollbars(a[0], a[1]);
1890 INCREF(None);
1891 return None;
1892}
1893
1894static object *
Guido van Rossum541c8c01991-05-05 20:13:41 +00001895stdwin_getdefwinpos(self, args)
1896 object *self;
Guido van Rossum33f17701991-02-13 23:19:39 +00001897 object *args;
1898{
1899 int h, v;
1900 if (!getnoarg(args))
1901 return NULL;
1902 wgetdefwinpos(&h, &v);
1903 return makepoint(h, v);
1904}
1905
1906static object *
Guido van Rossum541c8c01991-05-05 20:13:41 +00001907stdwin_getdefwinsize(self, args)
1908 object *self;
Guido van Rossum33f17701991-02-13 23:19:39 +00001909 object *args;
1910{
1911 int width, height;
1912 if (!getnoarg(args))
1913 return NULL;
1914 wgetdefwinsize(&width, &height);
1915 return makepoint(width, height);
1916}
1917
1918static object *
Guido van Rossum541c8c01991-05-05 20:13:41 +00001919stdwin_getdefscrollbars(self, args)
1920 object *self;
Guido van Rossum0c2290b1991-04-03 19:12:14 +00001921 object *args;
1922{
1923 int h, v;
1924 if (!getnoarg(args))
1925 return NULL;
1926 wgetdefscrollbars(&h, &v);
1927 return makepoint(h, v);
1928}
1929
1930static object *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001931stdwin_menucreate(self, args)
1932 object *self;
1933 object *args;
1934{
Guido van Rossumfc58e581992-01-27 16:45:55 +00001935 char *title;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001936 if (!getstrarg(args, &title))
1937 return NULL;
1938 wmenusetdeflocal(0);
1939 return (object *)newmenuobject(title);
1940}
1941
1942static object *
1943stdwin_askfile(self, args)
1944 object *self;
1945 object *args;
1946{
Guido van Rossumfc58e581992-01-27 16:45:55 +00001947 char *prompt, *dflt;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001948 int new, ret;
1949 char buf[256];
1950 if (!getstrstrintarg(args, &prompt, &dflt, &new))
1951 return NULL;
Guido van Rossumfc58e581992-01-27 16:45:55 +00001952 strncpy(buf, dflt, sizeof buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001953 buf[sizeof buf - 1] = '\0';
Guido van Rossumff4949e1992-08-05 19:58:53 +00001954 BGN_STDWIN
Guido van Rossumfc58e581992-01-27 16:45:55 +00001955 ret = waskfile(prompt, buf, sizeof buf, new);
Guido van Rossumff4949e1992-08-05 19:58:53 +00001956 END_STDWIN
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001957 if (!ret) {
1958 err_set(KeyboardInterrupt);
1959 return NULL;
1960 }
1961 return newstringobject(buf);
1962}
1963
1964static object *
1965stdwin_askync(self, args)
1966 object *self;
1967 object *args;
1968{
Guido van Rossumfc58e581992-01-27 16:45:55 +00001969 char *prompt;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001970 int new, ret;
1971 if (!getstrintarg(args, &prompt, &new))
1972 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001973 BGN_STDWIN
Guido van Rossumfc58e581992-01-27 16:45:55 +00001974 ret = waskync(prompt, new);
Guido van Rossumff4949e1992-08-05 19:58:53 +00001975 END_STDWIN
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001976 if (ret < 0) {
1977 err_set(KeyboardInterrupt);
1978 return NULL;
1979 }
1980 return newintobject((long)ret);
1981}
1982
1983static object *
1984stdwin_askstr(self, args)
1985 object *self;
1986 object *args;
1987{
Guido van Rossumfc58e581992-01-27 16:45:55 +00001988 char *prompt, *dflt;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001989 int ret;
1990 char buf[256];
1991 if (!getstrstrarg(args, &prompt, &dflt))
1992 return NULL;
Guido van Rossumfc58e581992-01-27 16:45:55 +00001993 strncpy(buf, dflt, sizeof buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001994 buf[sizeof buf - 1] = '\0';
Guido van Rossumff4949e1992-08-05 19:58:53 +00001995 BGN_STDWIN
Guido van Rossumfc58e581992-01-27 16:45:55 +00001996 ret = waskstr(prompt, buf, sizeof buf);
Guido van Rossumff4949e1992-08-05 19:58:53 +00001997 END_STDWIN
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001998 if (!ret) {
1999 err_set(KeyboardInterrupt);
2000 return NULL;
2001 }
2002 return newstringobject(buf);
2003}
2004
2005static object *
2006stdwin_message(self, args)
2007 object *self;
2008 object *args;
2009{
Guido van Rossumfc58e581992-01-27 16:45:55 +00002010 char *msg;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002011 if (!getstrarg(args, &msg))
2012 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002013 BGN_STDWIN
Guido van Rossumfc58e581992-01-27 16:45:55 +00002014 wmessage(msg);
Guido van Rossumff4949e1992-08-05 19:58:53 +00002015 END_STDWIN
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002016 INCREF(None);
2017 return None;
2018}
2019
2020static object *
2021stdwin_fleep(self, args)
2022 object *self;
2023 object *args;
2024{
2025 if (!getnoarg(args))
2026 return NULL;
2027 wfleep();
2028 INCREF(None);
2029 return None;
2030}
2031
2032static object *
2033stdwin_setcutbuffer(self, args)
2034 object *self;
2035 object *args;
2036{
Guido van Rossumfc58e581992-01-27 16:45:55 +00002037 int i, size;
2038 char *str;
2039 if (!getargs(args, "(is#)", &i, &str, &size))
Guido van Rossum124967c1990-11-06 15:17:35 +00002040 return NULL;
Guido van Rossumfc58e581992-01-27 16:45:55 +00002041 wsetcutbuffer(i, str, size);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002042 INCREF(None);
2043 return None;
2044}
2045
2046static object *
Guido van Rossum246b9d81991-06-03 10:55:14 +00002047stdwin_getactive(self, args)
2048 object *self;
2049 object *args;
2050{
2051 return window2object(wgetactive());
2052}
2053
2054static object *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002055stdwin_getcutbuffer(self, args)
2056 object *self;
2057 object *args;
2058{
Guido van Rossum5b10f451990-10-30 16:01:48 +00002059 int i;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002060 char *str;
Guido van Rossum01769f01990-10-30 13:39:00 +00002061 int len;
Guido van Rossum124967c1990-11-06 15:17:35 +00002062 if (!getintarg(args, &i))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002063 return NULL;
Guido van Rossum5b10f451990-10-30 16:01:48 +00002064 str = wgetcutbuffer(i, &len);
Guido van Rossum01769f01990-10-30 13:39:00 +00002065 if (str == NULL) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002066 str = "";
Guido van Rossum01769f01990-10-30 13:39:00 +00002067 len = 0;
2068 }
2069 return newsizedstringobject(str, len);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002070}
2071
Guido van Rossum5b10f451990-10-30 16:01:48 +00002072static object *
2073stdwin_rotatecutbuffers(self, args)
2074 object *self;
2075 object *args;
2076{
2077 int i;
2078 if (!getintarg(args, &i))
2079 return NULL;
2080 wrotatecutbuffers(i);
2081 INCREF(None);
2082 return None;
2083}
2084
2085static object *
2086stdwin_getselection(self, args)
2087 object *self;
2088 object *args;
2089{
2090 int sel;
2091 char *data;
2092 int len;
2093 if (!getintarg(args, &sel))
2094 return NULL;
2095 data = wgetselection(sel, &len);
2096 if (data == NULL) {
2097 data = "";
2098 len = 0;
2099 }
2100 return newsizedstringobject(data, len);
2101}
2102
2103static object *
2104stdwin_resetselection(self, args)
2105 object *self;
2106 object *args;
2107{
2108 int sel;
2109 if (!getintarg(args, &sel))
2110 return NULL;
2111 wresetselection(sel);
2112 INCREF(None);
2113 return None;
2114}
2115
Guido van Rossum0c2290b1991-04-03 19:12:14 +00002116static object *
2117stdwin_fetchcolor(self, args)
2118 object *self;
2119 object *args;
2120{
Guido van Rossumfc58e581992-01-27 16:45:55 +00002121 char *colorname;
Guido van Rossum34679b71993-01-26 13:33:44 +00002122 COLOR color;
Guido van Rossum0c2290b1991-04-03 19:12:14 +00002123 if (!getstrarg(args, &colorname))
2124 return NULL;
Guido van Rossum34679b71993-01-26 13:33:44 +00002125 color = wfetchcolor(colorname);
2126#ifdef BADCOLOR
2127 if (color == BADCOLOR) {
2128 err_setstr(StdwinError, "color name not found");
2129 return NULL;
2130 }
2131#endif
2132 return newintobject((long)color);
Guido van Rossum0c2290b1991-04-03 19:12:14 +00002133}
2134
Guido van Rossum541c8c01991-05-05 20:13:41 +00002135static object *
2136stdwin_getscrsize(self, args)
2137 object *self;
2138 object *args;
2139{
2140 int width, height;
2141 if (!getnoarg(args))
2142 return NULL;
2143 wgetscrsize(&width, &height);
2144 return makepoint(width, height);
2145}
2146
2147static object *
2148stdwin_getscrmm(self, args)
2149 object *self;
2150 object *args;
2151{
2152 int width, height;
2153 if (!getnoarg(args))
2154 return NULL;
2155 wgetscrmm(&width, &height);
2156 return makepoint(width, height);
2157}
2158
Guido van Rossumed233a51992-06-23 09:07:03 +00002159#ifdef unix
2160static object *
2161stdwin_connectionnumber(self, args)
2162 object *self;
2163 object *args;
2164{
2165 if (!getnoarg(args))
2166 return NULL;
2167 return newintobject((long) wconnectionnumber());
2168}
2169#endif
2170
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002171static struct methodlist stdwin_methods[] = {
2172 {"askfile", stdwin_askfile},
2173 {"askstr", stdwin_askstr},
2174 {"askync", stdwin_askync},
Guido van Rossum27201061991-04-16 08:43:03 +00002175 {"fetchcolor", stdwin_fetchcolor},
Guido van Rossumed233a51992-06-23 09:07:03 +00002176#ifdef unix
2177 {"fileno", stdwin_connectionnumber},
2178 {"connectionnumber", stdwin_connectionnumber},
2179#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002180 {"fleep", stdwin_fleep},
Guido van Rossum246b9d81991-06-03 10:55:14 +00002181 {"getactive", stdwin_getactive},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002182 {"getcutbuffer", stdwin_getcutbuffer},
Guido van Rossum0c2290b1991-04-03 19:12:14 +00002183 {"getdefscrollbars", stdwin_getdefscrollbars},
Guido van Rossum33f17701991-02-13 23:19:39 +00002184 {"getdefwinpos", stdwin_getdefwinpos},
2185 {"getdefwinsize", stdwin_getdefwinsize},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002186 {"getevent", stdwin_getevent},
Guido van Rossum541c8c01991-05-05 20:13:41 +00002187 {"getscrmm", stdwin_getscrmm},
2188 {"getscrsize", stdwin_getscrsize},
Guido van Rossum27201061991-04-16 08:43:03 +00002189 {"getselection", stdwin_getselection},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002190 {"menucreate", stdwin_menucreate},
2191 {"message", stdwin_message},
2192 {"open", stdwin_open},
Guido van Rossume8e7cf41991-01-16 14:06:18 +00002193 {"pollevent", stdwin_pollevent},
Guido van Rossum5b10f451990-10-30 16:01:48 +00002194 {"resetselection", stdwin_resetselection},
2195 {"rotatecutbuffers", stdwin_rotatecutbuffers},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002196 {"setcutbuffer", stdwin_setcutbuffer},
Guido van Rossum0c2290b1991-04-03 19:12:14 +00002197 {"setdefscrollbars", stdwin_setdefscrollbars},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002198 {"setdefwinpos", stdwin_setdefwinpos},
2199 {"setdefwinsize", stdwin_setdefwinsize},
2200
2201 /* Text measuring methods borrow code from drawing objects: */
2202 {"baseline", drawing_baseline},
2203 {"lineheight", drawing_lineheight},
2204 {"textbreak", drawing_textbreak},
2205 {"textwidth", drawing_textwidth},
Guido van Rossum0c2290b1991-04-03 19:12:14 +00002206
2207 /* Same for font setting methods: */
2208 {"setfont", drawing_setfont},
2209
2210 /* Same for color setting/getting methods: */
2211 {"getbgcolor", drawing_getbgcolor},
2212 {"getfgcolor", drawing_getfgcolor},
2213 {"setbgcolor", drawing_setbgcolor},
2214 {"setfgcolor", drawing_setfgcolor},
2215
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002216 {NULL, NULL} /* sentinel */
2217};
2218
2219void
2220initstdwin()
2221{
Guido van Rossumbbf94341991-12-16 15:44:53 +00002222 object *m, *d;
2223 static int inited = 0;
2224
Guido van Rossum2d14e211991-02-19 12:26:49 +00002225 if (!inited) {
2226 winit();
2227 inited = 1;
2228 }
Guido van Rossumbbf94341991-12-16 15:44:53 +00002229 m = initmodule("stdwin", stdwin_methods);
2230 d = getmoduledict(m);
2231
2232 /* Initialize stdwin.error exception */
2233 StdwinError = newstringobject("stdwin.error");
2234 if (StdwinError == NULL || dictinsert(d, "error", StdwinError) != 0)
2235 fatal("can't define stdwin.error");
Guido van Rossumff4949e1992-08-05 19:58:53 +00002236#ifdef USE_THREAD
2237 StdwinLock = allocate_lock();
2238 if (StdwinLock == NULL)
2239 fatal("can't allocate stdwin lock");
2240#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002241}