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