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