def AskToReload(win, cfile): """Show a dialog asking if the file should be reloaded @param win: Window to prompt dialog on top of @param cfile: the file to prompt for a reload of """ mdlg = wx.MessageDialog( win.frame, _("%s has been modified by another " "application.\n\nWould you like " "to Reload it?") % cfile, _("Reload File?"), wx.YES_NO | wx.ICON_INFORMATION) mdlg.CenterOnParent() result = mdlg.ShowModal() mdlg.Destroy() if result == wx.ID_YES: ret, rmsg = win.control.ReloadFile() if not ret: errmap = dict(filename=cfile, errmsg=rmsg) mdlg = wx.MessageDialog( win.frame, _("Failed to reload %(filename)s:\n" "Error: %(errmsg)s") % errmap, _("Error"), wx.OK | wx.ICON_ERROR) mdlg.ShowModal() mdlg.Destroy() else: win.control.SetModTime(util.GetFileModTime(cfile))
def OnIdle(self, evt): """Update tabs and check if files have been modified @param evt: Event that called this handler @type evt: wx.IdleEvent """ if wx.GetApp().IsActive() and \ Profile_Get('CHECKMOD') and self.GetPageCount(): cfile = self.control.GetFileName() lmod = util.GetFileModTime(cfile) if self.control.GetModTime() and \ not lmod and not os.path.exists(cfile): wx.CallAfter(PromptToReSave, self, cfile) elif self.control.GetModTime() < lmod: wx.CallAfter(AskToReload, self, cfile) else: return False
def setUp(self): self.app = common.EdApp(False) self.path = os.path.abspath('./data/test_read_utf8.txt') self.file = ed_txt.EdFile(self.path) self.mtime = util.GetFileModTime(self.path)
control.Destroy() else: control.SetText('') control.SetDocument(ed_txt.EdFile()) control.SetSavePoint() self.GetTopLevelParent().Thaw() return # Put control into page an place page in notebook if new_pg: control.Show() self.control = control # Setup Document self.control.SetModTime(util.GetFileModTime(path2file)) self.control.FindLexer() self.control.CheckEOL() self.control.EmptyUndoBuffer() if Profile_Get('SAVE_POS'): self.control.GotoPos(self.DocMgr.GetPos( self.control.GetFileName())) # Add the buffer to the notebook if new_pg: self.AddPage(self.control, filename) self.frame.SetTitle("%s - file://%s" % (filename, self.control.GetFileName())) self.frame.AddFileToHistory(path2file)
class EdPages(FNB.FlatNotebook): """Editras editor buffer botebook @todo: allow for tab styles to be configurable (maybe) """ def __init__(self, parent, id_num): """Initialize a notebook with a blank text control in it @param parent: parent window of the notebook @param id_num: this notebooks id """ FNB.FlatNotebook.__init__( self, parent, id_num, style=FNB.FNB_FF2 | FNB.FNB_X_ON_TAB | FNB.FNB_SMART_TABS | FNB.FNB_BACKGROUND_GRADIENT | FNB.FNB_DROPDOWN_TABS_LIST | FNB.FNB_ALLOW_FOREIGN_DND) # Notebook attributes self.LOG = wx.GetApp().GetLog() self.FindService = ed_search.TextFinder(self, self.GetCurrentCtrl) self.DocMgr = doctools.DocPositionMgr(ed_glob.CONFIG['CACHE_DIR'] + \ util.GetPathChar() + u'positions') self.pg_num = -1 # Track new pages (aka untitled docs) self.control = None self.frame = self.GetTopLevelParent() # MainWindow self._index = dict() # image list index # Set Additional Style Parameters self.SetNonActiveTabTextColour(wx.Colour(102, 102, 102)) ed_icon = ed_glob.CONFIG['SYSPIX_DIR'] + u"editra.png" self.SetNavigatorIcon(wx.Bitmap(ed_icon, wx.BITMAP_TYPE_PNG)) # Setup the ImageList and the default image imgl = wx.ImageList(16, 16) txtbmp = wx.ArtProvider.GetBitmap(str(synglob.ID_LANG_TXT), wx.ART_MENU) self._index[synglob.ID_LANG_TXT] = imgl.Add(txtbmp) self.SetImageList(imgl) # Notebook Events self.Bind(FNB.EVT_FLATNOTEBOOK_PAGE_CHANGING, self.OnPageChanging) self.Bind(FNB.EVT_FLATNOTEBOOK_PAGE_CHANGED, self.OnPageChanged) self.Bind(FNB.EVT_FLATNOTEBOOK_PAGE_CLOSING, self.OnPageClosing) self.Bind(FNB.EVT_FLATNOTEBOOK_PAGE_CLOSED, self.OnPageClosed) self.Bind(wx.stc.EVT_STC_MODIFIED, self.OnUpdatePageText) self._pages.Bind(wx.EVT_LEFT_UP, self.OnLeftUp) self.Bind(wx.EVT_IDLE, self.OnIdle) # Add a blank page self.NewPage() #---- End Init ----# #---- Function Definitions ----# def _NeedOpen(self, path): """Check if a file needs to be opened. If the file is already open in the notebook a dialog will be opened to ask if the user wants to reopen the file again. If the file is not open and exists or the user chooses to reopen the file again the function will return True else it will return False. @param path: file to check for """ result = wx.ID_YES if self.HasFileOpen(path): mdlg = wx.MessageDialog(self, _("File is already open in an existing " "page.\nDo you wish to open it again?"), _("Open File") + u"?", wx.YES_NO | wx.NO_DEFAULT | \ wx.ICON_INFORMATION) result = mdlg.ShowModal() mdlg.Destroy() if result == wx.ID_NO: for page in xrange(self.GetPageCount()): ctrl = self.GetPage(page) if path == ctrl.GetFileName(): self.SetSelection(page) self.ChangePage(page) break elif os.path.exists(path) and not os.path.isfile(path): result = wx.ID_NO else: pass return result == wx.ID_YES def GetCurrentCtrl(self): """Returns the control of the currently selected page in the notebook. @return: window object contained in current page or None """ if hasattr(self, 'control'): return self.control else: return None def GetFileNames(self): """Gets the name of all open files in the notebook @return: list of file names """ rlist = list() for buff in self.GetTextControls(): fname = buff.GetFileName() if fname != wx.EmptyString: rlist.append(fname) return rlist def LoadSessionFiles(self): """Load files from saved session data in profile @postcondition: Files saved from previous session are opened. If no files were found then only a single blank page is opened. """ files = Profile_Get('LAST_SESSION') if files is not None: for fname in files: if os.path.exists(fname) and os.access(fname, os.R_OK): self.OpenPage(os.path.dirname(fname), os.path.basename(fname)) if self.GetPageCount() == 0: self.NewPage() def NewPage(self): """Create a new notebook page with a blank text control @postcondition: a new page with an untitled document is opened """ self.pg_num += 1 self.control = ed_stc.EditraStc(self, wx.ID_ANY) self.LOG("[nb_evt] Page Creation ID: %d" % self.control.GetId()) self.AddPage(self.control, u"Untitled - %d" % self.pg_num) self.SetPageImage(self.GetSelection(), str(self.control.GetLangId())) def OpenPage(self, path, filename): """Open a File Inside of a New Page @param path: files base path @param filename: name of file to open """ path2file = os.path.join(path, filename) # Check if file needs to be opened if not self._NeedOpen(path2file): return # Create new control to place text on if necessary new_pg = True if self.GetPageCount(): if self.control.GetModify() or self.control.GetLength() or \ self.control.GetFileName() != u'': control = ed_stc.EditraStc(self, wx.ID_ANY) control.Hide() else: new_pg = False else: control = ed_stc.EditraStc(self, wx.ID_ANY) control.Hide() # Open file and get contents err = False in_txt = u'' enc = u'utf-8' if os.path.exists(path2file): try: in_txt, enc = util.GetDecodedText(path2file) except (UnicodeDecodeError, IOError, OSError), msg: self.LOG(("[ed_pages][err] Failed to open file %s\n" "[ed_pages][err] %s") % (path2file, msg)) # File could not be opened/read give up err = wx.MessageDialog(self, _("Editra could not properly " "open %s\n") \ % path2file, _("Error Opening File"), style=wx.OK | wx.CENTER | wx.ICON_ERROR) err.ShowModal() err.Destroy() if new_pg: control.Destroy() return # Put control into page an place page in notebook if new_pg: control.Show() self.control = control # Pass directory and file name info to control object to save reference self.control.SetText(in_txt, enc) self.control.SetFileName(path2file) self.control.SetModTime(util.GetFileModTime(path2file)) self.frame.AddFileToHistory(path2file) if new_pg: self.AddPage(self.control, filename) self.frame.SetTitle("%s - file://%s" % (filename, self.control.GetFileName())) self.SetPageText(self.GetSelection(), filename) self.LOG("[nb_evt] Opened Page: ID = %d" % self.GetSelection()) # Setup Document self.control.FindLexer() self.control.CheckEOL() self.control.EmptyUndoBuffer() if Profile_Get('SAVE_POS'): self.control.GotoPos(self.DocMgr.GetPos( self.control.GetFileName())) # Set tab image self.SetPageImage(self.GetSelection(), str(self.control.GetLangId())) # Refocus on selected page self.GoCurrentPage()
def testGetFileModTime(self): """Test getting a files modtime""" mtime = util.GetFileModTime(self.path) self.assertNotEqual(mtime, 0, "Mtime was: " + str(mtime))