Guido van Rossum | ecde781 | 1995-03-28 13:35:14 +0000 | [diff] [blame] | 1 | \chapter{Standard Windowing Interface} |
| 2 | |
| 3 | The modules in this chapter are available only on those systems where |
| 4 | the STDWIN library is available. STDWIN runs on \UNIX{} under X11 and |
| 5 | on the Macintosh. See CWI report CS-R8817. |
| 6 | |
| 7 | \strong{Warning:} Using STDWIN is not recommended for new |
| 8 | applications. It has never been ported to Microsoft Windows or |
| 9 | Windows NT, and for X11 or the Macintosh it lacks important |
| 10 | functionality --- in particular, it has no tools for the construction |
| 11 | of dialogs. For most platforms, alternative, native solutions exist |
| 12 | (though none are currently documented in this manual): Tkinter for |
| 13 | \UNIX{} under X11, native Xt with Motif or Athena widgets for \UNIX{} |
| 14 | under X11, Win32 for Windows and Windows NT, and a collection of |
| 15 | native toolkit interfaces for the Macintosh. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 16 | |
| 17 | \section{Built-in Module \sectcode{stdwin}} |
| 18 | \bimodindex{stdwin} |
| 19 | |
| 20 | This module defines several new object types and functions that |
Guido van Rossum | ecde781 | 1995-03-28 13:35:14 +0000 | [diff] [blame] | 21 | provide access to the functionality of STDWIN. |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 22 | |
| 23 | On Unix running X11, it can only be used if the \code{DISPLAY} |
| 24 | environment variable is set or an explicit \samp{-display |
| 25 | \var{displayname}} argument is passed to the Python interpreter. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 26 | |
| 27 | Functions have names that usually resemble their C STDWIN counterparts |
| 28 | with the initial `w' dropped. |
| 29 | Points are represented by pairs of integers; rectangles |
| 30 | by pairs of points. |
| 31 | For a complete description of STDWIN please refer to the documentation |
| 32 | of STDWIN for C programmers (aforementioned CWI report). |
| 33 | |
| 34 | \subsection{Functions Defined in Module \sectcode{stdwin}} |
Guido van Rossum | 86cb092 | 1995-03-20 12:59:56 +0000 | [diff] [blame] | 35 | \nodename{STDWIN Functions} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 36 | |
| 37 | The following functions are defined in the \code{stdwin} module: |
| 38 | |
| 39 | \renewcommand{\indexsubitem}{(in module stdwin)} |
| 40 | \begin{funcdesc}{open}{title} |
| 41 | Open a new window whose initial title is given by the string argument. |
| 42 | Return a window object; window object methods are described below.% |
| 43 | \footnote{The Python version of STDWIN does not support draw procedures; all |
| 44 | drawing requests are reported as draw events.} |
| 45 | \end{funcdesc} |
| 46 | |
| 47 | \begin{funcdesc}{getevent}{} |
| 48 | Wait for and return the next event. |
| 49 | An event is returned as a triple: the first element is the event |
| 50 | type, a small integer; the second element is the window object to which |
| 51 | the event applies, or |
| 52 | \code{None} |
| 53 | if it applies to no window in particular; |
| 54 | the third element is type-dependent. |
| 55 | Names for event types and command codes are defined in the standard |
| 56 | module |
| 57 | \code{stdwinevent}. |
| 58 | \end{funcdesc} |
| 59 | |
| 60 | \begin{funcdesc}{pollevent}{} |
| 61 | Return the next event, if one is immediately available. |
| 62 | If no event is available, return \code{()}. |
| 63 | \end{funcdesc} |
| 64 | |
| 65 | \begin{funcdesc}{getactive}{} |
| 66 | Return the window that is currently active, or \code{None} if no |
| 67 | window is currently active. (This can be emulated by monitoring |
| 68 | WE_ACTIVATE and WE_DEACTIVATE events.) |
| 69 | \end{funcdesc} |
| 70 | |
| 71 | \begin{funcdesc}{listfontnames}{pattern} |
| 72 | Return the list of font names in the system that match the pattern (a |
| 73 | string). The pattern should normally be \code{'*'}; returns all |
| 74 | available fonts. If the underlying window system is X11, other |
| 75 | patterns follow the standard X11 font selection syntax (as used e.g. |
| 76 | in resource definitions), i.e. the wildcard character \code{'*'} |
| 77 | matches any sequence of characters (including none) and \code{'?'} |
| 78 | matches any single character. |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 79 | On the Macintosh this function currently returns an empty list. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 80 | \end{funcdesc} |
| 81 | |
| 82 | \begin{funcdesc}{setdefscrollbars}{hflag\, vflag} |
| 83 | Set the flags controlling whether subsequently opened windows will |
| 84 | have horizontal and/or vertical scroll bars. |
| 85 | \end{funcdesc} |
| 86 | |
| 87 | \begin{funcdesc}{setdefwinpos}{h\, v} |
| 88 | Set the default window position for windows opened subsequently. |
| 89 | \end{funcdesc} |
| 90 | |
| 91 | \begin{funcdesc}{setdefwinsize}{width\, height} |
| 92 | Set the default window size for windows opened subsequently. |
| 93 | \end{funcdesc} |
| 94 | |
| 95 | \begin{funcdesc}{getdefscrollbars}{} |
| 96 | Return the flags controlling whether subsequently opened windows will |
| 97 | have horizontal and/or vertical scroll bars. |
| 98 | \end{funcdesc} |
| 99 | |
| 100 | \begin{funcdesc}{getdefwinpos}{} |
| 101 | Return the default window position for windows opened subsequently. |
| 102 | \end{funcdesc} |
| 103 | |
| 104 | \begin{funcdesc}{getdefwinsize}{} |
| 105 | Return the default window size for windows opened subsequently. |
| 106 | \end{funcdesc} |
| 107 | |
| 108 | \begin{funcdesc}{getscrsize}{} |
| 109 | Return the screen size in pixels. |
| 110 | \end{funcdesc} |
| 111 | |
| 112 | \begin{funcdesc}{getscrmm}{} |
| 113 | Return the screen size in millimeters. |
| 114 | \end{funcdesc} |
| 115 | |
| 116 | \begin{funcdesc}{fetchcolor}{colorname} |
| 117 | Return the pixel value corresponding to the given color name. |
| 118 | Return the default foreground color for unknown color names. |
Guido van Rossum | 16d6e71 | 1994-08-08 12:30:22 +0000 | [diff] [blame] | 119 | Hint: the following code tests whether you are on a machine that |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 120 | supports more than two colors: |
| 121 | \bcode\begin{verbatim} |
| 122 | if stdwin.fetchcolor('black') <> \ |
| 123 | stdwin.fetchcolor('red') <> \ |
| 124 | stdwin.fetchcolor('white'): |
| 125 | print 'color machine' |
| 126 | else: |
| 127 | print 'monochrome machine' |
| 128 | \end{verbatim}\ecode |
| 129 | \end{funcdesc} |
| 130 | |
| 131 | \begin{funcdesc}{setfgcolor}{pixel} |
| 132 | Set the default foreground color. |
| 133 | This will become the default foreground color of windows opened |
| 134 | subsequently, including dialogs. |
| 135 | \end{funcdesc} |
| 136 | |
| 137 | \begin{funcdesc}{setbgcolor}{pixel} |
| 138 | Set the default background color. |
| 139 | This will become the default background color of windows opened |
| 140 | subsequently, including dialogs. |
| 141 | \end{funcdesc} |
| 142 | |
| 143 | \begin{funcdesc}{getfgcolor}{} |
| 144 | Return the pixel value of the current default foreground color. |
| 145 | \end{funcdesc} |
| 146 | |
| 147 | \begin{funcdesc}{getbgcolor}{} |
| 148 | Return the pixel value of the current default background color. |
| 149 | \end{funcdesc} |
| 150 | |
| 151 | \begin{funcdesc}{setfont}{fontname} |
| 152 | Set the current default font. |
| 153 | This will become the default font for windows opened subsequently, |
| 154 | and is also used by the text measuring functions \code{textwidth}, |
| 155 | \code{textbreak}, \code{lineheight} and \code{baseline} below. |
| 156 | This accepts two more optional parameters, size and style: |
| 157 | Size is the font size (in `points'). |
| 158 | Style is a single character specifying the style, as follows: |
| 159 | \code{'b'} = bold, |
| 160 | \code{'i'} = italic, |
| 161 | \code{'o'} = bold + italic, |
| 162 | \code{'u'} = underline; |
| 163 | default style is roman. |
| 164 | Size and style are ignored under X11 but used on the Macintosh. |
| 165 | (Sorry for all this complexity --- a more uniform interface is being designed.) |
| 166 | \end{funcdesc} |
| 167 | |
| 168 | \begin{funcdesc}{menucreate}{title} |
| 169 | Create a menu object referring to a global menu (a menu that appears in |
| 170 | all windows). |
| 171 | Methods of menu objects are described below. |
| 172 | Note: normally, menus are created locally; see the window method |
| 173 | \code{menucreate} below. |
| 174 | \strong{Warning:} the menu only appears in a window as long as the object |
| 175 | returned by this call exists. |
| 176 | \end{funcdesc} |
| 177 | |
| 178 | \begin{funcdesc}{newbitmap}{width\, height} |
| 179 | Create a new bitmap object of the given dimensions. |
| 180 | Methods of bitmap objects are described below. |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 181 | Not available on the Macintosh. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 182 | \end{funcdesc} |
| 183 | |
| 184 | \begin{funcdesc}{fleep}{} |
| 185 | Cause a beep or bell (or perhaps a `visual bell' or flash, hence the |
| 186 | name). |
| 187 | \end{funcdesc} |
| 188 | |
| 189 | \begin{funcdesc}{message}{string} |
| 190 | Display a dialog box containing the string. |
| 191 | The user must click OK before the function returns. |
| 192 | \end{funcdesc} |
| 193 | |
| 194 | \begin{funcdesc}{askync}{prompt\, default} |
| 195 | Display a dialog that prompts the user to answer a question with yes or |
| 196 | no. |
| 197 | Return 0 for no, 1 for yes. |
| 198 | If the user hits the Return key, the default (which must be 0 or 1) is |
| 199 | returned. |
| 200 | If the user cancels the dialog, the |
| 201 | \code{KeyboardInterrupt} |
| 202 | exception is raised. |
| 203 | \end{funcdesc} |
| 204 | |
| 205 | \begin{funcdesc}{askstr}{prompt\, default} |
| 206 | Display a dialog that prompts the user for a string. |
| 207 | If the user hits the Return key, the default string is returned. |
| 208 | If the user cancels the dialog, the |
| 209 | \code{KeyboardInterrupt} |
| 210 | exception is raised. |
| 211 | \end{funcdesc} |
| 212 | |
| 213 | \begin{funcdesc}{askfile}{prompt\, default\, new} |
| 214 | Ask the user to specify a filename. |
| 215 | If |
| 216 | \var{new} |
| 217 | is zero it must be an existing file; otherwise, it must be a new file. |
| 218 | If the user cancels the dialog, the |
| 219 | \code{KeyboardInterrupt} |
| 220 | exception is raised. |
| 221 | \end{funcdesc} |
| 222 | |
| 223 | \begin{funcdesc}{setcutbuffer}{i\, string} |
| 224 | Store the string in the system's cut buffer number |
| 225 | \var{i}, |
| 226 | where it can be found (for pasting) by other applications. |
| 227 | On X11, there are 8 cut buffers (numbered 0..7). |
| 228 | Cut buffer number 0 is the `clipboard' on the Macintosh. |
| 229 | \end{funcdesc} |
| 230 | |
| 231 | \begin{funcdesc}{getcutbuffer}{i} |
| 232 | Return the contents of the system's cut buffer number |
| 233 | \var{i}. |
| 234 | \end{funcdesc} |
| 235 | |
| 236 | \begin{funcdesc}{rotatecutbuffers}{n} |
| 237 | On X11, rotate the 8 cut buffers by |
| 238 | \var{n}. |
| 239 | Ignored on the Macintosh. |
| 240 | \end{funcdesc} |
| 241 | |
| 242 | \begin{funcdesc}{getselection}{i} |
| 243 | Return X11 selection number |
| 244 | \var{i.} |
| 245 | Selections are not cut buffers. |
| 246 | Selection numbers are defined in module |
| 247 | \code{stdwinevents}. |
| 248 | Selection \code{WS_PRIMARY} is the |
| 249 | \dfn{primary} |
| 250 | selection (used by |
| 251 | xterm, |
| 252 | for instance); |
| 253 | selection \code{WS_SECONDARY} is the |
| 254 | \dfn{secondary} |
| 255 | selection; selection \code{WS_CLIPBOARD} is the |
| 256 | \dfn{clipboard} |
| 257 | selection (used by |
| 258 | xclipboard). |
| 259 | On the Macintosh, this always returns an empty string. |
| 260 | \end{funcdesc} |
| 261 | |
| 262 | \begin{funcdesc}{resetselection}{i} |
| 263 | Reset selection number |
| 264 | \var{i}, |
| 265 | if this process owns it. |
| 266 | (See window method |
| 267 | \code{setselection()}). |
| 268 | \end{funcdesc} |
| 269 | |
| 270 | \begin{funcdesc}{baseline}{} |
| 271 | Return the baseline of the current font (defined by STDWIN as the |
| 272 | vertical distance between the baseline and the top of the |
| 273 | characters). |
| 274 | \end{funcdesc} |
| 275 | |
| 276 | \begin{funcdesc}{lineheight}{} |
| 277 | Return the total line height of the current font. |
| 278 | \end{funcdesc} |
| 279 | |
| 280 | \begin{funcdesc}{textbreak}{str\, width} |
| 281 | Return the number of characters of the string that fit into a space of |
| 282 | \var{width} |
| 283 | bits wide when drawn in the curent font. |
| 284 | \end{funcdesc} |
| 285 | |
| 286 | \begin{funcdesc}{textwidth}{str} |
| 287 | Return the width in bits of the string when drawn in the current font. |
| 288 | \end{funcdesc} |
| 289 | |
| 290 | \begin{funcdesc}{connectionnumber}{} |
| 291 | \funcline{fileno}{} |
| 292 | (X11 under \UNIX{} only) Return the ``connection number'' used by the |
| 293 | underlying X11 implementation. (This is normally the file number of |
| 294 | the socket.) Both functions return the same value; |
| 295 | \code{connectionnumber()} is named after the corresponding function in |
| 296 | X11 and STDWIN, while \code{fileno()} makes it possible to use the |
| 297 | \code{stdwin} module as a ``file'' object parameter to |
| 298 | \code{select.select()}. Note that if \code{select()} implies that |
| 299 | input is possible on \code{stdwin}, this does not guarantee that an |
| 300 | event is ready --- it may be some internal communication going on |
| 301 | between the X server and the client library. Thus, you should call |
| 302 | \code{stdwin.pollevent()} until it returns \code{None} to check for |
| 303 | events if you don't want your program to block. Because of internal |
| 304 | buffering in X11, it is also possible that \code{stdwin.pollevent()} |
| 305 | returns an event while \code{select()} does not find \code{stdwin} to |
| 306 | be ready, so you should read any pending events with |
| 307 | \code{stdwin.pollevent()} until it returns \code{None} before entering |
| 308 | a blocking \code{select()} call. |
| 309 | \ttindex{select} |
| 310 | \end{funcdesc} |
| 311 | |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 312 | \subsection{Window Objects} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 313 | |
| 314 | Window objects are created by \code{stdwin.open()}. They are closed |
| 315 | by their \code{close()} method or when they are garbage-collected. |
| 316 | Window objects have the following methods: |
| 317 | |
| 318 | \renewcommand{\indexsubitem}{(window method)} |
| 319 | |
| 320 | \begin{funcdesc}{begindrawing}{} |
| 321 | Return a drawing object, whose methods (described below) allow drawing |
| 322 | in the window. |
| 323 | \end{funcdesc} |
| 324 | |
| 325 | \begin{funcdesc}{change}{rect} |
| 326 | Invalidate the given rectangle; this may cause a draw event. |
| 327 | \end{funcdesc} |
| 328 | |
| 329 | \begin{funcdesc}{gettitle}{} |
| 330 | Returns the window's title string. |
| 331 | \end{funcdesc} |
| 332 | |
| 333 | \begin{funcdesc}{getdocsize}{} |
| 334 | \begin{sloppypar} |
| 335 | Return a pair of integers giving the size of the document as set by |
| 336 | \code{setdocsize()}. |
| 337 | \end{sloppypar} |
| 338 | \end{funcdesc} |
| 339 | |
| 340 | \begin{funcdesc}{getorigin}{} |
| 341 | Return a pair of integers giving the origin of the window with respect |
| 342 | to the document. |
| 343 | \end{funcdesc} |
| 344 | |
| 345 | \begin{funcdesc}{gettitle}{} |
| 346 | Return the window's title string. |
| 347 | \end{funcdesc} |
| 348 | |
| 349 | \begin{funcdesc}{getwinsize}{} |
| 350 | Return a pair of integers giving the size of the window. |
| 351 | \end{funcdesc} |
| 352 | |
| 353 | \begin{funcdesc}{getwinpos}{} |
| 354 | Return a pair of integers giving the position of the window's upper |
| 355 | left corner (relative to the upper left corner of the screen). |
| 356 | \end{funcdesc} |
| 357 | |
| 358 | \begin{funcdesc}{menucreate}{title} |
| 359 | Create a menu object referring to a local menu (a menu that appears |
| 360 | only in this window). |
| 361 | Methods of menu objects are described below. |
| 362 | {\bf Warning:} the menu only appears as long as the object |
| 363 | returned by this call exists. |
| 364 | \end{funcdesc} |
| 365 | |
| 366 | \begin{funcdesc}{scroll}{rect\, point} |
| 367 | Scroll the given rectangle by the vector given by the point. |
| 368 | \end{funcdesc} |
| 369 | |
| 370 | \begin{funcdesc}{setdocsize}{point} |
| 371 | Set the size of the drawing document. |
| 372 | \end{funcdesc} |
| 373 | |
| 374 | \begin{funcdesc}{setorigin}{point} |
| 375 | Move the origin of the window (its upper left corner) |
| 376 | to the given point in the document. |
| 377 | \end{funcdesc} |
| 378 | |
| 379 | \begin{funcdesc}{setselection}{i\, str} |
| 380 | Attempt to set X11 selection number |
| 381 | \var{i} |
| 382 | to the string |
| 383 | \var{str}. |
| 384 | (See stdwin method |
| 385 | \code{getselection()} |
| 386 | for the meaning of |
| 387 | \var{i}.) |
| 388 | Return true if it succeeds. |
| 389 | If succeeds, the window ``owns'' the selection until |
Guido van Rossum | 16d6e71 | 1994-08-08 12:30:22 +0000 | [diff] [blame] | 390 | (a) another application takes ownership of the selection; or |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 391 | (b) the window is deleted; or |
| 392 | (c) the application clears ownership by calling |
| 393 | \code{stdwin.resetselection(\var{i})}. |
| 394 | When another application takes ownership of the selection, a |
| 395 | \code{WE_LOST_SEL} |
| 396 | event is received for no particular window and with the selection number |
| 397 | as detail. |
| 398 | Ignored on the Macintosh. |
| 399 | \end{funcdesc} |
| 400 | |
| 401 | \begin{funcdesc}{settimer}{dsecs} |
| 402 | Schedule a timer event for the window in |
| 403 | \code{\var{dsecs}/10} |
| 404 | seconds. |
| 405 | \end{funcdesc} |
| 406 | |
| 407 | \begin{funcdesc}{settitle}{title} |
| 408 | Set the window's title string. |
| 409 | \end{funcdesc} |
| 410 | |
| 411 | \begin{funcdesc}{setwincursor}{name} |
| 412 | \begin{sloppypar} |
| 413 | Set the window cursor to a cursor of the given name. |
| 414 | It raises the |
| 415 | \code{RuntimeError} |
| 416 | exception if no cursor of the given name exists. |
| 417 | Suitable names include |
| 418 | \code{'ibeam'}, |
| 419 | \code{'arrow'}, |
| 420 | \code{'cross'}, |
| 421 | \code{'watch'} |
| 422 | and |
| 423 | \code{'plus'}. |
| 424 | On X11, there are many more (see |
| 425 | \file{<X11/cursorfont.h>}). |
| 426 | \end{sloppypar} |
| 427 | \end{funcdesc} |
| 428 | |
| 429 | \begin{funcdesc}{setwinpos}{h\, v} |
| 430 | Set the the position of the window's upper left corner (relative to |
| 431 | the upper left corner of the screen). |
| 432 | \end{funcdesc} |
| 433 | |
| 434 | \begin{funcdesc}{setwinsize}{width\, height} |
| 435 | Set the window's size. |
| 436 | \end{funcdesc} |
| 437 | |
| 438 | \begin{funcdesc}{show}{rect} |
| 439 | Try to ensure that the given rectangle of the document is visible in |
| 440 | the window. |
| 441 | \end{funcdesc} |
| 442 | |
| 443 | \begin{funcdesc}{textcreate}{rect} |
| 444 | Create a text-edit object in the document at the given rectangle. |
| 445 | Methods of text-edit objects are described below. |
| 446 | \end{funcdesc} |
| 447 | |
| 448 | \begin{funcdesc}{setactive}{} |
| 449 | Attempt to make this window the active window. If successful, this |
| 450 | will generate a WE_ACTIVATE event (and a WE_DEACTIVATE event in case |
| 451 | another window in this application became inactive). |
| 452 | \end{funcdesc} |
| 453 | |
| 454 | \begin{funcdesc}{close}{} |
| 455 | Discard the window object. It should not be used again. |
| 456 | \end{funcdesc} |
| 457 | |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 458 | \subsection{Drawing Objects} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 459 | |
| 460 | Drawing objects are created exclusively by the window method |
| 461 | \code{begindrawing()}. |
| 462 | Only one drawing object can exist at any given time; the drawing object |
| 463 | must be deleted to finish drawing. |
| 464 | No drawing object may exist when |
| 465 | \code{stdwin.getevent()} |
| 466 | is called. |
| 467 | Drawing objects have the following methods: |
| 468 | |
| 469 | \renewcommand{\indexsubitem}{(drawing method)} |
| 470 | |
| 471 | \begin{funcdesc}{box}{rect} |
| 472 | Draw a box just inside a rectangle. |
| 473 | \end{funcdesc} |
| 474 | |
| 475 | \begin{funcdesc}{circle}{center\, radius} |
| 476 | Draw a circle with given center point and radius. |
| 477 | \end{funcdesc} |
| 478 | |
| 479 | \begin{funcdesc}{elarc}{center\, \(rh\, rv\)\, \(a1\, a2\)} |
| 480 | Draw an elliptical arc with given center point. |
| 481 | \code{(\var{rh}, \var{rv})} |
| 482 | gives the half sizes of the horizontal and vertical radii. |
| 483 | \code{(\var{a1}, \var{a2})} |
| 484 | gives the angles (in degrees) of the begin and end points. |
| 485 | 0 degrees is at 3 o'clock, 90 degrees is at 12 o'clock. |
| 486 | \end{funcdesc} |
| 487 | |
| 488 | \begin{funcdesc}{erase}{rect} |
| 489 | Erase a rectangle. |
| 490 | \end{funcdesc} |
| 491 | |
| 492 | \begin{funcdesc}{fillcircle}{center\, radius} |
| 493 | Draw a filled circle with given center point and radius. |
| 494 | \end{funcdesc} |
| 495 | |
| 496 | \begin{funcdesc}{fillelarc}{center\, \(rh\, rv\)\, \(a1\, a2\)} |
| 497 | Draw a filled elliptical arc; arguments as for \code{elarc}. |
| 498 | \end{funcdesc} |
| 499 | |
| 500 | \begin{funcdesc}{fillpoly}{points} |
| 501 | Draw a filled polygon given by a list (or tuple) of points. |
| 502 | \end{funcdesc} |
| 503 | |
| 504 | \begin{funcdesc}{invert}{rect} |
| 505 | Invert a rectangle. |
| 506 | \end{funcdesc} |
| 507 | |
| 508 | \begin{funcdesc}{line}{p1\, p2} |
| 509 | Draw a line from point |
| 510 | \var{p1} |
| 511 | to |
| 512 | \var{p2}. |
| 513 | \end{funcdesc} |
| 514 | |
| 515 | \begin{funcdesc}{paint}{rect} |
| 516 | Fill a rectangle. |
| 517 | \end{funcdesc} |
| 518 | |
| 519 | \begin{funcdesc}{poly}{points} |
| 520 | Draw the lines connecting the given list (or tuple) of points. |
| 521 | \end{funcdesc} |
| 522 | |
| 523 | \begin{funcdesc}{shade}{rect\, percent} |
| 524 | Fill a rectangle with a shading pattern that is about |
| 525 | \var{percent} |
| 526 | percent filled. |
| 527 | \end{funcdesc} |
| 528 | |
| 529 | \begin{funcdesc}{text}{p\, str} |
| 530 | Draw a string starting at point p (the point specifies the |
| 531 | top left coordinate of the string). |
| 532 | \end{funcdesc} |
| 533 | |
| 534 | \begin{funcdesc}{xorcircle}{center\, radius} |
| 535 | \funcline{xorelarc}{center\, \(rh\, rv\)\, \(a1\, a2\)} |
| 536 | \funcline{xorline}{p1\, p2} |
| 537 | \funcline{xorpoly}{points} |
| 538 | Draw a circle, an elliptical arc, a line or a polygon, respectively, |
| 539 | in XOR mode. |
| 540 | \end{funcdesc} |
| 541 | |
| 542 | \begin{funcdesc}{setfgcolor}{} |
| 543 | \funcline{setbgcolor}{} |
| 544 | \funcline{getfgcolor}{} |
| 545 | \funcline{getbgcolor}{} |
| 546 | These functions are similar to the corresponding functions described |
| 547 | above for the |
| 548 | \code{stdwin} |
| 549 | module, but affect or return the colors currently used for drawing |
| 550 | instead of the global default colors. |
| 551 | When a drawing object is created, its colors are set to the window's |
| 552 | default colors, which are in turn initialized from the global default |
| 553 | colors when the window is created. |
| 554 | \end{funcdesc} |
| 555 | |
| 556 | \begin{funcdesc}{setfont}{} |
| 557 | \funcline{baseline}{} |
| 558 | \funcline{lineheight}{} |
| 559 | \funcline{textbreak}{} |
| 560 | \funcline{textwidth}{} |
| 561 | These functions are similar to the corresponding functions described |
| 562 | above for the |
| 563 | \code{stdwin} |
| 564 | module, but affect or use the current drawing font instead of |
| 565 | the global default font. |
| 566 | When a drawing object is created, its font is set to the window's |
| 567 | default font, which is in turn initialized from the global default |
| 568 | font when the window is created. |
| 569 | \end{funcdesc} |
| 570 | |
| 571 | \begin{funcdesc}{bitmap}{point\, bitmap\, mask} |
| 572 | Draw the \var{bitmap} with its top left corner at \var{point}. |
| 573 | If the optional \var{mask} argument is present, it should be either |
| 574 | the same object as \var{bitmap}, to draw only those bits that are set |
| 575 | in the bitmap, in the foreground color, or \code{None}, to draw all |
| 576 | bits (ones are drawn in the foreground color, zeros in the background |
| 577 | color). |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 578 | Not available on the Macintosh. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 579 | \end{funcdesc} |
| 580 | |
| 581 | \begin{funcdesc}{cliprect}{rect} |
| 582 | Set the ``clipping region'' to a rectangle. |
| 583 | The clipping region limits the effect of all drawing operations, until |
| 584 | it is changed again or until the drawing object is closed. When a |
| 585 | drawing object is created the clipping region is set to the entire |
| 586 | window. When an object to be drawn falls partly outside the clipping |
| 587 | region, the set of pixels drawn is the intersection of the clipping |
| 588 | region and the set of pixels that would be drawn by the same operation |
| 589 | in the absence of a clipping region. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 590 | \end{funcdesc} |
| 591 | |
| 592 | \begin{funcdesc}{noclip}{} |
| 593 | Reset the clipping region to the entire window. |
| 594 | \end{funcdesc} |
| 595 | |
| 596 | \begin{funcdesc}{close}{} |
| 597 | \funcline{enddrawing}{} |
| 598 | Discard the drawing object. It should not be used again. |
| 599 | \end{funcdesc} |
| 600 | |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 601 | \subsection{Menu Objects} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 602 | |
| 603 | A menu object represents a menu. |
| 604 | The menu is destroyed when the menu object is deleted. |
| 605 | The following methods are defined: |
| 606 | |
| 607 | \renewcommand{\indexsubitem}{(menu method)} |
| 608 | |
| 609 | \begin{funcdesc}{additem}{text\, shortcut} |
| 610 | Add a menu item with given text. |
| 611 | The shortcut must be a string of length 1, or omitted (to specify no |
| 612 | shortcut). |
| 613 | \end{funcdesc} |
| 614 | |
| 615 | \begin{funcdesc}{setitem}{i\, text} |
| 616 | Set the text of item number |
| 617 | \var{i}. |
| 618 | \end{funcdesc} |
| 619 | |
| 620 | \begin{funcdesc}{enable}{i\, flag} |
| 621 | Enable or disables item |
| 622 | \var{i}. |
| 623 | \end{funcdesc} |
| 624 | |
| 625 | \begin{funcdesc}{check}{i\, flag} |
| 626 | Set or clear the |
| 627 | \dfn{check mark} |
| 628 | for item |
| 629 | \var{i}. |
| 630 | \end{funcdesc} |
| 631 | |
| 632 | \begin{funcdesc}{close}{} |
| 633 | Discard the menu object. It should not be used again. |
| 634 | \end{funcdesc} |
| 635 | |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 636 | \subsection{Bitmap Objects} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 637 | |
| 638 | A bitmap represents a rectangular array of bits. |
| 639 | The top left bit has coordinate (0, 0). |
| 640 | A bitmap can be drawn with the \code{bitmap} method of a drawing object. |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 641 | Bitmaps are currently not available on the Macintosh. |
| 642 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 643 | The following methods are defined: |
| 644 | |
| 645 | \renewcommand{\indexsubitem}{(bitmap method)} |
| 646 | |
| 647 | \begin{funcdesc}{getsize}{} |
| 648 | Return a tuple representing the width and height of the bitmap. |
| 649 | (This returns the values that have been passed to the \code{newbitmap} |
| 650 | function.) |
| 651 | \end{funcdesc} |
| 652 | |
| 653 | \begin{funcdesc}{setbit}{point\, bit} |
| 654 | Set the value of the bit indicated by \var{point} to \var{bit}. |
| 655 | \end{funcdesc} |
| 656 | |
| 657 | \begin{funcdesc}{getbit}{point} |
| 658 | Return the value of the bit indicated by \var{point}. |
| 659 | \end{funcdesc} |
| 660 | |
| 661 | \begin{funcdesc}{close}{} |
| 662 | Discard the bitmap object. It should not be used again. |
| 663 | \end{funcdesc} |
| 664 | |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 665 | \subsection{Text-edit Objects} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 666 | |
| 667 | A text-edit object represents a text-edit block. |
| 668 | For semantics, see the STDWIN documentation for C programmers. |
| 669 | The following methods exist: |
| 670 | |
| 671 | \renewcommand{\indexsubitem}{(text-edit method)} |
| 672 | |
| 673 | \begin{funcdesc}{arrow}{code} |
| 674 | Pass an arrow event to the text-edit block. |
| 675 | The |
| 676 | \var{code} |
| 677 | must be one of |
| 678 | \code{WC_LEFT}, |
| 679 | \code{WC_RIGHT}, |
| 680 | \code{WC_UP} |
| 681 | or |
| 682 | \code{WC_DOWN} |
| 683 | (see module |
| 684 | \code{stdwinevents}). |
| 685 | \end{funcdesc} |
| 686 | |
| 687 | \begin{funcdesc}{draw}{rect} |
| 688 | Pass a draw event to the text-edit block. |
| 689 | The rectangle specifies the redraw area. |
| 690 | \end{funcdesc} |
| 691 | |
| 692 | \begin{funcdesc}{event}{type\, window\, detail} |
| 693 | Pass an event gotten from |
| 694 | \code{stdwin.getevent()} |
| 695 | to the text-edit block. |
| 696 | Return true if the event was handled. |
| 697 | \end{funcdesc} |
| 698 | |
| 699 | \begin{funcdesc}{getfocus}{} |
| 700 | Return 2 integers representing the start and end positions of the |
| 701 | focus, usable as slice indices on the string returned by |
| 702 | \code{gettext()}. |
| 703 | \end{funcdesc} |
| 704 | |
| 705 | \begin{funcdesc}{getfocustext}{} |
| 706 | Return the text in the focus. |
| 707 | \end{funcdesc} |
| 708 | |
| 709 | \begin{funcdesc}{getrect}{} |
| 710 | Return a rectangle giving the actual position of the text-edit block. |
| 711 | (The bottom coordinate may differ from the initial position because |
| 712 | the block automatically shrinks or grows to fit.) |
| 713 | \end{funcdesc} |
| 714 | |
| 715 | \begin{funcdesc}{gettext}{} |
| 716 | Return the entire text buffer. |
| 717 | \end{funcdesc} |
| 718 | |
| 719 | \begin{funcdesc}{move}{rect} |
| 720 | Specify a new position for the text-edit block in the document. |
| 721 | \end{funcdesc} |
| 722 | |
| 723 | \begin{funcdesc}{replace}{str} |
| 724 | Replace the text in the focus by the given string. |
| 725 | The new focus is an insert point at the end of the string. |
| 726 | \end{funcdesc} |
| 727 | |
| 728 | \begin{funcdesc}{setfocus}{i\, j} |
| 729 | Specify the new focus. |
| 730 | Out-of-bounds values are silently clipped. |
| 731 | \end{funcdesc} |
| 732 | |
| 733 | \begin{funcdesc}{settext}{str} |
| 734 | Replace the entire text buffer by the given string and set the focus |
| 735 | to \code{(0, 0)}. |
| 736 | \end{funcdesc} |
| 737 | |
| 738 | \begin{funcdesc}{setview}{rect} |
| 739 | Set the view rectangle to \var{rect}. If \var{rect} is \code{None}, |
| 740 | viewing mode is reset. In viewing mode, all output from the text-edit |
| 741 | object is clipped to the viewing rectangle. This may be useful to |
| 742 | implement your own scrolling text subwindow. |
| 743 | \end{funcdesc} |
| 744 | |
| 745 | \begin{funcdesc}{close}{} |
| 746 | Discard the text-edit object. It should not be used again. |
| 747 | \end{funcdesc} |
| 748 | |
| 749 | \subsection{Example} |
Guido van Rossum | 86cb092 | 1995-03-20 12:59:56 +0000 | [diff] [blame] | 750 | \nodename{STDWIN Example} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 751 | |
| 752 | Here is a minimal example of using STDWIN in Python. |
| 753 | It creates a window and draws the string ``Hello world'' in the top |
| 754 | left corner of the window. |
| 755 | The window will be correctly redrawn when covered and re-exposed. |
| 756 | The program quits when the close icon or menu item is requested. |
| 757 | |
| 758 | \bcode\begin{verbatim} |
| 759 | import stdwin |
| 760 | from stdwinevents import * |
| 761 | |
| 762 | def main(): |
| 763 | mywin = stdwin.open('Hello') |
| 764 | # |
| 765 | while 1: |
| 766 | (type, win, detail) = stdwin.getevent() |
| 767 | if type == WE_DRAW: |
| 768 | draw = win.begindrawing() |
| 769 | draw.text((0, 0), 'Hello, world') |
| 770 | del draw |
| 771 | elif type == WE_CLOSE: |
| 772 | break |
| 773 | |
| 774 | main() |
| 775 | \end{verbatim}\ecode |
| 776 | |
| 777 | \section{Standard Module \sectcode{stdwinevents}} |
| 778 | \stmodindex{stdwinevents} |
| 779 | |
| 780 | This module defines constants used by STDWIN for event types |
| 781 | (\code{WE_ACTIVATE} etc.), command codes (\code{WC_LEFT} etc.) |
| 782 | and selection types (\code{WS_PRIMARY} etc.). |
| 783 | Read the file for details. |
| 784 | Suggested usage is |
| 785 | |
| 786 | \bcode\begin{verbatim} |
| 787 | >>> from stdwinevents import * |
| 788 | >>> |
| 789 | \end{verbatim}\ecode |
| 790 | |
| 791 | \section{Standard Module \sectcode{rect}} |
| 792 | \stmodindex{rect} |
| 793 | |
| 794 | This module contains useful operations on rectangles. |
| 795 | A rectangle is defined as in module |
| 796 | \code{stdwin}: |
| 797 | a pair of points, where a point is a pair of integers. |
| 798 | For example, the rectangle |
| 799 | |
| 800 | \bcode\begin{verbatim} |
| 801 | (10, 20), (90, 80) |
| 802 | \end{verbatim}\ecode |
| 803 | |
| 804 | is a rectangle whose left, top, right and bottom edges are 10, 20, 90 |
| 805 | and 80, respectively. |
| 806 | Note that the positive vertical axis points down (as in |
| 807 | \code{stdwin}). |
| 808 | |
| 809 | The module defines the following objects: |
| 810 | |
| 811 | \renewcommand{\indexsubitem}{(in module rect)} |
| 812 | \begin{excdesc}{error} |
| 813 | The exception raised by functions in this module when they detect an |
| 814 | error. |
| 815 | The exception argument is a string describing the problem in more |
| 816 | detail. |
| 817 | \end{excdesc} |
| 818 | |
| 819 | \begin{datadesc}{empty} |
| 820 | The rectangle returned when some operations return an empty result. |
| 821 | This makes it possible to quickly check whether a result is empty: |
| 822 | |
| 823 | \bcode\begin{verbatim} |
| 824 | >>> import rect |
| 825 | >>> r1 = (10, 20), (90, 80) |
| 826 | >>> r2 = (0, 0), (10, 20) |
| 827 | >>> r3 = rect.intersect([r1, r2]) |
| 828 | >>> if r3 is rect.empty: print 'Empty intersection' |
| 829 | Empty intersection |
| 830 | >>> |
| 831 | \end{verbatim}\ecode |
| 832 | \end{datadesc} |
| 833 | |
| 834 | \begin{funcdesc}{is_empty}{r} |
| 835 | Returns true if the given rectangle is empty. |
| 836 | A rectangle |
| 837 | \code{(\var{left}, \var{top}), (\var{right}, \var{bottom})} |
| 838 | is empty if |
| 839 | \iftexi |
| 840 | \code{\var{left} >= \var{right}} or \code{\var{top} => \var{bottom}}. |
| 841 | \else |
| 842 | $\var{left} \geq \var{right}$ or $\var{top} \geq \var{bottom}$. |
| 843 | %%JHXXX{\em left~$\geq$~right} or {\em top~$\leq$~bottom}. |
| 844 | \fi |
| 845 | \end{funcdesc} |
| 846 | |
| 847 | \begin{funcdesc}{intersect}{list} |
| 848 | Returns the intersection of all rectangles in the list argument. |
| 849 | It may also be called with a tuple argument. |
| 850 | Raises |
| 851 | \code{rect.error} |
| 852 | if the list is empty. |
| 853 | Returns |
| 854 | \code{rect.empty} |
| 855 | if the intersection of the rectangles is empty. |
| 856 | \end{funcdesc} |
| 857 | |
| 858 | \begin{funcdesc}{union}{list} |
| 859 | Returns the smallest rectangle that contains all non-empty rectangles in |
| 860 | the list argument. |
| 861 | It may also be called with a tuple argument or with two or more |
| 862 | rectangles as arguments. |
| 863 | Returns |
| 864 | \code{rect.empty} |
| 865 | if the list is empty or all its rectangles are empty. |
| 866 | \end{funcdesc} |
| 867 | |
| 868 | \begin{funcdesc}{pointinrect}{point\, rect} |
| 869 | Returns true if the point is inside the rectangle. |
| 870 | By definition, a point |
| 871 | \code{(\var{h}, \var{v})} |
| 872 | is inside a rectangle |
| 873 | \code{(\var{left}, \var{top}), (\var{right}, \var{bottom})} if |
| 874 | \iftexi |
| 875 | \code{\var{left} <= \var{h} < \var{right}} and |
| 876 | \code{\var{top} <= \var{v} < \var{bottom}}. |
| 877 | \else |
| 878 | $\var{left} \leq \var{h} < \var{right}$ and |
| 879 | $\var{top} \leq \var{v} < \var{bottom}$. |
| 880 | \fi |
| 881 | \end{funcdesc} |
| 882 | |
| 883 | \begin{funcdesc}{inset}{rect\, \(dh\, dv\)} |
| 884 | Returns a rectangle that lies inside the |
| 885 | \code{rect} |
| 886 | argument by |
| 887 | \var{dh} |
| 888 | pixels horizontally |
| 889 | and |
| 890 | \var{dv} |
| 891 | pixels |
| 892 | vertically. |
| 893 | If |
| 894 | \var{dh} |
| 895 | or |
| 896 | \var{dv} |
| 897 | is negative, the result lies outside |
| 898 | \var{rect}. |
| 899 | \end{funcdesc} |
| 900 | |
| 901 | \begin{funcdesc}{rect2geom}{rect} |
| 902 | Converts a rectangle to geometry representation: |
| 903 | \code{(\var{left}, \var{top}), (\var{width}, \var{height})}. |
| 904 | \end{funcdesc} |
| 905 | |
| 906 | \begin{funcdesc}{geom2rect}{geom} |
| 907 | Converts a rectangle given in geometry representation back to the |
| 908 | standard rectangle representation |
| 909 | \code{(\var{left}, \var{top}), (\var{right}, \var{bottom})}. |
| 910 | \end{funcdesc} |