PageRenderTime 44ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/source/library/Interlace/AdornedPasteUp/Documents/RectangularDocumentFrame.cs

https://bitbucket.org/VahidN/interlace
C# | 279 lines | 200 code | 54 blank | 25 comment | 15 complexity | fbe59a627b5463348b4adb86a3ec13a2 MD5 | raw file
  1. #region Using Directives and Copyright Notice
  2. // Copyright (c) 2007-2010, Computer Consultancy Pty Ltd
  3. // All rights reserved.
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are met:
  7. // * Redistributions of source code must retain the above copyright
  8. // notice, this list of conditions and the following disclaimer.
  9. // * Redistributions in binary form must reproduce the above copyright
  10. // notice, this list of conditions and the following disclaimer in the
  11. // documentation and/or other materials provided with the distribution.
  12. // * Neither the name of the Computer Consultancy Pty Ltd nor the
  13. // names of its contributors may be used to endorse or promote products
  14. // derived from this software without specific prior written permission.
  15. //
  16. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. // ARE DISCLAIMED. IN NO EVENT SHALL COMPUTER CONSULTANCY PTY LTD BE LIABLE
  20. // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22. // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23. // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  24. // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  25. // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  26. // DAMAGE.
  27. using System;
  28. using System.Collections.Generic;
  29. using System.Drawing;
  30. using System.Drawing.Drawing2D;
  31. using System.Text;
  32. using Interlace.AdornedPasteUp.Rendering;
  33. using Interlace.PropertyLists;
  34. #endregion
  35. namespace Interlace.AdornedPasteUp.Documents
  36. {
  37. enum DocumentFrameDragFocus
  38. {
  39. None,
  40. Location
  41. }
  42. public class RectangularDocumentFrame : DocumentFrame
  43. {
  44. static List<IHandleFlyweight> _handles;
  45. IDocumentObject _framedObject;
  46. public Rectangle _clipBounds;
  47. bool _enableTopCutMarks = true;
  48. bool _enableBottomCutMarks = true;
  49. bool _enableLeftCutMarks = true;
  50. bool _enableRightCutMarks = true;
  51. class Memento : DocumentFrameMemento
  52. {
  53. internal Rectangle _clipBounds;
  54. public Memento(RectangularDocumentFrame frame)
  55. : base(frame)
  56. {
  57. _clipBounds = frame._clipBounds;
  58. }
  59. internal override void Restore(DocumentFrame frame)
  60. {
  61. base.Restore(frame);
  62. RectangularDocumentFrame rectangularFrame = (RectangularDocumentFrame)frame;
  63. rectangularFrame._clipBounds = _clipBounds;
  64. }
  65. }
  66. static RectangularDocumentFrame()
  67. {
  68. _handles = new List<IHandleFlyweight>();
  69. _handles.Add(new MoveHandleFlyweight());
  70. _handles.AddRange(ClippingHandleFlyweight.CreateStandardClippingHandles());
  71. }
  72. public RectangularDocumentFrame(IDocumentObject framedObject)
  73. {
  74. _offsetInDocument = new Point(50, 50);
  75. _clipBounds = framedObject.DefaultBounds;
  76. _framedObject = framedObject;
  77. }
  78. public RectangularDocumentFrame(DocumentDeserializationContext context, PropertyDictionary frameDictionary)
  79. : base(context, frameDictionary)
  80. {
  81. if (!frameDictionary.HasDictionaryFor("clipBounds"))
  82. {
  83. throw new DocumentReadingException("A label frame is missing the required \"clipBounds\" field.");
  84. }
  85. if (!frameDictionary.HasDictionaryFor("framedObject"))
  86. {
  87. throw new DocumentReadingException("A label frame is missing the required \"framedObject\" field.");
  88. }
  89. _clipBounds = PropertyBuilders.ToRectangle(frameDictionary.DictionaryFor("clipBounds"));
  90. PropertyDictionary framedObjectDictionary = frameDictionary.DictionaryFor("framedObject");
  91. string framedObjectType = framedObjectDictionary.StringFor("type", "missing");
  92. switch (framedObjectType)
  93. {
  94. case "image":
  95. _framedObject = new DocumentImage(context, framedObjectDictionary);
  96. break;
  97. case "fill":
  98. _framedObject = new DocumentFill(context, framedObjectDictionary);
  99. break;
  100. default:
  101. throw new DocumentReadingException(string.Format("The document object type \"{0}\" is not supported in this version.",
  102. framedObjectType));
  103. }
  104. }
  105. internal override PropertyDictionary Serialize(DocumentSerializationContext context)
  106. {
  107. PropertyDictionary dictionary = base.Serialize(context);
  108. dictionary.SetValueFor("type", "rectangular");
  109. dictionary.SetValueFor("clipBounds", PropertyBuilders.FromRectangle(_clipBounds));
  110. dictionary.SetValueFor("framedObject", _framedObject.Serialize(context));
  111. return dictionary;
  112. }
  113. public override Document Document
  114. {
  115. get
  116. {
  117. return base.Document;
  118. }
  119. internal set
  120. {
  121. base.Document = value;
  122. _framedObject.Document = value;
  123. }
  124. }
  125. public IDocumentObject FramedObject
  126. {
  127. get { return _framedObject; }
  128. }
  129. public Rectangle ClipBounds
  130. {
  131. get { return _clipBounds; }
  132. set { _clipBounds = value; }
  133. }
  134. public Rectangle ObjectBounds
  135. {
  136. get { return _framedObject.ObjectBounds; }
  137. }
  138. public override Rectangle HitBounds
  139. {
  140. get
  141. {
  142. Rectangle hitBounds = _clipBounds;
  143. hitBounds.Offset(_offsetInDocument);
  144. return hitBounds;
  145. }
  146. }
  147. public override Rectangle PaintBounds
  148. {
  149. get
  150. {
  151. Rectangle paintBounds = _clipBounds;
  152. paintBounds.Offset(_offsetInDocument);
  153. return paintBounds;
  154. }
  155. }
  156. public override void Paint(Graphics g, DocumentPaintResources resources)
  157. {
  158. Point paintTopLeft = _clipBounds.Location + new Size(_offsetInDocument);
  159. _framedObject.Paint(_clipBounds, paintTopLeft, g, resources);
  160. Rectangle border = _clipBounds;
  161. border.Offset(_offsetInDocument);
  162. Rectangle objectBounds = _framedObject.ObjectBounds;
  163. if (_enableTopCutMarks && objectBounds.Top != _clipBounds.Top) PaintBorder(g, resources, border.Location, border.Location + new Size(border.Width - 1, 0));
  164. if (_enableBottomCutMarks && objectBounds.Bottom != _clipBounds.Bottom) PaintBorder(g, resources, border.Location + new Size(0, border.Height - 1), border.Location + new Size(border.Width - 1, border.Height - 1));
  165. if (_enableLeftCutMarks && objectBounds.Left != _clipBounds.Left) PaintBorder(g, resources, border.Location, border.Location + new Size(0, border.Height - 1));
  166. if (_enableRightCutMarks && objectBounds.Right != _clipBounds.Right) PaintBorder(g, resources, border.Location + new Size(border.Width - 1, 0), border.Location + new Size(border.Width - 1, border.Height - 1));
  167. }
  168. public void PaintBorder(Graphics g, DocumentPaintResources resources, Point first, Point second)
  169. {
  170. using (Pen pen = new Pen(SystemColors.ControlDark, 1.0f))
  171. {
  172. pen.DashStyle = DashStyle.Dash;
  173. g.DrawLine(pen, first, second);
  174. }
  175. }
  176. public override DocumentFrameMemento CreateMemento()
  177. {
  178. return new Memento(this);
  179. }
  180. public override void RestoreFromMemento(DocumentFrameMemento memento)
  181. {
  182. memento.Restore(this);
  183. }
  184. protected override IEnumerable<IHandleFlyweight> Handles
  185. {
  186. get { return _handles; }
  187. }
  188. public bool EnableTopCutMarks
  189. {
  190. get { return _enableTopCutMarks; }
  191. set
  192. {
  193. _enableTopCutMarks = value;
  194. FirePropertyChanged("EnableTopCutMarks");
  195. }
  196. }
  197. public bool EnableBottomCutMarks
  198. {
  199. get { return _enableBottomCutMarks; }
  200. set
  201. {
  202. _enableBottomCutMarks = value;
  203. FirePropertyChanged("EnableBottomCutMarks");
  204. }
  205. }
  206. public bool EnableLeftCutMarks
  207. {
  208. get { return _enableLeftCutMarks; }
  209. set
  210. {
  211. _enableLeftCutMarks = value;
  212. FirePropertyChanged("EnableLeftCutMarks");
  213. }
  214. }
  215. public bool EnableRightCutMarks
  216. {
  217. get { return _enableRightCutMarks; }
  218. set
  219. {
  220. _enableRightCutMarks = value;
  221. FirePropertyChanged("EnableRightCutMarks");
  222. }
  223. }
  224. }
  225. }