|
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using EnvDTE;
namespace RegionsAddIn
{
/// <summary>
/// Summary description for ExplorerControl.
/// </summary>
public class ExplorerControl : System.Windows.Forms.UserControl
{
private System.Windows.Forms.ToolBar toolBar;
private System.Windows.Forms.ImageList imageListButtons;
private System.ComponentModel.IContainer components;
private System.Windows.Forms.ToolBarButton buttonGoToRegion;
private System.Windows.Forms.ToolBarButton buttonPasteToRegion;
private System.Windows.Forms.ToolBarButton Separator;
private System.Windows.Forms.ToolBarButton buttonManagerSetup;
private Document _document;
private RegionsAddIn.RegionTree _regionTree;
private UserPreferences _userPreferences;
public ExplorerControl()
{
// This call is required by the Windows.Forms Form Designer.
InitializeComponent();
}
#region Properties
/// <summary>
/// The RegionTree control for this control.
/// </summary>
public RegionTree RegionTree
{
get {return _regionTree;}
}
/// <summary>
/// The User preferences.
/// </summary>
public UserPreferences UserPreferences
{
get {return _userPreferences;}
set {_userPreferences = value;}
}
#endregion
/// <summary>
/// Clear the RegionTree control.
/// </summary>
public void Clear()
{
_regionTree.Clear();
}
/// <summary>
/// Refresh the RegionTree control.
/// </summary>
/// <param name="document">
/// EnvDTE.Document the tree represents.
/// </param>
public void Refresh(Document document)
{
string selectedItemPath = string.Empty;
if(_regionTree.TreeView.SelectedNode != null)
selectedItemPath = _regionTree.TreeView.SelectedNode.FullPath;
buttonGoToRegion.Enabled = false;
buttonPasteToRegion.Enabled = false;
_document = document;
_regionTree.UserPreferences = _userPreferences;
_regionTree.BuildTree(document);
_regionTree.SelectNode(selectedItemPath, _regionTree.TreeView.Nodes);
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(ExplorerControl));
this.toolBar = new System.Windows.Forms.ToolBar();
this.buttonGoToRegion = new System.Windows.Forms.ToolBarButton();
this.buttonPasteToRegion = new System.Windows.Forms.ToolBarButton();
this.Separator = new System.Windows.Forms.ToolBarButton();
this.buttonManagerSetup = new System.Windows.Forms.ToolBarButton();
this.imageListButtons = new System.Windows.Forms.ImageList(this.components);
this._regionTree = new RegionsAddIn.RegionTree();
this.SuspendLayout();
//
// toolBar
//
this.toolBar.Appearance = System.Windows.Forms.ToolBarAppearance.Flat;
this.toolBar.Buttons.AddRange(new System.Windows.Forms.ToolBarButton[] {
this.buttonGoToRegion,
this.buttonPasteToRegion,
this.Separator,
this.buttonManagerSetup});
this.toolBar.ButtonSize = new System.Drawing.Size(20, 20);
this.toolBar.Divider = false;
this.toolBar.DropDownArrows = true;
this.toolBar.ImageList = this.imageListButtons;
this.toolBar.Location = new System.Drawing.Point(0, 0);
this.toolBar.Name = "toolBar";
this.toolBar.ShowToolTips = true;
this.toolBar.Size = new System.Drawing.Size(232, 26);
this.toolBar.TabIndex = 1;
this.toolBar.ButtonClick += new System.Windows.Forms.ToolBarButtonClickEventHandler(this.toolBar_ButtonClick);
//
// buttonGoToRegion
//
this.buttonGoToRegion.Enabled = false;
this.buttonGoToRegion.ImageIndex = 0;
this.buttonGoToRegion.Tag = "Go";
this.buttonGoToRegion.ToolTipText = "Go To Region";
//
// buttonPasteToRegion
//
this.buttonPasteToRegion.Enabled = false;
this.buttonPasteToRegion.ImageIndex = 1;
this.buttonPasteToRegion.Tag = "Paste";
this.buttonPasteToRegion.ToolTipText = "Paste To Region";
//
// Separator
//
this.Separator.Style = System.Windows.Forms.ToolBarButtonStyle.Separator;
//
// buttonManagerSetup
//
this.buttonManagerSetup.ImageIndex = 2;
this.buttonManagerSetup.Tag = "Setup";
this.buttonManagerSetup.ToolTipText = "Setup Region Manager";
//
// imageListButtons
//
this.imageListButtons.ImageSize = new System.Drawing.Size(16, 16);
this.imageListButtons.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageListButtons.ImageStream")));
this.imageListButtons.TransparentColor = System.Drawing.Color.Transparent;
//
// _regionTree
//
this._regionTree.ApplicationObject = null;
this._regionTree.Dock = System.Windows.Forms.DockStyle.Fill;
this._regionTree.Location = new System.Drawing.Point(0, 26);
this._regionTree.Name = "_regionTree";
this._regionTree.Size = new System.Drawing.Size(232, 230);
this._regionTree.TabIndex = 2;
this._regionTree.TreeDragDrop += new System.Windows.Forms.DragEventHandler(this._regionTree_DragDrop);
this._regionTree.TreeDragEnter += new System.Windows.Forms.DragEventHandler(this._regionTree_DragEnter);
this._regionTree.TreeDoubleClick += new System.EventHandler(this._regionTree_DoubleClick);
this._regionTree.TreeAfterSelect += new System.Windows.Forms.TreeViewEventHandler(this._regionTree_AfterSelect);
this._regionTree.TreeDragOver += new System.Windows.Forms.DragEventHandler(this._regionTree_DragOver);
//
// ExplorerControl
//
this.Controls.Add(this._regionTree);
this.Controls.Add(this.toolBar);
this.Name = "ExplorerControl";
this.Size = new System.Drawing.Size(232, 256);
this.ResumeLayout(false);
}
#endregion
#region Drag 'n' Drop
/// <summary>
/// Handler for DragEnter event.
/// </summary>
private void _regionTree_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
{
Refresh(_document);
if(!e.Data.GetDataPresent(DataFormats.Text))
{
e.Effect = DragDropEffects.None;
return;
}
if((e.KeyState & 4) == 4 && (e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move)
{
// SHIFT KeyState for move.
e.Effect = DragDropEffects.Move;
}
else if((e.KeyState & 8) == 8 && (e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy)
{
// CTL KeyState for copy.
e.Effect = DragDropEffects.Copy;
}
else if((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move)
{
// By default, the drop action should be move, if allowed.
e.Effect = DragDropEffects.Move;
}
else
e.Effect = DragDropEffects.None;
}
/// <summary>
/// Handler for DragOver event.
/// </summary>
private void _regionTree_DragOver(object sender, System.Windows.Forms.DragEventArgs e)
{
if(!e.Data.GetDataPresent(DataFormats.Text))
{
e.Effect = DragDropEffects.None;
return;
}
TreeNode targetNode = _regionTree.TreeView.GetNodeAt(_regionTree.TreeView.PointToClient(new Point(e.X, e.Y)));
if(!((TreeItemTagData)targetNode.Tag).IsRegion)
{
e.Effect = DragDropEffects.None;
return;
}
if(targetNode.ForeColor == SystemColors.GrayText)
{
//only allow copy if target region is inside current selection
e.Effect = DragDropEffects.Copy;
return;
}
if((e.KeyState & 4) == 4 && (e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move)
{
// SHIFT KeyState for move.
e.Effect = DragDropEffects.Move;
}
else if((e.KeyState & 8) == 8 && (e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy)
{
// CTL KeyState for copy.
e.Effect = DragDropEffects.Copy;
}
else if((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move)
{
// By default, the drop action should be move, if allowed.
e.Effect = DragDropEffects.Move;
}
else
e.Effect = DragDropEffects.None;
}
/// <summary>
/// Handler for DragDrop event.
/// </summary>
private void _regionTree_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
{
if(e.Data.GetDataPresent(DataFormats.Text))
{
TreeNode targetNode = _regionTree.TreeView.GetNodeAt(_regionTree.TreeView.PointToClient(new Point(e.X, e.Y)));
if(((TreeItemTagData)targetNode.Tag).IsRegion)
{
String text = (string)e.Data.GetData(DataFormats.Text);
// Perform drag and drop, depending upon the effect.
if(e.Effect == DragDropEffects.Copy || e.Effect == DragDropEffects.Move)
{
// Insert the text.
_regionTree.AddTextToRegion((TreeItemTagData)targetNode.Tag, text, 0);
_regionTree.BuildTree(_document);
}
}
}
}
#endregion
/// <summary>
/// Handler for toolbar button click event.
/// </summary>
private void toolBar_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
{
switch(e.Button.Tag.ToString())
{
case "Go": //buttonGoToRegion
{
if(((TreeItemTagData)_regionTree.SelectedNode.Tag).IsRegion)
{
GoToRegion(_regionTree.SelectedNode);
}
break;
}
case "Paste": //buttonPasteToRegion
{
if(((TreeItemTagData)_regionTree.SelectedNode.Tag).IsRegion)
{
string text = string.Empty;
//get clipboard text
try
{
IDataObject data = Clipboard.GetDataObject();
// If the data is text, then get the text from the clipboard.
if(data.GetDataPresent(DataFormats.Text))
text = data.GetData(DataFormats.Text).ToString();
}
catch(Exception)
{
MessageBox.Show("An error occured while pasting data.");
break;
}
_regionTree.AddTextToRegion((TreeItemTagData)_regionTree.SelectedNode.Tag, text, 0);
}
break;
}
case "Setup": //buttonManagerSetup
{
AddinSetup dlg = new AddinSetup(_userPreferences);
dlg.ShowDialog();
dlg.Dispose();
break;
}
}
}
/// <summary>
/// Handler for tree selection changed event.
/// </summary>
private void _regionTree_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)
{
if(_regionTree.SelectedNode != null)
{
if(((TreeItemTagData)_regionTree.SelectedNode.Tag).IsRegion)
{
buttonGoToRegion.Enabled = true;
buttonPasteToRegion.Enabled = true;
}
else
{
buttonGoToRegion.Enabled = false;
buttonPasteToRegion.Enabled = false;
}
}
else
{
buttonGoToRegion.Enabled = false;
buttonPasteToRegion.Enabled = false;
}
}
/// <summary>
/// Handler for tree selection doubleclick event.
/// </summary>
private void _regionTree_DoubleClick(object sender, System.EventArgs e)
{
if(((TreeItemTagData)_regionTree.SelectedNode.Tag).IsRegion)
{
GoToRegion(_regionTree.SelectedNode);
}
}
/// <summary>
/// Display the start of the specified region.
/// </summary>
/// <param name="region">
/// TreeItemTagData containing the start line of the target region.
/// </param>
private void GoToRegion(TreeNode node)
{
//find the window containing the text for this document
//(VS automation has a bug, which can show a DesignerHost window instead of the text document window)
bool windowFound = false;
foreach(Window window in _document.Windows)
{
if(window.Object.GetType().ToString() != "Microsoft.VisualStudio.Designer.Host.DesignerHost")
{
window.SetFocus();
windowFound = true;
}
}
if(!windowFound)
{
this._regionTree.ApplicationObject.StatusBar.Text = "Unable to find active document";
return;
}
try
{
TextDocument doc = _document.Object("TextDocument") as TextDocument;
doc.Selection.MoveTo(((TreeItemTagData)node.Tag).StartLine, 1, false);
}
catch(ArgumentException)
{
//the line we are trying to move to is hidden in a collapsed outline
//try to expand the parent outlines of this line
TreeNode parent = node.Parent;
ArrayList lineNumbers = new ArrayList();
lineNumbers.Insert(0, ((TreeItemTagData)node.Tag).StartLine);
while(parent != null)
{
lineNumbers.Insert(0, ((TreeItemTagData)parent.Tag).StartLine);
parent = parent.Parent;
}
foreach(Int32 line in lineNumbers)
{
Int32 index = lineNumbers.IndexOf(line);
try
{
TextDocument doc = _document.Object("TextDocument") as TextDocument;
doc.Selection.MoveTo(line, 1, false);
}
catch(ArgumentException)
{
//line is in a collapsed outline
Int32 collapsedLine = (Int32)lineNumbers[index -1];
ExpandOutline(collapsedLine);
TextDocument doc = _document.Object("TextDocument") as TextDocument;
doc.Selection.MoveTo(line, 1, false);
}
}
}
}
/// <summary>
/// Expand outline.
/// </summary>
/// <param name="line">
/// line number to expand.
/// </param>
private void ExpandOutline(Int32 line)
{
TextDocument doc = _document.Object("TextDocument") as TextDocument;
doc.Selection.MoveTo(line, 1, false);
//we have to set the focus to the texteditor window,
//to allow us to use the Edit.ToggleOutliningExpansion command.
Window window = _document.ActiveWindow;
window.SetFocus();
_regionTree.ApplicationObject.ExecuteCommand("Edit.ToggleOutliningExpansion","");
}
}
}
|
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.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.