1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 '''Plugin provides basic dialogs for Save, Save As, Open, New, and Close
22 functionality. Shows a dialog for Save As and Open. Checks if the existing
23 document is modified before closing or overwriting it.
24
25 This is a simple plugin to get I/O up ASAP and to allow a simple editor for
26 those that don't need more. A more robust implementation might do things like
27 take care of automatically FTPing or CVSing or provide other virtual filesystem
28 functionality.
29
30 This plugin provides the following actions:
31 SaveActive
32 Save the active buffer. Shows a dialog to choose a filename only
33 if the buffer does not have one associated yet.
34 SaveActiveAs
35 Save the active buffer, showing a dialog to choose a new filename.
36 OpenFile
37 Open a file and load it into the active textview.
38 CloseFile
39 Close the active buffer.
40 NewFile
41 Create a new untitled file in the active buffer.
42
43
44 This plugin issues the following events:
45 FileSaved
46 When a buffer is saved
47 FileOpened
48 When a buffer is opened
49 FileClosed
50 When a buffer is closed
51 NewFileCreated
52 When a new untitled buffer is created
53
54 This plugin invokes the following actions:
55 SetStatusMessage
56 When an open or save event occurs'''
57
58 from pallavi.EventActionManager import Action
59 import wx
60 import os, os.path
61
63 bm = wx.ArtProvider.GetBitmap
64 save = Action("SaveActive", SaveActive, "Save", "Save the active text view", bm(wx.ART_FILE_SAVE))
65 saveas = Action("SaveActiveAs", SaveActiveAs, "Save As", "Save the active textview with a specific filename", bm(wx.ART_FILE_SAVE_AS))
66 open = Action("OpenFile", OpenFile, "Open", "Open a file into the active text view", bm(wx.ART_FILE_OPEN))
67 close = Action("CloseFile", CloseFile, "Close", "Close the document in the active textview")
68 new = Action("NewFile", NewFile, "New", "Create a new empty document", bm(wx.ART_NEW))
69 def verifyNotebookClose(event):
70 CloseFile()
71 event.Veto()
72 pallavi.focusedView.bufferNotebook.Bind(wx.aui.EVT_AUINOTEBOOK_PAGE_CLOSE, verifyNotebookClose)
73
74 for action in save, saveas, open, close, new:
75 actions.AddAction(action)
76
78 '''Save the active buffer to its file. If no filename has yet been specified, prompt
79 the saveas dialog'''
80 textview = pallavi.focusedView.focusedTextView
81 if data != None:
82 textview.SaveFile(data)
83 pallavi.focusedView.ChangeFilename(textview, data)
84 eventBus.QueueEvent("FileSaved", data)
85 elif "filename" not in dir(textview) or \
86 textview.filename == None or \
87 textview.filename.startswith("Untitled"):
88 SaveActiveAs(data)
89 else:
90 textview.SaveFile(textview.filename)
91 actions.Invoke("SetStatusMessage", os.path.basename(textview.filename) + " Saved")
92 eventBus.QueueEvent("FileSaved", textview.filename)
93
95 '''Prompt for a filename and save the active buffer to that file'''
96 dlg = wx.FileDialog(pallavi.focusedView, message="Save As...", style=wx.SAVE | wx.CHANGE_DIR)
97 if dlg.ShowModal() == wx.ID_OK:
98 SaveActive(dlg.GetPath())
99 dlg.Destroy()
100
102 '''If data is none, prompt for filenames and load the files contents
103 into the active textview. Otherwise, assume data is a list of filenames to be opened.'''
104 if data == None:
105 dlg = wx.FileDialog(pallavi.focusedView, message="Open...", style=wx.OPEN | wx.MULTIPLE | wx.CHANGE_DIR)
106 if dlg.ShowModal() == wx.ID_OK:
107 data = dlg.GetPaths()
108 dlg.Destroy()
109 else:
110 dlg.Destroy()
111 return
112
113 for filename in data:
114 if not filename in pallavi.focusedView.textViews.keys():
115 pallavi.focusedView.AddTextView(filename, loadfile=os.path.exists(filename))
116 else:
117 pallavi.focusedView.bufferNotebook.SetSelection(pallavi.focusedView.bufferNotebook.GetPageIndex(pallavi.focusedView.textViews[filename]))
118 if os.path.exists(filename):
119 os.chdir(os.path.dirname(filename))
120 actions.Invoke("SetStatusMessage", os.path.basename(filename) + " Opened")
121 eventBus.QueueEvent("FileOpened", filename)
122
137
143
145 '''Determine if its ok to close the document in the active textview. If the document
146 hasn't been modified since last save, its ok. If it has, we ask the user
147 if we should ignore tha changes or not. Return True if its ok to discard
148 the document, false otherwise.'''
149 if pallavi.focusedView.focusedTextView.GetModify():
150 dlg = wx.MessageDialog(pallavi.focusedView, "The current document has been modified since the last save.\n\nDiscard changes?",
151 "Discard Changes?", wx.YES_NO | wx.ICON_QUESTION | wx.CENTRE)
152 if dlg.ShowModal() != wx.ID_YES:
153 dlg.Destroy()
154 return False
155 dlg.Destroy()
156 return True
157