Package pallavi :: Package plugins :: Module IODialog
[hide private]
[frames] | no frames]

Source Code for Module pallavi.plugins.IODialog

  1  # Copyright (c) 2006-2007 Dusty Phillips 
  2   
  3  # Permission is hereby granted, free of charge, to any person obtaining a 
  4  # copy of this software and associated documentation files (the "Software"), 
  5  # to deal in the Software without restriction, including without limitation 
  6  # the rights to use, copy, modify, merge, publish, distribute, sublicense, 
  7  # and/or sell copies of the Software, and to permit persons to whom the 
  8  # Software is furnished to do so, subject to the following conditions: 
  9   
 10  # The above copyright notice and this permission notice shall be included in 
 11  # all copies or substantial portions of the Software. 
 12   
 13  # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 14  # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 15  # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
 16  # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 17  # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
 18  # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
 19  # DEALINGS IN THE SOFTWARE. 
 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   
62 -def setup():
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
77 -def SaveActive(data=None):
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
94 -def SaveActiveAs(data=None):
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
101 -def OpenFile(data=None):
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
123 -def CloseFile(data=None):
124 '''Close the current file and remove it from the buffer switcher. 125 Return true if the buffer was closed, false if it was not.''' 126 if VerifyModified(): 127 filename = pallavi.focusedView.focusedTextView.filename 128 pallavi.focusedView.DeleteTextView(pallavi.focusedView.focusedTextView) 129 actions.Invoke("SetStatusMessage", os.path.basename(filename) + " Closed") 130 eventBus.QueueEvent("FileClosed", filename) 131 132 if len(pallavi.focusedView.textViews) == 1: 133 actions.Invoke("NewFile") 134 return True 135 136 return False
137
138 -def NewFile(data=None):
139 '''Create a new blank buffer in the focused textview. The old docpointer will 140 remain in the docSelector''' 141 pallavi.focusedView.AddTextView(pallavi.nextUntitled()) 142 eventBus.QueueEvent("NewFileCreated")
143
144 -def VerifyModified():
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