DesignerClipboard.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. using System.Windows.Forms;
  6. using System.IO;
  7. using System.Drawing;
  8. using FastReport.Utils;
  9. using FastReport.Data;
  10. using FastReport.Design.ImportPlugins;
  11. namespace FastReport.Design
  12. {
  13. internal class DesignerClipboard
  14. {
  15. private readonly Designer designer;
  16. public bool CanPaste
  17. {
  18. get
  19. {
  20. try
  21. {
  22. return Clipboard.ContainsData("FastReport") || Clipboard.ContainsText() || Clipboard.ContainsImage();
  23. }
  24. catch
  25. {
  26. return false;
  27. }
  28. }
  29. }
  30. public void Cut()
  31. {
  32. Copy();
  33. designer.cmdDelete.Invoke();
  34. }
  35. public void Copy()
  36. {
  37. // Things that we should consider:
  38. // - when copy an object that is selected, we must save its absolute position instead of relative Top, Left.
  39. // This is necessary when copying several objects that have different parents.
  40. // - when copy a parent object and its child, and both are selected, we must exclude a child from the
  41. // selected objects. This is necessary to avoid duplicate copy of the child object - it will be
  42. // copied by its parent.
  43. if (designer.SelectedObjects == null)
  44. return;
  45. using (ClipboardParent parent = new ClipboardParent())
  46. {
  47. Hashtable bounds = new Hashtable();
  48. parent.PageType = designer.SelectedObjects[0].Page.GetType();
  49. foreach (Base c in designer.Objects)
  50. {
  51. if (c.IsSelected)
  52. {
  53. if (c.HasFlag(Flags.CanCopy))
  54. {
  55. if (!parent.Contains(c))
  56. {
  57. if (c is ComponentBase)
  58. {
  59. ComponentBase c1 = c as ComponentBase;
  60. bounds[c1] = c1.Bounds;
  61. c1.SetDesigning(false);
  62. c1.Left = c1.AbsLeft;
  63. c1.Top = c1.AbsTop;
  64. }
  65. parent.Objects.Add(c);
  66. }
  67. }
  68. }
  69. }
  70. using (MemoryStream stream = new MemoryStream())
  71. using (FRWriter writer = new FRWriter())
  72. {
  73. writer.SerializeTo = SerializeTo.Clipboard;
  74. writer.Write(parent);
  75. writer.Save(stream);
  76. Clipboard.SetData("FastReport", stream);
  77. }
  78. // restore components' state
  79. foreach (Base c in parent.Objects)
  80. {
  81. if (c is ComponentBase)
  82. {
  83. ComponentBase c1 = c as ComponentBase;
  84. if (bounds[c1] != null)
  85. c1.Bounds = (RectangleF)bounds[c1];
  86. c1.SetDesigning(true);
  87. }
  88. }
  89. }
  90. }
  91. public void Paste()
  92. {
  93. if (Clipboard.ContainsData("FastReport"))
  94. {
  95. using (ClipboardParent parent = new ClipboardParent())
  96. using (MemoryStream stream = Clipboard.GetData("FastReport") as MemoryStream)
  97. using (FRReader reader = new FRReader(null))
  98. {
  99. reader.DeserializeFrom = SerializeTo.Clipboard;
  100. reader.Load(stream);
  101. reader.Read(parent);
  102. PageBase page = designer.ActiveReportTab.ActivePage;
  103. if (page.GetType() == parent.PageType)
  104. {
  105. designer.ActiveReportTab.ActivePageDesigner.Paste(parent.Objects, InsertFrom.Clipboard);
  106. }
  107. }
  108. }
  109. if (Clipboard.ContainsText())
  110. {
  111. ObjectCollection list = new ObjectCollection();
  112. TextObject obj = new TextObject();
  113. obj.Text = Clipboard.GetText();
  114. list.Add(obj);
  115. designer.ActiveReportTab.ActivePageDesigner.Paste(list, InsertFrom.NewObject);
  116. return;
  117. }
  118. if (Clipboard.ContainsImage())
  119. {
  120. ObjectCollection list = new ObjectCollection();
  121. PictureObject pic = new PictureObject();
  122. Image x = Clipboard.GetImage();
  123. pic.Image = x;
  124. pic.Width = x.Width;
  125. pic.Height = x.Height;
  126. list.Add(pic);
  127. designer.ActiveReportTab.ActivePageDesigner.Paste(list, InsertFrom.NewObject);
  128. return;
  129. }
  130. }
  131. public DesignerClipboard(Designer designer)
  132. {
  133. this.designer = designer;
  134. }
  135. static DesignerClipboard()
  136. {
  137. RegisteredObjects.InternalAdd(typeof(ClipboardParent), "", 0);
  138. }
  139. private class ClipboardParent : Base
  140. {
  141. private ObjectCollection objects;
  142. private Type pageType;
  143. public ObjectCollection Objects
  144. {
  145. get { return objects; }
  146. }
  147. public new ObjectCollection AllObjects
  148. {
  149. get
  150. {
  151. ObjectCollection result = new ObjectCollection();
  152. foreach (Base c in Objects)
  153. EnumObjects(c, result);
  154. return result;
  155. }
  156. }
  157. public Type PageType
  158. {
  159. get { return pageType; }
  160. set { pageType = value; }
  161. }
  162. private void EnumObjects(Base c, ObjectCollection list)
  163. {
  164. if (c != this)
  165. list.Add(c);
  166. foreach (Base obj in c.ChildObjects)
  167. EnumObjects(obj, list);
  168. }
  169. public bool Contains(Base obj)
  170. {
  171. return AllObjects.Contains(obj);
  172. }
  173. public override void Assign(Base source)
  174. {
  175. BaseAssign(source);
  176. }
  177. public override void Serialize(FRWriter writer)
  178. {
  179. writer.ItemName = Name;
  180. writer.WriteStr("PageType", pageType.Name);
  181. foreach (Base c in objects)
  182. {
  183. writer.Write(c);
  184. }
  185. }
  186. public override void Deserialize(FRReader reader)
  187. {
  188. pageType = RegisteredObjects.FindType(reader.ReadStr("PageType"));
  189. while (reader.NextItem())
  190. {
  191. Base c = reader.Read() as Base;
  192. if (c != null)
  193. objects.Add(c);
  194. }
  195. }
  196. public ClipboardParent()
  197. {
  198. objects = new ObjectCollection();
  199. Name = "ClipboardParent";
  200. }
  201. }
  202. }
  203. }