David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 1 | # changes by dscherer@cmu.edu |
| 2 | # - FileList.open() takes an optional 3rd parameter action, which is |
| 3 | # called instead of creating a new EditorWindow. This enables |
| 4 | # things like 'open in same window'. |
| 5 | |
| 6 | import os |
| 7 | from Tkinter import * |
| 8 | import tkMessageBox |
| 9 | |
| 10 | import WindowList |
| 11 | |
| 12 | #$ event <<open-new-window>> |
| 13 | #$ win <Control-n> |
| 14 | #$ unix <Control-x><Control-n> |
| 15 | |
| 16 | # (This is labeled as 'Exit'in the File menu) |
| 17 | #$ event <<close-all-windows>> |
| 18 | #$ win <Control-q> |
| 19 | #$ unix <Control-x><Control-c> |
| 20 | |
| 21 | class FileList: |
| 22 | |
| 23 | from EditorWindow import EditorWindow |
| 24 | EditorWindow.Toplevel = WindowList.ListedToplevel # XXX Patch it! |
| 25 | |
| 26 | def __init__(self, root): |
| 27 | self.root = root |
| 28 | self.dict = {} |
| 29 | self.inversedict = {} |
| 30 | self.vars = {} # For EditorWindow.getrawvar (shared Tcl variables) |
| 31 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 32 | def open(self, filename, action=None): |
| 33 | assert filename |
| 34 | filename = self.canonize(filename) |
| 35 | if os.path.isdir(filename): |
| 36 | tkMessageBox.showerror( |
| 37 | "Is A Directory", |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 38 | "The path %r is a directory." % (filename,), |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 39 | master=self.root) |
| 40 | return None |
| 41 | key = os.path.normcase(filename) |
| 42 | if self.dict.has_key(key): |
| 43 | edit = self.dict[key] |
| 44 | edit.wakeup() |
| 45 | return edit |
| 46 | if not os.path.exists(filename): |
| 47 | tkMessageBox.showinfo( |
| 48 | "New File", |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 49 | "Opening non-existent file %r" % (filename,), |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 50 | master=self.root) |
| 51 | if action is None: |
| 52 | return self.EditorWindow(self, filename, key) |
| 53 | else: |
| 54 | return action(filename) |
| 55 | |
Kurt B. Kaiser | df8a40f | 2001-07-14 04:45:32 +0000 | [diff] [blame] | 56 | def gotofileline(self, filename, lineno=None): |
| 57 | edit = self.open(filename) |
| 58 | if edit is not None and lineno is not None: |
| 59 | edit.gotoline(lineno) |
| 60 | |
Kurt B. Kaiser | d2f4861 | 2003-06-05 02:34:04 +0000 | [diff] [blame] | 61 | def new(self, filename=None): |
| 62 | return self.EditorWindow(self, filename) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 63 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 64 | def close_all_callback(self, event): |
| 65 | for edit in self.inversedict.keys(): |
| 66 | reply = edit.close() |
| 67 | if reply == "cancel": |
| 68 | break |
| 69 | return "break" |
| 70 | |
| 71 | def close_edit(self, edit): |
| 72 | try: |
| 73 | key = self.inversedict[edit] |
| 74 | except KeyError: |
| 75 | print "Don't know this EditorWindow object. (close)" |
| 76 | return |
| 77 | if key: |
| 78 | del self.dict[key] |
| 79 | del self.inversedict[edit] |
| 80 | if not self.inversedict: |
| 81 | self.root.quit() |
| 82 | |
| 83 | def filename_changed_edit(self, edit): |
| 84 | edit.saved_change_hook() |
| 85 | try: |
| 86 | key = self.inversedict[edit] |
| 87 | except KeyError: |
| 88 | print "Don't know this EditorWindow object. (rename)" |
| 89 | return |
| 90 | filename = edit.io.filename |
| 91 | if not filename: |
| 92 | if key: |
| 93 | del self.dict[key] |
| 94 | self.inversedict[edit] = None |
| 95 | return |
| 96 | filename = self.canonize(filename) |
| 97 | newkey = os.path.normcase(filename) |
| 98 | if newkey == key: |
| 99 | return |
| 100 | if self.dict.has_key(newkey): |
| 101 | conflict = self.dict[newkey] |
| 102 | self.inversedict[conflict] = None |
| 103 | tkMessageBox.showerror( |
| 104 | "Name Conflict", |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 105 | "You now have multiple edit windows open for %r" % (filename,), |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 106 | master=self.root) |
| 107 | self.dict[newkey] = edit |
| 108 | self.inversedict[edit] = newkey |
| 109 | if key: |
| 110 | try: |
| 111 | del self.dict[key] |
| 112 | except KeyError: |
| 113 | pass |
| 114 | |
| 115 | def canonize(self, filename): |
| 116 | if not os.path.isabs(filename): |
| 117 | try: |
| 118 | pwd = os.getcwd() |
| 119 | except os.error: |
| 120 | pass |
| 121 | else: |
| 122 | filename = os.path.join(pwd, filename) |
| 123 | return os.path.normpath(filename) |
| 124 | |
| 125 | |
Kurt B. Kaiser | df8a40f | 2001-07-14 04:45:32 +0000 | [diff] [blame] | 126 | def _test(): |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 127 | from EditorWindow import fixwordbreaks |
| 128 | import sys |
| 129 | root = Tk() |
| 130 | fixwordbreaks(root) |
| 131 | root.withdraw() |
| 132 | flist = FileList(root) |
| 133 | if sys.argv[1:]: |
| 134 | for filename in sys.argv[1:]: |
| 135 | flist.open(filename) |
| 136 | else: |
| 137 | flist.new() |
| 138 | if flist.inversedict: |
| 139 | root.mainloop() |
| 140 | |
| 141 | if __name__ == '__main__': |
Kurt B. Kaiser | df8a40f | 2001-07-14 04:45:32 +0000 | [diff] [blame] | 142 | _test() |