blob: eb411123fc7f8e13fc35ff983daa568f95d0e273 [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
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000107/* Window and menu object types declared here because of forward references */
108
109typedef struct {
110 OB_HEAD
111 object *w_title;
112 WINDOW *w_win;
113 object *w_attr; /* Attributes dictionary */
114} windowobject;
115
Guido van Rossumb6775db1994-08-01 11:34:53 +0000116staticforward typeobject Windowtype;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000117
118#define is_windowobject(wp) ((wp)->ob_type == &Windowtype)
119
120typedef struct {
121 OB_HEAD
122 MENU *m_menu;
123 int m_id;
124 object *m_attr; /* Attributes dictionary */
125} menuobject;
126
Guido van Rossumb6775db1994-08-01 11:34:53 +0000127staticforward typeobject Menutype;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000128
129#define is_menuobject(mp) ((mp)->ob_type == &Menutype)
130
Guido van Rossumbf80e541993-02-08 15:49:17 +0000131typedef struct {
132 OB_HEAD
133 BITMAP *b_bitmap;
134 object *b_attr; /* Attributes dictionary */
135} bitmapobject;
136
Guido van Rossumb6775db1994-08-01 11:34:53 +0000137staticforward typeobject Bitmaptype;
Guido van Rossumbf80e541993-02-08 15:49:17 +0000138
139#define is_bitmapobject(mp) ((mp)->ob_type == &Bitmaptype)
140
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000141
142/* Strongly stdwin-specific argument handlers */
143
144static int
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000145getmenudetail(v, ep)
146 object *v;
147 EVENT *ep;
148{
Guido van Rossumfc58e581992-01-27 16:45:55 +0000149 menuobject *mp;
150 if (!getargs(v, "(Oi)", &mp, &ep->u.m.item))
151 return 0;
152 if (!is_menuobject(mp))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000153 return err_badarg();
Guido van Rossumfc58e581992-01-27 16:45:55 +0000154 ep->u.m.id = mp->m_id;
155 return 1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000156}
157
158static int
159geteventarg(v, ep)
160 object *v;
161 EVENT *ep;
162{
163 object *wp, *detail;
164 int a[4];
Guido van Rossumfc58e581992-01-27 16:45:55 +0000165 if (!getargs(v, "(iOO)", &ep->type, &wp, &detail))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000166 return 0;
Guido van Rossumfc58e581992-01-27 16:45:55 +0000167 if (is_windowobject(wp))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000168 ep->window = ((windowobject *)wp) -> w_win;
Guido van Rossumfc58e581992-01-27 16:45:55 +0000169 else if (wp == None)
170 ep->window = NULL;
171 else
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000172 return err_badarg();
Guido van Rossumfc58e581992-01-27 16:45:55 +0000173 switch (ep->type) {
174 case WE_CHAR: {
175 char c;
176 if (!getargs(detail, "c", &c))
177 return 0;
178 ep->u.character = c;
179 return 1;
180 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000181 case WE_COMMAND:
182 return getintarg(detail, &ep->u.command);
183 case WE_DRAW:
184 if (!getrectarg(detail, a))
185 return 0;
186 ep->u.area.left = a[0];
187 ep->u.area.top = a[1];
188 ep->u.area.right = a[2];
189 ep->u.area.bottom = a[3];
190 return 1;
191 case WE_MOUSE_DOWN:
192 case WE_MOUSE_UP:
193 case WE_MOUSE_MOVE:
Guido van Rossumfc58e581992-01-27 16:45:55 +0000194 return getargs(detail, "((ii)iii)",
195 &ep->u.where.h, &ep->u.where.v,
196 &ep->u.where.clicks,
197 &ep->u.where.button,
198 &ep->u.where.mask);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000199 case WE_MENU:
200 return getmenudetail(detail, ep);
Guido van Rossum3ee199e1992-06-30 12:48:26 +0000201 case WE_KEY:
202 return getargs(detail, "(ii)",
203 &ep->u.key.code, &ep->u.key.mask);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000204 default:
205 return 1;
206 }
207}
208
209
210/* Return construction tools */
211
212static object *
213makepoint(a, b)
214 int a, b;
215{
Guido van Rossum2ee12f41992-04-13 15:54:35 +0000216 return mkvalue("(ii)", a, b);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000217}
218
219static object *
220makerect(a, b, c, d)
221 int a, b, c, d;
222{
Guido van Rossum2ee12f41992-04-13 15:54:35 +0000223 return mkvalue("((ii)(ii))", a, b, c, d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000224}
225
226
227/* Drawing objects */
228
229typedef struct {
230 OB_HEAD
231 windowobject *d_ref;
232} drawingobject;
233
234static drawingobject *Drawing; /* Set to current drawing object, or NULL */
235
236/* Drawing methods */
237
Guido van Rossum3c284741991-11-27 14:54:54 +0000238static object *
239drawing_close(dp)
240 drawingobject *dp;
241{
242 if (dp->d_ref != NULL) {
243 wenddrawing(dp->d_ref->w_win);
244 Drawing = NULL;
245 DECREF(dp->d_ref);
246 dp->d_ref = NULL;
247 }
248 INCREF(None);
249 return None;
250}
Guido van Rossum77b46041992-01-14 18:41:24 +0000251
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000252static void
253drawing_dealloc(dp)
254 drawingobject *dp;
255{
Guido van Rossum3c284741991-11-27 14:54:54 +0000256 if (dp->d_ref != NULL) {
257 wenddrawing(dp->d_ref->w_win);
258 Drawing = NULL;
259 DECREF(dp->d_ref);
260 dp->d_ref = NULL;
261 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000262 free((char *)dp);
263}
264
265static object *
266drawing_generic(dp, args, func)
267 drawingobject *dp;
268 object *args;
269 void (*func) FPROTO((int, int, int, int));
270{
271 int a[4];
272 if (!getrectarg(args, a))
273 return NULL;
274 (*func)(a[0], a[1], a[2], a[3]);
275 INCREF(None);
276 return None;
277}
278
279static object *
280drawing_line(dp, args)
281 drawingobject *dp;
282 object *args;
283{
Guido van Rossumbf109731991-03-06 13:14:12 +0000284 return drawing_generic(dp, args, wdrawline);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000285}
286
287static object *
288drawing_xorline(dp, args)
289 drawingobject *dp;
290 object *args;
291{
Guido van Rossumbf109731991-03-06 13:14:12 +0000292 return drawing_generic(dp, args, wxorline);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000293}
294
295static object *
296drawing_circle(dp, args)
297 drawingobject *dp;
298 object *args;
299{
300 int a[3];
301 if (!getpointintarg(args, a))
302 return NULL;
303 wdrawcircle(a[0], a[1], a[2]);
304 INCREF(None);
305 return None;
306}
Guido van Rossuma2a181a1991-05-14 12:09:25 +0000307
Guido van Rossum27201061991-04-16 08:43:03 +0000308static object *
309drawing_fillcircle(dp, args)
310 drawingobject *dp;
311 object *args;
312{
313 int a[3];
314 if (!getpointintarg(args, a))
315 return NULL;
316 wfillcircle(a[0], a[1], a[2]);
317 INCREF(None);
318 return None;
319}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000320
321static object *
Guido van Rossuma2a181a1991-05-14 12:09:25 +0000322drawing_xorcircle(dp, args)
323 drawingobject *dp;
324 object *args;
325{
326 int a[3];
327 if (!getpointintarg(args, a))
328 return NULL;
329 wxorcircle(a[0], a[1], a[2]);
330 INCREF(None);
331 return None;
332}
333
334static object *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000335drawing_elarc(dp, args)
336 drawingobject *dp;
337 object *args;
338{
339 int a[6];
340 if (!get3pointarg(args, a))
341 return NULL;
342 wdrawelarc(a[0], a[1], a[2], a[3], a[4], a[5]);
343 INCREF(None);
344 return None;
345}
346
347static object *
Guido van Rossum27201061991-04-16 08:43:03 +0000348drawing_fillelarc(dp, args)
349 drawingobject *dp;
350 object *args;
351{
352 int a[6];
353 if (!get3pointarg(args, a))
354 return NULL;
355 wfillelarc(a[0], a[1], a[2], a[3], a[4], a[5]);
356 INCREF(None);
357 return None;
358}
359
360static object *
Guido van Rossuma2a181a1991-05-14 12:09:25 +0000361drawing_xorelarc(dp, args)
362 drawingobject *dp;
363 object *args;
364{
365 int a[6];
366 if (!get3pointarg(args, a))
367 return NULL;
368 wxorelarc(a[0], a[1], a[2], a[3], a[4], a[5]);
369 INCREF(None);
370 return None;
371}
372
373static object *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000374drawing_box(dp, args)
375 drawingobject *dp;
376 object *args;
377{
Guido van Rossumbf109731991-03-06 13:14:12 +0000378 return drawing_generic(dp, args, wdrawbox);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000379}
380
381static object *
382drawing_erase(dp, args)
383 drawingobject *dp;
384 object *args;
385{
Guido van Rossumbf109731991-03-06 13:14:12 +0000386 return drawing_generic(dp, args, werase);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000387}
388
389static object *
390drawing_paint(dp, args)
391 drawingobject *dp;
392 object *args;
393{
Guido van Rossumbf109731991-03-06 13:14:12 +0000394 return drawing_generic(dp, args, wpaint);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000395}
396
397static object *
398drawing_invert(dp, args)
399 drawingobject *dp;
400 object *args;
401{
Guido van Rossumbf109731991-03-06 13:14:12 +0000402 return drawing_generic(dp, args, winvert);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000403}
404
Guido van Rossum27201061991-04-16 08:43:03 +0000405static POINT *
406getpointsarray(v, psize)
407 object *v;
408 int *psize;
409{
410 int n = -1;
411 object * (*getitem) PROTO((object *, int));
412 int i;
413 POINT *points;
414
415 if (v == NULL)
416 ;
417 else if (is_listobject(v)) {
418 n = getlistsize(v);
419 getitem = getlistitem;
420 }
421 else if (is_tupleobject(v)) {
422 n = gettuplesize(v);
423 getitem = gettupleitem;
424 }
425
426 if (n <= 0) {
427 (void) err_badarg();
428 return NULL;
429 }
430
431 points = NEW(POINT, n);
432 if (points == NULL) {
433 (void) err_nomem();
434 return NULL;
435 }
436
437 for (i = 0; i < n; i++) {
438 object *w = (*getitem)(v, i);
439 int a[2];
440 if (!getpointarg(w, a)) {
441 DEL(points);
442 return NULL;
443 }
444 points[i].h = a[0];
445 points[i].v = a[1];
446 }
447
448 *psize = n;
449 return points;
450}
451
452static object *
453drawing_poly(dp, args)
454 drawingobject *dp;
455 object *args;
456{
457 int n;
458 POINT *points = getpointsarray(args, &n);
459 if (points == NULL)
460 return NULL;
461 wdrawpoly(n, points);
462 DEL(points);
463 INCREF(None);
464 return None;
465}
466
467static object *
468drawing_fillpoly(dp, args)
469 drawingobject *dp;
470 object *args;
471{
472 int n;
473 POINT *points = getpointsarray(args, &n);
474 if (points == NULL)
475 return NULL;
476 wfillpoly(n, points);
477 DEL(points);
478 INCREF(None);
479 return None;
480}
481
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000482static object *
Guido van Rossuma2a181a1991-05-14 12:09:25 +0000483drawing_xorpoly(dp, args)
484 drawingobject *dp;
485 object *args;
486{
487 int n;
488 POINT *points = getpointsarray(args, &n);
489 if (points == NULL)
490 return NULL;
491 wxorpoly(n, points);
492 DEL(points);
493 INCREF(None);
494 return None;
495}
496
497static object *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000498drawing_cliprect(dp, args)
499 drawingobject *dp;
500 object *args;
501{
Guido van Rossumbf109731991-03-06 13:14:12 +0000502 return drawing_generic(dp, args, wcliprect);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000503}
504
505static object *
506drawing_noclip(dp, args)
507 drawingobject *dp;
508 object *args;
509{
510 if (!getnoarg(args))
511 return NULL;
512 wnoclip();
513 INCREF(None);
514 return None;
515}
516
517static object *
518drawing_shade(dp, args)
519 drawingobject *dp;
520 object *args;
521{
522 int a[5];
523 if (!getrectintarg(args, a))
524 return NULL;
525 wshade(a[0], a[1], a[2], a[3], a[4]);
526 INCREF(None);
527 return None;
528}
529
530static object *
531drawing_text(dp, args)
532 drawingobject *dp;
533 object *args;
534{
Guido van Rossumfc58e581992-01-27 16:45:55 +0000535 int h, v, size;
536 char *text;
537 if (!getargs(args, "((ii)s#)", &h, &v, &text, &size))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000538 return NULL;
Guido van Rossumfc58e581992-01-27 16:45:55 +0000539 wdrawtext(h, v, text, size);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000540 INCREF(None);
541 return None;
542}
543
544/* The following four are also used as stdwin functions */
545
546static object *
547drawing_lineheight(dp, args)
548 drawingobject *dp;
549 object *args;
550{
551 if (!getnoarg(args))
552 return NULL;
553 return newintobject((long)wlineheight());
554}
555
556static object *
557drawing_baseline(dp, args)
558 drawingobject *dp;
559 object *args;
560{
561 if (!getnoarg(args))
562 return NULL;
563 return newintobject((long)wbaseline());
564}
565
566static object *
567drawing_textwidth(dp, args)
568 drawingobject *dp;
569 object *args;
570{
Guido van Rossumfc58e581992-01-27 16:45:55 +0000571 char *text;
572 int size;
573 if (!getargs(args, "s#", &text, &size))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000574 return NULL;
Guido van Rossumfc58e581992-01-27 16:45:55 +0000575 return newintobject((long)wtextwidth(text, size));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000576}
577
578static object *
579drawing_textbreak(dp, args)
580 drawingobject *dp;
581 object *args;
582{
Guido van Rossumfc58e581992-01-27 16:45:55 +0000583 char *text;
584 int size, width;
585 if (!getargs(args, "(s#i)", &text, &size, &width))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000586 return NULL;
Guido van Rossumfc58e581992-01-27 16:45:55 +0000587 return newintobject((long)wtextbreak(text, size, width));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000588}
589
Guido van Rossum0c2290b1991-04-03 19:12:14 +0000590static object *
591drawing_setfont(self, args)
592 drawingobject *self;
593 object *args;
594{
Guido van Rossumfc58e581992-01-27 16:45:55 +0000595 char *font;
596 char style = '\0';
597 int size = 0;
598 if (args == NULL || !is_tupleobject(args)) {
Guido van Rossum3c8ba7a1992-02-05 11:15:00 +0000599 if (!getargs(args, "z", &font))
Guido van Rossum50429a11991-04-04 15:24:07 +0000600 return NULL;
601 }
602 else {
Guido van Rossumfc58e581992-01-27 16:45:55 +0000603 int n = gettuplesize(args);
604 if (n == 2) {
605 if (!getargs(args, "(zi)", &font, &size))
606 return NULL;
607 }
608 else if (!getargs(args, "(zic)", &font, &size, &style)) {
609 err_clear();
610 if (!getargs(args, "(zci)", &font, &style, &size))
611 return NULL;
Guido van Rossum50429a11991-04-04 15:24:07 +0000612 }
613 }
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000614 if (font != NULL) {
615 if (!wsetfont(font)) {
616 err_setstr(StdwinError, "font not found");
617 return NULL;
618 }
619 }
Guido van Rossum50429a11991-04-04 15:24:07 +0000620 if (size != 0)
621 wsetsize(size);
Guido van Rossumfc58e581992-01-27 16:45:55 +0000622 switch (style) {
623 case 'b':
624 wsetbold();
625 break;
626 case 'i':
627 wsetitalic();
628 break;
629 case 'o':
630 wsetbolditalic();
631 break;
632 case 'u':
633 wsetunderline();
634 break;
635 case 'p':
636 wsetplain();
637 break;
638 }
Guido van Rossum0c2290b1991-04-03 19:12:14 +0000639 INCREF(None);
640 return None;
641}
642
643static object *
644drawing_getbgcolor(self, args)
645 object *self;
646 object *args;
647{
648 if (!getnoarg(args))
649 return NULL;
650 return newintobject((long)wgetbgcolor());
651}
652
653static object *
654drawing_getfgcolor(self, args)
655 object *self;
656 object *args;
657{
658 if (!getnoarg(args))
659 return NULL;
660 return newintobject((long)wgetfgcolor());
661}
662
663static object *
664drawing_setbgcolor(self, args)
665 object *self;
666 object *args;
667{
668 long color;
669 if (!getlongarg(args, &color))
670 return NULL;
671 wsetbgcolor((COLOR)color);
672 INCREF(None);
673 return None;
674}
675
676static object *
677drawing_setfgcolor(self, args)
678 object *self;
679 object *args;
680{
681 long color;
682 if (!getlongarg(args, &color))
683 return NULL;
684 wsetfgcolor((COLOR)color);
685 INCREF(None);
686 return None;
687}
688
Guido van Rossume9066061993-07-29 13:14:32 +0000689#ifdef HAVE_BITMAPS
690
Guido van Rossumbf80e541993-02-08 15:49:17 +0000691static object *
692drawing_bitmap(self, args)
693 object *self;
694 object *args;
695{
696 int h, v;
697 object *bp;
698 object *mask = NULL;
699 if (!getargs(args, "((ii)O)", &h, &v, &bp)) {
700 err_clear();
701 if (!getargs(args, "((ii)OO)", &h, &v, &bp, &mask))
702 return NULL;
703 if (mask == None)
704 mask = NULL;
705 else if (!is_bitmapobject(mask)) {
706 err_badarg();
707 return NULL;
708 }
709 }
710 if (!is_bitmapobject(bp)) {
711 err_badarg();
712 return NULL;
713 }
714 if (((bitmapobject *)bp)->b_bitmap == NULL ||
715 mask != NULL && ((bitmapobject *)mask)->b_bitmap == NULL) {
716 err_setstr(StdwinError, "bitmap object already close");
717 return NULL;
718 }
719 if (mask == NULL)
720 wdrawbitmap(h, v, ((bitmapobject *)bp)->b_bitmap, ALLBITS);
721 else
722 wdrawbitmap(h, v,
723 ((bitmapobject *)bp)->b_bitmap,
724 ((bitmapobject *)bp)->b_bitmap);
725 INCREF(None);
726 return None;
727}
728
Guido van Rossume9066061993-07-29 13:14:32 +0000729#endif /* HAVE_BITMAPS */
730
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000731static struct methodlist drawing_methods[] = {
Guido van Rossume9066061993-07-29 13:14:32 +0000732#ifdef HAVE_BITMAPS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000733 {"bitmap", (method)drawing_bitmap},
Guido van Rossume9066061993-07-29 13:14:32 +0000734#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000735 {"box", (method)drawing_box},
736 {"circle", (method)drawing_circle},
737 {"cliprect", (method)drawing_cliprect},
738 {"close", (method)drawing_close},
739 {"elarc", (method)drawing_elarc},
740 {"enddrawing", (method)drawing_close},
741 {"erase", (method)drawing_erase},
742 {"fillcircle", (method)drawing_fillcircle},
743 {"fillelarc", (method)drawing_fillelarc},
744 {"fillpoly", (method)drawing_fillpoly},
745 {"invert", (method)drawing_invert},
746 {"line", (method)drawing_line},
747 {"noclip", (method)drawing_noclip},
748 {"paint", (method)drawing_paint},
749 {"poly", (method)drawing_poly},
750 {"shade", (method)drawing_shade},
751 {"text", (method)drawing_text},
752 {"xorcircle", (method)drawing_xorcircle},
753 {"xorelarc", (method)drawing_xorelarc},
754 {"xorline", (method)drawing_xorline},
755 {"xorpoly", (method)drawing_xorpoly},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000756
757 /* Text measuring methods: */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000758 {"baseline", (method)drawing_baseline},
759 {"lineheight", (method)drawing_lineheight},
760 {"textbreak", (method)drawing_textbreak},
761 {"textwidth", (method)drawing_textwidth},
Guido van Rossum0c2290b1991-04-03 19:12:14 +0000762
763 /* Font setting methods: */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000764 {"setfont", (method)drawing_setfont},
Guido van Rossum0c2290b1991-04-03 19:12:14 +0000765
766 /* Color methods: */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000767 {"getbgcolor", (method)drawing_getbgcolor},
768 {"getfgcolor", (method)drawing_getfgcolor},
769 {"setbgcolor", (method)drawing_setbgcolor},
770 {"setfgcolor", (method)drawing_setfgcolor},
Guido van Rossum0c2290b1991-04-03 19:12:14 +0000771
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000772 {NULL, NULL} /* sentinel */
773};
774
775static object *
Guido van Rossum77b46041992-01-14 18:41:24 +0000776drawing_getattr(dp, name)
777 drawingobject *dp;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000778 char *name;
779{
Guido van Rossum77b46041992-01-14 18:41:24 +0000780 if (dp->d_ref == NULL) {
781 err_setstr(StdwinError, "drawing object already closed");
782 return NULL;
783 }
784 return findmethod(drawing_methods, (object *)dp, name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000785}
786
Guido van Rossum541c8c01991-05-05 20:13:41 +0000787typeobject Drawingtype = {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000788 OB_HEAD_INIT(&Typetype)
789 0, /*ob_size*/
790 "drawing", /*tp_name*/
791 sizeof(drawingobject), /*tp_size*/
792 0, /*tp_itemsize*/
793 /* methods */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000794 (destructor)drawing_dealloc, /*tp_dealloc*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000795 0, /*tp_print*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000796 (getattrfunc)drawing_getattr, /*tp_getattr*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000797 0, /*tp_setattr*/
798 0, /*tp_compare*/
799 0, /*tp_repr*/
800};
801
802
803/* Text(edit) objects */
804
805typedef struct {
806 OB_HEAD
807 TEXTEDIT *t_text;
808 windowobject *t_ref;
809 object *t_attr; /* Attributes dictionary */
810} textobject;
811
Guido van Rossumb6775db1994-08-01 11:34:53 +0000812staticforward typeobject Texttype;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000813
814static textobject *
815newtextobject(wp, left, top, right, bottom)
816 windowobject *wp;
817 int left, top, right, bottom;
818{
819 textobject *tp;
820 tp = NEWOBJ(textobject, &Texttype);
821 if (tp == NULL)
822 return NULL;
823 tp->t_attr = NULL;
824 INCREF(wp);
825 tp->t_ref = wp;
826 tp->t_text = tecreate(wp->w_win, left, top, right, bottom);
827 if (tp->t_text == NULL) {
828 DECREF(tp);
829 return (textobject *) err_nomem();
830 }
831 return tp;
832}
833
834/* Text(edit) methods */
835
836static void
837text_dealloc(tp)
838 textobject *tp;
839{
840 if (tp->t_text != NULL)
841 tefree(tp->t_text);
Guido van Rossum3c284741991-11-27 14:54:54 +0000842 XDECREF(tp->t_attr);
843 XDECREF(tp->t_ref);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000844 DEL(tp);
845}
846
847static object *
Guido van Rossum3c284741991-11-27 14:54:54 +0000848text_close(tp, args)
849 textobject *tp;
850 object *args;
851{
852 if (tp->t_text != NULL) {
853 tefree(tp->t_text);
854 tp->t_text = NULL;
855 }
856 if (tp->t_attr != NULL) {
857 DECREF(tp->t_attr);
858 tp->t_attr = NULL;
859 }
860 if (tp->t_ref != NULL) {
861 DECREF(tp->t_ref);
862 tp->t_ref = NULL;
863 }
864 INCREF(None);
865 return None;
866}
867
868static object *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000869text_arrow(self, args)
870 textobject *self;
871 object *args;
872{
873 int code;
874 if (!getintarg(args, &code))
875 return NULL;
876 tearrow(self->t_text, code);
877 INCREF(None);
878 return None;
879}
880
881static object *
882text_draw(self, args)
883 textobject *self;
884 object *args;
885{
886 register TEXTEDIT *tp = self->t_text;
887 int a[4];
888 int left, top, right, bottom;
889 if (!getrectarg(args, a))
890 return NULL;
891 if (Drawing != NULL) {
Guido van Rossum87e7ea71991-12-10 14:00:03 +0000892 err_setstr(StdwinError, "already drawing");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000893 return NULL;
894 }
895 /* Clip to text area and ignore if area is empty */
896 left = tegetleft(tp);
897 top = tegettop(tp);
898 right = tegetright(tp);
899 bottom = tegetbottom(tp);
900 if (a[0] < left) a[0] = left;
901 if (a[1] < top) a[1] = top;
902 if (a[2] > right) a[2] = right;
903 if (a[3] > bottom) a[3] = bottom;
904 if (a[0] < a[2] && a[1] < a[3]) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000905 wbegindrawing(self->t_ref->w_win);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000906 tedrawnew(tp, a[0], a[1], a[2], a[3]);
907 wenddrawing(self->t_ref->w_win);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000908 }
909 INCREF(None);
910 return None;
911}
912
913static object *
914text_event(self, args)
915 textobject *self;
916 object *args;
917{
918 register TEXTEDIT *tp = self->t_text;
919 EVENT e;
920 if (!geteventarg(args, &e))
921 return NULL;
922 if (e.type == WE_MOUSE_DOWN) {
Guido van Rossum33f17701991-02-13 23:19:39 +0000923 /* Cheat at the margins */
924 int width, height;
925 wgetdocsize(e.window, &width, &height);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000926 if (e.u.where.h < 0 && tegetleft(tp) == 0)
927 e.u.where.h = 0;
Guido van Rossum33f17701991-02-13 23:19:39 +0000928 else if (e.u.where.h > width && tegetright(tp) == width)
929 e.u.where.h = width;
930 if (e.u.where.v < 0 && tegettop(tp) == 0)
931 e.u.where.v = 0;
932 else if (e.u.where.v > height && tegetright(tp) == height)
933 e.u.where.v = height;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000934 }
935 return newintobject((long) teevent(tp, &e));
936}
937
938static object *
939text_getfocus(self, args)
940 textobject *self;
941 object *args;
942{
943 if (!getnoarg(args))
944 return NULL;
945 return makepoint(tegetfoc1(self->t_text), tegetfoc2(self->t_text));
946}
947
948static object *
949text_getfocustext(self, args)
950 textobject *self;
951 object *args;
952{
953 int f1, f2;
954 char *text;
955 if (!getnoarg(args))
956 return NULL;
957 f1 = tegetfoc1(self->t_text);
958 f2 = tegetfoc2(self->t_text);
959 text = tegettext(self->t_text);
960 return newsizedstringobject(text + f1, f2-f1);
961}
962
963static object *
964text_getrect(self, args)
965 textobject *self;
966 object *args;
967{
968 if (!getnoarg(args))
969 return NULL;
970 return makerect(tegetleft(self->t_text),
971 tegettop(self->t_text),
972 tegetright(self->t_text),
973 tegetbottom(self->t_text));
974}
975
976static object *
977text_gettext(self, args)
978 textobject *self;
979 object *args;
980{
981 if (!getnoarg(args))
982 return NULL;
983 return newsizedstringobject(tegettext(self->t_text),
984 tegetlen(self->t_text));
985}
986
987static object *
988text_move(self, args)
989 textobject *self;
990 object *args;
991{
992 int a[4];
993 if (!getrectarg(args, a))
994 return NULL;
995 temovenew(self->t_text, a[0], a[1], a[2], a[3]);
996 INCREF(None);
997 return None;
998}
999
1000static object *
Guido van Rossum4b9cf8e1991-05-28 21:57:04 +00001001text_replace(self, args)
1002 textobject *self;
1003 object *args;
1004{
Guido van Rossumfc58e581992-01-27 16:45:55 +00001005 char *text;
Guido van Rossum4b9cf8e1991-05-28 21:57:04 +00001006 if (!getstrarg(args, &text))
1007 return NULL;
Guido van Rossumfc58e581992-01-27 16:45:55 +00001008 tereplace(self->t_text, text);
Guido van Rossum4b9cf8e1991-05-28 21:57:04 +00001009 INCREF(None);
1010 return None;
1011}
1012
1013static object *
1014text_setactive(self, args)
1015 textobject *self;
1016 object *args;
1017{
1018 int flag;
1019 if (!getintarg(args, &flag))
1020 return NULL;
1021 tesetactive(self->t_text, flag);
1022 INCREF(None);
1023 return None;
1024}
1025
1026static object *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001027text_setfocus(self, args)
1028 textobject *self;
1029 object *args;
1030{
1031 int a[2];
1032 if (!getpointarg(args, a))
1033 return NULL;
1034 tesetfocus(self->t_text, a[0], a[1]);
1035 INCREF(None);
1036 return None;
1037}
1038
1039static object *
Guido van Rossum541c8c01991-05-05 20:13:41 +00001040text_settext(self, args)
1041 textobject *self;
1042 object *args;
1043{
Guido van Rossumfc58e581992-01-27 16:45:55 +00001044 char *text;
Guido van Rossum541c8c01991-05-05 20:13:41 +00001045 char *buf;
1046 int size;
Guido van Rossumfc58e581992-01-27 16:45:55 +00001047 if (!getargs(args, "s#", &text, &size))
Guido van Rossum541c8c01991-05-05 20:13:41 +00001048 return NULL;
Guido van Rossum541c8c01991-05-05 20:13:41 +00001049 if ((buf = NEW(char, size)) == NULL) {
Guido van Rossum87e7ea71991-12-10 14:00:03 +00001050 return err_nomem();
Guido van Rossum541c8c01991-05-05 20:13:41 +00001051 }
Guido van Rossumfc58e581992-01-27 16:45:55 +00001052 memcpy(buf, text, size);
Guido van Rossum541c8c01991-05-05 20:13:41 +00001053 tesetbuf(self->t_text, buf, size); /* Becomes owner of buffer */
1054 INCREF(None);
1055 return None;
1056}
1057
1058static object *
Guido van Rossum4b9cf8e1991-05-28 21:57:04 +00001059text_setview(self, args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001060 textobject *self;
1061 object *args;
1062{
Guido van Rossum4b9cf8e1991-05-28 21:57:04 +00001063 int a[4];
1064 if (args == None)
1065 tenoview(self->t_text);
1066 else {
1067 if (!getrectarg(args, a))
1068 return NULL;
1069 tesetview(self->t_text, a[0], a[1], a[2], a[3]);
1070 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001071 INCREF(None);
1072 return None;
1073}
1074
1075static struct methodlist text_methods[] = {
Guido van Rossumb6775db1994-08-01 11:34:53 +00001076 {"arrow", (method)text_arrow},
1077 {"close", (method)text_close},
1078 {"draw", (method)text_draw},
1079 {"event", (method)text_event},
1080 {"getfocus", (method)text_getfocus},
1081 {"getfocustext",(method)text_getfocustext},
1082 {"getrect", (method)text_getrect},
1083 {"gettext", (method)text_gettext},
1084 {"move", (method)text_move},
1085 {"replace", (method)text_replace},
1086 {"setactive", (method)text_setactive},
1087 {"setfocus", (method)text_setfocus},
1088 {"settext", (method)text_settext},
1089 {"setview", (method)text_setview},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001090 {NULL, NULL} /* sentinel */
1091};
1092
1093static object *
1094text_getattr(tp, name)
1095 textobject *tp;
1096 char *name;
1097{
Guido van Rossum85f50761991-10-20 20:22:50 +00001098 object *v = NULL;
Guido van Rossum77b46041992-01-14 18:41:24 +00001099 if (tp->t_ref == NULL) {
1100 err_setstr(StdwinError, "text object already closed");
1101 return NULL;
1102 }
Guido van Rossum85f50761991-10-20 20:22:50 +00001103 if (strcmp(name, "__dict__") == 0) {
1104 v = tp->t_attr;
1105 if (v == NULL)
1106 v = None;
1107 }
1108 else if (tp->t_attr != NULL) {
1109 v = dictlookup(tp->t_attr, name);
1110 }
1111 if (v != NULL) {
1112 INCREF(v);
1113 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001114 }
1115 return findmethod(text_methods, (object *)tp, name);
1116}
1117
1118static int
1119text_setattr(tp, name, v)
1120 textobject *tp;
1121 char *name;
1122 object *v;
1123{
1124 if (tp->t_attr == NULL) {
1125 tp->t_attr = newdictobject();
1126 if (tp->t_attr == NULL)
1127 return -1;
1128 }
Guido van Rossum94472a01992-09-04 09:45:18 +00001129 if (v == NULL) {
1130 int rv = dictremove(tp->t_attr, name);
1131 if (rv < 0)
1132 err_setstr(AttributeError,
1133 "delete non-existing text object attribute");
1134 return rv;
1135 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001136 else
1137 return dictinsert(tp->t_attr, name, v);
1138}
1139
Guido van Rossumb6775db1994-08-01 11:34:53 +00001140static typeobject Texttype = {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001141 OB_HEAD_INIT(&Typetype)
1142 0, /*ob_size*/
1143 "textedit", /*tp_name*/
1144 sizeof(textobject), /*tp_size*/
1145 0, /*tp_itemsize*/
1146 /* methods */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001147 (destructor)text_dealloc, /*tp_dealloc*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001148 0, /*tp_print*/
Guido van Rossumb6775db1994-08-01 11:34:53 +00001149 (getattrfunc)text_getattr, /*tp_getattr*/
1150 (setattrfunc)text_setattr, /*tp_setattr*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001151 0, /*tp_compare*/
1152 0, /*tp_repr*/
1153};
1154
1155
1156/* Menu objects */
1157
Guido van Rossum2d14e211991-02-19 12:26:49 +00001158#define IDOFFSET 10 /* Menu IDs we use start here */
Guido van Rossum27201061991-04-16 08:43:03 +00001159#define MAXNMENU 200 /* Max #menus we allow */
Guido van Rossum2d14e211991-02-19 12:26:49 +00001160static menuobject *menulist[MAXNMENU];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001161
Guido van Rossumfc58e581992-01-27 16:45:55 +00001162static menuobject *newmenuobject PROTO((char *));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001163static menuobject *
1164newmenuobject(title)
Guido van Rossumfc58e581992-01-27 16:45:55 +00001165 char *title;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001166{
1167 int id;
1168 MENU *menu;
1169 menuobject *mp;
Guido van Rossum2d14e211991-02-19 12:26:49 +00001170 for (id = 0; id < MAXNMENU; id++) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001171 if (menulist[id] == NULL)
1172 break;
1173 }
Guido van Rossum27201061991-04-16 08:43:03 +00001174 if (id >= MAXNMENU) {
Guido van Rossum87e7ea71991-12-10 14:00:03 +00001175 err_setstr(StdwinError, "creating too many menus");
Guido van Rossum27201061991-04-16 08:43:03 +00001176 return NULL;
1177 }
Guido van Rossumfc58e581992-01-27 16:45:55 +00001178 menu = wmenucreate(id + IDOFFSET, title);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001179 if (menu == NULL)
1180 return (menuobject *) err_nomem();
1181 mp = NEWOBJ(menuobject, &Menutype);
1182 if (mp != NULL) {
1183 mp->m_menu = menu;
Guido van Rossum2d14e211991-02-19 12:26:49 +00001184 mp->m_id = id + IDOFFSET;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001185 mp->m_attr = NULL;
1186 menulist[id] = mp;
1187 }
1188 else
1189 wmenudelete(menu);
1190 return mp;
1191}
1192
1193/* Menu methods */
1194
1195static void
1196menu_dealloc(mp)
1197 menuobject *mp;
1198{
1199
Guido van Rossum2d14e211991-02-19 12:26:49 +00001200 int id = mp->m_id - IDOFFSET;
1201 if (id >= 0 && id < MAXNMENU && menulist[id] == mp) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001202 menulist[id] = NULL;
1203 }
Guido van Rossum77b46041992-01-14 18:41:24 +00001204 if (mp->m_menu != NULL)
1205 wmenudelete(mp->m_menu);
1206 XDECREF(mp->m_attr);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001207 DEL(mp);
1208}
1209
1210static object *
Guido van Rossum77b46041992-01-14 18:41:24 +00001211menu_close(mp, args)
1212 menuobject *mp;
1213 object *args;
1214{
1215 int id = mp->m_id - IDOFFSET;
1216 if (id >= 0 && id < MAXNMENU && menulist[id] == mp) {
1217 menulist[id] = NULL;
1218 }
1219 mp->m_id = -1;
1220 if (mp->m_menu != NULL)
1221 wmenudelete(mp->m_menu);
1222 mp->m_menu = NULL;
1223 XDECREF(mp->m_attr);
1224 mp->m_attr = NULL;
1225 INCREF(None);
1226 return None;
1227}
1228
1229static object *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001230menu_additem(self, args)
1231 menuobject *self;
1232 object *args;
1233{
Guido van Rossumfc58e581992-01-27 16:45:55 +00001234 char *text;
1235 int shortcut = -1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001236 if (is_tupleobject(args)) {
Guido van Rossumfc58e581992-01-27 16:45:55 +00001237 char c;
1238 if (!getargs(args, "(sc)", &text, &c))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001239 return NULL;
Guido van Rossumfc58e581992-01-27 16:45:55 +00001240 shortcut = c;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001241 }
Guido van Rossumfc58e581992-01-27 16:45:55 +00001242 else if (!getstrarg(args, &text))
1243 return NULL;
1244 wmenuadditem(self->m_menu, text, shortcut);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001245 INCREF(None);
1246 return None;
1247}
1248
1249static object *
1250menu_setitem(self, args)
1251 menuobject *self;
1252 object *args;
1253{
1254 int index;
Guido van Rossumfc58e581992-01-27 16:45:55 +00001255 char *text;
Guido van Rossum234f9421993-06-17 12:35:49 +00001256 if (!getargs(args, "(is)", &index, &text))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001257 return NULL;
Guido van Rossumfc58e581992-01-27 16:45:55 +00001258 wmenusetitem(self->m_menu, index, text);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001259 INCREF(None);
1260 return None;
1261}
1262
1263static object *
1264menu_enable(self, args)
1265 menuobject *self;
1266 object *args;
1267{
1268 int index;
1269 int flag;
Guido van Rossum234f9421993-06-17 12:35:49 +00001270 if (!getargs(args, "(ii)", &index, &flag))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001271 return NULL;
1272 wmenuenable(self->m_menu, index, flag);
1273 INCREF(None);
1274 return None;
1275}
1276
1277static object *
1278menu_check(self, args)
1279 menuobject *self;
1280 object *args;
1281{
1282 int index;
1283 int flag;
Guido van Rossum234f9421993-06-17 12:35:49 +00001284 if (!getargs(args, "(ii)", &index, &flag))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001285 return NULL;
1286 wmenucheck(self->m_menu, index, flag);
1287 INCREF(None);
1288 return None;
1289}
1290
1291static struct methodlist menu_methods[] = {
Guido van Rossumb6775db1994-08-01 11:34:53 +00001292 {"additem", (method)menu_additem},
1293 {"setitem", (method)menu_setitem},
1294 {"enable", (method)menu_enable},
1295 {"check", (method)menu_check},
1296 {"close", (method)menu_close},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001297 {NULL, NULL} /* sentinel */
1298};
1299
1300static object *
1301menu_getattr(mp, name)
1302 menuobject *mp;
1303 char *name;
1304{
Guido van Rossum85f50761991-10-20 20:22:50 +00001305 object *v = NULL;
Guido van Rossum77b46041992-01-14 18:41:24 +00001306 if (mp->m_menu == NULL) {
1307 err_setstr(StdwinError, "menu object already closed");
1308 return NULL;
1309 }
Guido van Rossum85f50761991-10-20 20:22:50 +00001310 if (strcmp(name, "__dict__") == 0) {
1311 v = mp->m_attr;
1312 if (v == NULL)
1313 v = None;
1314 }
1315 else if (mp->m_attr != NULL) {
1316 v = dictlookup(mp->m_attr, name);
1317 }
1318 if (v != NULL) {
1319 INCREF(v);
1320 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001321 }
1322 return findmethod(menu_methods, (object *)mp, name);
1323}
1324
1325static int
1326menu_setattr(mp, name, v)
1327 menuobject *mp;
1328 char *name;
1329 object *v;
1330{
1331 if (mp->m_attr == NULL) {
1332 mp->m_attr = newdictobject();
1333 if (mp->m_attr == NULL)
1334 return -1;
1335 }
Guido van Rossum94472a01992-09-04 09:45:18 +00001336 if (v == NULL) {
1337 int rv = dictremove(mp->m_attr, name);
1338 if (rv < 0)
1339 err_setstr(AttributeError,
1340 "delete non-existing menu object attribute");
1341 return rv;
1342 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001343 else
1344 return dictinsert(mp->m_attr, name, v);
1345}
1346
Guido van Rossumb6775db1994-08-01 11:34:53 +00001347static typeobject Menutype = {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001348 OB_HEAD_INIT(&Typetype)
1349 0, /*ob_size*/
1350 "menu", /*tp_name*/
1351 sizeof(menuobject), /*tp_size*/
1352 0, /*tp_itemsize*/
1353 /* methods */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001354 (destructor)menu_dealloc, /*tp_dealloc*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001355 0, /*tp_print*/
Guido van Rossumb6775db1994-08-01 11:34:53 +00001356 (getattrfunc)menu_getattr, /*tp_getattr*/
1357 (setattrfunc)menu_setattr, /*tp_setattr*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001358 0, /*tp_compare*/
1359 0, /*tp_repr*/
1360};
1361
1362
Guido van Rossume9066061993-07-29 13:14:32 +00001363#ifdef HAVE_BITMAPS
1364
Guido van Rossumbf80e541993-02-08 15:49:17 +00001365/* Bitmaps objects */
1366
1367static bitmapobject *newbitmapobject PROTO((int, int));
1368static bitmapobject *
1369newbitmapobject(width, height)
1370 int width, height;
1371{
1372 BITMAP *bitmap;
1373 bitmapobject *bp;
1374 bitmap = wnewbitmap(width, height);
1375 if (bitmap == NULL)
1376 return (bitmapobject *) err_nomem();
1377 bp = NEWOBJ(bitmapobject, &Bitmaptype);
1378 if (bp != NULL) {
1379 bp->b_bitmap = bitmap;
1380 bp->b_attr = NULL;
1381 }
1382 else
1383 wfreebitmap(bitmap);
1384 return bp;
1385}
1386
1387/* Bitmap methods */
1388
1389static void
1390bitmap_dealloc(bp)
1391 bitmapobject *bp;
1392{
1393 if (bp->b_bitmap != NULL)
1394 wfreebitmap(bp->b_bitmap);
1395 XDECREF(bp->b_attr);
1396 DEL(bp);
1397}
1398
1399static object *
1400bitmap_close(bp, args)
1401 bitmapobject *bp;
1402 object *args;
1403{
1404 if (bp->b_bitmap != NULL)
1405 wfreebitmap(bp->b_bitmap);
1406 bp->b_bitmap = NULL;
1407 XDECREF(bp->b_attr);
1408 bp->b_attr = NULL;
1409 INCREF(None);
1410 return None;
1411}
1412
1413static object *
1414bitmap_setbit(self, args)
1415 bitmapobject *self;
1416 object *args;
1417{
1418 int a[3];
1419 if (!getpointintarg(args, a))
1420 return NULL;
1421 wsetbit(self->b_bitmap, a[0], a[1], a[2]);
1422 INCREF(None);
1423 return None;
1424}
1425
1426static object *
1427bitmap_getbit(self, args)
1428 bitmapobject *self;
1429 object *args;
1430{
1431 int a[2];
1432 if (!getpointarg(args, a))
1433 return NULL;
1434 return newintobject((long) wgetbit(self->b_bitmap, a[0], a[1]));
1435}
1436
1437static object *
1438bitmap_getsize(self, args)
1439 bitmapobject *self;
1440 object *args;
1441{
1442 int width, height;
1443 if (!getnoarg(args))
1444 return NULL;
1445 wgetbitmapsize(self->b_bitmap, &width, &height);
1446 return mkvalue("(ii)", width, height);
1447}
1448
1449static struct methodlist bitmap_methods[] = {
Guido van Rossumb6775db1994-08-01 11:34:53 +00001450 {"close", (method)bitmap_close},
1451 {"getsize", (method)bitmap_getsize},
1452 {"getbit", (method)bitmap_getbit},
1453 {"setbit", (method)bitmap_setbit},
Guido van Rossumbf80e541993-02-08 15:49:17 +00001454 {NULL, NULL} /* sentinel */
1455};
1456
1457static object *
1458bitmap_getattr(bp, name)
1459 bitmapobject *bp;
1460 char *name;
1461{
1462 object *v = NULL;
1463 if (bp->b_bitmap == NULL) {
1464 err_setstr(StdwinError, "bitmap object already closed");
1465 return NULL;
1466 }
1467 if (strcmp(name, "__dict__") == 0) {
1468 v = bp->b_attr;
1469 if (v == NULL)
1470 v = None;
1471 }
1472 else if (bp->b_attr != NULL) {
1473 v = dictlookup(bp->b_attr, name);
1474 }
1475 if (v != NULL) {
1476 INCREF(v);
1477 return v;
1478 }
1479 return findmethod(bitmap_methods, (object *)bp, name);
1480}
1481
1482static int
1483bitmap_setattr(bp, name, v)
1484 bitmapobject *bp;
1485 char *name;
1486 object *v;
1487{
1488 if (bp->b_attr == NULL) {
1489 bp->b_attr = newdictobject();
1490 if (bp->b_attr == NULL)
1491 return -1;
1492 }
1493 if (v == NULL) {
1494 int rv = dictremove(bp->b_attr, name);
1495 if (rv < 0)
1496 err_setstr(AttributeError,
1497 "delete non-existing bitmap object attribute");
1498 return rv;
1499 }
1500 else
1501 return dictinsert(bp->b_attr, name, v);
1502}
1503
Guido van Rossumb6775db1994-08-01 11:34:53 +00001504static typeobject Bitmaptype = {
Guido van Rossumbf80e541993-02-08 15:49:17 +00001505 OB_HEAD_INIT(&Typetype)
1506 0, /*ob_size*/
1507 "bitmap", /*tp_name*/
1508 sizeof(bitmapobject), /*tp_size*/
1509 0, /*tp_itemsize*/
1510 /* methods */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001511 (destructor)bitmap_dealloc, /*tp_dealloc*/
Guido van Rossumbf80e541993-02-08 15:49:17 +00001512 0, /*tp_print*/
Guido van Rossumb6775db1994-08-01 11:34:53 +00001513 (getattrfunc)bitmap_getattr, /*tp_getattr*/
1514 (setattrfunc)bitmap_setattr, /*tp_setattr*/
Guido van Rossumbf80e541993-02-08 15:49:17 +00001515 0, /*tp_compare*/
1516 0, /*tp_repr*/
1517};
1518
Guido van Rossume9066061993-07-29 13:14:32 +00001519#endif /* HAVE_BITMAPS */
1520
Guido van Rossumbf80e541993-02-08 15:49:17 +00001521
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001522/* Windows */
1523
1524#define MAXNWIN 50
1525static windowobject *windowlist[MAXNWIN];
1526
1527/* Window methods */
1528
1529static void
1530window_dealloc(wp)
1531 windowobject *wp;
1532{
1533 if (wp->w_win != NULL) {
1534 int tag = wgettag(wp->w_win);
1535 if (tag >= 0 && tag < MAXNWIN)
1536 windowlist[tag] = NULL;
1537 else
1538 fprintf(stderr, "XXX help! tag %d in window_dealloc\n",
1539 tag);
1540 wclose(wp->w_win);
1541 }
1542 DECREF(wp->w_title);
1543 if (wp->w_attr != NULL)
1544 DECREF(wp->w_attr);
1545 free((char *)wp);
1546}
1547
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001548static object *
Guido van Rossum3c284741991-11-27 14:54:54 +00001549window_close(wp, args)
1550 windowobject *wp;
1551 object *args;
1552{
1553 if (wp->w_win != NULL) {
1554 int tag = wgettag(wp->w_win);
1555 if (tag >= 0 && tag < MAXNWIN)
1556 windowlist[tag] = NULL;
1557 wclose(wp->w_win);
1558 wp->w_win = NULL;
1559 }
1560 INCREF(None);
1561 return None;
1562}
1563
1564static object *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001565window_begindrawing(wp, args)
1566 windowobject *wp;
1567 object *args;
1568{
1569 drawingobject *dp;
1570 if (!getnoarg(args))
1571 return NULL;
1572 if (Drawing != NULL) {
Guido van Rossum87e7ea71991-12-10 14:00:03 +00001573 err_setstr(StdwinError, "already drawing");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001574 return NULL;
1575 }
1576 dp = NEWOBJ(drawingobject, &Drawingtype);
1577 if (dp == NULL)
1578 return NULL;
1579 Drawing = dp;
1580 INCREF(wp);
1581 dp->d_ref = wp;
1582 wbegindrawing(wp->w_win);
1583 return (object *)dp;
1584}
1585
1586static object *
1587window_change(wp, args)
1588 windowobject *wp;
1589 object *args;
1590{
1591 int a[4];
1592 if (!getrectarg(args, a))
1593 return NULL;
1594 wchange(wp->w_win, a[0], a[1], a[2], a[3]);
1595 INCREF(None);
1596 return None;
1597}
1598
1599static object *
1600window_gettitle(wp, args)
1601 windowobject *wp;
1602 object *args;
1603{
1604 if (!getnoarg(args))
1605 return NULL;
1606 INCREF(wp->w_title);
1607 return wp->w_title;
1608}
1609
1610static object *
Guido van Rossum541c8c01991-05-05 20:13:41 +00001611window_getwinpos(wp, args)
1612 windowobject *wp;
1613 object *args;
1614{
1615 int h, v;
1616 if (!getnoarg(args))
1617 return NULL;
1618 wgetwinpos(wp->w_win, &h, &v);
1619 return makepoint(h, v);
1620}
1621
1622static object *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001623window_getwinsize(wp, args)
1624 windowobject *wp;
1625 object *args;
1626{
1627 int width, height;
1628 if (!getnoarg(args))
1629 return NULL;
1630 wgetwinsize(wp->w_win, &width, &height);
1631 return makepoint(width, height);
1632}
1633
1634static object *
Guido van Rossumbf80e541993-02-08 15:49:17 +00001635window_setwinpos(wp, args)
1636 windowobject *wp;
1637 object *args;
1638{
1639 int a[2];
1640 if (!getpointarg(args, a))
1641 return NULL;
1642 wsetwinpos(wp->w_win, a[0], a[1]);
1643 INCREF(None);
1644 return None;
1645}
1646
1647static object *
1648window_setwinsize(wp, args)
1649 windowobject *wp;
1650 object *args;
1651{
1652 int a[2];
1653 if (!getpointarg(args, a))
1654 return NULL;
1655 wsetwinsize(wp->w_win, a[0], a[1]);
1656 INCREF(None);
1657 return None;
1658}
1659
1660static object *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001661window_getdocsize(wp, args)
1662 windowobject *wp;
1663 object *args;
1664{
1665 int width, height;
1666 if (!getnoarg(args))
1667 return NULL;
1668 wgetdocsize(wp->w_win, &width, &height);
1669 return makepoint(width, height);
1670}
1671
1672static object *
1673window_getorigin(wp, args)
1674 windowobject *wp;
1675 object *args;
1676{
1677 int width, height;
1678 if (!getnoarg(args))
1679 return NULL;
1680 wgetorigin(wp->w_win, &width, &height);
1681 return makepoint(width, height);
1682}
1683
1684static object *
1685window_scroll(wp, args)
1686 windowobject *wp;
1687 object *args;
1688{
1689 int a[6];
1690 if (!getrectpointarg(args, a))
1691 return NULL;
1692 wscroll(wp->w_win, a[0], a[1], a[2], a[3], a[4], a[5]);
1693 INCREF(None);
1694 return None;
1695}
1696
1697static object *
1698window_setdocsize(wp, args)
1699 windowobject *wp;
1700 object *args;
1701{
1702 int a[2];
1703 if (!getpointarg(args, a))
1704 return NULL;
1705 wsetdocsize(wp->w_win, a[0], a[1]);
1706 INCREF(None);
1707 return None;
1708}
1709
1710static object *
1711window_setorigin(wp, args)
1712 windowobject *wp;
1713 object *args;
1714{
1715 int a[2];
1716 if (!getpointarg(args, a))
1717 return NULL;
1718 wsetorigin(wp->w_win, a[0], a[1]);
1719 INCREF(None);
1720 return None;
1721}
1722
1723static object *
1724window_settitle(wp, args)
1725 windowobject *wp;
1726 object *args;
1727{
1728 object *title;
Guido van Rossum234f9421993-06-17 12:35:49 +00001729 if (!getargs(args, "S", &title))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001730 return NULL;
1731 DECREF(wp->w_title);
1732 INCREF(title);
1733 wp->w_title = title;
1734 wsettitle(wp->w_win, getstringvalue(title));
1735 INCREF(None);
1736 return None;
1737}
1738
1739static object *
1740window_show(wp, args)
1741 windowobject *wp;
1742 object *args;
1743{
1744 int a[4];
1745 if (!getrectarg(args, a))
1746 return NULL;
1747 wshow(wp->w_win, a[0], a[1], a[2], a[3]);
1748 INCREF(None);
1749 return None;
1750}
1751
1752static object *
1753window_settimer(wp, args)
1754 windowobject *wp;
1755 object *args;
1756{
1757 int a;
1758 if (!getintarg(args, &a))
1759 return NULL;
1760 wsettimer(wp->w_win, a);
1761 INCREF(None);
1762 return None;
1763}
1764
1765static object *
1766window_menucreate(self, args)
1767 windowobject *self;
1768 object *args;
1769{
1770 menuobject *mp;
Guido van Rossumfc58e581992-01-27 16:45:55 +00001771 char *title;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001772 if (!getstrarg(args, &title))
1773 return NULL;
1774 wmenusetdeflocal(1);
1775 mp = newmenuobject(title);
1776 if (mp == NULL)
1777 return NULL;
1778 wmenuattach(self->w_win, mp->m_menu);
1779 return (object *)mp;
1780}
1781
1782static object *
1783window_textcreate(self, args)
1784 windowobject *self;
1785 object *args;
1786{
1787 textobject *tp;
1788 int a[4];
1789 if (!getrectarg(args, a))
1790 return NULL;
1791 return (object *)
1792 newtextobject(self, a[0], a[1], a[2], a[3]);
1793}
1794
Guido van Rossum5b10f451990-10-30 16:01:48 +00001795static object *
1796window_setselection(self, args)
1797 windowobject *self;
1798 object *args;
1799{
Guido van Rossumfc58e581992-01-27 16:45:55 +00001800 int sel, size, ok;
1801 char *text;
1802 if (!getargs(args, "(is#)", &sel, &text, &size))
Guido van Rossum5b10f451990-10-30 16:01:48 +00001803 return NULL;
Guido van Rossumfc58e581992-01-27 16:45:55 +00001804 ok = wsetselection(self->w_win, sel, text, size);
Guido van Rossum5b10f451990-10-30 16:01:48 +00001805 return newintobject(ok);
1806}
1807
1808static object *
1809window_setwincursor(self, args)
1810 windowobject *self;
1811 object *args;
1812{
Guido van Rossumfc58e581992-01-27 16:45:55 +00001813 char *name;
Guido van Rossum5b10f451990-10-30 16:01:48 +00001814 CURSOR *c;
Guido van Rossum3c8ba7a1992-02-05 11:15:00 +00001815 if (!getargs(args, "z", &name))
Guido van Rossum5b10f451990-10-30 16:01:48 +00001816 return NULL;
Guido van Rossum3c8ba7a1992-02-05 11:15:00 +00001817 if (name == NULL)
1818 c = NULL;
1819 else {
1820 c = wfetchcursor(name);
1821 if (c == NULL) {
1822 err_setstr(StdwinError, "no such cursor");
1823 return NULL;
1824 }
Guido van Rossum5b10f451990-10-30 16:01:48 +00001825 }
1826 wsetwincursor(self->w_win, c);
1827 INCREF(None);
1828 return None;
1829}
1830
Guido van Rossumfc58e581992-01-27 16:45:55 +00001831static object *
1832window_setactive(self, args)
1833 windowobject *self;
1834 object *args;
1835{
1836 if (!getnoarg(args))
1837 return NULL;
1838 wsetactive(self->w_win);
1839 INCREF(None);
1840 return None;
1841}
1842
Guido van Rossum8dcbbac1991-07-27 21:42:24 +00001843#ifdef CWI_HACKS
1844static object *
1845window_getxwindowid(self, args)
1846 windowobject *self;
1847 object *args;
1848{
1849 long wid = wgetxwindowid(self->w_win);
1850 return newintobject(wid);
1851}
1852#endif
1853
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001854static struct methodlist window_methods[] = {
Guido van Rossumb6775db1994-08-01 11:34:53 +00001855 {"begindrawing",(method)window_begindrawing},
1856 {"change", (method)window_change},
1857 {"close", (method)window_close},
1858 {"getdocsize", (method)window_getdocsize},
1859 {"getorigin", (method)window_getorigin},
1860 {"gettitle", (method)window_gettitle},
1861 {"getwinpos", (method)window_getwinpos},
1862 {"getwinsize", (method)window_getwinsize},
1863 {"menucreate", (method)window_menucreate},
1864 {"scroll", (method)window_scroll},
1865 {"setactive", (method)window_setactive},
1866 {"setdocsize", (method)window_setdocsize},
1867 {"setorigin", (method)window_setorigin},
1868 {"setselection",(method)window_setselection},
1869 {"settimer", (method)window_settimer},
1870 {"settitle", (method)window_settitle},
1871 {"setwincursor",(method)window_setwincursor},
1872 {"setwinpos", (method)window_setwinpos},
1873 {"setwinsize", (method)window_setwinsize},
1874 {"show", (method)window_show},
1875 {"textcreate", (method)window_textcreate},
Guido van Rossum8dcbbac1991-07-27 21:42:24 +00001876#ifdef CWI_HACKS
Guido van Rossumb6775db1994-08-01 11:34:53 +00001877 {"getxwindowid",(method)window_getxwindowid},
Guido van Rossum8dcbbac1991-07-27 21:42:24 +00001878#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001879 {NULL, NULL} /* sentinel */
1880};
1881
1882static object *
1883window_getattr(wp, name)
1884 windowobject *wp;
1885 char *name;
1886{
Guido van Rossum85f50761991-10-20 20:22:50 +00001887 object *v = NULL;
Guido van Rossum77b46041992-01-14 18:41:24 +00001888 if (wp->w_win == NULL) {
1889 err_setstr(StdwinError, "window already closed");
1890 return NULL;
1891 }
Guido van Rossum85f50761991-10-20 20:22:50 +00001892 if (strcmp(name, "__dict__") == 0) {
1893 v = wp->w_attr;
1894 if (v == NULL)
1895 v = None;
1896 }
1897 else if (wp->w_attr != NULL) {
1898 v = dictlookup(wp->w_attr, name);
1899 }
1900 if (v != NULL) {
1901 INCREF(v);
1902 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001903 }
1904 return findmethod(window_methods, (object *)wp, name);
1905}
1906
1907static int
1908window_setattr(wp, name, v)
1909 windowobject *wp;
1910 char *name;
1911 object *v;
1912{
1913 if (wp->w_attr == NULL) {
1914 wp->w_attr = newdictobject();
1915 if (wp->w_attr == NULL)
1916 return -1;
1917 }
Guido van Rossum94472a01992-09-04 09:45:18 +00001918 if (v == NULL) {
1919 int rv = dictremove(wp->w_attr, name);
1920 if (rv < 0)
1921 err_setstr(AttributeError,
1922 "delete non-existing menu object attribute");
1923 return rv;
1924 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001925 else
1926 return dictinsert(wp->w_attr, name, v);
1927}
1928
Guido van Rossumb6775db1994-08-01 11:34:53 +00001929static typeobject Windowtype = {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001930 OB_HEAD_INIT(&Typetype)
1931 0, /*ob_size*/
1932 "window", /*tp_name*/
1933 sizeof(windowobject), /*tp_size*/
1934 0, /*tp_itemsize*/
1935 /* methods */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001936 (destructor)window_dealloc, /*tp_dealloc*/
Guido van Rossum7066dd71992-09-17 17:54:56 +00001937 0, /*tp_print*/
Guido van Rossumb6775db1994-08-01 11:34:53 +00001938 (getattrfunc)window_getattr, /*tp_getattr*/
1939 (setattrfunc)window_setattr, /*tp_setattr*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001940 0, /*tp_compare*/
1941 0, /*tp_repr*/
1942};
1943
1944/* Stdwin methods */
1945
1946static object *
Guido van Rossumcacd9571993-10-18 11:44:47 +00001947stdwin_done(sw, args)
1948 object *sw;
1949 object *args;
1950{
1951 if (!getnoarg(args))
1952 return NULL;
1953 wdone();
1954 /* XXX There is no protection against continued use of
1955 XXX stdwin functions or objects after this call is made.
1956 XXX Use at own risk */
1957 INCREF(None);
1958 return None;
1959}
1960
1961static object *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001962stdwin_open(sw, args)
1963 object *sw;
1964 object *args;
1965{
1966 int tag;
1967 object *title;
1968 windowobject *wp;
Guido van Rossum234f9421993-06-17 12:35:49 +00001969 if (!getargs(args, "S", &title))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001970 return NULL;
1971 for (tag = 0; tag < MAXNWIN; tag++) {
1972 if (windowlist[tag] == NULL)
1973 break;
1974 }
Guido van Rossum27201061991-04-16 08:43:03 +00001975 if (tag >= MAXNWIN) {
Guido van Rossum87e7ea71991-12-10 14:00:03 +00001976 err_setstr(StdwinError, "creating too many windows");
Guido van Rossum27201061991-04-16 08:43:03 +00001977 return NULL;
1978 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001979 wp = NEWOBJ(windowobject, &Windowtype);
1980 if (wp == NULL)
1981 return NULL;
1982 INCREF(title);
1983 wp->w_title = title;
1984 wp->w_win = wopen(getstringvalue(title), (void (*)()) NULL);
1985 wp->w_attr = NULL;
1986 if (wp->w_win == NULL) {
1987 DECREF(wp);
1988 return NULL;
1989 }
1990 windowlist[tag] = wp;
1991 wsettag(wp->w_win, tag);
1992 return (object *)wp;
1993}
1994
1995static object *
Guido van Rossum246b9d81991-06-03 10:55:14 +00001996window2object(win)
1997 WINDOW *win;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001998{
Guido van Rossum246b9d81991-06-03 10:55:14 +00001999 object *w;
2000 if (win == NULL)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002001 w = None;
2002 else {
Guido van Rossum246b9d81991-06-03 10:55:14 +00002003 int tag = wgettag(win);
2004 if (tag < 0 || tag >= MAXNWIN || windowlist[tag] == NULL ||
2005 windowlist[tag]->w_win != win)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002006 w = None;
2007 else
2008 w = (object *)windowlist[tag];
2009 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002010 INCREF(w);
Guido van Rossum246b9d81991-06-03 10:55:14 +00002011 return w;
2012}
2013
2014static object *
2015stdwin_get_poll_event(poll, args)
2016 int poll;
2017 object *args;
2018{
2019 EVENT e;
Guido van Rossum2ee12f41992-04-13 15:54:35 +00002020 object *u, *v, *w;
Guido van Rossum246b9d81991-06-03 10:55:14 +00002021 if (!getnoarg(args))
2022 return NULL;
2023 if (Drawing != NULL) {
Guido van Rossum87e7ea71991-12-10 14:00:03 +00002024 err_setstr(StdwinError, "cannot getevent() while drawing");
Guido van Rossum246b9d81991-06-03 10:55:14 +00002025 return NULL;
2026 }
2027 again:
Guido van Rossumff4949e1992-08-05 19:58:53 +00002028 BGN_STDWIN
Guido van Rossum246b9d81991-06-03 10:55:14 +00002029 if (poll) {
2030 if (!wpollevent(&e)) {
Guido van Rossumff4949e1992-08-05 19:58:53 +00002031 RET_STDWIN
Guido van Rossum246b9d81991-06-03 10:55:14 +00002032 INCREF(None);
2033 return None;
2034 }
2035 }
2036 else
2037 wgetevent(&e);
Guido van Rossumff4949e1992-08-05 19:58:53 +00002038 END_STDWIN
Guido van Rossum246b9d81991-06-03 10:55:14 +00002039 if (e.type == WE_COMMAND && e.u.command == WC_CANCEL) {
2040 /* Turn keyboard interrupts into exceptions */
2041 err_set(KeyboardInterrupt);
2042 return NULL;
2043 }
2044 if (e.type == WE_COMMAND && e.u.command == WC_CLOSE) {
2045 /* Turn WC_CLOSE commands into WE_CLOSE events */
2046 e.type = WE_CLOSE;
2047 }
Guido van Rossum2ee12f41992-04-13 15:54:35 +00002048 v = window2object(e.window);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002049 switch (e.type) {
2050 case WE_CHAR:
2051 {
2052 char c[1];
2053 c[0] = e.u.character;
2054 w = newsizedstringobject(c, 1);
2055 }
2056 break;
2057 case WE_COMMAND:
2058 w = newintobject((long)e.u.command);
2059 break;
2060 case WE_DRAW:
2061 w = makerect(e.u.area.left, e.u.area.top,
2062 e.u.area.right, e.u.area.bottom);
2063 break;
2064 case WE_MOUSE_DOWN:
2065 case WE_MOUSE_MOVE:
2066 case WE_MOUSE_UP:
Guido van Rossum2ee12f41992-04-13 15:54:35 +00002067 w = mkvalue("((ii)iii)",
2068 e.u.where.h, e.u.where.v,
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002069 e.u.where.clicks,
2070 e.u.where.button,
2071 e.u.where.mask);
2072 break;
2073 case WE_MENU:
Guido van Rossum2d14e211991-02-19 12:26:49 +00002074 if (e.u.m.id >= IDOFFSET && e.u.m.id < IDOFFSET+MAXNMENU &&
2075 menulist[e.u.m.id - IDOFFSET] != NULL)
Guido van Rossum2ee12f41992-04-13 15:54:35 +00002076 w = mkvalue("(Oi)",
2077 menulist[e.u.m.id - IDOFFSET], e.u.m.item);
Guido van Rossum246b9d81991-06-03 10:55:14 +00002078 else {
2079 /* Ghost menu event.
2080 Can occur only on the Mac if another part
2081 of the aplication has installed a menu;
2082 like the THINK C console library. */
2083 DECREF(v);
2084 goto again;
2085 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002086 break;
Guido van Rossum3ee199e1992-06-30 12:48:26 +00002087 case WE_KEY:
2088 w = mkvalue("(ii)", e.u.key.code, e.u.key.mask);
2089 break;
Guido van Rossum5b10f451990-10-30 16:01:48 +00002090 case WE_LOST_SEL:
2091 w = newintobject((long)e.u.sel);
2092 break;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002093 default:
2094 w = None;
2095 INCREF(w);
2096 break;
2097 }
2098 if (w == NULL) {
2099 DECREF(v);
2100 return NULL;
2101 }
Guido van Rossum2ee12f41992-04-13 15:54:35 +00002102 u = mkvalue("(iOO)", e.type, v, w);
2103 XDECREF(v);
2104 XDECREF(w);
2105 return u;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002106}
2107
2108static object *
Guido van Rossume8e7cf41991-01-16 14:06:18 +00002109stdwin_getevent(sw, args)
2110 object *sw;
2111 object *args;
2112{
2113 return stdwin_get_poll_event(0, args);
2114}
2115
2116static object *
2117stdwin_pollevent(sw, args)
2118 object *sw;
2119 object *args;
2120{
2121 return stdwin_get_poll_event(1, args);
2122}
2123
2124static object *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002125stdwin_setdefwinpos(sw, args)
2126 object *sw;
2127 object *args;
2128{
2129 int a[2];
2130 if (!getpointarg(args, a))
2131 return NULL;
2132 wsetdefwinpos(a[0], a[1]);
2133 INCREF(None);
2134 return None;
2135}
2136
2137static object *
2138stdwin_setdefwinsize(sw, args)
2139 object *sw;
2140 object *args;
2141{
2142 int a[2];
2143 if (!getpointarg(args, a))
2144 return NULL;
2145 wsetdefwinsize(a[0], a[1]);
2146 INCREF(None);
2147 return None;
2148}
2149
2150static object *
Guido van Rossum0c2290b1991-04-03 19:12:14 +00002151stdwin_setdefscrollbars(sw, args)
2152 object *sw;
2153 object *args;
2154{
2155 int a[2];
2156 if (!getpointarg(args, a))
2157 return NULL;
2158 wsetdefscrollbars(a[0], a[1]);
2159 INCREF(None);
2160 return None;
2161}
2162
2163static object *
Guido van Rossum541c8c01991-05-05 20:13:41 +00002164stdwin_getdefwinpos(self, args)
2165 object *self;
Guido van Rossum33f17701991-02-13 23:19:39 +00002166 object *args;
2167{
2168 int h, v;
2169 if (!getnoarg(args))
2170 return NULL;
2171 wgetdefwinpos(&h, &v);
2172 return makepoint(h, v);
2173}
2174
2175static object *
Guido van Rossum541c8c01991-05-05 20:13:41 +00002176stdwin_getdefwinsize(self, args)
2177 object *self;
Guido van Rossum33f17701991-02-13 23:19:39 +00002178 object *args;
2179{
2180 int width, height;
2181 if (!getnoarg(args))
2182 return NULL;
2183 wgetdefwinsize(&width, &height);
2184 return makepoint(width, height);
2185}
2186
2187static object *
Guido van Rossum541c8c01991-05-05 20:13:41 +00002188stdwin_getdefscrollbars(self, args)
2189 object *self;
Guido van Rossum0c2290b1991-04-03 19:12:14 +00002190 object *args;
2191{
2192 int h, v;
2193 if (!getnoarg(args))
2194 return NULL;
2195 wgetdefscrollbars(&h, &v);
2196 return makepoint(h, v);
2197}
2198
2199static object *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002200stdwin_menucreate(self, args)
2201 object *self;
2202 object *args;
2203{
Guido van Rossumfc58e581992-01-27 16:45:55 +00002204 char *title;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002205 if (!getstrarg(args, &title))
2206 return NULL;
2207 wmenusetdeflocal(0);
2208 return (object *)newmenuobject(title);
2209}
2210
2211static object *
2212stdwin_askfile(self, args)
2213 object *self;
2214 object *args;
2215{
Guido van Rossumfc58e581992-01-27 16:45:55 +00002216 char *prompt, *dflt;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002217 int new, ret;
2218 char buf[256];
Guido van Rossum234f9421993-06-17 12:35:49 +00002219 if (!getargs(args, "(ssi)", &prompt, &dflt, &new))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002220 return NULL;
Guido van Rossumfc58e581992-01-27 16:45:55 +00002221 strncpy(buf, dflt, sizeof buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002222 buf[sizeof buf - 1] = '\0';
Guido van Rossumff4949e1992-08-05 19:58:53 +00002223 BGN_STDWIN
Guido van Rossumfc58e581992-01-27 16:45:55 +00002224 ret = waskfile(prompt, buf, sizeof buf, new);
Guido van Rossumff4949e1992-08-05 19:58:53 +00002225 END_STDWIN
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002226 if (!ret) {
2227 err_set(KeyboardInterrupt);
2228 return NULL;
2229 }
2230 return newstringobject(buf);
2231}
2232
2233static object *
2234stdwin_askync(self, args)
2235 object *self;
2236 object *args;
2237{
Guido van Rossumfc58e581992-01-27 16:45:55 +00002238 char *prompt;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002239 int new, ret;
Guido van Rossum234f9421993-06-17 12:35:49 +00002240 if (!getargs(args, "(si)", &prompt, &new))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002241 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002242 BGN_STDWIN
Guido van Rossumfc58e581992-01-27 16:45:55 +00002243 ret = waskync(prompt, new);
Guido van Rossumff4949e1992-08-05 19:58:53 +00002244 END_STDWIN
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002245 if (ret < 0) {
2246 err_set(KeyboardInterrupt);
2247 return NULL;
2248 }
2249 return newintobject((long)ret);
2250}
2251
2252static object *
2253stdwin_askstr(self, args)
2254 object *self;
2255 object *args;
2256{
Guido van Rossumfc58e581992-01-27 16:45:55 +00002257 char *prompt, *dflt;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002258 int ret;
2259 char buf[256];
Guido van Rossum234f9421993-06-17 12:35:49 +00002260 if (!getargs(args, "(ss)", &prompt, &dflt))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002261 return NULL;
Guido van Rossumfc58e581992-01-27 16:45:55 +00002262 strncpy(buf, dflt, sizeof buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002263 buf[sizeof buf - 1] = '\0';
Guido van Rossumff4949e1992-08-05 19:58:53 +00002264 BGN_STDWIN
Guido van Rossumfc58e581992-01-27 16:45:55 +00002265 ret = waskstr(prompt, buf, sizeof buf);
Guido van Rossumff4949e1992-08-05 19:58:53 +00002266 END_STDWIN
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002267 if (!ret) {
2268 err_set(KeyboardInterrupt);
2269 return NULL;
2270 }
2271 return newstringobject(buf);
2272}
2273
2274static object *
2275stdwin_message(self, args)
2276 object *self;
2277 object *args;
2278{
Guido van Rossumfc58e581992-01-27 16:45:55 +00002279 char *msg;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002280 if (!getstrarg(args, &msg))
2281 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002282 BGN_STDWIN
Guido van Rossumfc58e581992-01-27 16:45:55 +00002283 wmessage(msg);
Guido van Rossumff4949e1992-08-05 19:58:53 +00002284 END_STDWIN
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002285 INCREF(None);
2286 return None;
2287}
2288
2289static object *
2290stdwin_fleep(self, args)
2291 object *self;
2292 object *args;
2293{
2294 if (!getnoarg(args))
2295 return NULL;
2296 wfleep();
2297 INCREF(None);
2298 return None;
2299}
2300
2301static object *
2302stdwin_setcutbuffer(self, args)
2303 object *self;
2304 object *args;
2305{
Guido van Rossumfc58e581992-01-27 16:45:55 +00002306 int i, size;
2307 char *str;
2308 if (!getargs(args, "(is#)", &i, &str, &size))
Guido van Rossum124967c1990-11-06 15:17:35 +00002309 return NULL;
Guido van Rossumfc58e581992-01-27 16:45:55 +00002310 wsetcutbuffer(i, str, size);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002311 INCREF(None);
2312 return None;
2313}
2314
2315static object *
Guido van Rossum246b9d81991-06-03 10:55:14 +00002316stdwin_getactive(self, args)
2317 object *self;
2318 object *args;
2319{
2320 return window2object(wgetactive());
2321}
2322
2323static object *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002324stdwin_getcutbuffer(self, args)
2325 object *self;
2326 object *args;
2327{
Guido van Rossum5b10f451990-10-30 16:01:48 +00002328 int i;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002329 char *str;
Guido van Rossum01769f01990-10-30 13:39:00 +00002330 int len;
Guido van Rossum124967c1990-11-06 15:17:35 +00002331 if (!getintarg(args, &i))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002332 return NULL;
Guido van Rossum5b10f451990-10-30 16:01:48 +00002333 str = wgetcutbuffer(i, &len);
Guido van Rossum01769f01990-10-30 13:39:00 +00002334 if (str == NULL) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002335 str = "";
Guido van Rossum01769f01990-10-30 13:39:00 +00002336 len = 0;
2337 }
2338 return newsizedstringobject(str, len);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002339}
2340
Guido van Rossum5b10f451990-10-30 16:01:48 +00002341static object *
2342stdwin_rotatecutbuffers(self, args)
2343 object *self;
2344 object *args;
2345{
2346 int i;
2347 if (!getintarg(args, &i))
2348 return NULL;
2349 wrotatecutbuffers(i);
2350 INCREF(None);
2351 return None;
2352}
2353
2354static object *
2355stdwin_getselection(self, args)
2356 object *self;
2357 object *args;
2358{
2359 int sel;
2360 char *data;
2361 int len;
2362 if (!getintarg(args, &sel))
2363 return NULL;
2364 data = wgetselection(sel, &len);
2365 if (data == NULL) {
2366 data = "";
2367 len = 0;
2368 }
2369 return newsizedstringobject(data, len);
2370}
2371
2372static object *
2373stdwin_resetselection(self, args)
2374 object *self;
2375 object *args;
2376{
2377 int sel;
2378 if (!getintarg(args, &sel))
2379 return NULL;
2380 wresetselection(sel);
2381 INCREF(None);
2382 return None;
2383}
2384
Guido van Rossum0c2290b1991-04-03 19:12:14 +00002385static object *
2386stdwin_fetchcolor(self, args)
2387 object *self;
2388 object *args;
2389{
Guido van Rossumfc58e581992-01-27 16:45:55 +00002390 char *colorname;
Guido van Rossum34679b71993-01-26 13:33:44 +00002391 COLOR color;
Guido van Rossum0c2290b1991-04-03 19:12:14 +00002392 if (!getstrarg(args, &colorname))
2393 return NULL;
Guido van Rossum34679b71993-01-26 13:33:44 +00002394 color = wfetchcolor(colorname);
2395#ifdef BADCOLOR
2396 if (color == BADCOLOR) {
2397 err_setstr(StdwinError, "color name not found");
2398 return NULL;
2399 }
2400#endif
2401 return newintobject((long)color);
Guido van Rossum0c2290b1991-04-03 19:12:14 +00002402}
2403
Guido van Rossum541c8c01991-05-05 20:13:41 +00002404static object *
2405stdwin_getscrsize(self, args)
2406 object *self;
2407 object *args;
2408{
2409 int width, height;
2410 if (!getnoarg(args))
2411 return NULL;
2412 wgetscrsize(&width, &height);
2413 return makepoint(width, height);
2414}
2415
2416static object *
2417stdwin_getscrmm(self, args)
2418 object *self;
2419 object *args;
2420{
2421 int width, height;
2422 if (!getnoarg(args))
2423 return NULL;
2424 wgetscrmm(&width, &height);
2425 return makepoint(width, height);
2426}
2427
Guido van Rossumed233a51992-06-23 09:07:03 +00002428#ifdef unix
2429static object *
2430stdwin_connectionnumber(self, args)
2431 object *self;
2432 object *args;
2433{
2434 if (!getnoarg(args))
2435 return NULL;
2436 return newintobject((long) wconnectionnumber());
2437}
2438#endif
2439
Guido van Rossumbf80e541993-02-08 15:49:17 +00002440static object *
2441stdwin_listfontnames(self, args)
2442 object *self;
2443 object *args;
2444{
2445 char *pattern;
2446 char **fontnames;
2447 int count;
2448 object *list;
2449 if (!getargs(args, "z", &pattern))
2450 return NULL;
2451 fontnames = wlistfontnames(pattern, &count);
2452 list = newlistobject(count);
2453 if (list != NULL) {
2454 int i;
2455 for (i = 0; i < count; i++) {
2456 object *v = newstringobject(fontnames[i]);
2457 if (v == NULL) {
2458 DECREF(list);
2459 list = NULL;
2460 break;
2461 }
2462 setlistitem(list, i, v);
2463 }
2464 }
2465 return list;
2466}
2467
Guido van Rossume9066061993-07-29 13:14:32 +00002468#ifdef HAVE_BITMAPS
Guido van Rossumbf80e541993-02-08 15:49:17 +00002469static object *
2470stdwin_newbitmap(self, args)
2471 object *self;
2472 object *args;
2473{
2474 int width, height;
2475 bitmapobject *bp;
2476 if (!getargs(args, "(ii)", &width, &height))
2477 return NULL;
2478 return (object *)newbitmapobject(width, height);
2479}
Guido van Rossume9066061993-07-29 13:14:32 +00002480#endif
Guido van Rossumbf80e541993-02-08 15:49:17 +00002481
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002482static struct methodlist stdwin_methods[] = {
2483 {"askfile", stdwin_askfile},
2484 {"askstr", stdwin_askstr},
2485 {"askync", stdwin_askync},
Guido van Rossumcacd9571993-10-18 11:44:47 +00002486 {"done", stdwin_done},
Guido van Rossum27201061991-04-16 08:43:03 +00002487 {"fetchcolor", stdwin_fetchcolor},
Guido van Rossumed233a51992-06-23 09:07:03 +00002488#ifdef unix
2489 {"fileno", stdwin_connectionnumber},
2490 {"connectionnumber", stdwin_connectionnumber},
2491#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002492 {"fleep", stdwin_fleep},
Guido van Rossum246b9d81991-06-03 10:55:14 +00002493 {"getactive", stdwin_getactive},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002494 {"getcutbuffer", stdwin_getcutbuffer},
Guido van Rossum0c2290b1991-04-03 19:12:14 +00002495 {"getdefscrollbars", stdwin_getdefscrollbars},
Guido van Rossum33f17701991-02-13 23:19:39 +00002496 {"getdefwinpos", stdwin_getdefwinpos},
2497 {"getdefwinsize", stdwin_getdefwinsize},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002498 {"getevent", stdwin_getevent},
Guido van Rossum541c8c01991-05-05 20:13:41 +00002499 {"getscrmm", stdwin_getscrmm},
2500 {"getscrsize", stdwin_getscrsize},
Guido van Rossum27201061991-04-16 08:43:03 +00002501 {"getselection", stdwin_getselection},
Guido van Rossumbf80e541993-02-08 15:49:17 +00002502 {"listfontnames", stdwin_listfontnames},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002503 {"menucreate", stdwin_menucreate},
2504 {"message", stdwin_message},
Guido van Rossume9066061993-07-29 13:14:32 +00002505#ifdef HAVE_BITMAPS
Guido van Rossumbf80e541993-02-08 15:49:17 +00002506 {"newbitmap", stdwin_newbitmap},
Guido van Rossume9066061993-07-29 13:14:32 +00002507#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002508 {"open", stdwin_open},
Guido van Rossume8e7cf41991-01-16 14:06:18 +00002509 {"pollevent", stdwin_pollevent},
Guido van Rossum5b10f451990-10-30 16:01:48 +00002510 {"resetselection", stdwin_resetselection},
2511 {"rotatecutbuffers", stdwin_rotatecutbuffers},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002512 {"setcutbuffer", stdwin_setcutbuffer},
Guido van Rossum0c2290b1991-04-03 19:12:14 +00002513 {"setdefscrollbars", stdwin_setdefscrollbars},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002514 {"setdefwinpos", stdwin_setdefwinpos},
2515 {"setdefwinsize", stdwin_setdefwinsize},
2516
2517 /* Text measuring methods borrow code from drawing objects: */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002518 {"baseline", (method)drawing_baseline},
2519 {"lineheight", (method)drawing_lineheight},
2520 {"textbreak", (method)drawing_textbreak},
2521 {"textwidth", (method)drawing_textwidth},
Guido van Rossum0c2290b1991-04-03 19:12:14 +00002522
2523 /* Same for font setting methods: */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002524 {"setfont", (method)drawing_setfont},
Guido van Rossum0c2290b1991-04-03 19:12:14 +00002525
2526 /* Same for color setting/getting methods: */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002527 {"getbgcolor", (method)drawing_getbgcolor},
2528 {"getfgcolor", (method)drawing_getfgcolor},
2529 {"setbgcolor", (method)drawing_setbgcolor},
2530 {"setfgcolor", (method)drawing_setfgcolor},
Guido van Rossum0c2290b1991-04-03 19:12:14 +00002531
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002532 {NULL, NULL} /* sentinel */
2533};
2534
Guido van Rossumb6775db1994-08-01 11:34:53 +00002535#ifndef macintosh
Guido van Rossumcacd9571993-10-18 11:44:47 +00002536static int
2537checkstringlist(args, ps, pn)
2538 object *args;
2539 char ***ps;
2540 int *pn;
2541{
2542 int i, n;
2543 char **s;
2544 if (!is_listobject(args)) {
2545 err_setstr(TypeError, "list of strings expected");
2546 return 0;
2547 }
2548 n = getlistsize(args);
2549 s = NEW(char *, n+1);
2550 if (s == NULL) {
2551 err_nomem();
2552 return 0;
2553 }
2554 for (i = 0; i < n; i++) {
2555 object *item = getlistitem(args, i);
2556 if (!is_stringobject(item)) {
2557 err_setstr(TypeError, "list of strings expected");
2558 return 0;
2559 }
2560 s[i] = getstringvalue(item);
2561 }
2562 s[n] = NULL; /* In case caller wants a NULL-terminated list */
2563 *ps = s;
2564 *pn = n;
2565 return 1;
2566}
2567
2568static int
2569putbackstringlist(list, s, n)
2570 object *list;
2571 char **s;
2572 int n;
2573{
2574 int oldsize = getlistsize(list);
2575 object *newlist;
2576 int i;
2577 if (n == oldsize)
2578 return 1;
2579 newlist = newlistobject(n);
2580 for (i = 0; i < n && newlist != NULL; i++) {
2581 object *item = newstringobject(s[i]);
2582 if (item == NULL) {
2583 DECREF(newlist);
2584 newlist = NULL;
2585 }
2586 else
2587 setlistitem(newlist, i, item);
2588 }
2589 if (newlist == NULL)
2590 return 0;
2591 (*list->ob_type->tp_as_sequence->sq_ass_slice)
2592 (list, 0, oldsize, newlist);
2593 DECREF(newlist);
2594 return 1;
2595}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002596#endif /* macintosh */
Guido van Rossumcacd9571993-10-18 11:44:47 +00002597
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002598void
2599initstdwin()
2600{
Guido van Rossumbbf94341991-12-16 15:44:53 +00002601 object *m, *d;
2602 static int inited = 0;
Guido van Rossumc45611d1993-11-17 22:58:56 +00002603 char buf[1000];
Guido van Rossumbbf94341991-12-16 15:44:53 +00002604
Guido van Rossum2d14e211991-02-19 12:26:49 +00002605 if (!inited) {
Guido van Rossumb6775db1994-08-01 11:34:53 +00002606#ifdef macintosh
2607 winit();
2608#else
Guido van Rossumcacd9571993-10-18 11:44:47 +00002609 int argc = 0;
2610 char **argv = NULL;
2611 object *sys_argv = sysget("argv");
2612 if (sys_argv != NULL) {
2613 if (!checkstringlist(sys_argv, &argv, &argc))
2614 err_clear();
2615 }
Guido van Rossumc45611d1993-11-17 22:58:56 +00002616 if (argc > 0) {
2617 /* If argv[0] has a ".py" suffix, remove the suffix */
2618 char *p = strrchr(argv[0], '.');
2619 if (p != NULL && strcmp(p, ".py") == 0) {
2620 int n = p - argv[0];
2621 if (n >= sizeof(buf))
2622 n = sizeof(buf)-1;
2623 strncpy(buf, argv[0], n);
2624 buf[n] = '\0';
2625 argv[0] = buf;
2626 }
2627 }
Guido van Rossumcacd9571993-10-18 11:44:47 +00002628 winitargs(&argc, &argv);
2629 if (argv != NULL) {
2630 if (!putbackstringlist(sys_argv, argv, argc))
2631 err_clear();
2632 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002633#endif
Guido van Rossum2d14e211991-02-19 12:26:49 +00002634 inited = 1;
2635 }
Guido van Rossumbbf94341991-12-16 15:44:53 +00002636 m = initmodule("stdwin", stdwin_methods);
2637 d = getmoduledict(m);
2638
2639 /* Initialize stdwin.error exception */
2640 StdwinError = newstringobject("stdwin.error");
2641 if (StdwinError == NULL || dictinsert(d, "error", StdwinError) != 0)
2642 fatal("can't define stdwin.error");
Guido van Rossumb6775db1994-08-01 11:34:53 +00002643#ifdef WITH_THREAD
Guido van Rossumff4949e1992-08-05 19:58:53 +00002644 StdwinLock = allocate_lock();
2645 if (StdwinLock == NULL)
2646 fatal("can't allocate stdwin lock");
2647#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002648}