blob: b256695bb4d786467c40ed7717397c8d22fefeae [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
Guido van Rossum524b5881995-01-04 19:10:35 +00002Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
Guido van Rossumf70e43a1991-02-19 12:39:46 +00004
5 All Rights Reserved
6
7Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
9provided that the above copyright notice appear in all copies and that
10both that copyright notice and this permission notice appear in
11supporting documentation, and that the names of Stichting Mathematisch
12Centrum or CWI not be used in advertising or publicity pertaining to
13distribution of the software without specific, written prior permission.
14
15STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22
23******************************************************************/
24
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000025/* Stdwin module */
26
27/* Stdwin itself is a module, not a separate object type.
28 Object types defined here:
29 wp: a window
30 dp: a drawing structure (only one can exist at a time)
31 mp: a menu
32 tp: a textedit block
Guido van Rossumbf80e541993-02-08 15:49:17 +000033 bp: a bitmap
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000034*/
35
36/* Rules for translating C stdwin function calls into Python stwin:
37 - All names drop their initial letter 'w'
38 - Functions with a window as first parameter are methods of window objects
39 - There is no equivalent for wclose(); just delete the window object
40 (all references to it!) (XXX maybe this is a bad idea)
41 - w.begindrawing() returns a drawing object
42 - There is no equivalent for wenddrawing(win); just delete the drawing
43 object (all references to it!) (XXX maybe this is a bad idea)
44 - Functions that may only be used inside wbegindrawing / wendddrawing
45 are methods of the drawing object; this includes the text measurement
46 functions (which however have doubles as module functions).
47 - Methods of the drawing object drop an initial 'draw' from their name
48 if they have it, e.g., wdrawline() --> d.line()
49 - The obvious type conversions: int --> intobject; string --> stringobject
50 - A text parameter followed by a length parameter is only a text (string)
51 parameter in Python
52 - A point or other pair of horizontal and vertical coordinates is always
53 a pair of integers in Python
54 - Two points forming a rectangle or endpoints of a line segment are a
55 pair of points in Python
56 - The arguments to d.elarc() are three points.
57 - The functions wgetclip() and wsetclip() are translated into
58 stdwin.getcutbuffer() and stdwin.setcutbuffer(); 'clip' is really
59 a bad word for what these functions do (clipping has a different
60 meaning in the drawing world), while cutbuffer is standard X jargon.
Guido van Rossum01769f01990-10-30 13:39:00 +000061 XXX This must change again in the light of changes to stdwin!
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000062 - For textedit, similar rules hold, but they are less strict.
63 XXX more?
64*/
65
Guido van Rossum3f5da241990-12-20 15:06:42 +000066#include "allobjects.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000067#include "modsupport.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +000068#include "ceval.h"
Guido van Rossumcacd9571993-10-18 11:44:47 +000069#include "sysmodule.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000070
Guido van Rossum0b0db8e1993-01-21 16:07:51 +000071#ifdef macintosh
Guido van Rossume9066061993-07-29 13:14:32 +000072#include ":::stdwin:H:stdwin.h"
Guido van Rossum0b0db8e1993-01-21 16:07:51 +000073#else /* !macintosh */
Guido van Rossum3f5da241990-12-20 15:06:42 +000074#include "stdwin.h"
Guido van Rossume9066061993-07-29 13:14:32 +000075#define HAVE_BITMAPS
Guido van Rossum0b0db8e1993-01-21 16:07:51 +000076#endif /* !macintosh */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000077
Guido van Rossumb6775db1994-08-01 11:34:53 +000078#ifdef WITH_THREAD
Guido van Rossumff4949e1992-08-05 19:58:53 +000079
80#include "thread.h"
81
82static type_lock StdwinLock; /* Lock held when interpreter not locked */
83
84#define BGN_STDWIN BGN_SAVE acquire_lock(StdwinLock, 1);
85#define RET_STDWIN release_lock(StdwinLock); RET_SAVE
86#define END_STDWIN release_lock(StdwinLock); END_SAVE
87
88#else
89
90#define BGN_STDWIN BGN_SAVE
91#define RET_STDWIN RET_SAVE
92#define END_STDWIN END_SAVE
93
94#endif
95
Guido van Rossum234f9421993-06-17 12:35:49 +000096#define getpointarg(v, a) getargs(v, "(ii)", a, (a)+1)
97#define get3pointarg(v, a) getargs(v, "((ii)(ii)(ii))", \
98 a, a+1, a+2, a+3, a+4, a+5)
99#define getrectarg(v, a) getargs(v, "((ii)(ii))", a, a+1, a+2, a+3)
100#define getrectintarg(v, a) getargs(v, "(((ii)(ii))i)", a, a+1, a+2, a+3, a+4)
101#define getpointintarg(v, a) getargs(v, "((ii)i)", a, a+1, a+2)
102#define getrectpointarg(v, a) getargs(v, "(((ii)(ii))(ii))", \
103 a, a+1, a+2, a+3, a+4, a+5)
104
Guido van Rossumbbf94341991-12-16 15:44:53 +0000105static object *StdwinError; /* Exception stdwin.error */
Guido van Rossum87e7ea71991-12-10 14:00:03 +0000106
Jack Jansend56c1091995-01-27 14:44:16 +0000107#ifdef macintosh
108#include "macglue.h"
109#endif
Jack Jansen3d7f6bd1995-01-26 16:40:10 +0000110
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000111/* Window and menu object types declared here because of forward references */
112
113typedef struct {
114 OB_HEAD
115 object *w_title;
116 WINDOW *w_win;
117 object *w_attr; /* Attributes dictionary */
118} windowobject;
119
Guido van Rossumb6775db1994-08-01 11:34:53 +0000120staticforward typeobject Windowtype;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000121
122#define is_windowobject(wp) ((wp)->ob_type == &Windowtype)
123
124typedef struct {
125 OB_HEAD
126 MENU *m_menu;
127 int m_id;
128 object *m_attr; /* Attributes dictionary */
129} menuobject;
130
Guido van Rossumb6775db1994-08-01 11:34:53 +0000131staticforward typeobject Menutype;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000132
133#define is_menuobject(mp) ((mp)->ob_type == &Menutype)
134
Guido van Rossumbf80e541993-02-08 15:49:17 +0000135typedef struct {
136 OB_HEAD
137 BITMAP *b_bitmap;
138 object *b_attr; /* Attributes dictionary */
139} bitmapobject;
140
Guido van Rossumb6775db1994-08-01 11:34:53 +0000141staticforward typeobject Bitmaptype;
Guido van Rossumbf80e541993-02-08 15:49:17 +0000142
143#define is_bitmapobject(mp) ((mp)->ob_type == &Bitmaptype)
144
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000145
146/* Strongly stdwin-specific argument handlers */
147
148static int
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000149getmenudetail(v, ep)
150 object *v;
151 EVENT *ep;
152{
Guido van Rossumfc58e581992-01-27 16:45:55 +0000153 menuobject *mp;
154 if (!getargs(v, "(Oi)", &mp, &ep->u.m.item))
155 return 0;
156 if (!is_menuobject(mp))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000157 return err_badarg();
Guido van Rossumfc58e581992-01-27 16:45:55 +0000158 ep->u.m.id = mp->m_id;
159 return 1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000160}
161
162static int
163geteventarg(v, ep)
164 object *v;
165 EVENT *ep;
166{
167 object *wp, *detail;
168 int a[4];
Guido van Rossumfc58e581992-01-27 16:45:55 +0000169 if (!getargs(v, "(iOO)", &ep->type, &wp, &detail))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000170 return 0;
Guido van Rossumfc58e581992-01-27 16:45:55 +0000171 if (is_windowobject(wp))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000172 ep->window = ((windowobject *)wp) -> w_win;
Guido van Rossumfc58e581992-01-27 16:45:55 +0000173 else if (wp == None)
174 ep->window = NULL;
175 else
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000176 return err_badarg();
Guido van Rossumfc58e581992-01-27 16:45:55 +0000177 switch (ep->type) {
178 case WE_CHAR: {
179 char c;
180 if (!getargs(detail, "c", &c))
181 return 0;
182 ep->u.character = c;
183 return 1;
184 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000185 case WE_COMMAND:
186 return getintarg(detail, &ep->u.command);
187 case WE_DRAW:
188 if (!getrectarg(detail, a))
189 return 0;
190 ep->u.area.left = a[0];
191 ep->u.area.top = a[1];
192 ep->u.area.right = a[2];
193 ep->u.area.bottom = a[3];
194 return 1;
195 case WE_MOUSE_DOWN:
196 case WE_MOUSE_UP:
197 case WE_MOUSE_MOVE:
Guido van Rossumfc58e581992-01-27 16:45:55 +0000198 return getargs(detail, "((ii)iii)",
199 &ep->u.where.h, &ep->u.where.v,
200 &ep->u.where.clicks,
201 &ep->u.where.button,
202 &ep->u.where.mask);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000203 case WE_MENU:
204 return getmenudetail(detail, ep);
Guido van Rossum3ee199e1992-06-30 12:48:26 +0000205 case WE_KEY:
206 return getargs(detail, "(ii)",
207 &ep->u.key.code, &ep->u.key.mask);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000208 default:
209 return 1;
210 }
211}
212
213
214/* Return construction tools */
215
216static object *
217makepoint(a, b)
218 int a, b;
219{
Guido van Rossum2ee12f41992-04-13 15:54:35 +0000220 return mkvalue("(ii)", a, b);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000221}
222
223static object *
224makerect(a, b, c, d)
225 int a, b, c, d;
226{
Guido van Rossum2ee12f41992-04-13 15:54:35 +0000227 return mkvalue("((ii)(ii))", a, b, c, d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000228}
229
230
231/* Drawing objects */
232
233typedef struct {
234 OB_HEAD
235 windowobject *d_ref;
236} drawingobject;
237
238static drawingobject *Drawing; /* Set to current drawing object, or NULL */
239
240/* Drawing methods */
241
Guido van Rossum3c284741991-11-27 14:54:54 +0000242static object *
243drawing_close(dp)
244 drawingobject *dp;
245{
246 if (dp->d_ref != NULL) {
247 wenddrawing(dp->d_ref->w_win);
248 Drawing = NULL;
249 DECREF(dp->d_ref);
250 dp->d_ref = NULL;
251 }
252 INCREF(None);
253 return None;
254}
Guido van Rossum77b46041992-01-14 18:41:24 +0000255
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000256static void
257drawing_dealloc(dp)
258 drawingobject *dp;
259{
Guido van Rossum3c284741991-11-27 14:54:54 +0000260 if (dp->d_ref != NULL) {
261 wenddrawing(dp->d_ref->w_win);
262 Drawing = NULL;
263 DECREF(dp->d_ref);
264 dp->d_ref = NULL;
265 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000266 free((char *)dp);
267}
268
269static object *
270drawing_generic(dp, args, func)
271 drawingobject *dp;
272 object *args;
273 void (*func) FPROTO((int, int, int, int));
274{
275 int a[4];
276 if (!getrectarg(args, a))
277 return NULL;
278 (*func)(a[0], a[1], a[2], a[3]);
279 INCREF(None);
280 return None;
281}
282
283static object *
284drawing_line(dp, args)
285 drawingobject *dp;
286 object *args;
287{
Guido van Rossumbf109731991-03-06 13:14:12 +0000288 return drawing_generic(dp, args, wdrawline);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000289}
290
291static object *
292drawing_xorline(dp, args)
293 drawingobject *dp;
294 object *args;
295{
Guido van Rossumbf109731991-03-06 13:14:12 +0000296 return drawing_generic(dp, args, wxorline);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000297}
298
299static object *
300drawing_circle(dp, args)
301 drawingobject *dp;
302 object *args;
303{
304 int a[3];
305 if (!getpointintarg(args, a))
306 return NULL;
307 wdrawcircle(a[0], a[1], a[2]);
308 INCREF(None);
309 return None;
310}
Guido van Rossuma2a181a1991-05-14 12:09:25 +0000311
Guido van Rossum27201061991-04-16 08:43:03 +0000312static object *
313drawing_fillcircle(dp, args)
314 drawingobject *dp;
315 object *args;
316{
317 int a[3];
318 if (!getpointintarg(args, a))
319 return NULL;
320 wfillcircle(a[0], a[1], a[2]);
321 INCREF(None);
322 return None;
323}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000324
325static object *
Guido van Rossuma2a181a1991-05-14 12:09:25 +0000326drawing_xorcircle(dp, args)
327 drawingobject *dp;
328 object *args;
329{
330 int a[3];
331 if (!getpointintarg(args, a))
332 return NULL;
333 wxorcircle(a[0], a[1], a[2]);
334 INCREF(None);
335 return None;
336}
337
338static object *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000339drawing_elarc(dp, args)
340 drawingobject *dp;
341 object *args;
342{
343 int a[6];
344 if (!get3pointarg(args, a))
345 return NULL;
346 wdrawelarc(a[0], a[1], a[2], a[3], a[4], a[5]);
347 INCREF(None);
348 return None;
349}
350
351static object *
Guido van Rossum27201061991-04-16 08:43:03 +0000352drawing_fillelarc(dp, args)
353 drawingobject *dp;
354 object *args;
355{
356 int a[6];
357 if (!get3pointarg(args, a))
358 return NULL;
359 wfillelarc(a[0], a[1], a[2], a[3], a[4], a[5]);
360 INCREF(None);
361 return None;
362}
363
364static object *
Guido van Rossuma2a181a1991-05-14 12:09:25 +0000365drawing_xorelarc(dp, args)
366 drawingobject *dp;
367 object *args;
368{
369 int a[6];
370 if (!get3pointarg(args, a))
371 return NULL;
372 wxorelarc(a[0], a[1], a[2], a[3], a[4], a[5]);
373 INCREF(None);
374 return None;
375}
376
377static object *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000378drawing_box(dp, args)
379 drawingobject *dp;
380 object *args;
381{
Guido van Rossumbf109731991-03-06 13:14:12 +0000382 return drawing_generic(dp, args, wdrawbox);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000383}
384
385static object *
386drawing_erase(dp, args)
387 drawingobject *dp;
388 object *args;
389{
Guido van Rossumbf109731991-03-06 13:14:12 +0000390 return drawing_generic(dp, args, werase);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000391}
392
393static object *
394drawing_paint(dp, args)
395 drawingobject *dp;
396 object *args;
397{
Guido van Rossumbf109731991-03-06 13:14:12 +0000398 return drawing_generic(dp, args, wpaint);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000399}
400
401static object *
402drawing_invert(dp, args)
403 drawingobject *dp;
404 object *args;
405{
Guido van Rossumbf109731991-03-06 13:14:12 +0000406 return drawing_generic(dp, args, winvert);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000407}
408
Guido van Rossum27201061991-04-16 08:43:03 +0000409static POINT *
410getpointsarray(v, psize)
411 object *v;
412 int *psize;
413{
414 int n = -1;
415 object * (*getitem) PROTO((object *, int));
416 int i;
417 POINT *points;
418
419 if (v == NULL)
420 ;
421 else if (is_listobject(v)) {
422 n = getlistsize(v);
423 getitem = getlistitem;
424 }
425 else if (is_tupleobject(v)) {
426 n = gettuplesize(v);
427 getitem = gettupleitem;
428 }
429
430 if (n <= 0) {
431 (void) err_badarg();
432 return NULL;
433 }
434
435 points = NEW(POINT, n);
436 if (points == NULL) {
437 (void) err_nomem();
438 return NULL;
439 }
440
441 for (i = 0; i < n; i++) {
442 object *w = (*getitem)(v, i);
443 int a[2];
444 if (!getpointarg(w, a)) {
445 DEL(points);
446 return NULL;
447 }
448 points[i].h = a[0];
449 points[i].v = a[1];
450 }
451
452 *psize = n;
453 return points;
454}
455
456static object *
457drawing_poly(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 wdrawpoly(n, points);
466 DEL(points);
467 INCREF(None);
468 return None;
469}
470
471static object *
472drawing_fillpoly(dp, args)
473 drawingobject *dp;
474 object *args;
475{
476 int n;
477 POINT *points = getpointsarray(args, &n);
478 if (points == NULL)
479 return NULL;
480 wfillpoly(n, points);
481 DEL(points);
482 INCREF(None);
483 return None;
484}
485
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000486static object *
Guido van Rossuma2a181a1991-05-14 12:09:25 +0000487drawing_xorpoly(dp, args)
488 drawingobject *dp;
489 object *args;
490{
491 int n;
492 POINT *points = getpointsarray(args, &n);
493 if (points == NULL)
494 return NULL;
495 wxorpoly(n, points);
496 DEL(points);
497 INCREF(None);
498 return None;
499}
500
501static object *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000502drawing_cliprect(dp, args)
503 drawingobject *dp;
504 object *args;
505{
Guido van Rossumbf109731991-03-06 13:14:12 +0000506 return drawing_generic(dp, args, wcliprect);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000507}
508
509static object *
510drawing_noclip(dp, args)
511 drawingobject *dp;
512 object *args;
513{
514 if (!getnoarg(args))
515 return NULL;
516 wnoclip();
517 INCREF(None);
518 return None;
519}
520
521static object *
522drawing_shade(dp, args)
523 drawingobject *dp;
524 object *args;
525{
526 int a[5];
527 if (!getrectintarg(args, a))
528 return NULL;
529 wshade(a[0], a[1], a[2], a[3], a[4]);
530 INCREF(None);
531 return None;
532}
533
534static object *
535drawing_text(dp, args)
536 drawingobject *dp;
537 object *args;
538{
Guido van Rossumfc58e581992-01-27 16:45:55 +0000539 int h, v, size;
540 char *text;
541 if (!getargs(args, "((ii)s#)", &h, &v, &text, &size))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000542 return NULL;
Guido van Rossumfc58e581992-01-27 16:45:55 +0000543 wdrawtext(h, v, text, size);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000544 INCREF(None);
545 return None;
546}
547
548/* The following four are also used as stdwin functions */
549
550static object *
551drawing_lineheight(dp, args)
552 drawingobject *dp;
553 object *args;
554{
555 if (!getnoarg(args))
556 return NULL;
557 return newintobject((long)wlineheight());
558}
559
560static object *
561drawing_baseline(dp, args)
562 drawingobject *dp;
563 object *args;
564{
565 if (!getnoarg(args))
566 return NULL;
567 return newintobject((long)wbaseline());
568}
569
570static object *
571drawing_textwidth(dp, args)
572 drawingobject *dp;
573 object *args;
574{
Guido van Rossumfc58e581992-01-27 16:45:55 +0000575 char *text;
576 int size;
577 if (!getargs(args, "s#", &text, &size))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000578 return NULL;
Guido van Rossumfc58e581992-01-27 16:45:55 +0000579 return newintobject((long)wtextwidth(text, size));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000580}
581
582static object *
583drawing_textbreak(dp, args)
584 drawingobject *dp;
585 object *args;
586{
Guido van Rossumfc58e581992-01-27 16:45:55 +0000587 char *text;
588 int size, width;
589 if (!getargs(args, "(s#i)", &text, &size, &width))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000590 return NULL;
Guido van Rossumfc58e581992-01-27 16:45:55 +0000591 return newintobject((long)wtextbreak(text, size, width));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000592}
593
Guido van Rossum0c2290b1991-04-03 19:12:14 +0000594static object *
595drawing_setfont(self, args)
596 drawingobject *self;
597 object *args;
598{
Guido van Rossumfc58e581992-01-27 16:45:55 +0000599 char *font;
600 char style = '\0';
601 int size = 0;
602 if (args == NULL || !is_tupleobject(args)) {
Guido van Rossum3c8ba7a1992-02-05 11:15:00 +0000603 if (!getargs(args, "z", &font))
Guido van Rossum50429a11991-04-04 15:24:07 +0000604 return NULL;
605 }
606 else {
Guido van Rossumfc58e581992-01-27 16:45:55 +0000607 int n = gettuplesize(args);
608 if (n == 2) {
609 if (!getargs(args, "(zi)", &font, &size))
610 return NULL;
611 }
612 else if (!getargs(args, "(zic)", &font, &size, &style)) {
613 err_clear();
614 if (!getargs(args, "(zci)", &font, &style, &size))
615 return NULL;
Guido van Rossum50429a11991-04-04 15:24:07 +0000616 }
617 }
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000618 if (font != NULL) {
619 if (!wsetfont(font)) {
620 err_setstr(StdwinError, "font not found");
621 return NULL;
622 }
623 }
Guido van Rossum50429a11991-04-04 15:24:07 +0000624 if (size != 0)
625 wsetsize(size);
Guido van Rossumfc58e581992-01-27 16:45:55 +0000626 switch (style) {
627 case 'b':
628 wsetbold();
629 break;
630 case 'i':
631 wsetitalic();
632 break;
633 case 'o':
634 wsetbolditalic();
635 break;
636 case 'u':
637 wsetunderline();
638 break;
639 case 'p':
640 wsetplain();
641 break;
642 }
Guido van Rossum0c2290b1991-04-03 19:12:14 +0000643 INCREF(None);
644 return None;
645}
646
647static object *
648drawing_getbgcolor(self, args)
649 object *self;
650 object *args;
651{
652 if (!getnoarg(args))
653 return NULL;
654 return newintobject((long)wgetbgcolor());
655}
656
657static object *
658drawing_getfgcolor(self, args)
659 object *self;
660 object *args;
661{
662 if (!getnoarg(args))
663 return NULL;
664 return newintobject((long)wgetfgcolor());
665}
666
667static object *
668drawing_setbgcolor(self, args)
669 object *self;
670 object *args;
671{
672 long color;
673 if (!getlongarg(args, &color))
674 return NULL;
675 wsetbgcolor((COLOR)color);
676 INCREF(None);
677 return None;
678}
679
680static object *
681drawing_setfgcolor(self, args)
682 object *self;
683 object *args;
684{
685 long color;
686 if (!getlongarg(args, &color))
687 return NULL;
688 wsetfgcolor((COLOR)color);
689 INCREF(None);
690 return None;
691}
692
Guido van Rossume9066061993-07-29 13:14:32 +0000693#ifdef HAVE_BITMAPS
694
Guido van Rossumbf80e541993-02-08 15:49:17 +0000695static object *
696drawing_bitmap(self, args)
697 object *self;
698 object *args;
699{
700 int h, v;
701 object *bp;
702 object *mask = NULL;
703 if (!getargs(args, "((ii)O)", &h, &v, &bp)) {
704 err_clear();
705 if (!getargs(args, "((ii)OO)", &h, &v, &bp, &mask))
706 return NULL;
707 if (mask == None)
708 mask = NULL;
709 else if (!is_bitmapobject(mask)) {
710 err_badarg();
711 return NULL;
712 }
713 }
714 if (!is_bitmapobject(bp)) {
715 err_badarg();
716 return NULL;
717 }
718 if (((bitmapobject *)bp)->b_bitmap == NULL ||
719 mask != NULL && ((bitmapobject *)mask)->b_bitmap == NULL) {
720 err_setstr(StdwinError, "bitmap object already close");
721 return NULL;
722 }
723 if (mask == NULL)
724 wdrawbitmap(h, v, ((bitmapobject *)bp)->b_bitmap, ALLBITS);
725 else
726 wdrawbitmap(h, v,
727 ((bitmapobject *)bp)->b_bitmap,
728 ((bitmapobject *)bp)->b_bitmap);
729 INCREF(None);
730 return None;
731}
732
Guido van Rossume9066061993-07-29 13:14:32 +0000733#endif /* HAVE_BITMAPS */
734
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000735static struct methodlist drawing_methods[] = {
Guido van Rossume9066061993-07-29 13:14:32 +0000736#ifdef HAVE_BITMAPS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000737 {"bitmap", (method)drawing_bitmap},
Guido van Rossume9066061993-07-29 13:14:32 +0000738#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000739 {"box", (method)drawing_box},
740 {"circle", (method)drawing_circle},
741 {"cliprect", (method)drawing_cliprect},
742 {"close", (method)drawing_close},
743 {"elarc", (method)drawing_elarc},
744 {"enddrawing", (method)drawing_close},
745 {"erase", (method)drawing_erase},
746 {"fillcircle", (method)drawing_fillcircle},
747 {"fillelarc", (method)drawing_fillelarc},
748 {"fillpoly", (method)drawing_fillpoly},
749 {"invert", (method)drawing_invert},
750 {"line", (method)drawing_line},
751 {"noclip", (method)drawing_noclip},
752 {"paint", (method)drawing_paint},
753 {"poly", (method)drawing_poly},
754 {"shade", (method)drawing_shade},
755 {"text", (method)drawing_text},
756 {"xorcircle", (method)drawing_xorcircle},
757 {"xorelarc", (method)drawing_xorelarc},
758 {"xorline", (method)drawing_xorline},
759 {"xorpoly", (method)drawing_xorpoly},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000760
761 /* Text measuring methods: */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000762 {"baseline", (method)drawing_baseline},
763 {"lineheight", (method)drawing_lineheight},
764 {"textbreak", (method)drawing_textbreak},
765 {"textwidth", (method)drawing_textwidth},
Guido van Rossum0c2290b1991-04-03 19:12:14 +0000766
767 /* Font setting methods: */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000768 {"setfont", (method)drawing_setfont},
Guido van Rossum0c2290b1991-04-03 19:12:14 +0000769
770 /* Color methods: */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000771 {"getbgcolor", (method)drawing_getbgcolor},
772 {"getfgcolor", (method)drawing_getfgcolor},
773 {"setbgcolor", (method)drawing_setbgcolor},
774 {"setfgcolor", (method)drawing_setfgcolor},
Guido van Rossum0c2290b1991-04-03 19:12:14 +0000775
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000776 {NULL, NULL} /* sentinel */
777};
778
779static object *
Guido van Rossum77b46041992-01-14 18:41:24 +0000780drawing_getattr(dp, name)
781 drawingobject *dp;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000782 char *name;
783{
Guido van Rossum77b46041992-01-14 18:41:24 +0000784 if (dp->d_ref == NULL) {
785 err_setstr(StdwinError, "drawing object already closed");
786 return NULL;
787 }
788 return findmethod(drawing_methods, (object *)dp, name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000789}
790
Guido van Rossum541c8c01991-05-05 20:13:41 +0000791typeobject Drawingtype = {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000792 OB_HEAD_INIT(&Typetype)
793 0, /*ob_size*/
794 "drawing", /*tp_name*/
795 sizeof(drawingobject), /*tp_size*/
796 0, /*tp_itemsize*/
797 /* methods */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000798 (destructor)drawing_dealloc, /*tp_dealloc*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000799 0, /*tp_print*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000800 (getattrfunc)drawing_getattr, /*tp_getattr*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000801 0, /*tp_setattr*/
802 0, /*tp_compare*/
803 0, /*tp_repr*/
804};
805
806
807/* Text(edit) objects */
808
809typedef struct {
810 OB_HEAD
811 TEXTEDIT *t_text;
812 windowobject *t_ref;
813 object *t_attr; /* Attributes dictionary */
814} textobject;
815
Guido van Rossumb6775db1994-08-01 11:34:53 +0000816staticforward typeobject Texttype;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000817
818static textobject *
819newtextobject(wp, left, top, right, bottom)
820 windowobject *wp;
821 int left, top, right, bottom;
822{
823 textobject *tp;
824 tp = NEWOBJ(textobject, &Texttype);
825 if (tp == NULL)
826 return NULL;
827 tp->t_attr = NULL;
828 INCREF(wp);
829 tp->t_ref = wp;
830 tp->t_text = tecreate(wp->w_win, left, top, right, bottom);
831 if (tp->t_text == NULL) {
832 DECREF(tp);
833 return (textobject *) err_nomem();
834 }
835 return tp;
836}
837
838/* Text(edit) methods */
839
840static void
841text_dealloc(tp)
842 textobject *tp;
843{
844 if (tp->t_text != NULL)
845 tefree(tp->t_text);
Guido van Rossum3c284741991-11-27 14:54:54 +0000846 XDECREF(tp->t_attr);
847 XDECREF(tp->t_ref);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000848 DEL(tp);
849}
850
851static object *
Guido van Rossum3c284741991-11-27 14:54:54 +0000852text_close(tp, args)
853 textobject *tp;
854 object *args;
855{
856 if (tp->t_text != NULL) {
857 tefree(tp->t_text);
858 tp->t_text = NULL;
859 }
860 if (tp->t_attr != NULL) {
861 DECREF(tp->t_attr);
862 tp->t_attr = NULL;
863 }
864 if (tp->t_ref != NULL) {
865 DECREF(tp->t_ref);
866 tp->t_ref = NULL;
867 }
868 INCREF(None);
869 return None;
870}
871
872static object *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000873text_arrow(self, args)
874 textobject *self;
875 object *args;
876{
877 int code;
878 if (!getintarg(args, &code))
879 return NULL;
880 tearrow(self->t_text, code);
881 INCREF(None);
882 return None;
883}
884
885static object *
886text_draw(self, args)
887 textobject *self;
888 object *args;
889{
890 register TEXTEDIT *tp = self->t_text;
891 int a[4];
892 int left, top, right, bottom;
893 if (!getrectarg(args, a))
894 return NULL;
895 if (Drawing != NULL) {
Guido van Rossum87e7ea71991-12-10 14:00:03 +0000896 err_setstr(StdwinError, "already drawing");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000897 return NULL;
898 }
899 /* Clip to text area and ignore if area is empty */
900 left = tegetleft(tp);
901 top = tegettop(tp);
902 right = tegetright(tp);
903 bottom = tegetbottom(tp);
904 if (a[0] < left) a[0] = left;
905 if (a[1] < top) a[1] = top;
906 if (a[2] > right) a[2] = right;
907 if (a[3] > bottom) a[3] = bottom;
908 if (a[0] < a[2] && a[1] < a[3]) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000909 wbegindrawing(self->t_ref->w_win);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000910 tedrawnew(tp, a[0], a[1], a[2], a[3]);
911 wenddrawing(self->t_ref->w_win);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000912 }
913 INCREF(None);
914 return None;
915}
916
917static object *
918text_event(self, args)
919 textobject *self;
920 object *args;
921{
922 register TEXTEDIT *tp = self->t_text;
923 EVENT e;
924 if (!geteventarg(args, &e))
925 return NULL;
926 if (e.type == WE_MOUSE_DOWN) {
Guido van Rossum33f17701991-02-13 23:19:39 +0000927 /* Cheat at the margins */
928 int width, height;
929 wgetdocsize(e.window, &width, &height);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000930 if (e.u.where.h < 0 && tegetleft(tp) == 0)
931 e.u.where.h = 0;
Guido van Rossum33f17701991-02-13 23:19:39 +0000932 else if (e.u.where.h > width && tegetright(tp) == width)
933 e.u.where.h = width;
934 if (e.u.where.v < 0 && tegettop(tp) == 0)
935 e.u.where.v = 0;
936 else if (e.u.where.v > height && tegetright(tp) == height)
937 e.u.where.v = height;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000938 }
939 return newintobject((long) teevent(tp, &e));
940}
941
942static object *
943text_getfocus(self, args)
944 textobject *self;
945 object *args;
946{
947 if (!getnoarg(args))
948 return NULL;
949 return makepoint(tegetfoc1(self->t_text), tegetfoc2(self->t_text));
950}
951
952static object *
953text_getfocustext(self, args)
954 textobject *self;
955 object *args;
956{
957 int f1, f2;
958 char *text;
959 if (!getnoarg(args))
960 return NULL;
961 f1 = tegetfoc1(self->t_text);
962 f2 = tegetfoc2(self->t_text);
963 text = tegettext(self->t_text);
964 return newsizedstringobject(text + f1, f2-f1);
965}
966
967static object *
968text_getrect(self, args)
969 textobject *self;
970 object *args;
971{
972 if (!getnoarg(args))
973 return NULL;
974 return makerect(tegetleft(self->t_text),
975 tegettop(self->t_text),
976 tegetright(self->t_text),
977 tegetbottom(self->t_text));
978}
979
980static object *
981text_gettext(self, args)
982 textobject *self;
983 object *args;
984{
985 if (!getnoarg(args))
986 return NULL;
987 return newsizedstringobject(tegettext(self->t_text),
988 tegetlen(self->t_text));
989}
990
991static object *
992text_move(self, args)
993 textobject *self;
994 object *args;
995{
996 int a[4];
997 if (!getrectarg(args, a))
998 return NULL;
999 temovenew(self->t_text, a[0], a[1], a[2], a[3]);
1000 INCREF(None);
1001 return None;
1002}
1003
1004static object *
Guido van Rossum4b9cf8e1991-05-28 21:57:04 +00001005text_replace(self, args)
1006 textobject *self;
1007 object *args;
1008{
Guido van Rossumfc58e581992-01-27 16:45:55 +00001009 char *text;
Guido van Rossum4b9cf8e1991-05-28 21:57:04 +00001010 if (!getstrarg(args, &text))
1011 return NULL;
Guido van Rossumfc58e581992-01-27 16:45:55 +00001012 tereplace(self->t_text, text);
Guido van Rossum4b9cf8e1991-05-28 21:57:04 +00001013 INCREF(None);
1014 return None;
1015}
1016
1017static object *
1018text_setactive(self, args)
1019 textobject *self;
1020 object *args;
1021{
1022 int flag;
1023 if (!getintarg(args, &flag))
1024 return NULL;
1025 tesetactive(self->t_text, flag);
1026 INCREF(None);
1027 return None;
1028}
1029
1030static object *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001031text_setfocus(self, args)
1032 textobject *self;
1033 object *args;
1034{
1035 int a[2];
1036 if (!getpointarg(args, a))
1037 return NULL;
1038 tesetfocus(self->t_text, a[0], a[1]);
1039 INCREF(None);
1040 return None;
1041}
1042
1043static object *
Guido van Rossum541c8c01991-05-05 20:13:41 +00001044text_settext(self, args)
1045 textobject *self;
1046 object *args;
1047{
Guido van Rossumfc58e581992-01-27 16:45:55 +00001048 char *text;
Guido van Rossum541c8c01991-05-05 20:13:41 +00001049 char *buf;
1050 int size;
Guido van Rossumfc58e581992-01-27 16:45:55 +00001051 if (!getargs(args, "s#", &text, &size))
Guido van Rossum541c8c01991-05-05 20:13:41 +00001052 return NULL;
Guido van Rossum541c8c01991-05-05 20:13:41 +00001053 if ((buf = NEW(char, size)) == NULL) {
Guido van Rossum87e7ea71991-12-10 14:00:03 +00001054 return err_nomem();
Guido van Rossum541c8c01991-05-05 20:13:41 +00001055 }
Guido van Rossumfc58e581992-01-27 16:45:55 +00001056 memcpy(buf, text, size);
Guido van Rossum541c8c01991-05-05 20:13:41 +00001057 tesetbuf(self->t_text, buf, size); /* Becomes owner of buffer */
1058 INCREF(None);
1059 return None;
1060}
1061
1062static object *
Guido van Rossum4b9cf8e1991-05-28 21:57:04 +00001063text_setview(self, args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001064 textobject *self;
1065 object *args;
1066{
Guido van Rossum4b9cf8e1991-05-28 21:57:04 +00001067 int a[4];
1068 if (args == None)
1069 tenoview(self->t_text);
1070 else {
1071 if (!getrectarg(args, a))
1072 return NULL;
1073 tesetview(self->t_text, a[0], a[1], a[2], a[3]);
1074 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001075 INCREF(None);
1076 return None;
1077}
1078
1079static struct methodlist text_methods[] = {
Guido van Rossumb6775db1994-08-01 11:34:53 +00001080 {"arrow", (method)text_arrow},
1081 {"close", (method)text_close},
1082 {"draw", (method)text_draw},
1083 {"event", (method)text_event},
1084 {"getfocus", (method)text_getfocus},
1085 {"getfocustext",(method)text_getfocustext},
1086 {"getrect", (method)text_getrect},
1087 {"gettext", (method)text_gettext},
1088 {"move", (method)text_move},
1089 {"replace", (method)text_replace},
1090 {"setactive", (method)text_setactive},
1091 {"setfocus", (method)text_setfocus},
1092 {"settext", (method)text_settext},
1093 {"setview", (method)text_setview},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001094 {NULL, NULL} /* sentinel */
1095};
1096
1097static object *
1098text_getattr(tp, name)
1099 textobject *tp;
1100 char *name;
1101{
Guido van Rossum85f50761991-10-20 20:22:50 +00001102 object *v = NULL;
Guido van Rossum77b46041992-01-14 18:41:24 +00001103 if (tp->t_ref == NULL) {
1104 err_setstr(StdwinError, "text object already closed");
1105 return NULL;
1106 }
Guido van Rossum85f50761991-10-20 20:22:50 +00001107 if (strcmp(name, "__dict__") == 0) {
1108 v = tp->t_attr;
1109 if (v == NULL)
1110 v = None;
1111 }
1112 else if (tp->t_attr != NULL) {
1113 v = dictlookup(tp->t_attr, name);
1114 }
1115 if (v != NULL) {
1116 INCREF(v);
1117 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001118 }
1119 return findmethod(text_methods, (object *)tp, name);
1120}
1121
1122static int
1123text_setattr(tp, name, v)
1124 textobject *tp;
1125 char *name;
1126 object *v;
1127{
1128 if (tp->t_attr == NULL) {
1129 tp->t_attr = newdictobject();
1130 if (tp->t_attr == NULL)
1131 return -1;
1132 }
Guido van Rossum94472a01992-09-04 09:45:18 +00001133 if (v == NULL) {
1134 int rv = dictremove(tp->t_attr, name);
1135 if (rv < 0)
1136 err_setstr(AttributeError,
1137 "delete non-existing text object attribute");
1138 return rv;
1139 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001140 else
1141 return dictinsert(tp->t_attr, name, v);
1142}
1143
Guido van Rossumb6775db1994-08-01 11:34:53 +00001144static typeobject Texttype = {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001145 OB_HEAD_INIT(&Typetype)
1146 0, /*ob_size*/
1147 "textedit", /*tp_name*/
1148 sizeof(textobject), /*tp_size*/
1149 0, /*tp_itemsize*/
1150 /* methods */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001151 (destructor)text_dealloc, /*tp_dealloc*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001152 0, /*tp_print*/
Guido van Rossumb6775db1994-08-01 11:34:53 +00001153 (getattrfunc)text_getattr, /*tp_getattr*/
1154 (setattrfunc)text_setattr, /*tp_setattr*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001155 0, /*tp_compare*/
1156 0, /*tp_repr*/
1157};
1158
1159
1160/* Menu objects */
1161
Guido van Rossum2d14e211991-02-19 12:26:49 +00001162#define IDOFFSET 10 /* Menu IDs we use start here */
Guido van Rossum27201061991-04-16 08:43:03 +00001163#define MAXNMENU 200 /* Max #menus we allow */
Guido van Rossum2d14e211991-02-19 12:26:49 +00001164static menuobject *menulist[MAXNMENU];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001165
Guido van Rossumfc58e581992-01-27 16:45:55 +00001166static menuobject *newmenuobject PROTO((char *));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001167static menuobject *
1168newmenuobject(title)
Guido van Rossumfc58e581992-01-27 16:45:55 +00001169 char *title;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001170{
1171 int id;
1172 MENU *menu;
1173 menuobject *mp;
Guido van Rossum2d14e211991-02-19 12:26:49 +00001174 for (id = 0; id < MAXNMENU; id++) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001175 if (menulist[id] == NULL)
1176 break;
1177 }
Guido van Rossum27201061991-04-16 08:43:03 +00001178 if (id >= MAXNMENU) {
Guido van Rossum87e7ea71991-12-10 14:00:03 +00001179 err_setstr(StdwinError, "creating too many menus");
Guido van Rossum27201061991-04-16 08:43:03 +00001180 return NULL;
1181 }
Guido van Rossumfc58e581992-01-27 16:45:55 +00001182 menu = wmenucreate(id + IDOFFSET, title);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001183 if (menu == NULL)
1184 return (menuobject *) err_nomem();
1185 mp = NEWOBJ(menuobject, &Menutype);
1186 if (mp != NULL) {
1187 mp->m_menu = menu;
Guido van Rossum2d14e211991-02-19 12:26:49 +00001188 mp->m_id = id + IDOFFSET;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001189 mp->m_attr = NULL;
1190 menulist[id] = mp;
1191 }
1192 else
1193 wmenudelete(menu);
1194 return mp;
1195}
1196
1197/* Menu methods */
1198
1199static void
1200menu_dealloc(mp)
1201 menuobject *mp;
1202{
1203
Guido van Rossum2d14e211991-02-19 12:26:49 +00001204 int id = mp->m_id - IDOFFSET;
1205 if (id >= 0 && id < MAXNMENU && menulist[id] == mp) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001206 menulist[id] = NULL;
1207 }
Guido van Rossum77b46041992-01-14 18:41:24 +00001208 if (mp->m_menu != NULL)
1209 wmenudelete(mp->m_menu);
1210 XDECREF(mp->m_attr);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001211 DEL(mp);
1212}
1213
1214static object *
Guido van Rossum77b46041992-01-14 18:41:24 +00001215menu_close(mp, args)
1216 menuobject *mp;
1217 object *args;
1218{
1219 int id = mp->m_id - IDOFFSET;
1220 if (id >= 0 && id < MAXNMENU && menulist[id] == mp) {
1221 menulist[id] = NULL;
1222 }
1223 mp->m_id = -1;
1224 if (mp->m_menu != NULL)
1225 wmenudelete(mp->m_menu);
1226 mp->m_menu = NULL;
1227 XDECREF(mp->m_attr);
1228 mp->m_attr = NULL;
1229 INCREF(None);
1230 return None;
1231}
1232
1233static object *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001234menu_additem(self, args)
1235 menuobject *self;
1236 object *args;
1237{
Guido van Rossumfc58e581992-01-27 16:45:55 +00001238 char *text;
1239 int shortcut = -1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001240 if (is_tupleobject(args)) {
Guido van Rossumfc58e581992-01-27 16:45:55 +00001241 char c;
1242 if (!getargs(args, "(sc)", &text, &c))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001243 return NULL;
Guido van Rossumfc58e581992-01-27 16:45:55 +00001244 shortcut = c;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001245 }
Guido van Rossumfc58e581992-01-27 16:45:55 +00001246 else if (!getstrarg(args, &text))
1247 return NULL;
1248 wmenuadditem(self->m_menu, text, shortcut);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001249 INCREF(None);
1250 return None;
1251}
1252
1253static object *
1254menu_setitem(self, args)
1255 menuobject *self;
1256 object *args;
1257{
1258 int index;
Guido van Rossumfc58e581992-01-27 16:45:55 +00001259 char *text;
Guido van Rossum234f9421993-06-17 12:35:49 +00001260 if (!getargs(args, "(is)", &index, &text))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001261 return NULL;
Guido van Rossumfc58e581992-01-27 16:45:55 +00001262 wmenusetitem(self->m_menu, index, text);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001263 INCREF(None);
1264 return None;
1265}
1266
1267static object *
1268menu_enable(self, args)
1269 menuobject *self;
1270 object *args;
1271{
1272 int index;
1273 int flag;
Guido van Rossum234f9421993-06-17 12:35:49 +00001274 if (!getargs(args, "(ii)", &index, &flag))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001275 return NULL;
1276 wmenuenable(self->m_menu, index, flag);
1277 INCREF(None);
1278 return None;
1279}
1280
1281static object *
1282menu_check(self, args)
1283 menuobject *self;
1284 object *args;
1285{
1286 int index;
1287 int flag;
Guido van Rossum234f9421993-06-17 12:35:49 +00001288 if (!getargs(args, "(ii)", &index, &flag))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001289 return NULL;
1290 wmenucheck(self->m_menu, index, flag);
1291 INCREF(None);
1292 return None;
1293}
1294
1295static struct methodlist menu_methods[] = {
Guido van Rossumb6775db1994-08-01 11:34:53 +00001296 {"additem", (method)menu_additem},
1297 {"setitem", (method)menu_setitem},
1298 {"enable", (method)menu_enable},
1299 {"check", (method)menu_check},
1300 {"close", (method)menu_close},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001301 {NULL, NULL} /* sentinel */
1302};
1303
1304static object *
1305menu_getattr(mp, name)
1306 menuobject *mp;
1307 char *name;
1308{
Guido van Rossum85f50761991-10-20 20:22:50 +00001309 object *v = NULL;
Guido van Rossum77b46041992-01-14 18:41:24 +00001310 if (mp->m_menu == NULL) {
1311 err_setstr(StdwinError, "menu object already closed");
1312 return NULL;
1313 }
Guido van Rossum85f50761991-10-20 20:22:50 +00001314 if (strcmp(name, "__dict__") == 0) {
1315 v = mp->m_attr;
1316 if (v == NULL)
1317 v = None;
1318 }
1319 else if (mp->m_attr != NULL) {
1320 v = dictlookup(mp->m_attr, name);
1321 }
1322 if (v != NULL) {
1323 INCREF(v);
1324 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001325 }
1326 return findmethod(menu_methods, (object *)mp, name);
1327}
1328
1329static int
1330menu_setattr(mp, name, v)
1331 menuobject *mp;
1332 char *name;
1333 object *v;
1334{
1335 if (mp->m_attr == NULL) {
1336 mp->m_attr = newdictobject();
1337 if (mp->m_attr == NULL)
1338 return -1;
1339 }
Guido van Rossum94472a01992-09-04 09:45:18 +00001340 if (v == NULL) {
1341 int rv = dictremove(mp->m_attr, name);
1342 if (rv < 0)
1343 err_setstr(AttributeError,
1344 "delete non-existing menu object attribute");
1345 return rv;
1346 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001347 else
1348 return dictinsert(mp->m_attr, name, v);
1349}
1350
Guido van Rossumb6775db1994-08-01 11:34:53 +00001351static typeobject Menutype = {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001352 OB_HEAD_INIT(&Typetype)
1353 0, /*ob_size*/
1354 "menu", /*tp_name*/
1355 sizeof(menuobject), /*tp_size*/
1356 0, /*tp_itemsize*/
1357 /* methods */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001358 (destructor)menu_dealloc, /*tp_dealloc*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001359 0, /*tp_print*/
Guido van Rossumb6775db1994-08-01 11:34:53 +00001360 (getattrfunc)menu_getattr, /*tp_getattr*/
1361 (setattrfunc)menu_setattr, /*tp_setattr*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001362 0, /*tp_compare*/
1363 0, /*tp_repr*/
1364};
1365
1366
Guido van Rossume9066061993-07-29 13:14:32 +00001367#ifdef HAVE_BITMAPS
1368
Guido van Rossumbf80e541993-02-08 15:49:17 +00001369/* Bitmaps objects */
1370
1371static bitmapobject *newbitmapobject PROTO((int, int));
1372static bitmapobject *
1373newbitmapobject(width, height)
1374 int width, height;
1375{
1376 BITMAP *bitmap;
1377 bitmapobject *bp;
1378 bitmap = wnewbitmap(width, height);
1379 if (bitmap == NULL)
1380 return (bitmapobject *) err_nomem();
1381 bp = NEWOBJ(bitmapobject, &Bitmaptype);
1382 if (bp != NULL) {
1383 bp->b_bitmap = bitmap;
1384 bp->b_attr = NULL;
1385 }
1386 else
1387 wfreebitmap(bitmap);
1388 return bp;
1389}
1390
1391/* Bitmap methods */
1392
1393static void
1394bitmap_dealloc(bp)
1395 bitmapobject *bp;
1396{
1397 if (bp->b_bitmap != NULL)
1398 wfreebitmap(bp->b_bitmap);
1399 XDECREF(bp->b_attr);
1400 DEL(bp);
1401}
1402
1403static object *
1404bitmap_close(bp, args)
1405 bitmapobject *bp;
1406 object *args;
1407{
1408 if (bp->b_bitmap != NULL)
1409 wfreebitmap(bp->b_bitmap);
1410 bp->b_bitmap = NULL;
1411 XDECREF(bp->b_attr);
1412 bp->b_attr = NULL;
1413 INCREF(None);
1414 return None;
1415}
1416
1417static object *
1418bitmap_setbit(self, args)
1419 bitmapobject *self;
1420 object *args;
1421{
1422 int a[3];
1423 if (!getpointintarg(args, a))
1424 return NULL;
1425 wsetbit(self->b_bitmap, a[0], a[1], a[2]);
1426 INCREF(None);
1427 return None;
1428}
1429
1430static object *
1431bitmap_getbit(self, args)
1432 bitmapobject *self;
1433 object *args;
1434{
1435 int a[2];
1436 if (!getpointarg(args, a))
1437 return NULL;
1438 return newintobject((long) wgetbit(self->b_bitmap, a[0], a[1]));
1439}
1440
1441static object *
1442bitmap_getsize(self, args)
1443 bitmapobject *self;
1444 object *args;
1445{
1446 int width, height;
1447 if (!getnoarg(args))
1448 return NULL;
1449 wgetbitmapsize(self->b_bitmap, &width, &height);
1450 return mkvalue("(ii)", width, height);
1451}
1452
1453static struct methodlist bitmap_methods[] = {
Guido van Rossumb6775db1994-08-01 11:34:53 +00001454 {"close", (method)bitmap_close},
1455 {"getsize", (method)bitmap_getsize},
1456 {"getbit", (method)bitmap_getbit},
1457 {"setbit", (method)bitmap_setbit},
Guido van Rossumbf80e541993-02-08 15:49:17 +00001458 {NULL, NULL} /* sentinel */
1459};
1460
1461static object *
1462bitmap_getattr(bp, name)
1463 bitmapobject *bp;
1464 char *name;
1465{
1466 object *v = NULL;
1467 if (bp->b_bitmap == NULL) {
1468 err_setstr(StdwinError, "bitmap object already closed");
1469 return NULL;
1470 }
1471 if (strcmp(name, "__dict__") == 0) {
1472 v = bp->b_attr;
1473 if (v == NULL)
1474 v = None;
1475 }
1476 else if (bp->b_attr != NULL) {
1477 v = dictlookup(bp->b_attr, name);
1478 }
1479 if (v != NULL) {
1480 INCREF(v);
1481 return v;
1482 }
1483 return findmethod(bitmap_methods, (object *)bp, name);
1484}
1485
1486static int
1487bitmap_setattr(bp, name, v)
1488 bitmapobject *bp;
1489 char *name;
1490 object *v;
1491{
1492 if (bp->b_attr == NULL) {
1493 bp->b_attr = newdictobject();
1494 if (bp->b_attr == NULL)
1495 return -1;
1496 }
1497 if (v == NULL) {
1498 int rv = dictremove(bp->b_attr, name);
1499 if (rv < 0)
1500 err_setstr(AttributeError,
1501 "delete non-existing bitmap object attribute");
1502 return rv;
1503 }
1504 else
1505 return dictinsert(bp->b_attr, name, v);
1506}
1507
Guido van Rossumb6775db1994-08-01 11:34:53 +00001508static typeobject Bitmaptype = {
Guido van Rossumbf80e541993-02-08 15:49:17 +00001509 OB_HEAD_INIT(&Typetype)
1510 0, /*ob_size*/
1511 "bitmap", /*tp_name*/
1512 sizeof(bitmapobject), /*tp_size*/
1513 0, /*tp_itemsize*/
1514 /* methods */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001515 (destructor)bitmap_dealloc, /*tp_dealloc*/
Guido van Rossumbf80e541993-02-08 15:49:17 +00001516 0, /*tp_print*/
Guido van Rossumb6775db1994-08-01 11:34:53 +00001517 (getattrfunc)bitmap_getattr, /*tp_getattr*/
1518 (setattrfunc)bitmap_setattr, /*tp_setattr*/
Guido van Rossumbf80e541993-02-08 15:49:17 +00001519 0, /*tp_compare*/
1520 0, /*tp_repr*/
1521};
1522
Guido van Rossume9066061993-07-29 13:14:32 +00001523#endif /* HAVE_BITMAPS */
1524
Guido van Rossumbf80e541993-02-08 15:49:17 +00001525
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001526/* Windows */
1527
1528#define MAXNWIN 50
1529static windowobject *windowlist[MAXNWIN];
1530
1531/* Window methods */
1532
1533static void
1534window_dealloc(wp)
1535 windowobject *wp;
1536{
1537 if (wp->w_win != NULL) {
1538 int tag = wgettag(wp->w_win);
1539 if (tag >= 0 && tag < MAXNWIN)
1540 windowlist[tag] = NULL;
1541 else
1542 fprintf(stderr, "XXX help! tag %d in window_dealloc\n",
1543 tag);
1544 wclose(wp->w_win);
1545 }
1546 DECREF(wp->w_title);
1547 if (wp->w_attr != NULL)
1548 DECREF(wp->w_attr);
1549 free((char *)wp);
1550}
1551
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001552static object *
Guido van Rossum3c284741991-11-27 14:54:54 +00001553window_close(wp, args)
1554 windowobject *wp;
1555 object *args;
1556{
1557 if (wp->w_win != NULL) {
1558 int tag = wgettag(wp->w_win);
1559 if (tag >= 0 && tag < MAXNWIN)
1560 windowlist[tag] = NULL;
1561 wclose(wp->w_win);
1562 wp->w_win = NULL;
1563 }
1564 INCREF(None);
1565 return None;
1566}
1567
1568static object *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001569window_begindrawing(wp, args)
1570 windowobject *wp;
1571 object *args;
1572{
1573 drawingobject *dp;
1574 if (!getnoarg(args))
1575 return NULL;
1576 if (Drawing != NULL) {
Guido van Rossum87e7ea71991-12-10 14:00:03 +00001577 err_setstr(StdwinError, "already drawing");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001578 return NULL;
1579 }
1580 dp = NEWOBJ(drawingobject, &Drawingtype);
1581 if (dp == NULL)
1582 return NULL;
1583 Drawing = dp;
1584 INCREF(wp);
1585 dp->d_ref = wp;
1586 wbegindrawing(wp->w_win);
1587 return (object *)dp;
1588}
1589
1590static object *
1591window_change(wp, args)
1592 windowobject *wp;
1593 object *args;
1594{
1595 int a[4];
1596 if (!getrectarg(args, a))
1597 return NULL;
1598 wchange(wp->w_win, a[0], a[1], a[2], a[3]);
1599 INCREF(None);
1600 return None;
1601}
1602
1603static object *
1604window_gettitle(wp, args)
1605 windowobject *wp;
1606 object *args;
1607{
1608 if (!getnoarg(args))
1609 return NULL;
1610 INCREF(wp->w_title);
1611 return wp->w_title;
1612}
1613
1614static object *
Guido van Rossum541c8c01991-05-05 20:13:41 +00001615window_getwinpos(wp, args)
1616 windowobject *wp;
1617 object *args;
1618{
1619 int h, v;
1620 if (!getnoarg(args))
1621 return NULL;
1622 wgetwinpos(wp->w_win, &h, &v);
1623 return makepoint(h, v);
1624}
1625
1626static object *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001627window_getwinsize(wp, args)
1628 windowobject *wp;
1629 object *args;
1630{
1631 int width, height;
1632 if (!getnoarg(args))
1633 return NULL;
1634 wgetwinsize(wp->w_win, &width, &height);
1635 return makepoint(width, height);
1636}
1637
1638static object *
Guido van Rossumbf80e541993-02-08 15:49:17 +00001639window_setwinpos(wp, args)
1640 windowobject *wp;
1641 object *args;
1642{
1643 int a[2];
1644 if (!getpointarg(args, a))
1645 return NULL;
1646 wsetwinpos(wp->w_win, a[0], a[1]);
1647 INCREF(None);
1648 return None;
1649}
1650
1651static object *
1652window_setwinsize(wp, args)
1653 windowobject *wp;
1654 object *args;
1655{
1656 int a[2];
1657 if (!getpointarg(args, a))
1658 return NULL;
1659 wsetwinsize(wp->w_win, a[0], a[1]);
1660 INCREF(None);
1661 return None;
1662}
1663
1664static object *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001665window_getdocsize(wp, args)
1666 windowobject *wp;
1667 object *args;
1668{
1669 int width, height;
1670 if (!getnoarg(args))
1671 return NULL;
1672 wgetdocsize(wp->w_win, &width, &height);
1673 return makepoint(width, height);
1674}
1675
1676static object *
1677window_getorigin(wp, args)
1678 windowobject *wp;
1679 object *args;
1680{
1681 int width, height;
1682 if (!getnoarg(args))
1683 return NULL;
1684 wgetorigin(wp->w_win, &width, &height);
1685 return makepoint(width, height);
1686}
1687
1688static object *
1689window_scroll(wp, args)
1690 windowobject *wp;
1691 object *args;
1692{
1693 int a[6];
1694 if (!getrectpointarg(args, a))
1695 return NULL;
1696 wscroll(wp->w_win, a[0], a[1], a[2], a[3], a[4], a[5]);
1697 INCREF(None);
1698 return None;
1699}
1700
1701static object *
1702window_setdocsize(wp, args)
1703 windowobject *wp;
1704 object *args;
1705{
1706 int a[2];
1707 if (!getpointarg(args, a))
1708 return NULL;
1709 wsetdocsize(wp->w_win, a[0], a[1]);
1710 INCREF(None);
1711 return None;
1712}
1713
1714static object *
1715window_setorigin(wp, args)
1716 windowobject *wp;
1717 object *args;
1718{
1719 int a[2];
1720 if (!getpointarg(args, a))
1721 return NULL;
1722 wsetorigin(wp->w_win, a[0], a[1]);
1723 INCREF(None);
1724 return None;
1725}
1726
1727static object *
1728window_settitle(wp, args)
1729 windowobject *wp;
1730 object *args;
1731{
1732 object *title;
Guido van Rossum234f9421993-06-17 12:35:49 +00001733 if (!getargs(args, "S", &title))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001734 return NULL;
1735 DECREF(wp->w_title);
1736 INCREF(title);
1737 wp->w_title = title;
1738 wsettitle(wp->w_win, getstringvalue(title));
1739 INCREF(None);
1740 return None;
1741}
1742
1743static object *
1744window_show(wp, args)
1745 windowobject *wp;
1746 object *args;
1747{
1748 int a[4];
1749 if (!getrectarg(args, a))
1750 return NULL;
1751 wshow(wp->w_win, a[0], a[1], a[2], a[3]);
1752 INCREF(None);
1753 return None;
1754}
1755
1756static object *
1757window_settimer(wp, args)
1758 windowobject *wp;
1759 object *args;
1760{
1761 int a;
1762 if (!getintarg(args, &a))
1763 return NULL;
1764 wsettimer(wp->w_win, a);
1765 INCREF(None);
1766 return None;
1767}
1768
1769static object *
1770window_menucreate(self, args)
1771 windowobject *self;
1772 object *args;
1773{
1774 menuobject *mp;
Guido van Rossumfc58e581992-01-27 16:45:55 +00001775 char *title;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001776 if (!getstrarg(args, &title))
1777 return NULL;
1778 wmenusetdeflocal(1);
1779 mp = newmenuobject(title);
1780 if (mp == NULL)
1781 return NULL;
1782 wmenuattach(self->w_win, mp->m_menu);
1783 return (object *)mp;
1784}
1785
1786static object *
1787window_textcreate(self, args)
1788 windowobject *self;
1789 object *args;
1790{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001791 int a[4];
1792 if (!getrectarg(args, a))
1793 return NULL;
1794 return (object *)
1795 newtextobject(self, a[0], a[1], a[2], a[3]);
1796}
1797
Guido van Rossum5b10f451990-10-30 16:01:48 +00001798static object *
1799window_setselection(self, args)
1800 windowobject *self;
1801 object *args;
1802{
Guido van Rossumfc58e581992-01-27 16:45:55 +00001803 int sel, size, ok;
1804 char *text;
1805 if (!getargs(args, "(is#)", &sel, &text, &size))
Guido van Rossum5b10f451990-10-30 16:01:48 +00001806 return NULL;
Guido van Rossumfc58e581992-01-27 16:45:55 +00001807 ok = wsetselection(self->w_win, sel, text, size);
Guido van Rossum5b10f451990-10-30 16:01:48 +00001808 return newintobject(ok);
1809}
1810
1811static object *
1812window_setwincursor(self, args)
1813 windowobject *self;
1814 object *args;
1815{
Guido van Rossumfc58e581992-01-27 16:45:55 +00001816 char *name;
Guido van Rossum5b10f451990-10-30 16:01:48 +00001817 CURSOR *c;
Guido van Rossum3c8ba7a1992-02-05 11:15:00 +00001818 if (!getargs(args, "z", &name))
Guido van Rossum5b10f451990-10-30 16:01:48 +00001819 return NULL;
Guido van Rossum3c8ba7a1992-02-05 11:15:00 +00001820 if (name == NULL)
1821 c = NULL;
1822 else {
1823 c = wfetchcursor(name);
1824 if (c == NULL) {
1825 err_setstr(StdwinError, "no such cursor");
1826 return NULL;
1827 }
Guido van Rossum5b10f451990-10-30 16:01:48 +00001828 }
1829 wsetwincursor(self->w_win, c);
1830 INCREF(None);
1831 return None;
1832}
1833
Guido van Rossumfc58e581992-01-27 16:45:55 +00001834static object *
1835window_setactive(self, args)
1836 windowobject *self;
1837 object *args;
1838{
1839 if (!getnoarg(args))
1840 return NULL;
1841 wsetactive(self->w_win);
1842 INCREF(None);
1843 return None;
1844}
1845
Guido van Rossum8dcbbac1991-07-27 21:42:24 +00001846#ifdef CWI_HACKS
1847static object *
1848window_getxwindowid(self, args)
1849 windowobject *self;
1850 object *args;
1851{
1852 long wid = wgetxwindowid(self->w_win);
1853 return newintobject(wid);
1854}
1855#endif
1856
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001857static struct methodlist window_methods[] = {
Guido van Rossumb6775db1994-08-01 11:34:53 +00001858 {"begindrawing",(method)window_begindrawing},
1859 {"change", (method)window_change},
1860 {"close", (method)window_close},
1861 {"getdocsize", (method)window_getdocsize},
1862 {"getorigin", (method)window_getorigin},
1863 {"gettitle", (method)window_gettitle},
1864 {"getwinpos", (method)window_getwinpos},
1865 {"getwinsize", (method)window_getwinsize},
1866 {"menucreate", (method)window_menucreate},
1867 {"scroll", (method)window_scroll},
1868 {"setactive", (method)window_setactive},
1869 {"setdocsize", (method)window_setdocsize},
1870 {"setorigin", (method)window_setorigin},
1871 {"setselection",(method)window_setselection},
1872 {"settimer", (method)window_settimer},
1873 {"settitle", (method)window_settitle},
1874 {"setwincursor",(method)window_setwincursor},
1875 {"setwinpos", (method)window_setwinpos},
1876 {"setwinsize", (method)window_setwinsize},
1877 {"show", (method)window_show},
1878 {"textcreate", (method)window_textcreate},
Guido van Rossum8dcbbac1991-07-27 21:42:24 +00001879#ifdef CWI_HACKS
Guido van Rossumb6775db1994-08-01 11:34:53 +00001880 {"getxwindowid",(method)window_getxwindowid},
Guido van Rossum8dcbbac1991-07-27 21:42:24 +00001881#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001882 {NULL, NULL} /* sentinel */
1883};
1884
1885static object *
1886window_getattr(wp, name)
1887 windowobject *wp;
1888 char *name;
1889{
Guido van Rossum85f50761991-10-20 20:22:50 +00001890 object *v = NULL;
Guido van Rossum77b46041992-01-14 18:41:24 +00001891 if (wp->w_win == NULL) {
1892 err_setstr(StdwinError, "window already closed");
1893 return NULL;
1894 }
Guido van Rossum85f50761991-10-20 20:22:50 +00001895 if (strcmp(name, "__dict__") == 0) {
1896 v = wp->w_attr;
1897 if (v == NULL)
1898 v = None;
1899 }
1900 else if (wp->w_attr != NULL) {
1901 v = dictlookup(wp->w_attr, name);
1902 }
1903 if (v != NULL) {
1904 INCREF(v);
1905 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001906 }
1907 return findmethod(window_methods, (object *)wp, name);
1908}
1909
1910static int
1911window_setattr(wp, name, v)
1912 windowobject *wp;
1913 char *name;
1914 object *v;
1915{
1916 if (wp->w_attr == NULL) {
1917 wp->w_attr = newdictobject();
1918 if (wp->w_attr == NULL)
1919 return -1;
1920 }
Guido van Rossum94472a01992-09-04 09:45:18 +00001921 if (v == NULL) {
1922 int rv = dictremove(wp->w_attr, name);
1923 if (rv < 0)
1924 err_setstr(AttributeError,
1925 "delete non-existing menu object attribute");
1926 return rv;
1927 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001928 else
1929 return dictinsert(wp->w_attr, name, v);
1930}
1931
Guido van Rossumb6775db1994-08-01 11:34:53 +00001932static typeobject Windowtype = {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001933 OB_HEAD_INIT(&Typetype)
1934 0, /*ob_size*/
1935 "window", /*tp_name*/
1936 sizeof(windowobject), /*tp_size*/
1937 0, /*tp_itemsize*/
1938 /* methods */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001939 (destructor)window_dealloc, /*tp_dealloc*/
Guido van Rossum7066dd71992-09-17 17:54:56 +00001940 0, /*tp_print*/
Guido van Rossumb6775db1994-08-01 11:34:53 +00001941 (getattrfunc)window_getattr, /*tp_getattr*/
1942 (setattrfunc)window_setattr, /*tp_setattr*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001943 0, /*tp_compare*/
1944 0, /*tp_repr*/
1945};
1946
1947/* Stdwin methods */
1948
1949static object *
Guido van Rossumcacd9571993-10-18 11:44:47 +00001950stdwin_done(sw, args)
1951 object *sw;
1952 object *args;
1953{
1954 if (!getnoarg(args))
1955 return NULL;
1956 wdone();
1957 /* XXX There is no protection against continued use of
1958 XXX stdwin functions or objects after this call is made.
1959 XXX Use at own risk */
1960 INCREF(None);
1961 return None;
1962}
1963
1964static object *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001965stdwin_open(sw, args)
1966 object *sw;
1967 object *args;
1968{
1969 int tag;
1970 object *title;
1971 windowobject *wp;
Guido van Rossum234f9421993-06-17 12:35:49 +00001972 if (!getargs(args, "S", &title))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001973 return NULL;
1974 for (tag = 0; tag < MAXNWIN; tag++) {
1975 if (windowlist[tag] == NULL)
1976 break;
1977 }
Guido van Rossum27201061991-04-16 08:43:03 +00001978 if (tag >= MAXNWIN) {
Guido van Rossum87e7ea71991-12-10 14:00:03 +00001979 err_setstr(StdwinError, "creating too many windows");
Guido van Rossum27201061991-04-16 08:43:03 +00001980 return NULL;
1981 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001982 wp = NEWOBJ(windowobject, &Windowtype);
1983 if (wp == NULL)
1984 return NULL;
1985 INCREF(title);
1986 wp->w_title = title;
1987 wp->w_win = wopen(getstringvalue(title), (void (*)()) NULL);
1988 wp->w_attr = NULL;
1989 if (wp->w_win == NULL) {
1990 DECREF(wp);
1991 return NULL;
1992 }
1993 windowlist[tag] = wp;
1994 wsettag(wp->w_win, tag);
1995 return (object *)wp;
1996}
1997
1998static object *
Guido van Rossum246b9d81991-06-03 10:55:14 +00001999window2object(win)
2000 WINDOW *win;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002001{
Guido van Rossum246b9d81991-06-03 10:55:14 +00002002 object *w;
2003 if (win == NULL)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002004 w = None;
2005 else {
Guido van Rossum246b9d81991-06-03 10:55:14 +00002006 int tag = wgettag(win);
2007 if (tag < 0 || tag >= MAXNWIN || windowlist[tag] == NULL ||
2008 windowlist[tag]->w_win != win)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002009 w = None;
2010 else
2011 w = (object *)windowlist[tag];
2012 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002013 INCREF(w);
Guido van Rossum246b9d81991-06-03 10:55:14 +00002014 return w;
2015}
2016
2017static object *
2018stdwin_get_poll_event(poll, args)
2019 int poll;
2020 object *args;
2021{
2022 EVENT e;
Guido van Rossum2ee12f41992-04-13 15:54:35 +00002023 object *u, *v, *w;
Guido van Rossum246b9d81991-06-03 10:55:14 +00002024 if (!getnoarg(args))
2025 return NULL;
2026 if (Drawing != NULL) {
Guido van Rossum87e7ea71991-12-10 14:00:03 +00002027 err_setstr(StdwinError, "cannot getevent() while drawing");
Guido van Rossum246b9d81991-06-03 10:55:14 +00002028 return NULL;
2029 }
2030 again:
Guido van Rossumff4949e1992-08-05 19:58:53 +00002031 BGN_STDWIN
Guido van Rossum246b9d81991-06-03 10:55:14 +00002032 if (poll) {
2033 if (!wpollevent(&e)) {
Guido van Rossumff4949e1992-08-05 19:58:53 +00002034 RET_STDWIN
Guido van Rossum246b9d81991-06-03 10:55:14 +00002035 INCREF(None);
2036 return None;
2037 }
2038 }
2039 else
2040 wgetevent(&e);
Guido van Rossumff4949e1992-08-05 19:58:53 +00002041 END_STDWIN
Guido van Rossum246b9d81991-06-03 10:55:14 +00002042 if (e.type == WE_COMMAND && e.u.command == WC_CANCEL) {
2043 /* Turn keyboard interrupts into exceptions */
2044 err_set(KeyboardInterrupt);
2045 return NULL;
2046 }
2047 if (e.type == WE_COMMAND && e.u.command == WC_CLOSE) {
2048 /* Turn WC_CLOSE commands into WE_CLOSE events */
2049 e.type = WE_CLOSE;
2050 }
Guido van Rossum2ee12f41992-04-13 15:54:35 +00002051 v = window2object(e.window);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002052 switch (e.type) {
2053 case WE_CHAR:
2054 {
2055 char c[1];
2056 c[0] = e.u.character;
2057 w = newsizedstringobject(c, 1);
2058 }
2059 break;
2060 case WE_COMMAND:
2061 w = newintobject((long)e.u.command);
2062 break;
2063 case WE_DRAW:
2064 w = makerect(e.u.area.left, e.u.area.top,
2065 e.u.area.right, e.u.area.bottom);
2066 break;
2067 case WE_MOUSE_DOWN:
2068 case WE_MOUSE_MOVE:
2069 case WE_MOUSE_UP:
Guido van Rossum2ee12f41992-04-13 15:54:35 +00002070 w = mkvalue("((ii)iii)",
2071 e.u.where.h, e.u.where.v,
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002072 e.u.where.clicks,
2073 e.u.where.button,
2074 e.u.where.mask);
2075 break;
2076 case WE_MENU:
Guido van Rossum2d14e211991-02-19 12:26:49 +00002077 if (e.u.m.id >= IDOFFSET && e.u.m.id < IDOFFSET+MAXNMENU &&
2078 menulist[e.u.m.id - IDOFFSET] != NULL)
Guido van Rossum2ee12f41992-04-13 15:54:35 +00002079 w = mkvalue("(Oi)",
2080 menulist[e.u.m.id - IDOFFSET], e.u.m.item);
Guido van Rossum246b9d81991-06-03 10:55:14 +00002081 else {
2082 /* Ghost menu event.
2083 Can occur only on the Mac if another part
2084 of the aplication has installed a menu;
2085 like the THINK C console library. */
2086 DECREF(v);
2087 goto again;
2088 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002089 break;
Guido van Rossum3ee199e1992-06-30 12:48:26 +00002090 case WE_KEY:
2091 w = mkvalue("(ii)", e.u.key.code, e.u.key.mask);
2092 break;
Guido van Rossum5b10f451990-10-30 16:01:48 +00002093 case WE_LOST_SEL:
2094 w = newintobject((long)e.u.sel);
2095 break;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002096 default:
2097 w = None;
2098 INCREF(w);
2099 break;
2100 }
2101 if (w == NULL) {
2102 DECREF(v);
2103 return NULL;
2104 }
Guido van Rossum2ee12f41992-04-13 15:54:35 +00002105 u = mkvalue("(iOO)", e.type, v, w);
2106 XDECREF(v);
2107 XDECREF(w);
2108 return u;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002109}
2110
2111static object *
Guido van Rossume8e7cf41991-01-16 14:06:18 +00002112stdwin_getevent(sw, args)
2113 object *sw;
2114 object *args;
2115{
2116 return stdwin_get_poll_event(0, args);
2117}
2118
2119static object *
2120stdwin_pollevent(sw, args)
2121 object *sw;
2122 object *args;
2123{
2124 return stdwin_get_poll_event(1, args);
2125}
2126
2127static object *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002128stdwin_setdefwinpos(sw, args)
2129 object *sw;
2130 object *args;
2131{
2132 int a[2];
2133 if (!getpointarg(args, a))
2134 return NULL;
2135 wsetdefwinpos(a[0], a[1]);
2136 INCREF(None);
2137 return None;
2138}
2139
2140static object *
2141stdwin_setdefwinsize(sw, args)
2142 object *sw;
2143 object *args;
2144{
2145 int a[2];
2146 if (!getpointarg(args, a))
2147 return NULL;
2148 wsetdefwinsize(a[0], a[1]);
2149 INCREF(None);
2150 return None;
2151}
2152
2153static object *
Guido van Rossum0c2290b1991-04-03 19:12:14 +00002154stdwin_setdefscrollbars(sw, args)
2155 object *sw;
2156 object *args;
2157{
2158 int a[2];
2159 if (!getpointarg(args, a))
2160 return NULL;
2161 wsetdefscrollbars(a[0], a[1]);
2162 INCREF(None);
2163 return None;
2164}
2165
2166static object *
Guido van Rossum541c8c01991-05-05 20:13:41 +00002167stdwin_getdefwinpos(self, args)
2168 object *self;
Guido van Rossum33f17701991-02-13 23:19:39 +00002169 object *args;
2170{
2171 int h, v;
2172 if (!getnoarg(args))
2173 return NULL;
2174 wgetdefwinpos(&h, &v);
2175 return makepoint(h, v);
2176}
2177
2178static object *
Guido van Rossum541c8c01991-05-05 20:13:41 +00002179stdwin_getdefwinsize(self, args)
2180 object *self;
Guido van Rossum33f17701991-02-13 23:19:39 +00002181 object *args;
2182{
2183 int width, height;
2184 if (!getnoarg(args))
2185 return NULL;
2186 wgetdefwinsize(&width, &height);
2187 return makepoint(width, height);
2188}
2189
2190static object *
Guido van Rossum541c8c01991-05-05 20:13:41 +00002191stdwin_getdefscrollbars(self, args)
2192 object *self;
Guido van Rossum0c2290b1991-04-03 19:12:14 +00002193 object *args;
2194{
2195 int h, v;
2196 if (!getnoarg(args))
2197 return NULL;
2198 wgetdefscrollbars(&h, &v);
2199 return makepoint(h, v);
2200}
2201
2202static object *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002203stdwin_menucreate(self, args)
2204 object *self;
2205 object *args;
2206{
Guido van Rossumfc58e581992-01-27 16:45:55 +00002207 char *title;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002208 if (!getstrarg(args, &title))
2209 return NULL;
2210 wmenusetdeflocal(0);
2211 return (object *)newmenuobject(title);
2212}
2213
2214static object *
2215stdwin_askfile(self, args)
2216 object *self;
2217 object *args;
2218{
Guido van Rossumfc58e581992-01-27 16:45:55 +00002219 char *prompt, *dflt;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002220 int new, ret;
2221 char buf[256];
Guido van Rossum234f9421993-06-17 12:35:49 +00002222 if (!getargs(args, "(ssi)", &prompt, &dflt, &new))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002223 return NULL;
Guido van Rossumfc58e581992-01-27 16:45:55 +00002224 strncpy(buf, dflt, sizeof buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002225 buf[sizeof buf - 1] = '\0';
Guido van Rossumff4949e1992-08-05 19:58:53 +00002226 BGN_STDWIN
Guido van Rossumfc58e581992-01-27 16:45:55 +00002227 ret = waskfile(prompt, buf, sizeof buf, new);
Guido van Rossumff4949e1992-08-05 19:58:53 +00002228 END_STDWIN
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002229 if (!ret) {
2230 err_set(KeyboardInterrupt);
2231 return NULL;
2232 }
2233 return newstringobject(buf);
2234}
2235
2236static object *
2237stdwin_askync(self, args)
2238 object *self;
2239 object *args;
2240{
Guido van Rossumfc58e581992-01-27 16:45:55 +00002241 char *prompt;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002242 int new, ret;
Guido van Rossum234f9421993-06-17 12:35:49 +00002243 if (!getargs(args, "(si)", &prompt, &new))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002244 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002245 BGN_STDWIN
Guido van Rossumfc58e581992-01-27 16:45:55 +00002246 ret = waskync(prompt, new);
Guido van Rossumff4949e1992-08-05 19:58:53 +00002247 END_STDWIN
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002248 if (ret < 0) {
2249 err_set(KeyboardInterrupt);
2250 return NULL;
2251 }
2252 return newintobject((long)ret);
2253}
2254
2255static object *
2256stdwin_askstr(self, args)
2257 object *self;
2258 object *args;
2259{
Guido van Rossumfc58e581992-01-27 16:45:55 +00002260 char *prompt, *dflt;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002261 int ret;
2262 char buf[256];
Guido van Rossum234f9421993-06-17 12:35:49 +00002263 if (!getargs(args, "(ss)", &prompt, &dflt))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002264 return NULL;
Guido van Rossumfc58e581992-01-27 16:45:55 +00002265 strncpy(buf, dflt, sizeof buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002266 buf[sizeof buf - 1] = '\0';
Guido van Rossumff4949e1992-08-05 19:58:53 +00002267 BGN_STDWIN
Guido van Rossumfc58e581992-01-27 16:45:55 +00002268 ret = waskstr(prompt, buf, sizeof buf);
Guido van Rossumff4949e1992-08-05 19:58:53 +00002269 END_STDWIN
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002270 if (!ret) {
2271 err_set(KeyboardInterrupt);
2272 return NULL;
2273 }
2274 return newstringobject(buf);
2275}
2276
2277static object *
2278stdwin_message(self, args)
2279 object *self;
2280 object *args;
2281{
Guido van Rossumfc58e581992-01-27 16:45:55 +00002282 char *msg;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002283 if (!getstrarg(args, &msg))
2284 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002285 BGN_STDWIN
Guido van Rossumfc58e581992-01-27 16:45:55 +00002286 wmessage(msg);
Guido van Rossumff4949e1992-08-05 19:58:53 +00002287 END_STDWIN
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002288 INCREF(None);
2289 return None;
2290}
2291
2292static object *
2293stdwin_fleep(self, args)
2294 object *self;
2295 object *args;
2296{
2297 if (!getnoarg(args))
2298 return NULL;
2299 wfleep();
2300 INCREF(None);
2301 return None;
2302}
2303
2304static object *
2305stdwin_setcutbuffer(self, args)
2306 object *self;
2307 object *args;
2308{
Guido van Rossumfc58e581992-01-27 16:45:55 +00002309 int i, size;
2310 char *str;
2311 if (!getargs(args, "(is#)", &i, &str, &size))
Guido van Rossum124967c1990-11-06 15:17:35 +00002312 return NULL;
Guido van Rossumfc58e581992-01-27 16:45:55 +00002313 wsetcutbuffer(i, str, size);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002314 INCREF(None);
2315 return None;
2316}
2317
2318static object *
Guido van Rossum246b9d81991-06-03 10:55:14 +00002319stdwin_getactive(self, args)
2320 object *self;
2321 object *args;
2322{
2323 return window2object(wgetactive());
2324}
2325
2326static object *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002327stdwin_getcutbuffer(self, args)
2328 object *self;
2329 object *args;
2330{
Guido van Rossum5b10f451990-10-30 16:01:48 +00002331 int i;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002332 char *str;
Guido van Rossum01769f01990-10-30 13:39:00 +00002333 int len;
Guido van Rossum124967c1990-11-06 15:17:35 +00002334 if (!getintarg(args, &i))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002335 return NULL;
Guido van Rossum5b10f451990-10-30 16:01:48 +00002336 str = wgetcutbuffer(i, &len);
Guido van Rossum01769f01990-10-30 13:39:00 +00002337 if (str == NULL) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002338 str = "";
Guido van Rossum01769f01990-10-30 13:39:00 +00002339 len = 0;
2340 }
2341 return newsizedstringobject(str, len);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002342}
2343
Guido van Rossum5b10f451990-10-30 16:01:48 +00002344static object *
2345stdwin_rotatecutbuffers(self, args)
2346 object *self;
2347 object *args;
2348{
2349 int i;
2350 if (!getintarg(args, &i))
2351 return NULL;
2352 wrotatecutbuffers(i);
2353 INCREF(None);
2354 return None;
2355}
2356
2357static object *
2358stdwin_getselection(self, args)
2359 object *self;
2360 object *args;
2361{
2362 int sel;
2363 char *data;
2364 int len;
2365 if (!getintarg(args, &sel))
2366 return NULL;
2367 data = wgetselection(sel, &len);
2368 if (data == NULL) {
2369 data = "";
2370 len = 0;
2371 }
2372 return newsizedstringobject(data, len);
2373}
2374
2375static object *
2376stdwin_resetselection(self, args)
2377 object *self;
2378 object *args;
2379{
2380 int sel;
2381 if (!getintarg(args, &sel))
2382 return NULL;
2383 wresetselection(sel);
2384 INCREF(None);
2385 return None;
2386}
2387
Guido van Rossum0c2290b1991-04-03 19:12:14 +00002388static object *
2389stdwin_fetchcolor(self, args)
2390 object *self;
2391 object *args;
2392{
Guido van Rossumfc58e581992-01-27 16:45:55 +00002393 char *colorname;
Guido van Rossum34679b71993-01-26 13:33:44 +00002394 COLOR color;
Guido van Rossum0c2290b1991-04-03 19:12:14 +00002395 if (!getstrarg(args, &colorname))
2396 return NULL;
Guido van Rossum34679b71993-01-26 13:33:44 +00002397 color = wfetchcolor(colorname);
2398#ifdef BADCOLOR
2399 if (color == BADCOLOR) {
2400 err_setstr(StdwinError, "color name not found");
2401 return NULL;
2402 }
2403#endif
2404 return newintobject((long)color);
Guido van Rossum0c2290b1991-04-03 19:12:14 +00002405}
2406
Guido van Rossum541c8c01991-05-05 20:13:41 +00002407static object *
2408stdwin_getscrsize(self, args)
2409 object *self;
2410 object *args;
2411{
2412 int width, height;
2413 if (!getnoarg(args))
2414 return NULL;
2415 wgetscrsize(&width, &height);
2416 return makepoint(width, height);
2417}
2418
2419static object *
2420stdwin_getscrmm(self, args)
2421 object *self;
2422 object *args;
2423{
2424 int width, height;
2425 if (!getnoarg(args))
2426 return NULL;
2427 wgetscrmm(&width, &height);
2428 return makepoint(width, height);
2429}
2430
Guido van Rossumed233a51992-06-23 09:07:03 +00002431#ifdef unix
2432static object *
2433stdwin_connectionnumber(self, args)
2434 object *self;
2435 object *args;
2436{
2437 if (!getnoarg(args))
2438 return NULL;
2439 return newintobject((long) wconnectionnumber());
2440}
2441#endif
2442
Guido van Rossumbf80e541993-02-08 15:49:17 +00002443static object *
2444stdwin_listfontnames(self, args)
2445 object *self;
2446 object *args;
2447{
2448 char *pattern;
2449 char **fontnames;
2450 int count;
2451 object *list;
2452 if (!getargs(args, "z", &pattern))
2453 return NULL;
2454 fontnames = wlistfontnames(pattern, &count);
2455 list = newlistobject(count);
2456 if (list != NULL) {
2457 int i;
2458 for (i = 0; i < count; i++) {
2459 object *v = newstringobject(fontnames[i]);
2460 if (v == NULL) {
2461 DECREF(list);
2462 list = NULL;
2463 break;
2464 }
2465 setlistitem(list, i, v);
2466 }
2467 }
2468 return list;
2469}
2470
Guido van Rossume9066061993-07-29 13:14:32 +00002471#ifdef HAVE_BITMAPS
Guido van Rossumbf80e541993-02-08 15:49:17 +00002472static object *
2473stdwin_newbitmap(self, args)
2474 object *self;
2475 object *args;
2476{
2477 int width, height;
2478 bitmapobject *bp;
2479 if (!getargs(args, "(ii)", &width, &height))
2480 return NULL;
2481 return (object *)newbitmapobject(width, height);
2482}
Guido van Rossume9066061993-07-29 13:14:32 +00002483#endif
Guido van Rossumbf80e541993-02-08 15:49:17 +00002484
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002485static struct methodlist stdwin_methods[] = {
2486 {"askfile", stdwin_askfile},
2487 {"askstr", stdwin_askstr},
2488 {"askync", stdwin_askync},
Guido van Rossumcacd9571993-10-18 11:44:47 +00002489 {"done", stdwin_done},
Guido van Rossum27201061991-04-16 08:43:03 +00002490 {"fetchcolor", stdwin_fetchcolor},
Guido van Rossumed233a51992-06-23 09:07:03 +00002491#ifdef unix
2492 {"fileno", stdwin_connectionnumber},
2493 {"connectionnumber", stdwin_connectionnumber},
2494#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002495 {"fleep", stdwin_fleep},
Guido van Rossum246b9d81991-06-03 10:55:14 +00002496 {"getactive", stdwin_getactive},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002497 {"getcutbuffer", stdwin_getcutbuffer},
Guido van Rossum0c2290b1991-04-03 19:12:14 +00002498 {"getdefscrollbars", stdwin_getdefscrollbars},
Guido van Rossum33f17701991-02-13 23:19:39 +00002499 {"getdefwinpos", stdwin_getdefwinpos},
2500 {"getdefwinsize", stdwin_getdefwinsize},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002501 {"getevent", stdwin_getevent},
Guido van Rossum541c8c01991-05-05 20:13:41 +00002502 {"getscrmm", stdwin_getscrmm},
2503 {"getscrsize", stdwin_getscrsize},
Guido van Rossum27201061991-04-16 08:43:03 +00002504 {"getselection", stdwin_getselection},
Guido van Rossumbf80e541993-02-08 15:49:17 +00002505 {"listfontnames", stdwin_listfontnames},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002506 {"menucreate", stdwin_menucreate},
2507 {"message", stdwin_message},
Guido van Rossume9066061993-07-29 13:14:32 +00002508#ifdef HAVE_BITMAPS
Guido van Rossumbf80e541993-02-08 15:49:17 +00002509 {"newbitmap", stdwin_newbitmap},
Guido van Rossume9066061993-07-29 13:14:32 +00002510#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002511 {"open", stdwin_open},
Guido van Rossume8e7cf41991-01-16 14:06:18 +00002512 {"pollevent", stdwin_pollevent},
Guido van Rossum5b10f451990-10-30 16:01:48 +00002513 {"resetselection", stdwin_resetselection},
2514 {"rotatecutbuffers", stdwin_rotatecutbuffers},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002515 {"setcutbuffer", stdwin_setcutbuffer},
Guido van Rossum0c2290b1991-04-03 19:12:14 +00002516 {"setdefscrollbars", stdwin_setdefscrollbars},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002517 {"setdefwinpos", stdwin_setdefwinpos},
2518 {"setdefwinsize", stdwin_setdefwinsize},
2519
2520 /* Text measuring methods borrow code from drawing objects: */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002521 {"baseline", (method)drawing_baseline},
2522 {"lineheight", (method)drawing_lineheight},
2523 {"textbreak", (method)drawing_textbreak},
2524 {"textwidth", (method)drawing_textwidth},
Guido van Rossum0c2290b1991-04-03 19:12:14 +00002525
2526 /* Same for font setting methods: */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002527 {"setfont", (method)drawing_setfont},
Guido van Rossum0c2290b1991-04-03 19:12:14 +00002528
2529 /* Same for color setting/getting methods: */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002530 {"getbgcolor", (method)drawing_getbgcolor},
2531 {"getfgcolor", (method)drawing_getfgcolor},
2532 {"setbgcolor", (method)drawing_setbgcolor},
2533 {"setfgcolor", (method)drawing_setfgcolor},
Guido van Rossum0c2290b1991-04-03 19:12:14 +00002534
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002535 {NULL, NULL} /* sentinel */
2536};
2537
Guido van Rossumb6775db1994-08-01 11:34:53 +00002538#ifndef macintosh
Guido van Rossumcacd9571993-10-18 11:44:47 +00002539static int
2540checkstringlist(args, ps, pn)
2541 object *args;
2542 char ***ps;
2543 int *pn;
2544{
2545 int i, n;
2546 char **s;
2547 if (!is_listobject(args)) {
2548 err_setstr(TypeError, "list of strings expected");
2549 return 0;
2550 }
2551 n = getlistsize(args);
2552 s = NEW(char *, n+1);
2553 if (s == NULL) {
2554 err_nomem();
2555 return 0;
2556 }
2557 for (i = 0; i < n; i++) {
2558 object *item = getlistitem(args, i);
2559 if (!is_stringobject(item)) {
2560 err_setstr(TypeError, "list of strings expected");
2561 return 0;
2562 }
2563 s[i] = getstringvalue(item);
2564 }
2565 s[n] = NULL; /* In case caller wants a NULL-terminated list */
2566 *ps = s;
2567 *pn = n;
2568 return 1;
2569}
2570
2571static int
2572putbackstringlist(list, s, n)
2573 object *list;
2574 char **s;
2575 int n;
2576{
2577 int oldsize = getlistsize(list);
2578 object *newlist;
2579 int i;
2580 if (n == oldsize)
2581 return 1;
2582 newlist = newlistobject(n);
2583 for (i = 0; i < n && newlist != NULL; i++) {
2584 object *item = newstringobject(s[i]);
2585 if (item == NULL) {
2586 DECREF(newlist);
2587 newlist = NULL;
2588 }
2589 else
2590 setlistitem(newlist, i, item);
2591 }
2592 if (newlist == NULL)
2593 return 0;
2594 (*list->ob_type->tp_as_sequence->sq_ass_slice)
2595 (list, 0, oldsize, newlist);
2596 DECREF(newlist);
2597 return 1;
2598}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002599#endif /* macintosh */
Guido van Rossumcacd9571993-10-18 11:44:47 +00002600
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002601void
2602initstdwin()
2603{
Guido van Rossumbbf94341991-12-16 15:44:53 +00002604 object *m, *d;
2605 static int inited = 0;
2606
Guido van Rossum2d14e211991-02-19 12:26:49 +00002607 if (!inited) {
Guido van Rossumb6775db1994-08-01 11:34:53 +00002608#ifdef macintosh
2609 winit();
Jack Jansend56c1091995-01-27 14:44:16 +00002610 PyMac_DoYieldEnabled = 0;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002611#else
Guido van Rossum39cb5ce1995-01-26 00:37:10 +00002612 char buf[1000];
Guido van Rossumcacd9571993-10-18 11:44:47 +00002613 int argc = 0;
2614 char **argv = NULL;
2615 object *sys_argv = sysget("argv");
2616 if (sys_argv != NULL) {
2617 if (!checkstringlist(sys_argv, &argv, &argc))
2618 err_clear();
2619 }
Guido van Rossumc45611d1993-11-17 22:58:56 +00002620 if (argc > 0) {
2621 /* If argv[0] has a ".py" suffix, remove the suffix */
2622 char *p = strrchr(argv[0], '.');
2623 if (p != NULL && strcmp(p, ".py") == 0) {
2624 int n = p - argv[0];
2625 if (n >= sizeof(buf))
2626 n = sizeof(buf)-1;
2627 strncpy(buf, argv[0], n);
2628 buf[n] = '\0';
2629 argv[0] = buf;
2630 }
2631 }
Guido van Rossumcacd9571993-10-18 11:44:47 +00002632 winitargs(&argc, &argv);
2633 if (argv != NULL) {
2634 if (!putbackstringlist(sys_argv, argv, argc))
2635 err_clear();
2636 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002637#endif
Guido van Rossum2d14e211991-02-19 12:26:49 +00002638 inited = 1;
2639 }
Guido van Rossumbbf94341991-12-16 15:44:53 +00002640 m = initmodule("stdwin", stdwin_methods);
2641 d = getmoduledict(m);
2642
2643 /* Initialize stdwin.error exception */
2644 StdwinError = newstringobject("stdwin.error");
2645 if (StdwinError == NULL || dictinsert(d, "error", StdwinError) != 0)
2646 fatal("can't define stdwin.error");
Guido van Rossumb6775db1994-08-01 11:34:53 +00002647#ifdef WITH_THREAD
Guido van Rossumff4949e1992-08-05 19:58:53 +00002648 StdwinLock = allocate_lock();
2649 if (StdwinLock == NULL)
2650 fatal("can't allocate stdwin lock");
2651#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002652}