1 | using System;
|
---|
2 | using System.Diagnostics;
|
---|
3 | using System.Collections;
|
---|
4 | using System.Drawing;
|
---|
5 | using System.Windows.Forms;
|
---|
6 | using System.IO;
|
---|
7 | using System.Runtime.Serialization;
|
---|
8 | using System.Runtime.Serialization.Formatters.Binary;
|
---|
9 | namespace Netron.Diagramming.Core
|
---|
10 | {
|
---|
11 | // ----------------------------------------------------------------------
|
---|
12 | /// <summary>
|
---|
13 | /// A tool that copies the selected entities to the clipboard.
|
---|
14 | /// </summary>
|
---|
15 | // ----------------------------------------------------------------------
|
---|
16 | public class CopyTool : AbstractTool
|
---|
17 | {
|
---|
18 | #region Fields
|
---|
19 | #endregion
|
---|
20 |
|
---|
21 | #region Constructor
|
---|
22 |
|
---|
23 | // ------------------------------------------------------------------
|
---|
24 | /// <summary>
|
---|
25 | /// Initializes a new instance of the <see cref="T:CopyTool"/> class.
|
---|
26 | /// </summary>
|
---|
27 | /// <param name="name">string: The name of the tool.</param>
|
---|
28 | // ------------------------------------------------------------------
|
---|
29 | public CopyTool(string name)
|
---|
30 | : base(name)
|
---|
31 | {
|
---|
32 | }
|
---|
33 | #endregion
|
---|
34 |
|
---|
35 | #region Methods
|
---|
36 |
|
---|
37 | // ------------------------------------------------------------------
|
---|
38 | /// <summary>
|
---|
39 | /// Called when the tool is activated.
|
---|
40 | /// </summary>
|
---|
41 | // ------------------------------------------------------------------
|
---|
42 | protected override void OnActivateTool()
|
---|
43 | {
|
---|
44 | if(Selection.SelectedItems.Count == 0)
|
---|
45 | return;
|
---|
46 |
|
---|
47 | try
|
---|
48 | {
|
---|
49 | //Clear the Anchors otherwise subsequent Copy operations will
|
---|
50 | // raise an exception due to the fact that the Anchors class
|
---|
51 | // is a static helper class.
|
---|
52 | Anchors.Clear();
|
---|
53 |
|
---|
54 | // First add the image to the Windows Clipboard so it can be
|
---|
55 | // pasted into other applications, like PowerPoint.
|
---|
56 | Bitmap image = Selection.ToBitmap();
|
---|
57 | Clipboard.SetDataObject(image, true);
|
---|
58 |
|
---|
59 | // This will create a volatile collection of entities, but they
|
---|
60 | // need to be unwrapped! I never managed to get things
|
---|
61 | // working by putting the serialized collection directly onto
|
---|
62 | // the Clipboad. Thanks to Leppie's suggestion it works by
|
---|
63 | // putting the Stream onto the Clipboard, which is weird but
|
---|
64 | // it works.
|
---|
65 | //MemoryStream copy = Selection.SelectedItems.ToStream();
|
---|
66 | //DataFormats.Format format =
|
---|
67 | // DataFormats.GetFormat(typeof(CopyTool).FullName);
|
---|
68 |
|
---|
69 | //IDataObject dataObject = new DataObject();
|
---|
70 | //dataObject.SetData(format.Name, false, copy);
|
---|
71 | //Clipboard.SetDataObject(dataObject, false);
|
---|
72 |
|
---|
73 | // Rather than placing the stream of entities on the Windows
|
---|
74 | // Clipboard, we're using our custom clipboard to keep the
|
---|
75 | // Windows clipboard for moving data across apps.
|
---|
76 | NetronClipboard.Clear();
|
---|
77 | NetronClipboard.Add(Selection.SelectedItems.Copy());
|
---|
78 |
|
---|
79 | }
|
---|
80 | catch (Exception exc)
|
---|
81 | {
|
---|
82 | //throw new InconsistencyException(
|
---|
83 | // "The Copy operation failed.", exc);
|
---|
84 | MessageBox.Show("Unable to copy the selection.\n\n" +
|
---|
85 | "Error Message: " + exc.Message);
|
---|
86 | }
|
---|
87 | finally
|
---|
88 | {
|
---|
89 | DeactivateTool();
|
---|
90 | }
|
---|
91 | }
|
---|
92 |
|
---|
93 | #endregion
|
---|
94 | }
|
---|
95 |
|
---|
96 | }
|
---|