Click here to Skip to main content
15,879,068 members
Articles / Programming Languages / XSLT

XML Visualizer v.2

Rate me:
Please Sign up or sign in to vote.
4.88/5 (107 votes)
16 Nov 2016CPOL5 min read 292.5K   18.7K   404  
XML Visualizer v.2 improves the standard XML Visualizer in Visual Studio 2005, 2008, 2010, 2012, 2013 and 2015.
// Xml Visualizer v.2
// by Lars Hove Christiansen (larshove@gmail.com)
// http://www.codeplex.com/XmlVisualizer

using System;
using System.Windows.Forms;
using ICSharpCode.TextEditor;
using ICSharpCode.TextEditor.Document;

namespace XmlVisualizer
{
	internal partial class EditorUserControl : UserControl
	{
		public delegate void CaretChangeEventHandler(int line, int column);
		public event CaretChangeEventHandler CaretChangeEvent;

		private bool textChanged;

		public EditorUserControl()
		{
			InitializeComponent();

			textEditorControl.TabIndent = 3;
			textEditorControl.Document.HighlightingStrategy = HighlightingManager.Manager.FindHighlighter("XML");

			textEditorControl.ActiveTextAreaControl.Caret.PositionChanged += Caret_PositionChanged;
			textEditorControl.Document.DocumentChanged += Document_DocumentChanged;
		}

		private void Caret_PositionChanged(object sender, EventArgs e)
		{
			int line = textEditorControl.ActiveTextAreaControl.Caret.Line + 1;
			int column = textEditorControl.ActiveTextAreaControl.Caret.Column + 1;

			if (CaretChangeEvent != null)
			{
				CaretChangeEvent(line, column);
			}
		}

		private void Document_DocumentChanged(object sender, DocumentEventArgs e)
		{
			textChanged = true;
		}

		public string GetText()
		{
			return textEditorControl.Text;
		}

		public void SetText(string text)
		{
			bool modified = ChangesInEditor;
			textEditorControl.Text = text;
			textEditorControl.Refresh();
			ChangesInEditor = modified;
		}

		public void SetFocus()
		{
			textEditorControl.Focus();
		}

		public int GetActiveLineNumber()
		{
			return textEditorControl.ActiveTextAreaControl.Caret.Line;
		}

		public int GetActiveColumnNumber()
		{
			return textEditorControl.ActiveTextAreaControl.Caret.Column;
		}

		public void GotoLine(int line, int column)
		{
			textEditorControl.ActiveTextAreaControl.Caret.Line = line - 1;
			textEditorControl.ActiveTextAreaControl.Caret.Column = column - 1;
			textEditorControl.ActiveTextAreaControl.CenterViewOn(line - 1, 0);
			textEditorControl.Focus();
		}

		public bool ChangesInEditor
		{
			get
			{
				return textChanged;
			}
			set
			{
				textChanged = value;
			}
		}

		private void toolStripMenuItemRedo_Click(object sender, EventArgs e)
		{
			textEditorControl.Redo();
		}

		private void toolStripMenuItemUndo_Click(object sender, EventArgs e)
		{
			textEditorControl.Undo();
		}

		private void contextMenuStrip_Opening(object sender, System.ComponentModel.CancelEventArgs e)
		{
			if (textEditorControl.Document.UndoStack.CanUndo)
			{
				toolStripMenuItemUndo.Enabled = true;
			}
			else
			{
				toolStripMenuItemUndo.Enabled = false;
			}

			if (textEditorControl.Document.UndoStack.CanRedo)
			{
				toolStripMenuItemRedo.Enabled = true;
			}
			else
			{
				toolStripMenuItemRedo.Enabled = false;
			}
		}

		private void toolStripMenuItemCut_Click(object sender, EventArgs e)
		{
			textEditorControl.ActiveTextAreaControl.TextArea.ClipboardHandler.Cut(sender, e);
		}

		private void toolStripMenuItemCopy_Click(object sender, EventArgs e)
		{
			textEditorControl.ActiveTextAreaControl.TextArea.ClipboardHandler.Copy(sender, e);
		}

		private void toolStripMenuItemPaste_Click(object sender, EventArgs e)
		{
			textEditorControl.ActiveTextAreaControl.TextArea.ClipboardHandler.Paste(sender, e);
		}

		private void toolStripMenuItemSelectAll_Click(object sender, EventArgs e)
		{
			SelectAll();
		}

		protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
		{
			if (textEditorControl.ActiveTextAreaControl.TextArea.Focused)
			{
				if ((int)keyData == 131137) // Keys.Control && keyData == Keys.A
				{
					SelectAll();
					return true;
				}
			}

			return base.ProcessCmdKey(ref msg, keyData);
		}

		private void SelectAll()
		{
			TextLocation startPosition = new TextLocation(0, 0);

			int textLength = textEditorControl.ActiveTextAreaControl.Document.TextLength;
			TextLocation endPosition = new TextLocation();
			endPosition.Column = textEditorControl.Document.OffsetToPosition(textLength).Column;
			endPosition.Line = textEditorControl.Document.OffsetToPosition(textLength).Line;

			textEditorControl.ActiveTextAreaControl.SelectionManager.SetSelection(startPosition, endPosition);
			textEditorControl.ActiveTextAreaControl.Caret.Position = endPosition;
		}

		private void toolStripMenuItemDelete_Click(object sender, EventArgs e)
		{
			textEditorControl.ActiveTextAreaControl.TextArea.ClipboardHandler.Delete(sender, e);
		}

		public void Search(string searchText)
		{
			if (searchText.Trim().Trim().Length == 0)
			{
				return;
			}

			int startIndex = textEditorControl.ActiveTextAreaControl.Caret.Offset;

			if (startIndex > textEditorControl.Document.TextContent.ToLower().LastIndexOf(searchText.ToLower()))
			{
				startIndex = 0;
			}

			int offset = textEditorControl.Document.TextContent.ToLower().IndexOf(searchText.ToLower(), startIndex);

			if (offset >= 0)
			{
				TextLocation startPosition = textEditorControl.Document.OffsetToPosition(offset);
				TextLocation endPosition = textEditorControl.Document.OffsetToPosition(offset + searchText.Length);

				textEditorControl.ActiveTextAreaControl.SelectionManager.SetSelection(startPosition, endPosition);

				GotoLine(textEditorControl.Document.GetLineNumberForOffset(offset) + 1, startPosition.Column + searchText.Length + 1);
			}
			else
			{
				Util.ShowMessage(string.Format("'{0}' not found.", searchText));
			}
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Denmark Denmark
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions