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