import os import tempfile import tkFileDialog import tkMessageBox from IdleConf import idleconf #$ event <> #$ win #$ unix #$ event <> #$ win #$ unix #$ event <> #$ win #$ unix #$ event <> #$ win #$ unix #$ event <> #$ win #$ unix class IOBinding: def __init__(self, editwin): self.editwin = editwin self.text = editwin.text self.__id_open = self.text.bind("<>", self.open) self.__id_save = self.text.bind("<>", self.save) self.__id_saveas = self.text.bind("<>", self.save_as) self.__id_savecopy = self.text.bind("<>", self.save_a_copy) self.__id_print = self.text.bind("<>", self.print_window) def close(self): # Undo command bindings self.text.unbind("<>", self.__id_open) self.text.unbind("<>", self.__id_save) self.text.unbind("<>",self.__id_saveas) self.text.unbind("<>", self.__id_savecopy) self.text.unbind("<>", self.__id_print) # Break cycles self.editwin = None self.text = None self.filename_change_hook = None def get_saved(self): return self.editwin.get_saved() def set_saved(self, flag): self.editwin.set_saved(flag) def reset_undo(self): self.editwin.reset_undo() filename_change_hook = None def set_filename_change_hook(self, hook): self.filename_change_hook = hook filename = None def set_filename(self, filename): self.filename = filename self.set_saved(1) if self.filename_change_hook: self.filename_change_hook() def open(self, event): if self.editwin.flist: filename = self.askopenfile() if filename: self.editwin.flist.open(filename) else: self.text.focus_set() return "break" # Code for use outside IDLE: if self.get_saved(): reply = self.maybesave() if reply == "cancel": self.text.focus_set() return "break" filename = self.askopenfile() if filename: self.loadfile(filename) else: self.text.focus_set() return "break" def loadfile(self, filename): try: f = open(filename) chars = f.read() f.close() except IOError, msg: tkMessageBox.showerror("I/O Error", str(msg), master=self.text) return False self.text.delete("1.0", "end") self.set_filename(None) self.text.insert("1.0", chars) self.reset_undo() self.set_filename(filename) self.text.mark_set("insert", "1.0") self.text.see("insert") return True def maybesave(self): if self.get_saved(): return "yes" message = "Do you want to save %s before closing?" % ( self.filename or "this untitled document") m = tkMessageBox.Message( title="Save On Close", message=message, icon=tkMessageBox.QUESTION, type=tkMessageBox.YESNOCANCEL, master=self.text) reply = m.show() if reply == "yes": self.save(None) if not self.get_saved(): reply = "cancel" self.text.focus_set() return reply def save(self, event): if not self.filename: self.save_as(event) else: if self.writefile(self.filename): self.set_saved(1) self.text.focus_set() return "break" def save_as(self, event): filename = self.asksavefile() if filename: if self.writefile(filename): self.set_filename(filename) self.set_saved(1) self.text.focus_set() return "break" def save_a_copy(self, event): filename = self.asksavefile() if filename: self.writefile(filename) self.text.focus_set() return "break" def print_window(self, event): tempfilename = None if self.get_saved(): filename = self.filename else: filename = tempfilename = tempfile.mktemp() if not self.writefile(filename): os.unlink(tempfilename) return "break" edconf = idleconf.getsection('EditorWindow') command = edconf.get('print-command') command = command % filename if os.name == 'posix': command = command + " 2>&1" pipe = os.popen(command, "r") output = pipe.read().strip() status = pipe.close() if status: output = "Printing failed (exit status 0x%x)\n" % status + output if output: output = "Printing command: %s\n" % repr(command) + output tkMessageBox.showerror("Print status", output, master=self.text) return "break" def writefile(self, filename): self.fixlastline() chars = str(self.text.get("1.0", "end-1c")) try: f = open(filename, "w") f.write(chars) f.close() ## print "saved to", `filename` return True except IOError, msg: tkMessageBox.showerror("I/O Error", str(msg), master=self.text) return False def fixlastline(self): c = self.text.get("end-2c") if c != '\n': self.text.insert("end-1c", "\n") opendialog = None savedialog = None filetypes = [ ("Python and text files", "*.py *.pyw *.txt", "TEXT"), ("All text files", "*", "TEXT"), ("All files", "*"), ] def askopenfile(self): dir, base = self.defaultfilename("open") if not self.opendialog: self.opendialog = tkFileDialog.Open(master=self.text, filetypes=self.filetypes) return self.opendialog.show(initialdir=dir, initialfile=base) def defaultfilename(self, mode="open"): if self.filename: return os.path.split(self.filename) else: try: pwd = os.getcwd() except os.error: pwd = "" return pwd, "" def asksavefile(self): dir, base = self.defaultfilename("save") if not self.savedialog: self.savedialog = tkFileDialog.SaveAs(master=self.text, filetypes=self.filetypes) return self.savedialog.show(initialdir=dir, initialfile=base) def test(): root = Tk() class MyEditWin: def __init__(self, text): self.text = text self.flist = None self.text.bind("", self.open) self.text.bind("", self.save) self.text.bind("", self.save_as) self.text.bind("", self.save_a_copy) def get_saved(self): return 0 def set_saved(self, flag): pass def reset_undo(self): pass def open(self, event): self.text.event_generate("<>") def save(self, event): self.text.event_generate("<>") def save_as(self, event): self.text.event_generate("<>") def save_a_copy(self, event): self.text.event_generate("<>") text = Text(root) text.pack() text.focus_set() editwin = MyEditWin(text) io = IOBinding(editwin) root.mainloop() if __name__ == "__main__": from Tkinter import * test()