blob: 5ca798f49d00808b7110158c76b167f155928632 [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 Jansen3d7f6bd1995-01-26 16:40:10 +0000107int StdwinIsActive; /* True as soon as stdwin imported */
108
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000109/* Window and menu object types declared here because of forward references */
110
111typedef struct {
112 OB_HEAD
113 object *w_title;
114 WINDOW *w_win;
115 object *w_attr; /* Attributes dictionary */
116} windowobject;
117
Guido van Rossumb6775db1994-08-01 11:34:53 +0000118staticforward typeobject Windowtype;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000119
120#define is_windowobject(wp) ((wp)->ob_type == &Windowtype)
121
122typedef struct {
123 OB_HEAD
124 MENU *m_menu;
125 int m_id;
126 object *m_attr; /* Attributes dictionary */
127} menuobject;
128
Guido van Rossumb6775db1994-08-01 11:34:53 +0000129staticforward typeobject Menutype;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000130
131#define is_menuobject(mp) ((mp)->ob_type == &Menutype)
132
Guido van Rossumbf80e541993-02-08 15:49:17 +0000133typedef struct {
134 OB_HEAD
135 BITMAP *b_bitmap;
136 object *b_attr; /* Attributes dictionary */
137} bitmapobject;
138
Guido van Rossumb6775db1994-08-01 11:34:53 +0000139staticforward typeobject Bitmaptype;
Guido van Rossumbf80e541993-02-08 15:49:17 +0000140
141#define is_bitmapobject(mp) ((mp)->ob_type == &Bitmaptype)
142
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000143
144/* Strongly stdwin-specific argument handlers */
145
146static int
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000147getmenudetail(v, ep)
148 object *v;
149 EVENT *ep;
150{
Guido van Rossumfc58e581992-01-27 16:45:55 +0000151 menuobject *mp;
152 if (!getargs(v, "(Oi)", &mp, &ep->u.m.item))
153 return 0;
154 if (!is_menuobject(mp))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000155 return err_badarg();
Guido van Rossumfc58e581992-01-27 16:45:55 +0000156 ep->u.m.id = mp->m_id;
157 return 1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000158}
159
160static int
161geteventarg(v, ep)
162 object *v;
163 EVENT *ep;
164{
165 object *wp, *detail;
166 int a[4];
Guido van Rossumfc58e581992-01-27 16:45:55 +0000167 if (!getargs(v, "(iOO)", &ep->type, &wp, &detail))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000168 return 0;
Guido van Rossumfc58e581992-01-27 16:45:55 +0000169 if (is_windowobject(wp))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000170 ep->window = ((windowobject *)wp) -> w_win;
Guido van Rossumfc58e581992-01-27 16:45:55 +0000171 else if (wp == None)
172 ep->window = NULL;
173 else
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000174 return err_badarg();
Guido van Rossumfc58e581992-01-27 16:45:55 +0000175 switch (ep->type) {
176 case WE_CHAR: {
177 char c;
178 if (!getargs(detail, "c", &c))
179 return 0;
180 ep->u.character = c;
181 return 1;
182 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000183 case WE_COMMAND:
184 return getintarg(detail, &ep->u.command);
185 case WE_DRAW:
186 if (!getrectarg(detail, a))
187 return 0;
188 ep->u.area.left = a[0];
189 ep->u.area.top = a[1];
190 ep->u.area.right = a[2];
191 ep->u.area.bottom = a[3];
192 return 1;
193 case WE_MOUSE_DOWN:
194 case WE_MOUSE_UP:
195 case WE_MOUSE_MOVE:
Guido van Rossumfc58e581992-01-27 16:45:55 +0000196 return getargs(detail, "((ii)iii)",
197 &ep->u.where.h, &ep->u.where.v,
198 &ep->u.where.clicks,
199 &ep->u.where.button,
200 &ep->u.where.mask);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000201 case WE_MENU:
202 return getmenudetail(detail, ep);
Guido van Rossum3ee199e1992-06-30 12:48:26 +0000203 case WE_KEY:
204 return getargs(detail, "(ii)",
205 &ep->u.key.code, &ep->u.key.mask);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000206 default:
207 return 1;
208 }
209}
210
211
212/* Return construction tools */
213
214static object *
215makepoint(a, b)
216 int a, b;
217{
Guido van Rossum2ee12f41992-04-13 15:54:35 +0000218 return mkvalue("(ii)", a, b);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000219}
220
221static object *
222makerect(a, b, c, d)
223 int a, b, c, d;
224{
Guido van Rossum2ee12f41992-04-13 15:54:35 +0000225 return mkvalue("((ii)(ii))", a, b, c, d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000226}
227
228
229/* Drawing objects */
230
231typedef struct {
232 OB_HEAD
233 windowobject *d_ref;
234} drawingobject;
235
236static drawingobject *Drawing; /* Set to current drawing object, or NULL */
237
238/* Drawing methods */
239
Guido van Rossum3c284741991-11-27 14:54:54 +0000240static object *
241drawing_close(dp)
242 drawingobject *dp;
243{
244 if (dp->d_ref != NULL) {
245 wenddrawing(dp->d_ref->w_win);
246 Drawing = NULL;
247 DECREF(dp->d_ref);
248 dp->d_ref = NULL;
249 }
250 INCREF(None);
251 return None;
252}
Guido van Rossum77b46041992-01-14 18:41:24 +0000253
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000254static void
255drawing_dealloc(dp)
256 drawingobject *dp;
257{
Guido van Rossum3c284741991-11-27 14:54:54 +0000258 if (dp->d_ref != NULL) {
259 wenddrawing(dp->d_ref->w_win);
260 Drawing = NULL;
261 DECREF(dp->d_ref);
262 dp->d_ref = NULL;
263 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000264 free((char *)dp);
265}
266
267static object *
268drawing_generic(dp, args, func)
269 drawingobject *dp;
270 object *args;
271 void (*func) FPROTO((int, int, int, int));
272{
273 int a[4];
274 if (!getrectarg(args, a))
275 return NULL;
276 (*func)(a[0], a[1], a[2], a[3]);
277 INCREF(None);
278 return None;
279}
280
281static object *
282drawing_line(dp, args)
283 drawingobject *dp;
284 object *args;
285{
Guido van Rossumbf109731991-03-06 13:14:12 +0000286 return drawing_generic(dp, args, wdrawline);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000287}
288
289static object *
290drawing_xorline(dp, args)
291 drawingobject *dp;
292 object *args;
293{
Guido van Rossumbf109731991-03-06 13:14:12 +0000294 return drawing_generic(dp, args, wxorline);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000295}
296
297static object *
298drawing_circle(dp, args)
299 drawingobject *dp;
300 object *args;
301{
302 int a[3];
303 if (!getpointintarg(args, a))
304 return NULL;
305 wdrawcircle(a[0], a[1], a[2]);
306 INCREF(None);
307 return None;
308}
Guido van Rossuma2a181a1991-05-14 12:09:25 +0000309
Guido van Rossum27201061991-04-16 08:43:03 +0000310static object *
311drawing_fillcircle(dp, args)
312 drawingobject *dp;
313 object *args;
314{
315 int a[3];
316 if (!getpointintarg(args, a))
317 return NULL;
318 wfillcircle(a[0], a[1], a[2]);
319 INCREF(None);
320 return None;
321}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000322
323static object *
Guido van Rossuma2a181a1991-05-14 12:09:25 +0000324drawing_xorcircle(dp, args)
325 drawingobject *dp;
326 object *args;
327{
328 int a[3];
329 if (!getpointintarg(args, a))
330 return NULL;
331 wxorcircle(a[0], a[1], a[2]);
332 INCREF(None);
333 return None;
334}
335
336static object *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000337drawing_elarc(dp, args)
338 drawingobject *dp;
339 object *args;
340{
341 int a[6];
342 if (!get3pointarg(args, a))
343 return NULL;
344 wdrawelarc(a[0], a[1], a[2], a[3], a[4], a[5]);
345 INCREF(None);
346 return None;
347}
348
349static object *
Guido van Rossum27201061991-04-16 08:43:03 +0000350drawing_fillelarc(dp, args)
351 drawingobject *dp;
352 object *args;
353{
354 int a[6];
355 if (!get3pointarg(args, a))
356 return NULL;
357 wfillelarc(a[0], a[1], a[2], a[3], a[4], a[5]);
358 INCREF(None);
359 return None;
360}
361
362static object *
Guido van Rossuma2a181a1991-05-14 12:09:25 +0000363drawing_xorelarc(dp, args)
364 drawingobject *dp;
365 object *args;
366{
367 int a[6];
368 if (!get3pointarg(args, a))
369 return NULL;
370 wxorelarc(a[0], a[1], a[2], a[3], a[4], a[5]);
371 INCREF(None);
372 return None;
373}
374
375static object *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000376drawing_box(dp, args)
377 drawingobject *dp;
378 object *args;
379{
Guido van Rossumbf109731991-03-06 13:14:12 +0000380 return drawing_generic(dp, args, wdrawbox);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000381}
382
383static object *
384drawing_erase(dp, args)
385 drawingobject *dp;
386 object *args;
387{
Guido van Rossumbf109731991-03-06 13:14:12 +0000388 return drawing_generic(dp, args, werase);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000389}
390
391static object *
392drawing_paint(dp, args)
393 drawingobject *dp;
394 object *args;
395{
Guido van Rossumbf109731991-03-06 13:14:12 +0000396 return drawing_generic(dp, args, wpaint);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000397}
398
399static object *
400drawing_invert(dp, args)
401 drawingobject *dp;
402 object *args;
403{
Guido van Rossumbf109731991-03-06 13:14:12 +0000404 return drawing_generic(dp, args, winvert);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000405}
406
Guido van Rossum27201061991-04-16 08:43:03 +0000407static POINT *
408getpointsarray(v, psize)
409 object *v;
410 int *psize;
411{
412 int n = -1;
413 object * (*getitem) PROTO((object *, int));
414 int i;
415 POINT *points;
416
417 if (v == NULL)
418 ;
419 else if (is_listobject(v)) {
420 n = getlistsize(v);
421 getitem = getlistitem;
422 }
423 else if (is_tupleobject(v)) {
424 n = gettuplesize(v);
425 getitem = gettupleitem;
426 }
427
428 if (n <= 0) {
429 (void) err_badarg();
430 return NULL;
431 }
432
433 points = NEW(POINT, n);
434 if (points == NULL) {
435 (void) err_nomem();
436 return NULL;
437 }
438
439 for (i = 0; i < n; i++) {
440 object *w = (*getitem)(v, i);
441 int a[2];
442 if (!getpointarg(w, a)) {
443 DEL(points);
444 return NULL;
445 }
446 points[i].h = a[0];
447 points[i].v = a[1];
448 }
449
450 *psize = n;
451 return points;
452}
453
454static object *
455drawing_poly(dp, args)
456 drawingobject *dp;
457 object *args;
458{
459 int n;
460 POINT *points = getpointsarray(args, &n);
461 if (points == NULL)
462 return NULL;
463 wdrawpoly(n, points);
464 DEL(points);
465 INCREF(None);
466 return None;
467}
468
469static object *
470drawing_fillpoly(dp, args)
471 drawingobject *dp;
472 object *args;
473{
474 int n;
475 POINT *points = getpointsarray(args, &n);
476 if (points == NULL)
477 return NULL;
478 wfillpoly(n, points);
479 DEL(points);
480 INCREF(None);
481 return None;
482}
483
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000484static object *
Guido van Rossuma2a181a1991-05-14 12:09:25 +0000485drawing_xorpoly(dp, args)
486 drawingobject *dp;
487 object *args;
488{
489 int n;
490 POINT *points = getpointsarray(args, &n);
491 if (points == NULL)
492 return NULL;
493 wxorpoly(n, points);
494 DEL(points);
495 INCREF(None);
496 return None;
497}
498
499static object *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000500drawing_cliprect(dp, args)
501 drawingobject *dp;
502 object *args;
503{
Guido van Rossumbf109731991-03-06 13:14:12 +0000504 return drawing_generic(dp, args, wcliprect);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000505}
506
507static object *
508drawing_noclip(dp, args)
509 drawingobject *dp;
510 object *args;
511{
512 if (!getnoarg(args))
513 return NULL;
514 wnoclip();
515 INCREF(None);
516 return None;
517}
518
519static object *
520drawing_shade(dp, args)
521 drawingobject *dp;
522 object *args;
523{
524 int a[5];
525 if (!getrectintarg(args, a))
526 return NULL;
527 wshade(a[0], a[1], a[2], a[3], a[4]);
528 INCREF(None);
529 return None;
530}
531
532static object *
533drawing_text(dp, args)
534 drawingobject *dp;
535 object *args;
536{
Guido van Rossumfc58e581992-01-27 16:45:55 +0000537 int h, v, size;
538 char *text;
539 if (!getargs(args, "((ii)s#)", &h, &v, &text, &size))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000540 return NULL;
Guido van Rossumfc58e581992-01-27 16:45:55 +0000541 wdrawtext(h, v, text, size);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000542 INCREF(None);
543 return None;
544}
545
546/* The following four are also used as stdwin functions */
547
548static object *
549drawing_lineheight(dp, args)
550 drawingobject *dp;
551 object *args;
552{
553 if (!getnoarg(args))
554 return NULL;
555 return newintobject((long)wlineheight());
556}
557
558static object *
559drawing_baseline(dp, args)
560 drawingobject *dp;
561 object *args;
562{
563 if (!getnoarg(args))
564 return NULL;
565 return newintobject((long)wbaseline());
566}
567
568static object *
569drawing_textwidth(dp, args)
570 drawingobject *dp;
571 object *args;
572{
Guido van Rossumfc58e581992-01-27 16:45:55 +0000573 char *text;
574 int size;
575 if (!getargs(args, "s#", &text, &size))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000576 return NULL;
Guido van Rossumfc58e581992-01-27 16:45:55 +0000577 return newintobject((long)wtextwidth(text, size));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000578}
579
580static object *
581drawing_textbreak(dp, args)
582 drawingobject *dp;
583 object *args;
584{
Guido van Rossumfc58e581992-01-27 16:45:55 +0000585 char *text;
586 int size, width;
587 if (!getargs(args, "(s#i)", &text, &size, &width))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000588 return NULL;
Guido van Rossumfc58e581992-01-27 16:45:55 +0000589 return newintobject((long)wtextbreak(text, size, width));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000590}
591
Guido van Rossum0c2290b1991-04-03 19:12:14 +0000592static object *
593drawing_setfont(self, args)
594 drawingobject *self;
595 object *args;
596{
Guido van Rossumfc58e581992-01-27 16:45:55 +0000597 char *font;
598 char style = '\0';
599 int size = 0;
600 if (args == NULL || !is_tupleobject(args)) {
Guido van Rossum3c8ba7a1992-02-05 11:15:00 +0000601 if (!getargs(args, "z", &font))
Guido van Rossum50429a11991-04-04 15:24:07 +0000602 return NULL;
603 }
604 else {
Guido van Rossumfc58e581992-01-27 16:45:55 +0000605 int n = gettuplesize(args);
606 if (n == 2) {
607 if (!getargs(args, "(zi)", &font, &size))
608 return NULL;
609 }
610 else if (!getargs(args, "(zic)", &font, &size, &style)) {
611 err_clear();
612 if (!getargs(args, "(zci)", &font, &style, &size))
613 return NULL;
Guido van Rossum50429a11991-04-04 15:24:07 +0000614 }
615 }
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000616 if (font != NULL) {
617 if (!wsetfont(font)) {
618 err_setstr(StdwinError, "font not found");
619 return NULL;
620 }
621 }
Guido van Rossum50429a11991-04-04 15:24:07 +0000622 if (size != 0)
623 wsetsize(size);
Guido van Rossumfc58e581992-01-27 16:45:55 +0000624 switch (style) {
625 case 'b':
626 wsetbold();
627 break;
628 case 'i':
629 wsetitalic();
630 break;
631 case 'o':
632 wsetbolditalic();
633 break;
634 case 'u':
635 wsetunderline();
636 break;
637 case 'p':
638 wsetplain();
639 break;
640 }
Guido van Rossum0c2290b1991-04-03 19:12:14 +0000641 INCREF(None);
642 return None;
643}
644
645static object *
646drawing_getbgcolor(self, args)
647 object *self;
648 object *args;
649{
650 if (!getnoarg(args))
651 return NULL;
652 return newintobject((long)wgetbgcolor());
653}
654
655static object *
656drawing_getfgcolor(self, args)
657 object *self;
658 object *args;
659{
660 if (!getnoarg(args))
661 return NULL;
662 return newintobject((long)wgetfgcolor());
663}
664
665static object *
666drawing_setbgcolor(self, args)
667 object *self;
668 object *args;
669{
670 long color;
671 if (!getlongarg(args, &color))
672 return NULL;
673 wsetbgcolor((COLOR)color);
674 INCREF(None);
675 return None;
676}
677
678static object *
679drawing_setfgcolor(self, args)
680 object *self;
681 object *args;
682{
683 long color;
684 if (!getlongarg(args, &color))
685 return NULL;
686 wsetfgcolor((COLOR)color);
687 INCREF(None);
688 return None;
689}
690
Guido van Rossume9066061993-07-29 13:14:32 +0000691#ifdef HAVE_BITMAPS
692
Guido van Rossumbf80e541993-02-08 15:49:17 +0000693static object *
694drawing_bitmap(self, args)
695 object *self;
696 object *args;
697{
698 int h, v;
699 object *bp;
700 object *mask = NULL;
701 if (!getargs(args, "((ii)O)", &h, &v, &bp)) {
702 err_clear();
703 if (!getargs(args, "((ii)OO)", &h, &v, &bp, &mask))
704 return NULL;
705 if (mask == None)
706 mask = NULL;
707 else if (!is_bitmapobject(mask)) {
708 err_badarg();
709 return NULL;
710 }
711 }
712 if (!is_bitmapobject(bp)) {
713 err_badarg();
714 return NULL;
715 }
716 if (((bitmapobject *)bp)->b_bitmap == NULL ||
717 mask != NULL && ((bitmapobject *)mask)->b_bitmap == NULL) {
718 err_setstr(StdwinError, "bitmap object already close");
719 return NULL;
720 }
721 if (mask == NULL)
722 wdrawbitmap(h, v, ((bitmapobject *)bp)->b_bitmap, ALLBITS);
723 else
724 wdrawbitmap(h, v,
725 ((bitmapobject *)bp)->b_bitmap,
726 ((bitmapobject *)bp)->b_bitmap);
727 INCREF(None);
728 return None;
729}
730
Guido van Rossume9066061993-07-29 13:14:32 +0000731#endif /* HAVE_BITMAPS */
732
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000733static struct methodlist drawing_methods[] = {
Guido van Rossume9066061993-07-29 13:14:32 +0000734#ifdef HAVE_BITMAPS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000735 {"bitmap", (method)drawing_bitmap},
Guido van Rossume9066061993-07-29 13:14:32 +0000736#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000737 {"box", (method)drawing_box},
738 {"circle", (method)drawing_circle},
739 {"cliprect", (method)drawing_cliprect},
740 {"close", (method)drawing_close},
741 {"elarc", (method)drawing_elarc},
742 {"enddrawing", (method)drawing_close},
743 {"erase", (method)drawing_erase},
744 {"fillcircle", (method)drawing_fillcircle},
745 {"fillelarc", (method)drawing_fillelarc},
746 {"fillpoly", (method)drawing_fillpoly},
747 {"invert", (method)drawing_invert},
748 {"line", (method)drawing_line},
749 {"noclip", (method)drawing_noclip},
750 {"paint", (method)drawing_paint},
751 {"poly", (method)drawing_poly},
752 {"shade", (method)drawing_shade},
753 {"text", (method)drawing_text},
754 {"xorcircle", (method)drawing_xorcircle},
755 {"xorelarc", (method)drawing_xorelarc},
756 {"xorline", (method)drawing_xorline},
757 {"xorpoly", (method)drawing_xorpoly},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000758
759 /* Text measuring methods: */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000760 {"baseline", (method)drawing_baseline},
761 {"lineheight", (method)drawing_lineheight},
762 {"textbreak", (method)drawing_textbreak},
763 {"textwidth", (method)drawing_textwidth},
Guido van Rossum0c2290b1991-04-03 19:12:14 +0000764
765 /* Font setting methods: */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000766 {"setfont", (method)drawing_setfont},
Guido van Rossum0c2290b1991-04-03 19:12:14 +0000767
768 /* Color methods: */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000769 {"getbgcolor", (method)drawing_getbgcolor},
770 {"getfgcolor", (method)drawing_getfgcolor},
771 {"setbgcolor", (method)drawing_setbgcolor},
772 {"setfgcolor", (method)drawing_setfgcolor},
Guido van Rossum0c2290b1991-04-03 19:12:14 +0000773
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000774 {NULL, NULL} /* sentinel */
775};
776
777static object *
Guido van Rossum77b46041992-01-14 18:41:24 +0000778drawing_getattr(dp, name)
779 drawingobject *dp;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000780 char *name;
781{
Guido van Rossum77b46041992-01-14 18:41:24 +0000782 if (dp->d_ref == NULL) {
783 err_setstr(StdwinError, "drawing object already closed");
784 return NULL;
785 }
786 return findmethod(drawing_methods, (object *)dp, name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000787}
788
Guido van Rossum541c8c01991-05-05 20:13:41 +0000789typeobject Drawingtype = {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000790 OB_HEAD_INIT(&Typetype)
791 0, /*ob_size*/
792 "drawing", /*tp_name*/
793 sizeof(drawingobject), /*tp_size*/
794 0, /*tp_itemsize*/
795 /* methods */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000796 (destructor)drawing_dealloc, /*tp_dealloc*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000797 0, /*tp_print*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000798 (getattrfunc)drawing_getattr, /*tp_getattr*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000799 0, /*tp_setattr*/
800 0, /*tp_compare*/
801 0, /*tp_repr*/
802};
803
804
805/* Text(edit) objects */
806
807typedef struct {
808 OB_HEAD
809 TEXTEDIT *t_text;
810 windowobject *t_ref;
811 object *t_attr; /* Attributes dictionary */
812} textobject;
813
Guido van Rossumb6775db1994-08-01 11:34:53 +0000814staticforward typeobject Texttype;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000815
816static textobject *
817newtextobject(wp, left, top, right, bottom)
818 windowobject *wp;
819 int left, top, right, bottom;
820{
821 textobject *tp;
822 tp = NEWOBJ(textobject, &Texttype);
823 if (tp == NULL)
824 return NULL;
825 tp->t_attr = NULL;
826 INCREF(wp);
827 tp->t_ref = wp;
828 tp->t_text = tecreate(wp->w_win, left, top, right, bottom);
829 if (tp->t_text == NULL) {
830 DECREF(tp);
831 return (textobject *) err_nomem();
832 }
833 return tp;
834}
835
836/* Text(edit) methods */
837
838static void
839text_dealloc(tp)
840 textobject *tp;
841{
842 if (tp->t_text != NULL)
843 tefree(tp->t_text);
Guido van Rossum3c284741991-11-27 14:54:54 +0000844 XDECREF(tp->t_attr);
845 XDECREF(tp->t_ref);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000846 DEL(tp);
847}
848
849static object *
Guido van Rossum3c284741991-11-27 14:54:54 +0000850text_close(tp, args)
851 textobject *tp;
852 object *args;
853{
854 if (tp->t_text != NULL) {
855 tefree(tp->t_text);
856 tp->t_text = NULL;
857 }
858 if (tp->t_attr != NULL) {
859 DECREF(tp->t_attr);
860 tp->t_attr = NULL;
861 }
862 if (tp->t_ref != NULL) {
863 DECREF(tp->t_ref);
864 tp->t_ref = NULL;
865 }
866 INCREF(None);
867 return None;
868}
869
870static object *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000871text_arrow(self, args)
872 textobject *self;
873 object *args;
874{
875 int code;
876 if (!getintarg(args, &code))
877 return NULL;
878 tearrow(self->t_text, code);
879 INCREF(None);
880 return None;
881}
882
883static object *
884text_draw(self, args)
885 textobject *self;
886 object *args;
887{
888 register TEXTEDIT *tp = self->t_text;
889 int a[4];
890 int left, top, right, bottom;
891 if (!getrectarg(args, a))
892 return NULL;
893 if (Drawing != NULL) {
Guido van Rossum87e7ea71991-12-10 14:00:03 +0000894 err_setstr(StdwinError, "already drawing");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000895 return NULL;
896 }
897 /* Clip to text area and ignore if area is empty */
898 left = tegetleft(tp);
899 top = tegettop(tp);
900 right = tegetright(tp);
901 bottom = tegetbottom(tp);
902 if (a[0] < left) a[0] = left;
903 if (a[1] < top) a[1] = top;
904 if (a[2] > right) a[2] = right;
905 if (a[3] > bottom) a[3] = bottom;
906 if (a[0] < a[2] && a[1] < a[3]) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000907 wbegindrawing(self->t_ref->w_win);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000908 tedrawnew(tp, a[0], a[1], a[2], a[3]);
909 wenddrawing(self->t_ref->w_win);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000910 }
911 INCREF(None);
912 return None;
913}
914
915static object *
916text_event(self, args)
917 textobject *self;
918 object *args;
919{
920 register TEXTEDIT *tp = self->t_text;
921 EVENT e;
922 if (!geteventarg(args, &e))
923 return NULL;
924 if (e.type == WE_MOUSE_DOWN) {
Guido van Rossum33f17701991-02-13 23:19:39 +0000925 /* Cheat at the margins */
926 int width, height;
927 wgetdocsize(e.window, &width, &height);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000928 if (e.u.where.h < 0 && tegetleft(tp) == 0)
929 e.u.where.h = 0;
Guido van Rossum33f17701991-02-13 23:19:39 +0000930 else if (e.u.where.h > width && tegetright(tp) == width)
931 e.u.where.h = width;
932 if (e.u.where.v < 0 && tegettop(tp) == 0)
933 e.u.where.v = 0;
934 else if (e.u.where.v > height && tegetright(tp) == height)
935 e.u.where.v = height;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000936 }
937 return newintobject((long) teevent(tp, &e));
938}
939
940static object *
941text_getfocus(self, args)
942 textobject *self;
943 object *args;
944{
945 if (!getnoarg(args))
946 return NULL;
947 return makepoint(tegetfoc1(self->t_text), tegetfoc2(self->t_text));
948}
949
950static object *
951text_getfocustext(self, args)
952 textobject *self;
953 object *args;
954{
955 int f1, f2;
956 char *text;
957 if (!getnoarg(args))
958 return NULL;
959 f1 = tegetfoc1(self->t_text);
960 f2 = tegetfoc2(self->t_text);
961 text = tegettext(self->t_text);
962 return newsizedstringobject(text + f1, f2-f1);
963}
964
965static object *
966text_getrect(self, args)
967 textobject *self;
968 object *args;
969{
970 if (!getnoarg(args))
971 return NULL;
972 return makerect(tegetleft(self->t_text),
973 tegettop(self->t_text),
974 tegetright(self->t_text),
975 tegetbottom(self->t_text));
976}
977
978static object *
979text_gettext(self, args)
980 textobject *self;
981 object *args;
982{
983 if (!getnoarg(args))
984 return NULL;
985 return newsizedstringobject(tegettext(self->t_text),
986 tegetlen(self->t_text));
987}
988
989static object *
990text_move(self, args)
991 textobject *self;
992 object *args;
993{
994 int a[4];
995 if (!getrectarg(args, a))
996 return NULL;
997 temovenew(self->t_text, a[0], a[1], a[2], a[3]);
998 INCREF(None);
999 return None;
1000}
1001
1002static object *
Guido van Rossum4b9cf8e1991-05-28 21:57:04 +00001003text_replace(self, args)
1004 textobject *self;
1005 object *args;
1006{
Guido van Rossumfc58e581992-01-27 16:45:55 +00001007 char *text;
Guido van Rossum4b9cf8e1991-05-28 21:57:04 +00001008 if (!getstrarg(args, &text))
1009 return NULL;
Guido van Rossumfc58e581992-01-27 16:45:55 +00001010 tereplace(self->t_text, text);
Guido van Rossum4b9cf8e1991-05-28 21:57:04 +00001011 INCREF(None);
1012 return None;
1013}
1014
1015static object *
1016text_setactive(self, args)
1017 textobject *self;
1018 object *args;
1019{
1020 int flag;
1021 if (!getintarg(args, &flag))
1022 return NULL;
1023 tesetactive(self->t_text, flag);
1024 INCREF(None);
1025 return None;
1026}
1027
1028static object *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001029text_setfocus(self, args)
1030 textobject *self;
1031 object *args;
1032{
1033 int a[2];
1034 if (!getpointarg(args, a))
1035 return NULL;
1036 tesetfocus(self->t_text, a[0], a[1]);
1037 INCREF(None);
1038 return None;
1039}
1040
1041static object *
Guido van Rossum541c8c01991-05-05 20:13:41 +00001042text_settext(self, args)
1043 textobject *self;
1044 object *args;
1045{
Guido van Rossumfc58e581992-01-27 16:45:55 +00001046 char *text;
Guido van Rossum541c8c01991-05-05 20:13:41 +00001047 char *buf;
1048 int size;
Guido van Rossumfc58e581992-01-27 16:45:55 +00001049 if (!getargs(args, "s#", &text, &size))
Guido van Rossum541c8c01991-05-05 20:13:41 +00001050 return NULL;
Guido van Rossum541c8c01991-05-05 20:13:41 +00001051 if ((buf = NEW(char, size)) == NULL) {
Guido van Rossum87e7ea71991-12-10 14:00:03 +00001052 return err_nomem();
Guido van Rossum541c8c01991-05-05 20:13:41 +00001053 }
Guido van Rossumfc58e581992-01-27 16:45:55 +00001054 memcpy(buf, text, size);
Guido van Rossum541c8c01991-05-05 20:13:41 +00001055 tesetbuf(self->t_text, buf, size); /* Becomes owner of buffer */
1056 INCREF(None);
1057 return None;
1058}
1059
1060static object *
Guido van Rossum4b9cf8e1991-05-28 21:57:04 +00001061text_setview(self, args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001062 textobject *self;
1063 object *args;
1064{
Guido van Rossum4b9cf8e1991-05-28 21:57:04 +00001065 int a[4];
1066 if (args == None)
1067 tenoview(self->t_text);
1068 else {
1069 if (!getrectarg(args, a))
1070 return NULL;
1071 tesetview(self->t_text, a[0], a[1], a[2], a[3]);
1072 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001073 INCREF(None);
1074 return None;
1075}
1076
1077static struct methodlist text_methods[] = {
Guido van Rossumb6775db1994-08-01 11:34:53 +00001078 {"arrow", (method)text_arrow},
1079 {"close", (method)text_close},
1080 {"draw", (method)text_draw},
1081 {"event", (method)text_event},
1082 {"getfocus", (method)text_getfocus},
1083 {"getfocustext",(method)text_getfocustext},
1084 {"getrect", (method)text_getrect},
1085 {"gettext", (method)text_gettext},
1086 {"move", (method)text_move},
1087 {"replace", (method)text_replace},
1088 {"setactive", (method)text_setactive},
1089 {"setfocus", (method)text_setfocus},
1090 {"settext", (method)text_settext},
1091 {"setview", (method)text_setview},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001092 {NULL, NULL} /* sentinel */
1093};
1094
1095static object *
1096text_getattr(tp, name)
1097 textobject *tp;
1098 char *name;
1099{
Guido van Rossum85f50761991-10-20 20:22:50 +00001100 object *v = NULL;
Guido van Rossum77b46041992-01-14 18:41:24 +00001101 if (tp->t_ref == NULL) {
1102 err_setstr(StdwinError, "text object already closed");
1103 return NULL;
1104 }
Guido van Rossum85f50761991-10-20 20:22:50 +00001105 if (strcmp(name, "__dict__") == 0) {
1106 v = tp->t_attr;
1107 if (v == NULL)
1108 v = None;
1109 }
1110 else if (tp->t_attr != NULL) {
1111 v = dictlookup(tp->t_attr, name);
1112 }
1113 if (v != NULL) {
1114 INCREF(v);
1115 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001116 }
1117 return findmethod(text_methods, (object *)tp, name);
1118}
1119
1120static int
1121text_setattr(tp, name, v)
1122 textobject *tp;
1123 char *name;
1124 object *v;
1125{
1126 if (tp->t_attr == NULL) {
1127 tp->t_attr = newdictobject();
1128 if (tp->t_attr == NULL)
1129 return -1;
1130 }
Guido van Rossum94472a01992-09-04 09:45:18 +00001131 if (v == NULL) {
1132 int rv = dictremove(tp->t_attr, name);
1133 if (rv < 0)
1134 err_setstr(AttributeError,
1135 "delete non-existing text object attribute");
1136 return rv;
1137 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001138 else
1139 return dictinsert(tp->t_attr, name, v);
1140}
1141
Guido van Rossumb6775db1994-08-01 11:34:53 +00001142static typeobject Texttype = {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001143 OB_HEAD_INIT(&Typetype)
1144 0, /*ob_size*/
1145 "textedit", /*tp_name*/
1146 sizeof(textobject), /*tp_size*/
1147 0, /*tp_itemsize*/
1148 /* methods */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001149 (destructor)text_dealloc, /*tp_dealloc*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001150 0, /*tp_print*/
Guido van Rossumb6775db1994-08-01 11:34:53 +00001151 (getattrfunc)text_getattr, /*tp_getattr*/
1152 (setattrfunc)text_setattr, /*tp_setattr*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001153 0, /*tp_compare*/
1154 0, /*tp_repr*/
1155};
1156
1157
1158/* Menu objects */
1159
Guido van Rossum2d14e211991-02-19 12:26:49 +00001160#define IDOFFSET 10 /* Menu IDs we use start here */
Guido van Rossum27201061991-04-16 08:43:03 +00001161#define MAXNMENU 200 /* Max #menus we allow */
Guido van Rossum2d14e211991-02-19 12:26:49 +00001162static menuobject *menulist[MAXNMENU];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001163
Guido van Rossumfc58e581992-01-27 16:45:55 +00001164static menuobject *newmenuobject PROTO((char *));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001165static menuobject *
1166newmenuobject(title)
Guido van Rossumfc58e581992-01-27 16:45:55 +00001167 char *title;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001168{
1169 int id;
1170 MENU *menu;
1171 menuobject *mp;
Guido van Rossum2d14e211991-02-19 12:26:49 +00001172 for (id = 0; id < MAXNMENU; id++) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001173 if (menulist[id] == NULL)
1174 break;
1175 }
Guido van Rossum27201061991-04-16 08:43:03 +00001176 if (id >= MAXNMENU) {
Guido van Rossum87e7ea71991-12-10 14:00:03 +00001177 err_setstr(StdwinError, "creating too many menus");
Guido van Rossum27201061991-04-16 08:43:03 +00001178 return NULL;
1179 }
Guido van Rossumfc58e581992-01-27 16:45:55 +00001180 menu = wmenucreate(id + IDOFFSET, title);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001181 if (menu == NULL)
1182 return (menuobject *) err_nomem();
1183 mp = NEWOBJ(menuobject, &Menutype);
1184 if (mp != NULL) {
1185 mp->m_menu = menu;
Guido van Rossum2d14e211991-02-19 12:26:49 +00001186 mp->m_id = id + IDOFFSET;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001187 mp->m_attr = NULL;
1188 menulist[id] = mp;
1189 }
1190 else
1191 wmenudelete(menu);
1192 return mp;
1193}
1194
1195/* Menu methods */
1196
1197static void
1198menu_dealloc(mp)
1199 menuobject *mp;
1200{
1201
Guido van Rossum2d14e211991-02-19 12:26:49 +00001202 int id = mp->m_id - IDOFFSET;
1203 if (id >= 0 && id < MAXNMENU && menulist[id] == mp) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001204 menulist[id] = NULL;
1205 }
Guido van Rossum77b46041992-01-14 18:41:24 +00001206 if (mp->m_menu != NULL)
1207 wmenudelete(mp->m_menu);
1208 XDECREF(mp->m_attr);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001209 DEL(mp);
1210}
1211
1212static object *
Guido van Rossum77b46041992-01-14 18:41:24 +00001213menu_close(mp, args)
1214 menuobject *mp;
1215 object *args;
1216{
1217 int id = mp->m_id - IDOFFSET;
1218 if (id >= 0 && id < MAXNMENU && menulist[id] == mp) {
1219 menulist[id] = NULL;
1220 }
1221 mp->m_id = -1;
1222 if (mp->m_menu != NULL)
1223 wmenudelete(mp->m_menu);
1224 mp->m_menu = NULL;
1225 XDECREF(mp->m_attr);
1226 mp->m_attr = NULL;
1227 INCREF(None);
1228 return None;
1229}
1230
1231static object *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001232menu_additem(self, args)
1233 menuobject *self;
1234 object *args;
1235{
Guido van Rossumfc58e581992-01-27 16:45:55 +00001236 char *text;
1237 int shortcut = -1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001238 if (is_tupleobject(args)) {
Guido van Rossumfc58e581992-01-27 16:45:55 +00001239 char c;
1240 if (!getargs(args, "(sc)", &text, &c))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001241 return NULL;
Guido van Rossumfc58e581992-01-27 16:45:55 +00001242 shortcut = c;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001243 }
Guido van Rossumfc58e581992-01-27 16:45:55 +00001244 else if (!getstrarg(args, &text))
1245 return NULL;
1246 wmenuadditem(self->m_menu, text, shortcut);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001247 INCREF(None);
1248 return None;
1249}
1250
1251static object *
1252menu_setitem(self, args)
1253 menuobject *self;
1254 object *args;
1255{
1256 int index;
Guido van Rossumfc58e581992-01-27 16:45:55 +00001257 char *text;
Guido van Rossum234f9421993-06-17 12:35:49 +00001258 if (!getargs(args, "(is)", &index, &text))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001259 return NULL;
Guido van Rossumfc58e581992-01-27 16:45:55 +00001260 wmenusetitem(self->m_menu, index, text);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001261 INCREF(None);
1262 return None;
1263}
1264
1265static object *
1266menu_enable(self, args)
1267 menuobject *self;
1268 object *args;
1269{
1270 int index;
1271 int flag;
Guido van Rossum234f9421993-06-17 12:35:49 +00001272 if (!getargs(args, "(ii)", &index, &flag))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001273 return NULL;
1274 wmenuenable(self->m_menu, index, flag);
1275 INCREF(None);
1276 return None;
1277}
1278
1279static object *
1280menu_check(self, args)
1281 menuobject *self;
1282 object *args;
1283{
1284 int index;
1285 int flag;
Guido van Rossum234f9421993-06-17 12:35:49 +00001286 if (!getargs(args, "(ii)", &index, &flag))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001287 return NULL;
1288 wmenucheck(self->m_menu, index, flag);
1289 INCREF(None);
1290 return None;
1291}
1292
1293static struct methodlist menu_methods[] = {
Guido van Rossumb6775db1994-08-01 11:34:53 +00001294 {"additem", (method)menu_additem},
1295 {"setitem", (method)menu_setitem},
1296 {"enable", (method)menu_enable},
1297 {"check", (method)menu_check},
1298 {"close", (method)menu_close},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001299 {NULL, NULL} /* sentinel */
1300};
1301
1302static object *
1303menu_getattr(mp, name)
1304 menuobject *mp;
1305 char *name;
1306{
Guido van Rossum85f50761991-10-20 20:22:50 +00001307 object *v = NULL;
Guido van Rossum77b46041992-01-14 18:41:24 +00001308 if (mp->m_menu == NULL) {
1309 err_setstr(StdwinError, "menu object already closed");
1310 return NULL;
1311 }
Guido van Rossum85f50761991-10-20 20:22:50 +00001312 if (strcmp(name, "__dict__") == 0) {
1313 v = mp->m_attr;
1314 if (v == NULL)
1315 v = None;
1316 }
1317 else if (mp->m_attr != NULL) {
1318 v = dictlookup(mp->m_attr, name);
1319 }
1320 if (v != NULL) {
1321 INCREF(v);
1322 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001323 }
1324 return findmethod(menu_methods, (object *)mp, name);
1325}
1326
1327static int
1328menu_setattr(mp, name, v)
1329 menuobject *mp;
1330 char *name;
1331 object *v;
1332{
1333 if (mp->m_attr == NULL) {
1334 mp->m_attr = newdictobject();
1335 if (mp->m_attr == NULL)
1336 return -1;
1337 }
Guido van Rossum94472a01992-09-04 09:45:18 +00001338 if (v == NULL) {
1339 int rv = dictremove(mp->m_attr, name);
1340 if (rv < 0)
1341 err_setstr(AttributeError,
1342 "delete non-existing menu object attribute");
1343 return rv;
1344 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001345 else
1346 return dictinsert(mp->m_attr, name, v);
1347}
1348
Guido van Rossumb6775db1994-08-01 11:34:53 +00001349static typeobject Menutype = {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001350 OB_HEAD_INIT(&Typetype)
1351 0, /*ob_size*/
1352 "menu", /*tp_name*/
1353 sizeof(menuobject), /*tp_size*/
1354 0, /*tp_itemsize*/
1355 /* methods */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001356 (destructor)menu_dealloc, /*tp_dealloc*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001357 0, /*tp_print*/
Guido van Rossumb6775db1994-08-01 11:34:53 +00001358 (getattrfunc)menu_getattr, /*tp_getattr*/
1359 (setattrfunc)menu_setattr, /*tp_setattr*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001360 0, /*tp_compare*/
1361 0, /*tp_repr*/
1362};
1363
1364
Guido van Rossume9066061993-07-29 13:14:32 +00001365#ifdef HAVE_BITMAPS
1366
Guido van Rossumbf80e541993-02-08 15:49:17 +00001367/* Bitmaps objects */
1368
1369static bitmapobject *newbitmapobject PROTO((int, int));
1370static bitmapobject *
1371newbitmapobject(width, height)
1372 int width, height;
1373{
1374 BITMAP *bitmap;
1375 bitmapobject *bp;
1376 bitmap = wnewbitmap(width, height);
1377 if (bitmap == NULL)
1378 return (bitmapobject *) err_nomem();
1379 bp = NEWOBJ(bitmapobject, &Bitmaptype);
1380 if (bp != NULL) {
1381 bp->b_bitmap = bitmap;
1382 bp->b_attr = NULL;
1383 }
1384 else
1385 wfreebitmap(bitmap);
1386 return bp;
1387}
1388
1389/* Bitmap methods */
1390
1391static void
1392bitmap_dealloc(bp)
1393 bitmapobject *bp;
1394{
1395 if (bp->b_bitmap != NULL)
1396 wfreebitmap(bp->b_bitmap);
1397 XDECREF(bp->b_attr);
1398 DEL(bp);
1399}
1400
1401static object *
1402bitmap_close(bp, args)
1403 bitmapobject *bp;
1404 object *args;
1405{
1406 if (bp->b_bitmap != NULL)
1407 wfreebitmap(bp->b_bitmap);
1408 bp->b_bitmap = NULL;
1409 XDECREF(bp->b_attr);
1410 bp->b_attr = NULL;
1411 INCREF(None);
1412 return None;
1413}
1414
1415static object *
1416bitmap_setbit(self, args)
1417 bitmapobject *self;
1418 object *args;
1419{
1420 int a[3];
1421 if (!getpointintarg(args, a))
1422 return NULL;
1423 wsetbit(self->b_bitmap, a[0], a[1], a[2]);
1424 INCREF(None);
1425 return None;
1426}
1427
1428static object *
1429bitmap_getbit(self, args)
1430 bitmapobject *self;
1431 object *args;
1432{
1433 int a[2];
1434 if (!getpointarg(args, a))
1435 return NULL;
1436 return newintobject((long) wgetbit(self->b_bitmap, a[0], a[1]));
1437}
1438
1439static object *
1440bitmap_getsize(self, args)
1441 bitmapobject *self;
1442 object *args;
1443{
1444 int width, height;
1445 if (!getnoarg(args))
1446 return NULL;
1447 wgetbitmapsize(self->b_bitmap, &width, &height);
1448 return mkvalue("(ii)", width, height);
1449}
1450
1451static struct methodlist bitmap_methods[] = {
Guido van Rossumb6775db1994-08-01 11:34:53 +00001452 {"close", (method)bitmap_close},
1453 {"getsize", (method)bitmap_getsize},
1454 {"getbit", (method)bitmap_getbit},
1455 {"setbit", (method)bitmap_setbit},
Guido van Rossumbf80e541993-02-08 15:49:17 +00001456 {NULL, NULL} /* sentinel */
1457};
1458
1459static object *
1460bitmap_getattr(bp, name)
1461 bitmapobject *bp;
1462 char *name;
1463{
1464 object *v = NULL;
1465 if (bp->b_bitmap == NULL) {
1466 err_setstr(StdwinError, "bitmap object already closed");
1467 return NULL;
1468 }
1469 if (strcmp(name, "__dict__") == 0) {
1470 v = bp->b_attr;
1471 if (v == NULL)
1472 v = None;
1473 }
1474 else if (bp->b_attr != NULL) {
1475 v = dictlookup(bp->b_attr, name);
1476 }
1477 if (v != NULL) {
1478 INCREF(v);
1479 return v;
1480 }
1481 return findmethod(bitmap_methods, (object *)bp, name);
1482}
1483
1484static int
1485bitmap_setattr(bp, name, v)
1486 bitmapobject *bp;
1487 char *name;
1488 object *v;
1489{
1490 if (bp->b_attr == NULL) {
1491 bp->b_attr = newdictobject();
1492 if (bp->b_attr == NULL)
1493 return -1;
1494 }
1495 if (v == NULL) {
1496 int rv = dictremove(bp->b_attr, name);
1497 if (rv < 0)
1498 err_setstr(AttributeError,
1499 "delete non-existing bitmap object attribute");
1500 return rv;
1501 }
1502 else
1503 return dictinsert(bp->b_attr, name, v);
1504}
1505
Guido van Rossumb6775db1994-08-01 11:34:53 +00001506static typeobject Bitmaptype = {
Guido van Rossumbf80e541993-02-08 15:49:17 +00001507 OB_HEAD_INIT(&Typetype)
1508 0, /*ob_size*/
1509 "bitmap", /*tp_name*/
1510 sizeof(bitmapobject), /*tp_size*/
1511 0, /*tp_itemsize*/
1512 /* methods */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001513 (destructor)bitmap_dealloc, /*tp_dealloc*/
Guido van Rossumbf80e541993-02-08 15:49:17 +00001514 0, /*tp_print*/
Guido van Rossumb6775db1994-08-01 11:34:53 +00001515 (getattrfunc)bitmap_getattr, /*tp_getattr*/
1516 (setattrfunc)bitmap_setattr, /*tp_setattr*/
Guido van Rossumbf80e541993-02-08 15:49:17 +00001517 0, /*tp_compare*/
1518 0, /*tp_repr*/
1519};
1520
Guido van Rossume9066061993-07-29 13:14:32 +00001521#endif /* HAVE_BITMAPS */
1522
Guido van Rossumbf80e541993-02-08 15:49:17 +00001523
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001524/* Windows */
1525
1526#define MAXNWIN 50
1527static windowobject *windowlist[MAXNWIN];
1528
1529/* Window methods */
1530
1531static void
1532window_dealloc(wp)
1533 windowobject *wp;
1534{
1535 if (wp->w_win != NULL) {
1536 int tag = wgettag(wp->w_win);
1537 if (tag >= 0 && tag < MAXNWIN)
1538 windowlist[tag] = NULL;
1539 else
1540 fprintf(stderr, "XXX help! tag %d in window_dealloc\n",
1541 tag);
1542 wclose(wp->w_win);
1543 }
1544 DECREF(wp->w_title);
1545 if (wp->w_attr != NULL)
1546 DECREF(wp->w_attr);
1547 free((char *)wp);
1548}
1549
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001550static object *
Guido van Rossum3c284741991-11-27 14:54:54 +00001551window_close(wp, args)
1552 windowobject *wp;
1553 object *args;
1554{
1555 if (wp->w_win != NULL) {
1556 int tag = wgettag(wp->w_win);
1557 if (tag >= 0 && tag < MAXNWIN)
1558 windowlist[tag] = NULL;
1559 wclose(wp->w_win);
1560 wp->w_win = NULL;
1561 }
1562 INCREF(None);
1563 return None;
1564}
1565
1566static object *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001567window_begindrawing(wp, args)
1568 windowobject *wp;
1569 object *args;
1570{
1571 drawingobject *dp;
1572 if (!getnoarg(args))
1573 return NULL;
1574 if (Drawing != NULL) {
Guido van Rossum87e7ea71991-12-10 14:00:03 +00001575 err_setstr(StdwinError, "already drawing");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001576 return NULL;
1577 }
1578 dp = NEWOBJ(drawingobject, &Drawingtype);
1579 if (dp == NULL)
1580 return NULL;
1581 Drawing = dp;
1582 INCREF(wp);
1583 dp->d_ref = wp;
1584 wbegindrawing(wp->w_win);
1585 return (object *)dp;
1586}
1587
1588static object *
1589window_change(wp, args)
1590 windowobject *wp;
1591 object *args;
1592{
1593 int a[4];
1594 if (!getrectarg(args, a))
1595 return NULL;
1596 wchange(wp->w_win, a[0], a[1], a[2], a[3]);
1597 INCREF(None);
1598 return None;
1599}
1600
1601static object *
1602window_gettitle(wp, args)
1603 windowobject *wp;
1604 object *args;
1605{
1606 if (!getnoarg(args))
1607 return NULL;
1608 INCREF(wp->w_title);
1609 return wp->w_title;
1610}
1611
1612static object *
Guido van Rossum541c8c01991-05-05 20:13:41 +00001613window_getwinpos(wp, args)
1614 windowobject *wp;
1615 object *args;
1616{
1617 int h, v;
1618 if (!getnoarg(args))
1619 return NULL;
1620 wgetwinpos(wp->w_win, &h, &v);
1621 return makepoint(h, v);
1622}
1623
1624static object *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001625window_getwinsize(wp, args)
1626 windowobject *wp;
1627 object *args;
1628{
1629 int width, height;
1630 if (!getnoarg(args))
1631 return NULL;
1632 wgetwinsize(wp->w_win, &width, &height);
1633 return makepoint(width, height);
1634}
1635
1636static object *
Guido van Rossumbf80e541993-02-08 15:49:17 +00001637window_setwinpos(wp, args)
1638 windowobject *wp;
1639 object *args;
1640{
1641 int a[2];
1642 if (!getpointarg(args, a))
1643 return NULL;
1644 wsetwinpos(wp->w_win, a[0], a[1]);
1645 INCREF(None);
1646 return None;
1647}
1648
1649static object *
1650window_setwinsize(wp, args)
1651 windowobject *wp;
1652 object *args;
1653{
1654 int a[2];
1655 if (!getpointarg(args, a))
1656 return NULL;
1657 wsetwinsize(wp->w_win, a[0], a[1]);
1658 INCREF(None);
1659 return None;
1660}
1661
1662static object *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001663window_getdocsize(wp, args)
1664 windowobject *wp;
1665 object *args;
1666{
1667 int width, height;
1668 if (!getnoarg(args))
1669 return NULL;
1670 wgetdocsize(wp->w_win, &width, &height);
1671 return makepoint(width, height);
1672}
1673
1674static object *
1675window_getorigin(wp, args)
1676 windowobject *wp;
1677 object *args;
1678{
1679 int width, height;
1680 if (!getnoarg(args))
1681 return NULL;
1682 wgetorigin(wp->w_win, &width, &height);
1683 return makepoint(width, height);
1684}
1685
1686static object *
1687window_scroll(wp, args)
1688 windowobject *wp;
1689 object *args;
1690{
1691 int a[6];
1692 if (!getrectpointarg(args, a))
1693 return NULL;
1694 wscroll(wp->w_win, a[0], a[1], a[2], a[3], a[4], a[5]);
1695 INCREF(None);
1696 return None;
1697}
1698
1699static object *
1700window_setdocsize(wp, args)
1701 windowobject *wp;
1702 object *args;
1703{
1704 int a[2];
1705 if (!getpointarg(args, a))
1706 return NULL;
1707 wsetdocsize(wp->w_win, a[0], a[1]);
1708 INCREF(None);
1709 return None;
1710}
1711
1712static object *
1713window_setorigin(wp, args)
1714 windowobject *wp;
1715 object *args;
1716{
1717 int a[2];
1718 if (!getpointarg(args, a))
1719 return NULL;
1720 wsetorigin(wp->w_win, a[0], a[1]);
1721 INCREF(None);
1722 return None;
1723}
1724
1725static object *
1726window_settitle(wp, args)
1727 windowobject *wp;
1728 object *args;
1729{
1730 object *title;
Guido van Rossum234f9421993-06-17 12:35:49 +00001731 if (!getargs(args, "S", &title))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001732 return NULL;
1733 DECREF(wp->w_title);
1734 INCREF(title);
1735 wp->w_title = title;
1736 wsettitle(wp->w_win, getstringvalue(title));
1737 INCREF(None);
1738 return None;
1739}
1740
1741static object *
1742window_show(wp, args)
1743 windowobject *wp;
1744 object *args;
1745{
1746 int a[4];
1747 if (!getrectarg(args, a))
1748 return NULL;
1749 wshow(wp->w_win, a[0], a[1], a[2], a[3]);
1750 INCREF(None);
1751 return None;
1752}
1753
1754static object *
1755window_settimer(wp, args)
1756 windowobject *wp;
1757 object *args;
1758{
1759 int a;
1760 if (!getintarg(args, &a))
1761 return NULL;
1762 wsettimer(wp->w_win, a);
1763 INCREF(None);
1764 return None;
1765}
1766
1767static object *
1768window_menucreate(self, args)
1769 windowobject *self;
1770 object *args;
1771{
1772 menuobject *mp;
Guido van Rossumfc58e581992-01-27 16:45:55 +00001773 char *title;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001774 if (!getstrarg(args, &title))
1775 return NULL;
1776 wmenusetdeflocal(1);
1777 mp = newmenuobject(title);
1778 if (mp == NULL)
1779 return NULL;
1780 wmenuattach(self->w_win, mp->m_menu);
1781 return (object *)mp;
1782}
1783
1784static object *
1785window_textcreate(self, args)
1786 windowobject *self;
1787 object *args;
1788{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001789 int a[4];
1790 if (!getrectarg(args, a))
1791 return NULL;
1792 return (object *)
1793 newtextobject(self, a[0], a[1], a[2], a[3]);
1794}
1795
Guido van Rossum5b10f451990-10-30 16:01:48 +00001796static object *
1797window_setselection(self, args)
1798 windowobject *self;
1799 object *args;
1800{
Guido van Rossumfc58e581992-01-27 16:45:55 +00001801 int sel, size, ok;
1802 char *text;
1803 if (!getargs(args, "(is#)", &sel, &text, &size))
Guido van Rossum5b10f451990-10-30 16:01:48 +00001804 return NULL;
Guido van Rossumfc58e581992-01-27 16:45:55 +00001805 ok = wsetselection(self->w_win, sel, text, size);
Guido van Rossum5b10f451990-10-30 16:01:48 +00001806 return newintobject(ok);
1807}
1808
1809static object *
1810window_setwincursor(self, args)
1811 windowobject *self;
1812 object *args;
1813{
Guido van Rossumfc58e581992-01-27 16:45:55 +00001814 char *name;
Guido van Rossum5b10f451990-10-30 16:01:48 +00001815 CURSOR *c;
Guido van Rossum3c8ba7a1992-02-05 11:15:00 +00001816 if (!getargs(args, "z", &name))
Guido van Rossum5b10f451990-10-30 16:01:48 +00001817 return NULL;
Guido van Rossum3c8ba7a1992-02-05 11:15:00 +00001818 if (name == NULL)
1819 c = NULL;
1820 else {
1821 c = wfetchcursor(name);
1822 if (c == NULL) {
1823 err_setstr(StdwinError, "no such cursor");
1824 return NULL;
1825 }
Guido van Rossum5b10f451990-10-30 16:01:48 +00001826 }
1827 wsetwincursor(self->w_win, c);
1828 INCREF(None);
1829 return None;
1830}
1831
Guido van Rossumfc58e581992-01-27 16:45:55 +00001832static object *
1833window_setactive(self, args)
1834 windowobject *self;
1835 object *args;
1836{
1837 if (!getnoarg(args))
1838 return NULL;
1839 wsetactive(self->w_win);
1840 INCREF(None);
1841 return None;
1842}
1843
Guido van Rossum8dcbbac1991-07-27 21:42:24 +00001844#ifdef CWI_HACKS
1845static object *
1846window_getxwindowid(self, args)
1847 windowobject *self;
1848 object *args;
1849{
1850 long wid = wgetxwindowid(self->w_win);
1851 return newintobject(wid);
1852}
1853#endif
1854
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001855static struct methodlist window_methods[] = {
Guido van Rossumb6775db1994-08-01 11:34:53 +00001856 {"begindrawing",(method)window_begindrawing},
1857 {"change", (method)window_change},
1858 {"close", (method)window_close},
1859 {"getdocsize", (method)window_getdocsize},
1860 {"getorigin", (method)window_getorigin},
1861 {"gettitle", (method)window_gettitle},
1862 {"getwinpos", (method)window_getwinpos},
1863 {"getwinsize", (method)window_getwinsize},
1864 {"menucreate", (method)window_menucreate},
1865 {"scroll", (method)window_scroll},
1866 {"setactive", (method)window_setactive},
1867 {"setdocsize", (method)window_setdocsize},
1868 {"setorigin", (method)window_setorigin},
1869 {"setselection",(method)window_setselection},
1870 {"settimer", (method)window_settimer},
1871 {"settitle", (method)window_settitle},
1872 {"setwincursor",(method)window_setwincursor},
1873 {"setwinpos", (method)window_setwinpos},
1874 {"setwinsize", (method)window_setwinsize},
1875 {"show", (method)window_show},
1876 {"textcreate", (method)window_textcreate},
Guido van Rossum8dcbbac1991-07-27 21:42:24 +00001877#ifdef CWI_HACKS
Guido van Rossumb6775db1994-08-01 11:34:53 +00001878 {"getxwindowid",(method)window_getxwindowid},
Guido van Rossum8dcbbac1991-07-27 21:42:24 +00001879#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001880 {NULL, NULL} /* sentinel */
1881};
1882
1883static object *
1884window_getattr(wp, name)
1885 windowobject *wp;
1886 char *name;
1887{
Guido van Rossum85f50761991-10-20 20:22:50 +00001888 object *v = NULL;
Guido van Rossum77b46041992-01-14 18:41:24 +00001889 if (wp->w_win == NULL) {
1890 err_setstr(StdwinError, "window already closed");
1891 return NULL;
1892 }
Guido van Rossum85f50761991-10-20 20:22:50 +00001893 if (strcmp(name, "__dict__") == 0) {
1894 v = wp->w_attr;
1895 if (v == NULL)
1896 v = None;
1897 }
1898 else if (wp->w_attr != NULL) {
1899 v = dictlookup(wp->w_attr, name);
1900 }
1901 if (v != NULL) {
1902 INCREF(v);
1903 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001904 }
1905 return findmethod(window_methods, (object *)wp, name);
1906}
1907
1908static int
1909window_setattr(wp, name, v)
1910 windowobject *wp;
1911 char *name;
1912 object *v;
1913{
1914 if (wp->w_attr == NULL) {
1915 wp->w_attr = newdictobject();
1916 if (wp->w_attr == NULL)
1917 return -1;
1918 }
Guido van Rossum94472a01992-09-04 09:45:18 +00001919 if (v == NULL) {
1920 int rv = dictremove(wp->w_attr, name);
1921 if (rv < 0)
1922 err_setstr(AttributeError,
1923 "delete non-existing menu object attribute");
1924 return rv;
1925 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001926 else
1927 return dictinsert(wp->w_attr, name, v);
1928}
1929
Guido van Rossumb6775db1994-08-01 11:34:53 +00001930static typeobject Windowtype = {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001931 OB_HEAD_INIT(&Typetype)
1932 0, /*ob_size*/
1933 "window", /*tp_name*/
1934 sizeof(windowobject), /*tp_size*/
1935 0, /*tp_itemsize*/
1936 /* methods */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001937 (destructor)window_dealloc, /*tp_dealloc*/
Guido van Rossum7066dd71992-09-17 17:54:56 +00001938 0, /*tp_print*/
Guido van Rossumb6775db1994-08-01 11:34:53 +00001939 (getattrfunc)window_getattr, /*tp_getattr*/
1940 (setattrfunc)window_setattr, /*tp_setattr*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001941 0, /*tp_compare*/
1942 0, /*tp_repr*/
1943};
1944
1945/* Stdwin methods */
1946
1947static object *
Guido van Rossumcacd9571993-10-18 11:44:47 +00001948stdwin_done(sw, args)
1949 object *sw;
1950 object *args;
1951{
1952 if (!getnoarg(args))
1953 return NULL;
1954 wdone();
1955 /* XXX There is no protection against continued use of
1956 XXX stdwin functions or objects after this call is made.
1957 XXX Use at own risk */
1958 INCREF(None);
1959 return None;
1960}
1961
1962static object *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001963stdwin_open(sw, args)
1964 object *sw;
1965 object *args;
1966{
1967 int tag;
1968 object *title;
1969 windowobject *wp;
Guido van Rossum234f9421993-06-17 12:35:49 +00001970 if (!getargs(args, "S", &title))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001971 return NULL;
1972 for (tag = 0; tag < MAXNWIN; tag++) {
1973 if (windowlist[tag] == NULL)
1974 break;
1975 }
Guido van Rossum27201061991-04-16 08:43:03 +00001976 if (tag >= MAXNWIN) {
Guido van Rossum87e7ea71991-12-10 14:00:03 +00001977 err_setstr(StdwinError, "creating too many windows");
Guido van Rossum27201061991-04-16 08:43:03 +00001978 return NULL;
1979 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001980 wp = NEWOBJ(windowobject, &Windowtype);
1981 if (wp == NULL)
1982 return NULL;
1983 INCREF(title);
1984 wp->w_title = title;
1985 wp->w_win = wopen(getstringvalue(title), (void (*)()) NULL);
1986 wp->w_attr = NULL;
1987 if (wp->w_win == NULL) {
1988 DECREF(wp);
1989 return NULL;
1990 }
1991 windowlist[tag] = wp;
1992 wsettag(wp->w_win, tag);
1993 return (object *)wp;
1994}
1995
1996static object *
Guido van Rossum246b9d81991-06-03 10:55:14 +00001997window2object(win)
1998 WINDOW *win;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001999{
Guido van Rossum246b9d81991-06-03 10:55:14 +00002000 object *w;
2001 if (win == NULL)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002002 w = None;
2003 else {
Guido van Rossum246b9d81991-06-03 10:55:14 +00002004 int tag = wgettag(win);
2005 if (tag < 0 || tag >= MAXNWIN || windowlist[tag] == NULL ||
2006 windowlist[tag]->w_win != win)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002007 w = None;
2008 else
2009 w = (object *)windowlist[tag];
2010 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002011 INCREF(w);
Guido van Rossum246b9d81991-06-03 10:55:14 +00002012 return w;
2013}
2014
2015static object *
2016stdwin_get_poll_event(poll, args)
2017 int poll;
2018 object *args;
2019{
2020 EVENT e;
Guido van Rossum2ee12f41992-04-13 15:54:35 +00002021 object *u, *v, *w;
Guido van Rossum246b9d81991-06-03 10:55:14 +00002022 if (!getnoarg(args))
2023 return NULL;
2024 if (Drawing != NULL) {
Guido van Rossum87e7ea71991-12-10 14:00:03 +00002025 err_setstr(StdwinError, "cannot getevent() while drawing");
Guido van Rossum246b9d81991-06-03 10:55:14 +00002026 return NULL;
2027 }
2028 again:
Guido van Rossumff4949e1992-08-05 19:58:53 +00002029 BGN_STDWIN
Guido van Rossum246b9d81991-06-03 10:55:14 +00002030 if (poll) {
2031 if (!wpollevent(&e)) {
Guido van Rossumff4949e1992-08-05 19:58:53 +00002032 RET_STDWIN
Guido van Rossum246b9d81991-06-03 10:55:14 +00002033 INCREF(None);
2034 return None;
2035 }
2036 }
2037 else
2038 wgetevent(&e);
Guido van Rossumff4949e1992-08-05 19:58:53 +00002039 END_STDWIN
Guido van Rossum246b9d81991-06-03 10:55:14 +00002040 if (e.type == WE_COMMAND && e.u.command == WC_CANCEL) {
2041 /* Turn keyboard interrupts into exceptions */
2042 err_set(KeyboardInterrupt);
2043 return NULL;
2044 }
2045 if (e.type == WE_COMMAND && e.u.command == WC_CLOSE) {
2046 /* Turn WC_CLOSE commands into WE_CLOSE events */
2047 e.type = WE_CLOSE;
2048 }
Guido van Rossum2ee12f41992-04-13 15:54:35 +00002049 v = window2object(e.window);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002050 switch (e.type) {
2051 case WE_CHAR:
2052 {
2053 char c[1];
2054 c[0] = e.u.character;
2055 w = newsizedstringobject(c, 1);
2056 }
2057 break;
2058 case WE_COMMAND:
2059 w = newintobject((long)e.u.command);
2060 break;
2061 case WE_DRAW:
2062 w = makerect(e.u.area.left, e.u.area.top,
2063 e.u.area.right, e.u.area.bottom);
2064 break;
2065 case WE_MOUSE_DOWN:
2066 case WE_MOUSE_MOVE:
2067 case WE_MOUSE_UP:
Guido van Rossum2ee12f41992-04-13 15:54:35 +00002068 w = mkvalue("((ii)iii)",
2069 e.u.where.h, e.u.where.v,
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002070 e.u.where.clicks,
2071 e.u.where.button,
2072 e.u.where.mask);
2073 break;
2074 case WE_MENU:
Guido van Rossum2d14e211991-02-19 12:26:49 +00002075 if (e.u.m.id >= IDOFFSET && e.u.m.id < IDOFFSET+MAXNMENU &&
2076 menulist[e.u.m.id - IDOFFSET] != NULL)
Guido van Rossum2ee12f41992-04-13 15:54:35 +00002077 w = mkvalue("(Oi)",
2078 menulist[e.u.m.id - IDOFFSET], e.u.m.item);
Guido van Rossum246b9d81991-06-03 10:55:14 +00002079 else {
2080 /* Ghost menu event.
2081 Can occur only on the Mac if another part
2082 of the aplication has installed a menu;
2083 like the THINK C console library. */
2084 DECREF(v);
2085 goto again;
2086 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002087 break;
Guido van Rossum3ee199e1992-06-30 12:48:26 +00002088 case WE_KEY:
2089 w = mkvalue("(ii)", e.u.key.code, e.u.key.mask);
2090 break;
Guido van Rossum5b10f451990-10-30 16:01:48 +00002091 case WE_LOST_SEL:
2092 w = newintobject((long)e.u.sel);
2093 break;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002094 default:
2095 w = None;
2096 INCREF(w);
2097 break;
2098 }
2099 if (w == NULL) {
2100 DECREF(v);
2101 return NULL;
2102 }
Guido van Rossum2ee12f41992-04-13 15:54:35 +00002103 u = mkvalue("(iOO)", e.type, v, w);
2104 XDECREF(v);
2105 XDECREF(w);
2106 return u;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002107}
2108
2109static object *
Guido van Rossume8e7cf41991-01-16 14:06:18 +00002110stdwin_getevent(sw, args)
2111 object *sw;
2112 object *args;
2113{
2114 return stdwin_get_poll_event(0, args);
2115}
2116
2117static object *
2118stdwin_pollevent(sw, args)
2119 object *sw;
2120 object *args;
2121{
2122 return stdwin_get_poll_event(1, args);
2123}
2124
2125static object *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002126stdwin_setdefwinpos(sw, args)
2127 object *sw;
2128 object *args;
2129{
2130 int a[2];
2131 if (!getpointarg(args, a))
2132 return NULL;
2133 wsetdefwinpos(a[0], a[1]);
2134 INCREF(None);
2135 return None;
2136}
2137
2138static object *
2139stdwin_setdefwinsize(sw, args)
2140 object *sw;
2141 object *args;
2142{
2143 int a[2];
2144 if (!getpointarg(args, a))
2145 return NULL;
2146 wsetdefwinsize(a[0], a[1]);
2147 INCREF(None);
2148 return None;
2149}
2150
2151static object *
Guido van Rossum0c2290b1991-04-03 19:12:14 +00002152stdwin_setdefscrollbars(sw, args)
2153 object *sw;
2154 object *args;
2155{
2156 int a[2];
2157 if (!getpointarg(args, a))
2158 return NULL;
2159 wsetdefscrollbars(a[0], a[1]);
2160 INCREF(None);
2161 return None;
2162}
2163
2164static object *
Guido van Rossum541c8c01991-05-05 20:13:41 +00002165stdwin_getdefwinpos(self, args)
2166 object *self;
Guido van Rossum33f17701991-02-13 23:19:39 +00002167 object *args;
2168{
2169 int h, v;
2170 if (!getnoarg(args))
2171 return NULL;
2172 wgetdefwinpos(&h, &v);
2173 return makepoint(h, v);
2174}
2175
2176static object *
Guido van Rossum541c8c01991-05-05 20:13:41 +00002177stdwin_getdefwinsize(self, args)
2178 object *self;
Guido van Rossum33f17701991-02-13 23:19:39 +00002179 object *args;
2180{
2181 int width, height;
2182 if (!getnoarg(args))
2183 return NULL;
2184 wgetdefwinsize(&width, &height);
2185 return makepoint(width, height);
2186}
2187
2188static object *
Guido van Rossum541c8c01991-05-05 20:13:41 +00002189stdwin_getdefscrollbars(self, args)
2190 object *self;
Guido van Rossum0c2290b1991-04-03 19:12:14 +00002191 object *args;
2192{
2193 int h, v;
2194 if (!getnoarg(args))
2195 return NULL;
2196 wgetdefscrollbars(&h, &v);
2197 return makepoint(h, v);
2198}
2199
2200static object *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002201stdwin_menucreate(self, args)
2202 object *self;
2203 object *args;
2204{
Guido van Rossumfc58e581992-01-27 16:45:55 +00002205 char *title;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002206 if (!getstrarg(args, &title))
2207 return NULL;
2208 wmenusetdeflocal(0);
2209 return (object *)newmenuobject(title);
2210}
2211
2212static object *
2213stdwin_askfile(self, args)
2214 object *self;
2215 object *args;
2216{
Guido van Rossumfc58e581992-01-27 16:45:55 +00002217 char *prompt, *dflt;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002218 int new, ret;
2219 char buf[256];
Guido van Rossum234f9421993-06-17 12:35:49 +00002220 if (!getargs(args, "(ssi)", &prompt, &dflt, &new))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002221 return NULL;
Guido van Rossumfc58e581992-01-27 16:45:55 +00002222 strncpy(buf, dflt, sizeof buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002223 buf[sizeof buf - 1] = '\0';
Guido van Rossumff4949e1992-08-05 19:58:53 +00002224 BGN_STDWIN
Guido van Rossumfc58e581992-01-27 16:45:55 +00002225 ret = waskfile(prompt, buf, sizeof buf, new);
Guido van Rossumff4949e1992-08-05 19:58:53 +00002226 END_STDWIN
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002227 if (!ret) {
2228 err_set(KeyboardInterrupt);
2229 return NULL;
2230 }
2231 return newstringobject(buf);
2232}
2233
2234static object *
2235stdwin_askync(self, args)
2236 object *self;
2237 object *args;
2238{
Guido van Rossumfc58e581992-01-27 16:45:55 +00002239 char *prompt;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002240 int new, ret;
Guido van Rossum234f9421993-06-17 12:35:49 +00002241 if (!getargs(args, "(si)", &prompt, &new))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002242 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002243 BGN_STDWIN
Guido van Rossumfc58e581992-01-27 16:45:55 +00002244 ret = waskync(prompt, new);
Guido van Rossumff4949e1992-08-05 19:58:53 +00002245 END_STDWIN
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002246 if (ret < 0) {
2247 err_set(KeyboardInterrupt);
2248 return NULL;
2249 }
2250 return newintobject((long)ret);
2251}
2252
2253static object *
2254stdwin_askstr(self, args)
2255 object *self;
2256 object *args;
2257{
Guido van Rossumfc58e581992-01-27 16:45:55 +00002258 char *prompt, *dflt;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002259 int ret;
2260 char buf[256];
Guido van Rossum234f9421993-06-17 12:35:49 +00002261 if (!getargs(args, "(ss)", &prompt, &dflt))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002262 return NULL;
Guido van Rossumfc58e581992-01-27 16:45:55 +00002263 strncpy(buf, dflt, sizeof buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002264 buf[sizeof buf - 1] = '\0';
Guido van Rossumff4949e1992-08-05 19:58:53 +00002265 BGN_STDWIN
Guido van Rossumfc58e581992-01-27 16:45:55 +00002266 ret = waskstr(prompt, buf, sizeof buf);
Guido van Rossumff4949e1992-08-05 19:58:53 +00002267 END_STDWIN
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002268 if (!ret) {
2269 err_set(KeyboardInterrupt);
2270 return NULL;
2271 }
2272 return newstringobject(buf);
2273}
2274
2275static object *
2276stdwin_message(self, args)
2277 object *self;
2278 object *args;
2279{
Guido van Rossumfc58e581992-01-27 16:45:55 +00002280 char *msg;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002281 if (!getstrarg(args, &msg))
2282 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002283 BGN_STDWIN
Guido van Rossumfc58e581992-01-27 16:45:55 +00002284 wmessage(msg);
Guido van Rossumff4949e1992-08-05 19:58:53 +00002285 END_STDWIN
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002286 INCREF(None);
2287 return None;
2288}
2289
2290static object *
2291stdwin_fleep(self, args)
2292 object *self;
2293 object *args;
2294{
2295 if (!getnoarg(args))
2296 return NULL;
2297 wfleep();
2298 INCREF(None);
2299 return None;
2300}
2301
2302static object *
2303stdwin_setcutbuffer(self, args)
2304 object *self;
2305 object *args;
2306{
Guido van Rossumfc58e581992-01-27 16:45:55 +00002307 int i, size;
2308 char *str;
2309 if (!getargs(args, "(is#)", &i, &str, &size))
Guido van Rossum124967c1990-11-06 15:17:35 +00002310 return NULL;
Guido van Rossumfc58e581992-01-27 16:45:55 +00002311 wsetcutbuffer(i, str, size);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002312 INCREF(None);
2313 return None;
2314}
2315
2316static object *
Guido van Rossum246b9d81991-06-03 10:55:14 +00002317stdwin_getactive(self, args)
2318 object *self;
2319 object *args;
2320{
2321 return window2object(wgetactive());
2322}
2323
2324static object *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002325stdwin_getcutbuffer(self, args)
2326 object *self;
2327 object *args;
2328{
Guido van Rossum5b10f451990-10-30 16:01:48 +00002329 int i;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002330 char *str;
Guido van Rossum01769f01990-10-30 13:39:00 +00002331 int len;
Guido van Rossum124967c1990-11-06 15:17:35 +00002332 if (!getintarg(args, &i))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002333 return NULL;
Guido van Rossum5b10f451990-10-30 16:01:48 +00002334 str = wgetcutbuffer(i, &len);
Guido van Rossum01769f01990-10-30 13:39:00 +00002335 if (str == NULL) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002336 str = "";
Guido van Rossum01769f01990-10-30 13:39:00 +00002337 len = 0;
2338 }
2339 return newsizedstringobject(str, len);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002340}
2341
Guido van Rossum5b10f451990-10-30 16:01:48 +00002342static object *
2343stdwin_rotatecutbuffers(self, args)
2344 object *self;
2345 object *args;
2346{
2347 int i;
2348 if (!getintarg(args, &i))
2349 return NULL;
2350 wrotatecutbuffers(i);
2351 INCREF(None);
2352 return None;
2353}
2354
2355static object *
2356stdwin_getselection(self, args)
2357 object *self;
2358 object *args;
2359{
2360 int sel;
2361 char *data;
2362 int len;
2363 if (!getintarg(args, &sel))
2364 return NULL;
2365 data = wgetselection(sel, &len);
2366 if (data == NULL) {
2367 data = "";
2368 len = 0;
2369 }
2370 return newsizedstringobject(data, len);
2371}
2372
2373static object *
2374stdwin_resetselection(self, args)
2375 object *self;
2376 object *args;
2377{
2378 int sel;
2379 if (!getintarg(args, &sel))
2380 return NULL;
2381 wresetselection(sel);
2382 INCREF(None);
2383 return None;
2384}
2385
Guido van Rossum0c2290b1991-04-03 19:12:14 +00002386static object *
2387stdwin_fetchcolor(self, args)
2388 object *self;
2389 object *args;
2390{
Guido van Rossumfc58e581992-01-27 16:45:55 +00002391 char *colorname;
Guido van Rossum34679b71993-01-26 13:33:44 +00002392 COLOR color;
Guido van Rossum0c2290b1991-04-03 19:12:14 +00002393 if (!getstrarg(args, &colorname))
2394 return NULL;
Guido van Rossum34679b71993-01-26 13:33:44 +00002395 color = wfetchcolor(colorname);
2396#ifdef BADCOLOR
2397 if (color == BADCOLOR) {
2398 err_setstr(StdwinError, "color name not found");
2399 return NULL;
2400 }
2401#endif
2402 return newintobject((long)color);
Guido van Rossum0c2290b1991-04-03 19:12:14 +00002403}
2404
Guido van Rossum541c8c01991-05-05 20:13:41 +00002405static object *
2406stdwin_getscrsize(self, args)
2407 object *self;
2408 object *args;
2409{
2410 int width, height;
2411 if (!getnoarg(args))
2412 return NULL;
2413 wgetscrsize(&width, &height);
2414 return makepoint(width, height);
2415}
2416
2417static object *
2418stdwin_getscrmm(self, args)
2419 object *self;
2420 object *args;
2421{
2422 int width, height;
2423 if (!getnoarg(args))
2424 return NULL;
2425 wgetscrmm(&width, &height);
2426 return makepoint(width, height);
2427}
2428
Guido van Rossumed233a51992-06-23 09:07:03 +00002429#ifdef unix
2430static object *
2431stdwin_connectionnumber(self, args)
2432 object *self;
2433 object *args;
2434{
2435 if (!getnoarg(args))
2436 return NULL;
2437 return newintobject((long) wconnectionnumber());
2438}
2439#endif
2440
Guido van Rossumbf80e541993-02-08 15:49:17 +00002441static object *
2442stdwin_listfontnames(self, args)
2443 object *self;
2444 object *args;
2445{
2446 char *pattern;
2447 char **fontnames;
2448 int count;
2449 object *list;
2450 if (!getargs(args, "z", &pattern))
2451 return NULL;
2452 fontnames = wlistfontnames(pattern, &count);
2453 list = newlistobject(count);
2454 if (list != NULL) {
2455 int i;
2456 for (i = 0; i < count; i++) {
2457 object *v = newstringobject(fontnames[i]);
2458 if (v == NULL) {
2459 DECREF(list);
2460 list = NULL;
2461 break;
2462 }
2463 setlistitem(list, i, v);
2464 }
2465 }
2466 return list;
2467}
2468
Guido van Rossume9066061993-07-29 13:14:32 +00002469#ifdef HAVE_BITMAPS
Guido van Rossumbf80e541993-02-08 15:49:17 +00002470static object *
2471stdwin_newbitmap(self, args)
2472 object *self;
2473 object *args;
2474{
2475 int width, height;
2476 bitmapobject *bp;
2477 if (!getargs(args, "(ii)", &width, &height))
2478 return NULL;
2479 return (object *)newbitmapobject(width, height);
2480}
Guido van Rossume9066061993-07-29 13:14:32 +00002481#endif
Guido van Rossumbf80e541993-02-08 15:49:17 +00002482
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002483static struct methodlist stdwin_methods[] = {
2484 {"askfile", stdwin_askfile},
2485 {"askstr", stdwin_askstr},
2486 {"askync", stdwin_askync},
Guido van Rossumcacd9571993-10-18 11:44:47 +00002487 {"done", stdwin_done},
Guido van Rossum27201061991-04-16 08:43:03 +00002488 {"fetchcolor", stdwin_fetchcolor},
Guido van Rossumed233a51992-06-23 09:07:03 +00002489#ifdef unix
2490 {"fileno", stdwin_connectionnumber},
2491 {"connectionnumber", stdwin_connectionnumber},
2492#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002493 {"fleep", stdwin_fleep},
Guido van Rossum246b9d81991-06-03 10:55:14 +00002494 {"getactive", stdwin_getactive},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002495 {"getcutbuffer", stdwin_getcutbuffer},
Guido van Rossum0c2290b1991-04-03 19:12:14 +00002496 {"getdefscrollbars", stdwin_getdefscrollbars},
Guido van Rossum33f17701991-02-13 23:19:39 +00002497 {"getdefwinpos", stdwin_getdefwinpos},
2498 {"getdefwinsize", stdwin_getdefwinsize},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002499 {"getevent", stdwin_getevent},
Guido van Rossum541c8c01991-05-05 20:13:41 +00002500 {"getscrmm", stdwin_getscrmm},
2501 {"getscrsize", stdwin_getscrsize},
Guido van Rossum27201061991-04-16 08:43:03 +00002502 {"getselection", stdwin_getselection},
Guido van Rossumbf80e541993-02-08 15:49:17 +00002503 {"listfontnames", stdwin_listfontnames},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002504 {"menucreate", stdwin_menucreate},
2505 {"message", stdwin_message},
Guido van Rossume9066061993-07-29 13:14:32 +00002506#ifdef HAVE_BITMAPS
Guido van Rossumbf80e541993-02-08 15:49:17 +00002507 {"newbitmap", stdwin_newbitmap},
Guido van Rossume9066061993-07-29 13:14:32 +00002508#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002509 {"open", stdwin_open},
Guido van Rossume8e7cf41991-01-16 14:06:18 +00002510 {"pollevent", stdwin_pollevent},
Guido van Rossum5b10f451990-10-30 16:01:48 +00002511 {"resetselection", stdwin_resetselection},
2512 {"rotatecutbuffers", stdwin_rotatecutbuffers},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002513 {"setcutbuffer", stdwin_setcutbuffer},
Guido van Rossum0c2290b1991-04-03 19:12:14 +00002514 {"setdefscrollbars", stdwin_setdefscrollbars},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002515 {"setdefwinpos", stdwin_setdefwinpos},
2516 {"setdefwinsize", stdwin_setdefwinsize},
2517
2518 /* Text measuring methods borrow code from drawing objects: */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002519 {"baseline", (method)drawing_baseline},
2520 {"lineheight", (method)drawing_lineheight},
2521 {"textbreak", (method)drawing_textbreak},
2522 {"textwidth", (method)drawing_textwidth},
Guido van Rossum0c2290b1991-04-03 19:12:14 +00002523
2524 /* Same for font setting methods: */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002525 {"setfont", (method)drawing_setfont},
Guido van Rossum0c2290b1991-04-03 19:12:14 +00002526
2527 /* Same for color setting/getting methods: */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002528 {"getbgcolor", (method)drawing_getbgcolor},
2529 {"getfgcolor", (method)drawing_getfgcolor},
2530 {"setbgcolor", (method)drawing_setbgcolor},
2531 {"setfgcolor", (method)drawing_setfgcolor},
Guido van Rossum0c2290b1991-04-03 19:12:14 +00002532
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002533 {NULL, NULL} /* sentinel */
2534};
2535
Guido van Rossumb6775db1994-08-01 11:34:53 +00002536#ifndef macintosh
Guido van Rossumcacd9571993-10-18 11:44:47 +00002537static int
2538checkstringlist(args, ps, pn)
2539 object *args;
2540 char ***ps;
2541 int *pn;
2542{
2543 int i, n;
2544 char **s;
2545 if (!is_listobject(args)) {
2546 err_setstr(TypeError, "list of strings expected");
2547 return 0;
2548 }
2549 n = getlistsize(args);
2550 s = NEW(char *, n+1);
2551 if (s == NULL) {
2552 err_nomem();
2553 return 0;
2554 }
2555 for (i = 0; i < n; i++) {
2556 object *item = getlistitem(args, i);
2557 if (!is_stringobject(item)) {
2558 err_setstr(TypeError, "list of strings expected");
2559 return 0;
2560 }
2561 s[i] = getstringvalue(item);
2562 }
2563 s[n] = NULL; /* In case caller wants a NULL-terminated list */
2564 *ps = s;
2565 *pn = n;
2566 return 1;
2567}
2568
2569static int
2570putbackstringlist(list, s, n)
2571 object *list;
2572 char **s;
2573 int n;
2574{
2575 int oldsize = getlistsize(list);
2576 object *newlist;
2577 int i;
2578 if (n == oldsize)
2579 return 1;
2580 newlist = newlistobject(n);
2581 for (i = 0; i < n && newlist != NULL; i++) {
2582 object *item = newstringobject(s[i]);
2583 if (item == NULL) {
2584 DECREF(newlist);
2585 newlist = NULL;
2586 }
2587 else
2588 setlistitem(newlist, i, item);
2589 }
2590 if (newlist == NULL)
2591 return 0;
2592 (*list->ob_type->tp_as_sequence->sq_ass_slice)
2593 (list, 0, oldsize, newlist);
2594 DECREF(newlist);
2595 return 1;
2596}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002597#endif /* macintosh */
Guido van Rossumcacd9571993-10-18 11:44:47 +00002598
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002599void
2600initstdwin()
2601{
Guido van Rossumbbf94341991-12-16 15:44:53 +00002602 object *m, *d;
2603 static int inited = 0;
2604
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 Rossum39cb5ce1995-01-26 00:37:10 +00002609 char buf[1000];
Guido van Rossumcacd9571993-10-18 11:44:47 +00002610 int argc = 0;
2611 char **argv = NULL;
2612 object *sys_argv = sysget("argv");
2613 if (sys_argv != NULL) {
2614 if (!checkstringlist(sys_argv, &argv, &argc))
2615 err_clear();
2616 }
Guido van Rossumc45611d1993-11-17 22:58:56 +00002617 if (argc > 0) {
2618 /* If argv[0] has a ".py" suffix, remove the suffix */
2619 char *p = strrchr(argv[0], '.');
2620 if (p != NULL && strcmp(p, ".py") == 0) {
2621 int n = p - argv[0];
2622 if (n >= sizeof(buf))
2623 n = sizeof(buf)-1;
2624 strncpy(buf, argv[0], n);
2625 buf[n] = '\0';
2626 argv[0] = buf;
2627 }
2628 }
Guido van Rossumcacd9571993-10-18 11:44:47 +00002629 winitargs(&argc, &argv);
2630 if (argv != NULL) {
2631 if (!putbackstringlist(sys_argv, argv, argc))
2632 err_clear();
2633 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002634#endif
Guido van Rossum2d14e211991-02-19 12:26:49 +00002635 inited = 1;
Jack Jansen3d7f6bd1995-01-26 16:40:10 +00002636 StdwinIsActive = 1;
Guido van Rossum2d14e211991-02-19 12:26:49 +00002637 }
Guido van Rossumbbf94341991-12-16 15:44:53 +00002638 m = initmodule("stdwin", stdwin_methods);
2639 d = getmoduledict(m);
2640
2641 /* Initialize stdwin.error exception */
2642 StdwinError = newstringobject("stdwin.error");
2643 if (StdwinError == NULL || dictinsert(d, "error", StdwinError) != 0)
2644 fatal("can't define stdwin.error");
Guido van Rossumb6775db1994-08-01 11:34:53 +00002645#ifdef WITH_THREAD
Guido van Rossumff4949e1992-08-05 19:58:53 +00002646 StdwinLock = allocate_lock();
2647 if (StdwinLock == NULL)
2648 fatal("can't allocate stdwin lock");
2649#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002650}