/// <summary> /// Applies the file state to a scintilla control /// </summary> public static void ApplyFileState(ITabbedDocument document, Boolean restorePosition) { try { if (!document.IsEditable) return; String fileStateDir = FileNameHelper.FileStateDir; String fileName = ConvertToFileName(document.FileName); String stateFile = Path.Combine(fileStateDir, fileName + ".fdb"); if (File.Exists(stateFile)) { StateObject so = new StateObject(); so = (StateObject)ObjectSerializer.Deserialize(stateFile, so); ApplyStateObject(document.SciControl, so, restorePosition); } } catch (Exception ex) { ErrorManager.ShowError(ex); } }
/// <summary> /// Applies the state object to a scintilla control /// </summary> private static void ApplyStateObject(ScintillaControl sci, StateObject so, Boolean restorePosition) { if (so.LineCount != sci.LineCount) return; sci.Refresh(); // Update the scintilla control state for (Int32 i = 0; i < so.FoldedLines.Count; i++) { Int32 foldedLine = so.FoldedLines[i]; sci.ToggleFold(foldedLine); } if (so.BookmarkedLines != null) { for (Int32 i = 0; i < so.BookmarkedLines.Count; i++) { Int32 bookmarkedLine = so.BookmarkedLines[i]; sci.MarkerAdd(bookmarkedLine, 0); } sci.Refresh(); // Update again } if (restorePosition) { sci.FirstVisibleLine = so.LineScroll; Int32 line = sci.LineFromPosition(so.Position); sci.SetSel(so.Position, so.Position); sci.EnsureVisible(line); } }
/// <summary> /// Gets the state object from a scintilla control /// </summary> private static StateObject GetStateObject(ScintillaControl sci) { StateObject so = new StateObject(); so.LineCount = sci.LineCount; so.Position = sci.CurrentPos; so.FileName = sci.FileName; so.LineScroll = sci.FirstVisibleLine; for (Int32 line = 0;; line++) { Int32 lineNext = sci.ContractedFoldNext(line); if ((line < 0) || (lineNext < line)) break; line = lineNext; so.FoldedLines.Add(line); } Int32 lineBookmark = -1; while ((lineBookmark = sci.MarkerNext(lineBookmark + 1, 1 << 0)) >= 0) { so.BookmarkedLines.Add(lineBookmark); } return so; }
/// <summary> /// Gets the state object from a scintilla control /// </summary> private static StateObject GetStateObject(ScintillaControl sci) { StateObject so = new StateObject(); so.LineCount = sci.LineCount; so.Position = sci.CurrentPos; so.FileName = sci.FileName; for (Int32 i = 0; i < sci.LineCount; i++) { if (!sci.FoldExpanded(i)) so.FoldedLines.Add(i); if ((sci.MarkerGet(i) & (1 << 0)) != 0) { so.BookmarkedLines.Add(i); } } return so; }