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

/source/library/Interlace/AdornedPasteUp/Editing/PasteUpControl.cs

https://bitbucket.org/VahidN/interlace
C# | 343 lines | 239 code | 75 blank | 29 comment | 37 complexity | 880b56867ad0f1f876e75ac1debbbe59 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.ComponentModel;
  30. using System.Data;
  31. using System.Drawing;
  32. using System.Drawing.Imaging;
  33. using System.IO;
  34. using System.Text;
  35. using System.Windows.Forms;
  36. using Interlace.AdornedPasteUp.Documents;
  37. using Interlace.AdornedPasteUp.Rendering;
  38. using Interlace.PropertyLists;
  39. #endregion
  40. namespace Interlace.AdornedPasteUp.Editing
  41. {
  42. public partial class PasteUpControl : Control
  43. {
  44. Document _document = null;
  45. DocumentFrame _selectedFrame = null;
  46. DragController _dragController = null;
  47. ImageLinkCache _cache = null;
  48. public PasteUpControl()
  49. {
  50. InitializeComponent();
  51. SetStyle(ControlStyles.AllPaintingInWmPaint, true);
  52. SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
  53. Document = new Document();
  54. Text = "Untitled";
  55. this.DocumentChanged += new EventHandler(PasteUpControl_DocumentChanged);
  56. }
  57. void PasteUpControl_DocumentChanged(object sender, EventArgs e)
  58. {
  59. string value = Document.FilePathOrNull;
  60. if (value == null)
  61. {
  62. Text = "Untitled";
  63. }
  64. else
  65. {
  66. Text = Path.GetFileName(value);
  67. }
  68. }
  69. protected override void OnHandleDestroyed(EventArgs e)
  70. {
  71. if (_cache != null)
  72. {
  73. _cache.Dispose();
  74. _cache = null;
  75. }
  76. base.OnHandleDestroyed(e);
  77. }
  78. public event EventHandler DocumentChanged;
  79. public Document Document
  80. {
  81. get { return _document; }
  82. set
  83. {
  84. if (_document == value) return;
  85. // Detach from the current document, if any:
  86. if (_document != null)
  87. {
  88. SelectNone();
  89. _document.Frames.ListChanged -= new ListChangedEventHandler(_frames_ListChanged);
  90. _cache.Dispose();
  91. _cache = null;
  92. }
  93. // Switch to the new document:
  94. _document = value;
  95. _document.Frames.ListChanged += new ListChangedEventHandler(_frames_ListChanged);
  96. _cache = new ImageLinkCache(_document.ImageLinkManager);
  97. Invalidate();
  98. if (DocumentChanged != null) DocumentChanged(this, EventArgs.Empty);
  99. }
  100. }
  101. public DocumentFrame SelectedFrame
  102. {
  103. get { return _selectedFrame; }
  104. }
  105. public event EventHandler SelectedFrameChanged;
  106. public void SelectNone()
  107. {
  108. _selectedFrame = null;
  109. if (SelectedFrameChanged != null) SelectedFrameChanged(this, EventArgs.Empty);
  110. }
  111. void _frames_ListChanged(object sender, ListChangedEventArgs e)
  112. {
  113. Invalidate();
  114. }
  115. public void AddNewFrame(DocumentFrame documentFrame)
  116. {
  117. _document.Frames.Add(documentFrame);
  118. }
  119. protected override void OnPaintBackground(PaintEventArgs pevent)
  120. {
  121. }
  122. protected override void OnPaint(PaintEventArgs e)
  123. {
  124. e.Graphics.FillRectangle(Brushes.White, ClientRectangle);
  125. using (DocumentPaintResources resources = new DocumentPaintResources(_cache))
  126. {
  127. resources.IsFadedFrame = false;
  128. foreach (DocumentFrame frame in _document.Frames)
  129. {
  130. resources.IsSelectedFrame = (frame == _selectedFrame);
  131. resources.SelectedHandleOrNull = _dragController != null ? _dragController.Handle : null;
  132. frame.Paint(e.Graphics, resources);
  133. if (frame == _selectedFrame)
  134. {
  135. frame.PaintHandles(e.Graphics, resources);
  136. resources.IsFadedFrame = true;
  137. }
  138. }
  139. }
  140. }
  141. DocumentFrame FindHitFrameOrNull(Point location)
  142. {
  143. for (int i = 0; i < _document.Frames.Count; i++)
  144. {
  145. DocumentFrame frame = _document.Frames[_document.Frames.Count - i - 1];
  146. if (frame.HitBounds.Contains(location)) return frame;
  147. }
  148. return null;
  149. }
  150. void PasteUpControl_MouseDown(object sender, MouseEventArgs e)
  151. {
  152. // Test hitting a handle first:
  153. if (_selectedFrame != null)
  154. {
  155. IHandleFlyweight handle = _selectedFrame.FindHitHandleOrNull(e.Location);
  156. if (handle != null)
  157. {
  158. _dragController = new DragController(e.Location, _selectedFrame, handle);
  159. _dragController.BeginDrag();
  160. return;
  161. }
  162. }
  163. // Then try hitting a frame:
  164. DocumentFrame hitFrame = FindHitFrameOrNull(e.Location);
  165. if (_selectedFrame != hitFrame)
  166. {
  167. _selectedFrame = hitFrame;
  168. if (SelectedFrameChanged != null) SelectedFrameChanged(this, EventArgs.Empty);
  169. Invalidate();
  170. }
  171. }
  172. private void PasteUpControl_MouseUp(object sender, MouseEventArgs e)
  173. {
  174. if (_dragController == null) return;
  175. _dragController.EndDrag();
  176. _dragController = null;
  177. Invalidate();
  178. }
  179. private void PasteUpControl_MouseMove(object sender, MouseEventArgs e)
  180. {
  181. if (_dragController == null) return;
  182. _dragController.UpdateDrag(e.Location);
  183. Invalidate();
  184. }
  185. public void Save()
  186. {
  187. if (_document.FilePathOrNull == null)
  188. {
  189. SaveAs();
  190. }
  191. else
  192. {
  193. PropertyDictionary dictionary = _document.Serialize(_document.FilePathOrNull);
  194. dictionary.PersistToFile(_document.FilePathOrNull);
  195. }
  196. }
  197. public void SaveAs()
  198. {
  199. using (SaveFileDialog saveFileDialog = new SaveFileDialog())
  200. {
  201. saveFileDialog.Filter = "Adorned Image (*.ati)|*.ati";
  202. if (saveFileDialog.ShowDialog() == DialogResult.OK)
  203. {
  204. PropertyDictionary dictionary = _document.Serialize(saveFileDialog.FileName);
  205. dictionary.PersistToFile(saveFileDialog.FileName);
  206. _document.FilePathOrNull = saveFileDialog.FileName;
  207. }
  208. }
  209. }
  210. public void SaveImage()
  211. {
  212. using (SaveFileDialog saveFileDialog = new SaveFileDialog())
  213. {
  214. saveFileDialog.Filter = "PNG Image (*.png)|*.png";
  215. if (saveFileDialog.ShowDialog() == DialogResult.OK)
  216. {
  217. using (PasteUpRenderer renderer = new PasteUpRenderer(_document))
  218. {
  219. using (Bitmap bitmap = renderer.Render())
  220. {
  221. bitmap.Save(saveFileDialog.FileName);
  222. }
  223. }
  224. }
  225. }
  226. }
  227. public void Open(string fileName)
  228. {
  229. PropertyDictionary dictionary = PropertyDictionary.FromFile(fileName);
  230. Document _newDocument = Document.Deserialize(dictionary, Path.GetDirectoryName(fileName));
  231. _newDocument.FilePathOrNull = fileName;
  232. Document = _newDocument;
  233. }
  234. public void DeleteFrame(DocumentFrame documentFrame)
  235. {
  236. _document.DeleteFrame(documentFrame);
  237. }
  238. public void BringFrameToFront(DocumentFrame documentFrame)
  239. {
  240. _document.BringFrameToFront(documentFrame);
  241. }
  242. public void SendFrameToBack(DocumentFrame documentFrame)
  243. {
  244. _document.SendFrameToBack(documentFrame);
  245. }
  246. public void AddEncodedImage(byte[] image)
  247. {
  248. ImageLink link = new ImageLink(image);
  249. _document.Frames.Add(new RectangularDocumentFrame(new DocumentImage(link)));
  250. }
  251. private void PasteUpControl_DragEnter(object sender, DragEventArgs e)
  252. {
  253. if (e.Data.GetDataPresent(DataFormats.FileDrop, false))
  254. {
  255. e.Effect = DragDropEffects.Link;
  256. }
  257. }
  258. private void PasteUpControl_DragDrop(object sender, DragEventArgs e)
  259. {
  260. string[] fileNames = e.Data.GetData(DataFormats.FileDrop) as string[];
  261. foreach (string fileName in fileNames)
  262. {
  263. ImageLink link = new ImageLink(Path.GetFullPath(fileName));
  264. _document.Frames.Add(new RectangularDocumentFrame(new DocumentImage(link)));
  265. }
  266. }
  267. }
  268. }