// // // // // $Revision: 2691 $ // using System; using System.Collections.Generic; using System.Drawing; namespace ICSharpCode.TextEditor.Document { /// /// This class is used to store a pair of lineNr and its color /// public class CustomLine { public int StartLineNr; public int EndLineNr; public Color Color; public bool ReadOnly; public CustomLine(int lineNr, Color customColor, bool readOnly) { this.StartLineNr = this.EndLineNr = lineNr; this.Color = customColor; this.ReadOnly = readOnly; } public CustomLine(int startLineNr, int endLineNr, Color customColor, bool readOnly) { this.StartLineNr = startLineNr; this.EndLineNr = endLineNr; this.Color = customColor; this.ReadOnly = readOnly; } } /// /// This class handles the bookmarks for a buffer /// public class CustomLineManager : ICustomLineManager { List lines = new List(); /// /// Creates a new instance of /// internal CustomLineManager(LineManager lineTracker) { lineTracker.LineCountChanged += MoveIndices; } /// /// Contains all custom lines /// public List CustomLines { get { return lines; } } /// /// Returns the Color if the line lineNr has custom bg color /// otherwise returns defaultColor /// public Color GetCustomColor(int lineNr, Color defaultColor) { foreach(CustomLine line in lines) if (line.StartLineNr <= lineNr && line.EndLineNr >= lineNr) return line.Color; return defaultColor; } /// /// Returns the ReadOnly if the line lineNr is custom /// otherwise returns default /// public bool IsReadOnly(int lineNr, bool defaultReadOnly) { foreach(CustomLine line in lines) if (line.StartLineNr <= lineNr && line.EndLineNr >= lineNr) return line.ReadOnly; return defaultReadOnly; } /// /// Returns true if selection is read only /// public bool IsReadOnly(ISelection selection, bool defaultReadOnly) { int startLine = selection.StartPosition.Y; int endLine = selection.EndPosition.Y; foreach (CustomLine customLine in lines) { if (customLine.ReadOnly == false) continue; if (startLine < customLine.StartLineNr && endLine < customLine.StartLineNr) continue; if (startLine > customLine.EndLineNr && endLine > customLine.EndLineNr) continue; return true; } return defaultReadOnly; } /// /// Clears all custom lines /// public void Clear() { OnBeforeChanged(); lines.Clear(); OnChanged(); } /// /// Is fired before the change /// public event EventHandler BeforeChanged; /// /// Is fired after the change /// public event EventHandler Changed; void OnChanged() { if (Changed != null) { Changed(this, null); } } void OnBeforeChanged() { if (BeforeChanged != null) { BeforeChanged(this, null); } } /// /// Set Custom Line at the line lineNr /// public void AddCustomLine(int lineNr, Color customColor, bool readOnly) { OnBeforeChanged(); lines.Add(new CustomLine(lineNr, customColor, readOnly)); OnChanged(); } /// /// Add Custom Lines from the line startLineNr to the line endLineNr /// public void AddCustomLine(int startLineNr, int endLineNr, Color customColor, bool readOnly) { OnBeforeChanged(); lines.Add(new CustomLine(startLineNr, endLineNr, customColor, readOnly)); OnChanged(); } /// /// Remove Custom Line at the line lineNr /// public void RemoveCustomLine(int lineNr) { for (int i = 0; i < lines.Count; ++i) { if (((CustomLine)lines[i]).StartLineNr <= lineNr && ((CustomLine)lines[i]).EndLineNr >= lineNr) { OnBeforeChanged(); lines.RemoveAt(i); OnChanged(); return; } } } /// /// This method moves all indices from index upward count lines /// (useful for deletion/insertion of text) /// void MoveIndices(object sender, LineCountChangeEventArgs e) { bool changed = false; OnBeforeChanged(); for (int i = 0; i < lines.Count; ++i) { int startLineNr = ((CustomLine)lines[i]).StartLineNr; int endLineNr = ((CustomLine)lines[i]).EndLineNr; if (e.LineStart >= startLineNr && e.LineStart < endLineNr) { changed = true; ((CustomLine)lines[i]).EndLineNr += e.LinesMoved; } else if (e.LineStart < startLineNr) { ((CustomLine)lines[i]).StartLineNr += e.LinesMoved; ((CustomLine)lines[i]).EndLineNr += e.LinesMoved; } else { } /* if (e.LinesMoved < 0 && lineNr == e.LineStart) { lines.RemoveAt(i); --i; changed = true; } else if (lineNr > e.LineStart + 1 || (e.LinesMoved < 0 && lineNr > e.LineStart)) { changed = true; ((CustomLine)lines[i]).StartLineNr += e.LinesMoved; ((CustomLine)lines[i]).EndLineNr += e.LinesMoved; } */ } if (changed) { OnChanged(); } } } }