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