def OpenPage(self, path, filename, quiet=False): """Open a File Inside of a New Page @param path: files base path @param filename: name of file to open @keyword quiet: Open/Switch to the file quietly if it is already open. """ path2file = os.path.join(path, filename) # Check if file needs to be opened # TODO: these steps could be combined together with some # refactoring of the _NeedOpen method. Requires extra # testing though to check for dependancies on current # behavior. if quiet and self.HasFileOpen(path2file): self.GotoPage(path2file) return elif not self._NeedOpen(path2file): return # Create new control to place text on if necessary self.GetTopLevelParent().Freeze() new_pg = True if self.GetPageCount(): if self.control.GetModify() or self.control.GetLength() or \ self.control.GetFileName() != u'': control = ed_editv.EdEditorView(self, wx.ID_ANY) control.Hide() else: new_pg = False control = self.control else: control = ed_editv.EdEditorView(self, wx.ID_ANY) control.Hide() # Open file and get contents result = False if os.path.exists(path2file): try: result = control.LoadFile(path2file) except Exception, msg: self.LOG("[ed_pages][err] Failed to open file %s\n" % path2file) self.LOG("[ed_pages][err] %s" % msg) # File could not be opened/read give up # Don't raise a dialog during a session load error as if the # dialog is shown before the mainwindow is ready it can cause # the app to freeze. if not self._ses_load: ed_mdlg.OpenErrorDlg(self, path2file, msg) control.GetDocument().ClearLastError() control.SetFileName('') # Reset the file name if new_pg: control.Destroy() self.GetTopLevelParent().Thaw() return
def NewPage(self): """Create a new notebook page with a blank text control @postcondition: a new page with an untitled document is opened """ self.Freeze() self.pg_num += 1 self.control = ed_editv.EdEditorView(self, wx.ID_ANY) self.control.SetEncoding(Profile_Get('ENCODING')) self.LOG("[ed_pages][evt] New Page Created ID: %d" % self.control.GetId()) self.AddPage(self.control, _("Untitled - %d") % self.pg_num) self.SetPageImage(self.GetSelection(), str(self.control.GetLangId())) # Set the control up the the preferred default lexer dlexer = Profile_Get('DEFAULT_LEX', 'str', 'Plain Text') ext_reg = syntax.ExtensionRegister() ext_lst = ext_reg.get(dlexer, [ 'txt', ]) self.control.FindLexer(ext_lst[0]) # Set the modified callback notifier doc = self.control.GetDocument() doc.AddModifiedCallback(self.control.FireModified) self.Thaw()
def NewPage(self): """Create a new notebook page with a blank text control @postcondition: a new page with an untitled document is opened """ frame = self.GetTopLevelParent() frame.Freeze() try: self.control = ed_editv.EdEditorView(self) self.LOG("[ed_pages][evt] New Page Created") self.control.Hide() self.AddPage(self.control) self.control.Show() finally: frame.Thaw() # Set the control up the the preferred default lexer dlexer = Profile_Get('DEFAULT_LEX', 'str', synglob.LANG_TXT) ext_reg = syntax.ExtensionRegister() ext_lst = ext_reg.get(dlexer, [ 'txt', ]) self.control.FindLexer(ext_lst[0]) self.SetPageImage(self.GetSelection(), str(self.control.GetLangId())) # Set the modified callback notifier doc = self.control.GetDocument() doc.AddModifiedCallback(self.control.FireModified)
def OpenFileObject(self, fileobj): """Open a new text editor page with the given file object. The file object must be an instance of ed_txt.EdFile. @param fileobj: File Object """ # Create the control self.GetTopLevelParent().Freeze() control = ed_editv.EdEditorView(self, wx.ID_ANY) control.Hide() # Load the files data path = fileobj.GetPath() filename = ebmlib.GetFileName(path) control.SetDocument(fileobj) result = control.ReloadFile() # Setup the buffer fileobj.AddModifiedCallback(control.FireModified) # Setup the notebook self.control = control self.control.FindLexer() self.control.EmptyUndoBuffer() self.control.Show() self.AddPage(self.control, filename) self.frame.SetTitle(self.control.GetTitleString()) self.frame.AddFileToHistory(path) self.SetPageText(self.GetSelection(), filename) self.LOG("[ed_pages][evt] Opened Page: %s" % filename) # Set tab image cpage = self.GetSelection() if fileobj.ReadOnly: super(EdPages, self).SetPageImage(cpage, self._index[ed_glob.ID_READONLY]) else: self.SetPageImage(cpage, str(self.control.GetLangId())) self.GetTopLevelParent().Thaw() # Refocus on selected page self.GoCurrentPage() ed_msg.PostMessage(ed_msg.EDMSG_FILE_OPENED, self.control.GetFileName(), context=self.frame.GetId()) if Profile_Get('WARN_EOL', default=True) and not fileobj.IsRawBytes(): self.control.CheckEOL()
def OpenDocPointer(self, ptr, doc, title=u''): """Open a page using an stc document poiner @param ptr: EdEditorView document Pointer @param doc: EdFile instance @keyword title: tab title """ self.GetTopLevelParent().Freeze() nbuff = self.GetCurrentPage() need_add = False if nbuff.GetFileName() or nbuff.GetLength(): need_add = True nbuff = ed_editv.EdEditorView(self) nbuff.SetDocPointer(ptr) nbuff.SetDocument(doc) doc.AddModifiedCallback(nbuff.FireModified) nbuff.FindLexer() path = nbuff.GetFileName() if Profile_Get('SAVE_POS'): pos = self.DocMgr.GetPos(path) nbuff.SetCaretPos(pos) nbuff.ScrollToColumn(0) if title: filename = title else: filename = ebmlib.GetFileName(path) if need_add: self.AddPage(nbuff, filename) else: self.SetPageText(self.GetSelection(), filename) self.frame.SetTitle(nbuff.GetTitleString()) self.LOG("[ed_pages][evt] Opened Page: %s" % filename) # Set tab image # TODO: Handle read only images self.SetPageImage(self.GetSelection(), str(nbuff.GetLangId())) # Refocus on selected page self.control = nbuff self.GoCurrentPage() self.GetTopLevelParent().Thaw() ed_msg.PostMessage(ed_msg.EDMSG_FILE_OPENED, nbuff.GetFileName(), context=self.frame.GetId())
def DocDuplicated(self, path): """Check for if the given path is open elswhere and duplicate the docpointer. @param path: string """ doc = ed_msg.RequestResult(ed_msg.EDREQ_DOCPOINTER, [self, path]) if hasattr(doc, 'GetDocPointer'): self.GetTopLevelParent().Freeze() nbuff = ed_editv.EdEditorView(self, wx.ID_ANY) nbuff.SetDocPointer(doc.GetDocPointer()) doc = doc.GetDocument() nbuff.SetDocument(doc) doc.AddModifiedCallback(nbuff.FireModified) nbuff.FindLexer() nbuff.EmptyUndoBuffer() if Profile_Get('SAVE_POS'): pos = self.DocMgr.GetPos(nbuff.GetFileName()) nbuff.GotoPos(pos) nbuff.ScrollToColumn(0) filename = util.GetFileName(path) self.AddPage(nbuff, filename) self.frame.SetTitle(nbuff.GetTitleString()) self.SetPageText(self.GetSelection(), filename) self.LOG("[ed_pages][evt] Opened Page: %s" % filename) # Set tab image self.SetPageImage(self.GetSelection(), str(nbuff.GetLangId())) # Refocus on selected page self.control = nbuff self.GoCurrentPage() self.GetTopLevelParent().Thaw() ed_msg.PostMessage(ed_msg.EDMSG_FILE_OPENED, nbuff.GetFileName()) return True else: return False