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