// * // * Copyright (C) 2008 Roger Alsing : http://www.RogerAlsing.com // * // * This library is free software; you can redistribute it and/or modify it // * under the terms of the GNU Lesser General Public License 2.1 or later, as // * published by the Free Software Foundation. See the included license.txt // * or http://www.gnu.org/copyleft/lesser.html for details. // * // * #region using... using System; using System.ComponentModel; using System.ComponentModel.Design; using System.Drawing; using System.Drawing.Design; using System.IO; using System.Resources; using System.Windows.Forms; using Alsing.Drawing.GDI; using Alsing.SourceCode; using Alsing.Windows.Forms.CoreLib; using Alsing.Windows.Forms.SyntaxBox; #endregion namespace Alsing.Windows.Forms { /// /// Syntaxbox control that can be used as a pure text editor or as a code editor when a syntaxfile is used. /// [Designer(typeof (SyntaxBoxDesigner), typeof (IDesigner))] public class SyntaxBoxControl : SplitViewParentControl { protected internal bool DisableAutoList; protected internal bool DisableFindForm; protected internal bool DisableInfoTip; protected internal bool DisableIntelliMouse; #region General Declarations private bool _AllowBreakPoints = true; private Color _BackColor = Color.White; private Color _BracketBackColor = Color.LightSteelBlue; private Color _BracketBorderColor = Color.DarkBlue; private Color _BracketForeColor = Color.Black; private bool _BracketMatching = true; private Color _BreakPointBackColor = Color.DarkRed; private Color _BreakPointForeColor = Color.White; private SyntaxDocument _Document; private string _FontName = "Courier new"; private float _FontSize = 10f; private Color _GutterMarginBorderColor = SystemColors.ControlDark; private Color _GutterMarginColor = SystemColors.Control; private int _GutterMarginWidth = 19; private bool _HighLightActiveLine; private Color _HighLightedLineColor = Color.LightYellow; private Color _InactiveSelectionBackColor = SystemColors.ControlDark; private Color _InactiveSelectionForeColor = SystemColors.ControlLight; private IndentStyle _Indent = IndentStyle.LastRow; private KeyboardActionList _KeyboardActions = new KeyboardActionList(); private Color _LineNumberBackColor = SystemColors.Window; private Color _LineNumberBorderColor = Color.Teal; private Color _LineNumberForeColor = Color.Teal; private Color _OutlineColor = SystemColors.ControlDark; private bool _ParseOnPaste; private Color _ScopeBackColor = Color.Transparent; private Color _ScopeIndicatorColor = Color.Transparent; private Color _SelectionBackColor = SystemColors.Highlight; private Color _SelectionForeColor = SystemColors.HighlightText; private Color _SeparatorColor = SystemColors.Control; private bool _ShowGutterMargin = true; private bool _ShowLineNumbers = true; private bool _ShowTabGuides; private bool _ShowWhitespace; private int _SmoothScrollSpeed = 2; private Color _TabGuideColor = ControlPaint.Light(SystemColors.ControlLight) ; private int _TabSize = 4; private int _TooltipDelay = 240; private bool _VirtualWhitespace; private Color _WhitespaceColor = SystemColors.ControlDark; private IContainer components; #endregion #region Internal Components/Controls private ImageList _AutoListIcons; private ImageList _GutterIcons; private Timer ParseTimer; #endregion #region Public Events /// /// An event that is fired when the cursor hovers a pattern; /// public event WordMouseHandler WordMouseHover = null; /// /// An event that is fired when the cursor hovers a pattern; /// public event WordMouseHandler WordMouseDown = null; /// /// An event that is fired when the control has updated the clipboard /// public event CopyHandler ClipboardUpdated = null; /// /// Event fired when the caret of the active view have moved. /// public event EventHandler CaretChange = null; /// /// /// public event EventHandler SelectionChange = null; /// /// Event fired when the user presses the up or the down button on the infotip. /// public event EventHandler InfoTipSelectedIndexChanged = null; /// /// Event fired when a row is rendered. /// public event RowPaintHandler RenderRow = null; /// /// An event that is fired when mouse down occurs on a row /// public event RowMouseHandler RowMouseDown = null; /// /// An event that is fired when mouse move occurs on a row /// public event RowMouseHandler RowMouseMove = null; /// /// An event that is fired when mouse up occurs on a row /// public event RowMouseHandler RowMouseUp = null; /// /// An event that is fired when a click occurs on a row /// public event RowMouseHandler RowClick = null; /// /// An event that is fired when a double click occurs on a row /// public event RowMouseHandler RowDoubleClick = null; #endregion //END PUBLIC EGENTS #region Public Properties #region PUBLIC PROPERTY SHOWEOLMARKER private bool _ShowEOLMarker; [Category("Appearance"), Description( "Determines if a ¶ should be displayed at the end of a line") ] [DefaultValue(false)] public bool ShowEOLMarker { get { return _ShowEOLMarker; } set { _ShowEOLMarker = value; Redraw(); } } #endregion #region PUBLIC PROPERTY EOLMARKERCOLOR private Color _EOLMarkerColor = Color.Red; [Category("Appearance"), Description("The color of the EOL marker") ] [DefaultValue(typeof (Color), "Red")] public Color EOLMarkerColor { get { return _EOLMarkerColor; } set { _EOLMarkerColor = value; Redraw(); } } #endregion #region PUBLIC PROPERTY AUTOLISTAUTOSELECT private bool _AutoListAutoSelect = true; [DefaultValue(true)] public bool AutoListAutoSelect { get { return _AutoListAutoSelect; } set { _AutoListAutoSelect = value; } } #endregion #region PUBLIC PROPERTY COPYASRTF [Category("Behavior - Clipboard"), Description("determines if the copy actions should be stored as RTF")] [DefaultValue(typeof (Color), "false")] public bool CopyAsRTF { get; set; } #endregion private bool _CollapsedBlockTooltipsEnabled = true; [Category("Appearance - Scopes"), Description( "The color of the active scope")] [DefaultValue(typeof (Color), "Transparent")] public Color ScopeBackColor { get { return _ScopeBackColor; } set { _ScopeBackColor = value; Redraw(); } } [Category("Appearance - Scopes"), Description( "The color of the scope indicator")] [DefaultValue(typeof (Color), "Transparent")] public Color ScopeIndicatorColor { get { return _ScopeIndicatorColor; } set { _ScopeIndicatorColor = value; Redraw(); } } /// /// Positions the AutoList /// [Category("Behavior")] [Browsable(false)] public TextPoint AutoListPosition { get { return ((EditViewControl) _ActiveView).AutoListPosition; } set { if ((_ActiveView) == null) return; ((EditViewControl) _ActiveView).AutoListPosition = value; } } /// /// Positions the InfoTip /// [Category("Behavior")] [Browsable(false)] public TextPoint InfoTipPosition { get { return ((EditViewControl) _ActiveView).InfoTipPosition; } set { if ((_ActiveView) == null) return; ((EditViewControl) _ActiveView).InfoTipPosition = value; } } /// /// Prevents the control from changing the cursor. /// [Description("Prevents the control from changing the cursor.")] [Category("Appearance")] [Browsable(false)] public bool LockCursorUpdate { get; set; } /// /// The row padding in pixels. /// [Category("Appearance"), Description("The number of pixels to add between rows")] [DefaultValue(0)] public int RowPadding { get; set; } /// /// The selected index in the infotip. /// [Category("Appearance - Infotip"), Description( "The currently active selection in the infotip")] [Browsable(false) ] public int InfoTipSelectedIndex { get { return ((EditViewControl) _ActiveView).InfoTip.SelectedIndex; } set { if ((_ActiveView) == null || ((EditViewControl) _ActiveView).InfoTip == null) return; ((EditViewControl) _ActiveView).InfoTip.SelectedIndex = value; } } /// /// Gets or Sets the image used in the infotip. /// [Category("Appearance - InfoTip"), Description( "An image to show in the infotip")] [DefaultValue(null)] public Image InfoTipImage { get { return ((EditViewControl) _ActiveView).InfoTip.Image; } set { if ((_ActiveView) == null || ((EditViewControl) _ActiveView).InfoTip == null) return; ((EditViewControl) _ActiveView).InfoTip.Image = value; } } /// /// Get or Sets the number of choices that could be made in the infotip. /// [Category("Appearance"), Description( "Get or Sets the number of choices that could be made in the infotip")] [Browsable(false)] public int InfoTipCount { get { return ((EditViewControl) _ActiveView).InfoTip.Count; } set { if ((_ActiveView) == null || ((EditViewControl) _ActiveView).InfoTip == null) return; ((EditViewControl) _ActiveView).InfoTip.Count = value; ((EditViewControl) _ActiveView).InfoTip.Init(); } } /// /// The text in the Infotip. /// ///
/// The text uses a HTML like syntax.
///
/// Supported tags are:
///
/// <Font Size="Size in Pixels" Face="Font Name" Color="Named color" ></Font> Set Font size,color and fontname.
/// <HR> : Inserts a horizontal separator line.
/// <BR> : Line break.
/// <B></B> : Activate/Deactivate Bold style.
/// <I></I> : Activate/Deactivate Italic style.
/// <U></U> : Activate/Deactivate Underline style.
///
/// /// /// MySyntaxBox.InfoTipText="public void MyMethod ( <b> string text </b> );"; /// /// [Category("Appearance - InfoTip"), Description("The infotip text") ] [DefaultValue("")] public string InfoTipText { get { return ((EditViewControl) _ActiveView).InfoTip.Data; } set { if ((_ActiveView) == null || ((EditViewControl) _ActiveView).InfoTip == null) return; ((EditViewControl) _ActiveView).InfoTip.Data = value; } } /// /// Gets the Selection object from the active view. /// [Browsable(false)] public Selection Selection { get { if ((_ActiveView) != null) { return ((EditViewControl) _ActiveView).Selection; } return null; } } /// /// Collection of KeyboardActions that is used by the control. /// Keyboard actions to add shortcut key combinations to certain tasks. /// [Browsable(false)] [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] public KeyboardActionList KeyboardActions { get { return _KeyboardActions; } set { _KeyboardActions = value; } } /// /// Gets or Sets if the AutoList is visible in the active view. /// [Category("Appearance"), Description( "Gets or Sets if the AutoList is visible in the active view.") ] [Browsable(false)] public bool AutoListVisible { get { return (_ActiveView) != null && ((EditViewControl) _ActiveView).AutoListVisible; } set { if ((_ActiveView) != null) ((EditViewControl) _ActiveView).AutoListVisible = value; } } /// /// Gets or Sets if the InfoTip is visible in the active view. /// [Category("Appearance"), Description( "Gets or Sets if the InfoTip is visible in the active view.") ] [Browsable(false)] public bool InfoTipVisible { get { return (_ActiveView) != null && ((EditViewControl) _ActiveView).InfoTipVisible; } set { if ((_ActiveView) != null) ((EditViewControl) _ActiveView).InfoTipVisible = value; } } /// /// Gets if the control can perform a Copy action. /// [Browsable(false)] public bool CanCopy { get { return ((EditViewControl) _ActiveView).CanCopy; } } /// /// Gets if the control can perform a Paste action. /// (if the clipboard contains a valid text). /// [Browsable(false)] public bool CanPaste { get { return ((EditViewControl) _ActiveView).CanPaste; } } /// /// Gets if the control can perform a ReDo action. /// [Browsable(false)] public bool CanRedo { get { return ((EditViewControl) _ActiveView).CanRedo; } } /// /// Gets if the control can perform an Undo action. /// [Browsable(false)] public bool CanUndo { get { return ((EditViewControl) _ActiveView).CanUndo; } } /// /// Gets or Sets the imagelist to use in the gutter margin. /// /// /// Image Index 0 is used to display the Breakpoint icon. /// Image Index 1 is used to display the Bookmark icon. /// [Category("Appearance - Gutter Margin"), Description( "Gets or Sets the imagelist to use in the gutter margin.")] public ImageList GutterIcons { get { return _GutterIcons; } set { _GutterIcons = value; Redraw(); } } /// /// Gets or Sets the imagelist to use in the autolist. /// [Category("Appearance"), Description( "Gets or Sets the imagelist to use in the autolist.") ] [DefaultValue(null)] public ImageList AutoListIcons { get { return _AutoListIcons; } set { _AutoListIcons = value; foreach (EditViewControl ev in Views) { if (ev != null && ev.AutoList != null) ev.AutoList.Images = value; } Redraw(); } } /// /// Gets or Sets the color to use when rendering Tab guides. /// [Category("Appearance - Tabs")] [Description( "Gets or Sets the color to use when rendering Tab guides.") ] [DefaultValue(typeof (Color), "Control")] public Color TabGuideColor { get { return _TabGuideColor; } set { _TabGuideColor = value; Redraw(); } } /// /// Gets or Sets the color of the bracket match borders. /// /// /// NOTE: use Color.Transparent to turn off the bracket match borders. /// [Category("Appearance - Bracket Match")] [Description( "Gets or Sets the color of the bracket match borders.") ] [DefaultValue(typeof (Color), "DarkBlue")] public Color BracketBorderColor { get { return _BracketBorderColor; } set { _BracketBorderColor = value; Redraw(); } } /// /// Gets or Sets if the control should render Tab guides. /// [Category("Appearance - Tabs")] [Description( "Gets or Sets if the control should render Tab guides.") ] [DefaultValue(false)] public bool ShowTabGuides { get { return _ShowTabGuides; } set { _ShowTabGuides = value; Redraw(); } } /// /// Gets or Sets the color to use when rendering whitespace characters /// [Category("Appearance")] [Description( "Gets or Sets the color to use when rendering whitespace characters.")] [DefaultValue(typeof (Color), "Control")] public Color WhitespaceColor { get { return _WhitespaceColor; } set { _WhitespaceColor = value; Redraw(); } } /// /// Gets or Sets the color of the code Outlining (both folding lines and collapsed blocks). /// [Category("Appearance")] [Description( "Gets or Sets the color of the code Outlining (both folding lines and collapsed blocks).")] [DefaultValue(typeof (Color), "ControlDark")] public Color OutlineColor { get { return _OutlineColor; } set { _OutlineColor = value; InitGraphics(); Redraw(); } } /// /// Determines if the control should use a smooth scroll when scrolling one row up or down. /// [Category("Behavior")] [Description("Determines if the control should use a smooth scroll when scrolling one row up or down.")] [DefaultValue(typeof (Color), "False")] public bool SmoothScroll { get; set; } /// /// Gets or Sets the speed of the vertical scroll when SmoothScroll is activated /// [Category("Behavior")] [Description( "Gets or Sets the speed of the vertical scroll when SmoothScroll is activated")] [DefaultValue(2)] public int SmoothScrollSpeed { get { return _SmoothScrollSpeed; } set { if (value <= 0) { throw (new Exception("Scroll speed may not be less than 1")); } _SmoothScrollSpeed = value; } } /// /// Gets or Sets if the control can display breakpoints or not. /// [Category("Behavior")] [Description( "Gets or Sets if the control can display breakpoints or not.") ] [DefaultValue(true)] public bool AllowBreakPoints { get { return _AllowBreakPoints; } set { _AllowBreakPoints = value; } } /// /// Gets or Sets if the control should perform a full parse of the document when content is drag dropped or pasted into the control /// [Category("Behavior - Clipboard")] [Description( "Gets or Sets if the control should perform a full parse of the document when content is drag dropped or pasted into the control" )] [DefaultValue(false)] public bool ParseOnPaste { get { return _ParseOnPaste; } set { _ParseOnPaste = value; Redraw(); } } /// /// Gets or Sets the Size of the font. /// /// [Category("Appearance - Font")] [Description("The size of the font") ] [DefaultValue(10f)] public float FontSize { get { return _FontSize; } set { _FontSize = value; InitGraphics(); Redraw(); } } /// /// Determines what indentstyle to use on a new line. /// [Category("Behavior")] [Description( "Determines how the the control indents a new line") ] [DefaultValue(IndentStyle.LastRow)] public IndentStyle Indent { get { return _Indent; } set { _Indent = value; } } /// /// Gets or Sets the SyntaxDocument the control is currently attatched to. /// [Category("Content")] [Description( "The SyntaxDocument that is attatched to the contro")] public SyntaxDocument Document { get { return _Document; } set { AttachDocument(value); } } /// /// Get or Set the delay before the tooltip is displayed over a collapsed block /// [Category("Behavior")] [Description( "The delay before the tooltip is displayed over a collapsed block")] [DefaultValue(240)] public int TooltipDelay { get { return _TooltipDelay; } set { _TooltipDelay = value; } } // ROB: Added property to turn collapsed block tooltips on and off. /// /// Get or Set whether or not tooltips will be deplayed for collapsed blocks. /// [Category("Behavior")] [Description("The delay before the tooltip is displayed over a collapsed block")] [DefaultValue(true)] public bool CollapsedBlockTooltipsEnabled { get { return _CollapsedBlockTooltipsEnabled; } set { _CollapsedBlockTooltipsEnabled = value; } } // END-ROB ---------------------------------------------------------- /// /// Get or Set the delay before the tooltip is displayed over a collapsed block /// [Category("Behavior")] [Description("Determines if the control is readonly or not")] [DefaultValue(false)] public bool ReadOnly { get; set; } /// /// Gets or Sets the name of the font. /// /// [Category("Appearance - Font")] [Description( "The name of the font that is used to render the control") ] [Editor(typeof (FontList), typeof (UITypeEditor))] [DefaultValue("Courier New") ] public string FontName { get { return _FontName; } set { if (Views == null) return; _FontName = value; InitGraphics(); foreach (EditViewControl evc in Views) evc.CalcMaxCharWidth(); Redraw(); } } /// /// Gets or Sets if bracketmatching is active /// /// /// [Category("Appearance - Bracket Match")] [Description( "Determines if the control should highlight scope patterns") ] [DefaultValue(true)] public bool BracketMatching { get { return _BracketMatching; } set { _BracketMatching = value; Redraw(); } } /// /// Gets or Sets if Virtual Whitespace is active. /// /// [Category("Behavior")] [Description( "Determines if virtual Whitespace is active")] [DefaultValue(false) ] public bool VirtualWhitespace { get { return _VirtualWhitespace; } set { _VirtualWhitespace = value; Redraw(); } } /// /// Gets or Sets the separator Color. /// /// /// [Category("Appearance")] [Description("The separator color")] [DefaultValue (typeof (Color), "Control")] public Color SeparatorColor { get { return _SeparatorColor; } set { _SeparatorColor = value; Redraw(); } } /// /// Gets or Sets the foreground Color to use when BracketMatching is activated. /// /// /// [Category("Appearance - Bracket Match")] [Description( "The foreground color to use when BracketMatching is activated") ] [DefaultValue(typeof (Color), "Black")] public Color BracketForeColor { get { return _BracketForeColor; } set { _BracketForeColor = value; Redraw(); } } /// /// Gets or Sets the background Color to use when BracketMatching is activated. /// /// /// [Category("Appearance - Bracket Match")] [Description( "The background color to use when BracketMatching is activated") ] [DefaultValue(typeof (Color), "LightSteelBlue")] public Color BracketBackColor { get { return _BracketBackColor; } set { _BracketBackColor = value; Redraw(); } } /// /// The inactive selection background color. /// [Category("Appearance - Selection")] [Description( "The inactive selection background color.")] [DefaultValue(typeof (Color), "ControlDark")] public Color InactiveSelectionBackColor { get { return _InactiveSelectionBackColor; } set { _InactiveSelectionBackColor = value; Redraw(); } } /// /// The inactive selection foreground color. /// [Category("Appearance - Selection")] [Description( "The inactive selection foreground color.")] [DefaultValue(typeof (Color), "ControlLight")] public Color InactiveSelectionForeColor { get { return _InactiveSelectionForeColor; } set { _InactiveSelectionForeColor = value; Redraw(); } } /// /// The selection background color. /// [Category("Appearance - Selection")] [Description( "The selection background color.")] [DefaultValue(typeof (Color), "Highlight")] public Color SelectionBackColor { get { return _SelectionBackColor; } set { _SelectionBackColor = value; Redraw(); } } /// /// The selection foreground color. /// [Category("Appearance - Selection")] [Description( "The selection foreground color.")] [DefaultValue(typeof (Color), "HighlightText")] public Color SelectionForeColor { get { return _SelectionForeColor; } set { _SelectionForeColor = value; Redraw(); } } /// /// Gets or Sets the border Color of the gutter margin. /// /// [Category("Appearance - Gutter Margin")] [Description( "The border color of the gutter margin")] [DefaultValue(typeof (Color), "ControlDark")] public Color GutterMarginBorderColor { get { return _GutterMarginBorderColor; } set { _GutterMarginBorderColor = value; InitGraphics(); Redraw(); } } /// /// Gets or Sets the border Color of the line number margin /// /// /// [Category("Appearance - Line Numbers")] [Description( "The border color of the line number margin")] [DefaultValue (typeof (Color), "Teal")] public Color LineNumberBorderColor { get { return _LineNumberBorderColor; } set { _LineNumberBorderColor = value; InitGraphics(); Redraw(); } } /// /// Gets or Sets the foreground Color of a Breakpoint. /// /// [Category("Appearance - BreakPoints")] [Description( "The foreground color of a Breakpoint")] [DefaultValue(typeof (Color), "White")] public Color BreakPointForeColor { get { return _BreakPointForeColor; } set { _BreakPointForeColor = value; Redraw(); } } /// /// Gets or Sets the background Color to use for breakpoint rows. /// /// [Category("Appearance - BreakPoints")] [Description( "The background color to use when BracketMatching is activated") ] [DefaultValue(typeof (Color), "DarkRed")] public Color BreakPointBackColor { get { return _BreakPointBackColor; } set { _BreakPointBackColor = value; Redraw(); } } /// /// Gets or Sets the foreground Color of line numbers. /// /// /// [Category("Appearance - Line Numbers")] [Description( "The foreground color of line numbers")] [DefaultValue(typeof (Color), "Teal")] public Color LineNumberForeColor { get { return _LineNumberForeColor; } set { _LineNumberForeColor = value; InitGraphics(); Redraw(); } } /// /// Gets or Sets the background Color of line numbers. /// /// /// [Category("Appearance - Line Numbers")] [Description( "The background color of line numbers")] [DefaultValue(typeof (Color), "Window")] public Color LineNumberBackColor { get { return _LineNumberBackColor; } set { _LineNumberBackColor = value; InitGraphics(); Redraw(); } } /// /// Gets or Sets the Color of the gutter margin /// /// [Category("Appearance - Gutter Margin")] [Description( "The color of the gutter margin")] [DefaultValue(typeof (Color), "Control")] public Color GutterMarginColor { get { return _GutterMarginColor; } set { _GutterMarginColor = value; InitGraphics(); Redraw(); } } /// /// Gets or Sets the background Color of the client area. /// [Category("Appearance")] [Description( "The background color of the client area")] [DefaultValue(typeof (Color), "Window")] public new Color BackColor { get { return _BackColor; } set { _BackColor = value; InitGraphics(); Redraw(); } } /// /// Gets or Sets the background Color of the active line. /// /// [Category("Appearance - Active Line")] [Description( "The background color of the active line")] [DefaultValue(typeof (Color), "LightYellow")] public Color HighLightedLineColor { get { return _HighLightedLineColor; } set { _HighLightedLineColor = value; InitGraphics(); Redraw(); } } /// /// Determines if the active line should be highlighted. /// [Category("Appearance - Active Line")] [Description( "Determines if the active line should be highlighted") ] [DefaultValue(false)] public bool HighLightActiveLine { get { return _HighLightActiveLine; } set { _HighLightActiveLine = value; Redraw(); } } /// /// Determines if Whitespace should be rendered as symbols. /// [Category("Appearance")] [Description( "Determines if Whitespace should be rendered as symbols") ] [DefaultValue(false)] public bool ShowWhitespace { get { return _ShowWhitespace; } set { _ShowWhitespace = value; Redraw(); } } /// /// Determines if the line number margin should be visible. /// [Category("Appearance - Line Numbers")] [Description( "Determines if the line number margin should be visible") ] [DefaultValue(true)] public bool ShowLineNumbers { get { return _ShowLineNumbers; } set { _ShowLineNumbers = value; Redraw(); } } /// /// Determines if the gutter margin should be visible. /// [Category("Appearance - Gutter Margin")] [Description( "Determines if the gutter margin should be visible") ] [DefaultValue(true)] public bool ShowGutterMargin { get { return _ShowGutterMargin; } set { _ShowGutterMargin = value; Redraw(); } } /// /// Gets or Sets the witdth of the gutter margin in pixels. /// [Category("Appearance - Gutter Margin")] [Description( "Determines the width of the gutter margin in pixels") ] [DefaultValue(19)] public int GutterMarginWidth { get { return _GutterMarginWidth; } set { _GutterMarginWidth = value; Redraw(); } } // ROB: Added .TabsToSpaces property. /// /// Gets or Sets the 'Tabs To Spaces' feature of the editor. /// [Category("Appearance - Tabs")] [Description("Determines whether or not the SyntaxBox converts tabs to spaces as you type.")] [DefaultValue(false)] public bool TabsToSpaces { get; set; } /// /// Get or Sets the size of a TAB char in number of SPACES. /// [Category("Appearance - Tabs")] [Description( "Determines the size of a TAB in number of SPACE chars") ] [DefaultValue(4)] public int TabSize { get { return _TabSize; } set { _TabSize = value; Redraw(); } } #region PUBLIC PROPERTY SHOWSCOPEINDICATOR private bool _ShowScopeIndicator; [Category("Appearance - Scopes"), Description( "Determines if the scope indicator should be shown") ] [DefaultValue(true)] public bool ShowScopeIndicator { get { return _ShowScopeIndicator; } set { _ShowScopeIndicator = value; Redraw(); } } #endregion // END-ROB // ROB: Added method: ConvertTabsToSpaces() /// /// Converts all tabs to spaces the size of .TabSize in the Document. /// public void ConvertTabsToSpaces() { if (_Document != null) { _Document.StartUndoCapture(); var spaces = new string(' ', _TabSize); // Iterate all rows and convert tabs to spaces. for (int count = 0; count < _Document.Count; count++) { Row row = _Document[count]; string rowText = row.Text; string newText = rowText.Replace("\t", spaces); // If this has made a change to the row, update it. if (newText != rowText) { _Document.DeleteRange(new TextRange(0, count, rowText.Length, count)); _Document.InsertText(newText, 0, count, true); } } _Document.EndUndoCapture(); } } // END-ROB // ROB: Added method: ConvertSpacesToTabs() /// /// Converts all spaces the size of .TabSize in the Document to tabs. /// public void ConvertSpacesToTabs() { if (_Document != null) { _Document.StartUndoCapture(); var spaces = new string(' ', _TabSize); // Iterate all rows and convert tabs to spaces. for (int count = 0; count < _Document.Count; count++) { Row row = _Document[count]; string rowText = row.Text; string newText = rowText.Replace(spaces, "\t"); // If this has made a change to the row, update it. if (newText != rowText) { _Document.DeleteRange(new TextRange(0, count, rowText.Length - 1, count)); _Document.InsertText(newText, 0, count, true); } } _Document.EndUndoCapture(); } } // END-ROB #endregion // PUBLIC PROPERTIES #region Public Methods /// /// Gets the Caret object from the active view. /// [Browsable(false)] public Caret Caret { get { if ((_ActiveView) != null) { return ((EditViewControl) _ActiveView).Caret; } return null; } } public void ScrollIntoView(int RowIndex) { ((EditViewControl) _ActiveView).ScrollIntoView(RowIndex); } /// /// Disables painting while loading data into the Autolist /// /// /// /// /// /// /// MySyntaxBox.AutoListClear(); /// MySyntaxBox.AutoListBeginLoad(); /// MySyntaxBox.AutoListAdd ("test",1); /// MySyntaxBox.AutoListAdd ("test",2); /// MySyntaxBox.AutoListAdd ("test",3); /// MySyntaxBox.AutoListAdd ("test",4); /// MySyntaxBox.AutoListEndLoad(); /// /// public void AutoListBeginLoad() { ((EditViewControl) _ActiveView).AutoListBeginLoad(); } /// /// Resumes painting and autosizes the Autolist. /// public void AutoListEndLoad() { ((EditViewControl) _ActiveView).AutoListEndLoad(); } /// /// Clears the content in the autolist. /// public void AutoListClear() { ((EditViewControl) _ActiveView).AutoList.Clear(); } /// /// Adds an item to the autolist control. /// /// /// /// MySyntaxBox.AutoListClear(); /// MySyntaxBox.AutoListBeginLoad(); /// MySyntaxBox.AutoListAdd ("test",1); /// MySyntaxBox.AutoListAdd ("test",2); /// MySyntaxBox.AutoListAdd ("test",3); /// MySyntaxBox.AutoListAdd ("test",4); /// MySyntaxBox.AutoListEndLoad(); /// /// /// The text to display in the autolist /// The image index in the AutoListIcons public void AutoListAdd(string text, int ImageIndex) { ((EditViewControl) _ActiveView).AutoList.Add(text, ImageIndex); } /// /// Adds an item to the autolist control. /// /// The text to display in the autolist /// The text to insert in the code /// The image index in the AutoListIcons public void AutoListAdd(string text, string InsertText, int ImageIndex) { ((EditViewControl) _ActiveView).AutoList.Add(text, InsertText, ImageIndex); } /// /// Adds an item to the autolist control. /// /// The text to display in the autolist /// The text to insert in the code /// /// The image index in the AutoListIcons public void AutoListAdd(string text, string InsertText, string ToolTip, int ImageIndex) { ((EditViewControl) _ActiveView).AutoList.Add(text, InsertText, ToolTip, ImageIndex); } /// /// Converts a Client pixel coordinate into a TextPoint (Column/Row) /// /// Pixel x position /// Pixel y position /// The row and column at the given pixel coordinate. public TextPoint CharFromPixel(int x, int y) { return ((EditViewControl) _ActiveView).CharFromPixel(x, y); } /// /// Clears the selection in the active view. /// public void ClearSelection() { ((EditViewControl) _ActiveView).ClearSelection(); } /// /// Executes a Copy action on the selection in the active view. /// public void Copy() { ((EditViewControl) _ActiveView).Copy(); } /// /// Executes a Cut action on the selection in the active view. /// public void Cut() { ((EditViewControl) _ActiveView).Cut(); } /// /// Executes a Delete action on the selection in the active view. /// public void Delete() { ((EditViewControl) _ActiveView).Delete(); } /// /// Moves the caret of the active view to a specific row. /// /// the row to jump to public void GotoLine(int RowIndex) { ((EditViewControl) _ActiveView).GotoLine(RowIndex); } /// /// Moves the caret of the active view to the next bookmark. /// public void GotoNextBookmark() { ((EditViewControl) _ActiveView).GotoNextBookmark(); } /// /// Moves the caret of the active view to the previous bookmark. /// public void GotoPreviousBookmark() { ((EditViewControl) _ActiveView).GotoPreviousBookmark(); } /// /// Takes a pixel position and returns true if that position is inside the selected text. /// /// /// Pixel x position. /// Pixel y position /// true if the position is inside the selection. public bool IsOverSelection(int x, int y) { return ((EditViewControl) _ActiveView).IsOverSelection(x, y); } /// /// Execute a Paste action if possible. /// public void Paste() { ((EditViewControl) _ActiveView).Paste(); } /// /// Execute a ReDo action if possible. /// public void Redo() { ((EditViewControl) _ActiveView).Redo(); } /// /// Makes the caret in the active view visible on screen. /// public void ScrollIntoView() { ((EditViewControl) _ActiveView).ScrollIntoView(); } /// /// Scrolls the active view to a specific position. /// /// public void ScrollIntoView(TextPoint Pos) { ((EditViewControl) _ActiveView).ScrollIntoView(Pos); } /// /// Select all the text in the active view. /// public void SelectAll() { ((EditViewControl) _ActiveView).SelectAll(); } /// /// Selects the next word (from the current caret position) that matches the parameter criterias. /// /// The pattern to find /// Match case , true/false /// Match whole words only , true/false /// To be implemented public void FindNext(string Pattern, bool MatchCase, bool WholeWords, bool UseRegEx) { ((EditViewControl) _ActiveView).SelectNext(Pattern, MatchCase, WholeWords, UseRegEx); } /// /// Finds the next occurance of the pattern in the find/replace dialog /// public void FindNext() { ((EditViewControl) _ActiveView).FindNext(); } /// /// Shows the default GotoLine dialog. /// /// /// /// //Display the Goto Line dialog /// MySyntaxBox.ShowGotoLine(); /// /// public void ShowGotoLine() { ((EditViewControl) _ActiveView).ShowGotoLine(); } /// /// Not yet implemented /// public void ShowSettings() { ((EditViewControl) _ActiveView).ShowSettings(); } /// /// Toggles a bookmark on the active row of the active view. /// public void ToggleBookmark() { ((EditViewControl) _ActiveView).ToggleBookmark(); } /// /// Executes an undo action if possible. /// public void Undo() { ((EditViewControl) _ActiveView).Undo(); } /// /// Shows the Find dialog /// /// /// /// //Show FindReplace dialog /// MySyntaxBox.ShowFind(); /// /// public void ShowFind() { ((EditViewControl) _ActiveView).ShowFind(); } /// /// Shows the Replace dialog /// /// /// /// //Show FindReplace dialog /// MySyntaxBox.ShowReplace(); /// /// public void ShowReplace() { ((EditViewControl) _ActiveView).ShowReplace(); } #endregion //END Public Methods [Browsable(false)] [Obsolete("Use .FontName and .FontSize", true)] public override Font Font { get { return base.Font; } set { base.Font = value; } } // [Browsable(true)] // [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)] // [RefreshProperties (RefreshProperties.All)] // public override string Text // { // get // { // return this.Document.Text; // } // set // { // this.Document.Text=value; // } // } [Browsable(false)] [Obsolete("Apply a syntax instead", true)] public override Color ForeColor { get { return base.ForeColor; } set { base.ForeColor = value; } } /// /// The currently highlighted text in the autolist. /// [Browsable(false)] public string AutoListSelectedText { get { return ((EditViewControl) _ActiveView).AutoList.SelectedText; } set { if ((_ActiveView) == null || ((EditViewControl) _ActiveView).AutoList == null) return; ((EditViewControl) _ActiveView).AutoList.SelectItem(value); } } public void Save(string filename) { string text = Document.Text; var swr = new StreamWriter(filename); swr.Write(text); swr.Flush(); swr.Close(); } public void Open(string filename) { if (Document == null) throw new NullReferenceException("CodeEditorControl.Document"); var swr = new StreamReader(filename); Document.Text = swr.ReadToEnd(); swr.Close(); } public void AttachDocument(SyntaxDocument document) { //_Document=document; if (_Document != null) { _Document.ParsingCompleted -= OnParsingCompleted; _Document.Parsing -= OnParse; _Document.Change -= OnChange; } if (document == null) document = new SyntaxDocument(); _Document = document; if (_Document != null) { _Document.ParsingCompleted += OnParsingCompleted; _Document.Parsing += OnParse; _Document.Change += OnChange; } Redraw(); } protected virtual void OnParse(object Sender, EventArgs e) { foreach (EditViewControl ev in Views) { ev.OnParse(); } } protected virtual void OnParsingCompleted(object Sender, EventArgs e) { foreach (EditViewControl ev in Views) { ev.Invalidate(); } } protected virtual void OnChange(object Sender, EventArgs e) { if (Views == null) return; foreach (EditViewControl ev in Views) { ev.OnChange(); } OnTextChanged(EventArgs.Empty); } public void RemoveCurrentRow() { ((EditViewControl) _ActiveView).RemoveCurrentRow(); } public void CutClear() { ((EditViewControl) _ActiveView).CutClear(); } public void AutoListInsertSelectedText() { ((EditViewControl) _ActiveView).InsertAutolistText(); } protected override SplitViewChildControl GetNewView() { return new EditViewControl(this); } protected override void OnImeModeChanged(EventArgs e) { base.OnImeModeChanged(e); foreach (EditViewControl ev in Views) { ev.ImeMode = ImeMode; } } #region Constructor /// /// Default constructor for the SyntaxBoxControl /// public SyntaxBoxControl() { try { Document = new SyntaxDocument(); CreateViews(); InitializeComponent(); SetStyle(ControlStyles.Selectable, true); //assign keys KeyboardActions.Add(new KeyboardAction(Keys.Z, false, true, false, false, Undo)); KeyboardActions.Add(new KeyboardAction(Keys.Y, false, true, false, false, Redo)); KeyboardActions.Add(new KeyboardAction(Keys.F3, false, false, false, true, FindNext)); KeyboardActions.Add(new KeyboardAction(Keys.C, false, true, false, true, Copy)); KeyboardActions.Add(new KeyboardAction(Keys.X, false, true, false, false, CutClear)); KeyboardActions.Add(new KeyboardAction(Keys.V, false, true, false, false, Paste)); KeyboardActions.Add(new KeyboardAction(Keys.Insert, false, true, false, true, Copy)); KeyboardActions.Add(new KeyboardAction(Keys.Delete, true, false, false, false, Cut)); KeyboardActions.Add(new KeyboardAction(Keys.Insert, true, false, false, false, Paste)); KeyboardActions.Add(new KeyboardAction(Keys.A, false, true, false, true, SelectAll)); KeyboardActions.Add(new KeyboardAction(Keys.F, false, true, false, false, ShowFind)); KeyboardActions.Add(new KeyboardAction(Keys.H, false, true, false, false, ShowReplace)); KeyboardActions.Add(new KeyboardAction(Keys.G, false, true, false, true, ShowGotoLine)); KeyboardActions.Add(new KeyboardAction(Keys.T, false, true, false, false, ShowSettings)); KeyboardActions.Add(new KeyboardAction(Keys.F2, false, true, false, true, ToggleBookmark)); KeyboardActions.Add(new KeyboardAction(Keys.F2, false, false, false, true, GotoNextBookmark)); KeyboardActions.Add(new KeyboardAction(Keys.F2, true, false, false, true, GotoPreviousBookmark) ); KeyboardActions.Add(new KeyboardAction(Keys.Escape, false, false, false, true, ClearSelection)); KeyboardActions.Add(new KeyboardAction(Keys.Tab, false, false, false, false, Selection.Indent)); KeyboardActions.Add(new KeyboardAction(Keys.Tab, true, false, false, false, Selection.Outdent)); AutoListIcons = _AutoListIcons; } catch { // Console.WriteLine (x.StackTrace); } } #endregion //END Constructor #region EventHandlers protected virtual void OnClipboardUpdated(CopyEventArgs e) { if (ClipboardUpdated != null) ClipboardUpdated(this, e); } protected virtual void OnRowMouseDown(RowMouseEventArgs e) { if (RowMouseDown != null) RowMouseDown(this, e); } protected virtual void OnRowMouseMove(RowMouseEventArgs e) { if (RowMouseMove != null) RowMouseMove(this, e); } protected virtual void OnRowMouseUp(RowMouseEventArgs e) { if (RowMouseUp != null) RowMouseUp(this, e); } protected virtual void OnRowClick(RowMouseEventArgs e) { if (RowClick != null) RowClick(this, e); } protected virtual void OnRowDoubleClick(RowMouseEventArgs e) { if (RowDoubleClick != null) RowDoubleClick(this, e); } private void ParseTimer_Tick(object sender, EventArgs e) { Document.ParseSome(); } protected virtual void OnInfoTipSelectedIndexChanged() { if (InfoTipSelectedIndexChanged != null) InfoTipSelectedIndexChanged(null, null); } /// /// /// /// protected override void OnEnter(EventArgs e) { base.OnEnter(e); if ((_ActiveView) != null) { (_ActiveView).Focus(); } } private void View_RowClick(object sender, RowMouseEventArgs e) { OnRowClick(e); } private void View_RowDoubleClick(object sender, RowMouseEventArgs e) { OnRowDoubleClick(e); } private void View_RowMouseDown(object sender, RowMouseEventArgs e) { OnRowMouseDown(e); } private void View_RowMouseMove(object sender, RowMouseEventArgs e) { OnRowMouseMove(e); } private void View_RowMouseUp(object sender, RowMouseEventArgs e) { OnRowMouseUp(e); } private void View_ClipboardUpdated(object sender, CopyEventArgs e) { OnClipboardUpdated(e); } public void OnRenderRow(RowPaintEventArgs e) { if (RenderRow != null) RenderRow(this, e); } public void OnWordMouseHover(ref WordMouseEventArgs e) { if (WordMouseHover != null) WordMouseHover(this, ref e); } public void OnWordMouseDown(ref WordMouseEventArgs e) { if (WordMouseDown != null) WordMouseDown(this, ref e); } protected virtual void OnCaretChange(object sender) { if (CaretChange != null) CaretChange(this, null); } protected virtual void OnSelectionChange(object sender) { if (SelectionChange != null) SelectionChange(this, null); } private void View_CaretChanged(object s, EventArgs e) { OnCaretChange(s); } private void View_SelectionChanged(object s, EventArgs e) { OnSelectionChange(s); } private void View_DoubleClick(object sender, EventArgs e) { OnDoubleClick(e); } private void View_MouseUp(object sender, MouseEventArgs e) { var ev = (EditViewControl) sender; var ea = new MouseEventArgs(e.Button, e.Clicks, e.X + ev.Location.X + ev.BorderWidth, e.Y + ev.Location.Y + ev.BorderWidth, e.Delta); OnMouseUp(ea); } private void View_MouseMove(object sender, MouseEventArgs e) { var ev = (EditViewControl) sender; var ea = new MouseEventArgs(e.Button, e.Clicks, e.X + ev.Location.X + ev.BorderWidth, e.Y + ev.Location.Y + ev.BorderWidth, e.Delta); OnMouseMove(ea); } private void View_MouseLeave(object sender, EventArgs e) { OnMouseLeave(e); } private void View_MouseHover(object sender, EventArgs e) { OnMouseHover(e); } private void View_MouseEnter(object sender, EventArgs e) { OnMouseEnter(e); } private void View_MouseDown(object sender, MouseEventArgs e) { var ev = (EditViewControl) sender; var ea = new MouseEventArgs(e.Button, e.Clicks, e.X + ev.Location.X + ev.BorderWidth, e.Y + ev.Location.Y + ev.BorderWidth, e.Delta); OnMouseDown(ea); } private void View_KeyUp(object sender, KeyEventArgs e) { OnKeyUp(e); } private void View_KeyPress(object sender, KeyPressEventArgs e) { OnKeyPress(e); } private void View_KeyDown(object sender, KeyEventArgs e) { OnKeyDown(e); } private void View_Click(object sender, EventArgs e) { OnClick(e); } private void View_DragOver(object sender, DragEventArgs e) { OnDragOver(e); } private void View_DragLeave(object sender, EventArgs e) { OnDragLeave(e); } private void View_DragEnter(object sender, DragEventArgs e) { OnDragEnter(e); } private void View_DragDrop(object sender, DragEventArgs e) { OnDragDrop(e); } private void View_InfoTipSelectedIndexChanged(object sender, EventArgs e) { OnInfoTipSelectedIndexChanged(); } #endregion #region DISPOSE() /// /// /// protected override void Dispose(bool disposing) { if (disposing) { if (components != null) components.Dispose(); } base.Dispose(disposing); } #endregion //END DISPOSE #region Private/Protected/Internal methods private void InitializeComponent() { components = new Container(); var resources = new ResourceManager(typeof (SyntaxBoxControl)); _GutterIcons = new ImageList(components); _AutoListIcons = new ImageList(components); ParseTimer = new Timer (components); // // _GutterIcons // _GutterIcons.ColorDepth = ColorDepth.Depth32Bit; _GutterIcons.ImageSize = new Size(17, 17); _GutterIcons.ImageStream = ((ImageListStreamer) (resources.GetObject( "_GutterIcons.ImageStream"))); _GutterIcons.TransparentColor = Color.Transparent; // // _AutoListIcons // _AutoListIcons.ColorDepth = ColorDepth.Depth8Bit; _AutoListIcons.ImageSize = new Size(16, 16); _AutoListIcons.ImageStream = ( (ImageListStreamer) (resources.GetObject( "_AutoListIcons.ImageStream"))); _AutoListIcons.TransparentColor = Color.Transparent; // // ParseTimer // ParseTimer.Enabled = true; ParseTimer.Interval = 1; ParseTimer.Tick += ParseTimer_Tick; } protected override void OnLoad(EventArgs e) { Refresh(); } private void Redraw() { if (Views == null) return; foreach (EditViewControl ev in Views) { if (ev != null) { ev.Refresh(); } } } private void InitGraphics() { if (Views == null || Parent == null) return; foreach (EditViewControl ev in Views) { ev.InitGraphics(); } } protected override void CreateViews() { base.CreateViews(); foreach (EditViewControl ev in Views) { if (DoOnce && ev == LowerRight) continue; //attatch events to views ev.Enter += View_Enter; ev.Leave += View_Leave; ev.GotFocus += View_Enter; ev.LostFocus += View_Leave; ev.CaretChange += View_CaretChanged; ev.SelectionChange += View_SelectionChanged; ev.Click += View_Click; ev.DoubleClick += View_DoubleClick; ev.MouseDown += View_MouseDown; ev.MouseEnter += View_MouseEnter; ev.MouseHover += View_MouseHover; ev.MouseLeave += View_MouseLeave; ev.MouseMove += View_MouseMove; ev.MouseUp += View_MouseUp; ev.KeyDown += View_KeyDown; ev.KeyPress += View_KeyPress; ev.KeyUp += View_KeyUp; ev.DragDrop += View_DragDrop; ev.DragOver += View_DragOver; ev.DragLeave += View_DragLeave; ev.DragEnter += View_DragEnter; if (ev.InfoTip != null) { ev.InfoTip.Data = ""; ev.InfoTip.SelectedIndexChanged += View_InfoTipSelectedIndexChanged; } ev.RowClick += View_RowClick; ev.RowDoubleClick += View_RowDoubleClick; ev.RowMouseDown += View_RowMouseDown; ev.RowMouseMove += View_RowMouseMove; ev.RowMouseUp += View_RowMouseUp; ev.ClipboardUpdated += View_ClipboardUpdated; } DoOnce = true; AutoListIcons = AutoListIcons; InfoTipImage = InfoTipImage; ChildBorderStyle = ChildBorderStyle; ChildBorderColor = ChildBorderColor; BackColor = BackColor; Document = Document; ImeMode = ImeMode; Redraw(); } #endregion //END Private/Protected/Internal methods } }