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