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