using System;
using System.Diagnostics;
using System.Collections;
using System.Drawing;
using System.Windows.Forms;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
namespace Netron.Diagramming.Core
{
// ----------------------------------------------------------------------
///
/// A tool that copies the selected entities to the clipboard.
///
// ----------------------------------------------------------------------
public class CopyTool : AbstractTool
{
#region Fields
#endregion
#region Constructor
// ------------------------------------------------------------------
///
/// Initializes a new instance of the class.
///
/// string: The name of the tool.
// ------------------------------------------------------------------
public CopyTool(string name)
: base(name)
{
}
#endregion
#region Methods
// ------------------------------------------------------------------
///
/// Called when the tool is activated.
///
// ------------------------------------------------------------------
protected override void OnActivateTool()
{
if(Selection.SelectedItems.Count == 0)
return;
try
{
//Clear the Anchors otherwise subsequent Copy operations will
// raise an exception due to the fact that the Anchors class
// is a static helper class.
Anchors.Clear();
// First add the image to the Windows Clipboard so it can be
// pasted into other applications, like PowerPoint.
Bitmap image = Selection.ToBitmap();
Clipboard.SetDataObject(image, true);
// This will create a volatile collection of entities, but they
// need to be unwrapped! I never managed to get things
// working by putting the serialized collection directly onto
// the Clipboad. Thanks to Leppie's suggestion it works by
// putting the Stream onto the Clipboard, which is weird but
// it works.
//MemoryStream copy = Selection.SelectedItems.ToStream();
//DataFormats.Format format =
// DataFormats.GetFormat(typeof(CopyTool).FullName);
//IDataObject dataObject = new DataObject();
//dataObject.SetData(format.Name, false, copy);
//Clipboard.SetDataObject(dataObject, false);
// Rather than placing the stream of entities on the Windows
// Clipboard, we're using our custom clipboard to keep the
// Windows clipboard for moving data across apps.
NetronClipboard.Clear();
NetronClipboard.Add(Selection.SelectedItems.Copy());
}
catch (Exception exc)
{
//throw new InconsistencyException(
// "The Copy operation failed.", exc);
MessageBox.Show("Unable to copy the selection.\n\n" +
"Error Message: " + exc.Message);
}
finally
{
DeactivateTool();
}
}
#endregion
}
}