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