3 # A minimal single-window text editor using STDWIN's text objects.
5 # Usage: microedit file
7 # This is not intended as a real application but as an introduction
8 # to STDWIN programming in Python, especially text objects.
9 # Once you understand microedit.py, study miniedit.py to learn
10 # about multiple windows and menus, cut and paste, etc.
15 from stdwinevents
import *
22 # Get the filename argument and read its contents as one very
24 # An exception will terminate the program if there is no argument
25 # or if the file could not be read...
27 filename
= sys
.argv
[1]
28 fp
= open(filename
, 'r')
30 del fp
# Close the file
32 # Create the window, using the filename as window title
34 window
= stdwin
.open(filename
)
36 # Add a simple File menu to the window with two items
38 filemenu
= window
.menucreate('File')
39 filemenu
.additem('Save', 'S') # Item 0 (shortcut Meta-S)
40 filemenu
.additem('Save As...') # Item 1
42 # Create a text object occupying the entire window
43 # and fill it with the file's contents
45 corner
= window
.getwinsize() # (width, height)
46 area
= (0, 0), corner
# Rectangle as large as the window
47 text
= window
.textcreate(area
)
48 text
.settext(contents
)
49 del contents
# Get rid of contents object
50 fix_textsize(window
, text
) # Set document size accordingly
52 # Main event loop -- stop if a close request comes in.
54 # STDWIN applications should regularly call stdwin.getevent()
55 # otherwise the windows won't function as expected.
61 type, w
, detail
= e
= stdwin
.getevent()
63 # Event decoding switch
66 break # Stop (no check for saved file!)
69 # The window was resized --
70 # let the text object recompute the line breaks
71 # and change the document size accordingly,
72 # so scroll bars will work
74 fix_textsize(window
, text
)
77 # Execute a file menu request (our only menu)
82 # "Save": save to the current filename
84 dummy
= save_file(window
, text
, filename
)
87 # "Save As": ask a new filename, save to it,
88 # and make it the current filename
90 # NB: askfile raises KeyboardInterrupt
91 # if the user cancels the dialog, hence
95 newfile
= stdwin
.askfile( \
96 'Save as:', filename
, 1)
97 except KeyboardInterrupt:
100 if save_file(window
, text
, newfile
):
102 window
.settitle(filename
)
105 # The text object has handled the event.
106 # Fix the document size if necessary.
107 # Note: this sometimes fixes the size
108 # unnecessarily, e.g., for arrow keys.
110 if type in (WE_CHAR
, WE_COMMAND
):
111 fix_docsize(window
, text
)
114 # Save the window's contents to the filename.
115 # If the open() fails, put up a warning message and return 0;
116 # if the save succeeds, return 1.
118 def save_file(window
, text
, filename
):
120 # Open the file for writing, handling exceptions
123 fp
= open(filename
, 'w')
125 stdwin
.message('Cannot create ' + filename
)
128 # Get the contents of the text object as one very long string
130 contents
= text
.gettext()
132 # Write the contents to the file
136 # The file is automatically closed when this routine returns
141 # Change the size of the text object to fit in the window,
142 # and then fix the window's document size to fit around the text object.
144 def fix_textsize(window
, text
):
146 # Compute a rectangle as large as the window
148 corner
= window
.getwinsize() # (width, height)
149 area
= (0, 0), (corner
)
151 # Move the text object to this rectangle.
152 # Note: text.move() ignores the bottom coordinate!
156 # Now fix the document size accordingly
158 fix_docsize(window
, text
)
161 # Fix the document size, after the text has changed
163 def fix_docsize(window
, text
):
165 # Get the actual rectangle occupied by the text object.
166 # This has the same left, top and right, but a different bottom.
168 area
= text
.getrect()
170 # Compute the true height of the text object
172 origin
, corner
= area
173 width
, height
= corner
175 # Set the document height to the text object's height.
176 # The width is zero since we don't want a horizontal scroll bar.
178 window
.setdocsize(0, height
)
181 # Once all functions are defined, call main()