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