def save_as_dialog(window): prompt = 'Save ' + window.gettitle() + ' as:' filename = stdwin.askfile(prompt, window.filename, 1) if save_file(window, filename): window.filename = filename window.settitle(filename) window.changed = 0
def do_save(win): try: if win.filename == '': win.filename = stdwin.askfile('Open file', '', 1) f = open(win.filename, 'w') f.write(win.editor.gettext()) # except KeyboardInterrupt: pass # Don't give an error on cancel
def do_open(win): try: filename = stdwin.askfile('Open file', '', 0) win = makewindow() win.filename = filename win.editor.replace(open(filename, 'r').read()) win.editor.setfocus(0, 0) win.settitle(win.filename) # except KeyboardInterrupt: pass # Don't give an error on cancel
def save(self): try: file = stdwin.askfile('Save to file:', '', 1) except KeyboardInterrupt: return file = string.strip(file) if not file: return try: f = open(file, 'w') except IOError, msg: stdwin.message(file + ': ' + msg) return
def save_source(self): try: file = stdwin.askfile('Save source to file:', '', 1) except KeyboardInterrupt: return file = string.strip(file) if not file: return try: f = open(file, 'w') f.write(self.cur_data) f.close() except IOError, msg: stdwin.message(file + ': ' + msg)
def save_hook(self): if not G.data: stdwin.fleep() else: prompt = 'Store sampled data on file: ' try: G.savefile = stdwin.askfile(prompt, G.savefile, 1) except KeyboardInterrupt: return try: fp = open(G.savefile, 'w') fp.write(Magics[G.rate] + G.data) except: stdwin.message('Cannot create ' + file)
def save_copy_dialog(window): prompt = 'Save a copy of ' + window.gettitle() + ' as:' filename = stdwin.askfile(prompt, window.filename, 1) void = save_file(window, filename)
def main(): # # Get the filename argument and read its contents as one very # large string. # An exception will terminate the program if there is no argument # or if the file could not be read... # filename = sys.argv[1] fp = open(filename, 'r') contents = fp.read() del fp # Close the file # # Create the window, using the filename as window title # window = stdwin.open(filename) # # Add a simple File menu to the window with two items # filemenu = window.menucreate('File') filemenu.additem('Save', 'S') # Item 0 (shortcut Meta-S) filemenu.additem('Save As...') # Item 1 # # Create a text object occupying the entire window # and fill it with the file's contents # corner = window.getwinsize() # (width, height) area = (0, 0), corner # Rectangle as large as the window text = window.textcreate(area) text.settext(contents) del contents # Get rid of contents object fix_textsize(window, text) # Set document size accordingly # # Main event loop -- stop if a close request comes in. # # STDWIN applications should regularly call stdwin.getevent() # otherwise the windows won't function as expected. # while 1: # # Get the next event # type, w, detail = e = stdwin.getevent() # # Event decoding switch # if type == WE_CLOSE: break # Stop (no check for saved file!) elif type == WE_SIZE: # # The window was resized -- # let the text object recompute the line breaks # and change the document size accordingly, # so scroll bars will work # fix_textsize(window, text) elif type == WE_MENU: # # Execute a file menu request (our only menu) # menu, item = detail if item == 0: # # "Save": save to the current filename # dummy = save_file(window, text, filename) elif item == 1: # # "Save As": ask a new filename, save to it, # and make it the current filename # # NB: askfile raises KeyboardInterrupt # if the user cancels the dialog, hence # the try statement # try: newfile = stdwin.askfile( \ 'Save as:', filename, 1) except KeyboardInterrupt: newfile = '' if newfile: if save_file(window, text, newfile): filename = newfile window.settitle(filename) elif text.event(e): # # The text object has handled the event. # Fix the document size if necessary. # Note: this sometimes fixes the size # unnecessarily, e.g., for arrow keys. # if type in (WE_CHAR, WE_COMMAND): fix_docsize(window, text)
def open_dialog(current_ignored): filename = stdwin.askfile('Open file:', '', 0) open_file(filename)
def askfile(prompt, default, new): return stdwin.askfile(prompt, default, new)