#region Using Directives using System; using System.ComponentModel; using System.Drawing; using System.Windows.Forms; #endregion Using Directives namespace ScintillaNET { /// /// Manages Caret Settings /// /// /// The caret is the blinking line that indicates the current document position. This /// is sometimes referred to as cursor. /// [TypeConverterAttribute(typeof(System.ComponentModel.ExpandableObjectConverter))] public class CaretInfo : TopLevelHelper { #region Methods /// /// Places the caret somewhere within the document that is displayed in the /// Scintilla Window /// /// /// If the caret is already visible in the current scrolled view this method does /// nothing. /// public void BringIntoView() { NativeScintilla.MoveCaretInsideView(); } /// /// Scintilla remembers the x value of the last position horizontally moved to explicitly by the user and this value is then /// used when moving vertically such as by using the up and down keys. This method sets the current x position of the caret as /// the remembered value. /// public void ChooseCaretX() { NativeScintilla.ChooseCaretX(); } /// /// Scrolls the Scintilla window so that the Caret is visible. /// public void EnsureVisible() { NativeScintilla.ScrollCaret(); } /// /// Places the caret at the specified document position /// /// Position in the document to place the caret public void Goto(int position) { NativeScintilla.GotoPos(position); } private void ResetBlinkRate() { BlinkRate = SystemInformation.CaretBlinkTime; } private void ResetColor() { Color = Color.Black; } private void ResetCurrentLineBackgroundAlpha() { CurrentLineBackgroundAlpha = 256; } private void ResetCurrentLineBackgroundColor() { CurrentLineBackgroundColor = Color.Yellow; } private void ResetHighlightCurrentLine() { HighlightCurrentLine = false; } private void ResetIsSticky() { IsSticky = false; } private void ResetStyle() { Style = CaretStyle.Line; } private void ResetWidth() { Width = SystemInformation.CaretWidth; } protected internal bool ShouldSerialize() { return ShouldSerializeBlinkRate() || ShouldSerializeColor() || ShouldSerializeCurrentLineBackgroundColor() || ShouldSerializeWidth() || ShouldSerializeHighlightCurrentLine() || ShouldSerializeCurrentLineBackgroundAlpha() || ShouldSerializeStyle() || ShouldSerializeIsSticky(); } private bool ShouldSerializeBlinkRate() { return BlinkRate != SystemInformation.CaretBlinkTime; } private bool ShouldSerializeColor() { return Color != Color.Black; } private bool ShouldSerializeCurrentLineBackgroundAlpha() { return CurrentLineBackgroundAlpha != 256; } private bool ShouldSerializeCurrentLineBackgroundColor() { return CurrentLineBackgroundColor != Color.Yellow; } private bool ShouldSerializeHighlightCurrentLine() { return HighlightCurrentLine; } private bool ShouldSerializeIsSticky() { return IsSticky; } private bool ShouldSerializeStyle() { return Style != CaretStyle.Line; } private bool ShouldSerializeWidth() { return Width != SystemInformation.CaretWidth; } public override string ToString() { return ShouldSerialize() ? base.ToString() : string.Empty; } #endregion Methods #region Properties /// /// Gets/Sets the current anchor position /// /// /// If the anchor position is less than the Caret Position it acts as the _start of /// the selection. /// [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] public int Anchor { get { return NativeScintilla.GetAnchor(); } set { NativeScintilla.SetAnchor(value); } } /// /// Gets/Sets the time interval in milliseconds that the caret should blink. /// /// /// This defaults to the system default value. /// public int BlinkRate { get { return NativeScintilla.GetCaretPeriod(); } set { NativeScintilla.SetCaretPeriod(value); } } /// /// Gets/Sets the color of the Caret. /// /// Defaults to black public Color Color { get { if (Scintilla.ColorBag.ContainsKey("Caret.Color")) return Scintilla.ColorBag["Caret.Color"]; Color c = Utilities.RgbToColor(NativeScintilla.GetCaretFore()); if (c == Color.FromArgb(0, 0, 0)) return Color.Black; return c; } set { if (value == Color) return; if (value.IsKnownColor) { if (Color == Color.Black) Scintilla.ColorBag.Remove("Caret.Color"); else Scintilla.ColorBag["Caret.Color"] = value; } NativeScintilla.SetCaretFore(Utilities.ColorToRgb(value)); } } /// /// Gets/Sets the transparency alpha of the CurrentLine Background highlight /// /// /// Values range from 0 to 256. Default is 256. /// public int CurrentLineBackgroundAlpha { get { return NativeScintilla.GetCaretLineBackAlpha(); } set { NativeScintilla.SetCaretLineBackAlpha(value); } } /// /// Gets/Sets the color of the document line where the caret currently resides /// /// /// The property must be set to true in order /// for this to to take effect. /// public Color CurrentLineBackgroundColor { get { if (Scintilla.ColorBag.ContainsKey("Caret.CurrentLineBackgroundColor")) return Scintilla.ColorBag["Caret.CurrentLineBackgroundColor"]; Color c = Utilities.RgbToColor(NativeScintilla.GetCaretLineBack()); if (c.ToArgb() == Color.Yellow.ToArgb()) return Color.Yellow; return c; } set { if (value == Color) return; if (value.IsKnownColor) { if (Color == Color.Yellow) Scintilla.ColorBag.Remove("Caret.CurrentLineBackgroundColor"); else Scintilla.ColorBag["Caret.CurrentLineBackgroundColor"] = value; } NativeScintilla.SetCaretLineBack(Utilities.ColorToRgb(value)); } } /// /// Gets/Sets if the current document line where the caret resides is highlighted. /// /// /// determines the color. /// public bool HighlightCurrentLine { get { return NativeScintilla.GetCaretLineVisible(); } set { NativeScintilla.SetCaretLineVisible(value); } } /// /// Controls when the last position of the caret on the line is saved. When set to true, the position is not saved when you type a character, a tab, paste the clipboard content or press backspace /// /// /// Defaults to false /// public bool IsSticky { get { return NativeScintilla.GetCaretSticky(); } set { NativeScintilla.SetCaretSticky(value); } } /// /// Gets/Sets the current Line Number that the caret resides. /// [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] public int LineNumber { get { return NativeScintilla.LineFromPosition(NativeScintilla.GetCurrentPos()); } set { NativeScintilla.GotoLine(value); } } /// /// Gets/Sets the current document position where the caret resides /// [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] public int Position { get { return NativeScintilla.GetCurrentPos(); } set { NativeScintilla.SetCurrentPos(value); } } /// /// Gets/Sets the displayed. /// public CaretStyle Style { get { return (CaretStyle)NativeScintilla.GetCaretStyle(); } set { NativeScintilla.SetCaretStyle((int)value); } } /// /// Gets/Sets the width in pixels of the Caret /// /// /// This defaults to the system default. /// public int Width { get { return NativeScintilla.GetCaretWidth(); } set { NativeScintilla.SetCaretWidth(value); } } #endregion Properties #region Constructors protected internal CaretInfo(Scintilla scintilla) : base(scintilla) { BlinkRate = SystemInformation.CaretBlinkTime; Width = SystemInformation.CaretWidth; } #endregion Constructors } }