Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame^] | 1 | /*********************************************************** |
| 2 | Copyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The |
| 3 | Netherlands. |
| 4 | |
| 5 | All Rights Reserved |
| 6 | |
| 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
| 9 | provided that the above copyright notice appear in all copies and that |
| 10 | both that copyright notice and this permission notice appear in |
| 11 | supporting documentation, and that the names of Stichting Mathematisch |
| 12 | Centrum or CWI not be used in advertising or publicity pertaining to |
| 13 | distribution of the software without specific, written prior permission. |
| 14 | |
| 15 | STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO |
| 16 | THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 17 | FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE |
| 18 | FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 19 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 20 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 21 | OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 22 | |
| 23 | ******************************************************************/ |
| 24 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 25 | /* Stdwin module */ |
| 26 | |
| 27 | /* Stdwin itself is a module, not a separate object type. |
| 28 | Object types defined here: |
| 29 | wp: a window |
| 30 | dp: a drawing structure (only one can exist at a time) |
| 31 | mp: a menu |
| 32 | tp: a textedit block |
| 33 | */ |
| 34 | |
| 35 | /* Rules for translating C stdwin function calls into Python stwin: |
| 36 | - All names drop their initial letter 'w' |
| 37 | - Functions with a window as first parameter are methods of window objects |
| 38 | - There is no equivalent for wclose(); just delete the window object |
| 39 | (all references to it!) (XXX maybe this is a bad idea) |
| 40 | - w.begindrawing() returns a drawing object |
| 41 | - There is no equivalent for wenddrawing(win); just delete the drawing |
| 42 | object (all references to it!) (XXX maybe this is a bad idea) |
| 43 | - Functions that may only be used inside wbegindrawing / wendddrawing |
| 44 | are methods of the drawing object; this includes the text measurement |
| 45 | functions (which however have doubles as module functions). |
| 46 | - Methods of the drawing object drop an initial 'draw' from their name |
| 47 | if they have it, e.g., wdrawline() --> d.line() |
| 48 | - The obvious type conversions: int --> intobject; string --> stringobject |
| 49 | - A text parameter followed by a length parameter is only a text (string) |
| 50 | parameter in Python |
| 51 | - A point or other pair of horizontal and vertical coordinates is always |
| 52 | a pair of integers in Python |
| 53 | - Two points forming a rectangle or endpoints of a line segment are a |
| 54 | pair of points in Python |
| 55 | - The arguments to d.elarc() are three points. |
| 56 | - The functions wgetclip() and wsetclip() are translated into |
| 57 | stdwin.getcutbuffer() and stdwin.setcutbuffer(); 'clip' is really |
| 58 | a bad word for what these functions do (clipping has a different |
| 59 | meaning in the drawing world), while cutbuffer is standard X jargon. |
Guido van Rossum | 01769f0 | 1990-10-30 13:39:00 +0000 | [diff] [blame] | 60 | XXX This must change again in the light of changes to stdwin! |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 61 | - For textedit, similar rules hold, but they are less strict. |
| 62 | XXX more? |
| 63 | */ |
| 64 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 65 | #include "allobjects.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 66 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 67 | #include "modsupport.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 68 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 69 | #include "stdwin.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 70 | |
| 71 | /* Window and menu object types declared here because of forward references */ |
| 72 | |
| 73 | typedef struct { |
| 74 | OB_HEAD |
| 75 | object *w_title; |
| 76 | WINDOW *w_win; |
| 77 | object *w_attr; /* Attributes dictionary */ |
| 78 | } windowobject; |
| 79 | |
| 80 | extern typeobject Windowtype; /* Really static, forward */ |
| 81 | |
| 82 | #define is_windowobject(wp) ((wp)->ob_type == &Windowtype) |
| 83 | |
| 84 | typedef struct { |
| 85 | OB_HEAD |
| 86 | MENU *m_menu; |
| 87 | int m_id; |
| 88 | object *m_attr; /* Attributes dictionary */ |
| 89 | } menuobject; |
| 90 | |
| 91 | extern typeobject Menutype; /* Really static, forward */ |
| 92 | |
| 93 | #define is_menuobject(mp) ((mp)->ob_type == &Menutype) |
| 94 | |
| 95 | |
| 96 | /* Strongly stdwin-specific argument handlers */ |
| 97 | |
| 98 | static int |
| 99 | getmousedetail(v, ep) |
| 100 | object *v; |
| 101 | EVENT *ep; |
| 102 | { |
| 103 | if (v == NULL || !is_tupleobject(v) || gettuplesize(v) != 4) |
| 104 | return err_badarg(); |
| 105 | return getintintarg(gettupleitem(v, 0), |
| 106 | &ep->u.where.h, &ep->u.where.v) && |
| 107 | getintarg(gettupleitem(v, 1), &ep->u.where.clicks) && |
| 108 | getintarg(gettupleitem(v, 2), &ep->u.where.button) && |
| 109 | getintarg(gettupleitem(v, 3), &ep->u.where.mask); |
| 110 | } |
| 111 | |
| 112 | static int |
| 113 | getmenudetail(v, ep) |
| 114 | object *v; |
| 115 | EVENT *ep; |
| 116 | { |
| 117 | object *mp; |
| 118 | if (v == NULL || !is_tupleobject(v) || gettuplesize(v) != 2) |
| 119 | return err_badarg(); |
| 120 | mp = gettupleitem(v, 0); |
| 121 | if (mp == NULL || !is_menuobject(mp)) |
| 122 | return err_badarg(); |
| 123 | ep->u.m.id = ((menuobject *)mp) -> m_id; |
| 124 | return getintarg(gettupleitem(v, 1), &ep->u.m.item); |
| 125 | } |
| 126 | |
| 127 | static int |
| 128 | geteventarg(v, ep) |
| 129 | object *v; |
| 130 | EVENT *ep; |
| 131 | { |
| 132 | object *wp, *detail; |
| 133 | int a[4]; |
| 134 | if (v == NULL || !is_tupleobject(v) || gettuplesize(v) != 3) |
| 135 | return err_badarg(); |
| 136 | if (!getintarg(gettupleitem(v, 0), &ep->type)) |
| 137 | return 0; |
| 138 | wp = gettupleitem(v, 1); |
| 139 | if (wp == None) |
| 140 | ep->window = NULL; |
| 141 | else if (wp == NULL || !is_windowobject(wp)) |
| 142 | return err_badarg(); |
| 143 | else |
| 144 | ep->window = ((windowobject *)wp) -> w_win; |
| 145 | detail = gettupleitem(v, 2); |
| 146 | switch (ep->type) { |
| 147 | case WE_CHAR: |
| 148 | if (!is_stringobject(detail) || getstringsize(detail) != 1) |
| 149 | return err_badarg(); |
| 150 | ep->u.character = getstringvalue(detail)[0]; |
| 151 | return 1; |
| 152 | case WE_COMMAND: |
| 153 | return getintarg(detail, &ep->u.command); |
| 154 | case WE_DRAW: |
| 155 | if (!getrectarg(detail, a)) |
| 156 | return 0; |
| 157 | ep->u.area.left = a[0]; |
| 158 | ep->u.area.top = a[1]; |
| 159 | ep->u.area.right = a[2]; |
| 160 | ep->u.area.bottom = a[3]; |
| 161 | return 1; |
| 162 | case WE_MOUSE_DOWN: |
| 163 | case WE_MOUSE_UP: |
| 164 | case WE_MOUSE_MOVE: |
| 165 | return getmousedetail(detail, ep); |
| 166 | case WE_MENU: |
| 167 | return getmenudetail(detail, ep); |
| 168 | default: |
| 169 | return 1; |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | |
| 174 | /* Return construction tools */ |
| 175 | |
| 176 | static object * |
| 177 | makepoint(a, b) |
| 178 | int a, b; |
| 179 | { |
| 180 | object *v; |
| 181 | object *w; |
| 182 | if ((v = newtupleobject(2)) == NULL) |
| 183 | return NULL; |
| 184 | if ((w = newintobject((long)a)) == NULL || |
| 185 | settupleitem(v, 0, w) != 0 || |
| 186 | (w = newintobject((long)b)) == NULL || |
| 187 | settupleitem(v, 1, w) != 0) { |
| 188 | DECREF(v); |
| 189 | return NULL; |
| 190 | } |
| 191 | return v; |
| 192 | } |
| 193 | |
| 194 | static object * |
| 195 | makerect(a, b, c, d) |
| 196 | int a, b, c, d; |
| 197 | { |
| 198 | object *v; |
| 199 | object *w; |
| 200 | if ((v = newtupleobject(2)) == NULL) |
| 201 | return NULL; |
| 202 | if ((w = makepoint(a, b)) == NULL || |
| 203 | settupleitem(v, 0, w) != 0 || |
| 204 | (w = makepoint(c, d)) == NULL || |
| 205 | settupleitem(v, 1, w) != 0) { |
| 206 | DECREF(v); |
| 207 | return NULL; |
| 208 | } |
| 209 | return v; |
| 210 | } |
| 211 | |
| 212 | static object * |
| 213 | makemouse(hor, ver, clicks, button, mask) |
| 214 | int hor, ver, clicks, button, mask; |
| 215 | { |
| 216 | object *v; |
| 217 | object *w; |
| 218 | if ((v = newtupleobject(4)) == NULL) |
| 219 | return NULL; |
| 220 | if ((w = makepoint(hor, ver)) == NULL || |
| 221 | settupleitem(v, 0, w) != 0 || |
| 222 | (w = newintobject((long)clicks)) == NULL || |
| 223 | settupleitem(v, 1, w) != 0 || |
| 224 | (w = newintobject((long)button)) == NULL || |
| 225 | settupleitem(v, 2, w) != 0 || |
| 226 | (w = newintobject((long)mask)) == NULL || |
| 227 | settupleitem(v, 3, w) != 0) { |
| 228 | DECREF(v); |
| 229 | return NULL; |
| 230 | } |
| 231 | return v; |
| 232 | } |
| 233 | |
| 234 | static object * |
| 235 | makemenu(mp, item) |
| 236 | object *mp; |
| 237 | int item; |
| 238 | { |
| 239 | object *v; |
| 240 | object *w; |
| 241 | if ((v = newtupleobject(2)) == NULL) |
| 242 | return NULL; |
| 243 | INCREF(mp); |
| 244 | if (settupleitem(v, 0, mp) != 0 || |
| 245 | (w = newintobject((long)item)) == NULL || |
| 246 | settupleitem(v, 1, w) != 0) { |
| 247 | DECREF(v); |
| 248 | return NULL; |
| 249 | } |
| 250 | return v; |
| 251 | } |
| 252 | |
| 253 | |
| 254 | /* Drawing objects */ |
| 255 | |
| 256 | typedef struct { |
| 257 | OB_HEAD |
| 258 | windowobject *d_ref; |
| 259 | } drawingobject; |
| 260 | |
| 261 | static drawingobject *Drawing; /* Set to current drawing object, or NULL */ |
| 262 | |
| 263 | /* Drawing methods */ |
| 264 | |
| 265 | static void |
| 266 | drawing_dealloc(dp) |
| 267 | drawingobject *dp; |
| 268 | { |
| 269 | wenddrawing(dp->d_ref->w_win); |
| 270 | Drawing = NULL; |
| 271 | DECREF(dp->d_ref); |
| 272 | free((char *)dp); |
| 273 | } |
| 274 | |
| 275 | static object * |
| 276 | drawing_generic(dp, args, func) |
| 277 | drawingobject *dp; |
| 278 | object *args; |
| 279 | void (*func) FPROTO((int, int, int, int)); |
| 280 | { |
| 281 | int a[4]; |
| 282 | if (!getrectarg(args, a)) |
| 283 | return NULL; |
| 284 | (*func)(a[0], a[1], a[2], a[3]); |
| 285 | INCREF(None); |
| 286 | return None; |
| 287 | } |
| 288 | |
| 289 | static object * |
| 290 | drawing_line(dp, args) |
| 291 | drawingobject *dp; |
| 292 | object *args; |
| 293 | { |
| 294 | drawing_generic(dp, args, wdrawline); |
| 295 | } |
| 296 | |
| 297 | static object * |
| 298 | drawing_xorline(dp, args) |
| 299 | drawingobject *dp; |
| 300 | object *args; |
| 301 | { |
| 302 | drawing_generic(dp, args, wxorline); |
| 303 | } |
| 304 | |
| 305 | static object * |
| 306 | drawing_circle(dp, args) |
| 307 | drawingobject *dp; |
| 308 | object *args; |
| 309 | { |
| 310 | int a[3]; |
| 311 | if (!getpointintarg(args, a)) |
| 312 | return NULL; |
| 313 | wdrawcircle(a[0], a[1], a[2]); |
| 314 | INCREF(None); |
| 315 | return None; |
| 316 | } |
| 317 | |
| 318 | static object * |
| 319 | drawing_elarc(dp, args) |
| 320 | drawingobject *dp; |
| 321 | object *args; |
| 322 | { |
| 323 | int a[6]; |
| 324 | if (!get3pointarg(args, a)) |
| 325 | return NULL; |
| 326 | wdrawelarc(a[0], a[1], a[2], a[3], a[4], a[5]); |
| 327 | INCREF(None); |
| 328 | return None; |
| 329 | } |
| 330 | |
| 331 | static object * |
| 332 | drawing_box(dp, args) |
| 333 | drawingobject *dp; |
| 334 | object *args; |
| 335 | { |
| 336 | drawing_generic(dp, args, wdrawbox); |
| 337 | } |
| 338 | |
| 339 | static object * |
| 340 | drawing_erase(dp, args) |
| 341 | drawingobject *dp; |
| 342 | object *args; |
| 343 | { |
| 344 | drawing_generic(dp, args, werase); |
| 345 | } |
| 346 | |
| 347 | static object * |
| 348 | drawing_paint(dp, args) |
| 349 | drawingobject *dp; |
| 350 | object *args; |
| 351 | { |
| 352 | drawing_generic(dp, args, wpaint); |
| 353 | } |
| 354 | |
| 355 | static object * |
| 356 | drawing_invert(dp, args) |
| 357 | drawingobject *dp; |
| 358 | object *args; |
| 359 | { |
| 360 | drawing_generic(dp, args, winvert); |
| 361 | } |
| 362 | |
| 363 | static object * |
| 364 | drawing_cliprect(dp, args) |
| 365 | drawingobject *dp; |
| 366 | object *args; |
| 367 | { |
| 368 | drawing_generic(dp, args, wcliprect); |
| 369 | } |
| 370 | |
| 371 | static object * |
| 372 | drawing_noclip(dp, args) |
| 373 | drawingobject *dp; |
| 374 | object *args; |
| 375 | { |
| 376 | if (!getnoarg(args)) |
| 377 | return NULL; |
| 378 | wnoclip(); |
| 379 | INCREF(None); |
| 380 | return None; |
| 381 | } |
| 382 | |
| 383 | static object * |
| 384 | drawing_shade(dp, args) |
| 385 | drawingobject *dp; |
| 386 | object *args; |
| 387 | { |
| 388 | int a[5]; |
| 389 | if (!getrectintarg(args, a)) |
| 390 | return NULL; |
| 391 | wshade(a[0], a[1], a[2], a[3], a[4]); |
| 392 | INCREF(None); |
| 393 | return None; |
| 394 | } |
| 395 | |
| 396 | static object * |
| 397 | drawing_text(dp, args) |
| 398 | drawingobject *dp; |
| 399 | object *args; |
| 400 | { |
| 401 | int a[2]; |
| 402 | object *s; |
| 403 | if (!getpointstrarg(args, a, &s)) |
| 404 | return NULL; |
| 405 | wdrawtext(a[0], a[1], getstringvalue(s), (int)getstringsize(s)); |
| 406 | INCREF(None); |
| 407 | return None; |
| 408 | } |
| 409 | |
| 410 | /* The following four are also used as stdwin functions */ |
| 411 | |
| 412 | static object * |
| 413 | drawing_lineheight(dp, args) |
| 414 | drawingobject *dp; |
| 415 | object *args; |
| 416 | { |
| 417 | if (!getnoarg(args)) |
| 418 | return NULL; |
| 419 | return newintobject((long)wlineheight()); |
| 420 | } |
| 421 | |
| 422 | static object * |
| 423 | drawing_baseline(dp, args) |
| 424 | drawingobject *dp; |
| 425 | object *args; |
| 426 | { |
| 427 | if (!getnoarg(args)) |
| 428 | return NULL; |
| 429 | return newintobject((long)wbaseline()); |
| 430 | } |
| 431 | |
| 432 | static object * |
| 433 | drawing_textwidth(dp, args) |
| 434 | drawingobject *dp; |
| 435 | object *args; |
| 436 | { |
| 437 | object *s; |
| 438 | if (!getstrarg(args, &s)) |
| 439 | return NULL; |
| 440 | return newintobject( |
| 441 | (long)wtextwidth(getstringvalue(s), (int)getstringsize(s))); |
| 442 | } |
| 443 | |
| 444 | static object * |
| 445 | drawing_textbreak(dp, args) |
| 446 | drawingobject *dp; |
| 447 | object *args; |
| 448 | { |
| 449 | object *s; |
| 450 | int a; |
| 451 | if (!getstrintarg(args, &s, &a)) |
| 452 | return NULL; |
| 453 | return newintobject( |
| 454 | (long)wtextbreak(getstringvalue(s), (int)getstringsize(s), a)); |
| 455 | } |
| 456 | |
| 457 | static struct methodlist drawing_methods[] = { |
| 458 | {"box", drawing_box}, |
| 459 | {"circle", drawing_circle}, |
| 460 | {"cliprect", drawing_cliprect}, |
| 461 | {"elarc", drawing_elarc}, |
| 462 | {"erase", drawing_erase}, |
| 463 | {"invert", drawing_invert}, |
| 464 | {"line", drawing_line}, |
| 465 | {"noclip", drawing_noclip}, |
| 466 | {"paint", drawing_paint}, |
| 467 | {"shade", drawing_shade}, |
| 468 | {"text", drawing_text}, |
| 469 | {"xorline", drawing_xorline}, |
| 470 | |
| 471 | /* Text measuring methods: */ |
| 472 | {"baseline", drawing_baseline}, |
| 473 | {"lineheight", drawing_lineheight}, |
| 474 | {"textbreak", drawing_textbreak}, |
| 475 | {"textwidth", drawing_textwidth}, |
| 476 | {NULL, NULL} /* sentinel */ |
| 477 | }; |
| 478 | |
| 479 | static object * |
| 480 | drawing_getattr(wp, name) |
| 481 | drawingobject *wp; |
| 482 | char *name; |
| 483 | { |
| 484 | return findmethod(drawing_methods, (object *)wp, name); |
| 485 | } |
| 486 | |
| 487 | static typeobject Drawingtype = { |
| 488 | OB_HEAD_INIT(&Typetype) |
| 489 | 0, /*ob_size*/ |
| 490 | "drawing", /*tp_name*/ |
| 491 | sizeof(drawingobject), /*tp_size*/ |
| 492 | 0, /*tp_itemsize*/ |
| 493 | /* methods */ |
| 494 | drawing_dealloc, /*tp_dealloc*/ |
| 495 | 0, /*tp_print*/ |
| 496 | drawing_getattr, /*tp_getattr*/ |
| 497 | 0, /*tp_setattr*/ |
| 498 | 0, /*tp_compare*/ |
| 499 | 0, /*tp_repr*/ |
| 500 | }; |
| 501 | |
| 502 | |
| 503 | /* Text(edit) objects */ |
| 504 | |
| 505 | typedef struct { |
| 506 | OB_HEAD |
| 507 | TEXTEDIT *t_text; |
| 508 | windowobject *t_ref; |
| 509 | object *t_attr; /* Attributes dictionary */ |
| 510 | } textobject; |
| 511 | |
| 512 | extern typeobject Texttype; /* Really static, forward */ |
| 513 | |
| 514 | static textobject * |
| 515 | newtextobject(wp, left, top, right, bottom) |
| 516 | windowobject *wp; |
| 517 | int left, top, right, bottom; |
| 518 | { |
| 519 | textobject *tp; |
| 520 | tp = NEWOBJ(textobject, &Texttype); |
| 521 | if (tp == NULL) |
| 522 | return NULL; |
| 523 | tp->t_attr = NULL; |
| 524 | INCREF(wp); |
| 525 | tp->t_ref = wp; |
| 526 | tp->t_text = tecreate(wp->w_win, left, top, right, bottom); |
| 527 | if (tp->t_text == NULL) { |
| 528 | DECREF(tp); |
| 529 | return (textobject *) err_nomem(); |
| 530 | } |
| 531 | return tp; |
| 532 | } |
| 533 | |
| 534 | /* Text(edit) methods */ |
| 535 | |
| 536 | static void |
| 537 | text_dealloc(tp) |
| 538 | textobject *tp; |
| 539 | { |
| 540 | if (tp->t_text != NULL) |
| 541 | tefree(tp->t_text); |
| 542 | if (tp->t_attr != NULL) |
| 543 | DECREF(tp->t_attr); |
| 544 | DECREF(tp->t_ref); |
| 545 | DEL(tp); |
| 546 | } |
| 547 | |
| 548 | static object * |
| 549 | text_arrow(self, args) |
| 550 | textobject *self; |
| 551 | object *args; |
| 552 | { |
| 553 | int code; |
| 554 | if (!getintarg(args, &code)) |
| 555 | return NULL; |
| 556 | tearrow(self->t_text, code); |
| 557 | INCREF(None); |
| 558 | return None; |
| 559 | } |
| 560 | |
| 561 | static object * |
| 562 | text_draw(self, args) |
| 563 | textobject *self; |
| 564 | object *args; |
| 565 | { |
| 566 | register TEXTEDIT *tp = self->t_text; |
| 567 | int a[4]; |
| 568 | int left, top, right, bottom; |
| 569 | if (!getrectarg(args, a)) |
| 570 | return NULL; |
| 571 | if (Drawing != NULL) { |
| 572 | err_setstr(RuntimeError, "not drawing"); |
| 573 | return NULL; |
| 574 | } |
| 575 | /* Clip to text area and ignore if area is empty */ |
| 576 | left = tegetleft(tp); |
| 577 | top = tegettop(tp); |
| 578 | right = tegetright(tp); |
| 579 | bottom = tegetbottom(tp); |
| 580 | if (a[0] < left) a[0] = left; |
| 581 | if (a[1] < top) a[1] = top; |
| 582 | if (a[2] > right) a[2] = right; |
| 583 | if (a[3] > bottom) a[3] = bottom; |
| 584 | if (a[0] < a[2] && a[1] < a[3]) { |
| 585 | /* Hide/show focus around draw call; these are undocumented, |
| 586 | but required here to get the highlighting correct. |
| 587 | The call to werase is also required for this reason. |
| 588 | Finally, this forces us to require (above) that we are NOT |
| 589 | already drawing. */ |
| 590 | tehidefocus(tp); |
| 591 | wbegindrawing(self->t_ref->w_win); |
| 592 | werase(a[0], a[1], a[2], a[3]); |
| 593 | tedrawnew(tp, a[0], a[1], a[2], a[3]); |
| 594 | wenddrawing(self->t_ref->w_win); |
| 595 | teshowfocus(tp); |
| 596 | } |
| 597 | INCREF(None); |
| 598 | return None; |
| 599 | } |
| 600 | |
| 601 | static object * |
| 602 | text_event(self, args) |
| 603 | textobject *self; |
| 604 | object *args; |
| 605 | { |
| 606 | register TEXTEDIT *tp = self->t_text; |
| 607 | EVENT e; |
| 608 | if (!geteventarg(args, &e)) |
| 609 | return NULL; |
| 610 | if (e.type == WE_MOUSE_DOWN) { |
Guido van Rossum | 33f1770 | 1991-02-13 23:19:39 +0000 | [diff] [blame] | 611 | /* Cheat at the margins */ |
| 612 | int width, height; |
| 613 | wgetdocsize(e.window, &width, &height); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 614 | if (e.u.where.h < 0 && tegetleft(tp) == 0) |
| 615 | e.u.where.h = 0; |
Guido van Rossum | 33f1770 | 1991-02-13 23:19:39 +0000 | [diff] [blame] | 616 | else if (e.u.where.h > width && tegetright(tp) == width) |
| 617 | e.u.where.h = width; |
| 618 | if (e.u.where.v < 0 && tegettop(tp) == 0) |
| 619 | e.u.where.v = 0; |
| 620 | else if (e.u.where.v > height && tegetright(tp) == height) |
| 621 | e.u.where.v = height; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 622 | } |
| 623 | return newintobject((long) teevent(tp, &e)); |
| 624 | } |
| 625 | |
| 626 | static object * |
| 627 | text_getfocus(self, args) |
| 628 | textobject *self; |
| 629 | object *args; |
| 630 | { |
| 631 | if (!getnoarg(args)) |
| 632 | return NULL; |
| 633 | return makepoint(tegetfoc1(self->t_text), tegetfoc2(self->t_text)); |
| 634 | } |
| 635 | |
| 636 | static object * |
| 637 | text_getfocustext(self, args) |
| 638 | textobject *self; |
| 639 | object *args; |
| 640 | { |
| 641 | int f1, f2; |
| 642 | char *text; |
| 643 | if (!getnoarg(args)) |
| 644 | return NULL; |
| 645 | f1 = tegetfoc1(self->t_text); |
| 646 | f2 = tegetfoc2(self->t_text); |
| 647 | text = tegettext(self->t_text); |
| 648 | return newsizedstringobject(text + f1, f2-f1); |
| 649 | } |
| 650 | |
| 651 | static object * |
| 652 | text_getrect(self, args) |
| 653 | textobject *self; |
| 654 | object *args; |
| 655 | { |
| 656 | if (!getnoarg(args)) |
| 657 | return NULL; |
| 658 | return makerect(tegetleft(self->t_text), |
| 659 | tegettop(self->t_text), |
| 660 | tegetright(self->t_text), |
| 661 | tegetbottom(self->t_text)); |
| 662 | } |
| 663 | |
| 664 | static object * |
| 665 | text_gettext(self, args) |
| 666 | textobject *self; |
| 667 | object *args; |
| 668 | { |
| 669 | if (!getnoarg(args)) |
| 670 | return NULL; |
| 671 | return newsizedstringobject(tegettext(self->t_text), |
| 672 | tegetlen(self->t_text)); |
| 673 | } |
| 674 | |
| 675 | static object * |
| 676 | text_move(self, args) |
| 677 | textobject *self; |
| 678 | object *args; |
| 679 | { |
| 680 | int a[4]; |
| 681 | if (!getrectarg(args, a)) |
| 682 | return NULL; |
| 683 | temovenew(self->t_text, a[0], a[1], a[2], a[3]); |
| 684 | INCREF(None); |
| 685 | return None; |
| 686 | } |
| 687 | |
| 688 | static object * |
| 689 | text_setfocus(self, args) |
| 690 | textobject *self; |
| 691 | object *args; |
| 692 | { |
| 693 | int a[2]; |
| 694 | if (!getpointarg(args, a)) |
| 695 | return NULL; |
| 696 | tesetfocus(self->t_text, a[0], a[1]); |
| 697 | INCREF(None); |
| 698 | return None; |
| 699 | } |
| 700 | |
| 701 | static object * |
| 702 | text_replace(self, args) |
| 703 | textobject *self; |
| 704 | object *args; |
| 705 | { |
| 706 | object *text; |
| 707 | if (!getstrarg(args, &text)) |
| 708 | return NULL; |
| 709 | tereplace(self->t_text, getstringvalue(text)); |
| 710 | INCREF(None); |
| 711 | return None; |
| 712 | } |
| 713 | |
| 714 | static struct methodlist text_methods[] = { |
| 715 | "arrow", text_arrow, |
| 716 | "draw", text_draw, |
| 717 | "event", text_event, |
| 718 | "getfocus", text_getfocus, |
| 719 | "getfocustext", text_getfocustext, |
| 720 | "getrect", text_getrect, |
| 721 | "gettext", text_gettext, |
| 722 | "move", text_move, |
| 723 | "replace", text_replace, |
| 724 | "setfocus", text_setfocus, |
| 725 | {NULL, NULL} /* sentinel */ |
| 726 | }; |
| 727 | |
| 728 | static object * |
| 729 | text_getattr(tp, name) |
| 730 | textobject *tp; |
| 731 | char *name; |
| 732 | { |
| 733 | if (tp->t_attr != NULL) { |
| 734 | object *v = dictlookup(tp->t_attr, name); |
| 735 | if (v != NULL) { |
| 736 | INCREF(v); |
| 737 | return v; |
| 738 | } |
| 739 | } |
| 740 | return findmethod(text_methods, (object *)tp, name); |
| 741 | } |
| 742 | |
| 743 | static int |
| 744 | text_setattr(tp, name, v) |
| 745 | textobject *tp; |
| 746 | char *name; |
| 747 | object *v; |
| 748 | { |
| 749 | if (tp->t_attr == NULL) { |
| 750 | tp->t_attr = newdictobject(); |
| 751 | if (tp->t_attr == NULL) |
| 752 | return -1; |
| 753 | } |
| 754 | if (v == NULL) |
| 755 | return dictremove(tp->t_attr, name); |
| 756 | else |
| 757 | return dictinsert(tp->t_attr, name, v); |
| 758 | } |
| 759 | |
| 760 | static typeobject Texttype = { |
| 761 | OB_HEAD_INIT(&Typetype) |
| 762 | 0, /*ob_size*/ |
| 763 | "textedit", /*tp_name*/ |
| 764 | sizeof(textobject), /*tp_size*/ |
| 765 | 0, /*tp_itemsize*/ |
| 766 | /* methods */ |
| 767 | text_dealloc, /*tp_dealloc*/ |
| 768 | 0, /*tp_print*/ |
| 769 | text_getattr, /*tp_getattr*/ |
| 770 | text_setattr, /*tp_setattr*/ |
| 771 | 0, /*tp_compare*/ |
| 772 | 0, /*tp_repr*/ |
| 773 | }; |
| 774 | |
| 775 | |
| 776 | /* Menu objects */ |
| 777 | |
Guido van Rossum | 2d14e21 | 1991-02-19 12:26:49 +0000 | [diff] [blame] | 778 | #define IDOFFSET 10 /* Menu IDs we use start here */ |
| 779 | #define MAXNMENU 20 /* Max #menus we allow */ |
| 780 | static menuobject *menulist[MAXNMENU]; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 781 | |
| 782 | static menuobject * |
| 783 | newmenuobject(title) |
| 784 | object *title; |
| 785 | { |
| 786 | int id; |
| 787 | MENU *menu; |
| 788 | menuobject *mp; |
Guido van Rossum | 2d14e21 | 1991-02-19 12:26:49 +0000 | [diff] [blame] | 789 | for (id = 0; id < MAXNMENU; id++) { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 790 | if (menulist[id] == NULL) |
| 791 | break; |
| 792 | } |
| 793 | if (id >= MAXNMENU) |
| 794 | return (menuobject *) err_nomem(); |
Guido van Rossum | 2d14e21 | 1991-02-19 12:26:49 +0000 | [diff] [blame] | 795 | menu = wmenucreate(id + IDOFFSET, getstringvalue(title)); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 796 | if (menu == NULL) |
| 797 | return (menuobject *) err_nomem(); |
| 798 | mp = NEWOBJ(menuobject, &Menutype); |
| 799 | if (mp != NULL) { |
| 800 | mp->m_menu = menu; |
Guido van Rossum | 2d14e21 | 1991-02-19 12:26:49 +0000 | [diff] [blame] | 801 | mp->m_id = id + IDOFFSET; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 802 | mp->m_attr = NULL; |
| 803 | menulist[id] = mp; |
| 804 | } |
| 805 | else |
| 806 | wmenudelete(menu); |
| 807 | return mp; |
| 808 | } |
| 809 | |
| 810 | /* Menu methods */ |
| 811 | |
| 812 | static void |
| 813 | menu_dealloc(mp) |
| 814 | menuobject *mp; |
| 815 | { |
| 816 | |
Guido van Rossum | 2d14e21 | 1991-02-19 12:26:49 +0000 | [diff] [blame] | 817 | int id = mp->m_id - IDOFFSET; |
| 818 | if (id >= 0 && id < MAXNMENU && menulist[id] == mp) { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 819 | menulist[id] = NULL; |
| 820 | } |
| 821 | wmenudelete(mp->m_menu); |
| 822 | if (mp->m_attr != NULL) |
| 823 | DECREF(mp->m_attr); |
| 824 | DEL(mp); |
| 825 | } |
| 826 | |
| 827 | static object * |
| 828 | menu_additem(self, args) |
| 829 | menuobject *self; |
| 830 | object *args; |
| 831 | { |
| 832 | object *text; |
| 833 | int shortcut; |
| 834 | if (is_tupleobject(args)) { |
| 835 | object *v; |
| 836 | if (!getstrstrarg(args, &text, &v)) |
| 837 | return NULL; |
| 838 | if (getstringsize(v) != 1) { |
| 839 | err_badarg(); |
| 840 | return NULL; |
| 841 | } |
| 842 | shortcut = *getstringvalue(v) & 0xff; |
| 843 | } |
| 844 | else { |
| 845 | if (!getstrarg(args, &text)) |
| 846 | return NULL; |
| 847 | shortcut = -1; |
| 848 | } |
| 849 | wmenuadditem(self->m_menu, getstringvalue(text), shortcut); |
| 850 | INCREF(None); |
| 851 | return None; |
| 852 | } |
| 853 | |
| 854 | static object * |
| 855 | menu_setitem(self, args) |
| 856 | menuobject *self; |
| 857 | object *args; |
| 858 | { |
| 859 | int index; |
| 860 | object *text; |
| 861 | if (!getintstrarg(args, &index, &text)) |
| 862 | return NULL; |
| 863 | wmenusetitem(self->m_menu, index, getstringvalue(text)); |
| 864 | INCREF(None); |
| 865 | return None; |
| 866 | } |
| 867 | |
| 868 | static object * |
| 869 | menu_enable(self, args) |
| 870 | menuobject *self; |
| 871 | object *args; |
| 872 | { |
| 873 | int index; |
| 874 | int flag; |
| 875 | if (!getintintarg(args, &index, &flag)) |
| 876 | return NULL; |
| 877 | wmenuenable(self->m_menu, index, flag); |
| 878 | INCREF(None); |
| 879 | return None; |
| 880 | } |
| 881 | |
| 882 | static object * |
| 883 | menu_check(self, args) |
| 884 | menuobject *self; |
| 885 | object *args; |
| 886 | { |
| 887 | int index; |
| 888 | int flag; |
| 889 | if (!getintintarg(args, &index, &flag)) |
| 890 | return NULL; |
| 891 | wmenucheck(self->m_menu, index, flag); |
| 892 | INCREF(None); |
| 893 | return None; |
| 894 | } |
| 895 | |
| 896 | static struct methodlist menu_methods[] = { |
| 897 | "additem", menu_additem, |
| 898 | "setitem", menu_setitem, |
| 899 | "enable", menu_enable, |
| 900 | "check", menu_check, |
| 901 | {NULL, NULL} /* sentinel */ |
| 902 | }; |
| 903 | |
| 904 | static object * |
| 905 | menu_getattr(mp, name) |
| 906 | menuobject *mp; |
| 907 | char *name; |
| 908 | { |
| 909 | if (mp->m_attr != NULL) { |
| 910 | object *v = dictlookup(mp->m_attr, name); |
| 911 | if (v != NULL) { |
| 912 | INCREF(v); |
| 913 | return v; |
| 914 | } |
| 915 | } |
| 916 | return findmethod(menu_methods, (object *)mp, name); |
| 917 | } |
| 918 | |
| 919 | static int |
| 920 | menu_setattr(mp, name, v) |
| 921 | menuobject *mp; |
| 922 | char *name; |
| 923 | object *v; |
| 924 | { |
| 925 | if (mp->m_attr == NULL) { |
| 926 | mp->m_attr = newdictobject(); |
| 927 | if (mp->m_attr == NULL) |
| 928 | return -1; |
| 929 | } |
| 930 | if (v == NULL) |
| 931 | return dictremove(mp->m_attr, name); |
| 932 | else |
| 933 | return dictinsert(mp->m_attr, name, v); |
| 934 | } |
| 935 | |
| 936 | static typeobject Menutype = { |
| 937 | OB_HEAD_INIT(&Typetype) |
| 938 | 0, /*ob_size*/ |
| 939 | "menu", /*tp_name*/ |
| 940 | sizeof(menuobject), /*tp_size*/ |
| 941 | 0, /*tp_itemsize*/ |
| 942 | /* methods */ |
| 943 | menu_dealloc, /*tp_dealloc*/ |
| 944 | 0, /*tp_print*/ |
| 945 | menu_getattr, /*tp_getattr*/ |
| 946 | menu_setattr, /*tp_setattr*/ |
| 947 | 0, /*tp_compare*/ |
| 948 | 0, /*tp_repr*/ |
| 949 | }; |
| 950 | |
| 951 | |
| 952 | /* Windows */ |
| 953 | |
| 954 | #define MAXNWIN 50 |
| 955 | static windowobject *windowlist[MAXNWIN]; |
| 956 | |
| 957 | /* Window methods */ |
| 958 | |
| 959 | static void |
| 960 | window_dealloc(wp) |
| 961 | windowobject *wp; |
| 962 | { |
| 963 | if (wp->w_win != NULL) { |
| 964 | int tag = wgettag(wp->w_win); |
| 965 | if (tag >= 0 && tag < MAXNWIN) |
| 966 | windowlist[tag] = NULL; |
| 967 | else |
| 968 | fprintf(stderr, "XXX help! tag %d in window_dealloc\n", |
| 969 | tag); |
| 970 | wclose(wp->w_win); |
| 971 | } |
| 972 | DECREF(wp->w_title); |
| 973 | if (wp->w_attr != NULL) |
| 974 | DECREF(wp->w_attr); |
| 975 | free((char *)wp); |
| 976 | } |
| 977 | |
| 978 | static void |
| 979 | window_print(wp, fp, flags) |
| 980 | windowobject *wp; |
| 981 | FILE *fp; |
| 982 | int flags; |
| 983 | { |
| 984 | fprintf(fp, "<window titled '%s'>", getstringvalue(wp->w_title)); |
| 985 | } |
| 986 | |
| 987 | static object * |
| 988 | window_begindrawing(wp, args) |
| 989 | windowobject *wp; |
| 990 | object *args; |
| 991 | { |
| 992 | drawingobject *dp; |
| 993 | if (!getnoarg(args)) |
| 994 | return NULL; |
| 995 | if (Drawing != NULL) { |
| 996 | err_setstr(RuntimeError, "already drawing"); |
| 997 | return NULL; |
| 998 | } |
| 999 | dp = NEWOBJ(drawingobject, &Drawingtype); |
| 1000 | if (dp == NULL) |
| 1001 | return NULL; |
| 1002 | Drawing = dp; |
| 1003 | INCREF(wp); |
| 1004 | dp->d_ref = wp; |
| 1005 | wbegindrawing(wp->w_win); |
| 1006 | return (object *)dp; |
| 1007 | } |
| 1008 | |
| 1009 | static object * |
| 1010 | window_change(wp, args) |
| 1011 | windowobject *wp; |
| 1012 | object *args; |
| 1013 | { |
| 1014 | int a[4]; |
| 1015 | if (!getrectarg(args, a)) |
| 1016 | return NULL; |
| 1017 | wchange(wp->w_win, a[0], a[1], a[2], a[3]); |
| 1018 | INCREF(None); |
| 1019 | return None; |
| 1020 | } |
| 1021 | |
| 1022 | static object * |
| 1023 | window_gettitle(wp, args) |
| 1024 | windowobject *wp; |
| 1025 | object *args; |
| 1026 | { |
| 1027 | if (!getnoarg(args)) |
| 1028 | return NULL; |
| 1029 | INCREF(wp->w_title); |
| 1030 | return wp->w_title; |
| 1031 | } |
| 1032 | |
| 1033 | static object * |
| 1034 | window_getwinsize(wp, args) |
| 1035 | windowobject *wp; |
| 1036 | object *args; |
| 1037 | { |
| 1038 | int width, height; |
| 1039 | if (!getnoarg(args)) |
| 1040 | return NULL; |
| 1041 | wgetwinsize(wp->w_win, &width, &height); |
| 1042 | return makepoint(width, height); |
| 1043 | } |
| 1044 | |
| 1045 | static object * |
| 1046 | window_getdocsize(wp, args) |
| 1047 | windowobject *wp; |
| 1048 | object *args; |
| 1049 | { |
| 1050 | int width, height; |
| 1051 | if (!getnoarg(args)) |
| 1052 | return NULL; |
| 1053 | wgetdocsize(wp->w_win, &width, &height); |
| 1054 | return makepoint(width, height); |
| 1055 | } |
| 1056 | |
| 1057 | static object * |
| 1058 | window_getorigin(wp, args) |
| 1059 | windowobject *wp; |
| 1060 | object *args; |
| 1061 | { |
| 1062 | int width, height; |
| 1063 | if (!getnoarg(args)) |
| 1064 | return NULL; |
| 1065 | wgetorigin(wp->w_win, &width, &height); |
| 1066 | return makepoint(width, height); |
| 1067 | } |
| 1068 | |
| 1069 | static object * |
| 1070 | window_scroll(wp, args) |
| 1071 | windowobject *wp; |
| 1072 | object *args; |
| 1073 | { |
| 1074 | int a[6]; |
| 1075 | if (!getrectpointarg(args, a)) |
| 1076 | return NULL; |
| 1077 | wscroll(wp->w_win, a[0], a[1], a[2], a[3], a[4], a[5]); |
| 1078 | INCREF(None); |
| 1079 | return None; |
| 1080 | } |
| 1081 | |
| 1082 | static object * |
| 1083 | window_setdocsize(wp, args) |
| 1084 | windowobject *wp; |
| 1085 | object *args; |
| 1086 | { |
| 1087 | int a[2]; |
| 1088 | if (!getpointarg(args, a)) |
| 1089 | return NULL; |
| 1090 | wsetdocsize(wp->w_win, a[0], a[1]); |
| 1091 | INCREF(None); |
| 1092 | return None; |
| 1093 | } |
| 1094 | |
| 1095 | static object * |
| 1096 | window_setorigin(wp, args) |
| 1097 | windowobject *wp; |
| 1098 | object *args; |
| 1099 | { |
| 1100 | int a[2]; |
| 1101 | if (!getpointarg(args, a)) |
| 1102 | return NULL; |
| 1103 | wsetorigin(wp->w_win, a[0], a[1]); |
| 1104 | INCREF(None); |
| 1105 | return None; |
| 1106 | } |
| 1107 | |
| 1108 | static object * |
| 1109 | window_settitle(wp, args) |
| 1110 | windowobject *wp; |
| 1111 | object *args; |
| 1112 | { |
| 1113 | object *title; |
| 1114 | if (!getstrarg(args, &title)) |
| 1115 | return NULL; |
| 1116 | DECREF(wp->w_title); |
| 1117 | INCREF(title); |
| 1118 | wp->w_title = title; |
| 1119 | wsettitle(wp->w_win, getstringvalue(title)); |
| 1120 | INCREF(None); |
| 1121 | return None; |
| 1122 | } |
| 1123 | |
| 1124 | static object * |
| 1125 | window_show(wp, args) |
| 1126 | windowobject *wp; |
| 1127 | object *args; |
| 1128 | { |
| 1129 | int a[4]; |
| 1130 | if (!getrectarg(args, a)) |
| 1131 | return NULL; |
| 1132 | wshow(wp->w_win, a[0], a[1], a[2], a[3]); |
| 1133 | INCREF(None); |
| 1134 | return None; |
| 1135 | } |
| 1136 | |
| 1137 | static object * |
| 1138 | window_settimer(wp, args) |
| 1139 | windowobject *wp; |
| 1140 | object *args; |
| 1141 | { |
| 1142 | int a; |
| 1143 | if (!getintarg(args, &a)) |
| 1144 | return NULL; |
| 1145 | wsettimer(wp->w_win, a); |
| 1146 | INCREF(None); |
| 1147 | return None; |
| 1148 | } |
| 1149 | |
| 1150 | static object * |
| 1151 | window_menucreate(self, args) |
| 1152 | windowobject *self; |
| 1153 | object *args; |
| 1154 | { |
| 1155 | menuobject *mp; |
| 1156 | object *title; |
| 1157 | if (!getstrarg(args, &title)) |
| 1158 | return NULL; |
| 1159 | wmenusetdeflocal(1); |
| 1160 | mp = newmenuobject(title); |
| 1161 | if (mp == NULL) |
| 1162 | return NULL; |
| 1163 | wmenuattach(self->w_win, mp->m_menu); |
| 1164 | return (object *)mp; |
| 1165 | } |
| 1166 | |
| 1167 | static object * |
| 1168 | window_textcreate(self, args) |
| 1169 | windowobject *self; |
| 1170 | object *args; |
| 1171 | { |
| 1172 | textobject *tp; |
| 1173 | int a[4]; |
| 1174 | if (!getrectarg(args, a)) |
| 1175 | return NULL; |
| 1176 | return (object *) |
| 1177 | newtextobject(self, a[0], a[1], a[2], a[3]); |
| 1178 | } |
| 1179 | |
Guido van Rossum | 5b10f45 | 1990-10-30 16:01:48 +0000 | [diff] [blame] | 1180 | static object * |
| 1181 | window_setselection(self, args) |
| 1182 | windowobject *self; |
| 1183 | object *args; |
| 1184 | { |
| 1185 | int sel; |
| 1186 | object *str; |
| 1187 | int ok; |
| 1188 | if (!getintstrarg(args, &sel, &str)) |
| 1189 | return NULL; |
| 1190 | ok = wsetselection(self->w_win, sel, |
| 1191 | getstringvalue(str), (int)getstringsize(str)); |
| 1192 | return newintobject(ok); |
| 1193 | } |
| 1194 | |
| 1195 | static object * |
| 1196 | window_setwincursor(self, args) |
| 1197 | windowobject *self; |
| 1198 | object *args; |
| 1199 | { |
| 1200 | object *str; |
| 1201 | CURSOR *c; |
| 1202 | if (!getstrarg(args, &str)) |
| 1203 | return NULL; |
| 1204 | c = wfetchcursor(getstringvalue(str)); |
| 1205 | if (c == NULL) { |
| 1206 | err_setstr(RuntimeError, "no such cursor"); |
| 1207 | return NULL; |
| 1208 | } |
| 1209 | wsetwincursor(self->w_win, c); |
| 1210 | INCREF(None); |
| 1211 | return None; |
| 1212 | } |
| 1213 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1214 | static struct methodlist window_methods[] = { |
| 1215 | {"begindrawing",window_begindrawing}, |
| 1216 | {"change", window_change}, |
| 1217 | {"getdocsize", window_getdocsize}, |
| 1218 | {"getorigin", window_getorigin}, |
| 1219 | {"gettitle", window_gettitle}, |
| 1220 | {"getwinsize", window_getwinsize}, |
| 1221 | {"menucreate", window_menucreate}, |
| 1222 | {"scroll", window_scroll}, |
Guido van Rossum | 5b10f45 | 1990-10-30 16:01:48 +0000 | [diff] [blame] | 1223 | {"setwincursor",window_setwincursor}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1224 | {"setdocsize", window_setdocsize}, |
| 1225 | {"setorigin", window_setorigin}, |
Guido van Rossum | 5b10f45 | 1990-10-30 16:01:48 +0000 | [diff] [blame] | 1226 | {"setselection",window_setselection}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1227 | {"settimer", window_settimer}, |
| 1228 | {"settitle", window_settitle}, |
| 1229 | {"show", window_show}, |
| 1230 | {"textcreate", window_textcreate}, |
| 1231 | {NULL, NULL} /* sentinel */ |
| 1232 | }; |
| 1233 | |
| 1234 | static object * |
| 1235 | window_getattr(wp, name) |
| 1236 | windowobject *wp; |
| 1237 | char *name; |
| 1238 | { |
| 1239 | if (wp->w_attr != NULL) { |
| 1240 | object *v = dictlookup(wp->w_attr, name); |
| 1241 | if (v != NULL) { |
| 1242 | INCREF(v); |
| 1243 | return v; |
| 1244 | } |
| 1245 | } |
| 1246 | return findmethod(window_methods, (object *)wp, name); |
| 1247 | } |
| 1248 | |
| 1249 | static int |
| 1250 | window_setattr(wp, name, v) |
| 1251 | windowobject *wp; |
| 1252 | char *name; |
| 1253 | object *v; |
| 1254 | { |
| 1255 | if (wp->w_attr == NULL) { |
| 1256 | wp->w_attr = newdictobject(); |
| 1257 | if (wp->w_attr == NULL) |
| 1258 | return -1; |
| 1259 | } |
| 1260 | if (v == NULL) |
| 1261 | return dictremove(wp->w_attr, name); |
| 1262 | else |
| 1263 | return dictinsert(wp->w_attr, name, v); |
| 1264 | } |
| 1265 | |
| 1266 | static typeobject Windowtype = { |
| 1267 | OB_HEAD_INIT(&Typetype) |
| 1268 | 0, /*ob_size*/ |
| 1269 | "window", /*tp_name*/ |
| 1270 | sizeof(windowobject), /*tp_size*/ |
| 1271 | 0, /*tp_itemsize*/ |
| 1272 | /* methods */ |
| 1273 | window_dealloc, /*tp_dealloc*/ |
| 1274 | window_print, /*tp_print*/ |
| 1275 | window_getattr, /*tp_getattr*/ |
| 1276 | window_setattr, /*tp_setattr*/ |
| 1277 | 0, /*tp_compare*/ |
| 1278 | 0, /*tp_repr*/ |
| 1279 | }; |
| 1280 | |
| 1281 | /* Stdwin methods */ |
| 1282 | |
| 1283 | static object * |
| 1284 | stdwin_open(sw, args) |
| 1285 | object *sw; |
| 1286 | object *args; |
| 1287 | { |
| 1288 | int tag; |
| 1289 | object *title; |
| 1290 | windowobject *wp; |
| 1291 | if (!getstrarg(args, &title)) |
| 1292 | return NULL; |
| 1293 | for (tag = 0; tag < MAXNWIN; tag++) { |
| 1294 | if (windowlist[tag] == NULL) |
| 1295 | break; |
| 1296 | } |
| 1297 | if (tag >= MAXNWIN) |
| 1298 | return err_nomem(); |
| 1299 | wp = NEWOBJ(windowobject, &Windowtype); |
| 1300 | if (wp == NULL) |
| 1301 | return NULL; |
| 1302 | INCREF(title); |
| 1303 | wp->w_title = title; |
| 1304 | wp->w_win = wopen(getstringvalue(title), (void (*)()) NULL); |
| 1305 | wp->w_attr = NULL; |
| 1306 | if (wp->w_win == NULL) { |
| 1307 | DECREF(wp); |
| 1308 | return NULL; |
| 1309 | } |
| 1310 | windowlist[tag] = wp; |
| 1311 | wsettag(wp->w_win, tag); |
| 1312 | return (object *)wp; |
| 1313 | } |
| 1314 | |
| 1315 | static object * |
Guido van Rossum | e8e7cf4 | 1991-01-16 14:06:18 +0000 | [diff] [blame] | 1316 | stdwin_get_poll_event(poll, args) |
| 1317 | int poll; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1318 | object *args; |
| 1319 | { |
| 1320 | EVENT e; |
| 1321 | object *v, *w; |
| 1322 | if (!getnoarg(args)) |
| 1323 | return NULL; |
| 1324 | if (Drawing != NULL) { |
| 1325 | err_setstr(RuntimeError, "cannot getevent() while drawing"); |
| 1326 | return NULL; |
| 1327 | } |
Guido van Rossum | e8e7cf4 | 1991-01-16 14:06:18 +0000 | [diff] [blame] | 1328 | /* again: */ |
| 1329 | if (poll) { |
| 1330 | if (!wpollevent(&e)) { |
| 1331 | INCREF(None); |
| 1332 | return None; |
| 1333 | } |
| 1334 | } |
| 1335 | else |
| 1336 | wgetevent(&e); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1337 | if (e.type == WE_COMMAND && e.u.command == WC_CANCEL) { |
| 1338 | /* Turn keyboard interrupts into exceptions */ |
| 1339 | err_set(KeyboardInterrupt); |
| 1340 | return NULL; |
| 1341 | } |
Guido van Rossum | e8e7cf4 | 1991-01-16 14:06:18 +0000 | [diff] [blame] | 1342 | /* |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1343 | if (e.window == NULL && (e.type == WE_COMMAND || e.type == WE_CHAR)) |
| 1344 | goto again; |
Guido van Rossum | e8e7cf4 | 1991-01-16 14:06:18 +0000 | [diff] [blame] | 1345 | */ |
Guido van Rossum | 124967c | 1990-11-06 15:17:35 +0000 | [diff] [blame] | 1346 | if (e.type == WE_COMMAND && e.u.command == WC_CLOSE) { |
| 1347 | /* Turn WC_CLOSE commands into WE_CLOSE events */ |
| 1348 | e.type = WE_CLOSE; |
| 1349 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1350 | v = newtupleobject(3); |
| 1351 | if (v == NULL) |
| 1352 | return NULL; |
| 1353 | if ((w = newintobject((long)e.type)) == NULL) { |
| 1354 | DECREF(v); |
| 1355 | return NULL; |
| 1356 | } |
| 1357 | settupleitem(v, 0, w); |
| 1358 | if (e.window == NULL) |
| 1359 | w = None; |
| 1360 | else { |
| 1361 | int tag = wgettag(e.window); |
| 1362 | if (tag < 0 || tag >= MAXNWIN || windowlist[tag] == NULL) |
| 1363 | w = None; |
| 1364 | else |
| 1365 | w = (object *)windowlist[tag]; |
Guido van Rossum | e8e7cf4 | 1991-01-16 14:06:18 +0000 | [diff] [blame] | 1366 | #ifdef sgi |
| 1367 | /* XXX Trap for unexplained weird bug */ |
| 1368 | if ((long)w == (long)0x80000001) { |
| 1369 | err_setstr(SystemError, |
| 1370 | "bad pointer in stdwin.getevent()"); |
| 1371 | return NULL; |
| 1372 | } |
| 1373 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1374 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1375 | INCREF(w); |
| 1376 | settupleitem(v, 1, w); |
| 1377 | switch (e.type) { |
| 1378 | case WE_CHAR: |
| 1379 | { |
| 1380 | char c[1]; |
| 1381 | c[0] = e.u.character; |
| 1382 | w = newsizedstringobject(c, 1); |
| 1383 | } |
| 1384 | break; |
| 1385 | case WE_COMMAND: |
| 1386 | w = newintobject((long)e.u.command); |
| 1387 | break; |
| 1388 | case WE_DRAW: |
| 1389 | w = makerect(e.u.area.left, e.u.area.top, |
| 1390 | e.u.area.right, e.u.area.bottom); |
| 1391 | break; |
| 1392 | case WE_MOUSE_DOWN: |
| 1393 | case WE_MOUSE_MOVE: |
| 1394 | case WE_MOUSE_UP: |
| 1395 | w = makemouse(e.u.where.h, e.u.where.v, |
| 1396 | e.u.where.clicks, |
| 1397 | e.u.where.button, |
| 1398 | e.u.where.mask); |
| 1399 | break; |
| 1400 | case WE_MENU: |
Guido van Rossum | 2d14e21 | 1991-02-19 12:26:49 +0000 | [diff] [blame] | 1401 | if (e.u.m.id >= IDOFFSET && e.u.m.id < IDOFFSET+MAXNMENU && |
| 1402 | menulist[e.u.m.id - IDOFFSET] != NULL) |
| 1403 | w = (object *)menulist[e.u.m.id - IDOFFSET]; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1404 | else |
| 1405 | w = None; |
| 1406 | w = makemenu(w, e.u.m.item); |
| 1407 | break; |
Guido van Rossum | 5b10f45 | 1990-10-30 16:01:48 +0000 | [diff] [blame] | 1408 | case WE_LOST_SEL: |
| 1409 | w = newintobject((long)e.u.sel); |
| 1410 | break; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1411 | default: |
| 1412 | w = None; |
| 1413 | INCREF(w); |
| 1414 | break; |
| 1415 | } |
| 1416 | if (w == NULL) { |
| 1417 | DECREF(v); |
| 1418 | return NULL; |
| 1419 | } |
| 1420 | settupleitem(v, 2, w); |
| 1421 | return v; |
| 1422 | } |
| 1423 | |
| 1424 | static object * |
Guido van Rossum | e8e7cf4 | 1991-01-16 14:06:18 +0000 | [diff] [blame] | 1425 | stdwin_getevent(sw, args) |
| 1426 | object *sw; |
| 1427 | object *args; |
| 1428 | { |
| 1429 | return stdwin_get_poll_event(0, args); |
| 1430 | } |
| 1431 | |
| 1432 | static object * |
| 1433 | stdwin_pollevent(sw, args) |
| 1434 | object *sw; |
| 1435 | object *args; |
| 1436 | { |
| 1437 | return stdwin_get_poll_event(1, args); |
| 1438 | } |
| 1439 | |
| 1440 | static object * |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1441 | stdwin_setdefwinpos(sw, args) |
| 1442 | object *sw; |
| 1443 | object *args; |
| 1444 | { |
| 1445 | int a[2]; |
| 1446 | if (!getpointarg(args, a)) |
| 1447 | return NULL; |
| 1448 | wsetdefwinpos(a[0], a[1]); |
| 1449 | INCREF(None); |
| 1450 | return None; |
| 1451 | } |
| 1452 | |
| 1453 | static object * |
| 1454 | stdwin_setdefwinsize(sw, args) |
| 1455 | object *sw; |
| 1456 | object *args; |
| 1457 | { |
| 1458 | int a[2]; |
| 1459 | if (!getpointarg(args, a)) |
| 1460 | return NULL; |
| 1461 | wsetdefwinsize(a[0], a[1]); |
| 1462 | INCREF(None); |
| 1463 | return None; |
| 1464 | } |
| 1465 | |
| 1466 | static object * |
Guido van Rossum | 33f1770 | 1991-02-13 23:19:39 +0000 | [diff] [blame] | 1467 | stdwin_getdefwinpos(wp, args) |
| 1468 | windowobject *wp; |
| 1469 | object *args; |
| 1470 | { |
| 1471 | int h, v; |
| 1472 | if (!getnoarg(args)) |
| 1473 | return NULL; |
| 1474 | wgetdefwinpos(&h, &v); |
| 1475 | return makepoint(h, v); |
| 1476 | } |
| 1477 | |
| 1478 | static object * |
| 1479 | stdwin_getdefwinsize(wp, args) |
| 1480 | windowobject *wp; |
| 1481 | object *args; |
| 1482 | { |
| 1483 | int width, height; |
| 1484 | if (!getnoarg(args)) |
| 1485 | return NULL; |
| 1486 | wgetdefwinsize(&width, &height); |
| 1487 | return makepoint(width, height); |
| 1488 | } |
| 1489 | |
| 1490 | static object * |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1491 | stdwin_menucreate(self, args) |
| 1492 | object *self; |
| 1493 | object *args; |
| 1494 | { |
| 1495 | object *title; |
| 1496 | if (!getstrarg(args, &title)) |
| 1497 | return NULL; |
| 1498 | wmenusetdeflocal(0); |
| 1499 | return (object *)newmenuobject(title); |
| 1500 | } |
| 1501 | |
| 1502 | static object * |
| 1503 | stdwin_askfile(self, args) |
| 1504 | object *self; |
| 1505 | object *args; |
| 1506 | { |
| 1507 | object *prompt, *dflt; |
| 1508 | int new, ret; |
| 1509 | char buf[256]; |
| 1510 | if (!getstrstrintarg(args, &prompt, &dflt, &new)) |
| 1511 | return NULL; |
| 1512 | strncpy(buf, getstringvalue(dflt), sizeof buf); |
| 1513 | buf[sizeof buf - 1] = '\0'; |
| 1514 | ret = waskfile(getstringvalue(prompt), buf, sizeof buf, new); |
| 1515 | if (!ret) { |
| 1516 | err_set(KeyboardInterrupt); |
| 1517 | return NULL; |
| 1518 | } |
| 1519 | return newstringobject(buf); |
| 1520 | } |
| 1521 | |
| 1522 | static object * |
| 1523 | stdwin_askync(self, args) |
| 1524 | object *self; |
| 1525 | object *args; |
| 1526 | { |
| 1527 | object *prompt; |
| 1528 | int new, ret; |
| 1529 | if (!getstrintarg(args, &prompt, &new)) |
| 1530 | return NULL; |
| 1531 | ret = waskync(getstringvalue(prompt), new); |
| 1532 | if (ret < 0) { |
| 1533 | err_set(KeyboardInterrupt); |
| 1534 | return NULL; |
| 1535 | } |
| 1536 | return newintobject((long)ret); |
| 1537 | } |
| 1538 | |
| 1539 | static object * |
| 1540 | stdwin_askstr(self, args) |
| 1541 | object *self; |
| 1542 | object *args; |
| 1543 | { |
| 1544 | object *prompt, *dflt; |
| 1545 | int ret; |
| 1546 | char buf[256]; |
| 1547 | if (!getstrstrarg(args, &prompt, &dflt)) |
| 1548 | return NULL; |
| 1549 | strncpy(buf, getstringvalue(dflt), sizeof buf); |
| 1550 | buf[sizeof buf - 1] = '\0'; |
| 1551 | ret = waskstr(getstringvalue(prompt), buf, sizeof buf); |
| 1552 | if (!ret) { |
| 1553 | err_set(KeyboardInterrupt); |
| 1554 | return NULL; |
| 1555 | } |
| 1556 | return newstringobject(buf); |
| 1557 | } |
| 1558 | |
| 1559 | static object * |
| 1560 | stdwin_message(self, args) |
| 1561 | object *self; |
| 1562 | object *args; |
| 1563 | { |
| 1564 | object *msg; |
| 1565 | if (!getstrarg(args, &msg)) |
| 1566 | return NULL; |
| 1567 | wmessage(getstringvalue(msg)); |
| 1568 | INCREF(None); |
| 1569 | return None; |
| 1570 | } |
| 1571 | |
| 1572 | static object * |
| 1573 | stdwin_fleep(self, args) |
| 1574 | object *self; |
| 1575 | object *args; |
| 1576 | { |
| 1577 | if (!getnoarg(args)) |
| 1578 | return NULL; |
| 1579 | wfleep(); |
| 1580 | INCREF(None); |
| 1581 | return None; |
| 1582 | } |
| 1583 | |
| 1584 | static object * |
| 1585 | stdwin_setcutbuffer(self, args) |
| 1586 | object *self; |
| 1587 | object *args; |
| 1588 | { |
Guido van Rossum | 5b10f45 | 1990-10-30 16:01:48 +0000 | [diff] [blame] | 1589 | int i; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1590 | object *str; |
Guido van Rossum | 124967c | 1990-11-06 15:17:35 +0000 | [diff] [blame] | 1591 | if (!getintstrarg(args, &i, &str)) |
| 1592 | return NULL; |
Guido van Rossum | 5b10f45 | 1990-10-30 16:01:48 +0000 | [diff] [blame] | 1593 | wsetcutbuffer(i, getstringvalue(str), getstringsize(str)); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1594 | INCREF(None); |
| 1595 | return None; |
| 1596 | } |
| 1597 | |
| 1598 | static object * |
| 1599 | stdwin_getcutbuffer(self, args) |
| 1600 | object *self; |
| 1601 | object *args; |
| 1602 | { |
Guido van Rossum | 5b10f45 | 1990-10-30 16:01:48 +0000 | [diff] [blame] | 1603 | int i; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1604 | char *str; |
Guido van Rossum | 01769f0 | 1990-10-30 13:39:00 +0000 | [diff] [blame] | 1605 | int len; |
Guido van Rossum | 124967c | 1990-11-06 15:17:35 +0000 | [diff] [blame] | 1606 | if (!getintarg(args, &i)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1607 | return NULL; |
Guido van Rossum | 5b10f45 | 1990-10-30 16:01:48 +0000 | [diff] [blame] | 1608 | str = wgetcutbuffer(i, &len); |
Guido van Rossum | 01769f0 | 1990-10-30 13:39:00 +0000 | [diff] [blame] | 1609 | if (str == NULL) { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1610 | str = ""; |
Guido van Rossum | 01769f0 | 1990-10-30 13:39:00 +0000 | [diff] [blame] | 1611 | len = 0; |
| 1612 | } |
| 1613 | return newsizedstringobject(str, len); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1614 | } |
| 1615 | |
Guido van Rossum | 5b10f45 | 1990-10-30 16:01:48 +0000 | [diff] [blame] | 1616 | static object * |
| 1617 | stdwin_rotatecutbuffers(self, args) |
| 1618 | object *self; |
| 1619 | object *args; |
| 1620 | { |
| 1621 | int i; |
| 1622 | if (!getintarg(args, &i)) |
| 1623 | return NULL; |
| 1624 | wrotatecutbuffers(i); |
| 1625 | INCREF(None); |
| 1626 | return None; |
| 1627 | } |
| 1628 | |
| 1629 | static object * |
| 1630 | stdwin_getselection(self, args) |
| 1631 | object *self; |
| 1632 | object *args; |
| 1633 | { |
| 1634 | int sel; |
| 1635 | char *data; |
| 1636 | int len; |
| 1637 | if (!getintarg(args, &sel)) |
| 1638 | return NULL; |
| 1639 | data = wgetselection(sel, &len); |
| 1640 | if (data == NULL) { |
| 1641 | data = ""; |
| 1642 | len = 0; |
| 1643 | } |
| 1644 | return newsizedstringobject(data, len); |
| 1645 | } |
| 1646 | |
| 1647 | static object * |
| 1648 | stdwin_resetselection(self, args) |
| 1649 | object *self; |
| 1650 | object *args; |
| 1651 | { |
| 1652 | int sel; |
| 1653 | if (!getintarg(args, &sel)) |
| 1654 | return NULL; |
| 1655 | wresetselection(sel); |
| 1656 | INCREF(None); |
| 1657 | return None; |
| 1658 | } |
| 1659 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1660 | static struct methodlist stdwin_methods[] = { |
| 1661 | {"askfile", stdwin_askfile}, |
| 1662 | {"askstr", stdwin_askstr}, |
| 1663 | {"askync", stdwin_askync}, |
| 1664 | {"fleep", stdwin_fleep}, |
Guido van Rossum | 5b10f45 | 1990-10-30 16:01:48 +0000 | [diff] [blame] | 1665 | {"getselection", stdwin_getselection}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1666 | {"getcutbuffer", stdwin_getcutbuffer}, |
Guido van Rossum | 33f1770 | 1991-02-13 23:19:39 +0000 | [diff] [blame] | 1667 | {"getdefwinpos", stdwin_getdefwinpos}, |
| 1668 | {"getdefwinsize", stdwin_getdefwinsize}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1669 | {"getevent", stdwin_getevent}, |
| 1670 | {"menucreate", stdwin_menucreate}, |
| 1671 | {"message", stdwin_message}, |
| 1672 | {"open", stdwin_open}, |
Guido van Rossum | e8e7cf4 | 1991-01-16 14:06:18 +0000 | [diff] [blame] | 1673 | {"pollevent", stdwin_pollevent}, |
Guido van Rossum | 5b10f45 | 1990-10-30 16:01:48 +0000 | [diff] [blame] | 1674 | {"resetselection", stdwin_resetselection}, |
| 1675 | {"rotatecutbuffers", stdwin_rotatecutbuffers}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1676 | {"setcutbuffer", stdwin_setcutbuffer}, |
| 1677 | {"setdefwinpos", stdwin_setdefwinpos}, |
| 1678 | {"setdefwinsize", stdwin_setdefwinsize}, |
| 1679 | |
| 1680 | /* Text measuring methods borrow code from drawing objects: */ |
| 1681 | {"baseline", drawing_baseline}, |
| 1682 | {"lineheight", drawing_lineheight}, |
| 1683 | {"textbreak", drawing_textbreak}, |
| 1684 | {"textwidth", drawing_textwidth}, |
| 1685 | {NULL, NULL} /* sentinel */ |
| 1686 | }; |
| 1687 | |
| 1688 | void |
| 1689 | initstdwin() |
| 1690 | { |
Guido van Rossum | 2d14e21 | 1991-02-19 12:26:49 +0000 | [diff] [blame] | 1691 | static int inited; |
| 1692 | if (!inited) { |
| 1693 | winit(); |
| 1694 | inited = 1; |
| 1695 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1696 | initmodule("stdwin", stdwin_methods); |
| 1697 | } |