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