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 | |
Guido van Rossum | bbf9434 | 1991-12-16 15:44:53 +0000 | [diff] [blame] | 71 | static object *StdwinError; /* Exception stdwin.error */ |
Guido van Rossum | 87e7ea7 | 1991-12-10 14:00:03 +0000 | [diff] [blame] | 72 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 73 | /* Window and menu object types declared here because of forward references */ |
| 74 | |
| 75 | typedef struct { |
| 76 | OB_HEAD |
| 77 | object *w_title; |
| 78 | WINDOW *w_win; |
| 79 | object *w_attr; /* Attributes dictionary */ |
| 80 | } windowobject; |
| 81 | |
| 82 | extern typeobject Windowtype; /* Really static, forward */ |
| 83 | |
| 84 | #define is_windowobject(wp) ((wp)->ob_type == &Windowtype) |
| 85 | |
| 86 | typedef struct { |
| 87 | OB_HEAD |
| 88 | MENU *m_menu; |
| 89 | int m_id; |
| 90 | object *m_attr; /* Attributes dictionary */ |
| 91 | } menuobject; |
| 92 | |
| 93 | extern typeobject Menutype; /* Really static, forward */ |
| 94 | |
| 95 | #define is_menuobject(mp) ((mp)->ob_type == &Menutype) |
| 96 | |
| 97 | |
| 98 | /* Strongly stdwin-specific argument handlers */ |
| 99 | |
| 100 | static int |
| 101 | getmousedetail(v, ep) |
| 102 | object *v; |
| 103 | EVENT *ep; |
| 104 | { |
| 105 | if (v == NULL || !is_tupleobject(v) || gettuplesize(v) != 4) |
| 106 | return err_badarg(); |
| 107 | return getintintarg(gettupleitem(v, 0), |
| 108 | &ep->u.where.h, &ep->u.where.v) && |
| 109 | getintarg(gettupleitem(v, 1), &ep->u.where.clicks) && |
| 110 | getintarg(gettupleitem(v, 2), &ep->u.where.button) && |
| 111 | getintarg(gettupleitem(v, 3), &ep->u.where.mask); |
| 112 | } |
| 113 | |
| 114 | static int |
| 115 | getmenudetail(v, ep) |
| 116 | object *v; |
| 117 | EVENT *ep; |
| 118 | { |
| 119 | object *mp; |
| 120 | if (v == NULL || !is_tupleobject(v) || gettuplesize(v) != 2) |
| 121 | return err_badarg(); |
| 122 | mp = gettupleitem(v, 0); |
| 123 | if (mp == NULL || !is_menuobject(mp)) |
| 124 | return err_badarg(); |
| 125 | ep->u.m.id = ((menuobject *)mp) -> m_id; |
| 126 | return getintarg(gettupleitem(v, 1), &ep->u.m.item); |
| 127 | } |
| 128 | |
| 129 | static int |
| 130 | geteventarg(v, ep) |
| 131 | object *v; |
| 132 | EVENT *ep; |
| 133 | { |
| 134 | object *wp, *detail; |
| 135 | int a[4]; |
| 136 | if (v == NULL || !is_tupleobject(v) || gettuplesize(v) != 3) |
| 137 | return err_badarg(); |
| 138 | if (!getintarg(gettupleitem(v, 0), &ep->type)) |
| 139 | return 0; |
| 140 | wp = gettupleitem(v, 1); |
| 141 | if (wp == None) |
| 142 | ep->window = NULL; |
| 143 | else if (wp == NULL || !is_windowobject(wp)) |
| 144 | return err_badarg(); |
| 145 | else |
| 146 | ep->window = ((windowobject *)wp) -> w_win; |
| 147 | detail = gettupleitem(v, 2); |
| 148 | switch (ep->type) { |
| 149 | case WE_CHAR: |
| 150 | if (!is_stringobject(detail) || getstringsize(detail) != 1) |
| 151 | return err_badarg(); |
| 152 | ep->u.character = getstringvalue(detail)[0]; |
| 153 | return 1; |
| 154 | case WE_COMMAND: |
| 155 | return getintarg(detail, &ep->u.command); |
| 156 | case WE_DRAW: |
| 157 | if (!getrectarg(detail, a)) |
| 158 | return 0; |
| 159 | ep->u.area.left = a[0]; |
| 160 | ep->u.area.top = a[1]; |
| 161 | ep->u.area.right = a[2]; |
| 162 | ep->u.area.bottom = a[3]; |
| 163 | return 1; |
| 164 | case WE_MOUSE_DOWN: |
| 165 | case WE_MOUSE_UP: |
| 166 | case WE_MOUSE_MOVE: |
| 167 | return getmousedetail(detail, ep); |
| 168 | case WE_MENU: |
| 169 | return getmenudetail(detail, ep); |
| 170 | default: |
| 171 | return 1; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | |
| 176 | /* Return construction tools */ |
| 177 | |
| 178 | static object * |
| 179 | makepoint(a, b) |
| 180 | int a, b; |
| 181 | { |
| 182 | object *v; |
| 183 | object *w; |
| 184 | if ((v = newtupleobject(2)) == NULL) |
| 185 | return NULL; |
| 186 | if ((w = newintobject((long)a)) == NULL || |
| 187 | settupleitem(v, 0, w) != 0 || |
| 188 | (w = newintobject((long)b)) == NULL || |
| 189 | settupleitem(v, 1, w) != 0) { |
| 190 | DECREF(v); |
| 191 | return NULL; |
| 192 | } |
| 193 | return v; |
| 194 | } |
| 195 | |
| 196 | static object * |
| 197 | makerect(a, b, c, d) |
| 198 | int a, b, c, d; |
| 199 | { |
| 200 | object *v; |
| 201 | object *w; |
| 202 | if ((v = newtupleobject(2)) == NULL) |
| 203 | return NULL; |
| 204 | if ((w = makepoint(a, b)) == NULL || |
| 205 | settupleitem(v, 0, w) != 0 || |
| 206 | (w = makepoint(c, d)) == NULL || |
| 207 | settupleitem(v, 1, w) != 0) { |
| 208 | DECREF(v); |
| 209 | return NULL; |
| 210 | } |
| 211 | return v; |
| 212 | } |
| 213 | |
| 214 | static object * |
| 215 | makemouse(hor, ver, clicks, button, mask) |
| 216 | int hor, ver, clicks, button, mask; |
| 217 | { |
| 218 | object *v; |
| 219 | object *w; |
| 220 | if ((v = newtupleobject(4)) == NULL) |
| 221 | return NULL; |
| 222 | if ((w = makepoint(hor, ver)) == NULL || |
| 223 | settupleitem(v, 0, w) != 0 || |
| 224 | (w = newintobject((long)clicks)) == NULL || |
| 225 | settupleitem(v, 1, w) != 0 || |
| 226 | (w = newintobject((long)button)) == NULL || |
| 227 | settupleitem(v, 2, w) != 0 || |
| 228 | (w = newintobject((long)mask)) == NULL || |
| 229 | settupleitem(v, 3, w) != 0) { |
| 230 | DECREF(v); |
| 231 | return NULL; |
| 232 | } |
| 233 | return v; |
| 234 | } |
| 235 | |
| 236 | static object * |
| 237 | makemenu(mp, item) |
| 238 | object *mp; |
| 239 | int item; |
| 240 | { |
| 241 | object *v; |
| 242 | object *w; |
| 243 | if ((v = newtupleobject(2)) == NULL) |
| 244 | return NULL; |
| 245 | INCREF(mp); |
| 246 | if (settupleitem(v, 0, mp) != 0 || |
| 247 | (w = newintobject((long)item)) == NULL || |
| 248 | settupleitem(v, 1, w) != 0) { |
| 249 | DECREF(v); |
| 250 | return NULL; |
| 251 | } |
| 252 | return v; |
| 253 | } |
| 254 | |
| 255 | |
| 256 | /* Drawing objects */ |
| 257 | |
| 258 | typedef struct { |
| 259 | OB_HEAD |
| 260 | windowobject *d_ref; |
| 261 | } drawingobject; |
| 262 | |
| 263 | static drawingobject *Drawing; /* Set to current drawing object, or NULL */ |
| 264 | |
| 265 | /* Drawing methods */ |
| 266 | |
Guido van Rossum | 3c28474 | 1991-11-27 14:54:54 +0000 | [diff] [blame] | 267 | static object * |
| 268 | drawing_close(dp) |
| 269 | drawingobject *dp; |
| 270 | { |
| 271 | if (dp->d_ref != NULL) { |
| 272 | wenddrawing(dp->d_ref->w_win); |
| 273 | Drawing = NULL; |
| 274 | DECREF(dp->d_ref); |
| 275 | dp->d_ref = NULL; |
| 276 | } |
| 277 | INCREF(None); |
| 278 | return None; |
| 279 | } |
Guido van Rossum | 77b4604 | 1992-01-14 18:41:24 +0000 | [diff] [blame^] | 280 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 281 | static void |
| 282 | drawing_dealloc(dp) |
| 283 | drawingobject *dp; |
| 284 | { |
Guido van Rossum | 3c28474 | 1991-11-27 14:54:54 +0000 | [diff] [blame] | 285 | if (dp->d_ref != NULL) { |
| 286 | wenddrawing(dp->d_ref->w_win); |
| 287 | Drawing = NULL; |
| 288 | DECREF(dp->d_ref); |
| 289 | dp->d_ref = NULL; |
| 290 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 291 | free((char *)dp); |
| 292 | } |
| 293 | |
| 294 | static object * |
| 295 | drawing_generic(dp, args, func) |
| 296 | drawingobject *dp; |
| 297 | object *args; |
| 298 | void (*func) FPROTO((int, int, int, int)); |
| 299 | { |
| 300 | int a[4]; |
| 301 | if (!getrectarg(args, a)) |
| 302 | return NULL; |
| 303 | (*func)(a[0], a[1], a[2], a[3]); |
| 304 | INCREF(None); |
| 305 | return None; |
| 306 | } |
| 307 | |
| 308 | static object * |
| 309 | drawing_line(dp, args) |
| 310 | drawingobject *dp; |
| 311 | object *args; |
| 312 | { |
Guido van Rossum | bf10973 | 1991-03-06 13:14:12 +0000 | [diff] [blame] | 313 | return drawing_generic(dp, args, wdrawline); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | static object * |
| 317 | drawing_xorline(dp, args) |
| 318 | drawingobject *dp; |
| 319 | object *args; |
| 320 | { |
Guido van Rossum | bf10973 | 1991-03-06 13:14:12 +0000 | [diff] [blame] | 321 | return drawing_generic(dp, args, wxorline); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | static object * |
| 325 | drawing_circle(dp, args) |
| 326 | drawingobject *dp; |
| 327 | object *args; |
| 328 | { |
| 329 | int a[3]; |
| 330 | if (!getpointintarg(args, a)) |
| 331 | return NULL; |
| 332 | wdrawcircle(a[0], a[1], a[2]); |
| 333 | INCREF(None); |
| 334 | return None; |
| 335 | } |
Guido van Rossum | a2a181a | 1991-05-14 12:09:25 +0000 | [diff] [blame] | 336 | |
Guido van Rossum | 2720106 | 1991-04-16 08:43:03 +0000 | [diff] [blame] | 337 | static object * |
| 338 | drawing_fillcircle(dp, args) |
| 339 | drawingobject *dp; |
| 340 | object *args; |
| 341 | { |
| 342 | int a[3]; |
| 343 | if (!getpointintarg(args, a)) |
| 344 | return NULL; |
| 345 | wfillcircle(a[0], a[1], a[2]); |
| 346 | INCREF(None); |
| 347 | return None; |
| 348 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 349 | |
| 350 | static object * |
Guido van Rossum | a2a181a | 1991-05-14 12:09:25 +0000 | [diff] [blame] | 351 | drawing_xorcircle(dp, args) |
| 352 | drawingobject *dp; |
| 353 | object *args; |
| 354 | { |
| 355 | int a[3]; |
| 356 | if (!getpointintarg(args, a)) |
| 357 | return NULL; |
| 358 | wxorcircle(a[0], a[1], a[2]); |
| 359 | INCREF(None); |
| 360 | return None; |
| 361 | } |
| 362 | |
| 363 | static object * |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 364 | drawing_elarc(dp, args) |
| 365 | drawingobject *dp; |
| 366 | object *args; |
| 367 | { |
| 368 | int a[6]; |
| 369 | if (!get3pointarg(args, a)) |
| 370 | return NULL; |
| 371 | wdrawelarc(a[0], a[1], a[2], a[3], a[4], a[5]); |
| 372 | INCREF(None); |
| 373 | return None; |
| 374 | } |
| 375 | |
| 376 | static object * |
Guido van Rossum | 2720106 | 1991-04-16 08:43:03 +0000 | [diff] [blame] | 377 | drawing_fillelarc(dp, args) |
| 378 | drawingobject *dp; |
| 379 | object *args; |
| 380 | { |
| 381 | int a[6]; |
| 382 | if (!get3pointarg(args, a)) |
| 383 | return NULL; |
| 384 | wfillelarc(a[0], a[1], a[2], a[3], a[4], a[5]); |
| 385 | INCREF(None); |
| 386 | return None; |
| 387 | } |
| 388 | |
| 389 | static object * |
Guido van Rossum | a2a181a | 1991-05-14 12:09:25 +0000 | [diff] [blame] | 390 | drawing_xorelarc(dp, args) |
| 391 | drawingobject *dp; |
| 392 | object *args; |
| 393 | { |
| 394 | int a[6]; |
| 395 | if (!get3pointarg(args, a)) |
| 396 | return NULL; |
| 397 | wxorelarc(a[0], a[1], a[2], a[3], a[4], a[5]); |
| 398 | INCREF(None); |
| 399 | return None; |
| 400 | } |
| 401 | |
| 402 | static object * |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 403 | drawing_box(dp, args) |
| 404 | drawingobject *dp; |
| 405 | object *args; |
| 406 | { |
Guido van Rossum | bf10973 | 1991-03-06 13:14:12 +0000 | [diff] [blame] | 407 | return drawing_generic(dp, args, wdrawbox); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | static object * |
| 411 | drawing_erase(dp, args) |
| 412 | drawingobject *dp; |
| 413 | object *args; |
| 414 | { |
Guido van Rossum | bf10973 | 1991-03-06 13:14:12 +0000 | [diff] [blame] | 415 | return drawing_generic(dp, args, werase); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | static object * |
| 419 | drawing_paint(dp, args) |
| 420 | drawingobject *dp; |
| 421 | object *args; |
| 422 | { |
Guido van Rossum | bf10973 | 1991-03-06 13:14:12 +0000 | [diff] [blame] | 423 | return drawing_generic(dp, args, wpaint); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | static object * |
| 427 | drawing_invert(dp, args) |
| 428 | drawingobject *dp; |
| 429 | object *args; |
| 430 | { |
Guido van Rossum | bf10973 | 1991-03-06 13:14:12 +0000 | [diff] [blame] | 431 | return drawing_generic(dp, args, winvert); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 432 | } |
| 433 | |
Guido van Rossum | 2720106 | 1991-04-16 08:43:03 +0000 | [diff] [blame] | 434 | static POINT * |
| 435 | getpointsarray(v, psize) |
| 436 | object *v; |
| 437 | int *psize; |
| 438 | { |
| 439 | int n = -1; |
| 440 | object * (*getitem) PROTO((object *, int)); |
| 441 | int i; |
| 442 | POINT *points; |
| 443 | |
| 444 | if (v == NULL) |
| 445 | ; |
| 446 | else if (is_listobject(v)) { |
| 447 | n = getlistsize(v); |
| 448 | getitem = getlistitem; |
| 449 | } |
| 450 | else if (is_tupleobject(v)) { |
| 451 | n = gettuplesize(v); |
| 452 | getitem = gettupleitem; |
| 453 | } |
| 454 | |
| 455 | if (n <= 0) { |
| 456 | (void) err_badarg(); |
| 457 | return NULL; |
| 458 | } |
| 459 | |
| 460 | points = NEW(POINT, n); |
| 461 | if (points == NULL) { |
| 462 | (void) err_nomem(); |
| 463 | return NULL; |
| 464 | } |
| 465 | |
| 466 | for (i = 0; i < n; i++) { |
| 467 | object *w = (*getitem)(v, i); |
| 468 | int a[2]; |
| 469 | if (!getpointarg(w, a)) { |
| 470 | DEL(points); |
| 471 | return NULL; |
| 472 | } |
| 473 | points[i].h = a[0]; |
| 474 | points[i].v = a[1]; |
| 475 | } |
| 476 | |
| 477 | *psize = n; |
| 478 | return points; |
| 479 | } |
| 480 | |
| 481 | static object * |
| 482 | drawing_poly(dp, args) |
| 483 | drawingobject *dp; |
| 484 | object *args; |
| 485 | { |
| 486 | int n; |
| 487 | POINT *points = getpointsarray(args, &n); |
| 488 | if (points == NULL) |
| 489 | return NULL; |
| 490 | wdrawpoly(n, points); |
| 491 | DEL(points); |
| 492 | INCREF(None); |
| 493 | return None; |
| 494 | } |
| 495 | |
| 496 | static object * |
| 497 | drawing_fillpoly(dp, args) |
| 498 | drawingobject *dp; |
| 499 | object *args; |
| 500 | { |
| 501 | int n; |
| 502 | POINT *points = getpointsarray(args, &n); |
| 503 | if (points == NULL) |
| 504 | return NULL; |
| 505 | wfillpoly(n, points); |
| 506 | DEL(points); |
| 507 | INCREF(None); |
| 508 | return None; |
| 509 | } |
| 510 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 511 | static object * |
Guido van Rossum | a2a181a | 1991-05-14 12:09:25 +0000 | [diff] [blame] | 512 | drawing_xorpoly(dp, args) |
| 513 | drawingobject *dp; |
| 514 | object *args; |
| 515 | { |
| 516 | int n; |
| 517 | POINT *points = getpointsarray(args, &n); |
| 518 | if (points == NULL) |
| 519 | return NULL; |
| 520 | wxorpoly(n, points); |
| 521 | DEL(points); |
| 522 | INCREF(None); |
| 523 | return None; |
| 524 | } |
| 525 | |
| 526 | static object * |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 527 | drawing_cliprect(dp, args) |
| 528 | drawingobject *dp; |
| 529 | object *args; |
| 530 | { |
Guido van Rossum | bf10973 | 1991-03-06 13:14:12 +0000 | [diff] [blame] | 531 | return drawing_generic(dp, args, wcliprect); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | static object * |
| 535 | drawing_noclip(dp, args) |
| 536 | drawingobject *dp; |
| 537 | object *args; |
| 538 | { |
| 539 | if (!getnoarg(args)) |
| 540 | return NULL; |
| 541 | wnoclip(); |
| 542 | INCREF(None); |
| 543 | return None; |
| 544 | } |
| 545 | |
| 546 | static object * |
| 547 | drawing_shade(dp, args) |
| 548 | drawingobject *dp; |
| 549 | object *args; |
| 550 | { |
| 551 | int a[5]; |
| 552 | if (!getrectintarg(args, a)) |
| 553 | return NULL; |
| 554 | wshade(a[0], a[1], a[2], a[3], a[4]); |
| 555 | INCREF(None); |
| 556 | return None; |
| 557 | } |
| 558 | |
| 559 | static object * |
| 560 | drawing_text(dp, args) |
| 561 | drawingobject *dp; |
| 562 | object *args; |
| 563 | { |
| 564 | int a[2]; |
| 565 | object *s; |
| 566 | if (!getpointstrarg(args, a, &s)) |
| 567 | return NULL; |
| 568 | wdrawtext(a[0], a[1], getstringvalue(s), (int)getstringsize(s)); |
| 569 | INCREF(None); |
| 570 | return None; |
| 571 | } |
| 572 | |
| 573 | /* The following four are also used as stdwin functions */ |
| 574 | |
| 575 | static object * |
| 576 | drawing_lineheight(dp, args) |
| 577 | drawingobject *dp; |
| 578 | object *args; |
| 579 | { |
| 580 | if (!getnoarg(args)) |
| 581 | return NULL; |
| 582 | return newintobject((long)wlineheight()); |
| 583 | } |
| 584 | |
| 585 | static object * |
| 586 | drawing_baseline(dp, args) |
| 587 | drawingobject *dp; |
| 588 | object *args; |
| 589 | { |
| 590 | if (!getnoarg(args)) |
| 591 | return NULL; |
| 592 | return newintobject((long)wbaseline()); |
| 593 | } |
| 594 | |
| 595 | static object * |
| 596 | drawing_textwidth(dp, args) |
| 597 | drawingobject *dp; |
| 598 | object *args; |
| 599 | { |
| 600 | object *s; |
| 601 | if (!getstrarg(args, &s)) |
| 602 | return NULL; |
| 603 | return newintobject( |
| 604 | (long)wtextwidth(getstringvalue(s), (int)getstringsize(s))); |
| 605 | } |
| 606 | |
| 607 | static object * |
| 608 | drawing_textbreak(dp, args) |
| 609 | drawingobject *dp; |
| 610 | object *args; |
| 611 | { |
| 612 | object *s; |
| 613 | int a; |
| 614 | if (!getstrintarg(args, &s, &a)) |
| 615 | return NULL; |
| 616 | return newintobject( |
| 617 | (long)wtextbreak(getstringvalue(s), (int)getstringsize(s), a)); |
| 618 | } |
| 619 | |
Guido van Rossum | 0c2290b | 1991-04-03 19:12:14 +0000 | [diff] [blame] | 620 | static object * |
| 621 | drawing_setfont(self, args) |
| 622 | drawingobject *self; |
| 623 | object *args; |
| 624 | { |
Guido van Rossum | 50429a1 | 1991-04-04 15:24:07 +0000 | [diff] [blame] | 625 | object *font, *style; |
| 626 | int size; |
| 627 | if (args == NULL) { |
| 628 | err_badarg(); |
Guido van Rossum | 0c2290b | 1991-04-03 19:12:14 +0000 | [diff] [blame] | 629 | return NULL; |
Guido van Rossum | 50429a1 | 1991-04-04 15:24:07 +0000 | [diff] [blame] | 630 | } |
| 631 | if (is_stringobject(args)) { |
| 632 | font = args; |
| 633 | style = NULL; |
| 634 | size = 0; |
| 635 | } |
| 636 | else if (is_tupleobject(args)) { |
| 637 | int n = gettuplesize(args); |
| 638 | if (n == 2) { |
| 639 | if (!getstrintarg(args, &font, &size)) |
| 640 | return NULL; |
| 641 | style = NULL; |
| 642 | } |
| 643 | else if (!getstrstrintarg(args, &font, &style, &size)) |
| 644 | return NULL; |
| 645 | } |
| 646 | else { |
| 647 | err_badarg(); |
| 648 | return NULL; |
| 649 | } |
Guido van Rossum | 246b9d8 | 1991-06-03 10:55:14 +0000 | [diff] [blame] | 650 | wsetfont(getstringvalue(font)); |
Guido van Rossum | 50429a1 | 1991-04-04 15:24:07 +0000 | [diff] [blame] | 651 | if (style != NULL) { |
| 652 | switch (*getstringvalue(style)) { |
| 653 | case 'b': |
| 654 | wsetbold(); |
| 655 | break; |
| 656 | case 'i': |
| 657 | wsetitalic(); |
| 658 | break; |
| 659 | case 'o': |
| 660 | wsetbolditalic(); |
| 661 | break; |
| 662 | case 'u': |
| 663 | wsetunderline(); |
| 664 | break; |
| 665 | default: |
| 666 | wsetplain(); |
| 667 | break; |
| 668 | } |
| 669 | } |
| 670 | if (size != 0) |
| 671 | wsetsize(size); |
Guido van Rossum | 0c2290b | 1991-04-03 19:12:14 +0000 | [diff] [blame] | 672 | INCREF(None); |
| 673 | return None; |
| 674 | } |
| 675 | |
| 676 | static object * |
| 677 | drawing_getbgcolor(self, args) |
| 678 | object *self; |
| 679 | object *args; |
| 680 | { |
| 681 | if (!getnoarg(args)) |
| 682 | return NULL; |
| 683 | return newintobject((long)wgetbgcolor()); |
| 684 | } |
| 685 | |
| 686 | static object * |
| 687 | drawing_getfgcolor(self, args) |
| 688 | object *self; |
| 689 | object *args; |
| 690 | { |
| 691 | if (!getnoarg(args)) |
| 692 | return NULL; |
| 693 | return newintobject((long)wgetfgcolor()); |
| 694 | } |
| 695 | |
| 696 | static object * |
| 697 | drawing_setbgcolor(self, args) |
| 698 | object *self; |
| 699 | object *args; |
| 700 | { |
| 701 | long color; |
| 702 | if (!getlongarg(args, &color)) |
| 703 | return NULL; |
| 704 | wsetbgcolor((COLOR)color); |
| 705 | INCREF(None); |
| 706 | return None; |
| 707 | } |
| 708 | |
| 709 | static object * |
| 710 | drawing_setfgcolor(self, args) |
| 711 | object *self; |
| 712 | object *args; |
| 713 | { |
| 714 | long color; |
| 715 | if (!getlongarg(args, &color)) |
| 716 | return NULL; |
| 717 | wsetfgcolor((COLOR)color); |
| 718 | INCREF(None); |
| 719 | return None; |
| 720 | } |
| 721 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 722 | static struct methodlist drawing_methods[] = { |
| 723 | {"box", drawing_box}, |
| 724 | {"circle", drawing_circle}, |
| 725 | {"cliprect", drawing_cliprect}, |
Guido van Rossum | 3c28474 | 1991-11-27 14:54:54 +0000 | [diff] [blame] | 726 | {"close", drawing_close}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 727 | {"elarc", drawing_elarc}, |
Guido van Rossum | 3c28474 | 1991-11-27 14:54:54 +0000 | [diff] [blame] | 728 | {"enddrawing", drawing_close}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 729 | {"erase", drawing_erase}, |
Guido van Rossum | 2720106 | 1991-04-16 08:43:03 +0000 | [diff] [blame] | 730 | {"fillcircle", drawing_fillcircle}, |
Guido van Rossum | a2a181a | 1991-05-14 12:09:25 +0000 | [diff] [blame] | 731 | {"fillelarc", drawing_fillelarc}, |
Guido van Rossum | 2720106 | 1991-04-16 08:43:03 +0000 | [diff] [blame] | 732 | {"fillpoly", drawing_fillpoly}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 733 | {"invert", drawing_invert}, |
| 734 | {"line", drawing_line}, |
| 735 | {"noclip", drawing_noclip}, |
| 736 | {"paint", drawing_paint}, |
Guido van Rossum | 2720106 | 1991-04-16 08:43:03 +0000 | [diff] [blame] | 737 | {"poly", drawing_poly}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 738 | {"shade", drawing_shade}, |
| 739 | {"text", drawing_text}, |
Guido van Rossum | a2a181a | 1991-05-14 12:09:25 +0000 | [diff] [blame] | 740 | {"xorcircle", drawing_xorcircle}, |
| 741 | {"xorelarc", drawing_xorelarc}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 742 | {"xorline", drawing_xorline}, |
Guido van Rossum | a2a181a | 1991-05-14 12:09:25 +0000 | [diff] [blame] | 743 | {"xorpoly", drawing_xorpoly}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 744 | |
| 745 | /* Text measuring methods: */ |
| 746 | {"baseline", drawing_baseline}, |
| 747 | {"lineheight", drawing_lineheight}, |
| 748 | {"textbreak", drawing_textbreak}, |
| 749 | {"textwidth", drawing_textwidth}, |
Guido van Rossum | 0c2290b | 1991-04-03 19:12:14 +0000 | [diff] [blame] | 750 | |
| 751 | /* Font setting methods: */ |
| 752 | {"setfont", drawing_setfont}, |
| 753 | |
| 754 | /* Color methods: */ |
| 755 | {"getbgcolor", drawing_getbgcolor}, |
| 756 | {"getfgcolor", drawing_getfgcolor}, |
| 757 | {"setbgcolor", drawing_setbgcolor}, |
| 758 | {"setfgcolor", drawing_setfgcolor}, |
| 759 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 760 | {NULL, NULL} /* sentinel */ |
| 761 | }; |
| 762 | |
| 763 | static object * |
Guido van Rossum | 77b4604 | 1992-01-14 18:41:24 +0000 | [diff] [blame^] | 764 | drawing_getattr(dp, name) |
| 765 | drawingobject *dp; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 766 | char *name; |
| 767 | { |
Guido van Rossum | 77b4604 | 1992-01-14 18:41:24 +0000 | [diff] [blame^] | 768 | if (dp->d_ref == NULL) { |
| 769 | err_setstr(StdwinError, "drawing object already closed"); |
| 770 | return NULL; |
| 771 | } |
| 772 | return findmethod(drawing_methods, (object *)dp, name); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 773 | } |
| 774 | |
Guido van Rossum | 541c8c0 | 1991-05-05 20:13:41 +0000 | [diff] [blame] | 775 | typeobject Drawingtype = { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 776 | OB_HEAD_INIT(&Typetype) |
| 777 | 0, /*ob_size*/ |
| 778 | "drawing", /*tp_name*/ |
| 779 | sizeof(drawingobject), /*tp_size*/ |
| 780 | 0, /*tp_itemsize*/ |
| 781 | /* methods */ |
| 782 | drawing_dealloc, /*tp_dealloc*/ |
| 783 | 0, /*tp_print*/ |
| 784 | drawing_getattr, /*tp_getattr*/ |
| 785 | 0, /*tp_setattr*/ |
| 786 | 0, /*tp_compare*/ |
| 787 | 0, /*tp_repr*/ |
| 788 | }; |
| 789 | |
| 790 | |
| 791 | /* Text(edit) objects */ |
| 792 | |
| 793 | typedef struct { |
| 794 | OB_HEAD |
| 795 | TEXTEDIT *t_text; |
| 796 | windowobject *t_ref; |
| 797 | object *t_attr; /* Attributes dictionary */ |
| 798 | } textobject; |
| 799 | |
| 800 | extern typeobject Texttype; /* Really static, forward */ |
| 801 | |
| 802 | static textobject * |
| 803 | newtextobject(wp, left, top, right, bottom) |
| 804 | windowobject *wp; |
| 805 | int left, top, right, bottom; |
| 806 | { |
| 807 | textobject *tp; |
| 808 | tp = NEWOBJ(textobject, &Texttype); |
| 809 | if (tp == NULL) |
| 810 | return NULL; |
| 811 | tp->t_attr = NULL; |
| 812 | INCREF(wp); |
| 813 | tp->t_ref = wp; |
| 814 | tp->t_text = tecreate(wp->w_win, left, top, right, bottom); |
| 815 | if (tp->t_text == NULL) { |
| 816 | DECREF(tp); |
| 817 | return (textobject *) err_nomem(); |
| 818 | } |
| 819 | return tp; |
| 820 | } |
| 821 | |
| 822 | /* Text(edit) methods */ |
| 823 | |
| 824 | static void |
| 825 | text_dealloc(tp) |
| 826 | textobject *tp; |
| 827 | { |
| 828 | if (tp->t_text != NULL) |
| 829 | tefree(tp->t_text); |
Guido van Rossum | 3c28474 | 1991-11-27 14:54:54 +0000 | [diff] [blame] | 830 | XDECREF(tp->t_attr); |
| 831 | XDECREF(tp->t_ref); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 832 | DEL(tp); |
| 833 | } |
| 834 | |
| 835 | static object * |
Guido van Rossum | 3c28474 | 1991-11-27 14:54:54 +0000 | [diff] [blame] | 836 | text_close(tp, args) |
| 837 | textobject *tp; |
| 838 | object *args; |
| 839 | { |
| 840 | if (tp->t_text != NULL) { |
| 841 | tefree(tp->t_text); |
| 842 | tp->t_text = NULL; |
| 843 | } |
| 844 | if (tp->t_attr != NULL) { |
| 845 | DECREF(tp->t_attr); |
| 846 | tp->t_attr = NULL; |
| 847 | } |
| 848 | if (tp->t_ref != NULL) { |
| 849 | DECREF(tp->t_ref); |
| 850 | tp->t_ref = NULL; |
| 851 | } |
| 852 | INCREF(None); |
| 853 | return None; |
| 854 | } |
| 855 | |
| 856 | static object * |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 857 | text_arrow(self, args) |
| 858 | textobject *self; |
| 859 | object *args; |
| 860 | { |
| 861 | int code; |
| 862 | if (!getintarg(args, &code)) |
| 863 | return NULL; |
| 864 | tearrow(self->t_text, code); |
| 865 | INCREF(None); |
| 866 | return None; |
| 867 | } |
| 868 | |
| 869 | static object * |
| 870 | text_draw(self, args) |
| 871 | textobject *self; |
| 872 | object *args; |
| 873 | { |
| 874 | register TEXTEDIT *tp = self->t_text; |
| 875 | int a[4]; |
| 876 | int left, top, right, bottom; |
| 877 | if (!getrectarg(args, a)) |
| 878 | return NULL; |
| 879 | if (Drawing != NULL) { |
Guido van Rossum | 87e7ea7 | 1991-12-10 14:00:03 +0000 | [diff] [blame] | 880 | err_setstr(StdwinError, "already drawing"); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 881 | return NULL; |
| 882 | } |
| 883 | /* Clip to text area and ignore if area is empty */ |
| 884 | left = tegetleft(tp); |
| 885 | top = tegettop(tp); |
| 886 | right = tegetright(tp); |
| 887 | bottom = tegetbottom(tp); |
| 888 | if (a[0] < left) a[0] = left; |
| 889 | if (a[1] < top) a[1] = top; |
| 890 | if (a[2] > right) a[2] = right; |
| 891 | if (a[3] > bottom) a[3] = bottom; |
| 892 | if (a[0] < a[2] && a[1] < a[3]) { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 893 | wbegindrawing(self->t_ref->w_win); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 894 | tedrawnew(tp, a[0], a[1], a[2], a[3]); |
| 895 | wenddrawing(self->t_ref->w_win); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 896 | } |
| 897 | INCREF(None); |
| 898 | return None; |
| 899 | } |
| 900 | |
| 901 | static object * |
| 902 | text_event(self, args) |
| 903 | textobject *self; |
| 904 | object *args; |
| 905 | { |
| 906 | register TEXTEDIT *tp = self->t_text; |
| 907 | EVENT e; |
| 908 | if (!geteventarg(args, &e)) |
| 909 | return NULL; |
| 910 | if (e.type == WE_MOUSE_DOWN) { |
Guido van Rossum | 33f1770 | 1991-02-13 23:19:39 +0000 | [diff] [blame] | 911 | /* Cheat at the margins */ |
| 912 | int width, height; |
| 913 | wgetdocsize(e.window, &width, &height); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 914 | if (e.u.where.h < 0 && tegetleft(tp) == 0) |
| 915 | e.u.where.h = 0; |
Guido van Rossum | 33f1770 | 1991-02-13 23:19:39 +0000 | [diff] [blame] | 916 | else if (e.u.where.h > width && tegetright(tp) == width) |
| 917 | e.u.where.h = width; |
| 918 | if (e.u.where.v < 0 && tegettop(tp) == 0) |
| 919 | e.u.where.v = 0; |
| 920 | else if (e.u.where.v > height && tegetright(tp) == height) |
| 921 | e.u.where.v = height; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 922 | } |
| 923 | return newintobject((long) teevent(tp, &e)); |
| 924 | } |
| 925 | |
| 926 | static object * |
| 927 | text_getfocus(self, args) |
| 928 | textobject *self; |
| 929 | object *args; |
| 930 | { |
| 931 | if (!getnoarg(args)) |
| 932 | return NULL; |
| 933 | return makepoint(tegetfoc1(self->t_text), tegetfoc2(self->t_text)); |
| 934 | } |
| 935 | |
| 936 | static object * |
| 937 | text_getfocustext(self, args) |
| 938 | textobject *self; |
| 939 | object *args; |
| 940 | { |
| 941 | int f1, f2; |
| 942 | char *text; |
| 943 | if (!getnoarg(args)) |
| 944 | return NULL; |
| 945 | f1 = tegetfoc1(self->t_text); |
| 946 | f2 = tegetfoc2(self->t_text); |
| 947 | text = tegettext(self->t_text); |
| 948 | return newsizedstringobject(text + f1, f2-f1); |
| 949 | } |
| 950 | |
| 951 | static object * |
| 952 | text_getrect(self, args) |
| 953 | textobject *self; |
| 954 | object *args; |
| 955 | { |
| 956 | if (!getnoarg(args)) |
| 957 | return NULL; |
| 958 | return makerect(tegetleft(self->t_text), |
| 959 | tegettop(self->t_text), |
| 960 | tegetright(self->t_text), |
| 961 | tegetbottom(self->t_text)); |
| 962 | } |
| 963 | |
| 964 | static object * |
| 965 | text_gettext(self, args) |
| 966 | textobject *self; |
| 967 | object *args; |
| 968 | { |
| 969 | if (!getnoarg(args)) |
| 970 | return NULL; |
| 971 | return newsizedstringobject(tegettext(self->t_text), |
| 972 | tegetlen(self->t_text)); |
| 973 | } |
| 974 | |
| 975 | static object * |
| 976 | text_move(self, args) |
| 977 | textobject *self; |
| 978 | object *args; |
| 979 | { |
| 980 | int a[4]; |
| 981 | if (!getrectarg(args, a)) |
| 982 | return NULL; |
| 983 | temovenew(self->t_text, a[0], a[1], a[2], a[3]); |
| 984 | INCREF(None); |
| 985 | return None; |
| 986 | } |
| 987 | |
| 988 | static object * |
Guido van Rossum | 4b9cf8e | 1991-05-28 21:57:04 +0000 | [diff] [blame] | 989 | text_replace(self, args) |
| 990 | textobject *self; |
| 991 | object *args; |
| 992 | { |
| 993 | object *text; |
| 994 | if (!getstrarg(args, &text)) |
| 995 | return NULL; |
| 996 | tereplace(self->t_text, getstringvalue(text)); |
| 997 | INCREF(None); |
| 998 | return None; |
| 999 | } |
| 1000 | |
| 1001 | static object * |
| 1002 | text_setactive(self, args) |
| 1003 | textobject *self; |
| 1004 | object *args; |
| 1005 | { |
| 1006 | int flag; |
| 1007 | if (!getintarg(args, &flag)) |
| 1008 | return NULL; |
| 1009 | tesetactive(self->t_text, flag); |
| 1010 | INCREF(None); |
| 1011 | return None; |
| 1012 | } |
| 1013 | |
| 1014 | static object * |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1015 | text_setfocus(self, args) |
| 1016 | textobject *self; |
| 1017 | object *args; |
| 1018 | { |
| 1019 | int a[2]; |
| 1020 | if (!getpointarg(args, a)) |
| 1021 | return NULL; |
| 1022 | tesetfocus(self->t_text, a[0], a[1]); |
| 1023 | INCREF(None); |
| 1024 | return None; |
| 1025 | } |
| 1026 | |
| 1027 | static object * |
Guido van Rossum | 541c8c0 | 1991-05-05 20:13:41 +0000 | [diff] [blame] | 1028 | text_settext(self, args) |
| 1029 | textobject *self; |
| 1030 | object *args; |
| 1031 | { |
| 1032 | object *text; |
| 1033 | char *buf; |
| 1034 | int size; |
| 1035 | if (!getstrarg(args, &text)) |
| 1036 | return NULL; |
| 1037 | size = getstringsize(text); |
| 1038 | if ((buf = NEW(char, size)) == NULL) { |
Guido van Rossum | 87e7ea7 | 1991-12-10 14:00:03 +0000 | [diff] [blame] | 1039 | return err_nomem(); |
Guido van Rossum | 541c8c0 | 1991-05-05 20:13:41 +0000 | [diff] [blame] | 1040 | } |
| 1041 | memcpy(buf, getstringvalue(text), size); |
| 1042 | tesetbuf(self->t_text, buf, size); /* Becomes owner of buffer */ |
| 1043 | INCREF(None); |
| 1044 | return None; |
| 1045 | } |
| 1046 | |
| 1047 | static object * |
Guido van Rossum | 4b9cf8e | 1991-05-28 21:57:04 +0000 | [diff] [blame] | 1048 | text_setview(self, args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1049 | textobject *self; |
| 1050 | object *args; |
| 1051 | { |
Guido van Rossum | 4b9cf8e | 1991-05-28 21:57:04 +0000 | [diff] [blame] | 1052 | int a[4]; |
| 1053 | if (args == None) |
| 1054 | tenoview(self->t_text); |
| 1055 | else { |
| 1056 | if (!getrectarg(args, a)) |
| 1057 | return NULL; |
| 1058 | tesetview(self->t_text, a[0], a[1], a[2], a[3]); |
| 1059 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1060 | INCREF(None); |
| 1061 | return None; |
| 1062 | } |
| 1063 | |
| 1064 | static struct methodlist text_methods[] = { |
Guido van Rossum | 3c28474 | 1991-11-27 14:54:54 +0000 | [diff] [blame] | 1065 | {"arrow", text_arrow}, |
| 1066 | {"close", text_close}, |
| 1067 | {"draw", text_draw}, |
| 1068 | {"event", text_event}, |
| 1069 | {"getfocus", text_getfocus}, |
Guido van Rossum | 77b4604 | 1992-01-14 18:41:24 +0000 | [diff] [blame^] | 1070 | {"getfocustext",text_getfocustext}, |
Guido van Rossum | 3c28474 | 1991-11-27 14:54:54 +0000 | [diff] [blame] | 1071 | {"getrect", text_getrect}, |
| 1072 | {"gettext", text_gettext}, |
Guido van Rossum | 77b4604 | 1992-01-14 18:41:24 +0000 | [diff] [blame^] | 1073 | {"move", text_move}, |
Guido van Rossum | 3c28474 | 1991-11-27 14:54:54 +0000 | [diff] [blame] | 1074 | {"replace", text_replace}, |
| 1075 | {"setactive", text_setactive}, |
| 1076 | {"setfocus", text_setfocus}, |
| 1077 | {"settext", text_settext}, |
| 1078 | {"setview", text_setview}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1079 | {NULL, NULL} /* sentinel */ |
| 1080 | }; |
| 1081 | |
| 1082 | static object * |
| 1083 | text_getattr(tp, name) |
| 1084 | textobject *tp; |
| 1085 | char *name; |
| 1086 | { |
Guido van Rossum | 85f5076 | 1991-10-20 20:22:50 +0000 | [diff] [blame] | 1087 | object *v = NULL; |
Guido van Rossum | 77b4604 | 1992-01-14 18:41:24 +0000 | [diff] [blame^] | 1088 | if (tp->t_ref == NULL) { |
| 1089 | err_setstr(StdwinError, "text object already closed"); |
| 1090 | return NULL; |
| 1091 | } |
Guido van Rossum | 85f5076 | 1991-10-20 20:22:50 +0000 | [diff] [blame] | 1092 | if (strcmp(name, "__dict__") == 0) { |
| 1093 | v = tp->t_attr; |
| 1094 | if (v == NULL) |
| 1095 | v = None; |
| 1096 | } |
| 1097 | else if (tp->t_attr != NULL) { |
| 1098 | v = dictlookup(tp->t_attr, name); |
| 1099 | } |
| 1100 | if (v != NULL) { |
| 1101 | INCREF(v); |
| 1102 | return v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1103 | } |
| 1104 | return findmethod(text_methods, (object *)tp, name); |
| 1105 | } |
| 1106 | |
| 1107 | static int |
| 1108 | text_setattr(tp, name, v) |
| 1109 | textobject *tp; |
| 1110 | char *name; |
| 1111 | object *v; |
| 1112 | { |
| 1113 | if (tp->t_attr == NULL) { |
| 1114 | tp->t_attr = newdictobject(); |
| 1115 | if (tp->t_attr == NULL) |
| 1116 | return -1; |
| 1117 | } |
| 1118 | if (v == NULL) |
| 1119 | return dictremove(tp->t_attr, name); |
| 1120 | else |
| 1121 | return dictinsert(tp->t_attr, name, v); |
| 1122 | } |
| 1123 | |
Guido van Rossum | 541c8c0 | 1991-05-05 20:13:41 +0000 | [diff] [blame] | 1124 | typeobject Texttype = { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1125 | OB_HEAD_INIT(&Typetype) |
| 1126 | 0, /*ob_size*/ |
| 1127 | "textedit", /*tp_name*/ |
| 1128 | sizeof(textobject), /*tp_size*/ |
| 1129 | 0, /*tp_itemsize*/ |
| 1130 | /* methods */ |
| 1131 | text_dealloc, /*tp_dealloc*/ |
| 1132 | 0, /*tp_print*/ |
| 1133 | text_getattr, /*tp_getattr*/ |
| 1134 | text_setattr, /*tp_setattr*/ |
| 1135 | 0, /*tp_compare*/ |
| 1136 | 0, /*tp_repr*/ |
| 1137 | }; |
| 1138 | |
| 1139 | |
| 1140 | /* Menu objects */ |
| 1141 | |
Guido van Rossum | 2d14e21 | 1991-02-19 12:26:49 +0000 | [diff] [blame] | 1142 | #define IDOFFSET 10 /* Menu IDs we use start here */ |
Guido van Rossum | 2720106 | 1991-04-16 08:43:03 +0000 | [diff] [blame] | 1143 | #define MAXNMENU 200 /* Max #menus we allow */ |
Guido van Rossum | 2d14e21 | 1991-02-19 12:26:49 +0000 | [diff] [blame] | 1144 | static menuobject *menulist[MAXNMENU]; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1145 | |
| 1146 | static menuobject * |
| 1147 | newmenuobject(title) |
| 1148 | object *title; |
| 1149 | { |
| 1150 | int id; |
| 1151 | MENU *menu; |
| 1152 | menuobject *mp; |
Guido van Rossum | 2d14e21 | 1991-02-19 12:26:49 +0000 | [diff] [blame] | 1153 | for (id = 0; id < MAXNMENU; id++) { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1154 | if (menulist[id] == NULL) |
| 1155 | break; |
| 1156 | } |
Guido van Rossum | 2720106 | 1991-04-16 08:43:03 +0000 | [diff] [blame] | 1157 | if (id >= MAXNMENU) { |
Guido van Rossum | 87e7ea7 | 1991-12-10 14:00:03 +0000 | [diff] [blame] | 1158 | err_setstr(StdwinError, "creating too many menus"); |
Guido van Rossum | 2720106 | 1991-04-16 08:43:03 +0000 | [diff] [blame] | 1159 | return NULL; |
| 1160 | } |
Guido van Rossum | 2d14e21 | 1991-02-19 12:26:49 +0000 | [diff] [blame] | 1161 | menu = wmenucreate(id + IDOFFSET, getstringvalue(title)); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1162 | if (menu == NULL) |
| 1163 | return (menuobject *) err_nomem(); |
| 1164 | mp = NEWOBJ(menuobject, &Menutype); |
| 1165 | if (mp != NULL) { |
| 1166 | mp->m_menu = menu; |
Guido van Rossum | 2d14e21 | 1991-02-19 12:26:49 +0000 | [diff] [blame] | 1167 | mp->m_id = id + IDOFFSET; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1168 | mp->m_attr = NULL; |
| 1169 | menulist[id] = mp; |
| 1170 | } |
| 1171 | else |
| 1172 | wmenudelete(menu); |
| 1173 | return mp; |
| 1174 | } |
| 1175 | |
| 1176 | /* Menu methods */ |
| 1177 | |
| 1178 | static void |
| 1179 | menu_dealloc(mp) |
| 1180 | menuobject *mp; |
| 1181 | { |
| 1182 | |
Guido van Rossum | 2d14e21 | 1991-02-19 12:26:49 +0000 | [diff] [blame] | 1183 | int id = mp->m_id - IDOFFSET; |
| 1184 | if (id >= 0 && id < MAXNMENU && menulist[id] == mp) { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1185 | menulist[id] = NULL; |
| 1186 | } |
Guido van Rossum | 77b4604 | 1992-01-14 18:41:24 +0000 | [diff] [blame^] | 1187 | if (mp->m_menu != NULL) |
| 1188 | wmenudelete(mp->m_menu); |
| 1189 | XDECREF(mp->m_attr); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1190 | DEL(mp); |
| 1191 | } |
| 1192 | |
| 1193 | static object * |
Guido van Rossum | 77b4604 | 1992-01-14 18:41:24 +0000 | [diff] [blame^] | 1194 | menu_close(mp, args) |
| 1195 | menuobject *mp; |
| 1196 | object *args; |
| 1197 | { |
| 1198 | int id = mp->m_id - IDOFFSET; |
| 1199 | if (id >= 0 && id < MAXNMENU && menulist[id] == mp) { |
| 1200 | menulist[id] = NULL; |
| 1201 | } |
| 1202 | mp->m_id = -1; |
| 1203 | if (mp->m_menu != NULL) |
| 1204 | wmenudelete(mp->m_menu); |
| 1205 | mp->m_menu = NULL; |
| 1206 | XDECREF(mp->m_attr); |
| 1207 | mp->m_attr = NULL; |
| 1208 | INCREF(None); |
| 1209 | return None; |
| 1210 | } |
| 1211 | |
| 1212 | static object * |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1213 | menu_additem(self, args) |
| 1214 | menuobject *self; |
| 1215 | object *args; |
| 1216 | { |
| 1217 | object *text; |
| 1218 | int shortcut; |
| 1219 | if (is_tupleobject(args)) { |
| 1220 | object *v; |
| 1221 | if (!getstrstrarg(args, &text, &v)) |
| 1222 | return NULL; |
| 1223 | if (getstringsize(v) != 1) { |
| 1224 | err_badarg(); |
| 1225 | return NULL; |
| 1226 | } |
| 1227 | shortcut = *getstringvalue(v) & 0xff; |
| 1228 | } |
| 1229 | else { |
| 1230 | if (!getstrarg(args, &text)) |
| 1231 | return NULL; |
| 1232 | shortcut = -1; |
| 1233 | } |
| 1234 | wmenuadditem(self->m_menu, getstringvalue(text), shortcut); |
| 1235 | INCREF(None); |
| 1236 | return None; |
| 1237 | } |
| 1238 | |
| 1239 | static object * |
| 1240 | menu_setitem(self, args) |
| 1241 | menuobject *self; |
| 1242 | object *args; |
| 1243 | { |
| 1244 | int index; |
| 1245 | object *text; |
| 1246 | if (!getintstrarg(args, &index, &text)) |
| 1247 | return NULL; |
| 1248 | wmenusetitem(self->m_menu, index, getstringvalue(text)); |
| 1249 | INCREF(None); |
| 1250 | return None; |
| 1251 | } |
| 1252 | |
| 1253 | static object * |
| 1254 | menu_enable(self, args) |
| 1255 | menuobject *self; |
| 1256 | object *args; |
| 1257 | { |
| 1258 | int index; |
| 1259 | int flag; |
| 1260 | if (!getintintarg(args, &index, &flag)) |
| 1261 | return NULL; |
| 1262 | wmenuenable(self->m_menu, index, flag); |
| 1263 | INCREF(None); |
| 1264 | return None; |
| 1265 | } |
| 1266 | |
| 1267 | static object * |
| 1268 | menu_check(self, args) |
| 1269 | menuobject *self; |
| 1270 | object *args; |
| 1271 | { |
| 1272 | int index; |
| 1273 | int flag; |
| 1274 | if (!getintintarg(args, &index, &flag)) |
| 1275 | return NULL; |
| 1276 | wmenucheck(self->m_menu, index, flag); |
| 1277 | INCREF(None); |
| 1278 | return None; |
| 1279 | } |
| 1280 | |
| 1281 | static struct methodlist menu_methods[] = { |
Guido van Rossum | 3c28474 | 1991-11-27 14:54:54 +0000 | [diff] [blame] | 1282 | {"additem", menu_additem}, |
| 1283 | {"setitem", menu_setitem}, |
| 1284 | {"enable", menu_enable}, |
| 1285 | {"check", menu_check}, |
Guido van Rossum | 77b4604 | 1992-01-14 18:41:24 +0000 | [diff] [blame^] | 1286 | {"close", menu_close}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1287 | {NULL, NULL} /* sentinel */ |
| 1288 | }; |
| 1289 | |
| 1290 | static object * |
| 1291 | menu_getattr(mp, name) |
| 1292 | menuobject *mp; |
| 1293 | char *name; |
| 1294 | { |
Guido van Rossum | 85f5076 | 1991-10-20 20:22:50 +0000 | [diff] [blame] | 1295 | object *v = NULL; |
Guido van Rossum | 77b4604 | 1992-01-14 18:41:24 +0000 | [diff] [blame^] | 1296 | if (mp->m_menu == NULL) { |
| 1297 | err_setstr(StdwinError, "menu object already closed"); |
| 1298 | return NULL; |
| 1299 | } |
Guido van Rossum | 85f5076 | 1991-10-20 20:22:50 +0000 | [diff] [blame] | 1300 | if (strcmp(name, "__dict__") == 0) { |
| 1301 | v = mp->m_attr; |
| 1302 | if (v == NULL) |
| 1303 | v = None; |
| 1304 | } |
| 1305 | else if (mp->m_attr != NULL) { |
| 1306 | v = dictlookup(mp->m_attr, name); |
| 1307 | } |
| 1308 | if (v != NULL) { |
| 1309 | INCREF(v); |
| 1310 | return v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1311 | } |
| 1312 | return findmethod(menu_methods, (object *)mp, name); |
| 1313 | } |
| 1314 | |
| 1315 | static int |
| 1316 | menu_setattr(mp, name, v) |
| 1317 | menuobject *mp; |
| 1318 | char *name; |
| 1319 | object *v; |
| 1320 | { |
| 1321 | if (mp->m_attr == NULL) { |
| 1322 | mp->m_attr = newdictobject(); |
| 1323 | if (mp->m_attr == NULL) |
| 1324 | return -1; |
| 1325 | } |
| 1326 | if (v == NULL) |
| 1327 | return dictremove(mp->m_attr, name); |
| 1328 | else |
| 1329 | return dictinsert(mp->m_attr, name, v); |
| 1330 | } |
| 1331 | |
Guido van Rossum | 541c8c0 | 1991-05-05 20:13:41 +0000 | [diff] [blame] | 1332 | typeobject Menutype = { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1333 | OB_HEAD_INIT(&Typetype) |
| 1334 | 0, /*ob_size*/ |
| 1335 | "menu", /*tp_name*/ |
| 1336 | sizeof(menuobject), /*tp_size*/ |
| 1337 | 0, /*tp_itemsize*/ |
| 1338 | /* methods */ |
| 1339 | menu_dealloc, /*tp_dealloc*/ |
| 1340 | 0, /*tp_print*/ |
| 1341 | menu_getattr, /*tp_getattr*/ |
| 1342 | menu_setattr, /*tp_setattr*/ |
| 1343 | 0, /*tp_compare*/ |
| 1344 | 0, /*tp_repr*/ |
| 1345 | }; |
| 1346 | |
| 1347 | |
| 1348 | /* Windows */ |
| 1349 | |
| 1350 | #define MAXNWIN 50 |
| 1351 | static windowobject *windowlist[MAXNWIN]; |
| 1352 | |
| 1353 | /* Window methods */ |
| 1354 | |
| 1355 | static void |
| 1356 | window_dealloc(wp) |
| 1357 | windowobject *wp; |
| 1358 | { |
| 1359 | if (wp->w_win != NULL) { |
| 1360 | int tag = wgettag(wp->w_win); |
| 1361 | if (tag >= 0 && tag < MAXNWIN) |
| 1362 | windowlist[tag] = NULL; |
| 1363 | else |
| 1364 | fprintf(stderr, "XXX help! tag %d in window_dealloc\n", |
| 1365 | tag); |
| 1366 | wclose(wp->w_win); |
| 1367 | } |
| 1368 | DECREF(wp->w_title); |
| 1369 | if (wp->w_attr != NULL) |
| 1370 | DECREF(wp->w_attr); |
| 1371 | free((char *)wp); |
| 1372 | } |
| 1373 | |
Guido van Rossum | d783a46 | 1991-06-07 22:35:42 +0000 | [diff] [blame] | 1374 | static int |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1375 | window_print(wp, fp, flags) |
| 1376 | windowobject *wp; |
| 1377 | FILE *fp; |
| 1378 | int flags; |
| 1379 | { |
Guido van Rossum | 3c28474 | 1991-11-27 14:54:54 +0000 | [diff] [blame] | 1380 | fprintf(fp, "<%s window titled '%s'>", |
| 1381 | wp->w_win == NULL ? "closed" : "open", |
| 1382 | getstringvalue(wp->w_title)); |
Guido van Rossum | d783a46 | 1991-06-07 22:35:42 +0000 | [diff] [blame] | 1383 | return 0; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1384 | } |
| 1385 | |
| 1386 | static object * |
Guido van Rossum | 3c28474 | 1991-11-27 14:54:54 +0000 | [diff] [blame] | 1387 | window_close(wp, args) |
| 1388 | windowobject *wp; |
| 1389 | object *args; |
| 1390 | { |
| 1391 | if (wp->w_win != NULL) { |
| 1392 | int tag = wgettag(wp->w_win); |
| 1393 | if (tag >= 0 && tag < MAXNWIN) |
| 1394 | windowlist[tag] = NULL; |
| 1395 | wclose(wp->w_win); |
| 1396 | wp->w_win = NULL; |
| 1397 | } |
| 1398 | INCREF(None); |
| 1399 | return None; |
| 1400 | } |
| 1401 | |
| 1402 | static object * |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1403 | window_begindrawing(wp, args) |
| 1404 | windowobject *wp; |
| 1405 | object *args; |
| 1406 | { |
| 1407 | drawingobject *dp; |
| 1408 | if (!getnoarg(args)) |
| 1409 | return NULL; |
| 1410 | if (Drawing != NULL) { |
Guido van Rossum | 87e7ea7 | 1991-12-10 14:00:03 +0000 | [diff] [blame] | 1411 | err_setstr(StdwinError, "already drawing"); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1412 | return NULL; |
| 1413 | } |
| 1414 | dp = NEWOBJ(drawingobject, &Drawingtype); |
| 1415 | if (dp == NULL) |
| 1416 | return NULL; |
| 1417 | Drawing = dp; |
| 1418 | INCREF(wp); |
| 1419 | dp->d_ref = wp; |
| 1420 | wbegindrawing(wp->w_win); |
| 1421 | return (object *)dp; |
| 1422 | } |
| 1423 | |
| 1424 | static object * |
| 1425 | window_change(wp, args) |
| 1426 | windowobject *wp; |
| 1427 | object *args; |
| 1428 | { |
| 1429 | int a[4]; |
| 1430 | if (!getrectarg(args, a)) |
| 1431 | return NULL; |
| 1432 | wchange(wp->w_win, a[0], a[1], a[2], a[3]); |
| 1433 | INCREF(None); |
| 1434 | return None; |
| 1435 | } |
| 1436 | |
| 1437 | static object * |
| 1438 | window_gettitle(wp, args) |
| 1439 | windowobject *wp; |
| 1440 | object *args; |
| 1441 | { |
| 1442 | if (!getnoarg(args)) |
| 1443 | return NULL; |
| 1444 | INCREF(wp->w_title); |
| 1445 | return wp->w_title; |
| 1446 | } |
| 1447 | |
| 1448 | static object * |
Guido van Rossum | 541c8c0 | 1991-05-05 20:13:41 +0000 | [diff] [blame] | 1449 | window_getwinpos(wp, args) |
| 1450 | windowobject *wp; |
| 1451 | object *args; |
| 1452 | { |
| 1453 | int h, v; |
| 1454 | if (!getnoarg(args)) |
| 1455 | return NULL; |
| 1456 | wgetwinpos(wp->w_win, &h, &v); |
| 1457 | return makepoint(h, v); |
| 1458 | } |
| 1459 | |
| 1460 | static object * |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1461 | window_getwinsize(wp, args) |
| 1462 | windowobject *wp; |
| 1463 | object *args; |
| 1464 | { |
| 1465 | int width, height; |
| 1466 | if (!getnoarg(args)) |
| 1467 | return NULL; |
| 1468 | wgetwinsize(wp->w_win, &width, &height); |
| 1469 | return makepoint(width, height); |
| 1470 | } |
| 1471 | |
| 1472 | static object * |
| 1473 | window_getdocsize(wp, args) |
| 1474 | windowobject *wp; |
| 1475 | object *args; |
| 1476 | { |
| 1477 | int width, height; |
| 1478 | if (!getnoarg(args)) |
| 1479 | return NULL; |
| 1480 | wgetdocsize(wp->w_win, &width, &height); |
| 1481 | return makepoint(width, height); |
| 1482 | } |
| 1483 | |
| 1484 | static object * |
| 1485 | window_getorigin(wp, args) |
| 1486 | windowobject *wp; |
| 1487 | object *args; |
| 1488 | { |
| 1489 | int width, height; |
| 1490 | if (!getnoarg(args)) |
| 1491 | return NULL; |
| 1492 | wgetorigin(wp->w_win, &width, &height); |
| 1493 | return makepoint(width, height); |
| 1494 | } |
| 1495 | |
| 1496 | static object * |
| 1497 | window_scroll(wp, args) |
| 1498 | windowobject *wp; |
| 1499 | object *args; |
| 1500 | { |
| 1501 | int a[6]; |
| 1502 | if (!getrectpointarg(args, a)) |
| 1503 | return NULL; |
| 1504 | wscroll(wp->w_win, a[0], a[1], a[2], a[3], a[4], a[5]); |
| 1505 | INCREF(None); |
| 1506 | return None; |
| 1507 | } |
| 1508 | |
| 1509 | static object * |
| 1510 | window_setdocsize(wp, args) |
| 1511 | windowobject *wp; |
| 1512 | object *args; |
| 1513 | { |
| 1514 | int a[2]; |
| 1515 | if (!getpointarg(args, a)) |
| 1516 | return NULL; |
| 1517 | wsetdocsize(wp->w_win, a[0], a[1]); |
| 1518 | INCREF(None); |
| 1519 | return None; |
| 1520 | } |
| 1521 | |
| 1522 | static object * |
| 1523 | window_setorigin(wp, args) |
| 1524 | windowobject *wp; |
| 1525 | object *args; |
| 1526 | { |
| 1527 | int a[2]; |
| 1528 | if (!getpointarg(args, a)) |
| 1529 | return NULL; |
| 1530 | wsetorigin(wp->w_win, a[0], a[1]); |
| 1531 | INCREF(None); |
| 1532 | return None; |
| 1533 | } |
| 1534 | |
| 1535 | static object * |
| 1536 | window_settitle(wp, args) |
| 1537 | windowobject *wp; |
| 1538 | object *args; |
| 1539 | { |
| 1540 | object *title; |
| 1541 | if (!getstrarg(args, &title)) |
| 1542 | return NULL; |
| 1543 | DECREF(wp->w_title); |
| 1544 | INCREF(title); |
| 1545 | wp->w_title = title; |
| 1546 | wsettitle(wp->w_win, getstringvalue(title)); |
| 1547 | INCREF(None); |
| 1548 | return None; |
| 1549 | } |
| 1550 | |
| 1551 | static object * |
| 1552 | window_show(wp, args) |
| 1553 | windowobject *wp; |
| 1554 | object *args; |
| 1555 | { |
| 1556 | int a[4]; |
| 1557 | if (!getrectarg(args, a)) |
| 1558 | return NULL; |
| 1559 | wshow(wp->w_win, a[0], a[1], a[2], a[3]); |
| 1560 | INCREF(None); |
| 1561 | return None; |
| 1562 | } |
| 1563 | |
| 1564 | static object * |
| 1565 | window_settimer(wp, args) |
| 1566 | windowobject *wp; |
| 1567 | object *args; |
| 1568 | { |
| 1569 | int a; |
| 1570 | if (!getintarg(args, &a)) |
| 1571 | return NULL; |
| 1572 | wsettimer(wp->w_win, a); |
| 1573 | INCREF(None); |
| 1574 | return None; |
| 1575 | } |
| 1576 | |
| 1577 | static object * |
| 1578 | window_menucreate(self, args) |
| 1579 | windowobject *self; |
| 1580 | object *args; |
| 1581 | { |
| 1582 | menuobject *mp; |
| 1583 | object *title; |
| 1584 | if (!getstrarg(args, &title)) |
| 1585 | return NULL; |
| 1586 | wmenusetdeflocal(1); |
| 1587 | mp = newmenuobject(title); |
| 1588 | if (mp == NULL) |
| 1589 | return NULL; |
| 1590 | wmenuattach(self->w_win, mp->m_menu); |
| 1591 | return (object *)mp; |
| 1592 | } |
| 1593 | |
| 1594 | static object * |
| 1595 | window_textcreate(self, args) |
| 1596 | windowobject *self; |
| 1597 | object *args; |
| 1598 | { |
| 1599 | textobject *tp; |
| 1600 | int a[4]; |
| 1601 | if (!getrectarg(args, a)) |
| 1602 | return NULL; |
| 1603 | return (object *) |
| 1604 | newtextobject(self, a[0], a[1], a[2], a[3]); |
| 1605 | } |
| 1606 | |
Guido van Rossum | 5b10f45 | 1990-10-30 16:01:48 +0000 | [diff] [blame] | 1607 | static object * |
| 1608 | window_setselection(self, args) |
| 1609 | windowobject *self; |
| 1610 | object *args; |
| 1611 | { |
| 1612 | int sel; |
| 1613 | object *str; |
| 1614 | int ok; |
| 1615 | if (!getintstrarg(args, &sel, &str)) |
| 1616 | return NULL; |
| 1617 | ok = wsetselection(self->w_win, sel, |
| 1618 | getstringvalue(str), (int)getstringsize(str)); |
| 1619 | return newintobject(ok); |
| 1620 | } |
| 1621 | |
| 1622 | static object * |
| 1623 | window_setwincursor(self, args) |
| 1624 | windowobject *self; |
| 1625 | object *args; |
| 1626 | { |
| 1627 | object *str; |
| 1628 | CURSOR *c; |
| 1629 | if (!getstrarg(args, &str)) |
| 1630 | return NULL; |
| 1631 | c = wfetchcursor(getstringvalue(str)); |
| 1632 | if (c == NULL) { |
Guido van Rossum | 87e7ea7 | 1991-12-10 14:00:03 +0000 | [diff] [blame] | 1633 | err_setstr(StdwinError, "no such cursor"); |
Guido van Rossum | 5b10f45 | 1990-10-30 16:01:48 +0000 | [diff] [blame] | 1634 | return NULL; |
| 1635 | } |
| 1636 | wsetwincursor(self->w_win, c); |
| 1637 | INCREF(None); |
| 1638 | return None; |
| 1639 | } |
| 1640 | |
Guido van Rossum | 8dcbbac | 1991-07-27 21:42:24 +0000 | [diff] [blame] | 1641 | #ifdef CWI_HACKS |
| 1642 | static object * |
| 1643 | window_getxwindowid(self, args) |
| 1644 | windowobject *self; |
| 1645 | object *args; |
| 1646 | { |
| 1647 | long wid = wgetxwindowid(self->w_win); |
| 1648 | return newintobject(wid); |
| 1649 | } |
| 1650 | #endif |
| 1651 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1652 | static struct methodlist window_methods[] = { |
| 1653 | {"begindrawing",window_begindrawing}, |
| 1654 | {"change", window_change}, |
Guido van Rossum | 3c28474 | 1991-11-27 14:54:54 +0000 | [diff] [blame] | 1655 | {"close", window_close}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1656 | {"getdocsize", window_getdocsize}, |
| 1657 | {"getorigin", window_getorigin}, |
| 1658 | {"gettitle", window_gettitle}, |
Guido van Rossum | 541c8c0 | 1991-05-05 20:13:41 +0000 | [diff] [blame] | 1659 | {"getwinpos", window_getwinpos}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1660 | {"getwinsize", window_getwinsize}, |
| 1661 | {"menucreate", window_menucreate}, |
| 1662 | {"scroll", window_scroll}, |
| 1663 | {"setdocsize", window_setdocsize}, |
| 1664 | {"setorigin", window_setorigin}, |
Guido van Rossum | 5b10f45 | 1990-10-30 16:01:48 +0000 | [diff] [blame] | 1665 | {"setselection",window_setselection}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1666 | {"settimer", window_settimer}, |
| 1667 | {"settitle", window_settitle}, |
Guido van Rossum | 2720106 | 1991-04-16 08:43:03 +0000 | [diff] [blame] | 1668 | {"setwincursor",window_setwincursor}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1669 | {"show", window_show}, |
| 1670 | {"textcreate", window_textcreate}, |
Guido van Rossum | 8dcbbac | 1991-07-27 21:42:24 +0000 | [diff] [blame] | 1671 | #ifdef CWI_HACKS |
| 1672 | {"getxwindowid",window_getxwindowid}, |
| 1673 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1674 | {NULL, NULL} /* sentinel */ |
| 1675 | }; |
| 1676 | |
| 1677 | static object * |
| 1678 | window_getattr(wp, name) |
| 1679 | windowobject *wp; |
| 1680 | char *name; |
| 1681 | { |
Guido van Rossum | 85f5076 | 1991-10-20 20:22:50 +0000 | [diff] [blame] | 1682 | object *v = NULL; |
Guido van Rossum | 77b4604 | 1992-01-14 18:41:24 +0000 | [diff] [blame^] | 1683 | if (wp->w_win == NULL) { |
| 1684 | err_setstr(StdwinError, "window already closed"); |
| 1685 | return NULL; |
| 1686 | } |
Guido van Rossum | 85f5076 | 1991-10-20 20:22:50 +0000 | [diff] [blame] | 1687 | if (strcmp(name, "__dict__") == 0) { |
| 1688 | v = wp->w_attr; |
| 1689 | if (v == NULL) |
| 1690 | v = None; |
| 1691 | } |
| 1692 | else if (wp->w_attr != NULL) { |
| 1693 | v = dictlookup(wp->w_attr, name); |
| 1694 | } |
| 1695 | if (v != NULL) { |
| 1696 | INCREF(v); |
| 1697 | return v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1698 | } |
| 1699 | return findmethod(window_methods, (object *)wp, name); |
| 1700 | } |
| 1701 | |
| 1702 | static int |
| 1703 | window_setattr(wp, name, v) |
| 1704 | windowobject *wp; |
| 1705 | char *name; |
| 1706 | object *v; |
| 1707 | { |
| 1708 | if (wp->w_attr == NULL) { |
| 1709 | wp->w_attr = newdictobject(); |
| 1710 | if (wp->w_attr == NULL) |
| 1711 | return -1; |
| 1712 | } |
| 1713 | if (v == NULL) |
| 1714 | return dictremove(wp->w_attr, name); |
| 1715 | else |
| 1716 | return dictinsert(wp->w_attr, name, v); |
| 1717 | } |
| 1718 | |
Guido van Rossum | 541c8c0 | 1991-05-05 20:13:41 +0000 | [diff] [blame] | 1719 | typeobject Windowtype = { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1720 | OB_HEAD_INIT(&Typetype) |
| 1721 | 0, /*ob_size*/ |
| 1722 | "window", /*tp_name*/ |
| 1723 | sizeof(windowobject), /*tp_size*/ |
| 1724 | 0, /*tp_itemsize*/ |
| 1725 | /* methods */ |
| 1726 | window_dealloc, /*tp_dealloc*/ |
| 1727 | window_print, /*tp_print*/ |
| 1728 | window_getattr, /*tp_getattr*/ |
| 1729 | window_setattr, /*tp_setattr*/ |
| 1730 | 0, /*tp_compare*/ |
| 1731 | 0, /*tp_repr*/ |
| 1732 | }; |
| 1733 | |
| 1734 | /* Stdwin methods */ |
| 1735 | |
| 1736 | static object * |
| 1737 | stdwin_open(sw, args) |
| 1738 | object *sw; |
| 1739 | object *args; |
| 1740 | { |
| 1741 | int tag; |
| 1742 | object *title; |
| 1743 | windowobject *wp; |
| 1744 | if (!getstrarg(args, &title)) |
| 1745 | return NULL; |
| 1746 | for (tag = 0; tag < MAXNWIN; tag++) { |
| 1747 | if (windowlist[tag] == NULL) |
| 1748 | break; |
| 1749 | } |
Guido van Rossum | 2720106 | 1991-04-16 08:43:03 +0000 | [diff] [blame] | 1750 | if (tag >= MAXNWIN) { |
Guido van Rossum | 87e7ea7 | 1991-12-10 14:00:03 +0000 | [diff] [blame] | 1751 | err_setstr(StdwinError, "creating too many windows"); |
Guido van Rossum | 2720106 | 1991-04-16 08:43:03 +0000 | [diff] [blame] | 1752 | return NULL; |
| 1753 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1754 | wp = NEWOBJ(windowobject, &Windowtype); |
| 1755 | if (wp == NULL) |
| 1756 | return NULL; |
| 1757 | INCREF(title); |
| 1758 | wp->w_title = title; |
| 1759 | wp->w_win = wopen(getstringvalue(title), (void (*)()) NULL); |
| 1760 | wp->w_attr = NULL; |
| 1761 | if (wp->w_win == NULL) { |
| 1762 | DECREF(wp); |
| 1763 | return NULL; |
| 1764 | } |
| 1765 | windowlist[tag] = wp; |
| 1766 | wsettag(wp->w_win, tag); |
| 1767 | return (object *)wp; |
| 1768 | } |
| 1769 | |
| 1770 | static object * |
Guido van Rossum | 246b9d8 | 1991-06-03 10:55:14 +0000 | [diff] [blame] | 1771 | window2object(win) |
| 1772 | WINDOW *win; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1773 | { |
Guido van Rossum | 246b9d8 | 1991-06-03 10:55:14 +0000 | [diff] [blame] | 1774 | object *w; |
| 1775 | if (win == NULL) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1776 | w = None; |
| 1777 | else { |
Guido van Rossum | 246b9d8 | 1991-06-03 10:55:14 +0000 | [diff] [blame] | 1778 | int tag = wgettag(win); |
| 1779 | if (tag < 0 || tag >= MAXNWIN || windowlist[tag] == NULL || |
| 1780 | windowlist[tag]->w_win != win) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1781 | w = None; |
| 1782 | else |
| 1783 | w = (object *)windowlist[tag]; |
| 1784 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1785 | INCREF(w); |
Guido van Rossum | 246b9d8 | 1991-06-03 10:55:14 +0000 | [diff] [blame] | 1786 | return w; |
| 1787 | } |
| 1788 | |
| 1789 | static object * |
| 1790 | stdwin_get_poll_event(poll, args) |
| 1791 | int poll; |
| 1792 | object *args; |
| 1793 | { |
| 1794 | EVENT e; |
| 1795 | object *v, *w; |
| 1796 | if (!getnoarg(args)) |
| 1797 | return NULL; |
| 1798 | if (Drawing != NULL) { |
Guido van Rossum | 87e7ea7 | 1991-12-10 14:00:03 +0000 | [diff] [blame] | 1799 | err_setstr(StdwinError, "cannot getevent() while drawing"); |
Guido van Rossum | 246b9d8 | 1991-06-03 10:55:14 +0000 | [diff] [blame] | 1800 | return NULL; |
| 1801 | } |
| 1802 | again: |
| 1803 | if (poll) { |
| 1804 | if (!wpollevent(&e)) { |
| 1805 | INCREF(None); |
| 1806 | return None; |
| 1807 | } |
| 1808 | } |
| 1809 | else |
| 1810 | wgetevent(&e); |
| 1811 | if (e.type == WE_COMMAND && e.u.command == WC_CANCEL) { |
| 1812 | /* Turn keyboard interrupts into exceptions */ |
| 1813 | err_set(KeyboardInterrupt); |
| 1814 | return NULL; |
| 1815 | } |
| 1816 | if (e.type == WE_COMMAND && e.u.command == WC_CLOSE) { |
| 1817 | /* Turn WC_CLOSE commands into WE_CLOSE events */ |
| 1818 | e.type = WE_CLOSE; |
| 1819 | } |
| 1820 | v = newtupleobject(3); |
| 1821 | if (v == NULL) |
| 1822 | return NULL; |
| 1823 | if ((w = newintobject((long)e.type)) == NULL) { |
| 1824 | DECREF(v); |
| 1825 | return NULL; |
| 1826 | } |
| 1827 | settupleitem(v, 0, w); |
| 1828 | settupleitem(v, 1, window2object(e.window)); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1829 | switch (e.type) { |
| 1830 | case WE_CHAR: |
| 1831 | { |
| 1832 | char c[1]; |
| 1833 | c[0] = e.u.character; |
| 1834 | w = newsizedstringobject(c, 1); |
| 1835 | } |
| 1836 | break; |
| 1837 | case WE_COMMAND: |
| 1838 | w = newintobject((long)e.u.command); |
| 1839 | break; |
| 1840 | case WE_DRAW: |
| 1841 | w = makerect(e.u.area.left, e.u.area.top, |
| 1842 | e.u.area.right, e.u.area.bottom); |
| 1843 | break; |
| 1844 | case WE_MOUSE_DOWN: |
| 1845 | case WE_MOUSE_MOVE: |
| 1846 | case WE_MOUSE_UP: |
| 1847 | w = makemouse(e.u.where.h, e.u.where.v, |
| 1848 | e.u.where.clicks, |
| 1849 | e.u.where.button, |
| 1850 | e.u.where.mask); |
| 1851 | break; |
| 1852 | case WE_MENU: |
Guido van Rossum | 2d14e21 | 1991-02-19 12:26:49 +0000 | [diff] [blame] | 1853 | if (e.u.m.id >= IDOFFSET && e.u.m.id < IDOFFSET+MAXNMENU && |
| 1854 | menulist[e.u.m.id - IDOFFSET] != NULL) |
| 1855 | w = (object *)menulist[e.u.m.id - IDOFFSET]; |
Guido van Rossum | 246b9d8 | 1991-06-03 10:55:14 +0000 | [diff] [blame] | 1856 | else { |
| 1857 | /* Ghost menu event. |
| 1858 | Can occur only on the Mac if another part |
| 1859 | of the aplication has installed a menu; |
| 1860 | like the THINK C console library. */ |
| 1861 | DECREF(v); |
| 1862 | goto again; |
| 1863 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1864 | w = makemenu(w, e.u.m.item); |
| 1865 | break; |
Guido van Rossum | 5b10f45 | 1990-10-30 16:01:48 +0000 | [diff] [blame] | 1866 | case WE_LOST_SEL: |
| 1867 | w = newintobject((long)e.u.sel); |
| 1868 | break; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1869 | default: |
| 1870 | w = None; |
| 1871 | INCREF(w); |
| 1872 | break; |
| 1873 | } |
| 1874 | if (w == NULL) { |
| 1875 | DECREF(v); |
| 1876 | return NULL; |
| 1877 | } |
| 1878 | settupleitem(v, 2, w); |
| 1879 | return v; |
| 1880 | } |
| 1881 | |
| 1882 | static object * |
Guido van Rossum | e8e7cf4 | 1991-01-16 14:06:18 +0000 | [diff] [blame] | 1883 | stdwin_getevent(sw, args) |
| 1884 | object *sw; |
| 1885 | object *args; |
| 1886 | { |
| 1887 | return stdwin_get_poll_event(0, args); |
| 1888 | } |
| 1889 | |
| 1890 | static object * |
| 1891 | stdwin_pollevent(sw, args) |
| 1892 | object *sw; |
| 1893 | object *args; |
| 1894 | { |
| 1895 | return stdwin_get_poll_event(1, args); |
| 1896 | } |
| 1897 | |
| 1898 | static object * |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1899 | stdwin_setdefwinpos(sw, args) |
| 1900 | object *sw; |
| 1901 | object *args; |
| 1902 | { |
| 1903 | int a[2]; |
| 1904 | if (!getpointarg(args, a)) |
| 1905 | return NULL; |
| 1906 | wsetdefwinpos(a[0], a[1]); |
| 1907 | INCREF(None); |
| 1908 | return None; |
| 1909 | } |
| 1910 | |
| 1911 | static object * |
| 1912 | stdwin_setdefwinsize(sw, args) |
| 1913 | object *sw; |
| 1914 | object *args; |
| 1915 | { |
| 1916 | int a[2]; |
| 1917 | if (!getpointarg(args, a)) |
| 1918 | return NULL; |
| 1919 | wsetdefwinsize(a[0], a[1]); |
| 1920 | INCREF(None); |
| 1921 | return None; |
| 1922 | } |
| 1923 | |
| 1924 | static object * |
Guido van Rossum | 0c2290b | 1991-04-03 19:12:14 +0000 | [diff] [blame] | 1925 | stdwin_setdefscrollbars(sw, args) |
| 1926 | object *sw; |
| 1927 | object *args; |
| 1928 | { |
| 1929 | int a[2]; |
| 1930 | if (!getpointarg(args, a)) |
| 1931 | return NULL; |
| 1932 | wsetdefscrollbars(a[0], a[1]); |
| 1933 | INCREF(None); |
| 1934 | return None; |
| 1935 | } |
| 1936 | |
| 1937 | static object * |
Guido van Rossum | 541c8c0 | 1991-05-05 20:13:41 +0000 | [diff] [blame] | 1938 | stdwin_getdefwinpos(self, args) |
| 1939 | object *self; |
Guido van Rossum | 33f1770 | 1991-02-13 23:19:39 +0000 | [diff] [blame] | 1940 | object *args; |
| 1941 | { |
| 1942 | int h, v; |
| 1943 | if (!getnoarg(args)) |
| 1944 | return NULL; |
| 1945 | wgetdefwinpos(&h, &v); |
| 1946 | return makepoint(h, v); |
| 1947 | } |
| 1948 | |
| 1949 | static object * |
Guido van Rossum | 541c8c0 | 1991-05-05 20:13:41 +0000 | [diff] [blame] | 1950 | stdwin_getdefwinsize(self, args) |
| 1951 | object *self; |
Guido van Rossum | 33f1770 | 1991-02-13 23:19:39 +0000 | [diff] [blame] | 1952 | object *args; |
| 1953 | { |
| 1954 | int width, height; |
| 1955 | if (!getnoarg(args)) |
| 1956 | return NULL; |
| 1957 | wgetdefwinsize(&width, &height); |
| 1958 | return makepoint(width, height); |
| 1959 | } |
| 1960 | |
| 1961 | static object * |
Guido van Rossum | 541c8c0 | 1991-05-05 20:13:41 +0000 | [diff] [blame] | 1962 | stdwin_getdefscrollbars(self, args) |
| 1963 | object *self; |
Guido van Rossum | 0c2290b | 1991-04-03 19:12:14 +0000 | [diff] [blame] | 1964 | object *args; |
| 1965 | { |
| 1966 | int h, v; |
| 1967 | if (!getnoarg(args)) |
| 1968 | return NULL; |
| 1969 | wgetdefscrollbars(&h, &v); |
| 1970 | return makepoint(h, v); |
| 1971 | } |
| 1972 | |
| 1973 | static object * |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1974 | stdwin_menucreate(self, args) |
| 1975 | object *self; |
| 1976 | object *args; |
| 1977 | { |
| 1978 | object *title; |
| 1979 | if (!getstrarg(args, &title)) |
| 1980 | return NULL; |
| 1981 | wmenusetdeflocal(0); |
| 1982 | return (object *)newmenuobject(title); |
| 1983 | } |
| 1984 | |
| 1985 | static object * |
| 1986 | stdwin_askfile(self, args) |
| 1987 | object *self; |
| 1988 | object *args; |
| 1989 | { |
| 1990 | object *prompt, *dflt; |
| 1991 | int new, ret; |
| 1992 | char buf[256]; |
| 1993 | if (!getstrstrintarg(args, &prompt, &dflt, &new)) |
| 1994 | return NULL; |
| 1995 | strncpy(buf, getstringvalue(dflt), sizeof buf); |
| 1996 | buf[sizeof buf - 1] = '\0'; |
| 1997 | ret = waskfile(getstringvalue(prompt), buf, sizeof buf, new); |
| 1998 | if (!ret) { |
| 1999 | err_set(KeyboardInterrupt); |
| 2000 | return NULL; |
| 2001 | } |
| 2002 | return newstringobject(buf); |
| 2003 | } |
| 2004 | |
| 2005 | static object * |
| 2006 | stdwin_askync(self, args) |
| 2007 | object *self; |
| 2008 | object *args; |
| 2009 | { |
| 2010 | object *prompt; |
| 2011 | int new, ret; |
| 2012 | if (!getstrintarg(args, &prompt, &new)) |
| 2013 | return NULL; |
| 2014 | ret = waskync(getstringvalue(prompt), new); |
| 2015 | if (ret < 0) { |
| 2016 | err_set(KeyboardInterrupt); |
| 2017 | return NULL; |
| 2018 | } |
| 2019 | return newintobject((long)ret); |
| 2020 | } |
| 2021 | |
| 2022 | static object * |
| 2023 | stdwin_askstr(self, args) |
| 2024 | object *self; |
| 2025 | object *args; |
| 2026 | { |
| 2027 | object *prompt, *dflt; |
| 2028 | int ret; |
| 2029 | char buf[256]; |
| 2030 | if (!getstrstrarg(args, &prompt, &dflt)) |
| 2031 | return NULL; |
| 2032 | strncpy(buf, getstringvalue(dflt), sizeof buf); |
| 2033 | buf[sizeof buf - 1] = '\0'; |
| 2034 | ret = waskstr(getstringvalue(prompt), buf, sizeof buf); |
| 2035 | if (!ret) { |
| 2036 | err_set(KeyboardInterrupt); |
| 2037 | return NULL; |
| 2038 | } |
| 2039 | return newstringobject(buf); |
| 2040 | } |
| 2041 | |
| 2042 | static object * |
| 2043 | stdwin_message(self, args) |
| 2044 | object *self; |
| 2045 | object *args; |
| 2046 | { |
| 2047 | object *msg; |
| 2048 | if (!getstrarg(args, &msg)) |
| 2049 | return NULL; |
| 2050 | wmessage(getstringvalue(msg)); |
| 2051 | INCREF(None); |
| 2052 | return None; |
| 2053 | } |
| 2054 | |
| 2055 | static object * |
| 2056 | stdwin_fleep(self, args) |
| 2057 | object *self; |
| 2058 | object *args; |
| 2059 | { |
| 2060 | if (!getnoarg(args)) |
| 2061 | return NULL; |
| 2062 | wfleep(); |
| 2063 | INCREF(None); |
| 2064 | return None; |
| 2065 | } |
| 2066 | |
| 2067 | static object * |
| 2068 | stdwin_setcutbuffer(self, args) |
| 2069 | object *self; |
| 2070 | object *args; |
| 2071 | { |
Guido van Rossum | 5b10f45 | 1990-10-30 16:01:48 +0000 | [diff] [blame] | 2072 | int i; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2073 | object *str; |
Guido van Rossum | 124967c | 1990-11-06 15:17:35 +0000 | [diff] [blame] | 2074 | if (!getintstrarg(args, &i, &str)) |
| 2075 | return NULL; |
Guido van Rossum | 5b10f45 | 1990-10-30 16:01:48 +0000 | [diff] [blame] | 2076 | wsetcutbuffer(i, getstringvalue(str), getstringsize(str)); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2077 | INCREF(None); |
| 2078 | return None; |
| 2079 | } |
| 2080 | |
| 2081 | static object * |
Guido van Rossum | 246b9d8 | 1991-06-03 10:55:14 +0000 | [diff] [blame] | 2082 | stdwin_getactive(self, args) |
| 2083 | object *self; |
| 2084 | object *args; |
| 2085 | { |
| 2086 | return window2object(wgetactive()); |
| 2087 | } |
| 2088 | |
| 2089 | static object * |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2090 | stdwin_getcutbuffer(self, args) |
| 2091 | object *self; |
| 2092 | object *args; |
| 2093 | { |
Guido van Rossum | 5b10f45 | 1990-10-30 16:01:48 +0000 | [diff] [blame] | 2094 | int i; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2095 | char *str; |
Guido van Rossum | 01769f0 | 1990-10-30 13:39:00 +0000 | [diff] [blame] | 2096 | int len; |
Guido van Rossum | 124967c | 1990-11-06 15:17:35 +0000 | [diff] [blame] | 2097 | if (!getintarg(args, &i)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2098 | return NULL; |
Guido van Rossum | 5b10f45 | 1990-10-30 16:01:48 +0000 | [diff] [blame] | 2099 | str = wgetcutbuffer(i, &len); |
Guido van Rossum | 01769f0 | 1990-10-30 13:39:00 +0000 | [diff] [blame] | 2100 | if (str == NULL) { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2101 | str = ""; |
Guido van Rossum | 01769f0 | 1990-10-30 13:39:00 +0000 | [diff] [blame] | 2102 | len = 0; |
| 2103 | } |
| 2104 | return newsizedstringobject(str, len); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2105 | } |
| 2106 | |
Guido van Rossum | 5b10f45 | 1990-10-30 16:01:48 +0000 | [diff] [blame] | 2107 | static object * |
| 2108 | stdwin_rotatecutbuffers(self, args) |
| 2109 | object *self; |
| 2110 | object *args; |
| 2111 | { |
| 2112 | int i; |
| 2113 | if (!getintarg(args, &i)) |
| 2114 | return NULL; |
| 2115 | wrotatecutbuffers(i); |
| 2116 | INCREF(None); |
| 2117 | return None; |
| 2118 | } |
| 2119 | |
| 2120 | static object * |
| 2121 | stdwin_getselection(self, args) |
| 2122 | object *self; |
| 2123 | object *args; |
| 2124 | { |
| 2125 | int sel; |
| 2126 | char *data; |
| 2127 | int len; |
| 2128 | if (!getintarg(args, &sel)) |
| 2129 | return NULL; |
| 2130 | data = wgetselection(sel, &len); |
| 2131 | if (data == NULL) { |
| 2132 | data = ""; |
| 2133 | len = 0; |
| 2134 | } |
| 2135 | return newsizedstringobject(data, len); |
| 2136 | } |
| 2137 | |
| 2138 | static object * |
| 2139 | stdwin_resetselection(self, args) |
| 2140 | object *self; |
| 2141 | object *args; |
| 2142 | { |
| 2143 | int sel; |
| 2144 | if (!getintarg(args, &sel)) |
| 2145 | return NULL; |
| 2146 | wresetselection(sel); |
| 2147 | INCREF(None); |
| 2148 | return None; |
| 2149 | } |
| 2150 | |
Guido van Rossum | 0c2290b | 1991-04-03 19:12:14 +0000 | [diff] [blame] | 2151 | static object * |
| 2152 | stdwin_fetchcolor(self, args) |
| 2153 | object *self; |
| 2154 | object *args; |
| 2155 | { |
| 2156 | object *colorname; |
| 2157 | if (!getstrarg(args, &colorname)) |
| 2158 | return NULL; |
| 2159 | return newintobject((long)wfetchcolor(getstringvalue(colorname))); |
| 2160 | } |
| 2161 | |
Guido van Rossum | 541c8c0 | 1991-05-05 20:13:41 +0000 | [diff] [blame] | 2162 | static object * |
| 2163 | stdwin_getscrsize(self, args) |
| 2164 | object *self; |
| 2165 | object *args; |
| 2166 | { |
| 2167 | int width, height; |
| 2168 | if (!getnoarg(args)) |
| 2169 | return NULL; |
| 2170 | wgetscrsize(&width, &height); |
| 2171 | return makepoint(width, height); |
| 2172 | } |
| 2173 | |
| 2174 | static object * |
| 2175 | stdwin_getscrmm(self, args) |
| 2176 | object *self; |
| 2177 | object *args; |
| 2178 | { |
| 2179 | int width, height; |
| 2180 | if (!getnoarg(args)) |
| 2181 | return NULL; |
| 2182 | wgetscrmm(&width, &height); |
| 2183 | return makepoint(width, height); |
| 2184 | } |
| 2185 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2186 | static struct methodlist stdwin_methods[] = { |
| 2187 | {"askfile", stdwin_askfile}, |
| 2188 | {"askstr", stdwin_askstr}, |
| 2189 | {"askync", stdwin_askync}, |
Guido van Rossum | 2720106 | 1991-04-16 08:43:03 +0000 | [diff] [blame] | 2190 | {"fetchcolor", stdwin_fetchcolor}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2191 | {"fleep", stdwin_fleep}, |
Guido van Rossum | 246b9d8 | 1991-06-03 10:55:14 +0000 | [diff] [blame] | 2192 | {"getactive", stdwin_getactive}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2193 | {"getcutbuffer", stdwin_getcutbuffer}, |
Guido van Rossum | 0c2290b | 1991-04-03 19:12:14 +0000 | [diff] [blame] | 2194 | {"getdefscrollbars", stdwin_getdefscrollbars}, |
Guido van Rossum | 33f1770 | 1991-02-13 23:19:39 +0000 | [diff] [blame] | 2195 | {"getdefwinpos", stdwin_getdefwinpos}, |
| 2196 | {"getdefwinsize", stdwin_getdefwinsize}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2197 | {"getevent", stdwin_getevent}, |
Guido van Rossum | 541c8c0 | 1991-05-05 20:13:41 +0000 | [diff] [blame] | 2198 | {"getscrmm", stdwin_getscrmm}, |
| 2199 | {"getscrsize", stdwin_getscrsize}, |
Guido van Rossum | 2720106 | 1991-04-16 08:43:03 +0000 | [diff] [blame] | 2200 | {"getselection", stdwin_getselection}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2201 | {"menucreate", stdwin_menucreate}, |
| 2202 | {"message", stdwin_message}, |
| 2203 | {"open", stdwin_open}, |
Guido van Rossum | e8e7cf4 | 1991-01-16 14:06:18 +0000 | [diff] [blame] | 2204 | {"pollevent", stdwin_pollevent}, |
Guido van Rossum | 5b10f45 | 1990-10-30 16:01:48 +0000 | [diff] [blame] | 2205 | {"resetselection", stdwin_resetselection}, |
| 2206 | {"rotatecutbuffers", stdwin_rotatecutbuffers}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2207 | {"setcutbuffer", stdwin_setcutbuffer}, |
Guido van Rossum | 0c2290b | 1991-04-03 19:12:14 +0000 | [diff] [blame] | 2208 | {"setdefscrollbars", stdwin_setdefscrollbars}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2209 | {"setdefwinpos", stdwin_setdefwinpos}, |
| 2210 | {"setdefwinsize", stdwin_setdefwinsize}, |
| 2211 | |
| 2212 | /* Text measuring methods borrow code from drawing objects: */ |
| 2213 | {"baseline", drawing_baseline}, |
| 2214 | {"lineheight", drawing_lineheight}, |
| 2215 | {"textbreak", drawing_textbreak}, |
| 2216 | {"textwidth", drawing_textwidth}, |
Guido van Rossum | 0c2290b | 1991-04-03 19:12:14 +0000 | [diff] [blame] | 2217 | |
| 2218 | /* Same for font setting methods: */ |
| 2219 | {"setfont", drawing_setfont}, |
| 2220 | |
| 2221 | /* Same for color setting/getting methods: */ |
| 2222 | {"getbgcolor", drawing_getbgcolor}, |
| 2223 | {"getfgcolor", drawing_getfgcolor}, |
| 2224 | {"setbgcolor", drawing_setbgcolor}, |
| 2225 | {"setfgcolor", drawing_setfgcolor}, |
| 2226 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2227 | {NULL, NULL} /* sentinel */ |
| 2228 | }; |
| 2229 | |
| 2230 | void |
| 2231 | initstdwin() |
| 2232 | { |
Guido van Rossum | bbf9434 | 1991-12-16 15:44:53 +0000 | [diff] [blame] | 2233 | object *m, *d; |
| 2234 | static int inited = 0; |
| 2235 | |
Guido van Rossum | 2d14e21 | 1991-02-19 12:26:49 +0000 | [diff] [blame] | 2236 | if (!inited) { |
| 2237 | winit(); |
| 2238 | inited = 1; |
| 2239 | } |
Guido van Rossum | bbf9434 | 1991-12-16 15:44:53 +0000 | [diff] [blame] | 2240 | m = initmodule("stdwin", stdwin_methods); |
| 2241 | d = getmoduledict(m); |
| 2242 | |
| 2243 | /* Initialize stdwin.error exception */ |
| 2244 | StdwinError = newstringobject("stdwin.error"); |
| 2245 | if (StdwinError == NULL || dictinsert(d, "error", StdwinError) != 0) |
| 2246 | fatal("can't define stdwin.error"); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2247 | } |